Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/read.c |
| 3 | * |
| 4 | * Block I/O for NFS |
| 5 | * |
| 6 | * Partial copy of Linus' read cache modifications to fs/nfs/file.c |
| 7 | * modified for async RPC by okir@monad.swb.de |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/time.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/fcntl.h> |
| 14 | #include <linux/stat.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/sunrpc/clnt.h> |
| 19 | #include <linux/nfs_fs.h> |
| 20 | #include <linux/nfs_page.h> |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 21 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/system.h> |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 24 | #include "pnfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Andy Adamson | f11c88a | 2009-04-01 09:22:25 -0400 | [diff] [blame] | 26 | #include "nfs4_fs.h" |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 27 | #include "internal.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 28 | #include "iostat.h" |
David Howells | 9a9fc1c | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 29 | #include "fscache.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 32 | |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 33 | static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc); |
| 34 | static int nfs_pagein_one(struct nfs_pageio_descriptor *desc); |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame^] | 35 | static const struct nfs_pageio_ops nfs_pageio_read_ops; |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 36 | static const struct rpc_call_ops nfs_read_partial_ops; |
| 37 | static const struct rpc_call_ops nfs_read_full_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 39 | static struct kmem_cache *nfs_rdata_cachep; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 40 | static mempool_t *nfs_rdata_mempool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #define MIN_POOL_READ (32) |
| 43 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 44 | struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 45 | { |
Trond Myklebust | 93870d7 | 2010-05-13 12:51:03 -0400 | [diff] [blame] | 46 | struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_KERNEL); |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 47 | |
| 48 | if (p) { |
| 49 | memset(p, 0, sizeof(*p)); |
| 50 | INIT_LIST_HEAD(&p->pages); |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 51 | p->npages = pagecount; |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 52 | if (pagecount <= ARRAY_SIZE(p->page_array)) |
| 53 | p->pagevec = p->page_array; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 54 | else { |
Trond Myklebust | 93870d7 | 2010-05-13 12:51:03 -0400 | [diff] [blame] | 55 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL); |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 56 | if (!p->pagevec) { |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 57 | mempool_free(p, nfs_rdata_mempool); |
| 58 | p = NULL; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return p; |
| 63 | } |
| 64 | |
Trond Myklebust | 1ae88b2 | 2009-08-12 09:12:30 -0400 | [diff] [blame] | 65 | void nfs_readdata_free(struct nfs_read_data *p) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 66 | { |
| 67 | if (p && (p->pagevec != &p->page_array[0])) |
| 68 | kfree(p->pagevec); |
| 69 | mempool_free(p, nfs_rdata_mempool); |
| 70 | } |
| 71 | |
Trond Myklebust | 1ae88b2 | 2009-08-12 09:12:30 -0400 | [diff] [blame] | 72 | static void nfs_readdata_release(struct nfs_read_data *rdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 74 | put_lseg(rdata->lseg); |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 75 | put_nfs_open_context(rdata->args.context); |
| 76 | nfs_readdata_free(rdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int nfs_return_empty_page(struct page *page) |
| 81 | { |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 82 | zero_user(page, 0, PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | SetPageUptodate(page); |
| 84 | unlock_page(page); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 88 | static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) |
| 89 | { |
| 90 | unsigned int remainder = data->args.count - data->res.count; |
| 91 | unsigned int base = data->args.pgbase + data->res.count; |
| 92 | unsigned int pglen; |
| 93 | struct page **pages; |
| 94 | |
| 95 | if (data->res.eof == 0 || remainder == 0) |
| 96 | return; |
| 97 | /* |
| 98 | * Note: "remainder" can never be negative, since we check for |
| 99 | * this in the XDR code. |
| 100 | */ |
| 101 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 102 | base &= ~PAGE_CACHE_MASK; |
| 103 | pglen = PAGE_CACHE_SIZE - base; |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 104 | for (;;) { |
| 105 | if (remainder <= pglen) { |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 106 | zero_user(*pages, base, remainder); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 107 | break; |
| 108 | } |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 109 | zero_user(*pages, base, pglen); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 110 | pages++; |
| 111 | remainder -= pglen; |
| 112 | pglen = PAGE_CACHE_SIZE; |
| 113 | base = 0; |
| 114 | } |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame^] | 117 | static void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio, |
| 118 | struct inode *inode) |
| 119 | { |
| 120 | nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops, |
| 121 | NFS_SERVER(inode)->rsize, 0); |
| 122 | } |
| 123 | |
| 124 | static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, |
| 125 | struct inode *inode) |
| 126 | { |
| 127 | if (!pnfs_pageio_init_read(pgio, inode)) |
| 128 | nfs_pageio_init_read_mds(pgio, inode); |
| 129 | } |
| 130 | |
David Howells | f42b293 | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 131 | int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, |
| 132 | struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | struct nfs_page *new; |
| 135 | unsigned int len; |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 136 | struct nfs_pageio_descriptor pgio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 138 | len = nfs_page_length(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (len == 0) |
| 140 | return nfs_return_empty_page(page); |
| 141 | new = nfs_create_request(ctx, inode, page, 0, len); |
| 142 | if (IS_ERR(new)) { |
| 143 | unlock_page(page); |
| 144 | return PTR_ERR(new); |
| 145 | } |
| 146 | if (len < PAGE_CACHE_SIZE) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 147 | zero_user_segment(page, len, PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame^] | 149 | nfs_pageio_init_read(&pgio, inode); |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 150 | nfs_list_add_request(new, &pgio.pg_list); |
| 151 | pgio.pg_count = len; |
| 152 | |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame^] | 153 | nfs_pageio_complete(&pgio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static void nfs_readpage_release(struct nfs_page *req) |
| 158 | { |
David Howells | 7f8e05f | 2009-04-03 16:42:45 +0100 | [diff] [blame] | 159 | struct inode *d_inode = req->wb_context->path.dentry->d_inode; |
| 160 | |
| 161 | if (PageUptodate(req->wb_page)) |
| 162 | nfs_readpage_to_fscache(d_inode, req->wb_page, 0); |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | unlock_page(req->wb_page); |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 167 | req->wb_context->path.dentry->d_inode->i_sb->s_id, |
| 168 | (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | req->wb_bytes, |
| 170 | (long long)req_offset(req)); |
Nick Wilson | 10d2c46 | 2005-09-22 21:44:28 -0700 | [diff] [blame] | 171 | nfs_release_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Andy Adamson | dc70d7b | 2011-03-01 01:34:19 +0000 | [diff] [blame] | 174 | int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt, |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 175 | const struct rpc_call_ops *call_ops) |
| 176 | { |
| 177 | struct inode *inode = data->inode; |
| 178 | int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; |
| 179 | struct rpc_task *task; |
| 180 | struct rpc_message msg = { |
| 181 | .rpc_argp = &data->args, |
| 182 | .rpc_resp = &data->res, |
| 183 | .rpc_cred = data->cred, |
| 184 | }; |
| 185 | struct rpc_task_setup task_setup_data = { |
| 186 | .task = &data->task, |
| 187 | .rpc_client = clnt, |
| 188 | .rpc_message = &msg, |
| 189 | .callback_ops = call_ops, |
| 190 | .callback_data = data, |
| 191 | .workqueue = nfsiod_workqueue, |
| 192 | .flags = RPC_TASK_ASYNC | swap_flags, |
| 193 | }; |
| 194 | |
| 195 | /* Set up the initial task struct. */ |
| 196 | NFS_PROTO(inode)->read_setup(data, &msg); |
| 197 | |
| 198 | dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ " |
| 199 | "offset %llu)\n", |
| 200 | data->task.tk_pid, |
| 201 | inode->i_sb->s_id, |
| 202 | (long long)NFS_FILEID(inode), |
| 203 | data->args.count, |
| 204 | (unsigned long long)data->args.offset); |
| 205 | |
| 206 | task = rpc_run_task(&task_setup_data); |
| 207 | if (IS_ERR(task)) |
| 208 | return PTR_ERR(task); |
| 209 | rpc_put_task(task); |
| 210 | return 0; |
| 211 | } |
Andy Adamson | dc70d7b | 2011-03-01 01:34:19 +0000 | [diff] [blame] | 212 | EXPORT_SYMBOL_GPL(nfs_initiate_read); |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* |
| 215 | * Set up the NFS read request struct |
| 216 | */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 217 | static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 218 | const struct rpc_call_ops *call_ops, |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 219 | unsigned int count, unsigned int offset, |
| 220 | struct pnfs_layout_segment *lseg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 222 | struct inode *inode = req->wb_context->path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | data->req = req; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 225 | data->inode = inode; |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 226 | data->cred = req->wb_context->cred; |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 227 | data->lseg = get_lseg(lseg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | data->args.fh = NFS_FH(inode); |
| 230 | data->args.offset = req_offset(req) + offset; |
| 231 | data->args.pgbase = req->wb_pgbase + offset; |
| 232 | data->args.pages = data->pagevec; |
| 233 | data->args.count = count; |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 234 | data->args.context = get_nfs_open_context(req->wb_context); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 235 | data->args.lock_context = req->wb_lock_context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | data->res.fattr = &data->fattr; |
| 238 | data->res.count = count; |
| 239 | data->res.eof = 0; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 240 | nfs_fattr_init(&data->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 242 | if (data->lseg && |
| 243 | (pnfs_try_to_read_data(data, call_ops) == PNFS_ATTEMPTED)) |
| 244 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Andy Adamson | 64419a9 | 2011-03-01 01:34:16 +0000 | [diff] [blame] | 246 | return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void |
| 250 | nfs_async_read_error(struct list_head *head) |
| 251 | { |
| 252 | struct nfs_page *req; |
| 253 | |
| 254 | while (!list_empty(head)) { |
| 255 | req = nfs_list_entry(head->next); |
| 256 | nfs_list_remove_request(req); |
| 257 | SetPageError(req->wb_page); |
| 258 | nfs_readpage_release(req); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | * Generate multiple requests to fill a single page. |
| 264 | * |
| 265 | * We optimize to reduce the number of read operations on the wire. If we |
| 266 | * detect that we're reading a page, or an area of a page, that is past the |
| 267 | * end of file, we do not generate NFS read operations but just clear the |
| 268 | * parts of the page that would have come back zero from the server anyway. |
| 269 | * |
| 270 | * We rely on the cached value of i_size to make this determination; another |
| 271 | * client can fill pages on the server past our cached end-of-file, but we |
| 272 | * won't see the new data until our attribute cache is updated. This is more |
| 273 | * or less conventional NFS client behavior. |
| 274 | */ |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 275 | static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 277 | struct nfs_page *req = nfs_list_entry(desc->pg_list.next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | struct page *page = req->wb_page; |
| 279 | struct nfs_read_data *data; |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 280 | size_t rsize = NFS_SERVER(desc->pg_inode)->rsize, nbytes; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 281 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | int requests = 0; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 283 | int ret = 0; |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 284 | struct pnfs_layout_segment *lseg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | LIST_HEAD(list); |
| 286 | |
| 287 | nfs_list_remove_request(req); |
| 288 | |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 289 | nbytes = desc->pg_count; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 290 | do { |
| 291 | size_t len = min(nbytes,rsize); |
| 292 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 293 | data = nfs_readdata_alloc(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | if (!data) |
| 295 | goto out_bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | list_add(&data->pages, &list); |
| 297 | requests++; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 298 | nbytes -= len; |
| 299 | } while(nbytes != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | atomic_set(&req->wb_complete, requests); |
| 301 | |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 302 | BUG_ON(desc->pg_lseg != NULL); |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 303 | lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, |
| 304 | req_offset(req), desc->pg_count, |
| 305 | IOMODE_READ, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | ClearPageError(page); |
| 307 | offset = 0; |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 308 | nbytes = desc->pg_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | do { |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 310 | int ret2; |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | data = list_entry(list.next, struct nfs_read_data, pages); |
| 313 | list_del_init(&data->pages); |
| 314 | |
| 315 | data->pagevec[0] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 317 | if (nbytes < rsize) |
| 318 | rsize = nbytes; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 319 | ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 320 | rsize, offset, lseg); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 321 | if (ret == 0) |
| 322 | ret = ret2; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 323 | offset += rsize; |
| 324 | nbytes -= rsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } while (nbytes != 0); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 326 | put_lseg(lseg); |
Fred Isaman | 36fe432 | 2011-03-03 15:13:49 +0000 | [diff] [blame] | 327 | desc->pg_lseg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 329 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | out_bad: |
| 332 | while (!list_empty(&list)) { |
| 333 | data = list_entry(list.next, struct nfs_read_data, pages); |
| 334 | list_del(&data->pages); |
| 335 | nfs_readdata_free(data); |
| 336 | } |
| 337 | SetPageError(page); |
| 338 | nfs_readpage_release(req); |
| 339 | return -ENOMEM; |
| 340 | } |
| 341 | |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 342 | static int nfs_pagein_one(struct nfs_pageio_descriptor *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | struct nfs_page *req; |
| 345 | struct page **pages; |
| 346 | struct nfs_read_data *data; |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 347 | struct list_head *head = &desc->pg_list; |
| 348 | struct pnfs_layout_segment *lseg = desc->pg_lseg; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 349 | int ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 351 | data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base, |
| 352 | desc->pg_count)); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 353 | if (!data) { |
| 354 | nfs_async_read_error(head); |
| 355 | goto out; |
| 356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | pages = data->pagevec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | while (!list_empty(head)) { |
| 360 | req = nfs_list_entry(head->next); |
| 361 | nfs_list_remove_request(req); |
| 362 | nfs_list_add_request(req, &data->pages); |
| 363 | ClearPageError(req->wb_page); |
| 364 | *pages++ = req->wb_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | req = nfs_list_entry(data->pages.next); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 367 | if ((!lseg) && list_is_singular(&data->pages)) |
Benny Halevy | fb3296e | 2011-05-22 19:47:26 +0300 | [diff] [blame] | 368 | lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, |
| 369 | req_offset(req), desc->pg_count, |
| 370 | IOMODE_READ, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 372 | ret = nfs_read_rpcsetup(req, data, &nfs_read_full_ops, desc->pg_count, |
| 373 | 0, lseg); |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 374 | out: |
| 375 | put_lseg(lseg); |
Fred Isaman | 36fe432 | 2011-03-03 15:13:49 +0000 | [diff] [blame] | 376 | desc->pg_lseg = NULL; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 377 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame^] | 380 | int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) |
| 381 | { |
| 382 | if (desc->pg_bsize < PAGE_CACHE_SIZE) |
| 383 | return nfs_pagein_multi(desc); |
| 384 | return nfs_pagein_one(desc); |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(nfs_generic_pg_readpages); |
| 387 | |
| 388 | |
| 389 | static const struct nfs_pageio_ops nfs_pageio_read_ops = { |
| 390 | .pg_test = nfs_generic_pg_test, |
| 391 | .pg_doio = nfs_generic_pg_readpages, |
| 392 | }; |
| 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | /* |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 395 | * This is the callback from RPC telling us whether a reply was |
| 396 | * received or some error occurred (timeout or socket shutdown). |
| 397 | */ |
| 398 | int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) |
| 399 | { |
| 400 | int status; |
| 401 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 402 | dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid, |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 403 | task->tk_status); |
| 404 | |
| 405 | status = NFS_PROTO(data->inode)->read_done(task, data); |
| 406 | if (status != 0) |
| 407 | return status; |
| 408 | |
| 409 | nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count); |
| 410 | |
| 411 | if (task->tk_status == -ESTALE) { |
Benny Halevy | 3a10c30 | 2008-01-23 08:58:59 +0200 | [diff] [blame] | 412 | set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags); |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 413 | nfs_mark_for_revalidate(data->inode); |
| 414 | } |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 418 | static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 419 | { |
| 420 | struct nfs_readargs *argp = &data->args; |
| 421 | struct nfs_readres *resp = &data->res; |
| 422 | |
| 423 | if (resp->eof || resp->count == argp->count) |
Trond Myklebust | d61e612 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 424 | return; |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 425 | |
| 426 | /* This is a short read! */ |
| 427 | nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); |
| 428 | /* Has the server at least made some progress? */ |
| 429 | if (resp->count == 0) |
Trond Myklebust | d61e612 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 430 | return; |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 431 | |
| 432 | /* Yes, so retry the read at the end of the data */ |
Andy Adamson | cbdabc7 | 2011-03-01 01:34:20 +0000 | [diff] [blame] | 433 | data->mds_offset += resp->count; |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 434 | argp->offset += resp->count; |
| 435 | argp->pgbase += resp->count; |
| 436 | argp->count -= resp->count; |
Trond Myklebust | 0110ee1 | 2009-12-07 09:00:24 -0500 | [diff] [blame] | 437 | nfs_restart_rpc(task, NFS_SERVER(data->inode)->nfs_client); |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | * Handle a read reply that fills part of a page. |
| 442 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 443 | static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 445 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 447 | if (nfs_readpage_result(task, data) != 0) |
| 448 | return; |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 449 | if (task->tk_status < 0) |
| 450 | return; |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 451 | |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 452 | nfs_readpage_truncate_uninitialised_page(data); |
| 453 | nfs_readpage_retry(task, data); |
| 454 | } |
| 455 | |
| 456 | static void nfs_readpage_release_partial(void *calldata) |
| 457 | { |
| 458 | struct nfs_read_data *data = calldata; |
| 459 | struct nfs_page *req = data->req; |
| 460 | struct page *page = req->wb_page; |
| 461 | int status = data->task.tk_status; |
| 462 | |
| 463 | if (status < 0) |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 464 | SetPageError(page); |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (atomic_dec_and_test(&req->wb_complete)) { |
| 467 | if (!PageError(page)) |
| 468 | SetPageUptodate(page); |
| 469 | nfs_readpage_release(req); |
| 470 | } |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 471 | nfs_readdata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Andy Adamson | f11c88a | 2009-04-01 09:22:25 -0400 | [diff] [blame] | 474 | #if defined(CONFIG_NFS_V4_1) |
| 475 | void nfs_read_prepare(struct rpc_task *task, void *calldata) |
| 476 | { |
| 477 | struct nfs_read_data *data = calldata; |
| 478 | |
Trond Myklebust | 035168ab | 2010-06-16 09:52:26 -0400 | [diff] [blame] | 479 | if (nfs4_setup_sequence(NFS_SERVER(data->inode), |
Andy Adamson | f11c88a | 2009-04-01 09:22:25 -0400 | [diff] [blame] | 480 | &data->args.seq_args, &data->res.seq_res, |
| 481 | 0, task)) |
| 482 | return; |
| 483 | rpc_call_start(task); |
| 484 | } |
| 485 | #endif /* CONFIG_NFS_V4_1 */ |
| 486 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 487 | static const struct rpc_call_ops nfs_read_partial_ops = { |
Andy Adamson | f11c88a | 2009-04-01 09:22:25 -0400 | [diff] [blame] | 488 | #if defined(CONFIG_NFS_V4_1) |
| 489 | .rpc_call_prepare = nfs_read_prepare, |
| 490 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 491 | .rpc_call_done = nfs_readpage_result_partial, |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 492 | .rpc_release = nfs_readpage_release_partial, |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 493 | }; |
| 494 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 495 | static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) |
| 496 | { |
| 497 | unsigned int count = data->res.count; |
| 498 | unsigned int base = data->args.pgbase; |
| 499 | struct page **pages; |
| 500 | |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 501 | if (data->res.eof) |
| 502 | count = data->args.count; |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 503 | if (unlikely(count == 0)) |
| 504 | return; |
| 505 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 506 | base &= ~PAGE_CACHE_MASK; |
| 507 | count += base; |
| 508 | for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) |
| 509 | SetPageUptodate(*pages); |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 510 | if (count == 0) |
| 511 | return; |
| 512 | /* Was this a short read? */ |
| 513 | if (data->res.eof || data->res.count == data->args.count) |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 514 | SetPageUptodate(*pages); |
| 515 | } |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /* |
| 518 | * This is the callback from RPC telling us whether a reply was |
| 519 | * received or some error occurred (timeout or socket shutdown). |
| 520 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 521 | static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 523 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 525 | if (nfs_readpage_result(task, data) != 0) |
| 526 | return; |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 527 | if (task->tk_status < 0) |
| 528 | return; |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 529 | /* |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 530 | * Note: nfs_readpage_retry may change the values of |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 531 | * data->args. In the multi-page case, we therefore need |
Trond Myklebust | 0b67130 | 2006-11-14 16:12:23 -0500 | [diff] [blame] | 532 | * to ensure that we call nfs_readpage_set_pages_uptodate() |
| 533 | * first. |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 534 | */ |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 535 | nfs_readpage_truncate_uninitialised_page(data); |
| 536 | nfs_readpage_set_pages_uptodate(data); |
| 537 | nfs_readpage_retry(task, data); |
| 538 | } |
| 539 | |
| 540 | static void nfs_readpage_release_full(void *calldata) |
| 541 | { |
| 542 | struct nfs_read_data *data = calldata; |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | while (!list_empty(&data->pages)) { |
| 545 | struct nfs_page *req = nfs_list_entry(data->pages.next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 547 | nfs_list_remove_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | nfs_readpage_release(req); |
| 549 | } |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 550 | nfs_readdata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 553 | static const struct rpc_call_ops nfs_read_full_ops = { |
Andy Adamson | f11c88a | 2009-04-01 09:22:25 -0400 | [diff] [blame] | 554 | #if defined(CONFIG_NFS_V4_1) |
| 555 | .rpc_call_prepare = nfs_read_prepare, |
| 556 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 557 | .rpc_call_done = nfs_readpage_result_full, |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 558 | .rpc_release = nfs_readpage_release_full, |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 559 | }; |
| 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | * Read a page over NFS. |
| 563 | * We read the page synchronously in the following case: |
| 564 | * - The error flag is set for this page. This happens only when a |
| 565 | * previous async read operation failed. |
| 566 | */ |
| 567 | int nfs_readpage(struct file *file, struct page *page) |
| 568 | { |
| 569 | struct nfs_open_context *ctx; |
| 570 | struct inode *inode = page->mapping->host; |
| 571 | int error; |
| 572 | |
| 573 | dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", |
| 574 | page, PAGE_CACHE_SIZE, page->index); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 575 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); |
| 576 | nfs_add_stats(inode, NFSIOS_READPAGES, 1); |
| 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | /* |
| 579 | * Try to flush any pending writes to the file.. |
| 580 | * |
| 581 | * NOTE! Because we own the page lock, there cannot |
| 582 | * be any new pending writes generated at this point |
| 583 | * for this page (other pages can be written to). |
| 584 | */ |
| 585 | error = nfs_wb_page(inode, page); |
| 586 | if (error) |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 587 | goto out_unlock; |
| 588 | if (PageUptodate(page)) |
| 589 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 591 | error = -ESTALE; |
| 592 | if (NFS_STALE(inode)) |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 593 | goto out_unlock; |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (file == NULL) { |
Trond Myklebust | cf1308f | 2006-11-19 16:44:52 -0500 | [diff] [blame] | 596 | error = -EBADF; |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 597 | ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | if (ctx == NULL) |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 599 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } else |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 601 | ctx = get_nfs_open_context(nfs_file_open_context(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
David Howells | 9a9fc1c | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 603 | if (!IS_SYNC(inode)) { |
| 604 | error = nfs_readpage_from_fscache(ctx, inode, page); |
| 605 | if (error == 0) |
| 606 | goto out; |
| 607 | } |
| 608 | |
Trond Myklebust | 8e0969f | 2006-12-13 15:23:44 -0500 | [diff] [blame] | 609 | error = nfs_readpage_async(ctx, inode, page); |
| 610 | |
David Howells | 9a9fc1c | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 611 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | put_nfs_open_context(ctx); |
| 613 | return error; |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 614 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | unlock_page(page); |
| 616 | return error; |
| 617 | } |
| 618 | |
| 619 | struct nfs_readdesc { |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 620 | struct nfs_pageio_descriptor *pgio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | struct nfs_open_context *ctx; |
| 622 | }; |
| 623 | |
| 624 | static int |
| 625 | readpage_async_filler(void *data, struct page *page) |
| 626 | { |
| 627 | struct nfs_readdesc *desc = (struct nfs_readdesc *)data; |
| 628 | struct inode *inode = page->mapping->host; |
| 629 | struct nfs_page *new; |
| 630 | unsigned int len; |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 631 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 633 | len = nfs_page_length(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if (len == 0) |
| 635 | return nfs_return_empty_page(page); |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | new = nfs_create_request(desc->ctx, inode, page, 0, len); |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 638 | if (IS_ERR(new)) |
| 639 | goto out_error; |
| 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | if (len < PAGE_CACHE_SIZE) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 642 | zero_user_segment(page, len, PAGE_CACHE_SIZE); |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 643 | if (!nfs_pageio_add_request(desc->pgio, new)) { |
| 644 | error = desc->pgio->pg_error; |
| 645 | goto out_unlock; |
| 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return 0; |
Trond Myklebust | de05a0c | 2007-05-20 13:05:05 -0400 | [diff] [blame] | 648 | out_error: |
| 649 | error = PTR_ERR(new); |
| 650 | SetPageError(page); |
| 651 | out_unlock: |
| 652 | unlock_page(page); |
| 653 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | int nfs_readpages(struct file *filp, struct address_space *mapping, |
| 657 | struct list_head *pages, unsigned nr_pages) |
| 658 | { |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 659 | struct nfs_pageio_descriptor pgio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | struct nfs_readdesc desc = { |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 661 | .pgio = &pgio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | }; |
| 663 | struct inode *inode = mapping->host; |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 664 | unsigned long npages; |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 665 | int ret = -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", |
| 668 | inode->i_sb->s_id, |
| 669 | (long long)NFS_FILEID(inode), |
| 670 | nr_pages); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 671 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 673 | if (NFS_STALE(inode)) |
| 674 | goto out; |
| 675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (filp == NULL) { |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 677 | desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (desc.ctx == NULL) |
| 679 | return -EBADF; |
| 680 | } else |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 681 | desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); |
David Howells | 9a9fc1c | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 682 | |
| 683 | /* attempt to read as many of the pages as possible from the cache |
| 684 | * - this returns -ENOBUFS immediately if the cookie is negative |
| 685 | */ |
| 686 | ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, |
| 687 | pages, &nr_pages); |
| 688 | if (ret == 0) |
| 689 | goto read_complete; /* all pages were read */ |
| 690 | |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame^] | 691 | nfs_pageio_init_read(&pgio, inode); |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 694 | |
| 695 | nfs_pageio_complete(&pgio); |
| 696 | npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 697 | nfs_add_stats(inode, NFSIOS_READPAGES, npages); |
David Howells | 9a9fc1c | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 698 | read_complete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | put_nfs_open_context(desc.ctx); |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 700 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return ret; |
| 702 | } |
| 703 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 704 | int __init nfs_init_readpagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
| 706 | nfs_rdata_cachep = kmem_cache_create("nfs_read_data", |
| 707 | sizeof(struct nfs_read_data), |
| 708 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 709 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | if (nfs_rdata_cachep == NULL) |
| 711 | return -ENOMEM; |
| 712 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 713 | nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, |
| 714 | nfs_rdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (nfs_rdata_mempool == NULL) |
| 716 | return -ENOMEM; |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 721 | void nfs_destroy_readpagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
| 723 | mempool_destroy(nfs_rdata_mempool); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 724 | kmem_cache_destroy(nfs_rdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |