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> 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #include <linux/nfs_fs.h> 491da177e4SLinus Torvalds #include <linux/nfs_page.h> 501da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <asm/system.h> 531da177e4SLinus Torvalds #include <asm/uaccess.h> 541da177e4SLinus Torvalds #include <asm/atomic.h> 551da177e4SLinus Torvalds 568d5658c9STrond Myklebust #include "internal.h" 5791d5b470SChuck Lever #include "iostat.h" 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 601da177e4SLinus Torvalds 61e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* 641da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds struct nfs_direct_req { 671da177e4SLinus Torvalds struct kref kref; /* release manager */ 6815ce4a0cSChuck Lever 6915ce4a0cSChuck Lever /* I/O parameters */ 70a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 7199514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 7288467055SChuck Lever struct inode * inode; /* target file of i/o */ 7315ce4a0cSChuck Lever 7415ce4a0cSChuck Lever /* completion state */ 75607f31e8STrond Myklebust atomic_t io_count; /* i/os we're waiting for */ 7615ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 7715ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 781da177e4SLinus Torvalds error; /* any reported error */ 79d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 80fad61490STrond Myklebust 81fad61490STrond Myklebust /* commit state */ 82607f31e8STrond Myklebust struct list_head rewrite_list; /* saved nfs_write_data structs */ 83fad61490STrond Myklebust struct nfs_write_data * commit_data; /* special write_data for commits */ 84fad61490STrond Myklebust int flags; 85fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 86fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 87fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 881da177e4SLinus Torvalds }; 891da177e4SLinus Torvalds 90fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 91607f31e8STrond Myklebust static const struct rpc_call_ops nfs_write_direct_ops; 92607f31e8STrond Myklebust 93607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 94607f31e8STrond Myklebust { 95607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 96607f31e8STrond Myklebust } 97607f31e8STrond Myklebust 98607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 99607f31e8STrond Myklebust { 100607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 101607f31e8STrond Myklebust } 102607f31e8STrond Myklebust 1031da177e4SLinus Torvalds /** 104b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 105b8a32e2bSChuck Lever * @rw: direction (read or write) 106b8a32e2bSChuck Lever * @iocb: target I/O control block 107b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 108b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 109b8a32e2bSChuck Lever * @nr_segs: size of iovec array 110b8a32e2bSChuck Lever * 111b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 112b8a32e2bSChuck Lever * the NFS client supports direct I/O. However, we shunt off direct 113b8a32e2bSChuck Lever * read and write requests before the VFS gets them, so this method 114b8a32e2bSChuck Lever * should never be called. 1151da177e4SLinus Torvalds */ 116b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) 117b8a32e2bSChuck Lever { 118b8a32e2bSChuck Lever dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", 11901cce933SJosef "Jeff" Sipek iocb->ki_filp->f_path.dentry->d_name.name, 120e99170ffSTrond Myklebust (long long) pos, nr_segs); 121b8a32e2bSChuck Lever 122b8a32e2bSChuck Lever return -EINVAL; 123b8a32e2bSChuck Lever } 124b8a32e2bSChuck Lever 125d4a8f367STrond Myklebust static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count) 1266b45d858STrond Myklebust { 127d4a8f367STrond Myklebust unsigned int npages; 128749e146eSChuck Lever unsigned int i; 129d4a8f367STrond Myklebust 130d4a8f367STrond Myklebust if (count == 0) 131d4a8f367STrond Myklebust return; 132d4a8f367STrond Myklebust pages += (pgbase >> PAGE_SHIFT); 133d4a8f367STrond Myklebust npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT; 1346b45d858STrond Myklebust for (i = 0; i < npages; i++) { 1356b45d858STrond Myklebust struct page *page = pages[i]; 136607f31e8STrond Myklebust if (!PageCompound(page)) 137d4a8f367STrond Myklebust set_page_dirty(page); 1386b45d858STrond Myklebust } 1399c93ab7dSChuck Lever } 1409c93ab7dSChuck Lever 141749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 1429c93ab7dSChuck Lever { 143749e146eSChuck Lever unsigned int i; 144607f31e8STrond Myklebust for (i = 0; i < npages; i++) 145607f31e8STrond Myklebust page_cache_release(pages[i]); 1466b45d858STrond Myklebust } 1476b45d858STrond Myklebust 14893619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds struct nfs_direct_req *dreq; 1511da177e4SLinus Torvalds 152e94b1766SChristoph Lameter dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL); 1531da177e4SLinus Torvalds if (!dreq) 1541da177e4SLinus Torvalds return NULL; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds kref_init(&dreq->kref); 157607f31e8STrond Myklebust kref_get(&dreq->kref); 158d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 159fad61490STrond Myklebust INIT_LIST_HEAD(&dreq->rewrite_list); 16093619e59SChuck Lever dreq->iocb = NULL; 161a8881f5aSTrond Myklebust dreq->ctx = NULL; 16215ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 163607f31e8STrond Myklebust atomic_set(&dreq->io_count, 0); 16415ce4a0cSChuck Lever dreq->count = 0; 16515ce4a0cSChuck Lever dreq->error = 0; 166fad61490STrond Myklebust dreq->flags = 0; 16793619e59SChuck Lever 16893619e59SChuck Lever return dreq; 16993619e59SChuck Lever } 17093619e59SChuck Lever 171b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 174a8881f5aSTrond Myklebust 175a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 176a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 1771da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 180b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 181b4946ffbSTrond Myklebust { 182b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 183b4946ffbSTrond Myklebust } 184b4946ffbSTrond Myklebust 185d4cc948bSChuck Lever /* 186bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 187bc0fb201SChuck Lever */ 188bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 189bc0fb201SChuck Lever { 19015ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 191bc0fb201SChuck Lever 192bc0fb201SChuck Lever /* Async requests don't wait here */ 193bc0fb201SChuck Lever if (dreq->iocb) 194bc0fb201SChuck Lever goto out; 195bc0fb201SChuck Lever 196d72b7a6bSTrond Myklebust result = wait_for_completion_interruptible(&dreq->completion); 197bc0fb201SChuck Lever 198bc0fb201SChuck Lever if (!result) 19915ce4a0cSChuck Lever result = dreq->error; 200bc0fb201SChuck Lever if (!result) 20115ce4a0cSChuck Lever result = dreq->count; 202bc0fb201SChuck Lever 203bc0fb201SChuck Lever out: 204bc0fb201SChuck Lever return (ssize_t) result; 205bc0fb201SChuck Lever } 206bc0fb201SChuck Lever 207bc0fb201SChuck Lever /* 208607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 209607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 21063ab46abSChuck Lever */ 21163ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq) 21263ab46abSChuck Lever { 21363ab46abSChuck Lever if (dreq->iocb) { 21415ce4a0cSChuck Lever long res = (long) dreq->error; 21563ab46abSChuck Lever if (!res) 21615ce4a0cSChuck Lever res = (long) dreq->count; 21763ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 218d72b7a6bSTrond Myklebust } 219d72b7a6bSTrond Myklebust complete_all(&dreq->completion); 22063ab46abSChuck Lever 221b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 22263ab46abSChuck Lever } 22363ab46abSChuck Lever 22463ab46abSChuck Lever /* 225607f31e8STrond Myklebust * We must hold a reference to all the pages in this direct read request 226607f31e8STrond Myklebust * until the RPCs complete. This could be long *after* we are woken up in 227607f31e8STrond Myklebust * nfs_direct_wait (for instance, if someone hits ^C on a slow server). 22806cf6f2eSChuck Lever */ 229ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata) 2301da177e4SLinus Torvalds { 231ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 2321da177e4SLinus Torvalds struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 2331da177e4SLinus Torvalds 234ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 235ec06c096STrond Myklebust return; 2361da177e4SLinus Torvalds 23715ce4a0cSChuck Lever spin_lock(&dreq->lock); 238d4a8f367STrond Myklebust if (unlikely(task->tk_status < 0)) { 23915ce4a0cSChuck Lever dreq->error = task->tk_status; 24015ce4a0cSChuck Lever spin_unlock(&dreq->lock); 241d4a8f367STrond Myklebust } else { 242d4a8f367STrond Myklebust dreq->count += data->res.count; 243d4a8f367STrond Myklebust spin_unlock(&dreq->lock); 244d4a8f367STrond Myklebust nfs_direct_dirty_pages(data->pagevec, 245d4a8f367STrond Myklebust data->args.pgbase, 246d4a8f367STrond Myklebust data->res.count); 247d4a8f367STrond Myklebust } 248d4a8f367STrond Myklebust nfs_direct_release_pages(data->pagevec, data->npages); 2491da177e4SLinus Torvalds 250607f31e8STrond Myklebust if (put_dreq(dreq)) 25163ab46abSChuck Lever nfs_direct_complete(dreq); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 254ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = { 255ec06c096STrond Myklebust .rpc_call_done = nfs_direct_read_result, 256ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 257ec06c096STrond Myklebust }; 258ec06c096STrond Myklebust 259d4cc948bSChuck Lever /* 260607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 261607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 262607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 263607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 264607f31e8STrond Myklebust * no requests have been sent, just return an error. 2651da177e4SLinus Torvalds */ 266607f31e8STrond Myklebust static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos) 2671da177e4SLinus Torvalds { 268a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 26988be9f99STrond Myklebust struct inode *inode = ctx->path.dentry->d_inode; 2705dd602f2SChuck Lever size_t rsize = NFS_SERVER(inode)->rsize; 271607f31e8STrond Myklebust unsigned int pgbase; 272607f31e8STrond Myklebust int result; 273607f31e8STrond Myklebust ssize_t started = 0; 27482b145c5SChuck Lever 275607f31e8STrond Myklebust get_dreq(dreq); 276607f31e8STrond Myklebust 2771da177e4SLinus Torvalds do { 27882b145c5SChuck Lever struct nfs_read_data *data; 2795dd602f2SChuck Lever size_t bytes; 2801da177e4SLinus Torvalds 281e9f7bee1STrond Myklebust pgbase = user_addr & ~PAGE_MASK; 282e9f7bee1STrond Myklebust bytes = min(rsize,count); 283e9f7bee1STrond Myklebust 284607f31e8STrond Myklebust result = -ENOMEM; 2858d5658c9STrond Myklebust data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes)); 286607f31e8STrond Myklebust if (unlikely(!data)) 287607f31e8STrond Myklebust break; 288607f31e8STrond Myklebust 289607f31e8STrond Myklebust down_read(¤t->mm->mmap_sem); 290607f31e8STrond Myklebust result = get_user_pages(current, current->mm, user_addr, 291607f31e8STrond Myklebust data->npages, 1, 0, data->pagevec, NULL); 292607f31e8STrond Myklebust up_read(¤t->mm->mmap_sem); 293749e146eSChuck Lever if (result < 0) { 294749e146eSChuck Lever nfs_readdata_release(data); 295749e146eSChuck Lever break; 296749e146eSChuck Lever } 297749e146eSChuck Lever if ((unsigned)result < data->npages) { 298d9df8d6bSTrond Myklebust bytes = result * PAGE_SIZE; 299d9df8d6bSTrond Myklebust if (bytes <= pgbase) { 300607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 301607f31e8STrond Myklebust nfs_readdata_release(data); 302607f31e8STrond Myklebust break; 303607f31e8STrond Myklebust } 304d9df8d6bSTrond Myklebust bytes -= pgbase; 305d9df8d6bSTrond Myklebust data->npages = result; 306d9df8d6bSTrond Myklebust } 30706cf6f2eSChuck Lever 308607f31e8STrond Myklebust get_dreq(dreq); 309607f31e8STrond Myklebust 310607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 3111da177e4SLinus Torvalds data->inode = inode; 3121da177e4SLinus Torvalds data->cred = ctx->cred; 3131da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 3141da177e4SLinus Torvalds data->args.context = ctx; 31588467055SChuck Lever data->args.offset = pos; 3161da177e4SLinus Torvalds data->args.pgbase = pgbase; 317607f31e8STrond Myklebust data->args.pages = data->pagevec; 3181da177e4SLinus Torvalds data->args.count = bytes; 3191da177e4SLinus Torvalds data->res.fattr = &data->fattr; 3201da177e4SLinus Torvalds data->res.eof = 0; 3211da177e4SLinus Torvalds data->res.count = bytes; 3221da177e4SLinus Torvalds 323ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 324ec06c096STrond Myklebust &nfs_read_direct_ops, data); 3251da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long) inode; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds rpc_execute(&data->task); 3301da177e4SLinus Torvalds 331a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct read call " 332a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 3331da177e4SLinus Torvalds data->task.tk_pid, 3341da177e4SLinus Torvalds inode->i_sb->s_id, 3351da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 3361da177e4SLinus Torvalds bytes, 3371da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3381da177e4SLinus Torvalds 339607f31e8STrond Myklebust started += bytes; 340607f31e8STrond Myklebust user_addr += bytes; 34188467055SChuck Lever pos += bytes; 342e9f7bee1STrond Myklebust /* FIXME: Remove this unnecessary math from final patch */ 3431da177e4SLinus Torvalds pgbase += bytes; 3441da177e4SLinus Torvalds pgbase &= ~PAGE_MASK; 345e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds count -= bytes; 3481da177e4SLinus Torvalds } while (count != 0); 349607f31e8STrond Myklebust 350607f31e8STrond Myklebust if (put_dreq(dreq)) 351607f31e8STrond Myklebust nfs_direct_complete(dreq); 352607f31e8STrond Myklebust 353607f31e8STrond Myklebust if (started) 354607f31e8STrond Myklebust return 0; 355607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 35606cf6f2eSChuck Lever } 35706cf6f2eSChuck Lever 358607f31e8STrond Myklebust static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos) 3591da177e4SLinus Torvalds { 360607f31e8STrond Myklebust ssize_t result = 0; 3611da177e4SLinus Torvalds sigset_t oldset; 36299514f8fSChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 3631da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(inode); 3641da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3651da177e4SLinus Torvalds 366607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 3671da177e4SLinus Torvalds if (!dreq) 3681da177e4SLinus Torvalds return -ENOMEM; 3691da177e4SLinus Torvalds 37091d5b470SChuck Lever dreq->inode = inode; 371a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 372487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 373487b8372SChuck Lever dreq->iocb = iocb; 3741da177e4SLinus Torvalds 37591d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); 3761da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 377607f31e8STrond Myklebust result = nfs_direct_read_schedule(dreq, user_addr, count, pos); 378607f31e8STrond Myklebust if (!result) 379bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 3801da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 381b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds return result; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 386fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) 3871da177e4SLinus Torvalds { 388607f31e8STrond Myklebust while (!list_empty(&dreq->rewrite_list)) { 389607f31e8STrond Myklebust struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages); 390fad61490STrond Myklebust list_del(&data->pages); 391607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, data->npages); 392fad61490STrond Myklebust nfs_writedata_release(data); 393fad61490STrond Myklebust } 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 396fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 397fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 3981da177e4SLinus Torvalds { 399607f31e8STrond Myklebust struct inode *inode = dreq->inode; 400607f31e8STrond Myklebust struct list_head *p; 401607f31e8STrond Myklebust struct nfs_write_data *data; 4021da177e4SLinus Torvalds 403fad61490STrond Myklebust dreq->count = 0; 404607f31e8STrond Myklebust get_dreq(dreq); 4051da177e4SLinus Torvalds 406607f31e8STrond Myklebust list_for_each(p, &dreq->rewrite_list) { 407607f31e8STrond Myklebust data = list_entry(p, struct nfs_write_data, pages); 408607f31e8STrond Myklebust 409607f31e8STrond Myklebust get_dreq(dreq); 410607f31e8STrond Myklebust 411607f31e8STrond Myklebust /* 412607f31e8STrond Myklebust * Reset data->res. 413607f31e8STrond Myklebust */ 414607f31e8STrond Myklebust nfs_fattr_init(&data->fattr); 415607f31e8STrond Myklebust data->res.count = data->args.count; 416607f31e8STrond Myklebust memset(&data->verf, 0, sizeof(data->verf)); 417607f31e8STrond Myklebust 418607f31e8STrond Myklebust /* 419607f31e8STrond Myklebust * Reuse data->task; data->args should not have changed 420607f31e8STrond Myklebust * since the original request was sent. 421607f31e8STrond Myklebust */ 422607f31e8STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 423607f31e8STrond Myklebust &nfs_write_direct_ops, data); 424607f31e8STrond Myklebust NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE); 425607f31e8STrond Myklebust 426607f31e8STrond Myklebust data->task.tk_priority = RPC_PRIORITY_NORMAL; 427607f31e8STrond Myklebust data->task.tk_cookie = (unsigned long) inode; 428607f31e8STrond Myklebust 429607f31e8STrond Myklebust /* 430607f31e8STrond Myklebust * We're called via an RPC callback, so BKL is already held. 431607f31e8STrond Myklebust */ 432607f31e8STrond Myklebust rpc_execute(&data->task); 433607f31e8STrond Myklebust 434607f31e8STrond Myklebust dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 435607f31e8STrond Myklebust data->task.tk_pid, 436607f31e8STrond Myklebust inode->i_sb->s_id, 437607f31e8STrond Myklebust (long long)NFS_FILEID(inode), 438607f31e8STrond Myklebust data->args.count, 439607f31e8STrond Myklebust (unsigned long long)data->args.offset); 440607f31e8STrond Myklebust } 441607f31e8STrond Myklebust 442607f31e8STrond Myklebust if (put_dreq(dreq)) 443607f31e8STrond Myklebust nfs_direct_write_complete(dreq, inode); 444fad61490STrond Myklebust } 4451da177e4SLinus Torvalds 446fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) 447fad61490STrond Myklebust { 448fad61490STrond Myklebust struct nfs_write_data *data = calldata; 449fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 4501da177e4SLinus Torvalds 451fad61490STrond Myklebust /* Call the NFS version-specific code */ 452fad61490STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 453fad61490STrond Myklebust return; 454fad61490STrond Myklebust if (unlikely(task->tk_status < 0)) { 45560fa3f76STrond Myklebust dprintk("NFS: %5u commit failed with error %d.\n", 45660fa3f76STrond Myklebust task->tk_pid, task->tk_status); 457fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 45860fa3f76STrond Myklebust } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 459fad61490STrond Myklebust dprintk("NFS: %5u commit verify failed\n", task->tk_pid); 460fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 461fad61490STrond Myklebust } 462fad61490STrond Myklebust 463fad61490STrond Myklebust dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status); 464fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 465fad61490STrond Myklebust } 466fad61490STrond Myklebust 467fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = { 468fad61490STrond Myklebust .rpc_call_done = nfs_direct_commit_result, 469fad61490STrond Myklebust .rpc_release = nfs_commit_release, 470fad61490STrond Myklebust }; 471fad61490STrond Myklebust 472fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 473fad61490STrond Myklebust { 474fad61490STrond Myklebust struct nfs_write_data *data = dreq->commit_data; 475fad61490STrond Myklebust 476fad61490STrond Myklebust data->inode = dreq->inode; 477a8881f5aSTrond Myklebust data->cred = dreq->ctx->cred; 478fad61490STrond Myklebust 479fad61490STrond Myklebust data->args.fh = NFS_FH(data->inode); 480607f31e8STrond Myklebust data->args.offset = 0; 481607f31e8STrond Myklebust data->args.count = 0; 482fad61490STrond Myklebust data->res.count = 0; 483fad61490STrond Myklebust data->res.fattr = &data->fattr; 484fad61490STrond Myklebust data->res.verf = &data->verf; 485fad61490STrond Myklebust 486fad61490STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC, 487fad61490STrond Myklebust &nfs_commit_direct_ops, data); 488fad61490STrond Myklebust NFS_PROTO(data->inode)->commit_setup(data, 0); 489fad61490STrond Myklebust 490fad61490STrond Myklebust data->task.tk_priority = RPC_PRIORITY_NORMAL; 491fad61490STrond Myklebust data->task.tk_cookie = (unsigned long)data->inode; 492fad61490STrond Myklebust /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ 493fad61490STrond Myklebust dreq->commit_data = NULL; 494fad61490STrond Myklebust 495e99170ffSTrond Myklebust dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 4961da177e4SLinus Torvalds 497fad61490STrond Myklebust rpc_execute(&data->task); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 500fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 5011da177e4SLinus Torvalds { 502fad61490STrond Myklebust int flags = dreq->flags; 5031da177e4SLinus Torvalds 504fad61490STrond Myklebust dreq->flags = 0; 505fad61490STrond Myklebust switch (flags) { 506fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 507fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 5081da177e4SLinus Torvalds break; 509fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 510fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 5111da177e4SLinus Torvalds break; 5121da177e4SLinus Torvalds default: 513fad61490STrond Myklebust nfs_end_data_update(inode); 514fad61490STrond Myklebust if (dreq->commit_data != NULL) 515fad61490STrond Myklebust nfs_commit_free(dreq->commit_data); 516fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 517cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 518fad61490STrond Myklebust nfs_direct_complete(dreq); 5191da177e4SLinus Torvalds } 520fad61490STrond Myklebust } 521fad61490STrond Myklebust 522fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 523fad61490STrond Myklebust { 524e9f7bee1STrond Myklebust dreq->commit_data = nfs_commit_alloc(); 525fad61490STrond Myklebust if (dreq->commit_data != NULL) 526fad61490STrond Myklebust dreq->commit_data->req = (struct nfs_page *) dreq; 527fad61490STrond Myklebust } 528fad61490STrond Myklebust #else 529fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 530fad61490STrond Myklebust { 531fad61490STrond Myklebust dreq->commit_data = NULL; 532fad61490STrond Myklebust } 533fad61490STrond Myklebust 534fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 535fad61490STrond Myklebust { 536fad61490STrond Myklebust nfs_end_data_update(inode); 537fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 538cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 539fad61490STrond Myklebust nfs_direct_complete(dreq); 540fad61490STrond Myklebust } 541fad61490STrond Myklebust #endif 542fad61490STrond Myklebust 543462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata) 544462d5b32SChuck Lever { 545462d5b32SChuck Lever struct nfs_write_data *data = calldata; 546462d5b32SChuck Lever struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 547462d5b32SChuck Lever int status = task->tk_status; 548462d5b32SChuck Lever 549462d5b32SChuck Lever if (nfs_writeback_done(task, data) != 0) 550462d5b32SChuck Lever return; 551462d5b32SChuck Lever 55215ce4a0cSChuck Lever spin_lock(&dreq->lock); 553462d5b32SChuck Lever 55460fa3f76STrond Myklebust if (unlikely(dreq->error != 0)) 555eda3cef8STrond Myklebust goto out_unlock; 55660fa3f76STrond Myklebust if (unlikely(status < 0)) { 55760fa3f76STrond Myklebust /* An error has occured, so we should not commit */ 55860fa3f76STrond Myklebust dreq->flags = 0; 55960fa3f76STrond Myklebust dreq->error = status; 560eda3cef8STrond Myklebust } 561eda3cef8STrond Myklebust 56215ce4a0cSChuck Lever dreq->count += data->res.count; 56315ce4a0cSChuck Lever 564fad61490STrond Myklebust if (data->res.verf->committed != NFS_FILE_SYNC) { 565fad61490STrond Myklebust switch (dreq->flags) { 566fad61490STrond Myklebust case 0: 567fad61490STrond Myklebust memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); 568fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_DO_COMMIT; 569fad61490STrond Myklebust break; 570fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 571fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { 572fad61490STrond Myklebust dprintk("NFS: %5u write verify failed\n", task->tk_pid); 573fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 574fad61490STrond Myklebust } 575fad61490STrond Myklebust } 576fad61490STrond Myklebust } 577eda3cef8STrond Myklebust out_unlock: 578fad61490STrond Myklebust spin_unlock(&dreq->lock); 579fad61490STrond Myklebust } 580fad61490STrond Myklebust 581fad61490STrond Myklebust /* 582fad61490STrond Myklebust * NB: Return the value of the first error return code. Subsequent 583fad61490STrond Myklebust * errors after the first one are ignored. 584fad61490STrond Myklebust */ 585fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata) 586fad61490STrond Myklebust { 587fad61490STrond Myklebust struct nfs_write_data *data = calldata; 588fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 589fad61490STrond Myklebust 590607f31e8STrond Myklebust if (put_dreq(dreq)) 591fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 592462d5b32SChuck Lever } 593462d5b32SChuck Lever 594462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = { 595462d5b32SChuck Lever .rpc_call_done = nfs_direct_write_result, 596fad61490STrond Myklebust .rpc_release = nfs_direct_write_release, 597462d5b32SChuck Lever }; 598462d5b32SChuck Lever 599462d5b32SChuck Lever /* 600607f31e8STrond Myklebust * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 601607f31e8STrond Myklebust * operation. If nfs_writedata_alloc() or get_user_pages() fails, 602607f31e8STrond Myklebust * bail and stop sending more writes. Write length accounting is 603607f31e8STrond Myklebust * handled automatically by nfs_direct_write_result(). Otherwise, if 604607f31e8STrond Myklebust * no requests have been sent, just return an error. 605462d5b32SChuck Lever */ 606607f31e8STrond Myklebust static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync) 607462d5b32SChuck Lever { 608a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 60988be9f99STrond Myklebust struct inode *inode = ctx->path.dentry->d_inode; 610462d5b32SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 611607f31e8STrond Myklebust unsigned int pgbase; 612607f31e8STrond Myklebust int result; 613607f31e8STrond Myklebust ssize_t started = 0; 61482b145c5SChuck Lever 615607f31e8STrond Myklebust get_dreq(dreq); 616607f31e8STrond Myklebust 617462d5b32SChuck Lever do { 61882b145c5SChuck Lever struct nfs_write_data *data; 619462d5b32SChuck Lever size_t bytes; 620462d5b32SChuck Lever 621e9f7bee1STrond Myklebust pgbase = user_addr & ~PAGE_MASK; 622e9f7bee1STrond Myklebust bytes = min(wsize,count); 623e9f7bee1STrond Myklebust 624607f31e8STrond Myklebust result = -ENOMEM; 6258d5658c9STrond Myklebust data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes)); 626607f31e8STrond Myklebust if (unlikely(!data)) 627607f31e8STrond Myklebust break; 628607f31e8STrond Myklebust 629607f31e8STrond Myklebust down_read(¤t->mm->mmap_sem); 630607f31e8STrond Myklebust result = get_user_pages(current, current->mm, user_addr, 631607f31e8STrond Myklebust data->npages, 0, 0, data->pagevec, NULL); 632607f31e8STrond Myklebust up_read(¤t->mm->mmap_sem); 633749e146eSChuck Lever if (result < 0) { 634749e146eSChuck Lever nfs_writedata_release(data); 635749e146eSChuck Lever break; 636749e146eSChuck Lever } 637749e146eSChuck Lever if ((unsigned)result < data->npages) { 638d9df8d6bSTrond Myklebust bytes = result * PAGE_SIZE; 639d9df8d6bSTrond Myklebust if (bytes <= pgbase) { 640607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 641607f31e8STrond Myklebust nfs_writedata_release(data); 642607f31e8STrond Myklebust break; 643607f31e8STrond Myklebust } 644d9df8d6bSTrond Myklebust bytes -= pgbase; 645d9df8d6bSTrond Myklebust data->npages = result; 646d9df8d6bSTrond Myklebust } 647607f31e8STrond Myklebust 648607f31e8STrond Myklebust get_dreq(dreq); 649607f31e8STrond Myklebust 650fad61490STrond Myklebust list_move_tail(&data->pages, &dreq->rewrite_list); 651462d5b32SChuck Lever 652607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 653462d5b32SChuck Lever data->inode = inode; 654462d5b32SChuck Lever data->cred = ctx->cred; 655462d5b32SChuck Lever data->args.fh = NFS_FH(inode); 656462d5b32SChuck Lever data->args.context = ctx; 65788467055SChuck Lever data->args.offset = pos; 658462d5b32SChuck Lever data->args.pgbase = pgbase; 659607f31e8STrond Myklebust data->args.pages = data->pagevec; 660462d5b32SChuck Lever data->args.count = bytes; 661462d5b32SChuck Lever data->res.fattr = &data->fattr; 662462d5b32SChuck Lever data->res.count = bytes; 66347989d74SChuck Lever data->res.verf = &data->verf; 664462d5b32SChuck Lever 665462d5b32SChuck Lever rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 666462d5b32SChuck Lever &nfs_write_direct_ops, data); 667fad61490STrond Myklebust NFS_PROTO(inode)->write_setup(data, sync); 668462d5b32SChuck Lever 669462d5b32SChuck Lever data->task.tk_priority = RPC_PRIORITY_NORMAL; 670462d5b32SChuck Lever data->task.tk_cookie = (unsigned long) inode; 6711da177e4SLinus Torvalds 672462d5b32SChuck Lever rpc_execute(&data->task); 6731da177e4SLinus Torvalds 674a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct write call " 675a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 676462d5b32SChuck Lever data->task.tk_pid, 677462d5b32SChuck Lever inode->i_sb->s_id, 678462d5b32SChuck Lever (long long)NFS_FILEID(inode), 679462d5b32SChuck Lever bytes, 680462d5b32SChuck Lever (unsigned long long)data->args.offset); 681462d5b32SChuck Lever 682607f31e8STrond Myklebust started += bytes; 683607f31e8STrond Myklebust user_addr += bytes; 68488467055SChuck Lever pos += bytes; 685e9f7bee1STrond Myklebust 686e9f7bee1STrond Myklebust /* FIXME: Remove this useless math from the final patch */ 687462d5b32SChuck Lever pgbase += bytes; 688462d5b32SChuck Lever pgbase &= ~PAGE_MASK; 689e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 690462d5b32SChuck Lever 691462d5b32SChuck Lever count -= bytes; 692462d5b32SChuck Lever } while (count != 0); 693607f31e8STrond Myklebust 694607f31e8STrond Myklebust if (put_dreq(dreq)) 695607f31e8STrond Myklebust nfs_direct_write_complete(dreq, inode); 696607f31e8STrond Myklebust 697607f31e8STrond Myklebust if (started) 698607f31e8STrond Myklebust return 0; 699607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 70006cf6f2eSChuck Lever } 70106cf6f2eSChuck Lever 702607f31e8STrond Myklebust static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos) 703462d5b32SChuck Lever { 704607f31e8STrond Myklebust ssize_t result = 0; 705462d5b32SChuck Lever sigset_t oldset; 706c89f2ee5SChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 707462d5b32SChuck Lever struct rpc_clnt *clnt = NFS_CLIENT(inode); 708462d5b32SChuck Lever struct nfs_direct_req *dreq; 709fad61490STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize; 710fad61490STrond Myklebust int sync = 0; 711462d5b32SChuck Lever 712607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 713462d5b32SChuck Lever if (!dreq) 714462d5b32SChuck Lever return -ENOMEM; 715607f31e8STrond Myklebust nfs_alloc_commit_data(dreq); 716607f31e8STrond Myklebust 717fad61490STrond Myklebust if (dreq->commit_data == NULL || count < wsize) 718fad61490STrond Myklebust sync = FLUSH_STABLE; 719462d5b32SChuck Lever 720c89f2ee5SChuck Lever dreq->inode = inode; 721a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 722c89f2ee5SChuck Lever if (!is_sync_kiocb(iocb)) 723c89f2ee5SChuck Lever dreq->iocb = iocb; 724462d5b32SChuck Lever 72547989d74SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count); 72647989d74SChuck Lever 727462d5b32SChuck Lever nfs_begin_data_update(inode); 728462d5b32SChuck Lever 729462d5b32SChuck Lever rpc_clnt_sigmask(clnt, &oldset); 730607f31e8STrond Myklebust result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync); 731607f31e8STrond Myklebust if (!result) 732c89f2ee5SChuck Lever result = nfs_direct_wait(dreq); 733462d5b32SChuck Lever rpc_clnt_sigunmask(clnt, &oldset); 734b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 735462d5b32SChuck Lever 7361da177e4SLinus Torvalds return result; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds /** 7401da177e4SLinus Torvalds * nfs_file_direct_read - file direct read operation for NFS files 7411da177e4SLinus Torvalds * @iocb: target I/O control block 742027445c3SBadari Pulavarty * @iov: vector of user buffers into which to read data 743027445c3SBadari Pulavarty * @nr_segs: size of iov vector 74488467055SChuck Lever * @pos: byte offset in file where reading starts 7451da177e4SLinus Torvalds * 7461da177e4SLinus Torvalds * We use this function for direct reads instead of calling 7471da177e4SLinus Torvalds * generic_file_aio_read() in order to avoid gfar's check to see if 7481da177e4SLinus Torvalds * the request starts before the end of the file. For that check 7491da177e4SLinus Torvalds * to work, we must generate a GETATTR before each direct read, and 7501da177e4SLinus Torvalds * even then there is a window between the GETATTR and the subsequent 75188467055SChuck Lever * READ where the file size could change. Our preference is simply 7521da177e4SLinus Torvalds * to do all reads the application wants, and the server will take 7531da177e4SLinus Torvalds * care of managing the end of file boundary. 7541da177e4SLinus Torvalds * 7551da177e4SLinus Torvalds * This function also eliminates unnecessarily updating the file's 7561da177e4SLinus Torvalds * atime locally, as the NFS server sets the file's atime, and this 7571da177e4SLinus Torvalds * client must read the updated atime from the server back into its 7581da177e4SLinus Torvalds * cache. 7591da177e4SLinus Torvalds */ 760027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov, 761027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds ssize_t retval = -EINVAL; 7641da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 7651da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 766027445c3SBadari Pulavarty /* XXX: temporary */ 767027445c3SBadari Pulavarty const char __user *buf = iov[0].iov_base; 768027445c3SBadari Pulavarty size_t count = iov[0].iov_len; 7691da177e4SLinus Torvalds 770ce1a8e67SChuck Lever dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", 77101cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 77201cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 773ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 7741da177e4SLinus Torvalds 775027445c3SBadari Pulavarty if (nr_segs != 1) 7761da177e4SLinus Torvalds goto out; 777070ea602SChuck Lever 7781da177e4SLinus Torvalds retval = -EFAULT; 7790cdd80d0SChuck Lever if (!access_ok(VERIFY_WRITE, buf, count)) 7801da177e4SLinus Torvalds goto out; 7811da177e4SLinus Torvalds retval = 0; 7821da177e4SLinus Torvalds if (!count) 7831da177e4SLinus Torvalds goto out; 7841da177e4SLinus Torvalds 78529884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 7861da177e4SLinus Torvalds if (retval) 7871da177e4SLinus Torvalds goto out; 7881da177e4SLinus Torvalds 789607f31e8STrond Myklebust retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos); 7901da177e4SLinus Torvalds if (retval > 0) 7910cdd80d0SChuck Lever iocb->ki_pos = pos + retval; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds out: 7941da177e4SLinus Torvalds return retval; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /** 7981da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 7991da177e4SLinus Torvalds * @iocb: target I/O control block 800027445c3SBadari Pulavarty * @iov: vector of user buffers from which to write data 801027445c3SBadari Pulavarty * @nr_segs: size of iov vector 80288467055SChuck Lever * @pos: byte offset in file where writing starts 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * We use this function for direct writes instead of calling 8051da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 8061da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 8071da177e4SLinus Torvalds * the new i_size and this client must read the updated size 8081da177e4SLinus Torvalds * back into its cache. We let the server do generic write 8091da177e4SLinus Torvalds * parameter checking and report problems. 8101da177e4SLinus Torvalds * 8111da177e4SLinus Torvalds * We also avoid an unnecessary invocation of generic_osync_inode(), 8121da177e4SLinus Torvalds * as it is fairly meaningless to sync the metadata of an NFS file. 8131da177e4SLinus Torvalds * 8141da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8151da177e4SLinus Torvalds * 8161da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8171da177e4SLinus Torvalds * readers of this file. 8181da177e4SLinus Torvalds * 8191da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8201da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8211da177e4SLinus Torvalds */ 822027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov, 823027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 8241da177e4SLinus Torvalds { 825070ea602SChuck Lever ssize_t retval = -EINVAL; 8261da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 8271da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 828027445c3SBadari Pulavarty /* XXX: temporary */ 829027445c3SBadari Pulavarty const char __user *buf = iov[0].iov_base; 830027445c3SBadari Pulavarty size_t count = iov[0].iov_len; 8311da177e4SLinus Torvalds 832a3f565b1SChuck Lever dprintk("nfs: direct write(%s/%s, %lu@%Ld)\n", 83301cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 83401cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 835ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 8361da177e4SLinus Torvalds 837027445c3SBadari Pulavarty if (nr_segs != 1) 838070ea602SChuck Lever goto out; 839027445c3SBadari Pulavarty 840ce1a8e67SChuck Lever retval = generic_write_checks(file, &pos, &count, 0); 841ce1a8e67SChuck Lever if (retval) 8421da177e4SLinus Torvalds goto out; 843ce1a8e67SChuck Lever 844ce1a8e67SChuck Lever retval = -EINVAL; 845ce1a8e67SChuck Lever if ((ssize_t) count < 0) 8461da177e4SLinus Torvalds goto out; 8471da177e4SLinus Torvalds retval = 0; 8481da177e4SLinus Torvalds if (!count) 8491da177e4SLinus Torvalds goto out; 850ce1a8e67SChuck Lever 851ce1a8e67SChuck Lever retval = -EFAULT; 85247989d74SChuck Lever if (!access_ok(VERIFY_READ, buf, count)) 853ce1a8e67SChuck Lever goto out; 8541da177e4SLinus Torvalds 85529884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 8561da177e4SLinus Torvalds if (retval) 8571da177e4SLinus Torvalds goto out; 8581da177e4SLinus Torvalds 859607f31e8STrond Myklebust retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos); 8609eafa8ccSChuck Lever 8611da177e4SLinus Torvalds if (retval > 0) 862ce1a8e67SChuck Lever iocb->ki_pos = pos + retval; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds out: 8651da177e4SLinus Torvalds return retval; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 86888467055SChuck Lever /** 86988467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 87088467055SChuck Lever * 87188467055SChuck Lever */ 872f7b422b1SDavid Howells int __init nfs_init_directcache(void) 8731da177e4SLinus Torvalds { 8741da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 8751da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 876fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 877fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 878*20c2df83SPaul Mundt NULL); 8791da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 8801da177e4SLinus Torvalds return -ENOMEM; 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds return 0; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 88588467055SChuck Lever /** 886f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 88788467055SChuck Lever * 88888467055SChuck Lever */ 889266bee88SDavid Brownell void nfs_destroy_directcache(void) 8901da177e4SLinus Torvalds { 8911a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 8921da177e4SLinus Torvalds } 893