4 * Written 1992,1993 by Werner Almesberger
6 * regular file handling primitives for fat-based filesystems
9 #include <linux/module.h>
10 #include <linux/time.h>
11 #include <linux/msdos_fs.h>
12 #include <linux/smp_lock.h>
13 #include <linux/buffer_head.h>
14 #include <linux/writeback.h>
16 int fat_generic_ioctl(struct inode *inode, struct file *filp,
17 unsigned int cmd, unsigned long arg)
19 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
20 u32 __user *user_attr = (u32 __user *)arg;
23 case FAT_IOCTL_GET_ATTRIBUTES:
27 if (inode->i_ino == MSDOS_ROOT_INO)
30 attr = fat_attr(inode);
32 return put_user(attr, user_attr);
34 case FAT_IOCTL_SET_ATTRIBUTES:
37 int err, is_dir = S_ISDIR(inode->i_mode);
40 err = get_user(attr, user_attr);
44 mutex_lock(&inode->i_mutex);
46 if (IS_RDONLY(inode)) {
52 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
53 * prevents the user from turning us into a VFAT
54 * longname entry. Also, we obviously can't set
55 * any of the NTFS attributes in the high 24 bits.
57 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
58 /* Merge in ATTR_VOLUME and ATTR_DIR */
59 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
60 (is_dir ? ATTR_DIR : 0);
61 oldattr = fat_attr(inode);
63 /* Equivalent to a chmod() */
64 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
66 ia.ia_mode = MSDOS_MKMODE(attr,
67 S_IRWXUGO & ~sbi->options.fs_dmask)
70 ia.ia_mode = MSDOS_MKMODE(attr,
71 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
72 & ~sbi->options.fs_fmask)
76 /* The root directory has no attributes */
77 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
82 if (sbi->options.sys_immutable) {
83 if ((attr | oldattr) & ATTR_SYS) {
84 if (!capable(CAP_LINUX_IMMUTABLE)) {
91 /* This MUST be done before doing anything irreversible... */
92 err = notify_change(filp->f_dentry, &ia);
96 if (sbi->options.sys_immutable) {
98 inode->i_flags |= S_IMMUTABLE;
100 inode->i_flags &= S_IMMUTABLE;
103 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
104 mark_inode_dirty(inode);
106 mutex_unlock(&inode->i_mutex);
110 return -ENOTTY; /* Inappropriate ioctl for device */
114 struct file_operations fat_file_operations = {
115 .llseek = generic_file_llseek,
116 .read = do_sync_read,
117 .write = do_sync_write,
118 .readv = generic_file_readv,
119 .writev = generic_file_writev,
120 .aio_read = generic_file_aio_read,
121 .aio_write = generic_file_aio_write,
122 .mmap = generic_file_mmap,
123 .ioctl = fat_generic_ioctl,
125 .sendfile = generic_file_sendfile,
128 static int fat_cont_expand(struct inode *inode, loff_t size)
130 struct address_space *mapping = inode->i_mapping;
131 loff_t start = inode->i_size, count = size - inode->i_size;
134 err = generic_cont_expand_simple(inode, size);
138 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
139 mark_inode_dirty(inode);
141 err = sync_page_range_nolock(inode, mapping, start, count);
146 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
148 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
149 struct inode *inode = dentry->d_inode;
155 * Expand the file. Since inode_setattr() updates ->i_size
156 * before calling the ->truncate(), but FAT needs to fill the
159 if (attr->ia_valid & ATTR_SIZE) {
160 if (attr->ia_size > inode->i_size) {
161 error = fat_cont_expand(inode, attr->ia_size);
162 if (error || attr->ia_valid == ATTR_SIZE)
164 attr->ia_valid &= ~ATTR_SIZE;
168 error = inode_change_ok(inode, attr);
170 if (sbi->options.quiet)
174 if (((attr->ia_valid & ATTR_UID) &&
175 (attr->ia_uid != sbi->options.fs_uid)) ||
176 ((attr->ia_valid & ATTR_GID) &&
177 (attr->ia_gid != sbi->options.fs_gid)) ||
178 ((attr->ia_valid & ATTR_MODE) &&
179 (attr->ia_mode & ~MSDOS_VALID_MODE)))
183 if (sbi->options.quiet)
187 error = inode_setattr(inode, attr);
191 if (S_ISDIR(inode->i_mode))
192 mask = sbi->options.fs_dmask;
194 mask = sbi->options.fs_fmask;
195 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
201 EXPORT_SYMBOL_GPL(fat_notify_change);
203 /* Free all clusters after the skip'th cluster. */
204 static int fat_free(struct inode *inode, int skip)
206 struct super_block *sb = inode->i_sb;
207 int err, wait, free_start, i_start, i_logstart;
209 if (MSDOS_I(inode)->i_start == 0)
213 * Write a new EOF, and get the remaining cluster chain for freeing.
215 wait = IS_DIRSYNC(inode);
217 struct fat_entry fatent;
218 int ret, fclus, dclus;
220 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
223 else if (ret == FAT_ENT_EOF)
226 fatent_init(&fatent);
227 ret = fat_ent_read(inode, &fatent, dclus);
228 if (ret == FAT_ENT_EOF) {
229 fatent_brelse(&fatent);
231 } else if (ret == FAT_ENT_FREE) {
233 "%s: invalid cluster chain (i_pos %lld)",
234 __FUNCTION__, MSDOS_I(inode)->i_pos);
236 } else if (ret > 0) {
237 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
241 fatent_brelse(&fatent);
246 i_start = i_logstart = 0;
247 fat_cache_inval_inode(inode);
249 fat_cache_inval_inode(inode);
251 i_start = free_start = MSDOS_I(inode)->i_start;
252 i_logstart = MSDOS_I(inode)->i_logstart;
253 MSDOS_I(inode)->i_start = 0;
254 MSDOS_I(inode)->i_logstart = 0;
256 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
257 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
259 err = fat_sync_inode(inode);
263 mark_inode_dirty(inode);
264 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
266 /* Freeing the remained cluster chain */
267 return fat_free_clusters(inode, free_start);
271 MSDOS_I(inode)->i_start = i_start;
272 MSDOS_I(inode)->i_logstart = i_logstart;
277 void fat_truncate(struct inode *inode)
279 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
280 const unsigned int cluster_size = sbi->cluster_size;
284 * This protects against truncating a file bigger than it was then
285 * trying to write into the hole.
287 if (MSDOS_I(inode)->mmu_private > inode->i_size)
288 MSDOS_I(inode)->mmu_private = inode->i_size;
290 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
293 fat_free(inode, nr_clusters);
297 struct inode_operations fat_file_inode_operations = {
298 .truncate = fat_truncate,
299 .setattr = fat_notify_change,