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
8 #include <linux/export.h>
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/uio.h>
14 #include <linux/sched/mm.h>
15 #include <linux/task_io_accounting_ops.h>
16 #include <linux/netfs.h>
17 #include "internal.h"
18
19 /**
20 * netfs_single_mark_inode_dirty - Mark a single, monolithic object inode dirty
21 * @inode: The inode to mark
22 *
23 * Mark an inode that contains a single, monolithic object as dirty so that its
24 * writepages op will get called. If set, the SINGLE_NO_UPLOAD flag indicates
25 * that the object will only be written to the cache and not uploaded (e.g. AFS
26 * directory contents).
27 */
netfs_single_mark_inode_dirty(struct inode * inode)28 void netfs_single_mark_inode_dirty(struct inode *inode)
29 {
30 struct netfs_inode *ictx = netfs_inode(inode);
31 bool cache_only = test_bit(NETFS_ICTX_SINGLE_NO_UPLOAD, &ictx->flags);
32 bool caching = fscache_cookie_enabled(netfs_i_cookie(netfs_inode(inode)));
33
34 if (cache_only && !caching)
35 return;
36
37 mark_inode_dirty(inode);
38
39 if (caching && !(inode_state_read_once(inode) & I_PINNING_NETFS_WB)) {
40 bool need_use = false;
41
42 spin_lock(&inode->i_lock);
43 if (!(inode_state_read(inode) & I_PINNING_NETFS_WB)) {
44 inode_state_set(inode, I_PINNING_NETFS_WB);
45 need_use = true;
46 }
47 spin_unlock(&inode->i_lock);
48
49 if (need_use)
50 fscache_use_cookie(netfs_i_cookie(ictx), true);
51 }
52
53 }
54 EXPORT_SYMBOL(netfs_single_mark_inode_dirty);
55
netfs_single_begin_cache_read(struct netfs_io_request * rreq,struct netfs_inode * ctx)56 static int netfs_single_begin_cache_read(struct netfs_io_request *rreq, struct netfs_inode *ctx)
57 {
58 return fscache_begin_read_operation(&rreq->cache_resources, netfs_i_cookie(ctx));
59 }
60
netfs_single_read_cache(struct netfs_io_request * rreq,struct netfs_io_subrequest * subreq)61 static void netfs_single_read_cache(struct netfs_io_request *rreq,
62 struct netfs_io_subrequest *subreq)
63 {
64 struct netfs_cache_resources *cres = &rreq->cache_resources;
65
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 /*
73 * Perform a read to a buffer from the cache or the server. Only a single
74 * subreq is permitted as the object must be fetched in a single transaction.
75 */
netfs_single_dispatch_read(struct netfs_io_request * rreq)76 static int netfs_single_dispatch_read(struct netfs_io_request *rreq)
77 {
78 struct fscache_occupancy occ = {
79 .query_from = 0,
80 .query_to = rreq->len,
81 .cached_from[0] = ULLONG_MAX,
82 .cached_to[0] = ULLONG_MAX,
83 .cached_from[1] = ULLONG_MAX,
84 .cached_to[1] = ULLONG_MAX,
85 };
86 struct netfs_io_subrequest *subreq;
87 enum netfs_io_source source = NETFS_DOWNLOAD_FROM_SERVER;
88 int ret = 0;
89
90 /* Try to use the cache if the cache content matches the size of the
91 * remote file.
92 */
93 netfs_read_query_cache(rreq, &occ);
94 if (occ.cached_from[0] == 0 &&
95 occ.cached_to[0] >= rreq->len)
96 source = NETFS_READ_FROM_CACHE;
97
98 subreq = netfs_alloc_subrequest(rreq, source);
99 if (!subreq)
100 return -ENOMEM;
101
102 subreq->start = 0;
103 subreq->len = rreq->len;
104 subreq->io_iter = rreq->buffer.iter;
105
106 netfs_queue_read(rreq, subreq);
107
108 switch (subreq->source) {
109 case NETFS_DOWNLOAD_FROM_SERVER:
110 netfs_stat(&netfs_n_rh_download);
111 if (rreq->netfs_ops->prepare_read) {
112 ret = rreq->netfs_ops->prepare_read(subreq);
113 if (ret < 0)
114 goto cancel;
115 }
116
117 netfs_all_subreqs_queued(rreq);
118 rreq->netfs_ops->issue_read(subreq);
119 rreq->submitted += subreq->len;
120 break;
121 case NETFS_READ_FROM_CACHE:
122 if (rreq->cache_resources.ops->prepare_read) {
123 ret = rreq->cache_resources.ops->prepare_read(subreq);
124 if (ret < 0)
125 goto cancel;
126 }
127
128 netfs_all_subreqs_queued(rreq);
129 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
130 netfs_single_read_cache(rreq, subreq);
131 rreq->submitted += subreq->len;
132 ret = 0;
133 break;
134 default:
135 pr_warn("Unexpected single-read source %u\n", subreq->source);
136 WARN_ON_ONCE(true);
137 ret = -EIO;
138 goto cancel;
139 }
140
141 return ret;
142 cancel:
143 netfs_cancel_read(subreq, ret);
144 netfs_all_subreqs_queued(rreq);
145 netfs_wake_collector(rreq);
146 return ret;
147 }
148
149 /**
150 * netfs_read_single - Synchronously read a single blob of pages.
151 * @inode: The inode to read from.
152 * @file: The file we're using to read or NULL.
153 * @iter: The buffer we're reading into.
154 *
155 * Fulfil a read request for a single monolithic object by drawing data from
156 * the cache if possible, or the netfs if not. The buffer may be larger than
157 * the file content; unused beyond the EOF will be zero-filled. The content
158 * will be read with a single I/O request (though this may be retried).
159 *
160 * The calling netfs must initialise a netfs context contiguous to the vfs
161 * inode before calling this.
162 *
163 * This is usable whether or not caching is enabled. If caching is enabled,
164 * the data will be stored as a single object into the cache.
165 */
netfs_read_single(struct inode * inode,struct file * file,struct iov_iter * iter)166 ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iter *iter)
167 {
168 struct netfs_io_request *rreq;
169 struct netfs_inode *ictx = netfs_inode(inode);
170 ssize_t ret;
171
172 rreq = netfs_alloc_request(inode->i_mapping, file, 0, iov_iter_count(iter),
173 NETFS_READ_SINGLE);
174 if (IS_ERR(rreq))
175 return PTR_ERR(rreq);
176
177 rreq->progress_at = rreq->len;
178
179 ret = netfs_single_begin_cache_read(rreq, ictx);
180 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
181 goto cleanup_free;
182
183 netfs_stat(&netfs_n_rh_read_single);
184 trace_netfs_read(rreq, 0, rreq->len, netfs_read_trace_read_single);
185
186 rreq->buffer.iter = *iter;
187 netfs_single_dispatch_read(rreq);
188
189 ret = netfs_wait_for_read(rreq);
190 netfs_put_request(rreq, netfs_rreq_trace_put_return);
191 return ret;
192
193 cleanup_free:
194 netfs_put_failed_request(rreq);
195 return ret;
196 }
197 EXPORT_SYMBOL(netfs_read_single);
198