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/mount.h>
12 #include <linux/time.h>
13 #include <linux/buffer_head.h>
14 #include <linux/writeback.h>
15 #include <linux/backing-dev.h>
16 #include <linux/blkdev.h>
17 #include <linux/fsnotify.h>
18 #include <linux/security.h>
21 int fat_generic_ioctl(struct inode
*inode
, struct file
*filp
,
22 unsigned int cmd
, unsigned long arg
)
24 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
25 u32 __user
*user_attr
= (u32 __user
*)arg
;
28 case FAT_IOCTL_GET_ATTRIBUTES
:
32 mutex_lock(&inode
->i_mutex
);
33 attr
= fat_make_attrs(inode
);
34 mutex_unlock(&inode
->i_mutex
);
36 return put_user(attr
, user_attr
);
38 case FAT_IOCTL_SET_ATTRIBUTES
:
41 int err
, is_dir
= S_ISDIR(inode
->i_mode
);
44 err
= get_user(attr
, user_attr
);
48 mutex_lock(&inode
->i_mutex
);
50 err
= mnt_want_write(filp
->f_path
.mnt
);
52 goto up_no_drop_write
;
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_make_attrs(inode
);
66 /* Equivalent to a chmod() */
67 ia
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
68 ia
.ia_ctime
= current_fs_time(inode
->i_sb
);
70 ia
.ia_mode
= fat_make_mode(sbi
, attr
, S_IRWXUGO
);
72 ia
.ia_mode
= fat_make_mode(sbi
, attr
,
73 S_IRUGO
| S_IWUGO
| (inode
->i_mode
& S_IXUGO
));
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
)) {
92 * The security check is questionable... We single
93 * out the RO attribute for checking by the security
94 * module, just because it maps to a file mode.
96 err
= security_inode_setattr(filp
->f_path
.dentry
, &ia
);
100 /* This MUST be done before doing anything irreversible... */
101 err
= fat_setattr(filp
->f_path
.dentry
, &ia
);
105 fsnotify_change(filp
->f_path
.dentry
, ia
.ia_valid
);
106 if (sbi
->options
.sys_immutable
) {
108 inode
->i_flags
|= S_IMMUTABLE
;
110 inode
->i_flags
&= S_IMMUTABLE
;
113 fat_save_attrs(inode
, attr
);
114 mark_inode_dirty(inode
);
116 mnt_drop_write(filp
->f_path
.mnt
);
118 mutex_unlock(&inode
->i_mutex
);
122 return -ENOTTY
; /* Inappropriate ioctl for device */
126 static int fat_file_release(struct inode
*inode
, struct file
*filp
)
128 if ((filp
->f_mode
& FMODE_WRITE
) &&
129 MSDOS_SB(inode
->i_sb
)->options
.flush
) {
130 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
131 congestion_wait(WRITE
, HZ
/10);
136 int fat_file_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
138 struct inode
*inode
= dentry
->d_inode
;
141 res
= simple_fsync(filp
, dentry
, datasync
);
142 err
= sync_mapping_buffers(MSDOS_SB(inode
->i_sb
)->fat_inode
->i_mapping
);
144 return res
? res
: err
;
148 const struct file_operations fat_file_operations
= {
149 .llseek
= generic_file_llseek
,
150 .read
= do_sync_read
,
151 .write
= do_sync_write
,
152 .aio_read
= generic_file_aio_read
,
153 .aio_write
= generic_file_aio_write
,
154 .mmap
= generic_file_mmap
,
155 .release
= fat_file_release
,
156 .ioctl
= fat_generic_ioctl
,
157 .fsync
= fat_file_fsync
,
158 .splice_read
= generic_file_splice_read
,
161 static int fat_cont_expand(struct inode
*inode
, loff_t size
)
163 struct address_space
*mapping
= inode
->i_mapping
;
164 loff_t start
= inode
->i_size
, count
= size
- inode
->i_size
;
167 err
= generic_cont_expand_simple(inode
, size
);
171 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
172 mark_inode_dirty(inode
);
174 err
= sync_page_range_nolock(inode
, mapping
, start
, count
);
179 /* Free all clusters after the skip'th cluster. */
180 static int fat_free(struct inode
*inode
, int skip
)
182 struct super_block
*sb
= inode
->i_sb
;
183 int err
, wait
, free_start
, i_start
, i_logstart
;
185 if (MSDOS_I(inode
)->i_start
== 0)
188 fat_cache_inval_inode(inode
);
190 wait
= IS_DIRSYNC(inode
);
191 i_start
= free_start
= MSDOS_I(inode
)->i_start
;
192 i_logstart
= MSDOS_I(inode
)->i_logstart
;
194 /* First, we write the new file size. */
196 MSDOS_I(inode
)->i_start
= 0;
197 MSDOS_I(inode
)->i_logstart
= 0;
199 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
200 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
202 err
= fat_sync_inode(inode
);
204 MSDOS_I(inode
)->i_start
= i_start
;
205 MSDOS_I(inode
)->i_logstart
= i_logstart
;
209 mark_inode_dirty(inode
);
211 /* Write a new EOF, and get the remaining cluster chain for freeing. */
213 struct fat_entry fatent
;
214 int ret
, fclus
, dclus
;
216 ret
= fat_get_cluster(inode
, skip
- 1, &fclus
, &dclus
);
219 else if (ret
== FAT_ENT_EOF
)
222 fatent_init(&fatent
);
223 ret
= fat_ent_read(inode
, &fatent
, dclus
);
224 if (ret
== FAT_ENT_EOF
) {
225 fatent_brelse(&fatent
);
227 } else if (ret
== FAT_ENT_FREE
) {
229 "%s: invalid cluster chain (i_pos %lld)",
230 __func__
, MSDOS_I(inode
)->i_pos
);
232 } else if (ret
> 0) {
233 err
= fat_ent_write(inode
, &fatent
, FAT_ENT_EOF
, wait
);
237 fatent_brelse(&fatent
);
243 inode
->i_blocks
= skip
<< (MSDOS_SB(sb
)->cluster_bits
- 9);
245 /* Freeing the remained cluster chain */
246 return fat_free_clusters(inode
, free_start
);
249 void fat_truncate(struct inode
*inode
)
251 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
252 const unsigned int cluster_size
= sbi
->cluster_size
;
256 * This protects against truncating a file bigger than it was then
257 * trying to write into the hole.
259 if (MSDOS_I(inode
)->mmu_private
> inode
->i_size
)
260 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
262 nr_clusters
= (inode
->i_size
+ (cluster_size
- 1)) >> sbi
->cluster_bits
;
264 fat_free(inode
, nr_clusters
);
265 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
268 int fat_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
270 struct inode
*inode
= dentry
->d_inode
;
271 generic_fillattr(inode
, stat
);
272 stat
->blksize
= MSDOS_SB(inode
->i_sb
)->cluster_size
;
275 EXPORT_SYMBOL_GPL(fat_getattr
);
277 static int fat_sanitize_mode(const struct msdos_sb_info
*sbi
,
278 struct inode
*inode
, umode_t
*mode_ptr
)
283 * Note, the basic check is already done by a caller of
284 * (attr->ia_mode & ~FAT_VALID_MODE)
287 if (S_ISREG(inode
->i_mode
))
288 mask
= sbi
->options
.fs_fmask
;
290 mask
= sbi
->options
.fs_dmask
;
292 perm
= *mode_ptr
& ~(S_IFMT
| mask
);
295 * Of the r and x bits, all (subject to umask) must be present. Of the
296 * w bits, either all (subject to umask) or none must be present.
298 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
300 if ((perm
& (S_IRUGO
| S_IXUGO
)) != (inode
->i_mode
& (S_IRUGO
|S_IXUGO
)))
302 if (fat_mode_can_hold_ro(inode
)) {
303 if ((perm
& S_IWUGO
) && ((perm
& S_IWUGO
) != (S_IWUGO
& ~mask
)))
306 if ((perm
& S_IWUGO
) != (S_IWUGO
& ~mask
))
310 *mode_ptr
&= S_IFMT
| perm
;
315 static int fat_allow_set_time(struct msdos_sb_info
*sbi
, struct inode
*inode
)
317 mode_t allow_utime
= sbi
->options
.allow_utime
;
319 if (current_fsuid() != inode
->i_uid
) {
320 if (in_group_p(inode
->i_gid
))
322 if (allow_utime
& MAY_WRITE
)
326 /* use a default check */
330 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
331 /* valid file mode bits */
332 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
334 int fat_setattr(struct dentry
*dentry
, struct iattr
*attr
)
336 struct msdos_sb_info
*sbi
= MSDOS_SB(dentry
->d_sb
);
337 struct inode
*inode
= dentry
->d_inode
;
338 unsigned int ia_valid
;
342 * Expand the file. Since inode_setattr() updates ->i_size
343 * before calling the ->truncate(), but FAT needs to fill the
346 if (attr
->ia_valid
& ATTR_SIZE
) {
347 if (attr
->ia_size
> inode
->i_size
) {
348 error
= fat_cont_expand(inode
, attr
->ia_size
);
349 if (error
|| attr
->ia_valid
== ATTR_SIZE
)
351 attr
->ia_valid
&= ~ATTR_SIZE
;
355 /* Check for setting the inode time. */
356 ia_valid
= attr
->ia_valid
;
357 if (ia_valid
& TIMES_SET_FLAGS
) {
358 if (fat_allow_set_time(sbi
, inode
))
359 attr
->ia_valid
&= ~TIMES_SET_FLAGS
;
362 error
= inode_change_ok(inode
, attr
);
363 attr
->ia_valid
= ia_valid
;
365 if (sbi
->options
.quiet
)
370 if (((attr
->ia_valid
& ATTR_UID
) &&
371 (attr
->ia_uid
!= sbi
->options
.fs_uid
)) ||
372 ((attr
->ia_valid
& ATTR_GID
) &&
373 (attr
->ia_gid
!= sbi
->options
.fs_gid
)) ||
374 ((attr
->ia_valid
& ATTR_MODE
) &&
375 (attr
->ia_mode
& ~FAT_VALID_MODE
)))
379 if (sbi
->options
.quiet
)
385 * We don't return -EPERM here. Yes, strange, but this is too
388 if (attr
->ia_valid
& ATTR_MODE
) {
389 if (fat_sanitize_mode(sbi
, inode
, &attr
->ia_mode
) < 0)
390 attr
->ia_valid
&= ~ATTR_MODE
;
394 error
= inode_setattr(inode
, attr
);
398 EXPORT_SYMBOL_GPL(fat_setattr
);
400 const struct inode_operations fat_file_inode_operations
= {
401 .truncate
= fat_truncate
,
402 .setattr
= fat_setattr
,
403 .getattr
= fat_getattr
,