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 68e4e20512SAdrian Bunk static void nfs_readdata_free(struct nfs_read_data *p) 693feb2d49STrond Myklebust { 703feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 713feb2d49STrond Myklebust kfree(p->pagevec); 723feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 733feb2d49STrond Myklebust } 743feb2d49STrond Myklebust 75963d8fe5STrond Myklebust void nfs_readdata_release(void *data) 761da177e4SLinus Torvalds { 771da177e4SLinus Torvalds nfs_readdata_free(data); 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static 811da177e4SLinus Torvalds unsigned int nfs_page_length(struct inode *inode, struct page *page) 821da177e4SLinus Torvalds { 831da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 841da177e4SLinus Torvalds unsigned long idx; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds if (i_size <= 0) 871da177e4SLinus Torvalds return 0; 881da177e4SLinus Torvalds idx = (i_size - 1) >> PAGE_CACHE_SHIFT; 891da177e4SLinus Torvalds if (page->index > idx) 901da177e4SLinus Torvalds return 0; 911da177e4SLinus Torvalds if (page->index != idx) 921da177e4SLinus Torvalds return PAGE_CACHE_SIZE; 931da177e4SLinus Torvalds return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1)); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static 971da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 981da177e4SLinus Torvalds { 991da177e4SLinus Torvalds memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE); 1001da177e4SLinus Torvalds SetPageUptodate(page); 1011da177e4SLinus Torvalds unlock_page(page); 1021da177e4SLinus Torvalds return 0; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1051de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) 1061de3fc12STrond Myklebust { 1071de3fc12STrond Myklebust unsigned int remainder = data->args.count - data->res.count; 1081de3fc12STrond Myklebust unsigned int base = data->args.pgbase + data->res.count; 1091de3fc12STrond Myklebust unsigned int pglen; 1101de3fc12STrond Myklebust struct page **pages; 1111de3fc12STrond Myklebust 1121de3fc12STrond Myklebust if (data->res.eof == 0 || remainder == 0) 1131de3fc12STrond Myklebust return; 1141de3fc12STrond Myklebust /* 1151de3fc12STrond Myklebust * Note: "remainder" can never be negative, since we check for 1161de3fc12STrond Myklebust * this in the XDR code. 1171de3fc12STrond Myklebust */ 1181de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 1191de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 1201de3fc12STrond Myklebust pglen = PAGE_CACHE_SIZE - base; 12179558f36STrond Myklebust for (;;) { 12279558f36STrond Myklebust if (remainder <= pglen) { 1231de3fc12STrond Myklebust memclear_highpage_flush(*pages, base, remainder); 12479558f36STrond Myklebust break; 12579558f36STrond Myklebust } 12679558f36STrond Myklebust memclear_highpage_flush(*pages, base, pglen); 12779558f36STrond Myklebust pages++; 12879558f36STrond Myklebust remainder -= pglen; 12979558f36STrond Myklebust pglen = PAGE_CACHE_SIZE; 13079558f36STrond Myklebust base = 0; 13179558f36STrond Myklebust } 1321de3fc12STrond Myklebust } 1331de3fc12STrond Myklebust 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * Read a page synchronously. 1361da177e4SLinus Torvalds */ 1371da177e4SLinus Torvalds static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode, 1381da177e4SLinus Torvalds struct page *page) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds unsigned int rsize = NFS_SERVER(inode)->rsize; 1411da177e4SLinus Torvalds unsigned int count = PAGE_CACHE_SIZE; 1421da177e4SLinus Torvalds int result; 1431da177e4SLinus Torvalds struct nfs_read_data *rdata; 1441da177e4SLinus Torvalds 145e9f7bee1STrond Myklebust rdata = nfs_readdata_alloc(count); 1461da177e4SLinus Torvalds if (!rdata) 1471da177e4SLinus Torvalds return -ENOMEM; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds memset(rdata, 0, sizeof(*rdata)); 1501da177e4SLinus Torvalds rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); 1511da177e4SLinus Torvalds rdata->cred = ctx->cred; 1521da177e4SLinus Torvalds rdata->inode = inode; 1531da177e4SLinus Torvalds INIT_LIST_HEAD(&rdata->pages); 1541da177e4SLinus Torvalds rdata->args.fh = NFS_FH(inode); 1551da177e4SLinus Torvalds rdata->args.context = ctx; 1561da177e4SLinus Torvalds rdata->args.pages = &page; 1571da177e4SLinus Torvalds rdata->args.pgbase = 0UL; 1581da177e4SLinus Torvalds rdata->args.count = rsize; 1591da177e4SLinus Torvalds rdata->res.fattr = &rdata->fattr; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds dprintk("NFS: nfs_readpage_sync(%p)\n", page); 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * This works now because the socket layer never tries to DMA 1651da177e4SLinus Torvalds * into this buffer directly. 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds do { 1681da177e4SLinus Torvalds if (count < rsize) 1691da177e4SLinus Torvalds rdata->args.count = count; 1701da177e4SLinus Torvalds rdata->res.count = rdata->args.count; 1711da177e4SLinus Torvalds rdata->args.offset = page_offset(page) + rdata->args.pgbase; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n", 17454ceac45SDavid Howells NFS_SERVER(inode)->nfs_client->cl_hostname, 1751da177e4SLinus Torvalds inode->i_sb->s_id, 1761da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 1771da177e4SLinus Torvalds (unsigned long long)rdata->args.pgbase, 1781da177e4SLinus Torvalds rdata->args.count); 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds lock_kernel(); 1811da177e4SLinus Torvalds result = NFS_PROTO(inode)->read(rdata); 1821da177e4SLinus Torvalds unlock_kernel(); 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* 1851da177e4SLinus Torvalds * Even if we had a partial success we can't mark the page 1861da177e4SLinus Torvalds * cache valid. 1871da177e4SLinus Torvalds */ 1881da177e4SLinus Torvalds if (result < 0) { 1891da177e4SLinus Torvalds if (result == -EISDIR) 1901da177e4SLinus Torvalds result = -EINVAL; 1911da177e4SLinus Torvalds goto io_error; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds count -= result; 1941da177e4SLinus Torvalds rdata->args.pgbase += result; 19591d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result); 19691d5b470SChuck Lever 1971da177e4SLinus Torvalds /* Note: result == 0 should only happen if we're caching 1981da177e4SLinus Torvalds * a write that extends the file and punches a hole. 1991da177e4SLinus Torvalds */ 2001da177e4SLinus Torvalds if (rdata->res.eof != 0 || result == 0) 2011da177e4SLinus Torvalds break; 2021da177e4SLinus Torvalds } while (count); 203dc59250cSChuck Lever spin_lock(&inode->i_lock); 20455296809SChuck Lever NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME; 205dc59250cSChuck Lever spin_unlock(&inode->i_lock); 2061da177e4SLinus Torvalds 2077a524111STrond Myklebust if (rdata->res.eof || rdata->res.count == rdata->args.count) { 2081da177e4SLinus Torvalds SetPageUptodate(page); 2097a524111STrond Myklebust if (rdata->res.eof && count != 0) 2107a524111STrond Myklebust memclear_highpage_flush(page, rdata->args.pgbase, count); 2117a524111STrond Myklebust } 2121da177e4SLinus Torvalds result = 0; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds io_error: 2151da177e4SLinus Torvalds unlock_page(page); 2161da177e4SLinus Torvalds nfs_readdata_free(rdata); 2171da177e4SLinus Torvalds return result; 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 2211da177e4SLinus Torvalds struct page *page) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds LIST_HEAD(one_request); 2241da177e4SLinus Torvalds struct nfs_page *new; 2251da177e4SLinus Torvalds unsigned int len; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds len = nfs_page_length(inode, page); 2281da177e4SLinus Torvalds if (len == 0) 2291da177e4SLinus Torvalds return nfs_return_empty_page(page); 2301da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 2311da177e4SLinus Torvalds if (IS_ERR(new)) { 2321da177e4SLinus Torvalds unlock_page(page); 2331da177e4SLinus Torvalds return PTR_ERR(new); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 2361da177e4SLinus Torvalds memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds nfs_list_add_request(new, &one_request); 2391da177e4SLinus Torvalds nfs_pagein_one(&one_request, inode); 2401da177e4SLinus Torvalds return 0; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds unlock_page(req->wb_page); 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 2481da177e4SLinus Torvalds req->wb_context->dentry->d_inode->i_sb->s_id, 2491da177e4SLinus Torvalds (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 2501da177e4SLinus Torvalds req->wb_bytes, 2511da177e4SLinus Torvalds (long long)req_offset(req)); 25210d2c46fSNick Wilson nfs_clear_request(req); 25310d2c46fSNick Wilson nfs_release_request(req); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* 2571da177e4SLinus Torvalds * Set up the NFS read request struct 2581da177e4SLinus Torvalds */ 2591da177e4SLinus Torvalds static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 260ec06c096STrond Myklebust const struct rpc_call_ops *call_ops, 2611da177e4SLinus Torvalds unsigned int count, unsigned int offset) 2621da177e4SLinus Torvalds { 2631da177e4SLinus Torvalds struct inode *inode; 264ec06c096STrond Myklebust int flags; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds data->req = req; 2671da177e4SLinus Torvalds data->inode = inode = req->wb_context->dentry->d_inode; 2681da177e4SLinus Torvalds data->cred = req->wb_context->cred; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 2711da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 2721da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 2731da177e4SLinus Torvalds data->args.pages = data->pagevec; 2741da177e4SLinus Torvalds data->args.count = count; 2751da177e4SLinus Torvalds data->args.context = req->wb_context; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds data->res.fattr = &data->fattr; 2781da177e4SLinus Torvalds data->res.count = count; 2791da177e4SLinus Torvalds data->res.eof = 0; 2800e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 2811da177e4SLinus Torvalds 282ec06c096STrond Myklebust /* Set up the initial task struct. */ 283ec06c096STrond Myklebust flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); 284ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); 2851da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long)inode; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 2901da177e4SLinus Torvalds data->task.tk_pid, 2911da177e4SLinus Torvalds inode->i_sb->s_id, 2921da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 2931da177e4SLinus Torvalds count, 2941da177e4SLinus Torvalds (unsigned long long)data->args.offset); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds static void 2981da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds struct nfs_page *req; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds while (!list_empty(head)) { 3031da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3041da177e4SLinus Torvalds nfs_list_remove_request(req); 3051da177e4SLinus Torvalds SetPageError(req->wb_page); 3061da177e4SLinus Torvalds nfs_readpage_release(req); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* 3111da177e4SLinus Torvalds * Start an async read operation 3121da177e4SLinus Torvalds */ 3131da177e4SLinus Torvalds static void nfs_execute_read(struct nfs_read_data *data) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(data->inode); 3161da177e4SLinus Torvalds sigset_t oldset; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 3191da177e4SLinus Torvalds lock_kernel(); 3201da177e4SLinus Torvalds rpc_execute(&data->task); 3211da177e4SLinus Torvalds unlock_kernel(); 3221da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* 3261da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 3271da177e4SLinus Torvalds * 3281da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 3291da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 3301da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 3311da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 3321da177e4SLinus Torvalds * 3331da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 3341da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 3351da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 3361da177e4SLinus Torvalds * or less conventional NFS client behavior. 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds static int nfs_pagein_multi(struct list_head *head, struct inode *inode) 3391da177e4SLinus Torvalds { 3401da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 3411da177e4SLinus Torvalds struct page *page = req->wb_page; 3421da177e4SLinus Torvalds struct nfs_read_data *data; 343e9f7bee1STrond Myklebust size_t rsize = NFS_SERVER(inode)->rsize, nbytes; 344e9f7bee1STrond Myklebust unsigned int offset; 3451da177e4SLinus Torvalds int requests = 0; 3461da177e4SLinus Torvalds LIST_HEAD(list); 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds nfs_list_remove_request(req); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds nbytes = req->wb_bytes; 351e9f7bee1STrond Myklebust do { 352e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 353e9f7bee1STrond Myklebust 354e9f7bee1STrond Myklebust data = nfs_readdata_alloc(len); 3551da177e4SLinus Torvalds if (!data) 3561da177e4SLinus Torvalds goto out_bad; 3571da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 3581da177e4SLinus Torvalds list_add(&data->pages, &list); 3591da177e4SLinus Torvalds requests++; 360e9f7bee1STrond Myklebust nbytes -= len; 361e9f7bee1STrond Myklebust } while(nbytes != 0); 3621da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds ClearPageError(page); 3651da177e4SLinus Torvalds offset = 0; 3661da177e4SLinus Torvalds nbytes = req->wb_bytes; 3671da177e4SLinus Torvalds do { 3681da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3691da177e4SLinus Torvalds list_del_init(&data->pages); 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds data->pagevec[0] = page; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds if (nbytes > rsize) { 374ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 375ec06c096STrond Myklebust rsize, offset); 3761da177e4SLinus Torvalds offset += rsize; 3771da177e4SLinus Torvalds nbytes -= rsize; 3781da177e4SLinus Torvalds } else { 379ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 380ec06c096STrond Myklebust nbytes, offset); 3811da177e4SLinus Torvalds nbytes = 0; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds nfs_execute_read(data); 3841da177e4SLinus Torvalds } while (nbytes != 0); 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds return 0; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds out_bad: 3891da177e4SLinus Torvalds while (!list_empty(&list)) { 3901da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3911da177e4SLinus Torvalds list_del(&data->pages); 3921da177e4SLinus Torvalds nfs_readdata_free(data); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds SetPageError(page); 3951da177e4SLinus Torvalds nfs_readpage_release(req); 3961da177e4SLinus Torvalds return -ENOMEM; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds static int nfs_pagein_one(struct list_head *head, struct inode *inode) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds struct nfs_page *req; 4021da177e4SLinus Torvalds struct page **pages; 4031da177e4SLinus Torvalds struct nfs_read_data *data; 4041da177e4SLinus Torvalds unsigned int count; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) 4071da177e4SLinus Torvalds return nfs_pagein_multi(head, inode); 4081da177e4SLinus Torvalds 409e9f7bee1STrond Myklebust data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize); 4101da177e4SLinus Torvalds if (!data) 4111da177e4SLinus Torvalds goto out_bad; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 4141da177e4SLinus Torvalds pages = data->pagevec; 4151da177e4SLinus Torvalds count = 0; 4161da177e4SLinus Torvalds while (!list_empty(head)) { 4171da177e4SLinus Torvalds req = nfs_list_entry(head->next); 4181da177e4SLinus Torvalds nfs_list_remove_request(req); 4191da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 4201da177e4SLinus Torvalds ClearPageError(req->wb_page); 4211da177e4SLinus Torvalds *pages++ = req->wb_page; 4221da177e4SLinus Torvalds count += req->wb_bytes; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 4251da177e4SLinus Torvalds 426ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds nfs_execute_read(data); 4291da177e4SLinus Torvalds return 0; 4301da177e4SLinus Torvalds out_bad: 4311da177e4SLinus Torvalds nfs_async_read_error(head); 4321da177e4SLinus Torvalds return -ENOMEM; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds static int 4361da177e4SLinus Torvalds nfs_pagein_list(struct list_head *head, int rpages) 4371da177e4SLinus Torvalds { 4381da177e4SLinus Torvalds LIST_HEAD(one_request); 4391da177e4SLinus Torvalds struct nfs_page *req; 4401da177e4SLinus Torvalds int error = 0; 4411da177e4SLinus Torvalds unsigned int pages = 0; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds while (!list_empty(head)) { 4441da177e4SLinus Torvalds pages += nfs_coalesce_requests(head, &one_request, rpages); 4451da177e4SLinus Torvalds req = nfs_list_entry(one_request.next); 4461da177e4SLinus Torvalds error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode); 4471da177e4SLinus Torvalds if (error < 0) 4481da177e4SLinus Torvalds break; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds if (error >= 0) 4511da177e4SLinus Torvalds return pages; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds nfs_async_read_error(head); 4541da177e4SLinus Torvalds return error; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /* 4581da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 4591da177e4SLinus Torvalds */ 460ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 4611da177e4SLinus Torvalds { 462ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 4631da177e4SLinus Torvalds struct nfs_page *req = data->req; 4641da177e4SLinus Torvalds struct page *page = req->wb_page; 4651da177e4SLinus Torvalds 4661de3fc12STrond Myklebust if (likely(task->tk_status >= 0)) 4671de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 4681de3fc12STrond Myklebust else 4691de3fc12STrond Myklebust SetPageError(page); 470ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 471ec06c096STrond Myklebust return; 4721da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 4731da177e4SLinus Torvalds if (!PageError(page)) 4741da177e4SLinus Torvalds SetPageUptodate(page); 4751da177e4SLinus Torvalds nfs_readpage_release(req); 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 479ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 480ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 481ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 482ec06c096STrond Myklebust }; 483ec06c096STrond Myklebust 4841de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 4851de3fc12STrond Myklebust { 4861de3fc12STrond Myklebust unsigned int count = data->res.count; 4871de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 4881de3fc12STrond Myklebust struct page **pages; 4891de3fc12STrond Myklebust 49079558f36STrond Myklebust if (data->res.eof) 49179558f36STrond Myklebust count = data->args.count; 4921de3fc12STrond Myklebust if (unlikely(count == 0)) 4931de3fc12STrond Myklebust return; 4941de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 4951de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 4961de3fc12STrond Myklebust count += base; 4971de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 4981de3fc12STrond Myklebust SetPageUptodate(*pages); 49979558f36STrond Myklebust if (count != 0) 5001de3fc12STrond Myklebust SetPageUptodate(*pages); 5011de3fc12STrond Myklebust } 5021de3fc12STrond Myklebust 5031de3fc12STrond Myklebust static void nfs_readpage_set_pages_error(struct nfs_read_data *data) 5041de3fc12STrond Myklebust { 5051de3fc12STrond Myklebust unsigned int count = data->args.count; 5061de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 5071de3fc12STrond Myklebust struct page **pages; 5081de3fc12STrond Myklebust 5091de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 5101de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 5111de3fc12STrond Myklebust count += base; 5121de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 5131de3fc12STrond Myklebust SetPageError(*pages); 51479558f36STrond Myklebust if (count != 0) 51579558f36STrond Myklebust SetPageError(*pages); 5161de3fc12STrond Myklebust } 5171de3fc12STrond Myklebust 5181da177e4SLinus Torvalds /* 5191da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 5201da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 5211da177e4SLinus Torvalds */ 522ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 5231da177e4SLinus Torvalds { 524ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 5251da177e4SLinus Torvalds 5261de3fc12STrond Myklebust /* 5271de3fc12STrond Myklebust * Note: nfs_readpage_result may change the values of 5281de3fc12STrond Myklebust * data->args. In the multi-page case, we therefore need 5291de3fc12STrond Myklebust * to ensure that we call the next nfs_readpage_set_page_uptodate() 5301de3fc12STrond Myklebust * first in the multi-page case. 5311de3fc12STrond Myklebust */ 5321de3fc12STrond Myklebust if (likely(task->tk_status >= 0)) { 5331de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 5341de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 5351de3fc12STrond Myklebust } else 5361de3fc12STrond Myklebust nfs_readpage_set_pages_error(data); 537ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 538ec06c096STrond Myklebust return; 5391da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 5401da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 5411da177e4SLinus Torvalds 5421de3fc12STrond Myklebust nfs_list_remove_request(req); 5431da177e4SLinus Torvalds nfs_readpage_release(req); 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 547ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 548ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 549ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 550ec06c096STrond Myklebust }; 551ec06c096STrond Myklebust 5521da177e4SLinus Torvalds /* 5531da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 5541da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 5551da177e4SLinus Torvalds */ 556ec06c096STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds struct nfs_readargs *argp = &data->args; 5591da177e4SLinus Torvalds struct nfs_readres *resp = &data->res; 560ec06c096STrond Myklebust int status; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds dprintk("NFS: %4d nfs_readpage_result, (status %d)\n", 563ec06c096STrond Myklebust task->tk_pid, task->tk_status); 564ec06c096STrond Myklebust 565ec06c096STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 566ec06c096STrond Myklebust if (status != 0) 567ec06c096STrond Myklebust return status; 5681da177e4SLinus Torvalds 56991d5b470SChuck Lever nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count); 57091d5b470SChuck Lever 5715f004cf2STrond Myklebust if (task->tk_status < 0) { 5725f004cf2STrond Myklebust if (task->tk_status == -ESTALE) { 5735f004cf2STrond Myklebust set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode)); 5745f004cf2STrond Myklebust nfs_mark_for_revalidate(data->inode); 5755f004cf2STrond Myklebust } 5765f004cf2STrond Myklebust } else if (resp->count < argp->count && !resp->eof) { 5775f004cf2STrond Myklebust /* This is a short read! */ 57891d5b470SChuck Lever nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 5791da177e4SLinus Torvalds /* Has the server at least made some progress? */ 5801da177e4SLinus Torvalds if (resp->count != 0) { 5811da177e4SLinus Torvalds /* Yes, so retry the read at the end of the data */ 5821da177e4SLinus Torvalds argp->offset += resp->count; 5831da177e4SLinus Torvalds argp->pgbase += resp->count; 5841da177e4SLinus Torvalds argp->count -= resp->count; 5851da177e4SLinus Torvalds rpc_restart_call(task); 586ec06c096STrond Myklebust return -EAGAIN; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds task->tk_status = -EIO; 5891da177e4SLinus Torvalds } 590dc59250cSChuck Lever spin_lock(&data->inode->i_lock); 59155296809SChuck Lever NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME; 592dc59250cSChuck Lever spin_unlock(&data->inode->i_lock); 593ec06c096STrond Myklebust return 0; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds /* 5971da177e4SLinus Torvalds * Read a page over NFS. 5981da177e4SLinus Torvalds * We read the page synchronously in the following case: 5991da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 6001da177e4SLinus Torvalds * previous async read operation failed. 6011da177e4SLinus Torvalds */ 6021da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 6031da177e4SLinus Torvalds { 6041da177e4SLinus Torvalds struct nfs_open_context *ctx; 6051da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 6061da177e4SLinus Torvalds int error; 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 6091da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 61091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 61191d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 61291d5b470SChuck Lever 6131da177e4SLinus Torvalds /* 6141da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 6151da177e4SLinus Torvalds * 6161da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 6171da177e4SLinus Torvalds * be any new pending writes generated at this point 6181da177e4SLinus Torvalds * for this page (other pages can be written to). 6191da177e4SLinus Torvalds */ 6201da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 6211da177e4SLinus Torvalds if (error) 6221da177e4SLinus Torvalds goto out_error; 6231da177e4SLinus Torvalds 6245f004cf2STrond Myklebust error = -ESTALE; 6255f004cf2STrond Myklebust if (NFS_STALE(inode)) 6265f004cf2STrond Myklebust goto out_error; 6275f004cf2STrond Myklebust 6281da177e4SLinus Torvalds if (file == NULL) { 629d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 6301da177e4SLinus Torvalds if (ctx == NULL) 6311da177e4SLinus Torvalds return -EBADF; 6321da177e4SLinus Torvalds } else 6331da177e4SLinus Torvalds ctx = get_nfs_open_context((struct nfs_open_context *) 6341da177e4SLinus Torvalds file->private_data); 6351da177e4SLinus Torvalds if (!IS_SYNC(inode)) { 6361da177e4SLinus Torvalds error = nfs_readpage_async(ctx, inode, page); 6371da177e4SLinus Torvalds goto out; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds error = nfs_readpage_sync(ctx, inode, page); 6411da177e4SLinus Torvalds if (error < 0 && IS_SWAPFILE(inode)) 6421da177e4SLinus Torvalds printk("Aiee.. nfs swap-in of page failed!\n"); 6431da177e4SLinus Torvalds out: 6441da177e4SLinus Torvalds put_nfs_open_context(ctx); 6451da177e4SLinus Torvalds return error; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds out_error: 6481da177e4SLinus Torvalds unlock_page(page); 6491da177e4SLinus Torvalds return error; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds struct nfs_readdesc { 6531da177e4SLinus Torvalds struct list_head *head; 6541da177e4SLinus Torvalds struct nfs_open_context *ctx; 6551da177e4SLinus Torvalds }; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds static int 6581da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 6611da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 6621da177e4SLinus Torvalds struct nfs_page *new; 6631da177e4SLinus Torvalds unsigned int len; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds nfs_wb_page(inode, page); 6661da177e4SLinus Torvalds len = nfs_page_length(inode, page); 6671da177e4SLinus Torvalds if (len == 0) 6681da177e4SLinus Torvalds return nfs_return_empty_page(page); 6691da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 6701da177e4SLinus Torvalds if (IS_ERR(new)) { 6711da177e4SLinus Torvalds SetPageError(page); 6721da177e4SLinus Torvalds unlock_page(page); 6731da177e4SLinus Torvalds return PTR_ERR(new); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 6761da177e4SLinus Torvalds memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); 6771da177e4SLinus Torvalds nfs_list_add_request(new, desc->head); 6781da177e4SLinus Torvalds return 0; 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 6821da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 6831da177e4SLinus Torvalds { 6841da177e4SLinus Torvalds LIST_HEAD(head); 6851da177e4SLinus Torvalds struct nfs_readdesc desc = { 6861da177e4SLinus Torvalds .head = &head, 6871da177e4SLinus Torvalds }; 6881da177e4SLinus Torvalds struct inode *inode = mapping->host; 6891da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 6905f004cf2STrond Myklebust int ret = -ESTALE; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 6931da177e4SLinus Torvalds inode->i_sb->s_id, 6941da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 6951da177e4SLinus Torvalds nr_pages); 69691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 6971da177e4SLinus Torvalds 6985f004cf2STrond Myklebust if (NFS_STALE(inode)) 6995f004cf2STrond Myklebust goto out; 7005f004cf2STrond Myklebust 7011da177e4SLinus Torvalds if (filp == NULL) { 702d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 7031da177e4SLinus Torvalds if (desc.ctx == NULL) 7041da177e4SLinus Torvalds return -EBADF; 7051da177e4SLinus Torvalds } else 7061da177e4SLinus Torvalds desc.ctx = get_nfs_open_context((struct nfs_open_context *) 7071da177e4SLinus Torvalds filp->private_data); 7081da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 7091da177e4SLinus Torvalds if (!list_empty(&head)) { 7101da177e4SLinus Torvalds int err = nfs_pagein_list(&head, server->rpages); 7111da177e4SLinus Torvalds if (!ret) 71291d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, err); 7131da177e4SLinus Torvalds ret = err; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 7165f004cf2STrond Myklebust out: 7171da177e4SLinus Torvalds return ret; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 720f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 7211da177e4SLinus Torvalds { 7221da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 7231da177e4SLinus Torvalds sizeof(struct nfs_read_data), 7241da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 7251da177e4SLinus Torvalds NULL, NULL); 7261da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 7271da177e4SLinus Torvalds return -ENOMEM; 7281da177e4SLinus Torvalds 72993d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 7301da177e4SLinus Torvalds nfs_rdata_cachep); 7311da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 7321da177e4SLinus Torvalds return -ENOMEM; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds return 0; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 737266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 7401da177e4SLinus Torvalds if (kmem_cache_destroy(nfs_rdata_cachep)) 7411da177e4SLinus Torvalds printk(KERN_INFO "nfs_read_data: not all structures were freed\n"); 7421da177e4SLinus Torvalds } 743