11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/direct.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2003 by Chuck Lever <cel@netapp.com> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * High-performance uncached I/O for the Linux NFS client 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * There are important applications whose performance or correctness 91da177e4SLinus Torvalds * depends on uncached access to file data. Database clusters 101da177e4SLinus Torvalds * (multiple copies of the same instance running on separate hosts) 111da177e4SLinus Torvalds * implement their own cache coherency protocol that subsumes file 121da177e4SLinus Torvalds * system cache protocols. Applications that process datasets 131da177e4SLinus Torvalds * considerably larger than the client's memory do not always benefit 141da177e4SLinus Torvalds * from a local cache. A streaming video server, for instance, has no 151da177e4SLinus Torvalds * need to cache the contents of a file. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * When an application requests uncached I/O, all read and write requests 181da177e4SLinus Torvalds * are made directly to the server; data stored or fetched via these 191da177e4SLinus Torvalds * requests is not cached in the Linux page cache. The client does not 201da177e4SLinus Torvalds * correct unaligned requests from applications. All requested bytes are 211da177e4SLinus Torvalds * held on permanent storage before a direct write system call returns to 221da177e4SLinus Torvalds * an application. 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * Solaris implements an uncached I/O facility called directio() that 251da177e4SLinus Torvalds * is used for backups and sequential I/O to very large files. Solaris 261da177e4SLinus Torvalds * also supports uncaching whole NFS partitions with "-o forcedirectio," 271da177e4SLinus Torvalds * an undocumented mount option. 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with 301da177e4SLinus Torvalds * help from Andrew Morton. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * 18 Dec 2001 Initial implementation for 2.4 --cel 331da177e4SLinus Torvalds * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy 341da177e4SLinus Torvalds * 08 Jun 2003 Port to 2.5 APIs --cel 351da177e4SLinus Torvalds * 31 Mar 2004 Handle direct I/O without VFS support --cel 361da177e4SLinus Torvalds * 15 Sep 2004 Parallel async reads --cel 3788467055SChuck Lever * 04 May 2005 support O_DIRECT with aio --cel 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/errno.h> 421da177e4SLinus Torvalds #include <linux/sched.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/file.h> 451da177e4SLinus Torvalds #include <linux/pagemap.h> 461da177e4SLinus Torvalds #include <linux/kref.h> 475a0e3ad6STejun Heo #include <linux/slab.h> 487ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h> 496296556fSPeng Tao #include <linux/module.h> 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #include <linux/nfs_fs.h> 521da177e4SLinus Torvalds #include <linux/nfs_page.h> 531da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <asm/uaccess.h> 5660063497SArun Sharma #include <linux/atomic.h> 571da177e4SLinus Torvalds 588d5658c9STrond Myklebust #include "internal.h" 5991d5b470SChuck Lever #include "iostat.h" 601763da12SFred Isaman #include "pnfs.h" 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 631da177e4SLinus Torvalds 64e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds struct nfs_direct_req { 701da177e4SLinus Torvalds struct kref kref; /* release manager */ 7115ce4a0cSChuck Lever 7215ce4a0cSChuck Lever /* I/O parameters */ 73a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 74f11ac8dbSTrond Myklebust struct nfs_lock_context *l_ctx; /* Lock context info */ 7599514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 7688467055SChuck Lever struct inode * inode; /* target file of i/o */ 7715ce4a0cSChuck Lever 7815ce4a0cSChuck Lever /* completion state */ 79607f31e8STrond Myklebust atomic_t io_count; /* i/os we're waiting for */ 8015ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 8115ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 8235754bc0SPeng Tao bytes_left, /* bytes left to be sent */ 831da177e4SLinus Torvalds error; /* any reported error */ 84d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 85fad61490STrond Myklebust 86fad61490STrond Myklebust /* commit state */ 871763da12SFred Isaman struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ 881763da12SFred Isaman struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ 891763da12SFred Isaman struct work_struct work; 90fad61490STrond Myklebust int flags; 91fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 92fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 93fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 941da177e4SLinus Torvalds }; 951da177e4SLinus Torvalds 961763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; 971763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; 98fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 991763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work); 100607f31e8STrond Myklebust 101607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 102607f31e8STrond Myklebust { 103607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 104607f31e8STrond Myklebust } 105607f31e8STrond Myklebust 106607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 107607f31e8STrond Myklebust { 108607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 109607f31e8STrond Myklebust } 110607f31e8STrond Myklebust 1111da177e4SLinus Torvalds /** 112b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 113b8a32e2bSChuck Lever * @rw: direction (read or write) 114b8a32e2bSChuck Lever * @iocb: target I/O control block 115b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 116b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 117b8a32e2bSChuck Lever * @nr_segs: size of iovec array 118b8a32e2bSChuck Lever * 119b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 120a564b8f0SMel Gorman * the NFS client supports direct I/O. However, for most direct IO, we 121a564b8f0SMel Gorman * shunt off direct read and write requests before the VFS gets them, 122a564b8f0SMel Gorman * so this method is only ever called for swap. 1231da177e4SLinus Torvalds */ 124d8d3d94bSAl Viro ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos) 125b8a32e2bSChuck Lever { 126a564b8f0SMel Gorman #ifndef CONFIG_NFS_SWAP 1276de1472fSAl Viro dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n", 128d8d3d94bSAl Viro iocb->ki_filp, (long long) pos, iter->nr_segs); 129b8a32e2bSChuck Lever 130b8a32e2bSChuck Lever return -EINVAL; 131a564b8f0SMel Gorman #else 132a564b8f0SMel Gorman VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE); 133a564b8f0SMel Gorman 134a564b8f0SMel Gorman if (rw == READ || rw == KERNEL_READ) 135619d30b4SAl Viro return nfs_file_direct_read(iocb, iter, pos, 136a564b8f0SMel Gorman rw == READ ? true : false); 137619d30b4SAl Viro return nfs_file_direct_write(iocb, iter, pos, 138a564b8f0SMel Gorman rw == WRITE ? true : false); 139a564b8f0SMel Gorman #endif /* CONFIG_NFS_SWAP */ 140b8a32e2bSChuck Lever } 141b8a32e2bSChuck Lever 142749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 1439c93ab7dSChuck Lever { 144749e146eSChuck Lever unsigned int i; 145607f31e8STrond Myklebust for (i = 0; i < npages; i++) 146607f31e8STrond Myklebust page_cache_release(pages[i]); 1476b45d858STrond Myklebust } 1486b45d858STrond Myklebust 1491763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, 1501763da12SFred Isaman struct nfs_direct_req *dreq) 1511763da12SFred Isaman { 1521763da12SFred Isaman cinfo->lock = &dreq->lock; 1531763da12SFred Isaman cinfo->mds = &dreq->mds_cinfo; 1541763da12SFred Isaman cinfo->ds = &dreq->ds_cinfo; 1551763da12SFred Isaman cinfo->dreq = dreq; 1561763da12SFred Isaman cinfo->completion_ops = &nfs_direct_commit_completion_ops; 1571763da12SFred Isaman } 1581763da12SFred Isaman 15993619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds struct nfs_direct_req *dreq; 1621da177e4SLinus Torvalds 163292f3eeeSTrond Myklebust dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); 1641da177e4SLinus Torvalds if (!dreq) 1651da177e4SLinus Torvalds return NULL; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds kref_init(&dreq->kref); 168607f31e8STrond Myklebust kref_get(&dreq->kref); 169d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 1701763da12SFred Isaman INIT_LIST_HEAD(&dreq->mds_cinfo.list); 1711763da12SFred Isaman INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); 17215ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 17393619e59SChuck Lever 17493619e59SChuck Lever return dreq; 17593619e59SChuck Lever } 17693619e59SChuck Lever 177b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 180a8881f5aSTrond Myklebust 181f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 182f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 183a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 184a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 1851da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 188b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 189b4946ffbSTrond Myklebust { 190b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 191b4946ffbSTrond Myklebust } 192b4946ffbSTrond Myklebust 1936296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) 1946296556fSPeng Tao { 1956296556fSPeng Tao return dreq->bytes_left; 1966296556fSPeng Tao } 1976296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); 1986296556fSPeng Tao 199d4cc948bSChuck Lever /* 200bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 201bc0fb201SChuck Lever */ 202bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 203bc0fb201SChuck Lever { 20415ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 205bc0fb201SChuck Lever 206bc0fb201SChuck Lever /* Async requests don't wait here */ 207bc0fb201SChuck Lever if (dreq->iocb) 208bc0fb201SChuck Lever goto out; 209bc0fb201SChuck Lever 210150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 211bc0fb201SChuck Lever 212bc0fb201SChuck Lever if (!result) 21315ce4a0cSChuck Lever result = dreq->error; 214bc0fb201SChuck Lever if (!result) 21515ce4a0cSChuck Lever result = dreq->count; 216bc0fb201SChuck Lever 217bc0fb201SChuck Lever out: 218bc0fb201SChuck Lever return (ssize_t) result; 219bc0fb201SChuck Lever } 220bc0fb201SChuck Lever 221bc0fb201SChuck Lever /* 222607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 223607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 22463ab46abSChuck Lever */ 2259811cd57SChristoph Hellwig static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write) 22663ab46abSChuck Lever { 2279811cd57SChristoph Hellwig struct inode *inode = dreq->inode; 2289811cd57SChristoph Hellwig 2292a009ec9SChristoph Hellwig if (dreq->iocb && write) { 2309811cd57SChristoph Hellwig loff_t pos = dreq->iocb->ki_pos + dreq->count; 2319811cd57SChristoph Hellwig 2329811cd57SChristoph Hellwig spin_lock(&inode->i_lock); 2339811cd57SChristoph Hellwig if (i_size_read(inode) < pos) 2349811cd57SChristoph Hellwig i_size_write(inode, pos); 2359811cd57SChristoph Hellwig spin_unlock(&inode->i_lock); 2369811cd57SChristoph Hellwig } 2379811cd57SChristoph Hellwig 2381f90ee27SChristoph Hellwig if (write) 2392a009ec9SChristoph Hellwig nfs_zap_mapping(inode, inode->i_mapping); 2401f90ee27SChristoph Hellwig 2412a009ec9SChristoph Hellwig inode_dio_done(inode); 2422a009ec9SChristoph Hellwig 2432a009ec9SChristoph Hellwig if (dreq->iocb) { 2442a009ec9SChristoph Hellwig long res = (long) dreq->error; 2452a009ec9SChristoph Hellwig if (!res) 2462a009ec9SChristoph Hellwig res = (long) dreq->count; 24763ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 248d72b7a6bSTrond Myklebust } 2492a009ec9SChristoph Hellwig 250d72b7a6bSTrond Myklebust complete_all(&dreq->completion); 25163ab46abSChuck Lever 252b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 25363ab46abSChuck Lever } 25463ab46abSChuck Lever 2551385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req) 2561da177e4SLinus Torvalds { 2571e8968c5SNiels de Vos dprintk("NFS: direct read done (%s/%llu %d@%lld)\n", 258584aa810SFred Isaman req->wb_context->dentry->d_inode->i_sb->s_id, 2591e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 260584aa810SFred Isaman req->wb_bytes, 261584aa810SFred Isaman (long long)req_offset(req)); 262584aa810SFred Isaman nfs_release_request(req); 263fdd1e74cSTrond Myklebust } 264fdd1e74cSTrond Myklebust 265584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) 266fdd1e74cSTrond Myklebust { 267584aa810SFred Isaman unsigned long bytes = 0; 268584aa810SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 269fdd1e74cSTrond Myklebust 270584aa810SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 271584aa810SFred Isaman goto out_put; 2721da177e4SLinus Torvalds 27315ce4a0cSChuck Lever spin_lock(&dreq->lock); 274584aa810SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0)) 275584aa810SFred Isaman dreq->error = hdr->error; 276584aa810SFred Isaman else 277584aa810SFred Isaman dreq->count += hdr->good_bytes; 27815ce4a0cSChuck Lever spin_unlock(&dreq->lock); 2791da177e4SLinus Torvalds 280584aa810SFred Isaman while (!list_empty(&hdr->pages)) { 281584aa810SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 282584aa810SFred Isaman struct page *page = req->wb_page; 283584aa810SFred Isaman 284be7e9858SJeff Layton if (!PageCompound(page) && bytes < hdr->good_bytes) 2854bd8b010STrond Myklebust set_page_dirty(page); 286584aa810SFred Isaman bytes += req->wb_bytes; 287584aa810SFred Isaman nfs_list_remove_request(req); 288584aa810SFred Isaman nfs_direct_readpage_release(req); 289584aa810SFred Isaman } 290584aa810SFred Isaman out_put: 291607f31e8STrond Myklebust if (put_dreq(dreq)) 2929811cd57SChristoph Hellwig nfs_direct_complete(dreq, false); 293584aa810SFred Isaman hdr->release(hdr); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2963e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head) 297cd841605SFred Isaman { 298584aa810SFred Isaman struct nfs_page *req; 299cd841605SFred Isaman 300584aa810SFred Isaman while (!list_empty(head)) { 301584aa810SFred Isaman req = nfs_list_entry(head->next); 302584aa810SFred Isaman nfs_list_remove_request(req); 303584aa810SFred Isaman nfs_release_request(req); 304cd841605SFred Isaman } 305584aa810SFred Isaman } 306584aa810SFred Isaman 307584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) 308584aa810SFred Isaman { 309584aa810SFred Isaman get_dreq(hdr->dreq); 310584aa810SFred Isaman } 311584aa810SFred Isaman 312584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { 3133e9e0ca3STrond Myklebust .error_cleanup = nfs_read_sync_pgio_error, 314584aa810SFred Isaman .init_hdr = nfs_direct_pgio_init, 315584aa810SFred Isaman .completion = nfs_direct_read_completion, 316584aa810SFred Isaman }; 317cd841605SFred Isaman 318d4cc948bSChuck Lever /* 319607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 320607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 321607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 322607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 323607f31e8STrond Myklebust * no requests have been sent, just return an error. 3241da177e4SLinus Torvalds */ 325*91f79c43SAl Viro 326*91f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 327*91f79c43SAl Viro struct iov_iter *iter, 328*91f79c43SAl Viro loff_t pos) 3291da177e4SLinus Torvalds { 330*91f79c43SAl Viro struct nfs_pageio_descriptor desc; 331*91f79c43SAl Viro struct inode *inode = dreq->inode; 332*91f79c43SAl Viro ssize_t result = -EINVAL; 333*91f79c43SAl Viro size_t requested_bytes = 0; 334*91f79c43SAl Viro size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); 33582b145c5SChuck Lever 336*91f79c43SAl Viro NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode, 337*91f79c43SAl Viro &nfs_direct_read_completion_ops); 338*91f79c43SAl Viro get_dreq(dreq); 339*91f79c43SAl Viro desc.pg_dreq = dreq; 340*91f79c43SAl Viro atomic_inc(&inode->i_dio_count); 341*91f79c43SAl Viro 342*91f79c43SAl Viro while (iov_iter_count(iter)) { 343*91f79c43SAl Viro struct page **pagevec; 3445dd602f2SChuck Lever size_t bytes; 345*91f79c43SAl Viro size_t pgbase; 346*91f79c43SAl Viro unsigned npages, i; 3471da177e4SLinus Torvalds 348*91f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 349*91f79c43SAl Viro rsize, &pgbase); 350584aa810SFred Isaman if (result < 0) 351749e146eSChuck Lever break; 352a564b8f0SMel Gorman 353*91f79c43SAl Viro bytes = result; 354*91f79c43SAl Viro iov_iter_advance(iter, bytes); 355*91f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 356584aa810SFred Isaman for (i = 0; i < npages; i++) { 357584aa810SFred Isaman struct nfs_page *req; 358bf5fc402STrond Myklebust unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 359584aa810SFred Isaman /* XXX do we need to do the eof zeroing found in async_filler? */ 360584aa810SFred Isaman req = nfs_create_request(dreq->ctx, dreq->inode, 361584aa810SFred Isaman pagevec[i], 362584aa810SFred Isaman pgbase, req_len); 363584aa810SFred Isaman if (IS_ERR(req)) { 364584aa810SFred Isaman result = PTR_ERR(req); 365dbae4c73STrond Myklebust break; 366584aa810SFred Isaman } 367584aa810SFred Isaman req->wb_index = pos >> PAGE_SHIFT; 368584aa810SFred Isaman req->wb_offset = pos & ~PAGE_MASK; 369*91f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 370*91f79c43SAl Viro result = desc.pg_error; 371584aa810SFred Isaman nfs_release_request(req); 372584aa810SFred Isaman break; 373584aa810SFred Isaman } 374584aa810SFred Isaman pgbase = 0; 375584aa810SFred Isaman bytes -= req_len; 376*91f79c43SAl Viro requested_bytes += req_len; 377584aa810SFred Isaman pos += req_len; 37835754bc0SPeng Tao dreq->bytes_left -= req_len; 379584aa810SFred Isaman } 3806d74743bSTrond Myklebust nfs_direct_release_pages(pagevec, npages); 381*91f79c43SAl Viro kvfree(pagevec); 38219f73787SChuck Lever if (result < 0) 38319f73787SChuck Lever break; 38419f73787SChuck Lever } 38519f73787SChuck Lever 386584aa810SFred Isaman nfs_pageio_complete(&desc); 387584aa810SFred Isaman 388839f7ad6SChuck Lever /* 389839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 390839f7ad6SChuck Lever * generic layer handle the completion. 391839f7ad6SChuck Lever */ 392839f7ad6SChuck Lever if (requested_bytes == 0) { 3931f90ee27SChristoph Hellwig inode_dio_done(inode); 394839f7ad6SChuck Lever nfs_direct_req_release(dreq); 395839f7ad6SChuck Lever return result < 0 ? result : -EIO; 396839f7ad6SChuck Lever } 397839f7ad6SChuck Lever 39819f73787SChuck Lever if (put_dreq(dreq)) 3999811cd57SChristoph Hellwig nfs_direct_complete(dreq, false); 40019f73787SChuck Lever return 0; 40119f73787SChuck Lever } 40219f73787SChuck Lever 40314a3ec79SChristoph Hellwig /** 40414a3ec79SChristoph Hellwig * nfs_file_direct_read - file direct read operation for NFS files 40514a3ec79SChristoph Hellwig * @iocb: target I/O control block 406619d30b4SAl Viro * @iter: vector of user buffers into which to read data 40714a3ec79SChristoph Hellwig * @pos: byte offset in file where reading starts 40814a3ec79SChristoph Hellwig * 40914a3ec79SChristoph Hellwig * We use this function for direct reads instead of calling 41014a3ec79SChristoph Hellwig * generic_file_aio_read() in order to avoid gfar's check to see if 41114a3ec79SChristoph Hellwig * the request starts before the end of the file. For that check 41214a3ec79SChristoph Hellwig * to work, we must generate a GETATTR before each direct read, and 41314a3ec79SChristoph Hellwig * even then there is a window between the GETATTR and the subsequent 41414a3ec79SChristoph Hellwig * READ where the file size could change. Our preference is simply 41514a3ec79SChristoph Hellwig * to do all reads the application wants, and the server will take 41614a3ec79SChristoph Hellwig * care of managing the end of file boundary. 41714a3ec79SChristoph Hellwig * 41814a3ec79SChristoph Hellwig * This function also eliminates unnecessarily updating the file's 41914a3ec79SChristoph Hellwig * atime locally, as the NFS server sets the file's atime, and this 42014a3ec79SChristoph Hellwig * client must read the updated atime from the server back into its 42114a3ec79SChristoph Hellwig * cache. 42214a3ec79SChristoph Hellwig */ 423619d30b4SAl Viro ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter, 424619d30b4SAl Viro loff_t pos, bool uio) 4251da177e4SLinus Torvalds { 42614a3ec79SChristoph Hellwig struct file *file = iocb->ki_filp; 42714a3ec79SChristoph Hellwig struct address_space *mapping = file->f_mapping; 42814a3ec79SChristoph Hellwig struct inode *inode = mapping->host; 4291da177e4SLinus Torvalds struct nfs_direct_req *dreq; 430b3c54de6STrond Myklebust struct nfs_lock_context *l_ctx; 43114a3ec79SChristoph Hellwig ssize_t result = -EINVAL; 432a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 43314a3ec79SChristoph Hellwig nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 43414a3ec79SChristoph Hellwig 43514a3ec79SChristoph Hellwig dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", 43614a3ec79SChristoph Hellwig file, count, (long long) pos); 43714a3ec79SChristoph Hellwig 43814a3ec79SChristoph Hellwig result = 0; 43914a3ec79SChristoph Hellwig if (!count) 44014a3ec79SChristoph Hellwig goto out; 44114a3ec79SChristoph Hellwig 442d0b9875dSChristoph Hellwig mutex_lock(&inode->i_mutex); 44314a3ec79SChristoph Hellwig result = nfs_sync_mapping(mapping); 44414a3ec79SChristoph Hellwig if (result) 445d0b9875dSChristoph Hellwig goto out_unlock; 44614a3ec79SChristoph Hellwig 44714a3ec79SChristoph Hellwig task_io_account_read(count); 44814a3ec79SChristoph Hellwig 44914a3ec79SChristoph Hellwig result = -ENOMEM; 450607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 451f11ac8dbSTrond Myklebust if (dreq == NULL) 452d0b9875dSChristoph Hellwig goto out_unlock; 4531da177e4SLinus Torvalds 45491d5b470SChuck Lever dreq->inode = inode; 455619d30b4SAl Viro dreq->bytes_left = count; 456cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 457b3c54de6STrond Myklebust l_ctx = nfs_get_lock_context(dreq->ctx); 458b3c54de6STrond Myklebust if (IS_ERR(l_ctx)) { 459b3c54de6STrond Myklebust result = PTR_ERR(l_ctx); 460f11ac8dbSTrond Myklebust goto out_release; 461b3c54de6STrond Myklebust } 462b3c54de6STrond Myklebust dreq->l_ctx = l_ctx; 463487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 464487b8372SChuck Lever dreq->iocb = iocb; 4651da177e4SLinus Torvalds 466619d30b4SAl Viro NFS_I(inode)->read_io += count; 467*91f79c43SAl Viro result = nfs_direct_read_schedule_iovec(dreq, iter, pos); 468d0b9875dSChristoph Hellwig 469d0b9875dSChristoph Hellwig mutex_unlock(&inode->i_mutex); 470d0b9875dSChristoph Hellwig 47114a3ec79SChristoph Hellwig if (!result) { 472bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 47314a3ec79SChristoph Hellwig if (result > 0) 47414a3ec79SChristoph Hellwig iocb->ki_pos = pos + result; 47514a3ec79SChristoph Hellwig } 476d0b9875dSChristoph Hellwig 477d0b9875dSChristoph Hellwig nfs_direct_req_release(dreq); 478d0b9875dSChristoph Hellwig return result; 479d0b9875dSChristoph Hellwig 480f11ac8dbSTrond Myklebust out_release: 481b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 482d0b9875dSChristoph Hellwig out_unlock: 483d0b9875dSChristoph Hellwig mutex_unlock(&inode->i_mutex); 484f11ac8dbSTrond Myklebust out: 4851da177e4SLinus Torvalds return result; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 48889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 489fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 4901da177e4SLinus Torvalds { 4911763da12SFred Isaman struct nfs_pageio_descriptor desc; 4921763da12SFred Isaman struct nfs_page *req, *tmp; 4931763da12SFred Isaman LIST_HEAD(reqs); 4941763da12SFred Isaman struct nfs_commit_info cinfo; 4951763da12SFred Isaman LIST_HEAD(failed); 4961763da12SFred Isaman 4971763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 4981763da12SFred Isaman pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo); 4991763da12SFred Isaman spin_lock(cinfo.lock); 5001763da12SFred Isaman nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0); 5011763da12SFred Isaman spin_unlock(cinfo.lock); 5021da177e4SLinus Torvalds 503fad61490STrond Myklebust dreq->count = 0; 504607f31e8STrond Myklebust get_dreq(dreq); 5051da177e4SLinus Torvalds 506c95908e4SFred Isaman NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE, 5071763da12SFred Isaman &nfs_direct_write_completion_ops); 5081763da12SFred Isaman desc.pg_dreq = dreq; 509607f31e8STrond Myklebust 5101763da12SFred Isaman list_for_each_entry_safe(req, tmp, &reqs, wb_list) { 5111763da12SFred Isaman if (!nfs_pageio_add_request(&desc, req)) { 5124035c248STrond Myklebust nfs_list_remove_request(req); 5131763da12SFred Isaman nfs_list_add_request(req, &failed); 5141763da12SFred Isaman spin_lock(cinfo.lock); 5151763da12SFred Isaman dreq->flags = 0; 5161763da12SFred Isaman dreq->error = -EIO; 5171763da12SFred Isaman spin_unlock(cinfo.lock); 5181763da12SFred Isaman } 5195a695da2STrond Myklebust nfs_release_request(req); 5201763da12SFred Isaman } 5211763da12SFred Isaman nfs_pageio_complete(&desc); 522607f31e8STrond Myklebust 5234035c248STrond Myklebust while (!list_empty(&failed)) { 5244035c248STrond Myklebust req = nfs_list_entry(failed.next); 5254035c248STrond Myklebust nfs_list_remove_request(req); 5261d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 5274035c248STrond Myklebust } 528607f31e8STrond Myklebust 529607f31e8STrond Myklebust if (put_dreq(dreq)) 5301763da12SFred Isaman nfs_direct_write_complete(dreq, dreq->inode); 531fad61490STrond Myklebust } 5321da177e4SLinus Torvalds 5331763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data) 534fad61490STrond Myklebust { 5350b7c0153SFred Isaman struct nfs_direct_req *dreq = data->dreq; 5361763da12SFred Isaman struct nfs_commit_info cinfo; 5371763da12SFred Isaman struct nfs_page *req; 538c9d8f89dSTrond Myklebust int status = data->task.tk_status; 539c9d8f89dSTrond Myklebust 5401763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 541c9d8f89dSTrond Myklebust if (status < 0) { 54260fa3f76STrond Myklebust dprintk("NFS: %5u commit failed with error %d.\n", 543c9d8f89dSTrond Myklebust data->task.tk_pid, status); 544fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 54560fa3f76STrond Myklebust } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 546c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid); 547fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 548fad61490STrond Myklebust } 549fad61490STrond Myklebust 550c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status); 5511763da12SFred Isaman while (!list_empty(&data->pages)) { 5521763da12SFred Isaman req = nfs_list_entry(data->pages.next); 5531763da12SFred Isaman nfs_list_remove_request(req); 5541763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) { 5551763da12SFred Isaman /* Note the rewrite will go through mds */ 5561763da12SFred Isaman nfs_mark_request_commit(req, NULL, &cinfo); 557906369e4SFred Isaman } else 558906369e4SFred Isaman nfs_release_request(req); 5591d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 560fad61490STrond Myklebust } 561fad61490STrond Myklebust 5621763da12SFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 5631763da12SFred Isaman nfs_direct_write_complete(dreq, data->inode); 5641763da12SFred Isaman } 5651763da12SFred Isaman 5661763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi) 5671763da12SFred Isaman { 5681763da12SFred Isaman /* There is no lock to clear */ 5691763da12SFred Isaman } 5701763da12SFred Isaman 5711763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { 5721763da12SFred Isaman .completion = nfs_direct_commit_complete, 5731763da12SFred Isaman .error_cleanup = nfs_direct_error_cleanup, 574fad61490STrond Myklebust }; 575fad61490STrond Myklebust 576fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 577fad61490STrond Myklebust { 5781763da12SFred Isaman int res; 5791763da12SFred Isaman struct nfs_commit_info cinfo; 5801763da12SFred Isaman LIST_HEAD(mds_list); 581fad61490STrond Myklebust 5821763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 5831763da12SFred Isaman nfs_scan_commit(dreq->inode, &mds_list, &cinfo); 5841763da12SFred Isaman res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); 5851763da12SFred Isaman if (res < 0) /* res == -ENOMEM */ 5861763da12SFred Isaman nfs_direct_write_reschedule(dreq); 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work) 5901da177e4SLinus Torvalds { 5911763da12SFred Isaman struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); 592fad61490STrond Myklebust int flags = dreq->flags; 5931da177e4SLinus Torvalds 594fad61490STrond Myklebust dreq->flags = 0; 595fad61490STrond Myklebust switch (flags) { 596fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 597fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 5981da177e4SLinus Torvalds break; 599fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 600fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 6011da177e4SLinus Torvalds break; 6021da177e4SLinus Torvalds default: 6039811cd57SChristoph Hellwig nfs_direct_complete(dreq, true); 6041da177e4SLinus Torvalds } 605fad61490STrond Myklebust } 606fad61490STrond Myklebust 6071763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 608fad61490STrond Myklebust { 6091763da12SFred Isaman schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */ 610fad61490STrond Myklebust } 6111763da12SFred Isaman 612fad61490STrond Myklebust #else 61324fc9211SBryan Schumaker static void nfs_direct_write_schedule_work(struct work_struct *work) 61424fc9211SBryan Schumaker { 61524fc9211SBryan Schumaker } 616fad61490STrond Myklebust 617fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 618fad61490STrond Myklebust { 6199811cd57SChristoph Hellwig nfs_direct_complete(dreq, true); 620fad61490STrond Myklebust } 621fad61490STrond Myklebust #endif 622fad61490STrond Myklebust 6231763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) 6241763da12SFred Isaman { 6251763da12SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 6261763da12SFred Isaman struct nfs_commit_info cinfo; 6271763da12SFred Isaman int bit = -1; 6281763da12SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 6291763da12SFred Isaman 6301763da12SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 6311763da12SFred Isaman goto out_put; 6321763da12SFred Isaman 6331763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 6341763da12SFred Isaman 6351763da12SFred Isaman spin_lock(&dreq->lock); 6361763da12SFred Isaman 6371763da12SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { 6381763da12SFred Isaman dreq->flags = 0; 6391763da12SFred Isaman dreq->error = hdr->error; 6401763da12SFred Isaman } 6411763da12SFred Isaman if (dreq->error != 0) 6421763da12SFred Isaman bit = NFS_IOHDR_ERROR; 6431763da12SFred Isaman else { 6441763da12SFred Isaman dreq->count += hdr->good_bytes; 6451763da12SFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { 6461763da12SFred Isaman dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 6471763da12SFred Isaman bit = NFS_IOHDR_NEED_RESCHED; 6481763da12SFred Isaman } else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { 6491763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) 6501763da12SFred Isaman bit = NFS_IOHDR_NEED_RESCHED; 6511763da12SFred Isaman else if (dreq->flags == 0) { 6529bce008bSTrond Myklebust memcpy(&dreq->verf, hdr->verf, 6531763da12SFred Isaman sizeof(dreq->verf)); 6541763da12SFred Isaman bit = NFS_IOHDR_NEED_COMMIT; 6551763da12SFred Isaman dreq->flags = NFS_ODIRECT_DO_COMMIT; 6561763da12SFred Isaman } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) { 6579bce008bSTrond Myklebust if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) { 6581763da12SFred Isaman dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 6591763da12SFred Isaman bit = NFS_IOHDR_NEED_RESCHED; 6601763da12SFred Isaman } else 6611763da12SFred Isaman bit = NFS_IOHDR_NEED_COMMIT; 6621763da12SFred Isaman } 6631763da12SFred Isaman } 6641763da12SFred Isaman } 6651763da12SFred Isaman spin_unlock(&dreq->lock); 6661763da12SFred Isaman 6671763da12SFred Isaman while (!list_empty(&hdr->pages)) { 6681763da12SFred Isaman req = nfs_list_entry(hdr->pages.next); 6691763da12SFred Isaman nfs_list_remove_request(req); 6701763da12SFred Isaman switch (bit) { 6711763da12SFred Isaman case NFS_IOHDR_NEED_RESCHED: 6721763da12SFred Isaman case NFS_IOHDR_NEED_COMMIT: 67304277086STrond Myklebust kref_get(&req->wb_kref); 6741763da12SFred Isaman nfs_mark_request_commit(req, hdr->lseg, &cinfo); 6751763da12SFred Isaman } 6761d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 6771763da12SFred Isaman } 6781763da12SFred Isaman 6791763da12SFred Isaman out_put: 6801763da12SFred Isaman if (put_dreq(dreq)) 6811763da12SFred Isaman nfs_direct_write_complete(dreq, hdr->inode); 6821763da12SFred Isaman hdr->release(hdr); 6831763da12SFred Isaman } 6841763da12SFred Isaman 6853e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head) 6863e9e0ca3STrond Myklebust { 6873e9e0ca3STrond Myklebust struct nfs_page *req; 6883e9e0ca3STrond Myklebust 6893e9e0ca3STrond Myklebust while (!list_empty(head)) { 6903e9e0ca3STrond Myklebust req = nfs_list_entry(head->next); 6913e9e0ca3STrond Myklebust nfs_list_remove_request(req); 6921d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 6933e9e0ca3STrond Myklebust } 6943e9e0ca3STrond Myklebust } 6953e9e0ca3STrond Myklebust 6961763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { 6973e9e0ca3STrond Myklebust .error_cleanup = nfs_write_sync_pgio_error, 6981763da12SFred Isaman .init_hdr = nfs_direct_pgio_init, 6991763da12SFred Isaman .completion = nfs_direct_write_completion, 7001763da12SFred Isaman }; 7011763da12SFred Isaman 702*91f79c43SAl Viro 703*91f79c43SAl Viro /* 704*91f79c43SAl Viro * NB: Return the value of the first error return code. Subsequent 705*91f79c43SAl Viro * errors after the first one are ignored. 706*91f79c43SAl Viro */ 707*91f79c43SAl Viro /* 708*91f79c43SAl Viro * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 709*91f79c43SAl Viro * operation. If nfs_writedata_alloc() or get_user_pages() fails, 710*91f79c43SAl Viro * bail and stop sending more writes. Write length accounting is 711*91f79c43SAl Viro * handled automatically by nfs_direct_write_result(). Otherwise, if 712*91f79c43SAl Viro * no requests have been sent, just return an error. 713*91f79c43SAl Viro */ 71419f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 715619d30b4SAl Viro struct iov_iter *iter, 716*91f79c43SAl Viro loff_t pos) 71719f73787SChuck Lever { 7181763da12SFred Isaman struct nfs_pageio_descriptor desc; 7191d59d61fSTrond Myklebust struct inode *inode = dreq->inode; 72019f73787SChuck Lever ssize_t result = 0; 72119f73787SChuck Lever size_t requested_bytes = 0; 722*91f79c43SAl Viro size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); 72319f73787SChuck Lever 724c95908e4SFred Isaman NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE, 7251763da12SFred Isaman &nfs_direct_write_completion_ops); 7261763da12SFred Isaman desc.pg_dreq = dreq; 72719f73787SChuck Lever get_dreq(dreq); 7281d59d61fSTrond Myklebust atomic_inc(&inode->i_dio_count); 72919f73787SChuck Lever 730*91f79c43SAl Viro NFS_I(inode)->write_io += iov_iter_count(iter); 731*91f79c43SAl Viro while (iov_iter_count(iter)) { 732*91f79c43SAl Viro struct page **pagevec; 733*91f79c43SAl Viro size_t bytes; 734*91f79c43SAl Viro size_t pgbase; 735*91f79c43SAl Viro unsigned npages, i; 736*91f79c43SAl Viro 737*91f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 738*91f79c43SAl Viro wsize, &pgbase); 73919f73787SChuck Lever if (result < 0) 74019f73787SChuck Lever break; 741*91f79c43SAl Viro 742*91f79c43SAl Viro bytes = result; 743*91f79c43SAl Viro iov_iter_advance(iter, bytes); 744*91f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 745*91f79c43SAl Viro for (i = 0; i < npages; i++) { 746*91f79c43SAl Viro struct nfs_page *req; 747*91f79c43SAl Viro unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 748*91f79c43SAl Viro 749*91f79c43SAl Viro req = nfs_create_request(dreq->ctx, inode, 750*91f79c43SAl Viro pagevec[i], 751*91f79c43SAl Viro pgbase, req_len); 752*91f79c43SAl Viro if (IS_ERR(req)) { 753*91f79c43SAl Viro result = PTR_ERR(req); 75419f73787SChuck Lever break; 755*91f79c43SAl Viro } 756*91f79c43SAl Viro nfs_lock_request(req); 757*91f79c43SAl Viro req->wb_index = pos >> PAGE_SHIFT; 758*91f79c43SAl Viro req->wb_offset = pos & ~PAGE_MASK; 759*91f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 760*91f79c43SAl Viro result = desc.pg_error; 761*91f79c43SAl Viro nfs_unlock_and_release_request(req); 762*91f79c43SAl Viro break; 763*91f79c43SAl Viro } 764*91f79c43SAl Viro pgbase = 0; 765*91f79c43SAl Viro bytes -= req_len; 766*91f79c43SAl Viro requested_bytes += req_len; 767*91f79c43SAl Viro pos += req_len; 768*91f79c43SAl Viro dreq->bytes_left -= req_len; 769*91f79c43SAl Viro } 770*91f79c43SAl Viro nfs_direct_release_pages(pagevec, npages); 771*91f79c43SAl Viro kvfree(pagevec); 772*91f79c43SAl Viro if (result < 0) 773*91f79c43SAl Viro break; 77419f73787SChuck Lever } 7751763da12SFred Isaman nfs_pageio_complete(&desc); 77619f73787SChuck Lever 777839f7ad6SChuck Lever /* 778839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 779839f7ad6SChuck Lever * generic layer handle the completion. 780839f7ad6SChuck Lever */ 781839f7ad6SChuck Lever if (requested_bytes == 0) { 7821d59d61fSTrond Myklebust inode_dio_done(inode); 783839f7ad6SChuck Lever nfs_direct_req_release(dreq); 784839f7ad6SChuck Lever return result < 0 ? result : -EIO; 785839f7ad6SChuck Lever } 786839f7ad6SChuck Lever 78719f73787SChuck Lever if (put_dreq(dreq)) 78819f73787SChuck Lever nfs_direct_write_complete(dreq, dreq->inode); 78919f73787SChuck Lever return 0; 79019f73787SChuck Lever } 79119f73787SChuck Lever 7921da177e4SLinus Torvalds /** 7931da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 7941da177e4SLinus Torvalds * @iocb: target I/O control block 795619d30b4SAl Viro * @iter: vector of user buffers from which to write data 79688467055SChuck Lever * @pos: byte offset in file where writing starts 7971da177e4SLinus Torvalds * 7981da177e4SLinus Torvalds * We use this function for direct writes instead of calling 7991da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 8001da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 8011da177e4SLinus Torvalds * the new i_size and this client must read the updated size 8021da177e4SLinus Torvalds * back into its cache. We let the server do generic write 8031da177e4SLinus Torvalds * parameter checking and report problems. 8041da177e4SLinus Torvalds * 8051da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8061da177e4SLinus Torvalds * 8071da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8081da177e4SLinus Torvalds * readers of this file. 8091da177e4SLinus Torvalds * 8101da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8111da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8121da177e4SLinus Torvalds */ 813619d30b4SAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, 814619d30b4SAl Viro loff_t pos, bool uio) 8151da177e4SLinus Torvalds { 81622cd1bf1SChristoph Hellwig ssize_t result = -EINVAL; 8171da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 8181da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 81922cd1bf1SChristoph Hellwig struct inode *inode = mapping->host; 82022cd1bf1SChristoph Hellwig struct nfs_direct_req *dreq; 82122cd1bf1SChristoph Hellwig struct nfs_lock_context *l_ctx; 822a9ab5e84SChristoph Hellwig loff_t end; 823a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 824a9ab5e84SChristoph Hellwig end = (pos + count - 1) >> PAGE_CACHE_SHIFT; 825a9ab5e84SChristoph Hellwig 826c216fd70SChuck Lever nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 827c216fd70SChuck Lever 8286de1472fSAl Viro dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", 8296de1472fSAl Viro file, count, (long long) pos); 830027445c3SBadari Pulavarty 83122cd1bf1SChristoph Hellwig result = generic_write_checks(file, &pos, &count, 0); 83222cd1bf1SChristoph Hellwig if (result) 8331da177e4SLinus Torvalds goto out; 834ce1a8e67SChuck Lever 83522cd1bf1SChristoph Hellwig result = -EINVAL; 836ce1a8e67SChuck Lever if ((ssize_t) count < 0) 8371da177e4SLinus Torvalds goto out; 83822cd1bf1SChristoph Hellwig result = 0; 8391da177e4SLinus Torvalds if (!count) 8401da177e4SLinus Torvalds goto out; 841ce1a8e67SChuck Lever 842a9ab5e84SChristoph Hellwig mutex_lock(&inode->i_mutex); 843a9ab5e84SChristoph Hellwig 84422cd1bf1SChristoph Hellwig result = nfs_sync_mapping(mapping); 84522cd1bf1SChristoph Hellwig if (result) 846a9ab5e84SChristoph Hellwig goto out_unlock; 847a9ab5e84SChristoph Hellwig 848a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 849a9ab5e84SChristoph Hellwig result = invalidate_inode_pages2_range(mapping, 850a9ab5e84SChristoph Hellwig pos >> PAGE_CACHE_SHIFT, end); 851a9ab5e84SChristoph Hellwig if (result) 852a9ab5e84SChristoph Hellwig goto out_unlock; 853a9ab5e84SChristoph Hellwig } 8541da177e4SLinus Torvalds 8557ec10f26SKonstantin Khlebnikov task_io_account_write(count); 8567ec10f26SKonstantin Khlebnikov 85722cd1bf1SChristoph Hellwig result = -ENOMEM; 85822cd1bf1SChristoph Hellwig dreq = nfs_direct_req_alloc(); 85922cd1bf1SChristoph Hellwig if (!dreq) 860a9ab5e84SChristoph Hellwig goto out_unlock; 86122cd1bf1SChristoph Hellwig 86222cd1bf1SChristoph Hellwig dreq->inode = inode; 86322cd1bf1SChristoph Hellwig dreq->bytes_left = count; 86422cd1bf1SChristoph Hellwig dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 86522cd1bf1SChristoph Hellwig l_ctx = nfs_get_lock_context(dreq->ctx); 86622cd1bf1SChristoph Hellwig if (IS_ERR(l_ctx)) { 86722cd1bf1SChristoph Hellwig result = PTR_ERR(l_ctx); 86822cd1bf1SChristoph Hellwig goto out_release; 86922cd1bf1SChristoph Hellwig } 87022cd1bf1SChristoph Hellwig dreq->l_ctx = l_ctx; 87122cd1bf1SChristoph Hellwig if (!is_sync_kiocb(iocb)) 87222cd1bf1SChristoph Hellwig dreq->iocb = iocb; 87322cd1bf1SChristoph Hellwig 874*91f79c43SAl Viro result = nfs_direct_write_schedule_iovec(dreq, iter, pos); 875a9ab5e84SChristoph Hellwig 876a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 877a9ab5e84SChristoph Hellwig invalidate_inode_pages2_range(mapping, 878a9ab5e84SChristoph Hellwig pos >> PAGE_CACHE_SHIFT, end); 879a9ab5e84SChristoph Hellwig } 880a9ab5e84SChristoph Hellwig 881a9ab5e84SChristoph Hellwig mutex_unlock(&inode->i_mutex); 882a9ab5e84SChristoph Hellwig 88322cd1bf1SChristoph Hellwig if (!result) { 88422cd1bf1SChristoph Hellwig result = nfs_direct_wait(dreq); 88522cd1bf1SChristoph Hellwig if (result > 0) { 8861763da12SFred Isaman struct inode *inode = mapping->host; 8879eafa8ccSChuck Lever 88822cd1bf1SChristoph Hellwig iocb->ki_pos = pos + result; 8891763da12SFred Isaman spin_lock(&inode->i_lock); 8901763da12SFred Isaman if (i_size_read(inode) < iocb->ki_pos) 8911763da12SFred Isaman i_size_write(inode, iocb->ki_pos); 8921763da12SFred Isaman spin_unlock(&inode->i_lock); 8931763da12SFred Isaman } 89422cd1bf1SChristoph Hellwig } 895a9ab5e84SChristoph Hellwig nfs_direct_req_release(dreq); 896a9ab5e84SChristoph Hellwig return result; 897a9ab5e84SChristoph Hellwig 89822cd1bf1SChristoph Hellwig out_release: 89922cd1bf1SChristoph Hellwig nfs_direct_req_release(dreq); 900a9ab5e84SChristoph Hellwig out_unlock: 901a9ab5e84SChristoph Hellwig mutex_unlock(&inode->i_mutex); 9021da177e4SLinus Torvalds out: 90322cd1bf1SChristoph Hellwig return result; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 90688467055SChuck Lever /** 90788467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 90888467055SChuck Lever * 90988467055SChuck Lever */ 910f7b422b1SDavid Howells int __init nfs_init_directcache(void) 9111da177e4SLinus Torvalds { 9121da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 9131da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 914fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 915fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 91620c2df83SPaul Mundt NULL); 9171da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 9181da177e4SLinus Torvalds return -ENOMEM; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds return 0; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 92388467055SChuck Lever /** 924f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 92588467055SChuck Lever * 92688467055SChuck Lever */ 927266bee88SDavid Brownell void nfs_destroy_directcache(void) 9281da177e4SLinus Torvalds { 9291a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 9301da177e4SLinus Torvalds } 931