file.c 9.7 KB

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