file.c 8.9 KB

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