1 /*
2  *  linux/fs/hpfs/inode.c
3  *
4  *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5  *
6  *  inode VFS functions
7  */
8 
9 #include <linux/sched.h>
10 #include <linux/smp_lock.h>
11 #include "hpfs_fn.h"
12 
13 static struct file_operations hpfs_file_ops =
14 {
15 	llseek:		generic_file_llseek,
16 	read:		generic_file_read,
17 	write:		hpfs_file_write,
18 	mmap:		generic_file_mmap,
19 	open:		hpfs_open,
20 	release:	hpfs_file_release,
21 	fsync:		hpfs_file_fsync,
22 };
23 
24 static struct inode_operations hpfs_file_iops =
25 {
26 	truncate:	hpfs_truncate,
27 	setattr:	hpfs_notify_change,
28 };
29 
30 static struct file_operations hpfs_dir_ops =
31 {
32 	llseek:		hpfs_dir_lseek,
33 	read:		generic_read_dir,
34 	readdir:	hpfs_readdir,
35 	open:		hpfs_open,
36 	release:	hpfs_dir_release,
37 	fsync:		hpfs_file_fsync,
38 };
39 
40 static struct inode_operations hpfs_dir_iops =
41 {
42 	create:		hpfs_create,
43 	lookup:		hpfs_lookup,
44 	unlink:		hpfs_unlink,
45 	symlink:	hpfs_symlink,
46 	mkdir:		hpfs_mkdir,
47 	rmdir:		hpfs_rmdir,
48 	mknod:		hpfs_mknod,
49 	rename:		hpfs_rename,
50 	setattr:	hpfs_notify_change,
51 };
52 
53 struct address_space_operations hpfs_symlink_aops = {
54 	readpage:	hpfs_symlink_readpage
55 };
56 
hpfs_read_inode(struct inode * i)57 void hpfs_read_inode(struct inode *i)
58 {
59 	struct buffer_head *bh;
60 	struct fnode *fnode;
61 	struct super_block *sb = i->i_sb;
62 	unsigned char *ea;
63 	int ea_size;
64 	init_MUTEX(&i->i_hpfs_sem);
65 	i->i_uid = sb->s_hpfs_uid;
66 	i->i_gid = sb->s_hpfs_gid;
67 	i->i_mode = sb->s_hpfs_mode;
68 	i->i_hpfs_conv = sb->s_hpfs_conv;
69 	i->i_blksize = 512;
70 	i->i_size = -1;
71 	i->i_blocks = -1;
72 
73 	i->i_hpfs_dno = 0;
74 	i->i_hpfs_n_secs = 0;
75 	i->i_hpfs_file_sec = 0;
76 	i->i_hpfs_disk_sec = 0;
77 	i->i_hpfs_dpos = 0;
78 	i->i_hpfs_dsubdno = 0;
79 	i->i_hpfs_ea_mode = 0;
80 	i->i_hpfs_ea_uid = 0;
81 	i->i_hpfs_ea_gid = 0;
82 	i->i_hpfs_ea_size = 0;
83 	i->i_version = ++event;
84 
85 	i->i_hpfs_rddir_off = NULL;
86 	i->i_hpfs_dirty = 0;
87 
88 	i->i_atime = 0;
89 	i->i_mtime = 0;
90 	i->i_ctime = 0;
91 
92 	if (!i->i_sb->s_hpfs_rd_inode)
93 		hpfs_error(i->i_sb, "read_inode: s_hpfs_rd_inode == 0");
94 	if (i->i_sb->s_hpfs_rd_inode == 2) {
95 		i->i_mode |= S_IFREG;
96 		i->i_mode &= ~0111;
97 		i->i_op = &hpfs_file_iops;
98 		i->i_fop = &hpfs_file_ops;
99 		i->i_nlink = 1;
100 		return;
101 	}
102 	if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
103 		/*i->i_mode |= S_IFREG;
104 		i->i_mode &= ~0111;
105 		i->i_op = &hpfs_file_iops;
106 		i->i_fop = &hpfs_file_ops;
107 		i->i_nlink = 0;*/
108 		make_bad_inode(i);
109 		return;
110 	}
111 	if (i->i_sb->s_hpfs_eas) {
112 		if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
113 			if (ea_size == 2) {
114 				i->i_uid = ea[0] + (ea[1] << 8);
115 				i->i_hpfs_ea_uid = 1;
116 			}
117 			kfree(ea);
118 		}
119 		if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
120 			if (ea_size == 2) {
121 				i->i_gid = ea[0] + (ea[1] << 8);
122 				i->i_hpfs_ea_gid = 1;
123 			}
124 			kfree(ea);
125 		}
126 		if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
127 			kfree(ea);
128 			i->i_mode = S_IFLNK | 0777;
129 			i->i_op = &page_symlink_inode_operations;
130 			i->i_data.a_ops = &hpfs_symlink_aops;
131 			i->i_nlink = 1;
132 			i->i_size = ea_size;
133 			i->i_blocks = 1;
134 			brelse(bh);
135 			return;
136 		}
137 		if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
138 			int rdev = 0;
139 			umode_t mode = sb->s_hpfs_mode;
140 			if (ea_size == 2) {
141 				mode = ea[0] + (ea[1] << 8);
142 				i->i_hpfs_ea_mode = 1;
143 			}
144 			kfree(ea);
145 			i->i_mode = mode;
146 			if (S_ISBLK(mode) || S_ISCHR(mode)) {
147 				if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
148 					if (ea_size == 4)
149 						rdev = ea[0] + (ea[1] << 8) + (ea[2] << 16) + (ea[3] << 24);
150 					kfree(ea);
151 				}
152 			}
153 			if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
154 				brelse(bh);
155 				i->i_nlink = 1;
156 				i->i_size = 0;
157 				i->i_blocks = 1;
158 				init_special_inode(i, mode, rdev);
159 				return;
160 			}
161 		}
162 	}
163 	if (fnode->dirflag) {
164 		unsigned n_dnodes, n_subdirs;
165 		i->i_mode |= S_IFDIR;
166 		i->i_op = &hpfs_dir_iops;
167 		i->i_fop = &hpfs_dir_ops;
168 		i->i_hpfs_parent_dir = fnode->up;
169 		i->i_hpfs_dno = fnode->u.external[0].disk_secno;
170 		if (sb->s_hpfs_chk >= 2) {
171 			struct buffer_head *bh0;
172 			if (hpfs_map_fnode(sb, i->i_hpfs_parent_dir, &bh0)) brelse(bh0);
173 		}
174 		n_dnodes = 0; n_subdirs = 0;
175 		hpfs_count_dnodes(i->i_sb, i->i_hpfs_dno, &n_dnodes, &n_subdirs, NULL);
176 		i->i_blocks = 4 * n_dnodes;
177 		i->i_size = 2048 * n_dnodes;
178 		i->i_nlink = 2 + n_subdirs;
179 	} else {
180 		i->i_mode |= S_IFREG;
181 		if (!i->i_hpfs_ea_mode) i->i_mode &= ~0111;
182 		i->i_op = &hpfs_file_iops;
183 		i->i_fop = &hpfs_file_ops;
184 		i->i_nlink = 1;
185 		i->i_size = fnode->file_size;
186 		i->i_blocks = ((i->i_size + 511) >> 9) + 1;
187 		i->i_data.a_ops = &hpfs_aops;
188 		i->u.hpfs_i.mmu_private = i->i_size;
189 	}
190 	brelse(bh);
191 }
192 
hpfs_write_inode_ea(struct inode * i,struct fnode * fnode)193 void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
194 {
195 	/*if (fnode->acl_size_l || fnode->acl_size_s) {
196 		   Some unknown structures like ACL may be in fnode,
197 		   we'd better not overwrite them
198 		hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 stuctures", i->i_ino);
199 	} else*/ if (i->i_sb->s_hpfs_eas >= 2) {
200 		unsigned char ea[4];
201 		if ((i->i_uid != i->i_sb->s_hpfs_uid) || i->i_hpfs_ea_uid) {
202 			ea[0] = i->i_uid & 0xff;
203 			ea[1] = i->i_uid >> 8;
204 			hpfs_set_ea(i, fnode, "UID", ea, 2);
205 			i->i_hpfs_ea_uid = 1;
206 		}
207 		if ((i->i_gid != i->i_sb->s_hpfs_gid) || i->i_hpfs_ea_gid) {
208 			ea[0] = i->i_gid & 0xff;
209 			ea[1] = i->i_gid >> 8;
210 			hpfs_set_ea(i, fnode, "GID", ea, 2);
211 			i->i_hpfs_ea_gid = 1;
212 		}
213 		if (!S_ISLNK(i->i_mode))
214 			if ((i->i_mode != ((i->i_sb->s_hpfs_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
215 			  | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
216 			  && i->i_mode != ((i->i_sb->s_hpfs_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
217 			  | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || i->i_hpfs_ea_mode) {
218 				ea[0] = i->i_mode & 0xff;
219 				ea[1] = i->i_mode >> 8;
220 				hpfs_set_ea(i, fnode, "MODE", ea, 2);
221 				i->i_hpfs_ea_mode = 1;
222 			}
223 		if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
224 			int d = kdev_t_to_nr(i->i_rdev);
225 			ea[0] = d & 0xff;
226 			ea[1] = (d >> 8) & 0xff;
227 			ea[2] = (d >> 16) & 0xff;
228 			ea[3] = d >> 24;
229 			hpfs_set_ea(i, fnode, "DEV", ea, 4);
230 		}
231 	}
232 }
233 
hpfs_write_inode(struct inode * i)234 void hpfs_write_inode(struct inode *i)
235 {
236 	struct inode *parent;
237 	if (!i->i_nlink) return;
238 	if (i->i_ino == i->i_sb->s_hpfs_root) return;
239 	if (i->i_hpfs_rddir_off && !atomic_read(&i->i_count)) {
240 		if (*i->i_hpfs_rddir_off) printk("HPFS: write_inode: some position still there\n");
241 		kfree(i->i_hpfs_rddir_off);
242 		i->i_hpfs_rddir_off = NULL;
243 	}
244 	i->i_hpfs_dirty = 0;
245 	hpfs_lock_iget(i->i_sb, 1);
246 	parent = iget(i->i_sb, i->i_hpfs_parent_dir);
247 	hpfs_unlock_iget(i->i_sb);
248 	hpfs_lock_inode(parent);
249 	hpfs_write_inode_nolock(i);
250 	hpfs_unlock_inode(parent);
251 	iput(parent);
252 }
253 
hpfs_write_inode_nolock(struct inode * i)254 void hpfs_write_inode_nolock(struct inode *i)
255 {
256 	struct buffer_head *bh;
257 	struct fnode *fnode;
258 	struct quad_buffer_head qbh;
259 	struct hpfs_dirent *de;
260 	if (i->i_ino == i->i_sb->s_hpfs_root) return;
261 	if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
262 	if (i->i_ino != i->i_sb->s_hpfs_root && i->i_nlink) {
263 		if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
264 			brelse(bh);
265 			return;
266 		}
267 	} else de = NULL;
268 	if (S_ISREG(i->i_mode)) {
269 		fnode->file_size = i->i_size;
270 		if (de) de->file_size = i->i_size;
271 	} else if (S_ISDIR(i->i_mode)) {
272 		fnode->file_size = 0;
273 		if (de) de->file_size = 0;
274 	}
275 	hpfs_write_inode_ea(i, fnode);
276 	if (de) {
277 		de->write_date = gmt_to_local(i->i_sb, i->i_mtime);
278 		de->read_date = gmt_to_local(i->i_sb, i->i_atime);
279 		de->creation_date = gmt_to_local(i->i_sb, i->i_ctime);
280 		de->read_only = !(i->i_mode & 0222);
281 		de->ea_size = i->i_hpfs_ea_size;
282 		hpfs_mark_4buffers_dirty(&qbh);
283 		hpfs_brelse4(&qbh);
284 	}
285 	if (S_ISDIR(i->i_mode)) {
286 		if ((de = map_dirent(i, i->i_hpfs_dno, "\001\001", 2, NULL, &qbh))) {
287 			de->write_date = gmt_to_local(i->i_sb, i->i_mtime);
288 			de->read_date = gmt_to_local(i->i_sb, i->i_atime);
289 			de->creation_date = gmt_to_local(i->i_sb, i->i_ctime);
290 			de->read_only = !(i->i_mode & 0222);
291 			de->ea_size = /*i->i_hpfs_ea_size*/0;
292 			de->file_size = 0;
293 			hpfs_mark_4buffers_dirty(&qbh);
294 			hpfs_brelse4(&qbh);
295 		} else hpfs_error(i->i_sb, "directory %08x doesn't have '.' entry", i->i_ino);
296 	}
297 	mark_buffer_dirty(bh);
298 	brelse(bh);
299 }
300 
hpfs_notify_change(struct dentry * dentry,struct iattr * attr)301 int hpfs_notify_change(struct dentry *dentry, struct iattr *attr)
302 {
303 	struct inode *inode = dentry->d_inode;
304 	int error;
305 	if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
306 		return -EINVAL;
307 	if (inode->i_sb->s_hpfs_root == inode->i_ino) return -EINVAL;
308 	if ((error = inode_change_ok(inode, attr))) return error;
309 	error = inode_setattr(inode, attr);
310 	if (error) return error;
311 	hpfs_write_inode(inode);
312 	return 0;
313 }
314 
hpfs_write_if_changed(struct inode * inode)315 void hpfs_write_if_changed(struct inode *inode)
316 {
317 	if (inode->i_hpfs_dirty) {
318 		hpfs_write_inode(inode);
319 	}
320 }
321 
hpfs_delete_inode(struct inode * inode)322 void hpfs_delete_inode(struct inode *inode)
323 {
324 	lock_kernel();
325 	hpfs_remove_fnode(inode->i_sb, inode->i_ino);
326 	unlock_kernel();
327 	clear_inode(inode);
328 }
329