xref: /linux/fs/9p/vfs_addr.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
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/pagemap.h>
16 #include <linux/sched.h>
17 #include <linux/swap.h>
18 #include <linux/uio.h>
19 #include <linux/netfs.h>
20 #include <net/9p/9p.h>
21 #include <net/9p/client.h>
22 #include <trace/events/netfs.h>
23 
24 #include "v9fs.h"
25 #include "v9fs_vfs.h"
26 #include "cache.h"
27 #include "fid.h"
28 
29 static void v9fs_upload_to_server(struct netfs_io_subrequest *subreq)
30 {
31 	struct p9_fid *fid = subreq->rreq->netfs_priv;
32 	int err, len;
33 
34 	trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
35 	len = p9_client_write(fid, subreq->start, &subreq->io_iter, &err);
36 	netfs_write_subrequest_terminated(subreq, len ?: err, false);
37 }
38 
39 static void v9fs_upload_to_server_worker(struct work_struct *work)
40 {
41 	struct netfs_io_subrequest *subreq =
42 		container_of(work, struct netfs_io_subrequest, work);
43 
44 	v9fs_upload_to_server(subreq);
45 }
46 
47 /*
48  * Set up write requests for a writeback slice.  We need to add a write request
49  * for each write we want to make.
50  */
51 static void v9fs_create_write_requests(struct netfs_io_request *wreq, loff_t start, size_t len)
52 {
53 	struct netfs_io_subrequest *subreq;
54 
55 	subreq = netfs_create_write_request(wreq, NETFS_UPLOAD_TO_SERVER,
56 					    start, len, v9fs_upload_to_server_worker);
57 	if (subreq)
58 		netfs_queue_write_request(subreq);
59 }
60 
61 /**
62  * v9fs_issue_read - Issue a read from 9P
63  * @subreq: The read to make
64  */
65 static void v9fs_issue_read(struct netfs_io_subrequest *subreq)
66 {
67 	struct netfs_io_request *rreq = subreq->rreq;
68 	struct p9_fid *fid = rreq->netfs_priv;
69 	int total, err;
70 
71 	total = p9_client_read(fid, subreq->start + subreq->transferred,
72 			       &subreq->io_iter, &err);
73 
74 	/* if we just extended the file size, any portion not in
75 	 * cache won't be on server and is zeroes */
76 	__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
77 
78 	netfs_subreq_terminated(subreq, err ?: total, false);
79 }
80 
81 /**
82  * v9fs_init_request - Initialise a request
83  * @rreq: The read request
84  * @file: The file being read from
85  */
86 static int v9fs_init_request(struct netfs_io_request *rreq, struct file *file)
87 {
88 	struct p9_fid *fid;
89 	bool writing = (rreq->origin == NETFS_READ_FOR_WRITE ||
90 			rreq->origin == NETFS_WRITEBACK ||
91 			rreq->origin == NETFS_WRITETHROUGH ||
92 			rreq->origin == NETFS_LAUNDER_WRITE ||
93 			rreq->origin == NETFS_UNBUFFERED_WRITE ||
94 			rreq->origin == NETFS_DIO_WRITE);
95 
96 	if (file) {
97 		fid = file->private_data;
98 		if (!fid)
99 			goto no_fid;
100 		p9_fid_get(fid);
101 	} else {
102 		fid = v9fs_fid_find_inode(rreq->inode, writing, INVALID_UID, true);
103 		if (!fid)
104 			goto no_fid;
105 	}
106 
107 	/* we might need to read from a fid that was opened write-only
108 	 * for read-modify-write of page cache, use the writeback fid
109 	 * for that */
110 	WARN_ON(rreq->origin == NETFS_READ_FOR_WRITE && !(fid->mode & P9_ORDWR));
111 	rreq->netfs_priv = fid;
112 	return 0;
113 
114 no_fid:
115 	WARN_ONCE(1, "folio expected an open fid inode->i_ino=%lx\n",
116 		  rreq->inode->i_ino);
117 	return -EINVAL;
118 }
119 
120 /**
121  * v9fs_free_request - Cleanup request initialized by v9fs_init_rreq
122  * @rreq: The I/O request to clean up
123  */
124 static void v9fs_free_request(struct netfs_io_request *rreq)
125 {
126 	struct p9_fid *fid = rreq->netfs_priv;
127 
128 	p9_fid_put(fid);
129 }
130 
131 const struct netfs_request_ops v9fs_req_ops = {
132 	.init_request		= v9fs_init_request,
133 	.free_request		= v9fs_free_request,
134 	.issue_read		= v9fs_issue_read,
135 	.create_write_requests	= v9fs_create_write_requests,
136 };
137 
138 const struct address_space_operations v9fs_addr_operations = {
139 	.read_folio		= netfs_read_folio,
140 	.readahead		= netfs_readahead,
141 	.dirty_folio		= netfs_dirty_folio,
142 	.release_folio		= netfs_release_folio,
143 	.invalidate_folio	= netfs_invalidate_folio,
144 	.launder_folio		= netfs_launder_folio,
145 	.direct_IO		= noop_direct_IO,
146 	.writepages		= netfs_writepages,
147 };
148