1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/slab.h>
7 #include <linux/compat.h>
8 #include <linux/cred.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
11 #include <linux/fsnotify.h>
12 #include <linux/security.h>
13 #include <linux/msdos_fs.h>
15 #include "exfat_raw.h"
18 static int exfat_cont_expand(struct inode *inode, loff_t size)
20 struct address_space *mapping = inode->i_mapping;
21 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
24 err = generic_cont_expand_simple(inode, size);
28 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
29 mark_inode_dirty(inode);
34 err = filemap_fdatawrite_range(mapping, start, start + count - 1);
35 err2 = sync_mapping_buffers(mapping);
38 err2 = write_inode_now(inode, 1);
44 return filemap_fdatawait_range(mapping, start, start + count - 1);
47 static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
49 mode_t allow_utime = sbi->options.allow_utime;
51 if (!uid_eq(current_fsuid(), inode->i_uid)) {
52 if (in_group_p(inode->i_gid))
54 if (allow_utime & MAY_WRITE)
58 /* use a default check */
62 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
63 struct inode *inode, umode_t *mode_ptr)
65 mode_t i_mode, mask, perm;
67 i_mode = inode->i_mode;
69 mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
70 sbi->options.fs_fmask : sbi->options.fs_dmask;
71 perm = *mode_ptr & ~(S_IFMT | mask);
73 /* Of the r and x bits, all (subject to umask) must be present.*/
74 if ((perm & 0555) != (i_mode & 0555))
77 if (exfat_mode_can_hold_ro(inode)) {
79 * Of the w bits, either all (subject to umask) or none must
82 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
86 * If exfat_mode_can_hold_ro(inode) is false, can't change
89 if ((perm & 0222) != (0222 & ~mask))
93 *mode_ptr &= S_IFMT | perm;
98 /* resize the file length */
99 int __exfat_truncate(struct inode *inode)
101 unsigned int num_clusters_new, num_clusters_phys;
102 unsigned int last_clu = EXFAT_FREE_CLUSTER;
103 struct exfat_chain clu;
104 struct super_block *sb = inode->i_sb;
105 struct exfat_sb_info *sbi = EXFAT_SB(sb);
106 struct exfat_inode_info *ei = EXFAT_I(inode);
108 /* check if the given file ID is opened */
109 if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
112 exfat_set_volume_dirty(sb);
114 num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
115 num_clusters_phys = EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
117 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
119 if (i_size_read(inode) > 0) {
121 * Truncate FAT chain num_clusters after the first cluster
122 * num_clusters = min(new, phys);
124 unsigned int num_clusters =
125 min(num_clusters_new, num_clusters_phys);
129 * (defensive coding - works fine even with corrupted FAT table
131 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
132 clu.dir += num_clusters;
133 clu.size -= num_clusters;
135 while (num_clusters > 0) {
137 if (exfat_get_next_cluster(sb, &(clu.dir)))
145 ei->flags = ALLOC_NO_FAT_CHAIN;
146 ei->start_clu = EXFAT_EOF_CLUSTER;
149 if (ei->type == TYPE_FILE)
150 ei->attr |= EXFAT_ATTR_ARCHIVE;
153 * update the directory entry
155 * If the directory entry is updated by mark_inode_dirty(), the
156 * directory entry will be written after a writeback cycle of
157 * updating the bitmap/FAT, which may result in clusters being
158 * freed but referenced by the directory entry in the event of a
159 * sudden power failure.
160 * __exfat_write_inode() is called for directory entry, bitmap
161 * and FAT to be written in a same writeback.
163 if (__exfat_write_inode(inode, inode_needs_sync(inode)))
166 /* cut off from the FAT chain */
167 if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
168 last_clu != EXFAT_EOF_CLUSTER) {
169 if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
173 /* invalidate cache and free the clusters */
174 /* clear exfat cache */
175 exfat_cache_inval_inode(inode);
177 /* hint information */
178 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
179 ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
181 /* hint_stat will be used if this is directory. */
182 ei->hint_stat.eidx = 0;
183 ei->hint_stat.clu = ei->start_clu;
184 ei->hint_femp.eidx = EXFAT_HINT_NONE;
186 /* free the clusters */
187 if (exfat_free_cluster(inode, &clu))
193 void exfat_truncate(struct inode *inode)
195 struct super_block *sb = inode->i_sb;
196 struct exfat_sb_info *sbi = EXFAT_SB(sb);
197 struct exfat_inode_info *ei = EXFAT_I(inode);
198 unsigned int blocksize = i_blocksize(inode);
202 mutex_lock(&sbi->s_lock);
203 if (ei->start_clu == 0) {
205 * Empty start_clu != ~0 (not allocated)
207 exfat_fs_error(sb, "tried to truncate zeroed cluster.");
211 err = __exfat_truncate(inode);
215 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
217 aligned_size = i_size_read(inode);
218 if (aligned_size & (blocksize - 1)) {
219 aligned_size |= (blocksize - 1);
223 if (ei->i_size_ondisk > i_size_read(inode))
224 ei->i_size_ondisk = aligned_size;
226 if (ei->i_size_aligned > i_size_read(inode))
227 ei->i_size_aligned = aligned_size;
228 mutex_unlock(&sbi->s_lock);
231 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
232 struct kstat *stat, unsigned int request_mask,
233 unsigned int query_flags)
235 struct inode *inode = d_backing_inode(path->dentry);
236 struct exfat_inode_info *ei = EXFAT_I(inode);
238 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
239 exfat_truncate_atime(&stat->atime);
240 stat->result_mask |= STATX_BTIME;
241 stat->btime.tv_sec = ei->i_crtime.tv_sec;
242 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
243 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
247 int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
250 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
251 struct inode *inode = dentry->d_inode;
252 unsigned int ia_valid;
255 if ((attr->ia_valid & ATTR_SIZE) &&
256 attr->ia_size > i_size_read(inode)) {
257 error = exfat_cont_expand(inode, attr->ia_size);
258 if (error || attr->ia_valid == ATTR_SIZE)
260 attr->ia_valid &= ~ATTR_SIZE;
263 /* Check for setting the inode time. */
264 ia_valid = attr->ia_valid;
265 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
266 exfat_allow_set_time(sbi, inode)) {
267 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
271 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
272 attr->ia_valid = ia_valid;
276 if (((attr->ia_valid & ATTR_UID) &&
277 !uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
278 ((attr->ia_valid & ATTR_GID) &&
279 !gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
280 ((attr->ia_valid & ATTR_MODE) &&
281 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
287 * We don't return -EPERM here. Yes, strange, but this is too
290 if (attr->ia_valid & ATTR_MODE) {
291 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
292 attr->ia_valid &= ~ATTR_MODE;
295 if (attr->ia_valid & ATTR_SIZE)
296 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
298 setattr_copy(&nop_mnt_idmap, inode, attr);
299 exfat_truncate_inode_atime(inode);
301 if (attr->ia_valid & ATTR_SIZE) {
302 error = exfat_block_truncate_page(inode, attr->ia_size);
306 down_write(&EXFAT_I(inode)->truncate_lock);
307 truncate_setsize(inode, attr->ia_size);
310 * __exfat_write_inode() is called from exfat_truncate(), inode
311 * is already written by it, so mark_inode_dirty() is unneeded.
313 exfat_truncate(inode);
314 up_write(&EXFAT_I(inode)->truncate_lock);
316 mark_inode_dirty(inode);
323 * modified ioctls from fat/file.c by Welmer Almesberger
325 static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
329 inode_lock_shared(inode);
330 attr = exfat_make_attr(inode);
331 inode_unlock_shared(inode);
333 return put_user(attr, user_attr);
336 static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
338 struct inode *inode = file_inode(file);
339 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
340 int is_dir = S_ISDIR(inode->i_mode);
345 err = get_user(attr, user_attr);
349 err = mnt_want_write_file(file);
354 oldattr = exfat_make_attr(inode);
357 * Mask attributes so we don't set reserved fields.
359 attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM |
361 attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0);
363 /* Equivalent to a chmod() */
364 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
365 ia.ia_ctime = current_time(inode);
367 ia.ia_mode = exfat_make_mode(sbi, attr, 0777);
369 ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111));
371 /* The root directory has no attributes */
372 if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) {
374 goto out_unlock_inode;
377 if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) &&
378 !capable(CAP_LINUX_IMMUTABLE)) {
380 goto out_unlock_inode;
384 * The security check is questionable... We single
385 * out the RO attribute for checking by the security
386 * module, just because it maps to a file mode.
388 err = security_inode_setattr(file_mnt_idmap(file),
389 file->f_path.dentry, &ia);
391 goto out_unlock_inode;
393 /* This MUST be done before doing anything irreversible... */
394 err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia);
396 goto out_unlock_inode;
398 fsnotify_change(file->f_path.dentry, ia.ia_valid);
400 exfat_save_attr(inode, attr);
401 mark_inode_dirty(inode);
404 mnt_drop_write_file(file);
409 static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
411 struct fstrim_range range;
414 if (!capable(CAP_SYS_ADMIN))
417 if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
420 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
423 range.minlen = max_t(unsigned int, range.minlen,
424 bdev_discard_granularity(inode->i_sb->s_bdev));
426 ret = exfat_trim_fs(inode, &range);
430 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
436 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
438 struct inode *inode = file_inode(filp);
439 u32 __user *user_attr = (u32 __user *)arg;
442 case FAT_IOCTL_GET_ATTRIBUTES:
443 return exfat_ioctl_get_attributes(inode, user_attr);
444 case FAT_IOCTL_SET_ATTRIBUTES:
445 return exfat_ioctl_set_attributes(filp, user_attr);
447 return exfat_ioctl_fitrim(inode, arg);
454 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
457 return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
461 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
463 struct inode *inode = filp->f_mapping->host;
466 err = __generic_file_fsync(filp, start, end, datasync);
470 err = sync_blockdev(inode->i_sb->s_bdev);
474 return blkdev_issue_flush(inode->i_sb->s_bdev);
477 const struct file_operations exfat_file_operations = {
478 .llseek = generic_file_llseek,
479 .read_iter = generic_file_read_iter,
480 .write_iter = generic_file_write_iter,
481 .unlocked_ioctl = exfat_ioctl,
483 .compat_ioctl = exfat_compat_ioctl,
485 .mmap = generic_file_mmap,
486 .fsync = exfat_file_fsync,
487 .splice_read = filemap_splice_read,
488 .splice_write = iter_file_splice_write,
491 const struct inode_operations exfat_file_inode_operations = {
492 .setattr = exfat_setattr,
493 .getattr = exfat_getattr,