]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/fat/file.c
fat: additions to support fat_fallocate
[karo-tx-linux.git] / fs / fat / file.c
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
9 #include <linux/capability.h>
10 #include <linux/module.h>
11 #include <linux/compat.h>
12 #include <linux/mount.h>
13 #include <linux/time.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/blkdev.h>
18 #include <linux/fsnotify.h>
19 #include <linux/security.h>
20 #include <linux/falloc.h>
21 #include "fat.h"
22
23 static long fat_fallocate(struct file *file, int mode,
24                                 loff_t offset, loff_t len);
25 static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
26 {
27         u32 attr;
28
29         mutex_lock(&inode->i_mutex);
30         attr = fat_make_attrs(inode);
31         mutex_unlock(&inode->i_mutex);
32
33         return put_user(attr, user_attr);
34 }
35
36 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
37 {
38         struct inode *inode = file_inode(file);
39         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
40         int is_dir = S_ISDIR(inode->i_mode);
41         u32 attr, oldattr;
42         struct iattr ia;
43         int err;
44
45         err = get_user(attr, user_attr);
46         if (err)
47                 goto out;
48
49         err = mnt_want_write_file(file);
50         if (err)
51                 goto out;
52         mutex_lock(&inode->i_mutex);
53
54         /*
55          * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
56          * prevents the user from turning us into a VFAT
57          * longname entry.  Also, we obviously can't set
58          * any of the NTFS attributes in the high 24 bits.
59          */
60         attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
61         /* Merge in ATTR_VOLUME and ATTR_DIR */
62         attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
63                 (is_dir ? ATTR_DIR : 0);
64         oldattr = fat_make_attrs(inode);
65
66         /* Equivalent to a chmod() */
67         ia.ia_valid = ATTR_MODE | ATTR_CTIME;
68         ia.ia_ctime = current_fs_time(inode->i_sb);
69         if (is_dir)
70                 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
71         else {
72                 ia.ia_mode = fat_make_mode(sbi, attr,
73                         S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
74         }
75
76         /* The root directory has no attributes */
77         if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
78                 err = -EINVAL;
79                 goto out_unlock_inode;
80         }
81
82         if (sbi->options.sys_immutable &&
83             ((attr | oldattr) & ATTR_SYS) &&
84             !capable(CAP_LINUX_IMMUTABLE)) {
85                 err = -EPERM;
86                 goto out_unlock_inode;
87         }
88
89         /*
90          * The security check is questionable...  We single
91          * out the RO attribute for checking by the security
92          * module, just because it maps to a file mode.
93          */
94         err = security_inode_setattr(file->f_path.dentry, &ia);
95         if (err)
96                 goto out_unlock_inode;
97
98         /* This MUST be done before doing anything irreversible... */
99         err = fat_setattr(file->f_path.dentry, &ia);
100         if (err)
101                 goto out_unlock_inode;
102
103         fsnotify_change(file->f_path.dentry, ia.ia_valid);
104         if (sbi->options.sys_immutable) {
105                 if (attr & ATTR_SYS)
106                         inode->i_flags |= S_IMMUTABLE;
107                 else
108                         inode->i_flags &= ~S_IMMUTABLE;
109         }
110
111         fat_save_attrs(inode, attr);
112         mark_inode_dirty(inode);
113 out_unlock_inode:
114         mutex_unlock(&inode->i_mutex);
115         mnt_drop_write_file(file);
116 out:
117         return err;
118 }
119
120 static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
121 {
122         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
123         return put_user(sbi->vol_id, user_attr);
124 }
125
126 long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
127 {
128         struct inode *inode = file_inode(filp);
129         u32 __user *user_attr = (u32 __user *)arg;
130
131         switch (cmd) {
132         case FAT_IOCTL_GET_ATTRIBUTES:
133                 return fat_ioctl_get_attributes(inode, user_attr);
134         case FAT_IOCTL_SET_ATTRIBUTES:
135                 return fat_ioctl_set_attributes(filp, user_attr);
136         case FAT_IOCTL_GET_VOLUME_ID:
137                 return fat_ioctl_get_volume_id(inode, user_attr);
138         default:
139                 return -ENOTTY; /* Inappropriate ioctl for device */
140         }
141 }
142
143 #ifdef CONFIG_COMPAT
144 static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd,
145                                       unsigned long arg)
146
147 {
148         return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
149 }
150 #endif
151
152 static int fat_file_release(struct inode *inode, struct file *filp)
153 {
154
155         struct super_block *sb = inode->i_sb;
156         loff_t mmu_private_ideal;
157
158         /*
159          * Release unwritten fallocated blocks on file release.
160          * Do this only when the last open file descriptor is closed.
161          */
162         mutex_lock(&inode->i_mutex);
163         mmu_private_ideal = round_up(inode->i_size, sb->s_blocksize);
164
165         if (mmu_private_ideal < MSDOS_I(inode)->mmu_private &&
166             filp->f_dentry->d_count == 1)
167                 fat_truncate_blocks(inode, inode->i_size);
168         mutex_unlock(&inode->i_mutex);
169
170         if ((filp->f_mode & FMODE_WRITE) &&
171              MSDOS_SB(inode->i_sb)->options.flush) {
172                 fat_flush_inodes(inode->i_sb, inode, NULL);
173                 congestion_wait(BLK_RW_ASYNC, HZ/10);
174         }
175         return 0;
176 }
177
178 int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
179 {
180         struct inode *inode = filp->f_mapping->host;
181         int res, err;
182
183         res = generic_file_fsync(filp, start, end, datasync);
184         err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
185
186         return res ? res : err;
187 }
188
189
190 const struct file_operations fat_file_operations = {
191         .llseek         = generic_file_llseek,
192         .read           = do_sync_read,
193         .write          = do_sync_write,
194         .aio_read       = generic_file_aio_read,
195         .aio_write      = generic_file_aio_write,
196         .mmap           = generic_file_mmap,
197         .release        = fat_file_release,
198         .unlocked_ioctl = fat_generic_ioctl,
199 #ifdef CONFIG_COMPAT
200         .compat_ioctl   = fat_generic_compat_ioctl,
201 #endif
202         .fsync          = fat_file_fsync,
203         .splice_read    = generic_file_splice_read,
204         .fallocate      = fat_fallocate,
205 };
206
207 static int fat_cont_expand(struct inode *inode, loff_t size)
208 {
209         struct address_space *mapping = inode->i_mapping;
210         loff_t start = inode->i_size, count = size - inode->i_size;
211         int err;
212
213         err = generic_cont_expand_simple(inode, size);
214         if (err)
215                 goto out;
216
217         inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
218         mark_inode_dirty(inode);
219         if (IS_SYNC(inode)) {
220                 int err2;
221
222                 /*
223                  * Opencode syncing since we don't have a file open to use
224                  * standard fsync path.
225                  */
226                 err = filemap_fdatawrite_range(mapping, start,
227                                                start + count - 1);
228                 err2 = sync_mapping_buffers(mapping);
229                 if (!err)
230                         err = err2;
231                 err2 = write_inode_now(inode, 1);
232                 if (!err)
233                         err = err2;
234                 if (!err) {
235                         err =  filemap_fdatawait_range(mapping, start,
236                                                        start + count - 1);
237                 }
238         }
239 out:
240         return err;
241 }
242
243 /*
244  * Preallocate space for a file. This implements fat's fallocate file
245  * operation, which gets called from sys_fallocate system call. User
246  * space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set
247  * we just allocate clusters without zeroing them out. Otherwise we
248  * allocate and zero out clusters via an expanding truncate. The
249  * allocated clusters are freed in fat_file_release().
250  */
251 static long fat_fallocate(struct file *file, int mode,
252                                 loff_t offset, loff_t len)
253 {
254         int cluster, fclus, dclus;
255         int nr_cluster; /* Number of clusters to be allocated */
256         loff_t nr_bytes; /* Number of bytes to be allocated*/
257         loff_t free_bytes; /* Unused bytes in the last cluster of file*/
258         struct inode *inode = file->f_mapping->host;
259         struct super_block *sb = inode->i_sb;
260         struct msdos_sb_info *sbi = MSDOS_SB(sb);
261         int err = 0;
262
263         /* No support for hole punch or other fallocate flags. */
264         if (mode & ~FALLOC_FL_KEEP_SIZE)
265                 return -EOPNOTSUPP;
266
267         mutex_lock(&inode->i_mutex);
268         if ((offset + len) <= MSDOS_I(inode)->mmu_private) {
269                 fat_msg(sb, KERN_ERR,
270                         "fat_fallocate(): Blocks already allocated");
271                 err = -EINVAL;
272                 goto error;
273         }
274
275         if (mode & FALLOC_FL_KEEP_SIZE) {
276                 /* First compute the number of clusters to be allocated */
277                 if (inode->i_size > 0) {
278                         err = fat_get_cluster(inode, FAT_ENT_EOF,
279                                               &fclus, &dclus);
280                         if (err < 0) {
281                                 fat_msg(sb, KERN_ERR,
282                                         "fat_fallocate(): fat_get_cluster() error");
283                                 goto error;
284                         }
285                         free_bytes = ((fclus + 1) << sbi->cluster_bits) -
286                                      inode->i_size;
287                         nr_bytes = offset + len - inode->i_size - free_bytes;
288                         MSDOS_I(inode)->mmu_private = (fclus + 1) <<
289                                                       sbi->cluster_bits;
290                 } else
291                         nr_bytes = offset + len - inode->i_size;
292
293                 nr_cluster = (nr_bytes + (sbi->cluster_size - 1)) >>
294                              sbi->cluster_bits;
295
296                 /* Start the allocation.We are not zeroing out the clusters */
297                 while (nr_cluster-- > 0) {
298                         err = fat_alloc_clusters(inode, &cluster, 1);
299                         if (err) {
300                                 fat_msg(sb, KERN_ERR,
301                                         "fat_fallocate(): fat_alloc_clusters() error");
302                                 goto error;
303                         }
304                         err = fat_chain_add(inode, cluster, 1);
305                         if (err) {
306                                 fat_free_clusters(inode, cluster);
307                                 goto error;
308                         }
309                         MSDOS_I(inode)->mmu_private += sbi->cluster_size;
310                 }
311         } else {
312                 /* This is just an expanding truncate */
313                 err = fat_cont_expand(inode, (offset + len));
314                 if (err) {
315                         fat_msg(sb, KERN_ERR,
316                                 "fat_fallocate(): fat_cont_expand() error");
317                 }
318         }
319
320 error:
321         mutex_unlock(&inode->i_mutex);
322         return err;
323 }
324
325 /* Free all clusters after the skip'th cluster. */
326 static int fat_free(struct inode *inode, int skip)
327 {
328         struct super_block *sb = inode->i_sb;
329         int err, wait, free_start, i_start, i_logstart;
330
331         if (MSDOS_I(inode)->i_start == 0)
332                 return 0;
333
334         fat_cache_inval_inode(inode);
335
336         wait = IS_DIRSYNC(inode);
337         i_start = free_start = MSDOS_I(inode)->i_start;
338         i_logstart = MSDOS_I(inode)->i_logstart;
339
340         /* First, we write the new file size. */
341         if (!skip) {
342                 MSDOS_I(inode)->i_start = 0;
343                 MSDOS_I(inode)->i_logstart = 0;
344         }
345         MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
346         inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
347         if (wait) {
348                 err = fat_sync_inode(inode);
349                 if (err) {
350                         MSDOS_I(inode)->i_start = i_start;
351                         MSDOS_I(inode)->i_logstart = i_logstart;
352                         return err;
353                 }
354         } else
355                 mark_inode_dirty(inode);
356
357         /* Write a new EOF, and get the remaining cluster chain for freeing. */
358         if (skip) {
359                 struct fat_entry fatent;
360                 int ret, fclus, dclus;
361
362                 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
363                 if (ret < 0)
364                         return ret;
365                 else if (ret == FAT_ENT_EOF)
366                         return 0;
367
368                 fatent_init(&fatent);
369                 ret = fat_ent_read(inode, &fatent, dclus);
370                 if (ret == FAT_ENT_EOF) {
371                         fatent_brelse(&fatent);
372                         return 0;
373                 } else if (ret == FAT_ENT_FREE) {
374                         fat_fs_error(sb,
375                                      "%s: invalid cluster chain (i_pos %lld)",
376                                      __func__, MSDOS_I(inode)->i_pos);
377                         ret = -EIO;
378                 } else if (ret > 0) {
379                         err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
380                         if (err)
381                                 ret = err;
382                 }
383                 fatent_brelse(&fatent);
384                 if (ret < 0)
385                         return ret;
386
387                 free_start = ret;
388         }
389         inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
390
391         /* Freeing the remained cluster chain */
392         return fat_free_clusters(inode, free_start);
393 }
394
395 void fat_truncate_blocks(struct inode *inode, loff_t offset)
396 {
397         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
398         const unsigned int cluster_size = sbi->cluster_size;
399         int nr_clusters;
400
401         /*
402          * This protects against truncating a file bigger than it was then
403          * trying to write into the hole.
404          */
405         if (MSDOS_I(inode)->mmu_private > offset)
406                 MSDOS_I(inode)->mmu_private = offset;
407
408         nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
409
410         fat_free(inode, nr_clusters);
411         fat_flush_inodes(inode->i_sb, inode, NULL);
412 }
413
414 int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
415 {
416         struct inode *inode = dentry->d_inode;
417         generic_fillattr(inode, stat);
418         stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
419
420         if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
421                 /* Use i_pos for ino. This is used as fileid of nfs. */
422                 stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode);
423         }
424         return 0;
425 }
426 EXPORT_SYMBOL_GPL(fat_getattr);
427
428 static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
429                              struct inode *inode, umode_t *mode_ptr)
430 {
431         umode_t mask, perm;
432
433         /*
434          * Note, the basic check is already done by a caller of
435          * (attr->ia_mode & ~FAT_VALID_MODE)
436          */
437
438         if (S_ISREG(inode->i_mode))
439                 mask = sbi->options.fs_fmask;
440         else
441                 mask = sbi->options.fs_dmask;
442
443         perm = *mode_ptr & ~(S_IFMT | mask);
444
445         /*
446          * Of the r and x bits, all (subject to umask) must be present. Of the
447          * w bits, either all (subject to umask) or none must be present.
448          *
449          * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
450          */
451         if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
452                 return -EPERM;
453         if (fat_mode_can_hold_ro(inode)) {
454                 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
455                         return -EPERM;
456         } else {
457                 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
458                         return -EPERM;
459         }
460
461         *mode_ptr &= S_IFMT | perm;
462
463         return 0;
464 }
465
466 static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
467 {
468         umode_t allow_utime = sbi->options.allow_utime;
469
470         if (!uid_eq(current_fsuid(), inode->i_uid)) {
471                 if (in_group_p(inode->i_gid))
472                         allow_utime >>= 3;
473                 if (allow_utime & MAY_WRITE)
474                         return 1;
475         }
476
477         /* use a default check */
478         return 0;
479 }
480
481 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
482 /* valid file mode bits */
483 #define FAT_VALID_MODE  (S_IFREG | S_IFDIR | S_IRWXUGO)
484
485 int fat_setattr(struct dentry *dentry, struct iattr *attr)
486 {
487         struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
488         struct inode *inode = dentry->d_inode;
489         unsigned int ia_valid;
490         int error;
491         loff_t mmu_private_ideal;
492
493         mmu_private_ideal = round_up(inode->i_size, dentry->d_sb->s_blocksize);
494
495         /* Check for setting the inode time. */
496         ia_valid = attr->ia_valid;
497         if (ia_valid & TIMES_SET_FLAGS) {
498                 if (fat_allow_set_time(sbi, inode))
499                         attr->ia_valid &= ~TIMES_SET_FLAGS;
500         }
501
502         error = inode_change_ok(inode, attr);
503         attr->ia_valid = ia_valid;
504         if (error) {
505                 if (sbi->options.quiet)
506                         error = 0;
507                 goto out;
508         }
509
510         /*
511          * Expand the file. Since inode_setattr() updates ->i_size
512          * before calling the ->truncate(), but FAT needs to fill the
513          * hole before it. XXX: this is no longer true with new truncate
514          * sequence.
515          */
516         if (attr->ia_valid & ATTR_SIZE) {
517                 inode_dio_wait(inode);
518
519                 if (attr->ia_size > inode->i_size &&
520                     MSDOS_I(inode)->mmu_private <= mmu_private_ideal) {
521                         error = fat_cont_expand(inode, attr->ia_size);
522                         if (error || attr->ia_valid == ATTR_SIZE)
523                                 goto out;
524                         attr->ia_valid &= ~ATTR_SIZE;
525                 }
526         }
527
528         if (((attr->ia_valid & ATTR_UID) &&
529              (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
530             ((attr->ia_valid & ATTR_GID) &&
531              (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
532             ((attr->ia_valid & ATTR_MODE) &&
533              (attr->ia_mode & ~FAT_VALID_MODE)))
534                 error = -EPERM;
535
536         if (error) {
537                 if (sbi->options.quiet)
538                         error = 0;
539                 goto out;
540         }
541
542         /*
543          * We don't return -EPERM here. Yes, strange, but this is too
544          * old behavior.
545          */
546         if (attr->ia_valid & ATTR_MODE) {
547                 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
548                         attr->ia_valid &= ~ATTR_MODE;
549         }
550
551         if (attr->ia_valid & ATTR_SIZE) {
552                 down_write(&MSDOS_I(inode)->truncate_lock);
553                 truncate_setsize(inode, attr->ia_size);
554                 fat_truncate_blocks(inode, attr->ia_size);
555                 up_write(&MSDOS_I(inode)->truncate_lock);
556         }
557
558         setattr_copy(inode, attr);
559         mark_inode_dirty(inode);
560 out:
561         return error;
562 }
563 EXPORT_SYMBOL_GPL(fat_setattr);
564
565 const struct inode_operations fat_file_inode_operations = {
566         .setattr        = fat_setattr,
567         .getattr        = fat_getattr,
568 };