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 |
| 8 | * |
| 9 | * We do an ugly hack here in order to return proper error codes to the |
| 10 | * user program when a read request failed: since generic_file_read |
| 11 | * only checks the return value of inode->i_op->readpage() which is always 0 |
| 12 | * for async RPC, we set the error bit of the page to 1 when an error occurs, |
| 13 | * and make nfs_readpage transmit requests synchronously when encountering this. |
| 14 | * This is only a small problem, though, since we now retry all operations |
| 15 | * within the RPC code when root squashing is suspected. |
| 16 | */ |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/time.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/fcntl.h> |
| 22 | #include <linux/stat.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/sunrpc/clnt.h> |
| 27 | #include <linux/nfs_fs.h> |
| 28 | #include <linux/nfs_page.h> |
| 29 | #include <linux/smp_lock.h> |
| 30 | |
| 31 | #include <asm/system.h> |
| 32 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 33 | #include "iostat.h" |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 36 | |
| 37 | static int nfs_pagein_one(struct list_head *, struct inode *); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 38 | static const struct rpc_call_ops nfs_read_partial_ops; |
| 39 | static const struct rpc_call_ops nfs_read_full_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | static kmem_cache_t *nfs_rdata_cachep; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 42 | static mempool_t *nfs_rdata_mempool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #define MIN_POOL_READ (32) |
| 45 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 46 | struct nfs_read_data *nfs_readdata_alloc(size_t len) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 47 | { |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 48 | unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 49 | struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS); |
| 50 | |
| 51 | if (p) { |
| 52 | memset(p, 0, sizeof(*p)); |
| 53 | INIT_LIST_HEAD(&p->pages); |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 54 | p->npages = pagecount; |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 55 | if (pagecount <= ARRAY_SIZE(p->page_array)) |
| 56 | p->pagevec = p->page_array; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 57 | else { |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 58 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); |
| 59 | if (!p->pagevec) { |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 60 | mempool_free(p, nfs_rdata_mempool); |
| 61 | p = NULL; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return p; |
| 66 | } |
| 67 | |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame^] | 68 | static void nfs_readdata_rcu_free(struct rcu_head *head) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 69 | { |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame^] | 70 | struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu); |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 71 | if (p && (p->pagevec != &p->page_array[0])) |
| 72 | kfree(p->pagevec); |
| 73 | mempool_free(p, nfs_rdata_mempool); |
| 74 | } |
| 75 | |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame^] | 76 | static void nfs_readdata_free(struct nfs_read_data *rdata) |
| 77 | { |
| 78 | call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free); |
| 79 | } |
| 80 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 81 | void nfs_readdata_release(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | nfs_readdata_free(data); |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | unsigned int nfs_page_length(struct inode *inode, struct page *page) |
| 88 | { |
| 89 | loff_t i_size = i_size_read(inode); |
| 90 | unsigned long idx; |
| 91 | |
| 92 | if (i_size <= 0) |
| 93 | return 0; |
| 94 | idx = (i_size - 1) >> PAGE_CACHE_SHIFT; |
| 95 | if (page->index > idx) |
| 96 | return 0; |
| 97 | if (page->index != idx) |
| 98 | return PAGE_CACHE_SIZE; |
| 99 | return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1)); |
| 100 | } |
| 101 | |
| 102 | static |
| 103 | int nfs_return_empty_page(struct page *page) |
| 104 | { |
| 105 | memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE); |
| 106 | SetPageUptodate(page); |
| 107 | unlock_page(page); |
| 108 | return 0; |
| 109 | } |
| 110 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 111 | static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) |
| 112 | { |
| 113 | unsigned int remainder = data->args.count - data->res.count; |
| 114 | unsigned int base = data->args.pgbase + data->res.count; |
| 115 | unsigned int pglen; |
| 116 | struct page **pages; |
| 117 | |
| 118 | if (data->res.eof == 0 || remainder == 0) |
| 119 | return; |
| 120 | /* |
| 121 | * Note: "remainder" can never be negative, since we check for |
| 122 | * this in the XDR code. |
| 123 | */ |
| 124 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 125 | base &= ~PAGE_CACHE_MASK; |
| 126 | pglen = PAGE_CACHE_SIZE - base; |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 127 | for (;;) { |
| 128 | if (remainder <= pglen) { |
| 129 | memclear_highpage_flush(*pages, base, remainder); |
| 130 | break; |
| 131 | } |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 132 | memclear_highpage_flush(*pages, base, pglen); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 133 | pages++; |
| 134 | remainder -= pglen; |
| 135 | pglen = PAGE_CACHE_SIZE; |
| 136 | base = 0; |
| 137 | } |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* |
| 141 | * Read a page synchronously. |
| 142 | */ |
| 143 | static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode, |
| 144 | struct page *page) |
| 145 | { |
| 146 | unsigned int rsize = NFS_SERVER(inode)->rsize; |
| 147 | unsigned int count = PAGE_CACHE_SIZE; |
| 148 | int result; |
| 149 | struct nfs_read_data *rdata; |
| 150 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 151 | rdata = nfs_readdata_alloc(count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (!rdata) |
| 153 | return -ENOMEM; |
| 154 | |
| 155 | memset(rdata, 0, sizeof(*rdata)); |
| 156 | rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); |
| 157 | rdata->cred = ctx->cred; |
| 158 | rdata->inode = inode; |
| 159 | INIT_LIST_HEAD(&rdata->pages); |
| 160 | rdata->args.fh = NFS_FH(inode); |
| 161 | rdata->args.context = ctx; |
| 162 | rdata->args.pages = &page; |
| 163 | rdata->args.pgbase = 0UL; |
| 164 | rdata->args.count = rsize; |
| 165 | rdata->res.fattr = &rdata->fattr; |
| 166 | |
| 167 | dprintk("NFS: nfs_readpage_sync(%p)\n", page); |
| 168 | |
| 169 | /* |
| 170 | * This works now because the socket layer never tries to DMA |
| 171 | * into this buffer directly. |
| 172 | */ |
| 173 | do { |
| 174 | if (count < rsize) |
| 175 | rdata->args.count = count; |
| 176 | rdata->res.count = rdata->args.count; |
| 177 | rdata->args.offset = page_offset(page) + rdata->args.pgbase; |
| 178 | |
| 179 | dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n", |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 180 | NFS_SERVER(inode)->nfs_client->cl_hostname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | inode->i_sb->s_id, |
| 182 | (long long)NFS_FILEID(inode), |
| 183 | (unsigned long long)rdata->args.pgbase, |
| 184 | rdata->args.count); |
| 185 | |
| 186 | lock_kernel(); |
| 187 | result = NFS_PROTO(inode)->read(rdata); |
| 188 | unlock_kernel(); |
| 189 | |
| 190 | /* |
| 191 | * Even if we had a partial success we can't mark the page |
| 192 | * cache valid. |
| 193 | */ |
| 194 | if (result < 0) { |
| 195 | if (result == -EISDIR) |
| 196 | result = -EINVAL; |
| 197 | goto io_error; |
| 198 | } |
| 199 | count -= result; |
| 200 | rdata->args.pgbase += result; |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 201 | nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result); |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /* Note: result == 0 should only happen if we're caching |
| 204 | * a write that extends the file and punches a hole. |
| 205 | */ |
| 206 | if (rdata->res.eof != 0 || result == 0) |
| 207 | break; |
| 208 | } while (count); |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 209 | spin_lock(&inode->i_lock); |
Chuck Lever | 5529680 | 2005-08-18 11:24:09 -0700 | [diff] [blame] | 210 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME; |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 211 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Trond Myklebust | 7a52411 | 2006-09-15 16:03:45 -0400 | [diff] [blame] | 213 | if (rdata->res.eof || rdata->res.count == rdata->args.count) { |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 214 | SetPageUptodate(page); |
Trond Myklebust | 7a52411 | 2006-09-15 16:03:45 -0400 | [diff] [blame] | 215 | if (rdata->res.eof && count != 0) |
| 216 | memclear_highpage_flush(page, rdata->args.pgbase, count); |
| 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | result = 0; |
| 219 | |
| 220 | io_error: |
| 221 | unlock_page(page); |
| 222 | nfs_readdata_free(rdata); |
| 223 | return result; |
| 224 | } |
| 225 | |
| 226 | static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, |
| 227 | struct page *page) |
| 228 | { |
| 229 | LIST_HEAD(one_request); |
| 230 | struct nfs_page *new; |
| 231 | unsigned int len; |
| 232 | |
| 233 | len = nfs_page_length(inode, page); |
| 234 | if (len == 0) |
| 235 | return nfs_return_empty_page(page); |
| 236 | new = nfs_create_request(ctx, inode, page, 0, len); |
| 237 | if (IS_ERR(new)) { |
| 238 | unlock_page(page); |
| 239 | return PTR_ERR(new); |
| 240 | } |
| 241 | if (len < PAGE_CACHE_SIZE) |
| 242 | memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | nfs_list_add_request(new, &one_request); |
| 245 | nfs_pagein_one(&one_request, inode); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static void nfs_readpage_release(struct nfs_page *req) |
| 250 | { |
| 251 | unlock_page(req->wb_page); |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", |
| 254 | req->wb_context->dentry->d_inode->i_sb->s_id, |
| 255 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), |
| 256 | req->wb_bytes, |
| 257 | (long long)req_offset(req)); |
Nick Wilson | 10d2c46 | 2005-09-22 21:44:28 -0700 | [diff] [blame] | 258 | nfs_clear_request(req); |
| 259 | nfs_release_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Set up the NFS read request struct |
| 264 | */ |
| 265 | static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 266 | const struct rpc_call_ops *call_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | unsigned int count, unsigned int offset) |
| 268 | { |
| 269 | struct inode *inode; |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 270 | int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | data->req = req; |
| 273 | data->inode = inode = req->wb_context->dentry->d_inode; |
| 274 | data->cred = req->wb_context->cred; |
| 275 | |
| 276 | data->args.fh = NFS_FH(inode); |
| 277 | data->args.offset = req_offset(req) + offset; |
| 278 | data->args.pgbase = req->wb_pgbase + offset; |
| 279 | data->args.pages = data->pagevec; |
| 280 | data->args.count = count; |
| 281 | data->args.context = req->wb_context; |
| 282 | |
| 283 | data->res.fattr = &data->fattr; |
| 284 | data->res.count = count; |
| 285 | data->res.eof = 0; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 286 | nfs_fattr_init(&data->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 288 | /* Set up the initial task struct. */ |
| 289 | flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); |
| 290 | rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | NFS_PROTO(inode)->read_setup(data); |
| 292 | |
| 293 | data->task.tk_cookie = (unsigned long)inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", |
| 296 | data->task.tk_pid, |
| 297 | inode->i_sb->s_id, |
| 298 | (long long)NFS_FILEID(inode), |
| 299 | count, |
| 300 | (unsigned long long)data->args.offset); |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | nfs_async_read_error(struct list_head *head) |
| 305 | { |
| 306 | struct nfs_page *req; |
| 307 | |
| 308 | while (!list_empty(head)) { |
| 309 | req = nfs_list_entry(head->next); |
| 310 | nfs_list_remove_request(req); |
| 311 | SetPageError(req->wb_page); |
| 312 | nfs_readpage_release(req); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Start an async read operation |
| 318 | */ |
| 319 | static void nfs_execute_read(struct nfs_read_data *data) |
| 320 | { |
| 321 | struct rpc_clnt *clnt = NFS_CLIENT(data->inode); |
| 322 | sigset_t oldset; |
| 323 | |
| 324 | rpc_clnt_sigmask(clnt, &oldset); |
| 325 | lock_kernel(); |
| 326 | rpc_execute(&data->task); |
| 327 | unlock_kernel(); |
| 328 | rpc_clnt_sigunmask(clnt, &oldset); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Generate multiple requests to fill a single page. |
| 333 | * |
| 334 | * We optimize to reduce the number of read operations on the wire. If we |
| 335 | * detect that we're reading a page, or an area of a page, that is past the |
| 336 | * end of file, we do not generate NFS read operations but just clear the |
| 337 | * parts of the page that would have come back zero from the server anyway. |
| 338 | * |
| 339 | * We rely on the cached value of i_size to make this determination; another |
| 340 | * client can fill pages on the server past our cached end-of-file, but we |
| 341 | * won't see the new data until our attribute cache is updated. This is more |
| 342 | * or less conventional NFS client behavior. |
| 343 | */ |
| 344 | static int nfs_pagein_multi(struct list_head *head, struct inode *inode) |
| 345 | { |
| 346 | struct nfs_page *req = nfs_list_entry(head->next); |
| 347 | struct page *page = req->wb_page; |
| 348 | struct nfs_read_data *data; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 349 | size_t rsize = NFS_SERVER(inode)->rsize, nbytes; |
| 350 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | int requests = 0; |
| 352 | LIST_HEAD(list); |
| 353 | |
| 354 | nfs_list_remove_request(req); |
| 355 | |
| 356 | nbytes = req->wb_bytes; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 357 | do { |
| 358 | size_t len = min(nbytes,rsize); |
| 359 | |
| 360 | data = nfs_readdata_alloc(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (!data) |
| 362 | goto out_bad; |
| 363 | INIT_LIST_HEAD(&data->pages); |
| 364 | list_add(&data->pages, &list); |
| 365 | requests++; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 366 | nbytes -= len; |
| 367 | } while(nbytes != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | atomic_set(&req->wb_complete, requests); |
| 369 | |
| 370 | ClearPageError(page); |
| 371 | offset = 0; |
| 372 | nbytes = req->wb_bytes; |
| 373 | do { |
| 374 | data = list_entry(list.next, struct nfs_read_data, pages); |
| 375 | list_del_init(&data->pages); |
| 376 | |
| 377 | data->pagevec[0] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | if (nbytes > rsize) { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 380 | nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, |
| 381 | rsize, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | offset += rsize; |
| 383 | nbytes -= rsize; |
| 384 | } else { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 385 | nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, |
| 386 | nbytes, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | nbytes = 0; |
| 388 | } |
| 389 | nfs_execute_read(data); |
| 390 | } while (nbytes != 0); |
| 391 | |
| 392 | return 0; |
| 393 | |
| 394 | out_bad: |
| 395 | while (!list_empty(&list)) { |
| 396 | data = list_entry(list.next, struct nfs_read_data, pages); |
| 397 | list_del(&data->pages); |
| 398 | nfs_readdata_free(data); |
| 399 | } |
| 400 | SetPageError(page); |
| 401 | nfs_readpage_release(req); |
| 402 | return -ENOMEM; |
| 403 | } |
| 404 | |
| 405 | static int nfs_pagein_one(struct list_head *head, struct inode *inode) |
| 406 | { |
| 407 | struct nfs_page *req; |
| 408 | struct page **pages; |
| 409 | struct nfs_read_data *data; |
| 410 | unsigned int count; |
| 411 | |
| 412 | if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) |
| 413 | return nfs_pagein_multi(head, inode); |
| 414 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 415 | data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if (!data) |
| 417 | goto out_bad; |
| 418 | |
| 419 | INIT_LIST_HEAD(&data->pages); |
| 420 | pages = data->pagevec; |
| 421 | count = 0; |
| 422 | while (!list_empty(head)) { |
| 423 | req = nfs_list_entry(head->next); |
| 424 | nfs_list_remove_request(req); |
| 425 | nfs_list_add_request(req, &data->pages); |
| 426 | ClearPageError(req->wb_page); |
| 427 | *pages++ = req->wb_page; |
| 428 | count += req->wb_bytes; |
| 429 | } |
| 430 | req = nfs_list_entry(data->pages.next); |
| 431 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 432 | nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | nfs_execute_read(data); |
| 435 | return 0; |
| 436 | out_bad: |
| 437 | nfs_async_read_error(head); |
| 438 | return -ENOMEM; |
| 439 | } |
| 440 | |
| 441 | static int |
| 442 | nfs_pagein_list(struct list_head *head, int rpages) |
| 443 | { |
| 444 | LIST_HEAD(one_request); |
| 445 | struct nfs_page *req; |
| 446 | int error = 0; |
| 447 | unsigned int pages = 0; |
| 448 | |
| 449 | while (!list_empty(head)) { |
| 450 | pages += nfs_coalesce_requests(head, &one_request, rpages); |
| 451 | req = nfs_list_entry(one_request.next); |
| 452 | error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode); |
| 453 | if (error < 0) |
| 454 | break; |
| 455 | } |
| 456 | if (error >= 0) |
| 457 | return pages; |
| 458 | |
| 459 | nfs_async_read_error(head); |
| 460 | return error; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Handle a read reply that fills part of a page. |
| 465 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 466 | static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 468 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | struct nfs_page *req = data->req; |
| 470 | struct page *page = req->wb_page; |
| 471 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 472 | if (likely(task->tk_status >= 0)) |
| 473 | nfs_readpage_truncate_uninitialised_page(data); |
| 474 | else |
| 475 | SetPageError(page); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 476 | if (nfs_readpage_result(task, data) != 0) |
| 477 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (atomic_dec_and_test(&req->wb_complete)) { |
| 479 | if (!PageError(page)) |
| 480 | SetPageUptodate(page); |
| 481 | nfs_readpage_release(req); |
| 482 | } |
| 483 | } |
| 484 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 485 | static const struct rpc_call_ops nfs_read_partial_ops = { |
| 486 | .rpc_call_done = nfs_readpage_result_partial, |
| 487 | .rpc_release = nfs_readdata_release, |
| 488 | }; |
| 489 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 490 | static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) |
| 491 | { |
| 492 | unsigned int count = data->res.count; |
| 493 | unsigned int base = data->args.pgbase; |
| 494 | struct page **pages; |
| 495 | |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 496 | if (data->res.eof) |
| 497 | count = data->args.count; |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 498 | if (unlikely(count == 0)) |
| 499 | return; |
| 500 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 501 | base &= ~PAGE_CACHE_MASK; |
| 502 | count += base; |
| 503 | for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) |
| 504 | SetPageUptodate(*pages); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 505 | if (count != 0) |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 506 | SetPageUptodate(*pages); |
| 507 | } |
| 508 | |
| 509 | static void nfs_readpage_set_pages_error(struct nfs_read_data *data) |
| 510 | { |
| 511 | unsigned int count = data->args.count; |
| 512 | unsigned int base = data->args.pgbase; |
| 513 | struct page **pages; |
| 514 | |
| 515 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 516 | base &= ~PAGE_CACHE_MASK; |
| 517 | count += base; |
| 518 | for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) |
| 519 | SetPageError(*pages); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 520 | if (count != 0) |
| 521 | SetPageError(*pages); |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* |
| 525 | * This is the callback from RPC telling us whether a reply was |
| 526 | * received or some error occurred (timeout or socket shutdown). |
| 527 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 528 | static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 530 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 532 | /* |
| 533 | * Note: nfs_readpage_result may change the values of |
| 534 | * data->args. In the multi-page case, we therefore need |
| 535 | * to ensure that we call the next nfs_readpage_set_page_uptodate() |
| 536 | * first in the multi-page case. |
| 537 | */ |
| 538 | if (likely(task->tk_status >= 0)) { |
| 539 | nfs_readpage_truncate_uninitialised_page(data); |
| 540 | nfs_readpage_set_pages_uptodate(data); |
| 541 | } else |
| 542 | nfs_readpage_set_pages_error(data); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 543 | if (nfs_readpage_result(task, data) != 0) |
| 544 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | while (!list_empty(&data->pages)) { |
| 546 | struct nfs_page *req = nfs_list_entry(data->pages.next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 548 | nfs_list_remove_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | nfs_readpage_release(req); |
| 550 | } |
| 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 = { |
| 554 | .rpc_call_done = nfs_readpage_result_full, |
| 555 | .rpc_release = nfs_readdata_release, |
| 556 | }; |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* |
| 559 | * This is the callback from RPC telling us whether a reply was |
| 560 | * received or some error occurred (timeout or socket shutdown). |
| 561 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 562 | int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | struct nfs_readargs *argp = &data->args; |
| 565 | struct nfs_readres *resp = &data->res; |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 566 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
| 568 | dprintk("NFS: %4d nfs_readpage_result, (status %d)\n", |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 569 | task->tk_pid, task->tk_status); |
| 570 | |
| 571 | status = NFS_PROTO(data->inode)->read_done(task, data); |
| 572 | if (status != 0) |
| 573 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 575 | nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count); |
| 576 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 577 | if (task->tk_status < 0) { |
| 578 | if (task->tk_status == -ESTALE) { |
| 579 | set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode)); |
| 580 | nfs_mark_for_revalidate(data->inode); |
| 581 | } |
| 582 | } else if (resp->count < argp->count && !resp->eof) { |
| 583 | /* This is a short read! */ |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 584 | nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | /* Has the server at least made some progress? */ |
| 586 | if (resp->count != 0) { |
| 587 | /* Yes, so retry the read at the end of the data */ |
| 588 | argp->offset += resp->count; |
| 589 | argp->pgbase += resp->count; |
| 590 | argp->count -= resp->count; |
| 591 | rpc_restart_call(task); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 592 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | task->tk_status = -EIO; |
| 595 | } |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 596 | spin_lock(&data->inode->i_lock); |
Chuck Lever | 5529680 | 2005-08-18 11:24:09 -0700 | [diff] [blame] | 597 | NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME; |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 598 | spin_unlock(&data->inode->i_lock); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 599 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Read a page over NFS. |
| 604 | * We read the page synchronously in the following case: |
| 605 | * - The error flag is set for this page. This happens only when a |
| 606 | * previous async read operation failed. |
| 607 | */ |
| 608 | int nfs_readpage(struct file *file, struct page *page) |
| 609 | { |
| 610 | struct nfs_open_context *ctx; |
| 611 | struct inode *inode = page->mapping->host; |
| 612 | int error; |
| 613 | |
| 614 | dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", |
| 615 | page, PAGE_CACHE_SIZE, page->index); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 616 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); |
| 617 | nfs_add_stats(inode, NFSIOS_READPAGES, 1); |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | /* |
| 620 | * Try to flush any pending writes to the file.. |
| 621 | * |
| 622 | * NOTE! Because we own the page lock, there cannot |
| 623 | * be any new pending writes generated at this point |
| 624 | * for this page (other pages can be written to). |
| 625 | */ |
| 626 | error = nfs_wb_page(inode, page); |
| 627 | if (error) |
| 628 | goto out_error; |
| 629 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 630 | error = -ESTALE; |
| 631 | if (NFS_STALE(inode)) |
| 632 | goto out_error; |
| 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if (file == NULL) { |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 635 | ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | if (ctx == NULL) |
| 637 | return -EBADF; |
| 638 | } else |
| 639 | ctx = get_nfs_open_context((struct nfs_open_context *) |
| 640 | file->private_data); |
| 641 | if (!IS_SYNC(inode)) { |
| 642 | error = nfs_readpage_async(ctx, inode, page); |
| 643 | goto out; |
| 644 | } |
| 645 | |
| 646 | error = nfs_readpage_sync(ctx, inode, page); |
| 647 | if (error < 0 && IS_SWAPFILE(inode)) |
| 648 | printk("Aiee.. nfs swap-in of page failed!\n"); |
| 649 | out: |
| 650 | put_nfs_open_context(ctx); |
| 651 | return error; |
| 652 | |
| 653 | out_error: |
| 654 | unlock_page(page); |
| 655 | return error; |
| 656 | } |
| 657 | |
| 658 | struct nfs_readdesc { |
| 659 | struct list_head *head; |
| 660 | struct nfs_open_context *ctx; |
| 661 | }; |
| 662 | |
| 663 | static int |
| 664 | readpage_async_filler(void *data, struct page *page) |
| 665 | { |
| 666 | struct nfs_readdesc *desc = (struct nfs_readdesc *)data; |
| 667 | struct inode *inode = page->mapping->host; |
| 668 | struct nfs_page *new; |
| 669 | unsigned int len; |
| 670 | |
| 671 | nfs_wb_page(inode, page); |
| 672 | len = nfs_page_length(inode, page); |
| 673 | if (len == 0) |
| 674 | return nfs_return_empty_page(page); |
| 675 | new = nfs_create_request(desc->ctx, inode, page, 0, len); |
| 676 | if (IS_ERR(new)) { |
| 677 | SetPageError(page); |
| 678 | unlock_page(page); |
| 679 | return PTR_ERR(new); |
| 680 | } |
| 681 | if (len < PAGE_CACHE_SIZE) |
| 682 | memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | nfs_list_add_request(new, desc->head); |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | int nfs_readpages(struct file *filp, struct address_space *mapping, |
| 688 | struct list_head *pages, unsigned nr_pages) |
| 689 | { |
| 690 | LIST_HEAD(head); |
| 691 | struct nfs_readdesc desc = { |
| 692 | .head = &head, |
| 693 | }; |
| 694 | struct inode *inode = mapping->host; |
| 695 | struct nfs_server *server = NFS_SERVER(inode); |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 696 | int ret = -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", |
| 699 | inode->i_sb->s_id, |
| 700 | (long long)NFS_FILEID(inode), |
| 701 | nr_pages); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 702 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 704 | if (NFS_STALE(inode)) |
| 705 | goto out; |
| 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | if (filp == NULL) { |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 708 | desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (desc.ctx == NULL) |
| 710 | return -EBADF; |
| 711 | } else |
| 712 | desc.ctx = get_nfs_open_context((struct nfs_open_context *) |
| 713 | filp->private_data); |
| 714 | ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); |
| 715 | if (!list_empty(&head)) { |
| 716 | int err = nfs_pagein_list(&head, server->rpages); |
| 717 | if (!ret) |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 718 | nfs_add_stats(inode, NFSIOS_READPAGES, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | ret = err; |
| 720 | } |
| 721 | put_nfs_open_context(desc.ctx); |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 722 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | return ret; |
| 724 | } |
| 725 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 726 | int __init nfs_init_readpagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
| 728 | nfs_rdata_cachep = kmem_cache_create("nfs_read_data", |
| 729 | sizeof(struct nfs_read_data), |
| 730 | 0, SLAB_HWCACHE_ALIGN, |
| 731 | NULL, NULL); |
| 732 | if (nfs_rdata_cachep == NULL) |
| 733 | return -ENOMEM; |
| 734 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 735 | nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, |
| 736 | nfs_rdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (nfs_rdata_mempool == NULL) |
| 738 | return -ENOMEM; |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 743 | void nfs_destroy_readpagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
| 745 | mempool_destroy(nfs_rdata_mempool); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 746 | kmem_cache_destroy(nfs_rdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |