file.c 6.9 KB

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