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/blkdev.h>
14 #include <linux/backing-dev.h>
15 #include <linux/fsnotify.h>
16 #include <linux/security.h>
19 static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
23 mutex_lock(&inode->i_mutex);
24 attr = fat_make_attrs(inode);
25 mutex_unlock(&inode->i_mutex);
27 return put_user(attr, user_attr);
30 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
32 struct inode *inode = file_inode(file);
33 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
34 int is_dir = S_ISDIR(inode->i_mode);
39 err = get_user(attr, user_attr);
43 err = mnt_want_write_file(file);
46 mutex_lock(&inode->i_mutex);
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.
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);
60 /* Equivalent to a chmod() */
61 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
62 ia.ia_ctime = current_fs_time(inode->i_sb);
64 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
66 ia.ia_mode = fat_make_mode(sbi, attr,
67 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
70 /* The root directory has no attributes */
71 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
73 goto out_unlock_inode;
76 if (sbi->options.sys_immutable &&
77 ((attr | oldattr) & ATTR_SYS) &&
78 !capable(CAP_LINUX_IMMUTABLE)) {
80 goto out_unlock_inode;
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.
88 err = security_inode_setattr(file->f_path.dentry, &ia);
90 goto out_unlock_inode;
92 /* This MUST be done before doing anything irreversible... */
93 err = fat_setattr(file->f_path.dentry, &ia);
95 goto out_unlock_inode;
97 fsnotify_change(file->f_path.dentry, ia.ia_valid);
98 if (sbi->options.sys_immutable) {
100 inode->i_flags |= S_IMMUTABLE;
102 inode->i_flags &= ~S_IMMUTABLE;
105 fat_save_attrs(inode, attr);
106 mark_inode_dirty(inode);
108 mutex_unlock(&inode->i_mutex);
109 mnt_drop_write_file(file);
114 static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
116 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
117 return put_user(sbi->vol_id, user_attr);
120 long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
122 struct inode *inode = file_inode(filp);
123 u32 __user *user_attr = (u32 __user *)arg;
126 case FAT_IOCTL_GET_ATTRIBUTES:
127 return fat_ioctl_get_attributes(inode, user_attr);
128 case FAT_IOCTL_SET_ATTRIBUTES:
129 return fat_ioctl_set_attributes(filp, user_attr);
130 case FAT_IOCTL_GET_VOLUME_ID:
131 return fat_ioctl_get_volume_id(inode, user_attr);
133 return -ENOTTY; /* Inappropriate ioctl for device */
138 static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd,
142 return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
146 static int fat_file_release(struct inode *inode, struct file *filp)
148 if ((filp->f_mode & FMODE_WRITE) &&
149 MSDOS_SB(inode->i_sb)->options.flush) {
150 fat_flush_inodes(inode->i_sb, inode, NULL);
151 congestion_wait(BLK_RW_ASYNC, HZ/10);
156 int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
158 struct inode *inode = filp->f_mapping->host;
161 res = generic_file_fsync(filp, start, end, datasync);
162 err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
164 return res ? res : err;
168 const struct file_operations fat_file_operations = {
169 .llseek = generic_file_llseek,
170 .read_iter = generic_file_read_iter,
171 .write_iter = generic_file_write_iter,
172 .mmap = generic_file_mmap,
173 .release = fat_file_release,
174 .unlocked_ioctl = fat_generic_ioctl,
176 .compat_ioctl = fat_generic_compat_ioctl,
178 .fsync = fat_file_fsync,
179 .splice_read = generic_file_splice_read,
182 static int fat_cont_expand(struct inode *inode, loff_t size)
184 struct address_space *mapping = inode->i_mapping;
185 loff_t start = inode->i_size, count = size - inode->i_size;
188 err = generic_cont_expand_simple(inode, size);
192 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
193 mark_inode_dirty(inode);
194 if (IS_SYNC(inode)) {
198 * Opencode syncing since we don't have a file open to use
199 * standard fsync path.
201 err = filemap_fdatawrite_range(mapping, start,
203 err2 = sync_mapping_buffers(mapping);
206 err2 = write_inode_now(inode, 1);
210 err = filemap_fdatawait_range(mapping, start,
218 /* Free all clusters after the skip'th cluster. */
219 static int fat_free(struct inode *inode, int skip)
221 struct super_block *sb = inode->i_sb;
222 int err, wait, free_start, i_start, i_logstart;
224 if (MSDOS_I(inode)->i_start == 0)
227 fat_cache_inval_inode(inode);
229 wait = IS_DIRSYNC(inode);
230 i_start = free_start = MSDOS_I(inode)->i_start;
231 i_logstart = MSDOS_I(inode)->i_logstart;
233 /* First, we write the new file size. */
235 MSDOS_I(inode)->i_start = 0;
236 MSDOS_I(inode)->i_logstart = 0;
238 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
239 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
241 err = fat_sync_inode(inode);
243 MSDOS_I(inode)->i_start = i_start;
244 MSDOS_I(inode)->i_logstart = i_logstart;
248 mark_inode_dirty(inode);
250 /* Write a new EOF, and get the remaining cluster chain for freeing. */
252 struct fat_entry fatent;
253 int ret, fclus, dclus;
255 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
258 else if (ret == FAT_ENT_EOF)
261 fatent_init(&fatent);
262 ret = fat_ent_read(inode, &fatent, dclus);
263 if (ret == FAT_ENT_EOF) {
264 fatent_brelse(&fatent);
266 } else if (ret == FAT_ENT_FREE) {
268 "%s: invalid cluster chain (i_pos %lld)",
269 __func__, MSDOS_I(inode)->i_pos);
271 } else if (ret > 0) {
272 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
276 fatent_brelse(&fatent);
282 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
284 /* Freeing the remained cluster chain */
285 return fat_free_clusters(inode, free_start);
288 void fat_truncate_blocks(struct inode *inode, loff_t offset)
290 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
291 const unsigned int cluster_size = sbi->cluster_size;
295 * This protects against truncating a file bigger than it was then
296 * trying to write into the hole.
298 if (MSDOS_I(inode)->mmu_private > offset)
299 MSDOS_I(inode)->mmu_private = offset;
301 nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
303 fat_free(inode, nr_clusters);
304 fat_flush_inodes(inode->i_sb, inode, NULL);
307 int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
309 struct inode *inode = d_inode(dentry);
310 generic_fillattr(inode, stat);
311 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
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);
319 EXPORT_SYMBOL_GPL(fat_getattr);
321 static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
322 struct inode *inode, umode_t *mode_ptr)
327 * Note, the basic check is already done by a caller of
328 * (attr->ia_mode & ~FAT_VALID_MODE)
331 if (S_ISREG(inode->i_mode))
332 mask = sbi->options.fs_fmask;
334 mask = sbi->options.fs_dmask;
336 perm = *mode_ptr & ~(S_IFMT | mask);
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.
342 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
344 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
346 if (fat_mode_can_hold_ro(inode)) {
347 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
350 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
354 *mode_ptr &= S_IFMT | perm;
359 static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
361 umode_t allow_utime = sbi->options.allow_utime;
363 if (!uid_eq(current_fsuid(), inode->i_uid)) {
364 if (in_group_p(inode->i_gid))
366 if (allow_utime & MAY_WRITE)
370 /* use a default check */
374 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
375 /* valid file mode bits */
376 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
378 int fat_setattr(struct dentry *dentry, struct iattr *attr)
380 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
381 struct inode *inode = d_inode(dentry);
382 unsigned int ia_valid;
385 /* Check for setting the inode time. */
386 ia_valid = attr->ia_valid;
387 if (ia_valid & TIMES_SET_FLAGS) {
388 if (fat_allow_set_time(sbi, inode))
389 attr->ia_valid &= ~TIMES_SET_FLAGS;
392 error = inode_change_ok(inode, attr);
393 attr->ia_valid = ia_valid;
395 if (sbi->options.quiet)
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
406 if (attr->ia_valid & ATTR_SIZE) {
407 inode_dio_wait(inode);
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)
413 attr->ia_valid &= ~ATTR_SIZE;
417 if (((attr->ia_valid & ATTR_UID) &&
418 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
419 ((attr->ia_valid & ATTR_GID) &&
420 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
421 ((attr->ia_valid & ATTR_MODE) &&
422 (attr->ia_mode & ~FAT_VALID_MODE)))
426 if (sbi->options.quiet)
432 * We don't return -EPERM here. Yes, strange, but this is too
435 if (attr->ia_valid & ATTR_MODE) {
436 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
437 attr->ia_valid &= ~ATTR_MODE;
440 if (attr->ia_valid & ATTR_SIZE) {
441 error = fat_block_truncate_page(inode, attr->ia_size);
444 down_write(&MSDOS_I(inode)->truncate_lock);
445 truncate_setsize(inode, attr->ia_size);
446 fat_truncate_blocks(inode, attr->ia_size);
447 up_write(&MSDOS_I(inode)->truncate_lock);
450 setattr_copy(inode, attr);
451 mark_inode_dirty(inode);
455 EXPORT_SYMBOL_GPL(fat_setattr);
457 const struct inode_operations fat_file_inode_operations = {
458 .setattr = fat_setattr,
459 .getattr = fat_getattr,