af25c19c88fa4cdce6fa6ace74921a2ebde636c1
[deliverable/linux.git] / drivers / staging / lustre / lustre / osc / osc_cache.c
1 /*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2012, Intel Corporation.
31 *
32 */
33 /*
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Sun Microsystems, Inc.
36 *
37 * osc cache management.
38 *
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40 */
41
42 #define DEBUG_SUBSYSTEM S_OSC
43
44 #include "osc_cl_internal.h"
45 #include "osc_internal.h"
46
47 static int extent_debug; /* set it to be true for more debug */
48
49 static void osc_update_pending(struct osc_object *obj, int cmd, int delta);
50 static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
51 int state);
52 static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
53 struct osc_async_page *oap, int sent, int rc);
54 static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
55 int cmd);
56 static int osc_refresh_count(const struct lu_env *env,
57 struct osc_async_page *oap, int cmd);
58 static int osc_io_unplug_async(const struct lu_env *env,
59 struct client_obd *cli, struct osc_object *osc);
60 static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
61 unsigned int lost_grant);
62
63 static void osc_extent_tree_dump0(int level, struct osc_object *obj,
64 const char *func, int line);
65 #define osc_extent_tree_dump(lvl, obj) \
66 osc_extent_tree_dump0(lvl, obj, __func__, __LINE__)
67
68 /** \addtogroup osc
69 * @{
70 */
71
72 /* ------------------ osc extent ------------------ */
73 static inline char *ext_flags(struct osc_extent *ext, char *flags)
74 {
75 char *buf = flags;
76 *buf++ = ext->oe_rw ? 'r' : 'w';
77 if (ext->oe_intree)
78 *buf++ = 'i';
79 if (ext->oe_srvlock)
80 *buf++ = 's';
81 if (ext->oe_hp)
82 *buf++ = 'h';
83 if (ext->oe_urgent)
84 *buf++ = 'u';
85 if (ext->oe_memalloc)
86 *buf++ = 'm';
87 if (ext->oe_trunc_pending)
88 *buf++ = 't';
89 if (ext->oe_fsync_wait)
90 *buf++ = 'Y';
91 *buf = 0;
92 return flags;
93 }
94
95 static inline char list_empty_marker(struct list_head *list)
96 {
97 return list_empty(list) ? '-' : '+';
98 }
99
100 #define EXTSTR "[%lu -> %lu/%lu]"
101 #define EXTPARA(ext) (ext)->oe_start, (ext)->oe_end, (ext)->oe_max_end
102 static const char *oes_strings[] = {
103 "inv", "active", "cache", "locking", "lockdone", "rpc", "trunc", NULL };
104
105 #define OSC_EXTENT_DUMP(lvl, extent, fmt, ...) do { \
106 struct osc_extent *__ext = (extent); \
107 char __buf[16]; \
108 \
109 CDEBUG(lvl, \
110 "extent %p@{" EXTSTR ", " \
111 "[%d|%d|%c|%s|%s|%p], [%d|%d|%c|%c|%p|%u|%p]} " fmt, \
112 /* ----- extent part 0 ----- */ \
113 __ext, EXTPARA(__ext), \
114 /* ----- part 1 ----- */ \
115 atomic_read(&__ext->oe_refc), \
116 atomic_read(&__ext->oe_users), \
117 list_empty_marker(&__ext->oe_link), \
118 oes_strings[__ext->oe_state], ext_flags(__ext, __buf), \
119 __ext->oe_obj, \
120 /* ----- part 2 ----- */ \
121 __ext->oe_grants, __ext->oe_nr_pages, \
122 list_empty_marker(&__ext->oe_pages), \
123 waitqueue_active(&__ext->oe_waitq) ? '+' : '-', \
124 __ext->oe_osclock, __ext->oe_mppr, __ext->oe_owner, \
125 /* ----- part 4 ----- */ \
126 ## __VA_ARGS__); \
127 } while (0)
128
129 #undef EASSERTF
130 #define EASSERTF(expr, ext, fmt, args...) do { \
131 if (!(expr)) { \
132 OSC_EXTENT_DUMP(D_ERROR, (ext), fmt, ##args); \
133 osc_extent_tree_dump(D_ERROR, (ext)->oe_obj); \
134 LASSERT(expr); \
135 } \
136 } while (0)
137
138 #undef EASSERT
139 #define EASSERT(expr, ext) EASSERTF(expr, ext, "\n")
140
141 static inline struct osc_extent *rb_extent(struct rb_node *n)
142 {
143 if (n == NULL)
144 return NULL;
145
146 return container_of(n, struct osc_extent, oe_node);
147 }
148
149 static inline struct osc_extent *next_extent(struct osc_extent *ext)
150 {
151 if (ext == NULL)
152 return NULL;
153
154 LASSERT(ext->oe_intree);
155 return rb_extent(rb_next(&ext->oe_node));
156 }
157
158 static inline struct osc_extent *prev_extent(struct osc_extent *ext)
159 {
160 if (ext == NULL)
161 return NULL;
162
163 LASSERT(ext->oe_intree);
164 return rb_extent(rb_prev(&ext->oe_node));
165 }
166
167 static inline struct osc_extent *first_extent(struct osc_object *obj)
168 {
169 return rb_extent(rb_first(&obj->oo_root));
170 }
171
172 /* object must be locked by caller. */
173 static int osc_extent_sanity_check0(struct osc_extent *ext,
174 const char *func, const int line)
175 {
176 struct osc_object *obj = ext->oe_obj;
177 struct osc_async_page *oap;
178 int page_count;
179 int rc = 0;
180
181 if (!osc_object_is_locked(obj))
182 GOTO(out, rc = 9);
183
184 if (ext->oe_state >= OES_STATE_MAX)
185 GOTO(out, rc = 10);
186
187 if (atomic_read(&ext->oe_refc) <= 0)
188 GOTO(out, rc = 20);
189
190 if (atomic_read(&ext->oe_refc) < atomic_read(&ext->oe_users))
191 GOTO(out, rc = 30);
192
193 switch (ext->oe_state) {
194 case OES_INV:
195 if (ext->oe_nr_pages > 0 || !list_empty(&ext->oe_pages))
196 GOTO(out, rc = 35);
197 GOTO(out, rc = 0);
198 break;
199 case OES_ACTIVE:
200 if (atomic_read(&ext->oe_users) == 0)
201 GOTO(out, rc = 40);
202 if (ext->oe_hp)
203 GOTO(out, rc = 50);
204 if (ext->oe_fsync_wait && !ext->oe_urgent)
205 GOTO(out, rc = 55);
206 break;
207 case OES_CACHE:
208 if (ext->oe_grants == 0)
209 GOTO(out, rc = 60);
210 if (ext->oe_fsync_wait && !ext->oe_urgent && !ext->oe_hp)
211 GOTO(out, rc = 65);
212 default:
213 if (atomic_read(&ext->oe_users) > 0)
214 GOTO(out, rc = 70);
215 }
216
217 if (ext->oe_max_end < ext->oe_end || ext->oe_end < ext->oe_start)
218 GOTO(out, rc = 80);
219
220 if (ext->oe_osclock == NULL && ext->oe_grants > 0)
221 GOTO(out, rc = 90);
222
223 if (ext->oe_osclock) {
224 struct cl_lock_descr *descr;
225 descr = &ext->oe_osclock->cll_descr;
226 if (!(descr->cld_start <= ext->oe_start &&
227 descr->cld_end >= ext->oe_max_end))
228 GOTO(out, rc = 100);
229 }
230
231 if (ext->oe_nr_pages > ext->oe_mppr)
232 GOTO(out, rc = 105);
233
234 /* Do not verify page list if extent is in RPC. This is because an
235 * in-RPC extent is supposed to be exclusively accessible w/o lock. */
236 if (ext->oe_state > OES_CACHE)
237 GOTO(out, rc = 0);
238
239 if (!extent_debug)
240 GOTO(out, rc = 0);
241
242 page_count = 0;
243 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
244 pgoff_t index = oap2cl_page(oap)->cp_index;
245 ++page_count;
246 if (index > ext->oe_end || index < ext->oe_start)
247 GOTO(out, rc = 110);
248 }
249 if (page_count != ext->oe_nr_pages)
250 GOTO(out, rc = 120);
251
252 out:
253 if (rc != 0)
254 OSC_EXTENT_DUMP(D_ERROR, ext,
255 "%s:%d sanity check %p failed with rc = %d\n",
256 func, line, ext, rc);
257 return rc;
258 }
259
260 #define sanity_check_nolock(ext) \
261 osc_extent_sanity_check0(ext, __func__, __LINE__)
262
263 #define sanity_check(ext) ({ \
264 int __res; \
265 osc_object_lock((ext)->oe_obj); \
266 __res = sanity_check_nolock(ext); \
267 osc_object_unlock((ext)->oe_obj); \
268 __res; \
269 })
270
271
272 /**
273 * sanity check - to make sure there is no overlapped extent in the tree.
274 */
275 static int osc_extent_is_overlapped(struct osc_object *obj,
276 struct osc_extent *ext)
277 {
278 struct osc_extent *tmp;
279
280 LASSERT(osc_object_is_locked(obj));
281
282 if (!extent_debug)
283 return 0;
284
285 for (tmp = first_extent(obj); tmp != NULL; tmp = next_extent(tmp)) {
286 if (tmp == ext)
287 continue;
288 if (tmp->oe_end >= ext->oe_start &&
289 tmp->oe_start <= ext->oe_end)
290 return 1;
291 }
292 return 0;
293 }
294
295 static void osc_extent_state_set(struct osc_extent *ext, int state)
296 {
297 LASSERT(osc_object_is_locked(ext->oe_obj));
298 LASSERT(state >= OES_INV && state < OES_STATE_MAX);
299
300 /* Never try to sanity check a state changing extent :-) */
301 /* LASSERT(sanity_check_nolock(ext) == 0); */
302
303 /* TODO: validate the state machine */
304 ext->oe_state = state;
305 wake_up_all(&ext->oe_waitq);
306 }
307
308 static struct osc_extent *osc_extent_alloc(struct osc_object *obj)
309 {
310 struct osc_extent *ext;
311
312 OBD_SLAB_ALLOC_PTR_GFP(ext, osc_extent_kmem, GFP_IOFS);
313 if (ext == NULL)
314 return NULL;
315
316 RB_CLEAR_NODE(&ext->oe_node);
317 ext->oe_obj = obj;
318 atomic_set(&ext->oe_refc, 1);
319 atomic_set(&ext->oe_users, 0);
320 INIT_LIST_HEAD(&ext->oe_link);
321 ext->oe_state = OES_INV;
322 INIT_LIST_HEAD(&ext->oe_pages);
323 init_waitqueue_head(&ext->oe_waitq);
324 ext->oe_osclock = NULL;
325
326 return ext;
327 }
328
329 static void osc_extent_free(struct osc_extent *ext)
330 {
331 OBD_SLAB_FREE_PTR(ext, osc_extent_kmem);
332 }
333
334 static struct osc_extent *osc_extent_get(struct osc_extent *ext)
335 {
336 LASSERT(atomic_read(&ext->oe_refc) >= 0);
337 atomic_inc(&ext->oe_refc);
338 return ext;
339 }
340
341 static void osc_extent_put(const struct lu_env *env, struct osc_extent *ext)
342 {
343 LASSERT(atomic_read(&ext->oe_refc) > 0);
344 if (atomic_dec_and_test(&ext->oe_refc)) {
345 LASSERT(list_empty(&ext->oe_link));
346 LASSERT(atomic_read(&ext->oe_users) == 0);
347 LASSERT(ext->oe_state == OES_INV);
348 LASSERT(!ext->oe_intree);
349
350 if (ext->oe_osclock) {
351 cl_lock_put(env, ext->oe_osclock);
352 ext->oe_osclock = NULL;
353 }
354 osc_extent_free(ext);
355 }
356 }
357
358 /**
359 * osc_extent_put_trust() is a special version of osc_extent_put() when
360 * it's known that the caller is not the last user. This is to address the
361 * problem of lacking of lu_env ;-).
362 */
363 static void osc_extent_put_trust(struct osc_extent *ext)
364 {
365 LASSERT(atomic_read(&ext->oe_refc) > 1);
366 LASSERT(osc_object_is_locked(ext->oe_obj));
367 atomic_dec(&ext->oe_refc);
368 }
369
370 /**
371 * Return the extent which includes pgoff @index, or return the greatest
372 * previous extent in the tree.
373 */
374 static struct osc_extent *osc_extent_search(struct osc_object *obj,
375 pgoff_t index)
376 {
377 struct rb_node *n = obj->oo_root.rb_node;
378 struct osc_extent *tmp, *p = NULL;
379
380 LASSERT(osc_object_is_locked(obj));
381 while (n != NULL) {
382 tmp = rb_extent(n);
383 if (index < tmp->oe_start) {
384 n = n->rb_left;
385 } else if (index > tmp->oe_end) {
386 p = rb_extent(n);
387 n = n->rb_right;
388 } else {
389 return tmp;
390 }
391 }
392 return p;
393 }
394
395 /*
396 * Return the extent covering @index, otherwise return NULL.
397 * caller must have held object lock.
398 */
399 static struct osc_extent *osc_extent_lookup(struct osc_object *obj,
400 pgoff_t index)
401 {
402 struct osc_extent *ext;
403
404 ext = osc_extent_search(obj, index);
405 if (ext != NULL && ext->oe_start <= index && index <= ext->oe_end)
406 return osc_extent_get(ext);
407 return NULL;
408 }
409
410 /* caller must have held object lock. */
411 static void osc_extent_insert(struct osc_object *obj, struct osc_extent *ext)
412 {
413 struct rb_node **n = &obj->oo_root.rb_node;
414 struct rb_node *parent = NULL;
415 struct osc_extent *tmp;
416
417 LASSERT(ext->oe_intree == 0);
418 LASSERT(ext->oe_obj == obj);
419 LASSERT(osc_object_is_locked(obj));
420 while (*n != NULL) {
421 tmp = rb_extent(*n);
422 parent = *n;
423
424 if (ext->oe_end < tmp->oe_start)
425 n = &(*n)->rb_left;
426 else if (ext->oe_start > tmp->oe_end)
427 n = &(*n)->rb_right;
428 else
429 EASSERTF(0, tmp, EXTSTR, EXTPARA(ext));
430 }
431 rb_link_node(&ext->oe_node, parent, n);
432 rb_insert_color(&ext->oe_node, &obj->oo_root);
433 osc_extent_get(ext);
434 ext->oe_intree = 1;
435 }
436
437 /* caller must have held object lock. */
438 static void osc_extent_erase(struct osc_extent *ext)
439 {
440 struct osc_object *obj = ext->oe_obj;
441 LASSERT(osc_object_is_locked(obj));
442 if (ext->oe_intree) {
443 rb_erase(&ext->oe_node, &obj->oo_root);
444 ext->oe_intree = 0;
445 /* rbtree held a refcount */
446 osc_extent_put_trust(ext);
447 }
448 }
449
450 static struct osc_extent *osc_extent_hold(struct osc_extent *ext)
451 {
452 struct osc_object *obj = ext->oe_obj;
453
454 LASSERT(osc_object_is_locked(obj));
455 LASSERT(ext->oe_state == OES_ACTIVE || ext->oe_state == OES_CACHE);
456 if (ext->oe_state == OES_CACHE) {
457 osc_extent_state_set(ext, OES_ACTIVE);
458 osc_update_pending(obj, OBD_BRW_WRITE, -ext->oe_nr_pages);
459 }
460 atomic_inc(&ext->oe_users);
461 list_del_init(&ext->oe_link);
462 return osc_extent_get(ext);
463 }
464
465 static void __osc_extent_remove(struct osc_extent *ext)
466 {
467 LASSERT(osc_object_is_locked(ext->oe_obj));
468 LASSERT(list_empty(&ext->oe_pages));
469 osc_extent_erase(ext);
470 list_del_init(&ext->oe_link);
471 osc_extent_state_set(ext, OES_INV);
472 OSC_EXTENT_DUMP(D_CACHE, ext, "destroyed.\n");
473 }
474
475 static void osc_extent_remove(struct osc_extent *ext)
476 {
477 struct osc_object *obj = ext->oe_obj;
478
479 osc_object_lock(obj);
480 __osc_extent_remove(ext);
481 osc_object_unlock(obj);
482 }
483
484 /**
485 * This function is used to merge extents to get better performance. It checks
486 * if @cur and @victim are contiguous at chunk level.
487 */
488 static int osc_extent_merge(const struct lu_env *env, struct osc_extent *cur,
489 struct osc_extent *victim)
490 {
491 struct osc_object *obj = cur->oe_obj;
492 pgoff_t chunk_start;
493 pgoff_t chunk_end;
494 int ppc_bits;
495
496 LASSERT(cur->oe_state == OES_CACHE);
497 LASSERT(osc_object_is_locked(obj));
498 if (victim == NULL)
499 return -EINVAL;
500
501 if (victim->oe_state != OES_CACHE || victim->oe_fsync_wait)
502 return -EBUSY;
503
504 if (cur->oe_max_end != victim->oe_max_end)
505 return -ERANGE;
506
507 LASSERT(cur->oe_osclock == victim->oe_osclock);
508 ppc_bits = osc_cli(obj)->cl_chunkbits - PAGE_CACHE_SHIFT;
509 chunk_start = cur->oe_start >> ppc_bits;
510 chunk_end = cur->oe_end >> ppc_bits;
511 if (chunk_start != (victim->oe_end >> ppc_bits) + 1 &&
512 chunk_end + 1 != victim->oe_start >> ppc_bits)
513 return -ERANGE;
514
515 OSC_EXTENT_DUMP(D_CACHE, victim, "will be merged by %p.\n", cur);
516
517 cur->oe_start = min(cur->oe_start, victim->oe_start);
518 cur->oe_end = max(cur->oe_end, victim->oe_end);
519 cur->oe_grants += victim->oe_grants;
520 cur->oe_nr_pages += victim->oe_nr_pages;
521 /* only the following bits are needed to merge */
522 cur->oe_urgent |= victim->oe_urgent;
523 cur->oe_memalloc |= victim->oe_memalloc;
524 list_splice_init(&victim->oe_pages, &cur->oe_pages);
525 list_del_init(&victim->oe_link);
526 victim->oe_nr_pages = 0;
527
528 osc_extent_get(victim);
529 __osc_extent_remove(victim);
530 osc_extent_put(env, victim);
531
532 OSC_EXTENT_DUMP(D_CACHE, cur, "after merging %p.\n", victim);
533 return 0;
534 }
535
536 /**
537 * Drop user count of osc_extent, and unplug IO asynchronously.
538 */
539 int osc_extent_release(const struct lu_env *env, struct osc_extent *ext)
540 {
541 struct osc_object *obj = ext->oe_obj;
542 int rc = 0;
543
544 LASSERT(atomic_read(&ext->oe_users) > 0);
545 LASSERT(sanity_check(ext) == 0);
546 LASSERT(ext->oe_grants > 0);
547
548 if (atomic_dec_and_lock(&ext->oe_users, &obj->oo_lock)) {
549 LASSERT(ext->oe_state == OES_ACTIVE);
550 if (ext->oe_trunc_pending) {
551 /* a truncate process is waiting for this extent.
552 * This may happen due to a race, check
553 * osc_cache_truncate_start(). */
554 osc_extent_state_set(ext, OES_TRUNC);
555 ext->oe_trunc_pending = 0;
556 } else {
557 osc_extent_state_set(ext, OES_CACHE);
558 osc_update_pending(obj, OBD_BRW_WRITE,
559 ext->oe_nr_pages);
560
561 /* try to merge the previous and next extent. */
562 osc_extent_merge(env, ext, prev_extent(ext));
563 osc_extent_merge(env, ext, next_extent(ext));
564
565 if (ext->oe_urgent)
566 list_move_tail(&ext->oe_link,
567 &obj->oo_urgent_exts);
568 }
569 osc_object_unlock(obj);
570
571 osc_io_unplug_async(env, osc_cli(obj), obj);
572 }
573 osc_extent_put(env, ext);
574 return rc;
575 }
576
577 static inline int overlapped(struct osc_extent *ex1, struct osc_extent *ex2)
578 {
579 return !(ex1->oe_end < ex2->oe_start || ex2->oe_end < ex1->oe_start);
580 }
581
582 /**
583 * Find or create an extent which includes @index, core function to manage
584 * extent tree.
585 */
586 struct osc_extent *osc_extent_find(const struct lu_env *env,
587 struct osc_object *obj, pgoff_t index,
588 int *grants)
589
590 {
591 struct client_obd *cli = osc_cli(obj);
592 struct cl_lock *lock;
593 struct osc_extent *cur;
594 struct osc_extent *ext;
595 struct osc_extent *conflict = NULL;
596 struct osc_extent *found = NULL;
597 pgoff_t chunk;
598 pgoff_t max_end;
599 int max_pages; /* max_pages_per_rpc */
600 int chunksize;
601 int ppc_bits; /* pages per chunk bits */
602 int chunk_mask;
603 int rc;
604
605 cur = osc_extent_alloc(obj);
606 if (cur == NULL)
607 return ERR_PTR(-ENOMEM);
608
609 lock = cl_lock_at_pgoff(env, osc2cl(obj), index, NULL, 1, 0);
610 LASSERT(lock != NULL);
611 LASSERT(lock->cll_descr.cld_mode >= CLM_WRITE);
612
613 LASSERT(cli->cl_chunkbits >= PAGE_CACHE_SHIFT);
614 ppc_bits = cli->cl_chunkbits - PAGE_CACHE_SHIFT;
615 chunk_mask = ~((1 << ppc_bits) - 1);
616 chunksize = 1 << cli->cl_chunkbits;
617 chunk = index >> ppc_bits;
618
619 /* align end to rpc edge, rpc size may not be a power 2 integer. */
620 max_pages = cli->cl_max_pages_per_rpc;
621 LASSERT((max_pages & ~chunk_mask) == 0);
622 max_end = index - (index % max_pages) + max_pages - 1;
623 max_end = min_t(pgoff_t, max_end, lock->cll_descr.cld_end);
624
625 /* initialize new extent by parameters so far */
626 cur->oe_max_end = max_end;
627 cur->oe_start = index & chunk_mask;
628 cur->oe_end = ((index + ~chunk_mask + 1) & chunk_mask) - 1;
629 if (cur->oe_start < lock->cll_descr.cld_start)
630 cur->oe_start = lock->cll_descr.cld_start;
631 if (cur->oe_end > max_end)
632 cur->oe_end = max_end;
633 cur->oe_osclock = lock;
634 cur->oe_grants = 0;
635 cur->oe_mppr = max_pages;
636
637 /* grants has been allocated by caller */
638 LASSERTF(*grants >= chunksize + cli->cl_extent_tax,
639 "%u/%u/%u.\n", *grants, chunksize, cli->cl_extent_tax);
640 LASSERTF((max_end - cur->oe_start) < max_pages, EXTSTR, EXTPARA(cur));
641
642 restart:
643 osc_object_lock(obj);
644 ext = osc_extent_search(obj, cur->oe_start);
645 if (ext == NULL)
646 ext = first_extent(obj);
647 while (ext != NULL) {
648 loff_t ext_chk_start = ext->oe_start >> ppc_bits;
649 loff_t ext_chk_end = ext->oe_end >> ppc_bits;
650
651 LASSERT(sanity_check_nolock(ext) == 0);
652 if (chunk > ext_chk_end + 1)
653 break;
654
655 /* if covering by different locks, no chance to match */
656 if (lock != ext->oe_osclock) {
657 EASSERTF(!overlapped(ext, cur), ext,
658 EXTSTR, EXTPARA(cur));
659
660 ext = next_extent(ext);
661 continue;
662 }
663
664 /* discontiguous chunks? */
665 if (chunk + 1 < ext_chk_start) {
666 ext = next_extent(ext);
667 continue;
668 }
669
670 /* ok, from now on, ext and cur have these attrs:
671 * 1. covered by the same lock
672 * 2. contiguous at chunk level or overlapping. */
673
674 if (overlapped(ext, cur)) {
675 /* cur is the minimum unit, so overlapping means
676 * full contain. */
677 EASSERTF((ext->oe_start <= cur->oe_start &&
678 ext->oe_end >= cur->oe_end),
679 ext, EXTSTR, EXTPARA(cur));
680
681 if (ext->oe_state > OES_CACHE || ext->oe_fsync_wait) {
682 /* for simplicity, we wait for this extent to
683 * finish before going forward. */
684 conflict = osc_extent_get(ext);
685 break;
686 }
687
688 found = osc_extent_hold(ext);
689 break;
690 }
691
692 /* non-overlapped extent */
693 if (ext->oe_state != OES_CACHE || ext->oe_fsync_wait) {
694 /* we can't do anything for a non OES_CACHE extent, or
695 * if there is someone waiting for this extent to be
696 * flushed, try next one. */
697 ext = next_extent(ext);
698 continue;
699 }
700
701 /* check if they belong to the same rpc slot before trying to
702 * merge. the extents are not overlapped and contiguous at
703 * chunk level to get here. */
704 if (ext->oe_max_end != max_end) {
705 /* if they don't belong to the same RPC slot or
706 * max_pages_per_rpc has ever changed, do not merge. */
707 ext = next_extent(ext);
708 continue;
709 }
710
711 /* it's required that an extent must be contiguous at chunk
712 * level so that we know the whole extent is covered by grant
713 * (the pages in the extent are NOT required to be contiguous).
714 * Otherwise, it will be too much difficult to know which
715 * chunks have grants allocated. */
716
717 /* try to do front merge - extend ext's start */
718 if (chunk + 1 == ext_chk_start) {
719 /* ext must be chunk size aligned */
720 EASSERT((ext->oe_start & ~chunk_mask) == 0, ext);
721
722 /* pull ext's start back to cover cur */
723 ext->oe_start = cur->oe_start;
724 ext->oe_grants += chunksize;
725 *grants -= chunksize;
726
727 found = osc_extent_hold(ext);
728 } else if (chunk == ext_chk_end + 1) {
729 /* rear merge */
730 ext->oe_end = cur->oe_end;
731 ext->oe_grants += chunksize;
732 *grants -= chunksize;
733
734 /* try to merge with the next one because we just fill
735 * in a gap */
736 if (osc_extent_merge(env, ext, next_extent(ext)) == 0)
737 /* we can save extent tax from next extent */
738 *grants += cli->cl_extent_tax;
739
740 found = osc_extent_hold(ext);
741 }
742 if (found != NULL)
743 break;
744
745 ext = next_extent(ext);
746 }
747
748 osc_extent_tree_dump(D_CACHE, obj);
749 if (found != NULL) {
750 LASSERT(conflict == NULL);
751 if (!IS_ERR(found)) {
752 LASSERT(found->oe_osclock == cur->oe_osclock);
753 OSC_EXTENT_DUMP(D_CACHE, found,
754 "found caching ext for %lu.\n", index);
755 }
756 } else if (conflict == NULL) {
757 /* create a new extent */
758 EASSERT(osc_extent_is_overlapped(obj, cur) == 0, cur);
759 cur->oe_grants = chunksize + cli->cl_extent_tax;
760 *grants -= cur->oe_grants;
761 LASSERT(*grants >= 0);
762
763 cur->oe_state = OES_CACHE;
764 found = osc_extent_hold(cur);
765 osc_extent_insert(obj, cur);
766 OSC_EXTENT_DUMP(D_CACHE, cur, "add into tree %lu/%lu.\n",
767 index, lock->cll_descr.cld_end);
768 }
769 osc_object_unlock(obj);
770
771 if (conflict != NULL) {
772 LASSERT(found == NULL);
773
774 /* waiting for IO to finish. Please notice that it's impossible
775 * to be an OES_TRUNC extent. */
776 rc = osc_extent_wait(env, conflict, OES_INV);
777 osc_extent_put(env, conflict);
778 conflict = NULL;
779 if (rc < 0)
780 GOTO(out, found = ERR_PTR(rc));
781
782 goto restart;
783 }
784
785 out:
786 osc_extent_put(env, cur);
787 LASSERT(*grants >= 0);
788 return found;
789 }
790
791 /**
792 * Called when IO is finished to an extent.
793 */
794 int osc_extent_finish(const struct lu_env *env, struct osc_extent *ext,
795 int sent, int rc)
796 {
797 struct client_obd *cli = osc_cli(ext->oe_obj);
798 struct osc_async_page *oap;
799 struct osc_async_page *tmp;
800 int nr_pages = ext->oe_nr_pages;
801 int lost_grant = 0;
802 int blocksize = cli->cl_import->imp_obd->obd_osfs.os_bsize ? : 4096;
803 __u64 last_off = 0;
804 int last_count = -1;
805
806 OSC_EXTENT_DUMP(D_CACHE, ext, "extent finished.\n");
807
808 ext->oe_rc = rc ?: ext->oe_nr_pages;
809 EASSERT(ergo(rc == 0, ext->oe_state == OES_RPC), ext);
810 list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
811 oap_pending_item) {
812 list_del_init(&oap->oap_rpc_item);
813 list_del_init(&oap->oap_pending_item);
814 if (last_off <= oap->oap_obj_off) {
815 last_off = oap->oap_obj_off;
816 last_count = oap->oap_count;
817 }
818
819 --ext->oe_nr_pages;
820 osc_ap_completion(env, cli, oap, sent, rc);
821 }
822 EASSERT(ext->oe_nr_pages == 0, ext);
823
824 if (!sent) {
825 lost_grant = ext->oe_grants;
826 } else if (blocksize < PAGE_CACHE_SIZE &&
827 last_count != PAGE_CACHE_SIZE) {
828 /* For short writes we shouldn't count parts of pages that
829 * span a whole chunk on the OST side, or our accounting goes
830 * wrong. Should match the code in filter_grant_check. */
831 int offset = oap->oap_page_off & ~CFS_PAGE_MASK;
832 int count = oap->oap_count + (offset & (blocksize - 1));
833 int end = (offset + oap->oap_count) & (blocksize - 1);
834 if (end)
835 count += blocksize - end;
836
837 lost_grant = PAGE_CACHE_SIZE - count;
838 }
839 if (ext->oe_grants > 0)
840 osc_free_grant(cli, nr_pages, lost_grant);
841
842 osc_extent_remove(ext);
843 /* put the refcount for RPC */
844 osc_extent_put(env, ext);
845 return 0;
846 }
847
848 static int extent_wait_cb(struct osc_extent *ext, int state)
849 {
850 int ret;
851
852 osc_object_lock(ext->oe_obj);
853 ret = ext->oe_state == state;
854 osc_object_unlock(ext->oe_obj);
855
856 return ret;
857 }
858
859 /**
860 * Wait for the extent's state to become @state.
861 */
862 static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
863 int state)
864 {
865 struct osc_object *obj = ext->oe_obj;
866 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL,
867 LWI_ON_SIGNAL_NOOP, NULL);
868 int rc = 0;
869
870 osc_object_lock(obj);
871 LASSERT(sanity_check_nolock(ext) == 0);
872 /* `Kick' this extent only if the caller is waiting for it to be
873 * written out. */
874 if (state == OES_INV && !ext->oe_urgent && !ext->oe_hp) {
875 if (ext->oe_state == OES_ACTIVE) {
876 ext->oe_urgent = 1;
877 } else if (ext->oe_state == OES_CACHE) {
878 ext->oe_urgent = 1;
879 osc_extent_hold(ext);
880 rc = 1;
881 }
882 }
883 osc_object_unlock(obj);
884 if (rc == 1)
885 osc_extent_release(env, ext);
886
887 /* wait for the extent until its state becomes @state */
888 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), &lwi);
889 if (rc == -ETIMEDOUT) {
890 OSC_EXTENT_DUMP(D_ERROR, ext,
891 "%s: wait ext to %d timedout, recovery in progress?\n",
892 osc_export(obj)->exp_obd->obd_name, state);
893
894 lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
895 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state),
896 &lwi);
897 }
898 if (rc == 0 && ext->oe_rc < 0)
899 rc = ext->oe_rc;
900 return rc;
901 }
902
903 /**
904 * Discard pages with index greater than @size. If @ext is overlapped with
905 * @size, then partial truncate happens.
906 */
907 static int osc_extent_truncate(struct osc_extent *ext, pgoff_t trunc_index,
908 bool partial)
909 {
910 struct cl_env_nest nest;
911 struct lu_env *env;
912 struct cl_io *io;
913 struct osc_object *obj = ext->oe_obj;
914 struct client_obd *cli = osc_cli(obj);
915 struct osc_async_page *oap;
916 struct osc_async_page *tmp;
917 int pages_in_chunk = 0;
918 int ppc_bits = cli->cl_chunkbits - PAGE_CACHE_SHIFT;
919 __u64 trunc_chunk = trunc_index >> ppc_bits;
920 int grants = 0;
921 int nr_pages = 0;
922 int rc = 0;
923
924 LASSERT(sanity_check(ext) == 0);
925 LASSERT(ext->oe_state == OES_TRUNC);
926 LASSERT(!ext->oe_urgent);
927
928 /* Request new lu_env.
929 * We can't use that env from osc_cache_truncate_start() because
930 * it's from lov_io_sub and not fully initialized. */
931 env = cl_env_nested_get(&nest);
932 io = &osc_env_info(env)->oti_io;
933 io->ci_obj = cl_object_top(osc2cl(obj));
934 rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
935 if (rc < 0)
936 GOTO(out, rc);
937
938 /* discard all pages with index greater then trunc_index */
939 list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
940 oap_pending_item) {
941 struct cl_page *sub = oap2cl_page(oap);
942 struct cl_page *page = cl_page_top(sub);
943
944 LASSERT(list_empty(&oap->oap_rpc_item));
945
946 /* only discard the pages with their index greater than
947 * trunc_index, and ... */
948 if (sub->cp_index < trunc_index ||
949 (sub->cp_index == trunc_index && partial)) {
950 /* accounting how many pages remaining in the chunk
951 * so that we can calculate grants correctly. */
952 if (sub->cp_index >> ppc_bits == trunc_chunk)
953 ++pages_in_chunk;
954 continue;
955 }
956
957 list_del_init(&oap->oap_pending_item);
958
959 cl_page_get(page);
960 lu_ref_add(&page->cp_reference, "truncate", current);
961
962 if (cl_page_own(env, io, page) == 0) {
963 cl_page_unmap(env, io, page);
964 cl_page_discard(env, io, page);
965 cl_page_disown(env, io, page);
966 } else {
967 LASSERT(page->cp_state == CPS_FREEING);
968 LASSERT(0);
969 }
970
971 lu_ref_del(&page->cp_reference, "truncate", current);
972 cl_page_put(env, page);
973
974 --ext->oe_nr_pages;
975 ++nr_pages;
976 }
977 EASSERTF(ergo(ext->oe_start >= trunc_index + !!partial,
978 ext->oe_nr_pages == 0),
979 ext, "trunc_index %lu, partial %d\n", trunc_index, partial);
980
981 osc_object_lock(obj);
982 if (ext->oe_nr_pages == 0) {
983 LASSERT(pages_in_chunk == 0);
984 grants = ext->oe_grants;
985 ext->oe_grants = 0;
986 } else { /* calculate how many grants we can free */
987 int chunks = (ext->oe_end >> ppc_bits) - trunc_chunk;
988 pgoff_t last_index;
989
990
991 /* if there is no pages in this chunk, we can also free grants
992 * for the last chunk */
993 if (pages_in_chunk == 0) {
994 /* if this is the 1st chunk and no pages in this chunk,
995 * ext->oe_nr_pages must be zero, so we should be in
996 * the other if-clause. */
997 LASSERT(trunc_chunk > 0);
998 --trunc_chunk;
999 ++chunks;
1000 }
1001
1002 /* this is what we can free from this extent */
1003 grants = chunks << cli->cl_chunkbits;
1004 ext->oe_grants -= grants;
1005 last_index = ((trunc_chunk + 1) << ppc_bits) - 1;
1006 ext->oe_end = min(last_index, ext->oe_max_end);
1007 LASSERT(ext->oe_end >= ext->oe_start);
1008 LASSERT(ext->oe_grants > 0);
1009 }
1010 osc_object_unlock(obj);
1011
1012 if (grants > 0 || nr_pages > 0)
1013 osc_free_grant(cli, nr_pages, grants);
1014
1015 out:
1016 cl_io_fini(env, io);
1017 cl_env_nested_put(&nest, env);
1018 return rc;
1019 }
1020
1021 /**
1022 * This function is used to make the extent prepared for transfer.
1023 * A race with flusing page - ll_writepage() has to be handled cautiously.
1024 */
1025 static int osc_extent_make_ready(const struct lu_env *env,
1026 struct osc_extent *ext)
1027 {
1028 struct osc_async_page *oap;
1029 struct osc_async_page *last = NULL;
1030 struct osc_object *obj = ext->oe_obj;
1031 int page_count = 0;
1032 int rc;
1033
1034 /* we're going to grab page lock, so object lock must not be taken. */
1035 LASSERT(sanity_check(ext) == 0);
1036 /* in locking state, any process should not touch this extent. */
1037 EASSERT(ext->oe_state == OES_LOCKING, ext);
1038 EASSERT(ext->oe_owner != NULL, ext);
1039
1040 OSC_EXTENT_DUMP(D_CACHE, ext, "make ready\n");
1041
1042 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1043 ++page_count;
1044 if (last == NULL || last->oap_obj_off < oap->oap_obj_off)
1045 last = oap;
1046
1047 /* checking ASYNC_READY is race safe */
1048 if ((oap->oap_async_flags & ASYNC_READY) != 0)
1049 continue;
1050
1051 rc = osc_make_ready(env, oap, OBD_BRW_WRITE);
1052 switch (rc) {
1053 case 0:
1054 spin_lock(&oap->oap_lock);
1055 oap->oap_async_flags |= ASYNC_READY;
1056 spin_unlock(&oap->oap_lock);
1057 break;
1058 case -EALREADY:
1059 LASSERT((oap->oap_async_flags & ASYNC_READY) != 0);
1060 break;
1061 default:
1062 LASSERTF(0, "unknown return code: %d\n", rc);
1063 }
1064 }
1065
1066 LASSERT(page_count == ext->oe_nr_pages);
1067 LASSERT(last != NULL);
1068 /* the last page is the only one we need to refresh its count by
1069 * the size of file. */
1070 if (!(last->oap_async_flags & ASYNC_COUNT_STABLE)) {
1071 last->oap_count = osc_refresh_count(env, last, OBD_BRW_WRITE);
1072 LASSERT(last->oap_count > 0);
1073 LASSERT(last->oap_page_off + last->oap_count <= PAGE_CACHE_SIZE);
1074 last->oap_async_flags |= ASYNC_COUNT_STABLE;
1075 }
1076
1077 /* for the rest of pages, we don't need to call osf_refresh_count()
1078 * because it's known they are not the last page */
1079 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1080 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE)) {
1081 oap->oap_count = PAGE_CACHE_SIZE - oap->oap_page_off;
1082 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1083 }
1084 }
1085
1086 osc_object_lock(obj);
1087 osc_extent_state_set(ext, OES_RPC);
1088 osc_object_unlock(obj);
1089 /* get a refcount for RPC. */
1090 osc_extent_get(ext);
1091
1092 return 0;
1093 }
1094
1095 /**
1096 * Quick and simple version of osc_extent_find(). This function is frequently
1097 * called to expand the extent for the same IO. To expand the extent, the
1098 * page index must be in the same or next chunk of ext->oe_end.
1099 */
1100 static int osc_extent_expand(struct osc_extent *ext, pgoff_t index, int *grants)
1101 {
1102 struct osc_object *obj = ext->oe_obj;
1103 struct client_obd *cli = osc_cli(obj);
1104 struct osc_extent *next;
1105 int ppc_bits = cli->cl_chunkbits - PAGE_CACHE_SHIFT;
1106 pgoff_t chunk = index >> ppc_bits;
1107 pgoff_t end_chunk;
1108 pgoff_t end_index;
1109 int chunksize = 1 << cli->cl_chunkbits;
1110 int rc = 0;
1111
1112 LASSERT(ext->oe_max_end >= index && ext->oe_start <= index);
1113 osc_object_lock(obj);
1114 LASSERT(sanity_check_nolock(ext) == 0);
1115 end_chunk = ext->oe_end >> ppc_bits;
1116 if (chunk > end_chunk + 1)
1117 GOTO(out, rc = -ERANGE);
1118
1119 if (end_chunk >= chunk)
1120 GOTO(out, rc = 0);
1121
1122 LASSERT(end_chunk + 1 == chunk);
1123 /* try to expand this extent to cover @index */
1124 end_index = min(ext->oe_max_end, ((chunk + 1) << ppc_bits) - 1);
1125
1126 next = next_extent(ext);
1127 if (next != NULL && next->oe_start <= end_index)
1128 /* complex mode - overlapped with the next extent,
1129 * this case will be handled by osc_extent_find() */
1130 GOTO(out, rc = -EAGAIN);
1131
1132 ext->oe_end = end_index;
1133 ext->oe_grants += chunksize;
1134 *grants -= chunksize;
1135 LASSERT(*grants >= 0);
1136 EASSERTF(osc_extent_is_overlapped(obj, ext) == 0, ext,
1137 "overlapped after expanding for %lu.\n", index);
1138
1139 out:
1140 osc_object_unlock(obj);
1141 return rc;
1142 }
1143
1144 static void osc_extent_tree_dump0(int level, struct osc_object *obj,
1145 const char *func, int line)
1146 {
1147 struct osc_extent *ext;
1148 int cnt;
1149
1150 CDEBUG(level, "Dump object %p extents at %s:%d, mppr: %u.\n",
1151 obj, func, line, osc_cli(obj)->cl_max_pages_per_rpc);
1152
1153 /* osc_object_lock(obj); */
1154 cnt = 1;
1155 for (ext = first_extent(obj); ext != NULL; ext = next_extent(ext))
1156 OSC_EXTENT_DUMP(level, ext, "in tree %d.\n", cnt++);
1157
1158 cnt = 1;
1159 list_for_each_entry(ext, &obj->oo_hp_exts, oe_link)
1160 OSC_EXTENT_DUMP(level, ext, "hp %d.\n", cnt++);
1161
1162 cnt = 1;
1163 list_for_each_entry(ext, &obj->oo_urgent_exts, oe_link)
1164 OSC_EXTENT_DUMP(level, ext, "urgent %d.\n", cnt++);
1165
1166 cnt = 1;
1167 list_for_each_entry(ext, &obj->oo_reading_exts, oe_link)
1168 OSC_EXTENT_DUMP(level, ext, "reading %d.\n", cnt++);
1169 /* osc_object_unlock(obj); */
1170 }
1171
1172 /* ------------------ osc extent end ------------------ */
1173
1174 static inline int osc_is_ready(struct osc_object *osc)
1175 {
1176 return !list_empty(&osc->oo_ready_item) ||
1177 !list_empty(&osc->oo_hp_ready_item);
1178 }
1179
1180 #define OSC_IO_DEBUG(OSC, STR, args...) \
1181 CDEBUG(D_CACHE, "obj %p ready %d|%c|%c wr %d|%c|%c rd %d|%c " STR, \
1182 (OSC), osc_is_ready(OSC), \
1183 list_empty_marker(&(OSC)->oo_hp_ready_item), \
1184 list_empty_marker(&(OSC)->oo_ready_item), \
1185 atomic_read(&(OSC)->oo_nr_writes), \
1186 list_empty_marker(&(OSC)->oo_hp_exts), \
1187 list_empty_marker(&(OSC)->oo_urgent_exts), \
1188 atomic_read(&(OSC)->oo_nr_reads), \
1189 list_empty_marker(&(OSC)->oo_reading_exts), \
1190 ##args)
1191
1192 static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
1193 int cmd)
1194 {
1195 struct osc_page *opg = oap2osc_page(oap);
1196 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1197 int result;
1198
1199 LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
1200
1201 result = cl_page_make_ready(env, page, CRT_WRITE);
1202 if (result == 0)
1203 opg->ops_submit_time = cfs_time_current();
1204 return result;
1205 }
1206
1207 static int osc_refresh_count(const struct lu_env *env,
1208 struct osc_async_page *oap, int cmd)
1209 {
1210 struct osc_page *opg = oap2osc_page(oap);
1211 struct cl_page *page = oap2cl_page(oap);
1212 struct cl_object *obj;
1213 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
1214
1215 int result;
1216 loff_t kms;
1217
1218 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
1219 LASSERT(!(cmd & OBD_BRW_READ));
1220 LASSERT(opg != NULL);
1221 obj = opg->ops_cl.cpl_obj;
1222
1223 cl_object_attr_lock(obj);
1224 result = cl_object_attr_get(env, obj, attr);
1225 cl_object_attr_unlock(obj);
1226 if (result < 0)
1227 return result;
1228 kms = attr->cat_kms;
1229 if (cl_offset(obj, page->cp_index) >= kms)
1230 /* catch race with truncate */
1231 return 0;
1232 else if (cl_offset(obj, page->cp_index + 1) > kms)
1233 /* catch sub-page write at end of file */
1234 return kms % PAGE_CACHE_SIZE;
1235 else
1236 return PAGE_CACHE_SIZE;
1237 }
1238
1239 static int osc_completion(const struct lu_env *env, struct osc_async_page *oap,
1240 int cmd, int rc)
1241 {
1242 struct osc_page *opg = oap2osc_page(oap);
1243 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1244 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
1245 enum cl_req_type crt;
1246 int srvlock;
1247
1248 cmd &= ~OBD_BRW_NOQUOTA;
1249 LASSERT(equi(page->cp_state == CPS_PAGEIN, cmd == OBD_BRW_READ));
1250 LASSERT(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE));
1251 LASSERT(opg->ops_transfer_pinned);
1252
1253 /*
1254 * page->cp_req can be NULL if io submission failed before
1255 * cl_req was allocated.
1256 */
1257 if (page->cp_req != NULL)
1258 cl_req_page_done(env, page);
1259 LASSERT(page->cp_req == NULL);
1260
1261 crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
1262 /* Clear opg->ops_transfer_pinned before VM lock is released. */
1263 opg->ops_transfer_pinned = 0;
1264
1265 spin_lock(&obj->oo_seatbelt);
1266 LASSERT(opg->ops_submitter != NULL);
1267 LASSERT(!list_empty(&opg->ops_inflight));
1268 list_del_init(&opg->ops_inflight);
1269 opg->ops_submitter = NULL;
1270 spin_unlock(&obj->oo_seatbelt);
1271
1272 opg->ops_submit_time = 0;
1273 srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK;
1274
1275 /* statistic */
1276 if (rc == 0 && srvlock) {
1277 struct lu_device *ld = opg->ops_cl.cpl_obj->co_lu.lo_dev;
1278 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
1279 int bytes = oap->oap_count;
1280
1281 if (crt == CRT_READ)
1282 stats->os_lockless_reads += bytes;
1283 else
1284 stats->os_lockless_writes += bytes;
1285 }
1286
1287 /*
1288 * This has to be the last operation with the page, as locks are
1289 * released in cl_page_completion() and nothing except for the
1290 * reference counter protects page from concurrent reclaim.
1291 */
1292 lu_ref_del(&page->cp_reference, "transfer", page);
1293
1294 cl_page_completion(env, page, crt, rc);
1295
1296 return 0;
1297 }
1298
1299 #define OSC_DUMP_GRANT(cli, fmt, args...) do { \
1300 struct client_obd *__tmp = (cli); \
1301 CDEBUG(D_CACHE, "%s: { dirty: %ld/%ld dirty_pages: %d/%d " \
1302 "dropped: %ld avail: %ld, reserved: %ld, flight: %d } " fmt, \
1303 __tmp->cl_import->imp_obd->obd_name, \
1304 __tmp->cl_dirty, __tmp->cl_dirty_max, \
1305 atomic_read(&obd_dirty_pages), obd_max_dirty_pages, \
1306 __tmp->cl_lost_grant, __tmp->cl_avail_grant, \
1307 __tmp->cl_reserved_grant, __tmp->cl_w_in_flight, ##args); \
1308 } while (0)
1309
1310 /* caller must hold loi_list_lock */
1311 static void osc_consume_write_grant(struct client_obd *cli,
1312 struct brw_page *pga)
1313 {
1314 LASSERT(spin_is_locked(&cli->cl_loi_list_lock.lock));
1315 LASSERT(!(pga->flag & OBD_BRW_FROM_GRANT));
1316 atomic_inc(&obd_dirty_pages);
1317 cli->cl_dirty += PAGE_CACHE_SIZE;
1318 pga->flag |= OBD_BRW_FROM_GRANT;
1319 CDEBUG(D_CACHE, "using %lu grant credits for brw %p page %p\n",
1320 PAGE_CACHE_SIZE, pga, pga->pg);
1321 osc_update_next_shrink(cli);
1322 }
1323
1324 /* the companion to osc_consume_write_grant, called when a brw has completed.
1325 * must be called with the loi lock held. */
1326 static void osc_release_write_grant(struct client_obd *cli,
1327 struct brw_page *pga)
1328 {
1329 LASSERT(spin_is_locked(&cli->cl_loi_list_lock.lock));
1330 if (!(pga->flag & OBD_BRW_FROM_GRANT)) {
1331 return;
1332 }
1333
1334 pga->flag &= ~OBD_BRW_FROM_GRANT;
1335 atomic_dec(&obd_dirty_pages);
1336 cli->cl_dirty -= PAGE_CACHE_SIZE;
1337 if (pga->flag & OBD_BRW_NOCACHE) {
1338 pga->flag &= ~OBD_BRW_NOCACHE;
1339 atomic_dec(&obd_dirty_transit_pages);
1340 cli->cl_dirty_transit -= PAGE_CACHE_SIZE;
1341 }
1342 }
1343
1344 /**
1345 * To avoid sleeping with object lock held, it's good for us allocate enough
1346 * grants before entering into critical section.
1347 *
1348 * client_obd_list_lock held by caller
1349 */
1350 static int osc_reserve_grant(struct client_obd *cli, unsigned int bytes)
1351 {
1352 int rc = -EDQUOT;
1353
1354 if (cli->cl_avail_grant >= bytes) {
1355 cli->cl_avail_grant -= bytes;
1356 cli->cl_reserved_grant += bytes;
1357 rc = 0;
1358 }
1359 return rc;
1360 }
1361
1362 static void __osc_unreserve_grant(struct client_obd *cli,
1363 unsigned int reserved, unsigned int unused)
1364 {
1365 /* it's quite normal for us to get more grant than reserved.
1366 * Thinking about a case that two extents merged by adding a new
1367 * chunk, we can save one extent tax. If extent tax is greater than
1368 * one chunk, we can save more grant by adding a new chunk */
1369 cli->cl_reserved_grant -= reserved;
1370 if (unused > reserved) {
1371 cli->cl_avail_grant += reserved;
1372 cli->cl_lost_grant += unused - reserved;
1373 } else {
1374 cli->cl_avail_grant += unused;
1375 }
1376 }
1377
1378 void osc_unreserve_grant(struct client_obd *cli,
1379 unsigned int reserved, unsigned int unused)
1380 {
1381 client_obd_list_lock(&cli->cl_loi_list_lock);
1382 __osc_unreserve_grant(cli, reserved, unused);
1383 if (unused > 0)
1384 osc_wake_cache_waiters(cli);
1385 client_obd_list_unlock(&cli->cl_loi_list_lock);
1386 }
1387
1388 /**
1389 * Free grant after IO is finished or canceled.
1390 *
1391 * @lost_grant is used to remember how many grants we have allocated but not
1392 * used, we should return these grants to OST. There're two cases where grants
1393 * can be lost:
1394 * 1. truncate;
1395 * 2. blocksize at OST is less than PAGE_CACHE_SIZE and a partial page was
1396 * written. In this case OST may use less chunks to serve this partial
1397 * write. OSTs don't actually know the page size on the client side. so
1398 * clients have to calculate lost grant by the blocksize on the OST.
1399 * See filter_grant_check() for details.
1400 */
1401 static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
1402 unsigned int lost_grant)
1403 {
1404 int grant = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
1405
1406 client_obd_list_lock(&cli->cl_loi_list_lock);
1407 atomic_sub(nr_pages, &obd_dirty_pages);
1408 cli->cl_dirty -= nr_pages << PAGE_CACHE_SHIFT;
1409 cli->cl_lost_grant += lost_grant;
1410 if (cli->cl_avail_grant < grant && cli->cl_lost_grant >= grant) {
1411 /* borrow some grant from truncate to avoid the case that
1412 * truncate uses up all avail grant */
1413 cli->cl_lost_grant -= grant;
1414 cli->cl_avail_grant += grant;
1415 }
1416 osc_wake_cache_waiters(cli);
1417 client_obd_list_unlock(&cli->cl_loi_list_lock);
1418 CDEBUG(D_CACHE, "lost %u grant: %lu avail: %lu dirty: %lu\n",
1419 lost_grant, cli->cl_lost_grant,
1420 cli->cl_avail_grant, cli->cl_dirty);
1421 }
1422
1423 /**
1424 * The companion to osc_enter_cache(), called when @oap is no longer part of
1425 * the dirty accounting due to error.
1426 */
1427 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1428 {
1429 client_obd_list_lock(&cli->cl_loi_list_lock);
1430 osc_release_write_grant(cli, &oap->oap_brw_page);
1431 client_obd_list_unlock(&cli->cl_loi_list_lock);
1432 }
1433
1434 /**
1435 * Non-blocking version of osc_enter_cache() that consumes grant only when it
1436 * is available.
1437 */
1438 static int osc_enter_cache_try(struct client_obd *cli,
1439 struct osc_async_page *oap,
1440 int bytes, int transient)
1441 {
1442 int rc;
1443
1444 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1445
1446 rc = osc_reserve_grant(cli, bytes);
1447 if (rc < 0)
1448 return 0;
1449
1450 if (cli->cl_dirty + PAGE_CACHE_SIZE <= cli->cl_dirty_max &&
1451 atomic_read(&obd_dirty_pages) + 1 <= obd_max_dirty_pages) {
1452 osc_consume_write_grant(cli, &oap->oap_brw_page);
1453 if (transient) {
1454 cli->cl_dirty_transit += PAGE_CACHE_SIZE;
1455 atomic_inc(&obd_dirty_transit_pages);
1456 oap->oap_brw_flags |= OBD_BRW_NOCACHE;
1457 }
1458 rc = 1;
1459 } else {
1460 __osc_unreserve_grant(cli, bytes, bytes);
1461 rc = 0;
1462 }
1463 return rc;
1464 }
1465
1466 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1467 {
1468 int rc;
1469 client_obd_list_lock(&cli->cl_loi_list_lock);
1470 rc = list_empty(&ocw->ocw_entry);
1471 client_obd_list_unlock(&cli->cl_loi_list_lock);
1472 return rc;
1473 }
1474
1475 /**
1476 * The main entry to reserve dirty page accounting. Usually the grant reserved
1477 * in this function will be freed in bulk in osc_free_grant() unless it fails
1478 * to add osc cache, in that case, it will be freed in osc_exit_cache().
1479 *
1480 * The process will be put into sleep if it's already run out of grant.
1481 */
1482 static int osc_enter_cache(const struct lu_env *env, struct client_obd *cli,
1483 struct osc_async_page *oap, int bytes)
1484 {
1485 struct osc_object *osc = oap->oap_obj;
1486 struct lov_oinfo *loi = osc->oo_oinfo;
1487 struct osc_cache_waiter ocw;
1488 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1489 int rc = -EDQUOT;
1490
1491 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1492
1493 client_obd_list_lock(&cli->cl_loi_list_lock);
1494
1495 /* force the caller to try sync io. this can jump the list
1496 * of queued writes and create a discontiguous rpc stream */
1497 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_NO_GRANT) ||
1498 cli->cl_dirty_max < PAGE_CACHE_SIZE ||
1499 cli->cl_ar.ar_force_sync || loi->loi_ar.ar_force_sync)
1500 GOTO(out, rc = -EDQUOT);
1501
1502 /* Hopefully normal case - cache space and write credits available */
1503 if (osc_enter_cache_try(cli, oap, bytes, 0))
1504 GOTO(out, rc = 0);
1505
1506 /* We can get here for two reasons: too many dirty pages in cache, or
1507 * run out of grants. In both cases we should write dirty pages out.
1508 * Adding a cache waiter will trigger urgent write-out no matter what
1509 * RPC size will be.
1510 * The exiting condition is no avail grants and no dirty pages caching,
1511 * that really means there is no space on the OST. */
1512 init_waitqueue_head(&ocw.ocw_waitq);
1513 ocw.ocw_oap = oap;
1514 ocw.ocw_grant = bytes;
1515 while (cli->cl_dirty > 0 || cli->cl_w_in_flight > 0) {
1516 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1517 ocw.ocw_rc = 0;
1518 client_obd_list_unlock(&cli->cl_loi_list_lock);
1519
1520 osc_io_unplug_async(env, cli, NULL);
1521
1522 CDEBUG(D_CACHE, "%s: sleeping for cache space @ %p for %p\n",
1523 cli->cl_import->imp_obd->obd_name, &ocw, oap);
1524
1525 rc = l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1526
1527 client_obd_list_lock(&cli->cl_loi_list_lock);
1528
1529 /* l_wait_event is interrupted by signal */
1530 if (rc < 0) {
1531 list_del_init(&ocw.ocw_entry);
1532 GOTO(out, rc);
1533 }
1534
1535 LASSERT(list_empty(&ocw.ocw_entry));
1536 rc = ocw.ocw_rc;
1537
1538 if (rc != -EDQUOT)
1539 GOTO(out, rc);
1540 if (osc_enter_cache_try(cli, oap, bytes, 0))
1541 GOTO(out, rc = 0);
1542 }
1543 out:
1544 client_obd_list_unlock(&cli->cl_loi_list_lock);
1545 OSC_DUMP_GRANT(cli, "returned %d.\n", rc);
1546 return rc;
1547 }
1548
1549 /* caller must hold loi_list_lock */
1550 void osc_wake_cache_waiters(struct client_obd *cli)
1551 {
1552 struct list_head *l, *tmp;
1553 struct osc_cache_waiter *ocw;
1554
1555 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1556 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
1557 list_del_init(&ocw->ocw_entry);
1558
1559 ocw->ocw_rc = -EDQUOT;
1560 /* we can't dirty more */
1561 if ((cli->cl_dirty + PAGE_CACHE_SIZE > cli->cl_dirty_max) ||
1562 (atomic_read(&obd_dirty_pages) + 1 >
1563 obd_max_dirty_pages)) {
1564 CDEBUG(D_CACHE, "no dirty room: dirty: %ld "
1565 "osc max %ld, sys max %d\n", cli->cl_dirty,
1566 cli->cl_dirty_max, obd_max_dirty_pages);
1567 goto wakeup;
1568 }
1569
1570 ocw->ocw_rc = 0;
1571 if (!osc_enter_cache_try(cli, ocw->ocw_oap, ocw->ocw_grant, 0))
1572 ocw->ocw_rc = -EDQUOT;
1573
1574 wakeup:
1575 CDEBUG(D_CACHE, "wake up %p for oap %p, avail grant %ld, %d\n",
1576 ocw, ocw->ocw_oap, cli->cl_avail_grant, ocw->ocw_rc);
1577
1578 wake_up(&ocw->ocw_waitq);
1579 }
1580 }
1581
1582 static int osc_max_rpc_in_flight(struct client_obd *cli, struct osc_object *osc)
1583 {
1584 int hprpc = !!list_empty(&osc->oo_hp_exts);
1585 return rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight + hprpc;
1586 }
1587
1588 /* This maintains the lists of pending pages to read/write for a given object
1589 * (lop). This is used by osc_check_rpcs->osc_next_obj() and osc_list_maint()
1590 * to quickly find objects that are ready to send an RPC. */
1591 static int osc_makes_rpc(struct client_obd *cli, struct osc_object *osc,
1592 int cmd)
1593 {
1594 int invalid_import = 0;
1595
1596 /* if we have an invalid import we want to drain the queued pages
1597 * by forcing them through rpcs that immediately fail and complete
1598 * the pages. recovery relies on this to empty the queued pages
1599 * before canceling the locks and evicting down the llite pages */
1600 if ((cli->cl_import == NULL || cli->cl_import->imp_invalid))
1601 invalid_import = 1;
1602
1603 if (cmd & OBD_BRW_WRITE) {
1604 if (atomic_read(&osc->oo_nr_writes) == 0)
1605 return 0;
1606 if (invalid_import) {
1607 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1608 return 1;
1609 }
1610 if (!list_empty(&osc->oo_hp_exts)) {
1611 CDEBUG(D_CACHE, "high prio request forcing RPC\n");
1612 return 1;
1613 }
1614 if (!list_empty(&osc->oo_urgent_exts)) {
1615 CDEBUG(D_CACHE, "urgent request forcing RPC\n");
1616 return 1;
1617 }
1618 /* trigger a write rpc stream as long as there are dirtiers
1619 * waiting for space. as they're waiting, they're not going to
1620 * create more pages to coalesce with what's waiting.. */
1621 if (!list_empty(&cli->cl_cache_waiters)) {
1622 CDEBUG(D_CACHE, "cache waiters forcing RPC\n");
1623 return 1;
1624 }
1625 if (atomic_read(&osc->oo_nr_writes) >=
1626 cli->cl_max_pages_per_rpc)
1627 return 1;
1628 } else {
1629 if (atomic_read(&osc->oo_nr_reads) == 0)
1630 return 0;
1631 if (invalid_import) {
1632 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1633 return 1;
1634 }
1635 /* all read are urgent. */
1636 if (!list_empty(&osc->oo_reading_exts))
1637 return 1;
1638 }
1639
1640 return 0;
1641 }
1642
1643 static void osc_update_pending(struct osc_object *obj, int cmd, int delta)
1644 {
1645 struct client_obd *cli = osc_cli(obj);
1646 if (cmd & OBD_BRW_WRITE) {
1647 atomic_add(delta, &obj->oo_nr_writes);
1648 atomic_add(delta, &cli->cl_pending_w_pages);
1649 LASSERT(atomic_read(&obj->oo_nr_writes) >= 0);
1650 } else {
1651 atomic_add(delta, &obj->oo_nr_reads);
1652 atomic_add(delta, &cli->cl_pending_r_pages);
1653 LASSERT(atomic_read(&obj->oo_nr_reads) >= 0);
1654 }
1655 OSC_IO_DEBUG(obj, "update pending cmd %d delta %d.\n", cmd, delta);
1656 }
1657
1658 static int osc_makes_hprpc(struct osc_object *obj)
1659 {
1660 return !list_empty(&obj->oo_hp_exts);
1661 }
1662
1663 static void on_list(struct list_head *item, struct list_head *list, int should_be_on)
1664 {
1665 if (list_empty(item) && should_be_on)
1666 list_add_tail(item, list);
1667 else if (!list_empty(item) && !should_be_on)
1668 list_del_init(item);
1669 }
1670
1671 /* maintain the osc's cli list membership invariants so that osc_send_oap_rpc
1672 * can find pages to build into rpcs quickly */
1673 static int __osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1674 {
1675 if (osc_makes_hprpc(osc)) {
1676 /* HP rpc */
1677 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, 0);
1678 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 1);
1679 } else {
1680 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 0);
1681 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list,
1682 osc_makes_rpc(cli, osc, OBD_BRW_WRITE) ||
1683 osc_makes_rpc(cli, osc, OBD_BRW_READ));
1684 }
1685
1686 on_list(&osc->oo_write_item, &cli->cl_loi_write_list,
1687 atomic_read(&osc->oo_nr_writes) > 0);
1688
1689 on_list(&osc->oo_read_item, &cli->cl_loi_read_list,
1690 atomic_read(&osc->oo_nr_reads) > 0);
1691
1692 return osc_is_ready(osc);
1693 }
1694
1695 static int osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1696 {
1697 int is_ready;
1698
1699 client_obd_list_lock(&cli->cl_loi_list_lock);
1700 is_ready = __osc_list_maint(cli, osc);
1701 client_obd_list_unlock(&cli->cl_loi_list_lock);
1702
1703 return is_ready;
1704 }
1705
1706 /* this is trying to propagate async writeback errors back up to the
1707 * application. As an async write fails we record the error code for later if
1708 * the app does an fsync. As long as errors persist we force future rpcs to be
1709 * sync so that the app can get a sync error and break the cycle of queueing
1710 * pages for which writeback will fail. */
1711 static void osc_process_ar(struct osc_async_rc *ar, __u64 xid,
1712 int rc)
1713 {
1714 if (rc) {
1715 if (!ar->ar_rc)
1716 ar->ar_rc = rc;
1717
1718 ar->ar_force_sync = 1;
1719 ar->ar_min_xid = ptlrpc_sample_next_xid();
1720 return;
1721
1722 }
1723
1724 if (ar->ar_force_sync && (xid >= ar->ar_min_xid))
1725 ar->ar_force_sync = 0;
1726 }
1727
1728
1729 /* this must be called holding the loi list lock to give coverage to exit_cache,
1730 * async_flag maintenance, and oap_request */
1731 static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
1732 struct osc_async_page *oap, int sent, int rc)
1733 {
1734 struct osc_object *osc = oap->oap_obj;
1735 struct lov_oinfo *loi = osc->oo_oinfo;
1736 __u64 xid = 0;
1737
1738 if (oap->oap_request != NULL) {
1739 xid = ptlrpc_req_xid(oap->oap_request);
1740 ptlrpc_req_finished(oap->oap_request);
1741 oap->oap_request = NULL;
1742 }
1743
1744 /* As the transfer for this page is being done, clear the flags */
1745 spin_lock(&oap->oap_lock);
1746 oap->oap_async_flags = 0;
1747 spin_unlock(&oap->oap_lock);
1748 oap->oap_interrupted = 0;
1749
1750 if (oap->oap_cmd & OBD_BRW_WRITE && xid > 0) {
1751 client_obd_list_lock(&cli->cl_loi_list_lock);
1752 osc_process_ar(&cli->cl_ar, xid, rc);
1753 osc_process_ar(&loi->loi_ar, xid, rc);
1754 client_obd_list_unlock(&cli->cl_loi_list_lock);
1755 }
1756
1757 rc = osc_completion(env, oap, oap->oap_cmd, rc);
1758 if (rc)
1759 CERROR("completion on oap %p obj %p returns %d.\n",
1760 oap, osc, rc);
1761 }
1762
1763 /**
1764 * Try to add extent to one RPC. We need to think about the following things:
1765 * - # of pages must not be over max_pages_per_rpc
1766 * - extent must be compatible with previous ones
1767 */
1768 static int try_to_add_extent_for_io(struct client_obd *cli,
1769 struct osc_extent *ext, struct list_head *rpclist,
1770 int *pc, unsigned int *max_pages)
1771 {
1772 struct osc_extent *tmp;
1773
1774 EASSERT((ext->oe_state == OES_CACHE || ext->oe_state == OES_LOCK_DONE),
1775 ext);
1776
1777 *max_pages = max(ext->oe_mppr, *max_pages);
1778 if (*pc + ext->oe_nr_pages > *max_pages)
1779 return 0;
1780
1781 list_for_each_entry(tmp, rpclist, oe_link) {
1782 EASSERT(tmp->oe_owner == current, tmp);
1783 #if 0
1784 if (overlapped(tmp, ext)) {
1785 OSC_EXTENT_DUMP(D_ERROR, tmp, "overlapped %p.\n", ext);
1786 EASSERT(0, ext);
1787 }
1788 #endif
1789
1790 if (tmp->oe_srvlock != ext->oe_srvlock ||
1791 !tmp->oe_grants != !ext->oe_grants)
1792 return 0;
1793
1794 /* remove break for strict check */
1795 break;
1796 }
1797
1798 *pc += ext->oe_nr_pages;
1799 list_move_tail(&ext->oe_link, rpclist);
1800 ext->oe_owner = current;
1801 return 1;
1802 }
1803
1804 /**
1805 * In order to prevent multiple ptlrpcd from breaking contiguous extents,
1806 * get_write_extent() takes all appropriate extents in atomic.
1807 *
1808 * The following policy is used to collect extents for IO:
1809 * 1. Add as many HP extents as possible;
1810 * 2. Add the first urgent extent in urgent extent list and take it out of
1811 * urgent list;
1812 * 3. Add subsequent extents of this urgent extent;
1813 * 4. If urgent list is not empty, goto 2;
1814 * 5. Traverse the extent tree from the 1st extent;
1815 * 6. Above steps exit if there is no space in this RPC.
1816 */
1817 static int get_write_extents(struct osc_object *obj, struct list_head *rpclist)
1818 {
1819 struct client_obd *cli = osc_cli(obj);
1820 struct osc_extent *ext;
1821 int page_count = 0;
1822 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1823
1824 LASSERT(osc_object_is_locked(obj));
1825 while (!list_empty(&obj->oo_hp_exts)) {
1826 ext = list_entry(obj->oo_hp_exts.next, struct osc_extent,
1827 oe_link);
1828 LASSERT(ext->oe_state == OES_CACHE);
1829 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1830 &max_pages))
1831 return page_count;
1832 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1833 }
1834 if (page_count == max_pages)
1835 return page_count;
1836
1837 while (!list_empty(&obj->oo_urgent_exts)) {
1838 ext = list_entry(obj->oo_urgent_exts.next,
1839 struct osc_extent, oe_link);
1840 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1841 &max_pages))
1842 return page_count;
1843
1844 if (!ext->oe_intree)
1845 continue;
1846
1847 while ((ext = next_extent(ext)) != NULL) {
1848 if ((ext->oe_state != OES_CACHE) ||
1849 (!list_empty(&ext->oe_link) &&
1850 ext->oe_owner != NULL))
1851 continue;
1852
1853 if (!try_to_add_extent_for_io(cli, ext, rpclist,
1854 &page_count, &max_pages))
1855 return page_count;
1856 }
1857 }
1858 if (page_count == max_pages)
1859 return page_count;
1860
1861 ext = first_extent(obj);
1862 while (ext != NULL) {
1863 if ((ext->oe_state != OES_CACHE) ||
1864 /* this extent may be already in current rpclist */
1865 (!list_empty(&ext->oe_link) && ext->oe_owner != NULL)) {
1866 ext = next_extent(ext);
1867 continue;
1868 }
1869
1870 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1871 &max_pages))
1872 return page_count;
1873
1874 ext = next_extent(ext);
1875 }
1876 return page_count;
1877 }
1878
1879 static int
1880 osc_send_write_rpc(const struct lu_env *env, struct client_obd *cli,
1881 struct osc_object *osc, pdl_policy_t pol)
1882 {
1883 LIST_HEAD(rpclist);
1884 struct osc_extent *ext;
1885 struct osc_extent *tmp;
1886 struct osc_extent *first = NULL;
1887 obd_count page_count = 0;
1888 int srvlock = 0;
1889 int rc = 0;
1890
1891 LASSERT(osc_object_is_locked(osc));
1892
1893 page_count = get_write_extents(osc, &rpclist);
1894 LASSERT(equi(page_count == 0, list_empty(&rpclist)));
1895
1896 if (list_empty(&rpclist))
1897 return 0;
1898
1899 osc_update_pending(osc, OBD_BRW_WRITE, -page_count);
1900
1901 list_for_each_entry(ext, &rpclist, oe_link) {
1902 LASSERT(ext->oe_state == OES_CACHE ||
1903 ext->oe_state == OES_LOCK_DONE);
1904 if (ext->oe_state == OES_CACHE)
1905 osc_extent_state_set(ext, OES_LOCKING);
1906 else
1907 osc_extent_state_set(ext, OES_RPC);
1908 }
1909
1910 /* we're going to grab page lock, so release object lock because
1911 * lock order is page lock -> object lock. */
1912 osc_object_unlock(osc);
1913
1914 list_for_each_entry_safe(ext, tmp, &rpclist, oe_link) {
1915 if (ext->oe_state == OES_LOCKING) {
1916 rc = osc_extent_make_ready(env, ext);
1917 if (unlikely(rc < 0)) {
1918 list_del_init(&ext->oe_link);
1919 osc_extent_finish(env, ext, 0, rc);
1920 continue;
1921 }
1922 }
1923 if (first == NULL) {
1924 first = ext;
1925 srvlock = ext->oe_srvlock;
1926 } else {
1927 LASSERT(srvlock == ext->oe_srvlock);
1928 }
1929 }
1930
1931 if (!list_empty(&rpclist)) {
1932 LASSERT(page_count > 0);
1933 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_WRITE, pol);
1934 LASSERT(list_empty(&rpclist));
1935 }
1936
1937 osc_object_lock(osc);
1938 return rc;
1939 }
1940
1941 /**
1942 * prepare pages for ASYNC io and put pages in send queue.
1943 *
1944 * \param cmd OBD_BRW_* macroses
1945 * \param lop pending pages
1946 *
1947 * \return zero if no page added to send queue.
1948 * \return 1 if pages successfully added to send queue.
1949 * \return negative on errors.
1950 */
1951 static int
1952 osc_send_read_rpc(const struct lu_env *env, struct client_obd *cli,
1953 struct osc_object *osc, pdl_policy_t pol)
1954 {
1955 struct osc_extent *ext;
1956 struct osc_extent *next;
1957 LIST_HEAD(rpclist);
1958 int page_count = 0;
1959 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1960 int rc = 0;
1961
1962 LASSERT(osc_object_is_locked(osc));
1963 list_for_each_entry_safe(ext, next,
1964 &osc->oo_reading_exts, oe_link) {
1965 EASSERT(ext->oe_state == OES_LOCK_DONE, ext);
1966 if (!try_to_add_extent_for_io(cli, ext, &rpclist, &page_count,
1967 &max_pages))
1968 break;
1969 osc_extent_state_set(ext, OES_RPC);
1970 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1971 }
1972 LASSERT(page_count <= max_pages);
1973
1974 osc_update_pending(osc, OBD_BRW_READ, -page_count);
1975
1976 if (!list_empty(&rpclist)) {
1977 osc_object_unlock(osc);
1978
1979 LASSERT(page_count > 0);
1980 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_READ, pol);
1981 LASSERT(list_empty(&rpclist));
1982
1983 osc_object_lock(osc);
1984 }
1985 return rc;
1986 }
1987
1988 #define list_to_obj(list, item) ({ \
1989 struct list_head *__tmp = (list)->next; \
1990 list_del_init(__tmp); \
1991 list_entry(__tmp, struct osc_object, oo_##item); \
1992 })
1993
1994 /* This is called by osc_check_rpcs() to find which objects have pages that
1995 * we could be sending. These lists are maintained by osc_makes_rpc(). */
1996 static struct osc_object *osc_next_obj(struct client_obd *cli)
1997 {
1998 /* First return objects that have blocked locks so that they
1999 * will be flushed quickly and other clients can get the lock,
2000 * then objects which have pages ready to be stuffed into RPCs */
2001 if (!list_empty(&cli->cl_loi_hp_ready_list))
2002 return list_to_obj(&cli->cl_loi_hp_ready_list, hp_ready_item);
2003 if (!list_empty(&cli->cl_loi_ready_list))
2004 return list_to_obj(&cli->cl_loi_ready_list, ready_item);
2005
2006 /* then if we have cache waiters, return all objects with queued
2007 * writes. This is especially important when many small files
2008 * have filled up the cache and not been fired into rpcs because
2009 * they don't pass the nr_pending/object threshold */
2010 if (!list_empty(&cli->cl_cache_waiters) &&
2011 !list_empty(&cli->cl_loi_write_list))
2012 return list_to_obj(&cli->cl_loi_write_list, write_item);
2013
2014 /* then return all queued objects when we have an invalid import
2015 * so that they get flushed */
2016 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
2017 if (!list_empty(&cli->cl_loi_write_list))
2018 return list_to_obj(&cli->cl_loi_write_list, write_item);
2019 if (!list_empty(&cli->cl_loi_read_list))
2020 return list_to_obj(&cli->cl_loi_read_list, read_item);
2021 }
2022 return NULL;
2023 }
2024
2025 /* called with the loi list lock held */
2026 static void osc_check_rpcs(const struct lu_env *env, struct client_obd *cli,
2027 pdl_policy_t pol)
2028 {
2029 struct osc_object *osc;
2030 int rc = 0;
2031
2032 while ((osc = osc_next_obj(cli)) != NULL) {
2033 struct cl_object *obj = osc2cl(osc);
2034 struct lu_ref_link link;
2035
2036 OSC_IO_DEBUG(osc, "%lu in flight\n", rpcs_in_flight(cli));
2037
2038 if (osc_max_rpc_in_flight(cli, osc)) {
2039 __osc_list_maint(cli, osc);
2040 break;
2041 }
2042
2043 cl_object_get(obj);
2044 client_obd_list_unlock(&cli->cl_loi_list_lock);
2045 lu_object_ref_add_at(&obj->co_lu, &link, "check",
2046 current);
2047
2048 /* attempt some read/write balancing by alternating between
2049 * reads and writes in an object. The makes_rpc checks here
2050 * would be redundant if we were getting read/write work items
2051 * instead of objects. we don't want send_oap_rpc to drain a
2052 * partial read pending queue when we're given this object to
2053 * do io on writes while there are cache waiters */
2054 osc_object_lock(osc);
2055 if (osc_makes_rpc(cli, osc, OBD_BRW_WRITE)) {
2056 rc = osc_send_write_rpc(env, cli, osc, pol);
2057 if (rc < 0) {
2058 CERROR("Write request failed with %d\n", rc);
2059
2060 /* osc_send_write_rpc failed, mostly because of
2061 * memory pressure.
2062 *
2063 * It can't break here, because if:
2064 * - a page was submitted by osc_io_submit, so
2065 * page locked;
2066 * - no request in flight
2067 * - no subsequent request
2068 * The system will be in live-lock state,
2069 * because there is no chance to call
2070 * osc_io_unplug() and osc_check_rpcs() any
2071 * more. pdflush can't help in this case,
2072 * because it might be blocked at grabbing
2073 * the page lock as we mentioned.
2074 *
2075 * Anyway, continue to drain pages. */
2076 /* break; */
2077 }
2078 }
2079 if (osc_makes_rpc(cli, osc, OBD_BRW_READ)) {
2080 rc = osc_send_read_rpc(env, cli, osc, pol);
2081 if (rc < 0)
2082 CERROR("Read request failed with %d\n", rc);
2083 }
2084 osc_object_unlock(osc);
2085
2086 osc_list_maint(cli, osc);
2087 lu_object_ref_del_at(&obj->co_lu, &link, "check",
2088 current);
2089 cl_object_put(env, obj);
2090
2091 client_obd_list_lock(&cli->cl_loi_list_lock);
2092 }
2093 }
2094
2095 static int osc_io_unplug0(const struct lu_env *env, struct client_obd *cli,
2096 struct osc_object *osc, pdl_policy_t pol, int async)
2097 {
2098 int rc = 0;
2099
2100 if (osc != NULL && osc_list_maint(cli, osc) == 0)
2101 return 0;
2102
2103 if (!async) {
2104 /* disable osc_lru_shrink() temporarily to avoid
2105 * potential stack overrun problem. LU-2859 */
2106 atomic_inc(&cli->cl_lru_shrinkers);
2107 client_obd_list_lock(&cli->cl_loi_list_lock);
2108 osc_check_rpcs(env, cli, pol);
2109 client_obd_list_unlock(&cli->cl_loi_list_lock);
2110 atomic_dec(&cli->cl_lru_shrinkers);
2111 } else {
2112 CDEBUG(D_CACHE, "Queue writeback work for client %p.\n", cli);
2113 LASSERT(cli->cl_writeback_work != NULL);
2114 rc = ptlrpcd_queue_work(cli->cl_writeback_work);
2115 }
2116 return rc;
2117 }
2118
2119 static int osc_io_unplug_async(const struct lu_env *env,
2120 struct client_obd *cli, struct osc_object *osc)
2121 {
2122 /* XXX: policy is no use actually. */
2123 return osc_io_unplug0(env, cli, osc, PDL_POLICY_ROUND, 1);
2124 }
2125
2126 void osc_io_unplug(const struct lu_env *env, struct client_obd *cli,
2127 struct osc_object *osc, pdl_policy_t pol)
2128 {
2129 (void)osc_io_unplug0(env, cli, osc, pol, 0);
2130 }
2131
2132 int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops,
2133 struct page *page, loff_t offset)
2134 {
2135 struct obd_export *exp = osc_export(osc);
2136 struct osc_async_page *oap = &ops->ops_oap;
2137
2138 if (!page)
2139 return cfs_size_round(sizeof(*oap));
2140
2141 oap->oap_magic = OAP_MAGIC;
2142 oap->oap_cli = &exp->exp_obd->u.cli;
2143 oap->oap_obj = osc;
2144
2145 oap->oap_page = page;
2146 oap->oap_obj_off = offset;
2147 LASSERT(!(offset & ~CFS_PAGE_MASK));
2148
2149 if (!client_is_remote(exp) && capable(CFS_CAP_SYS_RESOURCE))
2150 oap->oap_brw_flags = OBD_BRW_NOQUOTA;
2151
2152 INIT_LIST_HEAD(&oap->oap_pending_item);
2153 INIT_LIST_HEAD(&oap->oap_rpc_item);
2154
2155 spin_lock_init(&oap->oap_lock);
2156 CDEBUG(D_INFO, "oap %p page %p obj off "LPU64"\n",
2157 oap, page, oap->oap_obj_off);
2158 return 0;
2159 }
2160
2161 int osc_queue_async_io(const struct lu_env *env, struct cl_io *io,
2162 struct osc_page *ops)
2163 {
2164 struct osc_io *oio = osc_env_io(env);
2165 struct osc_extent *ext = NULL;
2166 struct osc_async_page *oap = &ops->ops_oap;
2167 struct client_obd *cli = oap->oap_cli;
2168 struct osc_object *osc = oap->oap_obj;
2169 pgoff_t index;
2170 int grants = 0;
2171 int brw_flags = OBD_BRW_ASYNC;
2172 int cmd = OBD_BRW_WRITE;
2173 int need_release = 0;
2174 int rc = 0;
2175
2176 if (oap->oap_magic != OAP_MAGIC)
2177 return -EINVAL;
2178
2179 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2180 return -EIO;
2181
2182 if (!list_empty(&oap->oap_pending_item) ||
2183 !list_empty(&oap->oap_rpc_item))
2184 return -EBUSY;
2185
2186 /* Set the OBD_BRW_SRVLOCK before the page is queued. */
2187 brw_flags |= ops->ops_srvlock ? OBD_BRW_SRVLOCK : 0;
2188 if (!client_is_remote(osc_export(osc)) &&
2189 capable(CFS_CAP_SYS_RESOURCE)) {
2190 brw_flags |= OBD_BRW_NOQUOTA;
2191 cmd |= OBD_BRW_NOQUOTA;
2192 }
2193
2194 /* check if the file's owner/group is over quota */
2195 if (!(cmd & OBD_BRW_NOQUOTA)) {
2196 struct cl_object *obj;
2197 struct cl_attr *attr;
2198 unsigned int qid[MAXQUOTAS];
2199
2200 obj = cl_object_top(&osc->oo_cl);
2201 attr = &osc_env_info(env)->oti_attr;
2202
2203 cl_object_attr_lock(obj);
2204 rc = cl_object_attr_get(env, obj, attr);
2205 cl_object_attr_unlock(obj);
2206
2207 qid[USRQUOTA] = attr->cat_uid;
2208 qid[GRPQUOTA] = attr->cat_gid;
2209 if (rc == 0 && osc_quota_chkdq(cli, qid) == NO_QUOTA)
2210 rc = -EDQUOT;
2211 if (rc)
2212 return rc;
2213 }
2214
2215 oap->oap_cmd = cmd;
2216 oap->oap_page_off = ops->ops_from;
2217 oap->oap_count = ops->ops_to - ops->ops_from;
2218 oap->oap_async_flags = 0;
2219 oap->oap_brw_flags = brw_flags;
2220
2221 OSC_IO_DEBUG(osc, "oap %p page %p added for cmd %d\n",
2222 oap, oap->oap_page, oap->oap_cmd & OBD_BRW_RWMASK);
2223
2224 index = oap2cl_page(oap)->cp_index;
2225
2226 /* Add this page into extent by the following steps:
2227 * 1. if there exists an active extent for this IO, mostly this page
2228 * can be added to the active extent and sometimes we need to
2229 * expand extent to accommodate this page;
2230 * 2. otherwise, a new extent will be allocated. */
2231
2232 ext = oio->oi_active;
2233 if (ext != NULL && ext->oe_start <= index && ext->oe_max_end >= index) {
2234 /* one chunk plus extent overhead must be enough to write this
2235 * page */
2236 grants = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2237 if (ext->oe_end >= index)
2238 grants = 0;
2239
2240 /* it doesn't need any grant to dirty this page */
2241 client_obd_list_lock(&cli->cl_loi_list_lock);
2242 rc = osc_enter_cache_try(cli, oap, grants, 0);
2243 client_obd_list_unlock(&cli->cl_loi_list_lock);
2244 if (rc == 0) { /* try failed */
2245 grants = 0;
2246 need_release = 1;
2247 } else if (ext->oe_end < index) {
2248 int tmp = grants;
2249 /* try to expand this extent */
2250 rc = osc_extent_expand(ext, index, &tmp);
2251 if (rc < 0) {
2252 need_release = 1;
2253 /* don't free reserved grant */
2254 } else {
2255 OSC_EXTENT_DUMP(D_CACHE, ext,
2256 "expanded for %lu.\n", index);
2257 osc_unreserve_grant(cli, grants, tmp);
2258 grants = 0;
2259 }
2260 }
2261 rc = 0;
2262 } else if (ext != NULL) {
2263 /* index is located outside of active extent */
2264 need_release = 1;
2265 }
2266 if (need_release) {
2267 osc_extent_release(env, ext);
2268 oio->oi_active = NULL;
2269 ext = NULL;
2270 }
2271
2272 if (ext == NULL) {
2273 int tmp = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2274
2275 /* try to find new extent to cover this page */
2276 LASSERT(oio->oi_active == NULL);
2277 /* we may have allocated grant for this page if we failed
2278 * to expand the previous active extent. */
2279 LASSERT(ergo(grants > 0, grants >= tmp));
2280
2281 rc = 0;
2282 if (grants == 0) {
2283 /* we haven't allocated grant for this page. */
2284 rc = osc_enter_cache(env, cli, oap, tmp);
2285 if (rc == 0)
2286 grants = tmp;
2287 }
2288
2289 tmp = grants;
2290 if (rc == 0) {
2291 ext = osc_extent_find(env, osc, index, &tmp);
2292 if (IS_ERR(ext)) {
2293 LASSERT(tmp == grants);
2294 osc_exit_cache(cli, oap);
2295 rc = PTR_ERR(ext);
2296 ext = NULL;
2297 } else {
2298 oio->oi_active = ext;
2299 }
2300 }
2301 if (grants > 0)
2302 osc_unreserve_grant(cli, grants, tmp);
2303 }
2304
2305 LASSERT(ergo(rc == 0, ext != NULL));
2306 if (ext != NULL) {
2307 EASSERTF(ext->oe_end >= index && ext->oe_start <= index,
2308 ext, "index = %lu.\n", index);
2309 LASSERT((oap->oap_brw_flags & OBD_BRW_FROM_GRANT) != 0);
2310
2311 osc_object_lock(osc);
2312 if (ext->oe_nr_pages == 0)
2313 ext->oe_srvlock = ops->ops_srvlock;
2314 else
2315 LASSERT(ext->oe_srvlock == ops->ops_srvlock);
2316 ++ext->oe_nr_pages;
2317 list_add_tail(&oap->oap_pending_item, &ext->oe_pages);
2318 osc_object_unlock(osc);
2319 }
2320 return rc;
2321 }
2322
2323 int osc_teardown_async_page(const struct lu_env *env,
2324 struct osc_object *obj, struct osc_page *ops)
2325 {
2326 struct osc_async_page *oap = &ops->ops_oap;
2327 struct osc_extent *ext = NULL;
2328 int rc = 0;
2329
2330 LASSERT(oap->oap_magic == OAP_MAGIC);
2331
2332 CDEBUG(D_INFO, "teardown oap %p page %p at index %lu.\n",
2333 oap, ops, oap2cl_page(oap)->cp_index);
2334
2335 osc_object_lock(obj);
2336 if (!list_empty(&oap->oap_rpc_item)) {
2337 CDEBUG(D_CACHE, "oap %p is not in cache.\n", oap);
2338 rc = -EBUSY;
2339 } else if (!list_empty(&oap->oap_pending_item)) {
2340 ext = osc_extent_lookup(obj, oap2cl_page(oap)->cp_index);
2341 /* only truncated pages are allowed to be taken out.
2342 * See osc_extent_truncate() and osc_cache_truncate_start()
2343 * for details. */
2344 if (ext != NULL && ext->oe_state != OES_TRUNC) {
2345 OSC_EXTENT_DUMP(D_ERROR, ext, "trunc at %lu.\n",
2346 oap2cl_page(oap)->cp_index);
2347 rc = -EBUSY;
2348 }
2349 }
2350 osc_object_unlock(obj);
2351 if (ext != NULL)
2352 osc_extent_put(env, ext);
2353 return rc;
2354 }
2355
2356 /**
2357 * This is called when a page is picked up by kernel to write out.
2358 *
2359 * We should find out the corresponding extent and add the whole extent
2360 * into urgent list. The extent may be being truncated or used, handle it
2361 * carefully.
2362 */
2363 int osc_flush_async_page(const struct lu_env *env, struct cl_io *io,
2364 struct osc_page *ops)
2365 {
2366 struct osc_extent *ext = NULL;
2367 struct osc_object *obj = cl2osc(ops->ops_cl.cpl_obj);
2368 struct cl_page *cp = ops->ops_cl.cpl_page;
2369 pgoff_t index = cp->cp_index;
2370 struct osc_async_page *oap = &ops->ops_oap;
2371 bool unplug = false;
2372 int rc = 0;
2373
2374 osc_object_lock(obj);
2375 ext = osc_extent_lookup(obj, index);
2376 if (ext == NULL) {
2377 osc_extent_tree_dump(D_ERROR, obj);
2378 LASSERTF(0, "page index %lu is NOT covered.\n", index);
2379 }
2380
2381 switch (ext->oe_state) {
2382 case OES_RPC:
2383 case OES_LOCK_DONE:
2384 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(cp),
2385 "flush an in-rpc page?\n");
2386 LASSERT(0);
2387 break;
2388 case OES_LOCKING:
2389 /* If we know this extent is being written out, we should abort
2390 * so that the writer can make this page ready. Otherwise, there
2391 * exists a deadlock problem because other process can wait for
2392 * page writeback bit holding page lock; and meanwhile in
2393 * vvp_page_make_ready(), we need to grab page lock before
2394 * really sending the RPC. */
2395 case OES_TRUNC:
2396 /* race with truncate, page will be redirtied */
2397 case OES_ACTIVE:
2398 /* The extent is active so we need to abort and let the caller
2399 * re-dirty the page. If we continued on here, and we were the
2400 * one making the extent active, we could deadlock waiting for
2401 * the page writeback to clear but it won't because the extent
2402 * is active and won't be written out. */
2403 GOTO(out, rc = -EAGAIN);
2404 default:
2405 break;
2406 }
2407
2408 rc = cl_page_prep(env, io, cl_page_top(cp), CRT_WRITE);
2409 if (rc)
2410 GOTO(out, rc);
2411
2412 spin_lock(&oap->oap_lock);
2413 oap->oap_async_flags |= ASYNC_READY|ASYNC_URGENT;
2414 spin_unlock(&oap->oap_lock);
2415
2416 if (memory_pressure_get())
2417 ext->oe_memalloc = 1;
2418
2419 ext->oe_urgent = 1;
2420 if (ext->oe_state == OES_CACHE) {
2421 OSC_EXTENT_DUMP(D_CACHE, ext,
2422 "flush page %p make it urgent.\n", oap);
2423 if (list_empty(&ext->oe_link))
2424 list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2425 unplug = true;
2426 }
2427 rc = 0;
2428
2429 out:
2430 osc_object_unlock(obj);
2431 osc_extent_put(env, ext);
2432 if (unplug)
2433 osc_io_unplug_async(env, osc_cli(obj), obj);
2434 return rc;
2435 }
2436
2437 /**
2438 * this is called when a sync waiter receives an interruption. Its job is to
2439 * get the caller woken as soon as possible. If its page hasn't been put in an
2440 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
2441 * desiring interruption which will forcefully complete the rpc once the rpc
2442 * has timed out.
2443 */
2444 int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops)
2445 {
2446 struct osc_async_page *oap = &ops->ops_oap;
2447 struct osc_object *obj = oap->oap_obj;
2448 struct client_obd *cli = osc_cli(obj);
2449 struct osc_extent *ext;
2450 struct osc_extent *found = NULL;
2451 struct list_head *plist;
2452 pgoff_t index = oap2cl_page(oap)->cp_index;
2453 int rc = -EBUSY;
2454 int cmd;
2455
2456 LASSERT(!oap->oap_interrupted);
2457 oap->oap_interrupted = 1;
2458
2459 /* Find out the caching extent */
2460 osc_object_lock(obj);
2461 if (oap->oap_cmd & OBD_BRW_WRITE) {
2462 plist = &obj->oo_urgent_exts;
2463 cmd = OBD_BRW_WRITE;
2464 } else {
2465 plist = &obj->oo_reading_exts;
2466 cmd = OBD_BRW_READ;
2467 }
2468 list_for_each_entry(ext, plist, oe_link) {
2469 if (ext->oe_start <= index && ext->oe_end >= index) {
2470 LASSERT(ext->oe_state == OES_LOCK_DONE);
2471 /* For OES_LOCK_DONE state extent, it has already held
2472 * a refcount for RPC. */
2473 found = osc_extent_get(ext);
2474 break;
2475 }
2476 }
2477 if (found != NULL) {
2478 list_del_init(&found->oe_link);
2479 osc_update_pending(obj, cmd, -found->oe_nr_pages);
2480 osc_object_unlock(obj);
2481
2482 osc_extent_finish(env, found, 0, -EINTR);
2483 osc_extent_put(env, found);
2484 rc = 0;
2485 } else {
2486 osc_object_unlock(obj);
2487 /* ok, it's been put in an rpc. only one oap gets a request
2488 * reference */
2489 if (oap->oap_request != NULL) {
2490 ptlrpc_mark_interrupted(oap->oap_request);
2491 ptlrpcd_wake(oap->oap_request);
2492 ptlrpc_req_finished(oap->oap_request);
2493 oap->oap_request = NULL;
2494 }
2495 }
2496
2497 osc_list_maint(cli, obj);
2498 return rc;
2499 }
2500
2501 int osc_queue_sync_pages(const struct lu_env *env, struct osc_object *obj,
2502 struct list_head *list, int cmd, int brw_flags)
2503 {
2504 struct client_obd *cli = osc_cli(obj);
2505 struct osc_extent *ext;
2506 struct osc_async_page *oap, *tmp;
2507 int page_count = 0;
2508 int mppr = cli->cl_max_pages_per_rpc;
2509 pgoff_t start = CL_PAGE_EOF;
2510 pgoff_t end = 0;
2511
2512 list_for_each_entry(oap, list, oap_pending_item) {
2513 struct cl_page *cp = oap2cl_page(oap);
2514 if (cp->cp_index > end)
2515 end = cp->cp_index;
2516 if (cp->cp_index < start)
2517 start = cp->cp_index;
2518 ++page_count;
2519 mppr <<= (page_count > mppr);
2520 }
2521
2522 ext = osc_extent_alloc(obj);
2523 if (ext == NULL) {
2524 list_for_each_entry_safe(oap, tmp, list, oap_pending_item) {
2525 list_del_init(&oap->oap_pending_item);
2526 osc_ap_completion(env, cli, oap, 0, -ENOMEM);
2527 }
2528 return -ENOMEM;
2529 }
2530
2531 ext->oe_rw = !!(cmd & OBD_BRW_READ);
2532 ext->oe_urgent = 1;
2533 ext->oe_start = start;
2534 ext->oe_end = ext->oe_max_end = end;
2535 ext->oe_obj = obj;
2536 ext->oe_srvlock = !!(brw_flags & OBD_BRW_SRVLOCK);
2537 ext->oe_nr_pages = page_count;
2538 ext->oe_mppr = mppr;
2539 list_splice_init(list, &ext->oe_pages);
2540
2541 osc_object_lock(obj);
2542 /* Reuse the initial refcount for RPC, don't drop it */
2543 osc_extent_state_set(ext, OES_LOCK_DONE);
2544 if (cmd & OBD_BRW_WRITE) {
2545 list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2546 osc_update_pending(obj, OBD_BRW_WRITE, page_count);
2547 } else {
2548 list_add_tail(&ext->oe_link, &obj->oo_reading_exts);
2549 osc_update_pending(obj, OBD_BRW_READ, page_count);
2550 }
2551 osc_object_unlock(obj);
2552
2553 osc_io_unplug(env, cli, obj, PDL_POLICY_ROUND);
2554 return 0;
2555 }
2556
2557 /**
2558 * Called by osc_io_setattr_start() to freeze and destroy covering extents.
2559 */
2560 int osc_cache_truncate_start(const struct lu_env *env, struct osc_io *oio,
2561 struct osc_object *obj, __u64 size)
2562 {
2563 struct client_obd *cli = osc_cli(obj);
2564 struct osc_extent *ext;
2565 struct osc_extent *waiting = NULL;
2566 pgoff_t index;
2567 LIST_HEAD(list);
2568 int result = 0;
2569 bool partial;
2570
2571 /* pages with index greater or equal to index will be truncated. */
2572 index = cl_index(osc2cl(obj), size);
2573 partial = size > cl_offset(osc2cl(obj), index);
2574
2575 again:
2576 osc_object_lock(obj);
2577 ext = osc_extent_search(obj, index);
2578 if (ext == NULL)
2579 ext = first_extent(obj);
2580 else if (ext->oe_end < index)
2581 ext = next_extent(ext);
2582 while (ext != NULL) {
2583 EASSERT(ext->oe_state != OES_TRUNC, ext);
2584
2585 if (ext->oe_state > OES_CACHE || ext->oe_urgent) {
2586 /* if ext is in urgent state, it means there must exist
2587 * a page already having been flushed by write_page().
2588 * We have to wait for this extent because we can't
2589 * truncate that page. */
2590 LASSERT(!ext->oe_hp);
2591 OSC_EXTENT_DUMP(D_CACHE, ext,
2592 "waiting for busy extent\n");
2593 waiting = osc_extent_get(ext);
2594 break;
2595 }
2596
2597 OSC_EXTENT_DUMP(D_CACHE, ext, "try to trunc:"LPU64".\n", size);
2598
2599 osc_extent_get(ext);
2600 if (ext->oe_state == OES_ACTIVE) {
2601 /* though we grab inode mutex for write path, but we
2602 * release it before releasing extent(in osc_io_end()),
2603 * so there is a race window that an extent is still
2604 * in OES_ACTIVE when truncate starts. */
2605 LASSERT(!ext->oe_trunc_pending);
2606 ext->oe_trunc_pending = 1;
2607 } else {
2608 EASSERT(ext->oe_state == OES_CACHE, ext);
2609 osc_extent_state_set(ext, OES_TRUNC);
2610 osc_update_pending(obj, OBD_BRW_WRITE,
2611 -ext->oe_nr_pages);
2612 }
2613 EASSERT(list_empty(&ext->oe_link), ext);
2614 list_add_tail(&ext->oe_link, &list);
2615
2616 ext = next_extent(ext);
2617 }
2618 osc_object_unlock(obj);
2619
2620 osc_list_maint(cli, obj);
2621
2622 while (!list_empty(&list)) {
2623 int rc;
2624
2625 ext = list_entry(list.next, struct osc_extent, oe_link);
2626 list_del_init(&ext->oe_link);
2627
2628 /* extent may be in OES_ACTIVE state because inode mutex
2629 * is released before osc_io_end() in file write case */
2630 if (ext->oe_state != OES_TRUNC)
2631 osc_extent_wait(env, ext, OES_TRUNC);
2632
2633 rc = osc_extent_truncate(ext, index, partial);
2634 if (rc < 0) {
2635 if (result == 0)
2636 result = rc;
2637
2638 OSC_EXTENT_DUMP(D_ERROR, ext,
2639 "truncate error %d\n", rc);
2640 } else if (ext->oe_nr_pages == 0) {
2641 osc_extent_remove(ext);
2642 } else {
2643 /* this must be an overlapped extent which means only
2644 * part of pages in this extent have been truncated.
2645 */
2646 EASSERTF(ext->oe_start <= index, ext,
2647 "trunc index = %lu/%d.\n", index, partial);
2648 /* fix index to skip this partially truncated extent */
2649 index = ext->oe_end + 1;
2650 partial = false;
2651
2652 /* we need to hold this extent in OES_TRUNC state so
2653 * that no writeback will happen. This is to avoid
2654 * BUG 17397. */
2655 LASSERT(oio->oi_trunc == NULL);
2656 oio->oi_trunc = osc_extent_get(ext);
2657 OSC_EXTENT_DUMP(D_CACHE, ext,
2658 "trunc at "LPU64"\n", size);
2659 }
2660 osc_extent_put(env, ext);
2661 }
2662 if (waiting != NULL) {
2663 int rc;
2664
2665 /* ignore the result of osc_extent_wait the write initiator
2666 * should take care of it. */
2667 rc = osc_extent_wait(env, waiting, OES_INV);
2668 if (rc < 0)
2669 OSC_EXTENT_DUMP(D_CACHE, waiting, "error: %d.\n", rc);
2670
2671 osc_extent_put(env, waiting);
2672 waiting = NULL;
2673 goto again;
2674 }
2675 return result;
2676 }
2677
2678 /**
2679 * Called after osc_io_setattr_end to add oio->oi_trunc back to cache.
2680 */
2681 void osc_cache_truncate_end(const struct lu_env *env, struct osc_io *oio,
2682 struct osc_object *obj)
2683 {
2684 struct osc_extent *ext = oio->oi_trunc;
2685
2686 oio->oi_trunc = NULL;
2687 if (ext != NULL) {
2688 bool unplug = false;
2689
2690 EASSERT(ext->oe_nr_pages > 0, ext);
2691 EASSERT(ext->oe_state == OES_TRUNC, ext);
2692 EASSERT(!ext->oe_urgent, ext);
2693
2694 OSC_EXTENT_DUMP(D_CACHE, ext, "trunc -> cache.\n");
2695 osc_object_lock(obj);
2696 osc_extent_state_set(ext, OES_CACHE);
2697 if (ext->oe_fsync_wait && !ext->oe_urgent) {
2698 ext->oe_urgent = 1;
2699 list_move_tail(&ext->oe_link, &obj->oo_urgent_exts);
2700 unplug = true;
2701 }
2702 osc_update_pending(obj, OBD_BRW_WRITE, ext->oe_nr_pages);
2703 osc_object_unlock(obj);
2704 osc_extent_put(env, ext);
2705
2706 if (unplug)
2707 osc_io_unplug_async(env, osc_cli(obj), obj);
2708 }
2709 }
2710
2711 /**
2712 * Wait for extents in a specific range to be written out.
2713 * The caller must have called osc_cache_writeback_range() to issue IO
2714 * otherwise it will take a long time for this function to finish.
2715 *
2716 * Caller must hold inode_mutex , or cancel exclusive dlm lock so that
2717 * nobody else can dirty this range of file while we're waiting for
2718 * extents to be written.
2719 */
2720 int osc_cache_wait_range(const struct lu_env *env, struct osc_object *obj,
2721 pgoff_t start, pgoff_t end)
2722 {
2723 struct osc_extent *ext;
2724 pgoff_t index = start;
2725 int result = 0;
2726
2727 again:
2728 osc_object_lock(obj);
2729 ext = osc_extent_search(obj, index);
2730 if (ext == NULL)
2731 ext = first_extent(obj);
2732 else if (ext->oe_end < index)
2733 ext = next_extent(ext);
2734 while (ext != NULL) {
2735 int rc;
2736
2737 if (ext->oe_start > end)
2738 break;
2739
2740 if (!ext->oe_fsync_wait) {
2741 ext = next_extent(ext);
2742 continue;
2743 }
2744
2745 EASSERT(ergo(ext->oe_state == OES_CACHE,
2746 ext->oe_hp || ext->oe_urgent), ext);
2747 EASSERT(ergo(ext->oe_state == OES_ACTIVE,
2748 !ext->oe_hp && ext->oe_urgent), ext);
2749
2750 index = ext->oe_end + 1;
2751 osc_extent_get(ext);
2752 osc_object_unlock(obj);
2753
2754 rc = osc_extent_wait(env, ext, OES_INV);
2755 if (result == 0)
2756 result = rc;
2757 osc_extent_put(env, ext);
2758 goto again;
2759 }
2760 osc_object_unlock(obj);
2761
2762 OSC_IO_DEBUG(obj, "sync file range.\n");
2763 return result;
2764 }
2765
2766 /**
2767 * Called to write out a range of osc object.
2768 *
2769 * @hp : should be set this is caused by lock cancel;
2770 * @discard: is set if dirty pages should be dropped - file will be deleted or
2771 * truncated, this implies there is no partially discarding extents.
2772 *
2773 * Return how many pages will be issued, or error code if error occurred.
2774 */
2775 int osc_cache_writeback_range(const struct lu_env *env, struct osc_object *obj,
2776 pgoff_t start, pgoff_t end, int hp, int discard)
2777 {
2778 struct osc_extent *ext;
2779 LIST_HEAD(discard_list);
2780 bool unplug = false;
2781 int result = 0;
2782
2783 osc_object_lock(obj);
2784 ext = osc_extent_search(obj, start);
2785 if (ext == NULL)
2786 ext = first_extent(obj);
2787 else if (ext->oe_end < start)
2788 ext = next_extent(ext);
2789 while (ext != NULL) {
2790 if (ext->oe_start > end)
2791 break;
2792
2793 ext->oe_fsync_wait = 1;
2794 switch (ext->oe_state) {
2795 case OES_CACHE:
2796 result += ext->oe_nr_pages;
2797 if (!discard) {
2798 struct list_head *list = NULL;
2799 if (hp) {
2800 EASSERT(!ext->oe_hp, ext);
2801 ext->oe_hp = 1;
2802 list = &obj->oo_hp_exts;
2803 } else if (!ext->oe_urgent) {
2804 ext->oe_urgent = 1;
2805 list = &obj->oo_urgent_exts;
2806 }
2807 if (list != NULL)
2808 list_move_tail(&ext->oe_link, list);
2809 unplug = true;
2810 } else {
2811 /* the only discarder is lock cancelling, so
2812 * [start, end] must contain this extent */
2813 EASSERT(ext->oe_start >= start &&
2814 ext->oe_max_end <= end, ext);
2815 osc_extent_state_set(ext, OES_LOCKING);
2816 ext->oe_owner = current;
2817 list_move_tail(&ext->oe_link,
2818 &discard_list);
2819 osc_update_pending(obj, OBD_BRW_WRITE,
2820 -ext->oe_nr_pages);
2821 }
2822 break;
2823 case OES_ACTIVE:
2824 /* It's pretty bad to wait for ACTIVE extents, because
2825 * we don't know how long we will wait for it to be
2826 * flushed since it may be blocked at awaiting more
2827 * grants. We do this for the correctness of fsync. */
2828 LASSERT(hp == 0 && discard == 0);
2829 ext->oe_urgent = 1;
2830 break;
2831 case OES_TRUNC:
2832 /* this extent is being truncated, can't do anything
2833 * for it now. it will be set to urgent after truncate
2834 * is finished in osc_cache_truncate_end(). */
2835 default:
2836 break;
2837 }
2838 ext = next_extent(ext);
2839 }
2840 osc_object_unlock(obj);
2841
2842 LASSERT(ergo(!discard, list_empty(&discard_list)));
2843 if (!list_empty(&discard_list)) {
2844 struct osc_extent *tmp;
2845 int rc;
2846
2847 osc_list_maint(osc_cli(obj), obj);
2848 list_for_each_entry_safe(ext, tmp, &discard_list, oe_link) {
2849 list_del_init(&ext->oe_link);
2850 EASSERT(ext->oe_state == OES_LOCKING, ext);
2851
2852 /* Discard caching pages. We don't actually write this
2853 * extent out but we complete it as if we did. */
2854 rc = osc_extent_make_ready(env, ext);
2855 if (unlikely(rc < 0)) {
2856 OSC_EXTENT_DUMP(D_ERROR, ext,
2857 "make_ready returned %d\n", rc);
2858 if (result >= 0)
2859 result = rc;
2860 }
2861
2862 /* finish the extent as if the pages were sent */
2863 osc_extent_finish(env, ext, 0, 0);
2864 }
2865 }
2866
2867 if (unplug)
2868 osc_io_unplug(env, osc_cli(obj), obj, PDL_POLICY_ROUND);
2869
2870 if (hp || discard) {
2871 int rc;
2872 rc = osc_cache_wait_range(env, obj, start, end);
2873 if (result >= 0 && rc < 0)
2874 result = rc;
2875 }
2876
2877 OSC_IO_DEBUG(obj, "cache page out.\n");
2878 return result;
2879 }
2880
2881 /** @} osc */
This page took 0.092271 seconds and 4 git commands to generate.