file.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. .readv = generic_file_readv,
  114. .writev = generic_file_writev,
  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. .sendfile = generic_file_sendfile,
  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. int fat_notify_change(struct dentry *dentry, struct iattr *attr)
  139. {
  140. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  141. struct inode *inode = dentry->d_inode;
  142. int mask, error = 0;
  143. lock_kernel();
  144. /*
  145. * Expand the file. Since inode_setattr() updates ->i_size
  146. * before calling the ->truncate(), but FAT needs to fill the
  147. * hole before it.
  148. */
  149. if (attr->ia_valid & ATTR_SIZE) {
  150. if (attr->ia_size > inode->i_size) {
  151. error = fat_cont_expand(inode, attr->ia_size);
  152. if (error || attr->ia_valid == ATTR_SIZE)
  153. goto out;
  154. attr->ia_valid &= ~ATTR_SIZE;
  155. }
  156. }
  157. error = inode_change_ok(inode, attr);
  158. if (error) {
  159. if (sbi->options.quiet)
  160. error = 0;
  161. goto out;
  162. }
  163. if (((attr->ia_valid & ATTR_UID) &&
  164. (attr->ia_uid != sbi->options.fs_uid)) ||
  165. ((attr->ia_valid & ATTR_GID) &&
  166. (attr->ia_gid != sbi->options.fs_gid)) ||
  167. ((attr->ia_valid & ATTR_MODE) &&
  168. (attr->ia_mode & ~MSDOS_VALID_MODE)))
  169. error = -EPERM;
  170. if (error) {
  171. if (sbi->options.quiet)
  172. error = 0;
  173. goto out;
  174. }
  175. error = inode_setattr(inode, attr);
  176. if (error)
  177. goto out;
  178. if (S_ISDIR(inode->i_mode))
  179. mask = sbi->options.fs_dmask;
  180. else
  181. mask = sbi->options.fs_fmask;
  182. inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
  183. out:
  184. unlock_kernel();
  185. return error;
  186. }
  187. EXPORT_SYMBOL_GPL(fat_notify_change);
  188. /* Free all clusters after the skip'th cluster. */
  189. static int fat_free(struct inode *inode, int skip)
  190. {
  191. struct super_block *sb = inode->i_sb;
  192. int err, wait, free_start, i_start, i_logstart;
  193. if (MSDOS_I(inode)->i_start == 0)
  194. return 0;
  195. fat_cache_inval_inode(inode);
  196. wait = IS_DIRSYNC(inode);
  197. i_start = free_start = MSDOS_I(inode)->i_start;
  198. i_logstart = MSDOS_I(inode)->i_logstart;
  199. /* First, we write the new file size. */
  200. if (!skip) {
  201. MSDOS_I(inode)->i_start = 0;
  202. MSDOS_I(inode)->i_logstart = 0;
  203. }
  204. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  205. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  206. if (wait) {
  207. err = fat_sync_inode(inode);
  208. if (err) {
  209. MSDOS_I(inode)->i_start = i_start;
  210. MSDOS_I(inode)->i_logstart = i_logstart;
  211. return err;
  212. }
  213. } else
  214. mark_inode_dirty(inode);
  215. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  216. if (skip) {
  217. struct fat_entry fatent;
  218. int ret, fclus, dclus;
  219. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  220. if (ret < 0)
  221. return ret;
  222. else if (ret == FAT_ENT_EOF)
  223. return 0;
  224. fatent_init(&fatent);
  225. ret = fat_ent_read(inode, &fatent, dclus);
  226. if (ret == FAT_ENT_EOF) {
  227. fatent_brelse(&fatent);
  228. return 0;
  229. } else if (ret == FAT_ENT_FREE) {
  230. fat_fs_panic(sb,
  231. "%s: invalid cluster chain (i_pos %lld)",
  232. __FUNCTION__, MSDOS_I(inode)->i_pos);
  233. ret = -EIO;
  234. } else if (ret > 0) {
  235. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  236. if (err)
  237. ret = err;
  238. }
  239. fatent_brelse(&fatent);
  240. if (ret < 0)
  241. return ret;
  242. free_start = ret;
  243. }
  244. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  245. /* Freeing the remained cluster chain */
  246. return fat_free_clusters(inode, free_start);
  247. }
  248. void fat_truncate(struct inode *inode)
  249. {
  250. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  251. const unsigned int cluster_size = sbi->cluster_size;
  252. int nr_clusters;
  253. /*
  254. * This protects against truncating a file bigger than it was then
  255. * trying to write into the hole.
  256. */
  257. if (MSDOS_I(inode)->mmu_private > inode->i_size)
  258. MSDOS_I(inode)->mmu_private = inode->i_size;
  259. nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
  260. lock_kernel();
  261. fat_free(inode, nr_clusters);
  262. unlock_kernel();
  263. fat_flush_inodes(inode->i_sb, inode, NULL);
  264. }
  265. struct inode_operations fat_file_inode_operations = {
  266. .truncate = fat_truncate,
  267. .setattr = fat_notify_change,
  268. };