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>
15 int fat_generic_ioctl(struct inode *inode, struct file *filp,
16 unsigned int cmd, unsigned long arg)
18 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
19 u32 __user *user_attr = (u32 __user *)arg;
22 case FAT_IOCTL_GET_ATTRIBUTES:
26 if (inode->i_ino == MSDOS_ROOT_INO)
29 attr = fat_attr(inode);
31 return put_user(attr, user_attr);
33 case FAT_IOCTL_SET_ATTRIBUTES:
36 int err, is_dir = S_ISDIR(inode->i_mode);
39 err = get_user(attr, user_attr);
45 if (IS_RDONLY(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_attr(inode);
62 /* Equivalent to a chmod() */
63 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
65 ia.ia_mode = MSDOS_MKMODE(attr,
66 S_IRWXUGO & ~sbi->options.fs_dmask)
69 ia.ia_mode = MSDOS_MKMODE(attr,
70 (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
71 & ~sbi->options.fs_fmask)
75 /* The root directory has no attributes */
76 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
81 if (sbi->options.sys_immutable) {
82 if ((attr | oldattr) & ATTR_SYS) {
83 if (!capable(CAP_LINUX_IMMUTABLE)) {
90 /* This MUST be done before doing anything irreversible... */
91 err = notify_change(filp->f_dentry, &ia);
95 if (sbi->options.sys_immutable) {
97 inode->i_flags |= S_IMMUTABLE;
99 inode->i_flags &= S_IMMUTABLE;
102 MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
103 mark_inode_dirty(inode);
109 return -ENOTTY; /* Inappropriate ioctl for device */
113 struct file_operations fat_file_operations = {
114 .llseek = generic_file_llseek,
115 .read = do_sync_read,
116 .write = do_sync_write,
117 .readv = generic_file_readv,
118 .writev = generic_file_writev,
119 .aio_read = generic_file_aio_read,
120 .aio_write = generic_file_aio_write,
121 .mmap = generic_file_mmap,
122 .ioctl = fat_generic_ioctl,
124 .sendfile = generic_file_sendfile,
127 int fat_notify_change(struct dentry *dentry, struct iattr *attr)
129 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
130 struct inode *inode = dentry->d_inode;
135 /* FAT cannot truncate to a longer file */
136 if (attr->ia_valid & ATTR_SIZE) {
137 if (attr->ia_size > inode->i_size) {
143 error = inode_change_ok(inode, attr);
145 if (sbi->options.quiet)
149 if (((attr->ia_valid & ATTR_UID) &&
150 (attr->ia_uid != sbi->options.fs_uid)) ||
151 ((attr->ia_valid & ATTR_GID) &&
152 (attr->ia_gid != sbi->options.fs_gid)) ||
153 ((attr->ia_valid & ATTR_MODE) &&
154 (attr->ia_mode & ~MSDOS_VALID_MODE)))
158 if (sbi->options.quiet)
162 error = inode_setattr(inode, attr);
166 if (S_ISDIR(inode->i_mode))
167 mask = sbi->options.fs_dmask;
169 mask = sbi->options.fs_fmask;
170 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
176 EXPORT_SYMBOL(fat_notify_change);
178 /* Free all clusters after the skip'th cluster. */
179 static int fat_free(struct inode *inode, int skip)
181 struct super_block *sb = inode->i_sb;
182 int err, wait, free_start, i_start, i_logstart;
184 if (MSDOS_I(inode)->i_start == 0)
188 * Write a new EOF, and get the remaining cluster chain for freeing.
190 wait = IS_DIRSYNC(inode);
192 struct fat_entry fatent;
193 int ret, fclus, dclus;
195 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
198 else if (ret == FAT_ENT_EOF)
201 fatent_init(&fatent);
202 ret = fat_ent_read(inode, &fatent, dclus);
203 if (ret == FAT_ENT_EOF) {
204 fatent_brelse(&fatent);
206 } else if (ret == FAT_ENT_FREE) {
208 "%s: invalid cluster chain (i_pos %lld)",
209 __FUNCTION__, MSDOS_I(inode)->i_pos);
211 } else if (ret > 0) {
212 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
216 fatent_brelse(&fatent);
221 i_start = i_logstart = 0;
222 fat_cache_inval_inode(inode);
224 fat_cache_inval_inode(inode);
226 i_start = free_start = MSDOS_I(inode)->i_start;
227 i_logstart = MSDOS_I(inode)->i_logstart;
228 MSDOS_I(inode)->i_start = 0;
229 MSDOS_I(inode)->i_logstart = 0;
231 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
232 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
234 err = fat_sync_inode(inode);
238 mark_inode_dirty(inode);
239 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
241 /* Freeing the remained cluster chain */
242 return fat_free_clusters(inode, free_start);
246 MSDOS_I(inode)->i_start = i_start;
247 MSDOS_I(inode)->i_logstart = i_logstart;
252 void fat_truncate(struct inode *inode)
254 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
255 const unsigned int cluster_size = sbi->cluster_size;
259 * This protects against truncating a file bigger than it was then
260 * trying to write into the hole.
262 if (MSDOS_I(inode)->mmu_private > inode->i_size)
263 MSDOS_I(inode)->mmu_private = inode->i_size;
265 nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
268 fat_free(inode, nr_clusters);
272 struct inode_operations fat_file_inode_operations = {
273 .truncate = fat_truncate,
274 .setattr = fat_notify_change,