1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/read.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Block I/O for NFS 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Partial copy of Linus' read cache modifications to fs/nfs/file.c 81da177e4SLinus Torvalds * modified for async RPC by okir@monad.swb.de 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/time.h> 121da177e4SLinus Torvalds #include <linux/kernel.h> 131da177e4SLinus Torvalds #include <linux/errno.h> 141da177e4SLinus Torvalds #include <linux/fcntl.h> 151da177e4SLinus Torvalds #include <linux/stat.h> 161da177e4SLinus Torvalds #include <linux/mm.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 191da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 201da177e4SLinus Torvalds #include <linux/nfs_fs.h> 211da177e4SLinus Torvalds #include <linux/nfs_page.h> 2264419a9bSAndy Adamson #include <linux/module.h> 231da177e4SLinus Torvalds 24f11c88afSAndy Adamson #include "nfs4_fs.h" 2549a70f27STrond Myklebust #include "internal.h" 2691d5b470SChuck Lever #include "iostat.h" 279a9fc1c0SDavid Howells #include "fscache.h" 28fab5fc25SChristoph Hellwig #include "pnfs.h" 298224b273SChuck Lever #include "nfstrace.h" 3091d5b470SChuck Lever 311da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 321da177e4SLinus Torvalds 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 381e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_readhdr_alloc(void) 393feb2d49STrond Myklebust { 40fbe77c30SBenjamin Coddington struct nfs_pgio_header *p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL); 41fbe77c30SBenjamin Coddington 42fbe77c30SBenjamin Coddington if (p) 43fbe77c30SBenjamin Coddington p->rw_mode = FMODE_READ; 44fbe77c30SBenjamin Coddington return p; 454db6e0b7SFred Isaman } 464db6e0b7SFred Isaman 471e7f3a48SWeston Andros Adamson static void nfs_readhdr_free(struct nfs_pgio_header *rhdr) 483feb2d49STrond Myklebust { 49cd841605SFred Isaman kmem_cache_free(nfs_rdata_cachep, rhdr); 503feb2d49STrond Myklebust } 513feb2d49STrond Myklebust 521da177e4SLinus Torvalds static 531da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page) 541da177e4SLinus Torvalds { 5509cbfeafSKirill A. Shutemov zero_user(page, 0, PAGE_SIZE); 561da177e4SLinus Torvalds SetPageUptodate(page); 571da177e4SLinus Torvalds unlock_page(page); 581da177e4SLinus Torvalds return 0; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 611abb5088SBryan Schumaker void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, 62fab5fc25SChristoph Hellwig struct inode *inode, bool force_mds, 63061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 641751c363STrond Myklebust { 65fab5fc25SChristoph Hellwig struct nfs_server *server = NFS_SERVER(inode); 6641d8d5b7SAnna Schumaker const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; 67fab5fc25SChristoph Hellwig 68fab5fc25SChristoph Hellwig #ifdef CONFIG_NFS_V4_1 69fab5fc25SChristoph Hellwig if (server->pnfs_curr_ld && !force_mds) 70fab5fc25SChristoph Hellwig pg_ops = server->pnfs_curr_ld->pg_read_ops; 71fab5fc25SChristoph Hellwig #endif 724a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops, 733bde7afdSTrond Myklebust server->rsize, 0); 741751c363STrond Myklebust } 75ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_read); 761751c363STrond Myklebust 77b42ad64fSDave Wysochanski static void nfs_pageio_complete_read(struct nfs_pageio_descriptor *pgio) 781e83b173SDave Wysochanski { 791e83b173SDave Wysochanski struct nfs_pgio_mirror *pgm; 801e83b173SDave Wysochanski unsigned long npages; 811e83b173SDave Wysochanski 821e83b173SDave Wysochanski nfs_pageio_complete(pgio); 831e83b173SDave Wysochanski 841e83b173SDave Wysochanski /* It doesn't make sense to do mirrored reads! */ 851e83b173SDave Wysochanski WARN_ON_ONCE(pgio->pg_mirror_count != 1); 861e83b173SDave Wysochanski 871e83b173SDave Wysochanski pgm = &pgio->pg_mirrors[0]; 88b42ad64fSDave Wysochanski NFS_I(pgio->pg_inode)->read_io += pgm->pg_bytes_written; 891e83b173SDave Wysochanski npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >> PAGE_SHIFT; 90b42ad64fSDave Wysochanski nfs_add_stats(pgio->pg_inode, NFSIOS_READPAGES, npages); 911e83b173SDave Wysochanski } 921e83b173SDave Wysochanski 931e83b173SDave Wysochanski 94493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio) 95493292ddSTrond Myklebust { 96a7d42ddbSWeston Andros Adamson struct nfs_pgio_mirror *mirror; 97a7d42ddbSWeston Andros Adamson 986f29b9bbSKinglong Mee if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) 996f29b9bbSKinglong Mee pgio->pg_ops->pg_cleanup(pgio); 1006f29b9bbSKinglong Mee 10141d8d5b7SAnna Schumaker pgio->pg_ops = &nfs_pgio_rw_ops; 102a7d42ddbSWeston Andros Adamson 103a7d42ddbSWeston Andros Adamson /* read path should never have more than one mirror */ 104a7d42ddbSWeston Andros Adamson WARN_ON_ONCE(pgio->pg_mirror_count != 1); 105a7d42ddbSWeston Andros Adamson 106a7d42ddbSWeston Andros Adamson mirror = &pgio->pg_mirrors[0]; 107a7d42ddbSWeston Andros Adamson mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize; 108493292ddSTrond Myklebust } 1091f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds); 110493292ddSTrond Myklebust 1118f54c7a4STrond Myklebust static void nfs_readpage_release(struct nfs_page *req, int error) 1120bcbf039SPeng Tao { 1139fcd5960STrond Myklebust struct inode *inode = d_inode(nfs_req_openctx(req)->dentry); 1148f54c7a4STrond Myklebust struct page *page = req->wb_page; 1150bcbf039SPeng Tao 1160bcbf039SPeng Tao dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id, 1170bcbf039SPeng Tao (unsigned long long)NFS_FILEID(inode), req->wb_bytes, 1180bcbf039SPeng Tao (long long)req_offset(req)); 1190bcbf039SPeng Tao 1208f54c7a4STrond Myklebust if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT) 1218f54c7a4STrond Myklebust SetPageError(page); 1220bcbf039SPeng Tao if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) { 1238f54c7a4STrond Myklebust struct address_space *mapping = page_file_mapping(page); 1240bcbf039SPeng Tao 1258f54c7a4STrond Myklebust if (PageUptodate(page)) 126fc1c5abfSDave Wysochanski nfs_fscache_write_page(inode, page); 1278f54c7a4STrond Myklebust else if (!PageError(page) && !PagePrivate(page)) 1288f54c7a4STrond Myklebust generic_error_remove_page(mapping, page); 1298f54c7a4STrond Myklebust unlock_page(page); 1300bcbf039SPeng Tao } 1310bcbf039SPeng Tao nfs_release_request(req); 1320bcbf039SPeng Tao } 1330bcbf039SPeng Tao 1341af7e7f8SDave Wysochanski struct nfs_readdesc { 1351af7e7f8SDave Wysochanski struct nfs_pageio_descriptor pgio; 1361af7e7f8SDave Wysochanski struct nfs_open_context *ctx; 1371af7e7f8SDave Wysochanski }; 1381af7e7f8SDave Wysochanski 13967d0338eSWeston Andros Adamson static void nfs_page_group_set_uptodate(struct nfs_page *req) 14067d0338eSWeston Andros Adamson { 14167d0338eSWeston Andros Adamson if (nfs_page_group_sync_on_bit(req, PG_UPTODATE)) 14267d0338eSWeston Andros Adamson SetPageUptodate(req->wb_page); 14367d0338eSWeston Andros Adamson } 14467d0338eSWeston Andros Adamson 145061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr) 1464db6e0b7SFred Isaman { 1474db6e0b7SFred Isaman unsigned long bytes = 0; 1488f54c7a4STrond Myklebust int error; 1494db6e0b7SFred Isaman 1504db6e0b7SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 1514db6e0b7SFred Isaman goto out; 1524db6e0b7SFred Isaman while (!list_empty(&hdr->pages)) { 1534db6e0b7SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 1544db6e0b7SFred Isaman struct page *page = req->wb_page; 15568072992SWeston Andros Adamson unsigned long start = req->wb_pgbase; 15668072992SWeston Andros Adamson unsigned long end = req->wb_pgbase + req->wb_bytes; 1574db6e0b7SFred Isaman 1584db6e0b7SFred Isaman if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) { 15968072992SWeston Andros Adamson /* note: regions of the page not covered by a 1601e83b173SDave Wysochanski * request are zeroed in readpage_async_filler */ 16168072992SWeston Andros Adamson if (bytes > hdr->good_bytes) { 16268072992SWeston Andros Adamson /* nothing in this request was good, so zero 16368072992SWeston Andros Adamson * the full extent of the request */ 16468072992SWeston Andros Adamson zero_user_segment(page, start, end); 16568072992SWeston Andros Adamson 16668072992SWeston Andros Adamson } else if (hdr->good_bytes - bytes < req->wb_bytes) { 16768072992SWeston Andros Adamson /* part of this request has good bytes, but 16868072992SWeston Andros Adamson * not all. zero the bad bytes */ 16968072992SWeston Andros Adamson start += hdr->good_bytes - bytes; 17068072992SWeston Andros Adamson WARN_ON(start < req->wb_pgbase); 17168072992SWeston Andros Adamson zero_user_segment(page, start, end); 17268072992SWeston Andros Adamson } 1734db6e0b7SFred Isaman } 1748f54c7a4STrond Myklebust error = 0; 1754bd8b010STrond Myklebust bytes += req->wb_bytes; 1764bd8b010STrond Myklebust if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { 1774bd8b010STrond Myklebust if (bytes <= hdr->good_bytes) 17867d0338eSWeston Andros Adamson nfs_page_group_set_uptodate(req); 1798f54c7a4STrond Myklebust else { 1808f54c7a4STrond Myklebust error = hdr->error; 1818f54c7a4STrond Myklebust xchg(&nfs_req_openctx(req)->error, error); 1828f54c7a4STrond Myklebust } 1834bd8b010STrond Myklebust } else 18467d0338eSWeston Andros Adamson nfs_page_group_set_uptodate(req); 1854db6e0b7SFred Isaman nfs_list_remove_request(req); 1868f54c7a4STrond Myklebust nfs_readpage_release(req, error); 1874db6e0b7SFred Isaman } 1884db6e0b7SFred Isaman out: 1894db6e0b7SFred Isaman hdr->release(hdr); 1904db6e0b7SFred Isaman } 1914db6e0b7SFred Isaman 192d45f60c6SWeston Andros Adamson static void nfs_initiate_read(struct nfs_pgio_header *hdr, 193d45f60c6SWeston Andros Adamson struct rpc_message *msg, 194abde71f4STom Haynes const struct nfs_rpc_ops *rpc_ops, 1951ed26f33SAnna Schumaker struct rpc_task_setup *task_setup_data, int how) 19664419a9bSAndy Adamson { 197abde71f4STom Haynes rpc_ops->read_setup(hdr, msg); 1982343172dSTrond Myklebust trace_nfs_initiate_read(hdr); 19964419a9bSAndy Adamson } 20064419a9bSAndy Adamson 201061ae2edSFred Isaman static void 202df3accb8STrond Myklebust nfs_async_read_error(struct list_head *head, int error) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds struct nfs_page *req; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds while (!list_empty(head)) { 2071da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2081da177e4SLinus Torvalds nfs_list_remove_request(req); 2098f54c7a4STrond Myklebust nfs_readpage_release(req, error); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 213061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = { 214061ae2edSFred Isaman .error_cleanup = nfs_async_read_error, 215061ae2edSFred Isaman .completion = nfs_read_completion, 216061ae2edSFred Isaman }; 217061ae2edSFred Isaman 2181da177e4SLinus Torvalds /* 2190b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 2200b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 2210b671301STrond Myklebust */ 222d45f60c6SWeston Andros Adamson static int nfs_readpage_done(struct rpc_task *task, 223d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr, 2240eecb214SAnna Schumaker struct inode *inode) 2250b671301STrond Myklebust { 226d45f60c6SWeston Andros Adamson int status = NFS_PROTO(inode)->read_done(task, hdr); 2270b671301STrond Myklebust if (status != 0) 2280b671301STrond Myklebust return status; 2290b671301STrond Myklebust 230d45f60c6SWeston Andros Adamson nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count); 2312343172dSTrond Myklebust trace_nfs_readpage_done(task, hdr); 2320b671301STrond Myklebust 2330b671301STrond Myklebust if (task->tk_status == -ESTALE) { 23493ce4af7STrond Myklebust nfs_set_inode_stale(inode); 235cd841605SFred Isaman nfs_mark_for_revalidate(inode); 2360b671301STrond Myklebust } 2370b671301STrond Myklebust return 0; 2380b671301STrond Myklebust } 2390b671301STrond Myklebust 240d45f60c6SWeston Andros Adamson static void nfs_readpage_retry(struct rpc_task *task, 241d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr) 2420b671301STrond Myklebust { 243d45f60c6SWeston Andros Adamson struct nfs_pgio_args *argp = &hdr->args; 244d45f60c6SWeston Andros Adamson struct nfs_pgio_res *resp = &hdr->res; 2450b671301STrond Myklebust 2460b671301STrond Myklebust /* This is a short read! */ 247d45f60c6SWeston Andros Adamson nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD); 248fd2b6121SChuck Lever trace_nfs_readpage_short(task, hdr); 249fd2b6121SChuck Lever 2500b671301STrond Myklebust /* Has the server at least made some progress? */ 2514db6e0b7SFred Isaman if (resp->count == 0) { 252d45f60c6SWeston Andros Adamson nfs_set_pgio_error(hdr, -EIO, argp->offset); 253d61e612aSTrond Myklebust return; 2544db6e0b7SFred Isaman } 255f8417b48SKinglong Mee 256f8417b48SKinglong Mee /* For non rpc-based layout drivers, retry-through-MDS */ 257f8417b48SKinglong Mee if (!task->tk_ops) { 258f8417b48SKinglong Mee hdr->pnfs_error = -EAGAIN; 259f8417b48SKinglong Mee return; 260f8417b48SKinglong Mee } 261f8417b48SKinglong Mee 262d45f60c6SWeston Andros Adamson /* Yes, so retry the read at the end of the hdr */ 263d45f60c6SWeston Andros Adamson hdr->mds_offset += resp->count; 2640b671301STrond Myklebust argp->offset += resp->count; 2650b671301STrond Myklebust argp->pgbase += resp->count; 2660b671301STrond Myklebust argp->count -= resp->count; 2678c9cb714STrond Myklebust resp->count = 0; 2688c9cb714STrond Myklebust resp->eof = 0; 269d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 2700b671301STrond Myklebust } 2710b671301STrond Myklebust 272d45f60c6SWeston Andros Adamson static void nfs_readpage_result(struct rpc_task *task, 273d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr) 2741da177e4SLinus Torvalds { 275d45f60c6SWeston Andros Adamson if (hdr->res.eof) { 2761c6c4b74STrond Myklebust loff_t pos = hdr->args.offset + hdr->res.count; 2771c6c4b74STrond Myklebust unsigned int new = pos - hdr->io_start; 278fdd1e74cSTrond Myklebust 2791c6c4b74STrond Myklebust if (hdr->good_bytes > new) { 2801c6c4b74STrond Myklebust hdr->good_bytes = new; 2814db6e0b7SFred Isaman set_bit(NFS_IOHDR_EOF, &hdr->flags); 2824db6e0b7SFred Isaman clear_bit(NFS_IOHDR_ERROR, &hdr->flags); 2834db6e0b7SFred Isaman } 284f8417b48SKinglong Mee } else if (hdr->res.count < hdr->args.count) 285d45f60c6SWeston Andros Adamson nfs_readpage_retry(task, hdr); 2860b671301STrond Myklebust } 287fdd1e74cSTrond Myklebust 2881e83b173SDave Wysochanski static int 2898786fde8SMatthew Wilcox (Oracle) readpage_async_filler(struct nfs_readdesc *desc, struct page *page) 2901e83b173SDave Wysochanski { 2918cfb9015STrond Myklebust struct inode *inode = page_file_mapping(page)->host; 2928cfb9015STrond Myklebust unsigned int rsize = NFS_SERVER(inode)->rsize; 2931e83b173SDave Wysochanski struct nfs_page *new; 2948cfb9015STrond Myklebust unsigned int len, aligned_len; 2951e83b173SDave Wysochanski int error; 2961e83b173SDave Wysochanski 2971e83b173SDave Wysochanski len = nfs_page_length(page); 2981e83b173SDave Wysochanski if (len == 0) 2991e83b173SDave Wysochanski return nfs_return_empty_page(page); 3001e83b173SDave Wysochanski 3018cfb9015STrond Myklebust aligned_len = min_t(unsigned int, ALIGN(len, rsize), PAGE_SIZE); 3028cfb9015STrond Myklebust 30316f2f4e6SDavid Howells if (!IS_SYNC(page->mapping->host)) { 304fc1c5abfSDave Wysochanski error = nfs_fscache_read_page(page->mapping->host, page); 30516f2f4e6SDavid Howells if (error == 0) 30616f2f4e6SDavid Howells goto out_unlock; 30716f2f4e6SDavid Howells } 30816f2f4e6SDavid Howells 3098cfb9015STrond Myklebust new = nfs_create_request(desc->ctx, page, 0, aligned_len); 3101e83b173SDave Wysochanski if (IS_ERR(new)) 3111e83b173SDave Wysochanski goto out_error; 3121e83b173SDave Wysochanski 3131e83b173SDave Wysochanski if (len < PAGE_SIZE) 3141e83b173SDave Wysochanski zero_user_segment(page, len, PAGE_SIZE); 3151e83b173SDave Wysochanski if (!nfs_pageio_add_request(&desc->pgio, new)) { 3161e83b173SDave Wysochanski nfs_list_remove_request(new); 3171e83b173SDave Wysochanski error = desc->pgio.pg_error; 3181e83b173SDave Wysochanski nfs_readpage_release(new, error); 3191e83b173SDave Wysochanski goto out; 3201e83b173SDave Wysochanski } 3211e83b173SDave Wysochanski return 0; 3221e83b173SDave Wysochanski out_error: 3231e83b173SDave Wysochanski error = PTR_ERR(new); 32416f2f4e6SDavid Howells out_unlock: 3251e83b173SDave Wysochanski unlock_page(page); 3261e83b173SDave Wysochanski out: 3271e83b173SDave Wysochanski return error; 3281e83b173SDave Wysochanski } 3291e83b173SDave Wysochanski 3301da177e4SLinus Torvalds /* 3311da177e4SLinus Torvalds * Read a page over NFS. 3321da177e4SLinus Torvalds * We read the page synchronously in the following case: 3331da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 3341da177e4SLinus Torvalds * previous async read operation failed. 3351da177e4SLinus Torvalds */ 336*65d023afSMatthew Wilcox (Oracle) int nfs_read_folio(struct file *file, struct folio *folio) 3371da177e4SLinus Torvalds { 338*65d023afSMatthew Wilcox (Oracle) struct page *page = &folio->page; 3391af7e7f8SDave Wysochanski struct nfs_readdesc desc; 340d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 34149dee700SDave Wysochanski int ret; 3421da177e4SLinus Torvalds 343d9f87743SChuck Lever trace_nfs_aop_readpage(inode, page); 34491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 34591d5b470SChuck Lever 3461da177e4SLinus Torvalds /* 3471da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 3481da177e4SLinus Torvalds * 3491da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 3501da177e4SLinus Torvalds * be any new pending writes generated at this point 3511da177e4SLinus Torvalds * for this page (other pages can be written to). 3521da177e4SLinus Torvalds */ 35349dee700SDave Wysochanski ret = nfs_wb_page(inode, page); 35449dee700SDave Wysochanski if (ret) 355de05a0ccSTrond Myklebust goto out_unlock; 356de05a0ccSTrond Myklebust if (PageUptodate(page)) 357de05a0ccSTrond Myklebust goto out_unlock; 3581da177e4SLinus Torvalds 35949dee700SDave Wysochanski ret = -ESTALE; 3605f004cf2STrond Myklebust if (NFS_STALE(inode)) 361de05a0ccSTrond Myklebust goto out_unlock; 3625f004cf2STrond Myklebust 3631da177e4SLinus Torvalds if (file == NULL) { 36449dee700SDave Wysochanski ret = -EBADF; 3651af7e7f8SDave Wysochanski desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 3661af7e7f8SDave Wysochanski if (desc.ctx == NULL) 367de05a0ccSTrond Myklebust goto out_unlock; 3681da177e4SLinus Torvalds } else 3691af7e7f8SDave Wysochanski desc.ctx = get_nfs_open_context(nfs_file_open_context(file)); 3701da177e4SLinus Torvalds 371ba512c1bSDave Wysochanski xchg(&desc.ctx->error, 0); 3721e83b173SDave Wysochanski nfs_pageio_init_read(&desc.pgio, inode, false, 3731e83b173SDave Wysochanski &nfs_async_read_completion_ops); 3741e83b173SDave Wysochanski 3751e83b173SDave Wysochanski ret = readpage_async_filler(&desc, page); 376e0340f16SDave Wysochanski if (ret) 377e0340f16SDave Wysochanski goto out; 3781e83b173SDave Wysochanski 379b42ad64fSDave Wysochanski nfs_pageio_complete_read(&desc.pgio); 3801e83b173SDave Wysochanski ret = desc.pgio.pg_error < 0 ? desc.pgio.pg_error : 0; 38149dee700SDave Wysochanski if (!ret) { 38249dee700SDave Wysochanski ret = wait_on_page_locked_killable(page); 38349dee700SDave Wysochanski if (!PageUptodate(page) && !ret) 3841af7e7f8SDave Wysochanski ret = xchg(&desc.ctx->error, 0); 3858f54c7a4STrond Myklebust } 3869a9fc1c0SDavid Howells out: 3871af7e7f8SDave Wysochanski put_nfs_open_context(desc.ctx); 388d9f87743SChuck Lever trace_nfs_aop_readpage_done(inode, page, ret); 38949dee700SDave Wysochanski return ret; 390de05a0ccSTrond Myklebust out_unlock: 3911da177e4SLinus Torvalds unlock_page(page); 392d9f87743SChuck Lever trace_nfs_aop_readpage_done(inode, page, ret); 39349dee700SDave Wysochanski return ret; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3968786fde8SMatthew Wilcox (Oracle) void nfs_readahead(struct readahead_control *ractl) 3971da177e4SLinus Torvalds { 3988786fde8SMatthew Wilcox (Oracle) unsigned int nr_pages = readahead_count(ractl); 3998786fde8SMatthew Wilcox (Oracle) struct file *file = ractl->file; 40049dee700SDave Wysochanski struct nfs_readdesc desc; 4018786fde8SMatthew Wilcox (Oracle) struct inode *inode = ractl->mapping->host; 4028786fde8SMatthew Wilcox (Oracle) struct page *page; 40349dee700SDave Wysochanski int ret; 4041da177e4SLinus Torvalds 4058786fde8SMatthew Wilcox (Oracle) trace_nfs_aop_readahead(inode, readahead_pos(ractl), nr_pages); 40691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 4071da177e4SLinus Torvalds 40849dee700SDave Wysochanski ret = -ESTALE; 4095f004cf2STrond Myklebust if (NFS_STALE(inode)) 4105f004cf2STrond Myklebust goto out; 4115f004cf2STrond Myklebust 41249dee700SDave Wysochanski if (file == NULL) { 41349dee700SDave Wysochanski ret = -EBADF; 414d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 4151da177e4SLinus Torvalds if (desc.ctx == NULL) 41649dee700SDave Wysochanski goto out; 4171da177e4SLinus Torvalds } else 41849dee700SDave Wysochanski desc.ctx = get_nfs_open_context(nfs_file_open_context(file)); 4199a9fc1c0SDavid Howells 4201af7e7f8SDave Wysochanski nfs_pageio_init_read(&desc.pgio, inode, false, 421fab5fc25SChristoph Hellwig &nfs_async_read_completion_ops); 4228b09bee3STrond Myklebust 4238786fde8SMatthew Wilcox (Oracle) while ((page = readahead_page(ractl)) != NULL) { 4248786fde8SMatthew Wilcox (Oracle) ret = readpage_async_filler(&desc, page); 4258786fde8SMatthew Wilcox (Oracle) put_page(page); 4268786fde8SMatthew Wilcox (Oracle) if (ret) 4278786fde8SMatthew Wilcox (Oracle) break; 4288786fde8SMatthew Wilcox (Oracle) } 429a7d42ddbSWeston Andros Adamson 430b42ad64fSDave Wysochanski nfs_pageio_complete_read(&desc.pgio); 431a7d42ddbSWeston Andros Adamson 4321da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 4335f004cf2STrond Myklebust out: 434d9f87743SChuck Lever trace_nfs_aop_readahead_done(inode, nr_pages, ret); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 437f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 4381da177e4SLinus Torvalds { 4391da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 4401e7f3a48SWeston Andros Adamson sizeof(struct nfs_pgio_header), 4411da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 44220c2df83SPaul Mundt NULL); 4431da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 4441da177e4SLinus Torvalds return -ENOMEM; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds return 0; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 449266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 4501da177e4SLinus Torvalds { 4511a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 4521da177e4SLinus Torvalds } 4534a0de55cSAnna Schumaker 4544a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops = { 4554a0de55cSAnna Schumaker .rw_alloc_header = nfs_readhdr_alloc, 4564a0de55cSAnna Schumaker .rw_free_header = nfs_readhdr_free, 4570eecb214SAnna Schumaker .rw_done = nfs_readpage_done, 4580eecb214SAnna Schumaker .rw_result = nfs_readpage_result, 4591ed26f33SAnna Schumaker .rw_initiate = nfs_initiate_read, 4604a0de55cSAnna Schumaker }; 461