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/config.h> 421da177e4SLinus Torvalds #include <linux/errno.h> 431da177e4SLinus Torvalds #include <linux/sched.h> 441da177e4SLinus Torvalds #include <linux/kernel.h> 451da177e4SLinus Torvalds #include <linux/smp_lock.h> 461da177e4SLinus Torvalds #include <linux/file.h> 471da177e4SLinus Torvalds #include <linux/pagemap.h> 481da177e4SLinus Torvalds #include <linux/kref.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <linux/nfs_fs.h> 511da177e4SLinus Torvalds #include <linux/nfs_page.h> 521da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #include <asm/system.h> 551da177e4SLinus Torvalds #include <asm/uaccess.h> 561da177e4SLinus Torvalds #include <asm/atomic.h> 571da177e4SLinus Torvalds 5891d5b470SChuck Lever #include "iostat.h" 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds static kmem_cache_t *nfs_direct_cachep; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds struct nfs_direct_req { 681da177e4SLinus Torvalds struct kref kref; /* release manager */ 6915ce4a0cSChuck Lever 7015ce4a0cSChuck Lever /* I/O parameters */ 71a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 7299514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 7388467055SChuck Lever struct inode * inode; /* target file of i/o */ 7415ce4a0cSChuck Lever 7515ce4a0cSChuck Lever /* completion state */ 76b1c5921cSChuck Lever atomic_t io_count; /* i/os we're waiting for */ 7715ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 7815ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 791da177e4SLinus Torvalds error; /* any reported error */ 80d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 81fad61490STrond Myklebust 82fad61490STrond Myklebust /* commit state */ 83*82b145c5SChuck Lever struct list_head rewrite_list; /* saved nfs_write_data structs */ 84fad61490STrond Myklebust struct nfs_write_data * commit_data; /* special write_data for commits */ 85fad61490STrond Myklebust int flags; 86fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 87fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 88fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 891da177e4SLinus Torvalds }; 901da177e4SLinus Torvalds 91fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 92fedb595cSChuck Lever static const struct rpc_call_ops nfs_write_direct_ops; 931da177e4SLinus Torvalds 94b1c5921cSChuck Lever static inline void get_dreq(struct nfs_direct_req *dreq) 95b1c5921cSChuck Lever { 96b1c5921cSChuck Lever atomic_inc(&dreq->io_count); 97b1c5921cSChuck Lever } 98b1c5921cSChuck Lever 99b1c5921cSChuck Lever static inline int put_dreq(struct nfs_direct_req *dreq) 100b1c5921cSChuck Lever { 101b1c5921cSChuck Lever return atomic_dec_and_test(&dreq->io_count); 102b1c5921cSChuck Lever } 103b1c5921cSChuck Lever 10406cf6f2eSChuck Lever /* 10506cf6f2eSChuck Lever * "size" is never larger than rsize or wsize. 10606cf6f2eSChuck Lever */ 10706cf6f2eSChuck Lever static inline int nfs_direct_count_pages(unsigned long user_addr, size_t size) 10806cf6f2eSChuck Lever { 10906cf6f2eSChuck Lever int page_count; 11006cf6f2eSChuck Lever 11106cf6f2eSChuck Lever page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT; 11206cf6f2eSChuck Lever page_count -= user_addr >> PAGE_SHIFT; 11306cf6f2eSChuck Lever BUG_ON(page_count < 0); 11406cf6f2eSChuck Lever 11506cf6f2eSChuck Lever return page_count; 11606cf6f2eSChuck Lever } 11706cf6f2eSChuck Lever 118*82b145c5SChuck Lever static inline unsigned int nfs_max_pages(unsigned int size) 119*82b145c5SChuck Lever { 120*82b145c5SChuck Lever return (size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 121*82b145c5SChuck Lever } 122*82b145c5SChuck Lever 1231da177e4SLinus Torvalds /** 124b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 125b8a32e2bSChuck Lever * @rw: direction (read or write) 126b8a32e2bSChuck Lever * @iocb: target I/O control block 127b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 128b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 129b8a32e2bSChuck Lever * @nr_segs: size of iovec array 130b8a32e2bSChuck Lever * 131b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 132b8a32e2bSChuck Lever * the NFS client supports direct I/O. However, we shunt off direct 133b8a32e2bSChuck Lever * read and write requests before the VFS gets them, so this method 134b8a32e2bSChuck Lever * should never be called. 1351da177e4SLinus Torvalds */ 136b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) 137b8a32e2bSChuck Lever { 138b8a32e2bSChuck Lever dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", 139e99170ffSTrond Myklebust iocb->ki_filp->f_dentry->d_name.name, 140e99170ffSTrond Myklebust (long long) pos, nr_segs); 141b8a32e2bSChuck Lever 142b8a32e2bSChuck Lever return -EINVAL; 143b8a32e2bSChuck Lever } 144b8a32e2bSChuck Lever 1459c93ab7dSChuck Lever static void nfs_direct_dirty_pages(struct page **pages, int npages) 1466b45d858STrond Myklebust { 1476b45d858STrond Myklebust int i; 1486b45d858STrond Myklebust for (i = 0; i < npages; i++) { 1496b45d858STrond Myklebust struct page *page = pages[i]; 1509c93ab7dSChuck Lever if (!PageCompound(page)) 1516b45d858STrond Myklebust set_page_dirty_lock(page); 1526b45d858STrond Myklebust } 1539c93ab7dSChuck Lever } 1549c93ab7dSChuck Lever 1559c93ab7dSChuck Lever static void nfs_direct_release_pages(struct page **pages, int npages) 1569c93ab7dSChuck Lever { 1579c93ab7dSChuck Lever int i; 1589c93ab7dSChuck Lever for (i = 0; i < npages; i++) 1599c93ab7dSChuck Lever page_cache_release(pages[i]); 1606b45d858STrond Myklebust } 1616b45d858STrond Myklebust 16293619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds struct nfs_direct_req *dreq; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL); 1671da177e4SLinus Torvalds if (!dreq) 1681da177e4SLinus Torvalds return NULL; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds kref_init(&dreq->kref); 171*82b145c5SChuck Lever kref_get(&dreq->kref); 172d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 173fad61490STrond Myklebust INIT_LIST_HEAD(&dreq->rewrite_list); 17493619e59SChuck Lever dreq->iocb = NULL; 175a8881f5aSTrond Myklebust dreq->ctx = NULL; 17615ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 177b1c5921cSChuck Lever atomic_set(&dreq->io_count, 0); 17815ce4a0cSChuck Lever dreq->count = 0; 17915ce4a0cSChuck Lever dreq->error = 0; 180fad61490STrond Myklebust dreq->flags = 0; 18193619e59SChuck Lever 18293619e59SChuck Lever return dreq; 18393619e59SChuck Lever } 18493619e59SChuck Lever 1851da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref) 1861da177e4SLinus Torvalds { 1871da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 188a8881f5aSTrond Myklebust 189a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 190a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 1911da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 194d4cc948bSChuck Lever /* 195bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 196bc0fb201SChuck Lever */ 197bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 198bc0fb201SChuck Lever { 19915ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 200bc0fb201SChuck Lever 201bc0fb201SChuck Lever /* Async requests don't wait here */ 202bc0fb201SChuck Lever if (dreq->iocb) 203bc0fb201SChuck Lever goto out; 204bc0fb201SChuck Lever 205d72b7a6bSTrond Myklebust result = wait_for_completion_interruptible(&dreq->completion); 206bc0fb201SChuck Lever 207bc0fb201SChuck Lever if (!result) 20815ce4a0cSChuck Lever result = dreq->error; 209bc0fb201SChuck Lever if (!result) 21015ce4a0cSChuck Lever result = dreq->count; 211bc0fb201SChuck Lever 212bc0fb201SChuck Lever out: 213bc0fb201SChuck Lever kref_put(&dreq->kref, nfs_direct_req_release); 214bc0fb201SChuck Lever return (ssize_t) result; 215bc0fb201SChuck Lever } 216bc0fb201SChuck Lever 217bc0fb201SChuck Lever /* 21806cf6f2eSChuck Lever * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 21906cf6f2eSChuck Lever * the iocb is still valid here if this is a synchronous request. 22063ab46abSChuck Lever */ 22163ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq) 22263ab46abSChuck Lever { 22363ab46abSChuck Lever if (dreq->iocb) { 22415ce4a0cSChuck Lever long res = (long) dreq->error; 22563ab46abSChuck Lever if (!res) 22615ce4a0cSChuck Lever res = (long) dreq->count; 22763ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 228d72b7a6bSTrond Myklebust } 229d72b7a6bSTrond Myklebust complete_all(&dreq->completion); 23063ab46abSChuck Lever 23163ab46abSChuck Lever kref_put(&dreq->kref, nfs_direct_req_release); 23263ab46abSChuck Lever } 23363ab46abSChuck Lever 23463ab46abSChuck Lever /* 23506cf6f2eSChuck Lever * We must hold a reference to all the pages in this direct read request 23606cf6f2eSChuck Lever * until the RPCs complete. This could be long *after* we are woken up in 23706cf6f2eSChuck Lever * nfs_direct_wait (for instance, if someone hits ^C on a slow server). 23806cf6f2eSChuck Lever */ 239ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata) 2401da177e4SLinus Torvalds { 241ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 2421da177e4SLinus Torvalds struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 2431da177e4SLinus Torvalds 244ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 245ec06c096STrond Myklebust return; 2461da177e4SLinus Torvalds 24706cf6f2eSChuck Lever nfs_direct_dirty_pages(data->pagevec, data->npages); 24806cf6f2eSChuck Lever nfs_direct_release_pages(data->pagevec, data->npages); 24906cf6f2eSChuck Lever 25015ce4a0cSChuck Lever spin_lock(&dreq->lock); 25115ce4a0cSChuck Lever 25215ce4a0cSChuck Lever if (likely(task->tk_status >= 0)) 25315ce4a0cSChuck Lever dreq->count += data->res.count; 2541da177e4SLinus Torvalds else 25515ce4a0cSChuck Lever dreq->error = task->tk_status; 2561da177e4SLinus Torvalds 25715ce4a0cSChuck Lever spin_unlock(&dreq->lock); 2581da177e4SLinus Torvalds 25906cf6f2eSChuck Lever if (put_dreq(dreq)) 26063ab46abSChuck Lever nfs_direct_complete(dreq); 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 263ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = { 264ec06c096STrond Myklebust .rpc_call_done = nfs_direct_read_result, 265ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 266ec06c096STrond Myklebust }; 267ec06c096STrond Myklebust 268d4cc948bSChuck Lever /* 269*82b145c5SChuck Lever * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 270*82b145c5SChuck Lever * operation. If nfs_readdata_alloc() or get_user_pages() fails, 271*82b145c5SChuck Lever * bail and stop sending more reads. Read length accounting is 272*82b145c5SChuck Lever * handled automatically by nfs_direct_read_result(). Otherwise, if 273*82b145c5SChuck Lever * no requests have been sent, just return an error. 2741da177e4SLinus Torvalds */ 27506cf6f2eSChuck Lever static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos) 2761da177e4SLinus Torvalds { 277a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 278a8881f5aSTrond Myklebust struct inode *inode = ctx->dentry->d_inode; 2795dd602f2SChuck Lever size_t rsize = NFS_SERVER(inode)->rsize; 280*82b145c5SChuck Lever unsigned int rpages = nfs_max_pages(rsize); 28106cf6f2eSChuck Lever unsigned int pgbase; 28206cf6f2eSChuck Lever int result; 28306cf6f2eSChuck Lever ssize_t started = 0; 284*82b145c5SChuck Lever 285*82b145c5SChuck Lever get_dreq(dreq); 2861da177e4SLinus Torvalds 28751a7bc6cSChuck Lever pgbase = user_addr & ~PAGE_MASK; 2881da177e4SLinus Torvalds do { 289*82b145c5SChuck Lever struct nfs_read_data *data; 2905dd602f2SChuck Lever size_t bytes; 2911da177e4SLinus Torvalds 292*82b145c5SChuck Lever result = -ENOMEM; 293*82b145c5SChuck Lever data = nfs_readdata_alloc(rpages); 294*82b145c5SChuck Lever if (unlikely(!data)) 295*82b145c5SChuck Lever break; 296*82b145c5SChuck Lever 2971da177e4SLinus Torvalds bytes = rsize; 2981da177e4SLinus Torvalds if (count < rsize) 2991da177e4SLinus Torvalds bytes = count; 3001da177e4SLinus Torvalds 30106cf6f2eSChuck Lever data->npages = nfs_direct_count_pages(user_addr, bytes); 30206cf6f2eSChuck Lever down_read(¤t->mm->mmap_sem); 30306cf6f2eSChuck Lever result = get_user_pages(current, current->mm, user_addr, 30406cf6f2eSChuck Lever data->npages, 1, 0, data->pagevec, NULL); 30506cf6f2eSChuck Lever up_read(¤t->mm->mmap_sem); 306*82b145c5SChuck Lever if (unlikely(result < data->npages)) { 307*82b145c5SChuck Lever if (result > 0) 308*82b145c5SChuck Lever nfs_direct_release_pages(data->pagevec, result); 309*82b145c5SChuck Lever nfs_readdata_release(data); 310*82b145c5SChuck Lever break; 311*82b145c5SChuck Lever } 31206cf6f2eSChuck Lever 313*82b145c5SChuck Lever get_dreq(dreq); 314*82b145c5SChuck Lever 315*82b145c5SChuck Lever data->req = (struct nfs_page *) dreq; 3161da177e4SLinus Torvalds data->inode = inode; 3171da177e4SLinus Torvalds data->cred = ctx->cred; 3181da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 3191da177e4SLinus Torvalds data->args.context = ctx; 32088467055SChuck Lever data->args.offset = pos; 3211da177e4SLinus Torvalds data->args.pgbase = pgbase; 32206cf6f2eSChuck Lever data->args.pages = data->pagevec; 3231da177e4SLinus Torvalds data->args.count = bytes; 3241da177e4SLinus Torvalds data->res.fattr = &data->fattr; 3251da177e4SLinus Torvalds data->res.eof = 0; 3261da177e4SLinus Torvalds data->res.count = bytes; 3271da177e4SLinus Torvalds 328ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 329ec06c096STrond Myklebust &nfs_read_direct_ops, data); 3301da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long) inode; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds lock_kernel(); 3351da177e4SLinus Torvalds rpc_execute(&data->task); 3361da177e4SLinus Torvalds unlock_kernel(); 3371da177e4SLinus Torvalds 338606bbba0SChuck Lever dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n", 3391da177e4SLinus Torvalds data->task.tk_pid, 3401da177e4SLinus Torvalds inode->i_sb->s_id, 3411da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 3421da177e4SLinus Torvalds bytes, 3431da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3441da177e4SLinus Torvalds 34506cf6f2eSChuck Lever started += bytes; 34606cf6f2eSChuck Lever user_addr += bytes; 34788467055SChuck Lever pos += bytes; 3481da177e4SLinus Torvalds pgbase += bytes; 3491da177e4SLinus Torvalds pgbase &= ~PAGE_MASK; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds count -= bytes; 3521da177e4SLinus Torvalds } while (count != 0); 35306cf6f2eSChuck Lever 35406cf6f2eSChuck Lever if (put_dreq(dreq)) 35506cf6f2eSChuck Lever nfs_direct_complete(dreq); 3561da177e4SLinus Torvalds 35706cf6f2eSChuck Lever if (started) 35806cf6f2eSChuck Lever return 0; 35906cf6f2eSChuck Lever return result < 0 ? (ssize_t) result : -EFAULT; 36006cf6f2eSChuck Lever } 36106cf6f2eSChuck Lever 36206cf6f2eSChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos) 3631da177e4SLinus Torvalds { 364*82b145c5SChuck Lever ssize_t result = 0; 3651da177e4SLinus Torvalds sigset_t oldset; 36699514f8fSChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 3671da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(inode); 3681da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3691da177e4SLinus Torvalds 370*82b145c5SChuck Lever dreq = nfs_direct_req_alloc(); 3711da177e4SLinus Torvalds if (!dreq) 3721da177e4SLinus Torvalds return -ENOMEM; 3731da177e4SLinus Torvalds 37491d5b470SChuck Lever dreq->inode = inode; 375a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 376487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 377487b8372SChuck Lever dreq->iocb = iocb; 3781da177e4SLinus Torvalds 37991d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); 3801da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 38106cf6f2eSChuck Lever result = nfs_direct_read_schedule(dreq, user_addr, count, pos); 38206cf6f2eSChuck Lever if (!result) 383bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 3841da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds return result; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 389fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) 3901da177e4SLinus Torvalds { 391*82b145c5SChuck Lever while (!list_empty(&dreq->rewrite_list)) { 392*82b145c5SChuck Lever struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages); 393fad61490STrond Myklebust list_del(&data->pages); 39406cf6f2eSChuck Lever nfs_direct_release_pages(data->pagevec, data->npages); 395fad61490STrond Myklebust nfs_writedata_release(data); 396fad61490STrond Myklebust } 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 399fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 400fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 4011da177e4SLinus Torvalds { 402fedb595cSChuck Lever struct inode *inode = dreq->inode; 403fedb595cSChuck Lever struct list_head *p; 404fedb595cSChuck Lever struct nfs_write_data *data; 4051da177e4SLinus Torvalds 406fad61490STrond Myklebust dreq->count = 0; 407fedb595cSChuck Lever get_dreq(dreq); 4081da177e4SLinus Torvalds 409fedb595cSChuck Lever list_for_each(p, &dreq->rewrite_list) { 410fedb595cSChuck Lever data = list_entry(p, struct nfs_write_data, pages); 411fedb595cSChuck Lever 412fedb595cSChuck Lever get_dreq(dreq); 413fedb595cSChuck Lever 414fedb595cSChuck Lever /* 415fedb595cSChuck Lever * Reset data->res. 416fedb595cSChuck Lever */ 417fedb595cSChuck Lever nfs_fattr_init(&data->fattr); 418fedb595cSChuck Lever data->res.count = data->args.count; 419fedb595cSChuck Lever memset(&data->verf, 0, sizeof(data->verf)); 420fedb595cSChuck Lever 421fedb595cSChuck Lever /* 422fedb595cSChuck Lever * Reuse data->task; data->args should not have changed 423fedb595cSChuck Lever * since the original request was sent. 424fedb595cSChuck Lever */ 425fedb595cSChuck Lever rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 426fedb595cSChuck Lever &nfs_write_direct_ops, data); 427fedb595cSChuck Lever NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE); 428fedb595cSChuck Lever 429fedb595cSChuck Lever data->task.tk_priority = RPC_PRIORITY_NORMAL; 430fedb595cSChuck Lever data->task.tk_cookie = (unsigned long) inode; 431fedb595cSChuck Lever 432fedb595cSChuck Lever /* 433fedb595cSChuck Lever * We're called via an RPC callback, so BKL is already held. 434fedb595cSChuck Lever */ 435fedb595cSChuck Lever rpc_execute(&data->task); 436fedb595cSChuck Lever 437fedb595cSChuck Lever dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 438fedb595cSChuck Lever data->task.tk_pid, 439fedb595cSChuck Lever inode->i_sb->s_id, 440fedb595cSChuck Lever (long long)NFS_FILEID(inode), 441fedb595cSChuck Lever data->args.count, 442fedb595cSChuck Lever (unsigned long long)data->args.offset); 443fedb595cSChuck Lever } 444fedb595cSChuck Lever 445fedb595cSChuck Lever if (put_dreq(dreq)) 446fedb595cSChuck Lever nfs_direct_write_complete(dreq, inode); 447fad61490STrond Myklebust } 4481da177e4SLinus Torvalds 449fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) 450fad61490STrond Myklebust { 451fad61490STrond Myklebust struct nfs_write_data *data = calldata; 452fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 4531da177e4SLinus Torvalds 454fad61490STrond Myklebust /* Call the NFS version-specific code */ 455fad61490STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 456fad61490STrond Myklebust return; 457fad61490STrond Myklebust if (unlikely(task->tk_status < 0)) { 458fad61490STrond Myklebust dreq->error = task->tk_status; 459fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 460fad61490STrond Myklebust } 461fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 462fad61490STrond Myklebust dprintk("NFS: %5u commit verify failed\n", task->tk_pid); 463fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 464fad61490STrond Myklebust } 465fad61490STrond Myklebust 466fad61490STrond Myklebust dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status); 467fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 468fad61490STrond Myklebust } 469fad61490STrond Myklebust 470fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = { 471fad61490STrond Myklebust .rpc_call_done = nfs_direct_commit_result, 472fad61490STrond Myklebust .rpc_release = nfs_commit_release, 473fad61490STrond Myklebust }; 474fad61490STrond Myklebust 475fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 476fad61490STrond Myklebust { 477fad61490STrond Myklebust struct nfs_write_data *data = dreq->commit_data; 478fad61490STrond Myklebust 479fad61490STrond Myklebust data->inode = dreq->inode; 480a8881f5aSTrond Myklebust data->cred = dreq->ctx->cred; 481fad61490STrond Myklebust 482fad61490STrond Myklebust data->args.fh = NFS_FH(data->inode); 48351a7bc6cSChuck Lever data->args.offset = 0; 48451a7bc6cSChuck Lever data->args.count = 0; 485fad61490STrond Myklebust data->res.count = 0; 486fad61490STrond Myklebust data->res.fattr = &data->fattr; 487fad61490STrond Myklebust data->res.verf = &data->verf; 488fad61490STrond Myklebust 489fad61490STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC, 490fad61490STrond Myklebust &nfs_commit_direct_ops, data); 491fad61490STrond Myklebust NFS_PROTO(data->inode)->commit_setup(data, 0); 492fad61490STrond Myklebust 493fad61490STrond Myklebust data->task.tk_priority = RPC_PRIORITY_NORMAL; 494fad61490STrond Myklebust data->task.tk_cookie = (unsigned long)data->inode; 495fad61490STrond Myklebust /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ 496fad61490STrond Myklebust dreq->commit_data = NULL; 497fad61490STrond Myklebust 498e99170ffSTrond Myklebust dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds lock_kernel(); 501fad61490STrond Myklebust rpc_execute(&data->task); 5021da177e4SLinus Torvalds unlock_kernel(); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 505fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 5061da177e4SLinus Torvalds { 507fad61490STrond Myklebust int flags = dreq->flags; 5081da177e4SLinus Torvalds 509fad61490STrond Myklebust dreq->flags = 0; 510fad61490STrond Myklebust switch (flags) { 511fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 512fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 5131da177e4SLinus Torvalds break; 514fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 515fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 5161da177e4SLinus Torvalds break; 5171da177e4SLinus Torvalds default: 518fad61490STrond Myklebust nfs_end_data_update(inode); 519fad61490STrond Myklebust if (dreq->commit_data != NULL) 520fad61490STrond Myklebust nfs_commit_free(dreq->commit_data); 521fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 522fad61490STrond Myklebust nfs_direct_complete(dreq); 5231da177e4SLinus Torvalds } 524fad61490STrond Myklebust } 525fad61490STrond Myklebust 526fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 527fad61490STrond Myklebust { 528fad61490STrond Myklebust dreq->commit_data = nfs_commit_alloc(0); 529fad61490STrond Myklebust if (dreq->commit_data != NULL) 530fad61490STrond Myklebust dreq->commit_data->req = (struct nfs_page *) dreq; 531fad61490STrond Myklebust } 532fad61490STrond Myklebust #else 533fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 534fad61490STrond Myklebust { 535fad61490STrond Myklebust dreq->commit_data = NULL; 536fad61490STrond Myklebust } 537fad61490STrond Myklebust 538fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 539fad61490STrond Myklebust { 540fad61490STrond Myklebust nfs_end_data_update(inode); 541fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 542fad61490STrond Myklebust nfs_direct_complete(dreq); 543fad61490STrond Myklebust } 544fad61490STrond Myklebust #endif 545fad61490STrond Myklebust 546462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata) 547462d5b32SChuck Lever { 548462d5b32SChuck Lever struct nfs_write_data *data = calldata; 549462d5b32SChuck Lever struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 550462d5b32SChuck Lever int status = task->tk_status; 551462d5b32SChuck Lever 552462d5b32SChuck Lever if (nfs_writeback_done(task, data) != 0) 553462d5b32SChuck Lever return; 554462d5b32SChuck Lever 55515ce4a0cSChuck Lever spin_lock(&dreq->lock); 556462d5b32SChuck Lever 55715ce4a0cSChuck Lever if (likely(status >= 0)) 55815ce4a0cSChuck Lever dreq->count += data->res.count; 55915ce4a0cSChuck Lever else 560fad61490STrond Myklebust dreq->error = task->tk_status; 56115ce4a0cSChuck Lever 562fad61490STrond Myklebust if (data->res.verf->committed != NFS_FILE_SYNC) { 563fad61490STrond Myklebust switch (dreq->flags) { 564fad61490STrond Myklebust case 0: 565fad61490STrond Myklebust memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); 566fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_DO_COMMIT; 567fad61490STrond Myklebust break; 568fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 569fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { 570fad61490STrond Myklebust dprintk("NFS: %5u write verify failed\n", task->tk_pid); 571fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 572fad61490STrond Myklebust } 573fad61490STrond Myklebust } 574fad61490STrond Myklebust } 575fad61490STrond Myklebust 576fad61490STrond Myklebust spin_unlock(&dreq->lock); 577fad61490STrond Myklebust } 578fad61490STrond Myklebust 579fad61490STrond Myklebust /* 580fad61490STrond Myklebust * NB: Return the value of the first error return code. Subsequent 581fad61490STrond Myklebust * errors after the first one are ignored. 582fad61490STrond Myklebust */ 583fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata) 584fad61490STrond Myklebust { 585fad61490STrond Myklebust struct nfs_write_data *data = calldata; 586fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 587fad61490STrond Myklebust 588b1c5921cSChuck Lever if (put_dreq(dreq)) 589fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 590462d5b32SChuck Lever } 591462d5b32SChuck Lever 592462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = { 593462d5b32SChuck Lever .rpc_call_done = nfs_direct_write_result, 594fad61490STrond Myklebust .rpc_release = nfs_direct_write_release, 595462d5b32SChuck Lever }; 596462d5b32SChuck Lever 597462d5b32SChuck Lever /* 598*82b145c5SChuck Lever * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 599*82b145c5SChuck Lever * operation. If nfs_writedata_alloc() or get_user_pages() fails, 600*82b145c5SChuck Lever * bail and stop sending more writes. Write length accounting is 601*82b145c5SChuck Lever * handled automatically by nfs_direct_write_result(). Otherwise, if 602*82b145c5SChuck Lever * no requests have been sent, just return an error. 603462d5b32SChuck Lever */ 60406cf6f2eSChuck Lever static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync) 605462d5b32SChuck Lever { 606a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 607a8881f5aSTrond Myklebust struct inode *inode = ctx->dentry->d_inode; 608462d5b32SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 609*82b145c5SChuck Lever unsigned int wpages = nfs_max_pages(wsize); 61006cf6f2eSChuck Lever unsigned int pgbase; 61106cf6f2eSChuck Lever int result; 61206cf6f2eSChuck Lever ssize_t started = 0; 613*82b145c5SChuck Lever 614*82b145c5SChuck Lever get_dreq(dreq); 615462d5b32SChuck Lever 61651a7bc6cSChuck Lever pgbase = user_addr & ~PAGE_MASK; 617462d5b32SChuck Lever do { 618*82b145c5SChuck Lever struct nfs_write_data *data; 619462d5b32SChuck Lever size_t bytes; 620462d5b32SChuck Lever 621*82b145c5SChuck Lever result = -ENOMEM; 622*82b145c5SChuck Lever data = nfs_writedata_alloc(wpages); 623*82b145c5SChuck Lever if (unlikely(!data)) 624*82b145c5SChuck Lever break; 625*82b145c5SChuck Lever 626462d5b32SChuck Lever bytes = wsize; 627462d5b32SChuck Lever if (count < wsize) 628462d5b32SChuck Lever bytes = count; 629462d5b32SChuck Lever 63006cf6f2eSChuck Lever data->npages = nfs_direct_count_pages(user_addr, bytes); 63106cf6f2eSChuck Lever down_read(¤t->mm->mmap_sem); 63206cf6f2eSChuck Lever result = get_user_pages(current, current->mm, user_addr, 63306cf6f2eSChuck Lever data->npages, 0, 0, data->pagevec, NULL); 63406cf6f2eSChuck Lever up_read(¤t->mm->mmap_sem); 635*82b145c5SChuck Lever if (unlikely(result < data->npages)) { 636*82b145c5SChuck Lever if (result > 0) 637*82b145c5SChuck Lever nfs_direct_release_pages(data->pagevec, result); 638*82b145c5SChuck Lever nfs_writedata_release(data); 639*82b145c5SChuck Lever break; 640*82b145c5SChuck Lever } 641*82b145c5SChuck Lever 642*82b145c5SChuck Lever get_dreq(dreq); 64306cf6f2eSChuck Lever 644fad61490STrond Myklebust list_move_tail(&data->pages, &dreq->rewrite_list); 645462d5b32SChuck Lever 646*82b145c5SChuck Lever data->req = (struct nfs_page *) dreq; 647462d5b32SChuck Lever data->inode = inode; 648462d5b32SChuck Lever data->cred = ctx->cred; 649462d5b32SChuck Lever data->args.fh = NFS_FH(inode); 650462d5b32SChuck Lever data->args.context = ctx; 65188467055SChuck Lever data->args.offset = pos; 652462d5b32SChuck Lever data->args.pgbase = pgbase; 65306cf6f2eSChuck Lever data->args.pages = data->pagevec; 654462d5b32SChuck Lever data->args.count = bytes; 655462d5b32SChuck Lever data->res.fattr = &data->fattr; 656462d5b32SChuck Lever data->res.count = bytes; 65747989d74SChuck Lever data->res.verf = &data->verf; 658462d5b32SChuck Lever 659462d5b32SChuck Lever rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 660462d5b32SChuck Lever &nfs_write_direct_ops, data); 661fad61490STrond Myklebust NFS_PROTO(inode)->write_setup(data, sync); 662462d5b32SChuck Lever 663462d5b32SChuck Lever data->task.tk_priority = RPC_PRIORITY_NORMAL; 664462d5b32SChuck Lever data->task.tk_cookie = (unsigned long) inode; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds lock_kernel(); 667462d5b32SChuck Lever rpc_execute(&data->task); 6681da177e4SLinus Torvalds unlock_kernel(); 6691da177e4SLinus Torvalds 670606bbba0SChuck Lever dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n", 671462d5b32SChuck Lever data->task.tk_pid, 672462d5b32SChuck Lever inode->i_sb->s_id, 673462d5b32SChuck Lever (long long)NFS_FILEID(inode), 674462d5b32SChuck Lever bytes, 675462d5b32SChuck Lever (unsigned long long)data->args.offset); 676462d5b32SChuck Lever 67706cf6f2eSChuck Lever started += bytes; 67806cf6f2eSChuck Lever user_addr += bytes; 67988467055SChuck Lever pos += bytes; 680462d5b32SChuck Lever pgbase += bytes; 681462d5b32SChuck Lever pgbase &= ~PAGE_MASK; 682462d5b32SChuck Lever 683462d5b32SChuck Lever count -= bytes; 684462d5b32SChuck Lever } while (count != 0); 68506cf6f2eSChuck Lever 68606cf6f2eSChuck Lever if (put_dreq(dreq)) 68706cf6f2eSChuck Lever nfs_direct_write_complete(dreq, inode); 6881da177e4SLinus Torvalds 68906cf6f2eSChuck Lever if (started) 69006cf6f2eSChuck Lever return 0; 69106cf6f2eSChuck Lever return result < 0 ? (ssize_t) result : -EFAULT; 69206cf6f2eSChuck Lever } 69306cf6f2eSChuck Lever 69406cf6f2eSChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos) 695462d5b32SChuck Lever { 696*82b145c5SChuck Lever ssize_t result = 0; 697462d5b32SChuck Lever sigset_t oldset; 698c89f2ee5SChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 699462d5b32SChuck Lever struct rpc_clnt *clnt = NFS_CLIENT(inode); 700462d5b32SChuck Lever struct nfs_direct_req *dreq; 701fad61490STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize; 702fad61490STrond Myklebust int sync = 0; 703462d5b32SChuck Lever 704*82b145c5SChuck Lever dreq = nfs_direct_req_alloc(); 705462d5b32SChuck Lever if (!dreq) 706462d5b32SChuck Lever return -ENOMEM; 707*82b145c5SChuck Lever nfs_alloc_commit_data(dreq); 708*82b145c5SChuck Lever 709fad61490STrond Myklebust if (dreq->commit_data == NULL || count < wsize) 710fad61490STrond Myklebust sync = FLUSH_STABLE; 711462d5b32SChuck Lever 712c89f2ee5SChuck Lever dreq->inode = inode; 713a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 714c89f2ee5SChuck Lever if (!is_sync_kiocb(iocb)) 715c89f2ee5SChuck Lever dreq->iocb = iocb; 716462d5b32SChuck Lever 71747989d74SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count); 71847989d74SChuck Lever 719462d5b32SChuck Lever nfs_begin_data_update(inode); 720462d5b32SChuck Lever 721462d5b32SChuck Lever rpc_clnt_sigmask(clnt, &oldset); 72206cf6f2eSChuck Lever result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync); 72306cf6f2eSChuck Lever if (!result) 724c89f2ee5SChuck Lever result = nfs_direct_wait(dreq); 725462d5b32SChuck Lever rpc_clnt_sigunmask(clnt, &oldset); 726462d5b32SChuck Lever 7271da177e4SLinus Torvalds return result; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /** 7311da177e4SLinus Torvalds * nfs_file_direct_read - file direct read operation for NFS files 7321da177e4SLinus Torvalds * @iocb: target I/O control block 7331da177e4SLinus Torvalds * @buf: user's buffer into which to read data 73488467055SChuck Lever * @count: number of bytes to read 73588467055SChuck Lever * @pos: byte offset in file where reading starts 7361da177e4SLinus Torvalds * 7371da177e4SLinus Torvalds * We use this function for direct reads instead of calling 7381da177e4SLinus Torvalds * generic_file_aio_read() in order to avoid gfar's check to see if 7391da177e4SLinus Torvalds * the request starts before the end of the file. For that check 7401da177e4SLinus Torvalds * to work, we must generate a GETATTR before each direct read, and 7411da177e4SLinus Torvalds * even then there is a window between the GETATTR and the subsequent 74288467055SChuck Lever * READ where the file size could change. Our preference is simply 7431da177e4SLinus Torvalds * to do all reads the application wants, and the server will take 7441da177e4SLinus Torvalds * care of managing the end of file boundary. 7451da177e4SLinus Torvalds * 7461da177e4SLinus Torvalds * This function also eliminates unnecessarily updating the file's 7471da177e4SLinus Torvalds * atime locally, as the NFS server sets the file's atime, and this 7481da177e4SLinus Torvalds * client must read the updated atime from the server back into its 7491da177e4SLinus Torvalds * cache. 7501da177e4SLinus Torvalds */ 751d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, 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; 7561da177e4SLinus Torvalds 757ce1a8e67SChuck Lever dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", 7580bbacc40SChuck Lever file->f_dentry->d_parent->d_name.name, 7590bbacc40SChuck Lever file->f_dentry->d_name.name, 760ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds if (count < 0) 7631da177e4SLinus Torvalds goto out; 7641da177e4SLinus Torvalds retval = -EFAULT; 7650cdd80d0SChuck Lever if (!access_ok(VERIFY_WRITE, buf, count)) 7661da177e4SLinus Torvalds goto out; 7671da177e4SLinus Torvalds retval = 0; 7681da177e4SLinus Torvalds if (!count) 7691da177e4SLinus Torvalds goto out; 7701da177e4SLinus Torvalds 77129884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 7721da177e4SLinus Torvalds if (retval) 7731da177e4SLinus Torvalds goto out; 7741da177e4SLinus Torvalds 77506cf6f2eSChuck Lever retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos); 7761da177e4SLinus Torvalds if (retval > 0) 7770cdd80d0SChuck Lever iocb->ki_pos = pos + retval; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds out: 7801da177e4SLinus Torvalds return retval; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds /** 7841da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 7851da177e4SLinus Torvalds * @iocb: target I/O control block 7861da177e4SLinus Torvalds * @buf: user's buffer from which to write data 78788467055SChuck Lever * @count: number of bytes to write 78888467055SChuck Lever * @pos: byte offset in file where writing starts 7891da177e4SLinus Torvalds * 7901da177e4SLinus Torvalds * We use this function for direct writes instead of calling 7911da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 7921da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 7931da177e4SLinus Torvalds * the new i_size and this client must read the updated size 7941da177e4SLinus Torvalds * back into its cache. We let the server do generic write 7951da177e4SLinus Torvalds * parameter checking and report problems. 7961da177e4SLinus Torvalds * 7971da177e4SLinus Torvalds * We also avoid an unnecessary invocation of generic_osync_inode(), 7981da177e4SLinus Torvalds * as it is fairly meaningless to sync the metadata of an NFS file. 7991da177e4SLinus Torvalds * 8001da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8011da177e4SLinus Torvalds * 8021da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8031da177e4SLinus Torvalds * readers of this file. 8041da177e4SLinus Torvalds * 8051da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8061da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8071da177e4SLinus Torvalds */ 808d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) 8091da177e4SLinus Torvalds { 810ce1a8e67SChuck Lever ssize_t retval; 8111da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 8121da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 8131da177e4SLinus Torvalds 814ce1a8e67SChuck Lever dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", 8150bbacc40SChuck Lever file->f_dentry->d_parent->d_name.name, 816ce1a8e67SChuck Lever file->f_dentry->d_name.name, 817ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 8181da177e4SLinus Torvalds 819ce1a8e67SChuck Lever retval = generic_write_checks(file, &pos, &count, 0); 820ce1a8e67SChuck Lever if (retval) 8211da177e4SLinus Torvalds goto out; 822ce1a8e67SChuck Lever 823ce1a8e67SChuck Lever retval = -EINVAL; 824ce1a8e67SChuck Lever if ((ssize_t) count < 0) 8251da177e4SLinus Torvalds goto out; 8261da177e4SLinus Torvalds retval = 0; 8271da177e4SLinus Torvalds if (!count) 8281da177e4SLinus Torvalds goto out; 829ce1a8e67SChuck Lever 830ce1a8e67SChuck Lever retval = -EFAULT; 83147989d74SChuck Lever if (!access_ok(VERIFY_READ, buf, count)) 832ce1a8e67SChuck Lever goto out; 8331da177e4SLinus Torvalds 83429884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 8351da177e4SLinus Torvalds if (retval) 8361da177e4SLinus Torvalds goto out; 8371da177e4SLinus Torvalds 83806cf6f2eSChuck Lever retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos); 8399eafa8ccSChuck Lever 8409eafa8ccSChuck Lever /* 8419eafa8ccSChuck Lever * XXX: nfs_end_data_update() already ensures this file's 8429eafa8ccSChuck Lever * cached data is subsequently invalidated. Do we really 8439eafa8ccSChuck Lever * need to call invalidate_inode_pages2() again here? 8449eafa8ccSChuck Lever * 8459eafa8ccSChuck Lever * For aio writes, this invalidation will almost certainly 8469eafa8ccSChuck Lever * occur before the writes complete. Kind of racey. 8479eafa8ccSChuck Lever */ 8481da177e4SLinus Torvalds if (mapping->nrpages) 8491da177e4SLinus Torvalds invalidate_inode_pages2(mapping); 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 */ 879f7b422b1SDavid Howells void __exit nfs_destroy_directcache(void) 8801da177e4SLinus Torvalds { 8811da177e4SLinus Torvalds if (kmem_cache_destroy(nfs_direct_cachep)) 8821da177e4SLinus Torvalds printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); 8831da177e4SLinus Torvalds } 884