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/system.h> 551da177e4SLinus Torvalds #include <asm/uaccess.h> 56*60063497SArun Sharma #include <linux/atomic.h> 571da177e4SLinus Torvalds 588d5658c9STrond Myklebust #include "internal.h" 5991d5b470SChuck Lever #include "iostat.h" 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 621da177e4SLinus Torvalds 63e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 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 */ 8015ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 811da177e4SLinus Torvalds error; /* any reported error */ 82d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 83fad61490STrond Myklebust 84fad61490STrond Myklebust /* commit state */ 85607f31e8STrond Myklebust struct list_head rewrite_list; /* saved nfs_write_data structs */ 86fad61490STrond Myklebust struct nfs_write_data * commit_data; /* special write_data for commits */ 87fad61490STrond Myklebust int flags; 88fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 89fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 90fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 911da177e4SLinus Torvalds }; 921da177e4SLinus Torvalds 93fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 94607f31e8STrond Myklebust static const struct rpc_call_ops nfs_write_direct_ops; 95607f31e8STrond Myklebust 96607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 97607f31e8STrond Myklebust { 98607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 99607f31e8STrond Myklebust } 100607f31e8STrond Myklebust 101607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 102607f31e8STrond Myklebust { 103607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 104607f31e8STrond Myklebust } 105607f31e8STrond Myklebust 1061da177e4SLinus Torvalds /** 107b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 108b8a32e2bSChuck Lever * @rw: direction (read or write) 109b8a32e2bSChuck Lever * @iocb: target I/O control block 110b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 111b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 112b8a32e2bSChuck Lever * @nr_segs: size of iovec array 113b8a32e2bSChuck Lever * 114b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 115b8a32e2bSChuck Lever * the NFS client supports direct I/O. However, we shunt off direct 116b8a32e2bSChuck Lever * read and write requests before the VFS gets them, so this method 117b8a32e2bSChuck Lever * should never be called. 1181da177e4SLinus Torvalds */ 119b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) 120b8a32e2bSChuck Lever { 121b8a32e2bSChuck Lever dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", 12201cce933SJosef "Jeff" Sipek iocb->ki_filp->f_path.dentry->d_name.name, 123e99170ffSTrond Myklebust (long long) pos, nr_segs); 124b8a32e2bSChuck Lever 125b8a32e2bSChuck Lever return -EINVAL; 126b8a32e2bSChuck Lever } 127b8a32e2bSChuck Lever 128d4a8f367STrond Myklebust static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count) 1296b45d858STrond Myklebust { 130d4a8f367STrond Myklebust unsigned int npages; 131749e146eSChuck Lever unsigned int i; 132d4a8f367STrond Myklebust 133d4a8f367STrond Myklebust if (count == 0) 134d4a8f367STrond Myklebust return; 135d4a8f367STrond Myklebust pages += (pgbase >> PAGE_SHIFT); 136d4a8f367STrond Myklebust npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT; 1376b45d858STrond Myklebust for (i = 0; i < npages; i++) { 1386b45d858STrond Myklebust struct page *page = pages[i]; 139607f31e8STrond Myklebust if (!PageCompound(page)) 140d4a8f367STrond Myklebust set_page_dirty(page); 1416b45d858STrond Myklebust } 1429c93ab7dSChuck Lever } 1439c93ab7dSChuck Lever 144749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 1459c93ab7dSChuck Lever { 146749e146eSChuck Lever unsigned int i; 147607f31e8STrond Myklebust for (i = 0; i < npages; i++) 148607f31e8STrond Myklebust page_cache_release(pages[i]); 1496b45d858STrond Myklebust } 1506b45d858STrond Myklebust 15193619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds struct nfs_direct_req *dreq; 1541da177e4SLinus Torvalds 155e94b1766SChristoph Lameter dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL); 1561da177e4SLinus Torvalds if (!dreq) 1571da177e4SLinus Torvalds return NULL; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds kref_init(&dreq->kref); 160607f31e8STrond Myklebust kref_get(&dreq->kref); 161d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 162fad61490STrond Myklebust INIT_LIST_HEAD(&dreq->rewrite_list); 16393619e59SChuck Lever dreq->iocb = NULL; 164a8881f5aSTrond Myklebust dreq->ctx = NULL; 165f11ac8dbSTrond Myklebust dreq->l_ctx = NULL; 16615ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 167607f31e8STrond Myklebust atomic_set(&dreq->io_count, 0); 16815ce4a0cSChuck Lever dreq->count = 0; 16915ce4a0cSChuck Lever dreq->error = 0; 170fad61490STrond Myklebust dreq->flags = 0; 17193619e59SChuck Lever 17293619e59SChuck Lever return dreq; 17393619e59SChuck Lever } 17493619e59SChuck Lever 175b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 178a8881f5aSTrond Myklebust 179f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 180f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 181a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 182a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 1831da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 186b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 187b4946ffbSTrond Myklebust { 188b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 189b4946ffbSTrond Myklebust } 190b4946ffbSTrond Myklebust 191d4cc948bSChuck Lever /* 192bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 193bc0fb201SChuck Lever */ 194bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 195bc0fb201SChuck Lever { 19615ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 197bc0fb201SChuck Lever 198bc0fb201SChuck Lever /* Async requests don't wait here */ 199bc0fb201SChuck Lever if (dreq->iocb) 200bc0fb201SChuck Lever goto out; 201bc0fb201SChuck Lever 202150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 203bc0fb201SChuck Lever 204bc0fb201SChuck Lever if (!result) 20515ce4a0cSChuck Lever result = dreq->error; 206bc0fb201SChuck Lever if (!result) 20715ce4a0cSChuck Lever result = dreq->count; 208bc0fb201SChuck Lever 209bc0fb201SChuck Lever out: 210bc0fb201SChuck Lever return (ssize_t) result; 211bc0fb201SChuck Lever } 212bc0fb201SChuck Lever 213bc0fb201SChuck Lever /* 214607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 215607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 21663ab46abSChuck Lever */ 21763ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq) 21863ab46abSChuck Lever { 21963ab46abSChuck Lever if (dreq->iocb) { 22015ce4a0cSChuck Lever long res = (long) dreq->error; 22163ab46abSChuck Lever if (!res) 22215ce4a0cSChuck Lever res = (long) dreq->count; 22363ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 224d72b7a6bSTrond Myklebust } 225d72b7a6bSTrond Myklebust complete_all(&dreq->completion); 22663ab46abSChuck Lever 227b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 22863ab46abSChuck Lever } 22963ab46abSChuck Lever 23063ab46abSChuck Lever /* 231607f31e8STrond Myklebust * We must hold a reference to all the pages in this direct read request 232607f31e8STrond Myklebust * until the RPCs complete. This could be long *after* we are woken up in 233607f31e8STrond Myklebust * nfs_direct_wait (for instance, if someone hits ^C on a slow server). 23406cf6f2eSChuck Lever */ 235ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata) 2361da177e4SLinus Torvalds { 237ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 2381da177e4SLinus Torvalds 239fdd1e74cSTrond Myklebust nfs_readpage_result(task, data); 240fdd1e74cSTrond Myklebust } 241fdd1e74cSTrond Myklebust 242fdd1e74cSTrond Myklebust static void nfs_direct_read_release(void *calldata) 243fdd1e74cSTrond Myklebust { 244fdd1e74cSTrond Myklebust 245fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 246fdd1e74cSTrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 247fdd1e74cSTrond Myklebust int status = data->task.tk_status; 2481da177e4SLinus Torvalds 24915ce4a0cSChuck Lever spin_lock(&dreq->lock); 250fdd1e74cSTrond Myklebust if (unlikely(status < 0)) { 251fdd1e74cSTrond Myklebust dreq->error = status; 25215ce4a0cSChuck Lever spin_unlock(&dreq->lock); 253d4a8f367STrond Myklebust } else { 254d4a8f367STrond Myklebust dreq->count += data->res.count; 255d4a8f367STrond Myklebust spin_unlock(&dreq->lock); 256d4a8f367STrond Myklebust nfs_direct_dirty_pages(data->pagevec, 257d4a8f367STrond Myklebust data->args.pgbase, 258d4a8f367STrond Myklebust data->res.count); 259d4a8f367STrond Myklebust } 260d4a8f367STrond Myklebust nfs_direct_release_pages(data->pagevec, data->npages); 2611da177e4SLinus Torvalds 262607f31e8STrond Myklebust if (put_dreq(dreq)) 26363ab46abSChuck Lever nfs_direct_complete(dreq); 2641ae88b2eSTrond Myklebust nfs_readdata_free(data); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 267ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = { 268f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1) 269f11c88afSAndy Adamson .rpc_call_prepare = nfs_read_prepare, 270f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 271ec06c096STrond Myklebust .rpc_call_done = nfs_direct_read_result, 272fdd1e74cSTrond Myklebust .rpc_release = nfs_direct_read_release, 273ec06c096STrond Myklebust }; 274ec06c096STrond Myklebust 275d4cc948bSChuck Lever /* 276607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 277607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 278607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 279607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 280607f31e8STrond Myklebust * no requests have been sent, just return an error. 2811da177e4SLinus Torvalds */ 28202fe4946SChuck Lever static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq, 28302fe4946SChuck Lever const struct iovec *iov, 28402fe4946SChuck Lever loff_t pos) 2851da177e4SLinus Torvalds { 286a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 2873d4ff43dSAl Viro struct inode *inode = ctx->dentry->d_inode; 28802fe4946SChuck Lever unsigned long user_addr = (unsigned long)iov->iov_base; 28902fe4946SChuck Lever size_t count = iov->iov_len; 2905dd602f2SChuck Lever size_t rsize = NFS_SERVER(inode)->rsize; 29107737691STrond Myklebust struct rpc_task *task; 292bdc7f021STrond Myklebust struct rpc_message msg = { 293bdc7f021STrond Myklebust .rpc_cred = ctx->cred, 294bdc7f021STrond Myklebust }; 29584115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 29684115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 297bdc7f021STrond Myklebust .rpc_message = &msg, 29884115e1cSTrond Myklebust .callback_ops = &nfs_read_direct_ops, 299101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 30084115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 30184115e1cSTrond Myklebust }; 302607f31e8STrond Myklebust unsigned int pgbase; 303607f31e8STrond Myklebust int result; 304607f31e8STrond Myklebust ssize_t started = 0; 30582b145c5SChuck Lever 3061da177e4SLinus Torvalds do { 30782b145c5SChuck Lever struct nfs_read_data *data; 3085dd602f2SChuck Lever size_t bytes; 3091da177e4SLinus Torvalds 310e9f7bee1STrond Myklebust pgbase = user_addr & ~PAGE_MASK; 311e9f7bee1STrond Myklebust bytes = min(rsize,count); 312e9f7bee1STrond Myklebust 313607f31e8STrond Myklebust result = -ENOMEM; 3148d5658c9STrond Myklebust data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes)); 315607f31e8STrond Myklebust if (unlikely(!data)) 316607f31e8STrond Myklebust break; 317607f31e8STrond Myklebust 318607f31e8STrond Myklebust down_read(¤t->mm->mmap_sem); 319607f31e8STrond Myklebust result = get_user_pages(current, current->mm, user_addr, 320607f31e8STrond Myklebust data->npages, 1, 0, data->pagevec, NULL); 321607f31e8STrond Myklebust up_read(¤t->mm->mmap_sem); 322749e146eSChuck Lever if (result < 0) { 3231ae88b2eSTrond Myklebust nfs_readdata_free(data); 324749e146eSChuck Lever break; 325749e146eSChuck Lever } 326749e146eSChuck Lever if ((unsigned)result < data->npages) { 327d9df8d6bSTrond Myklebust bytes = result * PAGE_SIZE; 328d9df8d6bSTrond Myklebust if (bytes <= pgbase) { 329607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 3301ae88b2eSTrond Myklebust nfs_readdata_free(data); 331607f31e8STrond Myklebust break; 332607f31e8STrond Myklebust } 333d9df8d6bSTrond Myklebust bytes -= pgbase; 334d9df8d6bSTrond Myklebust data->npages = result; 335d9df8d6bSTrond Myklebust } 33606cf6f2eSChuck Lever 337607f31e8STrond Myklebust get_dreq(dreq); 338607f31e8STrond Myklebust 339607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 3401da177e4SLinus Torvalds data->inode = inode; 341bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 3421da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 3431ae88b2eSTrond Myklebust data->args.context = ctx; 344f11ac8dbSTrond Myklebust data->args.lock_context = dreq->l_ctx; 34588467055SChuck Lever data->args.offset = pos; 3461da177e4SLinus Torvalds data->args.pgbase = pgbase; 347607f31e8STrond Myklebust data->args.pages = data->pagevec; 3481da177e4SLinus Torvalds data->args.count = bytes; 3491da177e4SLinus Torvalds data->res.fattr = &data->fattr; 3501da177e4SLinus Torvalds data->res.eof = 0; 3511da177e4SLinus Torvalds data->res.count = bytes; 35265d26953SChuck Lever nfs_fattr_init(&data->fattr); 353bdc7f021STrond Myklebust msg.rpc_argp = &data->args; 354bdc7f021STrond Myklebust msg.rpc_resp = &data->res; 3551da177e4SLinus Torvalds 35607737691STrond Myklebust task_setup_data.task = &data->task; 35784115e1cSTrond Myklebust task_setup_data.callback_data = data; 358bdc7f021STrond Myklebust NFS_PROTO(inode)->read_setup(data, &msg); 3591da177e4SLinus Torvalds 36007737691STrond Myklebust task = rpc_run_task(&task_setup_data); 361dbae4c73STrond Myklebust if (IS_ERR(task)) 362dbae4c73STrond Myklebust break; 36307737691STrond Myklebust rpc_put_task(task); 3641da177e4SLinus Torvalds 365a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct read call " 366a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 3671da177e4SLinus Torvalds data->task.tk_pid, 3681da177e4SLinus Torvalds inode->i_sb->s_id, 3691da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 3701da177e4SLinus Torvalds bytes, 3711da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3721da177e4SLinus Torvalds 373607f31e8STrond Myklebust started += bytes; 374607f31e8STrond Myklebust user_addr += bytes; 37588467055SChuck Lever pos += bytes; 376e9f7bee1STrond Myklebust /* FIXME: Remove this unnecessary math from final patch */ 3771da177e4SLinus Torvalds pgbase += bytes; 3781da177e4SLinus Torvalds pgbase &= ~PAGE_MASK; 379e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds count -= bytes; 3821da177e4SLinus Torvalds } while (count != 0); 383607f31e8STrond Myklebust 384607f31e8STrond Myklebust if (started) 385c216fd70SChuck Lever return started; 386607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 38706cf6f2eSChuck Lever } 38806cf6f2eSChuck Lever 38919f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 39019f73787SChuck Lever const struct iovec *iov, 39119f73787SChuck Lever unsigned long nr_segs, 39219f73787SChuck Lever loff_t pos) 39319f73787SChuck Lever { 39419f73787SChuck Lever ssize_t result = -EINVAL; 39519f73787SChuck Lever size_t requested_bytes = 0; 39619f73787SChuck Lever unsigned long seg; 39719f73787SChuck Lever 39819f73787SChuck Lever get_dreq(dreq); 39919f73787SChuck Lever 40019f73787SChuck Lever for (seg = 0; seg < nr_segs; seg++) { 40119f73787SChuck Lever const struct iovec *vec = &iov[seg]; 40202fe4946SChuck Lever result = nfs_direct_read_schedule_segment(dreq, vec, pos); 40319f73787SChuck Lever if (result < 0) 40419f73787SChuck Lever break; 40519f73787SChuck Lever requested_bytes += result; 40619f73787SChuck Lever if ((size_t)result < vec->iov_len) 40719f73787SChuck Lever break; 40819f73787SChuck Lever pos += vec->iov_len; 40919f73787SChuck Lever } 41019f73787SChuck Lever 411839f7ad6SChuck Lever /* 412839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 413839f7ad6SChuck Lever * generic layer handle the completion. 414839f7ad6SChuck Lever */ 415839f7ad6SChuck Lever if (requested_bytes == 0) { 416839f7ad6SChuck Lever nfs_direct_req_release(dreq); 417839f7ad6SChuck Lever return result < 0 ? result : -EIO; 418839f7ad6SChuck Lever } 419839f7ad6SChuck Lever 42019f73787SChuck Lever if (put_dreq(dreq)) 42119f73787SChuck Lever nfs_direct_complete(dreq); 42219f73787SChuck Lever return 0; 42319f73787SChuck Lever } 42419f73787SChuck Lever 425c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov, 426c216fd70SChuck Lever unsigned long nr_segs, loff_t pos) 4271da177e4SLinus Torvalds { 428f11ac8dbSTrond Myklebust ssize_t result = -ENOMEM; 42999514f8fSChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 4301da177e4SLinus Torvalds struct nfs_direct_req *dreq; 4311da177e4SLinus Torvalds 432607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 433f11ac8dbSTrond Myklebust if (dreq == NULL) 434f11ac8dbSTrond Myklebust goto out; 4351da177e4SLinus Torvalds 43691d5b470SChuck Lever dreq->inode = inode; 437cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 438f11ac8dbSTrond Myklebust dreq->l_ctx = nfs_get_lock_context(dreq->ctx); 439f11ac8dbSTrond Myklebust if (dreq->l_ctx == NULL) 440f11ac8dbSTrond Myklebust goto out_release; 441487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 442487b8372SChuck Lever dreq->iocb = iocb; 4431da177e4SLinus Torvalds 444c216fd70SChuck Lever result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos); 445607f31e8STrond Myklebust if (!result) 446bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 447f11ac8dbSTrond Myklebust out_release: 448b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 449f11ac8dbSTrond Myklebust out: 4501da177e4SLinus Torvalds return result; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 453fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) 4541da177e4SLinus Torvalds { 455607f31e8STrond Myklebust while (!list_empty(&dreq->rewrite_list)) { 456607f31e8STrond Myklebust struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages); 457fad61490STrond Myklebust list_del(&data->pages); 458607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, data->npages); 4591ae88b2eSTrond Myklebust nfs_writedata_free(data); 460fad61490STrond Myklebust } 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 463fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 464fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 4651da177e4SLinus Torvalds { 466607f31e8STrond Myklebust struct inode *inode = dreq->inode; 467607f31e8STrond Myklebust struct list_head *p; 468607f31e8STrond Myklebust struct nfs_write_data *data; 46907737691STrond Myklebust struct rpc_task *task; 470bdc7f021STrond Myklebust struct rpc_message msg = { 471bdc7f021STrond Myklebust .rpc_cred = dreq->ctx->cred, 472bdc7f021STrond Myklebust }; 47384115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 47484115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 475a8b40bc7STerry Loftin .rpc_message = &msg, 47684115e1cSTrond Myklebust .callback_ops = &nfs_write_direct_ops, 477101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 47884115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 47984115e1cSTrond Myklebust }; 4801da177e4SLinus Torvalds 481fad61490STrond Myklebust dreq->count = 0; 482607f31e8STrond Myklebust get_dreq(dreq); 4831da177e4SLinus Torvalds 484607f31e8STrond Myklebust list_for_each(p, &dreq->rewrite_list) { 485607f31e8STrond Myklebust data = list_entry(p, struct nfs_write_data, pages); 486607f31e8STrond Myklebust 487607f31e8STrond Myklebust get_dreq(dreq); 488607f31e8STrond Myklebust 489bdc7f021STrond Myklebust /* Use stable writes */ 490bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 491bdc7f021STrond Myklebust 492607f31e8STrond Myklebust /* 493607f31e8STrond Myklebust * Reset data->res. 494607f31e8STrond Myklebust */ 495607f31e8STrond Myklebust nfs_fattr_init(&data->fattr); 496607f31e8STrond Myklebust data->res.count = data->args.count; 497607f31e8STrond Myklebust memset(&data->verf, 0, sizeof(data->verf)); 498607f31e8STrond Myklebust 499607f31e8STrond Myklebust /* 500607f31e8STrond Myklebust * Reuse data->task; data->args should not have changed 501607f31e8STrond Myklebust * since the original request was sent. 502607f31e8STrond Myklebust */ 50307737691STrond Myklebust task_setup_data.task = &data->task; 50484115e1cSTrond Myklebust task_setup_data.callback_data = data; 505bdc7f021STrond Myklebust msg.rpc_argp = &data->args; 506bdc7f021STrond Myklebust msg.rpc_resp = &data->res; 507bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 508607f31e8STrond Myklebust 509607f31e8STrond Myklebust /* 510607f31e8STrond Myklebust * We're called via an RPC callback, so BKL is already held. 511607f31e8STrond Myklebust */ 51207737691STrond Myklebust task = rpc_run_task(&task_setup_data); 51307737691STrond Myklebust if (!IS_ERR(task)) 51407737691STrond Myklebust rpc_put_task(task); 515607f31e8STrond Myklebust 516607f31e8STrond Myklebust dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 517607f31e8STrond Myklebust data->task.tk_pid, 518607f31e8STrond Myklebust inode->i_sb->s_id, 519607f31e8STrond Myklebust (long long)NFS_FILEID(inode), 520607f31e8STrond Myklebust data->args.count, 521607f31e8STrond Myklebust (unsigned long long)data->args.offset); 522607f31e8STrond Myklebust } 523607f31e8STrond Myklebust 524607f31e8STrond Myklebust if (put_dreq(dreq)) 525607f31e8STrond Myklebust nfs_direct_write_complete(dreq, inode); 526fad61490STrond Myklebust } 5271da177e4SLinus Torvalds 528fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) 529fad61490STrond Myklebust { 530fad61490STrond Myklebust struct nfs_write_data *data = calldata; 5311da177e4SLinus Torvalds 532fad61490STrond Myklebust /* Call the NFS version-specific code */ 533c9d8f89dSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 534c9d8f89dSTrond Myklebust } 535c9d8f89dSTrond Myklebust 536c9d8f89dSTrond Myklebust static void nfs_direct_commit_release(void *calldata) 537c9d8f89dSTrond Myklebust { 538c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 539c9d8f89dSTrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 540c9d8f89dSTrond Myklebust int status = data->task.tk_status; 541c9d8f89dSTrond Myklebust 542c9d8f89dSTrond Myklebust if (status < 0) { 54360fa3f76STrond Myklebust dprintk("NFS: %5u commit failed with error %d.\n", 544c9d8f89dSTrond Myklebust data->task.tk_pid, status); 545fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 54660fa3f76STrond Myklebust } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 547c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid); 548fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 549fad61490STrond Myklebust } 550fad61490STrond Myklebust 551c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status); 552fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 5531ae88b2eSTrond Myklebust nfs_commit_free(data); 554fad61490STrond Myklebust } 555fad61490STrond Myklebust 556fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = { 55721d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1) 55821d9a851SAndy Adamson .rpc_call_prepare = nfs_write_prepare, 55921d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 560fad61490STrond Myklebust .rpc_call_done = nfs_direct_commit_result, 561c9d8f89dSTrond Myklebust .rpc_release = nfs_direct_commit_release, 562fad61490STrond Myklebust }; 563fad61490STrond Myklebust 564fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 565fad61490STrond Myklebust { 566fad61490STrond Myklebust struct nfs_write_data *data = dreq->commit_data; 56707737691STrond Myklebust struct rpc_task *task; 568bdc7f021STrond Myklebust struct rpc_message msg = { 569bdc7f021STrond Myklebust .rpc_argp = &data->args, 570bdc7f021STrond Myklebust .rpc_resp = &data->res, 571bdc7f021STrond Myklebust .rpc_cred = dreq->ctx->cred, 572bdc7f021STrond Myklebust }; 57384115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 57407737691STrond Myklebust .task = &data->task, 57584115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(dreq->inode), 576bdc7f021STrond Myklebust .rpc_message = &msg, 57784115e1cSTrond Myklebust .callback_ops = &nfs_commit_direct_ops, 57884115e1cSTrond Myklebust .callback_data = data, 579101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 58084115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 58184115e1cSTrond Myklebust }; 582fad61490STrond Myklebust 583fad61490STrond Myklebust data->inode = dreq->inode; 584bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 585fad61490STrond Myklebust 586fad61490STrond Myklebust data->args.fh = NFS_FH(data->inode); 587607f31e8STrond Myklebust data->args.offset = 0; 588607f31e8STrond Myklebust data->args.count = 0; 5891ae88b2eSTrond Myklebust data->args.context = dreq->ctx; 590f11ac8dbSTrond Myklebust data->args.lock_context = dreq->l_ctx; 591fad61490STrond Myklebust data->res.count = 0; 592fad61490STrond Myklebust data->res.fattr = &data->fattr; 593fad61490STrond Myklebust data->res.verf = &data->verf; 59465d26953SChuck Lever nfs_fattr_init(&data->fattr); 595fad61490STrond Myklebust 596bdc7f021STrond Myklebust NFS_PROTO(data->inode)->commit_setup(data, &msg); 597fad61490STrond Myklebust 598fad61490STrond Myklebust /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ 599fad61490STrond Myklebust dreq->commit_data = NULL; 600fad61490STrond Myklebust 601e99170ffSTrond Myklebust dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 6021da177e4SLinus Torvalds 60307737691STrond Myklebust task = rpc_run_task(&task_setup_data); 60407737691STrond Myklebust if (!IS_ERR(task)) 60507737691STrond Myklebust rpc_put_task(task); 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 608fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 6091da177e4SLinus Torvalds { 610fad61490STrond Myklebust int flags = dreq->flags; 6111da177e4SLinus Torvalds 612fad61490STrond Myklebust dreq->flags = 0; 613fad61490STrond Myklebust switch (flags) { 614fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 615fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 6161da177e4SLinus Torvalds break; 617fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 618fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 6191da177e4SLinus Torvalds break; 6201da177e4SLinus Torvalds default: 621fad61490STrond Myklebust if (dreq->commit_data != NULL) 622fad61490STrond Myklebust nfs_commit_free(dreq->commit_data); 623fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 624cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 625fad61490STrond Myklebust nfs_direct_complete(dreq); 6261da177e4SLinus Torvalds } 627fad61490STrond Myklebust } 628fad61490STrond Myklebust 629fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 630fad61490STrond Myklebust { 631c9d8f89dSTrond Myklebust dreq->commit_data = nfs_commitdata_alloc(); 632fad61490STrond Myklebust if (dreq->commit_data != NULL) 633fad61490STrond Myklebust dreq->commit_data->req = (struct nfs_page *) dreq; 634fad61490STrond Myklebust } 635fad61490STrond Myklebust #else 636fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 637fad61490STrond Myklebust { 638fad61490STrond Myklebust dreq->commit_data = NULL; 639fad61490STrond Myklebust } 640fad61490STrond Myklebust 641fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 642fad61490STrond Myklebust { 643fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 644cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 645fad61490STrond Myklebust nfs_direct_complete(dreq); 646fad61490STrond Myklebust } 647fad61490STrond Myklebust #endif 648fad61490STrond Myklebust 649462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata) 650462d5b32SChuck Lever { 651462d5b32SChuck Lever struct nfs_write_data *data = calldata; 652462d5b32SChuck Lever 65383762c56SFred Isaman nfs_writeback_done(task, data); 654c9d8f89dSTrond Myklebust } 655c9d8f89dSTrond Myklebust 656c9d8f89dSTrond Myklebust /* 657c9d8f89dSTrond Myklebust * NB: Return the value of the first error return code. Subsequent 658c9d8f89dSTrond Myklebust * errors after the first one are ignored. 659c9d8f89dSTrond Myklebust */ 660c9d8f89dSTrond Myklebust static void nfs_direct_write_release(void *calldata) 661c9d8f89dSTrond Myklebust { 662c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 663c9d8f89dSTrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 664c9d8f89dSTrond Myklebust int status = data->task.tk_status; 665462d5b32SChuck Lever 66615ce4a0cSChuck Lever spin_lock(&dreq->lock); 667462d5b32SChuck Lever 66860fa3f76STrond Myklebust if (unlikely(status < 0)) { 669432409eeSNeil Brown /* An error has occurred, so we should not commit */ 67060fa3f76STrond Myklebust dreq->flags = 0; 67160fa3f76STrond Myklebust dreq->error = status; 672eda3cef8STrond Myklebust } 673432409eeSNeil Brown if (unlikely(dreq->error != 0)) 674432409eeSNeil Brown goto out_unlock; 675eda3cef8STrond Myklebust 67615ce4a0cSChuck Lever dreq->count += data->res.count; 67715ce4a0cSChuck Lever 678fad61490STrond Myklebust if (data->res.verf->committed != NFS_FILE_SYNC) { 679fad61490STrond Myklebust switch (dreq->flags) { 680fad61490STrond Myklebust case 0: 681fad61490STrond Myklebust memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); 682fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_DO_COMMIT; 683fad61490STrond Myklebust break; 684fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 685fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { 686c9d8f89dSTrond Myklebust dprintk("NFS: %5u write verify failed\n", data->task.tk_pid); 687fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 688fad61490STrond Myklebust } 689fad61490STrond Myklebust } 690fad61490STrond Myklebust } 691eda3cef8STrond Myklebust out_unlock: 692fad61490STrond Myklebust spin_unlock(&dreq->lock); 693fad61490STrond Myklebust 694607f31e8STrond Myklebust if (put_dreq(dreq)) 695fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 696462d5b32SChuck Lever } 697462d5b32SChuck Lever 698462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = { 699def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 700def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 701def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 702462d5b32SChuck Lever .rpc_call_done = nfs_direct_write_result, 703fad61490STrond Myklebust .rpc_release = nfs_direct_write_release, 704462d5b32SChuck Lever }; 705462d5b32SChuck Lever 706462d5b32SChuck Lever /* 707607f31e8STrond Myklebust * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 708607f31e8STrond Myklebust * operation. If nfs_writedata_alloc() or get_user_pages() fails, 709607f31e8STrond Myklebust * bail and stop sending more writes. Write length accounting is 710607f31e8STrond Myklebust * handled automatically by nfs_direct_write_result(). Otherwise, if 711607f31e8STrond Myklebust * no requests have been sent, just return an error. 712462d5b32SChuck Lever */ 71302fe4946SChuck Lever static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq, 71402fe4946SChuck Lever const struct iovec *iov, 71502fe4946SChuck Lever loff_t pos, int sync) 716462d5b32SChuck Lever { 717a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 7183d4ff43dSAl Viro struct inode *inode = ctx->dentry->d_inode; 71902fe4946SChuck Lever unsigned long user_addr = (unsigned long)iov->iov_base; 72002fe4946SChuck Lever size_t count = iov->iov_len; 72107737691STrond Myklebust struct rpc_task *task; 722bdc7f021STrond Myklebust struct rpc_message msg = { 723bdc7f021STrond Myklebust .rpc_cred = ctx->cred, 724bdc7f021STrond Myklebust }; 72584115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 72684115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 727bdc7f021STrond Myklebust .rpc_message = &msg, 72884115e1cSTrond Myklebust .callback_ops = &nfs_write_direct_ops, 729101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 73084115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC, 73184115e1cSTrond Myklebust }; 732462d5b32SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 733607f31e8STrond Myklebust unsigned int pgbase; 734607f31e8STrond Myklebust int result; 735607f31e8STrond Myklebust ssize_t started = 0; 73682b145c5SChuck Lever 737462d5b32SChuck Lever do { 73882b145c5SChuck Lever struct nfs_write_data *data; 739462d5b32SChuck Lever size_t bytes; 740462d5b32SChuck Lever 741e9f7bee1STrond Myklebust pgbase = user_addr & ~PAGE_MASK; 742e9f7bee1STrond Myklebust bytes = min(wsize,count); 743e9f7bee1STrond Myklebust 744607f31e8STrond Myklebust result = -ENOMEM; 7458d5658c9STrond Myklebust data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes)); 746607f31e8STrond Myklebust if (unlikely(!data)) 747607f31e8STrond Myklebust break; 748607f31e8STrond Myklebust 749607f31e8STrond Myklebust down_read(¤t->mm->mmap_sem); 750607f31e8STrond Myklebust result = get_user_pages(current, current->mm, user_addr, 751607f31e8STrond Myklebust data->npages, 0, 0, data->pagevec, NULL); 752607f31e8STrond Myklebust up_read(¤t->mm->mmap_sem); 753749e146eSChuck Lever if (result < 0) { 7541ae88b2eSTrond Myklebust nfs_writedata_free(data); 755749e146eSChuck Lever break; 756749e146eSChuck Lever } 757749e146eSChuck Lever if ((unsigned)result < data->npages) { 758d9df8d6bSTrond Myklebust bytes = result * PAGE_SIZE; 759d9df8d6bSTrond Myklebust if (bytes <= pgbase) { 760607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 7611ae88b2eSTrond Myklebust nfs_writedata_free(data); 762607f31e8STrond Myklebust break; 763607f31e8STrond Myklebust } 764d9df8d6bSTrond Myklebust bytes -= pgbase; 765d9df8d6bSTrond Myklebust data->npages = result; 766d9df8d6bSTrond Myklebust } 767607f31e8STrond Myklebust 768607f31e8STrond Myklebust get_dreq(dreq); 769607f31e8STrond Myklebust 770fad61490STrond Myklebust list_move_tail(&data->pages, &dreq->rewrite_list); 771462d5b32SChuck Lever 772607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 773462d5b32SChuck Lever data->inode = inode; 774bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 775462d5b32SChuck Lever data->args.fh = NFS_FH(inode); 7761ae88b2eSTrond Myklebust data->args.context = ctx; 777f11ac8dbSTrond Myklebust data->args.lock_context = dreq->l_ctx; 77888467055SChuck Lever data->args.offset = pos; 779462d5b32SChuck Lever data->args.pgbase = pgbase; 780607f31e8STrond Myklebust data->args.pages = data->pagevec; 781462d5b32SChuck Lever data->args.count = bytes; 782bdc7f021STrond Myklebust data->args.stable = sync; 783462d5b32SChuck Lever data->res.fattr = &data->fattr; 784462d5b32SChuck Lever data->res.count = bytes; 78547989d74SChuck Lever data->res.verf = &data->verf; 78665d26953SChuck Lever nfs_fattr_init(&data->fattr); 787462d5b32SChuck Lever 78807737691STrond Myklebust task_setup_data.task = &data->task; 78984115e1cSTrond Myklebust task_setup_data.callback_data = data; 790bdc7f021STrond Myklebust msg.rpc_argp = &data->args; 791bdc7f021STrond Myklebust msg.rpc_resp = &data->res; 792bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 793462d5b32SChuck Lever 79407737691STrond Myklebust task = rpc_run_task(&task_setup_data); 795dbae4c73STrond Myklebust if (IS_ERR(task)) 796dbae4c73STrond Myklebust break; 79707737691STrond Myklebust rpc_put_task(task); 7981da177e4SLinus Torvalds 799a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct write call " 800a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 801462d5b32SChuck Lever data->task.tk_pid, 802462d5b32SChuck Lever inode->i_sb->s_id, 803462d5b32SChuck Lever (long long)NFS_FILEID(inode), 804462d5b32SChuck Lever bytes, 805462d5b32SChuck Lever (unsigned long long)data->args.offset); 806462d5b32SChuck Lever 807607f31e8STrond Myklebust started += bytes; 808607f31e8STrond Myklebust user_addr += bytes; 80988467055SChuck Lever pos += bytes; 810e9f7bee1STrond Myklebust 811e9f7bee1STrond Myklebust /* FIXME: Remove this useless math from the final patch */ 812462d5b32SChuck Lever pgbase += bytes; 813462d5b32SChuck Lever pgbase &= ~PAGE_MASK; 814e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 815462d5b32SChuck Lever 816462d5b32SChuck Lever count -= bytes; 817462d5b32SChuck Lever } while (count != 0); 818607f31e8STrond Myklebust 819607f31e8STrond Myklebust if (started) 820c216fd70SChuck Lever return started; 821607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 82206cf6f2eSChuck Lever } 82306cf6f2eSChuck Lever 82419f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 82519f73787SChuck Lever const struct iovec *iov, 82619f73787SChuck Lever unsigned long nr_segs, 82719f73787SChuck Lever loff_t pos, int sync) 82819f73787SChuck Lever { 82919f73787SChuck Lever ssize_t result = 0; 83019f73787SChuck Lever size_t requested_bytes = 0; 83119f73787SChuck Lever unsigned long seg; 83219f73787SChuck Lever 83319f73787SChuck Lever get_dreq(dreq); 83419f73787SChuck Lever 83519f73787SChuck Lever for (seg = 0; seg < nr_segs; seg++) { 83619f73787SChuck Lever const struct iovec *vec = &iov[seg]; 83702fe4946SChuck Lever result = nfs_direct_write_schedule_segment(dreq, vec, 83819f73787SChuck Lever pos, sync); 83919f73787SChuck Lever if (result < 0) 84019f73787SChuck Lever break; 84119f73787SChuck Lever requested_bytes += result; 84219f73787SChuck Lever if ((size_t)result < vec->iov_len) 84319f73787SChuck Lever break; 84419f73787SChuck Lever pos += vec->iov_len; 84519f73787SChuck Lever } 84619f73787SChuck Lever 847839f7ad6SChuck Lever /* 848839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 849839f7ad6SChuck Lever * generic layer handle the completion. 850839f7ad6SChuck Lever */ 851839f7ad6SChuck Lever if (requested_bytes == 0) { 852839f7ad6SChuck Lever nfs_direct_req_release(dreq); 853839f7ad6SChuck Lever return result < 0 ? result : -EIO; 854839f7ad6SChuck Lever } 855839f7ad6SChuck Lever 85619f73787SChuck Lever if (put_dreq(dreq)) 85719f73787SChuck Lever nfs_direct_write_complete(dreq, dreq->inode); 85819f73787SChuck Lever return 0; 85919f73787SChuck Lever } 86019f73787SChuck Lever 861c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov, 862c216fd70SChuck Lever unsigned long nr_segs, loff_t pos, 863c216fd70SChuck Lever size_t count) 864462d5b32SChuck Lever { 865f11ac8dbSTrond Myklebust ssize_t result = -ENOMEM; 866c89f2ee5SChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 867462d5b32SChuck Lever struct nfs_direct_req *dreq; 868fad61490STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize; 869bdc7f021STrond Myklebust int sync = NFS_UNSTABLE; 870462d5b32SChuck Lever 871607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 872462d5b32SChuck Lever if (!dreq) 873f11ac8dbSTrond Myklebust goto out; 874607f31e8STrond Myklebust nfs_alloc_commit_data(dreq); 875607f31e8STrond Myklebust 876b47d19deSArun Bharadwaj if (dreq->commit_data == NULL || count <= wsize) 877bdc7f021STrond Myklebust sync = NFS_FILE_SYNC; 878462d5b32SChuck Lever 879c89f2ee5SChuck Lever dreq->inode = inode; 880cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 881f11ac8dbSTrond Myklebust dreq->l_ctx = nfs_get_lock_context(dreq->ctx); 882568a810dSSteve Dickson if (dreq->l_ctx == NULL) 883f11ac8dbSTrond Myklebust goto out_release; 884c89f2ee5SChuck Lever if (!is_sync_kiocb(iocb)) 885c89f2ee5SChuck Lever dreq->iocb = iocb; 886462d5b32SChuck Lever 887c216fd70SChuck Lever result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync); 888607f31e8STrond Myklebust if (!result) 889c89f2ee5SChuck Lever result = nfs_direct_wait(dreq); 890f11ac8dbSTrond Myklebust out_release: 891b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 892f11ac8dbSTrond Myklebust out: 8931da177e4SLinus Torvalds return result; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /** 8971da177e4SLinus Torvalds * nfs_file_direct_read - file direct read operation for NFS files 8981da177e4SLinus Torvalds * @iocb: target I/O control block 899027445c3SBadari Pulavarty * @iov: vector of user buffers into which to read data 900027445c3SBadari Pulavarty * @nr_segs: size of iov vector 90188467055SChuck Lever * @pos: byte offset in file where reading starts 9021da177e4SLinus Torvalds * 9031da177e4SLinus Torvalds * We use this function for direct reads instead of calling 9041da177e4SLinus Torvalds * generic_file_aio_read() in order to avoid gfar's check to see if 9051da177e4SLinus Torvalds * the request starts before the end of the file. For that check 9061da177e4SLinus Torvalds * to work, we must generate a GETATTR before each direct read, and 9071da177e4SLinus Torvalds * even then there is a window between the GETATTR and the subsequent 90888467055SChuck Lever * READ where the file size could change. Our preference is simply 9091da177e4SLinus Torvalds * to do all reads the application wants, and the server will take 9101da177e4SLinus Torvalds * care of managing the end of file boundary. 9111da177e4SLinus Torvalds * 9121da177e4SLinus Torvalds * This function also eliminates unnecessarily updating the file's 9131da177e4SLinus Torvalds * atime locally, as the NFS server sets the file's atime, and this 9141da177e4SLinus Torvalds * client must read the updated atime from the server back into its 9151da177e4SLinus Torvalds * cache. 9161da177e4SLinus Torvalds */ 917027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov, 918027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds ssize_t retval = -EINVAL; 9211da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9221da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 923c216fd70SChuck Lever size_t count; 9241da177e4SLinus Torvalds 925c216fd70SChuck Lever count = iov_length(iov, nr_segs); 926c216fd70SChuck Lever nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 927c216fd70SChuck Lever 9286da24bc9SChuck Lever dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n", 92901cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 93001cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 931c216fd70SChuck Lever count, (long long) pos); 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds retval = 0; 9341da177e4SLinus Torvalds if (!count) 9351da177e4SLinus Torvalds goto out; 9361da177e4SLinus Torvalds 93729884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 9381da177e4SLinus Torvalds if (retval) 9391da177e4SLinus Torvalds goto out; 9401da177e4SLinus Torvalds 9417ec10f26SKonstantin Khlebnikov task_io_account_read(count); 9427ec10f26SKonstantin Khlebnikov 943c216fd70SChuck Lever retval = nfs_direct_read(iocb, iov, nr_segs, pos); 9441da177e4SLinus Torvalds if (retval > 0) 9450cdd80d0SChuck Lever iocb->ki_pos = pos + retval; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds out: 9481da177e4SLinus Torvalds return retval; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds /** 9521da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 9531da177e4SLinus Torvalds * @iocb: target I/O control block 954027445c3SBadari Pulavarty * @iov: vector of user buffers from which to write data 955027445c3SBadari Pulavarty * @nr_segs: size of iov vector 95688467055SChuck Lever * @pos: byte offset in file where writing starts 9571da177e4SLinus Torvalds * 9581da177e4SLinus Torvalds * We use this function for direct writes instead of calling 9591da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 9601da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 9611da177e4SLinus Torvalds * the new i_size and this client must read the updated size 9621da177e4SLinus Torvalds * back into its cache. We let the server do generic write 9631da177e4SLinus Torvalds * parameter checking and report problems. 9641da177e4SLinus Torvalds * 9651da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 9661da177e4SLinus Torvalds * 9671da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 9681da177e4SLinus Torvalds * readers of this file. 9691da177e4SLinus Torvalds * 9701da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 9711da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 9721da177e4SLinus Torvalds */ 973027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov, 974027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 9751da177e4SLinus Torvalds { 976070ea602SChuck Lever ssize_t retval = -EINVAL; 9771da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9781da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 979c216fd70SChuck Lever size_t count; 9801da177e4SLinus Torvalds 981c216fd70SChuck Lever count = iov_length(iov, nr_segs); 982c216fd70SChuck Lever nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 983c216fd70SChuck Lever 9846da24bc9SChuck Lever dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n", 98501cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 98601cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 987c216fd70SChuck Lever count, (long long) pos); 988027445c3SBadari Pulavarty 989ce1a8e67SChuck Lever retval = generic_write_checks(file, &pos, &count, 0); 990ce1a8e67SChuck Lever if (retval) 9911da177e4SLinus Torvalds goto out; 992ce1a8e67SChuck Lever 993ce1a8e67SChuck Lever retval = -EINVAL; 994ce1a8e67SChuck Lever if ((ssize_t) count < 0) 9951da177e4SLinus Torvalds goto out; 9961da177e4SLinus Torvalds retval = 0; 9971da177e4SLinus Torvalds if (!count) 9981da177e4SLinus Torvalds goto out; 999ce1a8e67SChuck Lever 100029884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 10011da177e4SLinus Torvalds if (retval) 10021da177e4SLinus Torvalds goto out; 10031da177e4SLinus Torvalds 10047ec10f26SKonstantin Khlebnikov task_io_account_write(count); 10057ec10f26SKonstantin Khlebnikov 1006c216fd70SChuck Lever retval = nfs_direct_write(iocb, iov, nr_segs, pos, count); 10079eafa8ccSChuck Lever 10081da177e4SLinus Torvalds if (retval > 0) 1009ce1a8e67SChuck Lever iocb->ki_pos = pos + retval; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds out: 10121da177e4SLinus Torvalds return retval; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 101588467055SChuck Lever /** 101688467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 101788467055SChuck Lever * 101888467055SChuck Lever */ 1019f7b422b1SDavid Howells int __init nfs_init_directcache(void) 10201da177e4SLinus Torvalds { 10211da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 10221da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 1023fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 1024fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 102520c2df83SPaul Mundt NULL); 10261da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 10271da177e4SLinus Torvalds return -ENOMEM; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds return 0; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 103288467055SChuck Lever /** 1033f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 103488467055SChuck Lever * 103588467055SChuck Lever */ 1036266bee88SDavid Brownell void nfs_destroy_directcache(void) 10371da177e4SLinus Torvalds { 10381a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 10391da177e4SLinus Torvalds } 1040