file.c 14 KB

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