backing-file.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common helpers for stackable filesystems and backing files.
  4. *
  5. * Forked from fs/overlayfs/file.c.
  6. *
  7. * Copyright (C) 2017 Red Hat, Inc.
  8. * Copyright (C) 2023 CTERA Networks.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/backing-file.h>
  12. #include <linux/splice.h>
  13. #include <linux/mm.h>
  14. #include "internal.h"
  15. /**
  16. * backing_file_open - open a backing file for kernel internal use
  17. * @user_path: path that the user reuqested to open
  18. * @flags: open flags
  19. * @real_path: path of the backing file
  20. * @cred: credentials for open
  21. *
  22. * Open a backing file for a stackable filesystem (e.g., overlayfs).
  23. * @user_path may be on the stackable filesystem and @real_path on the
  24. * underlying filesystem. In this case, we want to be able to return the
  25. * @user_path of the stackable filesystem. This is done by embedding the
  26. * returned file into a container structure that also stores the stacked
  27. * file's path, which can be retrieved using backing_file_user_path().
  28. */
  29. struct file *backing_file_open(const struct path *user_path, int flags,
  30. const struct path *real_path,
  31. const struct cred *cred)
  32. {
  33. struct file *f;
  34. int error;
  35. f = alloc_empty_backing_file(flags, cred);
  36. if (IS_ERR(f))
  37. return f;
  38. path_get(user_path);
  39. *backing_file_user_path(f) = *user_path;
  40. error = vfs_open(real_path, f);
  41. if (error) {
  42. fput(f);
  43. f = ERR_PTR(error);
  44. }
  45. return f;
  46. }
  47. EXPORT_SYMBOL_GPL(backing_file_open);
  48. struct file *backing_tmpfile_open(const struct path *user_path, int flags,
  49. const struct path *real_parentpath,
  50. umode_t mode, const struct cred *cred)
  51. {
  52. struct mnt_idmap *real_idmap = mnt_idmap(real_parentpath->mnt);
  53. struct file *f;
  54. int error;
  55. f = alloc_empty_backing_file(flags, cred);
  56. if (IS_ERR(f))
  57. return f;
  58. path_get(user_path);
  59. *backing_file_user_path(f) = *user_path;
  60. error = vfs_tmpfile(real_idmap, real_parentpath, f, mode);
  61. if (error) {
  62. fput(f);
  63. f = ERR_PTR(error);
  64. }
  65. return f;
  66. }
  67. EXPORT_SYMBOL(backing_tmpfile_open);
  68. struct backing_aio {
  69. struct kiocb iocb;
  70. refcount_t ref;
  71. struct kiocb *orig_iocb;
  72. /* used for aio completion */
  73. void (*end_write)(struct file *, loff_t, ssize_t);
  74. struct work_struct work;
  75. long res;
  76. };
  77. static struct kmem_cache *backing_aio_cachep;
  78. #define BACKING_IOCB_MASK \
  79. (IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND)
  80. static rwf_t iocb_to_rw_flags(int flags)
  81. {
  82. return (__force rwf_t)(flags & BACKING_IOCB_MASK);
  83. }
  84. static void backing_aio_put(struct backing_aio *aio)
  85. {
  86. if (refcount_dec_and_test(&aio->ref)) {
  87. fput(aio->iocb.ki_filp);
  88. kmem_cache_free(backing_aio_cachep, aio);
  89. }
  90. }
  91. static void backing_aio_cleanup(struct backing_aio *aio, long res)
  92. {
  93. struct kiocb *iocb = &aio->iocb;
  94. struct kiocb *orig_iocb = aio->orig_iocb;
  95. if (aio->end_write)
  96. aio->end_write(orig_iocb->ki_filp, iocb->ki_pos, res);
  97. orig_iocb->ki_pos = iocb->ki_pos;
  98. backing_aio_put(aio);
  99. }
  100. static void backing_aio_rw_complete(struct kiocb *iocb, long res)
  101. {
  102. struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
  103. struct kiocb *orig_iocb = aio->orig_iocb;
  104. if (iocb->ki_flags & IOCB_WRITE)
  105. kiocb_end_write(iocb);
  106. backing_aio_cleanup(aio, res);
  107. orig_iocb->ki_complete(orig_iocb, res);
  108. }
  109. static void backing_aio_complete_work(struct work_struct *work)
  110. {
  111. struct backing_aio *aio = container_of(work, struct backing_aio, work);
  112. backing_aio_rw_complete(&aio->iocb, aio->res);
  113. }
  114. static void backing_aio_queue_completion(struct kiocb *iocb, long res)
  115. {
  116. struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
  117. /*
  118. * Punt to a work queue to serialize updates of mtime/size.
  119. */
  120. aio->res = res;
  121. INIT_WORK(&aio->work, backing_aio_complete_work);
  122. queue_work(file_inode(aio->orig_iocb->ki_filp)->i_sb->s_dio_done_wq,
  123. &aio->work);
  124. }
  125. static int backing_aio_init_wq(struct kiocb *iocb)
  126. {
  127. struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
  128. if (sb->s_dio_done_wq)
  129. return 0;
  130. return sb_init_dio_done_wq(sb);
  131. }
  132. ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
  133. struct kiocb *iocb, int flags,
  134. struct backing_file_ctx *ctx)
  135. {
  136. struct backing_aio *aio = NULL;
  137. const struct cred *old_cred;
  138. ssize_t ret;
  139. if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
  140. return -EIO;
  141. if (!iov_iter_count(iter))
  142. return 0;
  143. if (iocb->ki_flags & IOCB_DIRECT &&
  144. !(file->f_mode & FMODE_CAN_ODIRECT))
  145. return -EINVAL;
  146. old_cred = override_creds(ctx->cred);
  147. if (is_sync_kiocb(iocb)) {
  148. rwf_t rwf = iocb_to_rw_flags(flags);
  149. ret = vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
  150. } else {
  151. ret = -ENOMEM;
  152. aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
  153. if (!aio)
  154. goto out;
  155. aio->orig_iocb = iocb;
  156. kiocb_clone(&aio->iocb, iocb, get_file(file));
  157. aio->iocb.ki_complete = backing_aio_rw_complete;
  158. refcount_set(&aio->ref, 2);
  159. ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
  160. backing_aio_put(aio);
  161. if (ret != -EIOCBQUEUED)
  162. backing_aio_cleanup(aio, ret);
  163. }
  164. out:
  165. revert_creds(old_cred);
  166. if (ctx->accessed)
  167. ctx->accessed(ctx->user_file);
  168. return ret;
  169. }
  170. EXPORT_SYMBOL_GPL(backing_file_read_iter);
  171. ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
  172. struct kiocb *iocb, int flags,
  173. struct backing_file_ctx *ctx)
  174. {
  175. const struct cred *old_cred;
  176. ssize_t ret;
  177. if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
  178. return -EIO;
  179. if (!iov_iter_count(iter))
  180. return 0;
  181. ret = file_remove_privs(ctx->user_file);
  182. if (ret)
  183. return ret;
  184. if (iocb->ki_flags & IOCB_DIRECT &&
  185. !(file->f_mode & FMODE_CAN_ODIRECT))
  186. return -EINVAL;
  187. /*
  188. * Stacked filesystems don't support deferred completions, don't copy
  189. * this property in case it is set by the issuer.
  190. */
  191. flags &= ~IOCB_DIO_CALLER_COMP;
  192. old_cred = override_creds(ctx->cred);
  193. if (is_sync_kiocb(iocb)) {
  194. rwf_t rwf = iocb_to_rw_flags(flags);
  195. ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
  196. if (ctx->end_write)
  197. ctx->end_write(ctx->user_file, iocb->ki_pos, ret);
  198. } else {
  199. struct backing_aio *aio;
  200. ret = backing_aio_init_wq(iocb);
  201. if (ret)
  202. goto out;
  203. ret = -ENOMEM;
  204. aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
  205. if (!aio)
  206. goto out;
  207. aio->orig_iocb = iocb;
  208. aio->end_write = ctx->end_write;
  209. kiocb_clone(&aio->iocb, iocb, get_file(file));
  210. aio->iocb.ki_flags = flags;
  211. aio->iocb.ki_complete = backing_aio_queue_completion;
  212. refcount_set(&aio->ref, 2);
  213. ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
  214. backing_aio_put(aio);
  215. if (ret != -EIOCBQUEUED)
  216. backing_aio_cleanup(aio, ret);
  217. }
  218. out:
  219. revert_creds(old_cred);
  220. return ret;
  221. }
  222. EXPORT_SYMBOL_GPL(backing_file_write_iter);
  223. ssize_t backing_file_splice_read(struct file *in, loff_t *ppos,
  224. struct pipe_inode_info *pipe, size_t len,
  225. unsigned int flags,
  226. struct backing_file_ctx *ctx)
  227. {
  228. const struct cred *old_cred;
  229. ssize_t ret;
  230. if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING)))
  231. return -EIO;
  232. old_cred = override_creds(ctx->cred);
  233. ret = vfs_splice_read(in, ppos, pipe, len, flags);
  234. revert_creds(old_cred);
  235. if (ctx->accessed)
  236. ctx->accessed(ctx->user_file);
  237. return ret;
  238. }
  239. EXPORT_SYMBOL_GPL(backing_file_splice_read);
  240. ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
  241. struct file *out, loff_t *ppos, size_t len,
  242. unsigned int flags,
  243. struct backing_file_ctx *ctx)
  244. {
  245. const struct cred *old_cred;
  246. ssize_t ret;
  247. if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING)))
  248. return -EIO;
  249. if (!out->f_op->splice_write)
  250. return -EINVAL;
  251. ret = file_remove_privs(ctx->user_file);
  252. if (ret)
  253. return ret;
  254. old_cred = override_creds(ctx->cred);
  255. file_start_write(out);
  256. ret = out->f_op->splice_write(pipe, out, ppos, len, flags);
  257. file_end_write(out);
  258. revert_creds(old_cred);
  259. if (ctx->end_write)
  260. ctx->end_write(ctx->user_file, ppos ? *ppos : 0, ret);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL_GPL(backing_file_splice_write);
  264. int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
  265. struct backing_file_ctx *ctx)
  266. {
  267. const struct cred *old_cred;
  268. int ret;
  269. if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)) ||
  270. WARN_ON_ONCE(ctx->user_file != vma->vm_file))
  271. return -EIO;
  272. if (!file->f_op->mmap)
  273. return -ENODEV;
  274. vma_set_file(vma, file);
  275. old_cred = override_creds(ctx->cred);
  276. ret = call_mmap(vma->vm_file, vma);
  277. revert_creds(old_cred);
  278. if (ctx->accessed)
  279. ctx->accessed(ctx->user_file);
  280. return ret;
  281. }
  282. EXPORT_SYMBOL_GPL(backing_file_mmap);
  283. static int __init backing_aio_init(void)
  284. {
  285. backing_aio_cachep = KMEM_CACHE(backing_aio, SLAB_HWCACHE_ALIGN);
  286. if (!backing_aio_cachep)
  287. return -ENOMEM;
  288. return 0;
  289. }
  290. fs_initcall(backing_aio_init);