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> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <linux/nfs_fs.h> 511da177e4SLinus Torvalds #include <linux/nfs_page.h> 521da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #include <asm/uaccess.h> 5560063497SArun Sharma #include <linux/atomic.h> 561da177e4SLinus Torvalds 578d5658c9STrond Myklebust #include "internal.h" 5891d5b470SChuck Lever #include "iostat.h" 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 611da177e4SLinus Torvalds 62e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds struct nfs_direct_req { 681da177e4SLinus Torvalds struct kref kref; /* release manager */ 6915ce4a0cSChuck Lever 7015ce4a0cSChuck Lever /* I/O parameters */ 71a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 72f11ac8dbSTrond Myklebust struct nfs_lock_context *l_ctx; /* Lock context info */ 7399514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 7488467055SChuck Lever struct inode * inode; /* target file of i/o */ 7515ce4a0cSChuck Lever 7615ce4a0cSChuck Lever /* completion state */ 77607f31e8STrond Myklebust atomic_t io_count; /* i/os we're waiting for */ 7815ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 7915ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 801da177e4SLinus Torvalds error; /* any reported error */ 81d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 82fad61490STrond Myklebust 83fad61490STrond Myklebust /* commit state */ 84607f31e8STrond Myklebust struct list_head rewrite_list; /* saved nfs_write_data structs */ 85*0b7c0153SFred Isaman struct nfs_commit_data *commit_data; /* special write_data for commits */ 86fad61490STrond Myklebust int flags; 87fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 88fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 89fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 901da177e4SLinus Torvalds }; 911da177e4SLinus Torvalds 92fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 93607f31e8STrond Myklebust static const struct rpc_call_ops nfs_write_direct_ops; 94607f31e8STrond Myklebust 95607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 96607f31e8STrond Myklebust { 97607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 98607f31e8STrond Myklebust } 99607f31e8STrond Myklebust 100607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 101607f31e8STrond Myklebust { 102607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 103607f31e8STrond Myklebust } 104607f31e8STrond Myklebust 1051da177e4SLinus Torvalds /** 106b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 107b8a32e2bSChuck Lever * @rw: direction (read or write) 108b8a32e2bSChuck Lever * @iocb: target I/O control block 109b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 110b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 111b8a32e2bSChuck Lever * @nr_segs: size of iovec array 112b8a32e2bSChuck Lever * 113b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 114b8a32e2bSChuck Lever * the NFS client supports direct I/O. However, we shunt off direct 115b8a32e2bSChuck Lever * read and write requests before the VFS gets them, so this method 116b8a32e2bSChuck Lever * should never be called. 1171da177e4SLinus Torvalds */ 118b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) 119b8a32e2bSChuck Lever { 120b8a32e2bSChuck Lever dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", 12101cce933SJosef "Jeff" Sipek iocb->ki_filp->f_path.dentry->d_name.name, 122e99170ffSTrond Myklebust (long long) pos, nr_segs); 123b8a32e2bSChuck Lever 124b8a32e2bSChuck Lever return -EINVAL; 125b8a32e2bSChuck Lever } 126b8a32e2bSChuck Lever 127d4a8f367STrond Myklebust static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count) 1286b45d858STrond Myklebust { 129d4a8f367STrond Myklebust unsigned int npages; 130749e146eSChuck Lever unsigned int i; 131d4a8f367STrond Myklebust 132d4a8f367STrond Myklebust if (count == 0) 133d4a8f367STrond Myklebust return; 134d4a8f367STrond Myklebust pages += (pgbase >> PAGE_SHIFT); 135d4a8f367STrond Myklebust npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT; 1366b45d858STrond Myklebust for (i = 0; i < npages; i++) { 1376b45d858STrond Myklebust struct page *page = pages[i]; 138607f31e8STrond Myklebust if (!PageCompound(page)) 139d4a8f367STrond Myklebust set_page_dirty(page); 1406b45d858STrond Myklebust } 1419c93ab7dSChuck Lever } 1429c93ab7dSChuck Lever 143749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 1449c93ab7dSChuck Lever { 145749e146eSChuck Lever unsigned int i; 146607f31e8STrond Myklebust for (i = 0; i < npages; i++) 147607f31e8STrond Myklebust page_cache_release(pages[i]); 1486b45d858STrond Myklebust } 1496b45d858STrond Myklebust 15093619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds struct nfs_direct_req *dreq; 1531da177e4SLinus Torvalds 154e94b1766SChristoph Lameter dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL); 1551da177e4SLinus Torvalds if (!dreq) 1561da177e4SLinus Torvalds return NULL; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds kref_init(&dreq->kref); 159607f31e8STrond Myklebust kref_get(&dreq->kref); 160d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 161fad61490STrond Myklebust INIT_LIST_HEAD(&dreq->rewrite_list); 16293619e59SChuck Lever dreq->iocb = NULL; 163a8881f5aSTrond Myklebust dreq->ctx = NULL; 164f11ac8dbSTrond Myklebust dreq->l_ctx = NULL; 16515ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 166607f31e8STrond Myklebust atomic_set(&dreq->io_count, 0); 16715ce4a0cSChuck Lever dreq->count = 0; 16815ce4a0cSChuck Lever dreq->error = 0; 169fad61490STrond Myklebust dreq->flags = 0; 17093619e59SChuck Lever 17193619e59SChuck Lever return dreq; 17293619e59SChuck Lever } 17393619e59SChuck Lever 174b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 177a8881f5aSTrond Myklebust 178f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 179f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 180a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 181a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 1821da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 185b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 186b4946ffbSTrond Myklebust { 187b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 188b4946ffbSTrond Myklebust } 189b4946ffbSTrond Myklebust 190d4cc948bSChuck Lever /* 191bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 192bc0fb201SChuck Lever */ 193bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 194bc0fb201SChuck Lever { 19515ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 196bc0fb201SChuck Lever 197bc0fb201SChuck Lever /* Async requests don't wait here */ 198bc0fb201SChuck Lever if (dreq->iocb) 199bc0fb201SChuck Lever goto out; 200bc0fb201SChuck Lever 201150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 202bc0fb201SChuck Lever 203bc0fb201SChuck Lever if (!result) 20415ce4a0cSChuck Lever result = dreq->error; 205bc0fb201SChuck Lever if (!result) 20615ce4a0cSChuck Lever result = dreq->count; 207bc0fb201SChuck Lever 208bc0fb201SChuck Lever out: 209bc0fb201SChuck Lever return (ssize_t) result; 210bc0fb201SChuck Lever } 211bc0fb201SChuck Lever 212bc0fb201SChuck Lever /* 213607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 214607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 21563ab46abSChuck Lever */ 21663ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq) 21763ab46abSChuck Lever { 21863ab46abSChuck Lever if (dreq->iocb) { 21915ce4a0cSChuck Lever long res = (long) dreq->error; 22063ab46abSChuck Lever if (!res) 22115ce4a0cSChuck Lever res = (long) dreq->count; 22263ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 223d72b7a6bSTrond Myklebust } 224d72b7a6bSTrond Myklebust complete_all(&dreq->completion); 22563ab46abSChuck Lever 226b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 22763ab46abSChuck Lever } 22863ab46abSChuck Lever 22963ab46abSChuck Lever /* 230607f31e8STrond Myklebust * We must hold a reference to all the pages in this direct read request 231607f31e8STrond Myklebust * until the RPCs complete. This could be long *after* we are woken up in 232607f31e8STrond Myklebust * nfs_direct_wait (for instance, if someone hits ^C on a slow server). 23306cf6f2eSChuck Lever */ 234ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata) 2351da177e4SLinus Torvalds { 236ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 2371da177e4SLinus Torvalds 238fdd1e74cSTrond Myklebust nfs_readpage_result(task, data); 239fdd1e74cSTrond Myklebust } 240fdd1e74cSTrond Myklebust 241fdd1e74cSTrond Myklebust static void nfs_direct_read_release(void *calldata) 242fdd1e74cSTrond Myklebust { 243fdd1e74cSTrond Myklebust 244fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 245fdd1e74cSTrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 246fdd1e74cSTrond Myklebust int status = data->task.tk_status; 2471da177e4SLinus Torvalds 24815ce4a0cSChuck Lever spin_lock(&dreq->lock); 249fdd1e74cSTrond Myklebust if (unlikely(status < 0)) { 250fdd1e74cSTrond Myklebust dreq->error = status; 25115ce4a0cSChuck Lever spin_unlock(&dreq->lock); 252d4a8f367STrond Myklebust } else { 253d4a8f367STrond Myklebust dreq->count += data->res.count; 254d4a8f367STrond Myklebust spin_unlock(&dreq->lock); 255d4a8f367STrond Myklebust nfs_direct_dirty_pages(data->pagevec, 256d4a8f367STrond Myklebust data->args.pgbase, 257d4a8f367STrond Myklebust data->res.count); 258d4a8f367STrond Myklebust } 259d4a8f367STrond Myklebust nfs_direct_release_pages(data->pagevec, data->npages); 2601da177e4SLinus Torvalds 261607f31e8STrond Myklebust if (put_dreq(dreq)) 26263ab46abSChuck Lever nfs_direct_complete(dreq); 263a20c6becSFred Isaman nfs_readdata_release(data); 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 266ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = { 267f11c88afSAndy Adamson .rpc_call_prepare = nfs_read_prepare, 268ec06c096STrond Myklebust .rpc_call_done = nfs_direct_read_result, 269fdd1e74cSTrond Myklebust .rpc_release = nfs_direct_read_release, 270ec06c096STrond Myklebust }; 271ec06c096STrond Myklebust 272d4cc948bSChuck Lever /* 273607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 274607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 275607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 276607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 277607f31e8STrond Myklebust * no requests have been sent, just return an error. 2781da177e4SLinus Torvalds */ 27902fe4946SChuck Lever static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq, 28002fe4946SChuck Lever const struct iovec *iov, 28102fe4946SChuck Lever loff_t pos) 2821da177e4SLinus Torvalds { 283a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 2843d4ff43dSAl Viro struct inode *inode = ctx->dentry->d_inode; 28502fe4946SChuck Lever unsigned long user_addr = (unsigned long)iov->iov_base; 28602fe4946SChuck Lever size_t count = iov->iov_len; 2875dd602f2SChuck Lever size_t rsize = NFS_SERVER(inode)->rsize; 28807737691STrond Myklebust struct rpc_task *task; 289bdc7f021STrond Myklebust struct rpc_message msg = { 290bdc7f021STrond Myklebust .rpc_cred = ctx->cred, 291bdc7f021STrond Myklebust }; 29284115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 29384115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 294bdc7f021STrond Myklebust .rpc_message = &msg, 29584115e1cSTrond Myklebust .callback_ops = &nfs_read_direct_ops, 296101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 29784115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 29884115e1cSTrond Myklebust }; 299607f31e8STrond Myklebust unsigned int pgbase; 300607f31e8STrond Myklebust int result; 301607f31e8STrond Myklebust ssize_t started = 0; 30282b145c5SChuck Lever 3031da177e4SLinus Torvalds do { 30482b145c5SChuck Lever struct nfs_read_data *data; 3055dd602f2SChuck Lever size_t bytes; 3061da177e4SLinus Torvalds 307e9f7bee1STrond Myklebust pgbase = user_addr & ~PAGE_MASK; 308e9f7bee1STrond Myklebust bytes = min(rsize,count); 309e9f7bee1STrond Myklebust 310607f31e8STrond Myklebust result = -ENOMEM; 3118d5658c9STrond Myklebust data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes)); 312607f31e8STrond Myklebust if (unlikely(!data)) 313607f31e8STrond Myklebust break; 314607f31e8STrond Myklebust 315607f31e8STrond Myklebust down_read(¤t->mm->mmap_sem); 316607f31e8STrond Myklebust result = get_user_pages(current, current->mm, user_addr, 317607f31e8STrond Myklebust data->npages, 1, 0, data->pagevec, NULL); 318607f31e8STrond Myklebust up_read(¤t->mm->mmap_sem); 319749e146eSChuck Lever if (result < 0) { 3201ae88b2eSTrond Myklebust nfs_readdata_free(data); 321749e146eSChuck Lever break; 322749e146eSChuck Lever } 323749e146eSChuck Lever if ((unsigned)result < data->npages) { 324d9df8d6bSTrond Myklebust bytes = result * PAGE_SIZE; 325d9df8d6bSTrond Myklebust if (bytes <= pgbase) { 326607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 3271ae88b2eSTrond Myklebust nfs_readdata_free(data); 328607f31e8STrond Myklebust break; 329607f31e8STrond Myklebust } 330d9df8d6bSTrond Myklebust bytes -= pgbase; 331d9df8d6bSTrond Myklebust data->npages = result; 332d9df8d6bSTrond Myklebust } 33306cf6f2eSChuck Lever 334607f31e8STrond Myklebust get_dreq(dreq); 335607f31e8STrond Myklebust 336607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 3371da177e4SLinus Torvalds data->inode = inode; 338bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 3391da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 340a20c6becSFred Isaman data->args.context = get_nfs_open_context(ctx); 341f11ac8dbSTrond Myklebust data->args.lock_context = dreq->l_ctx; 34288467055SChuck Lever data->args.offset = pos; 3431da177e4SLinus Torvalds data->args.pgbase = pgbase; 344607f31e8STrond Myklebust data->args.pages = data->pagevec; 3451da177e4SLinus Torvalds data->args.count = bytes; 3461da177e4SLinus Torvalds data->res.fattr = &data->fattr; 3471da177e4SLinus Torvalds data->res.eof = 0; 3481da177e4SLinus Torvalds data->res.count = bytes; 34965d26953SChuck Lever nfs_fattr_init(&data->fattr); 350bdc7f021STrond Myklebust msg.rpc_argp = &data->args; 351bdc7f021STrond Myklebust msg.rpc_resp = &data->res; 3521da177e4SLinus Torvalds 35307737691STrond Myklebust task_setup_data.task = &data->task; 35484115e1cSTrond Myklebust task_setup_data.callback_data = data; 355bdc7f021STrond Myklebust NFS_PROTO(inode)->read_setup(data, &msg); 3561da177e4SLinus Torvalds 35707737691STrond Myklebust task = rpc_run_task(&task_setup_data); 358dbae4c73STrond Myklebust if (IS_ERR(task)) 359dbae4c73STrond Myklebust break; 36007737691STrond Myklebust rpc_put_task(task); 3611da177e4SLinus Torvalds 362a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct read call " 363a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 3641da177e4SLinus Torvalds data->task.tk_pid, 3651da177e4SLinus Torvalds inode->i_sb->s_id, 3661da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 3671da177e4SLinus Torvalds bytes, 3681da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3691da177e4SLinus Torvalds 370607f31e8STrond Myklebust started += bytes; 371607f31e8STrond Myklebust user_addr += bytes; 37288467055SChuck Lever pos += bytes; 373e9f7bee1STrond Myklebust /* FIXME: Remove this unnecessary math from final patch */ 3741da177e4SLinus Torvalds pgbase += bytes; 3751da177e4SLinus Torvalds pgbase &= ~PAGE_MASK; 376e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds count -= bytes; 3791da177e4SLinus Torvalds } while (count != 0); 380607f31e8STrond Myklebust 381607f31e8STrond Myklebust if (started) 382c216fd70SChuck Lever return started; 383607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 38406cf6f2eSChuck Lever } 38506cf6f2eSChuck Lever 38619f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 38719f73787SChuck Lever const struct iovec *iov, 38819f73787SChuck Lever unsigned long nr_segs, 38919f73787SChuck Lever loff_t pos) 39019f73787SChuck Lever { 39119f73787SChuck Lever ssize_t result = -EINVAL; 39219f73787SChuck Lever size_t requested_bytes = 0; 39319f73787SChuck Lever unsigned long seg; 39419f73787SChuck Lever 39519f73787SChuck Lever get_dreq(dreq); 39619f73787SChuck Lever 39719f73787SChuck Lever for (seg = 0; seg < nr_segs; seg++) { 39819f73787SChuck Lever const struct iovec *vec = &iov[seg]; 39902fe4946SChuck Lever result = nfs_direct_read_schedule_segment(dreq, vec, pos); 40019f73787SChuck Lever if (result < 0) 40119f73787SChuck Lever break; 40219f73787SChuck Lever requested_bytes += result; 40319f73787SChuck Lever if ((size_t)result < vec->iov_len) 40419f73787SChuck Lever break; 40519f73787SChuck Lever pos += vec->iov_len; 40619f73787SChuck Lever } 40719f73787SChuck Lever 408839f7ad6SChuck Lever /* 409839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 410839f7ad6SChuck Lever * generic layer handle the completion. 411839f7ad6SChuck Lever */ 412839f7ad6SChuck Lever if (requested_bytes == 0) { 413839f7ad6SChuck Lever nfs_direct_req_release(dreq); 414839f7ad6SChuck Lever return result < 0 ? result : -EIO; 415839f7ad6SChuck Lever } 416839f7ad6SChuck Lever 41719f73787SChuck Lever if (put_dreq(dreq)) 41819f73787SChuck Lever nfs_direct_complete(dreq); 41919f73787SChuck Lever return 0; 42019f73787SChuck Lever } 42119f73787SChuck Lever 422c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov, 423c216fd70SChuck Lever unsigned long nr_segs, loff_t pos) 4241da177e4SLinus Torvalds { 425f11ac8dbSTrond Myklebust ssize_t result = -ENOMEM; 42699514f8fSChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 4271da177e4SLinus Torvalds struct nfs_direct_req *dreq; 4281da177e4SLinus Torvalds 429607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 430f11ac8dbSTrond Myklebust if (dreq == NULL) 431f11ac8dbSTrond Myklebust goto out; 4321da177e4SLinus Torvalds 43391d5b470SChuck Lever dreq->inode = inode; 434cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 435f11ac8dbSTrond Myklebust dreq->l_ctx = nfs_get_lock_context(dreq->ctx); 436f11ac8dbSTrond Myklebust if (dreq->l_ctx == NULL) 437f11ac8dbSTrond Myklebust goto out_release; 438487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 439487b8372SChuck Lever dreq->iocb = iocb; 4401da177e4SLinus Torvalds 441c216fd70SChuck Lever result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos); 442607f31e8STrond Myklebust if (!result) 443bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 444f11ac8dbSTrond Myklebust out_release: 445b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 446f11ac8dbSTrond Myklebust out: 4471da177e4SLinus Torvalds return result; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 450fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) 4511da177e4SLinus Torvalds { 452607f31e8STrond Myklebust while (!list_empty(&dreq->rewrite_list)) { 453607f31e8STrond Myklebust struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages); 454fad61490STrond Myklebust list_del(&data->pages); 455607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, data->npages); 4561ae88b2eSTrond Myklebust nfs_writedata_free(data); 457fad61490STrond Myklebust } 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 460fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 461fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 4621da177e4SLinus Torvalds { 463607f31e8STrond Myklebust struct inode *inode = dreq->inode; 464607f31e8STrond Myklebust struct list_head *p; 465607f31e8STrond Myklebust struct nfs_write_data *data; 46607737691STrond Myklebust struct rpc_task *task; 467bdc7f021STrond Myklebust struct rpc_message msg = { 468bdc7f021STrond Myklebust .rpc_cred = dreq->ctx->cred, 469bdc7f021STrond Myklebust }; 47084115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 47184115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 472a8b40bc7STerry Loftin .rpc_message = &msg, 47384115e1cSTrond Myklebust .callback_ops = &nfs_write_direct_ops, 474101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 47584115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 47684115e1cSTrond Myklebust }; 4771da177e4SLinus Torvalds 478fad61490STrond Myklebust dreq->count = 0; 479607f31e8STrond Myklebust get_dreq(dreq); 4801da177e4SLinus Torvalds 481607f31e8STrond Myklebust list_for_each(p, &dreq->rewrite_list) { 482607f31e8STrond Myklebust data = list_entry(p, struct nfs_write_data, pages); 483607f31e8STrond Myklebust 484607f31e8STrond Myklebust get_dreq(dreq); 485607f31e8STrond Myklebust 486bdc7f021STrond Myklebust /* Use stable writes */ 487bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 488bdc7f021STrond Myklebust 489607f31e8STrond Myklebust /* 490607f31e8STrond Myklebust * Reset data->res. 491607f31e8STrond Myklebust */ 492607f31e8STrond Myklebust nfs_fattr_init(&data->fattr); 493607f31e8STrond Myklebust data->res.count = data->args.count; 494607f31e8STrond Myklebust memset(&data->verf, 0, sizeof(data->verf)); 495607f31e8STrond Myklebust 496607f31e8STrond Myklebust /* 497607f31e8STrond Myklebust * Reuse data->task; data->args should not have changed 498607f31e8STrond Myklebust * since the original request was sent. 499607f31e8STrond Myklebust */ 50007737691STrond Myklebust task_setup_data.task = &data->task; 50184115e1cSTrond Myklebust task_setup_data.callback_data = data; 502bdc7f021STrond Myklebust msg.rpc_argp = &data->args; 503bdc7f021STrond Myklebust msg.rpc_resp = &data->res; 504bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 505607f31e8STrond Myklebust 506607f31e8STrond Myklebust /* 507607f31e8STrond Myklebust * We're called via an RPC callback, so BKL is already held. 508607f31e8STrond Myklebust */ 50907737691STrond Myklebust task = rpc_run_task(&task_setup_data); 51007737691STrond Myklebust if (!IS_ERR(task)) 51107737691STrond Myklebust rpc_put_task(task); 512607f31e8STrond Myklebust 513607f31e8STrond Myklebust dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 514607f31e8STrond Myklebust data->task.tk_pid, 515607f31e8STrond Myklebust inode->i_sb->s_id, 516607f31e8STrond Myklebust (long long)NFS_FILEID(inode), 517607f31e8STrond Myklebust data->args.count, 518607f31e8STrond Myklebust (unsigned long long)data->args.offset); 519607f31e8STrond Myklebust } 520607f31e8STrond Myklebust 521607f31e8STrond Myklebust if (put_dreq(dreq)) 522607f31e8STrond Myklebust nfs_direct_write_complete(dreq, inode); 523fad61490STrond Myklebust } 5241da177e4SLinus Torvalds 525fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) 526fad61490STrond Myklebust { 527*0b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 5281da177e4SLinus Torvalds 529fad61490STrond Myklebust /* Call the NFS version-specific code */ 530c9d8f89dSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 531c9d8f89dSTrond Myklebust } 532c9d8f89dSTrond Myklebust 533c9d8f89dSTrond Myklebust static void nfs_direct_commit_release(void *calldata) 534c9d8f89dSTrond Myklebust { 535*0b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 536*0b7c0153SFred Isaman struct nfs_direct_req *dreq = data->dreq; 537c9d8f89dSTrond Myklebust int status = data->task.tk_status; 538c9d8f89dSTrond Myklebust 539c9d8f89dSTrond Myklebust if (status < 0) { 54060fa3f76STrond Myklebust dprintk("NFS: %5u commit failed with error %d.\n", 541c9d8f89dSTrond Myklebust data->task.tk_pid, status); 542fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 54360fa3f76STrond Myklebust } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 544c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid); 545fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 546fad61490STrond Myklebust } 547fad61490STrond Myklebust 548c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status); 549fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 5501ae88b2eSTrond Myklebust nfs_commit_free(data); 551fad61490STrond Myklebust } 552fad61490STrond Myklebust 553fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = { 554*0b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 555fad61490STrond Myklebust .rpc_call_done = nfs_direct_commit_result, 556c9d8f89dSTrond Myklebust .rpc_release = nfs_direct_commit_release, 557fad61490STrond Myklebust }; 558fad61490STrond Myklebust 559fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 560fad61490STrond Myklebust { 561*0b7c0153SFred Isaman struct nfs_commit_data *data = dreq->commit_data; 56207737691STrond Myklebust struct rpc_task *task; 563bdc7f021STrond Myklebust struct rpc_message msg = { 564bdc7f021STrond Myklebust .rpc_argp = &data->args, 565bdc7f021STrond Myklebust .rpc_resp = &data->res, 566bdc7f021STrond Myklebust .rpc_cred = dreq->ctx->cred, 567bdc7f021STrond Myklebust }; 56884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 56907737691STrond Myklebust .task = &data->task, 57084115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(dreq->inode), 571bdc7f021STrond Myklebust .rpc_message = &msg, 57284115e1cSTrond Myklebust .callback_ops = &nfs_commit_direct_ops, 57384115e1cSTrond Myklebust .callback_data = data, 574101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 57584115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 57684115e1cSTrond Myklebust }; 577fad61490STrond Myklebust 578fad61490STrond Myklebust data->inode = dreq->inode; 579bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 580fad61490STrond Myklebust 581fad61490STrond Myklebust data->args.fh = NFS_FH(data->inode); 582607f31e8STrond Myklebust data->args.offset = 0; 583607f31e8STrond Myklebust data->args.count = 0; 584fad61490STrond Myklebust data->res.fattr = &data->fattr; 585fad61490STrond Myklebust data->res.verf = &data->verf; 58665d26953SChuck Lever nfs_fattr_init(&data->fattr); 587fad61490STrond Myklebust 588bdc7f021STrond Myklebust NFS_PROTO(data->inode)->commit_setup(data, &msg); 589fad61490STrond Myklebust 590fad61490STrond Myklebust /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ 591fad61490STrond Myklebust dreq->commit_data = NULL; 592fad61490STrond Myklebust 593e99170ffSTrond Myklebust dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 5941da177e4SLinus Torvalds 59507737691STrond Myklebust task = rpc_run_task(&task_setup_data); 59607737691STrond Myklebust if (!IS_ERR(task)) 59707737691STrond Myklebust rpc_put_task(task); 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 600fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 6011da177e4SLinus Torvalds { 602fad61490STrond Myklebust int flags = dreq->flags; 6031da177e4SLinus Torvalds 604fad61490STrond Myklebust dreq->flags = 0; 605fad61490STrond Myklebust switch (flags) { 606fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 607fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 6081da177e4SLinus Torvalds break; 609fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 610fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 6111da177e4SLinus Torvalds break; 6121da177e4SLinus Torvalds default: 613fad61490STrond Myklebust if (dreq->commit_data != NULL) 614fad61490STrond Myklebust nfs_commit_free(dreq->commit_data); 615fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 616cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 617fad61490STrond Myklebust nfs_direct_complete(dreq); 6181da177e4SLinus Torvalds } 619fad61490STrond Myklebust } 620fad61490STrond Myklebust 621fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 622fad61490STrond Myklebust { 623c9d8f89dSTrond Myklebust dreq->commit_data = nfs_commitdata_alloc(); 624fad61490STrond Myklebust if (dreq->commit_data != NULL) 625*0b7c0153SFred Isaman dreq->commit_data->dreq = dreq; 626fad61490STrond Myklebust } 627fad61490STrond Myklebust #else 628fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 629fad61490STrond Myklebust { 630fad61490STrond Myklebust dreq->commit_data = NULL; 631fad61490STrond Myklebust } 632fad61490STrond Myklebust 633fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 634fad61490STrond Myklebust { 635fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 636cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 637fad61490STrond Myklebust nfs_direct_complete(dreq); 638fad61490STrond Myklebust } 639fad61490STrond Myklebust #endif 640fad61490STrond Myklebust 641462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata) 642462d5b32SChuck Lever { 643462d5b32SChuck Lever struct nfs_write_data *data = calldata; 644462d5b32SChuck Lever 64583762c56SFred Isaman nfs_writeback_done(task, data); 646c9d8f89dSTrond Myklebust } 647c9d8f89dSTrond Myklebust 648c9d8f89dSTrond Myklebust /* 649c9d8f89dSTrond Myklebust * NB: Return the value of the first error return code. Subsequent 650c9d8f89dSTrond Myklebust * errors after the first one are ignored. 651c9d8f89dSTrond Myklebust */ 652c9d8f89dSTrond Myklebust static void nfs_direct_write_release(void *calldata) 653c9d8f89dSTrond Myklebust { 654c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 655c9d8f89dSTrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 656c9d8f89dSTrond Myklebust int status = data->task.tk_status; 657462d5b32SChuck Lever 65815ce4a0cSChuck Lever spin_lock(&dreq->lock); 659462d5b32SChuck Lever 66060fa3f76STrond Myklebust if (unlikely(status < 0)) { 661432409eeSNeil Brown /* An error has occurred, so we should not commit */ 66260fa3f76STrond Myklebust dreq->flags = 0; 66360fa3f76STrond Myklebust dreq->error = status; 664eda3cef8STrond Myklebust } 665432409eeSNeil Brown if (unlikely(dreq->error != 0)) 666432409eeSNeil Brown goto out_unlock; 667eda3cef8STrond Myklebust 66815ce4a0cSChuck Lever dreq->count += data->res.count; 66915ce4a0cSChuck Lever 670fad61490STrond Myklebust if (data->res.verf->committed != NFS_FILE_SYNC) { 671fad61490STrond Myklebust switch (dreq->flags) { 672fad61490STrond Myklebust case 0: 673fad61490STrond Myklebust memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); 674fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_DO_COMMIT; 675fad61490STrond Myklebust break; 676fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 677fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { 678c9d8f89dSTrond Myklebust dprintk("NFS: %5u write verify failed\n", data->task.tk_pid); 679fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 680fad61490STrond Myklebust } 681fad61490STrond Myklebust } 682fad61490STrond Myklebust } 683eda3cef8STrond Myklebust out_unlock: 684fad61490STrond Myklebust spin_unlock(&dreq->lock); 685fad61490STrond Myklebust 686607f31e8STrond Myklebust if (put_dreq(dreq)) 687fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 688462d5b32SChuck Lever } 689462d5b32SChuck Lever 690462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = { 691def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 692462d5b32SChuck Lever .rpc_call_done = nfs_direct_write_result, 693fad61490STrond Myklebust .rpc_release = nfs_direct_write_release, 694462d5b32SChuck Lever }; 695462d5b32SChuck Lever 696462d5b32SChuck Lever /* 697607f31e8STrond Myklebust * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 698607f31e8STrond Myklebust * operation. If nfs_writedata_alloc() or get_user_pages() fails, 699607f31e8STrond Myklebust * bail and stop sending more writes. Write length accounting is 700607f31e8STrond Myklebust * handled automatically by nfs_direct_write_result(). Otherwise, if 701607f31e8STrond Myklebust * no requests have been sent, just return an error. 702462d5b32SChuck Lever */ 70302fe4946SChuck Lever static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq, 70402fe4946SChuck Lever const struct iovec *iov, 70502fe4946SChuck Lever loff_t pos, int sync) 706462d5b32SChuck Lever { 707a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 7083d4ff43dSAl Viro struct inode *inode = ctx->dentry->d_inode; 70902fe4946SChuck Lever unsigned long user_addr = (unsigned long)iov->iov_base; 71002fe4946SChuck Lever size_t count = iov->iov_len; 71107737691STrond Myklebust struct rpc_task *task; 712bdc7f021STrond Myklebust struct rpc_message msg = { 713bdc7f021STrond Myklebust .rpc_cred = ctx->cred, 714bdc7f021STrond Myklebust }; 71584115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 71684115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 717bdc7f021STrond Myklebust .rpc_message = &msg, 71884115e1cSTrond Myklebust .callback_ops = &nfs_write_direct_ops, 719101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 72084115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 72184115e1cSTrond Myklebust }; 722462d5b32SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 723607f31e8STrond Myklebust unsigned int pgbase; 724607f31e8STrond Myklebust int result; 725607f31e8STrond Myklebust ssize_t started = 0; 72682b145c5SChuck Lever 727462d5b32SChuck Lever do { 72882b145c5SChuck Lever struct nfs_write_data *data; 729462d5b32SChuck Lever size_t bytes; 730462d5b32SChuck Lever 731e9f7bee1STrond Myklebust pgbase = user_addr & ~PAGE_MASK; 732e9f7bee1STrond Myklebust bytes = min(wsize,count); 733e9f7bee1STrond Myklebust 734607f31e8STrond Myklebust result = -ENOMEM; 7358d5658c9STrond Myklebust data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes)); 736607f31e8STrond Myklebust if (unlikely(!data)) 737607f31e8STrond Myklebust break; 738607f31e8STrond Myklebust 739607f31e8STrond Myklebust down_read(¤t->mm->mmap_sem); 740607f31e8STrond Myklebust result = get_user_pages(current, current->mm, user_addr, 741607f31e8STrond Myklebust data->npages, 0, 0, data->pagevec, NULL); 742607f31e8STrond Myklebust up_read(¤t->mm->mmap_sem); 743749e146eSChuck Lever if (result < 0) { 7441ae88b2eSTrond Myklebust nfs_writedata_free(data); 745749e146eSChuck Lever break; 746749e146eSChuck Lever } 747749e146eSChuck Lever if ((unsigned)result < data->npages) { 748d9df8d6bSTrond Myklebust bytes = result * PAGE_SIZE; 749d9df8d6bSTrond Myklebust if (bytes <= pgbase) { 750607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 7511ae88b2eSTrond Myklebust nfs_writedata_free(data); 752607f31e8STrond Myklebust break; 753607f31e8STrond Myklebust } 754d9df8d6bSTrond Myklebust bytes -= pgbase; 755d9df8d6bSTrond Myklebust data->npages = result; 756d9df8d6bSTrond Myklebust } 757607f31e8STrond Myklebust 758607f31e8STrond Myklebust get_dreq(dreq); 759607f31e8STrond Myklebust 760fad61490STrond Myklebust list_move_tail(&data->pages, &dreq->rewrite_list); 761462d5b32SChuck Lever 762607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 763462d5b32SChuck Lever data->inode = inode; 764bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 765462d5b32SChuck Lever data->args.fh = NFS_FH(inode); 7661ae88b2eSTrond Myklebust data->args.context = ctx; 767f11ac8dbSTrond Myklebust data->args.lock_context = dreq->l_ctx; 76888467055SChuck Lever data->args.offset = pos; 769462d5b32SChuck Lever data->args.pgbase = pgbase; 770607f31e8STrond Myklebust data->args.pages = data->pagevec; 771462d5b32SChuck Lever data->args.count = bytes; 772bdc7f021STrond Myklebust data->args.stable = sync; 773462d5b32SChuck Lever data->res.fattr = &data->fattr; 774462d5b32SChuck Lever data->res.count = bytes; 77547989d74SChuck Lever data->res.verf = &data->verf; 77665d26953SChuck Lever nfs_fattr_init(&data->fattr); 777462d5b32SChuck Lever 77807737691STrond Myklebust task_setup_data.task = &data->task; 77984115e1cSTrond Myklebust task_setup_data.callback_data = data; 780bdc7f021STrond Myklebust msg.rpc_argp = &data->args; 781bdc7f021STrond Myklebust msg.rpc_resp = &data->res; 782bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 783462d5b32SChuck Lever 78407737691STrond Myklebust task = rpc_run_task(&task_setup_data); 785dbae4c73STrond Myklebust if (IS_ERR(task)) 786dbae4c73STrond Myklebust break; 78707737691STrond Myklebust rpc_put_task(task); 7881da177e4SLinus Torvalds 789a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct write call " 790a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 791462d5b32SChuck Lever data->task.tk_pid, 792462d5b32SChuck Lever inode->i_sb->s_id, 793462d5b32SChuck Lever (long long)NFS_FILEID(inode), 794462d5b32SChuck Lever bytes, 795462d5b32SChuck Lever (unsigned long long)data->args.offset); 796462d5b32SChuck Lever 797607f31e8STrond Myklebust started += bytes; 798607f31e8STrond Myklebust user_addr += bytes; 79988467055SChuck Lever pos += bytes; 800e9f7bee1STrond Myklebust 801e9f7bee1STrond Myklebust /* FIXME: Remove this useless math from the final patch */ 802462d5b32SChuck Lever pgbase += bytes; 803462d5b32SChuck Lever pgbase &= ~PAGE_MASK; 804e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 805462d5b32SChuck Lever 806462d5b32SChuck Lever count -= bytes; 807462d5b32SChuck Lever } while (count != 0); 808607f31e8STrond Myklebust 809607f31e8STrond Myklebust if (started) 810c216fd70SChuck Lever return started; 811607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 81206cf6f2eSChuck Lever } 81306cf6f2eSChuck Lever 81419f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 81519f73787SChuck Lever const struct iovec *iov, 81619f73787SChuck Lever unsigned long nr_segs, 81719f73787SChuck Lever loff_t pos, int sync) 81819f73787SChuck Lever { 81919f73787SChuck Lever ssize_t result = 0; 82019f73787SChuck Lever size_t requested_bytes = 0; 82119f73787SChuck Lever unsigned long seg; 82219f73787SChuck Lever 82319f73787SChuck Lever get_dreq(dreq); 82419f73787SChuck Lever 82519f73787SChuck Lever for (seg = 0; seg < nr_segs; seg++) { 82619f73787SChuck Lever const struct iovec *vec = &iov[seg]; 82702fe4946SChuck Lever result = nfs_direct_write_schedule_segment(dreq, vec, 82819f73787SChuck Lever pos, sync); 82919f73787SChuck Lever if (result < 0) 83019f73787SChuck Lever break; 83119f73787SChuck Lever requested_bytes += result; 83219f73787SChuck Lever if ((size_t)result < vec->iov_len) 83319f73787SChuck Lever break; 83419f73787SChuck Lever pos += vec->iov_len; 83519f73787SChuck Lever } 83619f73787SChuck Lever 837839f7ad6SChuck Lever /* 838839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 839839f7ad6SChuck Lever * generic layer handle the completion. 840839f7ad6SChuck Lever */ 841839f7ad6SChuck Lever if (requested_bytes == 0) { 842839f7ad6SChuck Lever nfs_direct_req_release(dreq); 843839f7ad6SChuck Lever return result < 0 ? result : -EIO; 844839f7ad6SChuck Lever } 845839f7ad6SChuck Lever 84619f73787SChuck Lever if (put_dreq(dreq)) 84719f73787SChuck Lever nfs_direct_write_complete(dreq, dreq->inode); 84819f73787SChuck Lever return 0; 84919f73787SChuck Lever } 85019f73787SChuck Lever 851c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov, 852c216fd70SChuck Lever unsigned long nr_segs, loff_t pos, 853c216fd70SChuck Lever size_t count) 854462d5b32SChuck Lever { 855f11ac8dbSTrond Myklebust ssize_t result = -ENOMEM; 856c89f2ee5SChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 857462d5b32SChuck Lever struct nfs_direct_req *dreq; 858fad61490STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize; 859bdc7f021STrond Myklebust int sync = NFS_UNSTABLE; 860462d5b32SChuck Lever 861607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 862462d5b32SChuck Lever if (!dreq) 863f11ac8dbSTrond Myklebust goto out; 864607f31e8STrond Myklebust nfs_alloc_commit_data(dreq); 865607f31e8STrond Myklebust 866b47d19deSArun Bharadwaj if (dreq->commit_data == NULL || count <= wsize) 867bdc7f021STrond Myklebust sync = NFS_FILE_SYNC; 868462d5b32SChuck Lever 869c89f2ee5SChuck Lever dreq->inode = inode; 870cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 871f11ac8dbSTrond Myklebust dreq->l_ctx = nfs_get_lock_context(dreq->ctx); 872568a810dSSteve Dickson if (dreq->l_ctx == NULL) 873f11ac8dbSTrond Myklebust goto out_release; 874c89f2ee5SChuck Lever if (!is_sync_kiocb(iocb)) 875c89f2ee5SChuck Lever dreq->iocb = iocb; 876462d5b32SChuck Lever 877c216fd70SChuck Lever result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync); 878607f31e8STrond Myklebust if (!result) 879c89f2ee5SChuck Lever result = nfs_direct_wait(dreq); 880f11ac8dbSTrond Myklebust out_release: 881b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 882f11ac8dbSTrond Myklebust out: 8831da177e4SLinus Torvalds return result; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds /** 8871da177e4SLinus Torvalds * nfs_file_direct_read - file direct read operation for NFS files 8881da177e4SLinus Torvalds * @iocb: target I/O control block 889027445c3SBadari Pulavarty * @iov: vector of user buffers into which to read data 890027445c3SBadari Pulavarty * @nr_segs: size of iov vector 89188467055SChuck Lever * @pos: byte offset in file where reading starts 8921da177e4SLinus Torvalds * 8931da177e4SLinus Torvalds * We use this function for direct reads instead of calling 8941da177e4SLinus Torvalds * generic_file_aio_read() in order to avoid gfar's check to see if 8951da177e4SLinus Torvalds * the request starts before the end of the file. For that check 8961da177e4SLinus Torvalds * to work, we must generate a GETATTR before each direct read, and 8971da177e4SLinus Torvalds * even then there is a window between the GETATTR and the subsequent 89888467055SChuck Lever * READ where the file size could change. Our preference is simply 8991da177e4SLinus Torvalds * to do all reads the application wants, and the server will take 9001da177e4SLinus Torvalds * care of managing the end of file boundary. 9011da177e4SLinus Torvalds * 9021da177e4SLinus Torvalds * This function also eliminates unnecessarily updating the file's 9031da177e4SLinus Torvalds * atime locally, as the NFS server sets the file's atime, and this 9041da177e4SLinus Torvalds * client must read the updated atime from the server back into its 9051da177e4SLinus Torvalds * cache. 9061da177e4SLinus Torvalds */ 907027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov, 908027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 9091da177e4SLinus Torvalds { 9101da177e4SLinus Torvalds ssize_t retval = -EINVAL; 9111da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9121da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 913c216fd70SChuck Lever size_t count; 9141da177e4SLinus Torvalds 915c216fd70SChuck Lever count = iov_length(iov, nr_segs); 916c216fd70SChuck Lever nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 917c216fd70SChuck Lever 9186da24bc9SChuck Lever dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n", 91901cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 92001cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 921c216fd70SChuck Lever count, (long long) pos); 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds retval = 0; 9241da177e4SLinus Torvalds if (!count) 9251da177e4SLinus Torvalds goto out; 9261da177e4SLinus Torvalds 92729884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 9281da177e4SLinus Torvalds if (retval) 9291da177e4SLinus Torvalds goto out; 9301da177e4SLinus Torvalds 9317ec10f26SKonstantin Khlebnikov task_io_account_read(count); 9327ec10f26SKonstantin Khlebnikov 933c216fd70SChuck Lever retval = nfs_direct_read(iocb, iov, nr_segs, pos); 9341da177e4SLinus Torvalds if (retval > 0) 9350cdd80d0SChuck Lever iocb->ki_pos = pos + retval; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds out: 9381da177e4SLinus Torvalds return retval; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /** 9421da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 9431da177e4SLinus Torvalds * @iocb: target I/O control block 944027445c3SBadari Pulavarty * @iov: vector of user buffers from which to write data 945027445c3SBadari Pulavarty * @nr_segs: size of iov vector 94688467055SChuck Lever * @pos: byte offset in file where writing starts 9471da177e4SLinus Torvalds * 9481da177e4SLinus Torvalds * We use this function for direct writes instead of calling 9491da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 9501da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 9511da177e4SLinus Torvalds * the new i_size and this client must read the updated size 9521da177e4SLinus Torvalds * back into its cache. We let the server do generic write 9531da177e4SLinus Torvalds * parameter checking and report problems. 9541da177e4SLinus Torvalds * 9551da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 9561da177e4SLinus Torvalds * 9571da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 9581da177e4SLinus Torvalds * readers of this file. 9591da177e4SLinus Torvalds * 9601da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 9611da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 9621da177e4SLinus Torvalds */ 963027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov, 964027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 9651da177e4SLinus Torvalds { 966070ea602SChuck Lever ssize_t retval = -EINVAL; 9671da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9681da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 969c216fd70SChuck Lever size_t count; 9701da177e4SLinus Torvalds 971c216fd70SChuck Lever count = iov_length(iov, nr_segs); 972c216fd70SChuck Lever nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 973c216fd70SChuck Lever 9746da24bc9SChuck Lever dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n", 97501cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 97601cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 977c216fd70SChuck Lever count, (long long) pos); 978027445c3SBadari Pulavarty 979ce1a8e67SChuck Lever retval = generic_write_checks(file, &pos, &count, 0); 980ce1a8e67SChuck Lever if (retval) 9811da177e4SLinus Torvalds goto out; 982ce1a8e67SChuck Lever 983ce1a8e67SChuck Lever retval = -EINVAL; 984ce1a8e67SChuck Lever if ((ssize_t) count < 0) 9851da177e4SLinus Torvalds goto out; 9861da177e4SLinus Torvalds retval = 0; 9871da177e4SLinus Torvalds if (!count) 9881da177e4SLinus Torvalds goto out; 989ce1a8e67SChuck Lever 99029884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 9911da177e4SLinus Torvalds if (retval) 9921da177e4SLinus Torvalds goto out; 9931da177e4SLinus Torvalds 9947ec10f26SKonstantin Khlebnikov task_io_account_write(count); 9957ec10f26SKonstantin Khlebnikov 996c216fd70SChuck Lever retval = nfs_direct_write(iocb, iov, nr_segs, pos, count); 9979eafa8ccSChuck Lever 9981da177e4SLinus Torvalds if (retval > 0) 999ce1a8e67SChuck Lever iocb->ki_pos = pos + retval; 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds out: 10021da177e4SLinus Torvalds return retval; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 100588467055SChuck Lever /** 100688467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 100788467055SChuck Lever * 100888467055SChuck Lever */ 1009f7b422b1SDavid Howells int __init nfs_init_directcache(void) 10101da177e4SLinus Torvalds { 10111da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 10121da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 1013fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 1014fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 101520c2df83SPaul Mundt NULL); 10161da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 10171da177e4SLinus Torvalds return -ENOMEM; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds return 0; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 102288467055SChuck Lever /** 1023f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 102488467055SChuck Lever * 102588467055SChuck Lever */ 1026266bee88SDavid Brownell void nfs_destroy_directcache(void) 10271da177e4SLinus Torvalds { 10281a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 10291da177e4SLinus Torvalds } 1030