Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/pagelist.c | |
3 | * | |
4 | * A set of helper functions for managing NFS read and write requests. | |
5 | * The main purpose of these routines is to provide support for the | |
6 | * coalescing of several requests into a single RPC call. | |
7 | * | |
8 | * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no> | |
9 | * | |
10 | */ | |
11 | ||
1da177e4 LT |
12 | #include <linux/slab.h> |
13 | #include <linux/file.h> | |
e8edc6e0 | 14 | #include <linux/sched.h> |
1da177e4 | 15 | #include <linux/sunrpc/clnt.h> |
1313e603 | 16 | #include <linux/nfs.h> |
1da177e4 LT |
17 | #include <linux/nfs3.h> |
18 | #include <linux/nfs4.h> | |
19 | #include <linux/nfs_page.h> | |
20 | #include <linux/nfs_fs.h> | |
21 | #include <linux/nfs_mount.h> | |
afeacc8c | 22 | #include <linux/export.h> |
1da177e4 | 23 | |
8d5658c9 | 24 | #include "internal.h" |
bae724ef | 25 | #include "pnfs.h" |
8d5658c9 | 26 | |
0eecb214 AS |
27 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
28 | ||
e18b890b | 29 | static struct kmem_cache *nfs_page_cachep; |
ef2c488c | 30 | static const struct rpc_call_ops nfs_pgio_common_ops; |
1da177e4 | 31 | |
00bfa30a | 32 | static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount) |
30dd374f FI |
33 | { |
34 | p->npages = pagecount; | |
35 | if (pagecount <= ARRAY_SIZE(p->page_array)) | |
36 | p->pagevec = p->page_array; | |
37 | else { | |
38 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL); | |
39 | if (!p->pagevec) | |
40 | p->npages = 0; | |
41 | } | |
42 | return p->pagevec != NULL; | |
43 | } | |
44 | ||
4db6e0b7 FI |
45 | void nfs_pgheader_init(struct nfs_pageio_descriptor *desc, |
46 | struct nfs_pgio_header *hdr, | |
47 | void (*release)(struct nfs_pgio_header *hdr)) | |
48 | { | |
a7d42ddb WAA |
49 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
50 | ||
51 | ||
52 | hdr->req = nfs_list_entry(mirror->pg_list.next); | |
4db6e0b7 FI |
53 | hdr->inode = desc->pg_inode; |
54 | hdr->cred = hdr->req->wb_context->cred; | |
55 | hdr->io_start = req_offset(hdr->req); | |
a7d42ddb | 56 | hdr->good_bytes = mirror->pg_count; |
584aa810 | 57 | hdr->dreq = desc->pg_dreq; |
f6166384 | 58 | hdr->layout_private = desc->pg_layout_private; |
4db6e0b7 | 59 | hdr->release = release; |
061ae2ed | 60 | hdr->completion_ops = desc->pg_completion_ops; |
584aa810 FI |
61 | if (hdr->completion_ops->init_hdr) |
62 | hdr->completion_ops->init_hdr(hdr); | |
a7d42ddb WAA |
63 | |
64 | hdr->pgio_mirror_idx = desc->pg_mirror_idx; | |
4db6e0b7 | 65 | } |
89d77c8f | 66 | EXPORT_SYMBOL_GPL(nfs_pgheader_init); |
4db6e0b7 FI |
67 | |
68 | void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos) | |
69 | { | |
70 | spin_lock(&hdr->lock); | |
71 | if (pos < hdr->io_start + hdr->good_bytes) { | |
72 | set_bit(NFS_IOHDR_ERROR, &hdr->flags); | |
73 | clear_bit(NFS_IOHDR_EOF, &hdr->flags); | |
74 | hdr->good_bytes = pos - hdr->io_start; | |
75 | hdr->error = error; | |
76 | } | |
77 | spin_unlock(&hdr->lock); | |
78 | } | |
79 | ||
1da177e4 LT |
80 | static inline struct nfs_page * |
81 | nfs_page_alloc(void) | |
82 | { | |
192e501b | 83 | struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO); |
72895b1a | 84 | if (p) |
1da177e4 | 85 | INIT_LIST_HEAD(&p->wb_list); |
1da177e4 LT |
86 | return p; |
87 | } | |
88 | ||
89 | static inline void | |
90 | nfs_page_free(struct nfs_page *p) | |
91 | { | |
92 | kmem_cache_free(nfs_page_cachep, p); | |
93 | } | |
94 | ||
577b4232 TM |
95 | static void |
96 | nfs_iocounter_inc(struct nfs_io_counter *c) | |
97 | { | |
98 | atomic_inc(&c->io_count); | |
99 | } | |
100 | ||
101 | static void | |
102 | nfs_iocounter_dec(struct nfs_io_counter *c) | |
103 | { | |
104 | if (atomic_dec_and_test(&c->io_count)) { | |
105 | clear_bit(NFS_IO_INPROGRESS, &c->flags); | |
4e857c58 | 106 | smp_mb__after_atomic(); |
577b4232 TM |
107 | wake_up_bit(&c->flags, NFS_IO_INPROGRESS); |
108 | } | |
109 | } | |
110 | ||
111 | static int | |
112 | __nfs_iocounter_wait(struct nfs_io_counter *c) | |
113 | { | |
114 | wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS); | |
115 | DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS); | |
116 | int ret = 0; | |
117 | ||
118 | do { | |
119 | prepare_to_wait(wq, &q.wait, TASK_KILLABLE); | |
120 | set_bit(NFS_IO_INPROGRESS, &c->flags); | |
121 | if (atomic_read(&c->io_count) == 0) | |
122 | break; | |
c1221321 | 123 | ret = nfs_wait_bit_killable(&q.key); |
92a56555 | 124 | } while (atomic_read(&c->io_count) != 0 && !ret); |
577b4232 TM |
125 | finish_wait(wq, &q.wait); |
126 | return ret; | |
127 | } | |
128 | ||
129 | /** | |
130 | * nfs_iocounter_wait - wait for i/o to complete | |
131 | * @c: nfs_io_counter to use | |
132 | * | |
133 | * returns -ERESTARTSYS if interrupted by a fatal signal. | |
134 | * Otherwise returns 0 once the io_count hits 0. | |
135 | */ | |
136 | int | |
137 | nfs_iocounter_wait(struct nfs_io_counter *c) | |
138 | { | |
139 | if (atomic_read(&c->io_count) == 0) | |
140 | return 0; | |
141 | return __nfs_iocounter_wait(c); | |
142 | } | |
143 | ||
2bfc6e56 WAA |
144 | /* |
145 | * nfs_page_group_lock - lock the head of the page group | |
146 | * @req - request in group that is to be locked | |
fd2f3a06 | 147 | * @nonblock - if true don't block waiting for lock |
2bfc6e56 WAA |
148 | * |
149 | * this lock must be held if modifying the page group list | |
e7029206 | 150 | * |
bc8a309e WAA |
151 | * return 0 on success, < 0 on error: -EDELAY if nonblocking or the |
152 | * result from wait_on_bit_lock | |
153 | * | |
154 | * NOTE: calling with nonblock=false should always have set the | |
155 | * lock bit (see fs/buffer.c and other uses of wait_on_bit_lock | |
156 | * with TASK_UNINTERRUPTIBLE), so there is no need to check the result. | |
2bfc6e56 | 157 | */ |
e7029206 | 158 | int |
fd2f3a06 | 159 | nfs_page_group_lock(struct nfs_page *req, bool nonblock) |
2bfc6e56 WAA |
160 | { |
161 | struct nfs_page *head = req->wb_head; | |
2bfc6e56 WAA |
162 | |
163 | WARN_ON_ONCE(head != head->wb_head); | |
164 | ||
bc8a309e WAA |
165 | if (!test_and_set_bit(PG_HEADLOCK, &head->wb_flags)) |
166 | return 0; | |
e7029206 | 167 | |
bc8a309e WAA |
168 | if (!nonblock) |
169 | return wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK, | |
170 | TASK_UNINTERRUPTIBLE); | |
171 | ||
172 | return -EAGAIN; | |
2bfc6e56 WAA |
173 | } |
174 | ||
7c3af975 WAA |
175 | /* |
176 | * nfs_page_group_lock_wait - wait for the lock to clear, but don't grab it | |
177 | * @req - a request in the group | |
178 | * | |
179 | * This is a blocking call to wait for the group lock to be cleared. | |
180 | */ | |
181 | void | |
182 | nfs_page_group_lock_wait(struct nfs_page *req) | |
183 | { | |
184 | struct nfs_page *head = req->wb_head; | |
185 | ||
186 | WARN_ON_ONCE(head != head->wb_head); | |
187 | ||
188 | wait_on_bit(&head->wb_flags, PG_HEADLOCK, | |
189 | TASK_UNINTERRUPTIBLE); | |
190 | } | |
191 | ||
2bfc6e56 WAA |
192 | /* |
193 | * nfs_page_group_unlock - unlock the head of the page group | |
194 | * @req - request in group that is to be unlocked | |
195 | */ | |
196 | void | |
197 | nfs_page_group_unlock(struct nfs_page *req) | |
198 | { | |
199 | struct nfs_page *head = req->wb_head; | |
200 | ||
201 | WARN_ON_ONCE(head != head->wb_head); | |
202 | ||
d1e1cda8 | 203 | smp_mb__before_atomic(); |
2bfc6e56 | 204 | clear_bit(PG_HEADLOCK, &head->wb_flags); |
d1e1cda8 | 205 | smp_mb__after_atomic(); |
2bfc6e56 WAA |
206 | wake_up_bit(&head->wb_flags, PG_HEADLOCK); |
207 | } | |
208 | ||
209 | /* | |
210 | * nfs_page_group_sync_on_bit_locked | |
211 | * | |
212 | * must be called with page group lock held | |
213 | */ | |
214 | static bool | |
215 | nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit) | |
216 | { | |
217 | struct nfs_page *head = req->wb_head; | |
218 | struct nfs_page *tmp; | |
219 | ||
220 | WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags)); | |
221 | WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags)); | |
222 | ||
223 | tmp = req->wb_this_page; | |
224 | while (tmp != req) { | |
225 | if (!test_bit(bit, &tmp->wb_flags)) | |
226 | return false; | |
227 | tmp = tmp->wb_this_page; | |
228 | } | |
229 | ||
230 | /* true! reset all bits */ | |
231 | tmp = req; | |
232 | do { | |
233 | clear_bit(bit, &tmp->wb_flags); | |
234 | tmp = tmp->wb_this_page; | |
235 | } while (tmp != req); | |
236 | ||
237 | return true; | |
238 | } | |
239 | ||
240 | /* | |
241 | * nfs_page_group_sync_on_bit - set bit on current request, but only | |
242 | * return true if the bit is set for all requests in page group | |
243 | * @req - request in page group | |
244 | * @bit - PG_* bit that is used to sync page group | |
245 | */ | |
246 | bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit) | |
247 | { | |
248 | bool ret; | |
249 | ||
fd2f3a06 | 250 | nfs_page_group_lock(req, false); |
2bfc6e56 WAA |
251 | ret = nfs_page_group_sync_on_bit_locked(req, bit); |
252 | nfs_page_group_unlock(req); | |
253 | ||
254 | return ret; | |
255 | } | |
256 | ||
257 | /* | |
258 | * nfs_page_group_init - Initialize the page group linkage for @req | |
259 | * @req - a new nfs request | |
260 | * @prev - the previous request in page group, or NULL if @req is the first | |
261 | * or only request in the group (the head). | |
262 | */ | |
263 | static inline void | |
264 | nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev) | |
265 | { | |
cb1410c7 | 266 | struct inode *inode; |
2bfc6e56 WAA |
267 | WARN_ON_ONCE(prev == req); |
268 | ||
269 | if (!prev) { | |
85710a83 | 270 | /* a head request */ |
2bfc6e56 WAA |
271 | req->wb_head = req; |
272 | req->wb_this_page = req; | |
273 | } else { | |
85710a83 | 274 | /* a subrequest */ |
2bfc6e56 WAA |
275 | WARN_ON_ONCE(prev->wb_this_page != prev->wb_head); |
276 | WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags)); | |
277 | req->wb_head = prev->wb_head; | |
278 | req->wb_this_page = prev->wb_this_page; | |
279 | prev->wb_this_page = req; | |
280 | ||
85710a83 WAA |
281 | /* All subrequests take a ref on the head request until |
282 | * nfs_page_group_destroy is called */ | |
283 | kref_get(&req->wb_head->wb_kref); | |
284 | ||
cb1410c7 WAA |
285 | /* grab extra ref and bump the request count if head request |
286 | * has extra ref from the write/commit path to handle handoff | |
287 | * between write and commit lists. */ | |
17089a29 | 288 | if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) { |
cb1410c7 | 289 | inode = page_file_mapping(req->wb_page)->host; |
17089a29 | 290 | set_bit(PG_INODE_REF, &req->wb_flags); |
2bfc6e56 | 291 | kref_get(&req->wb_kref); |
cb1410c7 WAA |
292 | spin_lock(&inode->i_lock); |
293 | NFS_I(inode)->nrequests++; | |
294 | spin_unlock(&inode->i_lock); | |
17089a29 | 295 | } |
2bfc6e56 WAA |
296 | } |
297 | } | |
298 | ||
299 | /* | |
300 | * nfs_page_group_destroy - sync the destruction of page groups | |
301 | * @req - request that no longer needs the page group | |
302 | * | |
303 | * releases the page group reference from each member once all | |
304 | * members have called this function. | |
305 | */ | |
306 | static void | |
307 | nfs_page_group_destroy(struct kref *kref) | |
308 | { | |
309 | struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref); | |
310 | struct nfs_page *tmp, *next; | |
311 | ||
85710a83 WAA |
312 | /* subrequests must release the ref on the head request */ |
313 | if (req->wb_head != req) | |
314 | nfs_release_request(req->wb_head); | |
315 | ||
2bfc6e56 WAA |
316 | if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN)) |
317 | return; | |
318 | ||
319 | tmp = req; | |
320 | do { | |
321 | next = tmp->wb_this_page; | |
322 | /* unlink and free */ | |
323 | tmp->wb_this_page = tmp; | |
324 | tmp->wb_head = tmp; | |
325 | nfs_free_request(tmp); | |
326 | tmp = next; | |
327 | } while (tmp != req); | |
328 | } | |
329 | ||
1da177e4 LT |
330 | /** |
331 | * nfs_create_request - Create an NFS read/write request. | |
c02f557d | 332 | * @ctx: open context to use |
1da177e4 | 333 | * @page: page to write |
2bfc6e56 | 334 | * @last: last nfs request created for this page group or NULL if head |
1da177e4 LT |
335 | * @offset: starting offset within the page for the write |
336 | * @count: number of bytes to read/write | |
337 | * | |
338 | * The page must be locked by the caller. This makes sure we never | |
a19b89ca | 339 | * create two different requests for the same page. |
1da177e4 LT |
340 | * User should ensure it is safe to sleep in this function. |
341 | */ | |
342 | struct nfs_page * | |
8c8f1ac1 | 343 | nfs_create_request(struct nfs_open_context *ctx, struct page *page, |
2bfc6e56 WAA |
344 | struct nfs_page *last, unsigned int offset, |
345 | unsigned int count) | |
1da177e4 | 346 | { |
1da177e4 | 347 | struct nfs_page *req; |
b3c54de6 | 348 | struct nfs_lock_context *l_ctx; |
1da177e4 | 349 | |
c58c8441 TM |
350 | if (test_bit(NFS_CONTEXT_BAD, &ctx->flags)) |
351 | return ERR_PTR(-EBADF); | |
18eb8842 TM |
352 | /* try to allocate the request struct */ |
353 | req = nfs_page_alloc(); | |
354 | if (req == NULL) | |
355 | return ERR_PTR(-ENOMEM); | |
1da177e4 | 356 | |
015f0212 | 357 | /* get lock context early so we can deal with alloc failures */ |
b3c54de6 TM |
358 | l_ctx = nfs_get_lock_context(ctx); |
359 | if (IS_ERR(l_ctx)) { | |
015f0212 | 360 | nfs_page_free(req); |
b3c54de6 | 361 | return ERR_CAST(l_ctx); |
015f0212 | 362 | } |
b3c54de6 | 363 | req->wb_lock_context = l_ctx; |
577b4232 | 364 | nfs_iocounter_inc(&l_ctx->io_count); |
015f0212 | 365 | |
1da177e4 LT |
366 | /* Initialize the request struct. Initially, we assume a |
367 | * long write-back delay. This will be adjusted in | |
368 | * update_nfs_request below if the region is not locked. */ | |
369 | req->wb_page = page; | |
d56b4ddf | 370 | req->wb_index = page_file_index(page); |
1da177e4 LT |
371 | page_cache_get(page); |
372 | req->wb_offset = offset; | |
373 | req->wb_pgbase = offset; | |
374 | req->wb_bytes = count; | |
1da177e4 | 375 | req->wb_context = get_nfs_open_context(ctx); |
c03b4024 | 376 | kref_init(&req->wb_kref); |
2bfc6e56 | 377 | nfs_page_group_init(req, last); |
1da177e4 LT |
378 | return req; |
379 | } | |
380 | ||
381 | /** | |
1d1afcbc | 382 | * nfs_unlock_request - Unlock request and wake up sleepers. |
1da177e4 LT |
383 | * @req: |
384 | */ | |
1d1afcbc | 385 | void nfs_unlock_request(struct nfs_page *req) |
1da177e4 LT |
386 | { |
387 | if (!NFS_WBACK_BUSY(req)) { | |
388 | printk(KERN_ERR "NFS: Invalid unlock attempted\n"); | |
389 | BUG(); | |
390 | } | |
4e857c58 | 391 | smp_mb__before_atomic(); |
1da177e4 | 392 | clear_bit(PG_BUSY, &req->wb_flags); |
4e857c58 | 393 | smp_mb__after_atomic(); |
464a98bd | 394 | wake_up_bit(&req->wb_flags, PG_BUSY); |
3aff4ebb TM |
395 | } |
396 | ||
397 | /** | |
1d1afcbc TM |
398 | * nfs_unlock_and_release_request - Unlock request and release the nfs_page |
399 | * @req: | |
3aff4ebb | 400 | */ |
1d1afcbc | 401 | void nfs_unlock_and_release_request(struct nfs_page *req) |
3aff4ebb | 402 | { |
1d1afcbc | 403 | nfs_unlock_request(req); |
1da177e4 LT |
404 | nfs_release_request(req); |
405 | } | |
406 | ||
4d65c520 | 407 | /* |
1da177e4 LT |
408 | * nfs_clear_request - Free up all resources allocated to the request |
409 | * @req: | |
410 | * | |
bb6fbc45 TM |
411 | * Release page and open context resources associated with a read/write |
412 | * request after it has completed. | |
1da177e4 | 413 | */ |
4d65c520 | 414 | static void nfs_clear_request(struct nfs_page *req) |
1da177e4 | 415 | { |
cd52ed35 | 416 | struct page *page = req->wb_page; |
bb6fbc45 | 417 | struct nfs_open_context *ctx = req->wb_context; |
f11ac8db | 418 | struct nfs_lock_context *l_ctx = req->wb_lock_context; |
bb6fbc45 | 419 | |
cd52ed35 | 420 | if (page != NULL) { |
cd52ed35 | 421 | page_cache_release(page); |
1da177e4 LT |
422 | req->wb_page = NULL; |
423 | } | |
f11ac8db | 424 | if (l_ctx != NULL) { |
577b4232 | 425 | nfs_iocounter_dec(&l_ctx->io_count); |
f11ac8db TM |
426 | nfs_put_lock_context(l_ctx); |
427 | req->wb_lock_context = NULL; | |
428 | } | |
bb6fbc45 TM |
429 | if (ctx != NULL) { |
430 | put_nfs_open_context(ctx); | |
431 | req->wb_context = NULL; | |
432 | } | |
1da177e4 LT |
433 | } |
434 | ||
1da177e4 LT |
435 | /** |
436 | * nfs_release_request - Release the count on an NFS read/write request | |
437 | * @req: request to release | |
438 | * | |
439 | * Note: Should never be called with the spinlock held! | |
440 | */ | |
d4581383 | 441 | void nfs_free_request(struct nfs_page *req) |
1da177e4 | 442 | { |
2bfc6e56 WAA |
443 | WARN_ON_ONCE(req->wb_this_page != req); |
444 | ||
445 | /* extra debug: make sure no sync bits are still set */ | |
446 | WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags)); | |
67d0338e WAA |
447 | WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags)); |
448 | WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags)); | |
20633f04 WAA |
449 | WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags)); |
450 | WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags)); | |
1da177e4 | 451 | |
bb6fbc45 | 452 | /* Release struct file and open context */ |
1da177e4 | 453 | nfs_clear_request(req); |
1da177e4 LT |
454 | nfs_page_free(req); |
455 | } | |
456 | ||
c03b4024 TM |
457 | void nfs_release_request(struct nfs_page *req) |
458 | { | |
2bfc6e56 | 459 | kref_put(&req->wb_kref, nfs_page_group_destroy); |
9f557cd8 TM |
460 | } |
461 | ||
1da177e4 LT |
462 | /** |
463 | * nfs_wait_on_request - Wait for a request to complete. | |
464 | * @req: request to wait upon. | |
465 | * | |
150030b7 | 466 | * Interruptible by fatal signals only. |
1da177e4 LT |
467 | * The user is responsible for holding a count on the request. |
468 | */ | |
469 | int | |
470 | nfs_wait_on_request(struct nfs_page *req) | |
471 | { | |
74316201 N |
472 | return wait_on_bit_io(&req->wb_flags, PG_BUSY, |
473 | TASK_UNINTERRUPTIBLE); | |
1da177e4 LT |
474 | } |
475 | ||
b4fdac1a WAA |
476 | /* |
477 | * nfs_generic_pg_test - determine if requests can be coalesced | |
478 | * @desc: pointer to descriptor | |
479 | * @prev: previous request in desc, or NULL | |
480 | * @req: this request | |
481 | * | |
482 | * Returns zero if @req can be coalesced into @desc, otherwise it returns | |
483 | * the size of the request. | |
484 | */ | |
485 | size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, | |
486 | struct nfs_page *prev, struct nfs_page *req) | |
5b36c7dc | 487 | { |
a7d42ddb WAA |
488 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
489 | ||
490 | ||
491 | if (mirror->pg_count > mirror->pg_bsize) { | |
f0cb9ab8 WAA |
492 | /* should never happen */ |
493 | WARN_ON_ONCE(1); | |
5b36c7dc | 494 | return 0; |
f0cb9ab8 | 495 | } |
5b36c7dc | 496 | |
2e11f829 CH |
497 | /* |
498 | * Limit the request size so that we can still allocate a page array | |
499 | * for it without upsetting the slab allocator. | |
500 | */ | |
a7d42ddb | 501 | if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) * |
2e11f829 CH |
502 | sizeof(struct page) > PAGE_SIZE) |
503 | return 0; | |
504 | ||
a7d42ddb | 505 | return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes); |
5b36c7dc | 506 | } |
19345cb2 | 507 | EXPORT_SYMBOL_GPL(nfs_generic_pg_test); |
5b36c7dc | 508 | |
1e7f3a48 | 509 | struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops) |
4a0de55c | 510 | { |
1e7f3a48 | 511 | struct nfs_pgio_header *hdr = ops->rw_alloc_header(); |
4a0de55c | 512 | |
1e7f3a48 | 513 | if (hdr) { |
4a0de55c | 514 | INIT_LIST_HEAD(&hdr->pages); |
4a0de55c | 515 | spin_lock_init(&hdr->lock); |
4a0de55c AS |
516 | hdr->rw_ops = ops; |
517 | } | |
1e7f3a48 | 518 | return hdr; |
4a0de55c | 519 | } |
1e7f3a48 | 520 | EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc); |
4a0de55c AS |
521 | |
522 | /* | |
1e7f3a48 | 523 | * nfs_pgio_header_free - Free a read or write header |
4a0de55c AS |
524 | * @hdr: The header to free |
525 | */ | |
1e7f3a48 | 526 | void nfs_pgio_header_free(struct nfs_pgio_header *hdr) |
4a0de55c | 527 | { |
1e7f3a48 | 528 | hdr->rw_ops->rw_free_header(hdr); |
4a0de55c | 529 | } |
1e7f3a48 | 530 | EXPORT_SYMBOL_GPL(nfs_pgio_header_free); |
4a0de55c | 531 | |
00bfa30a | 532 | /** |
4714fb51 WAA |
533 | * nfs_pgio_data_destroy - make @hdr suitable for reuse |
534 | * | |
535 | * Frees memory and releases refs from nfs_generic_pgio, so that it may | |
536 | * be called again. | |
537 | * | |
538 | * @hdr: A header that has had nfs_generic_pgio called | |
00bfa30a | 539 | */ |
d45f60c6 | 540 | void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr) |
00bfa30a | 541 | { |
3caa0c6e TM |
542 | if (hdr->args.context) |
543 | put_nfs_open_context(hdr->args.context); | |
d45f60c6 WAA |
544 | if (hdr->page_array.pagevec != hdr->page_array.page_array) |
545 | kfree(hdr->page_array.pagevec); | |
00bfa30a | 546 | } |
1e7f3a48 | 547 | EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy); |
00bfa30a | 548 | |
ce59515c AS |
549 | /** |
550 | * nfs_pgio_rpcsetup - Set up arguments for a pageio call | |
d45f60c6 | 551 | * @hdr: The pageio hdr |
ce59515c AS |
552 | * @count: Number of bytes to read |
553 | * @offset: Initial offset | |
554 | * @how: How to commit data (writes only) | |
555 | * @cinfo: Commit information for the call (writes only) | |
556 | */ | |
d45f60c6 | 557 | static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr, |
ce59515c AS |
558 | unsigned int count, unsigned int offset, |
559 | int how, struct nfs_commit_info *cinfo) | |
560 | { | |
d45f60c6 | 561 | struct nfs_page *req = hdr->req; |
ce59515c AS |
562 | |
563 | /* Set up the RPC argument and reply structs | |
d45f60c6 | 564 | * NB: take care not to mess about with hdr->commit et al. */ |
ce59515c | 565 | |
d45f60c6 WAA |
566 | hdr->args.fh = NFS_FH(hdr->inode); |
567 | hdr->args.offset = req_offset(req) + offset; | |
ce59515c | 568 | /* pnfs_set_layoutcommit needs this */ |
d45f60c6 WAA |
569 | hdr->mds_offset = hdr->args.offset; |
570 | hdr->args.pgbase = req->wb_pgbase + offset; | |
571 | hdr->args.pages = hdr->page_array.pagevec; | |
572 | hdr->args.count = count; | |
573 | hdr->args.context = get_nfs_open_context(req->wb_context); | |
574 | hdr->args.lock_context = req->wb_lock_context; | |
575 | hdr->args.stable = NFS_UNSTABLE; | |
ce59515c AS |
576 | switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { |
577 | case 0: | |
578 | break; | |
579 | case FLUSH_COND_STABLE: | |
580 | if (nfs_reqs_to_commit(cinfo)) | |
581 | break; | |
582 | default: | |
d45f60c6 | 583 | hdr->args.stable = NFS_FILE_SYNC; |
ce59515c AS |
584 | } |
585 | ||
d45f60c6 WAA |
586 | hdr->res.fattr = &hdr->fattr; |
587 | hdr->res.count = count; | |
588 | hdr->res.eof = 0; | |
c65e6254 | 589 | hdr->res.verf = &hdr->verf; |
d45f60c6 | 590 | nfs_fattr_init(&hdr->fattr); |
ce59515c AS |
591 | } |
592 | ||
a4cdda59 | 593 | /** |
d45f60c6 | 594 | * nfs_pgio_prepare - Prepare pageio hdr to go over the wire |
a4cdda59 | 595 | * @task: The current task |
d45f60c6 | 596 | * @calldata: pageio header to prepare |
a4cdda59 | 597 | */ |
6f92fa45 | 598 | static void nfs_pgio_prepare(struct rpc_task *task, void *calldata) |
a4cdda59 | 599 | { |
d45f60c6 | 600 | struct nfs_pgio_header *hdr = calldata; |
a4cdda59 | 601 | int err; |
d45f60c6 | 602 | err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr); |
a4cdda59 AS |
603 | if (err) |
604 | rpc_exit(task, err); | |
605 | } | |
606 | ||
d45f60c6 | 607 | int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr, |
46a5ab47 | 608 | struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops, |
1ed26f33 AS |
609 | const struct rpc_call_ops *call_ops, int how, int flags) |
610 | { | |
611 | struct rpc_task *task; | |
612 | struct rpc_message msg = { | |
d45f60c6 WAA |
613 | .rpc_argp = &hdr->args, |
614 | .rpc_resp = &hdr->res, | |
46a5ab47 | 615 | .rpc_cred = cred, |
1ed26f33 AS |
616 | }; |
617 | struct rpc_task_setup task_setup_data = { | |
618 | .rpc_client = clnt, | |
d45f60c6 | 619 | .task = &hdr->task, |
1ed26f33 AS |
620 | .rpc_message = &msg, |
621 | .callback_ops = call_ops, | |
d45f60c6 | 622 | .callback_data = hdr, |
1ed26f33 AS |
623 | .workqueue = nfsiod_workqueue, |
624 | .flags = RPC_TASK_ASYNC | flags, | |
625 | }; | |
626 | int ret = 0; | |
627 | ||
abde71f4 | 628 | hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how); |
1ed26f33 AS |
629 | |
630 | dprintk("NFS: %5u initiated pgio call " | |
631 | "(req %s/%llu, %u bytes @ offset %llu)\n", | |
d45f60c6 | 632 | hdr->task.tk_pid, |
343ae531 AS |
633 | hdr->inode->i_sb->s_id, |
634 | (unsigned long long)NFS_FILEID(hdr->inode), | |
d45f60c6 WAA |
635 | hdr->args.count, |
636 | (unsigned long long)hdr->args.offset); | |
1ed26f33 AS |
637 | |
638 | task = rpc_run_task(&task_setup_data); | |
639 | if (IS_ERR(task)) { | |
640 | ret = PTR_ERR(task); | |
641 | goto out; | |
642 | } | |
643 | if (how & FLUSH_SYNC) { | |
644 | ret = rpc_wait_for_completion_task(task); | |
645 | if (ret == 0) | |
646 | ret = task->tk_status; | |
647 | } | |
648 | rpc_put_task(task); | |
649 | out: | |
650 | return ret; | |
651 | } | |
652 | EXPORT_SYMBOL_GPL(nfs_initiate_pgio); | |
653 | ||
844c9e69 AS |
654 | /** |
655 | * nfs_pgio_error - Clean up from a pageio error | |
656 | * @desc: IO descriptor | |
657 | * @hdr: pageio header | |
658 | */ | |
ef2c488c | 659 | static int nfs_pgio_error(struct nfs_pageio_descriptor *desc, |
844c9e69 AS |
660 | struct nfs_pgio_header *hdr) |
661 | { | |
a7d42ddb WAA |
662 | struct nfs_pgio_mirror *mirror; |
663 | u32 midx; | |
664 | ||
844c9e69 | 665 | set_bit(NFS_IOHDR_REDO, &hdr->flags); |
d45f60c6 | 666 | nfs_pgio_data_destroy(hdr); |
4714fb51 | 667 | hdr->completion_ops->completion(hdr); |
a7d42ddb WAA |
668 | /* TODO: Make sure it's right to clean up all mirrors here |
669 | * and not just hdr->pgio_mirror_idx */ | |
670 | for (midx = 0; midx < desc->pg_mirror_count; midx++) { | |
671 | mirror = &desc->pg_mirrors[midx]; | |
672 | desc->pg_completion_ops->error_cleanup(&mirror->pg_list); | |
673 | } | |
844c9e69 AS |
674 | return -ENOMEM; |
675 | } | |
676 | ||
a4cdda59 AS |
677 | /** |
678 | * nfs_pgio_release - Release pageio data | |
d45f60c6 | 679 | * @calldata: The pageio header to release |
a4cdda59 | 680 | */ |
6f92fa45 | 681 | static void nfs_pgio_release(void *calldata) |
a4cdda59 | 682 | { |
d45f60c6 WAA |
683 | struct nfs_pgio_header *hdr = calldata; |
684 | if (hdr->rw_ops->rw_release) | |
685 | hdr->rw_ops->rw_release(hdr); | |
686 | nfs_pgio_data_destroy(hdr); | |
4714fb51 | 687 | hdr->completion_ops->completion(hdr); |
a4cdda59 AS |
688 | } |
689 | ||
a7d42ddb WAA |
690 | static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror, |
691 | unsigned int bsize) | |
692 | { | |
693 | INIT_LIST_HEAD(&mirror->pg_list); | |
694 | mirror->pg_bytes_written = 0; | |
695 | mirror->pg_count = 0; | |
696 | mirror->pg_bsize = bsize; | |
697 | mirror->pg_base = 0; | |
698 | mirror->pg_recoalesce = 0; | |
699 | } | |
700 | ||
1da177e4 | 701 | /** |
d8a5ad75 TM |
702 | * nfs_pageio_init - initialise a page io descriptor |
703 | * @desc: pointer to descriptor | |
bcb71bba TM |
704 | * @inode: pointer to inode |
705 | * @doio: pointer to io function | |
706 | * @bsize: io block size | |
707 | * @io_flags: extra parameters for the io function | |
d8a5ad75 | 708 | */ |
bcb71bba TM |
709 | void nfs_pageio_init(struct nfs_pageio_descriptor *desc, |
710 | struct inode *inode, | |
1751c363 | 711 | const struct nfs_pageio_ops *pg_ops, |
061ae2ed | 712 | const struct nfs_pgio_completion_ops *compl_ops, |
4a0de55c | 713 | const struct nfs_rw_ops *rw_ops, |
84dde76c | 714 | size_t bsize, |
bcb71bba | 715 | int io_flags) |
d8a5ad75 | 716 | { |
a7d42ddb WAA |
717 | struct nfs_pgio_mirror *new; |
718 | int i; | |
719 | ||
b31268ac | 720 | desc->pg_moreio = 0; |
bcb71bba | 721 | desc->pg_inode = inode; |
1751c363 | 722 | desc->pg_ops = pg_ops; |
061ae2ed | 723 | desc->pg_completion_ops = compl_ops; |
4a0de55c | 724 | desc->pg_rw_ops = rw_ops; |
bcb71bba TM |
725 | desc->pg_ioflags = io_flags; |
726 | desc->pg_error = 0; | |
94ad1c80 | 727 | desc->pg_lseg = NULL; |
584aa810 | 728 | desc->pg_dreq = NULL; |
f6166384 | 729 | desc->pg_layout_private = NULL; |
a7d42ddb WAA |
730 | desc->pg_bsize = bsize; |
731 | ||
732 | desc->pg_mirror_count = 1; | |
733 | desc->pg_mirror_idx = 0; | |
734 | ||
735 | if (pg_ops->pg_get_mirror_count) { | |
736 | /* until we have a request, we don't have an lseg and no | |
737 | * idea how many mirrors there will be */ | |
738 | new = kcalloc(NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX, | |
739 | sizeof(struct nfs_pgio_mirror), GFP_KERNEL); | |
740 | desc->pg_mirrors_dynamic = new; | |
741 | desc->pg_mirrors = new; | |
742 | ||
743 | for (i = 0; i < NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX; i++) | |
744 | nfs_pageio_mirror_init(&desc->pg_mirrors[i], bsize); | |
745 | } else { | |
746 | desc->pg_mirrors_dynamic = NULL; | |
747 | desc->pg_mirrors = desc->pg_mirrors_static; | |
748 | nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize); | |
749 | } | |
d8a5ad75 | 750 | } |
89d77c8f | 751 | EXPORT_SYMBOL_GPL(nfs_pageio_init); |
d8a5ad75 | 752 | |
0eecb214 AS |
753 | /** |
754 | * nfs_pgio_result - Basic pageio error handling | |
755 | * @task: The task that ran | |
d45f60c6 | 756 | * @calldata: Pageio header to check |
0eecb214 | 757 | */ |
6f92fa45 | 758 | static void nfs_pgio_result(struct rpc_task *task, void *calldata) |
0eecb214 | 759 | { |
d45f60c6 WAA |
760 | struct nfs_pgio_header *hdr = calldata; |
761 | struct inode *inode = hdr->inode; | |
0eecb214 AS |
762 | |
763 | dprintk("NFS: %s: %5u, (status %d)\n", __func__, | |
764 | task->tk_pid, task->tk_status); | |
765 | ||
d45f60c6 | 766 | if (hdr->rw_ops->rw_done(task, hdr, inode) != 0) |
0eecb214 AS |
767 | return; |
768 | if (task->tk_status < 0) | |
d45f60c6 | 769 | nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset); |
0eecb214 | 770 | else |
d45f60c6 | 771 | hdr->rw_ops->rw_result(task, hdr); |
0eecb214 AS |
772 | } |
773 | ||
ef2c488c AS |
774 | /* |
775 | * Create an RPC task for the given read or write request and kick it. | |
776 | * The page must have been locked by the caller. | |
777 | * | |
778 | * It may happen that the page we're passed is not marked dirty. | |
779 | * This is the case if nfs_updatepage detects a conflicting request | |
780 | * that has been written but not committed. | |
781 | */ | |
f0cb9ab8 WAA |
782 | int nfs_generic_pgio(struct nfs_pageio_descriptor *desc, |
783 | struct nfs_pgio_header *hdr) | |
ef2c488c | 784 | { |
a7d42ddb WAA |
785 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
786 | ||
ef2c488c | 787 | struct nfs_page *req; |
bba5c188 WAA |
788 | struct page **pages, |
789 | *last_page; | |
a7d42ddb | 790 | struct list_head *head = &mirror->pg_list; |
ef2c488c | 791 | struct nfs_commit_info cinfo; |
bba5c188 | 792 | unsigned int pagecount, pageused; |
ef2c488c | 793 | |
a7d42ddb | 794 | pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count); |
4714fb51 | 795 | if (!nfs_pgarray_set(&hdr->page_array, pagecount)) |
ef2c488c AS |
796 | return nfs_pgio_error(desc, hdr); |
797 | ||
798 | nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); | |
d45f60c6 | 799 | pages = hdr->page_array.pagevec; |
bba5c188 WAA |
800 | last_page = NULL; |
801 | pageused = 0; | |
ef2c488c AS |
802 | while (!list_empty(head)) { |
803 | req = nfs_list_entry(head->next); | |
804 | nfs_list_remove_request(req); | |
805 | nfs_list_add_request(req, &hdr->pages); | |
bba5c188 | 806 | |
bba5c188 | 807 | if (!last_page || last_page != req->wb_page) { |
bba5c188 | 808 | pageused++; |
b8fb9c30 TM |
809 | if (pageused > pagecount) |
810 | break; | |
811 | *pages++ = last_page = req->wb_page; | |
bba5c188 | 812 | } |
ef2c488c | 813 | } |
bba5c188 WAA |
814 | if (WARN_ON_ONCE(pageused != pagecount)) |
815 | return nfs_pgio_error(desc, hdr); | |
ef2c488c AS |
816 | |
817 | if ((desc->pg_ioflags & FLUSH_COND_STABLE) && | |
818 | (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) | |
819 | desc->pg_ioflags &= ~FLUSH_COND_STABLE; | |
820 | ||
821 | /* Set up the argument struct */ | |
a7d42ddb | 822 | nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo); |
ef2c488c AS |
823 | desc->pg_rpc_callops = &nfs_pgio_common_ops; |
824 | return 0; | |
825 | } | |
f0cb9ab8 | 826 | EXPORT_SYMBOL_GPL(nfs_generic_pgio); |
ef2c488c | 827 | |
41d8d5b7 | 828 | static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) |
cf485fcd | 829 | { |
a7d42ddb | 830 | struct nfs_pgio_mirror *mirror; |
cf485fcd AS |
831 | struct nfs_pgio_header *hdr; |
832 | int ret; | |
833 | ||
a7d42ddb WAA |
834 | mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
835 | ||
1e7f3a48 WAA |
836 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
837 | if (!hdr) { | |
a7d42ddb WAA |
838 | /* TODO: make sure this is right with mirroring - or |
839 | * should it back out all mirrors? */ | |
840 | desc->pg_completion_ops->error_cleanup(&mirror->pg_list); | |
cf485fcd AS |
841 | return -ENOMEM; |
842 | } | |
1e7f3a48 | 843 | nfs_pgheader_init(desc, hdr, nfs_pgio_header_free); |
cf485fcd AS |
844 | ret = nfs_generic_pgio(desc, hdr); |
845 | if (ret == 0) | |
7f714720 | 846 | ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode), |
46a5ab47 PT |
847 | hdr, |
848 | hdr->cred, | |
849 | NFS_PROTO(hdr->inode), | |
abde71f4 | 850 | desc->pg_rpc_callops, |
7f714720 | 851 | desc->pg_ioflags, 0); |
cf485fcd AS |
852 | return ret; |
853 | } | |
854 | ||
a7d42ddb WAA |
855 | /* |
856 | * nfs_pageio_setup_mirroring - determine if mirroring is to be used | |
857 | * by calling the pg_get_mirror_count op | |
858 | */ | |
859 | static int nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio, | |
860 | struct nfs_page *req) | |
861 | { | |
862 | int mirror_count = 1; | |
863 | ||
864 | if (!pgio->pg_ops->pg_get_mirror_count) | |
865 | return 0; | |
866 | ||
867 | mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req); | |
868 | ||
869 | if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) | |
870 | return -EINVAL; | |
871 | ||
872 | if (WARN_ON_ONCE(!pgio->pg_mirrors_dynamic)) | |
873 | return -EINVAL; | |
874 | ||
875 | pgio->pg_mirror_count = mirror_count; | |
876 | ||
877 | return 0; | |
878 | } | |
879 | ||
880 | /* | |
881 | * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1) | |
882 | */ | |
883 | void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio) | |
884 | { | |
885 | pgio->pg_mirror_count = 1; | |
886 | pgio->pg_mirror_idx = 0; | |
887 | } | |
888 | ||
889 | static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio) | |
890 | { | |
891 | pgio->pg_mirror_count = 1; | |
892 | pgio->pg_mirror_idx = 0; | |
893 | pgio->pg_mirrors = pgio->pg_mirrors_static; | |
894 | kfree(pgio->pg_mirrors_dynamic); | |
895 | pgio->pg_mirrors_dynamic = NULL; | |
896 | } | |
897 | ||
4109bb74 TM |
898 | static bool nfs_match_open_context(const struct nfs_open_context *ctx1, |
899 | const struct nfs_open_context *ctx2) | |
900 | { | |
901 | return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state; | |
902 | } | |
903 | ||
904 | static bool nfs_match_lock_context(const struct nfs_lock_context *l1, | |
905 | const struct nfs_lock_context *l2) | |
906 | { | |
907 | return l1->lockowner.l_owner == l2->lockowner.l_owner | |
908 | && l1->lockowner.l_pid == l2->lockowner.l_pid; | |
909 | } | |
910 | ||
d8a5ad75 TM |
911 | /** |
912 | * nfs_can_coalesce_requests - test two requests for compatibility | |
913 | * @prev: pointer to nfs_page | |
914 | * @req: pointer to nfs_page | |
915 | * | |
916 | * The nfs_page structures 'prev' and 'req' are compared to ensure that the | |
917 | * page data area they describe is contiguous, and that their RPC | |
918 | * credentials, NFSv4 open state, and lockowners are the same. | |
919 | * | |
920 | * Return 'true' if this is the case, else return 'false'. | |
921 | */ | |
18ad0a9f BH |
922 | static bool nfs_can_coalesce_requests(struct nfs_page *prev, |
923 | struct nfs_page *req, | |
924 | struct nfs_pageio_descriptor *pgio) | |
d8a5ad75 | 925 | { |
b4fdac1a WAA |
926 | size_t size; |
927 | ||
ab75e417 WAA |
928 | if (prev) { |
929 | if (!nfs_match_open_context(req->wb_context, prev->wb_context)) | |
930 | return false; | |
931 | if (req->wb_context->dentry->d_inode->i_flock != NULL && | |
932 | !nfs_match_lock_context(req->wb_lock_context, | |
933 | prev->wb_lock_context)) | |
934 | return false; | |
ab75e417 WAA |
935 | if (req_offset(req) != req_offset(prev) + prev->wb_bytes) |
936 | return false; | |
78270e8f WAA |
937 | if (req->wb_page == prev->wb_page) { |
938 | if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes) | |
939 | return false; | |
940 | } else { | |
941 | if (req->wb_pgbase != 0 || | |
942 | prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE) | |
943 | return false; | |
944 | } | |
ab75e417 | 945 | } |
b4fdac1a | 946 | size = pgio->pg_ops->pg_test(pgio, prev, req); |
f0cb9ab8 WAA |
947 | WARN_ON_ONCE(size > req->wb_bytes); |
948 | if (size && size < req->wb_bytes) | |
949 | req->wb_bytes = size; | |
b4fdac1a | 950 | return size > 0; |
d8a5ad75 TM |
951 | } |
952 | ||
953 | /** | |
bcb71bba | 954 | * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list. |
d8a5ad75 TM |
955 | * @desc: destination io descriptor |
956 | * @req: request | |
957 | * | |
958 | * Returns true if the request 'req' was successfully coalesced into the | |
959 | * existing list of pages 'desc'. | |
960 | */ | |
bcb71bba TM |
961 | static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc, |
962 | struct nfs_page *req) | |
d8a5ad75 | 963 | { |
a7d42ddb WAA |
964 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
965 | ||
ab75e417 | 966 | struct nfs_page *prev = NULL; |
a7d42ddb WAA |
967 | |
968 | if (mirror->pg_count != 0) { | |
969 | prev = nfs_list_entry(mirror->pg_list.prev); | |
5b36c7dc | 970 | } else { |
d8007d4d TM |
971 | if (desc->pg_ops->pg_init) |
972 | desc->pg_ops->pg_init(desc, req); | |
a7d42ddb | 973 | mirror->pg_base = req->wb_pgbase; |
5b36c7dc | 974 | } |
ab75e417 WAA |
975 | if (!nfs_can_coalesce_requests(prev, req, desc)) |
976 | return 0; | |
d8a5ad75 | 977 | nfs_list_remove_request(req); |
a7d42ddb WAA |
978 | nfs_list_add_request(req, &mirror->pg_list); |
979 | mirror->pg_count += req->wb_bytes; | |
d8a5ad75 TM |
980 | return 1; |
981 | } | |
982 | ||
bcb71bba TM |
983 | /* |
984 | * Helper for nfs_pageio_add_request and nfs_pageio_complete | |
985 | */ | |
986 | static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc) | |
987 | { | |
a7d42ddb WAA |
988 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
989 | ||
990 | ||
991 | if (!list_empty(&mirror->pg_list)) { | |
1751c363 | 992 | int error = desc->pg_ops->pg_doio(desc); |
bcb71bba TM |
993 | if (error < 0) |
994 | desc->pg_error = error; | |
995 | else | |
a7d42ddb | 996 | mirror->pg_bytes_written += mirror->pg_count; |
bcb71bba | 997 | } |
a7d42ddb WAA |
998 | if (list_empty(&mirror->pg_list)) { |
999 | mirror->pg_count = 0; | |
1000 | mirror->pg_base = 0; | |
bcb71bba TM |
1001 | } |
1002 | } | |
1003 | ||
1004 | /** | |
1005 | * nfs_pageio_add_request - Attempt to coalesce a request into a page list. | |
1006 | * @desc: destination io descriptor | |
1007 | * @req: request | |
1008 | * | |
2bfc6e56 WAA |
1009 | * This may split a request into subrequests which are all part of the |
1010 | * same page group. | |
1011 | * | |
bcb71bba TM |
1012 | * Returns true if the request 'req' was successfully coalesced into the |
1013 | * existing list of pages 'desc'. | |
1014 | */ | |
d9156f9f | 1015 | static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
8b09bee3 | 1016 | struct nfs_page *req) |
bcb71bba | 1017 | { |
a7d42ddb WAA |
1018 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
1019 | ||
2bfc6e56 WAA |
1020 | struct nfs_page *subreq; |
1021 | unsigned int bytes_left = 0; | |
1022 | unsigned int offset, pgbase; | |
1023 | ||
a7d42ddb WAA |
1024 | WARN_ON_ONCE(desc->pg_mirror_idx >= desc->pg_mirror_count); |
1025 | ||
bfd484a5 | 1026 | nfs_page_group_lock(req, false); |
2bfc6e56 WAA |
1027 | |
1028 | subreq = req; | |
1029 | bytes_left = subreq->wb_bytes; | |
1030 | offset = subreq->wb_offset; | |
1031 | pgbase = subreq->wb_pgbase; | |
1032 | ||
1033 | do { | |
1034 | if (!nfs_pageio_do_add_request(desc, subreq)) { | |
1035 | /* make sure pg_test call(s) did nothing */ | |
1036 | WARN_ON_ONCE(subreq->wb_bytes != bytes_left); | |
1037 | WARN_ON_ONCE(subreq->wb_offset != offset); | |
1038 | WARN_ON_ONCE(subreq->wb_pgbase != pgbase); | |
1039 | ||
1040 | nfs_page_group_unlock(req); | |
1041 | desc->pg_moreio = 1; | |
1042 | nfs_pageio_doio(desc); | |
1043 | if (desc->pg_error < 0) | |
1044 | return 0; | |
a7d42ddb | 1045 | if (mirror->pg_recoalesce) |
2bfc6e56 WAA |
1046 | return 0; |
1047 | /* retry add_request for this subreq */ | |
bfd484a5 | 1048 | nfs_page_group_lock(req, false); |
2bfc6e56 WAA |
1049 | continue; |
1050 | } | |
1051 | ||
1052 | /* check for buggy pg_test call(s) */ | |
1053 | WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE); | |
1054 | WARN_ON_ONCE(subreq->wb_bytes > bytes_left); | |
1055 | WARN_ON_ONCE(subreq->wb_bytes == 0); | |
1056 | ||
1057 | bytes_left -= subreq->wb_bytes; | |
1058 | offset += subreq->wb_bytes; | |
1059 | pgbase += subreq->wb_bytes; | |
1060 | ||
1061 | if (bytes_left) { | |
1062 | subreq = nfs_create_request(req->wb_context, | |
1063 | req->wb_page, | |
1064 | subreq, pgbase, bytes_left); | |
c1109558 TM |
1065 | if (IS_ERR(subreq)) |
1066 | goto err_ptr; | |
2bfc6e56 WAA |
1067 | nfs_lock_request(subreq); |
1068 | subreq->wb_offset = offset; | |
1069 | subreq->wb_index = req->wb_index; | |
1070 | } | |
1071 | } while (bytes_left > 0); | |
1072 | ||
1073 | nfs_page_group_unlock(req); | |
bcb71bba | 1074 | return 1; |
c1109558 TM |
1075 | err_ptr: |
1076 | desc->pg_error = PTR_ERR(subreq); | |
1077 | nfs_page_group_unlock(req); | |
1078 | return 0; | |
bcb71bba TM |
1079 | } |
1080 | ||
d9156f9f TM |
1081 | static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc) |
1082 | { | |
a7d42ddb | 1083 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx]; |
d9156f9f TM |
1084 | LIST_HEAD(head); |
1085 | ||
1086 | do { | |
a7d42ddb WAA |
1087 | list_splice_init(&mirror->pg_list, &head); |
1088 | mirror->pg_bytes_written -= mirror->pg_count; | |
1089 | mirror->pg_count = 0; | |
1090 | mirror->pg_base = 0; | |
1091 | mirror->pg_recoalesce = 0; | |
1092 | ||
f563b89b | 1093 | desc->pg_moreio = 0; |
d9156f9f TM |
1094 | |
1095 | while (!list_empty(&head)) { | |
1096 | struct nfs_page *req; | |
1097 | ||
1098 | req = list_first_entry(&head, struct nfs_page, wb_list); | |
1099 | nfs_list_remove_request(req); | |
1100 | if (__nfs_pageio_add_request(desc, req)) | |
1101 | continue; | |
1102 | if (desc->pg_error < 0) | |
1103 | return 0; | |
1104 | break; | |
1105 | } | |
a7d42ddb | 1106 | } while (mirror->pg_recoalesce); |
d9156f9f TM |
1107 | return 1; |
1108 | } | |
1109 | ||
a7d42ddb | 1110 | static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc, |
d9156f9f TM |
1111 | struct nfs_page *req) |
1112 | { | |
1113 | int ret; | |
1114 | ||
1115 | do { | |
1116 | ret = __nfs_pageio_add_request(desc, req); | |
1117 | if (ret) | |
1118 | break; | |
1119 | if (desc->pg_error < 0) | |
1120 | break; | |
1121 | ret = nfs_do_recoalesce(desc); | |
1122 | } while (ret); | |
a7d42ddb | 1123 | |
d9156f9f TM |
1124 | return ret; |
1125 | } | |
1126 | ||
a7d42ddb WAA |
1127 | int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
1128 | struct nfs_page *req) | |
1129 | { | |
1130 | u32 midx; | |
1131 | unsigned int pgbase, offset, bytes; | |
1132 | struct nfs_page *dupreq, *lastreq; | |
1133 | ||
1134 | pgbase = req->wb_pgbase; | |
1135 | offset = req->wb_offset; | |
1136 | bytes = req->wb_bytes; | |
1137 | ||
1138 | nfs_pageio_setup_mirroring(desc, req); | |
1139 | ||
1140 | for (midx = 0; midx < desc->pg_mirror_count; midx++) { | |
1141 | if (midx) { | |
1142 | nfs_page_group_lock(req, false); | |
1143 | ||
1144 | /* find the last request */ | |
1145 | for (lastreq = req->wb_head; | |
1146 | lastreq->wb_this_page != req->wb_head; | |
1147 | lastreq = lastreq->wb_this_page) | |
1148 | ; | |
1149 | ||
1150 | dupreq = nfs_create_request(req->wb_context, | |
1151 | req->wb_page, lastreq, pgbase, bytes); | |
1152 | ||
1153 | if (IS_ERR(dupreq)) { | |
1154 | nfs_page_group_unlock(req); | |
1155 | return 0; | |
1156 | } | |
1157 | ||
1158 | nfs_lock_request(dupreq); | |
1159 | nfs_page_group_unlock(req); | |
1160 | dupreq->wb_offset = offset; | |
1161 | dupreq->wb_index = req->wb_index; | |
1162 | } else | |
1163 | dupreq = req; | |
1164 | ||
1165 | desc->pg_mirror_idx = midx; | |
1166 | if (!nfs_pageio_add_request_mirror(desc, dupreq)) | |
1167 | return 0; | |
1168 | } | |
1169 | ||
1170 | return 1; | |
1171 | } | |
1172 | ||
1173 | /* | |
1174 | * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an | |
1175 | * nfs_pageio_descriptor | |
1176 | * @desc: pointer to io descriptor | |
1177 | */ | |
1178 | static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc, | |
1179 | u32 mirror_idx) | |
1180 | { | |
1181 | struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx]; | |
1182 | u32 restore_idx = desc->pg_mirror_idx; | |
1183 | ||
1184 | desc->pg_mirror_idx = mirror_idx; | |
1185 | for (;;) { | |
1186 | nfs_pageio_doio(desc); | |
1187 | if (!mirror->pg_recoalesce) | |
1188 | break; | |
1189 | if (!nfs_do_recoalesce(desc)) | |
1190 | break; | |
1191 | } | |
1192 | desc->pg_mirror_idx = restore_idx; | |
1193 | } | |
1194 | ||
53113ad3 WAA |
1195 | /* |
1196 | * nfs_pageio_resend - Transfer requests to new descriptor and resend | |
1197 | * @hdr - the pgio header to move request from | |
1198 | * @desc - the pageio descriptor to add requests to | |
1199 | * | |
1200 | * Try to move each request (nfs_page) from @hdr to @desc then attempt | |
1201 | * to send them. | |
1202 | * | |
1203 | * Returns 0 on success and < 0 on error. | |
1204 | */ | |
1205 | int nfs_pageio_resend(struct nfs_pageio_descriptor *desc, | |
1206 | struct nfs_pgio_header *hdr) | |
1207 | { | |
1208 | LIST_HEAD(failed); | |
1209 | ||
1210 | desc->pg_dreq = hdr->dreq; | |
1211 | while (!list_empty(&hdr->pages)) { | |
1212 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); | |
1213 | ||
1214 | nfs_list_remove_request(req); | |
1215 | if (!nfs_pageio_add_request(desc, req)) | |
1216 | nfs_list_add_request(req, &failed); | |
1217 | } | |
1218 | nfs_pageio_complete(desc); | |
1219 | if (!list_empty(&failed)) { | |
1220 | list_move(&failed, &hdr->pages); | |
1221 | return -EIO; | |
1222 | } | |
1223 | return 0; | |
1224 | } | |
1225 | EXPORT_SYMBOL_GPL(nfs_pageio_resend); | |
d9156f9f | 1226 | |
bcb71bba | 1227 | /** |
2176bf42 | 1228 | * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor |
bcb71bba TM |
1229 | * @desc: pointer to io descriptor |
1230 | */ | |
1231 | void nfs_pageio_complete(struct nfs_pageio_descriptor *desc) | |
1232 | { | |
a7d42ddb WAA |
1233 | u32 midx; |
1234 | ||
1235 | for (midx = 0; midx < desc->pg_mirror_count; midx++) | |
1236 | nfs_pageio_complete_mirror(desc, midx); | |
2176bf42 WAA |
1237 | |
1238 | if (desc->pg_ops->pg_cleanup) | |
1239 | desc->pg_ops->pg_cleanup(desc); | |
a7d42ddb | 1240 | nfs_pageio_cleanup_mirroring(desc); |
bcb71bba TM |
1241 | } |
1242 | ||
7fe7f848 TM |
1243 | /** |
1244 | * nfs_pageio_cond_complete - Conditional I/O completion | |
1245 | * @desc: pointer to io descriptor | |
1246 | * @index: page index | |
1247 | * | |
1248 | * It is important to ensure that processes don't try to take locks | |
1249 | * on non-contiguous ranges of pages as that might deadlock. This | |
1250 | * function should be called before attempting to wait on a locked | |
1251 | * nfs_page. It will complete the I/O if the page index 'index' | |
1252 | * is not contiguous with the existing list of pages in 'desc'. | |
1253 | */ | |
1254 | void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index) | |
1255 | { | |
a7d42ddb WAA |
1256 | struct nfs_pgio_mirror *mirror; |
1257 | struct nfs_page *prev; | |
1258 | u32 midx; | |
1259 | ||
1260 | for (midx = 0; midx < desc->pg_mirror_count; midx++) { | |
1261 | mirror = &desc->pg_mirrors[midx]; | |
1262 | if (!list_empty(&mirror->pg_list)) { | |
1263 | prev = nfs_list_entry(mirror->pg_list.prev); | |
1264 | if (index != prev->wb_index + 1) | |
1265 | nfs_pageio_complete_mirror(desc, midx); | |
1266 | } | |
7fe7f848 TM |
1267 | } |
1268 | } | |
1269 | ||
f7b422b1 | 1270 | int __init nfs_init_nfspagecache(void) |
1da177e4 LT |
1271 | { |
1272 | nfs_page_cachep = kmem_cache_create("nfs_page", | |
1273 | sizeof(struct nfs_page), | |
1274 | 0, SLAB_HWCACHE_ALIGN, | |
20c2df83 | 1275 | NULL); |
1da177e4 LT |
1276 | if (nfs_page_cachep == NULL) |
1277 | return -ENOMEM; | |
1278 | ||
1279 | return 0; | |
1280 | } | |
1281 | ||
266bee88 | 1282 | void nfs_destroy_nfspagecache(void) |
1da177e4 | 1283 | { |
1a1d92c1 | 1284 | kmem_cache_destroy(nfs_page_cachep); |
1da177e4 LT |
1285 | } |
1286 | ||
ef2c488c | 1287 | static const struct rpc_call_ops nfs_pgio_common_ops = { |
6f92fa45 AS |
1288 | .rpc_call_prepare = nfs_pgio_prepare, |
1289 | .rpc_call_done = nfs_pgio_result, | |
1290 | .rpc_release = nfs_pgio_release, | |
1291 | }; | |
41d8d5b7 AS |
1292 | |
1293 | const struct nfs_pageio_ops nfs_pgio_rw_ops = { | |
1294 | .pg_test = nfs_generic_pg_test, | |
1295 | .pg_doio = nfs_generic_pg_pgios, | |
1296 | }; |