file.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. int fat_generic_ioctl(struct inode *inode, struct file *filp,
  16. unsigned int cmd, unsigned long arg)
  17. {
  18. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  19. u32 __user *user_attr = (u32 __user *)arg;
  20. switch (cmd) {
  21. case FAT_IOCTL_GET_ATTRIBUTES:
  22. {
  23. u32 attr;
  24. if (inode->i_ino == MSDOS_ROOT_INO)
  25. attr = ATTR_DIR;
  26. else
  27. attr = fat_attr(inode);
  28. return put_user(attr, user_attr);
  29. }
  30. case FAT_IOCTL_SET_ATTRIBUTES:
  31. {
  32. u32 attr, oldattr;
  33. int err, is_dir = S_ISDIR(inode->i_mode);
  34. struct iattr ia;
  35. err = get_user(attr, user_attr);
  36. if (err)
  37. return err;
  38. mutex_lock(&inode->i_mutex);
  39. if (IS_RDONLY(inode)) {
  40. err = -EROFS;
  41. goto up;
  42. }
  43. /*
  44. * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
  45. * prevents the user from turning us into a VFAT
  46. * longname entry. Also, we obviously can't set
  47. * any of the NTFS attributes in the high 24 bits.
  48. */
  49. attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
  50. /* Merge in ATTR_VOLUME and ATTR_DIR */
  51. attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
  52. (is_dir ? ATTR_DIR : 0);
  53. oldattr = fat_attr(inode);
  54. /* Equivalent to a chmod() */
  55. ia.ia_valid = ATTR_MODE | ATTR_CTIME;
  56. if (is_dir) {
  57. ia.ia_mode = MSDOS_MKMODE(attr,
  58. S_IRWXUGO & ~sbi->options.fs_dmask)
  59. | S_IFDIR;
  60. } else {
  61. ia.ia_mode = MSDOS_MKMODE(attr,
  62. (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
  63. & ~sbi->options.fs_fmask)
  64. | S_IFREG;
  65. }
  66. /* The root directory has no attributes */
  67. if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
  68. err = -EINVAL;
  69. goto up;
  70. }
  71. if (sbi->options.sys_immutable) {
  72. if ((attr | oldattr) & ATTR_SYS) {
  73. if (!capable(CAP_LINUX_IMMUTABLE)) {
  74. err = -EPERM;
  75. goto up;
  76. }
  77. }
  78. }
  79. /* This MUST be done before doing anything irreversible... */
  80. err = notify_change(filp->f_dentry, &ia);
  81. if (err)
  82. goto up;
  83. if (sbi->options.sys_immutable) {
  84. if (attr & ATTR_SYS)
  85. inode->i_flags |= S_IMMUTABLE;
  86. else
  87. inode->i_flags &= S_IMMUTABLE;
  88. }
  89. MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
  90. mark_inode_dirty(inode);
  91. up:
  92. mutex_unlock(&inode->i_mutex);
  93. return err;
  94. }
  95. default:
  96. return -ENOTTY; /* Inappropriate ioctl for device */
  97. }
  98. }
  99. struct file_operations fat_file_operations = {
  100. .llseek = generic_file_llseek,
  101. .read = do_sync_read,
  102. .write = do_sync_write,
  103. .readv = generic_file_readv,
  104. .writev = generic_file_writev,
  105. .aio_read = generic_file_aio_read,
  106. .aio_write = generic_file_aio_write,
  107. .mmap = generic_file_mmap,
  108. .ioctl = fat_generic_ioctl,
  109. .fsync = file_fsync,
  110. .sendfile = generic_file_sendfile,
  111. };
  112. static int fat_cont_expand(struct inode *inode, loff_t size)
  113. {
  114. struct address_space *mapping = inode->i_mapping;
  115. loff_t start = inode->i_size, count = size - inode->i_size;
  116. int err;
  117. err = generic_cont_expand_simple(inode, size);
  118. if (err)
  119. goto out;
  120. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  121. mark_inode_dirty(inode);
  122. if (IS_SYNC(inode))
  123. err = sync_page_range_nolock(inode, mapping, start, count);
  124. out:
  125. return err;
  126. }
  127. int fat_notify_change(struct dentry *dentry, struct iattr *attr)
  128. {
  129. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  130. struct inode *inode = dentry->d_inode;
  131. int mask, error = 0;
  132. lock_kernel();
  133. /*
  134. * Expand the file. Since inode_setattr() updates ->i_size
  135. * before calling the ->truncate(), but FAT needs to fill the
  136. * hole before it.
  137. */
  138. if (attr->ia_valid & ATTR_SIZE) {
  139. if (attr->ia_size > inode->i_size) {
  140. error = fat_cont_expand(inode, attr->ia_size);
  141. if (error || attr->ia_valid == ATTR_SIZE)
  142. goto out;
  143. attr->ia_valid &= ~ATTR_SIZE;
  144. }
  145. }
  146. error = inode_change_ok(inode, attr);
  147. if (error) {
  148. if (sbi->options.quiet)
  149. error = 0;
  150. goto out;
  151. }
  152. if (((attr->ia_valid & ATTR_UID) &&
  153. (attr->ia_uid != sbi->options.fs_uid)) ||
  154. ((attr->ia_valid & ATTR_GID) &&
  155. (attr->ia_gid != sbi->options.fs_gid)) ||
  156. ((attr->ia_valid & ATTR_MODE) &&
  157. (attr->ia_mode & ~MSDOS_VALID_MODE)))
  158. error = -EPERM;
  159. if (error) {
  160. if (sbi->options.quiet)
  161. error = 0;
  162. goto out;
  163. }
  164. error = inode_setattr(inode, attr);
  165. if (error)
  166. goto out;
  167. if (S_ISDIR(inode->i_mode))
  168. mask = sbi->options.fs_dmask;
  169. else
  170. mask = sbi->options.fs_fmask;
  171. inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
  172. out:
  173. unlock_kernel();
  174. return error;
  175. }
  176. EXPORT_SYMBOL_GPL(fat_notify_change);
  177. /* Free all clusters after the skip'th cluster. */
  178. static int fat_free(struct inode *inode, int skip)
  179. {
  180. struct super_block *sb = inode->i_sb;
  181. int err, wait, free_start, i_start, i_logstart;
  182. if (MSDOS_I(inode)->i_start == 0)
  183. return 0;
  184. fat_cache_inval_inode(inode);
  185. wait = IS_DIRSYNC(inode);
  186. i_start = free_start = MSDOS_I(inode)->i_start;
  187. i_logstart = MSDOS_I(inode)->i_logstart;
  188. /* First, we write the new file size. */
  189. if (!skip) {
  190. MSDOS_I(inode)->i_start = 0;
  191. MSDOS_I(inode)->i_logstart = 0;
  192. }
  193. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  194. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  195. if (wait) {
  196. err = fat_sync_inode(inode);
  197. if (err) {
  198. MSDOS_I(inode)->i_start = i_start;
  199. MSDOS_I(inode)->i_logstart = i_logstart;
  200. return err;
  201. }
  202. } else
  203. mark_inode_dirty(inode);
  204. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  205. if (skip) {
  206. struct fat_entry fatent;
  207. int ret, fclus, dclus;
  208. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  209. if (ret < 0)
  210. return ret;
  211. else if (ret == FAT_ENT_EOF)
  212. return 0;
  213. fatent_init(&fatent);
  214. ret = fat_ent_read(inode, &fatent, dclus);
  215. if (ret == FAT_ENT_EOF) {
  216. fatent_brelse(&fatent);
  217. return 0;
  218. } else if (ret == FAT_ENT_FREE) {
  219. fat_fs_panic(sb,
  220. "%s: invalid cluster chain (i_pos %lld)",
  221. __FUNCTION__, MSDOS_I(inode)->i_pos);
  222. ret = -EIO;
  223. } else if (ret > 0) {
  224. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  225. if (err)
  226. ret = err;
  227. }
  228. fatent_brelse(&fatent);
  229. if (ret < 0)
  230. return ret;
  231. free_start = ret;
  232. }
  233. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  234. /* Freeing the remained cluster chain */
  235. return fat_free_clusters(inode, free_start);
  236. }
  237. void fat_truncate(struct inode *inode)
  238. {
  239. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  240. const unsigned int cluster_size = sbi->cluster_size;
  241. int nr_clusters;
  242. /*
  243. * This protects against truncating a file bigger than it was then
  244. * trying to write into the hole.
  245. */
  246. if (MSDOS_I(inode)->mmu_private > inode->i_size)
  247. MSDOS_I(inode)->mmu_private = inode->i_size;
  248. nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
  249. lock_kernel();
  250. fat_free(inode, nr_clusters);
  251. unlock_kernel();
  252. }
  253. struct inode_operations fat_file_inode_operations = {
  254. .truncate = fat_truncate,
  255. .setattr = fat_notify_change,
  256. };