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