xref: /openbmc/linux/fs/9p/vfs_addr.c (revision 48ca54e3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file contians vfs address (mmap) ops for 9P2000.
4  *
5  *  Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
6  *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/file.h>
13 #include <linux/stat.h>
14 #include <linux/string.h>
15 #include <linux/inet.h>
16 #include <linux/pagemap.h>
17 #include <linux/idr.h>
18 #include <linux/sched.h>
19 #include <linux/swap.h>
20 #include <linux/uio.h>
21 #include <linux/netfs.h>
22 #include <net/9p/9p.h>
23 #include <net/9p/client.h>
24 
25 #include "v9fs.h"
26 #include "v9fs_vfs.h"
27 #include "cache.h"
28 #include "fid.h"
29 
30 /**
31  * v9fs_issue_read - Issue a read from 9P
32  * @subreq: The read to make
33  */
34 static void v9fs_issue_read(struct netfs_io_subrequest *subreq)
35 {
36 	struct netfs_io_request *rreq = subreq->rreq;
37 	struct p9_fid *fid = rreq->netfs_priv;
38 	struct iov_iter to;
39 	loff_t pos = subreq->start + subreq->transferred;
40 	size_t len = subreq->len   - subreq->transferred;
41 	int total, err;
42 
43 	iov_iter_xarray(&to, READ, &rreq->mapping->i_pages, pos, len);
44 
45 	total = p9_client_read(fid, pos, &to, &err);
46 
47 	/* if we just extended the file size, any portion not in
48 	 * cache won't be on server and is zeroes */
49 	__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
50 
51 	netfs_subreq_terminated(subreq, err ?: total, false);
52 }
53 
54 /**
55  * v9fs_init_request - Initialise a read request
56  * @rreq: The read request
57  * @file: The file being read from
58  */
59 static int v9fs_init_request(struct netfs_io_request *rreq, struct file *file)
60 {
61 	struct p9_fid *fid = file->private_data;
62 
63 	refcount_inc(&fid->count);
64 	rreq->netfs_priv = fid;
65 	return 0;
66 }
67 
68 /**
69  * v9fs_free_request - Cleanup request initialized by v9fs_init_rreq
70  * @rreq: The I/O request to clean up
71  */
72 static void v9fs_free_request(struct netfs_io_request *rreq)
73 {
74 	struct p9_fid *fid = rreq->netfs_priv;
75 
76 	p9_client_clunk(fid);
77 }
78 
79 /**
80  * v9fs_begin_cache_operation - Begin a cache operation for a read
81  * @rreq: The read request
82  */
83 static int v9fs_begin_cache_operation(struct netfs_io_request *rreq)
84 {
85 #ifdef CONFIG_9P_FSCACHE
86 	struct fscache_cookie *cookie = v9fs_inode_cookie(V9FS_I(rreq->inode));
87 
88 	return fscache_begin_read_operation(&rreq->cache_resources, cookie);
89 #else
90 	return -ENOBUFS;
91 #endif
92 }
93 
94 const struct netfs_request_ops v9fs_req_ops = {
95 	.init_request		= v9fs_init_request,
96 	.free_request		= v9fs_free_request,
97 	.begin_cache_operation	= v9fs_begin_cache_operation,
98 	.issue_read		= v9fs_issue_read,
99 };
100 
101 /**
102  * v9fs_release_folio - release the private state associated with a folio
103  * @folio: The folio to be released
104  * @gfp: The caller's allocation restrictions
105  *
106  * Returns true if the page can be released, false otherwise.
107  */
108 
109 static bool v9fs_release_folio(struct folio *folio, gfp_t gfp)
110 {
111 	struct inode *inode = folio_inode(folio);
112 
113 	if (folio_test_private(folio))
114 		return false;
115 #ifdef CONFIG_9P_FSCACHE
116 	if (folio_test_fscache(folio)) {
117 		if (current_is_kswapd() || !(gfp & __GFP_FS))
118 			return false;
119 		folio_wait_fscache(folio);
120 	}
121 #endif
122 	fscache_note_page_release(v9fs_inode_cookie(V9FS_I(inode)));
123 	return true;
124 }
125 
126 static void v9fs_invalidate_folio(struct folio *folio, size_t offset,
127 				 size_t length)
128 {
129 	folio_wait_fscache(folio);
130 }
131 
132 static void v9fs_write_to_cache_done(void *priv, ssize_t transferred_or_error,
133 				     bool was_async)
134 {
135 	struct v9fs_inode *v9inode = priv;
136 	__le32 version;
137 
138 	if (IS_ERR_VALUE(transferred_or_error) &&
139 	    transferred_or_error != -ENOBUFS) {
140 		version = cpu_to_le32(v9inode->qid.version);
141 		fscache_invalidate(v9fs_inode_cookie(v9inode), &version,
142 				   i_size_read(&v9inode->netfs.inode), 0);
143 	}
144 }
145 
146 static int v9fs_vfs_write_folio_locked(struct folio *folio)
147 {
148 	struct inode *inode = folio_inode(folio);
149 	struct v9fs_inode *v9inode = V9FS_I(inode);
150 	struct fscache_cookie *cookie = v9fs_inode_cookie(v9inode);
151 	loff_t start = folio_pos(folio);
152 	loff_t i_size = i_size_read(inode);
153 	struct iov_iter from;
154 	size_t len = folio_size(folio);
155 	int err;
156 
157 	if (start >= i_size)
158 		return 0; /* Simultaneous truncation occurred */
159 
160 	len = min_t(loff_t, i_size - start, len);
161 
162 	iov_iter_xarray(&from, WRITE, &folio_mapping(folio)->i_pages, start, len);
163 
164 	/* We should have writeback_fid always set */
165 	BUG_ON(!v9inode->writeback_fid);
166 
167 	folio_wait_fscache(folio);
168 	folio_start_writeback(folio);
169 
170 	p9_client_write(v9inode->writeback_fid, start, &from, &err);
171 
172 	if (err == 0 &&
173 	    fscache_cookie_enabled(cookie) &&
174 	    test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags)) {
175 		folio_start_fscache(folio);
176 		fscache_write_to_cache(v9fs_inode_cookie(v9inode),
177 				       folio_mapping(folio), start, len, i_size,
178 				       v9fs_write_to_cache_done, v9inode,
179 				       true);
180 	}
181 
182 	folio_end_writeback(folio);
183 	return err;
184 }
185 
186 static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
187 {
188 	struct folio *folio = page_folio(page);
189 	int retval;
190 
191 	p9_debug(P9_DEBUG_VFS, "folio %p\n", folio);
192 
193 	retval = v9fs_vfs_write_folio_locked(folio);
194 	if (retval < 0) {
195 		if (retval == -EAGAIN) {
196 			folio_redirty_for_writepage(wbc, folio);
197 			retval = 0;
198 		} else {
199 			mapping_set_error(folio_mapping(folio), retval);
200 		}
201 	} else
202 		retval = 0;
203 
204 	folio_unlock(folio);
205 	return retval;
206 }
207 
208 static int v9fs_launder_folio(struct folio *folio)
209 {
210 	int retval;
211 
212 	if (folio_clear_dirty_for_io(folio)) {
213 		retval = v9fs_vfs_write_folio_locked(folio);
214 		if (retval)
215 			return retval;
216 	}
217 	folio_wait_fscache(folio);
218 	return 0;
219 }
220 
221 /**
222  * v9fs_direct_IO - 9P address space operation for direct I/O
223  * @iocb: target I/O control block
224  * @iter: The data/buffer to use
225  *
226  * The presence of v9fs_direct_IO() in the address space ops vector
227  * allowes open() O_DIRECT flags which would have failed otherwise.
228  *
229  * In the non-cached mode, we shunt off direct read and write requests before
230  * the VFS gets them, so this method should never be called.
231  *
232  * Direct IO is not 'yet' supported in the cached mode. Hence when
233  * this routine is called through generic_file_aio_read(), the read/write fails
234  * with an error.
235  *
236  */
237 static ssize_t
238 v9fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
239 {
240 	struct file *file = iocb->ki_filp;
241 	loff_t pos = iocb->ki_pos;
242 	ssize_t n;
243 	int err = 0;
244 
245 	if (iov_iter_rw(iter) == WRITE) {
246 		n = p9_client_write(file->private_data, pos, iter, &err);
247 		if (n) {
248 			struct inode *inode = file_inode(file);
249 			loff_t i_size = i_size_read(inode);
250 
251 			if (pos + n > i_size)
252 				inode_add_bytes(inode, pos + n - i_size);
253 		}
254 	} else {
255 		n = p9_client_read(file->private_data, pos, iter, &err);
256 	}
257 	return n ? n : err;
258 }
259 
260 static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
261 			    loff_t pos, unsigned int len,
262 			    struct page **subpagep, void **fsdata)
263 {
264 	int retval;
265 	struct folio *folio;
266 	struct v9fs_inode *v9inode = V9FS_I(mapping->host);
267 
268 	p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
269 
270 	BUG_ON(!v9inode->writeback_fid);
271 
272 	/* Prefetch area to be written into the cache if we're caching this
273 	 * file.  We need to do this before we get a lock on the page in case
274 	 * there's more than one writer competing for the same cache block.
275 	 */
276 	retval = netfs_write_begin(&v9inode->netfs, filp, mapping, pos, len, &folio, fsdata);
277 	if (retval < 0)
278 		return retval;
279 
280 	*subpagep = &folio->page;
281 	return retval;
282 }
283 
284 static int v9fs_write_end(struct file *filp, struct address_space *mapping,
285 			  loff_t pos, unsigned int len, unsigned int copied,
286 			  struct page *subpage, void *fsdata)
287 {
288 	loff_t last_pos = pos + copied;
289 	struct folio *folio = page_folio(subpage);
290 	struct inode *inode = mapping->host;
291 	struct v9fs_inode *v9inode = V9FS_I(inode);
292 
293 	p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
294 
295 	if (!folio_test_uptodate(folio)) {
296 		if (unlikely(copied < len)) {
297 			copied = 0;
298 			goto out;
299 		}
300 
301 		folio_mark_uptodate(folio);
302 	}
303 
304 	/*
305 	 * No need to use i_size_read() here, the i_size
306 	 * cannot change under us because we hold the i_mutex.
307 	 */
308 	if (last_pos > inode->i_size) {
309 		inode_add_bytes(inode, last_pos - inode->i_size);
310 		i_size_write(inode, last_pos);
311 		fscache_update_cookie(v9fs_inode_cookie(v9inode), NULL, &last_pos);
312 	}
313 	folio_mark_dirty(folio);
314 out:
315 	folio_unlock(folio);
316 	folio_put(folio);
317 
318 	return copied;
319 }
320 
321 #ifdef CONFIG_9P_FSCACHE
322 /*
323  * Mark a page as having been made dirty and thus needing writeback.  We also
324  * need to pin the cache object to write back to.
325  */
326 static bool v9fs_dirty_folio(struct address_space *mapping, struct folio *folio)
327 {
328 	struct v9fs_inode *v9inode = V9FS_I(mapping->host);
329 
330 	return fscache_dirty_folio(mapping, folio, v9fs_inode_cookie(v9inode));
331 }
332 #else
333 #define v9fs_dirty_folio filemap_dirty_folio
334 #endif
335 
336 const struct address_space_operations v9fs_addr_operations = {
337 	.read_folio = netfs_read_folio,
338 	.readahead = netfs_readahead,
339 	.dirty_folio = v9fs_dirty_folio,
340 	.writepage = v9fs_vfs_writepage,
341 	.write_begin = v9fs_write_begin,
342 	.write_end = v9fs_write_end,
343 	.release_folio = v9fs_release_folio,
344 	.invalidate_folio = v9fs_invalidate_folio,
345 	.launder_folio = v9fs_launder_folio,
346 	.direct_IO = v9fs_direct_IO,
347 };
348