file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * fs/sdcardfs/file.c
  3. *
  4. * Copyright (c) 2013 Samsung Electronics Co. Ltd
  5. * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
  6. * Sunghwan Yun, Sungjong Seo
  7. *
  8. * This program has been developed as a stackable file system based on
  9. * the WrapFS which written by
  10. *
  11. * Copyright (c) 1998-2011 Erez Zadok
  12. * Copyright (c) 2009 Shrikar Archak
  13. * Copyright (c) 2003-2011 Stony Brook University
  14. * Copyright (c) 2003-2011 The Research Foundation of SUNY
  15. *
  16. * This file is dual licensed. It may be redistributed and/or modified
  17. * under the terms of the Apache 2.0 License OR version 2 of the GNU
  18. * General Public License.
  19. */
  20. #include "sdcardfs.h"
  21. #ifdef CONFIG_SDCARD_FS_FADV_NOACTIVE
  22. #include <linux/backing-dev.h>
  23. #endif
  24. static ssize_t sdcardfs_read(struct file *file, char __user *buf,
  25. size_t count, loff_t *ppos)
  26. {
  27. int err;
  28. struct file *lower_file;
  29. struct dentry *dentry = file->f_path.dentry;
  30. #ifdef CONFIG_SDCARD_FS_FADV_NOACTIVE
  31. struct backing_dev_info *bdi;
  32. #endif
  33. lower_file = sdcardfs_lower_file(file);
  34. #ifdef CONFIG_SDCARD_FS_FADV_NOACTIVE
  35. if (file->f_mode & FMODE_NOACTIVE) {
  36. if (!(lower_file->f_mode & FMODE_NOACTIVE)) {
  37. bdi = lower_file->f_mapping->backing_dev_info;
  38. lower_file->f_ra.ra_pages = bdi->ra_pages * 2;
  39. spin_lock(&lower_file->f_lock);
  40. lower_file->f_mode |= FMODE_NOACTIVE;
  41. spin_unlock(&lower_file->f_lock);
  42. }
  43. }
  44. #endif
  45. err = vfs_read(lower_file, buf, count, ppos);
  46. /* update our inode atime upon a successful lower read */
  47. if (err >= 0)
  48. fsstack_copy_attr_atime(dentry->d_inode,
  49. lower_file->f_path.dentry->d_inode);
  50. return err;
  51. }
  52. static ssize_t sdcardfs_write(struct file *file, const char __user *buf,
  53. size_t count, loff_t *ppos)
  54. {
  55. int err = 0;
  56. struct file *lower_file;
  57. struct dentry *dentry = file->f_path.dentry;
  58. struct inode *inode = dentry->d_inode;
  59. /* check disk space */
  60. if (!check_min_free_space(dentry, count, 0)) {
  61. pr_err("No minimum free space.\n");
  62. return -ENOSPC;
  63. }
  64. lower_file = sdcardfs_lower_file(file);
  65. err = vfs_write(lower_file, buf, count, ppos);
  66. /* update our inode times+sizes upon a successful lower write */
  67. if (err >= 0) {
  68. if (sizeof(loff_t) > sizeof(long))
  69. mutex_lock(&inode->i_mutex);
  70. fsstack_copy_inode_size(inode, lower_file->f_path.dentry->d_inode);
  71. fsstack_copy_attr_times(inode, lower_file->f_path.dentry->d_inode);
  72. if (sizeof(loff_t) > sizeof(long))
  73. mutex_unlock(&inode->i_mutex);
  74. }
  75. return err;
  76. }
  77. static int sdcardfs_readdir(struct file *file, struct dir_context *ctx)
  78. {
  79. int err = 0;
  80. struct file *lower_file = NULL;
  81. struct dentry *dentry = file->f_path.dentry;
  82. lower_file = sdcardfs_lower_file(file);
  83. lower_file->f_pos = file->f_pos;
  84. err = iterate_dir(lower_file, ctx);
  85. file->f_pos = lower_file->f_pos;
  86. if (err >= 0) /* copy the atime */
  87. fsstack_copy_attr_atime(dentry->d_inode,
  88. lower_file->f_path.dentry->d_inode);
  89. return err;
  90. }
  91. static long sdcardfs_unlocked_ioctl(struct file *file, unsigned int cmd,
  92. unsigned long arg)
  93. {
  94. long err = -ENOTTY;
  95. struct file *lower_file;
  96. const struct cred *saved_cred = NULL;
  97. struct dentry *dentry = file->f_path.dentry;
  98. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
  99. lower_file = sdcardfs_lower_file(file);
  100. /* XXX: use vfs_ioctl if/when VFS exports it */
  101. if (!lower_file || !lower_file->f_op)
  102. goto out;
  103. /* save current_cred and override it */
  104. saved_cred = override_fsids(sbi, SDCARDFS_I(file->f_path.dentry->d_inode)->data);
  105. if (!saved_cred) {
  106. err = -ENOMEM;
  107. goto out;
  108. }
  109. if (lower_file->f_op->unlocked_ioctl)
  110. err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
  111. /* some ioctls can change inode attributes (EXT2_IOC_SETFLAGS) */
  112. if (!err)
  113. sdcardfs_copy_and_fix_attrs(file->f_path.dentry->d_inode,
  114. lower_file->f_path.dentry->d_inode);
  115. revert_fsids(saved_cred);
  116. out:
  117. return err;
  118. }
  119. #ifdef CONFIG_COMPAT
  120. static long sdcardfs_compat_ioctl(struct file *file, unsigned int cmd,
  121. unsigned long arg)
  122. {
  123. long err = -ENOTTY;
  124. struct file *lower_file;
  125. const struct cred *saved_cred = NULL;
  126. struct dentry *dentry = file->f_path.dentry;
  127. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
  128. lower_file = sdcardfs_lower_file(file);
  129. /* XXX: use vfs_ioctl if/when VFS exports it */
  130. if (!lower_file || !lower_file->f_op)
  131. goto out;
  132. /* save current_cred and override it */
  133. saved_cred = override_fsids(sbi, SDCARDFS_I(file->f_path.dentry->d_inode)->data);
  134. if (!saved_cred)
  135. return -ENOMEM;
  136. if (lower_file->f_op->compat_ioctl)
  137. err = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
  138. revert_fsids(saved_cred);
  139. out:
  140. return err;
  141. }
  142. #endif
  143. static int sdcardfs_mmap(struct file *file, struct vm_area_struct *vma)
  144. {
  145. int err = 0;
  146. bool willwrite;
  147. struct file *lower_file;
  148. const struct vm_operations_struct *saved_vm_ops = NULL;
  149. /* this might be deferred to mmap's writepage */
  150. willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
  151. /*
  152. * File systems which do not implement ->writepage may use
  153. * generic_file_readonly_mmap as their ->mmap op. If you call
  154. * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
  155. * But we cannot call the lower ->mmap op, so we can't tell that
  156. * writeable mappings won't work. Therefore, our only choice is to
  157. * check if the lower file system supports the ->writepage, and if
  158. * not, return EINVAL (the same error that
  159. * generic_file_readonly_mmap returns in that case).
  160. */
  161. lower_file = sdcardfs_lower_file(file);
  162. if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
  163. err = -EINVAL;
  164. pr_err("sdcardfs: lower file system does not support writeable mmap\n");
  165. goto out;
  166. }
  167. /*
  168. * find and save lower vm_ops.
  169. *
  170. * XXX: the VFS should have a cleaner way of finding the lower vm_ops
  171. */
  172. if (!SDCARDFS_F(file)->lower_vm_ops) {
  173. err = lower_file->f_op->mmap(lower_file, vma);
  174. if (err) {
  175. pr_err("sdcardfs: lower mmap failed %d\n", err);
  176. goto out;
  177. }
  178. saved_vm_ops = vma->vm_ops; /* save: came from lower ->mmap */
  179. }
  180. /*
  181. * Next 3 lines are all I need from generic_file_mmap. I definitely
  182. * don't want its test for ->readpage which returns -ENOEXEC.
  183. */
  184. file_accessed(file);
  185. vma->vm_ops = &sdcardfs_vm_ops;
  186. file->f_mapping->a_ops = &sdcardfs_aops; /* set our aops */
  187. if (!SDCARDFS_F(file)->lower_vm_ops) /* save for our ->fault */
  188. SDCARDFS_F(file)->lower_vm_ops = saved_vm_ops;
  189. vma->vm_private_data = file;
  190. get_file(lower_file);
  191. vma->vm_file = lower_file;
  192. out:
  193. return err;
  194. }
  195. static int sdcardfs_open(struct inode *inode, struct file *file)
  196. {
  197. int err = 0;
  198. struct file *lower_file = NULL;
  199. struct path lower_path;
  200. struct dentry *dentry = file->f_path.dentry;
  201. struct dentry *parent = dget_parent(dentry);
  202. struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
  203. const struct cred *saved_cred = NULL;
  204. /* don't open unhashed/deleted files */
  205. if (d_unhashed(dentry)) {
  206. err = -ENOENT;
  207. goto out_err;
  208. }
  209. if (!check_caller_access_to_name(parent->d_inode, &dentry->d_name)) {
  210. err = -EACCES;
  211. goto out_err;
  212. }
  213. /* save current_cred and override it */
  214. saved_cred = override_fsids(sbi, SDCARDFS_I(inode)->data);
  215. if (!saved_cred) {
  216. err = -ENOMEM;
  217. goto out_err;
  218. }
  219. file->private_data =
  220. kzalloc(sizeof(struct sdcardfs_file_info), GFP_KERNEL);
  221. if (!SDCARDFS_F(file)) {
  222. err = -ENOMEM;
  223. goto out_revert_cred;
  224. }
  225. /* open lower object and link sdcardfs's file struct to lower's */
  226. sdcardfs_get_lower_path(file->f_path.dentry, &lower_path);
  227. lower_file = dentry_open(lower_path.dentry, lower_path.mnt,
  228. file->f_flags, current_cred());
  229. if (IS_ERR(lower_file)) {
  230. err = PTR_ERR(lower_file);
  231. lower_file = sdcardfs_lower_file(file);
  232. if (lower_file) {
  233. sdcardfs_set_lower_file(file, NULL);
  234. fput(lower_file); /* fput calls dput for lower_dentry */
  235. }
  236. } else {
  237. sdcardfs_set_lower_file(file, lower_file);
  238. }
  239. if (err)
  240. kfree(SDCARDFS_F(file));
  241. else
  242. sdcardfs_copy_and_fix_attrs(inode, sdcardfs_lower_inode(inode));
  243. out_revert_cred:
  244. revert_fsids(saved_cred);
  245. out_err:
  246. dput(parent);
  247. return err;
  248. }
  249. static int sdcardfs_flush(struct file *file, fl_owner_t id)
  250. {
  251. int err = 0;
  252. struct file *lower_file = NULL;
  253. lower_file = sdcardfs_lower_file(file);
  254. if (lower_file && lower_file->f_op && lower_file->f_op->flush) {
  255. filemap_write_and_wait(file->f_mapping);
  256. err = lower_file->f_op->flush(lower_file, id);
  257. }
  258. return err;
  259. }
  260. /* release all lower object references & free the file info structure */
  261. static int sdcardfs_file_release(struct inode *inode, struct file *file)
  262. {
  263. struct file *lower_file;
  264. lower_file = sdcardfs_lower_file(file);
  265. if (lower_file) {
  266. sdcardfs_set_lower_file(file, NULL);
  267. fput(lower_file);
  268. }
  269. kfree(SDCARDFS_F(file));
  270. return 0;
  271. }
  272. static int sdcardfs_fsync(struct file *file, loff_t start, loff_t end,
  273. int datasync)
  274. {
  275. int err;
  276. struct file *lower_file;
  277. struct path lower_path;
  278. struct dentry *dentry = file->f_path.dentry;
  279. err = generic_file_fsync(file, start, end, datasync);
  280. if (err)
  281. goto out;
  282. lower_file = sdcardfs_lower_file(file);
  283. sdcardfs_get_lower_path(dentry, &lower_path);
  284. err = vfs_fsync_range(lower_file, start, end, datasync);
  285. sdcardfs_put_lower_path(dentry, &lower_path);
  286. out:
  287. return err;
  288. }
  289. static int sdcardfs_fasync(int fd, struct file *file, int flag)
  290. {
  291. int err = 0;
  292. struct file *lower_file = NULL;
  293. lower_file = sdcardfs_lower_file(file);
  294. if (lower_file->f_op && lower_file->f_op->fasync)
  295. err = lower_file->f_op->fasync(fd, lower_file, flag);
  296. return err;
  297. }
  298. /*
  299. * Sdcardfs cannot use generic_file_llseek as ->llseek, because it would
  300. * only set the offset of the upper file. So we have to implement our
  301. * own method to set both the upper and lower file offsets
  302. * consistently.
  303. */
  304. static loff_t sdcardfs_file_llseek(struct file *file, loff_t offset, int whence)
  305. {
  306. int err;
  307. struct file *lower_file;
  308. err = generic_file_llseek(file, offset, whence);
  309. if (err < 0)
  310. goto out;
  311. lower_file = sdcardfs_lower_file(file);
  312. err = generic_file_llseek(lower_file, offset, whence);
  313. out:
  314. return err;
  315. }
  316. const struct file_operations sdcardfs_main_fops = {
  317. .llseek = generic_file_llseek,
  318. .read = sdcardfs_read,
  319. .write = sdcardfs_write,
  320. .unlocked_ioctl = sdcardfs_unlocked_ioctl,
  321. #ifdef CONFIG_COMPAT
  322. .compat_ioctl = sdcardfs_compat_ioctl,
  323. #endif
  324. .mmap = sdcardfs_mmap,
  325. .open = sdcardfs_open,
  326. .flush = sdcardfs_flush,
  327. .release = sdcardfs_file_release,
  328. .fsync = sdcardfs_fsync,
  329. .fasync = sdcardfs_fasync,
  330. };
  331. /* trimmed directory options */
  332. const struct file_operations sdcardfs_dir_fops = {
  333. .llseek = sdcardfs_file_llseek,
  334. .read = generic_read_dir,
  335. .iterate = sdcardfs_readdir,
  336. .unlocked_ioctl = sdcardfs_unlocked_ioctl,
  337. #ifdef CONFIG_COMPAT
  338. .compat_ioctl = sdcardfs_compat_ioctl,
  339. #endif
  340. .open = sdcardfs_open,
  341. .release = sdcardfs_file_release,
  342. .flush = sdcardfs_flush,
  343. .fsync = sdcardfs_fsync,
  344. .fasync = sdcardfs_fasync,
  345. };