read_single.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Single, monolithic object support (e.g. AFS directory).
  3. *
  4. * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/export.h>
  8. #include <linux/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/slab.h>
  12. #include <linux/uio.h>
  13. #include <linux/sched/mm.h>
  14. #include <linux/task_io_accounting_ops.h>
  15. #include <linux/netfs.h>
  16. #include "internal.h"
  17. /**
  18. * netfs_single_mark_inode_dirty - Mark a single, monolithic object inode dirty
  19. * @inode: The inode to mark
  20. *
  21. * Mark an inode that contains a single, monolithic object as dirty so that its
  22. * writepages op will get called. If set, the SINGLE_NO_UPLOAD flag indicates
  23. * that the object will only be written to the cache and not uploaded (e.g. AFS
  24. * directory contents).
  25. */
  26. void netfs_single_mark_inode_dirty(struct inode *inode)
  27. {
  28. struct netfs_inode *ictx = netfs_inode(inode);
  29. bool cache_only = test_bit(NETFS_ICTX_SINGLE_NO_UPLOAD, &ictx->flags);
  30. bool caching = fscache_cookie_enabled(netfs_i_cookie(netfs_inode(inode)));
  31. if (cache_only && !caching)
  32. return;
  33. mark_inode_dirty(inode);
  34. if (caching && !(inode_state_read_once(inode) & I_PINNING_NETFS_WB)) {
  35. bool need_use = false;
  36. spin_lock(&inode->i_lock);
  37. if (!(inode_state_read(inode) & I_PINNING_NETFS_WB)) {
  38. inode_state_set(inode, I_PINNING_NETFS_WB);
  39. need_use = true;
  40. }
  41. spin_unlock(&inode->i_lock);
  42. if (need_use)
  43. fscache_use_cookie(netfs_i_cookie(ictx), true);
  44. }
  45. }
  46. EXPORT_SYMBOL(netfs_single_mark_inode_dirty);
  47. static int netfs_single_begin_cache_read(struct netfs_io_request *rreq, struct netfs_inode *ctx)
  48. {
  49. return fscache_begin_read_operation(&rreq->cache_resources, netfs_i_cookie(ctx));
  50. }
  51. static void netfs_single_cache_prepare_read(struct netfs_io_request *rreq,
  52. struct netfs_io_subrequest *subreq)
  53. {
  54. struct netfs_cache_resources *cres = &rreq->cache_resources;
  55. if (!cres->ops) {
  56. subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
  57. return;
  58. }
  59. subreq->source = cres->ops->prepare_read(subreq, rreq->i_size);
  60. trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
  61. }
  62. static void netfs_single_read_cache(struct netfs_io_request *rreq,
  63. struct netfs_io_subrequest *subreq)
  64. {
  65. struct netfs_cache_resources *cres = &rreq->cache_resources;
  66. _enter("R=%08x[%x]", rreq->debug_id, subreq->debug_index);
  67. netfs_stat(&netfs_n_rh_read);
  68. cres->ops->read(cres, subreq->start, &subreq->io_iter, NETFS_READ_HOLE_FAIL,
  69. netfs_cache_read_terminated, subreq);
  70. }
  71. /*
  72. * Perform a read to a buffer from the cache or the server. Only a single
  73. * subreq is permitted as the object must be fetched in a single transaction.
  74. */
  75. static int netfs_single_dispatch_read(struct netfs_io_request *rreq)
  76. {
  77. struct netfs_io_stream *stream = &rreq->io_streams[0];
  78. struct netfs_io_subrequest *subreq;
  79. int ret = 0;
  80. subreq = netfs_alloc_subrequest(rreq);
  81. if (!subreq)
  82. return -ENOMEM;
  83. subreq->source = NETFS_SOURCE_UNKNOWN;
  84. subreq->start = 0;
  85. subreq->len = rreq->len;
  86. subreq->io_iter = rreq->buffer.iter;
  87. __set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
  88. spin_lock(&rreq->lock);
  89. list_add_tail(&subreq->rreq_link, &stream->subrequests);
  90. trace_netfs_sreq(subreq, netfs_sreq_trace_added);
  91. /* Store list pointers before active flag */
  92. smp_store_release(&stream->active, true);
  93. spin_unlock(&rreq->lock);
  94. netfs_single_cache_prepare_read(rreq, subreq);
  95. switch (subreq->source) {
  96. case NETFS_DOWNLOAD_FROM_SERVER:
  97. netfs_stat(&netfs_n_rh_download);
  98. if (rreq->netfs_ops->prepare_read) {
  99. ret = rreq->netfs_ops->prepare_read(subreq);
  100. if (ret < 0)
  101. goto cancel;
  102. }
  103. rreq->netfs_ops->issue_read(subreq);
  104. rreq->submitted += subreq->len;
  105. break;
  106. case NETFS_READ_FROM_CACHE:
  107. trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
  108. netfs_single_read_cache(rreq, subreq);
  109. rreq->submitted += subreq->len;
  110. ret = 0;
  111. break;
  112. default:
  113. pr_warn("Unexpected single-read source %u\n", subreq->source);
  114. WARN_ON_ONCE(true);
  115. ret = -EIO;
  116. break;
  117. }
  118. smp_wmb(); /* Write lists before ALL_QUEUED. */
  119. set_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags);
  120. return ret;
  121. cancel:
  122. netfs_put_subrequest(subreq, netfs_sreq_trace_put_cancel);
  123. return ret;
  124. }
  125. /**
  126. * netfs_read_single - Synchronously read a single blob of pages.
  127. * @inode: The inode to read from.
  128. * @file: The file we're using to read or NULL.
  129. * @iter: The buffer we're reading into.
  130. *
  131. * Fulfil a read request for a single monolithic object by drawing data from
  132. * the cache if possible, or the netfs if not. The buffer may be larger than
  133. * the file content; unused beyond the EOF will be zero-filled. The content
  134. * will be read with a single I/O request (though this may be retried).
  135. *
  136. * The calling netfs must initialise a netfs context contiguous to the vfs
  137. * inode before calling this.
  138. *
  139. * This is usable whether or not caching is enabled. If caching is enabled,
  140. * the data will be stored as a single object into the cache.
  141. */
  142. ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iter *iter)
  143. {
  144. struct netfs_io_request *rreq;
  145. struct netfs_inode *ictx = netfs_inode(inode);
  146. ssize_t ret;
  147. rreq = netfs_alloc_request(inode->i_mapping, file, 0, iov_iter_count(iter),
  148. NETFS_READ_SINGLE);
  149. if (IS_ERR(rreq))
  150. return PTR_ERR(rreq);
  151. ret = netfs_single_begin_cache_read(rreq, ictx);
  152. if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
  153. goto cleanup_free;
  154. netfs_stat(&netfs_n_rh_read_single);
  155. trace_netfs_read(rreq, 0, rreq->len, netfs_read_trace_read_single);
  156. rreq->buffer.iter = *iter;
  157. netfs_single_dispatch_read(rreq);
  158. ret = netfs_wait_for_read(rreq);
  159. netfs_put_request(rreq, netfs_rreq_trace_put_return);
  160. return ret;
  161. cleanup_free:
  162. netfs_put_failed_request(rreq);
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(netfs_read_single);