file.c 6.2 KB

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