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/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 if (IS_RDONLY(inode
)) {
55 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
56 * prevents the user from turning us into a VFAT
57 * longname entry. Also, we obviously can't set
58 * any of the NTFS attributes in the high 24 bits.
60 attr
&= 0xff & ~(ATTR_VOLUME
| ATTR_DIR
);
61 /* Merge in ATTR_VOLUME and ATTR_DIR */
62 attr
|= (MSDOS_I(inode
)->i_attrs
& ATTR_VOLUME
) |
63 (is_dir
? ATTR_DIR
: 0);
64 oldattr
= fat_attr(inode
);
66 /* Equivalent to a chmod() */
67 ia
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
69 ia
.ia_mode
= MSDOS_MKMODE(attr
,
70 S_IRWXUGO
& ~sbi
->options
.fs_dmask
)
73 ia
.ia_mode
= MSDOS_MKMODE(attr
,
74 (S_IRUGO
| S_IWUGO
| (inode
->i_mode
& S_IXUGO
))
75 & ~sbi
->options
.fs_fmask
)
79 /* The root directory has no attributes */
80 if (inode
->i_ino
== MSDOS_ROOT_INO
&& attr
!= ATTR_DIR
) {
85 if (sbi
->options
.sys_immutable
) {
86 if ((attr
| oldattr
) & ATTR_SYS
) {
87 if (!capable(CAP_LINUX_IMMUTABLE
)) {
94 /* This MUST be done before doing anything irreversible... */
95 err
= notify_change(filp
->f_path
.dentry
, &ia
);
99 if (sbi
->options
.sys_immutable
) {
101 inode
->i_flags
|= S_IMMUTABLE
;
103 inode
->i_flags
&= S_IMMUTABLE
;
106 MSDOS_I(inode
)->i_attrs
= attr
& ATTR_UNUSED
;
107 mark_inode_dirty(inode
);
109 mutex_unlock(&inode
->i_mutex
);
113 return -ENOTTY
; /* Inappropriate ioctl for device */
117 static int fat_file_release(struct inode
*inode
, struct file
*filp
)
119 if ((filp
->f_mode
& FMODE_WRITE
) &&
120 MSDOS_SB(inode
->i_sb
)->options
.flush
) {
121 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
122 congestion_wait(WRITE
, HZ
/10);
127 const struct file_operations fat_file_operations
= {
128 .llseek
= generic_file_llseek
,
129 .read
= do_sync_read
,
130 .write
= do_sync_write
,
131 .aio_read
= generic_file_aio_read
,
132 .aio_write
= generic_file_aio_write
,
133 .mmap
= generic_file_mmap
,
134 .release
= fat_file_release
,
135 .ioctl
= fat_generic_ioctl
,
137 .splice_read
= generic_file_splice_read
,
140 static int fat_cont_expand(struct inode
*inode
, loff_t size
)
142 struct address_space
*mapping
= inode
->i_mapping
;
143 loff_t start
= inode
->i_size
, count
= size
- inode
->i_size
;
146 err
= generic_cont_expand_simple(inode
, size
);
150 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
151 mark_inode_dirty(inode
);
153 err
= sync_page_range_nolock(inode
, mapping
, start
, count
);
158 static int check_mode(const struct msdos_sb_info
*sbi
, mode_t mode
)
160 mode_t req
= mode
& ~S_IFMT
;
163 * Of the r and x bits, all (subject to umask) must be present. Of the
164 * w bits, either all (subject to umask) or none must be present.
168 req
&= ~sbi
->options
.fs_fmask
;
170 if ((req
& (S_IRUGO
| S_IXUGO
)) !=
171 ((S_IRUGO
| S_IXUGO
) & ~sbi
->options
.fs_fmask
))
174 if ((req
& S_IWUGO
) != 0 &&
175 (req
& S_IWUGO
) != (S_IWUGO
& ~sbi
->options
.fs_fmask
))
177 } else if (S_ISDIR(mode
)) {
178 req
&= ~sbi
->options
.fs_dmask
;
180 if ((req
& (S_IRUGO
| S_IXUGO
)) !=
181 ((S_IRUGO
| S_IXUGO
) & ~sbi
->options
.fs_dmask
))
184 if ((req
& S_IWUGO
) != 0 &&
185 (req
& S_IWUGO
) != (S_IWUGO
& ~sbi
->options
.fs_dmask
))
194 int fat_notify_change(struct dentry
*dentry
, struct iattr
*attr
)
196 struct msdos_sb_info
*sbi
= MSDOS_SB(dentry
->d_sb
);
197 struct inode
*inode
= dentry
->d_inode
;
203 * Expand the file. Since inode_setattr() updates ->i_size
204 * before calling the ->truncate(), but FAT needs to fill the
207 if (attr
->ia_valid
& ATTR_SIZE
) {
208 if (attr
->ia_size
> inode
->i_size
) {
209 error
= fat_cont_expand(inode
, attr
->ia_size
);
210 if (error
|| attr
->ia_valid
== ATTR_SIZE
)
212 attr
->ia_valid
&= ~ATTR_SIZE
;
216 error
= inode_change_ok(inode
, attr
);
218 if (sbi
->options
.quiet
)
222 if (((attr
->ia_valid
& ATTR_UID
) &&
223 (attr
->ia_uid
!= sbi
->options
.fs_uid
)) ||
224 ((attr
->ia_valid
& ATTR_GID
) &&
225 (attr
->ia_gid
!= sbi
->options
.fs_gid
)))
229 if (sbi
->options
.quiet
)
234 if (attr
->ia_valid
& ATTR_MODE
) {
235 error
= check_mode(sbi
, attr
->ia_mode
);
236 if (error
!= 0 && !sbi
->options
.quiet
)
240 error
= inode_setattr(inode
, attr
);
244 if (S_ISDIR(inode
->i_mode
))
245 mask
= sbi
->options
.fs_dmask
;
247 mask
= sbi
->options
.fs_fmask
;
248 inode
->i_mode
&= S_IFMT
| (S_IRWXUGO
& ~mask
);
254 EXPORT_SYMBOL_GPL(fat_notify_change
);
256 /* Free all clusters after the skip'th cluster. */
257 static int fat_free(struct inode
*inode
, int skip
)
259 struct super_block
*sb
= inode
->i_sb
;
260 int err
, wait
, free_start
, i_start
, i_logstart
;
262 if (MSDOS_I(inode
)->i_start
== 0)
265 fat_cache_inval_inode(inode
);
267 wait
= IS_DIRSYNC(inode
);
268 i_start
= free_start
= MSDOS_I(inode
)->i_start
;
269 i_logstart
= MSDOS_I(inode
)->i_logstart
;
271 /* First, we write the new file size. */
273 MSDOS_I(inode
)->i_start
= 0;
274 MSDOS_I(inode
)->i_logstart
= 0;
276 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
277 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
279 err
= fat_sync_inode(inode
);
281 MSDOS_I(inode
)->i_start
= i_start
;
282 MSDOS_I(inode
)->i_logstart
= i_logstart
;
286 mark_inode_dirty(inode
);
288 /* Write a new EOF, and get the remaining cluster chain for freeing. */
290 struct fat_entry fatent
;
291 int ret
, fclus
, dclus
;
293 ret
= fat_get_cluster(inode
, skip
- 1, &fclus
, &dclus
);
296 else if (ret
== FAT_ENT_EOF
)
299 fatent_init(&fatent
);
300 ret
= fat_ent_read(inode
, &fatent
, dclus
);
301 if (ret
== FAT_ENT_EOF
) {
302 fatent_brelse(&fatent
);
304 } else if (ret
== FAT_ENT_FREE
) {
306 "%s: invalid cluster chain (i_pos %lld)",
307 __FUNCTION__
, MSDOS_I(inode
)->i_pos
);
309 } else if (ret
> 0) {
310 err
= fat_ent_write(inode
, &fatent
, FAT_ENT_EOF
, wait
);
314 fatent_brelse(&fatent
);
320 inode
->i_blocks
= skip
<< (MSDOS_SB(sb
)->cluster_bits
- 9);
322 /* Freeing the remained cluster chain */
323 return fat_free_clusters(inode
, free_start
);
326 void fat_truncate(struct inode
*inode
)
328 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
329 const unsigned int cluster_size
= sbi
->cluster_size
;
333 * This protects against truncating a file bigger than it was then
334 * trying to write into the hole.
336 if (MSDOS_I(inode
)->mmu_private
> inode
->i_size
)
337 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
339 nr_clusters
= (inode
->i_size
+ (cluster_size
- 1)) >> sbi
->cluster_bits
;
342 fat_free(inode
, nr_clusters
);
344 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
347 int fat_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
349 struct inode
*inode
= dentry
->d_inode
;
350 generic_fillattr(inode
, stat
);
351 stat
->blksize
= MSDOS_SB(inode
->i_sb
)->cluster_size
;
354 EXPORT_SYMBOL_GPL(fat_getattr
);
356 const struct inode_operations fat_file_inode_operations
= {
357 .truncate
= fat_truncate
,
358 .setattr
= fat_notify_change
,
359 .getattr
= fat_getattr
,