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>
17 int fat_generic_ioctl(struct inode *inode, struct file *filp,
18 unsigned int cmd, unsigned long arg)
20 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
21 u32 __user *user_attr = (u32 __user *)arg;
24 case FAT_IOCTL_GET_ATTRIBUTES:
28 if (inode->i_ino == MSDOS_ROOT_INO)
31 attr = fat_attr(inode);
33 return put_user(attr, user_attr);
35 case FAT_IOCTL_SET_ATTRIBUTES:
38 int err, is_dir = S_ISDIR(inode->i_mode);
41 err = get_user(attr, user_attr);
45 mutex_lock(&inode->i_mutex);
47 if (IS_RDONLY(inode)) {
53 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
54 * prevents the user from turning us into a VFAT
55 * longname entry. Also, we obviously can't set
56 * any of the NTFS attributes in the high 24 bits.
58 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
59 /* Merge in ATTR_VOLUME and ATTR_DIR */
60 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
61 (is_dir ? ATTR_DIR : 0);
62 oldattr = fat_attr(inode);
64 /* Equivalent to a chmod() */
65 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
67 ia.ia_mode = MSDOS_MKMODE(attr,
68 S_IRWXUGO & ~sbi->options.fs_dmask)
71 ia.ia_mode = MSDOS_MKMODE(attr,
72 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
73 & ~sbi->options.fs_fmask)
77 /* The root directory has no attributes */
78 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
83 if (sbi->options.sys_immutable) {
84 if ((attr | oldattr) & ATTR_SYS) {
85 if (!capable(CAP_LINUX_IMMUTABLE)) {
92 /* This MUST be done before doing anything irreversible... */
93 err = notify_change(filp->f_dentry, &ia);
97 if (sbi->options.sys_immutable) {
99 inode->i_flags |= S_IMMUTABLE;
101 inode->i_flags &= S_IMMUTABLE;
104 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
105 mark_inode_dirty(inode);
107 mutex_unlock(&inode->i_mutex);
111 return -ENOTTY; /* Inappropriate ioctl for device */
115 const struct file_operations fat_file_operations = {
116 .llseek = generic_file_llseek,
117 .read = do_sync_read,
118 .write = do_sync_write,
119 .readv = generic_file_readv,
120 .writev = generic_file_writev,
121 .aio_read = generic_file_aio_read,
122 .aio_write = generic_file_aio_write,
123 .mmap = generic_file_mmap,
124 .ioctl = fat_generic_ioctl,
126 .sendfile = generic_file_sendfile,
129 static int fat_cont_expand(struct inode *inode, loff_t size)
131 struct address_space *mapping = inode->i_mapping;
132 loff_t start = inode->i_size, count = size - inode->i_size;
135 err = generic_cont_expand_simple(inode, size);
139 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
140 mark_inode_dirty(inode);
142 err = sync_page_range_nolock(inode, mapping, start, count);
147 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
149 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
150 struct inode *inode = dentry->d_inode;
156 * Expand the file. Since inode_setattr() updates ->i_size
157 * before calling the ->truncate(), but FAT needs to fill the
160 if (attr->ia_valid & ATTR_SIZE) {
161 if (attr->ia_size > inode->i_size) {
162 error = fat_cont_expand(inode, attr->ia_size);
163 if (error || attr->ia_valid == ATTR_SIZE)
165 attr->ia_valid &= ~ATTR_SIZE;
169 error = inode_change_ok(inode, attr);
171 if (sbi->options.quiet)
175 if (((attr->ia_valid & ATTR_UID) &&
176 (attr->ia_uid != sbi->options.fs_uid)) ||
177 ((attr->ia_valid & ATTR_GID) &&
178 (attr->ia_gid != sbi->options.fs_gid)) ||
179 ((attr->ia_valid & ATTR_MODE) &&
180 (attr->ia_mode & ~MSDOS_VALID_MODE)))
184 if (sbi->options.quiet)
188 error = inode_setattr(inode, attr);
192 if (S_ISDIR(inode->i_mode))
193 mask = sbi->options.fs_dmask;
195 mask = sbi->options.fs_fmask;
196 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
202 EXPORT_SYMBOL_GPL(fat_notify_change);
204 /* Free all clusters after the skip'th cluster. */
205 static int fat_free(struct inode *inode, int skip)
207 struct super_block *sb = inode->i_sb;
208 int err, wait, free_start, i_start, i_logstart;
210 if (MSDOS_I(inode)->i_start == 0)
213 fat_cache_inval_inode(inode);
215 wait = IS_DIRSYNC(inode);
216 i_start = free_start = MSDOS_I(inode)->i_start;
217 i_logstart = MSDOS_I(inode)->i_logstart;
219 /* First, we write the new file size. */
221 MSDOS_I(inode)->i_start = 0;
222 MSDOS_I(inode)->i_logstart = 0;
224 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
225 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
227 err = fat_sync_inode(inode);
229 MSDOS_I(inode)->i_start = i_start;
230 MSDOS_I(inode)->i_logstart = i_logstart;
234 mark_inode_dirty(inode);
236 /* Write a new EOF, and get the remaining cluster chain for freeing. */
238 struct fat_entry fatent;
239 int ret, fclus, dclus;
241 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
244 else if (ret == FAT_ENT_EOF)
247 fatent_init(&fatent);
248 ret = fat_ent_read(inode, &fatent, dclus);
249 if (ret == FAT_ENT_EOF) {
250 fatent_brelse(&fatent);
252 } else if (ret == FAT_ENT_FREE) {
254 "%s: invalid cluster chain (i_pos %lld)",
255 __FUNCTION__, MSDOS_I(inode)->i_pos);
257 } else if (ret > 0) {
258 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
262 fatent_brelse(&fatent);
268 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
270 /* Freeing the remained cluster chain */
271 return fat_free_clusters(inode, free_start);
274 void fat_truncate(struct inode *inode)
276 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
277 const unsigned int cluster_size = sbi->cluster_size;
281 * This protects against truncating a file bigger than it was then
282 * trying to write into the hole.
284 if (MSDOS_I(inode)->mmu_private > inode->i_size)
285 MSDOS_I(inode)->mmu_private = inode->i_size;
287 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
290 fat_free(inode, nr_clusters);
294 struct inode_operations fat_file_inode_operations = {
295 .truncate = fat_truncate,
296 .setattr = fat_notify_change,