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; 269a8881f5aSTrond Myklebust struct inode *inode = ctx->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) { 298607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 299607f31e8STrond Myklebust nfs_readdata_release(data); 300607f31e8STrond Myklebust break; 301607f31e8STrond Myklebust } 30206cf6f2eSChuck Lever 303607f31e8STrond Myklebust get_dreq(dreq); 304607f31e8STrond Myklebust 305607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 3061da177e4SLinus Torvalds data->inode = inode; 3071da177e4SLinus Torvalds data->cred = ctx->cred; 3081da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 3091da177e4SLinus Torvalds data->args.context = ctx; 31088467055SChuck Lever data->args.offset = pos; 3111da177e4SLinus Torvalds data->args.pgbase = pgbase; 312607f31e8STrond Myklebust data->args.pages = data->pagevec; 3131da177e4SLinus Torvalds data->args.count = bytes; 3141da177e4SLinus Torvalds data->res.fattr = &data->fattr; 3151da177e4SLinus Torvalds data->res.eof = 0; 3161da177e4SLinus Torvalds data->res.count = bytes; 3171da177e4SLinus Torvalds 318ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 319ec06c096STrond Myklebust &nfs_read_direct_ops, data); 3201da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long) inode; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds rpc_execute(&data->task); 3251da177e4SLinus Torvalds 326a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct read call " 327a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 3281da177e4SLinus Torvalds data->task.tk_pid, 3291da177e4SLinus Torvalds inode->i_sb->s_id, 3301da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 3311da177e4SLinus Torvalds bytes, 3321da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3331da177e4SLinus Torvalds 334607f31e8STrond Myklebust started += bytes; 335607f31e8STrond Myklebust user_addr += bytes; 33688467055SChuck Lever pos += bytes; 337e9f7bee1STrond Myklebust /* FIXME: Remove this unnecessary math from final patch */ 3381da177e4SLinus Torvalds pgbase += bytes; 3391da177e4SLinus Torvalds pgbase &= ~PAGE_MASK; 340e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds count -= bytes; 3431da177e4SLinus Torvalds } while (count != 0); 344607f31e8STrond Myklebust 345607f31e8STrond Myklebust if (put_dreq(dreq)) 346607f31e8STrond Myklebust nfs_direct_complete(dreq); 347607f31e8STrond Myklebust 348607f31e8STrond Myklebust if (started) 349607f31e8STrond Myklebust return 0; 350607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 35106cf6f2eSChuck Lever } 35206cf6f2eSChuck Lever 353607f31e8STrond Myklebust static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos) 3541da177e4SLinus Torvalds { 355607f31e8STrond Myklebust ssize_t result = 0; 3561da177e4SLinus Torvalds sigset_t oldset; 35799514f8fSChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 3581da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(inode); 3591da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3601da177e4SLinus Torvalds 361607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 3621da177e4SLinus Torvalds if (!dreq) 3631da177e4SLinus Torvalds return -ENOMEM; 3641da177e4SLinus Torvalds 36591d5b470SChuck Lever dreq->inode = inode; 366a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 367487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 368487b8372SChuck Lever dreq->iocb = iocb; 3691da177e4SLinus Torvalds 37091d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); 3711da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 372607f31e8STrond Myklebust result = nfs_direct_read_schedule(dreq, user_addr, count, pos); 373607f31e8STrond Myklebust if (!result) 374bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 3751da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 376b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds return result; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 381fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) 3821da177e4SLinus Torvalds { 383607f31e8STrond Myklebust while (!list_empty(&dreq->rewrite_list)) { 384607f31e8STrond Myklebust struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages); 385fad61490STrond Myklebust list_del(&data->pages); 386607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, data->npages); 387fad61490STrond Myklebust nfs_writedata_release(data); 388fad61490STrond Myklebust } 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 391fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 392fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 3931da177e4SLinus Torvalds { 394607f31e8STrond Myklebust struct inode *inode = dreq->inode; 395607f31e8STrond Myklebust struct list_head *p; 396607f31e8STrond Myklebust struct nfs_write_data *data; 3971da177e4SLinus Torvalds 398fad61490STrond Myklebust dreq->count = 0; 399607f31e8STrond Myklebust get_dreq(dreq); 4001da177e4SLinus Torvalds 401607f31e8STrond Myklebust list_for_each(p, &dreq->rewrite_list) { 402607f31e8STrond Myklebust data = list_entry(p, struct nfs_write_data, pages); 403607f31e8STrond Myklebust 404607f31e8STrond Myklebust get_dreq(dreq); 405607f31e8STrond Myklebust 406607f31e8STrond Myklebust /* 407607f31e8STrond Myklebust * Reset data->res. 408607f31e8STrond Myklebust */ 409607f31e8STrond Myklebust nfs_fattr_init(&data->fattr); 410607f31e8STrond Myklebust data->res.count = data->args.count; 411607f31e8STrond Myklebust memset(&data->verf, 0, sizeof(data->verf)); 412607f31e8STrond Myklebust 413607f31e8STrond Myklebust /* 414607f31e8STrond Myklebust * Reuse data->task; data->args should not have changed 415607f31e8STrond Myklebust * since the original request was sent. 416607f31e8STrond Myklebust */ 417607f31e8STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 418607f31e8STrond Myklebust &nfs_write_direct_ops, data); 419607f31e8STrond Myklebust NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE); 420607f31e8STrond Myklebust 421607f31e8STrond Myklebust data->task.tk_priority = RPC_PRIORITY_NORMAL; 422607f31e8STrond Myklebust data->task.tk_cookie = (unsigned long) inode; 423607f31e8STrond Myklebust 424607f31e8STrond Myklebust /* 425607f31e8STrond Myklebust * We're called via an RPC callback, so BKL is already held. 426607f31e8STrond Myklebust */ 427607f31e8STrond Myklebust rpc_execute(&data->task); 428607f31e8STrond Myklebust 429607f31e8STrond Myklebust dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 430607f31e8STrond Myklebust data->task.tk_pid, 431607f31e8STrond Myklebust inode->i_sb->s_id, 432607f31e8STrond Myklebust (long long)NFS_FILEID(inode), 433607f31e8STrond Myklebust data->args.count, 434607f31e8STrond Myklebust (unsigned long long)data->args.offset); 435607f31e8STrond Myklebust } 436607f31e8STrond Myklebust 437607f31e8STrond Myklebust if (put_dreq(dreq)) 438607f31e8STrond Myklebust nfs_direct_write_complete(dreq, inode); 439fad61490STrond Myklebust } 4401da177e4SLinus Torvalds 441fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) 442fad61490STrond Myklebust { 443fad61490STrond Myklebust struct nfs_write_data *data = calldata; 444fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 4451da177e4SLinus Torvalds 446fad61490STrond Myklebust /* Call the NFS version-specific code */ 447fad61490STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 448fad61490STrond Myklebust return; 449fad61490STrond Myklebust if (unlikely(task->tk_status < 0)) { 45060fa3f76STrond Myklebust dprintk("NFS: %5u commit failed with error %d.\n", 45160fa3f76STrond Myklebust task->tk_pid, task->tk_status); 452fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 45360fa3f76STrond Myklebust } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 454fad61490STrond Myklebust dprintk("NFS: %5u commit verify failed\n", task->tk_pid); 455fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 456fad61490STrond Myklebust } 457fad61490STrond Myklebust 458fad61490STrond Myklebust dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status); 459fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 460fad61490STrond Myklebust } 461fad61490STrond Myklebust 462fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = { 463fad61490STrond Myklebust .rpc_call_done = nfs_direct_commit_result, 464fad61490STrond Myklebust .rpc_release = nfs_commit_release, 465fad61490STrond Myklebust }; 466fad61490STrond Myklebust 467fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 468fad61490STrond Myklebust { 469fad61490STrond Myklebust struct nfs_write_data *data = dreq->commit_data; 470fad61490STrond Myklebust 471fad61490STrond Myklebust data->inode = dreq->inode; 472a8881f5aSTrond Myklebust data->cred = dreq->ctx->cred; 473fad61490STrond Myklebust 474fad61490STrond Myklebust data->args.fh = NFS_FH(data->inode); 475607f31e8STrond Myklebust data->args.offset = 0; 476607f31e8STrond Myklebust data->args.count = 0; 477fad61490STrond Myklebust data->res.count = 0; 478fad61490STrond Myklebust data->res.fattr = &data->fattr; 479fad61490STrond Myklebust data->res.verf = &data->verf; 480fad61490STrond Myklebust 481fad61490STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC, 482fad61490STrond Myklebust &nfs_commit_direct_ops, data); 483fad61490STrond Myklebust NFS_PROTO(data->inode)->commit_setup(data, 0); 484fad61490STrond Myklebust 485fad61490STrond Myklebust data->task.tk_priority = RPC_PRIORITY_NORMAL; 486fad61490STrond Myklebust data->task.tk_cookie = (unsigned long)data->inode; 487fad61490STrond Myklebust /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ 488fad61490STrond Myklebust dreq->commit_data = NULL; 489fad61490STrond Myklebust 490e99170ffSTrond Myklebust dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 4911da177e4SLinus Torvalds 492fad61490STrond Myklebust rpc_execute(&data->task); 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 495fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 4961da177e4SLinus Torvalds { 497fad61490STrond Myklebust int flags = dreq->flags; 4981da177e4SLinus Torvalds 499fad61490STrond Myklebust dreq->flags = 0; 500fad61490STrond Myklebust switch (flags) { 501fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 502fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 5031da177e4SLinus Torvalds break; 504fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 505fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 5061da177e4SLinus Torvalds break; 5071da177e4SLinus Torvalds default: 508fad61490STrond Myklebust nfs_end_data_update(inode); 509fad61490STrond Myklebust if (dreq->commit_data != NULL) 510fad61490STrond Myklebust nfs_commit_free(dreq->commit_data); 511fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 512cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 513fad61490STrond Myklebust nfs_direct_complete(dreq); 5141da177e4SLinus Torvalds } 515fad61490STrond Myklebust } 516fad61490STrond Myklebust 517fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 518fad61490STrond Myklebust { 519e9f7bee1STrond Myklebust dreq->commit_data = nfs_commit_alloc(); 520fad61490STrond Myklebust if (dreq->commit_data != NULL) 521fad61490STrond Myklebust dreq->commit_data->req = (struct nfs_page *) dreq; 522fad61490STrond Myklebust } 523fad61490STrond Myklebust #else 524fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 525fad61490STrond Myklebust { 526fad61490STrond Myklebust dreq->commit_data = NULL; 527fad61490STrond Myklebust } 528fad61490STrond Myklebust 529fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 530fad61490STrond Myklebust { 531fad61490STrond Myklebust nfs_end_data_update(inode); 532fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 533cd9ae2b6STrond Myklebust nfs_zap_mapping(inode, inode->i_mapping); 534fad61490STrond Myklebust nfs_direct_complete(dreq); 535fad61490STrond Myklebust } 536fad61490STrond Myklebust #endif 537fad61490STrond Myklebust 538462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata) 539462d5b32SChuck Lever { 540462d5b32SChuck Lever struct nfs_write_data *data = calldata; 541462d5b32SChuck Lever struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 542462d5b32SChuck Lever int status = task->tk_status; 543462d5b32SChuck Lever 544462d5b32SChuck Lever if (nfs_writeback_done(task, data) != 0) 545462d5b32SChuck Lever return; 546462d5b32SChuck Lever 54715ce4a0cSChuck Lever spin_lock(&dreq->lock); 548462d5b32SChuck Lever 54960fa3f76STrond Myklebust if (unlikely(dreq->error != 0)) 550eda3cef8STrond Myklebust goto out_unlock; 55160fa3f76STrond Myklebust if (unlikely(status < 0)) { 55260fa3f76STrond Myklebust /* An error has occured, so we should not commit */ 55360fa3f76STrond Myklebust dreq->flags = 0; 55460fa3f76STrond Myklebust dreq->error = status; 555eda3cef8STrond Myklebust } 556eda3cef8STrond Myklebust 55715ce4a0cSChuck Lever dreq->count += data->res.count; 55815ce4a0cSChuck Lever 559fad61490STrond Myklebust if (data->res.verf->committed != NFS_FILE_SYNC) { 560fad61490STrond Myklebust switch (dreq->flags) { 561fad61490STrond Myklebust case 0: 562fad61490STrond Myklebust memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); 563fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_DO_COMMIT; 564fad61490STrond Myklebust break; 565fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 566fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { 567fad61490STrond Myklebust dprintk("NFS: %5u write verify failed\n", task->tk_pid); 568fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 569fad61490STrond Myklebust } 570fad61490STrond Myklebust } 571fad61490STrond Myklebust } 572eda3cef8STrond Myklebust out_unlock: 573fad61490STrond Myklebust spin_unlock(&dreq->lock); 574fad61490STrond Myklebust } 575fad61490STrond Myklebust 576fad61490STrond Myklebust /* 577fad61490STrond Myklebust * NB: Return the value of the first error return code. Subsequent 578fad61490STrond Myklebust * errors after the first one are ignored. 579fad61490STrond Myklebust */ 580fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata) 581fad61490STrond Myklebust { 582fad61490STrond Myklebust struct nfs_write_data *data = calldata; 583fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 584fad61490STrond Myklebust 585607f31e8STrond Myklebust if (put_dreq(dreq)) 586fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 587462d5b32SChuck Lever } 588462d5b32SChuck Lever 589462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = { 590462d5b32SChuck Lever .rpc_call_done = nfs_direct_write_result, 591fad61490STrond Myklebust .rpc_release = nfs_direct_write_release, 592462d5b32SChuck Lever }; 593462d5b32SChuck Lever 594462d5b32SChuck Lever /* 595607f31e8STrond Myklebust * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 596607f31e8STrond Myklebust * operation. If nfs_writedata_alloc() or get_user_pages() fails, 597607f31e8STrond Myklebust * bail and stop sending more writes. Write length accounting is 598607f31e8STrond Myklebust * handled automatically by nfs_direct_write_result(). Otherwise, if 599607f31e8STrond Myklebust * no requests have been sent, just return an error. 600462d5b32SChuck Lever */ 601607f31e8STrond 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) 602462d5b32SChuck Lever { 603a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 604a8881f5aSTrond Myklebust struct inode *inode = ctx->dentry->d_inode; 605462d5b32SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 606607f31e8STrond Myklebust unsigned int pgbase; 607607f31e8STrond Myklebust int result; 608607f31e8STrond Myklebust ssize_t started = 0; 60982b145c5SChuck Lever 610607f31e8STrond Myklebust get_dreq(dreq); 611607f31e8STrond Myklebust 612462d5b32SChuck Lever do { 61382b145c5SChuck Lever struct nfs_write_data *data; 614462d5b32SChuck Lever size_t bytes; 615462d5b32SChuck Lever 616e9f7bee1STrond Myklebust pgbase = user_addr & ~PAGE_MASK; 617e9f7bee1STrond Myklebust bytes = min(wsize,count); 618e9f7bee1STrond Myklebust 619607f31e8STrond Myklebust result = -ENOMEM; 6208d5658c9STrond Myklebust data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes)); 621607f31e8STrond Myklebust if (unlikely(!data)) 622607f31e8STrond Myklebust break; 623607f31e8STrond Myklebust 624607f31e8STrond Myklebust down_read(¤t->mm->mmap_sem); 625607f31e8STrond Myklebust result = get_user_pages(current, current->mm, user_addr, 626607f31e8STrond Myklebust data->npages, 0, 0, data->pagevec, NULL); 627607f31e8STrond Myklebust up_read(¤t->mm->mmap_sem); 628749e146eSChuck Lever if (result < 0) { 629749e146eSChuck Lever nfs_writedata_release(data); 630749e146eSChuck Lever break; 631749e146eSChuck Lever } 632749e146eSChuck Lever if ((unsigned)result < data->npages) { 633607f31e8STrond Myklebust nfs_direct_release_pages(data->pagevec, result); 634607f31e8STrond Myklebust nfs_writedata_release(data); 635607f31e8STrond Myklebust break; 636607f31e8STrond Myklebust } 637607f31e8STrond Myklebust 638607f31e8STrond Myklebust get_dreq(dreq); 639607f31e8STrond Myklebust 640fad61490STrond Myklebust list_move_tail(&data->pages, &dreq->rewrite_list); 641462d5b32SChuck Lever 642607f31e8STrond Myklebust data->req = (struct nfs_page *) dreq; 643462d5b32SChuck Lever data->inode = inode; 644462d5b32SChuck Lever data->cred = ctx->cred; 645462d5b32SChuck Lever data->args.fh = NFS_FH(inode); 646462d5b32SChuck Lever data->args.context = ctx; 64788467055SChuck Lever data->args.offset = pos; 648462d5b32SChuck Lever data->args.pgbase = pgbase; 649607f31e8STrond Myklebust data->args.pages = data->pagevec; 650462d5b32SChuck Lever data->args.count = bytes; 651462d5b32SChuck Lever data->res.fattr = &data->fattr; 652462d5b32SChuck Lever data->res.count = bytes; 65347989d74SChuck Lever data->res.verf = &data->verf; 654462d5b32SChuck Lever 655462d5b32SChuck Lever rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 656462d5b32SChuck Lever &nfs_write_direct_ops, data); 657fad61490STrond Myklebust NFS_PROTO(inode)->write_setup(data, sync); 658462d5b32SChuck Lever 659462d5b32SChuck Lever data->task.tk_priority = RPC_PRIORITY_NORMAL; 660462d5b32SChuck Lever data->task.tk_cookie = (unsigned long) inode; 6611da177e4SLinus Torvalds 662462d5b32SChuck Lever rpc_execute(&data->task); 6631da177e4SLinus Torvalds 664a3f565b1SChuck Lever dprintk("NFS: %5u initiated direct write call " 665a3f565b1SChuck Lever "(req %s/%Ld, %zu bytes @ offset %Lu)\n", 666462d5b32SChuck Lever data->task.tk_pid, 667462d5b32SChuck Lever inode->i_sb->s_id, 668462d5b32SChuck Lever (long long)NFS_FILEID(inode), 669462d5b32SChuck Lever bytes, 670462d5b32SChuck Lever (unsigned long long)data->args.offset); 671462d5b32SChuck Lever 672607f31e8STrond Myklebust started += bytes; 673607f31e8STrond Myklebust user_addr += bytes; 67488467055SChuck Lever pos += bytes; 675e9f7bee1STrond Myklebust 676e9f7bee1STrond Myklebust /* FIXME: Remove this useless math from the final patch */ 677462d5b32SChuck Lever pgbase += bytes; 678462d5b32SChuck Lever pgbase &= ~PAGE_MASK; 679e9f7bee1STrond Myklebust BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); 680462d5b32SChuck Lever 681462d5b32SChuck Lever count -= bytes; 682462d5b32SChuck Lever } while (count != 0); 683607f31e8STrond Myklebust 684607f31e8STrond Myklebust if (put_dreq(dreq)) 685607f31e8STrond Myklebust nfs_direct_write_complete(dreq, inode); 686607f31e8STrond Myklebust 687607f31e8STrond Myklebust if (started) 688607f31e8STrond Myklebust return 0; 689607f31e8STrond Myklebust return result < 0 ? (ssize_t) result : -EFAULT; 69006cf6f2eSChuck Lever } 69106cf6f2eSChuck Lever 692607f31e8STrond Myklebust static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos) 693462d5b32SChuck Lever { 694607f31e8STrond Myklebust ssize_t result = 0; 695462d5b32SChuck Lever sigset_t oldset; 696c89f2ee5SChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 697462d5b32SChuck Lever struct rpc_clnt *clnt = NFS_CLIENT(inode); 698462d5b32SChuck Lever struct nfs_direct_req *dreq; 699fad61490STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize; 700fad61490STrond Myklebust int sync = 0; 701462d5b32SChuck Lever 702607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 703462d5b32SChuck Lever if (!dreq) 704462d5b32SChuck Lever return -ENOMEM; 705607f31e8STrond Myklebust nfs_alloc_commit_data(dreq); 706607f31e8STrond Myklebust 707fad61490STrond Myklebust if (dreq->commit_data == NULL || count < wsize) 708fad61490STrond Myklebust sync = FLUSH_STABLE; 709462d5b32SChuck Lever 710c89f2ee5SChuck Lever dreq->inode = inode; 711a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 712c89f2ee5SChuck Lever if (!is_sync_kiocb(iocb)) 713c89f2ee5SChuck Lever dreq->iocb = iocb; 714462d5b32SChuck Lever 71547989d74SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count); 71647989d74SChuck Lever 717462d5b32SChuck Lever nfs_begin_data_update(inode); 718462d5b32SChuck Lever 719462d5b32SChuck Lever rpc_clnt_sigmask(clnt, &oldset); 720607f31e8STrond Myklebust result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync); 721607f31e8STrond Myklebust if (!result) 722c89f2ee5SChuck Lever result = nfs_direct_wait(dreq); 723462d5b32SChuck Lever rpc_clnt_sigunmask(clnt, &oldset); 724b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 725462d5b32SChuck Lever 7261da177e4SLinus Torvalds return result; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /** 7301da177e4SLinus Torvalds * nfs_file_direct_read - file direct read operation for NFS files 7311da177e4SLinus Torvalds * @iocb: target I/O control block 732027445c3SBadari Pulavarty * @iov: vector of user buffers into which to read data 733027445c3SBadari Pulavarty * @nr_segs: size of iov vector 73488467055SChuck Lever * @pos: byte offset in file where reading starts 7351da177e4SLinus Torvalds * 7361da177e4SLinus Torvalds * We use this function for direct reads instead of calling 7371da177e4SLinus Torvalds * generic_file_aio_read() in order to avoid gfar's check to see if 7381da177e4SLinus Torvalds * the request starts before the end of the file. For that check 7391da177e4SLinus Torvalds * to work, we must generate a GETATTR before each direct read, and 7401da177e4SLinus Torvalds * even then there is a window between the GETATTR and the subsequent 74188467055SChuck Lever * READ where the file size could change. Our preference is simply 7421da177e4SLinus Torvalds * to do all reads the application wants, and the server will take 7431da177e4SLinus Torvalds * care of managing the end of file boundary. 7441da177e4SLinus Torvalds * 7451da177e4SLinus Torvalds * This function also eliminates unnecessarily updating the file's 7461da177e4SLinus Torvalds * atime locally, as the NFS server sets the file's atime, and this 7471da177e4SLinus Torvalds * client must read the updated atime from the server back into its 7481da177e4SLinus Torvalds * cache. 7491da177e4SLinus Torvalds */ 750027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov, 751027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds ssize_t retval = -EINVAL; 7541da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 7551da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 756027445c3SBadari Pulavarty /* XXX: temporary */ 757027445c3SBadari Pulavarty const char __user *buf = iov[0].iov_base; 758027445c3SBadari Pulavarty size_t count = iov[0].iov_len; 7591da177e4SLinus Torvalds 760ce1a8e67SChuck Lever dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", 76101cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 76201cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 763ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 7641da177e4SLinus Torvalds 765027445c3SBadari Pulavarty if (nr_segs != 1) 7661da177e4SLinus Torvalds goto out; 767*070ea602SChuck Lever 7681da177e4SLinus Torvalds retval = -EFAULT; 7690cdd80d0SChuck Lever if (!access_ok(VERIFY_WRITE, buf, count)) 7701da177e4SLinus Torvalds goto out; 7711da177e4SLinus Torvalds retval = 0; 7721da177e4SLinus Torvalds if (!count) 7731da177e4SLinus Torvalds goto out; 7741da177e4SLinus Torvalds 77529884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 7761da177e4SLinus Torvalds if (retval) 7771da177e4SLinus Torvalds goto out; 7781da177e4SLinus Torvalds 779607f31e8STrond Myklebust retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos); 7801da177e4SLinus Torvalds if (retval > 0) 7810cdd80d0SChuck Lever iocb->ki_pos = pos + retval; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds out: 7841da177e4SLinus Torvalds return retval; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds /** 7881da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 7891da177e4SLinus Torvalds * @iocb: target I/O control block 790027445c3SBadari Pulavarty * @iov: vector of user buffers from which to write data 791027445c3SBadari Pulavarty * @nr_segs: size of iov vector 79288467055SChuck Lever * @pos: byte offset in file where writing starts 7931da177e4SLinus Torvalds * 7941da177e4SLinus Torvalds * We use this function for direct writes instead of calling 7951da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 7961da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 7971da177e4SLinus Torvalds * the new i_size and this client must read the updated size 7981da177e4SLinus Torvalds * back into its cache. We let the server do generic write 7991da177e4SLinus Torvalds * parameter checking and report problems. 8001da177e4SLinus Torvalds * 8011da177e4SLinus Torvalds * We also avoid an unnecessary invocation of generic_osync_inode(), 8021da177e4SLinus Torvalds * as it is fairly meaningless to sync the metadata of an NFS file. 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8051da177e4SLinus Torvalds * 8061da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8071da177e4SLinus Torvalds * readers of this file. 8081da177e4SLinus Torvalds * 8091da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8101da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8111da177e4SLinus Torvalds */ 812027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov, 813027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 8141da177e4SLinus Torvalds { 815*070ea602SChuck Lever ssize_t retval = -EINVAL; 8161da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 8171da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 818027445c3SBadari Pulavarty /* XXX: temporary */ 819027445c3SBadari Pulavarty const char __user *buf = iov[0].iov_base; 820027445c3SBadari Pulavarty size_t count = iov[0].iov_len; 8211da177e4SLinus Torvalds 822a3f565b1SChuck Lever dprintk("nfs: direct write(%s/%s, %lu@%Ld)\n", 82301cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 82401cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, 825ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 8261da177e4SLinus Torvalds 827027445c3SBadari Pulavarty if (nr_segs != 1) 828*070ea602SChuck Lever goto out; 829027445c3SBadari Pulavarty 830ce1a8e67SChuck Lever retval = generic_write_checks(file, &pos, &count, 0); 831ce1a8e67SChuck Lever if (retval) 8321da177e4SLinus Torvalds goto out; 833ce1a8e67SChuck Lever 834ce1a8e67SChuck Lever retval = -EINVAL; 835ce1a8e67SChuck Lever if ((ssize_t) count < 0) 8361da177e4SLinus Torvalds goto out; 8371da177e4SLinus Torvalds retval = 0; 8381da177e4SLinus Torvalds if (!count) 8391da177e4SLinus Torvalds goto out; 840ce1a8e67SChuck Lever 841ce1a8e67SChuck Lever retval = -EFAULT; 84247989d74SChuck Lever if (!access_ok(VERIFY_READ, buf, count)) 843ce1a8e67SChuck Lever goto out; 8441da177e4SLinus Torvalds 84529884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 8461da177e4SLinus Torvalds if (retval) 8471da177e4SLinus Torvalds goto out; 8481da177e4SLinus Torvalds 849607f31e8STrond Myklebust retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos); 8509eafa8ccSChuck Lever 8511da177e4SLinus Torvalds if (retval > 0) 852ce1a8e67SChuck Lever iocb->ki_pos = pos + retval; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds out: 8551da177e4SLinus Torvalds return retval; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 85888467055SChuck Lever /** 85988467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 86088467055SChuck Lever * 86188467055SChuck Lever */ 862f7b422b1SDavid Howells int __init nfs_init_directcache(void) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 8651da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 866fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 867fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 8681da177e4SLinus Torvalds NULL, NULL); 8691da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 8701da177e4SLinus Torvalds return -ENOMEM; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds return 0; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 87588467055SChuck Lever /** 876f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 87788467055SChuck Lever * 87888467055SChuck Lever */ 879266bee88SDavid Brownell void nfs_destroy_directcache(void) 8801da177e4SLinus Torvalds { 8811a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 8821da177e4SLinus Torvalds } 883