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 */ 72*fad61490STrond Myklebust struct list_head list, /* nfs_read/write_data structs */ 73*fad61490STrond Myklebust rewrite_list; /* saved nfs_write_data structs */ 7499514f8fSChuck Lever struct file * filp; /* file descriptor */ 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 */ 78*fad61490STrond Myklebust unsigned long user_addr; /* location of user's buffer */ 79*fad61490STrond Myklebust size_t user_count; /* total bytes to move */ 80*fad61490STrond 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 */ 89*fad61490STrond Myklebust 90*fad61490STrond Myklebust /* commit state */ 91*fad61490STrond Myklebust struct nfs_write_data * commit_data; /* special write_data for commits */ 92*fad61490STrond Myklebust int flags; 93*fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 94*fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 95*fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 961da177e4SLinus Torvalds }; 971da177e4SLinus Torvalds 98*fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync); 99*fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 100*fad61490STrond 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); 177*fad61490STrond Myklebust INIT_LIST_HEAD(&dreq->rewrite_list); 17893619e59SChuck Lever dreq->iocb = NULL; 17915ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 18015ce4a0cSChuck Lever dreq->outstanding = 0; 18115ce4a0cSChuck Lever dreq->count = 0; 18215ce4a0cSChuck Lever dreq->error = 0; 183*fad61490STrond Myklebust dreq->flags = 0; 18493619e59SChuck Lever 18593619e59SChuck Lever return dreq; 18693619e59SChuck Lever } 18793619e59SChuck Lever 1881da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 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 20515ce4a0cSChuck Lever result = wait_event_interruptible(dreq->wait, (dreq->outstanding == 0)); 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 /* 21863ab46abSChuck Lever * We must hold a reference to all the pages in this direct read request 21963ab46abSChuck Lever * until the RPCs complete. This could be long *after* we are woken up in 22063ab46abSChuck Lever * nfs_direct_wait (for instance, if someone hits ^C on a slow server). 22163ab46abSChuck Lever * 22263ab46abSChuck Lever * In addition, synchronous I/O uses a stack-allocated iocb. Thus we 22363ab46abSChuck Lever * can't trust the iocb is still valid here if this is a synchronous 22463ab46abSChuck Lever * request. If the waiter is woken prematurely, the iocb is long gone. 22563ab46abSChuck Lever */ 22663ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq) 22763ab46abSChuck Lever { 22863ab46abSChuck Lever nfs_free_user_pages(dreq->pages, dreq->npages, 1); 22963ab46abSChuck Lever 23063ab46abSChuck Lever if (dreq->iocb) { 23115ce4a0cSChuck Lever long res = (long) dreq->error; 23263ab46abSChuck Lever if (!res) 23315ce4a0cSChuck Lever res = (long) dreq->count; 23463ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 23563ab46abSChuck Lever } else 23663ab46abSChuck Lever wake_up(&dreq->wait); 23763ab46abSChuck Lever 238a37ec012SChuck Lever iput(dreq->inode); 23963ab46abSChuck Lever kref_put(&dreq->kref, nfs_direct_req_release); 24063ab46abSChuck Lever } 24163ab46abSChuck Lever 24263ab46abSChuck Lever /* 2431da177e4SLinus Torvalds * Note we also set the number of requests we have in the dreq when we are 2441da177e4SLinus Torvalds * done. This prevents races with I/O completion so we will always wait 2451da177e4SLinus Torvalds * until all requests have been dispatched and completed. 2461da177e4SLinus Torvalds */ 2475dd602f2SChuck Lever static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds struct list_head *list; 2501da177e4SLinus Torvalds struct nfs_direct_req *dreq; 25140859d7eSChuck Lever unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 2521da177e4SLinus Torvalds 25393619e59SChuck Lever dreq = nfs_direct_req_alloc(); 2541da177e4SLinus Torvalds if (!dreq) 2551da177e4SLinus Torvalds return NULL; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds list = &dreq->list; 2581da177e4SLinus Torvalds for(;;) { 25940859d7eSChuck Lever struct nfs_read_data *data = nfs_readdata_alloc(rpages); 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds if (unlikely(!data)) { 2621da177e4SLinus Torvalds while (!list_empty(list)) { 2631da177e4SLinus Torvalds data = list_entry(list->next, 2641da177e4SLinus Torvalds struct nfs_read_data, pages); 2651da177e4SLinus Torvalds list_del(&data->pages); 2661da177e4SLinus Torvalds nfs_readdata_free(data); 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds kref_put(&dreq->kref, nfs_direct_req_release); 2691da177e4SLinus Torvalds return NULL; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 2731da177e4SLinus Torvalds list_add(&data->pages, list); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds data->req = (struct nfs_page *) dreq; 27615ce4a0cSChuck Lever dreq->outstanding++; 2771da177e4SLinus Torvalds if (nbytes <= rsize) 2781da177e4SLinus Torvalds break; 2791da177e4SLinus Torvalds nbytes -= rsize; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds kref_get(&dreq->kref); 2821da177e4SLinus Torvalds return dreq; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 285ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata) 2861da177e4SLinus Torvalds { 287ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 2881da177e4SLinus Torvalds struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 2891da177e4SLinus Torvalds 290ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 291ec06c096STrond Myklebust return; 2921da177e4SLinus Torvalds 29315ce4a0cSChuck Lever spin_lock(&dreq->lock); 29415ce4a0cSChuck Lever 29515ce4a0cSChuck Lever if (likely(task->tk_status >= 0)) 29615ce4a0cSChuck Lever dreq->count += data->res.count; 29715ce4a0cSChuck Lever else 29815ce4a0cSChuck Lever dreq->error = task->tk_status; 29915ce4a0cSChuck Lever 30015ce4a0cSChuck Lever if (--dreq->outstanding) { 30115ce4a0cSChuck Lever spin_unlock(&dreq->lock); 30215ce4a0cSChuck Lever return; 30315ce4a0cSChuck Lever } 30415ce4a0cSChuck Lever 30515ce4a0cSChuck Lever spin_unlock(&dreq->lock); 30663ab46abSChuck Lever nfs_direct_complete(dreq); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 309ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = { 310ec06c096STrond Myklebust .rpc_call_done = nfs_direct_read_result, 311ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 312ec06c096STrond Myklebust }; 313ec06c096STrond Myklebust 314d4cc948bSChuck Lever /* 3151da177e4SLinus Torvalds * For each nfs_read_data struct that was allocated on the list, dispatch 3161da177e4SLinus Torvalds * an NFS READ operation 3171da177e4SLinus Torvalds */ 318*fad61490STrond Myklebust static void nfs_direct_read_schedule(struct nfs_direct_req *dreq) 3191da177e4SLinus Torvalds { 32099514f8fSChuck Lever struct file *file = dreq->filp; 32199514f8fSChuck Lever struct inode *inode = file->f_mapping->host; 32299514f8fSChuck Lever struct nfs_open_context *ctx = (struct nfs_open_context *) 32399514f8fSChuck Lever file->private_data; 3241da177e4SLinus Torvalds struct list_head *list = &dreq->list; 3251da177e4SLinus Torvalds struct page **pages = dreq->pages; 326*fad61490STrond Myklebust size_t count = dreq->user_count; 327*fad61490STrond Myklebust loff_t pos = dreq->pos; 3285dd602f2SChuck Lever size_t rsize = NFS_SERVER(inode)->rsize; 3291da177e4SLinus Torvalds unsigned int curpage, pgbase; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds curpage = 0; 332*fad61490STrond Myklebust pgbase = dreq->user_addr & ~PAGE_MASK; 3331da177e4SLinus Torvalds do { 3341da177e4SLinus Torvalds struct nfs_read_data *data; 3355dd602f2SChuck Lever size_t bytes; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds bytes = rsize; 3381da177e4SLinus Torvalds if (count < rsize) 3391da177e4SLinus Torvalds bytes = count; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds data = list_entry(list->next, struct nfs_read_data, pages); 3421da177e4SLinus Torvalds list_del_init(&data->pages); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds data->inode = inode; 3451da177e4SLinus Torvalds data->cred = ctx->cred; 3461da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 3471da177e4SLinus Torvalds data->args.context = ctx; 34888467055SChuck Lever data->args.offset = pos; 3491da177e4SLinus Torvalds data->args.pgbase = pgbase; 3501da177e4SLinus Torvalds data->args.pages = &pages[curpage]; 3511da177e4SLinus Torvalds data->args.count = bytes; 3521da177e4SLinus Torvalds data->res.fattr = &data->fattr; 3531da177e4SLinus Torvalds data->res.eof = 0; 3541da177e4SLinus Torvalds data->res.count = bytes; 3551da177e4SLinus Torvalds 356ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 357ec06c096STrond Myklebust &nfs_read_direct_ops, data); 3581da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long) inode; 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds lock_kernel(); 3631da177e4SLinus Torvalds rpc_execute(&data->task); 3641da177e4SLinus Torvalds unlock_kernel(); 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 3671da177e4SLinus Torvalds data->task.tk_pid, 3681da177e4SLinus Torvalds inode->i_sb->s_id, 3691da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 3701da177e4SLinus Torvalds bytes, 3711da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3721da177e4SLinus Torvalds 37388467055SChuck Lever pos += bytes; 3741da177e4SLinus Torvalds pgbase += bytes; 3751da177e4SLinus Torvalds curpage += pgbase >> PAGE_SHIFT; 3761da177e4SLinus Torvalds pgbase &= ~PAGE_MASK; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds count -= bytes; 3791da177e4SLinus Torvalds } while (count != 0); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 38288467055SChuck 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) 3831da177e4SLinus Torvalds { 3841da177e4SLinus Torvalds ssize_t result; 3851da177e4SLinus Torvalds sigset_t oldset; 38699514f8fSChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 3871da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(inode); 3881da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize); 3911da177e4SLinus Torvalds if (!dreq) 3921da177e4SLinus Torvalds return -ENOMEM; 3931da177e4SLinus Torvalds 394*fad61490STrond Myklebust dreq->user_addr = user_addr; 395*fad61490STrond Myklebust dreq->user_count = count; 396*fad61490STrond Myklebust dreq->pos = pos; 3971da177e4SLinus Torvalds dreq->pages = pages; 3981da177e4SLinus Torvalds dreq->npages = nr_pages; 399a37ec012SChuck Lever igrab(inode); 40091d5b470SChuck Lever dreq->inode = inode; 40199514f8fSChuck Lever dreq->filp = iocb->ki_filp; 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); 407*fad61490STrond 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 414*fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) 415*fad61490STrond Myklebust { 416*fad61490STrond Myklebust list_splice_init(&dreq->rewrite_list, &dreq->list); 417*fad61490STrond Myklebust while (!list_empty(&dreq->list)) { 418*fad61490STrond Myklebust struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages); 419*fad61490STrond Myklebust list_del(&data->pages); 420*fad61490STrond Myklebust nfs_writedata_release(data); 421*fad61490STrond Myklebust } 422*fad61490STrond Myklebust } 423*fad61490STrond Myklebust 424*fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 425*fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 426*fad61490STrond Myklebust { 427*fad61490STrond Myklebust struct list_head *pos; 428*fad61490STrond Myklebust 429*fad61490STrond Myklebust list_splice_init(&dreq->rewrite_list, &dreq->list); 430*fad61490STrond Myklebust list_for_each(pos, &dreq->list) 431*fad61490STrond Myklebust dreq->outstanding++; 432*fad61490STrond Myklebust dreq->count = 0; 433*fad61490STrond Myklebust 434*fad61490STrond Myklebust nfs_direct_write_schedule(dreq, FLUSH_STABLE); 435*fad61490STrond Myklebust } 436*fad61490STrond Myklebust 437*fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) 438*fad61490STrond Myklebust { 439*fad61490STrond Myklebust struct nfs_write_data *data = calldata; 440*fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 441*fad61490STrond Myklebust 442*fad61490STrond Myklebust /* Call the NFS version-specific code */ 443*fad61490STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 444*fad61490STrond Myklebust return; 445*fad61490STrond Myklebust if (unlikely(task->tk_status < 0)) { 446*fad61490STrond Myklebust dreq->error = task->tk_status; 447*fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 448*fad61490STrond Myklebust } 449*fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { 450*fad61490STrond Myklebust dprintk("NFS: %5u commit verify failed\n", task->tk_pid); 451*fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 452*fad61490STrond Myklebust } 453*fad61490STrond Myklebust 454*fad61490STrond Myklebust dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status); 455*fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 456*fad61490STrond Myklebust } 457*fad61490STrond Myklebust 458*fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = { 459*fad61490STrond Myklebust .rpc_call_done = nfs_direct_commit_result, 460*fad61490STrond Myklebust .rpc_release = nfs_commit_release, 461*fad61490STrond Myklebust }; 462*fad61490STrond Myklebust 463*fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 464*fad61490STrond Myklebust { 465*fad61490STrond Myklebust struct file *file = dreq->filp; 466*fad61490STrond Myklebust struct nfs_open_context *ctx = (struct nfs_open_context *) 467*fad61490STrond Myklebust file->private_data; 468*fad61490STrond Myklebust struct nfs_write_data *data = dreq->commit_data; 469*fad61490STrond Myklebust struct rpc_task *task = &data->task; 470*fad61490STrond Myklebust 471*fad61490STrond Myklebust data->inode = dreq->inode; 472*fad61490STrond Myklebust data->cred = ctx->cred; 473*fad61490STrond Myklebust 474*fad61490STrond Myklebust data->args.fh = NFS_FH(data->inode); 475*fad61490STrond Myklebust data->args.offset = dreq->pos; 476*fad61490STrond Myklebust data->args.count = dreq->user_count; 477*fad61490STrond Myklebust data->res.count = 0; 478*fad61490STrond Myklebust data->res.fattr = &data->fattr; 479*fad61490STrond Myklebust data->res.verf = &data->verf; 480*fad61490STrond Myklebust 481*fad61490STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC, 482*fad61490STrond Myklebust &nfs_commit_direct_ops, data); 483*fad61490STrond Myklebust NFS_PROTO(data->inode)->commit_setup(data, 0); 484*fad61490STrond Myklebust 485*fad61490STrond Myklebust data->task.tk_priority = RPC_PRIORITY_NORMAL; 486*fad61490STrond Myklebust data->task.tk_cookie = (unsigned long)data->inode; 487*fad61490STrond Myklebust /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ 488*fad61490STrond Myklebust dreq->commit_data = NULL; 489*fad61490STrond Myklebust 490*fad61490STrond Myklebust dprintk("NFS: %5u initiated commit call\n", task->tk_pid); 491*fad61490STrond Myklebust 492*fad61490STrond Myklebust lock_kernel(); 493*fad61490STrond Myklebust rpc_execute(&data->task); 494*fad61490STrond Myklebust unlock_kernel(); 495*fad61490STrond Myklebust } 496*fad61490STrond Myklebust 497*fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 498*fad61490STrond Myklebust { 499*fad61490STrond Myklebust int flags = dreq->flags; 500*fad61490STrond Myklebust 501*fad61490STrond Myklebust dreq->flags = 0; 502*fad61490STrond Myklebust switch (flags) { 503*fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 504*fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 505*fad61490STrond Myklebust break; 506*fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 507*fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 508*fad61490STrond Myklebust break; 509*fad61490STrond Myklebust default: 510*fad61490STrond Myklebust nfs_end_data_update(inode); 511*fad61490STrond Myklebust if (dreq->commit_data != NULL) 512*fad61490STrond Myklebust nfs_commit_free(dreq->commit_data); 513*fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 514*fad61490STrond Myklebust nfs_direct_complete(dreq); 515*fad61490STrond Myklebust } 516*fad61490STrond Myklebust } 517*fad61490STrond Myklebust 518*fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 519*fad61490STrond Myklebust { 520*fad61490STrond Myklebust dreq->commit_data = nfs_commit_alloc(0); 521*fad61490STrond Myklebust if (dreq->commit_data != NULL) 522*fad61490STrond Myklebust dreq->commit_data->req = (struct nfs_page *) dreq; 523*fad61490STrond Myklebust } 524*fad61490STrond Myklebust #else 525*fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) 526*fad61490STrond Myklebust { 527*fad61490STrond Myklebust dreq->commit_data = NULL; 528*fad61490STrond Myklebust } 529*fad61490STrond Myklebust 530*fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 531*fad61490STrond Myklebust { 532*fad61490STrond Myklebust nfs_end_data_update(inode); 533*fad61490STrond Myklebust nfs_direct_free_writedata(dreq); 534*fad61490STrond Myklebust nfs_direct_complete(dreq); 535*fad61490STrond Myklebust } 536*fad61490STrond Myklebust #endif 537*fad61490STrond Myklebust 538462d5b32SChuck Lever static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize) 5391da177e4SLinus Torvalds { 540462d5b32SChuck Lever struct list_head *list; 541462d5b32SChuck Lever struct nfs_direct_req *dreq; 542462d5b32SChuck Lever unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 5431da177e4SLinus Torvalds 544462d5b32SChuck Lever dreq = nfs_direct_req_alloc(); 545462d5b32SChuck Lever if (!dreq) 546462d5b32SChuck Lever return NULL; 5471da177e4SLinus Torvalds 548462d5b32SChuck Lever list = &dreq->list; 549462d5b32SChuck Lever for(;;) { 550462d5b32SChuck Lever struct nfs_write_data *data = nfs_writedata_alloc(wpages); 5511da177e4SLinus Torvalds 552462d5b32SChuck Lever if (unlikely(!data)) { 553462d5b32SChuck Lever while (!list_empty(list)) { 554462d5b32SChuck Lever data = list_entry(list->next, 555462d5b32SChuck Lever struct nfs_write_data, pages); 556462d5b32SChuck Lever list_del(&data->pages); 557462d5b32SChuck Lever nfs_writedata_free(data); 558462d5b32SChuck Lever } 559462d5b32SChuck Lever kref_put(&dreq->kref, nfs_direct_req_release); 560462d5b32SChuck Lever return NULL; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 563462d5b32SChuck Lever INIT_LIST_HEAD(&data->pages); 564462d5b32SChuck Lever list_add(&data->pages, list); 5651da177e4SLinus Torvalds 566462d5b32SChuck Lever data->req = (struct nfs_page *) dreq; 56715ce4a0cSChuck Lever dreq->outstanding++; 568462d5b32SChuck Lever if (nbytes <= wsize) 5691da177e4SLinus Torvalds break; 570462d5b32SChuck Lever nbytes -= wsize; 571462d5b32SChuck Lever } 572*fad61490STrond Myklebust 573*fad61490STrond Myklebust nfs_alloc_commit_data(dreq); 574*fad61490STrond Myklebust 575462d5b32SChuck Lever kref_get(&dreq->kref); 576462d5b32SChuck Lever return dreq; 577462d5b32SChuck Lever } 5781da177e4SLinus Torvalds 579462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata) 580462d5b32SChuck Lever { 581462d5b32SChuck Lever struct nfs_write_data *data = calldata; 582462d5b32SChuck Lever struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 583462d5b32SChuck Lever int status = task->tk_status; 584462d5b32SChuck Lever 585462d5b32SChuck Lever if (nfs_writeback_done(task, data) != 0) 586462d5b32SChuck Lever return; 587462d5b32SChuck Lever 58815ce4a0cSChuck Lever spin_lock(&dreq->lock); 589462d5b32SChuck Lever 59015ce4a0cSChuck Lever if (likely(status >= 0)) 59115ce4a0cSChuck Lever dreq->count += data->res.count; 59215ce4a0cSChuck Lever else 593*fad61490STrond Myklebust dreq->error = task->tk_status; 59415ce4a0cSChuck Lever 595*fad61490STrond Myklebust if (data->res.verf->committed != NFS_FILE_SYNC) { 596*fad61490STrond Myklebust switch (dreq->flags) { 597*fad61490STrond Myklebust case 0: 598*fad61490STrond Myklebust memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); 599*fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_DO_COMMIT; 600*fad61490STrond Myklebust break; 601*fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 602*fad61490STrond Myklebust if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { 603*fad61490STrond Myklebust dprintk("NFS: %5u write verify failed\n", task->tk_pid); 604*fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 605*fad61490STrond Myklebust } 606*fad61490STrond Myklebust } 607*fad61490STrond Myklebust } 608*fad61490STrond Myklebust /* In case we have to resend */ 609*fad61490STrond Myklebust data->args.stable = NFS_FILE_SYNC; 610*fad61490STrond Myklebust 611*fad61490STrond Myklebust spin_unlock(&dreq->lock); 612*fad61490STrond Myklebust } 613*fad61490STrond Myklebust 614*fad61490STrond Myklebust /* 615*fad61490STrond Myklebust * NB: Return the value of the first error return code. Subsequent 616*fad61490STrond Myklebust * errors after the first one are ignored. 617*fad61490STrond Myklebust */ 618*fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata) 619*fad61490STrond Myklebust { 620*fad61490STrond Myklebust struct nfs_write_data *data = calldata; 621*fad61490STrond Myklebust struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; 622*fad61490STrond Myklebust 623*fad61490STrond Myklebust spin_lock(&dreq->lock); 62415ce4a0cSChuck Lever if (--dreq->outstanding) { 62515ce4a0cSChuck Lever spin_unlock(&dreq->lock); 62615ce4a0cSChuck Lever return; 62715ce4a0cSChuck Lever } 62815ce4a0cSChuck Lever spin_unlock(&dreq->lock); 62915ce4a0cSChuck Lever 630*fad61490STrond Myklebust nfs_direct_write_complete(dreq, data->inode); 631462d5b32SChuck Lever } 632462d5b32SChuck Lever 633462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = { 634462d5b32SChuck Lever .rpc_call_done = nfs_direct_write_result, 635*fad61490STrond Myklebust .rpc_release = nfs_direct_write_release, 636462d5b32SChuck Lever }; 637462d5b32SChuck Lever 638462d5b32SChuck Lever /* 639462d5b32SChuck Lever * For each nfs_write_data struct that was allocated on the list, dispatch 640462d5b32SChuck Lever * an NFS WRITE operation 641462d5b32SChuck Lever */ 642*fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync) 643462d5b32SChuck Lever { 644c89f2ee5SChuck Lever struct file *file = dreq->filp; 645c89f2ee5SChuck Lever struct inode *inode = file->f_mapping->host; 646c89f2ee5SChuck Lever struct nfs_open_context *ctx = (struct nfs_open_context *) 647c89f2ee5SChuck Lever file->private_data; 648462d5b32SChuck Lever struct list_head *list = &dreq->list; 649462d5b32SChuck Lever struct page **pages = dreq->pages; 650*fad61490STrond Myklebust size_t count = dreq->user_count; 651*fad61490STrond Myklebust loff_t pos = dreq->pos; 652462d5b32SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 653462d5b32SChuck Lever unsigned int curpage, pgbase; 654462d5b32SChuck Lever 655462d5b32SChuck Lever curpage = 0; 656*fad61490STrond Myklebust pgbase = dreq->user_addr & ~PAGE_MASK; 657462d5b32SChuck Lever do { 658462d5b32SChuck Lever struct nfs_write_data *data; 659462d5b32SChuck Lever size_t bytes; 660462d5b32SChuck Lever 661462d5b32SChuck Lever bytes = wsize; 662462d5b32SChuck Lever if (count < wsize) 663462d5b32SChuck Lever bytes = count; 664462d5b32SChuck Lever 665462d5b32SChuck Lever data = list_entry(list->next, struct nfs_write_data, pages); 666*fad61490STrond Myklebust list_move_tail(&data->pages, &dreq->rewrite_list); 667462d5b32SChuck Lever 668462d5b32SChuck Lever data->inode = inode; 669462d5b32SChuck Lever data->cred = ctx->cred; 670462d5b32SChuck Lever data->args.fh = NFS_FH(inode); 671462d5b32SChuck Lever data->args.context = ctx; 67288467055SChuck Lever data->args.offset = pos; 673462d5b32SChuck Lever data->args.pgbase = pgbase; 674462d5b32SChuck Lever data->args.pages = &pages[curpage]; 675462d5b32SChuck Lever data->args.count = bytes; 676462d5b32SChuck Lever data->res.fattr = &data->fattr; 677462d5b32SChuck Lever data->res.count = bytes; 67847989d74SChuck Lever data->res.verf = &data->verf; 679462d5b32SChuck Lever 680462d5b32SChuck Lever rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, 681462d5b32SChuck Lever &nfs_write_direct_ops, data); 682*fad61490STrond Myklebust NFS_PROTO(inode)->write_setup(data, sync); 683462d5b32SChuck Lever 684462d5b32SChuck Lever data->task.tk_priority = RPC_PRIORITY_NORMAL; 685462d5b32SChuck Lever data->task.tk_cookie = (unsigned long) inode; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds lock_kernel(); 688462d5b32SChuck Lever rpc_execute(&data->task); 6891da177e4SLinus Torvalds unlock_kernel(); 6901da177e4SLinus Torvalds 691462d5b32SChuck Lever dfprintk(VFS, "NFS: %4d initiated direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 692462d5b32SChuck Lever data->task.tk_pid, 693462d5b32SChuck Lever inode->i_sb->s_id, 694462d5b32SChuck Lever (long long)NFS_FILEID(inode), 695462d5b32SChuck Lever bytes, 696462d5b32SChuck Lever (unsigned long long)data->args.offset); 697462d5b32SChuck Lever 69888467055SChuck Lever pos += bytes; 699462d5b32SChuck Lever pgbase += bytes; 700462d5b32SChuck Lever curpage += pgbase >> PAGE_SHIFT; 701462d5b32SChuck Lever pgbase &= ~PAGE_MASK; 702462d5b32SChuck Lever 703462d5b32SChuck Lever count -= bytes; 704462d5b32SChuck Lever } while (count != 0); 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 70788467055SChuck 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) 708462d5b32SChuck Lever { 709462d5b32SChuck Lever ssize_t result; 710462d5b32SChuck Lever sigset_t oldset; 711c89f2ee5SChuck Lever struct inode *inode = iocb->ki_filp->f_mapping->host; 712462d5b32SChuck Lever struct rpc_clnt *clnt = NFS_CLIENT(inode); 713462d5b32SChuck Lever struct nfs_direct_req *dreq; 714*fad61490STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize; 715*fad61490STrond Myklebust int sync = 0; 716462d5b32SChuck Lever 717*fad61490STrond Myklebust dreq = nfs_direct_write_alloc(count, wsize); 718462d5b32SChuck Lever if (!dreq) 719462d5b32SChuck Lever return -ENOMEM; 720*fad61490STrond Myklebust if (dreq->commit_data == NULL || count < wsize) 721*fad61490STrond Myklebust sync = FLUSH_STABLE; 722462d5b32SChuck Lever 723*fad61490STrond Myklebust dreq->user_addr = user_addr; 724*fad61490STrond Myklebust dreq->user_count = count; 725*fad61490STrond Myklebust dreq->pos = pos; 726462d5b32SChuck Lever dreq->pages = pages; 727462d5b32SChuck Lever dreq->npages = nr_pages; 728a37ec012SChuck Lever igrab(inode); 729c89f2ee5SChuck Lever dreq->inode = inode; 730c89f2ee5SChuck Lever dreq->filp = iocb->ki_filp; 731c89f2ee5SChuck Lever if (!is_sync_kiocb(iocb)) 732c89f2ee5SChuck Lever dreq->iocb = iocb; 733462d5b32SChuck Lever 73447989d74SChuck Lever nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count); 73547989d74SChuck Lever 736462d5b32SChuck Lever nfs_begin_data_update(inode); 737462d5b32SChuck Lever 738462d5b32SChuck Lever rpc_clnt_sigmask(clnt, &oldset); 739*fad61490STrond Myklebust nfs_direct_write_schedule(dreq, sync); 740c89f2ee5SChuck Lever result = nfs_direct_wait(dreq); 741462d5b32SChuck Lever rpc_clnt_sigunmask(clnt, &oldset); 742462d5b32SChuck Lever 743462d5b32SChuck Lever return result; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /** 7471da177e4SLinus Torvalds * nfs_file_direct_read - file direct read operation for NFS files 7481da177e4SLinus Torvalds * @iocb: target I/O control block 7491da177e4SLinus Torvalds * @buf: user's buffer into which to read data 75088467055SChuck Lever * @count: number of bytes to read 75188467055SChuck Lever * @pos: byte offset in file where reading starts 7521da177e4SLinus Torvalds * 7531da177e4SLinus Torvalds * We use this function for direct reads instead of calling 7541da177e4SLinus Torvalds * generic_file_aio_read() in order to avoid gfar's check to see if 7551da177e4SLinus Torvalds * the request starts before the end of the file. For that check 7561da177e4SLinus Torvalds * to work, we must generate a GETATTR before each direct read, and 7571da177e4SLinus Torvalds * even then there is a window between the GETATTR and the subsequent 75888467055SChuck Lever * READ where the file size could change. Our preference is simply 7591da177e4SLinus Torvalds * to do all reads the application wants, and the server will take 7601da177e4SLinus Torvalds * care of managing the end of file boundary. 7611da177e4SLinus Torvalds * 7621da177e4SLinus Torvalds * This function also eliminates unnecessarily updating the file's 7631da177e4SLinus Torvalds * atime locally, as the NFS server sets the file's atime, and this 7641da177e4SLinus Torvalds * client must read the updated atime from the server back into its 7651da177e4SLinus Torvalds * cache. 7661da177e4SLinus Torvalds */ 767d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos) 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds ssize_t retval = -EINVAL; 7700cdd80d0SChuck Lever int page_count; 7710cdd80d0SChuck Lever struct page **pages; 7721da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 7731da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 7741da177e4SLinus Torvalds 775ce1a8e67SChuck Lever dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", 7760bbacc40SChuck Lever file->f_dentry->d_parent->d_name.name, 7770bbacc40SChuck Lever file->f_dentry->d_name.name, 778ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (count < 0) 7811da177e4SLinus Torvalds goto out; 7821da177e4SLinus Torvalds retval = -EFAULT; 7830cdd80d0SChuck Lever if (!access_ok(VERIFY_WRITE, buf, count)) 7841da177e4SLinus Torvalds goto out; 7851da177e4SLinus Torvalds retval = 0; 7861da177e4SLinus Torvalds if (!count) 7871da177e4SLinus Torvalds goto out; 7881da177e4SLinus Torvalds 78929884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 7901da177e4SLinus Torvalds if (retval) 7911da177e4SLinus Torvalds goto out; 7921da177e4SLinus Torvalds 7930cdd80d0SChuck Lever page_count = nfs_get_user_pages(READ, (unsigned long) buf, 7940cdd80d0SChuck Lever count, &pages); 7950cdd80d0SChuck Lever if (page_count < 0) { 7960cdd80d0SChuck Lever nfs_free_user_pages(pages, 0, 0); 7970cdd80d0SChuck Lever retval = page_count; 7980cdd80d0SChuck Lever goto out; 7990cdd80d0SChuck Lever } 8000cdd80d0SChuck Lever 80199514f8fSChuck Lever retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos, 8020cdd80d0SChuck Lever pages, page_count); 8031da177e4SLinus Torvalds if (retval > 0) 8040cdd80d0SChuck Lever iocb->ki_pos = pos + retval; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds out: 8071da177e4SLinus Torvalds return retval; 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds /** 8111da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 8121da177e4SLinus Torvalds * @iocb: target I/O control block 8131da177e4SLinus Torvalds * @buf: user's buffer from which to write data 81488467055SChuck Lever * @count: number of bytes to write 81588467055SChuck Lever * @pos: byte offset in file where writing starts 8161da177e4SLinus Torvalds * 8171da177e4SLinus Torvalds * We use this function for direct writes instead of calling 8181da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 8191da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 8201da177e4SLinus Torvalds * the new i_size and this client must read the updated size 8211da177e4SLinus Torvalds * back into its cache. We let the server do generic write 8221da177e4SLinus Torvalds * parameter checking and report problems. 8231da177e4SLinus Torvalds * 8241da177e4SLinus Torvalds * We also avoid an unnecessary invocation of generic_osync_inode(), 8251da177e4SLinus Torvalds * as it is fairly meaningless to sync the metadata of an NFS file. 8261da177e4SLinus Torvalds * 8271da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8281da177e4SLinus Torvalds * 8291da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8301da177e4SLinus Torvalds * readers of this file. 8311da177e4SLinus Torvalds * 8321da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8331da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8341da177e4SLinus Torvalds */ 835d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) 8361da177e4SLinus Torvalds { 837ce1a8e67SChuck Lever ssize_t retval; 83847989d74SChuck Lever int page_count; 83947989d74SChuck Lever struct page **pages; 8401da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 8411da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 8421da177e4SLinus Torvalds 843ce1a8e67SChuck Lever dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", 8440bbacc40SChuck Lever file->f_dentry->d_parent->d_name.name, 845ce1a8e67SChuck Lever file->f_dentry->d_name.name, 846ce1a8e67SChuck Lever (unsigned long) count, (long long) pos); 8471da177e4SLinus Torvalds 848ce1a8e67SChuck Lever retval = generic_write_checks(file, &pos, &count, 0); 849ce1a8e67SChuck Lever if (retval) 8501da177e4SLinus Torvalds goto out; 851ce1a8e67SChuck Lever 852ce1a8e67SChuck Lever retval = -EINVAL; 853ce1a8e67SChuck Lever if ((ssize_t) count < 0) 8541da177e4SLinus Torvalds goto out; 8551da177e4SLinus Torvalds retval = 0; 8561da177e4SLinus Torvalds if (!count) 8571da177e4SLinus Torvalds goto out; 858ce1a8e67SChuck Lever 859ce1a8e67SChuck Lever retval = -EFAULT; 86047989d74SChuck Lever if (!access_ok(VERIFY_READ, buf, count)) 861ce1a8e67SChuck Lever goto out; 8621da177e4SLinus Torvalds 86329884df0STrond Myklebust retval = nfs_sync_mapping(mapping); 8641da177e4SLinus Torvalds if (retval) 8651da177e4SLinus Torvalds goto out; 8661da177e4SLinus Torvalds 86747989d74SChuck Lever page_count = nfs_get_user_pages(WRITE, (unsigned long) buf, 86847989d74SChuck Lever count, &pages); 86947989d74SChuck Lever if (page_count < 0) { 87047989d74SChuck Lever nfs_free_user_pages(pages, 0, 0); 87147989d74SChuck Lever retval = page_count; 87247989d74SChuck Lever goto out; 87347989d74SChuck Lever } 87447989d74SChuck Lever 875c89f2ee5SChuck Lever retval = nfs_direct_write(iocb, (unsigned long) buf, count, 87647989d74SChuck Lever pos, pages, page_count); 8779eafa8ccSChuck Lever 8789eafa8ccSChuck Lever /* 8799eafa8ccSChuck Lever * XXX: nfs_end_data_update() already ensures this file's 8809eafa8ccSChuck Lever * cached data is subsequently invalidated. Do we really 8819eafa8ccSChuck Lever * need to call invalidate_inode_pages2() again here? 8829eafa8ccSChuck Lever * 8839eafa8ccSChuck Lever * For aio writes, this invalidation will almost certainly 8849eafa8ccSChuck Lever * occur before the writes complete. Kind of racey. 8859eafa8ccSChuck Lever */ 8861da177e4SLinus Torvalds if (mapping->nrpages) 8871da177e4SLinus Torvalds invalidate_inode_pages2(mapping); 8889eafa8ccSChuck Lever 8891da177e4SLinus Torvalds if (retval > 0) 890ce1a8e67SChuck Lever iocb->ki_pos = pos + retval; 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds out: 8931da177e4SLinus Torvalds return retval; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 89688467055SChuck Lever /** 89788467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 89888467055SChuck Lever * 89988467055SChuck Lever */ 9001da177e4SLinus Torvalds int nfs_init_directcache(void) 9011da177e4SLinus Torvalds { 9021da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 9031da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 9041da177e4SLinus Torvalds 0, SLAB_RECLAIM_ACCOUNT, 9051da177e4SLinus Torvalds NULL, NULL); 9061da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 9071da177e4SLinus Torvalds return -ENOMEM; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds return 0; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 91288467055SChuck Lever /** 91388467055SChuck Lever * nfs_init_directcache - destroy the slab cache for nfs_direct_req structures 91488467055SChuck Lever * 91588467055SChuck Lever */ 9161da177e4SLinus Torvalds void nfs_destroy_directcache(void) 9171da177e4SLinus Torvalds { 9181da177e4SLinus Torvalds if (kmem_cache_destroy(nfs_direct_cachep)) 9191da177e4SLinus Torvalds printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); 9201da177e4SLinus Torvalds } 921