1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contians vfs address (mmap) ops for 9P2000.
5 * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 #include <linux/module.h>
10 #include <linux/errno.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>
30 * Writeback calls this when it finds a folio that needs uploading. This isn't
31 * called if writeback only has copy-to-cache to deal with.
33 static void v9fs_begin_writeback(struct netfs_io_request
*wreq
)
37 fid
= v9fs_fid_find_inode(wreq
->inode
, true, INVALID_UID
, true);
39 WARN_ONCE(1, "folio expected an open fid inode->i_ino=%lx\n",
44 wreq
->wsize
= fid
->clnt
->msize
- P9_IOHDRSZ
;
46 wreq
->wsize
= min(wreq
->wsize
, fid
->iounit
);
47 wreq
->netfs_priv
= fid
;
48 wreq
->io_streams
[0].avail
= true;
52 * Issue a subrequest to write to the server.
54 static void v9fs_issue_write(struct netfs_io_subrequest
*subreq
)
56 struct p9_fid
*fid
= subreq
->rreq
->netfs_priv
;
59 len
= p9_client_write(fid
, subreq
->start
, &subreq
->io_iter
, &err
);
60 netfs_write_subrequest_terminated(subreq
, len
?: err
, false);
64 * v9fs_issue_read - Issue a read from 9P
65 * @subreq: The read to make
67 static void v9fs_issue_read(struct netfs_io_subrequest
*subreq
)
69 struct netfs_io_request
*rreq
= subreq
->rreq
;
70 struct p9_fid
*fid
= rreq
->netfs_priv
;
71 unsigned long long pos
= subreq
->start
+ subreq
->transferred
;
74 total
= p9_client_read(fid
, pos
, &subreq
->io_iter
, &err
);
76 /* if we just extended the file size, any portion not in
77 * cache won't be on server and is zeroes */
78 if (subreq
->rreq
->origin
!= NETFS_DIO_READ
)
79 __set_bit(NETFS_SREQ_CLEAR_TAIL
, &subreq
->flags
);
80 if (pos
+ total
>= i_size_read(rreq
->inode
))
81 __set_bit(NETFS_SREQ_HIT_EOF
, &subreq
->flags
);
84 subreq
->transferred
+= total
;
86 netfs_read_subreq_terminated(subreq
, err
, false);
90 * v9fs_init_request - Initialise a request
91 * @rreq: The read request
92 * @file: The file being read from
94 static int v9fs_init_request(struct netfs_io_request
*rreq
, struct file
*file
)
97 bool writing
= (rreq
->origin
== NETFS_READ_FOR_WRITE
||
98 rreq
->origin
== NETFS_WRITETHROUGH
||
99 rreq
->origin
== NETFS_UNBUFFERED_WRITE
||
100 rreq
->origin
== NETFS_DIO_WRITE
);
102 if (rreq
->origin
== NETFS_WRITEBACK
)
103 return 0; /* We don't get the write handle until we find we
104 * have actually dirty data and not just
105 * copy-to-cache data.
109 fid
= file
->private_data
;
114 fid
= v9fs_fid_find_inode(rreq
->inode
, writing
, INVALID_UID
, true);
119 rreq
->wsize
= fid
->clnt
->msize
- P9_IOHDRSZ
;
121 rreq
->wsize
= min(rreq
->wsize
, fid
->iounit
);
123 /* we might need to read from a fid that was opened write-only
124 * for read-modify-write of page cache, use the writeback fid
126 WARN_ON(rreq
->origin
== NETFS_READ_FOR_WRITE
&& !(fid
->mode
& P9_ORDWR
));
127 rreq
->netfs_priv
= fid
;
131 WARN_ONCE(1, "folio expected an open fid inode->i_ino=%lx\n",
137 * v9fs_free_request - Cleanup request initialized by v9fs_init_rreq
138 * @rreq: The I/O request to clean up
140 static void v9fs_free_request(struct netfs_io_request
*rreq
)
142 struct p9_fid
*fid
= rreq
->netfs_priv
;
147 const struct netfs_request_ops v9fs_req_ops
= {
148 .init_request
= v9fs_init_request
,
149 .free_request
= v9fs_free_request
,
150 .issue_read
= v9fs_issue_read
,
151 .begin_writeback
= v9fs_begin_writeback
,
152 .issue_write
= v9fs_issue_write
,
155 const struct address_space_operations v9fs_addr_operations
= {
156 .read_folio
= netfs_read_folio
,
157 .readahead
= netfs_readahead
,
158 .dirty_folio
= netfs_dirty_folio
,
159 .release_folio
= netfs_release_folio
,
160 .invalidate_folio
= netfs_invalidate_folio
,
161 .direct_IO
= noop_direct_IO
,
162 .writepages
= netfs_writepages
,