file.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/time.h>
  11. #include <linux/msdos_fs.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/writeback.h>
  15. #include <linux/blkdev.h>
  16. int fat_generic_ioctl(struct inode *inode, struct file *filp,
  17. unsigned int cmd, unsigned long arg)
  18. {
  19. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  20. u32 __user *user_attr = (u32 __user *)arg;
  21. switch (cmd) {
  22. case FAT_IOCTL_GET_ATTRIBUTES:
  23. {
  24. u32 attr;
  25. if (inode->i_ino == MSDOS_ROOT_INO)
  26. attr = ATTR_DIR;
  27. else
  28. attr = fat_attr(inode);
  29. return put_user(attr, user_attr);
  30. }
  31. case FAT_IOCTL_SET_ATTRIBUTES:
  32. {
  33. u32 attr, oldattr;
  34. int err, is_dir = S_ISDIR(inode->i_mode);
  35. struct iattr ia;
  36. err = get_user(attr, user_attr);
  37. if (err)
  38. return err;
  39. mutex_lock(&inode->i_mutex);
  40. if (IS_RDONLY(inode)) {
  41. err = -EROFS;
  42. goto up;
  43. }
  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_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. mutex_unlock(&inode->i_mutex);
  94. return err;
  95. }
  96. default:
  97. return -ENOTTY; /* Inappropriate ioctl for device */
  98. }
  99. }
  100. static int fat_file_release(struct inode *inode, struct file *filp)
  101. {
  102. if ((filp->f_mode & FMODE_WRITE) &&
  103. MSDOS_SB(inode->i_sb)->options.flush) {
  104. fat_flush_inodes(inode->i_sb, inode, NULL);
  105. blk_congestion_wait(WRITE, HZ/10);
  106. }
  107. return 0;
  108. }
  109. const struct file_operations fat_file_operations = {
  110. .llseek = generic_file_llseek,
  111. .read = do_sync_read,
  112. .write = do_sync_write,
  113. .aio_read = generic_file_aio_read,
  114. .aio_write = generic_file_aio_write,
  115. .mmap = generic_file_mmap,
  116. .release = fat_file_release,
  117. .ioctl = fat_generic_ioctl,
  118. .fsync = file_fsync,
  119. .sendfile = generic_file_sendfile,
  120. };
  121. static int fat_cont_expand(struct inode *inode, loff_t size)
  122. {
  123. struct address_space *mapping = inode->i_mapping;
  124. loff_t start = inode->i_size, count = size - inode->i_size;
  125. int err;
  126. err = generic_cont_expand_simple(inode, size);
  127. if (err)
  128. goto out;
  129. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  130. mark_inode_dirty(inode);
  131. if (IS_SYNC(inode))
  132. err = sync_page_range_nolock(inode, mapping, start, count);
  133. out:
  134. return err;
  135. }
  136. int fat_notify_change(struct dentry *dentry, struct iattr *attr)
  137. {
  138. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  139. struct inode *inode = dentry->d_inode;
  140. int mask, error = 0;
  141. lock_kernel();
  142. /*
  143. * Expand the file. Since inode_setattr() updates ->i_size
  144. * before calling the ->truncate(), but FAT needs to fill the
  145. * hole before it.
  146. */
  147. if (attr->ia_valid & ATTR_SIZE) {
  148. if (attr->ia_size > inode->i_size) {
  149. error = fat_cont_expand(inode, attr->ia_size);
  150. if (error || attr->ia_valid == ATTR_SIZE)
  151. goto out;
  152. attr->ia_valid &= ~ATTR_SIZE;
  153. }
  154. }
  155. error = inode_change_ok(inode, attr);
  156. if (error) {
  157. if (sbi->options.quiet)
  158. error = 0;
  159. goto out;
  160. }
  161. if (((attr->ia_valid & ATTR_UID) &&
  162. (attr->ia_uid != sbi->options.fs_uid)) ||
  163. ((attr->ia_valid & ATTR_GID) &&
  164. (attr->ia_gid != sbi->options.fs_gid)) ||
  165. ((attr->ia_valid & ATTR_MODE) &&
  166. (attr->ia_mode & ~MSDOS_VALID_MODE)))
  167. error = -EPERM;
  168. if (error) {
  169. if (sbi->options.quiet)
  170. error = 0;
  171. goto out;
  172. }
  173. error = inode_setattr(inode, attr);
  174. if (error)
  175. goto out;
  176. if (S_ISDIR(inode->i_mode))
  177. mask = sbi->options.fs_dmask;
  178. else
  179. mask = sbi->options.fs_fmask;
  180. inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
  181. out:
  182. unlock_kernel();
  183. return error;
  184. }
  185. EXPORT_SYMBOL_GPL(fat_notify_change);
  186. /* Free all clusters after the skip'th cluster. */
  187. static int fat_free(struct inode *inode, int skip)
  188. {
  189. struct super_block *sb = inode->i_sb;
  190. int err, wait, free_start, i_start, i_logstart;
  191. if (MSDOS_I(inode)->i_start == 0)
  192. return 0;
  193. fat_cache_inval_inode(inode);
  194. wait = IS_DIRSYNC(inode);
  195. i_start = free_start = MSDOS_I(inode)->i_start;
  196. i_logstart = MSDOS_I(inode)->i_logstart;
  197. /* First, we write the new file size. */
  198. if (!skip) {
  199. MSDOS_I(inode)->i_start = 0;
  200. MSDOS_I(inode)->i_logstart = 0;
  201. }
  202. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  203. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  204. if (wait) {
  205. err = fat_sync_inode(inode);
  206. if (err) {
  207. MSDOS_I(inode)->i_start = i_start;
  208. MSDOS_I(inode)->i_logstart = i_logstart;
  209. return err;
  210. }
  211. } else
  212. mark_inode_dirty(inode);
  213. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  214. if (skip) {
  215. struct fat_entry fatent;
  216. int ret, fclus, dclus;
  217. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  218. if (ret < 0)
  219. return ret;
  220. else if (ret == FAT_ENT_EOF)
  221. return 0;
  222. fatent_init(&fatent);
  223. ret = fat_ent_read(inode, &fatent, dclus);
  224. if (ret == FAT_ENT_EOF) {
  225. fatent_brelse(&fatent);
  226. return 0;
  227. } else if (ret == FAT_ENT_FREE) {
  228. fat_fs_panic(sb,
  229. "%s: invalid cluster chain (i_pos %lld)",
  230. __FUNCTION__, MSDOS_I(inode)->i_pos);
  231. ret = -EIO;
  232. } else if (ret > 0) {
  233. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  234. if (err)
  235. ret = err;
  236. }
  237. fatent_brelse(&fatent);
  238. if (ret < 0)
  239. return ret;
  240. free_start = ret;
  241. }
  242. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  243. /* Freeing the remained cluster chain */
  244. return fat_free_clusters(inode, free_start);
  245. }
  246. void fat_truncate(struct inode *inode)
  247. {
  248. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  249. const unsigned int cluster_size = sbi->cluster_size;
  250. int nr_clusters;
  251. /*
  252. * This protects against truncating a file bigger than it was then
  253. * trying to write into the hole.
  254. */
  255. if (MSDOS_I(inode)->mmu_private > inode->i_size)
  256. MSDOS_I(inode)->mmu_private = inode->i_size;
  257. nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
  258. lock_kernel();
  259. fat_free(inode, nr_clusters);
  260. unlock_kernel();
  261. fat_flush_inodes(inode->i_sb, inode, NULL);
  262. }
  263. struct inode_operations fat_file_inode_operations = {
  264. .truncate = fat_truncate,
  265. .setattr = fat_notify_change,
  266. };