file.c 8.8 KB

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