| 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 | /* | 
|---|
| 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. | 
|---|
| 32 | */ | 
|---|
| 33 | static void v9fs_begin_writeback(struct netfs_io_request *wreq) | 
|---|
| 34 | { | 
|---|
| 35 | struct p9_fid *fid; | 
|---|
| 36 |  | 
|---|
| 37 | fid = v9fs_fid_find_inode(inode: wreq->inode, want_writeable: true, INVALID_UID, any: true); | 
|---|
| 38 | if (!fid) { | 
|---|
| 39 | WARN_ONCE(1, "folio expected an open fid inode->i_ino=%lx\n", | 
|---|
| 40 | wreq->inode->i_ino); | 
|---|
| 41 | return; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | wreq->wsize = fid->clnt->msize - P9_IOHDRSZ; | 
|---|
| 45 | if (fid->iounit) | 
|---|
| 46 | wreq->wsize = min(wreq->wsize, fid->iounit); | 
|---|
| 47 | wreq->netfs_priv = fid; | 
|---|
| 48 | wreq->io_streams[0].avail = true; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | /* | 
|---|
| 52 | * Issue a subrequest to write to the server. | 
|---|
| 53 | */ | 
|---|
| 54 | static void v9fs_issue_write(struct netfs_io_subrequest *subreq) | 
|---|
| 55 | { | 
|---|
| 56 | struct p9_fid *fid = subreq->rreq->netfs_priv; | 
|---|
| 57 | int err, len; | 
|---|
| 58 |  | 
|---|
| 59 | len = p9_client_write(fid, offset: subreq->start, from: &subreq->io_iter, err: &err); | 
|---|
| 60 | if (len > 0) | 
|---|
| 61 | __set_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags); | 
|---|
| 62 | netfs_write_subrequest_terminated(op: subreq, transferred_or_error: len ?: err); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 | * v9fs_issue_read - Issue a read from 9P | 
|---|
| 67 | * @subreq: The read to make | 
|---|
| 68 | */ | 
|---|
| 69 | static void v9fs_issue_read(struct netfs_io_subrequest *subreq) | 
|---|
| 70 | { | 
|---|
| 71 | struct netfs_io_request *rreq = subreq->rreq; | 
|---|
| 72 | struct p9_fid *fid = rreq->netfs_priv; | 
|---|
| 73 | unsigned long long pos = subreq->start + subreq->transferred; | 
|---|
| 74 | int total, err; | 
|---|
| 75 |  | 
|---|
| 76 | total = p9_client_read(fid, offset: pos, to: &subreq->io_iter, err: &err); | 
|---|
| 77 |  | 
|---|
| 78 | /* if we just extended the file size, any portion not in | 
|---|
| 79 | * cache won't be on server and is zeroes */ | 
|---|
| 80 | if (subreq->rreq->origin != NETFS_UNBUFFERED_READ && | 
|---|
| 81 | subreq->rreq->origin != NETFS_DIO_READ) | 
|---|
| 82 | __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); | 
|---|
| 83 | if (pos + total >= i_size_read(inode: rreq->inode)) | 
|---|
| 84 | __set_bit(NETFS_SREQ_HIT_EOF, &subreq->flags); | 
|---|
| 85 | if (!err && total) { | 
|---|
| 86 | subreq->transferred += total; | 
|---|
| 87 | __set_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | subreq->error = err; | 
|---|
| 91 | netfs_read_subreq_terminated(subreq); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 | * v9fs_init_request - Initialise a request | 
|---|
| 96 | * @rreq: The read request | 
|---|
| 97 | * @file: The file being read from | 
|---|
| 98 | */ | 
|---|
| 99 | static int v9fs_init_request(struct netfs_io_request *rreq, struct file *file) | 
|---|
| 100 | { | 
|---|
| 101 | struct p9_fid *fid; | 
|---|
| 102 | bool writing = (rreq->origin == NETFS_READ_FOR_WRITE || | 
|---|
| 103 | rreq->origin == NETFS_WRITETHROUGH || | 
|---|
| 104 | rreq->origin == NETFS_UNBUFFERED_WRITE || | 
|---|
| 105 | rreq->origin == NETFS_DIO_WRITE); | 
|---|
| 106 |  | 
|---|
| 107 | if (rreq->origin == NETFS_WRITEBACK) | 
|---|
| 108 | return 0; /* We don't get the write handle until we find we | 
|---|
| 109 | * have actually dirty data and not just | 
|---|
| 110 | * copy-to-cache data. | 
|---|
| 111 | */ | 
|---|
| 112 |  | 
|---|
| 113 | if (file) { | 
|---|
| 114 | fid = file->private_data; | 
|---|
| 115 | if (!fid) | 
|---|
| 116 | goto no_fid; | 
|---|
| 117 | p9_fid_get(fid); | 
|---|
| 118 | } else { | 
|---|
| 119 | fid = v9fs_fid_find_inode(inode: rreq->inode, want_writeable: writing, INVALID_UID, any: true); | 
|---|
| 120 | if (!fid) | 
|---|
| 121 | goto no_fid; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | rreq->wsize = fid->clnt->msize - P9_IOHDRSZ; | 
|---|
| 125 | if (fid->iounit) | 
|---|
| 126 | rreq->wsize = min(rreq->wsize, fid->iounit); | 
|---|
| 127 |  | 
|---|
| 128 | /* we might need to read from a fid that was opened write-only | 
|---|
| 129 | * for read-modify-write of page cache, use the writeback fid | 
|---|
| 130 | * for that */ | 
|---|
| 131 | WARN_ON(rreq->origin == NETFS_READ_FOR_WRITE && !(fid->mode & P9_ORDWR)); | 
|---|
| 132 | rreq->netfs_priv = fid; | 
|---|
| 133 | return 0; | 
|---|
| 134 |  | 
|---|
| 135 | no_fid: | 
|---|
| 136 | WARN_ONCE(1, "folio expected an open fid inode->i_ino=%lx\n", | 
|---|
| 137 | rreq->inode->i_ino); | 
|---|
| 138 | return -EINVAL; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 | * v9fs_free_request - Cleanup request initialized by v9fs_init_rreq | 
|---|
| 143 | * @rreq: The I/O request to clean up | 
|---|
| 144 | */ | 
|---|
| 145 | static void v9fs_free_request(struct netfs_io_request *rreq) | 
|---|
| 146 | { | 
|---|
| 147 | struct p9_fid *fid = rreq->netfs_priv; | 
|---|
| 148 |  | 
|---|
| 149 | p9_fid_put(fid); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | const struct netfs_request_ops v9fs_req_ops = { | 
|---|
| 153 | .init_request		= v9fs_init_request, | 
|---|
| 154 | .free_request		= v9fs_free_request, | 
|---|
| 155 | .issue_read		= v9fs_issue_read, | 
|---|
| 156 | .begin_writeback	= v9fs_begin_writeback, | 
|---|
| 157 | .issue_write		= v9fs_issue_write, | 
|---|
| 158 | }; | 
|---|
| 159 |  | 
|---|
| 160 | const struct address_space_operations v9fs_addr_operations = { | 
|---|
| 161 | .read_folio		= netfs_read_folio, | 
|---|
| 162 | .readahead		= netfs_readahead, | 
|---|
| 163 | .dirty_folio		= netfs_dirty_folio, | 
|---|
| 164 | .release_folio		= netfs_release_folio, | 
|---|
| 165 | .invalidate_folio	= netfs_invalidate_folio, | 
|---|
| 166 | .direct_IO		= noop_direct_IO, | 
|---|
| 167 | .writepages		= netfs_writepages, | 
|---|
| 168 | .migrate_folio		= filemap_migrate_folio, | 
|---|
| 169 | }; | 
|---|
| 170 |  | 
|---|