1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/direct.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2003 by Chuck Lever <cel@netapp.com> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * High-performance uncached I/O for the Linux NFS client 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * There are important applications whose performance or correctness 101da177e4SLinus Torvalds * depends on uncached access to file data. Database clusters 111da177e4SLinus Torvalds * (multiple copies of the same instance running on separate hosts) 121da177e4SLinus Torvalds * implement their own cache coherency protocol that subsumes file 131da177e4SLinus Torvalds * system cache protocols. Applications that process datasets 141da177e4SLinus Torvalds * considerably larger than the client's memory do not always benefit 151da177e4SLinus Torvalds * from a local cache. A streaming video server, for instance, has no 161da177e4SLinus Torvalds * need to cache the contents of a file. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * When an application requests uncached I/O, all read and write requests 191da177e4SLinus Torvalds * are made directly to the server; data stored or fetched via these 201da177e4SLinus Torvalds * requests is not cached in the Linux page cache. The client does not 211da177e4SLinus Torvalds * correct unaligned requests from applications. All requested bytes are 221da177e4SLinus Torvalds * held on permanent storage before a direct write system call returns to 231da177e4SLinus Torvalds * an application. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Solaris implements an uncached I/O facility called directio() that 261da177e4SLinus Torvalds * is used for backups and sequential I/O to very large files. Solaris 271da177e4SLinus Torvalds * also supports uncaching whole NFS partitions with "-o forcedirectio," 281da177e4SLinus Torvalds * an undocumented mount option. 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with 311da177e4SLinus Torvalds * help from Andrew Morton. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * 18 Dec 2001 Initial implementation for 2.4 --cel 341da177e4SLinus Torvalds * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy 351da177e4SLinus Torvalds * 08 Jun 2003 Port to 2.5 APIs --cel 361da177e4SLinus Torvalds * 31 Mar 2004 Handle direct I/O without VFS support --cel 371da177e4SLinus Torvalds * 15 Sep 2004 Parallel async reads --cel 3888467055SChuck Lever * 04 May 2005 support O_DIRECT with aio --cel 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <linux/errno.h> 431da177e4SLinus Torvalds #include <linux/sched.h> 441da177e4SLinus Torvalds #include <linux/kernel.h> 451da177e4SLinus Torvalds #include <linux/file.h> 461da177e4SLinus Torvalds #include <linux/pagemap.h> 471da177e4SLinus Torvalds #include <linux/kref.h> 485a0e3ad6STejun Heo #include <linux/slab.h> 497ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h> 506296556fSPeng Tao #include <linux/module.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/nfs_fs.h> 531da177e4SLinus Torvalds #include <linux/nfs_page.h> 541da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 551da177e4SLinus Torvalds 567c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 5760063497SArun Sharma #include <linux/atomic.h> 581da177e4SLinus Torvalds 598d5658c9STrond Myklebust #include "internal.h" 6091d5b470SChuck Lever #include "iostat.h" 611763da12SFred Isaman #include "pnfs.h" 62a6b5a28eSDave Wysochanski #include "fscache.h" 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 651da177e4SLinus Torvalds 66e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds struct nfs_direct_req { 691da177e4SLinus Torvalds struct kref kref; /* release manager */ 7015ce4a0cSChuck Lever 7115ce4a0cSChuck Lever /* I/O parameters */ 72a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 73f11ac8dbSTrond Myklebust struct nfs_lock_context *l_ctx; /* Lock context info */ 7499514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 7588467055SChuck Lever struct inode * inode; /* target file of i/o */ 7615ce4a0cSChuck Lever 7715ce4a0cSChuck Lever /* completion state */ 78607f31e8STrond Myklebust atomic_t io_count; /* i/os we're waiting for */ 7915ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 800a00b77bSWeston Andros Adamson 81d9ee6553STrond Myklebust loff_t io_start; /* Start offset for I/O */ 8215ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 83ed3743a6SWeston Andros Adamson max_count, /* max expected count */ 8435754bc0SPeng Tao bytes_left, /* bytes left to be sent */ 851da177e4SLinus Torvalds error; /* any reported error */ 86d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 87fad61490STrond Myklebust 88fad61490STrond Myklebust /* commit state */ 891763da12SFred Isaman struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ 901763da12SFred Isaman struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ 911763da12SFred Isaman struct work_struct work; 92fad61490STrond Myklebust int flags; 93ad3cba22SDave Kleikamp /* for write */ 94fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 95fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 96ad3cba22SDave Kleikamp /* for read */ 97ad3cba22SDave Kleikamp #define NFS_ODIRECT_SHOULD_DIRTY (3) /* dirty user-space page after read */ 98fb5f7f20STrond Myklebust #define NFS_ODIRECT_DONE INT_MAX /* write verification failed */ 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 1011763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; 1021763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; 1034d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq); 1041763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work); 105607f31e8STrond Myklebust 106607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 107607f31e8STrond Myklebust { 108607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 109607f31e8STrond Myklebust } 110607f31e8STrond Myklebust 111607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 112607f31e8STrond Myklebust { 113607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 114607f31e8STrond Myklebust } 115607f31e8STrond Myklebust 1160a00b77bSWeston Andros Adamson static void 117031d73edSTrond Myklebust nfs_direct_handle_truncated(struct nfs_direct_req *dreq, 118031d73edSTrond Myklebust const struct nfs_pgio_header *hdr, 119031d73edSTrond Myklebust ssize_t dreq_len) 1200a00b77bSWeston Andros Adamson { 121031d73edSTrond Myklebust if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) || 122031d73edSTrond Myklebust test_bit(NFS_IOHDR_EOF, &hdr->flags))) 123031d73edSTrond Myklebust return; 124031d73edSTrond Myklebust if (dreq->max_count >= dreq_len) { 125031d73edSTrond Myklebust dreq->max_count = dreq_len; 126031d73edSTrond Myklebust if (dreq->count > dreq_len) 127031d73edSTrond Myklebust dreq->count = dreq_len; 128ed3743a6SWeston Andros Adamson 129031d73edSTrond Myklebust if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) 130031d73edSTrond Myklebust dreq->error = hdr->error; 131031d73edSTrond Myklebust else /* Clear outstanding error if this is EOF */ 132031d73edSTrond Myklebust dreq->error = 0; 1335fadeb47SPeng Tao } 1340a00b77bSWeston Andros Adamson } 135031d73edSTrond Myklebust 136031d73edSTrond Myklebust static void 137031d73edSTrond Myklebust nfs_direct_count_bytes(struct nfs_direct_req *dreq, 138031d73edSTrond Myklebust const struct nfs_pgio_header *hdr) 139031d73edSTrond Myklebust { 140031d73edSTrond Myklebust loff_t hdr_end = hdr->io_start + hdr->good_bytes; 141031d73edSTrond Myklebust ssize_t dreq_len = 0; 142031d73edSTrond Myklebust 143031d73edSTrond Myklebust if (hdr_end > dreq->io_start) 144031d73edSTrond Myklebust dreq_len = hdr_end - dreq->io_start; 145031d73edSTrond Myklebust 146031d73edSTrond Myklebust nfs_direct_handle_truncated(dreq, hdr, dreq_len); 147031d73edSTrond Myklebust 148031d73edSTrond Myklebust if (dreq_len > dreq->max_count) 149031d73edSTrond Myklebust dreq_len = dreq->max_count; 150031d73edSTrond Myklebust 151031d73edSTrond Myklebust if (dreq->count < dreq_len) 152031d73edSTrond Myklebust dreq->count = dreq_len; 1531ccbad9fSPeng Tao } 1540a00b77bSWeston Andros Adamson 1551da177e4SLinus Torvalds /** 156eb79f3afSNeilBrown * nfs_swap_rw - NFS address space operation for swap I/O 157b8a32e2bSChuck Lever * @iocb: target I/O control block 15890090ae6SAl Viro * @iter: I/O buffer 159b8a32e2bSChuck Lever * 160eb79f3afSNeilBrown * Perform IO to the swap-file. This is much like direct IO. 1611da177e4SLinus Torvalds */ 162eb79f3afSNeilBrown int nfs_swap_rw(struct kiocb *iocb, struct iov_iter *iter) 163b8a32e2bSChuck Lever { 164eb79f3afSNeilBrown ssize_t ret; 165ee8a1a8bSPeng Tao 16666ee59afSChristoph Hellwig VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE); 167a564b8f0SMel Gorman 1686f673763SOmar Sandoval if (iov_iter_rw(iter) == READ) 169eb79f3afSNeilBrown ret = nfs_file_direct_read(iocb, iter, true); 170eb79f3afSNeilBrown else 171eb79f3afSNeilBrown ret = nfs_file_direct_write(iocb, iter, true); 172eb79f3afSNeilBrown if (ret < 0) 173eb79f3afSNeilBrown return ret; 174eb79f3afSNeilBrown return 0; 175b8a32e2bSChuck Lever } 176b8a32e2bSChuck Lever 177749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 1789c93ab7dSChuck Lever { 179749e146eSChuck Lever unsigned int i; 180607f31e8STrond Myklebust for (i = 0; i < npages; i++) 18109cbfeafSKirill A. Shutemov put_page(pages[i]); 1826b45d858STrond Myklebust } 1836b45d858STrond Myklebust 1841763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, 1851763da12SFred Isaman struct nfs_direct_req *dreq) 1861763da12SFred Isaman { 187fe238e60SDave Wysochanski cinfo->inode = dreq->inode; 1881763da12SFred Isaman cinfo->mds = &dreq->mds_cinfo; 1891763da12SFred Isaman cinfo->ds = &dreq->ds_cinfo; 1901763da12SFred Isaman cinfo->dreq = dreq; 1911763da12SFred Isaman cinfo->completion_ops = &nfs_direct_commit_completion_ops; 1921763da12SFred Isaman } 1931763da12SFred Isaman 19493619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds struct nfs_direct_req *dreq; 1971da177e4SLinus Torvalds 198292f3eeeSTrond Myklebust dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); 1991da177e4SLinus Torvalds if (!dreq) 2001da177e4SLinus Torvalds return NULL; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds kref_init(&dreq->kref); 203607f31e8STrond Myklebust kref_get(&dreq->kref); 204d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 2051763da12SFred Isaman INIT_LIST_HEAD(&dreq->mds_cinfo.list); 206c21e7168STrond Myklebust pnfs_init_ds_commit_info(&dreq->ds_cinfo); 2071763da12SFred Isaman INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); 20815ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 20993619e59SChuck Lever 21093619e59SChuck Lever return dreq; 21193619e59SChuck Lever } 21293619e59SChuck Lever 213b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 216a8881f5aSTrond Myklebust 21718f41296STrond Myklebust pnfs_release_ds_info(&dreq->ds_cinfo, dreq->inode); 218f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 219f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 220a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 221a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 2221da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 225b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 226b4946ffbSTrond Myklebust { 227b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 228b4946ffbSTrond Myklebust } 229b4946ffbSTrond Myklebust 2306296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) 2316296556fSPeng Tao { 2326296556fSPeng Tao return dreq->bytes_left; 2336296556fSPeng Tao } 2346296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); 2356296556fSPeng Tao 236d4cc948bSChuck Lever /* 237bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 238bc0fb201SChuck Lever */ 239bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 240bc0fb201SChuck Lever { 24115ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 242bc0fb201SChuck Lever 243bc0fb201SChuck Lever /* Async requests don't wait here */ 244bc0fb201SChuck Lever if (dreq->iocb) 245bc0fb201SChuck Lever goto out; 246bc0fb201SChuck Lever 247150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 248bc0fb201SChuck Lever 249d2a7de0bSTrond Myklebust if (!result) { 250d2a7de0bSTrond Myklebust result = dreq->count; 251d2a7de0bSTrond Myklebust WARN_ON_ONCE(dreq->count < 0); 252d2a7de0bSTrond Myklebust } 253bc0fb201SChuck Lever if (!result) 25415ce4a0cSChuck Lever result = dreq->error; 255bc0fb201SChuck Lever 256bc0fb201SChuck Lever out: 257bc0fb201SChuck Lever return (ssize_t) result; 258bc0fb201SChuck Lever } 259bc0fb201SChuck Lever 260bc0fb201SChuck Lever /* 261607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 262607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 26363ab46abSChuck Lever */ 264f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq) 26563ab46abSChuck Lever { 2669811cd57SChristoph Hellwig struct inode *inode = dreq->inode; 2679811cd57SChristoph Hellwig 26865caafd0SOlga Kornievskaia inode_dio_end(inode); 26965caafd0SOlga Kornievskaia 2702a009ec9SChristoph Hellwig if (dreq->iocb) { 2712a009ec9SChristoph Hellwig long res = (long) dreq->error; 272d2a7de0bSTrond Myklebust if (dreq->count != 0) { 2732a009ec9SChristoph Hellwig res = (long) dreq->count; 274d2a7de0bSTrond Myklebust WARN_ON_ONCE(dreq->count < 0); 275d2a7de0bSTrond Myklebust } 2766b19b766SJens Axboe dreq->iocb->ki_complete(dreq->iocb, res); 277d72b7a6bSTrond Myklebust } 2782a009ec9SChristoph Hellwig 279024de8f1SDaniel Wagner complete(&dreq->completion); 28063ab46abSChuck Lever 281b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 28263ab46abSChuck Lever } 28363ab46abSChuck Lever 284584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) 285fdd1e74cSTrond Myklebust { 286584aa810SFred Isaman unsigned long bytes = 0; 287584aa810SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 288fdd1e74cSTrond Myklebust 28915ce4a0cSChuck Lever spin_lock(&dreq->lock); 290eb2c50daSTrond Myklebust if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) { 291eb2c50daSTrond Myklebust spin_unlock(&dreq->lock); 292eb2c50daSTrond Myklebust goto out_put; 293eb2c50daSTrond Myklebust } 294eb2c50daSTrond Myklebust 295031d73edSTrond Myklebust nfs_direct_count_bytes(dreq, hdr); 29615ce4a0cSChuck Lever spin_unlock(&dreq->lock); 2971da177e4SLinus Torvalds 298584aa810SFred Isaman while (!list_empty(&hdr->pages)) { 299584aa810SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 300584aa810SFred Isaman struct page *page = req->wb_page; 301584aa810SFred Isaman 302ad3cba22SDave Kleikamp if (!PageCompound(page) && bytes < hdr->good_bytes && 303ad3cba22SDave Kleikamp (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY)) 3044bd8b010STrond Myklebust set_page_dirty(page); 305584aa810SFred Isaman bytes += req->wb_bytes; 306584aa810SFred Isaman nfs_list_remove_request(req); 307beeb5338SAnna Schumaker nfs_release_request(req); 308584aa810SFred Isaman } 309584aa810SFred Isaman out_put: 310607f31e8STrond Myklebust if (put_dreq(dreq)) 311f7b5c340STrond Myklebust nfs_direct_complete(dreq); 312584aa810SFred Isaman hdr->release(hdr); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 315df3accb8STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head, int error) 316cd841605SFred Isaman { 317584aa810SFred Isaman struct nfs_page *req; 318cd841605SFred Isaman 319584aa810SFred Isaman while (!list_empty(head)) { 320584aa810SFred Isaman req = nfs_list_entry(head->next); 321584aa810SFred Isaman nfs_list_remove_request(req); 322584aa810SFred Isaman nfs_release_request(req); 323cd841605SFred Isaman } 324584aa810SFred Isaman } 325584aa810SFred Isaman 326584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) 327584aa810SFred Isaman { 328584aa810SFred Isaman get_dreq(hdr->dreq); 329584aa810SFred Isaman } 330584aa810SFred Isaman 331584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { 3323e9e0ca3STrond Myklebust .error_cleanup = nfs_read_sync_pgio_error, 333584aa810SFred Isaman .init_hdr = nfs_direct_pgio_init, 334584aa810SFred Isaman .completion = nfs_direct_read_completion, 335584aa810SFred Isaman }; 336cd841605SFred Isaman 337d4cc948bSChuck Lever /* 338607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 339607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 340607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 341607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 342607f31e8STrond Myklebust * no requests have been sent, just return an error. 3431da177e4SLinus Torvalds */ 34491f79c43SAl Viro 34591f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 34691f79c43SAl Viro struct iov_iter *iter, 34791f79c43SAl Viro loff_t pos) 3481da177e4SLinus Torvalds { 34991f79c43SAl Viro struct nfs_pageio_descriptor desc; 35091f79c43SAl Viro struct inode *inode = dreq->inode; 35191f79c43SAl Viro ssize_t result = -EINVAL; 35291f79c43SAl Viro size_t requested_bytes = 0; 35391f79c43SAl Viro size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); 35482b145c5SChuck Lever 35516b90578SLinus Torvalds nfs_pageio_init_read(&desc, dreq->inode, false, 35691f79c43SAl Viro &nfs_direct_read_completion_ops); 35791f79c43SAl Viro get_dreq(dreq); 35891f79c43SAl Viro desc.pg_dreq = dreq; 359fe0f07d0SJens Axboe inode_dio_begin(inode); 36091f79c43SAl Viro 36191f79c43SAl Viro while (iov_iter_count(iter)) { 36291f79c43SAl Viro struct page **pagevec; 3635dd602f2SChuck Lever size_t bytes; 36491f79c43SAl Viro size_t pgbase; 36591f79c43SAl Viro unsigned npages, i; 3661da177e4SLinus Torvalds 367*1ef255e2SAl Viro result = iov_iter_get_pages_alloc2(iter, &pagevec, 36891f79c43SAl Viro rsize, &pgbase); 369584aa810SFred Isaman if (result < 0) 370749e146eSChuck Lever break; 371a564b8f0SMel Gorman 37291f79c43SAl Viro bytes = result; 37391f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 374584aa810SFred Isaman for (i = 0; i < npages; i++) { 375584aa810SFred Isaman struct nfs_page *req; 376bf5fc402STrond Myklebust unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 377584aa810SFred Isaman /* XXX do we need to do the eof zeroing found in async_filler? */ 37828b1d3f5STrond Myklebust req = nfs_create_request(dreq->ctx, pagevec[i], 379584aa810SFred Isaman pgbase, req_len); 380584aa810SFred Isaman if (IS_ERR(req)) { 381584aa810SFred Isaman result = PTR_ERR(req); 382dbae4c73STrond Myklebust break; 383584aa810SFred Isaman } 384584aa810SFred Isaman req->wb_index = pos >> PAGE_SHIFT; 385584aa810SFred Isaman req->wb_offset = pos & ~PAGE_MASK; 38691f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 38791f79c43SAl Viro result = desc.pg_error; 388584aa810SFred Isaman nfs_release_request(req); 389584aa810SFred Isaman break; 390584aa810SFred Isaman } 391584aa810SFred Isaman pgbase = 0; 392584aa810SFred Isaman bytes -= req_len; 39391f79c43SAl Viro requested_bytes += req_len; 394584aa810SFred Isaman pos += req_len; 39535754bc0SPeng Tao dreq->bytes_left -= req_len; 396584aa810SFred Isaman } 3976d74743bSTrond Myklebust nfs_direct_release_pages(pagevec, npages); 39891f79c43SAl Viro kvfree(pagevec); 39919f73787SChuck Lever if (result < 0) 40019f73787SChuck Lever break; 40119f73787SChuck Lever } 40219f73787SChuck Lever 403584aa810SFred Isaman nfs_pageio_complete(&desc); 404584aa810SFred Isaman 405839f7ad6SChuck Lever /* 406839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 407839f7ad6SChuck Lever * generic layer handle the completion. 408839f7ad6SChuck Lever */ 409839f7ad6SChuck Lever if (requested_bytes == 0) { 410d03727b2SOlga Kornievskaia inode_dio_end(inode); 41165caafd0SOlga Kornievskaia nfs_direct_req_release(dreq); 412839f7ad6SChuck Lever return result < 0 ? result : -EIO; 413839f7ad6SChuck Lever } 414839f7ad6SChuck Lever 41519f73787SChuck Lever if (put_dreq(dreq)) 416f7b5c340STrond Myklebust nfs_direct_complete(dreq); 41785128b2bSAl Viro return requested_bytes; 41819f73787SChuck Lever } 41919f73787SChuck Lever 42014a3ec79SChristoph Hellwig /** 42114a3ec79SChristoph Hellwig * nfs_file_direct_read - file direct read operation for NFS files 42214a3ec79SChristoph Hellwig * @iocb: target I/O control block 423619d30b4SAl Viro * @iter: vector of user buffers into which to read data 42464158668SNeilBrown * @swap: flag indicating this is swap IO, not O_DIRECT IO 42514a3ec79SChristoph Hellwig * 42614a3ec79SChristoph Hellwig * We use this function for direct reads instead of calling 42714a3ec79SChristoph Hellwig * generic_file_aio_read() in order to avoid gfar's check to see if 42814a3ec79SChristoph Hellwig * the request starts before the end of the file. For that check 42914a3ec79SChristoph Hellwig * to work, we must generate a GETATTR before each direct read, and 43014a3ec79SChristoph Hellwig * even then there is a window between the GETATTR and the subsequent 43114a3ec79SChristoph Hellwig * READ where the file size could change. Our preference is simply 43214a3ec79SChristoph Hellwig * to do all reads the application wants, and the server will take 43314a3ec79SChristoph Hellwig * care of managing the end of file boundary. 43414a3ec79SChristoph Hellwig * 43514a3ec79SChristoph Hellwig * This function also eliminates unnecessarily updating the file's 43614a3ec79SChristoph Hellwig * atime locally, as the NFS server sets the file's atime, and this 43714a3ec79SChristoph Hellwig * client must read the updated atime from the server back into its 43814a3ec79SChristoph Hellwig * cache. 43914a3ec79SChristoph Hellwig */ 44064158668SNeilBrown ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter, 44164158668SNeilBrown bool swap) 4421da177e4SLinus Torvalds { 44314a3ec79SChristoph Hellwig struct file *file = iocb->ki_filp; 44414a3ec79SChristoph Hellwig struct address_space *mapping = file->f_mapping; 44514a3ec79SChristoph Hellwig struct inode *inode = mapping->host; 4461da177e4SLinus Torvalds struct nfs_direct_req *dreq; 447b3c54de6STrond Myklebust struct nfs_lock_context *l_ctx; 44886b93667SColin Ian King ssize_t result, requested; 449a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 45014a3ec79SChristoph Hellwig nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 45114a3ec79SChristoph Hellwig 45214a3ec79SChristoph Hellwig dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", 453c8b8e32dSChristoph Hellwig file, count, (long long) iocb->ki_pos); 45414a3ec79SChristoph Hellwig 45514a3ec79SChristoph Hellwig result = 0; 45614a3ec79SChristoph Hellwig if (!count) 45714a3ec79SChristoph Hellwig goto out; 45814a3ec79SChristoph Hellwig 45914a3ec79SChristoph Hellwig task_io_account_read(count); 46014a3ec79SChristoph Hellwig 46114a3ec79SChristoph Hellwig result = -ENOMEM; 462607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 463f11ac8dbSTrond Myklebust if (dreq == NULL) 464a5864c99STrond Myklebust goto out; 4651da177e4SLinus Torvalds 46691d5b470SChuck Lever dreq->inode = inode; 467ed3743a6SWeston Andros Adamson dreq->bytes_left = dreq->max_count = count; 468c8b8e32dSChristoph Hellwig dreq->io_start = iocb->ki_pos; 469cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 470b3c54de6STrond Myklebust l_ctx = nfs_get_lock_context(dreq->ctx); 471b3c54de6STrond Myklebust if (IS_ERR(l_ctx)) { 472b3c54de6STrond Myklebust result = PTR_ERR(l_ctx); 4738605cf0eSMisono Tomohiro nfs_direct_req_release(dreq); 474f11ac8dbSTrond Myklebust goto out_release; 475b3c54de6STrond Myklebust } 476b3c54de6STrond Myklebust dreq->l_ctx = l_ctx; 477487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 478487b8372SChuck Lever dreq->iocb = iocb; 4791da177e4SLinus Torvalds 480fcb14cb1SAl Viro if (user_backed_iter(iter)) 481ad3cba22SDave Kleikamp dreq->flags = NFS_ODIRECT_SHOULD_DIRTY; 482ad3cba22SDave Kleikamp 48364158668SNeilBrown if (!swap) 484a5864c99STrond Myklebust nfs_start_io_direct(inode); 485a5864c99STrond Myklebust 486619d30b4SAl Viro NFS_I(inode)->read_io += count; 48785128b2bSAl Viro requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos); 488d0b9875dSChristoph Hellwig 48964158668SNeilBrown if (!swap) 490a5864c99STrond Myklebust nfs_end_io_direct(inode); 491d0b9875dSChristoph Hellwig 49285128b2bSAl Viro if (requested > 0) { 493bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 49485128b2bSAl Viro if (result > 0) { 49585128b2bSAl Viro requested -= result; 496c8b8e32dSChristoph Hellwig iocb->ki_pos += result; 49714a3ec79SChristoph Hellwig } 49885128b2bSAl Viro iov_iter_revert(iter, requested); 49985128b2bSAl Viro } else { 50085128b2bSAl Viro result = requested; 50185128b2bSAl Viro } 502d0b9875dSChristoph Hellwig 503f11ac8dbSTrond Myklebust out_release: 504b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 505f11ac8dbSTrond Myklebust out: 5061da177e4SLinus Torvalds return result; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 509085d1e33STom Haynes static void 510ed5d588fSTrond Myklebust nfs_direct_join_group(struct list_head *list, struct inode *inode) 511ed5d588fSTrond Myklebust { 512ed5d588fSTrond Myklebust struct nfs_page *req, *next; 513ed5d588fSTrond Myklebust 514ed5d588fSTrond Myklebust list_for_each_entry(req, list, wb_list) { 515ed5d588fSTrond Myklebust if (req->wb_head != req || req->wb_this_page == req) 516ed5d588fSTrond Myklebust continue; 517ed5d588fSTrond Myklebust for (next = req->wb_this_page; 518ed5d588fSTrond Myklebust next != req->wb_head; 519ed5d588fSTrond Myklebust next = next->wb_this_page) { 520ed5d588fSTrond Myklebust nfs_list_remove_request(next); 521ed5d588fSTrond Myklebust nfs_release_request(next); 522ed5d588fSTrond Myklebust } 523ed5d588fSTrond Myklebust nfs_join_page_group(req, inode); 524ed5d588fSTrond Myklebust } 525ed5d588fSTrond Myklebust } 526ed5d588fSTrond Myklebust 527ed5d588fSTrond Myklebust static void 528085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode, 529085d1e33STom Haynes struct list_head *list, 530085d1e33STom Haynes struct nfs_commit_info *cinfo) 531085d1e33STom Haynes { 532e824f99aSTrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 5339c455a8cSTrond Myklebust pnfs_recover_commit_reqs(list, cinfo); 534085d1e33STom Haynes nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0); 535e824f99aSTrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 536085d1e33STom Haynes } 537085d1e33STom Haynes 538fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 5391da177e4SLinus Torvalds { 5401763da12SFred Isaman struct nfs_pageio_descriptor desc; 5411763da12SFred Isaman struct nfs_page *req, *tmp; 5421763da12SFred Isaman LIST_HEAD(reqs); 5431763da12SFred Isaman struct nfs_commit_info cinfo; 5441763da12SFred Isaman LIST_HEAD(failed); 5451763da12SFred Isaman 5461763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 547085d1e33STom Haynes nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); 5481da177e4SLinus Torvalds 549ed5d588fSTrond Myklebust nfs_direct_join_group(&reqs, dreq->inode); 550ed5d588fSTrond Myklebust 551fad61490STrond Myklebust dreq->count = 0; 552031d73edSTrond Myklebust dreq->max_count = 0; 553031d73edSTrond Myklebust list_for_each_entry(req, &reqs, wb_list) 554031d73edSTrond Myklebust dreq->max_count += req->wb_bytes; 555a5314a74STrond Myklebust nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo); 556607f31e8STrond Myklebust get_dreq(dreq); 5571da177e4SLinus Torvalds 558a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false, 5591763da12SFred Isaman &nfs_direct_write_completion_ops); 5601763da12SFred Isaman desc.pg_dreq = dreq; 561607f31e8STrond Myklebust 5621763da12SFred Isaman list_for_each_entry_safe(req, tmp, &reqs, wb_list) { 56333344e0fSTrond Myklebust /* Bump the transmission count */ 56433344e0fSTrond Myklebust req->wb_nio++; 5651763da12SFred Isaman if (!nfs_pageio_add_request(&desc, req)) { 566078b5fd9STrond Myklebust nfs_list_move_request(req, &failed); 567fe238e60SDave Wysochanski spin_lock(&cinfo.inode->i_lock); 5681763da12SFred Isaman dreq->flags = 0; 569d600ad1fSPeng Tao if (desc.pg_error < 0) 570d600ad1fSPeng Tao dreq->error = desc.pg_error; 571d600ad1fSPeng Tao else 5721763da12SFred Isaman dreq->error = -EIO; 573fe238e60SDave Wysochanski spin_unlock(&cinfo.inode->i_lock); 5741763da12SFred Isaman } 5755a695da2STrond Myklebust nfs_release_request(req); 5761763da12SFred Isaman } 5771763da12SFred Isaman nfs_pageio_complete(&desc); 578607f31e8STrond Myklebust 5794035c248STrond Myklebust while (!list_empty(&failed)) { 5804035c248STrond Myklebust req = nfs_list_entry(failed.next); 5814035c248STrond Myklebust nfs_list_remove_request(req); 5821d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 5834035c248STrond Myklebust } 584607f31e8STrond Myklebust 585607f31e8STrond Myklebust if (put_dreq(dreq)) 5864d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 587fad61490STrond Myklebust } 5881da177e4SLinus Torvalds 5891763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data) 590fad61490STrond Myklebust { 5911f28476dSTrond Myklebust const struct nfs_writeverf *verf = data->res.verf; 5920b7c0153SFred Isaman struct nfs_direct_req *dreq = data->dreq; 5931763da12SFred Isaman struct nfs_commit_info cinfo; 5941763da12SFred Isaman struct nfs_page *req; 595c9d8f89dSTrond Myklebust int status = data->task.tk_status; 596c9d8f89dSTrond Myklebust 597fb5f7f20STrond Myklebust if (status < 0) { 598fb5f7f20STrond Myklebust /* Errors in commit are fatal */ 599fb5f7f20STrond Myklebust dreq->error = status; 600fb5f7f20STrond Myklebust dreq->max_count = 0; 601fb5f7f20STrond Myklebust dreq->count = 0; 602fb5f7f20STrond Myklebust dreq->flags = NFS_ODIRECT_DONE; 603fb5f7f20STrond Myklebust } else if (dreq->flags == NFS_ODIRECT_DONE) 604fb5f7f20STrond Myklebust status = dreq->error; 605fb5f7f20STrond Myklebust 6061763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 607fad61490STrond Myklebust 6081763da12SFred Isaman while (!list_empty(&data->pages)) { 6091763da12SFred Isaman req = nfs_list_entry(data->pages.next); 6101763da12SFred Isaman nfs_list_remove_request(req); 6111f28476dSTrond Myklebust if (status >= 0 && !nfs_write_match_verf(verf, req)) { 6121f28476dSTrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 61333344e0fSTrond Myklebust /* 61433344e0fSTrond Myklebust * Despite the reboot, the write was successful, 61533344e0fSTrond Myklebust * so reset wb_nio. 61633344e0fSTrond Myklebust */ 61733344e0fSTrond Myklebust req->wb_nio = 0; 618b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, NULL, &cinfo, 0); 6191f28476dSTrond Myklebust } else /* Error or match */ 620906369e4SFred Isaman nfs_release_request(req); 6211d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 622fad61490STrond Myklebust } 623fad61490STrond Myklebust 624133a48abSTrond Myklebust if (nfs_commit_end(cinfo.mds)) 6254d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 6261763da12SFred Isaman } 6271763da12SFred Isaman 628b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo, 629b20135d0STrond Myklebust struct nfs_page *req) 6301763da12SFred Isaman { 631b20135d0STrond Myklebust struct nfs_direct_req *dreq = cinfo->dreq; 632b20135d0STrond Myklebust 633b20135d0STrond Myklebust spin_lock(&dreq->lock); 634fb5f7f20STrond Myklebust if (dreq->flags != NFS_ODIRECT_DONE) 635b20135d0STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 636b20135d0STrond Myklebust spin_unlock(&dreq->lock); 637b20135d0STrond Myklebust nfs_mark_request_commit(req, NULL, cinfo, 0); 6381763da12SFred Isaman } 6391763da12SFred Isaman 6401763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { 6411763da12SFred Isaman .completion = nfs_direct_commit_complete, 642b20135d0STrond Myklebust .resched_write = nfs_direct_resched_write, 643fad61490STrond Myklebust }; 644fad61490STrond Myklebust 645fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 646fad61490STrond Myklebust { 6471763da12SFred Isaman int res; 6481763da12SFred Isaman struct nfs_commit_info cinfo; 6491763da12SFred Isaman LIST_HEAD(mds_list); 650fad61490STrond Myklebust 6511763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 6521763da12SFred Isaman nfs_scan_commit(dreq->inode, &mds_list, &cinfo); 6531763da12SFred Isaman res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); 6541763da12SFred Isaman if (res < 0) /* res == -ENOMEM */ 6551763da12SFred Isaman nfs_direct_write_reschedule(dreq); 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 658fb5f7f20STrond Myklebust static void nfs_direct_write_clear_reqs(struct nfs_direct_req *dreq) 659fb5f7f20STrond Myklebust { 660fb5f7f20STrond Myklebust struct nfs_commit_info cinfo; 661fb5f7f20STrond Myklebust struct nfs_page *req; 662fb5f7f20STrond Myklebust LIST_HEAD(reqs); 663fb5f7f20STrond Myklebust 664fb5f7f20STrond Myklebust nfs_init_cinfo_from_dreq(&cinfo, dreq); 665fb5f7f20STrond Myklebust nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); 666fb5f7f20STrond Myklebust 667fb5f7f20STrond Myklebust while (!list_empty(&reqs)) { 668fb5f7f20STrond Myklebust req = nfs_list_entry(reqs.next); 669fb5f7f20STrond Myklebust nfs_list_remove_request(req); 670f02cec9dSTrond Myklebust nfs_release_request(req); 671fb5f7f20STrond Myklebust nfs_unlock_and_release_request(req); 672fb5f7f20STrond Myklebust } 673fb5f7f20STrond Myklebust } 674fb5f7f20STrond Myklebust 6751763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work) 6761da177e4SLinus Torvalds { 6771763da12SFred Isaman struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); 678fad61490STrond Myklebust int flags = dreq->flags; 6791da177e4SLinus Torvalds 680fad61490STrond Myklebust dreq->flags = 0; 681fad61490STrond Myklebust switch (flags) { 682fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 683fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 6841da177e4SLinus Torvalds break; 685fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 686fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 6871da177e4SLinus Torvalds break; 6881da177e4SLinus Torvalds default: 689fb5f7f20STrond Myklebust nfs_direct_write_clear_reqs(dreq); 690f7b5c340STrond Myklebust nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping); 691f7b5c340STrond Myklebust nfs_direct_complete(dreq); 6921da177e4SLinus Torvalds } 693fad61490STrond Myklebust } 694fad61490STrond Myklebust 6954d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq) 696fad61490STrond Myklebust { 69746483c2eSNeilBrown queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */ 698fad61490STrond Myklebust } 6991763da12SFred Isaman 7001763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) 7011763da12SFred Isaman { 7021763da12SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 7031763da12SFred Isaman struct nfs_commit_info cinfo; 7041763da12SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 7053731d44bSTrond Myklebust int flags = NFS_ODIRECT_DONE; 7061763da12SFred Isaman 7071763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 7081763da12SFred Isaman 7091763da12SFred Isaman spin_lock(&dreq->lock); 710eb2c50daSTrond Myklebust if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) { 711eb2c50daSTrond Myklebust spin_unlock(&dreq->lock); 712eb2c50daSTrond Myklebust goto out_put; 713eb2c50daSTrond Myklebust } 714eb2c50daSTrond Myklebust 715031d73edSTrond Myklebust nfs_direct_count_bytes(dreq, hdr); 7161f28476dSTrond Myklebust if (hdr->good_bytes != 0 && nfs_write_need_commit(hdr)) { 7173731d44bSTrond Myklebust if (!dreq->flags) 7181763da12SFred Isaman dreq->flags = NFS_ODIRECT_DO_COMMIT; 7193731d44bSTrond Myklebust flags = dreq->flags; 7201763da12SFred Isaman } 7211763da12SFred Isaman spin_unlock(&dreq->lock); 7221763da12SFred Isaman 7231763da12SFred Isaman while (!list_empty(&hdr->pages)) { 7242bfc6e56SWeston Andros Adamson 7251763da12SFred Isaman req = nfs_list_entry(hdr->pages.next); 7261763da12SFred Isaman nfs_list_remove_request(req); 7273731d44bSTrond Myklebust if (flags == NFS_ODIRECT_DO_COMMIT) { 72804277086STrond Myklebust kref_get(&req->wb_kref); 729ba838a75SChuck Lever memcpy(&req->wb_verf, &hdr->verf.verifier, 730ba838a75SChuck Lever sizeof(req->wb_verf)); 731b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, hdr->lseg, &cinfo, 732b57ff130SWeston Andros Adamson hdr->ds_commit_idx); 7333731d44bSTrond Myklebust } else if (flags == NFS_ODIRECT_RESCHED_WRITES) { 7343731d44bSTrond Myklebust kref_get(&req->wb_kref); 7353731d44bSTrond Myklebust nfs_mark_request_commit(req, NULL, &cinfo, 0); 7361763da12SFred Isaman } 7371d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 7381763da12SFred Isaman } 7391763da12SFred Isaman 7401763da12SFred Isaman out_put: 7411763da12SFred Isaman if (put_dreq(dreq)) 7424d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 7431763da12SFred Isaman hdr->release(hdr); 7441763da12SFred Isaman } 7451763da12SFred Isaman 746df3accb8STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head, int error) 7473e9e0ca3STrond Myklebust { 7483e9e0ca3STrond Myklebust struct nfs_page *req; 7493e9e0ca3STrond Myklebust 7503e9e0ca3STrond Myklebust while (!list_empty(head)) { 7513e9e0ca3STrond Myklebust req = nfs_list_entry(head->next); 7523e9e0ca3STrond Myklebust nfs_list_remove_request(req); 7531d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 7543e9e0ca3STrond Myklebust } 7553e9e0ca3STrond Myklebust } 7563e9e0ca3STrond Myklebust 757dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr) 758dc602dd7STrond Myklebust { 759dc602dd7STrond Myklebust struct nfs_direct_req *dreq = hdr->dreq; 760dc602dd7STrond Myklebust 761dc602dd7STrond Myklebust spin_lock(&dreq->lock); 762dc602dd7STrond Myklebust if (dreq->error == 0) { 763dc602dd7STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 764dc602dd7STrond Myklebust /* fake unstable write to let common nfs resend pages */ 765dc602dd7STrond Myklebust hdr->verf.committed = NFS_UNSTABLE; 7664daaeba9STrond Myklebust hdr->good_bytes = hdr->args.offset + hdr->args.count - 7674daaeba9STrond Myklebust hdr->io_start; 768dc602dd7STrond Myklebust } 769dc602dd7STrond Myklebust spin_unlock(&dreq->lock); 770dc602dd7STrond Myklebust } 771dc602dd7STrond Myklebust 7721763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { 7733e9e0ca3STrond Myklebust .error_cleanup = nfs_write_sync_pgio_error, 7741763da12SFred Isaman .init_hdr = nfs_direct_pgio_init, 7751763da12SFred Isaman .completion = nfs_direct_write_completion, 776dc602dd7STrond Myklebust .reschedule_io = nfs_direct_write_reschedule_io, 7771763da12SFred Isaman }; 7781763da12SFred Isaman 77991f79c43SAl Viro 78091f79c43SAl Viro /* 78191f79c43SAl Viro * NB: Return the value of the first error return code. Subsequent 78291f79c43SAl Viro * errors after the first one are ignored. 78391f79c43SAl Viro */ 78491f79c43SAl Viro /* 78591f79c43SAl Viro * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 78691f79c43SAl Viro * operation. If nfs_writedata_alloc() or get_user_pages() fails, 78791f79c43SAl Viro * bail and stop sending more writes. Write length accounting is 78891f79c43SAl Viro * handled automatically by nfs_direct_write_result(). Otherwise, if 78991f79c43SAl Viro * no requests have been sent, just return an error. 79091f79c43SAl Viro */ 79119f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 792619d30b4SAl Viro struct iov_iter *iter, 793c265de25SNeilBrown loff_t pos, int ioflags) 79419f73787SChuck Lever { 7951763da12SFred Isaman struct nfs_pageio_descriptor desc; 7961d59d61fSTrond Myklebust struct inode *inode = dreq->inode; 79719f73787SChuck Lever ssize_t result = 0; 79819f73787SChuck Lever size_t requested_bytes = 0; 79991f79c43SAl Viro size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); 80019f73787SChuck Lever 801c265de25SNeilBrown nfs_pageio_init_write(&desc, inode, ioflags, false, 8021763da12SFred Isaman &nfs_direct_write_completion_ops); 8031763da12SFred Isaman desc.pg_dreq = dreq; 80419f73787SChuck Lever get_dreq(dreq); 805fe0f07d0SJens Axboe inode_dio_begin(inode); 80619f73787SChuck Lever 80791f79c43SAl Viro NFS_I(inode)->write_io += iov_iter_count(iter); 80891f79c43SAl Viro while (iov_iter_count(iter)) { 80991f79c43SAl Viro struct page **pagevec; 81091f79c43SAl Viro size_t bytes; 81191f79c43SAl Viro size_t pgbase; 81291f79c43SAl Viro unsigned npages, i; 81391f79c43SAl Viro 814*1ef255e2SAl Viro result = iov_iter_get_pages_alloc2(iter, &pagevec, 81591f79c43SAl Viro wsize, &pgbase); 81619f73787SChuck Lever if (result < 0) 81719f73787SChuck Lever break; 81891f79c43SAl Viro 81991f79c43SAl Viro bytes = result; 82091f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 82191f79c43SAl Viro for (i = 0; i < npages; i++) { 82291f79c43SAl Viro struct nfs_page *req; 82391f79c43SAl Viro unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 82491f79c43SAl Viro 82528b1d3f5STrond Myklebust req = nfs_create_request(dreq->ctx, pagevec[i], 82691f79c43SAl Viro pgbase, req_len); 82791f79c43SAl Viro if (IS_ERR(req)) { 82891f79c43SAl Viro result = PTR_ERR(req); 82919f73787SChuck Lever break; 83091f79c43SAl Viro } 8310a00b77bSWeston Andros Adamson 832d600ad1fSPeng Tao if (desc.pg_error < 0) { 833d600ad1fSPeng Tao nfs_free_request(req); 834d600ad1fSPeng Tao result = desc.pg_error; 835d600ad1fSPeng Tao break; 836d600ad1fSPeng Tao } 8370a00b77bSWeston Andros Adamson 83891f79c43SAl Viro nfs_lock_request(req); 83991f79c43SAl Viro req->wb_index = pos >> PAGE_SHIFT; 84091f79c43SAl Viro req->wb_offset = pos & ~PAGE_MASK; 84191f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 84291f79c43SAl Viro result = desc.pg_error; 84391f79c43SAl Viro nfs_unlock_and_release_request(req); 84491f79c43SAl Viro break; 84591f79c43SAl Viro } 84691f79c43SAl Viro pgbase = 0; 84791f79c43SAl Viro bytes -= req_len; 84891f79c43SAl Viro requested_bytes += req_len; 84991f79c43SAl Viro pos += req_len; 85091f79c43SAl Viro dreq->bytes_left -= req_len; 85191f79c43SAl Viro } 85291f79c43SAl Viro nfs_direct_release_pages(pagevec, npages); 85391f79c43SAl Viro kvfree(pagevec); 85491f79c43SAl Viro if (result < 0) 85591f79c43SAl Viro break; 85619f73787SChuck Lever } 8571763da12SFred Isaman nfs_pageio_complete(&desc); 85819f73787SChuck Lever 859839f7ad6SChuck Lever /* 860839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 861839f7ad6SChuck Lever * generic layer handle the completion. 862839f7ad6SChuck Lever */ 863839f7ad6SChuck Lever if (requested_bytes == 0) { 864d03727b2SOlga Kornievskaia inode_dio_end(inode); 86565caafd0SOlga Kornievskaia nfs_direct_req_release(dreq); 866839f7ad6SChuck Lever return result < 0 ? result : -EIO; 867839f7ad6SChuck Lever } 868839f7ad6SChuck Lever 86919f73787SChuck Lever if (put_dreq(dreq)) 8704d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 87185128b2bSAl Viro return requested_bytes; 87219f73787SChuck Lever } 87319f73787SChuck Lever 8741da177e4SLinus Torvalds /** 8751da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 8761da177e4SLinus Torvalds * @iocb: target I/O control block 877619d30b4SAl Viro * @iter: vector of user buffers from which to write data 87864158668SNeilBrown * @swap: flag indicating this is swap IO, not O_DIRECT IO 8791da177e4SLinus Torvalds * 8801da177e4SLinus Torvalds * We use this function for direct writes instead of calling 8811da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 8821da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 8831da177e4SLinus Torvalds * the new i_size and this client must read the updated size 8841da177e4SLinus Torvalds * back into its cache. We let the server do generic write 8851da177e4SLinus Torvalds * parameter checking and report problems. 8861da177e4SLinus Torvalds * 8871da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8881da177e4SLinus Torvalds * 8891da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8901da177e4SLinus Torvalds * readers of this file. 8911da177e4SLinus Torvalds * 8921da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8931da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8941da177e4SLinus Torvalds */ 89564158668SNeilBrown ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, 89664158668SNeilBrown bool swap) 8971da177e4SLinus Torvalds { 8989a74a2b8SColin Ian King ssize_t result, requested; 89989698b24STrond Myklebust size_t count; 9001da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9011da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 90222cd1bf1SChristoph Hellwig struct inode *inode = mapping->host; 90322cd1bf1SChristoph Hellwig struct nfs_direct_req *dreq; 90422cd1bf1SChristoph Hellwig struct nfs_lock_context *l_ctx; 90565a4a1caSAl Viro loff_t pos, end; 906c216fd70SChuck Lever 9076de1472fSAl Viro dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", 9083309dd04SAl Viro file, iov_iter_count(iter), (long long) iocb->ki_pos); 909027445c3SBadari Pulavarty 91064158668SNeilBrown if (swap) 91164158668SNeilBrown /* bypass generic checks */ 91264158668SNeilBrown result = iov_iter_count(iter); 91364158668SNeilBrown else 91489698b24STrond Myklebust result = generic_write_checks(iocb, iter); 91589698b24STrond Myklebust if (result <= 0) 91689698b24STrond Myklebust return result; 91789698b24STrond Myklebust count = result; 91889698b24STrond Myklebust nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 9193309dd04SAl Viro 9203309dd04SAl Viro pos = iocb->ki_pos; 92109cbfeafSKirill A. Shutemov end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT; 922ce1a8e67SChuck Lever 92389698b24STrond Myklebust task_io_account_write(count); 9247ec10f26SKonstantin Khlebnikov 92522cd1bf1SChristoph Hellwig result = -ENOMEM; 92622cd1bf1SChristoph Hellwig dreq = nfs_direct_req_alloc(); 92722cd1bf1SChristoph Hellwig if (!dreq) 928a5864c99STrond Myklebust goto out; 92922cd1bf1SChristoph Hellwig 93022cd1bf1SChristoph Hellwig dreq->inode = inode; 93189698b24STrond Myklebust dreq->bytes_left = dreq->max_count = count; 9325fadeb47SPeng Tao dreq->io_start = pos; 93322cd1bf1SChristoph Hellwig dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 93422cd1bf1SChristoph Hellwig l_ctx = nfs_get_lock_context(dreq->ctx); 93522cd1bf1SChristoph Hellwig if (IS_ERR(l_ctx)) { 93622cd1bf1SChristoph Hellwig result = PTR_ERR(l_ctx); 9378605cf0eSMisono Tomohiro nfs_direct_req_release(dreq); 93822cd1bf1SChristoph Hellwig goto out_release; 93922cd1bf1SChristoph Hellwig } 94022cd1bf1SChristoph Hellwig dreq->l_ctx = l_ctx; 94122cd1bf1SChristoph Hellwig if (!is_sync_kiocb(iocb)) 94222cd1bf1SChristoph Hellwig dreq->iocb = iocb; 9439c455a8cSTrond Myklebust pnfs_init_ds_commit_info_ops(&dreq->ds_cinfo, inode); 94422cd1bf1SChristoph Hellwig 94564158668SNeilBrown if (swap) { 946c265de25SNeilBrown requested = nfs_direct_write_schedule_iovec(dreq, iter, pos, 947c265de25SNeilBrown FLUSH_STABLE); 94864158668SNeilBrown } else { 949a5864c99STrond Myklebust nfs_start_io_direct(inode); 950a5864c99STrond Myklebust 951c265de25SNeilBrown requested = nfs_direct_write_schedule_iovec(dreq, iter, pos, 952c265de25SNeilBrown FLUSH_COND_STABLE); 953a9ab5e84SChristoph Hellwig 954a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 955a9ab5e84SChristoph Hellwig invalidate_inode_pages2_range(mapping, 95609cbfeafSKirill A. Shutemov pos >> PAGE_SHIFT, end); 957a9ab5e84SChristoph Hellwig } 958a9ab5e84SChristoph Hellwig 959a5864c99STrond Myklebust nfs_end_io_direct(inode); 96064158668SNeilBrown } 961a9ab5e84SChristoph Hellwig 96285128b2bSAl Viro if (requested > 0) { 96322cd1bf1SChristoph Hellwig result = nfs_direct_wait(dreq); 96422cd1bf1SChristoph Hellwig if (result > 0) { 96585128b2bSAl Viro requested -= result; 96622cd1bf1SChristoph Hellwig iocb->ki_pos = pos + result; 967e2592217SChristoph Hellwig /* XXX: should check the generic_write_sync retval */ 968e2592217SChristoph Hellwig generic_write_sync(iocb, result); 9691763da12SFred Isaman } 97085128b2bSAl Viro iov_iter_revert(iter, requested); 97185128b2bSAl Viro } else { 97285128b2bSAl Viro result = requested; 97322cd1bf1SChristoph Hellwig } 974a6b5a28eSDave Wysochanski nfs_fscache_invalidate(inode, FSCACHE_INVAL_DIO_WRITE); 97522cd1bf1SChristoph Hellwig out_release: 97622cd1bf1SChristoph Hellwig nfs_direct_req_release(dreq); 977a5864c99STrond Myklebust out: 97822cd1bf1SChristoph Hellwig return result; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 98188467055SChuck Lever /** 98288467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 98388467055SChuck Lever * 98488467055SChuck Lever */ 985f7b422b1SDavid Howells int __init nfs_init_directcache(void) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 9881da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 989fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 990fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 99120c2df83SPaul Mundt NULL); 9921da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 9931da177e4SLinus Torvalds return -ENOMEM; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds return 0; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 99888467055SChuck Lever /** 999f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 100088467055SChuck Lever * 100188467055SChuck Lever */ 1002266bee88SDavid Brownell void nfs_destroy_directcache(void) 10031da177e4SLinus Torvalds { 10041a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 10051da177e4SLinus Torvalds } 1006