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