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/mount.h>
12 #include <linux/time.h>
13 #include <linux/buffer_head.h>
14 #include <linux/writeback.h>
15 #include <linux/backing-dev.h>
16 #include <linux/blkdev.h>
17 #include <linux/fsnotify.h>
18 #include <linux/security.h>
21 static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
25 mutex_lock(&inode->i_mutex);
26 attr = fat_make_attrs(inode);
27 mutex_unlock(&inode->i_mutex);
29 return put_user(attr, user_attr);
32 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
34 struct inode *inode = file->f_path.dentry->d_inode;
35 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
36 int is_dir = S_ISDIR(inode->i_mode);
41 err = get_user(attr, user_attr);
45 mutex_lock(&inode->i_mutex);
46 err = mnt_want_write(file->f_path.mnt);
48 goto out_unlock_inode;
51 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
52 * prevents the user from turning us into a VFAT
53 * longname entry. Also, we obviously can't set
54 * any of the NTFS attributes in the high 24 bits.
56 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
57 /* Merge in ATTR_VOLUME and ATTR_DIR */
58 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
59 (is_dir ? ATTR_DIR : 0);
60 oldattr = fat_make_attrs(inode);
62 /* Equivalent to a chmod() */
63 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
64 ia.ia_ctime = current_fs_time(inode->i_sb);
66 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
68 ia.ia_mode = fat_make_mode(sbi, attr,
69 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
72 /* The root directory has no attributes */
73 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
78 if (sbi->options.sys_immutable &&
79 ((attr | oldattr) & ATTR_SYS) &&
80 !capable(CAP_LINUX_IMMUTABLE)) {
86 * The security check is questionable... We single
87 * out the RO attribute for checking by the security
88 * module, just because it maps to a file mode.
90 err = security_inode_setattr(file->f_path.dentry, &ia);
94 /* This MUST be done before doing anything irreversible... */
95 err = fat_setattr(file->f_path.dentry, &ia);
99 fsnotify_change(file->f_path.dentry, ia.ia_valid);
100 if (sbi->options.sys_immutable) {
102 inode->i_flags |= S_IMMUTABLE;
104 inode->i_flags &= S_IMMUTABLE;
107 fat_save_attrs(inode, attr);
108 mark_inode_dirty(inode);
110 mnt_drop_write(file->f_path.mnt);
112 mutex_unlock(&inode->i_mutex);
117 int fat_generic_ioctl(struct inode *inode, struct file *filp,
118 unsigned int cmd, unsigned long arg)
120 u32 __user *user_attr = (u32 __user *)arg;
123 case FAT_IOCTL_GET_ATTRIBUTES:
124 return fat_ioctl_get_attributes(inode, user_attr);
125 case FAT_IOCTL_SET_ATTRIBUTES:
126 return fat_ioctl_set_attributes(filp, user_attr);
128 return -ENOTTY; /* Inappropriate ioctl for device */
132 static int fat_file_release(struct inode *inode, struct file *filp)
134 if ((filp->f_mode & FMODE_WRITE) &&
135 MSDOS_SB(inode->i_sb)->options.flush) {
136 fat_flush_inodes(inode->i_sb, inode, NULL);
137 congestion_wait(WRITE, HZ/10);
142 const struct file_operations fat_file_operations = {
143 .llseek = generic_file_llseek,
144 .read = do_sync_read,
145 .write = do_sync_write,
146 .aio_read = generic_file_aio_read,
147 .aio_write = generic_file_aio_write,
148 .mmap = generic_file_mmap,
149 .release = fat_file_release,
150 .ioctl = fat_generic_ioctl,
152 .splice_read = generic_file_splice_read,
155 static int fat_cont_expand(struct inode *inode, loff_t size)
157 struct address_space *mapping = inode->i_mapping;
158 loff_t start = inode->i_size, count = size - inode->i_size;
161 err = generic_cont_expand_simple(inode, size);
165 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
166 mark_inode_dirty(inode);
168 err = sync_page_range_nolock(inode, mapping, start, count);
173 /* Free all clusters after the skip'th cluster. */
174 static int fat_free(struct inode *inode, int skip)
176 struct super_block *sb = inode->i_sb;
177 int err, wait, free_start, i_start, i_logstart;
179 if (MSDOS_I(inode)->i_start == 0)
182 fat_cache_inval_inode(inode);
184 wait = IS_DIRSYNC(inode);
185 i_start = free_start = MSDOS_I(inode)->i_start;
186 i_logstart = MSDOS_I(inode)->i_logstart;
188 /* First, we write the new file size. */
190 MSDOS_I(inode)->i_start = 0;
191 MSDOS_I(inode)->i_logstart = 0;
193 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
194 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
196 err = fat_sync_inode(inode);
198 MSDOS_I(inode)->i_start = i_start;
199 MSDOS_I(inode)->i_logstart = i_logstart;
203 mark_inode_dirty(inode);
205 /* Write a new EOF, and get the remaining cluster chain for freeing. */
207 struct fat_entry fatent;
208 int ret, fclus, dclus;
210 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
213 else if (ret == FAT_ENT_EOF)
216 fatent_init(&fatent);
217 ret = fat_ent_read(inode, &fatent, dclus);
218 if (ret == FAT_ENT_EOF) {
219 fatent_brelse(&fatent);
221 } else if (ret == FAT_ENT_FREE) {
223 "%s: invalid cluster chain (i_pos %lld)",
224 __func__, MSDOS_I(inode)->i_pos);
226 } else if (ret > 0) {
227 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
231 fatent_brelse(&fatent);
237 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
239 /* Freeing the remained cluster chain */
240 return fat_free_clusters(inode, free_start);
243 void fat_truncate(struct inode *inode)
245 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
246 const unsigned int cluster_size = sbi->cluster_size;
250 * This protects against truncating a file bigger than it was then
251 * trying to write into the hole.
253 if (MSDOS_I(inode)->mmu_private > inode->i_size)
254 MSDOS_I(inode)->mmu_private = inode->i_size;
256 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
258 fat_free(inode, nr_clusters);
259 fat_flush_inodes(inode->i_sb, inode, NULL);
262 int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
264 struct inode *inode = dentry->d_inode;
265 generic_fillattr(inode, stat);
266 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
269 EXPORT_SYMBOL_GPL(fat_getattr);
271 static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
272 struct inode *inode, umode_t *mode_ptr)
277 * Note, the basic check is already done by a caller of
278 * (attr->ia_mode & ~FAT_VALID_MODE)
281 if (S_ISREG(inode->i_mode))
282 mask = sbi->options.fs_fmask;
284 mask = sbi->options.fs_dmask;
286 perm = *mode_ptr & ~(S_IFMT | mask);
289 * Of the r and x bits, all (subject to umask) must be present. Of the
290 * w bits, either all (subject to umask) or none must be present.
292 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
294 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
296 if (fat_mode_can_hold_ro(inode)) {
297 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
300 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
304 *mode_ptr &= S_IFMT | perm;
309 static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
311 mode_t allow_utime = sbi->options.allow_utime;
313 if (current_fsuid() != inode->i_uid) {
314 if (in_group_p(inode->i_gid))
316 if (allow_utime & MAY_WRITE)
320 /* use a default check */
324 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
325 /* valid file mode bits */
326 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
328 int fat_setattr(struct dentry *dentry, struct iattr *attr)
330 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
331 struct inode *inode = dentry->d_inode;
332 unsigned int ia_valid;
336 * Expand the file. Since inode_setattr() updates ->i_size
337 * before calling the ->truncate(), but FAT needs to fill the
340 if (attr->ia_valid & ATTR_SIZE) {
341 if (attr->ia_size > inode->i_size) {
342 error = fat_cont_expand(inode, attr->ia_size);
343 if (error || attr->ia_valid == ATTR_SIZE)
345 attr->ia_valid &= ~ATTR_SIZE;
349 /* Check for setting the inode time. */
350 ia_valid = attr->ia_valid;
351 if (ia_valid & TIMES_SET_FLAGS) {
352 if (fat_allow_set_time(sbi, inode))
353 attr->ia_valid &= ~TIMES_SET_FLAGS;
356 error = inode_change_ok(inode, attr);
357 attr->ia_valid = ia_valid;
359 if (sbi->options.quiet)
364 if (((attr->ia_valid & ATTR_UID) &&
365 (attr->ia_uid != sbi->options.fs_uid)) ||
366 ((attr->ia_valid & ATTR_GID) &&
367 (attr->ia_gid != sbi->options.fs_gid)) ||
368 ((attr->ia_valid & ATTR_MODE) &&
369 (attr->ia_mode & ~FAT_VALID_MODE)))
373 if (sbi->options.quiet)
379 * We don't return -EPERM here. Yes, strange, but this is too
382 if (attr->ia_valid & ATTR_MODE) {
383 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
384 attr->ia_valid &= ~ATTR_MODE;
388 error = inode_setattr(inode, attr);
392 EXPORT_SYMBOL_GPL(fat_setattr);
394 const struct inode_operations fat_file_inode_operations = {
395 .truncate = fat_truncate,
396 .setattr = fat_setattr,
397 .getattr = fat_getattr,