4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Sun Microsystems, Inc.
37 * osc cache management.
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_OSC
44 #include "osc_cl_internal.h"
45 #include "osc_internal.h"
47 static int extent_debug; /* set it to be true for more debug */
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,
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,
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);
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__)
72 /* ------------------ osc extent ------------------ */
73 static inline char *ext_flags(struct osc_extent *ext, char *flags)
76 *buf++ = ext->oe_rw ? 'r' : 'w';
87 if (ext->oe_trunc_pending)
89 if (ext->oe_fsync_wait)
95 static inline char list_empty_marker(cfs_list_t *list)
97 return cfs_list_empty(list) ? '-' : '+';
100 #define EXTSTR "[%lu -> %lu/%lu]"
101 #define EXTPARA(ext) (ext)->oe_start, (ext)->oe_end, (ext)->oe_max_end
103 #define OSC_EXTENT_DUMP(lvl, extent, fmt, ...) do { \
104 struct osc_extent *__ext = (extent); \
105 const char *__str[] = OES_STRINGS; \
109 "extent %p@{" EXTSTR ", " \
110 "[%d|%d|%c|%s|%s|%p], [%d|%d|%c|%c|%p|%u|%p]} " fmt, \
111 /* ----- extent part 0 ----- */ \
112 __ext, EXTPARA(__ext), \
113 /* ----- part 1 ----- */ \
114 cfs_atomic_read(&__ext->oe_refc), \
115 cfs_atomic_read(&__ext->oe_users), \
116 list_empty_marker(&__ext->oe_link), \
117 __str[__ext->oe_state], ext_flags(__ext, __buf), \
119 /* ----- part 2 ----- */ \
120 __ext->oe_grants, __ext->oe_nr_pages, \
121 list_empty_marker(&__ext->oe_pages), \
122 cfs_waitq_active(&__ext->oe_waitq) ? '+' : '-', \
123 __ext->oe_osclock, __ext->oe_mppr, __ext->oe_owner, \
124 /* ----- part 4 ----- */ \
129 #define EASSERTF(expr, ext, fmt, args...) do { \
131 OSC_EXTENT_DUMP(D_ERROR, (ext), fmt, ##args); \
132 osc_extent_tree_dump(D_ERROR, (ext)->oe_obj); \
138 #define EASSERT(expr, ext) EASSERTF(expr, ext, "\n")
140 static inline struct osc_extent *rb_extent(struct rb_node *n)
145 return container_of(n, struct osc_extent, oe_node);
148 static inline struct osc_extent *next_extent(struct osc_extent *ext)
153 LASSERT(ext->oe_intree);
154 return rb_extent(rb_next(&ext->oe_node));
157 static inline struct osc_extent *prev_extent(struct osc_extent *ext)
162 LASSERT(ext->oe_intree);
163 return rb_extent(rb_prev(&ext->oe_node));
166 static inline struct osc_extent *first_extent(struct osc_object *obj)
168 return rb_extent(rb_first(&obj->oo_root));
171 /* object must be locked by caller. */
172 static int osc_extent_sanity_check0(struct osc_extent *ext,
173 const char *func, const int line)
175 struct osc_object *obj = ext->oe_obj;
176 struct osc_async_page *oap;
180 if (!osc_object_is_locked(obj))
183 if (ext->oe_state >= OES_STATE_MAX)
186 if (cfs_atomic_read(&ext->oe_refc) <= 0)
189 if (cfs_atomic_read(&ext->oe_refc) < cfs_atomic_read(&ext->oe_users))
192 switch (ext->oe_state) {
194 if (ext->oe_nr_pages > 0 || !cfs_list_empty(&ext->oe_pages))
199 if (cfs_atomic_read(&ext->oe_users) == 0)
203 if (ext->oe_fsync_wait && !ext->oe_urgent)
207 if (ext->oe_grants == 0)
209 if (ext->oe_fsync_wait && !ext->oe_urgent && !ext->oe_hp)
212 if (cfs_atomic_read(&ext->oe_users) > 0)
216 if (ext->oe_max_end < ext->oe_end || ext->oe_end < ext->oe_start)
219 if (ext->oe_osclock == NULL && ext->oe_grants > 0)
222 if (ext->oe_osclock) {
223 struct cl_lock_descr *descr;
224 descr = &ext->oe_osclock->cll_descr;
225 if (!(descr->cld_start <= ext->oe_start &&
226 descr->cld_end >= ext->oe_max_end))
230 if (ext->oe_nr_pages > ext->oe_mppr)
233 /* Do not verify page list if extent is in RPC. This is because an
234 * in-RPC extent is supposed to be exclusively accessible w/o lock. */
235 if (ext->oe_state > OES_CACHE)
242 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
243 pgoff_t index = oap2cl_page(oap)->cp_index;
245 if (index > ext->oe_end || index < ext->oe_start)
248 if (page_count != ext->oe_nr_pages)
253 OSC_EXTENT_DUMP(D_ERROR, ext,
254 "%s:%d sanity check %p failed with rc = %d\n",
255 func, line, ext, rc);
259 #define sanity_check_nolock(ext) \
260 osc_extent_sanity_check0(ext, __func__, __LINE__)
262 #define sanity_check(ext) ({ \
264 osc_object_lock((ext)->oe_obj); \
265 __res = sanity_check_nolock(ext); \
266 osc_object_unlock((ext)->oe_obj); \
272 * sanity check - to make sure there is no overlapped extent in the tree.
274 static int osc_extent_is_overlapped(struct osc_object *obj,
275 struct osc_extent *ext)
277 struct osc_extent *tmp;
279 LASSERT(osc_object_is_locked(obj));
284 for (tmp = first_extent(obj); tmp != NULL; tmp = next_extent(tmp)) {
287 if (tmp->oe_end >= ext->oe_start &&
288 tmp->oe_start <= ext->oe_end)
294 static void osc_extent_state_set(struct osc_extent *ext, int state)
296 LASSERT(osc_object_is_locked(ext->oe_obj));
297 LASSERT(state >= OES_INV && state < OES_STATE_MAX);
299 /* Never try to sanity check a state changing extent :-) */
300 /* LASSERT(sanity_check_nolock(ext) == 0); */
302 /* TODO: validate the state machine */
303 ext->oe_state = state;
304 cfs_waitq_broadcast(&ext->oe_waitq);
307 static struct osc_extent *osc_extent_alloc(struct osc_object *obj)
309 struct osc_extent *ext;
311 OBD_SLAB_ALLOC_PTR_GFP(ext, osc_extent_kmem, CFS_ALLOC_STD);
315 RB_CLEAR_NODE(&ext->oe_node);
317 cfs_atomic_set(&ext->oe_refc, 1);
318 cfs_atomic_set(&ext->oe_users, 0);
319 CFS_INIT_LIST_HEAD(&ext->oe_link);
320 ext->oe_state = OES_INV;
321 CFS_INIT_LIST_HEAD(&ext->oe_pages);
322 cfs_waitq_init(&ext->oe_waitq);
323 ext->oe_osclock = NULL;
328 static void osc_extent_free(struct osc_extent *ext)
330 OBD_SLAB_FREE_PTR(ext, osc_extent_kmem);
333 static struct osc_extent *osc_extent_get(struct osc_extent *ext)
335 LASSERT(cfs_atomic_read(&ext->oe_refc) >= 0);
336 cfs_atomic_inc(&ext->oe_refc);
340 static void osc_extent_put(const struct lu_env *env, struct osc_extent *ext)
342 LASSERT(cfs_atomic_read(&ext->oe_refc) > 0);
343 if (cfs_atomic_dec_and_test(&ext->oe_refc)) {
344 LASSERT(cfs_list_empty(&ext->oe_link));
345 LASSERT(cfs_atomic_read(&ext->oe_users) == 0);
346 LASSERT(ext->oe_state == OES_INV);
347 LASSERT(!ext->oe_intree);
349 if (ext->oe_osclock) {
350 cl_lock_put(env, ext->oe_osclock);
351 ext->oe_osclock = NULL;
353 osc_extent_free(ext);
358 * osc_extent_put_trust() is a special version of osc_extent_put() when
359 * it's known that the caller is not the last user. This is to address the
360 * problem of lacking of lu_env ;-).
362 static void osc_extent_put_trust(struct osc_extent *ext)
364 LASSERT(cfs_atomic_read(&ext->oe_refc) > 1);
365 LASSERT(osc_object_is_locked(ext->oe_obj));
366 cfs_atomic_dec(&ext->oe_refc);
370 * Return the extent which includes pgoff @index, or return the greatest
371 * previous extent in the tree.
373 static struct osc_extent *osc_extent_search(struct osc_object *obj,
376 struct rb_node *n = obj->oo_root.rb_node;
377 struct osc_extent *tmp, *p = NULL;
379 LASSERT(osc_object_is_locked(obj));
382 if (index < tmp->oe_start) {
384 } else if (index > tmp->oe_end) {
395 * Return the extent covering @index, otherwise return NULL.
396 * caller must have held object lock.
398 static struct osc_extent *osc_extent_lookup(struct osc_object *obj,
401 struct osc_extent *ext;
403 ext = osc_extent_search(obj, index);
404 if (ext != NULL && ext->oe_start <= index && index <= ext->oe_end)
405 return osc_extent_get(ext);
409 /* caller must have held object lock. */
410 static void osc_extent_insert(struct osc_object *obj, struct osc_extent *ext)
412 struct rb_node **n = &obj->oo_root.rb_node;
413 struct rb_node *parent = NULL;
414 struct osc_extent *tmp;
416 LASSERT(ext->oe_intree == 0);
417 LASSERT(ext->oe_obj == obj);
418 LASSERT(osc_object_is_locked(obj));
423 if (ext->oe_end < tmp->oe_start)
425 else if (ext->oe_start > tmp->oe_end)
428 EASSERTF(0, tmp, EXTSTR, EXTPARA(ext));
430 rb_link_node(&ext->oe_node, parent, n);
431 rb_insert_color(&ext->oe_node, &obj->oo_root);
436 /* caller must have held object lock. */
437 static void osc_extent_erase(struct osc_extent *ext)
439 struct osc_object *obj = ext->oe_obj;
440 LASSERT(osc_object_is_locked(obj));
441 if (ext->oe_intree) {
442 rb_erase(&ext->oe_node, &obj->oo_root);
444 /* rbtree held a refcount */
445 osc_extent_put_trust(ext);
449 static struct osc_extent *osc_extent_hold(struct osc_extent *ext)
451 struct osc_object *obj = ext->oe_obj;
453 LASSERT(osc_object_is_locked(obj));
454 LASSERT(ext->oe_state == OES_ACTIVE || ext->oe_state == OES_CACHE);
455 if (ext->oe_state == OES_CACHE) {
456 osc_extent_state_set(ext, OES_ACTIVE);
457 osc_update_pending(obj, OBD_BRW_WRITE, -ext->oe_nr_pages);
459 cfs_atomic_inc(&ext->oe_users);
460 cfs_list_del_init(&ext->oe_link);
461 return osc_extent_get(ext);
464 static void __osc_extent_remove(struct osc_extent *ext)
466 LASSERT(osc_object_is_locked(ext->oe_obj));
467 LASSERT(cfs_list_empty(&ext->oe_pages));
468 osc_extent_erase(ext);
469 cfs_list_del_init(&ext->oe_link);
470 osc_extent_state_set(ext, OES_INV);
471 OSC_EXTENT_DUMP(D_CACHE, ext, "destroyed.\n");
474 static void osc_extent_remove(struct osc_extent *ext)
476 struct osc_object *obj = ext->oe_obj;
478 osc_object_lock(obj);
479 __osc_extent_remove(ext);
480 osc_object_unlock(obj);
484 * This function is used to merge extents to get better performance. It checks
485 * if @cur and @victim are contiguous at chunk level.
487 static int osc_extent_merge(const struct lu_env *env, struct osc_extent *cur,
488 struct osc_extent *victim)
490 struct osc_object *obj = cur->oe_obj;
495 LASSERT(cur->oe_state == OES_CACHE);
496 LASSERT(osc_object_is_locked(obj));
500 if (victim->oe_state != OES_CACHE || victim->oe_fsync_wait)
503 if (cur->oe_max_end != victim->oe_max_end)
506 LASSERT(cur->oe_osclock == victim->oe_osclock);
507 ppc_bits = osc_cli(obj)->cl_chunkbits - CFS_PAGE_SHIFT;
508 chunk_start = cur->oe_start >> ppc_bits;
509 chunk_end = cur->oe_end >> ppc_bits;
510 if (chunk_start != (victim->oe_end >> ppc_bits) + 1 &&
511 chunk_end + 1 != victim->oe_start >> ppc_bits)
514 OSC_EXTENT_DUMP(D_CACHE, victim, "will be merged by %p.\n", cur);
516 cur->oe_start = min(cur->oe_start, victim->oe_start);
517 cur->oe_end = max(cur->oe_end, victim->oe_end);
518 cur->oe_grants += victim->oe_grants;
519 cur->oe_nr_pages += victim->oe_nr_pages;
520 /* only the following bits are needed to merge */
521 cur->oe_urgent |= victim->oe_urgent;
522 cur->oe_memalloc |= victim->oe_memalloc;
523 cfs_list_splice_init(&victim->oe_pages, &cur->oe_pages);
524 cfs_list_del_init(&victim->oe_link);
525 victim->oe_nr_pages = 0;
527 osc_extent_get(victim);
528 __osc_extent_remove(victim);
529 osc_extent_put(env, victim);
531 OSC_EXTENT_DUMP(D_CACHE, cur, "after merging %p.\n", victim);
536 * Drop user count of osc_extent, and unplug IO asynchronously.
538 int osc_extent_release(const struct lu_env *env, struct osc_extent *ext)
540 struct osc_object *obj = ext->oe_obj;
544 LASSERT(cfs_atomic_read(&ext->oe_users) > 0);
545 LASSERT(sanity_check(ext) == 0);
546 LASSERT(ext->oe_grants > 0);
548 if (cfs_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;
557 osc_extent_state_set(ext, OES_CACHE);
558 osc_update_pending(obj, OBD_BRW_WRITE,
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));
566 cfs_list_move_tail(&ext->oe_link,
567 &obj->oo_urgent_exts);
569 osc_object_unlock(obj);
571 osc_io_unplug_async(env, osc_cli(obj), obj);
573 osc_extent_put(env, ext);
577 static inline int overlapped(struct osc_extent *ex1, struct osc_extent *ex2)
579 return !(ex1->oe_end < ex2->oe_start || ex2->oe_end < ex1->oe_start);
583 * Find or create an extent which includes @index, core function to manage
586 struct osc_extent *osc_extent_find(const struct lu_env *env,
587 struct osc_object *obj, pgoff_t index,
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;
599 int max_pages; /* max_pages_per_rpc */
601 int ppc_bits; /* pages per chunk bits */
606 cur = osc_extent_alloc(obj);
608 RETURN(ERR_PTR(-ENOMEM));
610 lock = cl_lock_at_pgoff(env, osc2cl(obj), index, NULL, 1, 0);
611 LASSERT(lock != NULL);
612 LASSERT(lock->cll_descr.cld_mode >= CLM_WRITE);
614 LASSERT(cli->cl_chunkbits >= CFS_PAGE_SHIFT);
615 ppc_bits = cli->cl_chunkbits - CFS_PAGE_SHIFT;
616 chunk_mask = ~((1 << ppc_bits) - 1);
617 chunksize = 1 << cli->cl_chunkbits;
618 chunk = index >> ppc_bits;
620 /* align end to rpc edge, rpc size may not be a power 2 integer. */
621 max_pages = cli->cl_max_pages_per_rpc;
622 LASSERT((max_pages & ~chunk_mask) == 0);
623 max_end = index - (index % max_pages) + max_pages - 1;
624 max_end = min_t(pgoff_t, max_end, lock->cll_descr.cld_end);
626 /* initialize new extent by parameters so far */
627 cur->oe_max_end = max_end;
628 cur->oe_start = index & chunk_mask;
629 cur->oe_end = ((index + ~chunk_mask + 1) & chunk_mask) - 1;
630 if (cur->oe_start < lock->cll_descr.cld_start)
631 cur->oe_start = lock->cll_descr.cld_start;
632 if (cur->oe_end > max_end)
633 cur->oe_end = max_end;
634 cur->oe_osclock = lock;
636 cur->oe_mppr = max_pages;
638 /* grants has been allocated by caller */
639 LASSERTF(*grants >= chunksize + cli->cl_extent_tax,
640 "%u/%u/%u.\n", *grants, chunksize, cli->cl_extent_tax);
641 LASSERTF((max_end - cur->oe_start) < max_pages, EXTSTR, EXTPARA(cur));
644 osc_object_lock(obj);
645 ext = osc_extent_search(obj, cur->oe_start);
647 ext = first_extent(obj);
648 while (ext != NULL) {
649 loff_t ext_chk_start = ext->oe_start >> ppc_bits;
650 loff_t ext_chk_end = ext->oe_end >> ppc_bits;
652 LASSERT(sanity_check_nolock(ext) == 0);
653 if (chunk > ext_chk_end + 1)
656 /* if covering by different locks, no chance to match */
657 if (lock != ext->oe_osclock) {
658 EASSERTF(!overlapped(ext, cur), ext,
659 EXTSTR, EXTPARA(cur));
661 ext = next_extent(ext);
665 /* discontiguous chunks? */
666 if (chunk + 1 < ext_chk_start) {
667 ext = next_extent(ext);
671 /* ok, from now on, ext and cur have these attrs:
672 * 1. covered by the same lock
673 * 2. contiguous at chunk level or overlapping. */
675 if (overlapped(ext, cur)) {
676 /* cur is the minimum unit, so overlapping means
678 EASSERTF((ext->oe_start <= cur->oe_start &&
679 ext->oe_end >= cur->oe_end),
680 ext, EXTSTR, EXTPARA(cur));
682 if (ext->oe_state > OES_CACHE || ext->oe_fsync_wait) {
683 /* for simplicity, we wait for this extent to
684 * finish before going forward. */
685 conflict = osc_extent_get(ext);
689 found = osc_extent_hold(ext);
693 /* non-overlapped extent */
694 if (ext->oe_state != OES_CACHE || ext->oe_fsync_wait) {
695 /* we can't do anything for a non OES_CACHE extent, or
696 * if there is someone waiting for this extent to be
697 * flushed, try next one. */
698 ext = next_extent(ext);
702 /* check if they belong to the same rpc slot before trying to
703 * merge. the extents are not overlapped and contiguous at
704 * chunk level to get here. */
705 if (ext->oe_max_end != max_end) {
706 /* if they don't belong to the same RPC slot or
707 * max_pages_per_rpc has ever changed, do not merge. */
708 ext = next_extent(ext);
712 /* it's required that an extent must be contiguous at chunk
713 * level so that we know the whole extent is covered by grant
714 * (the pages in the extent are NOT required to be contiguous).
715 * Otherwise, it will be too much difficult to know which
716 * chunks have grants allocated. */
718 /* try to do front merge - extend ext's start */
719 if (chunk + 1 == ext_chk_start) {
720 /* ext must be chunk size aligned */
721 EASSERT((ext->oe_start & ~chunk_mask) == 0, ext);
723 /* pull ext's start back to cover cur */
724 ext->oe_start = cur->oe_start;
725 ext->oe_grants += chunksize;
726 *grants -= chunksize;
728 found = osc_extent_hold(ext);
729 } else if (chunk == ext_chk_end + 1) {
731 ext->oe_end = cur->oe_end;
732 ext->oe_grants += chunksize;
733 *grants -= chunksize;
735 /* try to merge with the next one because we just fill
737 if (osc_extent_merge(env, ext, next_extent(ext)) == 0)
738 /* we can save extent tax from next extent */
739 *grants += cli->cl_extent_tax;
741 found = osc_extent_hold(ext);
746 ext = next_extent(ext);
749 osc_extent_tree_dump(D_CACHE, obj);
751 LASSERT(conflict == NULL);
752 if (!IS_ERR(found)) {
753 LASSERT(found->oe_osclock == cur->oe_osclock);
754 OSC_EXTENT_DUMP(D_CACHE, found,
755 "found caching ext for %lu.\n", index);
757 } else if (conflict == NULL) {
758 /* create a new extent */
759 EASSERT(osc_extent_is_overlapped(obj, cur) == 0, cur);
760 cur->oe_grants = chunksize + cli->cl_extent_tax;
761 *grants -= cur->oe_grants;
762 LASSERT(*grants >= 0);
764 cur->oe_state = OES_CACHE;
765 found = osc_extent_hold(cur);
766 osc_extent_insert(obj, cur);
767 OSC_EXTENT_DUMP(D_CACHE, cur, "add into tree %lu/%lu.\n",
768 index, lock->cll_descr.cld_end);
770 osc_object_unlock(obj);
772 if (conflict != NULL) {
773 LASSERT(found == NULL);
775 /* waiting for IO to finish. Please notice that it's impossible
776 * to be an OES_TRUNC extent. */
777 rc = osc_extent_wait(env, conflict, OES_INV);
778 osc_extent_put(env, conflict);
781 GOTO(out, found = ERR_PTR(rc));
788 osc_extent_put(env, cur);
789 LASSERT(*grants >= 0);
794 * Called when IO is finished to an extent.
796 int osc_extent_finish(const struct lu_env *env, struct osc_extent *ext,
799 struct client_obd *cli = osc_cli(ext->oe_obj);
800 struct osc_async_page *oap;
801 struct osc_async_page *tmp;
802 int nr_pages = ext->oe_nr_pages;
804 int blocksize = cli->cl_import->imp_obd->obd_osfs.os_bsize ? : 4096;
809 OSC_EXTENT_DUMP(D_CACHE, ext, "extent finished.\n");
811 ext->oe_rc = rc ?: ext->oe_nr_pages;
812 EASSERT(ergo(rc == 0, ext->oe_state == OES_RPC), ext);
813 cfs_list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
815 cfs_list_del_init(&oap->oap_rpc_item);
816 cfs_list_del_init(&oap->oap_pending_item);
817 if (last_off <= oap->oap_obj_off) {
818 last_off = oap->oap_obj_off;
819 last_count = oap->oap_count;
823 osc_ap_completion(env, cli, oap, sent, rc);
825 EASSERT(ext->oe_nr_pages == 0, ext);
828 lost_grant = ext->oe_grants;
829 } else if (blocksize < CFS_PAGE_SIZE &&
830 last_count != CFS_PAGE_SIZE) {
831 /* For short writes we shouldn't count parts of pages that
832 * span a whole chunk on the OST side, or our accounting goes
833 * wrong. Should match the code in filter_grant_check. */
834 int offset = oap->oap_page_off & ~CFS_PAGE_MASK;
835 int count = oap->oap_count + (offset & (blocksize - 1));
836 int end = (offset + oap->oap_count) & (blocksize - 1);
838 count += blocksize - end;
840 lost_grant = CFS_PAGE_SIZE - count;
842 if (ext->oe_grants > 0)
843 osc_free_grant(cli, nr_pages, lost_grant);
845 osc_extent_remove(ext);
846 /* put the refcount for RPC */
847 osc_extent_put(env, ext);
851 static int extent_wait_cb(struct osc_extent *ext, int state)
855 osc_object_lock(ext->oe_obj);
856 ret = ext->oe_state == state;
857 osc_object_unlock(ext->oe_obj);
863 * Wait for the extent's state to become @state.
865 static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
868 struct osc_object *obj = ext->oe_obj;
869 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL,
870 LWI_ON_SIGNAL_NOOP, NULL);
874 osc_object_lock(obj);
875 LASSERT(sanity_check_nolock(ext) == 0);
876 /* `Kick' this extent only if the caller is waiting for it to be
878 if (state == OES_INV && !ext->oe_urgent && !ext->oe_hp) {
879 if (ext->oe_state == OES_ACTIVE) {
881 } else if (ext->oe_state == OES_CACHE) {
883 osc_extent_hold(ext);
887 osc_object_unlock(obj);
889 osc_extent_release(env, ext);
891 /* wait for the extent until its state becomes @state */
892 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), &lwi);
893 if (rc == -ETIMEDOUT) {
894 OSC_EXTENT_DUMP(D_ERROR, ext,
895 "%s: wait ext to %d timedout, recovery in progress?\n",
896 osc_export(obj)->exp_obd->obd_name, state);
898 lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
899 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state),
902 if (rc == 0 && ext->oe_rc < 0)
908 * Discard pages with index greater than @size. If @ext is overlapped with
909 * @size, then partial truncate happens.
911 static int osc_extent_truncate(struct osc_extent *ext, pgoff_t trunc_index,
914 struct cl_env_nest nest;
917 struct osc_object *obj = ext->oe_obj;
918 struct client_obd *cli = osc_cli(obj);
919 struct osc_async_page *oap;
920 struct osc_async_page *tmp;
921 int pages_in_chunk = 0;
922 int ppc_bits = cli->cl_chunkbits - CFS_PAGE_SHIFT;
923 __u64 trunc_chunk = trunc_index >> ppc_bits;
929 LASSERT(sanity_check(ext) == 0);
930 LASSERT(ext->oe_state == OES_TRUNC);
931 LASSERT(!ext->oe_urgent);
933 /* Request new lu_env.
934 * We can't use that env from osc_cache_truncate_start() because
935 * it's from lov_io_sub and not fully initialized. */
936 env = cl_env_nested_get(&nest);
937 io = &osc_env_info(env)->oti_io;
938 io->ci_obj = cl_object_top(osc2cl(obj));
939 rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
943 /* discard all pages with index greater then trunc_index */
944 cfs_list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
946 struct cl_page *sub = oap2cl_page(oap);
947 struct cl_page *page = cl_page_top(sub);
949 LASSERT(cfs_list_empty(&oap->oap_rpc_item));
951 /* only discard the pages with their index greater than
952 * trunc_index, and ... */
953 if (sub->cp_index < trunc_index ||
954 (sub->cp_index == trunc_index && partial)) {
955 /* accounting how many pages remaining in the chunk
956 * so that we can calculate grants correctly. */
957 if (sub->cp_index >> ppc_bits == trunc_chunk)
962 cfs_list_del_init(&oap->oap_pending_item);
965 lu_ref_add(&page->cp_reference, "truncate", cfs_current());
967 if (cl_page_own(env, io, page) == 0) {
968 cl_page_unmap(env, io, page);
969 cl_page_discard(env, io, page);
970 cl_page_disown(env, io, page);
972 LASSERT(page->cp_state == CPS_FREEING);
976 lu_ref_del(&page->cp_reference, "truncate", cfs_current());
977 cl_page_put(env, page);
982 EASSERTF(ergo(ext->oe_start >= trunc_index + !!partial,
983 ext->oe_nr_pages == 0),
984 ext, "trunc_index %lu, partial %d\n", trunc_index, partial);
986 osc_object_lock(obj);
987 if (ext->oe_nr_pages == 0) {
988 LASSERT(pages_in_chunk == 0);
989 grants = ext->oe_grants;
991 } else { /* calculate how many grants we can free */
992 int chunks = (ext->oe_end >> ppc_bits) - trunc_chunk;
996 /* if there is no pages in this chunk, we can also free grants
997 * for the last chunk */
998 if (pages_in_chunk == 0) {
999 /* if this is the 1st chunk and no pages in this chunk,
1000 * ext->oe_nr_pages must be zero, so we should be in
1001 * the other if-clause. */
1002 LASSERT(trunc_chunk > 0);
1007 /* this is what we can free from this extent */
1008 grants = chunks << cli->cl_chunkbits;
1009 ext->oe_grants -= grants;
1010 last_index = ((trunc_chunk + 1) << ppc_bits) - 1;
1011 ext->oe_end = min(last_index, ext->oe_max_end);
1012 LASSERT(ext->oe_end >= ext->oe_start);
1013 LASSERT(ext->oe_grants > 0);
1015 osc_object_unlock(obj);
1017 if (grants > 0 || nr_pages > 0)
1018 osc_free_grant(cli, nr_pages, grants);
1021 cl_io_fini(env, io);
1022 cl_env_nested_put(&nest, env);
1027 * This function is used to make the extent prepared for transfer.
1028 * A race with flusing page - ll_writepage() has to be handled cautiously.
1030 static int osc_extent_make_ready(const struct lu_env *env,
1031 struct osc_extent *ext)
1033 struct osc_async_page *oap;
1034 struct osc_async_page *last = NULL;
1035 struct osc_object *obj = ext->oe_obj;
1040 /* we're going to grab page lock, so object lock must not be taken. */
1041 LASSERT(sanity_check(ext) == 0);
1042 /* in locking state, any process should not touch this extent. */
1043 EASSERT(ext->oe_state == OES_LOCKING, ext);
1044 EASSERT(ext->oe_owner != NULL, ext);
1046 OSC_EXTENT_DUMP(D_CACHE, ext, "make ready\n");
1048 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1050 if (last == NULL || last->oap_obj_off < oap->oap_obj_off)
1053 /* checking ASYNC_READY is race safe */
1054 if ((oap->oap_async_flags & ASYNC_READY) != 0)
1057 rc = osc_make_ready(env, oap, OBD_BRW_WRITE);
1060 spin_lock(&oap->oap_lock);
1061 oap->oap_async_flags |= ASYNC_READY;
1062 spin_unlock(&oap->oap_lock);
1065 LASSERT((oap->oap_async_flags & ASYNC_READY) != 0);
1068 LASSERTF(0, "unknown return code: %d\n", rc);
1072 LASSERT(page_count == ext->oe_nr_pages);
1073 LASSERT(last != NULL);
1074 /* the last page is the only one we need to refresh its count by
1075 * the size of file. */
1076 if (!(last->oap_async_flags & ASYNC_COUNT_STABLE)) {
1077 last->oap_count = osc_refresh_count(env, last, OBD_BRW_WRITE);
1078 LASSERT(last->oap_count > 0);
1079 LASSERT(last->oap_page_off + last->oap_count <= CFS_PAGE_SIZE);
1080 last->oap_async_flags |= ASYNC_COUNT_STABLE;
1083 /* for the rest of pages, we don't need to call osf_refresh_count()
1084 * because it's known they are not the last page */
1085 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1086 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE)) {
1087 oap->oap_count = CFS_PAGE_SIZE - oap->oap_page_off;
1088 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1092 osc_object_lock(obj);
1093 osc_extent_state_set(ext, OES_RPC);
1094 osc_object_unlock(obj);
1095 /* get a refcount for RPC. */
1096 osc_extent_get(ext);
1102 * Quick and simple version of osc_extent_find(). This function is frequently
1103 * called to expand the extent for the same IO. To expand the extent, the
1104 * page index must be in the same or next chunk of ext->oe_end.
1106 static int osc_extent_expand(struct osc_extent *ext, pgoff_t index, int *grants)
1108 struct osc_object *obj = ext->oe_obj;
1109 struct client_obd *cli = osc_cli(obj);
1110 struct osc_extent *next;
1111 int ppc_bits = cli->cl_chunkbits - CFS_PAGE_SHIFT;
1112 pgoff_t chunk = index >> ppc_bits;
1115 int chunksize = 1 << cli->cl_chunkbits;
1119 LASSERT(ext->oe_max_end >= index && ext->oe_start <= index);
1120 osc_object_lock(obj);
1121 LASSERT(sanity_check_nolock(ext) == 0);
1122 end_chunk = ext->oe_end >> ppc_bits;
1123 if (chunk > end_chunk + 1)
1124 GOTO(out, rc = -ERANGE);
1126 if (end_chunk >= chunk)
1129 LASSERT(end_chunk + 1 == chunk);
1130 /* try to expand this extent to cover @index */
1131 end_index = min(ext->oe_max_end, ((chunk + 1) << ppc_bits) - 1);
1133 next = next_extent(ext);
1134 if (next != NULL && next->oe_start <= end_index)
1135 /* complex mode - overlapped with the next extent,
1136 * this case will be handled by osc_extent_find() */
1137 GOTO(out, rc = -EAGAIN);
1139 ext->oe_end = end_index;
1140 ext->oe_grants += chunksize;
1141 *grants -= chunksize;
1142 LASSERT(*grants >= 0);
1143 EASSERTF(osc_extent_is_overlapped(obj, ext) == 0, ext,
1144 "overlapped after expanding for %lu.\n", index);
1148 osc_object_unlock(obj);
1152 static void osc_extent_tree_dump0(int level, struct osc_object *obj,
1153 const char *func, int line)
1155 struct osc_extent *ext;
1158 CDEBUG(level, "Dump object %p extents at %s:%d, mppr: %u.\n",
1159 obj, func, line, osc_cli(obj)->cl_max_pages_per_rpc);
1161 /* osc_object_lock(obj); */
1163 for (ext = first_extent(obj); ext != NULL; ext = next_extent(ext))
1164 OSC_EXTENT_DUMP(level, ext, "in tree %d.\n", cnt++);
1167 cfs_list_for_each_entry(ext, &obj->oo_hp_exts, oe_link)
1168 OSC_EXTENT_DUMP(level, ext, "hp %d.\n", cnt++);
1171 cfs_list_for_each_entry(ext, &obj->oo_urgent_exts, oe_link)
1172 OSC_EXTENT_DUMP(level, ext, "urgent %d.\n", cnt++);
1175 cfs_list_for_each_entry(ext, &obj->oo_reading_exts, oe_link)
1176 OSC_EXTENT_DUMP(level, ext, "reading %d.\n", cnt++);
1177 /* osc_object_unlock(obj); */
1180 /* ------------------ osc extent end ------------------ */
1182 static inline int osc_is_ready(struct osc_object *osc)
1184 return !cfs_list_empty(&osc->oo_ready_item) ||
1185 !cfs_list_empty(&osc->oo_hp_ready_item);
1188 #define OSC_IO_DEBUG(OSC, STR, args...) \
1189 CDEBUG(D_CACHE, "obj %p ready %d|%c|%c wr %d|%c|%c rd %d|%c " STR, \
1190 (OSC), osc_is_ready(OSC), \
1191 list_empty_marker(&(OSC)->oo_hp_ready_item), \
1192 list_empty_marker(&(OSC)->oo_ready_item), \
1193 cfs_atomic_read(&(OSC)->oo_nr_writes), \
1194 list_empty_marker(&(OSC)->oo_hp_exts), \
1195 list_empty_marker(&(OSC)->oo_urgent_exts), \
1196 cfs_atomic_read(&(OSC)->oo_nr_reads), \
1197 list_empty_marker(&(OSC)->oo_reading_exts), \
1200 static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
1203 struct osc_page *opg = oap2osc_page(oap);
1204 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1207 LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
1210 result = cl_page_make_ready(env, page, CRT_WRITE);
1212 opg->ops_submit_time = cfs_time_current();
1216 static int osc_refresh_count(const struct lu_env *env,
1217 struct osc_async_page *oap, int cmd)
1219 struct osc_page *opg = oap2osc_page(oap);
1220 struct cl_page *page = oap2cl_page(oap);
1221 struct cl_object *obj;
1222 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
1227 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
1228 LASSERT(!(cmd & OBD_BRW_READ));
1229 LASSERT(opg != NULL);
1230 obj = opg->ops_cl.cpl_obj;
1232 cl_object_attr_lock(obj);
1233 result = cl_object_attr_get(env, obj, attr);
1234 cl_object_attr_unlock(obj);
1237 kms = attr->cat_kms;
1238 if (cl_offset(obj, page->cp_index) >= kms)
1239 /* catch race with truncate */
1241 else if (cl_offset(obj, page->cp_index + 1) > kms)
1242 /* catch sub-page write at end of file */
1243 return kms % CFS_PAGE_SIZE;
1245 return CFS_PAGE_SIZE;
1248 static int osc_completion(const struct lu_env *env, struct osc_async_page *oap,
1251 struct osc_page *opg = oap2osc_page(oap);
1252 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1253 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
1254 enum cl_req_type crt;
1259 cmd &= ~OBD_BRW_NOQUOTA;
1260 LASSERT(equi(page->cp_state == CPS_PAGEIN, cmd == OBD_BRW_READ));
1261 LASSERT(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE));
1262 LASSERT(opg->ops_transfer_pinned);
1265 * page->cp_req can be NULL if io submission failed before
1266 * cl_req was allocated.
1268 if (page->cp_req != NULL)
1269 cl_req_page_done(env, page);
1270 LASSERT(page->cp_req == NULL);
1272 crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
1273 /* Clear opg->ops_transfer_pinned before VM lock is released. */
1274 opg->ops_transfer_pinned = 0;
1276 spin_lock(&obj->oo_seatbelt);
1277 LASSERT(opg->ops_submitter != NULL);
1278 LASSERT(!cfs_list_empty(&opg->ops_inflight));
1279 cfs_list_del_init(&opg->ops_inflight);
1280 opg->ops_submitter = NULL;
1281 spin_unlock(&obj->oo_seatbelt);
1283 opg->ops_submit_time = 0;
1284 srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK;
1287 if (rc == 0 && srvlock) {
1288 struct lu_device *ld = opg->ops_cl.cpl_obj->co_lu.lo_dev;
1289 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
1290 int bytes = oap->oap_count;
1292 if (crt == CRT_READ)
1293 stats->os_lockless_reads += bytes;
1295 stats->os_lockless_writes += bytes;
1299 * This has to be the last operation with the page, as locks are
1300 * released in cl_page_completion() and nothing except for the
1301 * reference counter protects page from concurrent reclaim.
1303 lu_ref_del(&page->cp_reference, "transfer", page);
1305 cl_page_completion(env, page, crt, rc);
1310 #define OSC_DUMP_GRANT(cli, fmt, args...) do { \
1311 struct client_obd *__tmp = (cli); \
1312 CDEBUG(D_CACHE, "%s: { dirty: %ld/%ld dirty_pages: %d/%d " \
1313 "dropped: %ld avail: %ld, reserved: %ld, flight: %d } " fmt, \
1314 __tmp->cl_import->imp_obd->obd_name, \
1315 __tmp->cl_dirty, __tmp->cl_dirty_max, \
1316 cfs_atomic_read(&obd_dirty_pages), obd_max_dirty_pages, \
1317 __tmp->cl_lost_grant, __tmp->cl_avail_grant, \
1318 __tmp->cl_reserved_grant, __tmp->cl_w_in_flight, ##args); \
1321 /* caller must hold loi_list_lock */
1322 static void osc_consume_write_grant(struct client_obd *cli,
1323 struct brw_page *pga)
1325 LASSERT_SPIN_LOCKED(&cli->cl_loi_list_lock.lock);
1326 LASSERT(!(pga->flag & OBD_BRW_FROM_GRANT));
1327 cfs_atomic_inc(&obd_dirty_pages);
1328 cli->cl_dirty += CFS_PAGE_SIZE;
1329 pga->flag |= OBD_BRW_FROM_GRANT;
1330 CDEBUG(D_CACHE, "using %lu grant credits for brw %p page %p\n",
1331 CFS_PAGE_SIZE, pga, pga->pg);
1332 osc_update_next_shrink(cli);
1335 /* the companion to osc_consume_write_grant, called when a brw has completed.
1336 * must be called with the loi lock held. */
1337 static void osc_release_write_grant(struct client_obd *cli,
1338 struct brw_page *pga)
1342 LASSERT_SPIN_LOCKED(&cli->cl_loi_list_lock.lock);
1343 if (!(pga->flag & OBD_BRW_FROM_GRANT)) {
1348 pga->flag &= ~OBD_BRW_FROM_GRANT;
1349 cfs_atomic_dec(&obd_dirty_pages);
1350 cli->cl_dirty -= CFS_PAGE_SIZE;
1351 if (pga->flag & OBD_BRW_NOCACHE) {
1352 pga->flag &= ~OBD_BRW_NOCACHE;
1353 cfs_atomic_dec(&obd_dirty_transit_pages);
1354 cli->cl_dirty_transit -= CFS_PAGE_SIZE;
1360 * To avoid sleeping with object lock held, it's good for us allocate enough
1361 * grants before entering into critical section.
1363 * client_obd_list_lock held by caller
1365 static int osc_reserve_grant(struct client_obd *cli, unsigned int bytes)
1369 if (cli->cl_avail_grant >= bytes) {
1370 cli->cl_avail_grant -= bytes;
1371 cli->cl_reserved_grant += bytes;
1377 static void __osc_unreserve_grant(struct client_obd *cli,
1378 unsigned int reserved, unsigned int unused)
1380 /* it's quite normal for us to get more grant than reserved.
1381 * Thinking about a case that two extents merged by adding a new
1382 * chunk, we can save one extent tax. If extent tax is greater than
1383 * one chunk, we can save more grant by adding a new chunk */
1384 cli->cl_reserved_grant -= reserved;
1385 if (unused > reserved) {
1386 cli->cl_avail_grant += reserved;
1387 cli->cl_lost_grant += unused - reserved;
1389 cli->cl_avail_grant += unused;
1393 void osc_unreserve_grant(struct client_obd *cli,
1394 unsigned int reserved, unsigned int unused)
1396 client_obd_list_lock(&cli->cl_loi_list_lock);
1397 __osc_unreserve_grant(cli, reserved, unused);
1399 osc_wake_cache_waiters(cli);
1400 client_obd_list_unlock(&cli->cl_loi_list_lock);
1404 * Free grant after IO is finished or canceled.
1406 * @lost_grant is used to remember how many grants we have allocated but not
1407 * used, we should return these grants to OST. There're two cases where grants
1410 * 2. blocksize at OST is less than CFS_PAGE_SIZE and a partial page was
1411 * written. In this case OST may use less chunks to serve this partial
1412 * write. OSTs don't actually know the page size on the client side. so
1413 * clients have to calculate lost grant by the blocksize on the OST.
1414 * See filter_grant_check() for details.
1416 static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
1417 unsigned int lost_grant)
1419 int grant = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
1421 client_obd_list_lock(&cli->cl_loi_list_lock);
1422 cfs_atomic_sub(nr_pages, &obd_dirty_pages);
1423 cli->cl_dirty -= nr_pages << CFS_PAGE_SHIFT;
1424 cli->cl_lost_grant += lost_grant;
1425 if (cli->cl_avail_grant < grant && cli->cl_lost_grant >= grant) {
1426 /* borrow some grant from truncate to avoid the case that
1427 * truncate uses up all avail grant */
1428 cli->cl_lost_grant -= grant;
1429 cli->cl_avail_grant += grant;
1431 osc_wake_cache_waiters(cli);
1432 client_obd_list_unlock(&cli->cl_loi_list_lock);
1433 CDEBUG(D_CACHE, "lost %u grant: %lu avail: %lu dirty: %lu\n",
1434 lost_grant, cli->cl_lost_grant,
1435 cli->cl_avail_grant, cli->cl_dirty);
1439 * The companion to osc_enter_cache(), called when @oap is no longer part of
1440 * the dirty accounting due to error.
1442 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1444 client_obd_list_lock(&cli->cl_loi_list_lock);
1445 osc_release_write_grant(cli, &oap->oap_brw_page);
1446 client_obd_list_unlock(&cli->cl_loi_list_lock);
1450 * Non-blocking version of osc_enter_cache() that consumes grant only when it
1453 static int osc_enter_cache_try(struct client_obd *cli,
1454 struct osc_async_page *oap,
1455 int bytes, int transient)
1459 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1461 rc = osc_reserve_grant(cli, bytes);
1465 if (cli->cl_dirty + CFS_PAGE_SIZE <= cli->cl_dirty_max &&
1466 cfs_atomic_read(&obd_dirty_pages) + 1 <= obd_max_dirty_pages) {
1467 osc_consume_write_grant(cli, &oap->oap_brw_page);
1469 cli->cl_dirty_transit += CFS_PAGE_SIZE;
1470 cfs_atomic_inc(&obd_dirty_transit_pages);
1471 oap->oap_brw_flags |= OBD_BRW_NOCACHE;
1475 __osc_unreserve_grant(cli, bytes, bytes);
1481 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1484 client_obd_list_lock(&cli->cl_loi_list_lock);
1485 rc = cfs_list_empty(&ocw->ocw_entry);
1486 client_obd_list_unlock(&cli->cl_loi_list_lock);
1491 * The main entry to reserve dirty page accounting. Usually the grant reserved
1492 * in this function will be freed in bulk in osc_free_grant() unless it fails
1493 * to add osc cache, in that case, it will be freed in osc_exit_cache().
1495 * The process will be put into sleep if it's already run out of grant.
1497 static int osc_enter_cache(const struct lu_env *env, struct client_obd *cli,
1498 struct osc_async_page *oap, int bytes)
1500 struct osc_object *osc = oap->oap_obj;
1501 struct lov_oinfo *loi = osc->oo_oinfo;
1502 struct osc_cache_waiter ocw;
1503 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1507 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1509 client_obd_list_lock(&cli->cl_loi_list_lock);
1511 /* force the caller to try sync io. this can jump the list
1512 * of queued writes and create a discontiguous rpc stream */
1513 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_NO_GRANT) ||
1514 cli->cl_dirty_max < CFS_PAGE_SIZE ||
1515 cli->cl_ar.ar_force_sync || loi->loi_ar.ar_force_sync)
1516 GOTO(out, rc = -EDQUOT);
1518 /* Hopefully normal case - cache space and write credits available */
1519 if (osc_enter_cache_try(cli, oap, bytes, 0))
1522 /* We can get here for two reasons: too many dirty pages in cache, or
1523 * run out of grants. In both cases we should write dirty pages out.
1524 * Adding a cache waiter will trigger urgent write-out no matter what
1526 * The exiting condition is no avail grants and no dirty pages caching,
1527 * that really means there is no space on the OST. */
1528 cfs_waitq_init(&ocw.ocw_waitq);
1530 ocw.ocw_grant = bytes;
1531 while (cli->cl_dirty > 0 || cli->cl_w_in_flight > 0) {
1532 cfs_list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1534 client_obd_list_unlock(&cli->cl_loi_list_lock);
1536 osc_io_unplug_async(env, cli, NULL);
1538 CDEBUG(D_CACHE, "%s: sleeping for cache space @ %p for %p\n",
1539 cli->cl_import->imp_obd->obd_name, &ocw, oap);
1541 rc = l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1543 client_obd_list_lock(&cli->cl_loi_list_lock);
1545 /* l_wait_event is interrupted by signal */
1547 cfs_list_del_init(&ocw.ocw_entry);
1551 LASSERT(cfs_list_empty(&ocw.ocw_entry));
1556 if (osc_enter_cache_try(cli, oap, bytes, 0))
1561 client_obd_list_unlock(&cli->cl_loi_list_lock);
1562 OSC_DUMP_GRANT(cli, "returned %d.\n", rc);
1566 /* caller must hold loi_list_lock */
1567 void osc_wake_cache_waiters(struct client_obd *cli)
1569 cfs_list_t *l, *tmp;
1570 struct osc_cache_waiter *ocw;
1573 cfs_list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1574 ocw = cfs_list_entry(l, struct osc_cache_waiter, ocw_entry);
1575 cfs_list_del_init(&ocw->ocw_entry);
1577 ocw->ocw_rc = -EDQUOT;
1578 /* we can't dirty more */
1579 if ((cli->cl_dirty + CFS_PAGE_SIZE > cli->cl_dirty_max) ||
1580 (cfs_atomic_read(&obd_dirty_pages) + 1 >
1581 obd_max_dirty_pages)) {
1582 CDEBUG(D_CACHE, "no dirty room: dirty: %ld "
1583 "osc max %ld, sys max %d\n", cli->cl_dirty,
1584 cli->cl_dirty_max, obd_max_dirty_pages);
1589 if (!osc_enter_cache_try(cli, ocw->ocw_oap, ocw->ocw_grant, 0))
1590 ocw->ocw_rc = -EDQUOT;
1593 CDEBUG(D_CACHE, "wake up %p for oap %p, avail grant %ld, %d\n",
1594 ocw, ocw->ocw_oap, cli->cl_avail_grant, ocw->ocw_rc);
1596 cfs_waitq_signal(&ocw->ocw_waitq);
1602 static int osc_max_rpc_in_flight(struct client_obd *cli, struct osc_object *osc)
1604 int hprpc = !!cfs_list_empty(&osc->oo_hp_exts);
1605 return rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight + hprpc;
1608 /* This maintains the lists of pending pages to read/write for a given object
1609 * (lop). This is used by osc_check_rpcs->osc_next_obj() and osc_list_maint()
1610 * to quickly find objects that are ready to send an RPC. */
1611 static int osc_makes_rpc(struct client_obd *cli, struct osc_object *osc,
1614 int invalid_import = 0;
1617 /* if we have an invalid import we want to drain the queued pages
1618 * by forcing them through rpcs that immediately fail and complete
1619 * the pages. recovery relies on this to empty the queued pages
1620 * before canceling the locks and evicting down the llite pages */
1621 if ((cli->cl_import == NULL || cli->cl_import->imp_invalid))
1624 if (cmd & OBD_BRW_WRITE) {
1625 if (cfs_atomic_read(&osc->oo_nr_writes) == 0)
1627 if (invalid_import) {
1628 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1631 if (!cfs_list_empty(&osc->oo_hp_exts)) {
1632 CDEBUG(D_CACHE, "high prio request forcing RPC\n");
1635 if (!cfs_list_empty(&osc->oo_urgent_exts)) {
1636 CDEBUG(D_CACHE, "urgent request forcing RPC\n");
1639 /* trigger a write rpc stream as long as there are dirtiers
1640 * waiting for space. as they're waiting, they're not going to
1641 * create more pages to coalesce with what's waiting.. */
1642 if (!cfs_list_empty(&cli->cl_cache_waiters)) {
1643 CDEBUG(D_CACHE, "cache waiters forcing RPC\n");
1646 if (cfs_atomic_read(&osc->oo_nr_writes) >=
1647 cli->cl_max_pages_per_rpc)
1650 if (cfs_atomic_read(&osc->oo_nr_reads) == 0)
1652 if (invalid_import) {
1653 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1656 /* all read are urgent. */
1657 if (!cfs_list_empty(&osc->oo_reading_exts))
1664 static void osc_update_pending(struct osc_object *obj, int cmd, int delta)
1666 struct client_obd *cli = osc_cli(obj);
1667 if (cmd & OBD_BRW_WRITE) {
1668 cfs_atomic_add(delta, &obj->oo_nr_writes);
1669 cfs_atomic_add(delta, &cli->cl_pending_w_pages);
1670 LASSERT(cfs_atomic_read(&obj->oo_nr_writes) >= 0);
1672 cfs_atomic_add(delta, &obj->oo_nr_reads);
1673 cfs_atomic_add(delta, &cli->cl_pending_r_pages);
1674 LASSERT(cfs_atomic_read(&obj->oo_nr_reads) >= 0);
1676 OSC_IO_DEBUG(obj, "update pending cmd %d delta %d.\n", cmd, delta);
1679 static int osc_makes_hprpc(struct osc_object *obj)
1681 return !cfs_list_empty(&obj->oo_hp_exts);
1684 static void on_list(cfs_list_t *item, cfs_list_t *list, int should_be_on)
1686 if (cfs_list_empty(item) && should_be_on)
1687 cfs_list_add_tail(item, list);
1688 else if (!cfs_list_empty(item) && !should_be_on)
1689 cfs_list_del_init(item);
1692 /* maintain the osc's cli list membership invariants so that osc_send_oap_rpc
1693 * can find pages to build into rpcs quickly */
1694 static int __osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1696 if (osc_makes_hprpc(osc)) {
1698 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, 0);
1699 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 1);
1701 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 0);
1702 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list,
1703 osc_makes_rpc(cli, osc, OBD_BRW_WRITE) ||
1704 osc_makes_rpc(cli, osc, OBD_BRW_READ));
1707 on_list(&osc->oo_write_item, &cli->cl_loi_write_list,
1708 cfs_atomic_read(&osc->oo_nr_writes) > 0);
1710 on_list(&osc->oo_read_item, &cli->cl_loi_read_list,
1711 cfs_atomic_read(&osc->oo_nr_reads) > 0);
1713 return osc_is_ready(osc);
1716 static int osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1720 client_obd_list_lock(&cli->cl_loi_list_lock);
1721 is_ready = __osc_list_maint(cli, osc);
1722 client_obd_list_unlock(&cli->cl_loi_list_lock);
1727 /* this is trying to propogate async writeback errors back up to the
1728 * application. As an async write fails we record the error code for later if
1729 * the app does an fsync. As long as errors persist we force future rpcs to be
1730 * sync so that the app can get a sync error and break the cycle of queueing
1731 * pages for which writeback will fail. */
1732 static void osc_process_ar(struct osc_async_rc *ar, __u64 xid,
1739 ar->ar_force_sync = 1;
1740 ar->ar_min_xid = ptlrpc_sample_next_xid();
1745 if (ar->ar_force_sync && (xid >= ar->ar_min_xid))
1746 ar->ar_force_sync = 0;
1749 /* this must be called holding the loi list lock to give coverage to exit_cache,
1750 * async_flag maintenance, and oap_request */
1751 static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
1752 struct osc_async_page *oap, int sent, int rc)
1754 struct osc_object *osc = oap->oap_obj;
1755 struct lov_oinfo *loi = osc->oo_oinfo;
1759 if (oap->oap_request != NULL) {
1760 xid = ptlrpc_req_xid(oap->oap_request);
1761 ptlrpc_req_finished(oap->oap_request);
1762 oap->oap_request = NULL;
1765 /* As the transfer for this page is being done, clear the flags */
1766 spin_lock(&oap->oap_lock);
1767 oap->oap_async_flags = 0;
1768 spin_unlock(&oap->oap_lock);
1769 oap->oap_interrupted = 0;
1771 if (oap->oap_cmd & OBD_BRW_WRITE && xid > 0) {
1772 client_obd_list_lock(&cli->cl_loi_list_lock);
1773 osc_process_ar(&cli->cl_ar, xid, rc);
1774 osc_process_ar(&loi->loi_ar, xid, rc);
1775 client_obd_list_unlock(&cli->cl_loi_list_lock);
1778 rc = osc_completion(env, oap, oap->oap_cmd, rc);
1780 CERROR("completion on oap %p obj %p returns %d.\n",
1787 * Try to add extent to one RPC. We need to think about the following things:
1788 * - # of pages must not be over max_pages_per_rpc
1789 * - extent must be compatible with previous ones
1791 static int try_to_add_extent_for_io(struct client_obd *cli,
1792 struct osc_extent *ext, cfs_list_t *rpclist,
1793 int *pc, unsigned int *max_pages)
1795 struct osc_extent *tmp;
1798 EASSERT((ext->oe_state == OES_CACHE || ext->oe_state == OES_LOCK_DONE),
1801 *max_pages = max(ext->oe_mppr, *max_pages);
1802 if (*pc + ext->oe_nr_pages > *max_pages)
1805 cfs_list_for_each_entry(tmp, rpclist, oe_link) {
1806 EASSERT(tmp->oe_owner == cfs_current(), tmp);
1808 if (overlapped(tmp, ext)) {
1809 OSC_EXTENT_DUMP(D_ERROR, tmp, "overlapped %p.\n", ext);
1814 if (tmp->oe_srvlock != ext->oe_srvlock ||
1815 !tmp->oe_grants != !ext->oe_grants)
1818 /* remove break for strict check */
1822 *pc += ext->oe_nr_pages;
1823 cfs_list_move_tail(&ext->oe_link, rpclist);
1824 ext->oe_owner = cfs_current();
1829 * In order to prevent multiple ptlrpcd from breaking contiguous extents,
1830 * get_write_extent() takes all appropriate extents in atomic.
1832 * The following policy is used to collect extents for IO:
1833 * 1. Add as many HP extents as possible;
1834 * 2. Add the first urgent extent in urgent extent list and take it out of
1836 * 3. Add subsequent extents of this urgent extent;
1837 * 4. If urgent list is not empty, goto 2;
1838 * 5. Traverse the extent tree from the 1st extent;
1839 * 6. Above steps exit if there is no space in this RPC.
1841 static int get_write_extents(struct osc_object *obj, cfs_list_t *rpclist)
1843 struct client_obd *cli = osc_cli(obj);
1844 struct osc_extent *ext;
1846 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1848 LASSERT(osc_object_is_locked(obj));
1849 while (!cfs_list_empty(&obj->oo_hp_exts)) {
1850 ext = cfs_list_entry(obj->oo_hp_exts.next, struct osc_extent,
1852 LASSERT(ext->oe_state == OES_CACHE);
1853 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1856 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1858 if (page_count == max_pages)
1861 while (!cfs_list_empty(&obj->oo_urgent_exts)) {
1862 ext = cfs_list_entry(obj->oo_urgent_exts.next,
1863 struct osc_extent, oe_link);
1864 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1868 if (!ext->oe_intree)
1871 while ((ext = next_extent(ext)) != NULL) {
1872 if ((ext->oe_state != OES_CACHE) ||
1873 (!cfs_list_empty(&ext->oe_link) &&
1874 ext->oe_owner != NULL))
1877 if (!try_to_add_extent_for_io(cli, ext, rpclist,
1878 &page_count, &max_pages))
1882 if (page_count == max_pages)
1885 ext = first_extent(obj);
1886 while (ext != NULL) {
1887 if ((ext->oe_state != OES_CACHE) ||
1888 /* this extent may be already in current rpclist */
1889 (!cfs_list_empty(&ext->oe_link) && ext->oe_owner != NULL)) {
1890 ext = next_extent(ext);
1894 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1898 ext = next_extent(ext);
1904 osc_send_write_rpc(const struct lu_env *env, struct client_obd *cli,
1905 struct osc_object *osc, pdl_policy_t pol)
1907 CFS_LIST_HEAD(rpclist);
1908 struct osc_extent *ext;
1909 struct osc_extent *tmp;
1910 struct osc_extent *first = NULL;
1911 obd_count page_count = 0;
1916 LASSERT(osc_object_is_locked(osc));
1918 page_count = get_write_extents(osc, &rpclist);
1919 LASSERT(equi(page_count == 0, cfs_list_empty(&rpclist)));
1921 if (cfs_list_empty(&rpclist))
1924 osc_update_pending(osc, OBD_BRW_WRITE, -page_count);
1926 cfs_list_for_each_entry(ext, &rpclist, oe_link) {
1927 LASSERT(ext->oe_state == OES_CACHE ||
1928 ext->oe_state == OES_LOCK_DONE);
1929 if (ext->oe_state == OES_CACHE)
1930 osc_extent_state_set(ext, OES_LOCKING);
1932 osc_extent_state_set(ext, OES_RPC);
1935 /* we're going to grab page lock, so release object lock because
1936 * lock order is page lock -> object lock. */
1937 osc_object_unlock(osc);
1939 cfs_list_for_each_entry_safe(ext, tmp, &rpclist, oe_link) {
1940 if (ext->oe_state == OES_LOCKING) {
1941 rc = osc_extent_make_ready(env, ext);
1942 if (unlikely(rc < 0)) {
1943 cfs_list_del_init(&ext->oe_link);
1944 osc_extent_finish(env, ext, 0, rc);
1948 if (first == NULL) {
1950 srvlock = ext->oe_srvlock;
1952 LASSERT(srvlock == ext->oe_srvlock);
1956 if (!cfs_list_empty(&rpclist)) {
1957 LASSERT(page_count > 0);
1958 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_WRITE, pol);
1959 LASSERT(cfs_list_empty(&rpclist));
1962 osc_object_lock(osc);
1967 * prepare pages for ASYNC io and put pages in send queue.
1969 * \param cmd OBD_BRW_* macroses
1970 * \param lop pending pages
1972 * \return zero if no page added to send queue.
1973 * \return 1 if pages successfully added to send queue.
1974 * \return negative on errors.
1977 osc_send_read_rpc(const struct lu_env *env, struct client_obd *cli,
1978 struct osc_object *osc, pdl_policy_t pol)
1980 struct osc_extent *ext;
1981 struct osc_extent *next;
1982 CFS_LIST_HEAD(rpclist);
1984 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1988 LASSERT(osc_object_is_locked(osc));
1989 cfs_list_for_each_entry_safe(ext, next,
1990 &osc->oo_reading_exts, oe_link) {
1991 EASSERT(ext->oe_state == OES_LOCK_DONE, ext);
1992 if (!try_to_add_extent_for_io(cli, ext, &rpclist, &page_count,
1995 osc_extent_state_set(ext, OES_RPC);
1996 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1998 LASSERT(page_count <= max_pages);
2000 osc_update_pending(osc, OBD_BRW_READ, -page_count);
2002 if (!cfs_list_empty(&rpclist)) {
2003 osc_object_unlock(osc);
2005 LASSERT(page_count > 0);
2006 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_READ, pol);
2007 LASSERT(cfs_list_empty(&rpclist));
2009 osc_object_lock(osc);
2014 #define list_to_obj(list, item) ({ \
2015 cfs_list_t *__tmp = (list)->next; \
2016 cfs_list_del_init(__tmp); \
2017 cfs_list_entry(__tmp, struct osc_object, oo_##item); \
2020 /* This is called by osc_check_rpcs() to find which objects have pages that
2021 * we could be sending. These lists are maintained by osc_makes_rpc(). */
2022 static struct osc_object *osc_next_obj(struct client_obd *cli)
2026 /* First return objects that have blocked locks so that they
2027 * will be flushed quickly and other clients can get the lock,
2028 * then objects which have pages ready to be stuffed into RPCs */
2029 if (!cfs_list_empty(&cli->cl_loi_hp_ready_list))
2030 RETURN(list_to_obj(&cli->cl_loi_hp_ready_list, hp_ready_item));
2031 if (!cfs_list_empty(&cli->cl_loi_ready_list))
2032 RETURN(list_to_obj(&cli->cl_loi_ready_list, ready_item));
2034 /* then if we have cache waiters, return all objects with queued
2035 * writes. This is especially important when many small files
2036 * have filled up the cache and not been fired into rpcs because
2037 * they don't pass the nr_pending/object threshhold */
2038 if (!cfs_list_empty(&cli->cl_cache_waiters) &&
2039 !cfs_list_empty(&cli->cl_loi_write_list))
2040 RETURN(list_to_obj(&cli->cl_loi_write_list, write_item));
2042 /* then return all queued objects when we have an invalid import
2043 * so that they get flushed */
2044 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
2045 if (!cfs_list_empty(&cli->cl_loi_write_list))
2046 RETURN(list_to_obj(&cli->cl_loi_write_list,
2048 if (!cfs_list_empty(&cli->cl_loi_read_list))
2049 RETURN(list_to_obj(&cli->cl_loi_read_list,
2055 /* called with the loi list lock held */
2056 static void osc_check_rpcs(const struct lu_env *env, struct client_obd *cli,
2059 struct osc_object *osc;
2063 while ((osc = osc_next_obj(cli)) != NULL) {
2064 struct cl_object *obj = osc2cl(osc);
2065 struct lu_ref_link *link;
2067 OSC_IO_DEBUG(osc, "%lu in flight\n", rpcs_in_flight(cli));
2069 if (osc_max_rpc_in_flight(cli, osc)) {
2070 __osc_list_maint(cli, osc);
2075 client_obd_list_unlock(&cli->cl_loi_list_lock);
2076 link = lu_object_ref_add(&obj->co_lu, "check", cfs_current());
2078 /* attempt some read/write balancing by alternating between
2079 * reads and writes in an object. The makes_rpc checks here
2080 * would be redundant if we were getting read/write work items
2081 * instead of objects. we don't want send_oap_rpc to drain a
2082 * partial read pending queue when we're given this object to
2083 * do io on writes while there are cache waiters */
2084 osc_object_lock(osc);
2085 if (osc_makes_rpc(cli, osc, OBD_BRW_WRITE)) {
2086 rc = osc_send_write_rpc(env, cli, osc, pol);
2088 CERROR("Write request failed with %d\n", rc);
2090 /* osc_send_write_rpc failed, mostly because of
2093 * It can't break here, because if:
2094 * - a page was submitted by osc_io_submit, so
2096 * - no request in flight
2097 * - no subsequent request
2098 * The system will be in live-lock state,
2099 * because there is no chance to call
2100 * osc_io_unplug() and osc_check_rpcs() any
2101 * more. pdflush can't help in this case,
2102 * because it might be blocked at grabbing
2103 * the page lock as we mentioned.
2105 * Anyway, continue to drain pages. */
2109 if (osc_makes_rpc(cli, osc, OBD_BRW_READ)) {
2110 rc = osc_send_read_rpc(env, cli, osc, pol);
2112 CERROR("Read request failed with %d\n", rc);
2114 osc_object_unlock(osc);
2116 osc_list_maint(cli, osc);
2117 lu_object_ref_del_at(&obj->co_lu, link, "check", cfs_current());
2118 cl_object_put(env, obj);
2120 client_obd_list_lock(&cli->cl_loi_list_lock);
2124 static int osc_io_unplug0(const struct lu_env *env, struct client_obd *cli,
2125 struct osc_object *osc, pdl_policy_t pol, int async)
2130 client_obd_list_lock(&cli->cl_loi_list_lock);
2132 has_rpcs = __osc_list_maint(cli, osc);
2135 /* disable osc_lru_shrink() temporarily to avoid
2136 * potential stack overrun problem. LU-2859 */
2137 cfs_atomic_inc(&cli->cl_lru_shrinkers);
2138 osc_check_rpcs(env, cli, pol);
2139 cfs_atomic_dec(&cli->cl_lru_shrinkers);
2141 CDEBUG(D_CACHE, "Queue writeback work for client %p.\n",
2143 LASSERT(cli->cl_writeback_work != NULL);
2144 rc = ptlrpcd_queue_work(cli->cl_writeback_work);
2147 client_obd_list_unlock(&cli->cl_loi_list_lock);
2151 static int osc_io_unplug_async(const struct lu_env *env,
2152 struct client_obd *cli, struct osc_object *osc)
2154 /* XXX: policy is no use actually. */
2155 return osc_io_unplug0(env, cli, osc, PDL_POLICY_ROUND, 1);
2158 void osc_io_unplug(const struct lu_env *env, struct client_obd *cli,
2159 struct osc_object *osc, pdl_policy_t pol)
2161 (void)osc_io_unplug0(env, cli, osc, pol, 0);
2164 int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops,
2165 cfs_page_t *page, loff_t offset)
2167 struct obd_export *exp = osc_export(osc);
2168 struct osc_async_page *oap = &ops->ops_oap;
2172 return cfs_size_round(sizeof(*oap));
2174 oap->oap_magic = OAP_MAGIC;
2175 oap->oap_cli = &exp->exp_obd->u.cli;
2178 oap->oap_page = page;
2179 oap->oap_obj_off = offset;
2180 LASSERT(!(offset & ~CFS_PAGE_MASK));
2182 if (!client_is_remote(exp) && cfs_capable(CFS_CAP_SYS_RESOURCE))
2183 oap->oap_brw_flags = OBD_BRW_NOQUOTA;
2185 CFS_INIT_LIST_HEAD(&oap->oap_pending_item);
2186 CFS_INIT_LIST_HEAD(&oap->oap_rpc_item);
2188 spin_lock_init(&oap->oap_lock);
2189 CDEBUG(D_INFO, "oap %p page %p obj off "LPU64"\n",
2190 oap, page, oap->oap_obj_off);
2194 int osc_queue_async_io(const struct lu_env *env, struct cl_io *io,
2195 struct osc_page *ops)
2197 struct osc_io *oio = osc_env_io(env);
2198 struct osc_extent *ext = NULL;
2199 struct osc_async_page *oap = &ops->ops_oap;
2200 struct client_obd *cli = oap->oap_cli;
2201 struct osc_object *osc = oap->oap_obj;
2204 int brw_flags = OBD_BRW_ASYNC;
2205 int cmd = OBD_BRW_WRITE;
2206 int need_release = 0;
2210 if (oap->oap_magic != OAP_MAGIC)
2213 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2216 if (!cfs_list_empty(&oap->oap_pending_item) ||
2217 !cfs_list_empty(&oap->oap_rpc_item))
2220 /* Set the OBD_BRW_SRVLOCK before the page is queued. */
2221 brw_flags |= ops->ops_srvlock ? OBD_BRW_SRVLOCK : 0;
2222 if (!client_is_remote(osc_export(osc)) &&
2223 cfs_capable(CFS_CAP_SYS_RESOURCE)) {
2224 brw_flags |= OBD_BRW_NOQUOTA;
2225 cmd |= OBD_BRW_NOQUOTA;
2228 /* check if the file's owner/group is over quota */
2229 if (!(cmd & OBD_BRW_NOQUOTA)) {
2230 struct cl_object *obj;
2231 struct cl_attr *attr;
2232 unsigned int qid[MAXQUOTAS];
2234 obj = cl_object_top(&osc->oo_cl);
2235 attr = &osc_env_info(env)->oti_attr;
2237 cl_object_attr_lock(obj);
2238 rc = cl_object_attr_get(env, obj, attr);
2239 cl_object_attr_unlock(obj);
2241 qid[USRQUOTA] = attr->cat_uid;
2242 qid[GRPQUOTA] = attr->cat_gid;
2243 if (rc == 0 && osc_quota_chkdq(cli, qid) == NO_QUOTA)
2250 oap->oap_page_off = ops->ops_from;
2251 oap->oap_count = ops->ops_to - ops->ops_from;
2252 oap->oap_async_flags = 0;
2253 oap->oap_brw_flags = brw_flags;
2255 OSC_IO_DEBUG(osc, "oap %p page %p added for cmd %d\n",
2256 oap, oap->oap_page, oap->oap_cmd & OBD_BRW_RWMASK);
2258 index = oap2cl_page(oap)->cp_index;
2260 /* Add this page into extent by the following steps:
2261 * 1. if there exists an active extent for this IO, mostly this page
2262 * can be added to the active extent and sometimes we need to
2263 * expand extent to accomodate this page;
2264 * 2. otherwise, a new extent will be allocated. */
2266 ext = oio->oi_active;
2267 if (ext != NULL && ext->oe_start <= index && ext->oe_max_end >= index) {
2268 /* one chunk plus extent overhead must be enough to write this
2270 grants = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2271 if (ext->oe_end >= index)
2274 /* it doesn't need any grant to dirty this page */
2275 client_obd_list_lock(&cli->cl_loi_list_lock);
2276 rc = osc_enter_cache_try(cli, oap, grants, 0);
2277 client_obd_list_unlock(&cli->cl_loi_list_lock);
2278 if (rc == 0) { /* try failed */
2281 } else if (ext->oe_end < index) {
2283 /* try to expand this extent */
2284 rc = osc_extent_expand(ext, index, &tmp);
2287 /* don't free reserved grant */
2289 OSC_EXTENT_DUMP(D_CACHE, ext,
2290 "expanded for %lu.\n", index);
2291 osc_unreserve_grant(cli, grants, tmp);
2296 } else if (ext != NULL) {
2297 /* index is located outside of active extent */
2301 osc_extent_release(env, ext);
2302 oio->oi_active = NULL;
2307 int tmp = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2309 /* try to find new extent to cover this page */
2310 LASSERT(oio->oi_active == NULL);
2311 /* we may have allocated grant for this page if we failed
2312 * to expand the previous active extent. */
2313 LASSERT(ergo(grants > 0, grants >= tmp));
2317 /* we haven't allocated grant for this page. */
2318 rc = osc_enter_cache(env, cli, oap, tmp);
2325 ext = osc_extent_find(env, osc, index, &tmp);
2327 LASSERT(tmp == grants);
2328 osc_exit_cache(cli, oap);
2332 oio->oi_active = ext;
2336 osc_unreserve_grant(cli, grants, tmp);
2339 LASSERT(ergo(rc == 0, ext != NULL));
2341 EASSERTF(ext->oe_end >= index && ext->oe_start <= index,
2342 ext, "index = %lu.\n", index);
2343 LASSERT((oap->oap_brw_flags & OBD_BRW_FROM_GRANT) != 0);
2345 osc_object_lock(osc);
2346 if (ext->oe_nr_pages == 0)
2347 ext->oe_srvlock = ops->ops_srvlock;
2349 LASSERT(ext->oe_srvlock == ops->ops_srvlock);
2351 cfs_list_add_tail(&oap->oap_pending_item, &ext->oe_pages);
2352 osc_object_unlock(osc);
2357 int osc_teardown_async_page(const struct lu_env *env,
2358 struct osc_object *obj, struct osc_page *ops)
2360 struct osc_async_page *oap = &ops->ops_oap;
2361 struct osc_extent *ext = NULL;
2365 LASSERT(oap->oap_magic == OAP_MAGIC);
2367 CDEBUG(D_INFO, "teardown oap %p page %p at index %lu.\n",
2368 oap, ops, oap2cl_page(oap)->cp_index);
2370 osc_object_lock(obj);
2371 if (!cfs_list_empty(&oap->oap_rpc_item)) {
2372 CDEBUG(D_CACHE, "oap %p is not in cache.\n", oap);
2374 } else if (!cfs_list_empty(&oap->oap_pending_item)) {
2375 ext = osc_extent_lookup(obj, oap2cl_page(oap)->cp_index);
2376 /* only truncated pages are allowed to be taken out.
2377 * See osc_extent_truncate() and osc_cache_truncate_start()
2379 if (ext != NULL && ext->oe_state != OES_TRUNC) {
2380 OSC_EXTENT_DUMP(D_ERROR, ext, "trunc at %lu.\n",
2381 oap2cl_page(oap)->cp_index);
2385 osc_object_unlock(obj);
2387 osc_extent_put(env, ext);
2392 * This is called when a page is picked up by kernel to write out.
2394 * We should find out the corresponding extent and add the whole extent
2395 * into urgent list. The extent may be being truncated or used, handle it
2398 int osc_flush_async_page(const struct lu_env *env, struct cl_io *io,
2399 struct osc_page *ops)
2401 struct osc_extent *ext = NULL;
2402 struct osc_object *obj = cl2osc(ops->ops_cl.cpl_obj);
2403 struct cl_page *cp = ops->ops_cl.cpl_page;
2404 pgoff_t index = cp->cp_index;
2405 struct osc_async_page *oap = &ops->ops_oap;
2406 bool unplug = false;
2410 osc_object_lock(obj);
2411 ext = osc_extent_lookup(obj, index);
2413 osc_extent_tree_dump(D_ERROR, obj);
2414 LASSERTF(0, "page index %lu is NOT covered.\n", index);
2417 switch (ext->oe_state) {
2420 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(cp),
2421 "flush an in-rpc page?\n");
2425 /* If we know this extent is being written out, we should abort
2426 * so that the writer can make this page ready. Otherwise, there
2427 * exists a deadlock problem because other process can wait for
2428 * page writeback bit holding page lock; and meanwhile in
2429 * vvp_page_make_ready(), we need to grab page lock before
2430 * really sending the RPC. */
2432 /* race with truncate, page will be redirtied */
2433 GOTO(out, rc = -EAGAIN);
2438 rc = cl_page_prep(env, io, cl_page_top(cp), CRT_WRITE);
2442 spin_lock(&oap->oap_lock);
2443 oap->oap_async_flags |= ASYNC_READY|ASYNC_URGENT;
2444 spin_unlock(&oap->oap_lock);
2446 if (cfs_memory_pressure_get())
2447 ext->oe_memalloc = 1;
2450 if (ext->oe_state == OES_CACHE) {
2451 OSC_EXTENT_DUMP(D_CACHE, ext,
2452 "flush page %p make it urgent.\n", oap);
2453 if (cfs_list_empty(&ext->oe_link))
2454 cfs_list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2461 osc_object_unlock(obj);
2462 osc_extent_put(env, ext);
2464 osc_io_unplug_async(env, osc_cli(obj), obj);
2469 * this is called when a sync waiter receives an interruption. Its job is to
2470 * get the caller woken as soon as possible. If its page hasn't been put in an
2471 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
2472 * desiring interruption which will forcefully complete the rpc once the rpc
2475 int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops)
2477 struct osc_async_page *oap = &ops->ops_oap;
2478 struct osc_object *obj = oap->oap_obj;
2479 struct client_obd *cli = osc_cli(obj);
2480 struct osc_extent *ext;
2481 struct osc_extent *found = NULL;
2483 pgoff_t index = oap2cl_page(oap)->cp_index;
2488 LASSERT(!oap->oap_interrupted);
2489 oap->oap_interrupted = 1;
2491 /* Find out the caching extent */
2492 osc_object_lock(obj);
2493 if (oap->oap_cmd & OBD_BRW_WRITE) {
2494 plist = &obj->oo_urgent_exts;
2495 cmd = OBD_BRW_WRITE;
2497 plist = &obj->oo_reading_exts;
2500 cfs_list_for_each_entry(ext, plist, oe_link) {
2501 if (ext->oe_start <= index && ext->oe_end >= index) {
2502 LASSERT(ext->oe_state == OES_LOCK_DONE);
2503 /* For OES_LOCK_DONE state extent, it has already held
2504 * a refcount for RPC. */
2505 found = osc_extent_get(ext);
2509 if (found != NULL) {
2510 cfs_list_del_init(&found->oe_link);
2511 osc_update_pending(obj, cmd, -found->oe_nr_pages);
2512 osc_object_unlock(obj);
2514 osc_extent_finish(env, found, 0, -EINTR);
2515 osc_extent_put(env, found);
2518 osc_object_unlock(obj);
2519 /* ok, it's been put in an rpc. only one oap gets a request
2521 if (oap->oap_request != NULL) {
2522 ptlrpc_mark_interrupted(oap->oap_request);
2523 ptlrpcd_wake(oap->oap_request);
2524 ptlrpc_req_finished(oap->oap_request);
2525 oap->oap_request = NULL;
2529 osc_list_maint(cli, obj);
2533 int osc_queue_sync_pages(const struct lu_env *env, struct osc_object *obj,
2534 cfs_list_t *list, int cmd, int brw_flags)
2536 struct client_obd *cli = osc_cli(obj);
2537 struct osc_extent *ext;
2538 struct osc_async_page *oap;
2540 int mppr = cli->cl_max_pages_per_rpc;
2541 pgoff_t start = CL_PAGE_EOF;
2545 cfs_list_for_each_entry(oap, list, oap_pending_item) {
2546 struct cl_page *cp = oap2cl_page(oap);
2547 if (cp->cp_index > end)
2549 if (cp->cp_index < start)
2550 start = cp->cp_index;
2552 mppr <<= (page_count > mppr);
2555 ext = osc_extent_alloc(obj);
2557 cfs_list_for_each_entry(oap, list, oap_pending_item) {
2558 cfs_list_del_init(&oap->oap_pending_item);
2559 osc_ap_completion(env, cli, oap, 0, -ENOMEM);
2564 ext->oe_rw = !!(cmd & OBD_BRW_READ);
2566 ext->oe_start = start;
2567 ext->oe_end = ext->oe_max_end = end;
2569 ext->oe_srvlock = !!(brw_flags & OBD_BRW_SRVLOCK);
2570 ext->oe_nr_pages = page_count;
2571 ext->oe_mppr = mppr;
2572 cfs_list_splice_init(list, &ext->oe_pages);
2574 osc_object_lock(obj);
2575 /* Reuse the initial refcount for RPC, don't drop it */
2576 osc_extent_state_set(ext, OES_LOCK_DONE);
2577 if (cmd & OBD_BRW_WRITE) {
2578 cfs_list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2579 osc_update_pending(obj, OBD_BRW_WRITE, page_count);
2581 cfs_list_add_tail(&ext->oe_link, &obj->oo_reading_exts);
2582 osc_update_pending(obj, OBD_BRW_READ, page_count);
2584 osc_object_unlock(obj);
2586 osc_io_unplug_async(env, cli, obj);
2591 * Called by osc_io_setattr_start() to freeze and destroy covering extents.
2593 int osc_cache_truncate_start(const struct lu_env *env, struct osc_io *oio,
2594 struct osc_object *obj, __u64 size)
2596 struct client_obd *cli = osc_cli(obj);
2597 struct osc_extent *ext;
2598 struct osc_extent *waiting = NULL;
2600 CFS_LIST_HEAD(list);
2605 /* pages with index greater or equal to index will be truncated. */
2606 index = cl_index(osc2cl(obj), size);
2607 partial = size > cl_offset(osc2cl(obj), index);
2610 osc_object_lock(obj);
2611 ext = osc_extent_search(obj, index);
2613 ext = first_extent(obj);
2614 else if (ext->oe_end < index)
2615 ext = next_extent(ext);
2616 while (ext != NULL) {
2617 EASSERT(ext->oe_state != OES_TRUNC, ext);
2619 if (ext->oe_state > OES_CACHE || ext->oe_urgent) {
2620 /* if ext is in urgent state, it means there must exist
2621 * a page already having been flushed by write_page().
2622 * We have to wait for this extent because we can't
2623 * truncate that page. */
2624 LASSERT(!ext->oe_hp);
2625 OSC_EXTENT_DUMP(D_CACHE, ext,
2626 "waiting for busy extent\n");
2627 waiting = osc_extent_get(ext);
2631 OSC_EXTENT_DUMP(D_CACHE, ext, "try to trunc:"LPU64".\n", size);
2633 osc_extent_get(ext);
2634 if (ext->oe_state == OES_ACTIVE) {
2635 /* though we grab inode mutex for write path, but we
2636 * release it before releasing extent(in osc_io_end()),
2637 * so there is a race window that an extent is still
2638 * in OES_ACTIVE when truncate starts. */
2639 LASSERT(!ext->oe_trunc_pending);
2640 ext->oe_trunc_pending = 1;
2642 EASSERT(ext->oe_state == OES_CACHE, ext);
2643 osc_extent_state_set(ext, OES_TRUNC);
2644 osc_update_pending(obj, OBD_BRW_WRITE,
2647 EASSERT(cfs_list_empty(&ext->oe_link), ext);
2648 cfs_list_add_tail(&ext->oe_link, &list);
2650 ext = next_extent(ext);
2652 osc_object_unlock(obj);
2654 osc_list_maint(cli, obj);
2656 while (!cfs_list_empty(&list)) {
2659 ext = cfs_list_entry(list.next, struct osc_extent, oe_link);
2660 cfs_list_del_init(&ext->oe_link);
2662 /* extent may be in OES_ACTIVE state because inode mutex
2663 * is released before osc_io_end() in file write case */
2664 if (ext->oe_state != OES_TRUNC)
2665 osc_extent_wait(env, ext, OES_TRUNC);
2667 rc = osc_extent_truncate(ext, index, partial);
2672 OSC_EXTENT_DUMP(D_ERROR, ext,
2673 "truncate error %d\n", rc);
2674 } else if (ext->oe_nr_pages == 0) {
2675 osc_extent_remove(ext);
2677 /* this must be an overlapped extent which means only
2678 * part of pages in this extent have been truncated.
2680 EASSERTF(ext->oe_start <= index, ext,
2681 "trunc index = %lu/%d.\n", index, partial);
2682 /* fix index to skip this partially truncated extent */
2683 index = ext->oe_end + 1;
2686 /* we need to hold this extent in OES_TRUNC state so
2687 * that no writeback will happen. This is to avoid
2689 LASSERT(oio->oi_trunc == NULL);
2690 oio->oi_trunc = osc_extent_get(ext);
2691 OSC_EXTENT_DUMP(D_CACHE, ext,
2692 "trunc at "LPU64"\n", size);
2694 osc_extent_put(env, ext);
2696 if (waiting != NULL) {
2699 /* ignore the result of osc_extent_wait the write initiator
2700 * should take care of it. */
2701 rc = osc_extent_wait(env, waiting, OES_INV);
2703 OSC_EXTENT_DUMP(D_CACHE, ext, "wait error: %d.\n", rc);
2705 osc_extent_put(env, waiting);
2713 * Called after osc_io_setattr_end to add oio->oi_trunc back to cache.
2715 void osc_cache_truncate_end(const struct lu_env *env, struct osc_io *oio,
2716 struct osc_object *obj)
2718 struct osc_extent *ext = oio->oi_trunc;
2720 oio->oi_trunc = NULL;
2722 bool unplug = false;
2724 EASSERT(ext->oe_nr_pages > 0, ext);
2725 EASSERT(ext->oe_state == OES_TRUNC, ext);
2726 EASSERT(!ext->oe_urgent, ext);
2728 OSC_EXTENT_DUMP(D_CACHE, ext, "trunc -> cache.\n");
2729 osc_object_lock(obj);
2730 osc_extent_state_set(ext, OES_CACHE);
2731 if (ext->oe_fsync_wait && !ext->oe_urgent) {
2733 cfs_list_move_tail(&ext->oe_link, &obj->oo_urgent_exts);
2736 osc_update_pending(obj, OBD_BRW_WRITE, ext->oe_nr_pages);
2737 osc_object_unlock(obj);
2738 osc_extent_put(env, ext);
2741 osc_io_unplug_async(env, osc_cli(obj), obj);
2746 * Wait for extents in a specific range to be written out.
2747 * The caller must have called osc_cache_writeback_range() to issue IO
2748 * otherwise it will take a long time for this function to finish.
2750 * Caller must hold inode_mutex , or cancel exclusive dlm lock so that
2751 * nobody else can dirty this range of file while we're waiting for
2752 * extents to be written.
2754 int osc_cache_wait_range(const struct lu_env *env, struct osc_object *obj,
2755 pgoff_t start, pgoff_t end)
2757 struct osc_extent *ext;
2758 pgoff_t index = start;
2763 osc_object_lock(obj);
2764 ext = osc_extent_search(obj, index);
2766 ext = first_extent(obj);
2767 else if (ext->oe_end < index)
2768 ext = next_extent(ext);
2769 while (ext != NULL) {
2772 if (ext->oe_start > end)
2775 if (!ext->oe_fsync_wait) {
2776 ext = next_extent(ext);
2780 EASSERT(ergo(ext->oe_state == OES_CACHE,
2781 ext->oe_hp || ext->oe_urgent), ext);
2782 EASSERT(ergo(ext->oe_state == OES_ACTIVE,
2783 !ext->oe_hp && ext->oe_urgent), ext);
2785 index = ext->oe_end + 1;
2786 osc_extent_get(ext);
2787 osc_object_unlock(obj);
2789 rc = osc_extent_wait(env, ext, OES_INV);
2792 osc_extent_put(env, ext);
2795 osc_object_unlock(obj);
2797 OSC_IO_DEBUG(obj, "sync file range.\n");
2802 * Called to write out a range of osc object.
2804 * @hp : should be set this is caused by lock cancel;
2805 * @discard: is set if dirty pages should be dropped - file will be deleted or
2806 * truncated, this implies there is no partially discarding extents.
2808 * Return how many pages will be issued, or error code if error occurred.
2810 int osc_cache_writeback_range(const struct lu_env *env, struct osc_object *obj,
2811 pgoff_t start, pgoff_t end, int hp, int discard)
2813 struct osc_extent *ext;
2814 CFS_LIST_HEAD(discard_list);
2815 bool unplug = false;
2819 osc_object_lock(obj);
2820 ext = osc_extent_search(obj, start);
2822 ext = first_extent(obj);
2823 else if (ext->oe_end < start)
2824 ext = next_extent(ext);
2825 while (ext != NULL) {
2826 if (ext->oe_start > end)
2829 ext->oe_fsync_wait = 1;
2830 switch (ext->oe_state) {
2832 result += ext->oe_nr_pages;
2834 cfs_list_t *list = NULL;
2836 EASSERT(!ext->oe_hp, ext);
2838 list = &obj->oo_hp_exts;
2839 } else if (!ext->oe_urgent) {
2841 list = &obj->oo_urgent_exts;
2844 cfs_list_move_tail(&ext->oe_link, list);
2847 /* the only discarder is lock cancelling, so
2848 * [start, end] must contain this extent */
2849 EASSERT(ext->oe_start >= start &&
2850 ext->oe_max_end <= end, ext);
2851 osc_extent_state_set(ext, OES_LOCKING);
2852 ext->oe_owner = cfs_current();
2853 cfs_list_move_tail(&ext->oe_link,
2855 osc_update_pending(obj, OBD_BRW_WRITE,
2860 /* It's pretty bad to wait for ACTIVE extents, because
2861 * we don't know how long we will wait for it to be
2862 * flushed since it may be blocked at awaiting more
2863 * grants. We do this for the correctness of fsync. */
2864 LASSERT(hp == 0 && discard == 0);
2868 /* this extent is being truncated, can't do anything
2869 * for it now. it will be set to urgent after truncate
2870 * is finished in osc_cache_truncate_end(). */
2874 ext = next_extent(ext);
2876 osc_object_unlock(obj);
2878 LASSERT(ergo(!discard, cfs_list_empty(&discard_list)));
2879 if (!cfs_list_empty(&discard_list)) {
2880 struct osc_extent *tmp;
2883 osc_list_maint(osc_cli(obj), obj);
2884 cfs_list_for_each_entry_safe(ext, tmp, &discard_list, oe_link) {
2885 cfs_list_del_init(&ext->oe_link);
2886 EASSERT(ext->oe_state == OES_LOCKING, ext);
2888 /* Discard caching pages. We don't actually write this
2889 * extent out but we complete it as if we did. */
2890 rc = osc_extent_make_ready(env, ext);
2891 if (unlikely(rc < 0)) {
2892 OSC_EXTENT_DUMP(D_ERROR, ext,
2893 "make_ready returned %d\n", rc);
2898 /* finish the extent as if the pages were sent */
2899 osc_extent_finish(env, ext, 0, 0);
2904 osc_io_unplug(env, osc_cli(obj), obj, PDL_POLICY_ROUND);
2906 if (hp || discard) {
2908 rc = osc_cache_wait_range(env, obj, start, end);
2909 if (result >= 0 && rc < 0)
2913 OSC_IO_DEBUG(obj, "cache page out.\n");