file.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. static int check_mode(const struct msdos_sb_info *sbi, mode_t mode)
  140. {
  141. mode_t req = mode & ~S_IFMT;
  142. /*
  143. * Of the r and x bits, all (subject to umask) must be present. Of the
  144. * w bits, either all (subject to umask) or none must be present.
  145. */
  146. if (S_ISREG(mode)) {
  147. req &= ~sbi->options.fs_fmask;
  148. if ((req & (S_IRUGO | S_IXUGO)) !=
  149. ((S_IRUGO | S_IXUGO) & ~sbi->options.fs_fmask))
  150. return -EPERM;
  151. if ((req & S_IWUGO) != 0 &&
  152. (req & S_IWUGO) != (S_IWUGO & ~sbi->options.fs_fmask))
  153. return -EPERM;
  154. } else if (S_ISDIR(mode)) {
  155. req &= ~sbi->options.fs_dmask;
  156. if ((req & (S_IRUGO | S_IXUGO)) !=
  157. ((S_IRUGO | S_IXUGO) & ~sbi->options.fs_dmask))
  158. return -EPERM;
  159. if ((req & S_IWUGO) != 0 &&
  160. (req & S_IWUGO) != (S_IWUGO & ~sbi->options.fs_dmask))
  161. return -EPERM;
  162. } else {
  163. return -EPERM;
  164. }
  165. return 0;
  166. }
  167. int fat_notify_change(struct dentry *dentry, struct iattr *attr)
  168. {
  169. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  170. struct inode *inode = dentry->d_inode;
  171. int mask, error = 0;
  172. lock_kernel();
  173. /*
  174. * Expand the file. Since inode_setattr() updates ->i_size
  175. * before calling the ->truncate(), but FAT needs to fill the
  176. * hole before it.
  177. */
  178. if (attr->ia_valid & ATTR_SIZE) {
  179. if (attr->ia_size > inode->i_size) {
  180. error = fat_cont_expand(inode, attr->ia_size);
  181. if (error || attr->ia_valid == ATTR_SIZE)
  182. goto out;
  183. attr->ia_valid &= ~ATTR_SIZE;
  184. }
  185. }
  186. error = inode_change_ok(inode, attr);
  187. if (error) {
  188. if (sbi->options.quiet)
  189. error = 0;
  190. goto out;
  191. }
  192. if (((attr->ia_valid & ATTR_UID) &&
  193. (attr->ia_uid != sbi->options.fs_uid)) ||
  194. ((attr->ia_valid & ATTR_GID) &&
  195. (attr->ia_gid != sbi->options.fs_gid)))
  196. error = -EPERM;
  197. if (error) {
  198. if (sbi->options.quiet)
  199. error = 0;
  200. goto out;
  201. }
  202. if (attr->ia_valid & ATTR_MODE) {
  203. error = check_mode(sbi, attr->ia_mode);
  204. if (error != 0 && !sbi->options.quiet)
  205. goto out;
  206. }
  207. error = inode_setattr(inode, attr);
  208. if (error)
  209. goto out;
  210. if (S_ISDIR(inode->i_mode))
  211. mask = sbi->options.fs_dmask;
  212. else
  213. mask = sbi->options.fs_fmask;
  214. inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
  215. out:
  216. unlock_kernel();
  217. return error;
  218. }
  219. EXPORT_SYMBOL_GPL(fat_notify_change);
  220. /* Free all clusters after the skip'th cluster. */
  221. static int fat_free(struct inode *inode, int skip)
  222. {
  223. struct super_block *sb = inode->i_sb;
  224. int err, wait, free_start, i_start, i_logstart;
  225. if (MSDOS_I(inode)->i_start == 0)
  226. return 0;
  227. fat_cache_inval_inode(inode);
  228. wait = IS_DIRSYNC(inode);
  229. i_start = free_start = MSDOS_I(inode)->i_start;
  230. i_logstart = MSDOS_I(inode)->i_logstart;
  231. /* First, we write the new file size. */
  232. if (!skip) {
  233. MSDOS_I(inode)->i_start = 0;
  234. MSDOS_I(inode)->i_logstart = 0;
  235. }
  236. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  237. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  238. if (wait) {
  239. err = fat_sync_inode(inode);
  240. if (err) {
  241. MSDOS_I(inode)->i_start = i_start;
  242. MSDOS_I(inode)->i_logstart = i_logstart;
  243. return err;
  244. }
  245. } else
  246. mark_inode_dirty(inode);
  247. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  248. if (skip) {
  249. struct fat_entry fatent;
  250. int ret, fclus, dclus;
  251. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  252. if (ret < 0)
  253. return ret;
  254. else if (ret == FAT_ENT_EOF)
  255. return 0;
  256. fatent_init(&fatent);
  257. ret = fat_ent_read(inode, &fatent, dclus);
  258. if (ret == FAT_ENT_EOF) {
  259. fatent_brelse(&fatent);
  260. return 0;
  261. } else if (ret == FAT_ENT_FREE) {
  262. fat_fs_panic(sb,
  263. "%s: invalid cluster chain (i_pos %lld)",
  264. __FUNCTION__, MSDOS_I(inode)->i_pos);
  265. ret = -EIO;
  266. } else if (ret > 0) {
  267. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  268. if (err)
  269. ret = err;
  270. }
  271. fatent_brelse(&fatent);
  272. if (ret < 0)
  273. return ret;
  274. free_start = ret;
  275. }
  276. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  277. /* Freeing the remained cluster chain */
  278. return fat_free_clusters(inode, free_start);
  279. }
  280. void fat_truncate(struct inode *inode)
  281. {
  282. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  283. const unsigned int cluster_size = sbi->cluster_size;
  284. int nr_clusters;
  285. /*
  286. * This protects against truncating a file bigger than it was then
  287. * trying to write into the hole.
  288. */
  289. if (MSDOS_I(inode)->mmu_private > inode->i_size)
  290. MSDOS_I(inode)->mmu_private = inode->i_size;
  291. nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
  292. lock_kernel();
  293. fat_free(inode, nr_clusters);
  294. unlock_kernel();
  295. fat_flush_inodes(inode->i_sb, inode, NULL);
  296. }
  297. int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  298. {
  299. struct inode *inode = dentry->d_inode;
  300. generic_fillattr(inode, stat);
  301. stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
  302. return 0;
  303. }
  304. EXPORT_SYMBOL_GPL(fat_getattr);
  305. const struct inode_operations fat_file_inode_operations = {
  306. .truncate = fat_truncate,
  307. .setattr = fat_notify_change,
  308. .getattr = fat_getattr,
  309. };