file.c 9.2 KB

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