11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/read.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Block I/O for NFS 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Partial copy of Linus' read cache modifications to fs/nfs/file.c 71da177e4SLinus Torvalds * modified for async RPC by okir@monad.swb.de 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * We do an ugly hack here in order to return proper error codes to the 101da177e4SLinus Torvalds * user program when a read request failed: since generic_file_read 111da177e4SLinus Torvalds * only checks the return value of inode->i_op->readpage() which is always 0 121da177e4SLinus Torvalds * for async RPC, we set the error bit of the page to 1 when an error occurs, 131da177e4SLinus Torvalds * and make nfs_readpage transmit requests synchronously when encountering this. 141da177e4SLinus Torvalds * This is only a small problem, though, since we now retry all operations 151da177e4SLinus Torvalds * within the RPC code when root squashing is suspected. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/time.h> 191da177e4SLinus Torvalds #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/errno.h> 211da177e4SLinus Torvalds #include <linux/fcntl.h> 221da177e4SLinus Torvalds #include <linux/stat.h> 231da177e4SLinus Torvalds #include <linux/mm.h> 241da177e4SLinus Torvalds #include <linux/slab.h> 251da177e4SLinus Torvalds #include <linux/pagemap.h> 261da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 271da177e4SLinus Torvalds #include <linux/nfs_fs.h> 281da177e4SLinus Torvalds #include <linux/nfs_page.h> 291da177e4SLinus Torvalds #include <linux/smp_lock.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include <asm/system.h> 321da177e4SLinus Torvalds 3391d5b470SChuck Lever #include "iostat.h" 3491d5b470SChuck Lever 351da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds static int nfs_pagein_one(struct list_head *, struct inode *); 38ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops; 39ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static kmem_cache_t *nfs_rdata_cachep; 423feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define MIN_POOL_READ (32) 451da177e4SLinus Torvalds 46e9f7bee1STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(size_t len) 473feb2d49STrond Myklebust { 48e9f7bee1STrond Myklebust unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 493feb2d49STrond Myklebust struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS); 503feb2d49STrond Myklebust 513feb2d49STrond Myklebust if (p) { 523feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 533feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 54e9f7bee1STrond Myklebust p->npages = pagecount; 550d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 560d0b5cb3SChuck Lever p->pagevec = p->page_array; 573feb2d49STrond Myklebust else { 580d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 590d0b5cb3SChuck Lever if (!p->pagevec) { 603feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 613feb2d49STrond Myklebust p = NULL; 623feb2d49STrond Myklebust } 633feb2d49STrond Myklebust } 643feb2d49STrond Myklebust } 653feb2d49STrond Myklebust return p; 663feb2d49STrond Myklebust } 673feb2d49STrond Myklebust 688aca67f0STrond Myklebust static void nfs_readdata_rcu_free(struct rcu_head *head) 693feb2d49STrond Myklebust { 708aca67f0STrond Myklebust struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu); 713feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 723feb2d49STrond Myklebust kfree(p->pagevec); 733feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 743feb2d49STrond Myklebust } 753feb2d49STrond Myklebust 768aca67f0STrond Myklebust static void nfs_readdata_free(struct nfs_read_data *rdata) 778aca67f0STrond Myklebust { 788aca67f0STrond Myklebust call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free); 798aca67f0STrond Myklebust } 808aca67f0STrond Myklebust 81963d8fe5STrond Myklebust void nfs_readdata_release(void *data) 821da177e4SLinus Torvalds { 831da177e4SLinus Torvalds nfs_readdata_free(data); 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds static 871da177e4SLinus Torvalds unsigned int nfs_page_length(struct inode *inode, struct page *page) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 901da177e4SLinus Torvalds unsigned long idx; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds if (i_size <= 0) 931da177e4SLinus Torvalds return 0; 941da177e4SLinus Torvalds idx = (i_size - 1) >> PAGE_CACHE_SHIFT; 951da177e4SLinus Torvalds if (page->index > idx) 961da177e4SLinus Torvalds return 0; 971da177e4SLinus Torvalds if (page->index != idx) 981da177e4SLinus Torvalds return PAGE_CACHE_SIZE; 991da177e4SLinus Torvalds return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1)); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static 1031da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE); 1061da177e4SLinus Torvalds SetPageUptodate(page); 1071da177e4SLinus Torvalds unlock_page(page); 1081da177e4SLinus Torvalds return 0; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) 1121de3fc12STrond Myklebust { 1131de3fc12STrond Myklebust unsigned int remainder = data->args.count - data->res.count; 1141de3fc12STrond Myklebust unsigned int base = data->args.pgbase + data->res.count; 1151de3fc12STrond Myklebust unsigned int pglen; 1161de3fc12STrond Myklebust struct page **pages; 1171de3fc12STrond Myklebust 1181de3fc12STrond Myklebust if (data->res.eof == 0 || remainder == 0) 1191de3fc12STrond Myklebust return; 1201de3fc12STrond Myklebust /* 1211de3fc12STrond Myklebust * Note: "remainder" can never be negative, since we check for 1221de3fc12STrond Myklebust * this in the XDR code. 1231de3fc12STrond Myklebust */ 1241de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 1251de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 1261de3fc12STrond Myklebust pglen = PAGE_CACHE_SIZE - base; 12779558f36STrond Myklebust for (;;) { 12879558f36STrond Myklebust if (remainder <= pglen) { 1291de3fc12STrond Myklebust memclear_highpage_flush(*pages, base, remainder); 13079558f36STrond Myklebust break; 13179558f36STrond Myklebust } 13279558f36STrond Myklebust memclear_highpage_flush(*pages, base, pglen); 13379558f36STrond Myklebust pages++; 13479558f36STrond Myklebust remainder -= pglen; 13579558f36STrond Myklebust pglen = PAGE_CACHE_SIZE; 13679558f36STrond Myklebust base = 0; 13779558f36STrond Myklebust } 1381de3fc12STrond Myklebust } 1391de3fc12STrond Myklebust 1401da177e4SLinus Torvalds /* 1411da177e4SLinus Torvalds * Read a page synchronously. 1421da177e4SLinus Torvalds */ 1431da177e4SLinus Torvalds static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode, 1441da177e4SLinus Torvalds struct page *page) 1451da177e4SLinus Torvalds { 1461da177e4SLinus Torvalds unsigned int rsize = NFS_SERVER(inode)->rsize; 1471da177e4SLinus Torvalds unsigned int count = PAGE_CACHE_SIZE; 1481da177e4SLinus Torvalds int result; 1491da177e4SLinus Torvalds struct nfs_read_data *rdata; 1501da177e4SLinus Torvalds 151e9f7bee1STrond Myklebust rdata = nfs_readdata_alloc(count); 1521da177e4SLinus Torvalds if (!rdata) 1531da177e4SLinus Torvalds return -ENOMEM; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds memset(rdata, 0, sizeof(*rdata)); 1561da177e4SLinus Torvalds rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); 1571da177e4SLinus Torvalds rdata->cred = ctx->cred; 1581da177e4SLinus Torvalds rdata->inode = inode; 1591da177e4SLinus Torvalds INIT_LIST_HEAD(&rdata->pages); 1601da177e4SLinus Torvalds rdata->args.fh = NFS_FH(inode); 1611da177e4SLinus Torvalds rdata->args.context = ctx; 1621da177e4SLinus Torvalds rdata->args.pages = &page; 1631da177e4SLinus Torvalds rdata->args.pgbase = 0UL; 1641da177e4SLinus Torvalds rdata->args.count = rsize; 1651da177e4SLinus Torvalds rdata->res.fattr = &rdata->fattr; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds dprintk("NFS: nfs_readpage_sync(%p)\n", page); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* 1701da177e4SLinus Torvalds * This works now because the socket layer never tries to DMA 1711da177e4SLinus Torvalds * into this buffer directly. 1721da177e4SLinus Torvalds */ 1731da177e4SLinus Torvalds do { 1741da177e4SLinus Torvalds if (count < rsize) 1751da177e4SLinus Torvalds rdata->args.count = count; 1761da177e4SLinus Torvalds rdata->res.count = rdata->args.count; 1771da177e4SLinus Torvalds rdata->args.offset = page_offset(page) + rdata->args.pgbase; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n", 18054ceac45SDavid Howells NFS_SERVER(inode)->nfs_client->cl_hostname, 1811da177e4SLinus Torvalds inode->i_sb->s_id, 1821da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 1831da177e4SLinus Torvalds (unsigned long long)rdata->args.pgbase, 1841da177e4SLinus Torvalds rdata->args.count); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds lock_kernel(); 1871da177e4SLinus Torvalds result = NFS_PROTO(inode)->read(rdata); 1881da177e4SLinus Torvalds unlock_kernel(); 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* 1911da177e4SLinus Torvalds * Even if we had a partial success we can't mark the page 1921da177e4SLinus Torvalds * cache valid. 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds if (result < 0) { 1951da177e4SLinus Torvalds if (result == -EISDIR) 1961da177e4SLinus Torvalds result = -EINVAL; 1971da177e4SLinus Torvalds goto io_error; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds count -= result; 2001da177e4SLinus Torvalds rdata->args.pgbase += result; 20191d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result); 20291d5b470SChuck Lever 2031da177e4SLinus Torvalds /* Note: result == 0 should only happen if we're caching 2041da177e4SLinus Torvalds * a write that extends the file and punches a hole. 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds if (rdata->res.eof != 0 || result == 0) 2071da177e4SLinus Torvalds break; 2081da177e4SLinus Torvalds } while (count); 209dc59250cSChuck Lever spin_lock(&inode->i_lock); 21055296809SChuck Lever NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME; 211dc59250cSChuck Lever spin_unlock(&inode->i_lock); 2121da177e4SLinus Torvalds 2137a524111STrond Myklebust if (rdata->res.eof || rdata->res.count == rdata->args.count) { 2141da177e4SLinus Torvalds SetPageUptodate(page); 2157a524111STrond Myklebust if (rdata->res.eof && count != 0) 2167a524111STrond Myklebust memclear_highpage_flush(page, rdata->args.pgbase, count); 2177a524111STrond Myklebust } 2181da177e4SLinus Torvalds result = 0; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds io_error: 2211da177e4SLinus Torvalds unlock_page(page); 2221da177e4SLinus Torvalds nfs_readdata_free(rdata); 2231da177e4SLinus Torvalds return result; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 2271da177e4SLinus Torvalds struct page *page) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds LIST_HEAD(one_request); 2301da177e4SLinus Torvalds struct nfs_page *new; 2311da177e4SLinus Torvalds unsigned int len; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds len = nfs_page_length(inode, page); 2341da177e4SLinus Torvalds if (len == 0) 2351da177e4SLinus Torvalds return nfs_return_empty_page(page); 2361da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 2371da177e4SLinus Torvalds if (IS_ERR(new)) { 2381da177e4SLinus Torvalds unlock_page(page); 2391da177e4SLinus Torvalds return PTR_ERR(new); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 2421da177e4SLinus Torvalds memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds nfs_list_add_request(new, &one_request); 2451da177e4SLinus Torvalds nfs_pagein_one(&one_request, inode); 2461da177e4SLinus Torvalds return 0; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 2501da177e4SLinus Torvalds { 2511da177e4SLinus Torvalds unlock_page(req->wb_page); 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 2541da177e4SLinus Torvalds req->wb_context->dentry->d_inode->i_sb->s_id, 2551da177e4SLinus Torvalds (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 2561da177e4SLinus Torvalds req->wb_bytes, 2571da177e4SLinus Torvalds (long long)req_offset(req)); 25810d2c46fSNick Wilson nfs_clear_request(req); 25910d2c46fSNick Wilson nfs_release_request(req); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* 2631da177e4SLinus Torvalds * Set up the NFS read request struct 2641da177e4SLinus Torvalds */ 2651da177e4SLinus Torvalds static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 266ec06c096STrond Myklebust const struct rpc_call_ops *call_ops, 2671da177e4SLinus Torvalds unsigned int count, unsigned int offset) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds struct inode *inode; 270ec06c096STrond Myklebust int flags; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds data->req = req; 2731da177e4SLinus Torvalds data->inode = inode = req->wb_context->dentry->d_inode; 2741da177e4SLinus Torvalds data->cred = req->wb_context->cred; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 2771da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 2781da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 2791da177e4SLinus Torvalds data->args.pages = data->pagevec; 2801da177e4SLinus Torvalds data->args.count = count; 2811da177e4SLinus Torvalds data->args.context = req->wb_context; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds data->res.fattr = &data->fattr; 2841da177e4SLinus Torvalds data->res.count = count; 2851da177e4SLinus Torvalds data->res.eof = 0; 2860e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 2871da177e4SLinus Torvalds 288ec06c096STrond Myklebust /* Set up the initial task struct. */ 289ec06c096STrond Myklebust flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); 290ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); 2911da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long)inode; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 2961da177e4SLinus Torvalds data->task.tk_pid, 2971da177e4SLinus Torvalds inode->i_sb->s_id, 2981da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 2991da177e4SLinus Torvalds count, 3001da177e4SLinus Torvalds (unsigned long long)data->args.offset); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static void 3041da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 3051da177e4SLinus Torvalds { 3061da177e4SLinus Torvalds struct nfs_page *req; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds while (!list_empty(head)) { 3091da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3101da177e4SLinus Torvalds nfs_list_remove_request(req); 3111da177e4SLinus Torvalds SetPageError(req->wb_page); 3121da177e4SLinus Torvalds nfs_readpage_release(req); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* 3171da177e4SLinus Torvalds * Start an async read operation 3181da177e4SLinus Torvalds */ 3191da177e4SLinus Torvalds static void nfs_execute_read(struct nfs_read_data *data) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(data->inode); 3221da177e4SLinus Torvalds sigset_t oldset; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 3251da177e4SLinus Torvalds lock_kernel(); 3261da177e4SLinus Torvalds rpc_execute(&data->task); 3271da177e4SLinus Torvalds unlock_kernel(); 3281da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* 3321da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 3331da177e4SLinus Torvalds * 3341da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 3351da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 3361da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 3371da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 3381da177e4SLinus Torvalds * 3391da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 3401da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 3411da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 3421da177e4SLinus Torvalds * or less conventional NFS client behavior. 3431da177e4SLinus Torvalds */ 3441da177e4SLinus Torvalds static int nfs_pagein_multi(struct list_head *head, struct inode *inode) 3451da177e4SLinus Torvalds { 3461da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 3471da177e4SLinus Torvalds struct page *page = req->wb_page; 3481da177e4SLinus Torvalds struct nfs_read_data *data; 349e9f7bee1STrond Myklebust size_t rsize = NFS_SERVER(inode)->rsize, nbytes; 350e9f7bee1STrond Myklebust unsigned int offset; 3511da177e4SLinus Torvalds int requests = 0; 3521da177e4SLinus Torvalds LIST_HEAD(list); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds nfs_list_remove_request(req); 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds nbytes = req->wb_bytes; 357e9f7bee1STrond Myklebust do { 358e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 359e9f7bee1STrond Myklebust 360e9f7bee1STrond Myklebust data = nfs_readdata_alloc(len); 3611da177e4SLinus Torvalds if (!data) 3621da177e4SLinus Torvalds goto out_bad; 3631da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 3641da177e4SLinus Torvalds list_add(&data->pages, &list); 3651da177e4SLinus Torvalds requests++; 366e9f7bee1STrond Myklebust nbytes -= len; 367e9f7bee1STrond Myklebust } while(nbytes != 0); 3681da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds ClearPageError(page); 3711da177e4SLinus Torvalds offset = 0; 3721da177e4SLinus Torvalds nbytes = req->wb_bytes; 3731da177e4SLinus Torvalds do { 3741da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3751da177e4SLinus Torvalds list_del_init(&data->pages); 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds data->pagevec[0] = page; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds if (nbytes > rsize) { 380ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 381ec06c096STrond Myklebust rsize, offset); 3821da177e4SLinus Torvalds offset += rsize; 3831da177e4SLinus Torvalds nbytes -= rsize; 3841da177e4SLinus Torvalds } else { 385ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 386ec06c096STrond Myklebust nbytes, offset); 3871da177e4SLinus Torvalds nbytes = 0; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds nfs_execute_read(data); 3901da177e4SLinus Torvalds } while (nbytes != 0); 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds return 0; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds out_bad: 3951da177e4SLinus Torvalds while (!list_empty(&list)) { 3961da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3971da177e4SLinus Torvalds list_del(&data->pages); 3981da177e4SLinus Torvalds nfs_readdata_free(data); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds SetPageError(page); 4011da177e4SLinus Torvalds nfs_readpage_release(req); 4021da177e4SLinus Torvalds return -ENOMEM; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds static int nfs_pagein_one(struct list_head *head, struct inode *inode) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds struct nfs_page *req; 4081da177e4SLinus Torvalds struct page **pages; 4091da177e4SLinus Torvalds struct nfs_read_data *data; 4101da177e4SLinus Torvalds unsigned int count; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) 4131da177e4SLinus Torvalds return nfs_pagein_multi(head, inode); 4141da177e4SLinus Torvalds 415e9f7bee1STrond Myklebust data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize); 4161da177e4SLinus Torvalds if (!data) 4171da177e4SLinus Torvalds goto out_bad; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 4201da177e4SLinus Torvalds pages = data->pagevec; 4211da177e4SLinus Torvalds count = 0; 4221da177e4SLinus Torvalds while (!list_empty(head)) { 4231da177e4SLinus Torvalds req = nfs_list_entry(head->next); 4241da177e4SLinus Torvalds nfs_list_remove_request(req); 4251da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 4261da177e4SLinus Torvalds ClearPageError(req->wb_page); 4271da177e4SLinus Torvalds *pages++ = req->wb_page; 4281da177e4SLinus Torvalds count += req->wb_bytes; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 4311da177e4SLinus Torvalds 432ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds nfs_execute_read(data); 4351da177e4SLinus Torvalds return 0; 4361da177e4SLinus Torvalds out_bad: 4371da177e4SLinus Torvalds nfs_async_read_error(head); 4381da177e4SLinus Torvalds return -ENOMEM; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds static int 4421da177e4SLinus Torvalds nfs_pagein_list(struct list_head *head, int rpages) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds LIST_HEAD(one_request); 4451da177e4SLinus Torvalds struct nfs_page *req; 4461da177e4SLinus Torvalds int error = 0; 4471da177e4SLinus Torvalds unsigned int pages = 0; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds while (!list_empty(head)) { 4501da177e4SLinus Torvalds pages += nfs_coalesce_requests(head, &one_request, rpages); 4511da177e4SLinus Torvalds req = nfs_list_entry(one_request.next); 4521da177e4SLinus Torvalds error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode); 4531da177e4SLinus Torvalds if (error < 0) 4541da177e4SLinus Torvalds break; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds if (error >= 0) 4571da177e4SLinus Torvalds return pages; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds nfs_async_read_error(head); 4601da177e4SLinus Torvalds return error; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /* 464*0b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 465*0b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 466*0b671301STrond Myklebust */ 467*0b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 468*0b671301STrond Myklebust { 469*0b671301STrond Myklebust int status; 470*0b671301STrond Myklebust 471*0b671301STrond Myklebust dprintk("%s: %4d, (status %d)\n", __FUNCTION__, task->tk_pid, 472*0b671301STrond Myklebust task->tk_status); 473*0b671301STrond Myklebust 474*0b671301STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 475*0b671301STrond Myklebust if (status != 0) 476*0b671301STrond Myklebust return status; 477*0b671301STrond Myklebust 478*0b671301STrond Myklebust nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); 479*0b671301STrond Myklebust 480*0b671301STrond Myklebust if (task->tk_status == -ESTALE) { 481*0b671301STrond Myklebust set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode)); 482*0b671301STrond Myklebust nfs_mark_for_revalidate(data->inode); 483*0b671301STrond Myklebust } 484*0b671301STrond Myklebust spin_lock(&data->inode->i_lock); 485*0b671301STrond Myklebust NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME; 486*0b671301STrond Myklebust spin_unlock(&data->inode->i_lock); 487*0b671301STrond Myklebust return 0; 488*0b671301STrond Myklebust } 489*0b671301STrond Myklebust 490*0b671301STrond Myklebust static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) 491*0b671301STrond Myklebust { 492*0b671301STrond Myklebust struct nfs_readargs *argp = &data->args; 493*0b671301STrond Myklebust struct nfs_readres *resp = &data->res; 494*0b671301STrond Myklebust 495*0b671301STrond Myklebust if (resp->eof || resp->count == argp->count) 496*0b671301STrond Myklebust return 0; 497*0b671301STrond Myklebust 498*0b671301STrond Myklebust /* This is a short read! */ 499*0b671301STrond Myklebust nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 500*0b671301STrond Myklebust /* Has the server at least made some progress? */ 501*0b671301STrond Myklebust if (resp->count == 0) 502*0b671301STrond Myklebust return 0; 503*0b671301STrond Myklebust 504*0b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 505*0b671301STrond Myklebust argp->offset += resp->count; 506*0b671301STrond Myklebust argp->pgbase += resp->count; 507*0b671301STrond Myklebust argp->count -= resp->count; 508*0b671301STrond Myklebust rpc_restart_call(task); 509*0b671301STrond Myklebust return -EAGAIN; 510*0b671301STrond Myklebust } 511*0b671301STrond Myklebust 512*0b671301STrond Myklebust /* 5131da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 5141da177e4SLinus Torvalds */ 515ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 5161da177e4SLinus Torvalds { 517ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 5181da177e4SLinus Torvalds struct nfs_page *req = data->req; 5191da177e4SLinus Torvalds struct page *page = req->wb_page; 5201da177e4SLinus Torvalds 521ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 522ec06c096STrond Myklebust return; 523*0b671301STrond Myklebust 524*0b671301STrond Myklebust if (likely(task->tk_status >= 0)) { 525*0b671301STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 526*0b671301STrond Myklebust if (nfs_readpage_retry(task, data) != 0) 527*0b671301STrond Myklebust return; 528*0b671301STrond Myklebust } 529*0b671301STrond Myklebust if (unlikely(task->tk_status < 0)) 530*0b671301STrond Myklebust SetPageError(page); 5311da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 5321da177e4SLinus Torvalds if (!PageError(page)) 5331da177e4SLinus Torvalds SetPageUptodate(page); 5341da177e4SLinus Torvalds nfs_readpage_release(req); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 538ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 539ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 540ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 541ec06c096STrond Myklebust }; 542ec06c096STrond Myklebust 5431de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 5441de3fc12STrond Myklebust { 5451de3fc12STrond Myklebust unsigned int count = data->res.count; 5461de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 5471de3fc12STrond Myklebust struct page **pages; 5481de3fc12STrond Myklebust 54979558f36STrond Myklebust if (data->res.eof) 55079558f36STrond Myklebust count = data->args.count; 5511de3fc12STrond Myklebust if (unlikely(count == 0)) 5521de3fc12STrond Myklebust return; 5531de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 5541de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 5551de3fc12STrond Myklebust count += base; 5561de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 5571de3fc12STrond Myklebust SetPageUptodate(*pages); 558*0b671301STrond Myklebust if (count == 0) 559*0b671301STrond Myklebust return; 560*0b671301STrond Myklebust /* Was this a short read? */ 561*0b671301STrond Myklebust if (data->res.eof || data->res.count == data->args.count) 5621de3fc12STrond Myklebust SetPageUptodate(*pages); 5631de3fc12STrond Myklebust } 5641de3fc12STrond Myklebust 5651da177e4SLinus Torvalds /* 5661da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 5671da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 5681da177e4SLinus Torvalds */ 569ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 5701da177e4SLinus Torvalds { 571ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 5721da177e4SLinus Torvalds 573*0b671301STrond Myklebust if (nfs_readpage_result(task, data) != 0) 574*0b671301STrond Myklebust return; 5751de3fc12STrond Myklebust /* 576*0b671301STrond Myklebust * Note: nfs_readpage_retry may change the values of 5771de3fc12STrond Myklebust * data->args. In the multi-page case, we therefore need 578*0b671301STrond Myklebust * to ensure that we call nfs_readpage_set_pages_uptodate() 579*0b671301STrond Myklebust * first. 5801de3fc12STrond Myklebust */ 5811de3fc12STrond Myklebust if (likely(task->tk_status >= 0)) { 5821de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 5831de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 584*0b671301STrond Myklebust if (nfs_readpage_retry(task, data) != 0) 585ec06c096STrond Myklebust return; 586*0b671301STrond Myklebust } 5871da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 5881da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 5891da177e4SLinus Torvalds 5901de3fc12STrond Myklebust nfs_list_remove_request(req); 5911da177e4SLinus Torvalds nfs_readpage_release(req); 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 595ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 596ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 597ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 598ec06c096STrond Myklebust }; 599ec06c096STrond Myklebust 6001da177e4SLinus Torvalds /* 6011da177e4SLinus Torvalds * Read a page over NFS. 6021da177e4SLinus Torvalds * We read the page synchronously in the following case: 6031da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 6041da177e4SLinus Torvalds * previous async read operation failed. 6051da177e4SLinus Torvalds */ 6061da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 6071da177e4SLinus Torvalds { 6081da177e4SLinus Torvalds struct nfs_open_context *ctx; 6091da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 6101da177e4SLinus Torvalds int error; 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 6131da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 61491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 61591d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 61691d5b470SChuck Lever 6171da177e4SLinus Torvalds /* 6181da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 6191da177e4SLinus Torvalds * 6201da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 6211da177e4SLinus Torvalds * be any new pending writes generated at this point 6221da177e4SLinus Torvalds * for this page (other pages can be written to). 6231da177e4SLinus Torvalds */ 6241da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 6251da177e4SLinus Torvalds if (error) 6261da177e4SLinus Torvalds goto out_error; 6271da177e4SLinus Torvalds 6285f004cf2STrond Myklebust error = -ESTALE; 6295f004cf2STrond Myklebust if (NFS_STALE(inode)) 6305f004cf2STrond Myklebust goto out_error; 6315f004cf2STrond Myklebust 6321da177e4SLinus Torvalds if (file == NULL) { 633d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 6341da177e4SLinus Torvalds if (ctx == NULL) 6351da177e4SLinus Torvalds return -EBADF; 6361da177e4SLinus Torvalds } else 6371da177e4SLinus Torvalds ctx = get_nfs_open_context((struct nfs_open_context *) 6381da177e4SLinus Torvalds file->private_data); 6391da177e4SLinus Torvalds if (!IS_SYNC(inode)) { 6401da177e4SLinus Torvalds error = nfs_readpage_async(ctx, inode, page); 6411da177e4SLinus Torvalds goto out; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds error = nfs_readpage_sync(ctx, inode, page); 6451da177e4SLinus Torvalds if (error < 0 && IS_SWAPFILE(inode)) 6461da177e4SLinus Torvalds printk("Aiee.. nfs swap-in of page failed!\n"); 6471da177e4SLinus Torvalds out: 6481da177e4SLinus Torvalds put_nfs_open_context(ctx); 6491da177e4SLinus Torvalds return error; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds out_error: 6521da177e4SLinus Torvalds unlock_page(page); 6531da177e4SLinus Torvalds return error; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds struct nfs_readdesc { 6571da177e4SLinus Torvalds struct list_head *head; 6581da177e4SLinus Torvalds struct nfs_open_context *ctx; 6591da177e4SLinus Torvalds }; 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds static int 6621da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 6631da177e4SLinus Torvalds { 6641da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 6651da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 6661da177e4SLinus Torvalds struct nfs_page *new; 6671da177e4SLinus Torvalds unsigned int len; 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds nfs_wb_page(inode, page); 6701da177e4SLinus Torvalds len = nfs_page_length(inode, page); 6711da177e4SLinus Torvalds if (len == 0) 6721da177e4SLinus Torvalds return nfs_return_empty_page(page); 6731da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 6741da177e4SLinus Torvalds if (IS_ERR(new)) { 6751da177e4SLinus Torvalds SetPageError(page); 6761da177e4SLinus Torvalds unlock_page(page); 6771da177e4SLinus Torvalds return PTR_ERR(new); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 6801da177e4SLinus Torvalds memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); 6811da177e4SLinus Torvalds nfs_list_add_request(new, desc->head); 6821da177e4SLinus Torvalds return 0; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 6861da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 6871da177e4SLinus Torvalds { 6881da177e4SLinus Torvalds LIST_HEAD(head); 6891da177e4SLinus Torvalds struct nfs_readdesc desc = { 6901da177e4SLinus Torvalds .head = &head, 6911da177e4SLinus Torvalds }; 6921da177e4SLinus Torvalds struct inode *inode = mapping->host; 6931da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 6945f004cf2STrond Myklebust int ret = -ESTALE; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 6971da177e4SLinus Torvalds inode->i_sb->s_id, 6981da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 6991da177e4SLinus Torvalds nr_pages); 70091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 7011da177e4SLinus Torvalds 7025f004cf2STrond Myklebust if (NFS_STALE(inode)) 7035f004cf2STrond Myklebust goto out; 7045f004cf2STrond Myklebust 7051da177e4SLinus Torvalds if (filp == NULL) { 706d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 7071da177e4SLinus Torvalds if (desc.ctx == NULL) 7081da177e4SLinus Torvalds return -EBADF; 7091da177e4SLinus Torvalds } else 7101da177e4SLinus Torvalds desc.ctx = get_nfs_open_context((struct nfs_open_context *) 7111da177e4SLinus Torvalds filp->private_data); 7121da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 7131da177e4SLinus Torvalds if (!list_empty(&head)) { 7141da177e4SLinus Torvalds int err = nfs_pagein_list(&head, server->rpages); 7151da177e4SLinus Torvalds if (!ret) 71691d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, err); 7171da177e4SLinus Torvalds ret = err; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 7205f004cf2STrond Myklebust out: 7211da177e4SLinus Torvalds return ret; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 724f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 7271da177e4SLinus Torvalds sizeof(struct nfs_read_data), 7281da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 7291da177e4SLinus Torvalds NULL, NULL); 7301da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 7311da177e4SLinus Torvalds return -ENOMEM; 7321da177e4SLinus Torvalds 73393d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 7341da177e4SLinus Torvalds nfs_rdata_cachep); 7351da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 7361da177e4SLinus Torvalds return -ENOMEM; 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds return 0; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 7421da177e4SLinus Torvalds { 7431da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 7441a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 7451da177e4SLinus Torvalds } 746