file.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_check_mode(const struct msdos_sb_info *sbi, struct inode *inode,
  225. mode_t mode)
  226. {
  227. mode_t mask, req = mode & ~S_IFMT;
  228. if (S_ISREG(mode))
  229. mask = sbi->options.fs_fmask;
  230. else
  231. mask = sbi->options.fs_dmask;
  232. /*
  233. * Of the r and x bits, all (subject to umask) must be present. Of the
  234. * w bits, either all (subject to umask) or none must be present.
  235. */
  236. req &= ~mask;
  237. if ((req & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
  238. return -EPERM;
  239. if ((req & S_IWUGO) && ((req & S_IWUGO) != (S_IWUGO & ~mask)))
  240. return -EPERM;
  241. return 0;
  242. }
  243. static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
  244. {
  245. mode_t allow_utime = sbi->options.allow_utime;
  246. if (current->fsuid != inode->i_uid) {
  247. if (in_group_p(inode->i_gid))
  248. allow_utime >>= 3;
  249. if (allow_utime & MAY_WRITE)
  250. return 1;
  251. }
  252. /* use a default check */
  253. return 0;
  254. }
  255. int fat_setattr(struct dentry *dentry, struct iattr *attr)
  256. {
  257. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  258. struct inode *inode = dentry->d_inode;
  259. int mask, error = 0;
  260. unsigned int ia_valid;
  261. lock_kernel();
  262. /*
  263. * Expand the file. Since inode_setattr() updates ->i_size
  264. * before calling the ->truncate(), but FAT needs to fill the
  265. * hole before it.
  266. */
  267. if (attr->ia_valid & ATTR_SIZE) {
  268. if (attr->ia_size > inode->i_size) {
  269. error = fat_cont_expand(inode, attr->ia_size);
  270. if (error || attr->ia_valid == ATTR_SIZE)
  271. goto out;
  272. attr->ia_valid &= ~ATTR_SIZE;
  273. }
  274. }
  275. /* Check for setting the inode time. */
  276. ia_valid = attr->ia_valid;
  277. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
  278. if (fat_allow_set_time(sbi, inode))
  279. attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
  280. }
  281. error = inode_change_ok(inode, attr);
  282. attr->ia_valid = ia_valid;
  283. if (error) {
  284. if (sbi->options.quiet)
  285. error = 0;
  286. goto out;
  287. }
  288. if (((attr->ia_valid & ATTR_UID) &&
  289. (attr->ia_uid != sbi->options.fs_uid)) ||
  290. ((attr->ia_valid & ATTR_GID) &&
  291. (attr->ia_gid != sbi->options.fs_gid)) ||
  292. ((attr->ia_valid & ATTR_MODE) &&
  293. fat_check_mode(sbi, inode, attr->ia_mode) < 0))
  294. error = -EPERM;
  295. if (error) {
  296. if (sbi->options.quiet)
  297. error = 0;
  298. goto out;
  299. }
  300. error = inode_setattr(inode, attr);
  301. if (error)
  302. goto out;
  303. if (S_ISDIR(inode->i_mode))
  304. mask = sbi->options.fs_dmask;
  305. else
  306. mask = sbi->options.fs_fmask;
  307. inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
  308. out:
  309. unlock_kernel();
  310. return error;
  311. }
  312. EXPORT_SYMBOL_GPL(fat_setattr);
  313. const struct inode_operations fat_file_inode_operations = {
  314. .truncate = fat_truncate,
  315. .setattr = fat_setattr,
  316. .getattr = fat_getattr,
  317. };