file.c 7.2 KB

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