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