iomode.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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, ff->args->open_outarg.backing_id);
  159. if (IS_ERR(fb))
  160. return PTR_ERR(fb);
  161. /* First passthrough file open denies caching inode io mode */
  162. err = fuse_file_uncached_io_open(inode, ff, fb);
  163. if (!err)
  164. return 0;
  165. fuse_passthrough_release(ff, fb);
  166. fuse_backing_put(fb);
  167. return err;
  168. }
  169. /* Request access to submit new io to inode via open file */
  170. int fuse_file_io_open(struct file *file, struct inode *inode)
  171. {
  172. struct fuse_file *ff = file->private_data;
  173. struct fuse_inode *fi = get_fuse_inode(inode);
  174. int err;
  175. /*
  176. * io modes are not relevant with DAX and with server that does not
  177. * implement open.
  178. */
  179. if (FUSE_IS_DAX(inode) || !ff->args)
  180. return 0;
  181. /*
  182. * Server is expected to use FOPEN_PASSTHROUGH for all opens of an inode
  183. * which is already open for passthrough.
  184. */
  185. err = -EINVAL;
  186. if (fuse_inode_backing(fi) && !(ff->open_flags & FOPEN_PASSTHROUGH))
  187. goto fail;
  188. /*
  189. * FOPEN_PARALLEL_DIRECT_WRITES requires FOPEN_DIRECT_IO.
  190. */
  191. if (!(ff->open_flags & FOPEN_DIRECT_IO))
  192. ff->open_flags &= ~FOPEN_PARALLEL_DIRECT_WRITES;
  193. /*
  194. * First passthrough file open denies caching inode io mode.
  195. * First caching file open enters caching inode io mode.
  196. *
  197. * Note that if user opens a file open with O_DIRECT, but server did
  198. * not specify FOPEN_DIRECT_IO, a later fcntl() could remove O_DIRECT,
  199. * so we put the inode in caching mode to prevent parallel dio.
  200. */
  201. if ((ff->open_flags & FOPEN_DIRECT_IO) &&
  202. !(ff->open_flags & FOPEN_PASSTHROUGH))
  203. return 0;
  204. if (ff->open_flags & FOPEN_PASSTHROUGH)
  205. err = fuse_file_passthrough_open(inode, file);
  206. else
  207. err = fuse_file_cached_io_open(inode, ff);
  208. if (err)
  209. goto fail;
  210. return 0;
  211. fail:
  212. pr_debug("failed to open file in requested io mode (open_flags=0x%x, err=%i).\n",
  213. ff->open_flags, err);
  214. /*
  215. * The file open mode determines the inode io mode.
  216. * Using incorrect open mode is a server mistake, which results in
  217. * user visible failure of open() with EIO error.
  218. */
  219. return -EIO;
  220. }
  221. /* No more pending io and no new io possible to inode via open/mmapped file */
  222. void fuse_file_io_release(struct fuse_file *ff, struct inode *inode)
  223. {
  224. struct fuse_inode *fi = get_fuse_inode(inode);
  225. /*
  226. * Last passthrough file close allows caching inode io mode.
  227. * Last caching file close exits caching inode io mode.
  228. */
  229. switch (ff->iomode) {
  230. case IOM_NONE:
  231. /* Nothing to do */
  232. break;
  233. case IOM_UNCACHED:
  234. fuse_file_uncached_io_release(ff, fi);
  235. break;
  236. case IOM_CACHED:
  237. fuse_file_cached_io_release(ff, fi);
  238. break;
  239. }
  240. }