file.c 10.0 KB

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