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/compat.h>
12 #include <linux/mount.h>
13 #include <linux/blkdev.h>
14 #include <linux/fsnotify.h>
15 #include <linux/security.h>
18 static int fat_ioctl_get_attributes(struct inode
*inode
, u32 __user
*user_attr
)
22 mutex_lock(&inode
->i_mutex
);
23 attr
= fat_make_attrs(inode
);
24 mutex_unlock(&inode
->i_mutex
);
26 return put_user(attr
, user_attr
);
29 static int fat_ioctl_set_attributes(struct file
*file
, u32 __user
*user_attr
)
31 struct inode
*inode
= file_inode(file
);
32 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
33 int is_dir
= S_ISDIR(inode
->i_mode
);
38 err
= get_user(attr
, user_attr
);
42 err
= mnt_want_write_file(file
);
45 mutex_lock(&inode
->i_mutex
);
48 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
49 * prevents the user from turning us into a VFAT
50 * longname entry. Also, we obviously can't set
51 * any of the NTFS attributes in the high 24 bits.
53 attr
&= 0xff & ~(ATTR_VOLUME
| ATTR_DIR
);
54 /* Merge in ATTR_VOLUME and ATTR_DIR */
55 attr
|= (MSDOS_I(inode
)->i_attrs
& ATTR_VOLUME
) |
56 (is_dir
? ATTR_DIR
: 0);
57 oldattr
= fat_make_attrs(inode
);
59 /* Equivalent to a chmod() */
60 ia
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
61 ia
.ia_ctime
= current_fs_time(inode
->i_sb
);
63 ia
.ia_mode
= fat_make_mode(sbi
, attr
, S_IRWXUGO
);
65 ia
.ia_mode
= fat_make_mode(sbi
, attr
,
66 S_IRUGO
| S_IWUGO
| (inode
->i_mode
& S_IXUGO
));
69 /* The root directory has no attributes */
70 if (inode
->i_ino
== MSDOS_ROOT_INO
&& attr
!= ATTR_DIR
) {
72 goto out_unlock_inode
;
75 if (sbi
->options
.sys_immutable
&&
76 ((attr
| oldattr
) & ATTR_SYS
) &&
77 !capable(CAP_LINUX_IMMUTABLE
)) {
79 goto out_unlock_inode
;
83 * The security check is questionable... We single
84 * out the RO attribute for checking by the security
85 * module, just because it maps to a file mode.
87 err
= security_inode_setattr(file
->f_path
.dentry
, &ia
);
89 goto out_unlock_inode
;
91 /* This MUST be done before doing anything irreversible... */
92 err
= fat_setattr(file
->f_path
.dentry
, &ia
);
94 goto out_unlock_inode
;
96 fsnotify_change(file
->f_path
.dentry
, ia
.ia_valid
);
97 if (sbi
->options
.sys_immutable
) {
99 inode
->i_flags
|= S_IMMUTABLE
;
101 inode
->i_flags
&= ~S_IMMUTABLE
;
104 fat_save_attrs(inode
, attr
);
105 mark_inode_dirty(inode
);
107 mutex_unlock(&inode
->i_mutex
);
108 mnt_drop_write_file(file
);
113 static int fat_ioctl_get_volume_id(struct inode
*inode
, u32 __user
*user_attr
)
115 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
116 return put_user(sbi
->vol_id
, user_attr
);
119 long fat_generic_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
121 struct inode
*inode
= file_inode(filp
);
122 u32 __user
*user_attr
= (u32 __user
*)arg
;
125 case FAT_IOCTL_GET_ATTRIBUTES
:
126 return fat_ioctl_get_attributes(inode
, user_attr
);
127 case FAT_IOCTL_SET_ATTRIBUTES
:
128 return fat_ioctl_set_attributes(filp
, user_attr
);
129 case FAT_IOCTL_GET_VOLUME_ID
:
130 return fat_ioctl_get_volume_id(inode
, user_attr
);
132 return -ENOTTY
; /* Inappropriate ioctl for device */
137 static long fat_generic_compat_ioctl(struct file
*filp
, unsigned int cmd
,
141 return fat_generic_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
));
145 static int fat_file_release(struct inode
*inode
, struct file
*filp
)
147 if ((filp
->f_mode
& FMODE_WRITE
) &&
148 MSDOS_SB(inode
->i_sb
)->options
.flush
) {
149 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
150 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
155 int fat_file_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
157 struct inode
*inode
= filp
->f_mapping
->host
;
160 res
= generic_file_fsync(filp
, start
, end
, datasync
);
161 err
= sync_mapping_buffers(MSDOS_SB(inode
->i_sb
)->fat_inode
->i_mapping
);
163 return res
? res
: err
;
167 const struct file_operations fat_file_operations
= {
168 .llseek
= generic_file_llseek
,
169 .read_iter
= generic_file_read_iter
,
170 .write_iter
= generic_file_write_iter
,
171 .mmap
= generic_file_mmap
,
172 .release
= fat_file_release
,
173 .unlocked_ioctl
= fat_generic_ioctl
,
175 .compat_ioctl
= fat_generic_compat_ioctl
,
177 .fsync
= fat_file_fsync
,
178 .splice_read
= generic_file_splice_read
,
181 static int fat_cont_expand(struct inode
*inode
, loff_t size
)
183 struct address_space
*mapping
= inode
->i_mapping
;
184 loff_t start
= inode
->i_size
, count
= size
- inode
->i_size
;
187 err
= generic_cont_expand_simple(inode
, size
);
191 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
192 mark_inode_dirty(inode
);
193 if (IS_SYNC(inode
)) {
197 * Opencode syncing since we don't have a file open to use
198 * standard fsync path.
200 err
= filemap_fdatawrite_range(mapping
, start
,
202 err2
= sync_mapping_buffers(mapping
);
205 err2
= write_inode_now(inode
, 1);
209 err
= filemap_fdatawait_range(mapping
, start
,
217 /* Free all clusters after the skip'th cluster. */
218 static int fat_free(struct inode
*inode
, int skip
)
220 struct super_block
*sb
= inode
->i_sb
;
221 int err
, wait
, free_start
, i_start
, i_logstart
;
223 if (MSDOS_I(inode
)->i_start
== 0)
226 fat_cache_inval_inode(inode
);
228 wait
= IS_DIRSYNC(inode
);
229 i_start
= free_start
= MSDOS_I(inode
)->i_start
;
230 i_logstart
= MSDOS_I(inode
)->i_logstart
;
232 /* First, we write the new file size. */
234 MSDOS_I(inode
)->i_start
= 0;
235 MSDOS_I(inode
)->i_logstart
= 0;
237 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
238 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
240 err
= fat_sync_inode(inode
);
242 MSDOS_I(inode
)->i_start
= i_start
;
243 MSDOS_I(inode
)->i_logstart
= i_logstart
;
247 mark_inode_dirty(inode
);
249 /* Write a new EOF, and get the remaining cluster chain for freeing. */
251 struct fat_entry fatent
;
252 int ret
, fclus
, dclus
;
254 ret
= fat_get_cluster(inode
, skip
- 1, &fclus
, &dclus
);
257 else if (ret
== FAT_ENT_EOF
)
260 fatent_init(&fatent
);
261 ret
= fat_ent_read(inode
, &fatent
, dclus
);
262 if (ret
== FAT_ENT_EOF
) {
263 fatent_brelse(&fatent
);
265 } else if (ret
== FAT_ENT_FREE
) {
267 "%s: invalid cluster chain (i_pos %lld)",
268 __func__
, MSDOS_I(inode
)->i_pos
);
270 } else if (ret
> 0) {
271 err
= fat_ent_write(inode
, &fatent
, FAT_ENT_EOF
, wait
);
275 fatent_brelse(&fatent
);
281 inode
->i_blocks
= skip
<< (MSDOS_SB(sb
)->cluster_bits
- 9);
283 /* Freeing the remained cluster chain */
284 return fat_free_clusters(inode
, free_start
);
287 void fat_truncate_blocks(struct inode
*inode
, loff_t offset
)
289 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
290 const unsigned int cluster_size
= sbi
->cluster_size
;
294 * This protects against truncating a file bigger than it was then
295 * trying to write into the hole.
297 if (MSDOS_I(inode
)->mmu_private
> offset
)
298 MSDOS_I(inode
)->mmu_private
= offset
;
300 nr_clusters
= (offset
+ (cluster_size
- 1)) >> sbi
->cluster_bits
;
302 fat_free(inode
, nr_clusters
);
303 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
306 int fat_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
308 struct inode
*inode
= d_inode(dentry
);
309 generic_fillattr(inode
, stat
);
310 stat
->blksize
= MSDOS_SB(inode
->i_sb
)->cluster_size
;
312 if (MSDOS_SB(inode
->i_sb
)->options
.nfs
== FAT_NFS_NOSTALE_RO
) {
313 /* Use i_pos for ino. This is used as fileid of nfs. */
314 stat
->ino
= fat_i_pos_read(MSDOS_SB(inode
->i_sb
), inode
);
318 EXPORT_SYMBOL_GPL(fat_getattr
);
320 static int fat_sanitize_mode(const struct msdos_sb_info
*sbi
,
321 struct inode
*inode
, umode_t
*mode_ptr
)
326 * Note, the basic check is already done by a caller of
327 * (attr->ia_mode & ~FAT_VALID_MODE)
330 if (S_ISREG(inode
->i_mode
))
331 mask
= sbi
->options
.fs_fmask
;
333 mask
= sbi
->options
.fs_dmask
;
335 perm
= *mode_ptr
& ~(S_IFMT
| mask
);
338 * Of the r and x bits, all (subject to umask) must be present. Of the
339 * w bits, either all (subject to umask) or none must be present.
341 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
343 if ((perm
& (S_IRUGO
| S_IXUGO
)) != (inode
->i_mode
& (S_IRUGO
|S_IXUGO
)))
345 if (fat_mode_can_hold_ro(inode
)) {
346 if ((perm
& S_IWUGO
) && ((perm
& S_IWUGO
) != (S_IWUGO
& ~mask
)))
349 if ((perm
& S_IWUGO
) != (S_IWUGO
& ~mask
))
353 *mode_ptr
&= S_IFMT
| perm
;
358 static int fat_allow_set_time(struct msdos_sb_info
*sbi
, struct inode
*inode
)
360 umode_t allow_utime
= sbi
->options
.allow_utime
;
362 if (!uid_eq(current_fsuid(), inode
->i_uid
)) {
363 if (in_group_p(inode
->i_gid
))
365 if (allow_utime
& MAY_WRITE
)
369 /* use a default check */
373 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
374 /* valid file mode bits */
375 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
377 int fat_setattr(struct dentry
*dentry
, struct iattr
*attr
)
379 struct msdos_sb_info
*sbi
= MSDOS_SB(dentry
->d_sb
);
380 struct inode
*inode
= d_inode(dentry
);
381 unsigned int ia_valid
;
384 /* Check for setting the inode time. */
385 ia_valid
= attr
->ia_valid
;
386 if (ia_valid
& TIMES_SET_FLAGS
) {
387 if (fat_allow_set_time(sbi
, inode
))
388 attr
->ia_valid
&= ~TIMES_SET_FLAGS
;
391 error
= setattr_prepare(dentry
, attr
);
392 attr
->ia_valid
= ia_valid
;
394 if (sbi
->options
.quiet
)
400 * Expand the file. Since inode_setattr() updates ->i_size
401 * before calling the ->truncate(), but FAT needs to fill the
402 * hole before it. XXX: this is no longer true with new truncate
405 if (attr
->ia_valid
& ATTR_SIZE
) {
406 inode_dio_wait(inode
);
408 if (attr
->ia_size
> inode
->i_size
) {
409 error
= fat_cont_expand(inode
, attr
->ia_size
);
410 if (error
|| attr
->ia_valid
== ATTR_SIZE
)
412 attr
->ia_valid
&= ~ATTR_SIZE
;
416 if (((attr
->ia_valid
& ATTR_UID
) &&
417 (!uid_eq(attr
->ia_uid
, sbi
->options
.fs_uid
))) ||
418 ((attr
->ia_valid
& ATTR_GID
) &&
419 (!gid_eq(attr
->ia_gid
, sbi
->options
.fs_gid
))) ||
420 ((attr
->ia_valid
& ATTR_MODE
) &&
421 (attr
->ia_mode
& ~FAT_VALID_MODE
)))
425 if (sbi
->options
.quiet
)
431 * We don't return -EPERM here. Yes, strange, but this is too
434 if (attr
->ia_valid
& ATTR_MODE
) {
435 if (fat_sanitize_mode(sbi
, inode
, &attr
->ia_mode
) < 0)
436 attr
->ia_valid
&= ~ATTR_MODE
;
439 if (attr
->ia_valid
& ATTR_SIZE
) {
440 error
= fat_block_truncate_page(inode
, attr
->ia_size
);
443 down_write(&MSDOS_I(inode
)->truncate_lock
);
444 truncate_setsize(inode
, attr
->ia_size
);
445 fat_truncate_blocks(inode
, attr
->ia_size
);
446 up_write(&MSDOS_I(inode
)->truncate_lock
);
449 setattr_copy(inode
, attr
);
450 mark_inode_dirty(inode
);
454 EXPORT_SYMBOL_GPL(fat_setattr
);
456 const struct inode_operations fat_file_inode_operations
= {
457 .setattr
= fat_setattr
,
458 .getattr
= fat_getattr
,