4 * Written 1992,1993 by Werner Almesberger
6 * regular file handling primitives for fat-based filesystems
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>
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)
29 mutex_lock(&inode->i_mutex);
30 attr = fat_make_attrs(inode);
31 mutex_unlock(&inode->i_mutex);
33 return put_user(attr, user_attr);
36 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
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);
45 err = get_user(attr, user_attr);
49 err = mnt_want_write_file(file);
52 mutex_lock(&inode->i_mutex);
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.
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);
66 /* Equivalent to a chmod() */
67 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
68 ia.ia_ctime = current_fs_time(inode->i_sb);
70 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
72 ia.ia_mode = fat_make_mode(sbi, attr,
73 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
76 /* The root directory has no attributes */
77 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
79 goto out_unlock_inode;
82 if (sbi->options.sys_immutable &&
83 ((attr | oldattr) & ATTR_SYS) &&
84 !capable(CAP_LINUX_IMMUTABLE)) {
86 goto out_unlock_inode;
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.
94 err = security_inode_setattr(file->f_path.dentry, &ia);
96 goto out_unlock_inode;
98 /* This MUST be done before doing anything irreversible... */
99 err = fat_setattr(file->f_path.dentry, &ia);
101 goto out_unlock_inode;
103 fsnotify_change(file->f_path.dentry, ia.ia_valid);
104 if (sbi->options.sys_immutable) {
106 inode->i_flags |= S_IMMUTABLE;
108 inode->i_flags &= ~S_IMMUTABLE;
111 fat_save_attrs(inode, attr);
112 mark_inode_dirty(inode);
114 mutex_unlock(&inode->i_mutex);
115 mnt_drop_write_file(file);
120 static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
122 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
123 return put_user(sbi->vol_id, user_attr);
126 long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
128 struct inode *inode = file_inode(filp);
129 u32 __user *user_attr = (u32 __user *)arg;
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);
139 return -ENOTTY; /* Inappropriate ioctl for device */
144 static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd,
148 return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
152 static int fat_file_release(struct inode *inode, struct file *filp)
155 struct super_block *sb = inode->i_sb;
156 loff_t mmu_private_ideal;
159 * Release unwritten fallocated blocks on file release.
160 * Do this only when the last open file descriptor is closed.
162 mutex_lock(&inode->i_mutex);
163 mmu_private_ideal = round_up(inode->i_size, sb->s_blocksize);
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);
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);
178 int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
180 struct inode *inode = filp->f_mapping->host;
183 res = generic_file_fsync(filp, start, end, datasync);
184 err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
186 return res ? res : err;
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,
200 .compat_ioctl = fat_generic_compat_ioctl,
202 .fsync = fat_file_fsync,
203 .splice_read = generic_file_splice_read,
204 .fallocate = fat_fallocate,
207 static int fat_cont_expand(struct inode *inode, loff_t size)
209 struct address_space *mapping = inode->i_mapping;
210 loff_t start = inode->i_size, count = size - inode->i_size;
213 err = generic_cont_expand_simple(inode, size);
217 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
218 mark_inode_dirty(inode);
219 if (IS_SYNC(inode)) {
223 * Opencode syncing since we don't have a file open to use
224 * standard fsync path.
226 err = filemap_fdatawrite_range(mapping, start,
228 err2 = sync_mapping_buffers(mapping);
231 err2 = write_inode_now(inode, 1);
235 err = filemap_fdatawait_range(mapping, start,
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().
251 static long fat_fallocate(struct file *file, int mode,
252 loff_t offset, loff_t len)
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);
263 /* No support for hole punch or other fallocate flags. */
264 if (mode & ~FALLOC_FL_KEEP_SIZE)
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");
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,
281 fat_msg(sb, KERN_ERR,
282 "fat_fallocate(): fat_get_cluster() error");
285 free_bytes = ((fclus + 1) << sbi->cluster_bits) -
287 nr_bytes = offset + len - inode->i_size - free_bytes;
288 MSDOS_I(inode)->mmu_private = (fclus + 1) <<
291 nr_bytes = offset + len - inode->i_size;
293 nr_cluster = (nr_bytes + (sbi->cluster_size - 1)) >>
296 /* Start the allocation.We are not zeroing out the clusters */
297 while (nr_cluster-- > 0) {
298 err = fat_alloc_clusters(inode, &cluster, 1);
300 fat_msg(sb, KERN_ERR,
301 "fat_fallocate(): fat_alloc_clusters() error");
304 err = fat_chain_add(inode, cluster, 1);
306 fat_free_clusters(inode, cluster);
309 MSDOS_I(inode)->mmu_private += sbi->cluster_size;
312 /* This is just an expanding truncate */
313 err = fat_cont_expand(inode, (offset + len));
315 fat_msg(sb, KERN_ERR,
316 "fat_fallocate(): fat_cont_expand() error");
321 mutex_unlock(&inode->i_mutex);
325 /* Free all clusters after the skip'th cluster. */
326 static int fat_free(struct inode *inode, int skip)
328 struct super_block *sb = inode->i_sb;
329 int err, wait, free_start, i_start, i_logstart;
331 if (MSDOS_I(inode)->i_start == 0)
334 fat_cache_inval_inode(inode);
336 wait = IS_DIRSYNC(inode);
337 i_start = free_start = MSDOS_I(inode)->i_start;
338 i_logstart = MSDOS_I(inode)->i_logstart;
340 /* First, we write the new file size. */
342 MSDOS_I(inode)->i_start = 0;
343 MSDOS_I(inode)->i_logstart = 0;
345 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
346 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
348 err = fat_sync_inode(inode);
350 MSDOS_I(inode)->i_start = i_start;
351 MSDOS_I(inode)->i_logstart = i_logstart;
355 mark_inode_dirty(inode);
357 /* Write a new EOF, and get the remaining cluster chain for freeing. */
359 struct fat_entry fatent;
360 int ret, fclus, dclus;
362 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
365 else if (ret == FAT_ENT_EOF)
368 fatent_init(&fatent);
369 ret = fat_ent_read(inode, &fatent, dclus);
370 if (ret == FAT_ENT_EOF) {
371 fatent_brelse(&fatent);
373 } else if (ret == FAT_ENT_FREE) {
375 "%s: invalid cluster chain (i_pos %lld)",
376 __func__, MSDOS_I(inode)->i_pos);
378 } else if (ret > 0) {
379 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
383 fatent_brelse(&fatent);
389 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
391 /* Freeing the remained cluster chain */
392 return fat_free_clusters(inode, free_start);
395 void fat_truncate_blocks(struct inode *inode, loff_t offset)
397 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
398 const unsigned int cluster_size = sbi->cluster_size;
402 * This protects against truncating a file bigger than it was then
403 * trying to write into the hole.
405 if (MSDOS_I(inode)->mmu_private > offset)
406 MSDOS_I(inode)->mmu_private = offset;
408 nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
410 fat_free(inode, nr_clusters);
411 fat_flush_inodes(inode->i_sb, inode, NULL);
414 int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
416 struct inode *inode = dentry->d_inode;
417 generic_fillattr(inode, stat);
418 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
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);
426 EXPORT_SYMBOL_GPL(fat_getattr);
428 static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
429 struct inode *inode, umode_t *mode_ptr)
434 * Note, the basic check is already done by a caller of
435 * (attr->ia_mode & ~FAT_VALID_MODE)
438 if (S_ISREG(inode->i_mode))
439 mask = sbi->options.fs_fmask;
441 mask = sbi->options.fs_dmask;
443 perm = *mode_ptr & ~(S_IFMT | mask);
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.
449 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
451 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
453 if (fat_mode_can_hold_ro(inode)) {
454 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
457 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
461 *mode_ptr &= S_IFMT | perm;
466 static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
468 umode_t allow_utime = sbi->options.allow_utime;
470 if (!uid_eq(current_fsuid(), inode->i_uid)) {
471 if (in_group_p(inode->i_gid))
473 if (allow_utime & MAY_WRITE)
477 /* use a default check */
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)
485 int fat_setattr(struct dentry *dentry, struct iattr *attr)
487 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
488 struct inode *inode = dentry->d_inode;
489 unsigned int ia_valid;
491 loff_t mmu_private_ideal;
493 mmu_private_ideal = round_up(inode->i_size, dentry->d_sb->s_blocksize);
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;
502 error = inode_change_ok(inode, attr);
503 attr->ia_valid = ia_valid;
505 if (sbi->options.quiet)
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
516 if (attr->ia_valid & ATTR_SIZE) {
517 inode_dio_wait(inode);
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)
524 attr->ia_valid &= ~ATTR_SIZE;
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)))
537 if (sbi->options.quiet)
543 * We don't return -EPERM here. Yes, strange, but this is too
546 if (attr->ia_valid & ATTR_MODE) {
547 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
548 attr->ia_valid &= ~ATTR_MODE;
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);
558 setattr_copy(inode, attr);
559 mark_inode_dirty(inode);
563 EXPORT_SYMBOL_GPL(fat_setattr);
565 const struct inode_operations fat_file_inode_operations = {
566 .setattr = fat_setattr,
567 .getattr = fat_getattr,