file.c 19 KB

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