• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
9  */
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>
17 #include <linux/inet.h>
18 #include <linux/pagemap.h>
19 #include <linux/idr.h>
20 #include <linux/sched.h>
21 #include <linux/uio.h>
22 #include <linux/bvec.h>
23 #include <net/9p/9p.h>
24 #include <net/9p/client.h>
25 
26 #include "v9fs.h"
27 #include "v9fs_vfs.h"
28 #include "cache.h"
29 #include "fid.h"
30 
31 /**
32  * v9fs_fid_readpage - read an entire page in from 9P
33  * @data: Opaque pointer to the fid being read
34  * @page: structure to page
35  *
36  */
v9fs_fid_readpage(void * data,struct page * page)37 static int v9fs_fid_readpage(void *data, struct page *page)
38 {
39 	struct p9_fid *fid = data;
40 	struct inode *inode = page->mapping->host;
41 	struct bio_vec bvec = {.bv_page = page, .bv_len = PAGE_SIZE};
42 	struct iov_iter to;
43 	int retval, err;
44 
45 	p9_debug(P9_DEBUG_VFS, "\n");
46 
47 	BUG_ON(!PageLocked(page));
48 
49 	retval = v9fs_readpage_from_fscache(inode, page);
50 	if (retval == 0)
51 		return retval;
52 
53 	iov_iter_bvec(&to, READ, &bvec, 1, PAGE_SIZE);
54 
55 	retval = p9_client_read(fid, page_offset(page), &to, &err);
56 	if (err) {
57 		v9fs_uncache_page(inode, page);
58 		retval = err;
59 		goto done;
60 	}
61 
62 	zero_user(page, retval, PAGE_SIZE - retval);
63 	flush_dcache_page(page);
64 	SetPageUptodate(page);
65 
66 	v9fs_readpage_to_fscache(inode, page);
67 	retval = 0;
68 
69 done:
70 	unlock_page(page);
71 	return retval;
72 }
73 
74 /**
75  * v9fs_vfs_readpage - read an entire page in from 9P
76  *
77  * @filp: file being read
78  * @page: structure to page
79  *
80  */
81 
v9fs_vfs_readpage(struct file * filp,struct page * page)82 static int v9fs_vfs_readpage(struct file *filp, struct page *page)
83 {
84 	return v9fs_fid_readpage(filp->private_data, page);
85 }
86 
87 /**
88  * v9fs_vfs_readpages - read a set of pages from 9P
89  *
90  * @filp: file being read
91  * @mapping: the address space
92  * @pages: list of pages to read
93  * @nr_pages: count of pages to read
94  *
95  */
96 
v9fs_vfs_readpages(struct file * filp,struct address_space * mapping,struct list_head * pages,unsigned nr_pages)97 static int v9fs_vfs_readpages(struct file *filp, struct address_space *mapping,
98 			     struct list_head *pages, unsigned nr_pages)
99 {
100 	int ret = 0;
101 	struct inode *inode;
102 
103 	inode = mapping->host;
104 	p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, filp);
105 
106 	ret = v9fs_readpages_from_fscache(inode, mapping, pages, &nr_pages);
107 	if (ret == 0)
108 		return ret;
109 
110 	ret = read_cache_pages(mapping, pages, v9fs_fid_readpage,
111 			filp->private_data);
112 	p9_debug(P9_DEBUG_VFS, "  = %d\n", ret);
113 	return ret;
114 }
115 
116 /**
117  * v9fs_release_page - release the private state associated with a page
118  * @page: The page to be released
119  * @gfp: The caller's allocation restrictions
120  *
121  * Returns 1 if the page can be released, false otherwise.
122  */
123 
v9fs_release_page(struct page * page,gfp_t gfp)124 static int v9fs_release_page(struct page *page, gfp_t gfp)
125 {
126 	if (PagePrivate(page))
127 		return 0;
128 	return v9fs_fscache_release_page(page, gfp);
129 }
130 
131 /**
132  * v9fs_invalidate_page - Invalidate a page completely or partially
133  * @page: The page to be invalidated
134  * @offset: offset of the invalidated region
135  * @length: length of the invalidated region
136  */
137 
v9fs_invalidate_page(struct page * page,unsigned int offset,unsigned int length)138 static void v9fs_invalidate_page(struct page *page, unsigned int offset,
139 				 unsigned int length)
140 {
141 	/*
142 	 * If called with zero offset, we should release
143 	 * the private state assocated with the page
144 	 */
145 	if (offset == 0 && length == PAGE_SIZE)
146 		v9fs_fscache_invalidate_page(page);
147 }
148 
v9fs_vfs_writepage_locked(struct page * page)149 static int v9fs_vfs_writepage_locked(struct page *page)
150 {
151 	struct inode *inode = page->mapping->host;
152 	struct v9fs_inode *v9inode = V9FS_I(inode);
153 	loff_t size = i_size_read(inode);
154 	struct iov_iter from;
155 	struct bio_vec bvec;
156 	int err, len;
157 
158 	if (page->index == size >> PAGE_SHIFT)
159 		len = size & ~PAGE_MASK;
160 	else
161 		len = PAGE_SIZE;
162 
163 	bvec.bv_page = page;
164 	bvec.bv_offset = 0;
165 	bvec.bv_len = len;
166 	iov_iter_bvec(&from, WRITE, &bvec, 1, len);
167 
168 	/* We should have writeback_fid always set */
169 	BUG_ON(!v9inode->writeback_fid);
170 
171 	set_page_writeback(page);
172 
173 	p9_client_write(v9inode->writeback_fid, page_offset(page), &from, &err);
174 
175 	end_page_writeback(page);
176 	return err;
177 }
178 
v9fs_vfs_writepage(struct page * page,struct writeback_control * wbc)179 static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
180 {
181 	int retval;
182 
183 	p9_debug(P9_DEBUG_VFS, "page %p\n", page);
184 
185 	retval = v9fs_vfs_writepage_locked(page);
186 	if (retval < 0) {
187 		if (retval == -EAGAIN) {
188 			redirty_page_for_writepage(wbc, page);
189 			retval = 0;
190 		} else {
191 			SetPageError(page);
192 			mapping_set_error(page->mapping, retval);
193 		}
194 	} else
195 		retval = 0;
196 
197 	unlock_page(page);
198 	return retval;
199 }
200 
201 /**
202  * v9fs_launder_page - Writeback a dirty page
203  * @page: The page to be cleaned up
204  *
205  * Returns 0 on success.
206  */
207 
v9fs_launder_page(struct page * page)208 static int v9fs_launder_page(struct page *page)
209 {
210 	int retval;
211 	struct inode *inode = page->mapping->host;
212 
213 	v9fs_fscache_wait_on_page_write(inode, page);
214 	if (clear_page_dirty_for_io(page)) {
215 		retval = v9fs_vfs_writepage_locked(page);
216 		if (retval)
217 			return retval;
218 	}
219 	return 0;
220 }
221 
222 /**
223  * v9fs_direct_IO - 9P address space operation for direct I/O
224  * @iocb: target I/O control block
225  * @iter: The data/buffer to use
226  *
227  * The presence of v9fs_direct_IO() in the address space ops vector
228  * allowes open() O_DIRECT flags which would have failed otherwise.
229  *
230  * In the non-cached mode, we shunt off direct read and write requests before
231  * the VFS gets them, so this method should never be called.
232  *
233  * Direct IO is not 'yet' supported in the cached mode. Hence when
234  * this routine is called through generic_file_aio_read(), the read/write fails
235  * with an error.
236  *
237  */
238 static ssize_t
v9fs_direct_IO(struct kiocb * iocb,struct iov_iter * iter)239 v9fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
240 {
241 	struct file *file = iocb->ki_filp;
242 	loff_t pos = iocb->ki_pos;
243 	ssize_t n;
244 	int err = 0;
245 
246 	if (iov_iter_rw(iter) == WRITE) {
247 		n = p9_client_write(file->private_data, pos, iter, &err);
248 		if (n) {
249 			struct inode *inode = file_inode(file);
250 			loff_t i_size = i_size_read(inode);
251 
252 			if (pos + n > i_size)
253 				inode_add_bytes(inode, pos + n - i_size);
254 		}
255 	} else {
256 		n = p9_client_read(file->private_data, pos, iter, &err);
257 	}
258 	return n ? n : err;
259 }
260 
v9fs_write_begin(struct file * filp,struct address_space * mapping,loff_t pos,unsigned int len,unsigned int flags,struct page ** pagep,void ** fsdata)261 static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
262 			    loff_t pos, unsigned int len, unsigned int flags,
263 			    struct page **pagep, void **fsdata)
264 {
265 	int retval = 0;
266 	struct page *page;
267 	struct v9fs_inode *v9inode;
268 	pgoff_t index = pos >> PAGE_SHIFT;
269 	struct inode *inode = mapping->host;
270 
271 
272 	p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
273 
274 	v9inode = V9FS_I(inode);
275 start:
276 	page = grab_cache_page_write_begin(mapping, index, flags);
277 	if (!page) {
278 		retval = -ENOMEM;
279 		goto out;
280 	}
281 	BUG_ON(!v9inode->writeback_fid);
282 	if (PageUptodate(page))
283 		goto out;
284 
285 	if (len == PAGE_SIZE)
286 		goto out;
287 
288 	retval = v9fs_fid_readpage(v9inode->writeback_fid, page);
289 	put_page(page);
290 	if (!retval)
291 		goto start;
292 out:
293 	*pagep = page;
294 	return retval;
295 }
296 
v9fs_write_end(struct file * filp,struct address_space * mapping,loff_t pos,unsigned int len,unsigned int copied,struct page * page,void * fsdata)297 static int v9fs_write_end(struct file *filp, struct address_space *mapping,
298 			  loff_t pos, unsigned int len, unsigned int copied,
299 			  struct page *page, void *fsdata)
300 {
301 	loff_t last_pos = pos + copied;
302 	struct inode *inode = page->mapping->host;
303 
304 	p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
305 
306 	if (!PageUptodate(page)) {
307 		if (unlikely(copied < len)) {
308 			copied = 0;
309 			goto out;
310 		} else if (len == PAGE_SIZE) {
311 			SetPageUptodate(page);
312 		}
313 	}
314 	/*
315 	 * No need to use i_size_read() here, the i_size
316 	 * cannot change under us because we hold the i_mutex.
317 	 */
318 	if (last_pos > inode->i_size) {
319 		inode_add_bytes(inode, last_pos - inode->i_size);
320 		i_size_write(inode, last_pos);
321 	}
322 	set_page_dirty(page);
323 out:
324 	unlock_page(page);
325 	put_page(page);
326 
327 	return copied;
328 }
329 
330 
331 const struct address_space_operations v9fs_addr_operations = {
332 	.readpage = v9fs_vfs_readpage,
333 	.readpages = v9fs_vfs_readpages,
334 	.set_page_dirty = __set_page_dirty_nobuffers,
335 	.writepage = v9fs_vfs_writepage,
336 	.write_begin = v9fs_write_begin,
337 	.write_end = v9fs_write_end,
338 	.releasepage = v9fs_release_page,
339 	.invalidatepage = v9fs_invalidate_page,
340 	.launder_page = v9fs_launder_page,
341 	.direct_IO = v9fs_direct_IO,
342 };
343