]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/fat/file.c | |
3 | * | |
4 | * Written 1992,1993 by Werner Almesberger | |
5 | * | |
6 | * regular file handling primitives for fat-based filesystems | |
7 | */ | |
8 | ||
16f7e0fe | 9 | #include <linux/capability.h> |
1da177e4 | 10 | #include <linux/module.h> |
7845bc3e | 11 | #include <linux/compat.h> |
42a74f20 | 12 | #include <linux/mount.h> |
ae78bf9c | 13 | #include <linux/blkdev.h> |
66114cad | 14 | #include <linux/backing-dev.h> |
b1da47e2 MS |
15 | #include <linux/fsnotify.h> |
16 | #include <linux/security.h> | |
9e975dae | 17 | #include "fat.h" |
1da177e4 | 18 | |
21bea495 CH |
19 | static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr) |
20 | { | |
21 | u32 attr; | |
22 | ||
23 | mutex_lock(&inode->i_mutex); | |
24 | attr = fat_make_attrs(inode); | |
25 | mutex_unlock(&inode->i_mutex); | |
26 | ||
27 | return put_user(attr, user_attr); | |
28 | } | |
29 | ||
30 | static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr) | |
1da177e4 | 31 | { |
496ad9aa | 32 | struct inode *inode = file_inode(file); |
1da177e4 | 33 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
21bea495 CH |
34 | int is_dir = S_ISDIR(inode->i_mode); |
35 | u32 attr, oldattr; | |
36 | struct iattr ia; | |
37 | int err; | |
1da177e4 | 38 | |
21bea495 CH |
39 | err = get_user(attr, user_attr); |
40 | if (err) | |
41 | goto out; | |
1da177e4 | 42 | |
a561be71 | 43 | err = mnt_want_write_file(file); |
21bea495 | 44 | if (err) |
e24f17da JK |
45 | goto out; |
46 | mutex_lock(&inode->i_mutex); | |
21bea495 CH |
47 | |
48 | /* | |
49 | * ATTR_VOLUME and ATTR_DIR cannot be changed; this also | |
50 | * prevents the user from turning us into a VFAT | |
51 | * longname entry. Also, we obviously can't set | |
52 | * any of the NTFS attributes in the high 24 bits. | |
53 | */ | |
54 | attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); | |
55 | /* Merge in ATTR_VOLUME and ATTR_DIR */ | |
56 | attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | | |
57 | (is_dir ? ATTR_DIR : 0); | |
58 | oldattr = fat_make_attrs(inode); | |
59 | ||
60 | /* Equivalent to a chmod() */ | |
61 | ia.ia_valid = ATTR_MODE | ATTR_CTIME; | |
62 | ia.ia_ctime = current_fs_time(inode->i_sb); | |
63 | if (is_dir) | |
64 | ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO); | |
65 | else { | |
66 | ia.ia_mode = fat_make_mode(sbi, attr, | |
67 | S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)); | |
68 | } | |
1da177e4 | 69 | |
21bea495 CH |
70 | /* The root directory has no attributes */ |
71 | if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { | |
72 | err = -EINVAL; | |
e24f17da | 73 | goto out_unlock_inode; |
1da177e4 | 74 | } |
1da177e4 | 75 | |
21bea495 CH |
76 | if (sbi->options.sys_immutable && |
77 | ((attr | oldattr) & ATTR_SYS) && | |
78 | !capable(CAP_LINUX_IMMUTABLE)) { | |
79 | err = -EPERM; | |
e24f17da | 80 | goto out_unlock_inode; |
21bea495 | 81 | } |
1da177e4 | 82 | |
21bea495 CH |
83 | /* |
84 | * The security check is questionable... We single | |
85 | * out the RO attribute for checking by the security | |
86 | * module, just because it maps to a file mode. | |
87 | */ | |
88 | err = security_inode_setattr(file->f_path.dentry, &ia); | |
89 | if (err) | |
e24f17da | 90 | goto out_unlock_inode; |
1da177e4 | 91 | |
21bea495 CH |
92 | /* This MUST be done before doing anything irreversible... */ |
93 | err = fat_setattr(file->f_path.dentry, &ia); | |
94 | if (err) | |
e24f17da | 95 | goto out_unlock_inode; |
21bea495 CH |
96 | |
97 | fsnotify_change(file->f_path.dentry, ia.ia_valid); | |
98 | if (sbi->options.sys_immutable) { | |
99 | if (attr & ATTR_SYS) | |
100 | inode->i_flags |= S_IMMUTABLE; | |
101 | else | |
1adffbae | 102 | inode->i_flags &= ~S_IMMUTABLE; |
21bea495 | 103 | } |
1da177e4 | 104 | |
21bea495 CH |
105 | fat_save_attrs(inode, attr); |
106 | mark_inode_dirty(inode); | |
21bea495 CH |
107 | out_unlock_inode: |
108 | mutex_unlock(&inode->i_mutex); | |
e24f17da | 109 | mnt_drop_write_file(file); |
21bea495 CH |
110 | out: |
111 | return err; | |
112 | } | |
1da177e4 | 113 | |
6e5b93ee ML |
114 | static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr) |
115 | { | |
116 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | |
117 | return put_user(sbi->vol_id, user_attr); | |
118 | } | |
119 | ||
7845bc3e | 120 | long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1da177e4 | 121 | { |
496ad9aa | 122 | struct inode *inode = file_inode(filp); |
1da177e4 | 123 | u32 __user *user_attr = (u32 __user *)arg; |
1da177e4 | 124 | |
1da177e4 LT |
125 | switch (cmd) { |
126 | case FAT_IOCTL_GET_ATTRIBUTES: | |
21bea495 | 127 | return fat_ioctl_get_attributes(inode, user_attr); |
1da177e4 | 128 | case FAT_IOCTL_SET_ATTRIBUTES: |
21bea495 | 129 | return fat_ioctl_set_attributes(filp, user_attr); |
6e5b93ee ML |
130 | case FAT_IOCTL_GET_VOLUME_ID: |
131 | return fat_ioctl_get_volume_id(inode, user_attr); | |
1da177e4 LT |
132 | default: |
133 | return -ENOTTY; /* Inappropriate ioctl for device */ | |
134 | } | |
135 | } | |
136 | ||
7845bc3e AB |
137 | #ifdef CONFIG_COMPAT |
138 | static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd, | |
139 | unsigned long arg) | |
140 | ||
141 | { | |
142 | return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | |
143 | } | |
144 | #endif | |
145 | ||
ae78bf9c CM |
146 | static int fat_file_release(struct inode *inode, struct file *filp) |
147 | { | |
148 | if ((filp->f_mode & FMODE_WRITE) && | |
149 | MSDOS_SB(inode->i_sb)->options.flush) { | |
150 | fat_flush_inodes(inode->i_sb, inode, NULL); | |
8aa7e847 | 151 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
ae78bf9c CM |
152 | } |
153 | return 0; | |
154 | } | |
155 | ||
02c24a82 | 156 | int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) |
b522412a | 157 | { |
7ea80859 | 158 | struct inode *inode = filp->f_mapping->host; |
b522412a AV |
159 | int res, err; |
160 | ||
02c24a82 | 161 | res = generic_file_fsync(filp, start, end, datasync); |
b522412a AV |
162 | err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping); |
163 | ||
164 | return res ? res : err; | |
165 | } | |
166 | ||
167 | ||
4b6f5d20 | 168 | const struct file_operations fat_file_operations = { |
1da177e4 | 169 | .llseek = generic_file_llseek, |
aad4f8bb | 170 | .read_iter = generic_file_read_iter, |
8174202b | 171 | .write_iter = generic_file_write_iter, |
1da177e4 | 172 | .mmap = generic_file_mmap, |
ae78bf9c | 173 | .release = fat_file_release, |
7845bc3e AB |
174 | .unlocked_ioctl = fat_generic_ioctl, |
175 | #ifdef CONFIG_COMPAT | |
176 | .compat_ioctl = fat_generic_compat_ioctl, | |
177 | #endif | |
b522412a | 178 | .fsync = fat_file_fsync, |
5ffc4ef4 | 179 | .splice_read = generic_file_splice_read, |
1da177e4 LT |
180 | }; |
181 | ||
05eb0b51 OH |
182 | static int fat_cont_expand(struct inode *inode, loff_t size) |
183 | { | |
184 | struct address_space *mapping = inode->i_mapping; | |
185 | loff_t start = inode->i_size, count = size - inode->i_size; | |
186 | int err; | |
187 | ||
188 | err = generic_cont_expand_simple(inode, size); | |
189 | if (err) | |
190 | goto out; | |
191 | ||
192 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; | |
193 | mark_inode_dirty(inode); | |
2f3d675b JK |
194 | if (IS_SYNC(inode)) { |
195 | int err2; | |
196 | ||
197 | /* | |
198 | * Opencode syncing since we don't have a file open to use | |
199 | * standard fsync path. | |
200 | */ | |
201 | err = filemap_fdatawrite_range(mapping, start, | |
202 | start + count - 1); | |
203 | err2 = sync_mapping_buffers(mapping); | |
204 | if (!err) | |
205 | err = err2; | |
206 | err2 = write_inode_now(inode, 1); | |
207 | if (!err) | |
208 | err = err2; | |
209 | if (!err) { | |
210 | err = filemap_fdatawait_range(mapping, start, | |
211 | start + count - 1); | |
212 | } | |
213 | } | |
05eb0b51 OH |
214 | out: |
215 | return err; | |
216 | } | |
217 | ||
1da177e4 LT |
218 | /* Free all clusters after the skip'th cluster. */ |
219 | static int fat_free(struct inode *inode, int skip) | |
220 | { | |
221 | struct super_block *sb = inode->i_sb; | |
222 | int err, wait, free_start, i_start, i_logstart; | |
223 | ||
224 | if (MSDOS_I(inode)->i_start == 0) | |
225 | return 0; | |
226 | ||
3b641407 OH |
227 | fat_cache_inval_inode(inode); |
228 | ||
1da177e4 | 229 | wait = IS_DIRSYNC(inode); |
3b641407 OH |
230 | i_start = free_start = MSDOS_I(inode)->i_start; |
231 | i_logstart = MSDOS_I(inode)->i_logstart; | |
232 | ||
233 | /* First, we write the new file size. */ | |
234 | if (!skip) { | |
235 | MSDOS_I(inode)->i_start = 0; | |
236 | MSDOS_I(inode)->i_logstart = 0; | |
237 | } | |
238 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; | |
239 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; | |
240 | if (wait) { | |
241 | err = fat_sync_inode(inode); | |
242 | if (err) { | |
243 | MSDOS_I(inode)->i_start = i_start; | |
244 | MSDOS_I(inode)->i_logstart = i_logstart; | |
245 | return err; | |
246 | } | |
247 | } else | |
248 | mark_inode_dirty(inode); | |
249 | ||
250 | /* Write a new EOF, and get the remaining cluster chain for freeing. */ | |
1da177e4 LT |
251 | if (skip) { |
252 | struct fat_entry fatent; | |
253 | int ret, fclus, dclus; | |
254 | ||
255 | ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); | |
256 | if (ret < 0) | |
257 | return ret; | |
258 | else if (ret == FAT_ENT_EOF) | |
259 | return 0; | |
260 | ||
261 | fatent_init(&fatent); | |
262 | ret = fat_ent_read(inode, &fatent, dclus); | |
263 | if (ret == FAT_ENT_EOF) { | |
264 | fatent_brelse(&fatent); | |
265 | return 0; | |
266 | } else if (ret == FAT_ENT_FREE) { | |
85c78591 | 267 | fat_fs_error(sb, |
1da177e4 | 268 | "%s: invalid cluster chain (i_pos %lld)", |
8e24eea7 | 269 | __func__, MSDOS_I(inode)->i_pos); |
1da177e4 LT |
270 | ret = -EIO; |
271 | } else if (ret > 0) { | |
272 | err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); | |
273 | if (err) | |
274 | ret = err; | |
275 | } | |
276 | fatent_brelse(&fatent); | |
277 | if (ret < 0) | |
278 | return ret; | |
279 | ||
280 | free_start = ret; | |
1da177e4 | 281 | } |
1da177e4 LT |
282 | inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); |
283 | ||
284 | /* Freeing the remained cluster chain */ | |
285 | return fat_free_clusters(inode, free_start); | |
1da177e4 LT |
286 | } |
287 | ||
459f6ed3 | 288 | void fat_truncate_blocks(struct inode *inode, loff_t offset) |
1da177e4 | 289 | { |
9c20616c | 290 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
1da177e4 LT |
291 | const unsigned int cluster_size = sbi->cluster_size; |
292 | int nr_clusters; | |
293 | ||
294 | /* | |
295 | * This protects against truncating a file bigger than it was then | |
296 | * trying to write into the hole. | |
297 | */ | |
459f6ed3 NP |
298 | if (MSDOS_I(inode)->mmu_private > offset) |
299 | MSDOS_I(inode)->mmu_private = offset; | |
1da177e4 | 300 | |
459f6ed3 | 301 | nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits; |
1da177e4 | 302 | |
1da177e4 | 303 | fat_free(inode, nr_clusters); |
ae78bf9c | 304 | fat_flush_inodes(inode->i_sb, inode, NULL); |
1da177e4 LT |
305 | } |
306 | ||
da63fc7c OH |
307 | int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
308 | { | |
2b0143b5 | 309 | struct inode *inode = d_inode(dentry); |
da63fc7c OH |
310 | generic_fillattr(inode, stat); |
311 | stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size; | |
ea3983ac NJ |
312 | |
313 | if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) { | |
314 | /* Use i_pos for ino. This is used as fileid of nfs. */ | |
315 | stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode); | |
316 | } | |
da63fc7c OH |
317 | return 0; |
318 | } | |
319 | EXPORT_SYMBOL_GPL(fat_getattr); | |
320 | ||
2d518f84 OH |
321 | static int fat_sanitize_mode(const struct msdos_sb_info *sbi, |
322 | struct inode *inode, umode_t *mode_ptr) | |
1278fdd3 | 323 | { |
dacd0e7b | 324 | umode_t mask, perm; |
1278fdd3 | 325 | |
2d518f84 OH |
326 | /* |
327 | * Note, the basic check is already done by a caller of | |
9c0aa1b8 | 328 | * (attr->ia_mode & ~FAT_VALID_MODE) |
2d518f84 OH |
329 | */ |
330 | ||
331 | if (S_ISREG(inode->i_mode)) | |
1278fdd3 OH |
332 | mask = sbi->options.fs_fmask; |
333 | else | |
334 | mask = sbi->options.fs_dmask; | |
335 | ||
2d518f84 OH |
336 | perm = *mode_ptr & ~(S_IFMT | mask); |
337 | ||
1278fdd3 OH |
338 | /* |
339 | * Of the r and x bits, all (subject to umask) must be present. Of the | |
340 | * w bits, either all (subject to umask) or none must be present. | |
dfc209c0 OH |
341 | * |
342 | * If fat_mode_can_hold_ro(inode) is false, can't change w bits. | |
1278fdd3 | 343 | */ |
2d518f84 | 344 | if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) |
1278fdd3 | 345 | return -EPERM; |
dfc209c0 OH |
346 | if (fat_mode_can_hold_ro(inode)) { |
347 | if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask))) | |
348 | return -EPERM; | |
349 | } else { | |
350 | if ((perm & S_IWUGO) != (S_IWUGO & ~mask)) | |
351 | return -EPERM; | |
352 | } | |
1278fdd3 | 353 | |
2d518f84 OH |
354 | *mode_ptr &= S_IFMT | perm; |
355 | ||
1278fdd3 OH |
356 | return 0; |
357 | } | |
358 | ||
1ae43f82 OH |
359 | static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode) |
360 | { | |
dacd0e7b | 361 | umode_t allow_utime = sbi->options.allow_utime; |
1ae43f82 | 362 | |
170782eb | 363 | if (!uid_eq(current_fsuid(), inode->i_uid)) { |
1ae43f82 OH |
364 | if (in_group_p(inode->i_gid)) |
365 | allow_utime >>= 3; | |
366 | if (allow_utime & MAY_WRITE) | |
367 | return 1; | |
368 | } | |
369 | ||
370 | /* use a default check */ | |
371 | return 0; | |
372 | } | |
373 | ||
17263849 | 374 | #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET) |
9c0aa1b8 OH |
375 | /* valid file mode bits */ |
376 | #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO) | |
17263849 | 377 | |
1278fdd3 OH |
378 | int fat_setattr(struct dentry *dentry, struct iattr *attr) |
379 | { | |
380 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); | |
2b0143b5 | 381 | struct inode *inode = d_inode(dentry); |
1ae43f82 | 382 | unsigned int ia_valid; |
dfc209c0 | 383 | int error; |
1278fdd3 | 384 | |
1ae43f82 OH |
385 | /* Check for setting the inode time. */ |
386 | ia_valid = attr->ia_valid; | |
17263849 | 387 | if (ia_valid & TIMES_SET_FLAGS) { |
1ae43f82 | 388 | if (fat_allow_set_time(sbi, inode)) |
17263849 | 389 | attr->ia_valid &= ~TIMES_SET_FLAGS; |
1ae43f82 OH |
390 | } |
391 | ||
1278fdd3 | 392 | error = inode_change_ok(inode, attr); |
1ae43f82 | 393 | attr->ia_valid = ia_valid; |
1278fdd3 OH |
394 | if (error) { |
395 | if (sbi->options.quiet) | |
396 | error = 0; | |
397 | goto out; | |
398 | } | |
2d518f84 | 399 | |
db78b877 CH |
400 | /* |
401 | * Expand the file. Since inode_setattr() updates ->i_size | |
402 | * before calling the ->truncate(), but FAT needs to fill the | |
403 | * hole before it. XXX: this is no longer true with new truncate | |
404 | * sequence. | |
405 | */ | |
406 | if (attr->ia_valid & ATTR_SIZE) { | |
562c72aa CH |
407 | inode_dio_wait(inode); |
408 | ||
db78b877 CH |
409 | if (attr->ia_size > inode->i_size) { |
410 | error = fat_cont_expand(inode, attr->ia_size); | |
411 | if (error || attr->ia_valid == ATTR_SIZE) | |
412 | goto out; | |
413 | attr->ia_valid &= ~ATTR_SIZE; | |
414 | } | |
415 | } | |
416 | ||
1278fdd3 | 417 | if (((attr->ia_valid & ATTR_UID) && |
170782eb | 418 | (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) || |
1278fdd3 | 419 | ((attr->ia_valid & ATTR_GID) && |
170782eb | 420 | (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) || |
e97e8de3 | 421 | ((attr->ia_valid & ATTR_MODE) && |
9c0aa1b8 | 422 | (attr->ia_mode & ~FAT_VALID_MODE))) |
1278fdd3 OH |
423 | error = -EPERM; |
424 | ||
425 | if (error) { | |
426 | if (sbi->options.quiet) | |
427 | error = 0; | |
428 | goto out; | |
429 | } | |
430 | ||
2d518f84 OH |
431 | /* |
432 | * We don't return -EPERM here. Yes, strange, but this is too | |
433 | * old behavior. | |
434 | */ | |
435 | if (attr->ia_valid & ATTR_MODE) { | |
436 | if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) | |
437 | attr->ia_valid &= ~ATTR_MODE; | |
438 | } | |
1278fdd3 | 439 | |
459f6ed3 | 440 | if (attr->ia_valid & ATTR_SIZE) { |
c0ef0cc9 NJ |
441 | error = fat_block_truncate_page(inode, attr->ia_size); |
442 | if (error) | |
443 | goto out; | |
58268691 | 444 | down_write(&MSDOS_I(inode)->truncate_lock); |
2c27c65e CH |
445 | truncate_setsize(inode, attr->ia_size); |
446 | fat_truncate_blocks(inode, attr->ia_size); | |
58268691 | 447 | up_write(&MSDOS_I(inode)->truncate_lock); |
459f6ed3 NP |
448 | } |
449 | ||
6a1a90ad | 450 | setattr_copy(inode, attr); |
459f6ed3 | 451 | mark_inode_dirty(inode); |
1278fdd3 | 452 | out: |
1278fdd3 OH |
453 | return error; |
454 | } | |
455 | EXPORT_SYMBOL_GPL(fat_setattr); | |
456 | ||
754661f1 | 457 | const struct inode_operations fat_file_inode_operations = { |
1278fdd3 | 458 | .setattr = fat_setattr, |
da63fc7c | 459 | .getattr = fat_getattr, |
1da177e4 | 460 | }; |