file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/fat/file.c
  4. *
  5. * Written 1992,1993 by Werner Almesberger
  6. *
  7. * regular file handling primitives for fat-based filesystems
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/module.h>
  11. #include <linux/compat.h>
  12. #include <linux/mount.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/fsnotify.h>
  16. #include <linux/security.h>
  17. #include <linux/falloc.h>
  18. #include "fat.h"
  19. static long fat_fallocate(struct file *file, int mode,
  20. loff_t offset, loff_t len);
  21. static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
  22. {
  23. u32 attr;
  24. inode_lock_shared(inode);
  25. attr = fat_make_attrs(inode);
  26. inode_unlock_shared(inode);
  27. return put_user(attr, user_attr);
  28. }
  29. static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
  30. {
  31. struct inode *inode = file_inode(file);
  32. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  33. int is_dir = S_ISDIR(inode->i_mode);
  34. u32 attr, oldattr;
  35. struct iattr ia;
  36. int err;
  37. err = get_user(attr, user_attr);
  38. if (err)
  39. goto out;
  40. err = mnt_want_write_file(file);
  41. if (err)
  42. goto out;
  43. inode_lock(inode);
  44. /*
  45. * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
  46. * prevents the user from turning us into a VFAT
  47. * longname entry. Also, we obviously can't set
  48. * any of the NTFS attributes in the high 24 bits.
  49. */
  50. attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
  51. /* Merge in ATTR_VOLUME and ATTR_DIR */
  52. attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
  53. (is_dir ? ATTR_DIR : 0);
  54. oldattr = fat_make_attrs(inode);
  55. /* Equivalent to a chmod() */
  56. ia.ia_valid = ATTR_MODE | ATTR_CTIME;
  57. ia.ia_ctime = current_time(inode);
  58. if (is_dir)
  59. ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
  60. else {
  61. ia.ia_mode = fat_make_mode(sbi, attr,
  62. S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
  63. }
  64. /* The root directory has no attributes */
  65. if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
  66. err = -EINVAL;
  67. goto out_unlock_inode;
  68. }
  69. if (sbi->options.sys_immutable &&
  70. ((attr | oldattr) & ATTR_SYS) &&
  71. !capable(CAP_LINUX_IMMUTABLE)) {
  72. err = -EPERM;
  73. goto out_unlock_inode;
  74. }
  75. /*
  76. * The security check is questionable... We single
  77. * out the RO attribute for checking by the security
  78. * module, just because it maps to a file mode.
  79. */
  80. err = security_inode_setattr(file_mnt_idmap(file),
  81. file->f_path.dentry, &ia);
  82. if (err)
  83. goto out_unlock_inode;
  84. /* This MUST be done before doing anything irreversible... */
  85. err = fat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia);
  86. if (err)
  87. goto out_unlock_inode;
  88. fsnotify_change(file->f_path.dentry, ia.ia_valid);
  89. if (sbi->options.sys_immutable) {
  90. if (attr & ATTR_SYS)
  91. inode->i_flags |= S_IMMUTABLE;
  92. else
  93. inode->i_flags &= ~S_IMMUTABLE;
  94. }
  95. fat_save_attrs(inode, attr);
  96. mark_inode_dirty(inode);
  97. out_unlock_inode:
  98. inode_unlock(inode);
  99. mnt_drop_write_file(file);
  100. out:
  101. return err;
  102. }
  103. static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
  104. {
  105. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  106. return put_user(sbi->vol_id, user_attr);
  107. }
  108. static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg)
  109. {
  110. struct super_block *sb = inode->i_sb;
  111. struct fstrim_range __user *user_range;
  112. struct fstrim_range range;
  113. int err;
  114. if (!capable(CAP_SYS_ADMIN))
  115. return -EPERM;
  116. if (!bdev_max_discard_sectors(sb->s_bdev))
  117. return -EOPNOTSUPP;
  118. user_range = (struct fstrim_range __user *)arg;
  119. if (copy_from_user(&range, user_range, sizeof(range)))
  120. return -EFAULT;
  121. range.minlen = max_t(unsigned int, range.minlen,
  122. bdev_discard_granularity(sb->s_bdev));
  123. err = fat_trim_fs(inode, &range);
  124. if (err < 0)
  125. return err;
  126. if (copy_to_user(user_range, &range, sizeof(range)))
  127. return -EFAULT;
  128. return 0;
  129. }
  130. long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  131. {
  132. struct inode *inode = file_inode(filp);
  133. u32 __user *user_attr = (u32 __user *)arg;
  134. switch (cmd) {
  135. case FAT_IOCTL_GET_ATTRIBUTES:
  136. return fat_ioctl_get_attributes(inode, user_attr);
  137. case FAT_IOCTL_SET_ATTRIBUTES:
  138. return fat_ioctl_set_attributes(filp, user_attr);
  139. case FAT_IOCTL_GET_VOLUME_ID:
  140. return fat_ioctl_get_volume_id(inode, user_attr);
  141. case FITRIM:
  142. return fat_ioctl_fitrim(inode, arg);
  143. default:
  144. return -ENOTTY; /* Inappropriate ioctl for device */
  145. }
  146. }
  147. static int fat_file_release(struct inode *inode, struct file *filp)
  148. {
  149. if ((filp->f_mode & FMODE_WRITE) &&
  150. MSDOS_SB(inode->i_sb)->options.flush) {
  151. fat_flush_inodes(inode->i_sb, inode, NULL);
  152. set_current_state(TASK_UNINTERRUPTIBLE);
  153. io_schedule_timeout(HZ/10);
  154. }
  155. return 0;
  156. }
  157. int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  158. {
  159. struct inode *inode = filp->f_mapping->host;
  160. int err;
  161. err = __generic_file_fsync(filp, start, end, datasync);
  162. if (err)
  163. return err;
  164. err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
  165. if (err)
  166. return err;
  167. return blkdev_issue_flush(inode->i_sb->s_bdev);
  168. }
  169. const struct file_operations fat_file_operations = {
  170. .llseek = generic_file_llseek,
  171. .read_iter = generic_file_read_iter,
  172. .write_iter = generic_file_write_iter,
  173. .mmap = generic_file_mmap,
  174. .release = fat_file_release,
  175. .unlocked_ioctl = fat_generic_ioctl,
  176. .compat_ioctl = compat_ptr_ioctl,
  177. .fsync = fat_file_fsync,
  178. .splice_read = filemap_splice_read,
  179. .splice_write = iter_file_splice_write,
  180. .fallocate = fat_fallocate,
  181. };
  182. static int fat_cont_expand(struct inode *inode, loff_t size)
  183. {
  184. struct address_space *mapping = inode->i_mapping;
  185. loff_t start = inode->i_size, count = size - inode->i_size;
  186. int err;
  187. err = generic_cont_expand_simple(inode, size);
  188. if (err)
  189. goto out;
  190. fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
  191. mark_inode_dirty(inode);
  192. if (IS_SYNC(inode)) {
  193. int err2;
  194. /*
  195. * Opencode syncing since we don't have a file open to use
  196. * standard fsync path.
  197. */
  198. err = filemap_fdatawrite_range(mapping, start,
  199. start + count - 1);
  200. err2 = sync_mapping_buffers(mapping);
  201. if (!err)
  202. err = err2;
  203. err2 = write_inode_now(inode, 1);
  204. if (!err)
  205. err = err2;
  206. if (!err) {
  207. err = filemap_fdatawait_range(mapping, start,
  208. start + count - 1);
  209. }
  210. }
  211. out:
  212. return err;
  213. }
  214. /*
  215. * Preallocate space for a file. This implements fat's fallocate file
  216. * operation, which gets called from sys_fallocate system call. User
  217. * space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set
  218. * we just allocate clusters without zeroing them out. Otherwise we
  219. * allocate and zero out clusters via an expanding truncate.
  220. */
  221. static long fat_fallocate(struct file *file, int mode,
  222. loff_t offset, loff_t len)
  223. {
  224. int nr_cluster; /* Number of clusters to be allocated */
  225. loff_t mm_bytes; /* Number of bytes to be allocated for file */
  226. loff_t ondisksize; /* block aligned on-disk size in bytes*/
  227. struct inode *inode = file->f_mapping->host;
  228. struct super_block *sb = inode->i_sb;
  229. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  230. int err = 0;
  231. /* No support for hole punch or other fallocate flags. */
  232. if (mode & ~FALLOC_FL_KEEP_SIZE)
  233. return -EOPNOTSUPP;
  234. /* No support for dir */
  235. if (!S_ISREG(inode->i_mode))
  236. return -EOPNOTSUPP;
  237. inode_lock(inode);
  238. if (mode & FALLOC_FL_KEEP_SIZE) {
  239. ondisksize = inode->i_blocks << 9;
  240. if ((offset + len) <= ondisksize)
  241. goto error;
  242. /* First compute the number of clusters to be allocated */
  243. mm_bytes = offset + len - ondisksize;
  244. nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >>
  245. sbi->cluster_bits;
  246. /* Start the allocation.We are not zeroing out the clusters */
  247. while (nr_cluster-- > 0) {
  248. err = fat_add_cluster(inode);
  249. if (err)
  250. goto error;
  251. }
  252. } else {
  253. if ((offset + len) <= i_size_read(inode))
  254. goto error;
  255. /* This is just an expanding truncate */
  256. err = fat_cont_expand(inode, (offset + len));
  257. }
  258. error:
  259. inode_unlock(inode);
  260. return err;
  261. }
  262. /* Free all clusters after the skip'th cluster. */
  263. static int fat_free(struct inode *inode, int skip)
  264. {
  265. struct super_block *sb = inode->i_sb;
  266. int err, wait, free_start, i_start, i_logstart;
  267. if (MSDOS_I(inode)->i_start == 0)
  268. return 0;
  269. fat_cache_inval_inode(inode);
  270. wait = IS_DIRSYNC(inode);
  271. i_start = free_start = MSDOS_I(inode)->i_start;
  272. i_logstart = MSDOS_I(inode)->i_logstart;
  273. /* First, we write the new file size. */
  274. if (!skip) {
  275. MSDOS_I(inode)->i_start = 0;
  276. MSDOS_I(inode)->i_logstart = 0;
  277. }
  278. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  279. fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
  280. if (wait) {
  281. err = fat_sync_inode(inode);
  282. if (err) {
  283. MSDOS_I(inode)->i_start = i_start;
  284. MSDOS_I(inode)->i_logstart = i_logstart;
  285. return err;
  286. }
  287. } else
  288. mark_inode_dirty(inode);
  289. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  290. if (skip) {
  291. struct fat_entry fatent;
  292. int ret, fclus, dclus;
  293. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  294. if (ret < 0)
  295. return ret;
  296. else if (ret == FAT_ENT_EOF)
  297. return 0;
  298. fatent_init(&fatent);
  299. ret = fat_ent_read(inode, &fatent, dclus);
  300. if (ret == FAT_ENT_EOF) {
  301. fatent_brelse(&fatent);
  302. return 0;
  303. } else if (ret == FAT_ENT_FREE) {
  304. fat_fs_error(sb,
  305. "%s: invalid cluster chain (i_pos %lld)",
  306. __func__, MSDOS_I(inode)->i_pos);
  307. ret = -EIO;
  308. } else if (ret > 0) {
  309. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  310. if (err)
  311. ret = err;
  312. }
  313. fatent_brelse(&fatent);
  314. if (ret < 0)
  315. return ret;
  316. free_start = ret;
  317. }
  318. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  319. /* Freeing the remained cluster chain */
  320. return fat_free_clusters(inode, free_start);
  321. }
  322. void fat_truncate_blocks(struct inode *inode, loff_t offset)
  323. {
  324. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  325. const unsigned int cluster_size = sbi->cluster_size;
  326. int nr_clusters;
  327. /*
  328. * This protects against truncating a file bigger than it was then
  329. * trying to write into the hole.
  330. */
  331. if (MSDOS_I(inode)->mmu_private > offset)
  332. MSDOS_I(inode)->mmu_private = offset;
  333. nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
  334. fat_free(inode, nr_clusters);
  335. fat_flush_inodes(inode->i_sb, inode, NULL);
  336. }
  337. int fat_getattr(struct mnt_idmap *idmap, const struct path *path,
  338. struct kstat *stat, u32 request_mask, unsigned int flags)
  339. {
  340. struct inode *inode = d_inode(path->dentry);
  341. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  342. generic_fillattr(idmap, request_mask, inode, stat);
  343. stat->blksize = sbi->cluster_size;
  344. if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) {
  345. /* Use i_pos for ino. This is used as fileid of nfs. */
  346. stat->ino = fat_i_pos_read(sbi, inode);
  347. }
  348. if (sbi->options.isvfat && request_mask & STATX_BTIME) {
  349. stat->result_mask |= STATX_BTIME;
  350. stat->btime = MSDOS_I(inode)->i_crtime;
  351. }
  352. return 0;
  353. }
  354. EXPORT_SYMBOL_GPL(fat_getattr);
  355. static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
  356. struct inode *inode, umode_t *mode_ptr)
  357. {
  358. umode_t mask, perm;
  359. /*
  360. * Note, the basic check is already done by a caller of
  361. * (attr->ia_mode & ~FAT_VALID_MODE)
  362. */
  363. if (S_ISREG(inode->i_mode))
  364. mask = sbi->options.fs_fmask;
  365. else
  366. mask = sbi->options.fs_dmask;
  367. perm = *mode_ptr & ~(S_IFMT | mask);
  368. /*
  369. * Of the r and x bits, all (subject to umask) must be present. Of the
  370. * w bits, either all (subject to umask) or none must be present.
  371. *
  372. * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
  373. */
  374. if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
  375. return -EPERM;
  376. if (fat_mode_can_hold_ro(inode)) {
  377. if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
  378. return -EPERM;
  379. } else {
  380. if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
  381. return -EPERM;
  382. }
  383. *mode_ptr &= S_IFMT | perm;
  384. return 0;
  385. }
  386. static int fat_allow_set_time(struct mnt_idmap *idmap,
  387. struct msdos_sb_info *sbi, struct inode *inode)
  388. {
  389. umode_t allow_utime = sbi->options.allow_utime;
  390. if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
  391. current_fsuid())) {
  392. if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode)))
  393. allow_utime >>= 3;
  394. if (allow_utime & MAY_WRITE)
  395. return 1;
  396. }
  397. /* use a default check */
  398. return 0;
  399. }
  400. #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
  401. /* valid file mode bits */
  402. #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
  403. int fat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  404. struct iattr *attr)
  405. {
  406. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  407. struct inode *inode = d_inode(dentry);
  408. unsigned int ia_valid;
  409. int error;
  410. /* Check for setting the inode time. */
  411. ia_valid = attr->ia_valid;
  412. if (ia_valid & TIMES_SET_FLAGS) {
  413. if (fat_allow_set_time(idmap, sbi, inode))
  414. attr->ia_valid &= ~TIMES_SET_FLAGS;
  415. }
  416. error = setattr_prepare(idmap, dentry, attr);
  417. attr->ia_valid = ia_valid;
  418. if (error) {
  419. if (sbi->options.quiet)
  420. error = 0;
  421. goto out;
  422. }
  423. /*
  424. * Expand the file. Since inode_setattr() updates ->i_size
  425. * before calling the ->truncate(), but FAT needs to fill the
  426. * hole before it. XXX: this is no longer true with new truncate
  427. * sequence.
  428. */
  429. if (attr->ia_valid & ATTR_SIZE) {
  430. inode_dio_wait(inode);
  431. if (attr->ia_size > inode->i_size) {
  432. error = fat_cont_expand(inode, attr->ia_size);
  433. if (error || attr->ia_valid == ATTR_SIZE)
  434. goto out;
  435. attr->ia_valid &= ~ATTR_SIZE;
  436. }
  437. }
  438. if (((attr->ia_valid & ATTR_UID) &&
  439. (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid),
  440. sbi->options.fs_uid))) ||
  441. ((attr->ia_valid & ATTR_GID) &&
  442. (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid),
  443. sbi->options.fs_gid))) ||
  444. ((attr->ia_valid & ATTR_MODE) &&
  445. (attr->ia_mode & ~FAT_VALID_MODE)))
  446. error = -EPERM;
  447. if (error) {
  448. if (sbi->options.quiet)
  449. error = 0;
  450. goto out;
  451. }
  452. /*
  453. * We don't return -EPERM here. Yes, strange, but this is too
  454. * old behavior.
  455. */
  456. if (attr->ia_valid & ATTR_MODE) {
  457. if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
  458. attr->ia_valid &= ~ATTR_MODE;
  459. }
  460. if (attr->ia_valid & ATTR_SIZE) {
  461. error = fat_block_truncate_page(inode, attr->ia_size);
  462. if (error)
  463. goto out;
  464. down_write(&MSDOS_I(inode)->truncate_lock);
  465. truncate_setsize(inode, attr->ia_size);
  466. fat_truncate_blocks(inode, attr->ia_size);
  467. up_write(&MSDOS_I(inode)->truncate_lock);
  468. }
  469. /*
  470. * setattr_copy can't truncate these appropriately, so we'll
  471. * copy them ourselves
  472. */
  473. if (attr->ia_valid & ATTR_ATIME)
  474. fat_truncate_time(inode, &attr->ia_atime, S_ATIME);
  475. if (attr->ia_valid & ATTR_CTIME)
  476. fat_truncate_time(inode, &attr->ia_ctime, S_CTIME);
  477. if (attr->ia_valid & ATTR_MTIME)
  478. fat_truncate_time(inode, &attr->ia_mtime, S_MTIME);
  479. attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME);
  480. setattr_copy(idmap, inode, attr);
  481. mark_inode_dirty(inode);
  482. out:
  483. return error;
  484. }
  485. EXPORT_SYMBOL_GPL(fat_setattr);
  486. const struct inode_operations fat_file_inode_operations = {
  487. .setattr = fat_setattr,
  488. .getattr = fat_getattr,
  489. .update_time = fat_update_time,
  490. };