Commit | Line | Data |
---|---|---|
98d91704 NJ |
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> | |
654762df | 7 | #include <linux/compat.h> |
98d91704 NJ |
8 | #include <linux/cred.h> |
9 | #include <linux/buffer_head.h> | |
5267456e | 10 | #include <linux/blkdev.h> |
0ab8ba71 JC |
11 | #include <linux/fsnotify.h> |
12 | #include <linux/security.h> | |
13 | #include <linux/msdos_fs.h> | |
11a347fb | 14 | #include <linux/writeback.h> |
98d91704 NJ |
15 | |
16 | #include "exfat_raw.h" | |
17 | #include "exfat_fs.h" | |
18 | ||
19 | static int exfat_cont_expand(struct inode *inode, loff_t size) | |
20 | { | |
f55c096f YM |
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; | |
98d91704 | 27 | |
f55c096f YM |
28 | ret = inode_newsize_ok(inode, size); |
29 | if (ret) | |
30 | return ret; | |
98d91704 | 31 | |
fba27cf0 | 32 | num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); |
f55c096f YM |
33 | new_num_clusters = EXFAT_B_TO_CLU_ROUND_UP(size, sbi); |
34 | ||
35 | if (new_num_clusters == num_clusters) | |
36 | goto out; | |
37 | ||
3a784504 YM |
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 | } | |
f55c096f | 49 | |
f55c096f YM |
50 | clu.size = 0; |
51 | clu.flags = ei->flags; | |
52 | ||
53 | ret = exfat_alloc_cluster(inode, new_num_clusters - num_clusters, | |
d7ed5232 | 54 | &clu, inode_needs_sync(inode)); |
f55c096f YM |
55 | if (ret) |
56 | return ret; | |
57 | ||
58 | /* Append new clusters to chain */ | |
3a784504 YM |
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; | |
f55c096f | 63 | |
3a784504 YM |
64 | if (clu.flags == ALLOC_FAT_CHAIN) |
65 | if (exfat_ent_set(sb, last_clu, clu.dir)) | |
66 | goto free_clu; | |
67 | } else | |
f55c096f YM |
68 | ei->start_clu = clu.dir; |
69 | ||
3a784504 YM |
70 | ei->flags = clu.flags; |
71 | ||
f55c096f | 72 | out: |
4c72a36e | 73 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
f55c096f YM |
74 | /* Expanded range not zeroed, do not update valid_size */ |
75 | i_size_write(inode, size); | |
98d91704 | 76 | |
f55c096f | 77 | inode->i_blocks = round_up(size, sbi->cluster_size) >> 9; |
d7ed5232 | 78 | mark_inode_dirty(inode); |
f55c096f | 79 | |
d7ed5232 | 80 | if (IS_SYNC(inode)) |
f55c096f YM |
81 | return write_inode_now(inode, 1); |
82 | ||
f55c096f | 83 | return 0; |
98d91704 | 84 | |
f55c096f YM |
85 | free_clu: |
86 | exfat_free_cluster(inode, &clu); | |
87 | return -EIO; | |
98d91704 NJ |
88 | } |
89 | ||
22482176 MJ |
90 | static bool exfat_allow_set_time(struct mnt_idmap *idmap, |
91 | struct exfat_sb_info *sbi, struct inode *inode) | |
98d91704 NJ |
92 | { |
93 | mode_t allow_utime = sbi->options.allow_utime; | |
94 | ||
22482176 MJ |
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))) | |
98d91704 NJ |
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 | ||
107 | static 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 */ | |
f7cde967 | 144 | int __exfat_truncate(struct inode *inode) |
98d91704 NJ |
145 | { |
146 | unsigned int num_clusters_new, num_clusters_phys; | |
147 | unsigned int last_clu = EXFAT_FREE_CLUSTER; | |
148 | struct exfat_chain clu; | |
98d91704 NJ |
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); | |
98d91704 NJ |
152 | |
153 | /* check if the given file ID is opened */ | |
154 | if (ei->type != TYPE_FILE && ei->type != TYPE_DIR) | |
155 | return -EPERM; | |
156 | ||
7018ec68 | 157 | exfat_set_volume_dirty(sb); |
98d91704 NJ |
158 | |
159 | num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); | |
fba27cf0 | 160 | num_clusters_phys = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); |
98d91704 NJ |
161 | |
162 | exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags); | |
163 | ||
f7cde967 | 164 | if (i_size_read(inode) > 0) { |
98d91704 NJ |
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 | ||
11a347fb YM |
194 | if (i_size_read(inode) < ei->valid_size) |
195 | ei->valid_size = i_size_read(inode); | |
196 | ||
98d91704 | 197 | if (ei->type == TYPE_FILE) |
0ab8ba71 | 198 | ei->attr |= EXFAT_ATTR_ARCHIVE; |
98d91704 | 199 | |
4493895b YM |
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 | */ | |
23e6e1c9 YM |
211 | if (__exfat_write_inode(inode, inode_needs_sync(inode))) |
212 | return -EIO; | |
98d91704 NJ |
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; | |
98d91704 NJ |
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 | ||
98d91704 NJ |
238 | return 0; |
239 | } | |
240 | ||
e981917b | 241 | void exfat_truncate(struct inode *inode) |
98d91704 NJ |
242 | { |
243 | struct super_block *sb = inode->i_sb; | |
244 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
7dee6f57 | 245 | struct exfat_inode_info *ei = EXFAT_I(inode); |
98d91704 NJ |
246 | int err; |
247 | ||
248 | mutex_lock(&sbi->s_lock); | |
7dee6f57 | 249 | if (ei->start_clu == 0) { |
98d91704 NJ |
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 | ||
f7cde967 | 257 | err = __exfat_truncate(inode); |
98d91704 NJ |
258 | if (err) |
259 | goto write_size; | |
260 | ||
39c1ce8e | 261 | inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9; |
98d91704 | 262 | write_size: |
98d91704 NJ |
263 | mutex_unlock(&sbi->s_lock); |
264 | } | |
265 | ||
b74d24f7 | 266 | int exfat_getattr(struct mnt_idmap *idmap, const struct path *path, |
549c7297 CB |
267 | struct kstat *stat, unsigned int request_mask, |
268 | unsigned int query_flags) | |
98d91704 NJ |
269 | { |
270 | struct inode *inode = d_backing_inode(path->dentry); | |
271 | struct exfat_inode_info *ei = EXFAT_I(inode); | |
272 | ||
22482176 | 273 | generic_fillattr(idmap, request_mask, inode, stat); |
81df1ad4 | 274 | exfat_truncate_atime(&stat->atime); |
98d91704 NJ |
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 | ||
c1632a0f | 282 | int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 283 | struct iattr *attr) |
98d91704 NJ |
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 | ||
f761fcdd DC |
290 | if (unlikely(exfat_forced_shutdown(inode->i_sb))) |
291 | return -EIO; | |
292 | ||
98d91704 NJ |
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)) && | |
22482176 | 304 | exfat_allow_set_time(idmap, sbi, inode)) { |
98d91704 NJ |
305 | attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | |
306 | ATTR_TIMES_SET); | |
307 | } | |
308 | ||
22482176 | 309 | error = setattr_prepare(idmap, dentry, attr); |
98d91704 NJ |
310 | attr->ia_valid = ia_valid; |
311 | if (error) | |
312 | goto out; | |
313 | ||
314 | if (((attr->ia_valid & ATTR_UID) && | |
22482176 MJ |
315 | (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid), |
316 | sbi->options.fs_uid))) || | |
98d91704 | 317 | ((attr->ia_valid & ATTR_GID) && |
22482176 MJ |
318 | (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid), |
319 | sbi->options.fs_gid))) || | |
98d91704 NJ |
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 | ||
4493895b | 335 | if (attr->ia_valid & ATTR_SIZE) |
4c72a36e | 336 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
4493895b | 337 | |
22482176 | 338 | setattr_copy(idmap, inode, attr); |
4c72a36e | 339 | exfat_truncate_inode_atime(inode); |
4493895b | 340 | |
98d91704 NJ |
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); | |
4493895b YM |
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 | */ | |
e981917b | 353 | exfat_truncate(inode); |
98d91704 | 354 | up_write(&EXFAT_I(inode)->truncate_lock); |
4493895b YM |
355 | } else |
356 | mark_inode_dirty(inode); | |
98d91704 NJ |
357 | |
358 | out: | |
359 | return error; | |
360 | } | |
361 | ||
0ab8ba71 JC |
362 | /* |
363 | * modified ioctls from fat/file.c by Welmer Almesberger | |
364 | */ | |
365 | static 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 | ||
376 | static 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); | |
442 | out_unlock_inode: | |
443 | inode_unlock(inode); | |
444 | mnt_drop_write_file(file); | |
445 | out: | |
446 | return err; | |
447 | } | |
448 | ||
654762df HK |
449 | static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg) |
450 | { | |
654762df HK |
451 | struct fstrim_range range; |
452 | int ret = 0; | |
453 | ||
454 | if (!capable(CAP_SYS_ADMIN)) | |
455 | return -EPERM; | |
456 | ||
70200574 | 457 | if (!bdev_max_discard_sectors(inode->i_sb->s_bdev)) |
654762df HK |
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, | |
7b47ef52 | 464 | bdev_discard_granularity(inode->i_sb->s_bdev)); |
654762df HK |
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 | ||
f761fcdd DC |
476 | static 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 | ||
654762df HK |
489 | long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
490 | { | |
491 | struct inode *inode = file_inode(filp); | |
0ab8ba71 | 492 | u32 __user *user_attr = (u32 __user *)arg; |
654762df HK |
493 | |
494 | switch (cmd) { | |
0ab8ba71 JC |
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); | |
f761fcdd DC |
499 | case EXFAT_IOC_SHUTDOWN: |
500 | return exfat_ioctl_shutdown(inode->i_sb, arg); | |
654762df HK |
501 | case FITRIM: |
502 | return exfat_ioctl_fitrim(inode, arg); | |
503 | default: | |
504 | return -ENOTTY; | |
505 | } | |
506 | } | |
507 | ||
508 | #ifdef CONFIG_COMPAT | |
509 | long 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 | ||
5267456e SS |
516 | int 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 | ||
f761fcdd DC |
521 | if (unlikely(exfat_forced_shutdown(inode->i_sb))) |
522 | return -EIO; | |
523 | ||
5267456e SS |
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 | ||
c6bf3f0e | 532 | return blkdev_issue_flush(inode->i_sb->s_bdev); |
5267456e SS |
533 | } |
534 | ||
6630ea49 | 535 | static int exfat_extend_valid_size(struct file *file, loff_t new_valid_size) |
11a347fb YM |
536 | { |
537 | int err; | |
6630ea49 | 538 | loff_t pos; |
11a347fb | 539 | struct inode *inode = file_inode(file); |
6630ea49 | 540 | struct exfat_inode_info *ei = EXFAT_I(inode); |
11a347fb YM |
541 | struct address_space *mapping = inode->i_mapping; |
542 | const struct address_space_operations *ops = mapping->a_ops; | |
543 | ||
6630ea49 YM |
544 | pos = ei->valid_size; |
545 | while (pos < new_valid_size) { | |
546 | u32 len; | |
1da86618 | 547 | struct folio *folio; |
98e2fb26 | 548 | unsigned long off; |
11a347fb | 549 | |
6630ea49 YM |
550 | len = PAGE_SIZE - (pos & (PAGE_SIZE - 1)); |
551 | if (pos + len > new_valid_size) | |
552 | len = new_valid_size - pos; | |
11a347fb | 553 | |
6630ea49 | 554 | err = ops->write_begin(file, mapping, pos, len, &folio, NULL); |
11a347fb YM |
555 | if (err) |
556 | goto out; | |
557 | ||
98e2fb26 YM |
558 | off = offset_in_folio(folio, pos); |
559 | folio_zero_new_buffers(folio, off, off + len); | |
560 | ||
6630ea49 | 561 | err = ops->write_end(file, mapping, pos, len, len, folio, NULL); |
11a347fb YM |
562 | if (err < 0) |
563 | goto out; | |
6630ea49 | 564 | pos += len; |
11a347fb YM |
565 | |
566 | balance_dirty_pages_ratelimited(mapping); | |
567 | cond_resched(); | |
568 | } | |
569 | ||
98e2fb26 YM |
570 | return 0; |
571 | ||
11a347fb YM |
572 | out: |
573 | return err; | |
574 | } | |
575 | ||
576 | static 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 | ||
2e94e5bb YM |
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 | ||
11a347fb | 603 | if (pos > valid_size) { |
6630ea49 | 604 | ret = exfat_extend_valid_size(file, pos); |
11a347fb YM |
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 | ||
632 | unlock: | |
633 | inode_unlock(inode); | |
634 | ||
635 | return ret; | |
636 | } | |
637 | ||
6630ea49 | 638 | static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf) |
11a347fb | 639 | { |
6630ea49 YM |
640 | int err; |
641 | struct vm_area_struct *vma = vmf->vma; | |
642 | struct file *file = vma->vm_file; | |
11a347fb YM |
643 | struct inode *inode = file_inode(file); |
644 | struct exfat_inode_info *ei = EXFAT_I(inode); | |
6630ea49 YM |
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), | |
11a347fb YM |
652 | start + vma->vm_end - vma->vm_start); |
653 | ||
6630ea49 YM |
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); | |
11a347fb YM |
659 | } |
660 | } | |
661 | ||
6630ea49 YM |
662 | inode_unlock(inode); |
663 | ||
664 | return filemap_page_mkwrite(vmf); | |
665 | } | |
666 | ||
667 | static 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 | ||
673 | static 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; | |
11a347fb YM |
678 | } |
679 | ||
98d91704 | 680 | const struct file_operations exfat_file_operations = { |
03577948 ES |
681 | .llseek = generic_file_llseek, |
682 | .read_iter = generic_file_read_iter, | |
11a347fb | 683 | .write_iter = exfat_file_write_iter, |
654762df HK |
684 | .unlocked_ioctl = exfat_ioctl, |
685 | #ifdef CONFIG_COMPAT | |
686 | .compat_ioctl = exfat_compat_ioctl, | |
687 | #endif | |
11a347fb | 688 | .mmap = exfat_file_mmap, |
5267456e | 689 | .fsync = exfat_file_fsync, |
2cb1e089 | 690 | .splice_read = filemap_splice_read, |
03577948 | 691 | .splice_write = iter_file_splice_write, |
98d91704 NJ |
692 | }; |
693 | ||
694 | const struct inode_operations exfat_file_inode_operations = { | |
695 | .setattr = exfat_setattr, | |
696 | .getattr = exfat_getattr, | |
697 | }; |