file.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. const struct file_operations fat_file_operations = {
  119. .llseek = generic_file_llseek,
  120. .read = do_sync_read,
  121. .write = do_sync_write,
  122. .aio_read = generic_file_aio_read,
  123. .aio_write = generic_file_aio_write,
  124. .mmap = generic_file_mmap,
  125. .release = fat_file_release,
  126. .ioctl = fat_generic_ioctl,
  127. .fsync = file_fsync,
  128. .splice_read = generic_file_splice_read,
  129. };
  130. static int fat_cont_expand(struct inode *inode, loff_t size)
  131. {
  132. struct address_space *mapping = inode->i_mapping;
  133. loff_t start = inode->i_size, count = size - inode->i_size;
  134. int err;
  135. err = generic_cont_expand_simple(inode, size);
  136. if (err)
  137. goto out;
  138. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  139. mark_inode_dirty(inode);
  140. if (IS_SYNC(inode))
  141. err = sync_page_range_nolock(inode, mapping, start, count);
  142. out:
  143. return err;
  144. }
  145. /* Free all clusters after the skip'th cluster. */
  146. static int fat_free(struct inode *inode, int skip)
  147. {
  148. struct super_block *sb = inode->i_sb;
  149. int err, wait, free_start, i_start, i_logstart;
  150. if (MSDOS_I(inode)->i_start == 0)
  151. return 0;
  152. fat_cache_inval_inode(inode);
  153. wait = IS_DIRSYNC(inode);
  154. i_start = free_start = MSDOS_I(inode)->i_start;
  155. i_logstart = MSDOS_I(inode)->i_logstart;
  156. /* First, we write the new file size. */
  157. if (!skip) {
  158. MSDOS_I(inode)->i_start = 0;
  159. MSDOS_I(inode)->i_logstart = 0;
  160. }
  161. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  162. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  163. if (wait) {
  164. err = fat_sync_inode(inode);
  165. if (err) {
  166. MSDOS_I(inode)->i_start = i_start;
  167. MSDOS_I(inode)->i_logstart = i_logstart;
  168. return err;
  169. }
  170. } else
  171. mark_inode_dirty(inode);
  172. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  173. if (skip) {
  174. struct fat_entry fatent;
  175. int ret, fclus, dclus;
  176. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  177. if (ret < 0)
  178. return ret;
  179. else if (ret == FAT_ENT_EOF)
  180. return 0;
  181. fatent_init(&fatent);
  182. ret = fat_ent_read(inode, &fatent, dclus);
  183. if (ret == FAT_ENT_EOF) {
  184. fatent_brelse(&fatent);
  185. return 0;
  186. } else if (ret == FAT_ENT_FREE) {
  187. fat_fs_panic(sb,
  188. "%s: invalid cluster chain (i_pos %lld)",
  189. __func__, MSDOS_I(inode)->i_pos);
  190. ret = -EIO;
  191. } else if (ret > 0) {
  192. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  193. if (err)
  194. ret = err;
  195. }
  196. fatent_brelse(&fatent);
  197. if (ret < 0)
  198. return ret;
  199. free_start = ret;
  200. }
  201. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  202. /* Freeing the remained cluster chain */
  203. return fat_free_clusters(inode, free_start);
  204. }
  205. void fat_truncate(struct inode *inode)
  206. {
  207. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  208. const unsigned int cluster_size = sbi->cluster_size;
  209. int nr_clusters;
  210. /*
  211. * This protects against truncating a file bigger than it was then
  212. * trying to write into the hole.
  213. */
  214. if (MSDOS_I(inode)->mmu_private > inode->i_size)
  215. MSDOS_I(inode)->mmu_private = inode->i_size;
  216. nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
  217. fat_free(inode, nr_clusters);
  218. fat_flush_inodes(inode->i_sb, inode, NULL);
  219. }
  220. int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  221. {
  222. struct inode *inode = dentry->d_inode;
  223. generic_fillattr(inode, stat);
  224. stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
  225. return 0;
  226. }
  227. EXPORT_SYMBOL_GPL(fat_getattr);
  228. static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
  229. struct inode *inode, umode_t *mode_ptr)
  230. {
  231. mode_t mask, perm;
  232. /*
  233. * Note, the basic check is already done by a caller of
  234. * (attr->ia_mode & ~FAT_VALID_MODE)
  235. */
  236. if (S_ISREG(inode->i_mode))
  237. mask = sbi->options.fs_fmask;
  238. else
  239. mask = sbi->options.fs_dmask;
  240. perm = *mode_ptr & ~(S_IFMT | mask);
  241. /*
  242. * Of the r and x bits, all (subject to umask) must be present. Of the
  243. * w bits, either all (subject to umask) or none must be present.
  244. *
  245. * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
  246. */
  247. if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
  248. return -EPERM;
  249. if (fat_mode_can_hold_ro(inode)) {
  250. if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
  251. return -EPERM;
  252. } else {
  253. if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
  254. return -EPERM;
  255. }
  256. *mode_ptr &= S_IFMT | perm;
  257. return 0;
  258. }
  259. static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
  260. {
  261. mode_t allow_utime = sbi->options.allow_utime;
  262. if (current_fsuid() != inode->i_uid) {
  263. if (in_group_p(inode->i_gid))
  264. allow_utime >>= 3;
  265. if (allow_utime & MAY_WRITE)
  266. return 1;
  267. }
  268. /* use a default check */
  269. return 0;
  270. }
  271. #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
  272. /* valid file mode bits */
  273. #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
  274. int fat_setattr(struct dentry *dentry, struct iattr *attr)
  275. {
  276. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  277. struct inode *inode = dentry->d_inode;
  278. unsigned int ia_valid;
  279. int error;
  280. /*
  281. * Expand the file. Since inode_setattr() updates ->i_size
  282. * before calling the ->truncate(), but FAT needs to fill the
  283. * hole before it.
  284. */
  285. if (attr->ia_valid & ATTR_SIZE) {
  286. if (attr->ia_size > inode->i_size) {
  287. error = fat_cont_expand(inode, attr->ia_size);
  288. if (error || attr->ia_valid == ATTR_SIZE)
  289. goto out;
  290. attr->ia_valid &= ~ATTR_SIZE;
  291. }
  292. }
  293. /* Check for setting the inode time. */
  294. ia_valid = attr->ia_valid;
  295. if (ia_valid & TIMES_SET_FLAGS) {
  296. if (fat_allow_set_time(sbi, inode))
  297. attr->ia_valid &= ~TIMES_SET_FLAGS;
  298. }
  299. error = inode_change_ok(inode, attr);
  300. attr->ia_valid = ia_valid;
  301. if (error) {
  302. if (sbi->options.quiet)
  303. error = 0;
  304. goto out;
  305. }
  306. if (((attr->ia_valid & ATTR_UID) &&
  307. (attr->ia_uid != sbi->options.fs_uid)) ||
  308. ((attr->ia_valid & ATTR_GID) &&
  309. (attr->ia_gid != sbi->options.fs_gid)) ||
  310. ((attr->ia_valid & ATTR_MODE) &&
  311. (attr->ia_mode & ~FAT_VALID_MODE)))
  312. error = -EPERM;
  313. if (error) {
  314. if (sbi->options.quiet)
  315. error = 0;
  316. goto out;
  317. }
  318. /*
  319. * We don't return -EPERM here. Yes, strange, but this is too
  320. * old behavior.
  321. */
  322. if (attr->ia_valid & ATTR_MODE) {
  323. if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
  324. attr->ia_valid &= ~ATTR_MODE;
  325. }
  326. if (attr->ia_valid)
  327. error = inode_setattr(inode, attr);
  328. out:
  329. return error;
  330. }
  331. EXPORT_SYMBOL_GPL(fat_setattr);
  332. const struct inode_operations fat_file_inode_operations = {
  333. .truncate = fat_truncate,
  334. .setattr = fat_setattr,
  335. .getattr = fat_getattr,
  336. };