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/time.h>
12 #include <linux/msdos_fs.h>
13 #include <linux/smp_lock.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/blkdev.h>
18 int fat_generic_ioctl(struct inode *inode, struct file *filp,
19 unsigned int cmd, unsigned long arg)
21 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
22 u32 __user *user_attr = (u32 __user *)arg;
25 case FAT_IOCTL_GET_ATTRIBUTES:
29 if (inode->i_ino == MSDOS_ROOT_INO)
32 attr = fat_attr(inode);
34 return put_user(attr, user_attr);
36 case FAT_IOCTL_SET_ATTRIBUTES:
39 int err, is_dir = S_ISDIR(inode->i_mode);
42 err = get_user(attr, user_attr);
46 mutex_lock(&inode->i_mutex);
48 if (IS_RDONLY(inode)) {
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_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 mutex_unlock(&inode->i_mutex);
112 return -ENOTTY; /* Inappropriate ioctl for device */
116 static int fat_file_release(struct inode *inode, struct file *filp)
118 if ((filp->f_mode & FMODE_WRITE) &&
119 MSDOS_SB(inode->i_sb)->options.flush) {
120 fat_flush_inodes(inode->i_sb, inode, NULL);
121 blk_congestion_wait(WRITE, HZ/10);
126 const struct file_operations fat_file_operations = {
127 .llseek = generic_file_llseek,
128 .read = do_sync_read,
129 .write = do_sync_write,
130 .readv = generic_file_readv,
131 .writev = generic_file_writev,
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 .sendfile = generic_file_sendfile,
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 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
161 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
162 struct inode *inode = dentry->d_inode;
168 * Expand the file. Since inode_setattr() updates ->i_size
169 * before calling the ->truncate(), but FAT needs to fill the
172 if (attr->ia_valid & ATTR_SIZE) {
173 if (attr->ia_size > inode->i_size) {
174 error = fat_cont_expand(inode, attr->ia_size);
175 if (error || attr->ia_valid == ATTR_SIZE)
177 attr->ia_valid &= ~ATTR_SIZE;
181 error = inode_change_ok(inode, attr);
183 if (sbi->options.quiet)
187 if (((attr->ia_valid & ATTR_UID) &&
188 (attr->ia_uid != sbi->options.fs_uid)) ||
189 ((attr->ia_valid & ATTR_GID) &&
190 (attr->ia_gid != sbi->options.fs_gid)) ||
191 ((attr->ia_valid & ATTR_MODE) &&
192 (attr->ia_mode & ~MSDOS_VALID_MODE)))
196 if (sbi->options.quiet)
200 error = inode_setattr(inode, attr);
204 if (S_ISDIR(inode->i_mode))
205 mask = sbi->options.fs_dmask;
207 mask = sbi->options.fs_fmask;
208 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
214 EXPORT_SYMBOL_GPL(fat_notify_change);
216 /* Free all clusters after the skip'th cluster. */
217 static int fat_free(struct inode *inode, int skip)
219 struct super_block *sb = inode->i_sb;
220 int err, wait, free_start, i_start, i_logstart;
222 if (MSDOS_I(inode)->i_start == 0)
225 fat_cache_inval_inode(inode);
227 wait = IS_DIRSYNC(inode);
228 i_start = free_start = MSDOS_I(inode)->i_start;
229 i_logstart = MSDOS_I(inode)->i_logstart;
231 /* First, we write the new file size. */
233 MSDOS_I(inode)->i_start = 0;
234 MSDOS_I(inode)->i_logstart = 0;
236 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
237 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
239 err = fat_sync_inode(inode);
241 MSDOS_I(inode)->i_start = i_start;
242 MSDOS_I(inode)->i_logstart = i_logstart;
246 mark_inode_dirty(inode);
248 /* Write a new EOF, and get the remaining cluster chain for freeing. */
250 struct fat_entry fatent;
251 int ret, fclus, dclus;
253 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
256 else if (ret == FAT_ENT_EOF)
259 fatent_init(&fatent);
260 ret = fat_ent_read(inode, &fatent, dclus);
261 if (ret == FAT_ENT_EOF) {
262 fatent_brelse(&fatent);
264 } else if (ret == FAT_ENT_FREE) {
266 "%s: invalid cluster chain (i_pos %lld)",
267 __FUNCTION__, MSDOS_I(inode)->i_pos);
269 } else if (ret > 0) {
270 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
274 fatent_brelse(&fatent);
280 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
282 /* Freeing the remained cluster chain */
283 return fat_free_clusters(inode, free_start);
286 void fat_truncate(struct inode *inode)
288 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
289 const unsigned int cluster_size = sbi->cluster_size;
293 * This protects against truncating a file bigger than it was then
294 * trying to write into the hole.
296 if (MSDOS_I(inode)->mmu_private > inode->i_size)
297 MSDOS_I(inode)->mmu_private = inode->i_size;
299 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
302 fat_free(inode, nr_clusters);
304 fat_flush_inodes(inode->i_sb, inode, NULL);
307 struct inode_operations fat_file_inode_operations = {
308 .truncate = fat_truncate,
309 .setattr = fat_notify_change,