xref: /openbmc/linux/fs/exfat/file.c (revision a4e1d0b7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4  */
5 
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 
12 #include "exfat_raw.h"
13 #include "exfat_fs.h"
14 
15 static int exfat_cont_expand(struct inode *inode, loff_t size)
16 {
17 	struct address_space *mapping = inode->i_mapping;
18 	loff_t start = i_size_read(inode), count = size - i_size_read(inode);
19 	int err, err2;
20 
21 	err = generic_cont_expand_simple(inode, size);
22 	if (err)
23 		return err;
24 
25 	inode->i_ctime = inode->i_mtime = current_time(inode);
26 	mark_inode_dirty(inode);
27 
28 	if (!IS_SYNC(inode))
29 		return 0;
30 
31 	err = filemap_fdatawrite_range(mapping, start, start + count - 1);
32 	err2 = sync_mapping_buffers(mapping);
33 	if (!err)
34 		err = err2;
35 	err2 = write_inode_now(inode, 1);
36 	if (!err)
37 		err = err2;
38 	if (err)
39 		return err;
40 
41 	return filemap_fdatawait_range(mapping, start, start + count - 1);
42 }
43 
44 static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
45 {
46 	mode_t allow_utime = sbi->options.allow_utime;
47 
48 	if (!uid_eq(current_fsuid(), inode->i_uid)) {
49 		if (in_group_p(inode->i_gid))
50 			allow_utime >>= 3;
51 		if (allow_utime & MAY_WRITE)
52 			return true;
53 	}
54 
55 	/* use a default check */
56 	return false;
57 }
58 
59 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
60 		struct inode *inode, umode_t *mode_ptr)
61 {
62 	mode_t i_mode, mask, perm;
63 
64 	i_mode = inode->i_mode;
65 
66 	mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
67 		sbi->options.fs_fmask : sbi->options.fs_dmask;
68 	perm = *mode_ptr & ~(S_IFMT | mask);
69 
70 	/* Of the r and x bits, all (subject to umask) must be present.*/
71 	if ((perm & 0555) != (i_mode & 0555))
72 		return -EPERM;
73 
74 	if (exfat_mode_can_hold_ro(inode)) {
75 		/*
76 		 * Of the w bits, either all (subject to umask) or none must
77 		 * be present.
78 		 */
79 		if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
80 			return -EPERM;
81 	} else {
82 		/*
83 		 * If exfat_mode_can_hold_ro(inode) is false, can't change
84 		 * w bits.
85 		 */
86 		if ((perm & 0222) != (0222 & ~mask))
87 			return -EPERM;
88 	}
89 
90 	*mode_ptr &= S_IFMT | perm;
91 
92 	return 0;
93 }
94 
95 /* resize the file length */
96 int __exfat_truncate(struct inode *inode, loff_t new_size)
97 {
98 	unsigned int num_clusters_new, num_clusters_phys;
99 	unsigned int last_clu = EXFAT_FREE_CLUSTER;
100 	struct exfat_chain clu;
101 	struct super_block *sb = inode->i_sb;
102 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
103 	struct exfat_inode_info *ei = EXFAT_I(inode);
104 
105 	/* check if the given file ID is opened */
106 	if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
107 		return -EPERM;
108 
109 	exfat_set_volume_dirty(sb);
110 
111 	num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
112 	num_clusters_phys = EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
113 
114 	exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
115 
116 	if (new_size > 0) {
117 		/*
118 		 * Truncate FAT chain num_clusters after the first cluster
119 		 * num_clusters = min(new, phys);
120 		 */
121 		unsigned int num_clusters =
122 			min(num_clusters_new, num_clusters_phys);
123 
124 		/*
125 		 * Follow FAT chain
126 		 * (defensive coding - works fine even with corrupted FAT table
127 		 */
128 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
129 			clu.dir += num_clusters;
130 			clu.size -= num_clusters;
131 		} else {
132 			while (num_clusters > 0) {
133 				last_clu = clu.dir;
134 				if (exfat_get_next_cluster(sb, &(clu.dir)))
135 					return -EIO;
136 
137 				num_clusters--;
138 				clu.size--;
139 			}
140 		}
141 	} else {
142 		ei->flags = ALLOC_NO_FAT_CHAIN;
143 		ei->start_clu = EXFAT_EOF_CLUSTER;
144 	}
145 
146 	i_size_write(inode, new_size);
147 
148 	if (ei->type == TYPE_FILE)
149 		ei->attr |= ATTR_ARCHIVE;
150 
151 	/*
152 	 * update the directory entry
153 	 *
154 	 * If the directory entry is updated by mark_inode_dirty(), the
155 	 * directory entry will be written after a writeback cycle of
156 	 * updating the bitmap/FAT, which may result in clusters being
157 	 * freed but referenced by the directory entry in the event of a
158 	 * sudden power failure.
159 	 * __exfat_write_inode() is called for directory entry, bitmap
160 	 * and FAT to be written in a same writeback.
161 	 */
162 	if (__exfat_write_inode(inode, inode_needs_sync(inode)))
163 		return -EIO;
164 
165 	/* cut off from the FAT chain */
166 	if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
167 			last_clu != EXFAT_EOF_CLUSTER) {
168 		if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
169 			return -EIO;
170 	}
171 
172 	/* invalidate cache and free the clusters */
173 	/* clear exfat cache */
174 	exfat_cache_inval_inode(inode);
175 
176 	/* hint information */
177 	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
178 	ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
179 
180 	/* hint_stat will be used if this is directory. */
181 	ei->hint_stat.eidx = 0;
182 	ei->hint_stat.clu = ei->start_clu;
183 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
184 
185 	/* free the clusters */
186 	if (exfat_free_cluster(inode, &clu))
187 		return -EIO;
188 
189 	return 0;
190 }
191 
192 void exfat_truncate(struct inode *inode, loff_t size)
193 {
194 	struct super_block *sb = inode->i_sb;
195 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
196 	struct exfat_inode_info *ei = EXFAT_I(inode);
197 	unsigned int blocksize = i_blocksize(inode);
198 	loff_t aligned_size;
199 	int err;
200 
201 	mutex_lock(&sbi->s_lock);
202 	if (ei->start_clu == 0) {
203 		/*
204 		 * Empty start_clu != ~0 (not allocated)
205 		 */
206 		exfat_fs_error(sb, "tried to truncate zeroed cluster.");
207 		goto write_size;
208 	}
209 
210 	err = __exfat_truncate(inode, i_size_read(inode));
211 	if (err)
212 		goto write_size;
213 
214 	inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >>
215 				inode->i_blkbits;
216 write_size:
217 	aligned_size = i_size_read(inode);
218 	if (aligned_size & (blocksize - 1)) {
219 		aligned_size |= (blocksize - 1);
220 		aligned_size++;
221 	}
222 
223 	if (ei->i_size_ondisk > i_size_read(inode))
224 		ei->i_size_ondisk = aligned_size;
225 
226 	if (ei->i_size_aligned > i_size_read(inode))
227 		ei->i_size_aligned = aligned_size;
228 	mutex_unlock(&sbi->s_lock);
229 }
230 
231 int exfat_getattr(struct user_namespace *mnt_uerns, const struct path *path,
232 		  struct kstat *stat, unsigned int request_mask,
233 		  unsigned int query_flags)
234 {
235 	struct inode *inode = d_backing_inode(path->dentry);
236 	struct exfat_inode_info *ei = EXFAT_I(inode);
237 
238 	generic_fillattr(&init_user_ns, 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;
244 	return 0;
245 }
246 
247 int exfat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
248 		  struct iattr *attr)
249 {
250 	struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
251 	struct inode *inode = dentry->d_inode;
252 	unsigned int ia_valid;
253 	int error;
254 
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)
259 			return error;
260 		attr->ia_valid &= ~ATTR_SIZE;
261 	}
262 
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 |
268 				ATTR_TIMES_SET);
269 	}
270 
271 	error = setattr_prepare(&init_user_ns, dentry, attr);
272 	attr->ia_valid = ia_valid;
273 	if (error)
274 		goto out;
275 
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)))) {
282 		error = -EPERM;
283 		goto out;
284 	}
285 
286 	/*
287 	 * We don't return -EPERM here. Yes, strange, but this is too
288 	 * old behavior.
289 	 */
290 	if (attr->ia_valid & ATTR_MODE) {
291 		if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
292 			attr->ia_valid &= ~ATTR_MODE;
293 	}
294 
295 	if (attr->ia_valid & ATTR_SIZE)
296 		inode->i_mtime = inode->i_ctime = current_time(inode);
297 
298 	setattr_copy(&init_user_ns, inode, attr);
299 	exfat_truncate_atime(&inode->i_atime);
300 
301 	if (attr->ia_valid & ATTR_SIZE) {
302 		error = exfat_block_truncate_page(inode, attr->ia_size);
303 		if (error)
304 			goto out;
305 
306 		down_write(&EXFAT_I(inode)->truncate_lock);
307 		truncate_setsize(inode, attr->ia_size);
308 
309 		/*
310 		 * __exfat_write_inode() is called from exfat_truncate(), inode
311 		 * is already written by it, so mark_inode_dirty() is unneeded.
312 		 */
313 		exfat_truncate(inode, attr->ia_size);
314 		up_write(&EXFAT_I(inode)->truncate_lock);
315 	} else
316 		mark_inode_dirty(inode);
317 
318 out:
319 	return error;
320 }
321 
322 static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
323 {
324 	struct fstrim_range range;
325 	int ret = 0;
326 
327 	if (!capable(CAP_SYS_ADMIN))
328 		return -EPERM;
329 
330 	if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
331 		return -EOPNOTSUPP;
332 
333 	if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
334 		return -EFAULT;
335 
336 	range.minlen = max_t(unsigned int, range.minlen,
337 				bdev_discard_granularity(inode->i_sb->s_bdev));
338 
339 	ret = exfat_trim_fs(inode, &range);
340 	if (ret < 0)
341 		return ret;
342 
343 	if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
344 		return -EFAULT;
345 
346 	return 0;
347 }
348 
349 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
350 {
351 	struct inode *inode = file_inode(filp);
352 
353 	switch (cmd) {
354 	case FITRIM:
355 		return exfat_ioctl_fitrim(inode, arg);
356 	default:
357 		return -ENOTTY;
358 	}
359 }
360 
361 #ifdef CONFIG_COMPAT
362 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
363 				unsigned long arg)
364 {
365 	return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
366 }
367 #endif
368 
369 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
370 {
371 	struct inode *inode = filp->f_mapping->host;
372 	int err;
373 
374 	err = __generic_file_fsync(filp, start, end, datasync);
375 	if (err)
376 		return err;
377 
378 	err = sync_blockdev(inode->i_sb->s_bdev);
379 	if (err)
380 		return err;
381 
382 	return blkdev_issue_flush(inode->i_sb->s_bdev);
383 }
384 
385 const struct file_operations exfat_file_operations = {
386 	.llseek		= generic_file_llseek,
387 	.read_iter	= generic_file_read_iter,
388 	.write_iter	= generic_file_write_iter,
389 	.unlocked_ioctl = exfat_ioctl,
390 #ifdef CONFIG_COMPAT
391 	.compat_ioctl = exfat_compat_ioctl,
392 #endif
393 	.mmap		= generic_file_mmap,
394 	.fsync		= exfat_file_fsync,
395 	.splice_read	= generic_file_splice_read,
396 	.splice_write	= iter_file_splice_write,
397 };
398 
399 const struct inode_operations exfat_file_inode_operations = {
400 	.setattr     = exfat_setattr,
401 	.getattr     = exfat_getattr,
402 };
403