Loading...
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/cred.h>
8#include <linux/buffer_head.h>
9#include <linux/blkdev.h>
10
11#include "exfat_raw.h"
12#include "exfat_fs.h"
13
14static int exfat_cont_expand(struct inode *inode, loff_t size)
15{
16 struct address_space *mapping = inode->i_mapping;
17 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
18 int err, err2;
19
20 err = generic_cont_expand_simple(inode, size);
21 if (err)
22 return err;
23
24 inode->i_ctime = inode->i_mtime = current_time(inode);
25 mark_inode_dirty(inode);
26
27 if (!IS_SYNC(inode))
28 return 0;
29
30 err = filemap_fdatawrite_range(mapping, start, start + count - 1);
31 err2 = sync_mapping_buffers(mapping);
32 if (!err)
33 err = err2;
34 err2 = write_inode_now(inode, 1);
35 if (!err)
36 err = err2;
37 if (err)
38 return err;
39
40 return filemap_fdatawait_range(mapping, start, start + count - 1);
41}
42
43static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
44{
45 mode_t allow_utime = sbi->options.allow_utime;
46
47 if (!uid_eq(current_fsuid(), inode->i_uid)) {
48 if (in_group_p(inode->i_gid))
49 allow_utime >>= 3;
50 if (allow_utime & MAY_WRITE)
51 return true;
52 }
53
54 /* use a default check */
55 return false;
56}
57
58static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
59 struct inode *inode, umode_t *mode_ptr)
60{
61 mode_t i_mode, mask, perm;
62
63 i_mode = inode->i_mode;
64
65 mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
66 sbi->options.fs_fmask : sbi->options.fs_dmask;
67 perm = *mode_ptr & ~(S_IFMT | mask);
68
69 /* Of the r and x bits, all (subject to umask) must be present.*/
70 if ((perm & 0555) != (i_mode & 0555))
71 return -EPERM;
72
73 if (exfat_mode_can_hold_ro(inode)) {
74 /*
75 * Of the w bits, either all (subject to umask) or none must
76 * be present.
77 */
78 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
79 return -EPERM;
80 } else {
81 /*
82 * If exfat_mode_can_hold_ro(inode) is false, can't change
83 * w bits.
84 */
85 if ((perm & 0222) != (0222 & ~mask))
86 return -EPERM;
87 }
88
89 *mode_ptr &= S_IFMT | perm;
90
91 return 0;
92}
93
94/* resize the file length */
95int __exfat_truncate(struct inode *inode, loff_t new_size)
96{
97 unsigned int num_clusters_new, num_clusters_phys;
98 unsigned int last_clu = EXFAT_FREE_CLUSTER;
99 struct exfat_chain clu;
100 struct super_block *sb = inode->i_sb;
101 struct exfat_sb_info *sbi = EXFAT_SB(sb);
102 struct exfat_inode_info *ei = EXFAT_I(inode);
103 int evict = (ei->dir.dir == DIR_DELETED) ? 1 : 0;
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 =
113 EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, sbi);
114
115 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
116
117 if (new_size > 0) {
118 /*
119 * Truncate FAT chain num_clusters after the first cluster
120 * num_clusters = min(new, phys);
121 */
122 unsigned int num_clusters =
123 min(num_clusters_new, num_clusters_phys);
124
125 /*
126 * Follow FAT chain
127 * (defensive coding - works fine even with corrupted FAT table
128 */
129 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
130 clu.dir += num_clusters;
131 clu.size -= num_clusters;
132 } else {
133 while (num_clusters > 0) {
134 last_clu = clu.dir;
135 if (exfat_get_next_cluster(sb, &(clu.dir)))
136 return -EIO;
137
138 num_clusters--;
139 clu.size--;
140 }
141 }
142 } else {
143 ei->flags = ALLOC_NO_FAT_CHAIN;
144 ei->start_clu = EXFAT_EOF_CLUSTER;
145 }
146
147 i_size_write(inode, new_size);
148
149 if (ei->type == TYPE_FILE)
150 ei->attr |= ATTR_ARCHIVE;
151
152 /* update the directory entry */
153 if (!evict) {
154 struct timespec64 ts;
155 struct exfat_dentry *ep, *ep2;
156 struct exfat_entry_set_cache *es;
157 int err;
158
159 es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry,
160 ES_ALL_ENTRIES);
161 if (!es)
162 return -EIO;
163 ep = exfat_get_dentry_cached(es, 0);
164 ep2 = exfat_get_dentry_cached(es, 1);
165
166 ts = current_time(inode);
167 exfat_set_entry_time(sbi, &ts,
168 &ep->dentry.file.modify_tz,
169 &ep->dentry.file.modify_time,
170 &ep->dentry.file.modify_date,
171 &ep->dentry.file.modify_time_cs);
172 ep->dentry.file.attr = cpu_to_le16(ei->attr);
173
174 /* File size should be zero if there is no cluster allocated */
175 if (ei->start_clu == EXFAT_EOF_CLUSTER) {
176 ep2->dentry.stream.valid_size = 0;
177 ep2->dentry.stream.size = 0;
178 } else {
179 ep2->dentry.stream.valid_size = cpu_to_le64(new_size);
180 ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
181 }
182
183 if (new_size == 0) {
184 /* Any directory can not be truncated to zero */
185 WARN_ON(ei->type != TYPE_FILE);
186
187 ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
188 ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
189 }
190
191 exfat_update_dir_chksum_with_entry_set(es);
192 err = exfat_free_dentry_set(es, inode_needs_sync(inode));
193 if (err)
194 return err;
195 }
196
197 /* cut off from the FAT chain */
198 if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
199 last_clu != EXFAT_EOF_CLUSTER) {
200 if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
201 return -EIO;
202 }
203
204 /* invalidate cache and free the clusters */
205 /* clear exfat cache */
206 exfat_cache_inval_inode(inode);
207
208 /* hint information */
209 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
210 ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
211 if (ei->rwoffset > new_size)
212 ei->rwoffset = new_size;
213
214 /* hint_stat will be used if this is directory. */
215 ei->hint_stat.eidx = 0;
216 ei->hint_stat.clu = ei->start_clu;
217 ei->hint_femp.eidx = EXFAT_HINT_NONE;
218
219 /* free the clusters */
220 if (exfat_free_cluster(inode, &clu))
221 return -EIO;
222
223 exfat_clear_volume_dirty(sb);
224
225 return 0;
226}
227
228void exfat_truncate(struct inode *inode, loff_t size)
229{
230 struct super_block *sb = inode->i_sb;
231 struct exfat_sb_info *sbi = EXFAT_SB(sb);
232 unsigned int blocksize = 1 << inode->i_blkbits;
233 loff_t aligned_size;
234 int err;
235
236 mutex_lock(&sbi->s_lock);
237 if (EXFAT_I(inode)->start_clu == 0) {
238 /*
239 * Empty start_clu != ~0 (not allocated)
240 */
241 exfat_fs_error(sb, "tried to truncate zeroed cluster.");
242 goto write_size;
243 }
244
245 err = __exfat_truncate(inode, i_size_read(inode));
246 if (err)
247 goto write_size;
248
249 inode->i_ctime = inode->i_mtime = current_time(inode);
250 if (IS_DIRSYNC(inode))
251 exfat_sync_inode(inode);
252 else
253 mark_inode_dirty(inode);
254
255 inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) &
256 ~(sbi->cluster_size - 1)) >> inode->i_blkbits;
257write_size:
258 aligned_size = i_size_read(inode);
259 if (aligned_size & (blocksize - 1)) {
260 aligned_size |= (blocksize - 1);
261 aligned_size++;
262 }
263
264 if (EXFAT_I(inode)->i_size_ondisk > i_size_read(inode))
265 EXFAT_I(inode)->i_size_ondisk = aligned_size;
266
267 if (EXFAT_I(inode)->i_size_aligned > i_size_read(inode))
268 EXFAT_I(inode)->i_size_aligned = aligned_size;
269 mutex_unlock(&sbi->s_lock);
270}
271
272int exfat_getattr(const struct path *path, struct kstat *stat,
273 unsigned int request_mask, unsigned int query_flags)
274{
275 struct inode *inode = d_backing_inode(path->dentry);
276 struct exfat_inode_info *ei = EXFAT_I(inode);
277
278 generic_fillattr(inode, stat);
279 exfat_truncate_atime(&stat->atime);
280 stat->result_mask |= STATX_BTIME;
281 stat->btime.tv_sec = ei->i_crtime.tv_sec;
282 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
283 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
284 return 0;
285}
286
287int exfat_setattr(struct dentry *dentry, struct iattr *attr)
288{
289 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
290 struct inode *inode = dentry->d_inode;
291 unsigned int ia_valid;
292 int error;
293
294 if ((attr->ia_valid & ATTR_SIZE) &&
295 attr->ia_size > i_size_read(inode)) {
296 error = exfat_cont_expand(inode, attr->ia_size);
297 if (error || attr->ia_valid == ATTR_SIZE)
298 return error;
299 attr->ia_valid &= ~ATTR_SIZE;
300 }
301
302 /* Check for setting the inode time. */
303 ia_valid = attr->ia_valid;
304 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
305 exfat_allow_set_time(sbi, inode)) {
306 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
307 ATTR_TIMES_SET);
308 }
309
310 error = setattr_prepare(dentry, attr);
311 attr->ia_valid = ia_valid;
312 if (error)
313 goto out;
314
315 if (((attr->ia_valid & ATTR_UID) &&
316 !uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
317 ((attr->ia_valid & ATTR_GID) &&
318 !gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
319 ((attr->ia_valid & ATTR_MODE) &&
320 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
321 error = -EPERM;
322 goto out;
323 }
324
325 /*
326 * We don't return -EPERM here. Yes, strange, but this is too
327 * old behavior.
328 */
329 if (attr->ia_valid & ATTR_MODE) {
330 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
331 attr->ia_valid &= ~ATTR_MODE;
332 }
333
334 if (attr->ia_valid & ATTR_SIZE) {
335 error = exfat_block_truncate_page(inode, attr->ia_size);
336 if (error)
337 goto out;
338
339 down_write(&EXFAT_I(inode)->truncate_lock);
340 truncate_setsize(inode, attr->ia_size);
341 exfat_truncate(inode, attr->ia_size);
342 up_write(&EXFAT_I(inode)->truncate_lock);
343 }
344
345 setattr_copy(inode, attr);
346 exfat_truncate_atime(&inode->i_atime);
347 mark_inode_dirty(inode);
348
349out:
350 return error;
351}
352
353int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
354{
355 struct inode *inode = filp->f_mapping->host;
356 int err;
357
358 err = __generic_file_fsync(filp, start, end, datasync);
359 if (err)
360 return err;
361
362 err = sync_blockdev(inode->i_sb->s_bdev);
363 if (err)
364 return err;
365
366 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
367}
368
369const struct file_operations exfat_file_operations = {
370 .llseek = generic_file_llseek,
371 .read_iter = generic_file_read_iter,
372 .write_iter = generic_file_write_iter,
373 .mmap = generic_file_mmap,
374 .fsync = exfat_file_fsync,
375 .splice_read = generic_file_splice_read,
376 .splice_write = iter_file_splice_write,
377};
378
379const struct inode_operations exfat_file_inode_operations = {
380 .setattr = exfat_setattr,
381 .getattr = exfat_getattr,
382};
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#include <linux/fsnotify.h>
12#include <linux/security.h>
13#include <linux/msdos_fs.h>
14#include <linux/writeback.h>
15
16#include "exfat_raw.h"
17#include "exfat_fs.h"
18
19static int exfat_cont_expand(struct inode *inode, loff_t size)
20{
21 int ret;
22 unsigned int num_clusters, new_num_clusters, last_clu;
23 struct exfat_inode_info *ei = EXFAT_I(inode);
24 struct super_block *sb = inode->i_sb;
25 struct exfat_sb_info *sbi = EXFAT_SB(sb);
26 struct exfat_chain clu;
27
28 ret = inode_newsize_ok(inode, size);
29 if (ret)
30 return ret;
31
32 num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
33 new_num_clusters = EXFAT_B_TO_CLU_ROUND_UP(size, sbi);
34
35 if (new_num_clusters == num_clusters)
36 goto out;
37
38 if (num_clusters) {
39 exfat_chain_set(&clu, ei->start_clu, num_clusters, ei->flags);
40 ret = exfat_find_last_cluster(sb, &clu, &last_clu);
41 if (ret)
42 return ret;
43
44 clu.dir = last_clu + 1;
45 } else {
46 last_clu = EXFAT_EOF_CLUSTER;
47 clu.dir = EXFAT_EOF_CLUSTER;
48 }
49
50 clu.size = 0;
51 clu.flags = ei->flags;
52
53 ret = exfat_alloc_cluster(inode, new_num_clusters - num_clusters,
54 &clu, inode_needs_sync(inode));
55 if (ret)
56 return ret;
57
58 /* Append new clusters to chain */
59 if (num_clusters) {
60 if (clu.flags != ei->flags)
61 if (exfat_chain_cont_cluster(sb, ei->start_clu, num_clusters))
62 goto free_clu;
63
64 if (clu.flags == ALLOC_FAT_CHAIN)
65 if (exfat_ent_set(sb, last_clu, clu.dir))
66 goto free_clu;
67 } else
68 ei->start_clu = clu.dir;
69
70 ei->flags = clu.flags;
71
72out:
73 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
74 /* Expanded range not zeroed, do not update valid_size */
75 i_size_write(inode, size);
76
77 inode->i_blocks = round_up(size, sbi->cluster_size) >> 9;
78 mark_inode_dirty(inode);
79
80 if (IS_SYNC(inode))
81 return write_inode_now(inode, 1);
82
83 return 0;
84
85free_clu:
86 exfat_free_cluster(inode, &clu);
87 return -EIO;
88}
89
90static bool exfat_allow_set_time(struct mnt_idmap *idmap,
91 struct exfat_sb_info *sbi, struct inode *inode)
92{
93 mode_t allow_utime = sbi->options.allow_utime;
94
95 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
96 current_fsuid())) {
97 if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode)))
98 allow_utime >>= 3;
99 if (allow_utime & MAY_WRITE)
100 return true;
101 }
102
103 /* use a default check */
104 return false;
105}
106
107static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
108 struct inode *inode, umode_t *mode_ptr)
109{
110 mode_t i_mode, mask, perm;
111
112 i_mode = inode->i_mode;
113
114 mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
115 sbi->options.fs_fmask : sbi->options.fs_dmask;
116 perm = *mode_ptr & ~(S_IFMT | mask);
117
118 /* Of the r and x bits, all (subject to umask) must be present.*/
119 if ((perm & 0555) != (i_mode & 0555))
120 return -EPERM;
121
122 if (exfat_mode_can_hold_ro(inode)) {
123 /*
124 * Of the w bits, either all (subject to umask) or none must
125 * be present.
126 */
127 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
128 return -EPERM;
129 } else {
130 /*
131 * If exfat_mode_can_hold_ro(inode) is false, can't change
132 * w bits.
133 */
134 if ((perm & 0222) != (0222 & ~mask))
135 return -EPERM;
136 }
137
138 *mode_ptr &= S_IFMT | perm;
139
140 return 0;
141}
142
143/* resize the file length */
144int __exfat_truncate(struct inode *inode)
145{
146 unsigned int num_clusters_new, num_clusters_phys;
147 unsigned int last_clu = EXFAT_FREE_CLUSTER;
148 struct exfat_chain clu;
149 struct super_block *sb = inode->i_sb;
150 struct exfat_sb_info *sbi = EXFAT_SB(sb);
151 struct exfat_inode_info *ei = EXFAT_I(inode);
152
153 /* check if the given file ID is opened */
154 if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
155 return -EPERM;
156
157 exfat_set_volume_dirty(sb);
158
159 num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
160 num_clusters_phys = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
161
162 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
163
164 if (i_size_read(inode) > 0) {
165 /*
166 * Truncate FAT chain num_clusters after the first cluster
167 * num_clusters = min(new, phys);
168 */
169 unsigned int num_clusters =
170 min(num_clusters_new, num_clusters_phys);
171
172 /*
173 * Follow FAT chain
174 * (defensive coding - works fine even with corrupted FAT table
175 */
176 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
177 clu.dir += num_clusters;
178 clu.size -= num_clusters;
179 } else {
180 while (num_clusters > 0) {
181 last_clu = clu.dir;
182 if (exfat_get_next_cluster(sb, &(clu.dir)))
183 return -EIO;
184
185 num_clusters--;
186 clu.size--;
187 }
188 }
189 } else {
190 ei->flags = ALLOC_NO_FAT_CHAIN;
191 ei->start_clu = EXFAT_EOF_CLUSTER;
192 }
193
194 if (i_size_read(inode) < ei->valid_size)
195 ei->valid_size = i_size_read(inode);
196
197 if (ei->type == TYPE_FILE)
198 ei->attr |= EXFAT_ATTR_ARCHIVE;
199
200 /*
201 * update the directory entry
202 *
203 * If the directory entry is updated by mark_inode_dirty(), the
204 * directory entry will be written after a writeback cycle of
205 * updating the bitmap/FAT, which may result in clusters being
206 * freed but referenced by the directory entry in the event of a
207 * sudden power failure.
208 * __exfat_write_inode() is called for directory entry, bitmap
209 * and FAT to be written in a same writeback.
210 */
211 if (__exfat_write_inode(inode, inode_needs_sync(inode)))
212 return -EIO;
213
214 /* cut off from the FAT chain */
215 if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
216 last_clu != EXFAT_EOF_CLUSTER) {
217 if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
218 return -EIO;
219 }
220
221 /* invalidate cache and free the clusters */
222 /* clear exfat cache */
223 exfat_cache_inval_inode(inode);
224
225 /* hint information */
226 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
227 ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
228
229 /* hint_stat will be used if this is directory. */
230 ei->hint_stat.eidx = 0;
231 ei->hint_stat.clu = ei->start_clu;
232 ei->hint_femp.eidx = EXFAT_HINT_NONE;
233
234 /* free the clusters */
235 if (exfat_free_cluster(inode, &clu))
236 return -EIO;
237
238 return 0;
239}
240
241void exfat_truncate(struct inode *inode)
242{
243 struct super_block *sb = inode->i_sb;
244 struct exfat_sb_info *sbi = EXFAT_SB(sb);
245 struct exfat_inode_info *ei = EXFAT_I(inode);
246 int err;
247
248 mutex_lock(&sbi->s_lock);
249 if (ei->start_clu == 0) {
250 /*
251 * Empty start_clu != ~0 (not allocated)
252 */
253 exfat_fs_error(sb, "tried to truncate zeroed cluster.");
254 goto write_size;
255 }
256
257 err = __exfat_truncate(inode);
258 if (err)
259 goto write_size;
260
261 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
262write_size:
263 mutex_unlock(&sbi->s_lock);
264}
265
266int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
267 struct kstat *stat, unsigned int request_mask,
268 unsigned int query_flags)
269{
270 struct inode *inode = d_backing_inode(path->dentry);
271 struct exfat_inode_info *ei = EXFAT_I(inode);
272
273 generic_fillattr(idmap, request_mask, inode, stat);
274 exfat_truncate_atime(&stat->atime);
275 stat->result_mask |= STATX_BTIME;
276 stat->btime.tv_sec = ei->i_crtime.tv_sec;
277 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
278 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
279 return 0;
280}
281
282int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
283 struct iattr *attr)
284{
285 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
286 struct inode *inode = dentry->d_inode;
287 unsigned int ia_valid;
288 int error;
289
290 if (unlikely(exfat_forced_shutdown(inode->i_sb)))
291 return -EIO;
292
293 if ((attr->ia_valid & ATTR_SIZE) &&
294 attr->ia_size > i_size_read(inode)) {
295 error = exfat_cont_expand(inode, attr->ia_size);
296 if (error || attr->ia_valid == ATTR_SIZE)
297 return error;
298 attr->ia_valid &= ~ATTR_SIZE;
299 }
300
301 /* Check for setting the inode time. */
302 ia_valid = attr->ia_valid;
303 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
304 exfat_allow_set_time(idmap, sbi, inode)) {
305 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
306 ATTR_TIMES_SET);
307 }
308
309 error = setattr_prepare(idmap, dentry, attr);
310 attr->ia_valid = ia_valid;
311 if (error)
312 goto out;
313
314 if (((attr->ia_valid & ATTR_UID) &&
315 (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid),
316 sbi->options.fs_uid))) ||
317 ((attr->ia_valid & ATTR_GID) &&
318 (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid),
319 sbi->options.fs_gid))) ||
320 ((attr->ia_valid & ATTR_MODE) &&
321 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
322 error = -EPERM;
323 goto out;
324 }
325
326 /*
327 * We don't return -EPERM here. Yes, strange, but this is too
328 * old behavior.
329 */
330 if (attr->ia_valid & ATTR_MODE) {
331 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
332 attr->ia_valid &= ~ATTR_MODE;
333 }
334
335 if (attr->ia_valid & ATTR_SIZE)
336 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
337
338 setattr_copy(idmap, inode, attr);
339 exfat_truncate_inode_atime(inode);
340
341 if (attr->ia_valid & ATTR_SIZE) {
342 error = exfat_block_truncate_page(inode, attr->ia_size);
343 if (error)
344 goto out;
345
346 down_write(&EXFAT_I(inode)->truncate_lock);
347 truncate_setsize(inode, attr->ia_size);
348
349 /*
350 * __exfat_write_inode() is called from exfat_truncate(), inode
351 * is already written by it, so mark_inode_dirty() is unneeded.
352 */
353 exfat_truncate(inode);
354 up_write(&EXFAT_I(inode)->truncate_lock);
355 } else
356 mark_inode_dirty(inode);
357
358out:
359 return error;
360}
361
362/*
363 * modified ioctls from fat/file.c by Welmer Almesberger
364 */
365static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
366{
367 u32 attr;
368
369 inode_lock_shared(inode);
370 attr = exfat_make_attr(inode);
371 inode_unlock_shared(inode);
372
373 return put_user(attr, user_attr);
374}
375
376static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
377{
378 struct inode *inode = file_inode(file);
379 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
380 int is_dir = S_ISDIR(inode->i_mode);
381 u32 attr, oldattr;
382 struct iattr ia;
383 int err;
384
385 err = get_user(attr, user_attr);
386 if (err)
387 goto out;
388
389 err = mnt_want_write_file(file);
390 if (err)
391 goto out;
392 inode_lock(inode);
393
394 oldattr = exfat_make_attr(inode);
395
396 /*
397 * Mask attributes so we don't set reserved fields.
398 */
399 attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM |
400 EXFAT_ATTR_ARCHIVE);
401 attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0);
402
403 /* Equivalent to a chmod() */
404 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
405 ia.ia_ctime = current_time(inode);
406 if (is_dir)
407 ia.ia_mode = exfat_make_mode(sbi, attr, 0777);
408 else
409 ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111));
410
411 /* The root directory has no attributes */
412 if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) {
413 err = -EINVAL;
414 goto out_unlock_inode;
415 }
416
417 if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) &&
418 !capable(CAP_LINUX_IMMUTABLE)) {
419 err = -EPERM;
420 goto out_unlock_inode;
421 }
422
423 /*
424 * The security check is questionable... We single
425 * out the RO attribute for checking by the security
426 * module, just because it maps to a file mode.
427 */
428 err = security_inode_setattr(file_mnt_idmap(file),
429 file->f_path.dentry, &ia);
430 if (err)
431 goto out_unlock_inode;
432
433 /* This MUST be done before doing anything irreversible... */
434 err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia);
435 if (err)
436 goto out_unlock_inode;
437
438 fsnotify_change(file->f_path.dentry, ia.ia_valid);
439
440 exfat_save_attr(inode, attr);
441 mark_inode_dirty(inode);
442out_unlock_inode:
443 inode_unlock(inode);
444 mnt_drop_write_file(file);
445out:
446 return err;
447}
448
449static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
450{
451 struct fstrim_range range;
452 int ret = 0;
453
454 if (!capable(CAP_SYS_ADMIN))
455 return -EPERM;
456
457 if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
458 return -EOPNOTSUPP;
459
460 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
461 return -EFAULT;
462
463 range.minlen = max_t(unsigned int, range.minlen,
464 bdev_discard_granularity(inode->i_sb->s_bdev));
465
466 ret = exfat_trim_fs(inode, &range);
467 if (ret < 0)
468 return ret;
469
470 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
471 return -EFAULT;
472
473 return 0;
474}
475
476static int exfat_ioctl_shutdown(struct super_block *sb, unsigned long arg)
477{
478 u32 flags;
479
480 if (!capable(CAP_SYS_ADMIN))
481 return -EPERM;
482
483 if (get_user(flags, (__u32 __user *)arg))
484 return -EFAULT;
485
486 return exfat_force_shutdown(sb, flags);
487}
488
489long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
490{
491 struct inode *inode = file_inode(filp);
492 u32 __user *user_attr = (u32 __user *)arg;
493
494 switch (cmd) {
495 case FAT_IOCTL_GET_ATTRIBUTES:
496 return exfat_ioctl_get_attributes(inode, user_attr);
497 case FAT_IOCTL_SET_ATTRIBUTES:
498 return exfat_ioctl_set_attributes(filp, user_attr);
499 case EXFAT_IOC_SHUTDOWN:
500 return exfat_ioctl_shutdown(inode->i_sb, arg);
501 case FITRIM:
502 return exfat_ioctl_fitrim(inode, arg);
503 default:
504 return -ENOTTY;
505 }
506}
507
508#ifdef CONFIG_COMPAT
509long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
510 unsigned long arg)
511{
512 return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
513}
514#endif
515
516int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
517{
518 struct inode *inode = filp->f_mapping->host;
519 int err;
520
521 if (unlikely(exfat_forced_shutdown(inode->i_sb)))
522 return -EIO;
523
524 err = __generic_file_fsync(filp, start, end, datasync);
525 if (err)
526 return err;
527
528 err = sync_blockdev(inode->i_sb->s_bdev);
529 if (err)
530 return err;
531
532 return blkdev_issue_flush(inode->i_sb->s_bdev);
533}
534
535static int exfat_extend_valid_size(struct file *file, loff_t new_valid_size)
536{
537 int err;
538 loff_t pos;
539 struct inode *inode = file_inode(file);
540 struct exfat_inode_info *ei = EXFAT_I(inode);
541 struct address_space *mapping = inode->i_mapping;
542 const struct address_space_operations *ops = mapping->a_ops;
543
544 pos = ei->valid_size;
545 while (pos < new_valid_size) {
546 u32 len;
547 struct folio *folio;
548 unsigned long off;
549
550 len = PAGE_SIZE - (pos & (PAGE_SIZE - 1));
551 if (pos + len > new_valid_size)
552 len = new_valid_size - pos;
553
554 err = ops->write_begin(file, mapping, pos, len, &folio, NULL);
555 if (err)
556 goto out;
557
558 off = offset_in_folio(folio, pos);
559 folio_zero_new_buffers(folio, off, off + len);
560
561 err = ops->write_end(file, mapping, pos, len, len, folio, NULL);
562 if (err < 0)
563 goto out;
564 pos += len;
565
566 balance_dirty_pages_ratelimited(mapping);
567 cond_resched();
568 }
569
570 return 0;
571
572out:
573 return err;
574}
575
576static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
577{
578 ssize_t ret;
579 struct file *file = iocb->ki_filp;
580 struct inode *inode = file_inode(file);
581 struct exfat_inode_info *ei = EXFAT_I(inode);
582 loff_t pos = iocb->ki_pos;
583 loff_t valid_size;
584
585 inode_lock(inode);
586
587 valid_size = ei->valid_size;
588
589 ret = generic_write_checks(iocb, iter);
590 if (ret <= 0)
591 goto unlock;
592
593 if (iocb->ki_flags & IOCB_DIRECT) {
594 unsigned long align = pos | iov_iter_alignment(iter);
595
596 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
597 !IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev))) {
598 ret = -EINVAL;
599 goto unlock;
600 }
601 }
602
603 if (pos > valid_size) {
604 ret = exfat_extend_valid_size(file, pos);
605 if (ret < 0 && ret != -ENOSPC) {
606 exfat_err(inode->i_sb,
607 "write: fail to zero from %llu to %llu(%zd)",
608 valid_size, pos, ret);
609 }
610 if (ret < 0)
611 goto unlock;
612 }
613
614 ret = __generic_file_write_iter(iocb, iter);
615 if (ret < 0)
616 goto unlock;
617
618 inode_unlock(inode);
619
620 if (pos > valid_size)
621 pos = valid_size;
622
623 if (iocb_is_dsync(iocb) && iocb->ki_pos > pos) {
624 ssize_t err = vfs_fsync_range(file, pos, iocb->ki_pos - 1,
625 iocb->ki_flags & IOCB_SYNC);
626 if (err < 0)
627 return err;
628 }
629
630 return ret;
631
632unlock:
633 inode_unlock(inode);
634
635 return ret;
636}
637
638static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf)
639{
640 int err;
641 struct vm_area_struct *vma = vmf->vma;
642 struct file *file = vma->vm_file;
643 struct inode *inode = file_inode(file);
644 struct exfat_inode_info *ei = EXFAT_I(inode);
645 loff_t start, end;
646
647 if (!inode_trylock(inode))
648 return VM_FAULT_RETRY;
649
650 start = ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
651 end = min_t(loff_t, i_size_read(inode),
652 start + vma->vm_end - vma->vm_start);
653
654 if (ei->valid_size < end) {
655 err = exfat_extend_valid_size(file, end);
656 if (err < 0) {
657 inode_unlock(inode);
658 return vmf_fs_error(err);
659 }
660 }
661
662 inode_unlock(inode);
663
664 return filemap_page_mkwrite(vmf);
665}
666
667static const struct vm_operations_struct exfat_file_vm_ops = {
668 .fault = filemap_fault,
669 .map_pages = filemap_map_pages,
670 .page_mkwrite = exfat_page_mkwrite,
671};
672
673static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma)
674{
675 file_accessed(file);
676 vma->vm_ops = &exfat_file_vm_ops;
677 return 0;
678}
679
680const struct file_operations exfat_file_operations = {
681 .llseek = generic_file_llseek,
682 .read_iter = generic_file_read_iter,
683 .write_iter = exfat_file_write_iter,
684 .unlocked_ioctl = exfat_ioctl,
685#ifdef CONFIG_COMPAT
686 .compat_ioctl = exfat_compat_ioctl,
687#endif
688 .mmap = exfat_file_mmap,
689 .fsync = exfat_file_fsync,
690 .splice_read = filemap_splice_read,
691 .splice_write = iter_file_splice_write,
692};
693
694const struct inode_operations exfat_file_inode_operations = {
695 .setattr = exfat_setattr,
696 .getattr = exfat_getattr,
697};