Blame view

fs/hpfs/inode.c 8.82 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
  /*
   *  linux/fs/hpfs/inode.c
   *
   *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
   *
   *  inode VFS functions
   */
5a0e3ad6a   Tejun Heo   include cleanup: ...
9
  #include <linux/slab.h>
0e1a43c71   Eric W. Biederman   userns: Convert h...
10
  #include <linux/user_namespace.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
17
18
19
20
  #include "hpfs_fn.h"
  
  void hpfs_init_inode(struct inode *i)
  {
  	struct super_block *sb = i->i_sb;
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  
  	i->i_uid = hpfs_sb(sb)->sb_uid;
  	i->i_gid = hpfs_sb(sb)->sb_gid;
  	i->i_mode = hpfs_sb(sb)->sb_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  	i->i_size = -1;
  	i->i_blocks = -1;
  	
  	hpfs_inode->i_dno = 0;
  	hpfs_inode->i_n_secs = 0;
  	hpfs_inode->i_file_sec = 0;
  	hpfs_inode->i_disk_sec = 0;
  	hpfs_inode->i_dpos = 0;
  	hpfs_inode->i_dsubdno = 0;
  	hpfs_inode->i_ea_mode = 0;
  	hpfs_inode->i_ea_uid = 0;
  	hpfs_inode->i_ea_gid = 0;
  	hpfs_inode->i_ea_size = 0;
  
  	hpfs_inode->i_rddir_off = NULL;
  	hpfs_inode->i_dirty = 0;
  
  	i->i_ctime.tv_sec = i->i_ctime.tv_nsec = 0;
  	i->i_mtime.tv_sec = i->i_mtime.tv_nsec = 0;
  	i->i_atime.tv_sec = i->i_atime.tv_nsec = 0;
  }
  
  void hpfs_read_inode(struct inode *i)
  {
  	struct buffer_head *bh;
  	struct fnode *fnode;
  	struct super_block *sb = i->i_sb;
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
7e7742ee0   Al Viro   sanitize signedne...
49
  	void *ea;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
54
55
56
  	int ea_size;
  
  	if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
  		/*i->i_mode |= S_IFREG;
  		i->i_mode &= ~0111;
  		i->i_op = &hpfs_file_iops;
  		i->i_fop = &hpfs_file_ops;
6d6b77f16   Miklos Szeredi   filesystems: add ...
57
  		clear_nlink(i);*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
60
61
62
63
  		make_bad_inode(i);
  		return;
  	}
  	if (hpfs_sb(i->i_sb)->sb_eas) {
  		if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
  			if (ea_size == 2) {
0e1a43c71   Eric W. Biederman   userns: Convert h...
64
  				i_uid_write(i, le16_to_cpu(*(__le16*)ea));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
  				hpfs_inode->i_ea_uid = 1;
  			}
  			kfree(ea);
  		}
  		if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
  			if (ea_size == 2) {
0e1a43c71   Eric W. Biederman   userns: Convert h...
71
  				i_gid_write(i, le16_to_cpu(*(__le16*)ea));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
76
77
78
79
  				hpfs_inode->i_ea_gid = 1;
  			}
  			kfree(ea);
  		}
  		if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
  			kfree(ea);
  			i->i_mode = S_IFLNK | 0777;
  			i->i_op = &page_symlink_inode_operations;
21fc61c73   Al Viro   don't put symlink...
80
  			inode_nohighmem(i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
  			i->i_data.a_ops = &hpfs_symlink_aops;
bfe868486   Miklos Szeredi   filesystems: add ...
82
  			set_nlink(i, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
85
86
87
88
89
90
91
  			i->i_size = ea_size;
  			i->i_blocks = 1;
  			brelse(bh);
  			return;
  		}
  		if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
  			int rdev = 0;
  			umode_t mode = hpfs_sb(sb)->sb_mode;
  			if (ea_size == 2) {
2ef103128   Al Viro   [PATCH] hpfs endi...
92
  				mode = le16_to_cpu(*(__le16*)ea);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
95
96
97
98
99
  				hpfs_inode->i_ea_mode = 1;
  			}
  			kfree(ea);
  			i->i_mode = mode;
  			if (S_ISBLK(mode) || S_ISCHR(mode)) {
  				if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
  					if (ea_size == 4)
2ef103128   Al Viro   [PATCH] hpfs endi...
100
  						rdev = le32_to_cpu(*(__le32*)ea);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
  					kfree(ea);
  				}
  			}
  			if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
  				brelse(bh);
bfe868486   Miklos Szeredi   filesystems: add ...
106
  				set_nlink(i, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
109
110
111
112
113
114
  				i->i_size = 0;
  				i->i_blocks = 1;
  				init_special_inode(i, mode,
  					new_decode_dev(rdev));
  				return;
  			}
  		}
  	}
c4c995430   Al Viro   hpfs: get rid of ...
115
  	if (fnode_is_dir(fnode)) {
7e7742ee0   Al Viro   sanitize signedne...
116
  		int n_dnodes, n_subdirs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
  		i->i_mode |= S_IFDIR;
  		i->i_op = &hpfs_dir_iops;
  		i->i_fop = &hpfs_dir_ops;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
120
121
  		hpfs_inode->i_parent_dir = le32_to_cpu(fnode->up);
  		hpfs_inode->i_dno = le32_to_cpu(fnode->u.external[0].disk_secno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
128
129
  		if (hpfs_sb(sb)->sb_chk >= 2) {
  			struct buffer_head *bh0;
  			if (hpfs_map_fnode(sb, hpfs_inode->i_parent_dir, &bh0)) brelse(bh0);
  		}
  		n_dnodes = 0; n_subdirs = 0;
  		hpfs_count_dnodes(i->i_sb, hpfs_inode->i_dno, &n_dnodes, &n_subdirs, NULL);
  		i->i_blocks = 4 * n_dnodes;
  		i->i_size = 2048 * n_dnodes;
bfe868486   Miklos Szeredi   filesystems: add ...
130
  		set_nlink(i, 2 + n_subdirs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
133
134
135
  	} else {
  		i->i_mode |= S_IFREG;
  		if (!hpfs_inode->i_ea_mode) i->i_mode &= ~0111;
  		i->i_op = &hpfs_file_iops;
  		i->i_fop = &hpfs_file_ops;
bfe868486   Miklos Szeredi   filesystems: add ...
136
  		set_nlink(i, 1);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
137
  		i->i_size = le32_to_cpu(fnode->file_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
141
142
143
144
145
146
147
  		i->i_blocks = ((i->i_size + 511) >> 9) + 1;
  		i->i_data.a_ops = &hpfs_aops;
  		hpfs_i(i)->mmu_private = i->i_size;
  	}
  	brelse(bh);
  }
  
  static void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
148
  	/*if (le32_to_cpu(fnode->acl_size_l) || le16_to_cpu(fnode->acl_size_s)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
  		   Some unknown structures like ACL may be in fnode,
  		   we'd better not overwrite them
8a168ca70   Masanari Iida   treewide: Fix typ...
151
  		hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 structures", i->i_ino);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
  	} else*/ if (hpfs_sb(i->i_sb)->sb_eas >= 2) {
2ef103128   Al Viro   [PATCH] hpfs endi...
153
  		__le32 ea;
0e1a43c71   Eric W. Biederman   userns: Convert h...
154
155
  		if (!uid_eq(i->i_uid, hpfs_sb(i->i_sb)->sb_uid) || hpfs_inode->i_ea_uid) {
  			ea = cpu_to_le32(i_uid_read(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
  			hpfs_set_ea(i, fnode, "UID", (char*)&ea, 2);
  			hpfs_inode->i_ea_uid = 1;
  		}
0e1a43c71   Eric W. Biederman   userns: Convert h...
159
160
  		if (!gid_eq(i->i_gid, hpfs_sb(i->i_sb)->sb_gid) || hpfs_inode->i_ea_gid) {
  			ea = cpu_to_le32(i_gid_read(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
162
163
164
165
166
167
168
169
  			hpfs_set_ea(i, fnode, "GID", (char *)&ea, 2);
  			hpfs_inode->i_ea_gid = 1;
  		}
  		if (!S_ISLNK(i->i_mode))
  			if ((i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
  			  | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
  			  && i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
  			  | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || hpfs_inode->i_ea_mode) {
  				ea = cpu_to_le32(i->i_mode);
2ef103128   Al Viro   [PATCH] hpfs endi...
170
  				/* sick, but legal */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  				hpfs_set_ea(i, fnode, "MODE", (char *)&ea, 2);
  				hpfs_inode->i_ea_mode = 1;
  			}
  		if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
  			ea = cpu_to_le32(new_encode_dev(i->i_rdev));
  			hpfs_set_ea(i, fnode, "DEV", (char *)&ea, 4);
  		}
  	}
  }
  
  void hpfs_write_inode(struct inode *i)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  	struct inode *parent;
  	if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
  	if (hpfs_inode->i_rddir_off && !atomic_read(&i->i_count)) {
b7cb1ce22   Fabian Frederick   fs/hpfs: convert ...
187
  		if (*hpfs_inode->i_rddir_off)
a19189e55   Fabian Frederick   fs/hpfs: increase...
188
189
  			pr_err("write_inode: some position still there
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
192
  		kfree(hpfs_inode->i_rddir_off);
  		hpfs_inode->i_rddir_off = NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
  	if (!i->i_nlink) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
196
197
198
199
200
201
202
203
  		return;
  	}
  	parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
  	if (parent) {
  		hpfs_inode->i_dirty = 0;
  		if (parent->i_state & I_NEW) {
  			hpfs_init_inode(parent);
  			hpfs_read_inode(parent);
  			unlock_new_inode(parent);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
  		hpfs_write_inode_nolock(i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  		iput(parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  }
  
  void hpfs_write_inode_nolock(struct inode *i)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  	struct buffer_head *bh;
  	struct fnode *fnode;
  	struct quad_buffer_head qbh;
  	struct hpfs_dirent *de;
  	if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
  	if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
  	if (i->i_ino != hpfs_sb(i->i_sb)->sb_root && i->i_nlink) {
  		if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
  			brelse(bh);
  			return;
  		}
  	} else de = NULL;
  	if (S_ISREG(i->i_mode)) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
225
226
  		fnode->file_size = cpu_to_le32(i->i_size);
  		if (de) de->file_size = cpu_to_le32(i->i_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  	} else if (S_ISDIR(i->i_mode)) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
228
229
  		fnode->file_size = cpu_to_le32(0);
  		if (de) de->file_size = cpu_to_le32(0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
  	}
  	hpfs_write_inode_ea(i, fnode);
  	if (de) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
233
234
235
  		de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
  		de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
  		de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
  		de->read_only = !(i->i_mode & 0222);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
237
  		de->ea_size = cpu_to_le32(hpfs_inode->i_ea_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
239
240
241
242
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
  	}
  	if (S_ISDIR(i->i_mode)) {
  		if ((de = map_dirent(i, hpfs_inode->i_dno, "\001\001", 2, NULL, &qbh))) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
243
244
245
  			de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
  			de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
  			de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
  			de->read_only = !(i->i_mode & 0222);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
247
248
  			de->ea_size = cpu_to_le32(/*hpfs_inode->i_ea_size*/0);
  			de->file_size = cpu_to_le32(0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
  			hpfs_mark_4buffers_dirty(&qbh);
  			hpfs_brelse4(&qbh);
18debbbcc   Randy Dunlap   [PATCH] hpfs: fix...
251
252
253
254
  		} else
  			hpfs_error(i->i_sb,
  				"directory %08lx doesn't have '.' entry",
  				(unsigned long)i->i_ino);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
257
258
  	}
  	mark_buffer_dirty(bh);
  	brelse(bh);
  }
ca30bc995   Christoph Hellwig   [PATCH] hpfs: cle...
259
  int hpfs_setattr(struct dentry *dentry, struct iattr *attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  {
2b0143b5c   David Howells   VFS: normal files...
261
  	struct inode *inode = d_inode(dentry);
ca30bc995   Christoph Hellwig   [PATCH] hpfs: cle...
262
  	int error = -EINVAL;
9a311b96c   Arnd Bergmann   hpfs: remove the BKL
263
  	hpfs_lock(inode->i_sb);
ca30bc995   Christoph Hellwig   [PATCH] hpfs: cle...
264
265
  	if (inode->i_ino == hpfs_sb(inode->i_sb)->sb_root)
  		goto out_unlock;
0e1a43c71   Eric W. Biederman   userns: Convert h...
266
267
  	if ((attr->ia_valid & ATTR_UID) &&
  	    from_kuid(&init_user_ns, attr->ia_uid) >= 0x10000)
48f10e8ce   Mikulas Patocka   HPFS: Restrict ui...
268
  		goto out_unlock;
0e1a43c71   Eric W. Biederman   userns: Convert h...
269
270
  	if ((attr->ia_valid & ATTR_GID) &&
  	    from_kgid(&init_user_ns, attr->ia_gid) >= 0x10000)
48f10e8ce   Mikulas Patocka   HPFS: Restrict ui...
271
  		goto out_unlock;
ca30bc995   Christoph Hellwig   [PATCH] hpfs: cle...
272
273
  	if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
  		goto out_unlock;
31051c85b   Jan Kara   fs: Give dentry t...
274
  	error = setattr_prepare(dentry, attr);
ca30bc995   Christoph Hellwig   [PATCH] hpfs: cle...
275
276
  	if (error)
  		goto out_unlock;
1025774ce   Christoph Hellwig   remove inode_setattr
277
278
  	if ((attr->ia_valid & ATTR_SIZE) &&
  	    attr->ia_size != i_size_read(inode)) {
70b31c4c8   Marco Stornelli   hpfs: drop vmtrun...
279
  		error = inode_newsize_ok(inode, attr->ia_size);
1025774ce   Christoph Hellwig   remove inode_setattr
280
  		if (error)
274052ef0   Dr. David Alan Gilbert   hpfs_setattr erro...
281
  			goto out_unlock;
70b31c4c8   Marco Stornelli   hpfs: drop vmtrun...
282
283
284
  
  		truncate_setsize(inode, attr->ia_size);
  		hpfs_truncate(inode);
1025774ce   Christoph Hellwig   remove inode_setattr
285
286
287
  	}
  
  	setattr_copy(inode, attr);
ca30bc995   Christoph Hellwig   [PATCH] hpfs: cle...
288
289
290
291
  
  	hpfs_write_inode(inode);
  
   out_unlock:
9a311b96c   Arnd Bergmann   hpfs: remove the BKL
292
  	hpfs_unlock(inode->i_sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
295
296
297
298
299
300
301
302
  	return error;
  }
  
  void hpfs_write_if_changed(struct inode *inode)
  {
  	struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  
  	if (hpfs_inode->i_dirty)
  		hpfs_write_inode(inode);
  }
ea5440092   Al Viro   switch hpfs to ->...
303
  void hpfs_evict_inode(struct inode *inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
  {
91b0abe36   Johannes Weiner   mm + fs: store sh...
305
  	truncate_inode_pages_final(&inode->i_data);
dbd5768f8   Jan Kara   vfs: Rename end_w...
306
  	clear_inode(inode);
ea5440092   Al Viro   switch hpfs to ->...
307
  	if (!inode->i_nlink) {
9a311b96c   Arnd Bergmann   hpfs: remove the BKL
308
  		hpfs_lock(inode->i_sb);
ea5440092   Al Viro   switch hpfs to ->...
309
  		hpfs_remove_fnode(inode->i_sb, inode->i_ino);
9a311b96c   Arnd Bergmann   hpfs: remove the BKL
310
  		hpfs_unlock(inode->i_sb);
ea5440092   Al Viro   switch hpfs to ->...
311
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  }