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 23f11c88afSAndy Adamson #include "nfs4_fs.h" 2449a70f27STrond Myklebust #include "internal.h" 2591d5b470SChuck Lever #include "iostat.h" 269a9fc1c0SDavid Howells #include "fscache.h" 27fab5fc25SChristoph Hellwig #include "pnfs.h" 2891d5b470SChuck Lever 291da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 301da177e4SLinus Torvalds 311751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops; 324db6e0b7SFred Isaman static const struct rpc_call_ops nfs_read_common_ops; 33061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops; 344a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops; 351da177e4SLinus Torvalds 36e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep; 371da177e4SLinus Torvalds 384a0de55cSAnna Schumaker static struct nfs_rw_header *nfs_readhdr_alloc(void) 393feb2d49STrond Myklebust { 404a0de55cSAnna Schumaker return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL); 414db6e0b7SFred Isaman } 424db6e0b7SFred Isaman 434a0de55cSAnna Schumaker static void nfs_readhdr_free(struct nfs_rw_header *rhdr) 443feb2d49STrond Myklebust { 45cd841605SFred Isaman kmem_cache_free(nfs_rdata_cachep, rhdr); 463feb2d49STrond Myklebust } 473feb2d49STrond Myklebust 481da177e4SLinus Torvalds static 491da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 501da177e4SLinus Torvalds { 51eebd2aa3SChristoph Lameter zero_user(page, 0, PAGE_CACHE_SIZE); 521da177e4SLinus Torvalds SetPageUptodate(page); 531da177e4SLinus Torvalds unlock_page(page); 541da177e4SLinus Torvalds return 0; 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 571abb5088SBryan Schumaker void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, 58fab5fc25SChristoph Hellwig struct inode *inode, bool force_mds, 59061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 601751c363STrond Myklebust { 61fab5fc25SChristoph Hellwig struct nfs_server *server = NFS_SERVER(inode); 62fab5fc25SChristoph Hellwig const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops; 63fab5fc25SChristoph Hellwig 64fab5fc25SChristoph Hellwig #ifdef CONFIG_NFS_V4_1 65fab5fc25SChristoph Hellwig if (server->pnfs_curr_ld && !force_mds) 66fab5fc25SChristoph Hellwig pg_ops = server->pnfs_curr_ld->pg_read_ops; 67fab5fc25SChristoph Hellwig #endif 684a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops, 694a0de55cSAnna Schumaker server->rsize, 0); 701751c363STrond Myklebust } 71ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_read); 721751c363STrond Myklebust 73493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio) 74493292ddSTrond Myklebust { 75493292ddSTrond Myklebust pgio->pg_ops = &nfs_pageio_read_ops; 76493292ddSTrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize; 77493292ddSTrond Myklebust } 781f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds); 79493292ddSTrond Myklebust 80f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 811da177e4SLinus Torvalds struct page *page) 821da177e4SLinus Torvalds { 831da177e4SLinus Torvalds struct nfs_page *new; 841da177e4SLinus Torvalds unsigned int len; 85c76069bdSFred Isaman struct nfs_pageio_descriptor pgio; 861da177e4SLinus Torvalds 8749a70f27STrond Myklebust len = nfs_page_length(page); 881da177e4SLinus Torvalds if (len == 0) 891da177e4SLinus Torvalds return nfs_return_empty_page(page); 901da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 911da177e4SLinus Torvalds if (IS_ERR(new)) { 921da177e4SLinus Torvalds unlock_page(page); 931da177e4SLinus Torvalds return PTR_ERR(new); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 96eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 971da177e4SLinus Torvalds 98fab5fc25SChristoph Hellwig nfs_pageio_init_read(&pgio, inode, false, 99fab5fc25SChristoph Hellwig &nfs_async_read_completion_ops); 100d8007d4dSTrond Myklebust nfs_pageio_add_request(&pgio, new); 1011751c363STrond Myklebust nfs_pageio_complete(&pgio); 1022701d086SAndy Adamson NFS_I(inode)->read_io += pgio.pg_bytes_written; 1031da177e4SLinus Torvalds return 0; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 1071da177e4SLinus Torvalds { 1083d4ff43dSAl Viro struct inode *d_inode = req->wb_context->dentry->d_inode; 1097f8e05f6SDavid Howells 1107f8e05f6SDavid Howells if (PageUptodate(req->wb_page)) 1117f8e05f6SDavid Howells nfs_readpage_to_fscache(d_inode, req->wb_page, 0); 1127f8e05f6SDavid Howells 1131da177e4SLinus Torvalds unlock_page(req->wb_page); 1141da177e4SLinus Torvalds 1151e8968c5SNiels de Vos dprintk("NFS: read done (%s/%Lu %d@%Ld)\n", 1163d4ff43dSAl Viro req->wb_context->dentry->d_inode->i_sb->s_id, 1171e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 1181da177e4SLinus Torvalds req->wb_bytes, 1191da177e4SLinus Torvalds (long long)req_offset(req)); 12010d2c46fSNick Wilson nfs_release_request(req); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1234db6e0b7SFred Isaman /* Note io was page aligned */ 124061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr) 1254db6e0b7SFred Isaman { 1264db6e0b7SFred Isaman unsigned long bytes = 0; 1274db6e0b7SFred Isaman 1284db6e0b7SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 1294db6e0b7SFred Isaman goto out; 1304db6e0b7SFred Isaman while (!list_empty(&hdr->pages)) { 1314db6e0b7SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 1324db6e0b7SFred Isaman struct page *page = req->wb_page; 1334db6e0b7SFred Isaman 1344db6e0b7SFred Isaman if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) { 1354db6e0b7SFred Isaman if (bytes > hdr->good_bytes) 1364db6e0b7SFred Isaman zero_user(page, 0, PAGE_SIZE); 1374db6e0b7SFred Isaman else if (hdr->good_bytes - bytes < PAGE_SIZE) 1384db6e0b7SFred Isaman zero_user_segment(page, 1394db6e0b7SFred Isaman hdr->good_bytes & ~PAGE_MASK, 1404db6e0b7SFred Isaman PAGE_SIZE); 1414db6e0b7SFred Isaman } 1424bd8b010STrond Myklebust bytes += req->wb_bytes; 1434bd8b010STrond Myklebust if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { 1444bd8b010STrond Myklebust if (bytes <= hdr->good_bytes) 1454bd8b010STrond Myklebust SetPageUptodate(page); 1464bd8b010STrond Myklebust } else 1474db6e0b7SFred Isaman SetPageUptodate(page); 1484db6e0b7SFred Isaman nfs_list_remove_request(req); 1494db6e0b7SFred Isaman nfs_readpage_release(req); 1504db6e0b7SFred Isaman } 1514db6e0b7SFred Isaman out: 1524db6e0b7SFred Isaman hdr->release(hdr); 1534db6e0b7SFred Isaman } 1544db6e0b7SFred Isaman 155c5996c4eSFred Isaman int nfs_initiate_read(struct rpc_clnt *clnt, 1569c7e1b3dSAnna Schumaker struct nfs_pgio_data *data, 1579f0ec176SAndy Adamson const struct rpc_call_ops *call_ops, int flags) 15864419a9bSAndy Adamson { 159cd841605SFred Isaman struct inode *inode = data->header->inode; 16064419a9bSAndy Adamson int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; 16164419a9bSAndy Adamson struct rpc_task *task; 16264419a9bSAndy Adamson struct rpc_message msg = { 16364419a9bSAndy Adamson .rpc_argp = &data->args, 16464419a9bSAndy Adamson .rpc_resp = &data->res, 165cd841605SFred Isaman .rpc_cred = data->header->cred, 16664419a9bSAndy Adamson }; 16764419a9bSAndy Adamson struct rpc_task_setup task_setup_data = { 16864419a9bSAndy Adamson .task = &data->task, 16964419a9bSAndy Adamson .rpc_client = clnt, 17064419a9bSAndy Adamson .rpc_message = &msg, 17164419a9bSAndy Adamson .callback_ops = call_ops, 17264419a9bSAndy Adamson .callback_data = data, 17364419a9bSAndy Adamson .workqueue = nfsiod_workqueue, 1749f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | swap_flags | flags, 17564419a9bSAndy Adamson }; 17664419a9bSAndy Adamson 17764419a9bSAndy Adamson /* Set up the initial task struct. */ 17864419a9bSAndy Adamson NFS_PROTO(inode)->read_setup(data, &msg); 17964419a9bSAndy Adamson 1801e8968c5SNiels de Vos dprintk("NFS: %5u initiated read call (req %s/%llu, %u bytes @ " 18164419a9bSAndy Adamson "offset %llu)\n", 18264419a9bSAndy Adamson data->task.tk_pid, 18364419a9bSAndy Adamson inode->i_sb->s_id, 1841e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(inode), 18564419a9bSAndy Adamson data->args.count, 18664419a9bSAndy Adamson (unsigned long long)data->args.offset); 18764419a9bSAndy Adamson 18864419a9bSAndy Adamson task = rpc_run_task(&task_setup_data); 18964419a9bSAndy Adamson if (IS_ERR(task)) 19064419a9bSAndy Adamson return PTR_ERR(task); 19164419a9bSAndy Adamson rpc_put_task(task); 19264419a9bSAndy Adamson return 0; 19364419a9bSAndy Adamson } 194dc70d7b3SAndy Adamson EXPORT_SYMBOL_GPL(nfs_initiate_read); 19564419a9bSAndy Adamson 1961da177e4SLinus Torvalds /* 1971da177e4SLinus Torvalds * Set up the NFS read request struct 1981da177e4SLinus Torvalds */ 1999c7e1b3dSAnna Schumaker static void nfs_read_rpcsetup(struct nfs_pgio_data *data, 2006e4efd56STrond Myklebust unsigned int count, unsigned int offset) 2011da177e4SLinus Torvalds { 2024db6e0b7SFred Isaman struct nfs_page *req = data->header->req; 2031da177e4SLinus Torvalds 2044db6e0b7SFred Isaman data->args.fh = NFS_FH(data->header->inode); 2051da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 2061da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 20730dd374fSFred Isaman data->args.pages = data->pages.pagevec; 2081da177e4SLinus Torvalds data->args.count = count; 209383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 210f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds data->res.fattr = &data->fattr; 2131da177e4SLinus Torvalds data->res.count = count; 2141da177e4SLinus Torvalds data->res.eof = 0; 2150e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 2166e4efd56STrond Myklebust } 2171da177e4SLinus Torvalds 2189c7e1b3dSAnna Schumaker static int nfs_do_read(struct nfs_pgio_data *data, 219493292ddSTrond Myklebust const struct rpc_call_ops *call_ops) 2206e4efd56STrond Myklebust { 221cd841605SFred Isaman struct inode *inode = data->header->inode; 2226e4efd56STrond Myklebust 2239f0ec176SAndy Adamson return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 226275acaafSTrond Myklebust static int 227275acaafSTrond Myklebust nfs_do_multiple_reads(struct list_head *head, 228493292ddSTrond Myklebust const struct rpc_call_ops *call_ops) 229275acaafSTrond Myklebust { 2309c7e1b3dSAnna Schumaker struct nfs_pgio_data *data; 231275acaafSTrond Myklebust int ret = 0; 232275acaafSTrond Myklebust 233275acaafSTrond Myklebust while (!list_empty(head)) { 234275acaafSTrond Myklebust int ret2; 235275acaafSTrond Myklebust 2369c7e1b3dSAnna Schumaker data = list_first_entry(head, struct nfs_pgio_data, list); 237275acaafSTrond Myklebust list_del_init(&data->list); 238275acaafSTrond Myklebust 239493292ddSTrond Myklebust ret2 = nfs_do_read(data, call_ops); 240275acaafSTrond Myklebust if (ret == 0) 241275acaafSTrond Myklebust ret = ret2; 242275acaafSTrond Myklebust } 243275acaafSTrond Myklebust return ret; 244275acaafSTrond Myklebust } 245275acaafSTrond Myklebust 246061ae2edSFred Isaman static void 2471da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds struct nfs_page *req; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds while (!list_empty(head)) { 2521da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2531da177e4SLinus Torvalds nfs_list_remove_request(req); 2541da177e4SLinus Torvalds nfs_readpage_release(req); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 258061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = { 259061ae2edSFred Isaman .error_cleanup = nfs_async_read_error, 260061ae2edSFred Isaman .completion = nfs_read_completion, 261061ae2edSFred Isaman }; 262061ae2edSFred Isaman 26325b11dcdSTrond Myklebust static void nfs_pagein_error(struct nfs_pageio_descriptor *desc, 26425b11dcdSTrond Myklebust struct nfs_pgio_header *hdr) 26525b11dcdSTrond Myklebust { 26625b11dcdSTrond Myklebust set_bit(NFS_IOHDR_REDO, &hdr->flags); 26725b11dcdSTrond Myklebust while (!list_empty(&hdr->rpc_list)) { 2689c7e1b3dSAnna Schumaker struct nfs_pgio_data *data = list_first_entry(&hdr->rpc_list, 2699c7e1b3dSAnna Schumaker struct nfs_pgio_data, list); 27025b11dcdSTrond Myklebust list_del(&data->list); 27100bfa30aSAnna Schumaker nfs_pgio_data_release(data); 27225b11dcdSTrond Myklebust } 27325b11dcdSTrond Myklebust desc->pg_completion_ops->error_cleanup(&desc->pg_list); 27425b11dcdSTrond Myklebust } 27525b11dcdSTrond Myklebust 2761da177e4SLinus Torvalds /* 2771da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 2781da177e4SLinus Torvalds * 2791da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 2801da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 2811da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 2821da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 2831da177e4SLinus Torvalds * 2841da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 2851da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 2861da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 2871da177e4SLinus Torvalds * or less conventional NFS client behavior. 2881da177e4SLinus Torvalds */ 2894db6e0b7SFred Isaman static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, 2904db6e0b7SFred Isaman struct nfs_pgio_header *hdr) 2911da177e4SLinus Torvalds { 2924db6e0b7SFred Isaman struct nfs_page *req = hdr->req; 2931da177e4SLinus Torvalds struct page *page = req->wb_page; 2949c7e1b3dSAnna Schumaker struct nfs_pgio_data *data; 295d097971dSTrond Myklebust size_t rsize = desc->pg_bsize, nbytes; 296e9f7bee1STrond Myklebust unsigned int offset; 2971da177e4SLinus Torvalds 298275acaafSTrond Myklebust offset = 0; 299c76069bdSFred Isaman nbytes = desc->pg_count; 300e9f7bee1STrond Myklebust do { 301e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 302e9f7bee1STrond Myklebust 30300bfa30aSAnna Schumaker data = nfs_pgio_data_alloc(hdr, 1); 30425b11dcdSTrond Myklebust if (!data) { 30525b11dcdSTrond Myklebust nfs_pagein_error(desc, hdr); 30625b11dcdSTrond Myklebust return -ENOMEM; 30725b11dcdSTrond Myklebust } 30830dd374fSFred Isaman data->pages.pagevec[0] = page; 3094db6e0b7SFred Isaman nfs_read_rpcsetup(data, len, offset); 3104db6e0b7SFred Isaman list_add(&data->list, &hdr->rpc_list); 311e9f7bee1STrond Myklebust nbytes -= len; 312275acaafSTrond Myklebust offset += len; 313e9f7bee1STrond Myklebust } while (nbytes != 0); 31425b11dcdSTrond Myklebust 31525b11dcdSTrond Myklebust nfs_list_remove_request(req); 31625b11dcdSTrond Myklebust nfs_list_add_request(req, &hdr->pages); 3174db6e0b7SFred Isaman desc->pg_rpc_callops = &nfs_read_common_ops; 3189146ab50STrond Myklebust return 0; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3214db6e0b7SFred Isaman static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, 3224db6e0b7SFred Isaman struct nfs_pgio_header *hdr) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct nfs_page *req; 3251da177e4SLinus Torvalds struct page **pages; 3269c7e1b3dSAnna Schumaker struct nfs_pgio_data *data; 327c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 3281da177e4SLinus Torvalds 32900bfa30aSAnna Schumaker data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base, 330c76069bdSFred Isaman desc->pg_count)); 3314db6e0b7SFred Isaman if (!data) { 33225b11dcdSTrond Myklebust nfs_pagein_error(desc, hdr); 3339146ab50STrond Myklebust return -ENOMEM; 334bae724efSFred Isaman } 3351da177e4SLinus Torvalds 33630dd374fSFred Isaman pages = data->pages.pagevec; 3371da177e4SLinus Torvalds while (!list_empty(head)) { 3381da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3391da177e4SLinus Torvalds nfs_list_remove_request(req); 3404db6e0b7SFred Isaman nfs_list_add_request(req, &hdr->pages); 3411da177e4SLinus Torvalds *pages++ = req->wb_page; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3444db6e0b7SFred Isaman nfs_read_rpcsetup(data, desc->pg_count, 0); 3454db6e0b7SFred Isaman list_add(&data->list, &hdr->rpc_list); 3464db6e0b7SFred Isaman desc->pg_rpc_callops = &nfs_read_common_ops; 3479146ab50STrond Myklebust return 0; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3504db6e0b7SFred Isaman int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, 3514db6e0b7SFred Isaman struct nfs_pgio_header *hdr) 352493292ddSTrond Myklebust { 353493292ddSTrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 3544db6e0b7SFred Isaman return nfs_pagein_multi(desc, hdr); 3554db6e0b7SFred Isaman return nfs_pagein_one(desc, hdr); 356493292ddSTrond Myklebust } 35789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_pagein); 358493292ddSTrond Myklebust 359493292ddSTrond Myklebust static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) 3601751c363STrond Myklebust { 361c0752cdfSAnna Schumaker struct nfs_rw_header *rhdr; 3624db6e0b7SFred Isaman struct nfs_pgio_header *hdr; 363275acaafSTrond Myklebust int ret; 364275acaafSTrond Myklebust 3654a0de55cSAnna Schumaker rhdr = nfs_rw_header_alloc(desc->pg_rw_ops); 3664db6e0b7SFred Isaman if (!rhdr) { 367061ae2edSFred Isaman desc->pg_completion_ops->error_cleanup(&desc->pg_list); 3684db6e0b7SFred Isaman return -ENOMEM; 3694db6e0b7SFred Isaman } 3704db6e0b7SFred Isaman hdr = &rhdr->header; 3714a0de55cSAnna Schumaker nfs_pgheader_init(desc, hdr, nfs_rw_header_free); 3724db6e0b7SFred Isaman atomic_inc(&hdr->refcnt); 3734db6e0b7SFred Isaman ret = nfs_generic_pagein(desc, hdr); 374275acaafSTrond Myklebust if (ret == 0) 3754db6e0b7SFred Isaman ret = nfs_do_multiple_reads(&hdr->rpc_list, 3764db6e0b7SFred Isaman desc->pg_rpc_callops); 3774db6e0b7SFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 378061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 379275acaafSTrond Myklebust return ret; 3801751c363STrond Myklebust } 3811751c363STrond Myklebust 3821751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops = { 3831751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 3841751c363STrond Myklebust .pg_doio = nfs_generic_pg_readpages, 3851751c363STrond Myklebust }; 3861751c363STrond Myklebust 3871da177e4SLinus Torvalds /* 3880b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 3890b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 3900b671301STrond Myklebust */ 391*0eecb214SAnna Schumaker static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data, 392*0eecb214SAnna Schumaker struct inode *inode) 3930b671301STrond Myklebust { 394*0eecb214SAnna Schumaker int status = NFS_PROTO(inode)->read_done(task, data); 3950b671301STrond Myklebust if (status != 0) 3960b671301STrond Myklebust return status; 3970b671301STrond Myklebust 398cd841605SFred Isaman nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count); 3990b671301STrond Myklebust 4000b671301STrond Myklebust if (task->tk_status == -ESTALE) { 401cd841605SFred Isaman set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 402cd841605SFred Isaman nfs_mark_for_revalidate(inode); 4030b671301STrond Myklebust } 4040b671301STrond Myklebust return 0; 4050b671301STrond Myklebust } 4060b671301STrond Myklebust 4079c7e1b3dSAnna Schumaker static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data) 4080b671301STrond Myklebust { 4093c6b899cSAnna Schumaker struct nfs_pgio_args *argp = &data->args; 4109137bdf3SAnna Schumaker struct nfs_pgio_res *resp = &data->res; 4110b671301STrond Myklebust 4120b671301STrond Myklebust /* This is a short read! */ 413cd841605SFred Isaman nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD); 4140b671301STrond Myklebust /* Has the server at least made some progress? */ 4154db6e0b7SFred Isaman if (resp->count == 0) { 4164db6e0b7SFred Isaman nfs_set_pgio_error(data->header, -EIO, argp->offset); 417d61e612aSTrond Myklebust return; 4184db6e0b7SFred Isaman } 4190b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 420cbdabc7fSAndy Adamson data->mds_offset += resp->count; 4210b671301STrond Myklebust argp->offset += resp->count; 4220b671301STrond Myklebust argp->pgbase += resp->count; 4230b671301STrond Myklebust argp->count -= resp->count; 424d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 4250b671301STrond Myklebust } 4260b671301STrond Myklebust 427*0eecb214SAnna Schumaker static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data) 4281da177e4SLinus Torvalds { 4294db6e0b7SFred Isaman struct nfs_pgio_header *hdr = data->header; 4301da177e4SLinus Torvalds 431*0eecb214SAnna Schumaker if (data->res.eof) { 4324db6e0b7SFred Isaman loff_t bound; 433fdd1e74cSTrond Myklebust 4344db6e0b7SFred Isaman bound = data->args.offset + data->res.count; 4354db6e0b7SFred Isaman spin_lock(&hdr->lock); 4364db6e0b7SFred Isaman if (bound < hdr->io_start + hdr->good_bytes) { 4374db6e0b7SFred Isaman set_bit(NFS_IOHDR_EOF, &hdr->flags); 4384db6e0b7SFred Isaman clear_bit(NFS_IOHDR_ERROR, &hdr->flags); 4394db6e0b7SFred Isaman hdr->good_bytes = bound - hdr->io_start; 4404db6e0b7SFred Isaman } 4414db6e0b7SFred Isaman spin_unlock(&hdr->lock); 4424db6e0b7SFred Isaman } else if (data->res.count != data->args.count) 443fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 4440b671301STrond Myklebust } 445fdd1e74cSTrond Myklebust 4464db6e0b7SFred Isaman static const struct rpc_call_ops nfs_read_common_ops = { 447a4cdda59SAnna Schumaker .rpc_call_prepare = nfs_pgio_prepare, 448*0eecb214SAnna Schumaker .rpc_call_done = nfs_pgio_result, 449a4cdda59SAnna Schumaker .rpc_release = nfs_pgio_release, 450ec06c096STrond Myklebust }; 451ec06c096STrond Myklebust 4521da177e4SLinus Torvalds /* 4531da177e4SLinus Torvalds * Read a page over NFS. 4541da177e4SLinus Torvalds * We read the page synchronously in the following case: 4551da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 4561da177e4SLinus Torvalds * previous async read operation failed. 4571da177e4SLinus Torvalds */ 4581da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds struct nfs_open_context *ctx; 461d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 4621da177e4SLinus Torvalds int error; 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 465d56b4ddfSMel Gorman page, PAGE_CACHE_SIZE, page_file_index(page)); 46691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 46791d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 46891d5b470SChuck Lever 4691da177e4SLinus Torvalds /* 4701da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 4711da177e4SLinus Torvalds * 4721da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 4731da177e4SLinus Torvalds * be any new pending writes generated at this point 4741da177e4SLinus Torvalds * for this page (other pages can be written to). 4751da177e4SLinus Torvalds */ 4761da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 4771da177e4SLinus Torvalds if (error) 478de05a0ccSTrond Myklebust goto out_unlock; 479de05a0ccSTrond Myklebust if (PageUptodate(page)) 480de05a0ccSTrond Myklebust goto out_unlock; 4811da177e4SLinus Torvalds 4825f004cf2STrond Myklebust error = -ESTALE; 4835f004cf2STrond Myklebust if (NFS_STALE(inode)) 484de05a0ccSTrond Myklebust goto out_unlock; 4855f004cf2STrond Myklebust 4861da177e4SLinus Torvalds if (file == NULL) { 487cf1308ffSTrond Myklebust error = -EBADF; 488d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 4891da177e4SLinus Torvalds if (ctx == NULL) 490de05a0ccSTrond Myklebust goto out_unlock; 4911da177e4SLinus Torvalds } else 492cd3758e3STrond Myklebust ctx = get_nfs_open_context(nfs_file_open_context(file)); 4931da177e4SLinus Torvalds 4949a9fc1c0SDavid Howells if (!IS_SYNC(inode)) { 4959a9fc1c0SDavid Howells error = nfs_readpage_from_fscache(ctx, inode, page); 4969a9fc1c0SDavid Howells if (error == 0) 4979a9fc1c0SDavid Howells goto out; 4989a9fc1c0SDavid Howells } 4999a9fc1c0SDavid Howells 5008e0969f0STrond Myklebust error = nfs_readpage_async(ctx, inode, page); 5018e0969f0STrond Myklebust 5029a9fc1c0SDavid Howells out: 5031da177e4SLinus Torvalds put_nfs_open_context(ctx); 5041da177e4SLinus Torvalds return error; 505de05a0ccSTrond Myklebust out_unlock: 5061da177e4SLinus Torvalds unlock_page(page); 5071da177e4SLinus Torvalds return error; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds struct nfs_readdesc { 5118b09bee3STrond Myklebust struct nfs_pageio_descriptor *pgio; 5121da177e4SLinus Torvalds struct nfs_open_context *ctx; 5131da177e4SLinus Torvalds }; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds static int 5161da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 5171da177e4SLinus Torvalds { 5181da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 519d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 5201da177e4SLinus Torvalds struct nfs_page *new; 5211da177e4SLinus Torvalds unsigned int len; 522de05a0ccSTrond Myklebust int error; 5231da177e4SLinus Torvalds 52449a70f27STrond Myklebust len = nfs_page_length(page); 5251da177e4SLinus Torvalds if (len == 0) 5261da177e4SLinus Torvalds return nfs_return_empty_page(page); 527de05a0ccSTrond Myklebust 5281da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 529de05a0ccSTrond Myklebust if (IS_ERR(new)) 530de05a0ccSTrond Myklebust goto out_error; 531de05a0ccSTrond Myklebust 5321da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 533eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 534f8512ad0SFred Isaman if (!nfs_pageio_add_request(desc->pgio, new)) { 535f8512ad0SFred Isaman error = desc->pgio->pg_error; 536f8512ad0SFred Isaman goto out_unlock; 537f8512ad0SFred Isaman } 5381da177e4SLinus Torvalds return 0; 539de05a0ccSTrond Myklebust out_error: 540de05a0ccSTrond Myklebust error = PTR_ERR(new); 541de05a0ccSTrond Myklebust out_unlock: 542de05a0ccSTrond Myklebust unlock_page(page); 543de05a0ccSTrond Myklebust return error; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 5471da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 5481da177e4SLinus Torvalds { 5498b09bee3STrond Myklebust struct nfs_pageio_descriptor pgio; 5501da177e4SLinus Torvalds struct nfs_readdesc desc = { 5518b09bee3STrond Myklebust .pgio = &pgio, 5521da177e4SLinus Torvalds }; 5531da177e4SLinus Torvalds struct inode *inode = mapping->host; 5548b09bee3STrond Myklebust unsigned long npages; 5555f004cf2STrond Myklebust int ret = -ESTALE; 5561da177e4SLinus Torvalds 5571e8968c5SNiels de Vos dprintk("NFS: nfs_readpages (%s/%Lu %d)\n", 5581da177e4SLinus Torvalds inode->i_sb->s_id, 5591e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(inode), 5601da177e4SLinus Torvalds nr_pages); 56191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 5621da177e4SLinus Torvalds 5635f004cf2STrond Myklebust if (NFS_STALE(inode)) 5645f004cf2STrond Myklebust goto out; 5655f004cf2STrond Myklebust 5661da177e4SLinus Torvalds if (filp == NULL) { 567d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 5681da177e4SLinus Torvalds if (desc.ctx == NULL) 5691da177e4SLinus Torvalds return -EBADF; 5701da177e4SLinus Torvalds } else 571cd3758e3STrond Myklebust desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); 5729a9fc1c0SDavid Howells 5739a9fc1c0SDavid Howells /* attempt to read as many of the pages as possible from the cache 5749a9fc1c0SDavid Howells * - this returns -ENOBUFS immediately if the cookie is negative 5759a9fc1c0SDavid Howells */ 5769a9fc1c0SDavid Howells ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, 5779a9fc1c0SDavid Howells pages, &nr_pages); 5789a9fc1c0SDavid Howells if (ret == 0) 5799a9fc1c0SDavid Howells goto read_complete; /* all pages were read */ 5809a9fc1c0SDavid Howells 581fab5fc25SChristoph Hellwig nfs_pageio_init_read(&pgio, inode, false, 582fab5fc25SChristoph Hellwig &nfs_async_read_completion_ops); 5838b09bee3STrond Myklebust 5841da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 5858b09bee3STrond Myklebust 5868b09bee3STrond Myklebust nfs_pageio_complete(&pgio); 5872701d086SAndy Adamson NFS_I(inode)->read_io += pgio.pg_bytes_written; 5888b09bee3STrond Myklebust npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 5898b09bee3STrond Myklebust nfs_add_stats(inode, NFSIOS_READPAGES, npages); 5909a9fc1c0SDavid Howells read_complete: 5911da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 5925f004cf2STrond Myklebust out: 5931da177e4SLinus Torvalds return ret; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 596f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 599c0752cdfSAnna Schumaker sizeof(struct nfs_rw_header), 6001da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 60120c2df83SPaul Mundt NULL); 6021da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 6031da177e4SLinus Torvalds return -ENOMEM; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds return 0; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 608266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 6091da177e4SLinus Torvalds { 6101a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 6111da177e4SLinus Torvalds } 6124a0de55cSAnna Schumaker 6134a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops = { 614a4cdda59SAnna Schumaker .rw_mode = FMODE_READ, 6154a0de55cSAnna Schumaker .rw_alloc_header = nfs_readhdr_alloc, 6164a0de55cSAnna Schumaker .rw_free_header = nfs_readhdr_free, 617*0eecb214SAnna Schumaker .rw_done = nfs_readpage_done, 618*0eecb214SAnna Schumaker .rw_result = nfs_readpage_result, 6194a0de55cSAnna Schumaker }; 620