blob: cce9ace651a2dbce718bf0d344cd6eae095fffd5 [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -08002/*
3 * linux/fs/9p/vfs_addr.c
4 *
5 * This file contians vfs address (mmap) ops for 9P2000.
6 *
7 * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -08009 */
10
11#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/fs.h>
14#include <linux/file.h>
15#include <linux/stat.h>
16#include <linux/string.h>
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080017#include <linux/inet.h>
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080018#include <linux/pagemap.h>
19#include <linux/idr.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080021#include <linux/uio.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060022#include <linux/bvec.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050023#include <net/9p/9p.h>
24#include <net/9p/client.h>
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080025
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080026#include "v9fs.h"
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080027#include "v9fs_vfs.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050028#include "cache.h"
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053029#include "fid.h"
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080030
31/**
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053032 * v9fs_fid_readpage - read an entire page in from 9P
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080033 *
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053034 * @fid: fid being read
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080035 * @page: structure to page
36 *
37 */
Christoph Hellwigf053cbd2019-07-11 20:55:26 -070038static int v9fs_fid_readpage(void *data, struct page *page)
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080039{
Christoph Hellwigf053cbd2019-07-11 20:55:26 -070040 struct p9_fid *fid = data;
Al Viroe1200fe62015-04-01 23:42:28 -040041 struct inode *inode = page->mapping->host;
42 struct bio_vec bvec = {.bv_page = page, .bv_len = PAGE_SIZE};
43 struct iov_iter to;
44 int retval, err;
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080045
Joe Perches5d385152011-11-28 10:40:46 -080046 p9_debug(P9_DEBUG_VFS, "\n");
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050047
48 BUG_ON(!PageLocked(page));
49
50 retval = v9fs_readpage_from_fscache(inode, page);
51 if (retval == 0)
52 return retval;
53
David Howellsaa563d72018-10-20 00:57:56 +010054 iov_iter_bvec(&to, READ, &bvec, 1, PAGE_SIZE);
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080055
Al Viroe1200fe62015-04-01 23:42:28 -040056 retval = p9_client_read(fid, page_offset(page), &to, &err);
57 if (err) {
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050058 v9fs_uncache_page(inode, page);
Al Viroe1200fe62015-04-01 23:42:28 -040059 retval = err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050060 goto done;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050061 }
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080062
Al Viroe1200fe62015-04-01 23:42:28 -040063 zero_user(page, retval, PAGE_SIZE - retval);
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080064 flush_dcache_page(page);
65 SetPageUptodate(page);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050066
67 v9fs_readpage_to_fscache(inode, page);
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080068 retval = 0;
69
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050070done:
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -080071 unlock_page(page);
72 return retval;
73}
74
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050075/**
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053076 * v9fs_vfs_readpage - read an entire page in from 9P
77 *
78 * @filp: file being read
79 * @page: structure to page
80 *
81 */
82
83static int v9fs_vfs_readpage(struct file *filp, struct page *page)
84{
85 return v9fs_fid_readpage(filp->private_data, page);
86}
87
88/**
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050089 * v9fs_vfs_readpages - read a set of pages from 9P
90 *
91 * @filp: file being read
92 * @mapping: the address space
93 * @pages: list of pages to read
94 * @nr_pages: count of pages to read
95 *
96 */
97
98static int v9fs_vfs_readpages(struct file *filp, struct address_space *mapping,
99 struct list_head *pages, unsigned nr_pages)
100{
101 int ret = 0;
102 struct inode *inode;
103
104 inode = mapping->host;
Joe Perches5d385152011-11-28 10:40:46 -0800105 p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, filp);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500106
107 ret = v9fs_readpages_from_fscache(inode, mapping, pages, &nr_pages);
108 if (ret == 0)
109 return ret;
110
Christoph Hellwigf053cbd2019-07-11 20:55:26 -0700111 ret = read_cache_pages(mapping, pages, v9fs_fid_readpage,
112 filp->private_data);
Joe Perches5d385152011-11-28 10:40:46 -0800113 p9_debug(P9_DEBUG_VFS, " = %d\n", ret);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500114 return ret;
115}
116
117/**
118 * v9fs_release_page - release the private state associated with a page
119 *
120 * Returns 1 if the page can be released, false otherwise.
121 */
122
123static int v9fs_release_page(struct page *page, gfp_t gfp)
124{
125 if (PagePrivate(page))
126 return 0;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500127 return v9fs_fscache_release_page(page, gfp);
128}
129
130/**
131 * v9fs_invalidate_page - Invalidate a page completely or partially
132 *
133 * @page: structure to page
134 * @offset: offset in the page
135 */
136
Lukas Czernerd47992f2013-05-21 23:17:23 -0400137static void v9fs_invalidate_page(struct page *page, unsigned int offset,
138 unsigned int length)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500139{
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530140 /*
141 * If called with zero offset, we should release
142 * the private state assocated with the page
143 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300144 if (offset == 0 && length == PAGE_SIZE)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500145 v9fs_fscache_invalidate_page(page);
146}
147
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530148static int v9fs_vfs_writepage_locked(struct page *page)
149{
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530150 struct inode *inode = page->mapping->host;
Al Viro371098c2015-04-01 21:54:42 -0400151 struct v9fs_inode *v9inode = V9FS_I(inode);
152 loff_t size = i_size_read(inode);
153 struct iov_iter from;
154 struct bio_vec bvec;
155 int err, len;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530156
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300157 if (page->index == size >> PAGE_SHIFT)
158 len = size & ~PAGE_MASK;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530159 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300160 len = PAGE_SIZE;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530161
Al Viro371098c2015-04-01 21:54:42 -0400162 bvec.bv_page = page;
163 bvec.bv_offset = 0;
164 bvec.bv_len = len;
David Howellsaa563d72018-10-20 00:57:56 +0100165 iov_iter_bvec(&from, WRITE, &bvec, 1, len);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530166
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530167 /* We should have writeback_fid always set */
168 BUG_ON(!v9inode->writeback_fid);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530169
Al Viro371098c2015-04-01 21:54:42 -0400170 set_page_writeback(page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530171
Al Viro371098c2015-04-01 21:54:42 -0400172 p9_client_write(v9inode->writeback_fid, page_offset(page), &from, &err);
173
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530174 end_page_writeback(page);
Al Viro371098c2015-04-01 21:54:42 -0400175 return err;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530176}
177
178static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
179{
180 int retval;
181
Dominique Martinetfb89b452014-01-10 13:44:09 +0100182 p9_debug(P9_DEBUG_VFS, "page %p\n", page);
183
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530184 retval = v9fs_vfs_writepage_locked(page);
185 if (retval < 0) {
186 if (retval == -EAGAIN) {
187 redirty_page_for_writepage(wbc, page);
188 retval = 0;
189 } else {
190 SetPageError(page);
191 mapping_set_error(page->mapping, retval);
192 }
193 } else
194 retval = 0;
195
196 unlock_page(page);
197 return retval;
198}
199
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500200/**
201 * v9fs_launder_page - Writeback a dirty page
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500202 * Returns 0 on success.
203 */
204
205static int v9fs_launder_page(struct page *page)
206{
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530207 int retval;
Aneesh Kumar K.V2efda792011-02-28 17:03:56 +0530208 struct inode *inode = page->mapping->host;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530209
Aneesh Kumar K.V2efda792011-02-28 17:03:56 +0530210 v9fs_fscache_wait_on_page_write(inode, page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530211 if (clear_page_dirty_for_io(page)) {
212 retval = v9fs_vfs_writepage_locked(page);
213 if (retval)
214 return retval;
215 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500216 return 0;
217}
218
jvrao3e24ad22010-08-24 15:43:28 +0000219/**
220 * v9fs_direct_IO - 9P address space operation for direct I/O
jvrao3e24ad22010-08-24 15:43:28 +0000221 * @iocb: target I/O control block
jvrao3e24ad22010-08-24 15:43:28 +0000222 *
223 * The presence of v9fs_direct_IO() in the address space ops vector
224 * allowes open() O_DIRECT flags which would have failed otherwise.
225 *
226 * In the non-cached mode, we shunt off direct read and write requests before
227 * the VFS gets them, so this method should never be called.
228 *
229 * Direct IO is not 'yet' supported in the cached mode. Hence when
230 * this routine is called through generic_file_aio_read(), the read/write fails
231 * with an error.
232 *
233 */
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530234static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700235v9fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
jvrao3e24ad22010-08-24 15:43:28 +0000236{
Al Viro9565a542015-04-01 22:32:23 -0400237 struct file *file = iocb->ki_filp;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700238 loff_t pos = iocb->ki_pos;
Al Viro42b1ab92015-04-01 23:49:24 -0400239 ssize_t n;
240 int err = 0;
Omar Sandoval6f673762015-03-16 04:33:52 -0700241 if (iov_iter_rw(iter) == WRITE) {
Al Viro42b1ab92015-04-01 23:49:24 -0400242 n = p9_client_write(file->private_data, pos, iter, &err);
243 if (n) {
Al Viro9565a542015-04-01 22:32:23 -0400244 struct inode *inode = file_inode(file);
245 loff_t i_size = i_size_read(inode);
Al Viro42b1ab92015-04-01 23:49:24 -0400246 if (pos + n > i_size)
247 inode_add_bytes(inode, pos + n - i_size);
Al Viro9565a542015-04-01 22:32:23 -0400248 }
Al Viro42b1ab92015-04-01 23:49:24 -0400249 } else {
250 n = p9_client_read(file->private_data, pos, iter, &err);
Al Viro9565a542015-04-01 22:32:23 -0400251 }
Al Viro42b1ab92015-04-01 23:49:24 -0400252 return n ? n : err;
jvrao3e24ad22010-08-24 15:43:28 +0000253}
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530254
255static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
256 loff_t pos, unsigned len, unsigned flags,
257 struct page **pagep, void **fsdata)
258{
259 int retval = 0;
260 struct page *page;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530261 struct v9fs_inode *v9inode;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300262 pgoff_t index = pos >> PAGE_SHIFT;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530263 struct inode *inode = mapping->host;
264
Dominique Martinetfb89b452014-01-10 13:44:09 +0100265
266 p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
267
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530268 v9inode = V9FS_I(inode);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530269start:
270 page = grab_cache_page_write_begin(mapping, index, flags);
271 if (!page) {
272 retval = -ENOMEM;
273 goto out;
274 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530275 BUG_ON(!v9inode->writeback_fid);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530276 if (PageUptodate(page))
277 goto out;
278
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300279 if (len == PAGE_SIZE)
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530280 goto out;
281
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530282 retval = v9fs_fid_readpage(v9inode->writeback_fid, page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300283 put_page(page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530284 if (!retval)
285 goto start;
286out:
287 *pagep = page;
288 return retval;
289}
290
291static int v9fs_write_end(struct file *filp, struct address_space *mapping,
292 loff_t pos, unsigned len, unsigned copied,
293 struct page *page, void *fsdata)
294{
295 loff_t last_pos = pos + copied;
296 struct inode *inode = page->mapping->host;
297
Dominique Martinetfb89b452014-01-10 13:44:09 +0100298 p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
299
Alexander Levin56ae4142017-04-10 18:46:51 +0000300 if (!PageUptodate(page)) {
301 if (unlikely(copied < len)) {
302 copied = 0;
303 goto out;
304 } else if (len == PAGE_SIZE) {
305 SetPageUptodate(page);
306 }
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530307 }
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530308 /*
309 * No need to use i_size_read() here, the i_size
310 * cannot change under us because we hold the i_mutex.
311 */
312 if (last_pos > inode->i_size) {
313 inode_add_bytes(inode, last_pos - inode->i_size);
314 i_size_write(inode, last_pos);
315 }
316 set_page_dirty(page);
Al Viro77469c32016-08-29 20:56:35 -0400317out:
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530318 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300319 put_page(page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530320
321 return copied;
322}
323
324
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700325const struct address_space_operations v9fs_addr_operations = {
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530326 .readpage = v9fs_vfs_readpage,
327 .readpages = v9fs_vfs_readpages,
328 .set_page_dirty = __set_page_dirty_nobuffers,
329 .writepage = v9fs_vfs_writepage,
330 .write_begin = v9fs_write_begin,
331 .write_end = v9fs_write_end,
332 .releasepage = v9fs_release_page,
333 .invalidatepage = v9fs_invalidate_page,
334 .launder_page = v9fs_launder_page,
335 .direct_IO = v9fs_direct_IO,
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -0800336};