file.c 17 KB

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