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> 2164419a9bSAndy Adamson #include <linux/module.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <asm/system.h> 24bae724efSFred Isaman #include "pnfs.h" 251da177e4SLinus Torvalds 26f11c88afSAndy Adamson #include "nfs4_fs.h" 2749a70f27STrond Myklebust #include "internal.h" 2891d5b470SChuck Lever #include "iostat.h" 299a9fc1c0SDavid Howells #include "fscache.h" 3091d5b470SChuck Lever 311da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 321da177e4SLinus Torvalds 33c76069bdSFred Isaman static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc); 34c76069bdSFred Isaman static int nfs_pagein_one(struct nfs_pageio_descriptor *desc); 35*1751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops; 36ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops; 37ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops; 381da177e4SLinus Torvalds 39e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep; 403feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define MIN_POOL_READ (32) 431da177e4SLinus Torvalds 448d5658c9STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount) 453feb2d49STrond Myklebust { 4693870d76STrond Myklebust struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_KERNEL); 473feb2d49STrond Myklebust 483feb2d49STrond Myklebust if (p) { 493feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 503feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 51e9f7bee1STrond Myklebust p->npages = pagecount; 520d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 530d0b5cb3SChuck Lever p->pagevec = p->page_array; 543feb2d49STrond Myklebust else { 5593870d76STrond Myklebust p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL); 560d0b5cb3SChuck Lever if (!p->pagevec) { 573feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 583feb2d49STrond Myklebust p = NULL; 593feb2d49STrond Myklebust } 603feb2d49STrond Myklebust } 613feb2d49STrond Myklebust } 623feb2d49STrond Myklebust return p; 633feb2d49STrond Myklebust } 643feb2d49STrond Myklebust 651ae88b2eSTrond Myklebust void nfs_readdata_free(struct nfs_read_data *p) 663feb2d49STrond Myklebust { 673feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 683feb2d49STrond Myklebust kfree(p->pagevec); 693feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 703feb2d49STrond Myklebust } 713feb2d49STrond Myklebust 721ae88b2eSTrond Myklebust static void nfs_readdata_release(struct nfs_read_data *rdata) 731da177e4SLinus Torvalds { 74bae724efSFred Isaman put_lseg(rdata->lseg); 75383ba719STrond Myklebust put_nfs_open_context(rdata->args.context); 76383ba719STrond Myklebust nfs_readdata_free(rdata); 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds static 801da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 811da177e4SLinus Torvalds { 82eebd2aa3SChristoph Lameter zero_user(page, 0, PAGE_CACHE_SIZE); 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) { 106eebd2aa3SChristoph Lameter zero_user(*pages, base, remainder); 10779558f36STrond Myklebust break; 10879558f36STrond Myklebust } 109eebd2aa3SChristoph Lameter zero_user(*pages, base, pglen); 11079558f36STrond Myklebust pages++; 11179558f36STrond Myklebust remainder -= pglen; 11279558f36STrond Myklebust pglen = PAGE_CACHE_SIZE; 11379558f36STrond Myklebust base = 0; 11479558f36STrond Myklebust } 1151de3fc12STrond Myklebust } 1161de3fc12STrond Myklebust 117*1751c363STrond Myklebust static void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio, 118*1751c363STrond Myklebust struct inode *inode) 119*1751c363STrond Myklebust { 120*1751c363STrond Myklebust nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops, 121*1751c363STrond Myklebust NFS_SERVER(inode)->rsize, 0); 122*1751c363STrond Myklebust } 123*1751c363STrond Myklebust 124*1751c363STrond Myklebust static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, 125*1751c363STrond Myklebust struct inode *inode) 126*1751c363STrond Myklebust { 127*1751c363STrond Myklebust if (!pnfs_pageio_init_read(pgio, inode)) 128*1751c363STrond Myklebust nfs_pageio_init_read_mds(pgio, inode); 129*1751c363STrond Myklebust } 130*1751c363STrond Myklebust 131f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 1321da177e4SLinus Torvalds struct page *page) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct nfs_page *new; 1351da177e4SLinus Torvalds unsigned int len; 136c76069bdSFred Isaman struct nfs_pageio_descriptor pgio; 1371da177e4SLinus Torvalds 13849a70f27STrond Myklebust len = nfs_page_length(page); 1391da177e4SLinus Torvalds if (len == 0) 1401da177e4SLinus Torvalds return nfs_return_empty_page(page); 1411da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 1421da177e4SLinus Torvalds if (IS_ERR(new)) { 1431da177e4SLinus Torvalds unlock_page(page); 1441da177e4SLinus Torvalds return PTR_ERR(new); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 147eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 1481da177e4SLinus Torvalds 149*1751c363STrond Myklebust nfs_pageio_init_read(&pgio, inode); 150c76069bdSFred Isaman nfs_list_add_request(new, &pgio.pg_list); 151c76069bdSFred Isaman pgio.pg_count = len; 152c76069bdSFred Isaman 153*1751c363STrond Myklebust nfs_pageio_complete(&pgio); 1541da177e4SLinus Torvalds return 0; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 1581da177e4SLinus Torvalds { 1597f8e05f6SDavid Howells struct inode *d_inode = req->wb_context->path.dentry->d_inode; 1607f8e05f6SDavid Howells 1617f8e05f6SDavid Howells if (PageUptodate(req->wb_page)) 1627f8e05f6SDavid Howells nfs_readpage_to_fscache(d_inode, req->wb_page, 0); 1637f8e05f6SDavid Howells 1641da177e4SLinus Torvalds unlock_page(req->wb_page); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 16788be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 16888be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1691da177e4SLinus Torvalds req->wb_bytes, 1701da177e4SLinus Torvalds (long long)req_offset(req)); 17110d2c46fSNick Wilson nfs_release_request(req); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 174dc70d7b3SAndy Adamson int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt, 17564419a9bSAndy Adamson const struct rpc_call_ops *call_ops) 17664419a9bSAndy Adamson { 17764419a9bSAndy Adamson struct inode *inode = data->inode; 17864419a9bSAndy Adamson int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; 17964419a9bSAndy Adamson struct rpc_task *task; 18064419a9bSAndy Adamson struct rpc_message msg = { 18164419a9bSAndy Adamson .rpc_argp = &data->args, 18264419a9bSAndy Adamson .rpc_resp = &data->res, 18364419a9bSAndy Adamson .rpc_cred = data->cred, 18464419a9bSAndy Adamson }; 18564419a9bSAndy Adamson struct rpc_task_setup task_setup_data = { 18664419a9bSAndy Adamson .task = &data->task, 18764419a9bSAndy Adamson .rpc_client = clnt, 18864419a9bSAndy Adamson .rpc_message = &msg, 18964419a9bSAndy Adamson .callback_ops = call_ops, 19064419a9bSAndy Adamson .callback_data = data, 19164419a9bSAndy Adamson .workqueue = nfsiod_workqueue, 19264419a9bSAndy Adamson .flags = RPC_TASK_ASYNC | swap_flags, 19364419a9bSAndy Adamson }; 19464419a9bSAndy Adamson 19564419a9bSAndy Adamson /* Set up the initial task struct. */ 19664419a9bSAndy Adamson NFS_PROTO(inode)->read_setup(data, &msg); 19764419a9bSAndy Adamson 19864419a9bSAndy Adamson dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ " 19964419a9bSAndy Adamson "offset %llu)\n", 20064419a9bSAndy Adamson data->task.tk_pid, 20164419a9bSAndy Adamson inode->i_sb->s_id, 20264419a9bSAndy Adamson (long long)NFS_FILEID(inode), 20364419a9bSAndy Adamson data->args.count, 20464419a9bSAndy Adamson (unsigned long long)data->args.offset); 20564419a9bSAndy Adamson 20664419a9bSAndy Adamson task = rpc_run_task(&task_setup_data); 20764419a9bSAndy Adamson if (IS_ERR(task)) 20864419a9bSAndy Adamson return PTR_ERR(task); 20964419a9bSAndy Adamson rpc_put_task(task); 21064419a9bSAndy Adamson return 0; 21164419a9bSAndy Adamson } 212dc70d7b3SAndy Adamson EXPORT_SYMBOL_GPL(nfs_initiate_read); 21364419a9bSAndy Adamson 2141da177e4SLinus Torvalds /* 2151da177e4SLinus Torvalds * Set up the NFS read request struct 2161da177e4SLinus Torvalds */ 217dbae4c73STrond Myklebust static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 218ec06c096STrond Myklebust const struct rpc_call_ops *call_ops, 219bae724efSFred Isaman unsigned int count, unsigned int offset, 220bae724efSFred Isaman struct pnfs_layout_segment *lseg) 2211da177e4SLinus Torvalds { 22284115e1cSTrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds data->req = req; 22584115e1cSTrond Myklebust data->inode = inode; 22664419a9bSAndy Adamson data->cred = req->wb_context->cred; 227bae724efSFred Isaman data->lseg = get_lseg(lseg); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 2301da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 2311da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 2321da177e4SLinus Torvalds data->args.pages = data->pagevec; 2331da177e4SLinus Torvalds data->args.count = count; 234383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 235f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds data->res.fattr = &data->fattr; 2381da177e4SLinus Torvalds data->res.count = count; 2391da177e4SLinus Torvalds data->res.eof = 0; 2400e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 2411da177e4SLinus Torvalds 24264419a9bSAndy Adamson if (data->lseg && 24364419a9bSAndy Adamson (pnfs_try_to_read_data(data, call_ops) == PNFS_ATTEMPTED)) 244dbae4c73STrond Myklebust return 0; 24564419a9bSAndy Adamson 24664419a9bSAndy Adamson return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops); 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds static void 2501da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 2511da177e4SLinus Torvalds { 2521da177e4SLinus Torvalds struct nfs_page *req; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds while (!list_empty(head)) { 2551da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2561da177e4SLinus Torvalds nfs_list_remove_request(req); 2571da177e4SLinus Torvalds SetPageError(req->wb_page); 2581da177e4SLinus Torvalds nfs_readpage_release(req); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* 2631da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 2641da177e4SLinus Torvalds * 2651da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 2661da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 2671da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 2681da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 2691da177e4SLinus Torvalds * 2701da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 2711da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 2721da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 2731da177e4SLinus Torvalds * or less conventional NFS client behavior. 2741da177e4SLinus Torvalds */ 275c76069bdSFred Isaman static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc) 2761da177e4SLinus Torvalds { 277c76069bdSFred Isaman struct nfs_page *req = nfs_list_entry(desc->pg_list.next); 2781da177e4SLinus Torvalds struct page *page = req->wb_page; 2791da177e4SLinus Torvalds struct nfs_read_data *data; 280c76069bdSFred Isaman size_t rsize = NFS_SERVER(desc->pg_inode)->rsize, nbytes; 281e9f7bee1STrond Myklebust unsigned int offset; 2821da177e4SLinus Torvalds int requests = 0; 283dbae4c73STrond Myklebust int ret = 0; 284c76069bdSFred Isaman struct pnfs_layout_segment *lseg; 2851da177e4SLinus Torvalds LIST_HEAD(list); 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds nfs_list_remove_request(req); 2881da177e4SLinus Torvalds 289c76069bdSFred Isaman nbytes = desc->pg_count; 290e9f7bee1STrond Myklebust do { 291e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 292e9f7bee1STrond Myklebust 2938d5658c9STrond Myklebust data = nfs_readdata_alloc(1); 2941da177e4SLinus Torvalds if (!data) 2951da177e4SLinus Torvalds goto out_bad; 2961da177e4SLinus Torvalds list_add(&data->pages, &list); 2971da177e4SLinus Torvalds requests++; 298e9f7bee1STrond Myklebust nbytes -= len; 299e9f7bee1STrond Myklebust } while(nbytes != 0); 3001da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 3011da177e4SLinus Torvalds 302c76069bdSFred Isaman BUG_ON(desc->pg_lseg != NULL); 303fb3296ebSBenny Halevy lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, 304fb3296ebSBenny Halevy req_offset(req), desc->pg_count, 305fb3296ebSBenny Halevy IOMODE_READ, GFP_KERNEL); 3061da177e4SLinus Torvalds ClearPageError(page); 3071da177e4SLinus Torvalds offset = 0; 308c76069bdSFred Isaman nbytes = desc->pg_count; 3091da177e4SLinus Torvalds do { 310dbae4c73STrond Myklebust int ret2; 311dbae4c73STrond Myklebust 3121da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3131da177e4SLinus Torvalds list_del_init(&data->pages); 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds data->pagevec[0] = page; 3161da177e4SLinus Torvalds 317bcb71bbaSTrond Myklebust if (nbytes < rsize) 318bcb71bbaSTrond Myklebust rsize = nbytes; 319dbae4c73STrond Myklebust ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 320bae724efSFred Isaman rsize, offset, lseg); 321dbae4c73STrond Myklebust if (ret == 0) 322dbae4c73STrond Myklebust ret = ret2; 3231da177e4SLinus Torvalds offset += rsize; 3241da177e4SLinus Torvalds nbytes -= rsize; 3251da177e4SLinus Torvalds } while (nbytes != 0); 326bae724efSFred Isaman put_lseg(lseg); 32736fe432dSFred Isaman desc->pg_lseg = NULL; 3281da177e4SLinus Torvalds 329dbae4c73STrond Myklebust return ret; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds out_bad: 3321da177e4SLinus Torvalds while (!list_empty(&list)) { 3331da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 3341da177e4SLinus Torvalds list_del(&data->pages); 3351da177e4SLinus Torvalds nfs_readdata_free(data); 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds SetPageError(page); 3381da177e4SLinus Torvalds nfs_readpage_release(req); 3391da177e4SLinus Torvalds return -ENOMEM; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 342c76069bdSFred Isaman static int nfs_pagein_one(struct nfs_pageio_descriptor *desc) 3431da177e4SLinus Torvalds { 3441da177e4SLinus Torvalds struct nfs_page *req; 3451da177e4SLinus Torvalds struct page **pages; 3461da177e4SLinus Torvalds struct nfs_read_data *data; 347c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 348c76069bdSFred Isaman struct pnfs_layout_segment *lseg = desc->pg_lseg; 349dbae4c73STrond Myklebust int ret = -ENOMEM; 3501da177e4SLinus Torvalds 351c76069bdSFred Isaman data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base, 352c76069bdSFred Isaman desc->pg_count)); 353bae724efSFred Isaman if (!data) { 354bae724efSFred Isaman nfs_async_read_error(head); 355bae724efSFred Isaman goto out; 356bae724efSFred Isaman } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds pages = data->pagevec; 3591da177e4SLinus Torvalds while (!list_empty(head)) { 3601da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3611da177e4SLinus Torvalds nfs_list_remove_request(req); 3621da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 3631da177e4SLinus Torvalds ClearPageError(req->wb_page); 3641da177e4SLinus Torvalds *pages++ = req->wb_page; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 367bae724efSFred Isaman if ((!lseg) && list_is_singular(&data->pages)) 368fb3296ebSBenny Halevy lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, 369fb3296ebSBenny Halevy req_offset(req), desc->pg_count, 370fb3296ebSBenny Halevy IOMODE_READ, GFP_KERNEL); 3711da177e4SLinus Torvalds 372c76069bdSFred Isaman ret = nfs_read_rpcsetup(req, data, &nfs_read_full_ops, desc->pg_count, 373c76069bdSFred Isaman 0, lseg); 374bae724efSFred Isaman out: 375bae724efSFred Isaman put_lseg(lseg); 37636fe432dSFred Isaman desc->pg_lseg = NULL; 377dbae4c73STrond Myklebust return ret; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 380*1751c363STrond Myklebust int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) 381*1751c363STrond Myklebust { 382*1751c363STrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 383*1751c363STrond Myklebust return nfs_pagein_multi(desc); 384*1751c363STrond Myklebust return nfs_pagein_one(desc); 385*1751c363STrond Myklebust } 386*1751c363STrond Myklebust EXPORT_SYMBOL_GPL(nfs_generic_pg_readpages); 387*1751c363STrond Myklebust 388*1751c363STrond Myklebust 389*1751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops = { 390*1751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 391*1751c363STrond Myklebust .pg_doio = nfs_generic_pg_readpages, 392*1751c363STrond Myklebust }; 393*1751c363STrond Myklebust 3941da177e4SLinus Torvalds /* 3950b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 3960b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 3970b671301STrond Myklebust */ 3980b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 3990b671301STrond Myklebust { 4000b671301STrond Myklebust int status; 4010b671301STrond Myklebust 4023110ff80SHarvey Harrison dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid, 4030b671301STrond Myklebust task->tk_status); 4040b671301STrond Myklebust 4050b671301STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 4060b671301STrond Myklebust if (status != 0) 4070b671301STrond Myklebust return status; 4080b671301STrond Myklebust 4090b671301STrond Myklebust nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); 4100b671301STrond Myklebust 4110b671301STrond Myklebust if (task->tk_status == -ESTALE) { 4123a10c30aSBenny Halevy set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags); 4130b671301STrond Myklebust nfs_mark_for_revalidate(data->inode); 4140b671301STrond Myklebust } 4150b671301STrond Myklebust return 0; 4160b671301STrond Myklebust } 4170b671301STrond Myklebust 418fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) 4190b671301STrond Myklebust { 4200b671301STrond Myklebust struct nfs_readargs *argp = &data->args; 4210b671301STrond Myklebust struct nfs_readres *resp = &data->res; 4220b671301STrond Myklebust 4230b671301STrond Myklebust if (resp->eof || resp->count == argp->count) 424d61e612aSTrond Myklebust return; 4250b671301STrond Myklebust 4260b671301STrond Myklebust /* This is a short read! */ 4270b671301STrond Myklebust nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 4280b671301STrond Myklebust /* Has the server at least made some progress? */ 4290b671301STrond Myklebust if (resp->count == 0) 430d61e612aSTrond Myklebust return; 4310b671301STrond Myklebust 4320b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 433cbdabc7fSAndy Adamson data->mds_offset += resp->count; 4340b671301STrond Myklebust argp->offset += resp->count; 4350b671301STrond Myklebust argp->pgbase += resp->count; 4360b671301STrond Myklebust argp->count -= resp->count; 4370110ee15STrond Myklebust nfs_restart_rpc(task, NFS_SERVER(data->inode)->nfs_client); 4380b671301STrond Myklebust } 4390b671301STrond Myklebust 4400b671301STrond Myklebust /* 4411da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 4421da177e4SLinus Torvalds */ 443ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 4441da177e4SLinus Torvalds { 445ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 4461da177e4SLinus Torvalds 447ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 448ec06c096STrond Myklebust return; 449fdd1e74cSTrond Myklebust if (task->tk_status < 0) 4500b671301STrond Myklebust return; 451fdd1e74cSTrond Myklebust 452fdd1e74cSTrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 453fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 4540b671301STrond Myklebust } 455fdd1e74cSTrond Myklebust 456fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata) 457fdd1e74cSTrond Myklebust { 458fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 459fdd1e74cSTrond Myklebust struct nfs_page *req = data->req; 460fdd1e74cSTrond Myklebust struct page *page = req->wb_page; 461fdd1e74cSTrond Myklebust int status = data->task.tk_status; 462fdd1e74cSTrond Myklebust 463fdd1e74cSTrond Myklebust if (status < 0) 4640b671301STrond Myklebust SetPageError(page); 465fdd1e74cSTrond Myklebust 4661da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 4671da177e4SLinus Torvalds if (!PageError(page)) 4681da177e4SLinus Torvalds SetPageUptodate(page); 4691da177e4SLinus Torvalds nfs_readpage_release(req); 4701da177e4SLinus Torvalds } 471fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 474f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1) 475f11c88afSAndy Adamson void nfs_read_prepare(struct rpc_task *task, void *calldata) 476f11c88afSAndy Adamson { 477f11c88afSAndy Adamson struct nfs_read_data *data = calldata; 478f11c88afSAndy Adamson 479035168abSTrond Myklebust if (nfs4_setup_sequence(NFS_SERVER(data->inode), 480f11c88afSAndy Adamson &data->args.seq_args, &data->res.seq_res, 481f11c88afSAndy Adamson 0, task)) 482f11c88afSAndy Adamson return; 483f11c88afSAndy Adamson rpc_call_start(task); 484f11c88afSAndy Adamson } 485f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 486f11c88afSAndy Adamson 487ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 488f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1) 489f11c88afSAndy Adamson .rpc_call_prepare = nfs_read_prepare, 490f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 491ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 492fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_partial, 493ec06c096STrond Myklebust }; 494ec06c096STrond Myklebust 4951de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 4961de3fc12STrond Myklebust { 4971de3fc12STrond Myklebust unsigned int count = data->res.count; 4981de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 4991de3fc12STrond Myklebust struct page **pages; 5001de3fc12STrond Myklebust 50179558f36STrond Myklebust if (data->res.eof) 50279558f36STrond Myklebust count = data->args.count; 5031de3fc12STrond Myklebust if (unlikely(count == 0)) 5041de3fc12STrond Myklebust return; 5051de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 5061de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 5071de3fc12STrond Myklebust count += base; 5081de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 5091de3fc12STrond Myklebust SetPageUptodate(*pages); 5100b671301STrond Myklebust if (count == 0) 5110b671301STrond Myklebust return; 5120b671301STrond Myklebust /* Was this a short read? */ 5130b671301STrond Myklebust if (data->res.eof || data->res.count == data->args.count) 5141de3fc12STrond Myklebust SetPageUptodate(*pages); 5151de3fc12STrond Myklebust } 5161de3fc12STrond Myklebust 5171da177e4SLinus Torvalds /* 5181da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 5191da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 5201da177e4SLinus Torvalds */ 521ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 5221da177e4SLinus Torvalds { 523ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 5241da177e4SLinus Torvalds 5250b671301STrond Myklebust if (nfs_readpage_result(task, data) != 0) 5260b671301STrond Myklebust return; 527fdd1e74cSTrond Myklebust if (task->tk_status < 0) 528fdd1e74cSTrond Myklebust return; 5291de3fc12STrond Myklebust /* 5300b671301STrond Myklebust * Note: nfs_readpage_retry may change the values of 5311de3fc12STrond Myklebust * data->args. In the multi-page case, we therefore need 5320b671301STrond Myklebust * to ensure that we call nfs_readpage_set_pages_uptodate() 5330b671301STrond Myklebust * first. 5341de3fc12STrond Myklebust */ 5351de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 5361de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 537fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 5380b671301STrond Myklebust } 539fdd1e74cSTrond Myklebust 540fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata) 541fdd1e74cSTrond Myklebust { 542fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 543fdd1e74cSTrond Myklebust 5441da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 5451da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 5461da177e4SLinus Torvalds 5471de3fc12STrond Myklebust nfs_list_remove_request(req); 5481da177e4SLinus Torvalds nfs_readpage_release(req); 5491da177e4SLinus Torvalds } 550fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 553ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 554f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1) 555f11c88afSAndy Adamson .rpc_call_prepare = nfs_read_prepare, 556f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 557ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 558fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_full, 559ec06c096STrond Myklebust }; 560ec06c096STrond Myklebust 5611da177e4SLinus Torvalds /* 5621da177e4SLinus Torvalds * Read a page over NFS. 5631da177e4SLinus Torvalds * We read the page synchronously in the following case: 5641da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 5651da177e4SLinus Torvalds * previous async read operation failed. 5661da177e4SLinus Torvalds */ 5671da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 5681da177e4SLinus Torvalds { 5691da177e4SLinus Torvalds struct nfs_open_context *ctx; 5701da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 5711da177e4SLinus Torvalds int error; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 5741da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 57591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 57691d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 57791d5b470SChuck Lever 5781da177e4SLinus Torvalds /* 5791da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 5801da177e4SLinus Torvalds * 5811da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 5821da177e4SLinus Torvalds * be any new pending writes generated at this point 5831da177e4SLinus Torvalds * for this page (other pages can be written to). 5841da177e4SLinus Torvalds */ 5851da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 5861da177e4SLinus Torvalds if (error) 587de05a0ccSTrond Myklebust goto out_unlock; 588de05a0ccSTrond Myklebust if (PageUptodate(page)) 589de05a0ccSTrond Myklebust goto out_unlock; 5901da177e4SLinus Torvalds 5915f004cf2STrond Myklebust error = -ESTALE; 5925f004cf2STrond Myklebust if (NFS_STALE(inode)) 593de05a0ccSTrond Myklebust goto out_unlock; 5945f004cf2STrond Myklebust 5951da177e4SLinus Torvalds if (file == NULL) { 596cf1308ffSTrond Myklebust error = -EBADF; 597d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 5981da177e4SLinus Torvalds if (ctx == NULL) 599de05a0ccSTrond Myklebust goto out_unlock; 6001da177e4SLinus Torvalds } else 601cd3758e3STrond Myklebust ctx = get_nfs_open_context(nfs_file_open_context(file)); 6021da177e4SLinus Torvalds 6039a9fc1c0SDavid Howells if (!IS_SYNC(inode)) { 6049a9fc1c0SDavid Howells error = nfs_readpage_from_fscache(ctx, inode, page); 6059a9fc1c0SDavid Howells if (error == 0) 6069a9fc1c0SDavid Howells goto out; 6079a9fc1c0SDavid Howells } 6089a9fc1c0SDavid Howells 6098e0969f0STrond Myklebust error = nfs_readpage_async(ctx, inode, page); 6108e0969f0STrond Myklebust 6119a9fc1c0SDavid Howells out: 6121da177e4SLinus Torvalds put_nfs_open_context(ctx); 6131da177e4SLinus Torvalds return error; 614de05a0ccSTrond Myklebust out_unlock: 6151da177e4SLinus Torvalds unlock_page(page); 6161da177e4SLinus Torvalds return error; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds struct nfs_readdesc { 6208b09bee3STrond Myklebust struct nfs_pageio_descriptor *pgio; 6211da177e4SLinus Torvalds struct nfs_open_context *ctx; 6221da177e4SLinus Torvalds }; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds static int 6251da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 6261da177e4SLinus Torvalds { 6271da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 6281da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 6291da177e4SLinus Torvalds struct nfs_page *new; 6301da177e4SLinus Torvalds unsigned int len; 631de05a0ccSTrond Myklebust int error; 6321da177e4SLinus Torvalds 63349a70f27STrond Myklebust len = nfs_page_length(page); 6341da177e4SLinus Torvalds if (len == 0) 6351da177e4SLinus Torvalds return nfs_return_empty_page(page); 636de05a0ccSTrond Myklebust 6371da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 638de05a0ccSTrond Myklebust if (IS_ERR(new)) 639de05a0ccSTrond Myklebust goto out_error; 640de05a0ccSTrond Myklebust 6411da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 642eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 643f8512ad0SFred Isaman if (!nfs_pageio_add_request(desc->pgio, new)) { 644f8512ad0SFred Isaman error = desc->pgio->pg_error; 645f8512ad0SFred Isaman goto out_unlock; 646f8512ad0SFred Isaman } 6471da177e4SLinus Torvalds return 0; 648de05a0ccSTrond Myklebust out_error: 649de05a0ccSTrond Myklebust error = PTR_ERR(new); 650de05a0ccSTrond Myklebust SetPageError(page); 651de05a0ccSTrond Myklebust out_unlock: 652de05a0ccSTrond Myklebust unlock_page(page); 653de05a0ccSTrond Myklebust return error; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 6571da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 6581da177e4SLinus Torvalds { 6598b09bee3STrond Myklebust struct nfs_pageio_descriptor pgio; 6601da177e4SLinus Torvalds struct nfs_readdesc desc = { 6618b09bee3STrond Myklebust .pgio = &pgio, 6621da177e4SLinus Torvalds }; 6631da177e4SLinus Torvalds struct inode *inode = mapping->host; 6648b09bee3STrond Myklebust unsigned long npages; 6655f004cf2STrond Myklebust int ret = -ESTALE; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 6681da177e4SLinus Torvalds inode->i_sb->s_id, 6691da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 6701da177e4SLinus Torvalds nr_pages); 67191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 6721da177e4SLinus Torvalds 6735f004cf2STrond Myklebust if (NFS_STALE(inode)) 6745f004cf2STrond Myklebust goto out; 6755f004cf2STrond Myklebust 6761da177e4SLinus Torvalds if (filp == NULL) { 677d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 6781da177e4SLinus Torvalds if (desc.ctx == NULL) 6791da177e4SLinus Torvalds return -EBADF; 6801da177e4SLinus Torvalds } else 681cd3758e3STrond Myklebust desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); 6829a9fc1c0SDavid Howells 6839a9fc1c0SDavid Howells /* attempt to read as many of the pages as possible from the cache 6849a9fc1c0SDavid Howells * - this returns -ENOBUFS immediately if the cookie is negative 6859a9fc1c0SDavid Howells */ 6869a9fc1c0SDavid Howells ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, 6879a9fc1c0SDavid Howells pages, &nr_pages); 6889a9fc1c0SDavid Howells if (ret == 0) 6899a9fc1c0SDavid Howells goto read_complete; /* all pages were read */ 6909a9fc1c0SDavid Howells 691*1751c363STrond Myklebust nfs_pageio_init_read(&pgio, inode); 6928b09bee3STrond Myklebust 6931da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 6948b09bee3STrond Myklebust 6958b09bee3STrond Myklebust nfs_pageio_complete(&pgio); 6968b09bee3STrond Myklebust npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 6978b09bee3STrond Myklebust nfs_add_stats(inode, NFSIOS_READPAGES, npages); 6989a9fc1c0SDavid Howells read_complete: 6991da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 7005f004cf2STrond Myklebust out: 7011da177e4SLinus Torvalds return ret; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 704f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 7051da177e4SLinus Torvalds { 7061da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 7071da177e4SLinus Torvalds sizeof(struct nfs_read_data), 7081da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 70920c2df83SPaul Mundt NULL); 7101da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 7111da177e4SLinus Torvalds return -ENOMEM; 7121da177e4SLinus Torvalds 71393d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 7141da177e4SLinus Torvalds nfs_rdata_cachep); 7151da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 7161da177e4SLinus Torvalds return -ENOMEM; 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds return 0; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 721266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 7241a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 7251da177e4SLinus Torvalds } 726