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 615e4424afSTrond Myklebust static void nfs_readdata_free(struct nfs_read_data *p) 623feb2d49STrond Myklebust { 633feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 643feb2d49STrond Myklebust kfree(p->pagevec); 653feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 663feb2d49STrond Myklebust } 673feb2d49STrond Myklebust 68963d8fe5STrond Myklebust void nfs_readdata_release(void *data) 691da177e4SLinus Torvalds { 70383ba719STrond Myklebust struct nfs_read_data *rdata = data; 71383ba719STrond Myklebust 72383ba719STrond Myklebust put_nfs_open_context(rdata->args.context); 73383ba719STrond Myklebust nfs_readdata_free(rdata); 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds static 771da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 781da177e4SLinus Torvalds { 79eebd2aa3SChristoph Lameter zero_user(page, 0, PAGE_CACHE_SIZE); 801da177e4SLinus Torvalds SetPageUptodate(page); 811da177e4SLinus Torvalds unlock_page(page); 821da177e4SLinus Torvalds return 0; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 851de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) 861de3fc12STrond Myklebust { 871de3fc12STrond Myklebust unsigned int remainder = data->args.count - data->res.count; 881de3fc12STrond Myklebust unsigned int base = data->args.pgbase + data->res.count; 891de3fc12STrond Myklebust unsigned int pglen; 901de3fc12STrond Myklebust struct page **pages; 911de3fc12STrond Myklebust 921de3fc12STrond Myklebust if (data->res.eof == 0 || remainder == 0) 931de3fc12STrond Myklebust return; 941de3fc12STrond Myklebust /* 951de3fc12STrond Myklebust * Note: "remainder" can never be negative, since we check for 961de3fc12STrond Myklebust * this in the XDR code. 971de3fc12STrond Myklebust */ 981de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 991de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 1001de3fc12STrond Myklebust pglen = PAGE_CACHE_SIZE - base; 10179558f36STrond Myklebust for (;;) { 10279558f36STrond Myklebust if (remainder <= pglen) { 103eebd2aa3SChristoph Lameter zero_user(*pages, base, remainder); 10479558f36STrond Myklebust break; 10579558f36STrond Myklebust } 106eebd2aa3SChristoph Lameter zero_user(*pages, base, pglen); 10779558f36STrond Myklebust pages++; 10879558f36STrond Myklebust remainder -= pglen; 10979558f36STrond Myklebust pglen = PAGE_CACHE_SIZE; 11079558f36STrond Myklebust base = 0; 11179558f36STrond Myklebust } 1121de3fc12STrond Myklebust } 1131de3fc12STrond Myklebust 1141da177e4SLinus Torvalds static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 1151da177e4SLinus Torvalds struct page *page) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds LIST_HEAD(one_request); 1181da177e4SLinus Torvalds struct nfs_page *new; 1191da177e4SLinus Torvalds unsigned int len; 1201da177e4SLinus Torvalds 12149a70f27STrond Myklebust len = nfs_page_length(page); 1221da177e4SLinus Torvalds if (len == 0) 1231da177e4SLinus Torvalds return nfs_return_empty_page(page); 1241da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 1251da177e4SLinus Torvalds if (IS_ERR(new)) { 1261da177e4SLinus Torvalds unlock_page(page); 1271da177e4SLinus Torvalds return PTR_ERR(new); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 130eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds nfs_list_add_request(new, &one_request); 133bcb71bbaSTrond Myklebust if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) 1348d5658c9STrond Myklebust nfs_pagein_multi(inode, &one_request, 1, len, 0); 135bcb71bbaSTrond Myklebust else 1368d5658c9STrond Myklebust nfs_pagein_one(inode, &one_request, 1, len, 0); 1371da177e4SLinus Torvalds return 0; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds unlock_page(req->wb_page); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 14588be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 14688be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1471da177e4SLinus Torvalds req->wb_bytes, 1481da177e4SLinus Torvalds (long long)req_offset(req)); 14910d2c46fSNick Wilson nfs_clear_request(req); 15010d2c46fSNick Wilson nfs_release_request(req); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* 1541da177e4SLinus Torvalds * Set up the NFS read request struct 1551da177e4SLinus Torvalds */ 1561da177e4SLinus Torvalds static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 157ec06c096STrond Myklebust const struct rpc_call_ops *call_ops, 1581da177e4SLinus Torvalds unsigned int count, unsigned int offset) 1591da177e4SLinus Torvalds { 16084115e1cSTrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 16184115e1cSTrond Myklebust int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; 16207737691STrond Myklebust struct rpc_task *task; 163bdc7f021STrond Myklebust struct rpc_message msg = { 164bdc7f021STrond Myklebust .rpc_argp = &data->args, 165bdc7f021STrond Myklebust .rpc_resp = &data->res, 166bdc7f021STrond Myklebust .rpc_cred = req->wb_context->cred, 167bdc7f021STrond Myklebust }; 16884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 16907737691STrond Myklebust .task = &data->task, 17084115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 171bdc7f021STrond Myklebust .rpc_message = &msg, 17284115e1cSTrond Myklebust .callback_ops = call_ops, 17384115e1cSTrond Myklebust .callback_data = data, 174101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 17584115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC | swap_flags, 17684115e1cSTrond Myklebust }; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds data->req = req; 17984115e1cSTrond Myklebust data->inode = inode; 180bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 1831da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 1841da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 1851da177e4SLinus Torvalds data->args.pages = data->pagevec; 1861da177e4SLinus Torvalds data->args.count = count; 187383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds data->res.fattr = &data->fattr; 1901da177e4SLinus Torvalds data->res.count = count; 1911da177e4SLinus Torvalds data->res.eof = 0; 1920e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 1931da177e4SLinus Torvalds 194ec06c096STrond Myklebust /* Set up the initial task struct. */ 195bdc7f021STrond Myklebust NFS_PROTO(inode)->read_setup(data, &msg); 1961da177e4SLinus Torvalds 197a3f565b1SChuck Lever dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 1981da177e4SLinus Torvalds data->task.tk_pid, 1991da177e4SLinus Torvalds inode->i_sb->s_id, 2001da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 2011da177e4SLinus Torvalds count, 2021da177e4SLinus Torvalds (unsigned long long)data->args.offset); 203bdc7f021STrond Myklebust 20407737691STrond Myklebust task = rpc_run_task(&task_setup_data); 20507737691STrond Myklebust if (!IS_ERR(task)) 20607737691STrond Myklebust rpc_put_task(task); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds static void 2101da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct nfs_page *req; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds while (!list_empty(head)) { 2151da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2161da177e4SLinus Torvalds nfs_list_remove_request(req); 2171da177e4SLinus Torvalds SetPageError(req->wb_page); 2181da177e4SLinus Torvalds nfs_readpage_release(req); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* 2231da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 2241da177e4SLinus Torvalds * 2251da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 2261da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 2271da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 2281da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 2291da177e4SLinus Torvalds * 2301da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 2311da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 2321da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 2331da177e4SLinus Torvalds * or less conventional NFS client behavior. 2341da177e4SLinus Torvalds */ 2358d5658c9STrond Myklebust static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 2381da177e4SLinus Torvalds struct page *page = req->wb_page; 2391da177e4SLinus Torvalds struct nfs_read_data *data; 240e9f7bee1STrond Myklebust size_t rsize = NFS_SERVER(inode)->rsize, nbytes; 241e9f7bee1STrond Myklebust unsigned int offset; 2421da177e4SLinus Torvalds int requests = 0; 2431da177e4SLinus Torvalds LIST_HEAD(list); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds nfs_list_remove_request(req); 2461da177e4SLinus Torvalds 247bcb71bbaSTrond Myklebust nbytes = count; 248e9f7bee1STrond Myklebust do { 249e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 250e9f7bee1STrond Myklebust 2518d5658c9STrond Myklebust data = nfs_readdata_alloc(1); 2521da177e4SLinus Torvalds if (!data) 2531da177e4SLinus Torvalds goto out_bad; 2541da177e4SLinus Torvalds list_add(&data->pages, &list); 2551da177e4SLinus Torvalds requests++; 256e9f7bee1STrond Myklebust nbytes -= len; 257e9f7bee1STrond Myklebust } while(nbytes != 0); 2581da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds ClearPageError(page); 2611da177e4SLinus Torvalds offset = 0; 262bcb71bbaSTrond Myklebust nbytes = count; 2631da177e4SLinus Torvalds do { 2641da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 2651da177e4SLinus Torvalds list_del_init(&data->pages); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds data->pagevec[0] = page; 2681da177e4SLinus Torvalds 269bcb71bbaSTrond Myklebust if (nbytes < rsize) 270bcb71bbaSTrond Myklebust rsize = nbytes; 271ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 272ec06c096STrond Myklebust rsize, offset); 2731da177e4SLinus Torvalds offset += rsize; 2741da177e4SLinus Torvalds nbytes -= rsize; 2751da177e4SLinus Torvalds } while (nbytes != 0); 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds return 0; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds out_bad: 2801da177e4SLinus Torvalds while (!list_empty(&list)) { 2811da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 2821da177e4SLinus Torvalds list_del(&data->pages); 2831da177e4SLinus Torvalds nfs_readdata_free(data); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds SetPageError(page); 2861da177e4SLinus Torvalds nfs_readpage_release(req); 2871da177e4SLinus Torvalds return -ENOMEM; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2908d5658c9STrond Myklebust static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct nfs_page *req; 2931da177e4SLinus Torvalds struct page **pages; 2941da177e4SLinus Torvalds struct nfs_read_data *data; 2951da177e4SLinus Torvalds 2968d5658c9STrond Myklebust data = nfs_readdata_alloc(npages); 2971da177e4SLinus Torvalds if (!data) 2981da177e4SLinus Torvalds goto out_bad; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds pages = data->pagevec; 3011da177e4SLinus Torvalds while (!list_empty(head)) { 3021da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3031da177e4SLinus Torvalds nfs_list_remove_request(req); 3041da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 3051da177e4SLinus Torvalds ClearPageError(req->wb_page); 3061da177e4SLinus Torvalds *pages++ = req->wb_page; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 3091da177e4SLinus Torvalds 310ec06c096STrond Myklebust nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); 3111da177e4SLinus Torvalds return 0; 3121da177e4SLinus Torvalds out_bad: 3131da177e4SLinus Torvalds nfs_async_read_error(head); 3141da177e4SLinus Torvalds return -ENOMEM; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* 3180b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 3190b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 3200b671301STrond Myklebust */ 3210b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 3220b671301STrond Myklebust { 3230b671301STrond Myklebust int status; 3240b671301STrond Myklebust 325a3f565b1SChuck Lever dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid, 3260b671301STrond Myklebust task->tk_status); 3270b671301STrond Myklebust 3280b671301STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 3290b671301STrond Myklebust if (status != 0) 3300b671301STrond Myklebust return status; 3310b671301STrond Myklebust 3320b671301STrond Myklebust nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); 3330b671301STrond Myklebust 3340b671301STrond Myklebust if (task->tk_status == -ESTALE) { 3353a10c30aSBenny Halevy set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags); 3360b671301STrond Myklebust nfs_mark_for_revalidate(data->inode); 3370b671301STrond Myklebust } 3380b671301STrond Myklebust return 0; 3390b671301STrond Myklebust } 3400b671301STrond Myklebust 341*fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) 3420b671301STrond Myklebust { 3430b671301STrond Myklebust struct nfs_readargs *argp = &data->args; 3440b671301STrond Myklebust struct nfs_readres *resp = &data->res; 3450b671301STrond Myklebust 3460b671301STrond Myklebust if (resp->eof || resp->count == argp->count) 347*fdd1e74cSTrond Myklebust return; 3480b671301STrond Myklebust 3490b671301STrond Myklebust /* This is a short read! */ 3500b671301STrond Myklebust nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 3510b671301STrond Myklebust /* Has the server at least made some progress? */ 3520b671301STrond Myklebust if (resp->count == 0) 353*fdd1e74cSTrond Myklebust return; 3540b671301STrond Myklebust 3550b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 3560b671301STrond Myklebust argp->offset += resp->count; 3570b671301STrond Myklebust argp->pgbase += resp->count; 3580b671301STrond Myklebust argp->count -= resp->count; 3590b671301STrond Myklebust rpc_restart_call(task); 3600b671301STrond Myklebust } 3610b671301STrond Myklebust 3620b671301STrond Myklebust /* 3631da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 3641da177e4SLinus Torvalds */ 365ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 3661da177e4SLinus Torvalds { 367ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 3681da177e4SLinus Torvalds 369ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 370ec06c096STrond Myklebust return; 371*fdd1e74cSTrond Myklebust if (task->tk_status < 0) 3720b671301STrond Myklebust return; 373*fdd1e74cSTrond Myklebust 374*fdd1e74cSTrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 375*fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 3760b671301STrond Myklebust } 377*fdd1e74cSTrond Myklebust 378*fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata) 379*fdd1e74cSTrond Myklebust { 380*fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 381*fdd1e74cSTrond Myklebust struct nfs_page *req = data->req; 382*fdd1e74cSTrond Myklebust struct page *page = req->wb_page; 383*fdd1e74cSTrond Myklebust int status = data->task.tk_status; 384*fdd1e74cSTrond Myklebust 385*fdd1e74cSTrond Myklebust if (status < 0) 3860b671301STrond Myklebust SetPageError(page); 387*fdd1e74cSTrond Myklebust 3881da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 3891da177e4SLinus Torvalds if (!PageError(page)) 3901da177e4SLinus Torvalds SetPageUptodate(page); 3911da177e4SLinus Torvalds nfs_readpage_release(req); 3921da177e4SLinus Torvalds } 393*fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 396ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 397ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 398*fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_partial, 399ec06c096STrond Myklebust }; 400ec06c096STrond Myklebust 4011de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 4021de3fc12STrond Myklebust { 4031de3fc12STrond Myklebust unsigned int count = data->res.count; 4041de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 4051de3fc12STrond Myklebust struct page **pages; 4061de3fc12STrond Myklebust 40779558f36STrond Myklebust if (data->res.eof) 40879558f36STrond Myklebust count = data->args.count; 4091de3fc12STrond Myklebust if (unlikely(count == 0)) 4101de3fc12STrond Myklebust return; 4111de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 4121de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 4131de3fc12STrond Myklebust count += base; 4141de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 4151de3fc12STrond Myklebust SetPageUptodate(*pages); 4160b671301STrond Myklebust if (count == 0) 4170b671301STrond Myklebust return; 4180b671301STrond Myklebust /* Was this a short read? */ 4190b671301STrond Myklebust if (data->res.eof || data->res.count == data->args.count) 4201de3fc12STrond Myklebust SetPageUptodate(*pages); 4211de3fc12STrond Myklebust } 4221de3fc12STrond Myklebust 4231da177e4SLinus Torvalds /* 4241da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 4251da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 4261da177e4SLinus Torvalds */ 427ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 4281da177e4SLinus Torvalds { 429ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 4301da177e4SLinus Torvalds 4310b671301STrond Myklebust if (nfs_readpage_result(task, data) != 0) 4320b671301STrond Myklebust return; 433*fdd1e74cSTrond Myklebust if (task->tk_status < 0) 434*fdd1e74cSTrond 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 nfs_readpage_truncate_uninitialised_page(data); 4421de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 443*fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 4440b671301STrond Myklebust } 445*fdd1e74cSTrond Myklebust 446*fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata) 447*fdd1e74cSTrond Myklebust { 448*fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 449*fdd1e74cSTrond Myklebust 4501da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 4511da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 4521da177e4SLinus Torvalds 4531de3fc12STrond Myklebust nfs_list_remove_request(req); 4541da177e4SLinus Torvalds nfs_readpage_release(req); 4551da177e4SLinus Torvalds } 456*fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 459ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 460ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 461*fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_full, 462ec06c096STrond Myklebust }; 463ec06c096STrond Myklebust 4641da177e4SLinus Torvalds /* 4651da177e4SLinus Torvalds * Read a page over NFS. 4661da177e4SLinus Torvalds * We read the page synchronously in the following case: 4671da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 4681da177e4SLinus Torvalds * previous async read operation failed. 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 4711da177e4SLinus Torvalds { 4721da177e4SLinus Torvalds struct nfs_open_context *ctx; 4731da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 4741da177e4SLinus Torvalds int error; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 4771da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 47891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 47991d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 48091d5b470SChuck Lever 4811da177e4SLinus Torvalds /* 4821da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 4831da177e4SLinus Torvalds * 4841da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 4851da177e4SLinus Torvalds * be any new pending writes generated at this point 4861da177e4SLinus Torvalds * for this page (other pages can be written to). 4871da177e4SLinus Torvalds */ 4881da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 4891da177e4SLinus Torvalds if (error) 490de05a0ccSTrond Myklebust goto out_unlock; 491de05a0ccSTrond Myklebust if (PageUptodate(page)) 492de05a0ccSTrond Myklebust goto out_unlock; 4931da177e4SLinus Torvalds 4945f004cf2STrond Myklebust error = -ESTALE; 4955f004cf2STrond Myklebust if (NFS_STALE(inode)) 496de05a0ccSTrond Myklebust goto out_unlock; 4975f004cf2STrond Myklebust 4981da177e4SLinus Torvalds if (file == NULL) { 499cf1308ffSTrond Myklebust error = -EBADF; 500d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 5011da177e4SLinus Torvalds if (ctx == NULL) 502de05a0ccSTrond Myklebust goto out_unlock; 5031da177e4SLinus Torvalds } else 504cd3758e3STrond Myklebust ctx = get_nfs_open_context(nfs_file_open_context(file)); 5051da177e4SLinus Torvalds 5068e0969f0STrond Myklebust error = nfs_readpage_async(ctx, inode, page); 5078e0969f0STrond Myklebust 5081da177e4SLinus Torvalds put_nfs_open_context(ctx); 5091da177e4SLinus Torvalds return error; 510de05a0ccSTrond Myklebust out_unlock: 5111da177e4SLinus Torvalds unlock_page(page); 5121da177e4SLinus Torvalds return error; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds struct nfs_readdesc { 5168b09bee3STrond Myklebust struct nfs_pageio_descriptor *pgio; 5171da177e4SLinus Torvalds struct nfs_open_context *ctx; 5181da177e4SLinus Torvalds }; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds static int 5211da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 5241da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 5251da177e4SLinus Torvalds struct nfs_page *new; 5261da177e4SLinus Torvalds unsigned int len; 527de05a0ccSTrond Myklebust int error; 5281da177e4SLinus Torvalds 529de05a0ccSTrond Myklebust error = nfs_wb_page(inode, page); 530de05a0ccSTrond Myklebust if (error) 531de05a0ccSTrond Myklebust goto out_unlock; 532de05a0ccSTrond Myklebust if (PageUptodate(page)) 533de05a0ccSTrond Myklebust goto out_unlock; 534de05a0ccSTrond Myklebust 53549a70f27STrond Myklebust len = nfs_page_length(page); 5361da177e4SLinus Torvalds if (len == 0) 5371da177e4SLinus Torvalds return nfs_return_empty_page(page); 538de05a0ccSTrond Myklebust 5391da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 540de05a0ccSTrond Myklebust if (IS_ERR(new)) 541de05a0ccSTrond Myklebust goto out_error; 542de05a0ccSTrond Myklebust 5431da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 544eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 545f8512ad0SFred Isaman if (!nfs_pageio_add_request(desc->pgio, new)) { 546f8512ad0SFred Isaman error = desc->pgio->pg_error; 547f8512ad0SFred Isaman goto out_unlock; 548f8512ad0SFred Isaman } 5491da177e4SLinus Torvalds return 0; 550de05a0ccSTrond Myklebust out_error: 551de05a0ccSTrond Myklebust error = PTR_ERR(new); 552de05a0ccSTrond Myklebust SetPageError(page); 553de05a0ccSTrond Myklebust out_unlock: 554de05a0ccSTrond Myklebust unlock_page(page); 555de05a0ccSTrond Myklebust return error; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 5591da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 5601da177e4SLinus Torvalds { 5618b09bee3STrond Myklebust struct nfs_pageio_descriptor pgio; 5621da177e4SLinus Torvalds struct nfs_readdesc desc = { 5638b09bee3STrond Myklebust .pgio = &pgio, 5641da177e4SLinus Torvalds }; 5651da177e4SLinus Torvalds struct inode *inode = mapping->host; 5661da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 5678b09bee3STrond Myklebust size_t rsize = server->rsize; 5688b09bee3STrond Myklebust unsigned long npages; 5695f004cf2STrond Myklebust int ret = -ESTALE; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 5721da177e4SLinus Torvalds inode->i_sb->s_id, 5731da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 5741da177e4SLinus Torvalds nr_pages); 57591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 5761da177e4SLinus Torvalds 5775f004cf2STrond Myklebust if (NFS_STALE(inode)) 5785f004cf2STrond Myklebust goto out; 5795f004cf2STrond Myklebust 5801da177e4SLinus Torvalds if (filp == NULL) { 581d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 5821da177e4SLinus Torvalds if (desc.ctx == NULL) 5831da177e4SLinus Torvalds return -EBADF; 5841da177e4SLinus Torvalds } else 585cd3758e3STrond Myklebust desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); 5868b09bee3STrond Myklebust if (rsize < PAGE_CACHE_SIZE) 5878b09bee3STrond Myklebust nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0); 5888b09bee3STrond Myklebust else 5898b09bee3STrond Myklebust nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0); 5908b09bee3STrond Myklebust 5911da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 5928b09bee3STrond Myklebust 5938b09bee3STrond Myklebust nfs_pageio_complete(&pgio); 5948b09bee3STrond Myklebust npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 5958b09bee3STrond Myklebust nfs_add_stats(inode, NFSIOS_READPAGES, npages); 5961da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 5975f004cf2STrond Myklebust out: 5981da177e4SLinus Torvalds return ret; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 601f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 6021da177e4SLinus Torvalds { 6031da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 6041da177e4SLinus Torvalds sizeof(struct nfs_read_data), 6051da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 60620c2df83SPaul Mundt NULL); 6071da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 6081da177e4SLinus Torvalds return -ENOMEM; 6091da177e4SLinus Torvalds 61093d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 6111da177e4SLinus Torvalds nfs_rdata_cachep); 6121da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 6131da177e4SLinus Torvalds return -ENOMEM; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds return 0; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 618266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 6211a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 6221da177e4SLinus Torvalds } 623