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/msdos_fs.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/blkdev.h>
19 int fat_generic_ioctl(struct inode *inode, struct file *filp,
20 unsigned int cmd, unsigned long arg)
22 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
23 u32 __user *user_attr = (u32 __user *)arg;
26 case FAT_IOCTL_GET_ATTRIBUTES:
30 if (inode->i_ino == MSDOS_ROOT_INO)
33 attr = fat_attr(inode);
35 return put_user(attr, user_attr);
37 case FAT_IOCTL_SET_ATTRIBUTES:
40 int err, is_dir = S_ISDIR(inode->i_mode);
43 err = get_user(attr, user_attr);
47 mutex_lock(&inode->i_mutex);
49 err = mnt_want_write(filp->f_path.mnt);
51 goto up_no_drop_write;
54 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
55 * prevents the user from turning us into a VFAT
56 * longname entry. Also, we obviously can't set
57 * any of the NTFS attributes in the high 24 bits.
59 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
60 /* Merge in ATTR_VOLUME and ATTR_DIR */
61 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
62 (is_dir ? ATTR_DIR : 0);
63 oldattr = fat_attr(inode);
65 /* Equivalent to a chmod() */
66 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
68 ia.ia_mode = MSDOS_MKMODE(attr,
69 S_IRWXUGO & ~sbi->options.fs_dmask)
72 ia.ia_mode = MSDOS_MKMODE(attr,
73 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
74 & ~sbi->options.fs_fmask)
78 /* The root directory has no attributes */
79 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
84 if (sbi->options.sys_immutable) {
85 if ((attr | oldattr) & ATTR_SYS) {
86 if (!capable(CAP_LINUX_IMMUTABLE)) {
93 /* This MUST be done before doing anything irreversible... */
94 err = notify_change(filp->f_path.dentry, &ia);
98 if (sbi->options.sys_immutable) {
100 inode->i_flags |= S_IMMUTABLE;
102 inode->i_flags &= S_IMMUTABLE;
105 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
106 mark_inode_dirty(inode);
108 mnt_drop_write(filp->f_path.mnt);
110 mutex_unlock(&inode->i_mutex);
114 return -ENOTTY; /* Inappropriate ioctl for device */
118 static int fat_file_release(struct inode *inode, struct file *filp)
120 if ((filp->f_mode & FMODE_WRITE) &&
121 MSDOS_SB(inode->i_sb)->options.flush) {
122 fat_flush_inodes(inode->i_sb, inode, NULL);
123 congestion_wait(WRITE, HZ/10);
128 const struct file_operations fat_file_operations = {
129 .llseek = generic_file_llseek,
130 .read = do_sync_read,
131 .write = do_sync_write,
132 .aio_read = generic_file_aio_read,
133 .aio_write = generic_file_aio_write,
134 .mmap = generic_file_mmap,
135 .release = fat_file_release,
136 .ioctl = fat_generic_ioctl,
138 .splice_read = generic_file_splice_read,
141 static int fat_cont_expand(struct inode *inode, loff_t size)
143 struct address_space *mapping = inode->i_mapping;
144 loff_t start = inode->i_size, count = size - inode->i_size;
147 err = generic_cont_expand_simple(inode, size);
151 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
152 mark_inode_dirty(inode);
154 err = sync_page_range_nolock(inode, mapping, start, count);
159 /* Free all clusters after the skip'th cluster. */
160 static int fat_free(struct inode *inode, int skip)
162 struct super_block *sb = inode->i_sb;
163 int err, wait, free_start, i_start, i_logstart;
165 if (MSDOS_I(inode)->i_start == 0)
168 fat_cache_inval_inode(inode);
170 wait = IS_DIRSYNC(inode);
171 i_start = free_start = MSDOS_I(inode)->i_start;
172 i_logstart = MSDOS_I(inode)->i_logstart;
174 /* First, we write the new file size. */
176 MSDOS_I(inode)->i_start = 0;
177 MSDOS_I(inode)->i_logstart = 0;
179 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
180 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
182 err = fat_sync_inode(inode);
184 MSDOS_I(inode)->i_start = i_start;
185 MSDOS_I(inode)->i_logstart = i_logstart;
189 mark_inode_dirty(inode);
191 /* Write a new EOF, and get the remaining cluster chain for freeing. */
193 struct fat_entry fatent;
194 int ret, fclus, dclus;
196 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
199 else if (ret == FAT_ENT_EOF)
202 fatent_init(&fatent);
203 ret = fat_ent_read(inode, &fatent, dclus);
204 if (ret == FAT_ENT_EOF) {
205 fatent_brelse(&fatent);
207 } else if (ret == FAT_ENT_FREE) {
209 "%s: invalid cluster chain (i_pos %lld)",
210 __func__, MSDOS_I(inode)->i_pos);
212 } else if (ret > 0) {
213 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
217 fatent_brelse(&fatent);
223 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
225 /* Freeing the remained cluster chain */
226 return fat_free_clusters(inode, free_start);
229 void fat_truncate(struct inode *inode)
231 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
232 const unsigned int cluster_size = sbi->cluster_size;
236 * This protects against truncating a file bigger than it was then
237 * trying to write into the hole.
239 if (MSDOS_I(inode)->mmu_private > inode->i_size)
240 MSDOS_I(inode)->mmu_private = inode->i_size;
242 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
244 fat_free(inode, nr_clusters);
245 fat_flush_inodes(inode->i_sb, inode, NULL);
248 int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
250 struct inode *inode = dentry->d_inode;
251 generic_fillattr(inode, stat);
252 stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
255 EXPORT_SYMBOL_GPL(fat_getattr);
257 static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
258 struct inode *inode, umode_t *mode_ptr)
263 * Note, the basic check is already done by a caller of
264 * (attr->ia_mode & ~MSDOS_VALID_MODE)
267 if (S_ISREG(inode->i_mode))
268 mask = sbi->options.fs_fmask;
270 mask = sbi->options.fs_dmask;
272 perm = *mode_ptr & ~(S_IFMT | mask);
275 * Of the r and x bits, all (subject to umask) must be present. Of the
276 * w bits, either all (subject to umask) or none must be present.
278 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
280 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
283 *mode_ptr &= S_IFMT | perm;
288 static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
290 mode_t allow_utime = sbi->options.allow_utime;
292 if (current->fsuid != inode->i_uid) {
293 if (in_group_p(inode->i_gid))
295 if (allow_utime & MAY_WRITE)
299 /* use a default check */
303 int fat_setattr(struct dentry *dentry, struct iattr *attr)
305 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
306 struct inode *inode = dentry->d_inode;
308 unsigned int ia_valid;
311 * Expand the file. Since inode_setattr() updates ->i_size
312 * before calling the ->truncate(), but FAT needs to fill the
315 if (attr->ia_valid & ATTR_SIZE) {
316 if (attr->ia_size > inode->i_size) {
317 error = fat_cont_expand(inode, attr->ia_size);
318 if (error || attr->ia_valid == ATTR_SIZE)
320 attr->ia_valid &= ~ATTR_SIZE;
324 /* Check for setting the inode time. */
325 ia_valid = attr->ia_valid;
326 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
327 if (fat_allow_set_time(sbi, inode))
328 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
331 error = inode_change_ok(inode, attr);
332 attr->ia_valid = ia_valid;
334 if (sbi->options.quiet)
339 if (((attr->ia_valid & ATTR_UID) &&
340 (attr->ia_uid != sbi->options.fs_uid)) ||
341 ((attr->ia_valid & ATTR_GID) &&
342 (attr->ia_gid != sbi->options.fs_gid)) ||
343 ((attr->ia_valid & ATTR_MODE) &&
344 (attr->ia_mode & ~MSDOS_VALID_MODE)))
348 if (sbi->options.quiet)
354 * We don't return -EPERM here. Yes, strange, but this is too
357 if (attr->ia_valid & ATTR_MODE) {
358 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
359 attr->ia_valid &= ~ATTR_MODE;
362 error = inode_setattr(inode, attr);
366 EXPORT_SYMBOL_GPL(fat_setattr);
368 const struct inode_operations fat_file_inode_operations = {
369 .truncate = fat_truncate,
370 .setattr = fat_setattr,
371 .getattr = fat_getattr,