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 33*49a70f27STrond Myklebust #include "internal.h" 3491d5b470SChuck Lever #include "iostat.h" 3591d5b470SChuck Lever 361da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds static int nfs_pagein_one(struct list_head *, struct inode *); 39ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops; 40ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds static kmem_cache_t *nfs_rdata_cachep; 433feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #define MIN_POOL_READ (32) 461da177e4SLinus Torvalds 47e9f7bee1STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(size_t len) 483feb2d49STrond Myklebust { 49e9f7bee1STrond Myklebust unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 503feb2d49STrond Myklebust struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS); 513feb2d49STrond Myklebust 523feb2d49STrond Myklebust if (p) { 533feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 543feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 55e9f7bee1STrond Myklebust p->npages = pagecount; 560d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 570d0b5cb3SChuck Lever p->pagevec = p->page_array; 583feb2d49STrond Myklebust else { 590d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 600d0b5cb3SChuck Lever if (!p->pagevec) { 613feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 623feb2d49STrond Myklebust p = NULL; 633feb2d49STrond Myklebust } 643feb2d49STrond Myklebust } 653feb2d49STrond Myklebust } 663feb2d49STrond Myklebust return p; 673feb2d49STrond Myklebust } 683feb2d49STrond Myklebust 698aca67f0STrond Myklebust static void nfs_readdata_rcu_free(struct rcu_head *head) 703feb2d49STrond Myklebust { 718aca67f0STrond Myklebust struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu); 723feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 733feb2d49STrond Myklebust kfree(p->pagevec); 743feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 753feb2d49STrond Myklebust } 763feb2d49STrond Myklebust 778aca67f0STrond Myklebust static void nfs_readdata_free(struct nfs_read_data *rdata) 788aca67f0STrond Myklebust { 798aca67f0STrond Myklebust call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free); 808aca67f0STrond Myklebust } 818aca67f0STrond Myklebust 82963d8fe5STrond Myklebust void nfs_readdata_release(void *data) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds nfs_readdata_free(data); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds static 881da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE); 911da177e4SLinus Torvalds SetPageUptodate(page); 921da177e4SLinus Torvalds unlock_page(page); 931da177e4SLinus Torvalds return 0; 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) 971de3fc12STrond Myklebust { 981de3fc12STrond Myklebust unsigned int remainder = data->args.count - data->res.count; 991de3fc12STrond Myklebust unsigned int base = data->args.pgbase + data->res.count; 1001de3fc12STrond Myklebust unsigned int pglen; 1011de3fc12STrond Myklebust struct page **pages; 1021de3fc12STrond Myklebust 1031de3fc12STrond Myklebust if (data->res.eof == 0 || remainder == 0) 1041de3fc12STrond Myklebust return; 1051de3fc12STrond Myklebust /* 1061de3fc12STrond Myklebust * Note: "remainder" can never be negative, since we check for 1071de3fc12STrond Myklebust * this in the XDR code. 1081de3fc12STrond Myklebust */ 1091de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 1101de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 1111de3fc12STrond Myklebust pglen = PAGE_CACHE_SIZE - base; 11279558f36STrond Myklebust for (;;) { 11379558f36STrond Myklebust if (remainder <= pglen) { 1141de3fc12STrond Myklebust memclear_highpage_flush(*pages, base, remainder); 11579558f36STrond Myklebust break; 11679558f36STrond Myklebust } 11779558f36STrond Myklebust memclear_highpage_flush(*pages, base, pglen); 11879558f36STrond Myklebust pages++; 11979558f36STrond Myklebust remainder -= pglen; 12079558f36STrond Myklebust pglen = PAGE_CACHE_SIZE; 12179558f36STrond Myklebust base = 0; 12279558f36STrond Myklebust } 1231de3fc12STrond Myklebust } 1241de3fc12STrond Myklebust 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * Read a page synchronously. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode, 1291da177e4SLinus Torvalds struct page *page) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds unsigned int rsize = NFS_SERVER(inode)->rsize; 1321da177e4SLinus Torvalds unsigned int count = PAGE_CACHE_SIZE; 133cf1308ffSTrond Myklebust int result = -ENOMEM; 1341da177e4SLinus Torvalds struct nfs_read_data *rdata; 1351da177e4SLinus Torvalds 136e9f7bee1STrond Myklebust rdata = nfs_readdata_alloc(count); 1371da177e4SLinus Torvalds if (!rdata) 138cf1308ffSTrond Myklebust goto out_unlock; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds memset(rdata, 0, sizeof(*rdata)); 1411da177e4SLinus Torvalds rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); 1421da177e4SLinus Torvalds rdata->cred = ctx->cred; 1431da177e4SLinus Torvalds rdata->inode = inode; 1441da177e4SLinus Torvalds INIT_LIST_HEAD(&rdata->pages); 1451da177e4SLinus Torvalds rdata->args.fh = NFS_FH(inode); 1461da177e4SLinus Torvalds rdata->args.context = ctx; 1471da177e4SLinus Torvalds rdata->args.pages = &page; 1481da177e4SLinus Torvalds rdata->args.pgbase = 0UL; 1491da177e4SLinus Torvalds rdata->args.count = rsize; 1501da177e4SLinus Torvalds rdata->res.fattr = &rdata->fattr; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds dprintk("NFS: nfs_readpage_sync(%p)\n", page); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 1551da177e4SLinus Torvalds * This works now because the socket layer never tries to DMA 1561da177e4SLinus Torvalds * into this buffer directly. 1571da177e4SLinus Torvalds */ 1581da177e4SLinus Torvalds do { 1591da177e4SLinus Torvalds if (count < rsize) 1601da177e4SLinus Torvalds rdata->args.count = count; 1611da177e4SLinus Torvalds rdata->res.count = rdata->args.count; 1621da177e4SLinus Torvalds rdata->args.offset = page_offset(page) + rdata->args.pgbase; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n", 16554ceac45SDavid Howells NFS_SERVER(inode)->nfs_client->cl_hostname, 1661da177e4SLinus Torvalds inode->i_sb->s_id, 1671da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 1681da177e4SLinus Torvalds (unsigned long long)rdata->args.pgbase, 1691da177e4SLinus Torvalds rdata->args.count); 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds lock_kernel(); 1721da177e4SLinus Torvalds result = NFS_PROTO(inode)->read(rdata); 1731da177e4SLinus Torvalds unlock_kernel(); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /* 1761da177e4SLinus Torvalds * Even if we had a partial success we can't mark the page 1771da177e4SLinus Torvalds * cache valid. 1781da177e4SLinus Torvalds */ 1791da177e4SLinus Torvalds if (result < 0) { 1801da177e4SLinus Torvalds if (result == -EISDIR) 1811da177e4SLinus Torvalds result = -EINVAL; 1821da177e4SLinus Torvalds goto io_error; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds count -= result; 1851da177e4SLinus Torvalds rdata->args.pgbase += result; 18691d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result); 18791d5b470SChuck Lever 1881da177e4SLinus Torvalds /* Note: result == 0 should only happen if we're caching 1891da177e4SLinus Torvalds * a write that extends the file and punches a hole. 1901da177e4SLinus Torvalds */ 1911da177e4SLinus Torvalds if (rdata->res.eof != 0 || result == 0) 1921da177e4SLinus Torvalds break; 1931da177e4SLinus Torvalds } while (count); 194dc59250cSChuck Lever spin_lock(&inode->i_lock); 19555296809SChuck Lever NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME; 196dc59250cSChuck Lever spin_unlock(&inode->i_lock); 1971da177e4SLinus Torvalds 1987a524111STrond Myklebust if (rdata->res.eof || rdata->res.count == rdata->args.count) { 1991da177e4SLinus Torvalds SetPageUptodate(page); 2007a524111STrond Myklebust if (rdata->res.eof && count != 0) 2017a524111STrond Myklebust memclear_highpage_flush(page, rdata->args.pgbase, count); 2027a524111STrond Myklebust } 2031da177e4SLinus Torvalds result = 0; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds io_error: 2061da177e4SLinus Torvalds nfs_readdata_free(rdata); 207cf1308ffSTrond Myklebust out_unlock: 208cf1308ffSTrond Myklebust unlock_page(page); 2091da177e4SLinus Torvalds return result; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 2131da177e4SLinus Torvalds struct page *page) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds LIST_HEAD(one_request); 2161da177e4SLinus Torvalds struct nfs_page *new; 2171da177e4SLinus Torvalds unsigned int len; 2181da177e4SLinus Torvalds 219*49a70f27STrond Myklebust len = nfs_page_length(page); 2201da177e4SLinus Torvalds if (len == 0) 2211da177e4SLinus Torvalds return nfs_return_empty_page(page); 2221da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 2231da177e4SLinus Torvalds if (IS_ERR(new)) { 2241da177e4SLinus Torvalds unlock_page(page); 2251da177e4SLinus Torvalds return PTR_ERR(new); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 2281da177e4SLinus Torvalds memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds nfs_list_add_request(new, &one_request); 2311da177e4SLinus Torvalds nfs_pagein_one(&one_request, inode); 2321da177e4SLinus Torvalds return 0; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds unlock_page(req->wb_page); 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 2401da177e4SLinus Torvalds req->wb_context->dentry->d_inode->i_sb->s_id, 2411da177e4SLinus Torvalds (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 2421da177e4SLinus Torvalds req->wb_bytes, 2431da177e4SLinus Torvalds (long long)req_offset(req)); 24410d2c46fSNick Wilson nfs_clear_request(req); 24510d2c46fSNick Wilson nfs_release_request(req); 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds /* 2491da177e4SLinus Torvalds * Set up the NFS read request struct 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 252ec06c096STrond Myklebust const struct rpc_call_ops *call_ops, 2531da177e4SLinus Torvalds unsigned int count, unsigned int offset) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds struct inode *inode; 256ec06c096STrond Myklebust int flags; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds data->req = req; 2591da177e4SLinus Torvalds data->inode = inode = req->wb_context->dentry->d_inode; 2601da177e4SLinus Torvalds data->cred = req->wb_context->cred; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 2631da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 2641da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 2651da177e4SLinus Torvalds data->args.pages = data->pagevec; 2661da177e4SLinus Torvalds data->args.count = count; 2671da177e4SLinus Torvalds data->args.context = req->wb_context; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds data->res.fattr = &data->fattr; 2701da177e4SLinus Torvalds data->res.count = count; 2711da177e4SLinus Torvalds data->res.eof = 0; 2720e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 2731da177e4SLinus Torvalds 274ec06c096STrond Myklebust /* Set up the initial task struct. */ 275ec06c096STrond Myklebust flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); 276ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); 2771da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long)inode; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 2821da177e4SLinus Torvalds data->task.tk_pid, 2831da177e4SLinus Torvalds inode->i_sb->s_id, 2841da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 2851da177e4SLinus Torvalds count, 2861da177e4SLinus Torvalds (unsigned long long)data->args.offset); 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static void 2901da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct nfs_page *req; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds while (!list_empty(head)) { 2951da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2961da177e4SLinus Torvalds nfs_list_remove_request(req); 2971da177e4SLinus Torvalds SetPageError(req->wb_page); 2981da177e4SLinus Torvalds nfs_readpage_release(req); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds /* 3031da177e4SLinus Torvalds * Start an async read operation 3041da177e4SLinus Torvalds */ 3051da177e4SLinus Torvalds static void nfs_execute_read(struct nfs_read_data *data) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(data->inode); 3081da177e4SLinus Torvalds sigset_t oldset; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 3111da177e4SLinus Torvalds rpc_execute(&data->task); 3121da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 3161da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 3171da177e4SLinus Torvalds * 3181da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 3191da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 3201da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 3211da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 3221da177e4SLinus Torvalds * 3231da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 3241da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 3251da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 3261da177e4SLinus Torvalds * or less conventional NFS client behavior. 3271da177e4SLinus Torvalds */ 3281da177e4SLinus Torvalds static int nfs_pagein_multi(struct list_head *head, struct inode *inode) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 3311da177e4SLinus Torvalds struct page *page = req->wb_page; 3321da177e4SLinus Torvalds struct nfs_read_data *data; 333e9f7bee1STrond Myklebust size_t rsize = NFS_SERVER(inode)->rsize, nbytes; 334e9f7bee1STrond Myklebust unsigned int offset; 3351da177e4SLinus Torvalds int requests = 0; 3361da177e4SLinus Torvalds LIST_HEAD(list); 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds nfs_list_remove_request(req); 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds nbytes = req->wb_bytes; 341e9f7bee1STrond Myklebust do { 342e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 343e9f7bee1STrond Myklebust 344e9f7bee1STrond Myklebust data = nfs_readdata_alloc(len); 3451da177e4SLinus Torvalds if (!data) 3461da177e4SLinus Torvalds goto out_bad; 3471da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 3481da177e4SLinus Torvalds list_add(&data->pages, &list); 3491da177e4SLinus Torvalds requests++; 350e9f7bee1STrond Myklebust nbytes -= len; 351e9f7bee1STrond Myklebust } while(nbytes != 0); 3521da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds ClearPageError(page); 3551da177e4SLinus Torvalds offset = 0; 3561da177e4SLinus Torvalds nbytes = req->wb_bytes; 3571da177e4SLinus Torvalds do { 3581da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3591da177e4SLinus Torvalds list_del_init(&data->pages); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds data->pagevec[0] = page; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds if (nbytes > rsize) { 364ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 365ec06c096STrond Myklebust rsize, offset); 3661da177e4SLinus Torvalds offset += rsize; 3671da177e4SLinus Torvalds nbytes -= rsize; 3681da177e4SLinus Torvalds } else { 369ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 370ec06c096STrond Myklebust nbytes, offset); 3711da177e4SLinus Torvalds nbytes = 0; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds nfs_execute_read(data); 3741da177e4SLinus Torvalds } while (nbytes != 0); 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds return 0; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds out_bad: 3791da177e4SLinus Torvalds while (!list_empty(&list)) { 3801da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3811da177e4SLinus Torvalds list_del(&data->pages); 3821da177e4SLinus Torvalds nfs_readdata_free(data); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds SetPageError(page); 3851da177e4SLinus Torvalds nfs_readpage_release(req); 3861da177e4SLinus Torvalds return -ENOMEM; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds static int nfs_pagein_one(struct list_head *head, struct inode *inode) 3901da177e4SLinus Torvalds { 3911da177e4SLinus Torvalds struct nfs_page *req; 3921da177e4SLinus Torvalds struct page **pages; 3931da177e4SLinus Torvalds struct nfs_read_data *data; 3941da177e4SLinus Torvalds unsigned int count; 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) 3971da177e4SLinus Torvalds return nfs_pagein_multi(head, inode); 3981da177e4SLinus Torvalds 399e9f7bee1STrond Myklebust data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize); 4001da177e4SLinus Torvalds if (!data) 4011da177e4SLinus Torvalds goto out_bad; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 4041da177e4SLinus Torvalds pages = data->pagevec; 4051da177e4SLinus Torvalds count = 0; 4061da177e4SLinus Torvalds while (!list_empty(head)) { 4071da177e4SLinus Torvalds req = nfs_list_entry(head->next); 4081da177e4SLinus Torvalds nfs_list_remove_request(req); 4091da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 4101da177e4SLinus Torvalds ClearPageError(req->wb_page); 4111da177e4SLinus Torvalds *pages++ = req->wb_page; 4121da177e4SLinus Torvalds count += req->wb_bytes; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 4151da177e4SLinus Torvalds 416ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds nfs_execute_read(data); 4191da177e4SLinus Torvalds return 0; 4201da177e4SLinus Torvalds out_bad: 4211da177e4SLinus Torvalds nfs_async_read_error(head); 4221da177e4SLinus Torvalds return -ENOMEM; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds static int 4261da177e4SLinus Torvalds nfs_pagein_list(struct list_head *head, int rpages) 4271da177e4SLinus Torvalds { 4281da177e4SLinus Torvalds LIST_HEAD(one_request); 4291da177e4SLinus Torvalds struct nfs_page *req; 4301da177e4SLinus Torvalds int error = 0; 4311da177e4SLinus Torvalds unsigned int pages = 0; 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds while (!list_empty(head)) { 4341da177e4SLinus Torvalds pages += nfs_coalesce_requests(head, &one_request, rpages); 4351da177e4SLinus Torvalds req = nfs_list_entry(one_request.next); 4361da177e4SLinus Torvalds error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode); 4371da177e4SLinus Torvalds if (error < 0) 4381da177e4SLinus Torvalds break; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds if (error >= 0) 4411da177e4SLinus Torvalds return pages; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds nfs_async_read_error(head); 4441da177e4SLinus Torvalds return error; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /* 4480b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 4490b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 4500b671301STrond Myklebust */ 4510b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 4520b671301STrond Myklebust { 4530b671301STrond Myklebust int status; 4540b671301STrond Myklebust 4550b671301STrond Myklebust dprintk("%s: %4d, (status %d)\n", __FUNCTION__, task->tk_pid, 4560b671301STrond Myklebust task->tk_status); 4570b671301STrond Myklebust 4580b671301STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 4590b671301STrond Myklebust if (status != 0) 4600b671301STrond Myklebust return status; 4610b671301STrond Myklebust 4620b671301STrond Myklebust nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); 4630b671301STrond Myklebust 4640b671301STrond Myklebust if (task->tk_status == -ESTALE) { 4650b671301STrond Myklebust set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode)); 4660b671301STrond Myklebust nfs_mark_for_revalidate(data->inode); 4670b671301STrond Myklebust } 4680b671301STrond Myklebust spin_lock(&data->inode->i_lock); 4690b671301STrond Myklebust NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME; 4700b671301STrond Myklebust spin_unlock(&data->inode->i_lock); 4710b671301STrond Myklebust return 0; 4720b671301STrond Myklebust } 4730b671301STrond Myklebust 4740b671301STrond Myklebust static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) 4750b671301STrond Myklebust { 4760b671301STrond Myklebust struct nfs_readargs *argp = &data->args; 4770b671301STrond Myklebust struct nfs_readres *resp = &data->res; 4780b671301STrond Myklebust 4790b671301STrond Myklebust if (resp->eof || resp->count == argp->count) 4800b671301STrond Myklebust return 0; 4810b671301STrond Myklebust 4820b671301STrond Myklebust /* This is a short read! */ 4830b671301STrond Myklebust nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 4840b671301STrond Myklebust /* Has the server at least made some progress? */ 4850b671301STrond Myklebust if (resp->count == 0) 4860b671301STrond Myklebust return 0; 4870b671301STrond Myklebust 4880b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 4890b671301STrond Myklebust argp->offset += resp->count; 4900b671301STrond Myklebust argp->pgbase += resp->count; 4910b671301STrond Myklebust argp->count -= resp->count; 4920b671301STrond Myklebust rpc_restart_call(task); 4930b671301STrond Myklebust return -EAGAIN; 4940b671301STrond Myklebust } 4950b671301STrond Myklebust 4960b671301STrond Myklebust /* 4971da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 4981da177e4SLinus Torvalds */ 499ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 5001da177e4SLinus Torvalds { 501ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 5021da177e4SLinus Torvalds struct nfs_page *req = data->req; 5031da177e4SLinus Torvalds struct page *page = req->wb_page; 5041da177e4SLinus Torvalds 505ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 506ec06c096STrond Myklebust return; 5070b671301STrond Myklebust 5080b671301STrond Myklebust if (likely(task->tk_status >= 0)) { 5090b671301STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 5100b671301STrond Myklebust if (nfs_readpage_retry(task, data) != 0) 5110b671301STrond Myklebust return; 5120b671301STrond Myklebust } 5130b671301STrond Myklebust if (unlikely(task->tk_status < 0)) 5140b671301STrond Myklebust SetPageError(page); 5151da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 5161da177e4SLinus Torvalds if (!PageError(page)) 5171da177e4SLinus Torvalds SetPageUptodate(page); 5181da177e4SLinus Torvalds nfs_readpage_release(req); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 522ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 523ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 524ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 525ec06c096STrond Myklebust }; 526ec06c096STrond Myklebust 5271de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 5281de3fc12STrond Myklebust { 5291de3fc12STrond Myklebust unsigned int count = data->res.count; 5301de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 5311de3fc12STrond Myklebust struct page **pages; 5321de3fc12STrond Myklebust 53379558f36STrond Myklebust if (data->res.eof) 53479558f36STrond Myklebust count = data->args.count; 5351de3fc12STrond Myklebust if (unlikely(count == 0)) 5361de3fc12STrond Myklebust return; 5371de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 5381de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 5391de3fc12STrond Myklebust count += base; 5401de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 5411de3fc12STrond Myklebust SetPageUptodate(*pages); 5420b671301STrond Myklebust if (count == 0) 5430b671301STrond Myklebust return; 5440b671301STrond Myklebust /* Was this a short read? */ 5450b671301STrond Myklebust if (data->res.eof || data->res.count == data->args.count) 5461de3fc12STrond Myklebust SetPageUptodate(*pages); 5471de3fc12STrond Myklebust } 5481de3fc12STrond Myklebust 5491da177e4SLinus Torvalds /* 5501da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 5511da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 5521da177e4SLinus Torvalds */ 553ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 5541da177e4SLinus Torvalds { 555ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 5561da177e4SLinus Torvalds 5570b671301STrond Myklebust if (nfs_readpage_result(task, data) != 0) 5580b671301STrond Myklebust return; 5591de3fc12STrond Myklebust /* 5600b671301STrond Myklebust * Note: nfs_readpage_retry may change the values of 5611de3fc12STrond Myklebust * data->args. In the multi-page case, we therefore need 5620b671301STrond Myklebust * to ensure that we call nfs_readpage_set_pages_uptodate() 5630b671301STrond Myklebust * first. 5641de3fc12STrond Myklebust */ 5651de3fc12STrond Myklebust if (likely(task->tk_status >= 0)) { 5661de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 5671de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 5680b671301STrond Myklebust if (nfs_readpage_retry(task, data) != 0) 569ec06c096STrond Myklebust return; 5700b671301STrond Myklebust } 5711da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 5721da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 5731da177e4SLinus Torvalds 5741de3fc12STrond Myklebust nfs_list_remove_request(req); 5751da177e4SLinus Torvalds nfs_readpage_release(req); 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 579ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 580ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 581ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 582ec06c096STrond Myklebust }; 583ec06c096STrond Myklebust 5841da177e4SLinus Torvalds /* 5851da177e4SLinus Torvalds * Read a page over NFS. 5861da177e4SLinus Torvalds * We read the page synchronously in the following case: 5871da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 5881da177e4SLinus Torvalds * previous async read operation failed. 5891da177e4SLinus Torvalds */ 5901da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds struct nfs_open_context *ctx; 5931da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 5941da177e4SLinus Torvalds int error; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 5971da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 59891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 59991d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 60091d5b470SChuck Lever 6011da177e4SLinus Torvalds /* 6021da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 6031da177e4SLinus Torvalds * 6041da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 6051da177e4SLinus Torvalds * be any new pending writes generated at this point 6061da177e4SLinus Torvalds * for this page (other pages can be written to). 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 6091da177e4SLinus Torvalds if (error) 6101da177e4SLinus Torvalds goto out_error; 6111da177e4SLinus Torvalds 6125f004cf2STrond Myklebust error = -ESTALE; 6135f004cf2STrond Myklebust if (NFS_STALE(inode)) 6145f004cf2STrond Myklebust goto out_error; 6155f004cf2STrond Myklebust 6161da177e4SLinus Torvalds if (file == NULL) { 617cf1308ffSTrond Myklebust error = -EBADF; 618d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 6191da177e4SLinus Torvalds if (ctx == NULL) 620cf1308ffSTrond Myklebust goto out_error; 6211da177e4SLinus Torvalds } else 6221da177e4SLinus Torvalds ctx = get_nfs_open_context((struct nfs_open_context *) 6231da177e4SLinus Torvalds file->private_data); 6241da177e4SLinus Torvalds if (!IS_SYNC(inode)) { 6251da177e4SLinus Torvalds error = nfs_readpage_async(ctx, inode, page); 6261da177e4SLinus Torvalds goto out; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds error = nfs_readpage_sync(ctx, inode, page); 6301da177e4SLinus Torvalds if (error < 0 && IS_SWAPFILE(inode)) 6311da177e4SLinus Torvalds printk("Aiee.. nfs swap-in of page failed!\n"); 6321da177e4SLinus Torvalds out: 6331da177e4SLinus Torvalds put_nfs_open_context(ctx); 6341da177e4SLinus Torvalds return error; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds out_error: 6371da177e4SLinus Torvalds unlock_page(page); 6381da177e4SLinus Torvalds return error; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds struct nfs_readdesc { 6421da177e4SLinus Torvalds struct list_head *head; 6431da177e4SLinus Torvalds struct nfs_open_context *ctx; 6441da177e4SLinus Torvalds }; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds static int 6471da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 6481da177e4SLinus Torvalds { 6491da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 6501da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 6511da177e4SLinus Torvalds struct nfs_page *new; 6521da177e4SLinus Torvalds unsigned int len; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds nfs_wb_page(inode, page); 655*49a70f27STrond Myklebust len = nfs_page_length(page); 6561da177e4SLinus Torvalds if (len == 0) 6571da177e4SLinus Torvalds return nfs_return_empty_page(page); 6581da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 6591da177e4SLinus Torvalds if (IS_ERR(new)) { 6601da177e4SLinus Torvalds SetPageError(page); 6611da177e4SLinus Torvalds unlock_page(page); 6621da177e4SLinus Torvalds return PTR_ERR(new); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 6651da177e4SLinus Torvalds memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); 6661da177e4SLinus Torvalds nfs_list_add_request(new, desc->head); 6671da177e4SLinus Torvalds return 0; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 6711da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds LIST_HEAD(head); 6741da177e4SLinus Torvalds struct nfs_readdesc desc = { 6751da177e4SLinus Torvalds .head = &head, 6761da177e4SLinus Torvalds }; 6771da177e4SLinus Torvalds struct inode *inode = mapping->host; 6781da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 6795f004cf2STrond Myklebust int ret = -ESTALE; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 6821da177e4SLinus Torvalds inode->i_sb->s_id, 6831da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 6841da177e4SLinus Torvalds nr_pages); 68591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 6861da177e4SLinus Torvalds 6875f004cf2STrond Myklebust if (NFS_STALE(inode)) 6885f004cf2STrond Myklebust goto out; 6895f004cf2STrond Myklebust 6901da177e4SLinus Torvalds if (filp == NULL) { 691d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 6921da177e4SLinus Torvalds if (desc.ctx == NULL) 6931da177e4SLinus Torvalds return -EBADF; 6941da177e4SLinus Torvalds } else 6951da177e4SLinus Torvalds desc.ctx = get_nfs_open_context((struct nfs_open_context *) 6961da177e4SLinus Torvalds filp->private_data); 6971da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 6981da177e4SLinus Torvalds if (!list_empty(&head)) { 6991da177e4SLinus Torvalds int err = nfs_pagein_list(&head, server->rpages); 7001da177e4SLinus Torvalds if (!ret) 70191d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, err); 7021da177e4SLinus Torvalds ret = err; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 7055f004cf2STrond Myklebust out: 7061da177e4SLinus Torvalds return ret; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 709f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 7121da177e4SLinus Torvalds sizeof(struct nfs_read_data), 7131da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 7141da177e4SLinus Torvalds NULL, NULL); 7151da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 7161da177e4SLinus Torvalds return -ENOMEM; 7171da177e4SLinus Torvalds 71893d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 7191da177e4SLinus Torvalds nfs_rdata_cachep); 7201da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 7211da177e4SLinus Torvalds return -ENOMEM; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds return 0; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 726266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 7271da177e4SLinus Torvalds { 7281da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 7291a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 7301da177e4SLinus Torvalds } 731