file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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/fsnotify.h>
  14. #include <linux/security.h>
  15. #include "fat.h"
  16. static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
  17. {
  18. u32 attr;
  19. mutex_lock(&inode->i_mutex);
  20. attr = fat_make_attrs(inode);
  21. mutex_unlock(&inode->i_mutex);
  22. return put_user(attr, user_attr);
  23. }
  24. static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
  25. {
  26. struct inode *inode = file_inode(file);
  27. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  28. int is_dir = S_ISDIR(inode->i_mode);
  29. u32 attr, oldattr;
  30. struct iattr ia;
  31. int err;
  32. err = get_user(attr, user_attr);
  33. if (err)
  34. goto out;
  35. err = mnt_want_write_file(file);
  36. if (err)
  37. goto out;
  38. mutex_lock(&inode->i_mutex);
  39. /*
  40. * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
  41. * prevents the user from turning us into a VFAT
  42. * longname entry. Also, we obviously can't set
  43. * any of the NTFS attributes in the high 24 bits.
  44. */
  45. attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
  46. /* Merge in ATTR_VOLUME and ATTR_DIR */
  47. attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
  48. (is_dir ? ATTR_DIR : 0);
  49. oldattr = fat_make_attrs(inode);
  50. /* Equivalent to a chmod() */
  51. ia.ia_valid = ATTR_MODE | ATTR_CTIME;
  52. ia.ia_ctime = current_fs_time(inode->i_sb);
  53. if (is_dir)
  54. ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
  55. else {
  56. ia.ia_mode = fat_make_mode(sbi, attr,
  57. S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
  58. }
  59. /* The root directory has no attributes */
  60. if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
  61. err = -EINVAL;
  62. goto out_unlock_inode;
  63. }
  64. if (sbi->options.sys_immutable &&
  65. ((attr | oldattr) & ATTR_SYS) &&
  66. !capable(CAP_LINUX_IMMUTABLE)) {
  67. err = -EPERM;
  68. goto out_unlock_inode;
  69. }
  70. /*
  71. * The security check is questionable... We single
  72. * out the RO attribute for checking by the security
  73. * module, just because it maps to a file mode.
  74. */
  75. err = security_inode_setattr(file->f_path.dentry, &ia);
  76. if (err)
  77. goto out_unlock_inode;
  78. /* This MUST be done before doing anything irreversible... */
  79. err = fat_setattr(file->f_path.dentry, &ia);
  80. if (err)
  81. goto out_unlock_inode;
  82. fsnotify_change(file->f_path.dentry, ia.ia_valid);
  83. if (sbi->options.sys_immutable) {
  84. if (attr & ATTR_SYS)
  85. inode->i_flags |= S_IMMUTABLE;
  86. else
  87. inode->i_flags &= ~S_IMMUTABLE;
  88. }
  89. fat_save_attrs(inode, attr);
  90. mark_inode_dirty(inode);
  91. out_unlock_inode:
  92. mutex_unlock(&inode->i_mutex);
  93. mnt_drop_write_file(file);
  94. out:
  95. return err;
  96. }
  97. static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
  98. {
  99. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  100. return put_user(sbi->vol_id, user_attr);
  101. }
  102. long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  103. {
  104. struct inode *inode = file_inode(filp);
  105. u32 __user *user_attr = (u32 __user *)arg;
  106. switch (cmd) {
  107. case FAT_IOCTL_GET_ATTRIBUTES:
  108. return fat_ioctl_get_attributes(inode, user_attr);
  109. case FAT_IOCTL_SET_ATTRIBUTES:
  110. return fat_ioctl_set_attributes(filp, user_attr);
  111. case FAT_IOCTL_GET_VOLUME_ID:
  112. return fat_ioctl_get_volume_id(inode, user_attr);
  113. default:
  114. return -ENOTTY; /* Inappropriate ioctl for device */
  115. }
  116. }
  117. #ifdef CONFIG_COMPAT
  118. static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd,
  119. unsigned long arg)
  120. {
  121. return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  122. }
  123. #endif
  124. static int fat_file_release(struct inode *inode, struct file *filp)
  125. {
  126. if ((filp->f_mode & FMODE_WRITE) &&
  127. MSDOS_SB(inode->i_sb)->options.flush) {
  128. fat_flush_inodes(inode->i_sb, inode, NULL);
  129. congestion_wait(BLK_RW_ASYNC, HZ/10);
  130. }
  131. return 0;
  132. }
  133. int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  134. {
  135. struct inode *inode = filp->f_mapping->host;
  136. int res, err;
  137. res = generic_file_fsync(filp, start, end, datasync);
  138. err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
  139. return res ? res : err;
  140. }
  141. const struct file_operations fat_file_operations = {
  142. .llseek = generic_file_llseek,
  143. .read_iter = generic_file_read_iter,
  144. .write_iter = generic_file_write_iter,
  145. .mmap = generic_file_mmap,
  146. .release = fat_file_release,
  147. .unlocked_ioctl = fat_generic_ioctl,
  148. #ifdef CONFIG_COMPAT
  149. .compat_ioctl = fat_generic_compat_ioctl,
  150. #endif
  151. .fsync = fat_file_fsync,
  152. .splice_read = generic_file_splice_read,
  153. };
  154. static int fat_cont_expand(struct inode *inode, loff_t size)
  155. {
  156. struct address_space *mapping = inode->i_mapping;
  157. loff_t start = inode->i_size, count = size - inode->i_size;
  158. int err;
  159. err = generic_cont_expand_simple(inode, size);
  160. if (err)
  161. goto out;
  162. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  163. mark_inode_dirty(inode);
  164. if (IS_SYNC(inode)) {
  165. int err2;
  166. /*
  167. * Opencode syncing since we don't have a file open to use
  168. * standard fsync path.
  169. */
  170. err = filemap_fdatawrite_range(mapping, start,
  171. start + count - 1);
  172. err2 = sync_mapping_buffers(mapping);
  173. if (!err)
  174. err = err2;
  175. err2 = write_inode_now(inode, 1);
  176. if (!err)
  177. err = err2;
  178. if (!err) {
  179. err = filemap_fdatawait_range(mapping, start,
  180. start + count - 1);
  181. }
  182. }
  183. out:
  184. return err;
  185. }
  186. /* Free all clusters after the skip'th cluster. */
  187. static int fat_free(struct inode *inode, int skip)
  188. {
  189. struct super_block *sb = inode->i_sb;
  190. int err, wait, free_start, i_start, i_logstart;
  191. if (MSDOS_I(inode)->i_start == 0)
  192. return 0;
  193. fat_cache_inval_inode(inode);
  194. wait = IS_DIRSYNC(inode);
  195. i_start = free_start = MSDOS_I(inode)->i_start;
  196. i_logstart = MSDOS_I(inode)->i_logstart;
  197. /* First, we write the new file size. */
  198. if (!skip) {
  199. MSDOS_I(inode)->i_start = 0;
  200. MSDOS_I(inode)->i_logstart = 0;
  201. }
  202. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  203. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  204. if (wait) {
  205. err = fat_sync_inode(inode);
  206. if (err) {
  207. MSDOS_I(inode)->i_start = i_start;
  208. MSDOS_I(inode)->i_logstart = i_logstart;
  209. return err;
  210. }
  211. } else
  212. mark_inode_dirty(inode);
  213. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  214. if (skip) {
  215. struct fat_entry fatent;
  216. int ret, fclus, dclus;
  217. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  218. if (ret < 0)
  219. return ret;
  220. else if (ret == FAT_ENT_EOF)
  221. return 0;
  222. fatent_init(&fatent);
  223. ret = fat_ent_read(inode, &fatent, dclus);
  224. if (ret == FAT_ENT_EOF) {
  225. fatent_brelse(&fatent);
  226. return 0;
  227. } else if (ret == FAT_ENT_FREE) {
  228. fat_fs_error(sb,
  229. "%s: invalid cluster chain (i_pos %lld)",
  230. __func__, MSDOS_I(inode)->i_pos);
  231. ret = -EIO;
  232. } else if (ret > 0) {
  233. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  234. if (err)
  235. ret = err;
  236. }
  237. fatent_brelse(&fatent);
  238. if (ret < 0)
  239. return ret;
  240. free_start = ret;
  241. }
  242. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  243. /* Freeing the remained cluster chain */
  244. return fat_free_clusters(inode, free_start);
  245. }
  246. void fat_truncate_blocks(struct inode *inode, loff_t offset)
  247. {
  248. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  249. const unsigned int cluster_size = sbi->cluster_size;
  250. int nr_clusters;
  251. /*
  252. * This protects against truncating a file bigger than it was then
  253. * trying to write into the hole.
  254. */
  255. if (MSDOS_I(inode)->mmu_private > offset)
  256. MSDOS_I(inode)->mmu_private = offset;
  257. nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
  258. fat_free(inode, nr_clusters);
  259. fat_flush_inodes(inode->i_sb, inode, NULL);
  260. }
  261. int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  262. {
  263. struct inode *inode = d_inode(dentry);
  264. generic_fillattr(inode, stat);
  265. stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
  266. if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
  267. /* Use i_pos for ino. This is used as fileid of nfs. */
  268. stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode);
  269. }
  270. return 0;
  271. }
  272. EXPORT_SYMBOL_GPL(fat_getattr);
  273. static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
  274. struct inode *inode, umode_t *mode_ptr)
  275. {
  276. umode_t mask, perm;
  277. /*
  278. * Note, the basic check is already done by a caller of
  279. * (attr->ia_mode & ~FAT_VALID_MODE)
  280. */
  281. if (S_ISREG(inode->i_mode))
  282. mask = sbi->options.fs_fmask;
  283. else
  284. mask = sbi->options.fs_dmask;
  285. perm = *mode_ptr & ~(S_IFMT | mask);
  286. /*
  287. * Of the r and x bits, all (subject to umask) must be present. Of the
  288. * w bits, either all (subject to umask) or none must be present.
  289. *
  290. * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
  291. */
  292. if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
  293. return -EPERM;
  294. if (fat_mode_can_hold_ro(inode)) {
  295. if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
  296. return -EPERM;
  297. } else {
  298. if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
  299. return -EPERM;
  300. }
  301. *mode_ptr &= S_IFMT | perm;
  302. return 0;
  303. }
  304. static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
  305. {
  306. umode_t allow_utime = sbi->options.allow_utime;
  307. if (!uid_eq(current_fsuid(), inode->i_uid)) {
  308. if (in_group_p(inode->i_gid))
  309. allow_utime >>= 3;
  310. if (allow_utime & MAY_WRITE)
  311. return 1;
  312. }
  313. /* use a default check */
  314. return 0;
  315. }
  316. #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
  317. /* valid file mode bits */
  318. #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
  319. int fat_setattr(struct dentry *dentry, struct iattr *attr)
  320. {
  321. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  322. struct inode *inode = d_inode(dentry);
  323. unsigned int ia_valid;
  324. int error;
  325. /* Check for setting the inode time. */
  326. ia_valid = attr->ia_valid;
  327. if (ia_valid & TIMES_SET_FLAGS) {
  328. if (fat_allow_set_time(sbi, inode))
  329. attr->ia_valid &= ~TIMES_SET_FLAGS;
  330. }
  331. error = inode_change_ok(inode, attr);
  332. attr->ia_valid = ia_valid;
  333. if (error) {
  334. if (sbi->options.quiet)
  335. error = 0;
  336. goto out;
  337. }
  338. /*
  339. * Expand the file. Since inode_setattr() updates ->i_size
  340. * before calling the ->truncate(), but FAT needs to fill the
  341. * hole before it. XXX: this is no longer true with new truncate
  342. * sequence.
  343. */
  344. if (attr->ia_valid & ATTR_SIZE) {
  345. inode_dio_wait(inode);
  346. if (attr->ia_size > inode->i_size) {
  347. error = fat_cont_expand(inode, attr->ia_size);
  348. if (error || attr->ia_valid == ATTR_SIZE)
  349. goto out;
  350. attr->ia_valid &= ~ATTR_SIZE;
  351. }
  352. }
  353. if (((attr->ia_valid & ATTR_UID) &&
  354. (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
  355. ((attr->ia_valid & ATTR_GID) &&
  356. (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
  357. ((attr->ia_valid & ATTR_MODE) &&
  358. (attr->ia_mode & ~FAT_VALID_MODE)))
  359. error = -EPERM;
  360. if (error) {
  361. if (sbi->options.quiet)
  362. error = 0;
  363. goto out;
  364. }
  365. /*
  366. * We don't return -EPERM here. Yes, strange, but this is too
  367. * old behavior.
  368. */
  369. if (attr->ia_valid & ATTR_MODE) {
  370. if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
  371. attr->ia_valid &= ~ATTR_MODE;
  372. }
  373. if (attr->ia_valid & ATTR_SIZE) {
  374. error = fat_block_truncate_page(inode, attr->ia_size);
  375. if (error)
  376. goto out;
  377. down_write(&MSDOS_I(inode)->truncate_lock);
  378. truncate_setsize(inode, attr->ia_size);
  379. fat_truncate_blocks(inode, attr->ia_size);
  380. up_write(&MSDOS_I(inode)->truncate_lock);
  381. }
  382. setattr_copy(inode, attr);
  383. mark_inode_dirty(inode);
  384. out:
  385. return error;
  386. }
  387. EXPORT_SYMBOL_GPL(fat_setattr);
  388. const struct inode_operations fat_file_inode_operations = {
  389. .setattr = fat_setattr,
  390. .getattr = fat_getattr,
  391. };