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" 5991d5b470SChuck Lever 601da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 611da177e4SLinus Torvalds 62143f412eSTrond Myklebust static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty); 631da177e4SLinus Torvalds static kmem_cache_t *nfs_direct_cachep; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds struct nfs_direct_req { 691da177e4SLinus Torvalds struct kref kref; /* release manager */ 7015ce4a0cSChuck Lever 7115ce4a0cSChuck Lever /* I/O parameters */ 72fad61490STrond Myklebust struct list_head list, /* nfs_read/write_data structs */ 73fad61490STrond Myklebust rewrite_list; /* saved nfs_write_data structs */ 74*a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 7599514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 761da177e4SLinus Torvalds wait_queue_head_t wait; /* wait for i/o completion */ 7788467055SChuck Lever struct inode * inode; /* target file of i/o */ 78fad61490STrond Myklebust unsigned long user_addr; /* location of user's buffer */ 79fad61490STrond Myklebust size_t user_count; /* total bytes to move */ 80fad61490STrond Myklebust loff_t pos; /* starting offset in file */ 811da177e4SLinus Torvalds struct page ** pages; /* pages in our buffer */ 821da177e4SLinus Torvalds unsigned int npages; /* count of pages */ 8315ce4a0cSChuck Lever 8415ce4a0cSChuck Lever /* completion state */ 8515ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 8615ce4a0cSChuck Lever int outstanding; /* i/os we're waiting for */ 8715ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 881da177e4SLinus Torvalds error; /* any reported error */ 89fad61490STrond Myklebust 90fad61490STrond Myklebust /* commit state */ 91fad61490STrond Myklebust struct nfs_write_data * commit_data; /* special write_data for commits */ 92fad61490STrond Myklebust int flags; 93fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 94fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 95fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 961da177e4SLinus Torvalds }; 971da177e4SLinus Torvalds 98fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync); 99fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 100fad61490STrond Myklebust 1011da177e4SLinus Torvalds /** 102b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 103b8a32e2bSChuck Lever * @rw: direction (read or write) 104b8a32e2bSChuck Lever * @iocb: target I/O control block 105b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 106b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 107b8a32e2bSChuck Lever * @nr_segs: size of iovec array 108b8a32e2bSChuck Lever * 109b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 110b8a32e2bSChuck Lever * the NFS client supports direct I/O. However, we shunt off direct 111b8a32e2bSChuck Lever * read and write requests before the VFS gets them, so this method 112b8a32e2bSChuck Lever * should never be called. 113b8a32e2bSChuck Lever */ 114b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) 115b8a32e2bSChuck Lever { 116b8a32e2bSChuck Lever struct dentry *dentry = iocb->ki_filp->f_dentry; 117b8a32e2bSChuck Lever 118b8a32e2bSChuck Lever dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", 119b8a32e2bSChuck Lever dentry->d_name.name, (long long) pos, nr_segs); 120b8a32e2bSChuck Lever 121b8a32e2bSChuck Lever return -EINVAL; 122b8a32e2bSChuck Lever } 123b8a32e2bSChuck Lever 124d4cc948bSChuck Lever static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds int result = -ENOMEM; 1271da177e4SLinus Torvalds unsigned long page_count; 1281da177e4SLinus Torvalds size_t array_size; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT; 1311da177e4SLinus Torvalds page_count -= user_addr >> PAGE_SHIFT; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds array_size = (page_count * sizeof(struct page *)); 1341da177e4SLinus Torvalds *pages = kmalloc(array_size, GFP_KERNEL); 1351da177e4SLinus Torvalds if (*pages) { 1361da177e4SLinus Torvalds down_read(¤t->mm->mmap_sem); 1371da177e4SLinus Torvalds result = get_user_pages(current, current->mm, user_addr, 1381da177e4SLinus Torvalds page_count, (rw == READ), 0, 1391da177e4SLinus Torvalds *pages, NULL); 1401da177e4SLinus Torvalds up_read(¤t->mm->mmap_sem); 141143f412eSTrond Myklebust /* 142143f412eSTrond Myklebust * If we got fewer pages than expected from get_user_pages(), 143143f412eSTrond Myklebust * the user buffer runs off the end of a mapping; return EFAULT. 144143f412eSTrond Myklebust */ 145143f412eSTrond Myklebust if (result >= 0 && result < page_count) { 146143f412eSTrond Myklebust nfs_free_user_pages(*pages, result, 0); 147143f412eSTrond Myklebust *pages = NULL; 148143f412eSTrond Myklebust result = -EFAULT; 149143f412eSTrond Myklebust } 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds return result; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 154d4cc948bSChuck Lever static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds int i; 1571da177e4SLinus Torvalds for (i = 0; i < npages; i++) { 158566dd606STrond Myklebust struct page *page = pages[i]; 159566dd606STrond Myklebust if (do_dirty && !PageCompound(page)) 160566dd606STrond Myklebust set_page_dirty_lock(page); 161566dd606STrond Myklebust page_cache_release(page); 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds kfree(pages); 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 16693619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 16793619e59SChuck Lever { 16893619e59SChuck Lever struct nfs_direct_req *dreq; 16993619e59SChuck Lever 17093619e59SChuck Lever dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL); 17193619e59SChuck Lever if (!dreq) 17293619e59SChuck Lever return NULL; 17393619e59SChuck Lever 17493619e59SChuck Lever kref_init(&dreq->kref); 17593619e59SChuck Lever init_waitqueue_head(&dreq->wait); 17693619e59SChuck Lever INIT_LIST_HEAD(&dreq->list); 177fad61490STrond Myklebust INIT_LIST_HEAD(&dreq->rewrite_list); 17893619e59SChuck Lever dreq->iocb = NULL; 179*a8881f5aSTrond Myklebust dreq->ctx = NULL; 18015ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 18115ce4a0cSChuck Lever dreq->outstanding = 0; 18215ce4a0cSChuck Lever dreq->count = 0; 18315ce4a0cSChuck Lever dreq->error = 0; 184fad61490STrond Myklebust dreq->flags = 0; 18593619e59SChuck Lever 18693619e59SChuck Lever return dreq; 18793619e59SChuck Lever } 18893619e59SChuck Lever 1891da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 192*a8881f5aSTrond Myklebust 193*a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 194*a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 1951da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 198d4cc948bSChuck Lever /* 199bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 200bc0fb201SChuck Lever */ 201bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 202bc0fb201SChuck Lever { 20315ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 204bc0fb201SChuck Lever 205bc0fb201SChuck Lever /* Async requests don't wait here */ 206bc0fb201SChuck Lever if (dreq->iocb) 207bc0fb201SChuck Lever goto out; 208bc0fb201SChuck Lever 20915ce4a0cSChuck Lever result = wait_event_interruptible(dreq->wait, (dreq->outstanding == 0)); 210bc0fb201SChuck Lever 211bc0fb201SChuck Lever if (!result) 21215ce4a0cSChuck Lever result = dreq->error; 213bc0fb201SChuck Lever if (!result) 21415ce4a0cSChuck Lever result = dreq->count; 215bc0fb201SChuck Lever 216bc0fb201SChuck Lever out: 217bc0fb201SChuck Lever kref_put(&dreq->kref, nfs_direct_req_release); 218bc0fb201SChuck Lever return (ssize_t) result; 219bc0fb201SChuck Lever } 220bc0fb201SChuck Lever 221bc0fb201SChuck Lever /* 22263ab46abSChuck Lever * We must hold a reference to all the pages in this direct read request 22363ab46abSChuck Lever * until the RPCs complete. This could be long *after* we are woken up in 22463ab46abSChuck Lever * nfs_direct_wait (for instance, if someone hits ^C on a slow server). 22563ab46abSChuck Lever * 22663ab46abSChuck Lever * In addition, synchronous I/O uses a stack-allocated iocb. Thus we 22763ab46abSChuck Lever * can't trust the iocb is still valid here if this is a synchronous 22863ab46abSChuck Lever * request. If the waiter is woken prematurely, the iocb is long gone. 22963ab46abSChuck Lever */ 23063ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq) 23163ab46abSChuck Lever { 23263ab46abSChuck Lever nfs_free_user_pages(dreq->pages, dreq->npages, 1); 23363ab46abSChuck Lever 23463ab46abSChuck Lever if (dreq->iocb) { 23515ce4a0cSChuck Lever long res = (long) dreq->error; 23663ab46abSChuck Lever if (!res) 23715ce4a0cSChuck Lever res = (long) dreq->count; 23863ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 23963ab46abSChuck Lever } else 24063ab46abSChuck Lever wake_up(&dreq->wait); 24163ab46abSChuck Lever 24263ab46abSChuck Lever kref_put(&dreq->kref, nfs_direct_req_release); 24363ab46abSChuck Lever } 24463ab46abSChuck Lever 24563ab46abSChuck Lever /* 2461da177e4SLinus Torvalds * Note we also set the number of requests we have in the dreq when we are 2471da177e4SLinus Torvalds * done. This prevents races with I/O completion so we will always wait 2481da177e4SLinus Torvalds * until all requests have been dispatched and completed. 2491da177e4SLinus Torvalds */ 2505dd602f2SChuck Lever static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize) 2511da177e4SLinus Torvalds { 2521da177e4SLinus Torvalds struct list_head *list; 2531da177e4SLinus Torvalds struct nfs_direct_req *dreq; 25440859d7eSChuck Lever unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 2551da177e4SLinus Torvalds 25693619e59SChuck Lever dreq = nfs_direct_req_alloc(); 2571da177e4SLinus Torvalds if (!dreq) 2581da177e4SLinus Torvalds return NULL; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds list = &dreq->list; 2611da177e4SLinus Torvalds for(;;) { 26240859d7eSChuck Lever struct nfs_read_data *data = nfs_readdata_alloc(rpages); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds if (unlikely(!data)) { 2651da177e4SLinus Torvalds while (!list_empty(list)) { 2661da177e4SLinus Torvalds data = list_entry(list->next, 2671da177e4SLinus Torvalds struct nfs_read_data, pages); 2681da177e4SLinus Torvalds list_del(&data->pages); 2691da177e4SLinus Torvalds nfs_readdata_free(data); 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds kref_put(&dreq->kref, nfs_direct_req_release); 2721da177e4SLinus Torvalds return NULL; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 2761da177e4SLinus Torvalds list_add(&data->pages, list); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds data->req = (struct nfs_page *) dreq; 27915ce4a0cSChuck Lever dreq->outstanding++; 2801da177e4SLinus Torvalds if (nbytes <= rsize) 2811da177e4SLinus Torvalds break; 2821da177e4SLinus Torvalds nbytes -= rsize; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds kref_get(&dreq->kref); 2851da177e4SLinus Torvalds return dreq; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata) 2891da177e4SLinus Torvalds { 290ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 2911da177e4SLinus Torvalds struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 2921da177e4SLinus Torvalds 293ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 294ec06c096STrond Myklebust return; 2951da177e4SLinus Torvalds 29615ce4a0cSChuck Lever spin_lock(&dreq->lock); 29715ce4a0cSChuck Lever 29815ce4a0cSChuck Lever if (likely(task->tk_status >= 0)) 29915ce4a0cSChuck Lever dreq->count += data->res.count; 30015ce4a0cSChuck Lever else 30115ce4a0cSChuck Lever dreq->error = task->tk_status; 30215ce4a0cSChuck Lever 30315ce4a0cSChuck Lever if (--dreq->outstanding) { 30415ce4a0cSChuck Lever spin_unlock(&dreq->lock); 30515ce4a0cSChuck Lever return; 30615ce4a0cSChuck Lever } 30715ce4a0cSChuck Lever 30815ce4a0cSChuck Lever spin_unlock(&dreq->lock); 30963ab46abSChuck Lever nfs_direct_complete(dreq); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 312ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = { 313ec06c096STrond Myklebust .rpc_call_done = nfs_direct_read_result, 314ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 315ec06c096STrond Myklebust }; 316ec06c096STrond Myklebust 317d4cc948bSChuck Lever /* 3181da177e4SLinus Torvalds * For each nfs_read_data struct that was allocated on the list, dispatch 3191da177e4SLinus Torvalds * an NFS READ operation 3201da177e4SLinus Torvalds */ 321fad61490STrond Myklebust static void nfs_direct_read_schedule(struct nfs_direct_req *dreq) 3221da177e4SLinus Torvalds { 323*a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 324*a8881f5aSTrond Myklebust struct inode *inode = ctx->dentry->d_inode; 3251da177e4SLinus Torvalds struct list_head *list = &dreq->list; 3261da177e4SLinus Torvalds struct page **pages = dreq->pages; 327fad61490STrond Myklebust size_t count = dreq->user_count; 328fad61490STrond Myklebust loff_t pos = dreq->pos; 3295dd602f2SChuck Lever size_t rsize = NFS_SERVER(inode)->rsize; 3301da177e4SLinus Torvalds unsigned int curpage, pgbase; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds curpage = 0; 333fad61490STrond Myklebust pgbase = dreq->user_addr & ~PAGE_MASK; 3341da177e4SLinus Torvalds do { 3351da177e4SLinus Torvalds struct nfs_read_data *data; 3365dd602f2SChuck Lever size_t bytes; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds bytes = rsize; 3391da177e4SLinus Torvalds if (count < rsize) 3401da177e4SLinus Torvalds bytes = count; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds data = list_entry(list->next, struct nfs_read_data, pages); 3431da177e4SLinus Torvalds list_del_init(&data->pages); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds data->inode = inode; 3461da177e4SLinus Torvalds data->cred = ctx->cred; 3471da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 3481da177e4SLinus Torvalds data->args.context = ctx; 34988467055SChuck Lever data->args.offset = pos; 3501da177e4SLinus Torvalds data->args.pgbase = pgbase; 3511da177e4SLinus Torvalds data->args.pages = &pages[curpage]; 3521da177e4SLinus Torvalds data->args.count = bytes; 3531da177e4SLinus Torvalds data->res.fattr = &data->fattr; 3541da177e4SLinus Torvalds data->res.eof = 0; 3551da177e4SLinus Torvalds data->res.count = bytes; 3561da177e4SLinus Torvalds 357ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 358ec06c096STrond Myklebust &nfs_read_direct_ops, data); 3591da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long) inode; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds lock_kernel(); 3641da177e4SLinus Torvalds rpc_execute(&data->task); 3651da177e4SLinus Torvalds unlock_kernel(); 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 3681da177e4SLinus Torvalds data->task.tk_pid, 3691da177e4SLinus Torvalds inode->i_sb->s_id, 3701da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 3711da177e4SLinus Torvalds bytes, 3721da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3731da177e4SLinus Torvalds 37488467055SChuck Lever pos += bytes; 3751da177e4SLinus Torvalds pgbase += bytes; 3761da177e4SLinus Torvalds curpage += pgbase >> PAGE_SHIFT; 3771da177e4SLinus Torvalds pgbase &= ~PAGE_MASK; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds count -= bytes; 3801da177e4SLinus Torvalds } while (count != 0); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 38388467055SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, unsigned int nr_pages) 3841da177e4SLinus Torvalds { 3851da177e4SLinus Torvalds ssize_t result; 3861da177e4SLinus Torvalds sigset_t oldset; 38799514f8fSChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 3881da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(inode); 3891da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize); 3921da177e4SLinus Torvalds if (!dreq) 3931da177e4SLinus Torvalds return -ENOMEM; 3941da177e4SLinus Torvalds 395fad61490STrond Myklebust dreq->user_addr = user_addr; 396fad61490STrond Myklebust dreq->user_count = count; 397fad61490STrond Myklebust dreq->pos = pos; 3981da177e4SLinus Torvalds dreq->pages = pages; 3991da177e4SLinus Torvalds dreq->npages = nr_pages; 40091d5b470SChuck Lever dreq->inode = inode; 401*a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 402487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 403487b8372SChuck Lever dreq->iocb = iocb; 4041da177e4SLinus Torvalds 40591d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); 4061da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 407fad61490STrond Myklebust nfs_direct_read_schedule(dreq); 408bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 4091da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds return result; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 414fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) 415fad61490STrond Myklebust { 416fad61490STrond Myklebust list_splice_init(&dreq->rewrite_list, &dreq->list); 417fad61490STrond Myklebust while (!list_empty(&dreq->list)) { 418fad61490STrond Myklebust struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages); 419fad61490STrond Myklebust list_del(&data->pages); 420fad61490STrond Myklebust nfs_writedata_release(data); 421fad61490STrond Myklebust } 422fad61490STrond Myklebust } 423fad61490STrond Myklebust 424fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 425fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 426fad61490STrond Myklebust { 427fad61490STrond Myklebust struct list_head *pos; 428fad61490STrond Myklebust 429fad61490STrond Myklebust list_splice_init(&dreq->rewrite_list, &dreq->list); 430fad61490STrond Myklebust list_for_each(pos, &dreq->list) 431fad61490STrond Myklebust dreq->outstanding++; 432fad61490STrond Myklebust dreq->count = 0; 433fad61490STrond Myklebust 434fad61490STrond Myklebust nfs_direct_write_schedule(dreq, FLUSH_STABLE); 435fad61490STrond Myklebust } 436fad61490STrond Myklebust 437fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) 438fad61490STrond Myklebust { 439fad61490STrond Myklebust struct nfs_write_data *data = calldata; 440fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 441fad61490STrond Myklebust 442fad61490STrond Myklebust /* Call the NFS version-specific code */ 443fad61490STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 444fad61490STrond Myklebust return; 445fad61490STrond Myklebust if (unlikely(task->tk_status < 0)) { 446fad61490STrond Myklebust dreq->error = task->tk_status; 447fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 448fad61490STrond Myklebust } 449fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 450fad61490STrond Myklebust dprintk("NFS: %5u commit verify failed\n", task->tk_pid); 451fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 452fad61490STrond Myklebust } 453fad61490STrond Myklebust 454fad61490STrond Myklebust dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status); 455fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 456fad61490STrond Myklebust } 457fad61490STrond Myklebust 458fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = { 459fad61490STrond Myklebust .rpc_call_done = nfs_direct_commit_result, 460fad61490STrond Myklebust .rpc_release = nfs_commit_release, 461fad61490STrond Myklebust }; 462fad61490STrond Myklebust 463fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 464fad61490STrond Myklebust { 465fad61490STrond Myklebust struct nfs_write_data *data = dreq->commit_data; 466fad61490STrond Myklebust struct rpc_task *task = &data->task; 467fad61490STrond Myklebust 468fad61490STrond Myklebust data->inode = dreq->inode; 469*a8881f5aSTrond Myklebust data->cred = dreq->ctx->cred; 470fad61490STrond Myklebust 471fad61490STrond Myklebust data->args.fh = NFS_FH(data->inode); 472fad61490STrond Myklebust data->args.offset = dreq->pos; 473fad61490STrond Myklebust data->args.count = dreq->user_count; 474fad61490STrond Myklebust data->res.count = 0; 475fad61490STrond Myklebust data->res.fattr = &data->fattr; 476fad61490STrond Myklebust data->res.verf = &data->verf; 477fad61490STrond Myklebust 478fad61490STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC, 479fad61490STrond Myklebust &nfs_commit_direct_ops, data); 480fad61490STrond Myklebust NFS_PROTO(data->inode)->commit_setup(data, 0); 481fad61490STrond Myklebust 482fad61490STrond Myklebust data->task.tk_priority = RPC_PRIORITY_NORMAL; 483fad61490STrond Myklebust data->task.tk_cookie = (unsigned long)data->inode; 484fad61490STrond Myklebust /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ 485fad61490STrond Myklebust dreq->commit_data = NULL; 486fad61490STrond Myklebust 487fad61490STrond Myklebust dprintk("NFS: %5u initiated commit call\n", task->tk_pid); 488fad61490STrond Myklebust 489fad61490STrond Myklebust lock_kernel(); 490fad61490STrond Myklebust rpc_execute(&data->task); 491fad61490STrond Myklebust unlock_kernel(); 492fad61490STrond Myklebust } 493fad61490STrond Myklebust 494fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 495fad61490STrond Myklebust { 496fad61490STrond Myklebust int flags = dreq->flags; 497fad61490STrond Myklebust 498fad61490STrond Myklebust dreq->flags = 0; 499fad61490STrond Myklebust switch (flags) { 500fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 501fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 502fad61490STrond Myklebust break; 503fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 504fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 505fad61490STrond Myklebust break; 506fad61490STrond Myklebust default: 507fad61490STrond Myklebust nfs_end_data_update(inode); 508fad61490STrond Myklebust if (dreq->commit_data != NULL) 509fad61490STrond Myklebust nfs_commit_free(dreq->commit_data); 510fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 511fad61490STrond Myklebust nfs_direct_complete(dreq); 512fad61490STrond Myklebust } 513fad61490STrond Myklebust } 514fad61490STrond Myklebust 515fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 516fad61490STrond Myklebust { 517fad61490STrond Myklebust dreq->commit_data = nfs_commit_alloc(0); 518fad61490STrond Myklebust if (dreq->commit_data != NULL) 519fad61490STrond Myklebust dreq->commit_data->req = (struct nfs_page *) dreq; 520fad61490STrond Myklebust } 521fad61490STrond Myklebust #else 522fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 523fad61490STrond Myklebust { 524fad61490STrond Myklebust dreq->commit_data = NULL; 525fad61490STrond Myklebust } 526fad61490STrond Myklebust 527fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 528fad61490STrond Myklebust { 529fad61490STrond Myklebust nfs_end_data_update(inode); 530fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 531fad61490STrond Myklebust nfs_direct_complete(dreq); 532fad61490STrond Myklebust } 533fad61490STrond Myklebust #endif 534fad61490STrond Myklebust 535462d5b32SChuck Lever static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize) 5361da177e4SLinus Torvalds { 537462d5b32SChuck Lever struct list_head *list; 538462d5b32SChuck Lever struct nfs_direct_req *dreq; 539462d5b32SChuck Lever unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 5401da177e4SLinus Torvalds 541462d5b32SChuck Lever dreq = nfs_direct_req_alloc(); 542462d5b32SChuck Lever if (!dreq) 543462d5b32SChuck Lever return NULL; 5441da177e4SLinus Torvalds 545462d5b32SChuck Lever list = &dreq->list; 546462d5b32SChuck Lever for(;;) { 547462d5b32SChuck Lever struct nfs_write_data *data = nfs_writedata_alloc(wpages); 5481da177e4SLinus Torvalds 549462d5b32SChuck Lever if (unlikely(!data)) { 550462d5b32SChuck Lever while (!list_empty(list)) { 551462d5b32SChuck Lever data = list_entry(list->next, 552462d5b32SChuck Lever struct nfs_write_data, pages); 553462d5b32SChuck Lever list_del(&data->pages); 554462d5b32SChuck Lever nfs_writedata_free(data); 555462d5b32SChuck Lever } 556462d5b32SChuck Lever kref_put(&dreq->kref, nfs_direct_req_release); 557462d5b32SChuck Lever return NULL; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 560462d5b32SChuck Lever INIT_LIST_HEAD(&data->pages); 561462d5b32SChuck Lever list_add(&data->pages, list); 5621da177e4SLinus Torvalds 563462d5b32SChuck Lever data->req = (struct nfs_page *) dreq; 56415ce4a0cSChuck Lever dreq->outstanding++; 565462d5b32SChuck Lever if (nbytes <= wsize) 5661da177e4SLinus Torvalds break; 567462d5b32SChuck Lever nbytes -= wsize; 568462d5b32SChuck Lever } 569fad61490STrond Myklebust 570fad61490STrond Myklebust nfs_alloc_commit_data(dreq); 571fad61490STrond Myklebust 572462d5b32SChuck Lever kref_get(&dreq->kref); 573462d5b32SChuck Lever return dreq; 574462d5b32SChuck Lever } 5751da177e4SLinus Torvalds 576462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata) 577462d5b32SChuck Lever { 578462d5b32SChuck Lever struct nfs_write_data *data = calldata; 579462d5b32SChuck Lever struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 580462d5b32SChuck Lever int status = task->tk_status; 581462d5b32SChuck Lever 582462d5b32SChuck Lever if (nfs_writeback_done(task, data) != 0) 583462d5b32SChuck Lever return; 584462d5b32SChuck Lever 58515ce4a0cSChuck Lever spin_lock(&dreq->lock); 586462d5b32SChuck Lever 58715ce4a0cSChuck Lever if (likely(status >= 0)) 58815ce4a0cSChuck Lever dreq->count += data->res.count; 58915ce4a0cSChuck Lever else 590fad61490STrond Myklebust dreq->error = task->tk_status; 59115ce4a0cSChuck Lever 592fad61490STrond Myklebust if (data->res.verf->committed != NFS_FILE_SYNC) { 593fad61490STrond Myklebust switch (dreq->flags) { 594fad61490STrond Myklebust case 0: 595fad61490STrond Myklebust memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); 596fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_DO_COMMIT; 597fad61490STrond Myklebust break; 598fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 599fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { 600fad61490STrond Myklebust dprintk("NFS: %5u write verify failed\n", task->tk_pid); 601fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 602fad61490STrond Myklebust } 603fad61490STrond Myklebust } 604fad61490STrond Myklebust } 605fad61490STrond Myklebust /* In case we have to resend */ 606fad61490STrond Myklebust data->args.stable = NFS_FILE_SYNC; 607fad61490STrond Myklebust 608fad61490STrond Myklebust spin_unlock(&dreq->lock); 609fad61490STrond Myklebust } 610fad61490STrond Myklebust 611fad61490STrond Myklebust /* 612fad61490STrond Myklebust * NB: Return the value of the first error return code. Subsequent 613fad61490STrond Myklebust * errors after the first one are ignored. 614fad61490STrond Myklebust */ 615fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata) 616fad61490STrond Myklebust { 617fad61490STrond Myklebust struct nfs_write_data *data = calldata; 618fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 619fad61490STrond Myklebust 620fad61490STrond Myklebust spin_lock(&dreq->lock); 62115ce4a0cSChuck Lever if (--dreq->outstanding) { 62215ce4a0cSChuck Lever spin_unlock(&dreq->lock); 62315ce4a0cSChuck Lever return; 62415ce4a0cSChuck Lever } 62515ce4a0cSChuck Lever spin_unlock(&dreq->lock); 62615ce4a0cSChuck Lever 627fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 628462d5b32SChuck Lever } 629462d5b32SChuck Lever 630462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = { 631462d5b32SChuck Lever .rpc_call_done = nfs_direct_write_result, 632fad61490STrond Myklebust .rpc_release = nfs_direct_write_release, 633462d5b32SChuck Lever }; 634462d5b32SChuck Lever 635462d5b32SChuck Lever /* 636462d5b32SChuck Lever * For each nfs_write_data struct that was allocated on the list, dispatch 637462d5b32SChuck Lever * an NFS WRITE operation 638462d5b32SChuck Lever */ 639fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync) 640462d5b32SChuck Lever { 641*a8881f5aSTrond Myklebust struct nfs_open_context *ctx = dreq->ctx; 642*a8881f5aSTrond Myklebust struct inode *inode = ctx->dentry->d_inode; 643462d5b32SChuck Lever struct list_head *list = &dreq->list; 644462d5b32SChuck Lever struct page **pages = dreq->pages; 645fad61490STrond Myklebust size_t count = dreq->user_count; 646fad61490STrond Myklebust loff_t pos = dreq->pos; 647462d5b32SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 648462d5b32SChuck Lever unsigned int curpage, pgbase; 649462d5b32SChuck Lever 650462d5b32SChuck Lever curpage = 0; 651fad61490STrond Myklebust pgbase = dreq->user_addr & ~PAGE_MASK; 652462d5b32SChuck Lever do { 653462d5b32SChuck Lever struct nfs_write_data *data; 654462d5b32SChuck Lever size_t bytes; 655462d5b32SChuck Lever 656462d5b32SChuck Lever bytes = wsize; 657462d5b32SChuck Lever if (count < wsize) 658462d5b32SChuck Lever bytes = count; 659462d5b32SChuck Lever 660462d5b32SChuck Lever data = list_entry(list->next, struct nfs_write_data, pages); 661fad61490STrond Myklebust list_move_tail(&data->pages, &dreq->rewrite_list); 662462d5b32SChuck Lever 663462d5b32SChuck Lever data->inode = inode; 664462d5b32SChuck Lever data->cred = ctx->cred; 665462d5b32SChuck Lever data->args.fh = NFS_FH(inode); 666462d5b32SChuck Lever data->args.context = ctx; 66788467055SChuck Lever data->args.offset = pos; 668462d5b32SChuck Lever data->args.pgbase = pgbase; 669462d5b32SChuck Lever data->args.pages = &pages[curpage]; 670462d5b32SChuck Lever data->args.count = bytes; 671462d5b32SChuck Lever data->res.fattr = &data->fattr; 672462d5b32SChuck Lever data->res.count = bytes; 67347989d74SChuck Lever data->res.verf = &data->verf; 674462d5b32SChuck Lever 675462d5b32SChuck Lever rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 676462d5b32SChuck Lever &nfs_write_direct_ops, data); 677fad61490STrond Myklebust NFS_PROTO(inode)->write_setup(data, sync); 678462d5b32SChuck Lever 679462d5b32SChuck Lever data->task.tk_priority = RPC_PRIORITY_NORMAL; 680462d5b32SChuck Lever data->task.tk_cookie = (unsigned long) inode; 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds lock_kernel(); 683462d5b32SChuck Lever rpc_execute(&data->task); 6841da177e4SLinus Torvalds unlock_kernel(); 6851da177e4SLinus Torvalds 686462d5b32SChuck Lever dfprintk(VFS, "NFS: %4d initiated direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 687462d5b32SChuck Lever data->task.tk_pid, 688462d5b32SChuck Lever inode->i_sb->s_id, 689462d5b32SChuck Lever (long long)NFS_FILEID(inode), 690462d5b32SChuck Lever bytes, 691462d5b32SChuck Lever (unsigned long long)data->args.offset); 692462d5b32SChuck Lever 69388467055SChuck Lever pos += bytes; 694462d5b32SChuck Lever pgbase += bytes; 695462d5b32SChuck Lever curpage += pgbase >> PAGE_SHIFT; 696462d5b32SChuck Lever pgbase &= ~PAGE_MASK; 697462d5b32SChuck Lever 698462d5b32SChuck Lever count -= bytes; 699462d5b32SChuck Lever } while (count != 0); 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 70288467055SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, int nr_pages) 703462d5b32SChuck Lever { 704462d5b32SChuck Lever ssize_t result; 705462d5b32SChuck Lever sigset_t oldset; 706c89f2ee5SChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 707462d5b32SChuck Lever struct rpc_clnt *clnt = NFS_CLIENT(inode); 708462d5b32SChuck Lever struct nfs_direct_req *dreq; 709fad61490STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize; 710fad61490STrond Myklebust int sync = 0; 711462d5b32SChuck Lever 712fad61490STrond Myklebust dreq = nfs_direct_write_alloc(count, wsize); 713462d5b32SChuck Lever if (!dreq) 714462d5b32SChuck Lever return -ENOMEM; 715fad61490STrond Myklebust if (dreq->commit_data == NULL || count < wsize) 716fad61490STrond Myklebust sync = FLUSH_STABLE; 717462d5b32SChuck Lever 718fad61490STrond Myklebust dreq->user_addr = user_addr; 719fad61490STrond Myklebust dreq->user_count = count; 720fad61490STrond Myklebust dreq->pos = pos; 721462d5b32SChuck Lever dreq->pages = pages; 722462d5b32SChuck Lever dreq->npages = nr_pages; 723c89f2ee5SChuck Lever dreq->inode = inode; 724*a8881f5aSTrond Myklebust dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); 725c89f2ee5SChuck Lever if (!is_sync_kiocb(iocb)) 726c89f2ee5SChuck Lever dreq->iocb = iocb; 727462d5b32SChuck Lever 72847989d74SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count); 72947989d74SChuck Lever 730462d5b32SChuck Lever nfs_begin_data_update(inode); 731462d5b32SChuck Lever 732462d5b32SChuck Lever rpc_clnt_sigmask(clnt, &oldset); 733fad61490STrond Myklebust nfs_direct_write_schedule(dreq, sync); 734c89f2ee5SChuck Lever result = nfs_direct_wait(dreq); 735462d5b32SChuck Lever rpc_clnt_sigunmask(clnt, &oldset); 736462d5b32SChuck Lever 737462d5b32SChuck Lever return result; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds /** 7411da177e4SLinus Torvalds * nfs_file_direct_read - file direct read operation for NFS files 7421da177e4SLinus Torvalds * @iocb: target I/O control block 7431da177e4SLinus Torvalds * @buf: user's buffer into which to read data 74488467055SChuck Lever * @count: number of bytes to read 74588467055SChuck Lever * @pos: byte offset in file where reading starts 7461da177e4SLinus Torvalds * 7471da177e4SLinus Torvalds * We use this function for direct reads instead of calling 7481da177e4SLinus Torvalds * generic_file_aio_read() in order to avoid gfar's check to see if 7491da177e4SLinus Torvalds * the request starts before the end of the file. For that check 7501da177e4SLinus Torvalds * to work, we must generate a GETATTR before each direct read, and 7511da177e4SLinus Torvalds * even then there is a window between the GETATTR and the subsequent 75288467055SChuck Lever * READ where the file size could change. Our preference is simply 7531da177e4SLinus Torvalds * to do all reads the application wants, and the server will take 7541da177e4SLinus Torvalds * care of managing the end of file boundary. 7551da177e4SLinus Torvalds * 7561da177e4SLinus Torvalds * This function also eliminates unnecessarily updating the file's 7571da177e4SLinus Torvalds * atime locally, as the NFS server sets the file's atime, and this 7581da177e4SLinus Torvalds * client must read the updated atime from the server back into its 7591da177e4SLinus Torvalds * cache. 7601da177e4SLinus Torvalds */ 761d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds ssize_t retval = -EINVAL; 7640cdd80d0SChuck Lever int page_count; 7650cdd80d0SChuck Lever struct page **pages; 7661da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 7671da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 7681da177e4SLinus Torvalds 769ce1a8e67SChuck Lever dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", 7700bbacc40SChuck Lever file->f_dentry->d_parent->d_name.name, 7710bbacc40SChuck Lever file->f_dentry->d_name.name, 772ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds if (count < 0) 7751da177e4SLinus Torvalds goto out; 7761da177e4SLinus Torvalds retval = -EFAULT; 7770cdd80d0SChuck Lever if (!access_ok(VERIFY_WRITE, buf, count)) 7781da177e4SLinus Torvalds goto out; 7791da177e4SLinus Torvalds retval = 0; 7801da177e4SLinus Torvalds if (!count) 7811da177e4SLinus Torvalds goto out; 7821da177e4SLinus Torvalds 78329884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 7841da177e4SLinus Torvalds if (retval) 7851da177e4SLinus Torvalds goto out; 7861da177e4SLinus Torvalds 7870cdd80d0SChuck Lever page_count = nfs_get_user_pages(READ, (unsigned long) buf, 7880cdd80d0SChuck Lever count, &pages); 7890cdd80d0SChuck Lever if (page_count < 0) { 7900cdd80d0SChuck Lever nfs_free_user_pages(pages, 0, 0); 7910cdd80d0SChuck Lever retval = page_count; 7920cdd80d0SChuck Lever goto out; 7930cdd80d0SChuck Lever } 7940cdd80d0SChuck Lever 79599514f8fSChuck Lever retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos, 7960cdd80d0SChuck Lever pages, page_count); 7971da177e4SLinus Torvalds if (retval > 0) 7980cdd80d0SChuck Lever iocb->ki_pos = pos + retval; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds out: 8011da177e4SLinus Torvalds return retval; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds /** 8051da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 8061da177e4SLinus Torvalds * @iocb: target I/O control block 8071da177e4SLinus Torvalds * @buf: user's buffer from which to write data 80888467055SChuck Lever * @count: number of bytes to write 80988467055SChuck Lever * @pos: byte offset in file where writing starts 8101da177e4SLinus Torvalds * 8111da177e4SLinus Torvalds * We use this function for direct writes instead of calling 8121da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 8131da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 8141da177e4SLinus Torvalds * the new i_size and this client must read the updated size 8151da177e4SLinus Torvalds * back into its cache. We let the server do generic write 8161da177e4SLinus Torvalds * parameter checking and report problems. 8171da177e4SLinus Torvalds * 8181da177e4SLinus Torvalds * We also avoid an unnecessary invocation of generic_osync_inode(), 8191da177e4SLinus Torvalds * as it is fairly meaningless to sync the metadata of an NFS file. 8201da177e4SLinus Torvalds * 8211da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8221da177e4SLinus Torvalds * 8231da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8241da177e4SLinus Torvalds * readers of this file. 8251da177e4SLinus Torvalds * 8261da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8271da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8281da177e4SLinus Torvalds */ 829d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) 8301da177e4SLinus Torvalds { 831ce1a8e67SChuck Lever ssize_t retval; 83247989d74SChuck Lever int page_count; 83347989d74SChuck Lever struct page **pages; 8341da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 8351da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 8361da177e4SLinus Torvalds 837ce1a8e67SChuck Lever dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", 8380bbacc40SChuck Lever file->f_dentry->d_parent->d_name.name, 839ce1a8e67SChuck Lever file->f_dentry->d_name.name, 840ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 8411da177e4SLinus Torvalds 842ce1a8e67SChuck Lever retval = generic_write_checks(file, &pos, &count, 0); 843ce1a8e67SChuck Lever if (retval) 8441da177e4SLinus Torvalds goto out; 845ce1a8e67SChuck Lever 846ce1a8e67SChuck Lever retval = -EINVAL; 847ce1a8e67SChuck Lever if ((ssize_t) count < 0) 8481da177e4SLinus Torvalds goto out; 8491da177e4SLinus Torvalds retval = 0; 8501da177e4SLinus Torvalds if (!count) 8511da177e4SLinus Torvalds goto out; 852ce1a8e67SChuck Lever 853ce1a8e67SChuck Lever retval = -EFAULT; 85447989d74SChuck Lever if (!access_ok(VERIFY_READ, buf, count)) 855ce1a8e67SChuck Lever goto out; 8561da177e4SLinus Torvalds 85729884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 8581da177e4SLinus Torvalds if (retval) 8591da177e4SLinus Torvalds goto out; 8601da177e4SLinus Torvalds 86147989d74SChuck Lever page_count = nfs_get_user_pages(WRITE, (unsigned long) buf, 86247989d74SChuck Lever count, &pages); 86347989d74SChuck Lever if (page_count < 0) { 86447989d74SChuck Lever nfs_free_user_pages(pages, 0, 0); 86547989d74SChuck Lever retval = page_count; 86647989d74SChuck Lever goto out; 86747989d74SChuck Lever } 86847989d74SChuck Lever 869c89f2ee5SChuck Lever retval = nfs_direct_write(iocb, (unsigned long) buf, count, 87047989d74SChuck Lever pos, pages, page_count); 8719eafa8ccSChuck Lever 8729eafa8ccSChuck Lever /* 8739eafa8ccSChuck Lever * XXX: nfs_end_data_update() already ensures this file's 8749eafa8ccSChuck Lever * cached data is subsequently invalidated. Do we really 8759eafa8ccSChuck Lever * need to call invalidate_inode_pages2() again here? 8769eafa8ccSChuck Lever * 8779eafa8ccSChuck Lever * For aio writes, this invalidation will almost certainly 8789eafa8ccSChuck Lever * occur before the writes complete. Kind of racey. 8799eafa8ccSChuck Lever */ 8801da177e4SLinus Torvalds if (mapping->nrpages) 8811da177e4SLinus Torvalds invalidate_inode_pages2(mapping); 8829eafa8ccSChuck Lever 8831da177e4SLinus Torvalds if (retval > 0) 884ce1a8e67SChuck Lever iocb->ki_pos = pos + retval; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds out: 8871da177e4SLinus Torvalds return retval; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 89088467055SChuck Lever /** 89188467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 89288467055SChuck Lever * 89388467055SChuck Lever */ 8941da177e4SLinus Torvalds int nfs_init_directcache(void) 8951da177e4SLinus Torvalds { 8961da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 8971da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 8981da177e4SLinus Torvalds 0, SLAB_RECLAIM_ACCOUNT, 8991da177e4SLinus Torvalds NULL, NULL); 9001da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 9011da177e4SLinus Torvalds return -ENOMEM; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds return 0; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 90688467055SChuck Lever /** 90788467055SChuck Lever * nfs_init_directcache - destroy the slab cache for nfs_direct_req structures 90888467055SChuck Lever * 90988467055SChuck Lever */ 9101da177e4SLinus Torvalds void nfs_destroy_directcache(void) 9111da177e4SLinus Torvalds { 9121da177e4SLinus Torvalds if (kmem_cache_destroy(nfs_direct_cachep)) 9131da177e4SLinus Torvalds printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); 9141da177e4SLinus Torvalds } 915