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 331751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops; 34ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops; 35ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops; 361da177e4SLinus Torvalds 37e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep; 383feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define MIN_POOL_READ (32) 411da177e4SLinus Torvalds 428d5658c9STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount) 433feb2d49STrond Myklebust { 4493870d76STrond Myklebust struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_KERNEL); 453feb2d49STrond Myklebust 463feb2d49STrond Myklebust if (p) { 473feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 483feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 49e9f7bee1STrond Myklebust p->npages = pagecount; 500d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 510d0b5cb3SChuck Lever p->pagevec = p->page_array; 523feb2d49STrond Myklebust else { 5393870d76STrond Myklebust p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL); 540d0b5cb3SChuck Lever if (!p->pagevec) { 553feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 563feb2d49STrond Myklebust p = NULL; 573feb2d49STrond Myklebust } 583feb2d49STrond Myklebust } 593feb2d49STrond Myklebust } 603feb2d49STrond Myklebust return p; 613feb2d49STrond Myklebust } 623feb2d49STrond Myklebust 631ae88b2eSTrond Myklebust void nfs_readdata_free(struct nfs_read_data *p) 643feb2d49STrond Myklebust { 653feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 663feb2d49STrond Myklebust kfree(p->pagevec); 673feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 683feb2d49STrond Myklebust } 693feb2d49STrond Myklebust 70493292ddSTrond Myklebust void nfs_readdata_release(struct nfs_read_data *rdata) 711da177e4SLinus Torvalds { 72bae724efSFred Isaman put_lseg(rdata->lseg); 73383ba719STrond Myklebust put_nfs_open_context(rdata->args.context); 74383ba719STrond Myklebust nfs_readdata_free(rdata); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds static 781da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 791da177e4SLinus Torvalds { 80eebd2aa3SChristoph Lameter zero_user(page, 0, PAGE_CACHE_SIZE); 811da177e4SLinus Torvalds SetPageUptodate(page); 821da177e4SLinus Torvalds unlock_page(page); 831da177e4SLinus Torvalds return 0; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 861de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) 871de3fc12STrond Myklebust { 881de3fc12STrond Myklebust unsigned int remainder = data->args.count - data->res.count; 891de3fc12STrond Myklebust unsigned int base = data->args.pgbase + data->res.count; 901de3fc12STrond Myklebust unsigned int pglen; 911de3fc12STrond Myklebust struct page **pages; 921de3fc12STrond Myklebust 931de3fc12STrond Myklebust if (data->res.eof == 0 || remainder == 0) 941de3fc12STrond Myklebust return; 951de3fc12STrond Myklebust /* 961de3fc12STrond Myklebust * Note: "remainder" can never be negative, since we check for 971de3fc12STrond Myklebust * this in the XDR code. 981de3fc12STrond Myklebust */ 991de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 1001de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 1011de3fc12STrond Myklebust pglen = PAGE_CACHE_SIZE - base; 10279558f36STrond Myklebust for (;;) { 10379558f36STrond Myklebust if (remainder <= pglen) { 104eebd2aa3SChristoph Lameter zero_user(*pages, base, remainder); 10579558f36STrond Myklebust break; 10679558f36STrond Myklebust } 107eebd2aa3SChristoph Lameter zero_user(*pages, base, pglen); 10879558f36STrond Myklebust pages++; 10979558f36STrond Myklebust remainder -= pglen; 11079558f36STrond Myklebust pglen = PAGE_CACHE_SIZE; 11179558f36STrond Myklebust base = 0; 11279558f36STrond Myklebust } 1131de3fc12STrond Myklebust } 1141de3fc12STrond Myklebust 1151f945357STrond Myklebust static void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio, 1161751c363STrond Myklebust struct inode *inode) 1171751c363STrond Myklebust { 1181751c363STrond Myklebust nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops, 1191751c363STrond Myklebust NFS_SERVER(inode)->rsize, 0); 1201751c363STrond Myklebust } 1211751c363STrond Myklebust 122493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio) 123493292ddSTrond Myklebust { 124493292ddSTrond Myklebust pgio->pg_ops = &nfs_pageio_read_ops; 125493292ddSTrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize; 126493292ddSTrond Myklebust } 1271f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds); 128493292ddSTrond Myklebust 1291751c363STrond Myklebust static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, 1301751c363STrond Myklebust struct inode *inode) 1311751c363STrond Myklebust { 1321751c363STrond Myklebust if (!pnfs_pageio_init_read(pgio, inode)) 1331751c363STrond Myklebust nfs_pageio_init_read_mds(pgio, inode); 1341751c363STrond Myklebust } 1351751c363STrond Myklebust 136f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 1371da177e4SLinus Torvalds struct page *page) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds struct nfs_page *new; 1401da177e4SLinus Torvalds unsigned int len; 141c76069bdSFred Isaman struct nfs_pageio_descriptor pgio; 1421da177e4SLinus Torvalds 14349a70f27STrond Myklebust len = nfs_page_length(page); 1441da177e4SLinus Torvalds if (len == 0) 1451da177e4SLinus Torvalds return nfs_return_empty_page(page); 1461da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 1471da177e4SLinus Torvalds if (IS_ERR(new)) { 1481da177e4SLinus Torvalds unlock_page(page); 1491da177e4SLinus Torvalds return PTR_ERR(new); 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 152eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 1531da177e4SLinus Torvalds 1541751c363STrond Myklebust nfs_pageio_init_read(&pgio, inode); 155d8007d4dSTrond Myklebust nfs_pageio_add_request(&pgio, new); 1561751c363STrond Myklebust nfs_pageio_complete(&pgio); 1571da177e4SLinus Torvalds return 0; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 1611da177e4SLinus Torvalds { 1623d4ff43dSAl Viro struct inode *d_inode = req->wb_context->dentry->d_inode; 1637f8e05f6SDavid Howells 1647f8e05f6SDavid Howells if (PageUptodate(req->wb_page)) 1657f8e05f6SDavid Howells nfs_readpage_to_fscache(d_inode, req->wb_page, 0); 1667f8e05f6SDavid Howells 1671da177e4SLinus Torvalds unlock_page(req->wb_page); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 1703d4ff43dSAl Viro req->wb_context->dentry->d_inode->i_sb->s_id, 1713d4ff43dSAl Viro (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 1721da177e4SLinus Torvalds req->wb_bytes, 1731da177e4SLinus Torvalds (long long)req_offset(req)); 17410d2c46fSNick Wilson nfs_release_request(req); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 177dc70d7b3SAndy Adamson int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt, 17864419a9bSAndy Adamson const struct rpc_call_ops *call_ops) 17964419a9bSAndy Adamson { 18064419a9bSAndy Adamson struct inode *inode = data->inode; 18164419a9bSAndy Adamson int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; 18264419a9bSAndy Adamson struct rpc_task *task; 18364419a9bSAndy Adamson struct rpc_message msg = { 18464419a9bSAndy Adamson .rpc_argp = &data->args, 18564419a9bSAndy Adamson .rpc_resp = &data->res, 18664419a9bSAndy Adamson .rpc_cred = data->cred, 18764419a9bSAndy Adamson }; 18864419a9bSAndy Adamson struct rpc_task_setup task_setup_data = { 18964419a9bSAndy Adamson .task = &data->task, 19064419a9bSAndy Adamson .rpc_client = clnt, 19164419a9bSAndy Adamson .rpc_message = &msg, 19264419a9bSAndy Adamson .callback_ops = call_ops, 19364419a9bSAndy Adamson .callback_data = data, 19464419a9bSAndy Adamson .workqueue = nfsiod_workqueue, 19564419a9bSAndy Adamson .flags = RPC_TASK_ASYNC | swap_flags, 19664419a9bSAndy Adamson }; 19764419a9bSAndy Adamson 19864419a9bSAndy Adamson /* Set up the initial task struct. */ 19964419a9bSAndy Adamson NFS_PROTO(inode)->read_setup(data, &msg); 20064419a9bSAndy Adamson 20164419a9bSAndy Adamson dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ " 20264419a9bSAndy Adamson "offset %llu)\n", 20364419a9bSAndy Adamson data->task.tk_pid, 20464419a9bSAndy Adamson inode->i_sb->s_id, 20564419a9bSAndy Adamson (long long)NFS_FILEID(inode), 20664419a9bSAndy Adamson data->args.count, 20764419a9bSAndy Adamson (unsigned long long)data->args.offset); 20864419a9bSAndy Adamson 20964419a9bSAndy Adamson task = rpc_run_task(&task_setup_data); 21064419a9bSAndy Adamson if (IS_ERR(task)) 21164419a9bSAndy Adamson return PTR_ERR(task); 21264419a9bSAndy Adamson rpc_put_task(task); 21364419a9bSAndy Adamson return 0; 21464419a9bSAndy Adamson } 215dc70d7b3SAndy Adamson EXPORT_SYMBOL_GPL(nfs_initiate_read); 21664419a9bSAndy Adamson 2171da177e4SLinus Torvalds /* 2181da177e4SLinus Torvalds * Set up the NFS read request struct 2191da177e4SLinus Torvalds */ 2206e4efd56STrond Myklebust static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 2216e4efd56STrond Myklebust unsigned int count, unsigned int offset) 2221da177e4SLinus Torvalds { 2233d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds data->req = req; 22684115e1cSTrond Myklebust data->inode = inode; 22764419a9bSAndy Adamson data->cred = req->wb_context->cred; 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); 2416e4efd56STrond Myklebust } 2421da177e4SLinus Torvalds 2436e4efd56STrond Myklebust static int nfs_do_read(struct nfs_read_data *data, 244493292ddSTrond Myklebust const struct rpc_call_ops *call_ops) 2456e4efd56STrond Myklebust { 2465f00bcb3SStephen Rothwell struct inode *inode = data->args.context->dentry->d_inode; 2476e4efd56STrond Myklebust 24864419a9bSAndy Adamson return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 251275acaafSTrond Myklebust static int 252275acaafSTrond Myklebust nfs_do_multiple_reads(struct list_head *head, 253493292ddSTrond Myklebust const struct rpc_call_ops *call_ops) 254275acaafSTrond Myklebust { 255275acaafSTrond Myklebust struct nfs_read_data *data; 256275acaafSTrond Myklebust int ret = 0; 257275acaafSTrond Myklebust 258275acaafSTrond Myklebust while (!list_empty(head)) { 259275acaafSTrond Myklebust int ret2; 260275acaafSTrond Myklebust 261275acaafSTrond Myklebust data = list_entry(head->next, struct nfs_read_data, list); 262275acaafSTrond Myklebust list_del_init(&data->list); 263275acaafSTrond Myklebust 264493292ddSTrond Myklebust ret2 = nfs_do_read(data, call_ops); 265275acaafSTrond Myklebust if (ret == 0) 266275acaafSTrond Myklebust ret = ret2; 267275acaafSTrond Myklebust } 268275acaafSTrond Myklebust return ret; 269275acaafSTrond Myklebust } 270275acaafSTrond Myklebust 2711da177e4SLinus Torvalds static void 2721da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds struct nfs_page *req; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds while (!list_empty(head)) { 2771da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2781da177e4SLinus Torvalds nfs_list_remove_request(req); 2791da177e4SLinus Torvalds nfs_readpage_release(req); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds /* 2841da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 2851da177e4SLinus Torvalds * 2861da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 2871da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 2881da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 2891da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 2901da177e4SLinus Torvalds * 2911da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 2921da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 2931da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 2941da177e4SLinus Torvalds * or less conventional NFS client behavior. 2951da177e4SLinus Torvalds */ 296275acaafSTrond Myklebust static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res) 2971da177e4SLinus Torvalds { 298c76069bdSFred Isaman struct nfs_page *req = nfs_list_entry(desc->pg_list.next); 2991da177e4SLinus Torvalds struct page *page = req->wb_page; 3001da177e4SLinus Torvalds struct nfs_read_data *data; 301d097971dSTrond Myklebust size_t rsize = desc->pg_bsize, nbytes; 302e9f7bee1STrond Myklebust unsigned int offset; 3031da177e4SLinus Torvalds int requests = 0; 304dbae4c73STrond Myklebust int ret = 0; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds nfs_list_remove_request(req); 3071da177e4SLinus Torvalds 308275acaafSTrond Myklebust offset = 0; 309c76069bdSFred Isaman nbytes = desc->pg_count; 310e9f7bee1STrond Myklebust do { 311e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 312e9f7bee1STrond Myklebust 3138d5658c9STrond Myklebust data = nfs_readdata_alloc(1); 3141da177e4SLinus Torvalds if (!data) 3151da177e4SLinus Torvalds goto out_bad; 316275acaafSTrond Myklebust data->pagevec[0] = page; 317275acaafSTrond Myklebust nfs_read_rpcsetup(req, data, len, offset); 318275acaafSTrond Myklebust list_add(&data->list, res); 3191da177e4SLinus Torvalds requests++; 320e9f7bee1STrond Myklebust nbytes -= len; 321275acaafSTrond Myklebust offset += len; 322e9f7bee1STrond Myklebust } while(nbytes != 0); 3231da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 32450828d7eSTrond Myklebust desc->pg_rpc_callops = &nfs_read_partial_ops; 325dbae4c73STrond Myklebust return ret; 3261da177e4SLinus Torvalds out_bad: 327275acaafSTrond Myklebust while (!list_empty(res)) { 328275acaafSTrond Myklebust data = list_entry(res->next, struct nfs_read_data, list); 3296e4efd56STrond Myklebust list_del(&data->list); 3301da177e4SLinus Torvalds nfs_readdata_free(data); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds nfs_readpage_release(req); 3331da177e4SLinus Torvalds return -ENOMEM; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 336275acaafSTrond Myklebust static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds struct nfs_page *req; 3391da177e4SLinus Torvalds struct page **pages; 3401da177e4SLinus Torvalds struct nfs_read_data *data; 341c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 3423b609184SPeng Tao int ret = 0; 3431da177e4SLinus Torvalds 344c76069bdSFred Isaman data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base, 345c76069bdSFred Isaman desc->pg_count)); 346bae724efSFred Isaman if (!data) { 347bae724efSFred Isaman nfs_async_read_error(head); 3483b609184SPeng Tao ret = -ENOMEM; 349bae724efSFred Isaman goto out; 350bae724efSFred Isaman } 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds pages = data->pagevec; 3531da177e4SLinus Torvalds while (!list_empty(head)) { 3541da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3551da177e4SLinus Torvalds nfs_list_remove_request(req); 3561da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 3571da177e4SLinus Torvalds *pages++ = req->wb_page; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 3601da177e4SLinus Torvalds 3616e4efd56STrond Myklebust nfs_read_rpcsetup(req, data, desc->pg_count, 0); 362275acaafSTrond Myklebust list_add(&data->list, res); 36350828d7eSTrond Myklebust desc->pg_rpc_callops = &nfs_read_full_ops; 364bae724efSFred Isaman out: 365dbae4c73STrond Myklebust return ret; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 368493292ddSTrond Myklebust int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head) 369493292ddSTrond Myklebust { 370493292ddSTrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 371493292ddSTrond Myklebust return nfs_pagein_multi(desc, head); 372493292ddSTrond Myklebust return nfs_pagein_one(desc, head); 373493292ddSTrond Myklebust } 374493292ddSTrond Myklebust 375493292ddSTrond Myklebust static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) 3761751c363STrond Myklebust { 377275acaafSTrond Myklebust LIST_HEAD(head); 378275acaafSTrond Myklebust int ret; 379275acaafSTrond Myklebust 380493292ddSTrond Myklebust ret = nfs_generic_pagein(desc, &head); 381275acaafSTrond Myklebust if (ret == 0) 382493292ddSTrond Myklebust ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops); 383275acaafSTrond Myklebust return ret; 3841751c363STrond Myklebust } 3851751c363STrond Myklebust 3861751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops = { 3871751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 3881751c363STrond Myklebust .pg_doio = nfs_generic_pg_readpages, 3891751c363STrond Myklebust }; 3901751c363STrond Myklebust 3911da177e4SLinus Torvalds /* 3920b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 3930b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 3940b671301STrond Myklebust */ 3950b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 3960b671301STrond Myklebust { 3970b671301STrond Myklebust int status; 3980b671301STrond Myklebust 3993110ff80SHarvey Harrison dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid, 4000b671301STrond Myklebust task->tk_status); 4010b671301STrond Myklebust 4020b671301STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 4030b671301STrond Myklebust if (status != 0) 4040b671301STrond Myklebust return status; 4050b671301STrond Myklebust 4060b671301STrond Myklebust nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); 4070b671301STrond Myklebust 4080b671301STrond Myklebust if (task->tk_status == -ESTALE) { 4093a10c30aSBenny Halevy set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags); 4100b671301STrond Myklebust nfs_mark_for_revalidate(data->inode); 4110b671301STrond Myklebust } 4120b671301STrond Myklebust return 0; 4130b671301STrond Myklebust } 4140b671301STrond Myklebust 415fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) 4160b671301STrond Myklebust { 4170b671301STrond Myklebust struct nfs_readargs *argp = &data->args; 4180b671301STrond Myklebust struct nfs_readres *resp = &data->res; 4190b671301STrond Myklebust 4200b671301STrond Myklebust if (resp->eof || resp->count == argp->count) 421d61e612aSTrond Myklebust return; 4220b671301STrond Myklebust 4230b671301STrond Myklebust /* This is a short read! */ 4240b671301STrond Myklebust nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 4250b671301STrond Myklebust /* Has the server at least made some progress? */ 4260b671301STrond Myklebust if (resp->count == 0) 427d61e612aSTrond Myklebust return; 4280b671301STrond Myklebust 4290b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 430cbdabc7fSAndy Adamson data->mds_offset += resp->count; 4310b671301STrond Myklebust argp->offset += resp->count; 4320b671301STrond Myklebust argp->pgbase += resp->count; 4330b671301STrond Myklebust argp->count -= resp->count; 434d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 4350b671301STrond Myklebust } 4360b671301STrond Myklebust 4370b671301STrond Myklebust /* 4381da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 4391da177e4SLinus Torvalds */ 440ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 4411da177e4SLinus Torvalds { 442ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 4431da177e4SLinus Torvalds 444ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 445ec06c096STrond Myklebust return; 446fdd1e74cSTrond Myklebust if (task->tk_status < 0) 4470b671301STrond Myklebust return; 448fdd1e74cSTrond Myklebust 449fdd1e74cSTrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 450fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 4510b671301STrond Myklebust } 452fdd1e74cSTrond Myklebust 453fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata) 454fdd1e74cSTrond Myklebust { 455fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 456fdd1e74cSTrond Myklebust struct nfs_page *req = data->req; 457fdd1e74cSTrond Myklebust struct page *page = req->wb_page; 458fdd1e74cSTrond Myklebust int status = data->task.tk_status; 459fdd1e74cSTrond Myklebust 460fdd1e74cSTrond Myklebust if (status < 0) 461fba73005STrond Myklebust set_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags); 462fdd1e74cSTrond Myklebust 4631da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 464fba73005STrond Myklebust if (!test_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags)) 4651da177e4SLinus Torvalds SetPageUptodate(page); 4661da177e4SLinus Torvalds nfs_readpage_release(req); 4671da177e4SLinus Torvalds } 468fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 471f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1) 472f11c88afSAndy Adamson void nfs_read_prepare(struct rpc_task *task, void *calldata) 473f11c88afSAndy Adamson { 474f11c88afSAndy Adamson struct nfs_read_data *data = calldata; 475f11c88afSAndy Adamson 476035168abSTrond Myklebust if (nfs4_setup_sequence(NFS_SERVER(data->inode), 477f11c88afSAndy Adamson &data->args.seq_args, &data->res.seq_res, 478f11c88afSAndy Adamson 0, task)) 479f11c88afSAndy Adamson return; 480f11c88afSAndy Adamson rpc_call_start(task); 481f11c88afSAndy Adamson } 482f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 483f11c88afSAndy Adamson 484ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 485f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1) 486f11c88afSAndy Adamson .rpc_call_prepare = nfs_read_prepare, 487f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 488ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 489fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_partial, 490ec06c096STrond Myklebust }; 491ec06c096STrond Myklebust 4921de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 4931de3fc12STrond Myklebust { 4941de3fc12STrond Myklebust unsigned int count = data->res.count; 4951de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 4961de3fc12STrond Myklebust struct page **pages; 4971de3fc12STrond Myklebust 49879558f36STrond Myklebust if (data->res.eof) 49979558f36STrond Myklebust count = data->args.count; 5001de3fc12STrond Myklebust if (unlikely(count == 0)) 5011de3fc12STrond Myklebust return; 5021de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 5031de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 5041de3fc12STrond Myklebust count += base; 5051de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 5061de3fc12STrond Myklebust SetPageUptodate(*pages); 5070b671301STrond Myklebust if (count == 0) 5080b671301STrond Myklebust return; 5090b671301STrond Myklebust /* Was this a short read? */ 5100b671301STrond Myklebust if (data->res.eof || data->res.count == data->args.count) 5111de3fc12STrond Myklebust SetPageUptodate(*pages); 5121de3fc12STrond Myklebust } 5131de3fc12STrond Myklebust 5141da177e4SLinus Torvalds /* 5151da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 5161da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 5171da177e4SLinus Torvalds */ 518ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 5191da177e4SLinus Torvalds { 520ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 5211da177e4SLinus Torvalds 5220b671301STrond Myklebust if (nfs_readpage_result(task, data) != 0) 5230b671301STrond Myklebust return; 524fdd1e74cSTrond Myklebust if (task->tk_status < 0) 525fdd1e74cSTrond Myklebust return; 5261de3fc12STrond Myklebust /* 5270b671301STrond Myklebust * Note: nfs_readpage_retry may change the values of 5281de3fc12STrond Myklebust * data->args. In the multi-page case, we therefore need 5290b671301STrond Myklebust * to ensure that we call nfs_readpage_set_pages_uptodate() 5300b671301STrond Myklebust * first. 5311de3fc12STrond Myklebust */ 5321de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 5331de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 534fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 5350b671301STrond Myklebust } 536fdd1e74cSTrond Myklebust 537fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata) 538fdd1e74cSTrond Myklebust { 539fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 5409b7eecdcSPeng Tao struct nfs_pageio_descriptor pgio; 541fdd1e74cSTrond Myklebust 5429b7eecdcSPeng Tao if (data->pnfs_error) { 5439b7eecdcSPeng Tao nfs_pageio_init_read_mds(&pgio, data->inode); 5449b7eecdcSPeng Tao pgio.pg_recoalesce = 1; 5459b7eecdcSPeng Tao } 5461da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 5471da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 5481da177e4SLinus Torvalds 5491de3fc12STrond Myklebust nfs_list_remove_request(req); 5509b7eecdcSPeng Tao if (!data->pnfs_error) 5511da177e4SLinus Torvalds nfs_readpage_release(req); 5529b7eecdcSPeng Tao else 5539b7eecdcSPeng Tao nfs_pageio_add_request(&pgio, req); 5541da177e4SLinus Torvalds } 5559b7eecdcSPeng Tao if (data->pnfs_error) 5569b7eecdcSPeng Tao nfs_pageio_complete(&pgio); 557fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 560ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 561f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1) 562f11c88afSAndy Adamson .rpc_call_prepare = nfs_read_prepare, 563f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 564ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 565fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_full, 566ec06c096STrond Myklebust }; 567ec06c096STrond Myklebust 5681da177e4SLinus Torvalds /* 5691da177e4SLinus Torvalds * Read a page over NFS. 5701da177e4SLinus Torvalds * We read the page synchronously in the following case: 5711da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 5721da177e4SLinus Torvalds * previous async read operation failed. 5731da177e4SLinus Torvalds */ 5741da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds struct nfs_open_context *ctx; 5771da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 5781da177e4SLinus Torvalds int error; 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 5811da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 58291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 58391d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 58491d5b470SChuck Lever 5851da177e4SLinus Torvalds /* 5861da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 5871da177e4SLinus Torvalds * 5881da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 5891da177e4SLinus Torvalds * be any new pending writes generated at this point 5901da177e4SLinus Torvalds * for this page (other pages can be written to). 5911da177e4SLinus Torvalds */ 5921da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 5931da177e4SLinus Torvalds if (error) 594de05a0ccSTrond Myklebust goto out_unlock; 595de05a0ccSTrond Myklebust if (PageUptodate(page)) 596de05a0ccSTrond Myklebust goto out_unlock; 5971da177e4SLinus Torvalds 5985f004cf2STrond Myklebust error = -ESTALE; 5995f004cf2STrond Myklebust if (NFS_STALE(inode)) 600de05a0ccSTrond Myklebust goto out_unlock; 6015f004cf2STrond Myklebust 6021da177e4SLinus Torvalds if (file == NULL) { 603cf1308ffSTrond Myklebust error = -EBADF; 604d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 6051da177e4SLinus Torvalds if (ctx == NULL) 606de05a0ccSTrond Myklebust goto out_unlock; 6071da177e4SLinus Torvalds } else 608cd3758e3STrond Myklebust ctx = get_nfs_open_context(nfs_file_open_context(file)); 6091da177e4SLinus Torvalds 6109a9fc1c0SDavid Howells if (!IS_SYNC(inode)) { 6119a9fc1c0SDavid Howells error = nfs_readpage_from_fscache(ctx, inode, page); 6129a9fc1c0SDavid Howells if (error == 0) 6139a9fc1c0SDavid Howells goto out; 6149a9fc1c0SDavid Howells } 6159a9fc1c0SDavid Howells 6168e0969f0STrond Myklebust error = nfs_readpage_async(ctx, inode, page); 6178e0969f0STrond Myklebust 6189a9fc1c0SDavid Howells out: 6191da177e4SLinus Torvalds put_nfs_open_context(ctx); 6201da177e4SLinus Torvalds return error; 621de05a0ccSTrond Myklebust out_unlock: 6221da177e4SLinus Torvalds unlock_page(page); 6231da177e4SLinus Torvalds return error; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds struct nfs_readdesc { 6278b09bee3STrond Myklebust struct nfs_pageio_descriptor *pgio; 6281da177e4SLinus Torvalds struct nfs_open_context *ctx; 6291da177e4SLinus Torvalds }; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds static int 6321da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 6331da177e4SLinus Torvalds { 6341da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 6351da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 6361da177e4SLinus Torvalds struct nfs_page *new; 6371da177e4SLinus Torvalds unsigned int len; 638de05a0ccSTrond Myklebust int error; 6391da177e4SLinus Torvalds 64049a70f27STrond Myklebust len = nfs_page_length(page); 6411da177e4SLinus Torvalds if (len == 0) 6421da177e4SLinus Torvalds return nfs_return_empty_page(page); 643de05a0ccSTrond Myklebust 6441da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 645de05a0ccSTrond Myklebust if (IS_ERR(new)) 646de05a0ccSTrond Myklebust goto out_error; 647de05a0ccSTrond Myklebust 6481da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 649eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 650f8512ad0SFred Isaman if (!nfs_pageio_add_request(desc->pgio, new)) { 651f8512ad0SFred Isaman error = desc->pgio->pg_error; 652f8512ad0SFred Isaman goto out_unlock; 653f8512ad0SFred Isaman } 6541da177e4SLinus Torvalds return 0; 655de05a0ccSTrond Myklebust out_error: 656de05a0ccSTrond Myklebust error = PTR_ERR(new); 657de05a0ccSTrond Myklebust out_unlock: 658de05a0ccSTrond Myklebust unlock_page(page); 659de05a0ccSTrond Myklebust return error; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 6631da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 6641da177e4SLinus Torvalds { 6658b09bee3STrond Myklebust struct nfs_pageio_descriptor pgio; 6661da177e4SLinus Torvalds struct nfs_readdesc desc = { 6678b09bee3STrond Myklebust .pgio = &pgio, 6681da177e4SLinus Torvalds }; 6691da177e4SLinus Torvalds struct inode *inode = mapping->host; 6708b09bee3STrond Myklebust unsigned long npages; 6715f004cf2STrond Myklebust int ret = -ESTALE; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 6741da177e4SLinus Torvalds inode->i_sb->s_id, 6751da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 6761da177e4SLinus Torvalds nr_pages); 67791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 6781da177e4SLinus Torvalds 6795f004cf2STrond Myklebust if (NFS_STALE(inode)) 6805f004cf2STrond Myklebust goto out; 6815f004cf2STrond Myklebust 6821da177e4SLinus Torvalds if (filp == NULL) { 683d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 6841da177e4SLinus Torvalds if (desc.ctx == NULL) 6851da177e4SLinus Torvalds return -EBADF; 6861da177e4SLinus Torvalds } else 687cd3758e3STrond Myklebust desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); 6889a9fc1c0SDavid Howells 6899a9fc1c0SDavid Howells /* attempt to read as many of the pages as possible from the cache 6909a9fc1c0SDavid Howells * - this returns -ENOBUFS immediately if the cookie is negative 6919a9fc1c0SDavid Howells */ 6929a9fc1c0SDavid Howells ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, 6939a9fc1c0SDavid Howells pages, &nr_pages); 6949a9fc1c0SDavid Howells if (ret == 0) 6959a9fc1c0SDavid Howells goto read_complete; /* all pages were read */ 6969a9fc1c0SDavid Howells 6971751c363STrond Myklebust nfs_pageio_init_read(&pgio, inode); 6988b09bee3STrond Myklebust 6991da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 7008b09bee3STrond Myklebust 7018b09bee3STrond Myklebust nfs_pageio_complete(&pgio); 7028b09bee3STrond Myklebust npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 7038b09bee3STrond Myklebust nfs_add_stats(inode, NFSIOS_READPAGES, npages); 7049a9fc1c0SDavid Howells read_complete: 7051da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 7065f004cf2STrond Myklebust out: 7071da177e4SLinus Torvalds return ret; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 7131da177e4SLinus Torvalds sizeof(struct nfs_read_data), 7141da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 71520c2df83SPaul Mundt NULL); 7161da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 7171da177e4SLinus Torvalds return -ENOMEM; 7181da177e4SLinus Torvalds 71993d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 7201da177e4SLinus Torvalds nfs_rdata_cachep); 7211da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 7221da177e4SLinus Torvalds return -ENOMEM; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds return 0; 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 727266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 7301a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 7311da177e4SLinus Torvalds } 732