11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/read.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Block I/O for NFS 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Partial copy of Linus' read cache modifications to fs/nfs/file.c 71da177e4SLinus Torvalds * modified for async RPC by okir@monad.swb.de 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/time.h> 111da177e4SLinus Torvalds #include <linux/kernel.h> 121da177e4SLinus Torvalds #include <linux/errno.h> 131da177e4SLinus Torvalds #include <linux/fcntl.h> 141da177e4SLinus Torvalds #include <linux/stat.h> 151da177e4SLinus Torvalds #include <linux/mm.h> 161da177e4SLinus Torvalds #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/pagemap.h> 181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 191da177e4SLinus Torvalds #include <linux/nfs_fs.h> 201da177e4SLinus Torvalds #include <linux/nfs_page.h> 211da177e4SLinus Torvalds #include <linux/smp_lock.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <asm/system.h> 241da177e4SLinus Torvalds 2549a70f27STrond Myklebust #include "internal.h" 2691d5b470SChuck Lever #include "iostat.h" 279a9fc1c0SDavid Howells #include "fscache.h" 2891d5b470SChuck Lever 291da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 301da177e4SLinus Torvalds 318d5658c9STrond Myklebust static int nfs_pagein_multi(struct inode *, struct list_head *, unsigned int, size_t, int); 328d5658c9STrond Myklebust static int nfs_pagein_one(struct inode *, struct list_head *, unsigned int, size_t, int); 33ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops; 34ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops; 351da177e4SLinus Torvalds 36e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep; 373feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #define MIN_POOL_READ (32) 401da177e4SLinus Torvalds 418d5658c9STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount) 423feb2d49STrond Myklebust { 43e6b4f8daSChristoph Lameter struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_NOFS); 443feb2d49STrond Myklebust 453feb2d49STrond Myklebust if (p) { 463feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 473feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 48e9f7bee1STrond Myklebust p->npages = pagecount; 490d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 500d0b5cb3SChuck Lever p->pagevec = p->page_array; 513feb2d49STrond Myklebust else { 520d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 530d0b5cb3SChuck Lever if (!p->pagevec) { 543feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 553feb2d49STrond Myklebust p = NULL; 563feb2d49STrond Myklebust } 573feb2d49STrond Myklebust } 583feb2d49STrond Myklebust } 593feb2d49STrond Myklebust return p; 603feb2d49STrond Myklebust } 613feb2d49STrond Myklebust 625e4424afSTrond Myklebust static void nfs_readdata_free(struct nfs_read_data *p) 633feb2d49STrond Myklebust { 643feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 653feb2d49STrond Myklebust kfree(p->pagevec); 663feb2d49STrond Myklebust mempool_free(p, nfs_rdata_mempool); 673feb2d49STrond Myklebust } 683feb2d49STrond Myklebust 69963d8fe5STrond Myklebust void nfs_readdata_release(void *data) 701da177e4SLinus Torvalds { 71383ba719STrond Myklebust struct nfs_read_data *rdata = data; 72383ba719STrond Myklebust 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 115f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, 1161da177e4SLinus Torvalds struct page *page) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds LIST_HEAD(one_request); 1191da177e4SLinus Torvalds struct nfs_page *new; 1201da177e4SLinus Torvalds unsigned int len; 1211da177e4SLinus Torvalds 12249a70f27STrond Myklebust len = nfs_page_length(page); 1231da177e4SLinus Torvalds if (len == 0) 1241da177e4SLinus Torvalds return nfs_return_empty_page(page); 1251da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, 0, len); 1261da177e4SLinus Torvalds if (IS_ERR(new)) { 1271da177e4SLinus Torvalds unlock_page(page); 1281da177e4SLinus Torvalds return PTR_ERR(new); 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 131eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds nfs_list_add_request(new, &one_request); 134bcb71bbaSTrond Myklebust if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) 1358d5658c9STrond Myklebust nfs_pagein_multi(inode, &one_request, 1, len, 0); 136bcb71bbaSTrond Myklebust else 1378d5658c9STrond Myklebust nfs_pagein_one(inode, &one_request, 1, len, 0); 1381da177e4SLinus Torvalds return 0; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds unlock_page(req->wb_page); 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", 14688be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 14788be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1481da177e4SLinus Torvalds req->wb_bytes, 1491da177e4SLinus Torvalds (long long)req_offset(req)); 15010d2c46fSNick Wilson nfs_clear_request(req); 15110d2c46fSNick Wilson nfs_release_request(req); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 1551da177e4SLinus Torvalds * Set up the NFS read request struct 1561da177e4SLinus Torvalds */ 157dbae4c73STrond Myklebust static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, 158ec06c096STrond Myklebust const struct rpc_call_ops *call_ops, 1591da177e4SLinus Torvalds unsigned int count, unsigned int offset) 1601da177e4SLinus Torvalds { 16184115e1cSTrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 16284115e1cSTrond Myklebust int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; 16307737691STrond Myklebust struct rpc_task *task; 164bdc7f021STrond Myklebust struct rpc_message msg = { 165bdc7f021STrond Myklebust .rpc_argp = &data->args, 166bdc7f021STrond Myklebust .rpc_resp = &data->res, 167bdc7f021STrond Myklebust .rpc_cred = req->wb_context->cred, 168bdc7f021STrond Myklebust }; 16984115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 17007737691STrond Myklebust .task = &data->task, 17184115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 172bdc7f021STrond Myklebust .rpc_message = &msg, 17384115e1cSTrond Myklebust .callback_ops = call_ops, 17484115e1cSTrond Myklebust .callback_data = data, 175101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 17684115e1cSTrond Myklebust .flags = RPC_TASK_ASYNC | swap_flags, 17784115e1cSTrond Myklebust }; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds data->req = req; 18084115e1cSTrond Myklebust data->inode = inode; 181bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 1841da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 1851da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 1861da177e4SLinus Torvalds data->args.pages = data->pagevec; 1871da177e4SLinus Torvalds data->args.count = count; 188383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds data->res.fattr = &data->fattr; 1911da177e4SLinus Torvalds data->res.count = count; 1921da177e4SLinus Torvalds data->res.eof = 0; 1930e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 1941da177e4SLinus Torvalds 195ec06c096STrond Myklebust /* Set up the initial task struct. */ 196bdc7f021STrond Myklebust NFS_PROTO(inode)->read_setup(data, &msg); 1971da177e4SLinus Torvalds 198a3f565b1SChuck Lever dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", 1991da177e4SLinus Torvalds data->task.tk_pid, 2001da177e4SLinus Torvalds inode->i_sb->s_id, 2011da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 2021da177e4SLinus Torvalds count, 2031da177e4SLinus Torvalds (unsigned long long)data->args.offset); 204bdc7f021STrond Myklebust 20507737691STrond Myklebust task = rpc_run_task(&task_setup_data); 206dbae4c73STrond Myklebust if (IS_ERR(task)) 207dbae4c73STrond Myklebust return PTR_ERR(task); 20807737691STrond Myklebust rpc_put_task(task); 209dbae4c73STrond Myklebust return 0; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds static void 2131da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct nfs_page *req; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds while (!list_empty(head)) { 2181da177e4SLinus Torvalds req = nfs_list_entry(head->next); 2191da177e4SLinus Torvalds nfs_list_remove_request(req); 2201da177e4SLinus Torvalds SetPageError(req->wb_page); 2211da177e4SLinus Torvalds nfs_readpage_release(req); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds /* 2261da177e4SLinus Torvalds * Generate multiple requests to fill a single page. 2271da177e4SLinus Torvalds * 2281da177e4SLinus Torvalds * We optimize to reduce the number of read operations on the wire. If we 2291da177e4SLinus Torvalds * detect that we're reading a page, or an area of a page, that is past the 2301da177e4SLinus Torvalds * end of file, we do not generate NFS read operations but just clear the 2311da177e4SLinus Torvalds * parts of the page that would have come back zero from the server anyway. 2321da177e4SLinus Torvalds * 2331da177e4SLinus Torvalds * We rely on the cached value of i_size to make this determination; another 2341da177e4SLinus Torvalds * client can fill pages on the server past our cached end-of-file, but we 2351da177e4SLinus Torvalds * won't see the new data until our attribute cache is updated. This is more 2361da177e4SLinus Torvalds * or less conventional NFS client behavior. 2371da177e4SLinus Torvalds */ 2388d5658c9STrond Myklebust static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 2411da177e4SLinus Torvalds struct page *page = req->wb_page; 2421da177e4SLinus Torvalds struct nfs_read_data *data; 243e9f7bee1STrond Myklebust size_t rsize = NFS_SERVER(inode)->rsize, nbytes; 244e9f7bee1STrond Myklebust unsigned int offset; 2451da177e4SLinus Torvalds int requests = 0; 246dbae4c73STrond Myklebust int ret = 0; 2471da177e4SLinus Torvalds LIST_HEAD(list); 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds nfs_list_remove_request(req); 2501da177e4SLinus Torvalds 251bcb71bbaSTrond Myklebust nbytes = count; 252e9f7bee1STrond Myklebust do { 253e9f7bee1STrond Myklebust size_t len = min(nbytes,rsize); 254e9f7bee1STrond Myklebust 2558d5658c9STrond Myklebust data = nfs_readdata_alloc(1); 2561da177e4SLinus Torvalds if (!data) 2571da177e4SLinus Torvalds goto out_bad; 2581da177e4SLinus Torvalds list_add(&data->pages, &list); 2591da177e4SLinus Torvalds requests++; 260e9f7bee1STrond Myklebust nbytes -= len; 261e9f7bee1STrond Myklebust } while(nbytes != 0); 2621da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds ClearPageError(page); 2651da177e4SLinus Torvalds offset = 0; 266bcb71bbaSTrond Myklebust nbytes = count; 2671da177e4SLinus Torvalds do { 268dbae4c73STrond Myklebust int ret2; 269dbae4c73STrond Myklebust 2701da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 2711da177e4SLinus Torvalds list_del_init(&data->pages); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds data->pagevec[0] = page; 2741da177e4SLinus Torvalds 275bcb71bbaSTrond Myklebust if (nbytes < rsize) 276bcb71bbaSTrond Myklebust rsize = nbytes; 277dbae4c73STrond Myklebust ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, 278ec06c096STrond Myklebust rsize, offset); 279dbae4c73STrond Myklebust if (ret == 0) 280dbae4c73STrond Myklebust ret = ret2; 2811da177e4SLinus Torvalds offset += rsize; 2821da177e4SLinus Torvalds nbytes -= rsize; 2831da177e4SLinus Torvalds } while (nbytes != 0); 2841da177e4SLinus Torvalds 285dbae4c73STrond Myklebust return ret; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds out_bad: 2881da177e4SLinus Torvalds while (!list_empty(&list)) { 2891da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_read_data, pages); 2901da177e4SLinus Torvalds list_del(&data->pages); 2911da177e4SLinus Torvalds nfs_readdata_free(data); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds SetPageError(page); 2941da177e4SLinus Torvalds nfs_readpage_release(req); 2951da177e4SLinus Torvalds return -ENOMEM; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2988d5658c9STrond Myklebust static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds struct nfs_page *req; 3011da177e4SLinus Torvalds struct page **pages; 3021da177e4SLinus Torvalds struct nfs_read_data *data; 303dbae4c73STrond Myklebust int ret = -ENOMEM; 3041da177e4SLinus Torvalds 3058d5658c9STrond Myklebust data = nfs_readdata_alloc(npages); 3061da177e4SLinus Torvalds if (!data) 3071da177e4SLinus Torvalds goto out_bad; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds pages = data->pagevec; 3101da177e4SLinus Torvalds while (!list_empty(head)) { 3111da177e4SLinus Torvalds req = nfs_list_entry(head->next); 3121da177e4SLinus Torvalds nfs_list_remove_request(req); 3131da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 3141da177e4SLinus Torvalds ClearPageError(req->wb_page); 3151da177e4SLinus Torvalds *pages++ = req->wb_page; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 3181da177e4SLinus Torvalds 319dbae4c73STrond Myklebust return nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); 3201da177e4SLinus Torvalds out_bad: 3211da177e4SLinus Torvalds nfs_async_read_error(head); 322dbae4c73STrond Myklebust return ret; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* 3260b671301STrond Myklebust * This is the callback from RPC telling us whether a reply was 3270b671301STrond Myklebust * received or some error occurred (timeout or socket shutdown). 3280b671301STrond Myklebust */ 3290b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) 3300b671301STrond Myklebust { 3310b671301STrond Myklebust int status; 3320b671301STrond Myklebust 3333110ff80SHarvey Harrison dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid, 3340b671301STrond Myklebust task->tk_status); 3350b671301STrond Myklebust 3360b671301STrond Myklebust status = NFS_PROTO(data->inode)->read_done(task, data); 3370b671301STrond Myklebust if (status != 0) 3380b671301STrond Myklebust return status; 3390b671301STrond Myklebust 3400b671301STrond Myklebust nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); 3410b671301STrond Myklebust 3420b671301STrond Myklebust if (task->tk_status == -ESTALE) { 3433a10c30aSBenny Halevy set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags); 3440b671301STrond Myklebust nfs_mark_for_revalidate(data->inode); 3450b671301STrond Myklebust } 3460b671301STrond Myklebust return 0; 3470b671301STrond Myklebust } 3480b671301STrond Myklebust 349fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) 3500b671301STrond Myklebust { 3510b671301STrond Myklebust struct nfs_readargs *argp = &data->args; 3520b671301STrond Myklebust struct nfs_readres *resp = &data->res; 3530b671301STrond Myklebust 3540b671301STrond Myklebust if (resp->eof || resp->count == argp->count) 355fdd1e74cSTrond Myklebust return; 3560b671301STrond Myklebust 3570b671301STrond Myklebust /* This is a short read! */ 3580b671301STrond Myklebust nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 3590b671301STrond Myklebust /* Has the server at least made some progress? */ 3600b671301STrond Myklebust if (resp->count == 0) 361fdd1e74cSTrond Myklebust return; 3620b671301STrond Myklebust 3630b671301STrond Myklebust /* Yes, so retry the read at the end of the data */ 3640b671301STrond Myklebust argp->offset += resp->count; 3650b671301STrond Myklebust argp->pgbase += resp->count; 3660b671301STrond Myklebust argp->count -= resp->count; 3670b671301STrond Myklebust rpc_restart_call(task); 3680b671301STrond Myklebust } 3690b671301STrond Myklebust 3700b671301STrond Myklebust /* 3711da177e4SLinus Torvalds * Handle a read reply that fills part of a page. 3721da177e4SLinus Torvalds */ 373ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) 3741da177e4SLinus Torvalds { 375ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 3761da177e4SLinus Torvalds 377ec06c096STrond Myklebust if (nfs_readpage_result(task, data) != 0) 378ec06c096STrond Myklebust return; 379fdd1e74cSTrond Myklebust if (task->tk_status < 0) 3800b671301STrond Myklebust return; 381fdd1e74cSTrond Myklebust 382fdd1e74cSTrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 383fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 3840b671301STrond Myklebust } 385fdd1e74cSTrond Myklebust 386fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata) 387fdd1e74cSTrond Myklebust { 388fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 389fdd1e74cSTrond Myklebust struct nfs_page *req = data->req; 390fdd1e74cSTrond Myklebust struct page *page = req->wb_page; 391fdd1e74cSTrond Myklebust int status = data->task.tk_status; 392fdd1e74cSTrond Myklebust 393fdd1e74cSTrond Myklebust if (status < 0) 3940b671301STrond Myklebust SetPageError(page); 395fdd1e74cSTrond Myklebust 3961da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) { 3971da177e4SLinus Torvalds if (!PageError(page)) 3981da177e4SLinus Torvalds SetPageUptodate(page); 3991da177e4SLinus Torvalds nfs_readpage_release(req); 4001da177e4SLinus Torvalds } 401fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 404ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = { 405ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_partial, 406fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_partial, 407ec06c096STrond Myklebust }; 408ec06c096STrond Myklebust 4091de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) 4101de3fc12STrond Myklebust { 4111de3fc12STrond Myklebust unsigned int count = data->res.count; 4121de3fc12STrond Myklebust unsigned int base = data->args.pgbase; 4131de3fc12STrond Myklebust struct page **pages; 4141de3fc12STrond Myklebust 41579558f36STrond Myklebust if (data->res.eof) 41679558f36STrond Myklebust count = data->args.count; 4171de3fc12STrond Myklebust if (unlikely(count == 0)) 4181de3fc12STrond Myklebust return; 4191de3fc12STrond Myklebust pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; 4201de3fc12STrond Myklebust base &= ~PAGE_CACHE_MASK; 4211de3fc12STrond Myklebust count += base; 4221de3fc12STrond Myklebust for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) 4231de3fc12STrond Myklebust SetPageUptodate(*pages); 4240b671301STrond Myklebust if (count == 0) 4250b671301STrond Myklebust return; 4260b671301STrond Myklebust /* Was this a short read? */ 4270b671301STrond Myklebust if (data->res.eof || data->res.count == data->args.count) 4281de3fc12STrond Myklebust SetPageUptodate(*pages); 4291de3fc12STrond Myklebust } 4301de3fc12STrond Myklebust 4311da177e4SLinus Torvalds /* 4321da177e4SLinus Torvalds * This is the callback from RPC telling us whether a reply was 4331da177e4SLinus Torvalds * received or some error occurred (timeout or socket shutdown). 4341da177e4SLinus Torvalds */ 435ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 4361da177e4SLinus Torvalds { 437ec06c096STrond Myklebust struct nfs_read_data *data = calldata; 4381da177e4SLinus Torvalds 4390b671301STrond Myklebust if (nfs_readpage_result(task, data) != 0) 4400b671301STrond Myklebust return; 441fdd1e74cSTrond Myklebust if (task->tk_status < 0) 442fdd1e74cSTrond Myklebust return; 4431de3fc12STrond Myklebust /* 4440b671301STrond Myklebust * Note: nfs_readpage_retry may change the values of 4451de3fc12STrond Myklebust * data->args. In the multi-page case, we therefore need 4460b671301STrond Myklebust * to ensure that we call nfs_readpage_set_pages_uptodate() 4470b671301STrond Myklebust * first. 4481de3fc12STrond Myklebust */ 4491de3fc12STrond Myklebust nfs_readpage_truncate_uninitialised_page(data); 4501de3fc12STrond Myklebust nfs_readpage_set_pages_uptodate(data); 451fdd1e74cSTrond Myklebust nfs_readpage_retry(task, data); 4520b671301STrond Myklebust } 453fdd1e74cSTrond Myklebust 454fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata) 455fdd1e74cSTrond Myklebust { 456fdd1e74cSTrond Myklebust struct nfs_read_data *data = calldata; 457fdd1e74cSTrond Myklebust 4581da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 4591da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(data->pages.next); 4601da177e4SLinus Torvalds 4611de3fc12STrond Myklebust nfs_list_remove_request(req); 4621da177e4SLinus Torvalds nfs_readpage_release(req); 4631da177e4SLinus Torvalds } 464fdd1e74cSTrond Myklebust nfs_readdata_release(calldata); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 467ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = { 468ec06c096STrond Myklebust .rpc_call_done = nfs_readpage_result_full, 469fdd1e74cSTrond Myklebust .rpc_release = nfs_readpage_release_full, 470ec06c096STrond Myklebust }; 471ec06c096STrond Myklebust 4721da177e4SLinus Torvalds /* 4731da177e4SLinus Torvalds * Read a page over NFS. 4741da177e4SLinus Torvalds * We read the page synchronously in the following case: 4751da177e4SLinus Torvalds * - The error flag is set for this page. This happens only when a 4761da177e4SLinus Torvalds * previous async read operation failed. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds struct nfs_open_context *ctx; 4811da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 4821da177e4SLinus Torvalds int error; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", 4851da177e4SLinus Torvalds page, PAGE_CACHE_SIZE, page->index); 48691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); 48791d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_READPAGES, 1); 48891d5b470SChuck Lever 4891da177e4SLinus Torvalds /* 4901da177e4SLinus Torvalds * Try to flush any pending writes to the file.. 4911da177e4SLinus Torvalds * 4921da177e4SLinus Torvalds * NOTE! Because we own the page lock, there cannot 4931da177e4SLinus Torvalds * be any new pending writes generated at this point 4941da177e4SLinus Torvalds * for this page (other pages can be written to). 4951da177e4SLinus Torvalds */ 4961da177e4SLinus Torvalds error = nfs_wb_page(inode, page); 4971da177e4SLinus Torvalds if (error) 498de05a0ccSTrond Myklebust goto out_unlock; 499de05a0ccSTrond Myklebust if (PageUptodate(page)) 500de05a0ccSTrond Myklebust goto out_unlock; 5011da177e4SLinus Torvalds 5025f004cf2STrond Myklebust error = -ESTALE; 5035f004cf2STrond Myklebust if (NFS_STALE(inode)) 504de05a0ccSTrond Myklebust goto out_unlock; 5055f004cf2STrond Myklebust 5061da177e4SLinus Torvalds if (file == NULL) { 507cf1308ffSTrond Myklebust error = -EBADF; 508d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 5091da177e4SLinus Torvalds if (ctx == NULL) 510de05a0ccSTrond Myklebust goto out_unlock; 5111da177e4SLinus Torvalds } else 512cd3758e3STrond Myklebust ctx = get_nfs_open_context(nfs_file_open_context(file)); 5131da177e4SLinus Torvalds 5149a9fc1c0SDavid Howells if (!IS_SYNC(inode)) { 5159a9fc1c0SDavid Howells error = nfs_readpage_from_fscache(ctx, inode, page); 5169a9fc1c0SDavid Howells if (error == 0) 5179a9fc1c0SDavid Howells goto out; 5189a9fc1c0SDavid Howells } 5199a9fc1c0SDavid Howells 5208e0969f0STrond Myklebust error = nfs_readpage_async(ctx, inode, page); 5218e0969f0STrond Myklebust 5229a9fc1c0SDavid Howells out: 5231da177e4SLinus Torvalds put_nfs_open_context(ctx); 5241da177e4SLinus Torvalds return error; 525de05a0ccSTrond Myklebust out_unlock: 5261da177e4SLinus Torvalds unlock_page(page); 5271da177e4SLinus Torvalds return error; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds struct nfs_readdesc { 5318b09bee3STrond Myklebust struct nfs_pageio_descriptor *pgio; 5321da177e4SLinus Torvalds struct nfs_open_context *ctx; 5331da177e4SLinus Torvalds }; 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds static int 5361da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page) 5371da177e4SLinus Torvalds { 5381da177e4SLinus Torvalds struct nfs_readdesc *desc = (struct nfs_readdesc *)data; 5391da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 5401da177e4SLinus Torvalds struct nfs_page *new; 5411da177e4SLinus Torvalds unsigned int len; 542de05a0ccSTrond Myklebust int error; 5431da177e4SLinus Torvalds 54449a70f27STrond Myklebust len = nfs_page_length(page); 5451da177e4SLinus Torvalds if (len == 0) 5461da177e4SLinus Torvalds return nfs_return_empty_page(page); 547de05a0ccSTrond Myklebust 5481da177e4SLinus Torvalds new = nfs_create_request(desc->ctx, inode, page, 0, len); 549de05a0ccSTrond Myklebust if (IS_ERR(new)) 550de05a0ccSTrond Myklebust goto out_error; 551de05a0ccSTrond Myklebust 5521da177e4SLinus Torvalds if (len < PAGE_CACHE_SIZE) 553eebd2aa3SChristoph Lameter zero_user_segment(page, len, PAGE_CACHE_SIZE); 554f8512ad0SFred Isaman if (!nfs_pageio_add_request(desc->pgio, new)) { 555f8512ad0SFred Isaman error = desc->pgio->pg_error; 556f8512ad0SFred Isaman goto out_unlock; 557f8512ad0SFred Isaman } 5581da177e4SLinus Torvalds return 0; 559de05a0ccSTrond Myklebust out_error: 560de05a0ccSTrond Myklebust error = PTR_ERR(new); 561de05a0ccSTrond Myklebust SetPageError(page); 562de05a0ccSTrond Myklebust out_unlock: 563de05a0ccSTrond Myklebust unlock_page(page); 564de05a0ccSTrond Myklebust return error; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping, 5681da177e4SLinus Torvalds struct list_head *pages, unsigned nr_pages) 5691da177e4SLinus Torvalds { 5708b09bee3STrond Myklebust struct nfs_pageio_descriptor pgio; 5711da177e4SLinus Torvalds struct nfs_readdesc desc = { 5728b09bee3STrond Myklebust .pgio = &pgio, 5731da177e4SLinus Torvalds }; 5741da177e4SLinus Torvalds struct inode *inode = mapping->host; 5751da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 5768b09bee3STrond Myklebust size_t rsize = server->rsize; 5778b09bee3STrond Myklebust unsigned long npages; 5785f004cf2STrond Myklebust int ret = -ESTALE; 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 5811da177e4SLinus Torvalds inode->i_sb->s_id, 5821da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 5831da177e4SLinus Torvalds nr_pages); 58491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 5851da177e4SLinus Torvalds 5865f004cf2STrond Myklebust if (NFS_STALE(inode)) 5875f004cf2STrond Myklebust goto out; 5885f004cf2STrond Myklebust 5891da177e4SLinus Torvalds if (filp == NULL) { 590d530838bSTrond Myklebust desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 5911da177e4SLinus Torvalds if (desc.ctx == NULL) 5921da177e4SLinus Torvalds return -EBADF; 5931da177e4SLinus Torvalds } else 594cd3758e3STrond Myklebust desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); 5959a9fc1c0SDavid Howells 5969a9fc1c0SDavid Howells /* attempt to read as many of the pages as possible from the cache 5979a9fc1c0SDavid Howells * - this returns -ENOBUFS immediately if the cookie is negative 5989a9fc1c0SDavid Howells */ 5999a9fc1c0SDavid Howells ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, 6009a9fc1c0SDavid Howells pages, &nr_pages); 6019a9fc1c0SDavid Howells if (ret == 0) 6029a9fc1c0SDavid Howells goto read_complete; /* all pages were read */ 6039a9fc1c0SDavid Howells 6048b09bee3STrond Myklebust if (rsize < PAGE_CACHE_SIZE) 6058b09bee3STrond Myklebust nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0); 6068b09bee3STrond Myklebust else 6078b09bee3STrond Myklebust nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0); 6088b09bee3STrond Myklebust 6091da177e4SLinus Torvalds ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); 6108b09bee3STrond Myklebust 6118b09bee3STrond Myklebust nfs_pageio_complete(&pgio); 6128b09bee3STrond Myklebust npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 6138b09bee3STrond Myklebust nfs_add_stats(inode, NFSIOS_READPAGES, npages); 6149a9fc1c0SDavid Howells read_complete: 6151da177e4SLinus Torvalds put_nfs_open_context(desc.ctx); 6165f004cf2STrond Myklebust out: 6171da177e4SLinus Torvalds return ret; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 620f7b422b1SDavid Howells int __init nfs_init_readpagecache(void) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 6231da177e4SLinus Torvalds sizeof(struct nfs_read_data), 6241da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 62520c2df83SPaul Mundt NULL); 6261da177e4SLinus Torvalds if (nfs_rdata_cachep == NULL) 6271da177e4SLinus Torvalds return -ENOMEM; 6281da177e4SLinus Torvalds 62993d2341cSMatthew Dobson nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, 6301da177e4SLinus Torvalds nfs_rdata_cachep); 6311da177e4SLinus Torvalds if (nfs_rdata_mempool == NULL) 6321da177e4SLinus Torvalds return -ENOMEM; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds return 0; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 637266bee88SDavid Brownell void nfs_destroy_readpagecache(void) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds mempool_destroy(nfs_rdata_mempool); 6401a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_rdata_cachep); 6411da177e4SLinus Torvalds } 642