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 101da177e4SLinus Torvalds #include <linux/time.h> 111da177e4SLinus Torvalds #include <linux/kernel.h> 121da177e4SLinus Torvalds #include <linux/errno.h> 131da177e4SLinus Torvalds #include <linux/fcntl.h> 141da177e4SLinus Torvalds #include <linux/stat.h> 151da177e4SLinus Torvalds #include <linux/mm.h> 161da177e4SLinus Torvalds #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/pagemap.h> 181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 191da177e4SLinus Torvalds #include <linux/nfs_fs.h> 201da177e4SLinus Torvalds #include <linux/nfs_page.h> 211da177e4SLinus Torvalds #include <linux/smp_lock.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <asm/system.h> 241da177e4SLinus Torvalds 2549a70f27STrond Myklebust #include "internal.h" 2691d5b470SChuck Lever #include "iostat.h" 2791d5b470SChuck Lever 281da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 291da177e4SLinus Torvalds 308d5658c9STrond Myklebust static int nfs_pagein_multi(struct inode *, struct list_head *, unsigned int, size_t, int); 318d5658c9STrond Myklebust static int nfs_pagein_one(struct inode *, struct list_head *, unsigned int, size_t, int); 32ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops; 33ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops; 341da177e4SLinus Torvalds 35e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep; 363feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool; 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #define MIN_POOL_READ (32) 391da177e4SLinus Torvalds 408d5658c9STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount) 413feb2d49STrond Myklebust { 42e6b4f8daSChristoph Lameter struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_NOFS); 433feb2d49STrond Myklebust 443feb2d49STrond Myklebust if (p) { 453feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 463feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 47e9f7bee1STrond Myklebust p->npages = pagecount; 480d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 490d0b5cb3SChuck Lever p->pagevec = p->page_array; 503feb2d49STrond Myklebust else { 510d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 520d0b5cb3SChuck Lever if (!p->pagevec) { 533feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 543feb2d49STrond Myklebust p = NULL; 553feb2d49STrond Myklebust } 563feb2d49STrond Myklebust } 573feb2d49STrond Myklebust } 583feb2d49STrond Myklebust return p; 593feb2d49STrond Myklebust } 603feb2d49STrond Myklebust 618aca67f0STrond Myklebust static void nfs_readdata_rcu_free(struct rcu_head *head) 623feb2d49STrond Myklebust { 638aca67f0STrond Myklebust struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu); 643feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 653feb2d49STrond Myklebust kfree(p->pagevec); 663feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 673feb2d49STrond Myklebust } 683feb2d49STrond Myklebust 698aca67f0STrond Myklebust static void nfs_readdata_free(struct nfs_read_data *rdata) 708aca67f0STrond Myklebust { 718aca67f0STrond Myklebust call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free); 728aca67f0STrond Myklebust } 738aca67f0STrond Myklebust 74963d8fe5STrond Myklebust void nfs_readdata_release(void *data) 751da177e4SLinus Torvalds { 761da177e4SLinus Torvalds nfs_readdata_free(data); 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds static 801da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 811da177e4SLinus Torvalds { 8260945cb7SNate Diller zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0); 831da177e4SLinus Torvalds SetPageUptodate(page); 841da177e4SLinus Torvalds unlock_page(page); 851da177e4SLinus Torvalds return 0; 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds 881de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) 891de3fc12STrond Myklebust { 901de3fc12STrond Myklebust unsigned int remainder = data->args.count - data->res.count; 911de3fc12STrond Myklebust unsigned int base = data->args.pgbase + data->res.count; 921de3fc12STrond Myklebust unsigned int pglen; 931de3fc12STrond Myklebust struct page **pages; 941de3fc12STrond Myklebust 951de3fc12STrond Myklebust if (data->res.eof == 0 || remainder == 0) 961de3fc12STrond Myklebust return; 971de3fc12STrond Myklebust /* 981de3fc12STrond Myklebust * Note: "remainder" can never be negative, since we check for 991de3fc12STrond Myklebust * this in the XDR code. 1001de3fc12STrond Myklebust */ 1011de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 1021de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 1031de3fc12STrond Myklebust pglen = PAGE_CACHE_SIZE - base; 10479558f36STrond Myklebust for (;;) { 10579558f36STrond Myklebust if (remainder <= pglen) { 10660945cb7SNate Diller zero_user_page(*pages, base, remainder, KM_USER0); 10779558f36STrond Myklebust break; 10879558f36STrond Myklebust } 10960945cb7SNate Diller zero_user_page(*pages, base, pglen, KM_USER0); 11079558f36STrond Myklebust pages++; 11179558f36STrond Myklebust remainder -= pglen; 11279558f36STrond Myklebust pglen = PAGE_CACHE_SIZE; 11379558f36STrond Myklebust base = 0; 11479558f36STrond Myklebust } 1151de3fc12STrond Myklebust } 1161de3fc12STrond Myklebust 1171da177e4SLinus Torvalds static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 1181da177e4SLinus Torvalds struct page *page) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds LIST_HEAD(one_request); 1211da177e4SLinus Torvalds struct nfs_page *new; 1221da177e4SLinus Torvalds unsigned int len; 1231da177e4SLinus Torvalds 12449a70f27STrond Myklebust len = nfs_page_length(page); 1251da177e4SLinus Torvalds if (len == 0) 1261da177e4SLinus Torvalds return nfs_return_empty_page(page); 1271da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 1281da177e4SLinus Torvalds if (IS_ERR(new)) { 1291da177e4SLinus Torvalds unlock_page(page); 1301da177e4SLinus Torvalds return PTR_ERR(new); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 13360945cb7SNate Diller zero_user_page(page, len, PAGE_CACHE_SIZE - len, KM_USER0); 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds nfs_list_add_request(new, &one_request); 136bcb71bbaSTrond Myklebust if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) 1378d5658c9STrond Myklebust nfs_pagein_multi(inode, &one_request, 1, len, 0); 138bcb71bbaSTrond Myklebust else 1398d5658c9STrond Myklebust nfs_pagein_one(inode, &one_request, 1, len, 0); 1401da177e4SLinus Torvalds return 0; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds unlock_page(req->wb_page); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 14888be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 14988be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1501da177e4SLinus Torvalds req->wb_bytes, 1511da177e4SLinus Torvalds (long long)req_offset(req)); 15210d2c46fSNick Wilson nfs_clear_request(req); 15310d2c46fSNick Wilson nfs_release_request(req); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* 1571da177e4SLinus Torvalds * Set up the NFS read request struct 1581da177e4SLinus Torvalds */ 1591da177e4SLinus Torvalds static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 160ec06c096STrond Myklebust const struct rpc_call_ops *call_ops, 1611da177e4SLinus Torvalds unsigned int count, unsigned int offset) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds struct inode *inode; 164ec06c096STrond Myklebust int flags; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds data->req = req; 16788be9f99STrond Myklebust data->inode = inode = req->wb_context->path.dentry->d_inode; 1681da177e4SLinus Torvalds data->cred = req->wb_context->cred; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 1711da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 1721da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 1731da177e4SLinus Torvalds data->args.pages = data->pagevec; 1741da177e4SLinus Torvalds data->args.count = count; 1751da177e4SLinus Torvalds data->args.context = req->wb_context; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds data->res.fattr = &data->fattr; 1781da177e4SLinus Torvalds data->res.count = count; 1791da177e4SLinus Torvalds data->res.eof = 0; 1800e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 1811da177e4SLinus Torvalds 182ec06c096STrond Myklebust /* Set up the initial task struct. */ 183ec06c096STrond Myklebust flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); 184ec06c096STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); 1851da177e4SLinus Torvalds NFS_PROTO(inode)->read_setup(data); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long)inode; 1881da177e4SLinus Torvalds 189a3f565b1SChuck Lever dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 1901da177e4SLinus Torvalds data->task.tk_pid, 1911da177e4SLinus Torvalds inode->i_sb->s_id, 1921da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 1931da177e4SLinus Torvalds count, 1941da177e4SLinus Torvalds (unsigned long long)data->args.offset); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds static void 1981da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds struct nfs_page *req; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds while (!list_empty(head)) { 2031da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2041da177e4SLinus Torvalds nfs_list_remove_request(req); 2051da177e4SLinus Torvalds SetPageError(req->wb_page); 2061da177e4SLinus Torvalds nfs_readpage_release(req); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* 2111da177e4SLinus Torvalds * Start an async read operation 2121da177e4SLinus Torvalds */ 2131da177e4SLinus Torvalds static void nfs_execute_read(struct nfs_read_data *data) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(data->inode); 2161da177e4SLinus Torvalds sigset_t oldset; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 2191da177e4SLinus Torvalds rpc_execute(&data->task); 2201da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds /* 2241da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 2251da177e4SLinus Torvalds * 2261da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 2271da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 2281da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 2291da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 2301da177e4SLinus Torvalds * 2311da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 2321da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 2331da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 2341da177e4SLinus Torvalds * or less conventional NFS client behavior. 2351da177e4SLinus Torvalds */ 2368d5658c9STrond Myklebust static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 2391da177e4SLinus Torvalds struct page *page = req->wb_page; 2401da177e4SLinus Torvalds struct nfs_read_data *data; 241e9f7bee1STrond Myklebust size_t rsize = NFS_SERVER(inode)->rsize, nbytes; 242e9f7bee1STrond Myklebust unsigned int offset; 2431da177e4SLinus Torvalds int requests = 0; 2441da177e4SLinus Torvalds LIST_HEAD(list); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds nfs_list_remove_request(req); 2471da177e4SLinus Torvalds 248bcb71bbaSTrond Myklebust nbytes = count; 249e9f7bee1STrond Myklebust do { 250e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 251e9f7bee1STrond Myklebust 2528d5658c9STrond Myklebust data = nfs_readdata_alloc(1); 2531da177e4SLinus Torvalds if (!data) 2541da177e4SLinus Torvalds goto out_bad; 2551da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 2561da177e4SLinus Torvalds list_add(&data->pages, &list); 2571da177e4SLinus Torvalds requests++; 258e9f7bee1STrond Myklebust nbytes -= len; 259e9f7bee1STrond Myklebust } while(nbytes != 0); 2601da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds ClearPageError(page); 2631da177e4SLinus Torvalds offset = 0; 264bcb71bbaSTrond Myklebust nbytes = count; 2651da177e4SLinus Torvalds do { 2661da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 2671da177e4SLinus Torvalds list_del_init(&data->pages); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds data->pagevec[0] = page; 2701da177e4SLinus Torvalds 271bcb71bbaSTrond Myklebust if (nbytes < rsize) 272bcb71bbaSTrond Myklebust rsize = nbytes; 273ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 274ec06c096STrond Myklebust rsize, offset); 2751da177e4SLinus Torvalds offset += rsize; 2761da177e4SLinus Torvalds nbytes -= rsize; 2771da177e4SLinus Torvalds nfs_execute_read(data); 2781da177e4SLinus Torvalds } while (nbytes != 0); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds return 0; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds out_bad: 2831da177e4SLinus Torvalds while (!list_empty(&list)) { 2841da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 2851da177e4SLinus Torvalds list_del(&data->pages); 2861da177e4SLinus Torvalds nfs_readdata_free(data); 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds SetPageError(page); 2891da177e4SLinus Torvalds nfs_readpage_release(req); 2901da177e4SLinus Torvalds return -ENOMEM; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2938d5658c9STrond Myklebust static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds struct nfs_page *req; 2961da177e4SLinus Torvalds struct page **pages; 2971da177e4SLinus Torvalds struct nfs_read_data *data; 2981da177e4SLinus Torvalds 2998d5658c9STrond Myklebust data = nfs_readdata_alloc(npages); 3001da177e4SLinus Torvalds if (!data) 3011da177e4SLinus Torvalds goto out_bad; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds INIT_LIST_HEAD(&data->pages); 3041da177e4SLinus Torvalds pages = data->pagevec; 3051da177e4SLinus Torvalds while (!list_empty(head)) { 3061da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3071da177e4SLinus Torvalds nfs_list_remove_request(req); 3081da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 3091da177e4SLinus Torvalds ClearPageError(req->wb_page); 3101da177e4SLinus Torvalds *pages++ = req->wb_page; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 3131da177e4SLinus Torvalds 314ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds nfs_execute_read(data); 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds out_bad: 3191da177e4SLinus Torvalds nfs_async_read_error(head); 3201da177e4SLinus Torvalds return -ENOMEM; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* 3240b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 3250b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 3260b671301STrond Myklebust */ 3270b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 3280b671301STrond Myklebust { 3290b671301STrond Myklebust int status; 3300b671301STrond Myklebust 331a3f565b1SChuck Lever dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid, 3320b671301STrond Myklebust task->tk_status); 3330b671301STrond Myklebust 3340b671301STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 3350b671301STrond Myklebust if (status != 0) 3360b671301STrond Myklebust return status; 3370b671301STrond Myklebust 3380b671301STrond Myklebust nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); 3390b671301STrond Myklebust 3400b671301STrond Myklebust if (task->tk_status == -ESTALE) { 3410b671301STrond Myklebust set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode)); 3420b671301STrond Myklebust nfs_mark_for_revalidate(data->inode); 3430b671301STrond Myklebust } 3440b671301STrond Myklebust spin_lock(&data->inode->i_lock); 3450b671301STrond Myklebust NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME; 3460b671301STrond Myklebust spin_unlock(&data->inode->i_lock); 3470b671301STrond Myklebust return 0; 3480b671301STrond Myklebust } 3490b671301STrond Myklebust 3500b671301STrond Myklebust static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) 3510b671301STrond Myklebust { 3520b671301STrond Myklebust struct nfs_readargs *argp = &data->args; 3530b671301STrond Myklebust struct nfs_readres *resp = &data->res; 3540b671301STrond Myklebust 3550b671301STrond Myklebust if (resp->eof || resp->count == argp->count) 3560b671301STrond Myklebust return 0; 3570b671301STrond Myklebust 3580b671301STrond Myklebust /* This is a short read! */ 3590b671301STrond Myklebust nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 3600b671301STrond Myklebust /* Has the server at least made some progress? */ 3610b671301STrond Myklebust if (resp->count == 0) 3620b671301STrond Myklebust return 0; 3630b671301STrond Myklebust 3640b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 3650b671301STrond Myklebust argp->offset += resp->count; 3660b671301STrond Myklebust argp->pgbase += resp->count; 3670b671301STrond Myklebust argp->count -= resp->count; 3680b671301STrond Myklebust rpc_restart_call(task); 3690b671301STrond Myklebust return -EAGAIN; 3700b671301STrond Myklebust } 3710b671301STrond Myklebust 3720b671301STrond Myklebust /* 3731da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 3741da177e4SLinus Torvalds */ 375ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 3761da177e4SLinus Torvalds { 377ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 3781da177e4SLinus Torvalds struct nfs_page *req = data->req; 3791da177e4SLinus Torvalds struct page *page = req->wb_page; 3801da177e4SLinus Torvalds 381ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 382ec06c096STrond Myklebust return; 3830b671301STrond Myklebust 3840b671301STrond Myklebust if (likely(task->tk_status >= 0)) { 3850b671301STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 3860b671301STrond Myklebust if (nfs_readpage_retry(task, data) != 0) 3870b671301STrond Myklebust return; 3880b671301STrond Myklebust } 3890b671301STrond Myklebust if (unlikely(task->tk_status < 0)) 3900b671301STrond Myklebust SetPageError(page); 3911da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 3921da177e4SLinus Torvalds if (!PageError(page)) 3931da177e4SLinus Torvalds SetPageUptodate(page); 3941da177e4SLinus Torvalds nfs_readpage_release(req); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 398ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 399ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 400ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 401ec06c096STrond Myklebust }; 402ec06c096STrond Myklebust 4031de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 4041de3fc12STrond Myklebust { 4051de3fc12STrond Myklebust unsigned int count = data->res.count; 4061de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 4071de3fc12STrond Myklebust struct page **pages; 4081de3fc12STrond Myklebust 40979558f36STrond Myklebust if (data->res.eof) 41079558f36STrond Myklebust count = data->args.count; 4111de3fc12STrond Myklebust if (unlikely(count == 0)) 4121de3fc12STrond Myklebust return; 4131de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 4141de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 4151de3fc12STrond Myklebust count += base; 4161de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 4171de3fc12STrond Myklebust SetPageUptodate(*pages); 4180b671301STrond Myklebust if (count == 0) 4190b671301STrond Myklebust return; 4200b671301STrond Myklebust /* Was this a short read? */ 4210b671301STrond Myklebust if (data->res.eof || data->res.count == data->args.count) 4221de3fc12STrond Myklebust SetPageUptodate(*pages); 4231de3fc12STrond Myklebust } 4241de3fc12STrond Myklebust 4251da177e4SLinus Torvalds /* 4261da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 4271da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 4281da177e4SLinus Torvalds */ 429ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 4301da177e4SLinus Torvalds { 431ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 4321da177e4SLinus Torvalds 4330b671301STrond Myklebust if (nfs_readpage_result(task, data) != 0) 4340b671301STrond Myklebust return; 4351de3fc12STrond Myklebust /* 4360b671301STrond Myklebust * Note: nfs_readpage_retry may change the values of 4371de3fc12STrond Myklebust * data->args. In the multi-page case, we therefore need 4380b671301STrond Myklebust * to ensure that we call nfs_readpage_set_pages_uptodate() 4390b671301STrond Myklebust * first. 4401de3fc12STrond Myklebust */ 4411de3fc12STrond Myklebust if (likely(task->tk_status >= 0)) { 4421de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 4431de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 4440b671301STrond Myklebust if (nfs_readpage_retry(task, data) != 0) 445ec06c096STrond Myklebust return; 4460b671301STrond Myklebust } 4471da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 4481da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 4491da177e4SLinus Torvalds 4501de3fc12STrond Myklebust nfs_list_remove_request(req); 4511da177e4SLinus Torvalds nfs_readpage_release(req); 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 455ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 456ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 457ec06c096STrond Myklebust .rpc_release = nfs_readdata_release, 458ec06c096STrond Myklebust }; 459ec06c096STrond Myklebust 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * Read a page over NFS. 4621da177e4SLinus Torvalds * We read the page synchronously in the following case: 4631da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 4641da177e4SLinus Torvalds * previous async read operation failed. 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 4671da177e4SLinus Torvalds { 4681da177e4SLinus Torvalds struct nfs_open_context *ctx; 4691da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 4701da177e4SLinus Torvalds int error; 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 4731da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 47491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 47591d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 47691d5b470SChuck Lever 4771da177e4SLinus Torvalds /* 4781da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 4791da177e4SLinus Torvalds * 4801da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 4811da177e4SLinus Torvalds * be any new pending writes generated at this point 4821da177e4SLinus Torvalds * for this page (other pages can be written to). 4831da177e4SLinus Torvalds */ 4841da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 4851da177e4SLinus Torvalds if (error) 486de05a0ccSTrond Myklebust goto out_unlock; 487de05a0ccSTrond Myklebust if (PageUptodate(page)) 488de05a0ccSTrond Myklebust goto out_unlock; 4891da177e4SLinus Torvalds 4905f004cf2STrond Myklebust error = -ESTALE; 4915f004cf2STrond Myklebust if (NFS_STALE(inode)) 492de05a0ccSTrond Myklebust goto out_unlock; 4935f004cf2STrond Myklebust 4941da177e4SLinus Torvalds if (file == NULL) { 495cf1308ffSTrond Myklebust error = -EBADF; 496d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 4971da177e4SLinus Torvalds if (ctx == NULL) 498de05a0ccSTrond Myklebust goto out_unlock; 4991da177e4SLinus Torvalds } else 5001da177e4SLinus Torvalds ctx = get_nfs_open_context((struct nfs_open_context *) 5011da177e4SLinus Torvalds file->private_data); 5021da177e4SLinus Torvalds 5038e0969f0STrond Myklebust error = nfs_readpage_async(ctx, inode, page); 5048e0969f0STrond Myklebust 5051da177e4SLinus Torvalds put_nfs_open_context(ctx); 5061da177e4SLinus Torvalds return error; 507de05a0ccSTrond Myklebust out_unlock: 5081da177e4SLinus Torvalds unlock_page(page); 5091da177e4SLinus Torvalds return error; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds struct nfs_readdesc { 5138b09bee3STrond Myklebust struct nfs_pageio_descriptor *pgio; 5141da177e4SLinus Torvalds struct nfs_open_context *ctx; 5151da177e4SLinus Torvalds }; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds static int 5181da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 5191da177e4SLinus Torvalds { 5201da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 5211da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 5221da177e4SLinus Torvalds struct nfs_page *new; 5231da177e4SLinus Torvalds unsigned int len; 524de05a0ccSTrond Myklebust int error; 5251da177e4SLinus Torvalds 526de05a0ccSTrond Myklebust error = nfs_wb_page(inode, page); 527de05a0ccSTrond Myklebust if (error) 528de05a0ccSTrond Myklebust goto out_unlock; 529de05a0ccSTrond Myklebust if (PageUptodate(page)) 530de05a0ccSTrond Myklebust goto out_unlock; 531de05a0ccSTrond Myklebust 53249a70f27STrond Myklebust len = nfs_page_length(page); 5331da177e4SLinus Torvalds if (len == 0) 5341da177e4SLinus Torvalds return nfs_return_empty_page(page); 535de05a0ccSTrond Myklebust 5361da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 537de05a0ccSTrond Myklebust if (IS_ERR(new)) 538de05a0ccSTrond Myklebust goto out_error; 539de05a0ccSTrond Myklebust 5401da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 54160945cb7SNate Diller zero_user_page(page, len, PAGE_CACHE_SIZE - len, KM_USER0); 5428b09bee3STrond Myklebust nfs_pageio_add_request(desc->pgio, new); 5431da177e4SLinus Torvalds return 0; 544de05a0ccSTrond Myklebust out_error: 545de05a0ccSTrond Myklebust error = PTR_ERR(new); 546de05a0ccSTrond Myklebust SetPageError(page); 547de05a0ccSTrond Myklebust out_unlock: 548de05a0ccSTrond Myklebust unlock_page(page); 549de05a0ccSTrond Myklebust return error; 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 5531da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 5541da177e4SLinus Torvalds { 5558b09bee3STrond Myklebust struct nfs_pageio_descriptor pgio; 5561da177e4SLinus Torvalds struct nfs_readdesc desc = { 5578b09bee3STrond Myklebust .pgio = &pgio, 5581da177e4SLinus Torvalds }; 5591da177e4SLinus Torvalds struct inode *inode = mapping->host; 5601da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 5618b09bee3STrond Myklebust size_t rsize = server->rsize; 5628b09bee3STrond Myklebust unsigned long npages; 5635f004cf2STrond Myklebust int ret = -ESTALE; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 5661da177e4SLinus Torvalds inode->i_sb->s_id, 5671da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 5681da177e4SLinus Torvalds nr_pages); 56991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 5701da177e4SLinus Torvalds 5715f004cf2STrond Myklebust if (NFS_STALE(inode)) 5725f004cf2STrond Myklebust goto out; 5735f004cf2STrond Myklebust 5741da177e4SLinus Torvalds if (filp == NULL) { 575d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 5761da177e4SLinus Torvalds if (desc.ctx == NULL) 5771da177e4SLinus Torvalds return -EBADF; 5781da177e4SLinus Torvalds } else 5791da177e4SLinus Torvalds desc.ctx = get_nfs_open_context((struct nfs_open_context *) 5801da177e4SLinus Torvalds filp->private_data); 5818b09bee3STrond Myklebust if (rsize < PAGE_CACHE_SIZE) 5828b09bee3STrond Myklebust nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0); 5838b09bee3STrond Myklebust else 5848b09bee3STrond Myklebust nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0); 5858b09bee3STrond Myklebust 5861da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 5878b09bee3STrond Myklebust 5888b09bee3STrond Myklebust nfs_pageio_complete(&pgio); 5898b09bee3STrond Myklebust npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 5908b09bee3STrond Myklebust nfs_add_stats(inode, NFSIOS_READPAGES, npages); 5911da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 5925f004cf2STrond Myklebust out: 5931da177e4SLinus Torvalds return ret; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 596f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 5991da177e4SLinus Torvalds sizeof(struct nfs_read_data), 6001da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 601*20c2df83SPaul Mundt NULL); 6021da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 6031da177e4SLinus Torvalds return -ENOMEM; 6041da177e4SLinus Torvalds 60593d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 6061da177e4SLinus Torvalds nfs_rdata_cachep); 6071da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 6081da177e4SLinus Torvalds return -ENOMEM; 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds return 0; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 613266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 6141da177e4SLinus Torvalds { 6151da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 6161a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 6171da177e4SLinus Torvalds } 618