file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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/compat.h>
  11. #include <linux/mount.h>
  12. #include <linux/time.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/writeback.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/fsnotify.h>
  18. #include <linux/security.h>
  19. #include "fat.h"
  20. #if defined(CONFIG_VMWARE_MVP)
  21. #include <linux/namei.h>
  22. #endif
  23. static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
  24. {
  25. u32 attr;
  26. mutex_lock(&inode->i_mutex);
  27. attr = fat_make_attrs(inode);
  28. mutex_unlock(&inode->i_mutex);
  29. return put_user(attr, user_attr);
  30. }
  31. static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
  32. {
  33. struct inode *inode = file->f_path.dentry->d_inode;
  34. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  35. int is_dir = S_ISDIR(inode->i_mode);
  36. u32 attr, oldattr;
  37. struct iattr ia;
  38. int err;
  39. err = get_user(attr, user_attr);
  40. if (err)
  41. goto out;
  42. mutex_lock(&inode->i_mutex);
  43. err = mnt_want_write_file(file);
  44. if (err)
  45. goto out_unlock_inode;
  46. /*
  47. * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
  48. * prevents the user from turning us into a VFAT
  49. * longname entry. Also, we obviously can't set
  50. * any of the NTFS attributes in the high 24 bits.
  51. */
  52. attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
  53. /* Merge in ATTR_VOLUME and ATTR_DIR */
  54. attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
  55. (is_dir ? ATTR_DIR : 0);
  56. oldattr = fat_make_attrs(inode);
  57. /* Equivalent to a chmod() */
  58. ia.ia_valid = ATTR_MODE | ATTR_CTIME;
  59. ia.ia_ctime = current_fs_time(inode->i_sb);
  60. if (is_dir)
  61. ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
  62. else {
  63. ia.ia_mode = fat_make_mode(sbi, attr,
  64. S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
  65. }
  66. /* The root directory has no attributes */
  67. if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
  68. err = -EINVAL;
  69. goto out_drop_write;
  70. }
  71. if (sbi->options.sys_immutable &&
  72. ((attr | oldattr) & ATTR_SYS) &&
  73. !capable(CAP_LINUX_IMMUTABLE)) {
  74. err = -EPERM;
  75. goto out_drop_write;
  76. }
  77. /*
  78. * The security check is questionable... We single
  79. * out the RO attribute for checking by the security
  80. * module, just because it maps to a file mode.
  81. */
  82. err = security_inode_setattr(file->f_path.dentry, &ia);
  83. if (err)
  84. goto out_drop_write;
  85. /* This MUST be done before doing anything irreversible... */
  86. err = fat_setattr(file->f_path.dentry, &ia);
  87. if (err)
  88. goto out_drop_write;
  89. fsnotify_change(file->f_path.dentry, ia.ia_valid);
  90. if (sbi->options.sys_immutable) {
  91. if (attr & ATTR_SYS)
  92. inode->i_flags |= S_IMMUTABLE;
  93. else
  94. inode->i_flags &= ~S_IMMUTABLE;
  95. }
  96. fat_save_attrs(inode, attr);
  97. mark_inode_dirty(inode);
  98. out_drop_write:
  99. mnt_drop_write_file(file);
  100. out_unlock_inode:
  101. mutex_unlock(&inode->i_mutex);
  102. out:
  103. return err;
  104. }
  105. #if defined(CONFIG_VMWARE_MVP)
  106. extern int _fat_fallocate(struct inode *inode, loff_t len);
  107. static long fat_vmw_extend(struct file *filp, unsigned long len)
  108. {
  109. struct inode *inode = filp->f_path.dentry->d_inode;
  110. loff_t off = len;
  111. const char mvpEnabledPath[] = "/data/data/com.vmware.mvp.enabled";
  112. struct path path;
  113. struct kstat stat;
  114. int err;
  115. /*
  116. * Perform some sanity checks (from do_fallocate)
  117. */
  118. if (len <= 0) {
  119. return -EINVAL;
  120. }
  121. if (!(filp->f_mode & FMODE_WRITE)) {
  122. return -EBADF;
  123. }
  124. /*
  125. * Revalidate the write permissions, in case security policy has
  126. * changed since the files were opened.
  127. */
  128. err = security_file_permission(filp, MAY_WRITE);
  129. if (err) {
  130. return err;
  131. }
  132. /*
  133. * Verify caller process belongs to mvp.
  134. */
  135. err = kern_path(mvpEnabledPath, 0, &path);
  136. if (err) {
  137. return err;
  138. }
  139. err = vfs_getattr(path.mnt, path.dentry, &stat);
  140. if (err) {
  141. return err;
  142. }
  143. if (current_euid() != stat.uid && current_euid() != 0) {
  144. return -EPERM;
  145. }
  146. /*
  147. * Every thing is clear, let's allocate space.
  148. */
  149. return _fat_fallocate(inode, off);
  150. }
  151. #endif
  152. long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  153. {
  154. struct inode *inode = filp->f_path.dentry->d_inode;
  155. u32 __user *user_attr = (u32 __user *)arg;
  156. switch (cmd) {
  157. case FAT_IOCTL_GET_ATTRIBUTES:
  158. return fat_ioctl_get_attributes(inode, user_attr);
  159. case FAT_IOCTL_SET_ATTRIBUTES:
  160. return fat_ioctl_set_attributes(filp, user_attr);
  161. #if defined(CONFIG_VMWARE_MVP)
  162. case FAT_IOCTL_VMW_EXTEND:
  163. return fat_vmw_extend(filp, arg);
  164. #endif
  165. default:
  166. return -ENOTTY; /* Inappropriate ioctl for device */
  167. }
  168. }
  169. #ifdef CONFIG_COMPAT
  170. static long fat_generic_compat_ioctl(struct file *filp, unsigned int cmd,
  171. unsigned long arg)
  172. {
  173. return fat_generic_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  174. }
  175. #endif
  176. static int fat_file_release(struct inode *inode, struct file *filp)
  177. {
  178. if ((filp->f_mode & FMODE_WRITE) &&
  179. MSDOS_SB(inode->i_sb)->options.flush) {
  180. fat_flush_inodes(inode->i_sb, inode, NULL);
  181. congestion_wait(BLK_RW_ASYNC, HZ/10);
  182. }
  183. return 0;
  184. }
  185. int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  186. {
  187. struct inode *inode = filp->f_mapping->host;
  188. int res, err;
  189. res = generic_file_fsync(filp, start, end, datasync);
  190. err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
  191. return res ? res : err;
  192. }
  193. const struct file_operations fat_file_operations = {
  194. .llseek = generic_file_llseek,
  195. .read = do_sync_read,
  196. .write = do_sync_write,
  197. .aio_read = generic_file_aio_read,
  198. .aio_write = generic_file_aio_write,
  199. .mmap = generic_file_mmap,
  200. .release = fat_file_release,
  201. .unlocked_ioctl = fat_generic_ioctl,
  202. #ifdef CONFIG_COMPAT
  203. .compat_ioctl = fat_generic_compat_ioctl,
  204. #endif
  205. .fsync = fat_file_fsync,
  206. .splice_read = generic_file_splice_read,
  207. };
  208. static int fat_cont_expand(struct inode *inode, loff_t size)
  209. {
  210. struct address_space *mapping = inode->i_mapping;
  211. loff_t start = inode->i_size, count = size - inode->i_size;
  212. int err;
  213. err = generic_cont_expand_simple(inode, size);
  214. if (err)
  215. goto out;
  216. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  217. mark_inode_dirty(inode);
  218. if (IS_SYNC(inode)) {
  219. int err2;
  220. /*
  221. * Opencode syncing since we don't have a file open to use
  222. * standard fsync path.
  223. */
  224. err = filemap_fdatawrite_range(mapping, start,
  225. start + count - 1);
  226. err2 = sync_mapping_buffers(mapping);
  227. if (!err)
  228. err = err2;
  229. err2 = write_inode_now(inode, 1);
  230. if (!err)
  231. err = err2;
  232. if (!err) {
  233. err = filemap_fdatawait_range(mapping, start,
  234. start + count - 1);
  235. }
  236. }
  237. out:
  238. return err;
  239. }
  240. /* Free all clusters after the skip'th cluster. */
  241. static int fat_free(struct inode *inode, int skip)
  242. {
  243. struct super_block *sb = inode->i_sb;
  244. int err, wait, free_start, i_start, i_logstart;
  245. if (MSDOS_I(inode)->i_start == 0)
  246. return 0;
  247. fat_cache_inval_inode(inode);
  248. wait = IS_DIRSYNC(inode);
  249. i_start = free_start = MSDOS_I(inode)->i_start;
  250. i_logstart = MSDOS_I(inode)->i_logstart;
  251. /* First, we write the new file size. */
  252. if (!skip) {
  253. MSDOS_I(inode)->i_start = 0;
  254. MSDOS_I(inode)->i_logstart = 0;
  255. }
  256. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  257. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  258. if (wait) {
  259. err = fat_sync_inode(inode);
  260. if (err) {
  261. MSDOS_I(inode)->i_start = i_start;
  262. MSDOS_I(inode)->i_logstart = i_logstart;
  263. return err;
  264. }
  265. } else
  266. mark_inode_dirty(inode);
  267. /* Write a new EOF, and get the remaining cluster chain for freeing. */
  268. if (skip) {
  269. struct fat_entry fatent;
  270. int ret, fclus, dclus;
  271. ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
  272. if (ret < 0)
  273. return ret;
  274. else if (ret == FAT_ENT_EOF)
  275. return 0;
  276. fatent_init(&fatent);
  277. ret = fat_ent_read(inode, &fatent, dclus);
  278. if (ret == FAT_ENT_EOF) {
  279. fatent_brelse(&fatent);
  280. return 0;
  281. } else if (ret == FAT_ENT_FREE) {
  282. fat_fs_error(sb,
  283. "%s: invalid cluster chain (i_pos %lld)",
  284. __func__, MSDOS_I(inode)->i_pos);
  285. ret = -EIO;
  286. } else if (ret > 0) {
  287. err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
  288. if (err)
  289. ret = err;
  290. }
  291. fatent_brelse(&fatent);
  292. if (ret < 0)
  293. return ret;
  294. free_start = ret;
  295. }
  296. inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
  297. /* Freeing the remained cluster chain */
  298. return fat_free_clusters(inode, free_start);
  299. }
  300. void fat_truncate_blocks(struct inode *inode, loff_t offset)
  301. {
  302. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  303. const unsigned int cluster_size = sbi->cluster_size;
  304. int nr_clusters;
  305. /*
  306. * This protects against truncating a file bigger than it was then
  307. * trying to write into the hole.
  308. */
  309. if (MSDOS_I(inode)->mmu_private > offset)
  310. MSDOS_I(inode)->mmu_private = offset;
  311. nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
  312. fat_free(inode, nr_clusters);
  313. fat_flush_inodes(inode->i_sb, inode, NULL);
  314. }
  315. int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  316. {
  317. struct inode *inode = dentry->d_inode;
  318. generic_fillattr(inode, stat);
  319. stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
  320. return 0;
  321. }
  322. EXPORT_SYMBOL_GPL(fat_getattr);
  323. static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
  324. struct inode *inode, umode_t *mode_ptr)
  325. {
  326. umode_t mask, perm;
  327. /*
  328. * Note, the basic check is already done by a caller of
  329. * (attr->ia_mode & ~FAT_VALID_MODE)
  330. */
  331. if (S_ISREG(inode->i_mode))
  332. mask = sbi->options.fs_fmask;
  333. else
  334. mask = sbi->options.fs_dmask;
  335. perm = *mode_ptr & ~(S_IFMT | mask);
  336. /*
  337. * Of the r and x bits, all (subject to umask) must be present. Of the
  338. * w bits, either all (subject to umask) or none must be present.
  339. *
  340. * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
  341. */
  342. if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
  343. return -EPERM;
  344. if (fat_mode_can_hold_ro(inode)) {
  345. if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
  346. return -EPERM;
  347. } else {
  348. if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
  349. return -EPERM;
  350. }
  351. *mode_ptr &= S_IFMT | perm;
  352. return 0;
  353. }
  354. static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
  355. {
  356. umode_t allow_utime = sbi->options.allow_utime;
  357. if (current_fsuid() != inode->i_uid) {
  358. if (in_group_p(inode->i_gid))
  359. allow_utime >>= 3;
  360. if (allow_utime & MAY_WRITE)
  361. return 1;
  362. }
  363. /* use a default check */
  364. return 0;
  365. }
  366. #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
  367. /* valid file mode bits */
  368. #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
  369. int fat_setattr(struct dentry *dentry, struct iattr *attr)
  370. {
  371. struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
  372. struct inode *inode = dentry->d_inode;
  373. unsigned int ia_valid;
  374. int error;
  375. /* Check for setting the inode time. */
  376. ia_valid = attr->ia_valid;
  377. if (ia_valid & TIMES_SET_FLAGS) {
  378. if (fat_allow_set_time(sbi, inode))
  379. attr->ia_valid &= ~TIMES_SET_FLAGS;
  380. }
  381. error = inode_change_ok(inode, attr);
  382. attr->ia_valid = ia_valid;
  383. if (error) {
  384. if (sbi->options.quiet)
  385. error = 0;
  386. goto out;
  387. }
  388. /*
  389. * Expand the file. Since inode_setattr() updates ->i_size
  390. * before calling the ->truncate(), but FAT needs to fill the
  391. * hole before it. XXX: this is no longer true with new truncate
  392. * sequence.
  393. */
  394. if (attr->ia_valid & ATTR_SIZE) {
  395. inode_dio_wait(inode);
  396. if (attr->ia_size > inode->i_size) {
  397. error = fat_cont_expand(inode, attr->ia_size);
  398. if (error || attr->ia_valid == ATTR_SIZE)
  399. goto out;
  400. attr->ia_valid &= ~ATTR_SIZE;
  401. }
  402. }
  403. if (((attr->ia_valid & ATTR_UID) &&
  404. (attr->ia_uid != sbi->options.fs_uid)) ||
  405. ((attr->ia_valid & ATTR_GID) &&
  406. (attr->ia_gid != sbi->options.fs_gid)) ||
  407. ((attr->ia_valid & ATTR_MODE) &&
  408. (attr->ia_mode & ~FAT_VALID_MODE)))
  409. error = -EPERM;
  410. if (error) {
  411. if (sbi->options.quiet)
  412. error = 0;
  413. goto out;
  414. }
  415. /*
  416. * We don't return -EPERM here. Yes, strange, but this is too
  417. * old behavior.
  418. */
  419. if (attr->ia_valid & ATTR_MODE) {
  420. if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
  421. attr->ia_valid &= ~ATTR_MODE;
  422. }
  423. if (attr->ia_valid & ATTR_SIZE) {
  424. down_write(&MSDOS_I(inode)->truncate_lock);
  425. truncate_setsize(inode, attr->ia_size);
  426. fat_truncate_blocks(inode, attr->ia_size);
  427. up_write(&MSDOS_I(inode)->truncate_lock);
  428. }
  429. setattr_copy(inode, attr);
  430. mark_inode_dirty(inode);
  431. out:
  432. return error;
  433. }
  434. EXPORT_SYMBOL_GPL(fat_setattr);
  435. const struct inode_operations fat_file_inode_operations = {
  436. .setattr = fat_setattr,
  437. .getattr = fat_getattr,
  438. #ifdef CONFIG_FAT_VIRTUAL_XATTR
  439. .setxattr = fat_setxattr,
  440. .getxattr = fat_getxattr,
  441. .listxattr = fat_listxattr,
  442. .removexattr = fat_removexattr,
  443. #endif
  444. };