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/msdos_fs.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/backing-dev.h>
17 #include <linux/blkdev.h>
18 #include <linux/fsnotify.h>
19 #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 if (inode
->i_ino
== MSDOS_ROOT_INO
)
35 attr
= fat_attr(inode
);
37 return put_user(attr
, user_attr
);
39 case FAT_IOCTL_SET_ATTRIBUTES
:
42 int err
, is_dir
= S_ISDIR(inode
->i_mode
);
45 err
= get_user(attr
, user_attr
);
49 mutex_lock(&inode
->i_mutex
);
51 err
= mnt_want_write(filp
->f_path
.mnt
);
53 goto up_no_drop_write
;
56 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
57 * prevents the user from turning us into a VFAT
58 * longname entry. Also, we obviously can't set
59 * any of the NTFS attributes in the high 24 bits.
61 attr
&= 0xff & ~(ATTR_VOLUME
| ATTR_DIR
);
62 /* Merge in ATTR_VOLUME and ATTR_DIR */
63 attr
|= (MSDOS_I(inode
)->i_attrs
& ATTR_VOLUME
) |
64 (is_dir
? ATTR_DIR
: 0);
65 oldattr
= fat_attr(inode
);
67 /* Equivalent to a chmod() */
68 ia
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
69 ia
.ia_ctime
= current_fs_time(inode
->i_sb
);
71 ia
.ia_mode
= MSDOS_MKMODE(attr
,
72 S_IRWXUGO
& ~sbi
->options
.fs_dmask
)
75 ia
.ia_mode
= MSDOS_MKMODE(attr
,
76 (S_IRUGO
| S_IWUGO
| (inode
->i_mode
& S_IXUGO
))
77 & ~sbi
->options
.fs_fmask
)
81 /* The root directory has no attributes */
82 if (inode
->i_ino
== MSDOS_ROOT_INO
&& attr
!= ATTR_DIR
) {
87 if (sbi
->options
.sys_immutable
) {
88 if ((attr
| oldattr
) & ATTR_SYS
) {
89 if (!capable(CAP_LINUX_IMMUTABLE
)) {
97 * The security check is questionable... We single
98 * out the RO attribute for checking by the security
99 * module, just because it maps to a file mode.
101 err
= security_inode_setattr(filp
->f_path
.dentry
, &ia
);
105 /* This MUST be done before doing anything irreversible... */
106 err
= fat_setattr(filp
->f_path
.dentry
, &ia
);
110 fsnotify_change(filp
->f_path
.dentry
, ia
.ia_valid
);
111 if (sbi
->options
.sys_immutable
) {
113 inode
->i_flags
|= S_IMMUTABLE
;
115 inode
->i_flags
&= S_IMMUTABLE
;
118 MSDOS_I(inode
)->i_attrs
= attr
& ATTR_UNUSED
;
119 mark_inode_dirty(inode
);
121 mnt_drop_write(filp
->f_path
.mnt
);
123 mutex_unlock(&inode
->i_mutex
);
127 return -ENOTTY
; /* Inappropriate ioctl for device */
131 static int fat_file_release(struct inode
*inode
, struct file
*filp
)
133 if ((filp
->f_mode
& FMODE_WRITE
) &&
134 MSDOS_SB(inode
->i_sb
)->options
.flush
) {
135 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
136 congestion_wait(WRITE
, HZ
/10);
141 const struct file_operations fat_file_operations
= {
142 .llseek
= generic_file_llseek
,
143 .read
= do_sync_read
,
144 .write
= do_sync_write
,
145 .aio_read
= generic_file_aio_read
,
146 .aio_write
= generic_file_aio_write
,
147 .mmap
= generic_file_mmap
,
148 .release
= fat_file_release
,
149 .ioctl
= fat_generic_ioctl
,
151 .splice_read
= generic_file_splice_read
,
154 static int fat_cont_expand(struct inode
*inode
, loff_t size
)
156 struct address_space
*mapping
= inode
->i_mapping
;
157 loff_t start
= inode
->i_size
, count
= size
- inode
->i_size
;
160 err
= generic_cont_expand_simple(inode
, size
);
164 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
165 mark_inode_dirty(inode
);
167 err
= sync_page_range_nolock(inode
, mapping
, start
, count
);
172 /* Free all clusters after the skip'th cluster. */
173 static int fat_free(struct inode
*inode
, int skip
)
175 struct super_block
*sb
= inode
->i_sb
;
176 int err
, wait
, free_start
, i_start
, i_logstart
;
178 if (MSDOS_I(inode
)->i_start
== 0)
181 fat_cache_inval_inode(inode
);
183 wait
= IS_DIRSYNC(inode
);
184 i_start
= free_start
= MSDOS_I(inode
)->i_start
;
185 i_logstart
= MSDOS_I(inode
)->i_logstart
;
187 /* First, we write the new file size. */
189 MSDOS_I(inode
)->i_start
= 0;
190 MSDOS_I(inode
)->i_logstart
= 0;
192 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
193 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
195 err
= fat_sync_inode(inode
);
197 MSDOS_I(inode
)->i_start
= i_start
;
198 MSDOS_I(inode
)->i_logstart
= i_logstart
;
202 mark_inode_dirty(inode
);
204 /* Write a new EOF, and get the remaining cluster chain for freeing. */
206 struct fat_entry fatent
;
207 int ret
, fclus
, dclus
;
209 ret
= fat_get_cluster(inode
, skip
- 1, &fclus
, &dclus
);
212 else if (ret
== FAT_ENT_EOF
)
215 fatent_init(&fatent
);
216 ret
= fat_ent_read(inode
, &fatent
, dclus
);
217 if (ret
== FAT_ENT_EOF
) {
218 fatent_brelse(&fatent
);
220 } else if (ret
== FAT_ENT_FREE
) {
222 "%s: invalid cluster chain (i_pos %lld)",
223 __func__
, MSDOS_I(inode
)->i_pos
);
225 } else if (ret
> 0) {
226 err
= fat_ent_write(inode
, &fatent
, FAT_ENT_EOF
, wait
);
230 fatent_brelse(&fatent
);
236 inode
->i_blocks
= skip
<< (MSDOS_SB(sb
)->cluster_bits
- 9);
238 /* Freeing the remained cluster chain */
239 return fat_free_clusters(inode
, free_start
);
242 void fat_truncate(struct inode
*inode
)
244 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
245 const unsigned int cluster_size
= sbi
->cluster_size
;
249 * This protects against truncating a file bigger than it was then
250 * trying to write into the hole.
252 if (MSDOS_I(inode
)->mmu_private
> inode
->i_size
)
253 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
255 nr_clusters
= (inode
->i_size
+ (cluster_size
- 1)) >> sbi
->cluster_bits
;
257 fat_free(inode
, nr_clusters
);
258 fat_flush_inodes(inode
->i_sb
, inode
, NULL
);
261 int fat_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
263 struct inode
*inode
= dentry
->d_inode
;
264 generic_fillattr(inode
, stat
);
265 stat
->blksize
= MSDOS_SB(inode
->i_sb
)->cluster_size
;
268 EXPORT_SYMBOL_GPL(fat_getattr
);
270 static int fat_sanitize_mode(const struct msdos_sb_info
*sbi
,
271 struct inode
*inode
, umode_t
*mode_ptr
)
276 * Note, the basic check is already done by a caller of
277 * (attr->ia_mode & ~MSDOS_VALID_MODE)
280 if (S_ISREG(inode
->i_mode
))
281 mask
= sbi
->options
.fs_fmask
;
283 mask
= sbi
->options
.fs_dmask
;
285 perm
= *mode_ptr
& ~(S_IFMT
| mask
);
288 * Of the r and x bits, all (subject to umask) must be present. Of the
289 * w bits, either all (subject to umask) or none must be present.
291 if ((perm
& (S_IRUGO
| S_IXUGO
)) != (inode
->i_mode
& (S_IRUGO
|S_IXUGO
)))
293 if ((perm
& S_IWUGO
) && ((perm
& S_IWUGO
) != (S_IWUGO
& ~mask
)))
296 *mode_ptr
&= S_IFMT
| perm
;
301 static int fat_allow_set_time(struct msdos_sb_info
*sbi
, struct inode
*inode
)
303 mode_t allow_utime
= sbi
->options
.allow_utime
;
305 if (current
->fsuid
!= inode
->i_uid
) {
306 if (in_group_p(inode
->i_gid
))
308 if (allow_utime
& MAY_WRITE
)
312 /* use a default check */
316 int fat_setattr(struct dentry
*dentry
, struct iattr
*attr
)
318 struct msdos_sb_info
*sbi
= MSDOS_SB(dentry
->d_sb
);
319 struct inode
*inode
= dentry
->d_inode
;
321 unsigned int ia_valid
;
324 * Expand the file. Since inode_setattr() updates ->i_size
325 * before calling the ->truncate(), but FAT needs to fill the
328 if (attr
->ia_valid
& ATTR_SIZE
) {
329 if (attr
->ia_size
> inode
->i_size
) {
330 error
= fat_cont_expand(inode
, attr
->ia_size
);
331 if (error
|| attr
->ia_valid
== ATTR_SIZE
)
333 attr
->ia_valid
&= ~ATTR_SIZE
;
337 /* Check for setting the inode time. */
338 ia_valid
= attr
->ia_valid
;
339 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
)) {
340 if (fat_allow_set_time(sbi
, inode
))
341 attr
->ia_valid
&= ~(ATTR_MTIME_SET
| ATTR_ATIME_SET
);
344 error
= inode_change_ok(inode
, attr
);
345 attr
->ia_valid
= ia_valid
;
347 if (sbi
->options
.quiet
)
352 if (((attr
->ia_valid
& ATTR_UID
) &&
353 (attr
->ia_uid
!= sbi
->options
.fs_uid
)) ||
354 ((attr
->ia_valid
& ATTR_GID
) &&
355 (attr
->ia_gid
!= sbi
->options
.fs_gid
)) ||
356 ((attr
->ia_valid
& ATTR_MODE
) &&
357 (attr
->ia_mode
& ~MSDOS_VALID_MODE
)))
361 if (sbi
->options
.quiet
)
367 * We don't return -EPERM here. Yes, strange, but this is too
370 if (attr
->ia_valid
& ATTR_MODE
) {
371 if (fat_sanitize_mode(sbi
, inode
, &attr
->ia_mode
) < 0)
372 attr
->ia_valid
&= ~ATTR_MODE
;
375 error
= inode_setattr(inode
, attr
);
379 EXPORT_SYMBOL_GPL(fat_setattr
);
381 const struct inode_operations fat_file_inode_operations
= {
382 .truncate
= fat_truncate
,
383 .setattr
= fat_setattr
,
384 .getattr
= fat_getattr
,