iomode.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FUSE inode io modes.
  4. *
  5. * Copyright (c) 2024 CTERA Networks.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/file.h>
  11. #include <linux/fs.h>
  12. /*
  13. * Return true if need to wait for new opens in caching mode.
  14. */
  15. static inline bool fuse_is_io_cache_wait(struct fuse_inode *fi)
  16. {
  17. return READ_ONCE(fi->iocachectr) < 0 && !fuse_inode_backing(fi);
  18. }
  19. /*
  20. * Called on cached file open() and on first mmap() of direct_io file.
  21. * Takes cached_io inode mode reference to be dropped on file release.
  22. *
  23. * Blocks new parallel dio writes and waits for the in-progress parallel dio
  24. * writes to complete.
  25. */
  26. int fuse_file_cached_io_open(struct inode *inode, struct fuse_file *ff)
  27. {
  28. struct fuse_inode *fi = get_fuse_inode(inode);
  29. /* There are no io modes if server does not implement open */
  30. if (!ff->args)
  31. return 0;
  32. spin_lock(&fi->lock);
  33. /*
  34. * Setting the bit advises new direct-io writes to use an exclusive
  35. * lock - without it the wait below might be forever.
  36. */
  37. while (fuse_is_io_cache_wait(fi)) {
  38. set_bit(FUSE_I_CACHE_IO_MODE, &fi->state);
  39. spin_unlock(&fi->lock);
  40. wait_event(fi->direct_io_waitq, !fuse_is_io_cache_wait(fi));
  41. spin_lock(&fi->lock);
  42. }
  43. /*
  44. * Check if inode entered passthrough io mode while waiting for parallel
  45. * dio write completion.
  46. */
  47. if (fuse_inode_backing(fi)) {
  48. clear_bit(FUSE_I_CACHE_IO_MODE, &fi->state);
  49. spin_unlock(&fi->lock);
  50. return -ETXTBSY;
  51. }
  52. WARN_ON(ff->iomode == IOM_UNCACHED);
  53. if (ff->iomode == IOM_NONE) {
  54. ff->iomode = IOM_CACHED;
  55. if (fi->iocachectr == 0)
  56. set_bit(FUSE_I_CACHE_IO_MODE, &fi->state);
  57. fi->iocachectr++;
  58. }
  59. spin_unlock(&fi->lock);
  60. return 0;
  61. }
  62. static void fuse_file_cached_io_release(struct fuse_file *ff,
  63. struct fuse_inode *fi)
  64. {
  65. spin_lock(&fi->lock);
  66. WARN_ON(fi->iocachectr <= 0);
  67. WARN_ON(ff->iomode != IOM_CACHED);
  68. ff->iomode = IOM_NONE;
  69. fi->iocachectr--;
  70. if (fi->iocachectr == 0)
  71. clear_bit(FUSE_I_CACHE_IO_MODE, &fi->state);
  72. spin_unlock(&fi->lock);
  73. }
  74. /* Start strictly uncached io mode where cache access is not allowed */
  75. int fuse_inode_uncached_io_start(struct fuse_inode *fi, struct fuse_backing *fb)
  76. {
  77. struct fuse_backing *oldfb;
  78. int err = 0;
  79. spin_lock(&fi->lock);
  80. /* deny conflicting backing files on same fuse inode */
  81. oldfb = fuse_inode_backing(fi);
  82. if (fb && oldfb && oldfb != fb) {
  83. err = -EBUSY;
  84. goto unlock;
  85. }
  86. if (fi->iocachectr > 0) {
  87. err = -ETXTBSY;
  88. goto unlock;
  89. }
  90. fi->iocachectr--;
  91. /* fuse inode holds a single refcount of backing file */
  92. if (fb && !oldfb) {
  93. oldfb = fuse_inode_backing_set(fi, fb);
  94. WARN_ON_ONCE(oldfb != NULL);
  95. } else {
  96. fuse_backing_put(fb);
  97. }
  98. unlock:
  99. spin_unlock(&fi->lock);
  100. return err;
  101. }
  102. /* Takes uncached_io inode mode reference to be dropped on file release */
  103. static int fuse_file_uncached_io_open(struct inode *inode,
  104. struct fuse_file *ff,
  105. struct fuse_backing *fb)
  106. {
  107. struct fuse_inode *fi = get_fuse_inode(inode);
  108. int err;
  109. err = fuse_inode_uncached_io_start(fi, fb);
  110. if (err)
  111. return err;
  112. WARN_ON(ff->iomode != IOM_NONE);
  113. ff->iomode = IOM_UNCACHED;
  114. return 0;
  115. }
  116. void fuse_inode_uncached_io_end(struct fuse_inode *fi)
  117. {
  118. struct fuse_backing *oldfb = NULL;
  119. spin_lock(&fi->lock);
  120. WARN_ON(fi->iocachectr >= 0);
  121. fi->iocachectr++;
  122. if (!fi->iocachectr) {
  123. wake_up(&fi->direct_io_waitq);
  124. oldfb = fuse_inode_backing_set(fi, NULL);
  125. }
  126. spin_unlock(&fi->lock);
  127. if (oldfb)
  128. fuse_backing_put(oldfb);
  129. }
  130. /* Drop uncached_io reference from passthrough open */
  131. static void fuse_file_uncached_io_release(struct fuse_file *ff,
  132. struct fuse_inode *fi)
  133. {
  134. WARN_ON(ff->iomode != IOM_UNCACHED);
  135. ff->iomode = IOM_NONE;
  136. fuse_inode_uncached_io_end(fi);
  137. }
  138. /*
  139. * Open flags that are allowed in combination with FOPEN_PASSTHROUGH.
  140. * A combination of FOPEN_PASSTHROUGH and FOPEN_DIRECT_IO means that read/write
  141. * operations go directly to the server, but mmap is done on the backing file.
  142. * FOPEN_PASSTHROUGH mode should not co-exist with any users of the fuse inode
  143. * page cache, so FOPEN_KEEP_CACHE is a strange and undesired combination.
  144. */
  145. #define FOPEN_PASSTHROUGH_MASK \
  146. (FOPEN_PASSTHROUGH | FOPEN_DIRECT_IO | FOPEN_PARALLEL_DIRECT_WRITES | \
  147. FOPEN_NOFLUSH)
  148. static int fuse_file_passthrough_open(struct inode *inode, struct file *file)
  149. {
  150. struct fuse_file *ff = file->private_data;
  151. struct fuse_conn *fc = get_fuse_conn(inode);
  152. struct fuse_backing *fb;
  153. int err;
  154. /* Check allowed conditions for file open in passthrough mode */
  155. if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) || !fc->passthrough ||
  156. (ff->open_flags & ~FOPEN_PASSTHROUGH_MASK))
  157. return -EINVAL;
  158. fb = fuse_passthrough_open(file, inode,
  159. ff->args->open_outarg.backing_id);
  160. if (IS_ERR(fb))
  161. return PTR_ERR(fb);
  162. /* First passthrough file open denies caching inode io mode */
  163. err = fuse_file_uncached_io_open(inode, ff, fb);
  164. if (!err)
  165. return 0;
  166. fuse_passthrough_release(ff, fb);
  167. fuse_backing_put(fb);
  168. return err;
  169. }
  170. /* Request access to submit new io to inode via open file */
  171. int fuse_file_io_open(struct file *file, struct inode *inode)
  172. {
  173. struct fuse_file *ff = file->private_data;
  174. struct fuse_inode *fi = get_fuse_inode(inode);
  175. int err;
  176. /*
  177. * io modes are not relevant with DAX and with server that does not
  178. * implement open.
  179. */
  180. if (FUSE_IS_DAX(inode) || !ff->args)
  181. return 0;
  182. /*
  183. * Server is expected to use FOPEN_PASSTHROUGH for all opens of an inode
  184. * which is already open for passthrough.
  185. */
  186. err = -EINVAL;
  187. if (fuse_inode_backing(fi) && !(ff->open_flags & FOPEN_PASSTHROUGH))
  188. goto fail;
  189. /*
  190. * FOPEN_PARALLEL_DIRECT_WRITES requires FOPEN_DIRECT_IO.
  191. */
  192. if (!(ff->open_flags & FOPEN_DIRECT_IO))
  193. ff->open_flags &= ~FOPEN_PARALLEL_DIRECT_WRITES;
  194. /*
  195. * First passthrough file open denies caching inode io mode.
  196. * First caching file open enters caching inode io mode.
  197. *
  198. * Note that if user opens a file open with O_DIRECT, but server did
  199. * not specify FOPEN_DIRECT_IO, a later fcntl() could remove O_DIRECT,
  200. * so we put the inode in caching mode to prevent parallel dio.
  201. */
  202. if ((ff->open_flags & FOPEN_DIRECT_IO) &&
  203. !(ff->open_flags & FOPEN_PASSTHROUGH))
  204. return 0;
  205. if (ff->open_flags & FOPEN_PASSTHROUGH)
  206. err = fuse_file_passthrough_open(inode, file);
  207. else
  208. err = fuse_file_cached_io_open(inode, ff);
  209. if (err)
  210. goto fail;
  211. return 0;
  212. fail:
  213. pr_debug("failed to open file in requested io mode (open_flags=0x%x, err=%i).\n",
  214. ff->open_flags, err);
  215. /*
  216. * The file open mode determines the inode io mode.
  217. * Using incorrect open mode is a server mistake, which results in
  218. * user visible failure of open() with EIO error.
  219. */
  220. return -EIO;
  221. }
  222. /* No more pending io and no new io possible to inode via open/mmapped file */
  223. void fuse_file_io_release(struct fuse_file *ff, struct inode *inode)
  224. {
  225. struct fuse_inode *fi = get_fuse_inode(inode);
  226. /*
  227. * Last passthrough file close allows caching inode io mode.
  228. * Last caching file close exits caching inode io mode.
  229. */
  230. switch (ff->iomode) {
  231. case IOM_NONE:
  232. /* Nothing to do */
  233. break;
  234. case IOM_UNCACHED:
  235. fuse_file_uncached_io_release(ff, fi);
  236. break;
  237. case IOM_CACHED:
  238. fuse_file_cached_io_release(ff, fi);
  239. break;
  240. }
  241. }