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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
33 * osc cache management.
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_OSC
40 #include "osc_cl_internal.h"
41 #include "osc_internal.h"
43 static int extent_debug; /* set it to be true for more debug */
45 static void osc_update_pending(struct osc_object *obj, int cmd, int delta);
46 static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
47 enum osc_extent_state state);
48 static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
49 struct osc_async_page *oap, int sent, int rc);
50 static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
52 static int osc_refresh_count(const struct lu_env *env,
53 struct osc_async_page *oap, int cmd);
54 static int osc_io_unplug_async(const struct lu_env *env,
55 struct client_obd *cli, struct osc_object *osc);
56 static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
57 unsigned int lost_grant);
59 static void osc_extent_tree_dump0(int level, struct osc_object *obj,
60 const char *func, int line);
61 #define osc_extent_tree_dump(lvl, obj) \
62 osc_extent_tree_dump0(lvl, obj, __func__, __LINE__)
68 /* ------------------ osc extent ------------------ */
69 static inline char *ext_flags(struct osc_extent *ext, char *flags)
72 *buf++ = ext->oe_rw ? 'r' : 'w';
85 if (ext->oe_trunc_pending)
87 if (ext->oe_fsync_wait)
93 static inline char list_empty_marker(struct list_head *list)
95 return list_empty(list) ? '-' : '+';
98 #define EXTSTR "[%lu -> %lu/%lu]"
99 #define EXTPARA(ext) (ext)->oe_start, (ext)->oe_end, (ext)->oe_max_end
100 static const char *oes_strings[] = {
101 "inv", "active", "cache", "locking", "lockdone", "rpc", "trunc", NULL };
103 #define OSC_EXTENT_DUMP(lvl, extent, fmt, ...) do { \
104 struct osc_extent *__ext = (extent); \
108 "extent %p@{" EXTSTR ", " \
109 "[%d|%d|%c|%s|%s|%p], [%d|%d|%c|%c|%p|%u|%p]} " fmt, \
110 /* ----- extent part 0 ----- */ \
111 __ext, EXTPARA(__ext), \
112 /* ----- part 1 ----- */ \
113 atomic_read(&__ext->oe_refc), \
114 atomic_read(&__ext->oe_users), \
115 list_empty_marker(&__ext->oe_link), \
116 oes_strings[__ext->oe_state], ext_flags(__ext, __buf), \
118 /* ----- part 2 ----- */ \
119 __ext->oe_grants, __ext->oe_nr_pages, \
120 list_empty_marker(&__ext->oe_pages), \
121 waitqueue_active(&__ext->oe_waitq) ? '+' : '-', \
122 __ext->oe_dlmlock, __ext->oe_mppr, __ext->oe_owner, \
123 /* ----- part 4 ----- */ \
125 if (lvl == D_ERROR && __ext->oe_dlmlock) \
126 LDLM_ERROR(__ext->oe_dlmlock, "extent: %p", __ext); \
128 LDLM_DEBUG(__ext->oe_dlmlock, "extent: %p", __ext); \
132 #define EASSERTF(expr, ext, fmt, args...) do { \
134 OSC_EXTENT_DUMP(D_ERROR, (ext), fmt, ##args); \
135 osc_extent_tree_dump(D_ERROR, (ext)->oe_obj); \
141 #define EASSERT(expr, ext) EASSERTF(expr, ext, "\n")
143 static inline struct osc_extent *rb_extent(struct rb_node *n)
148 return container_of(n, struct osc_extent, oe_node);
151 static inline struct osc_extent *next_extent(struct osc_extent *ext)
156 LASSERT(ext->oe_intree);
157 return rb_extent(rb_next(&ext->oe_node));
160 static inline struct osc_extent *prev_extent(struct osc_extent *ext)
165 LASSERT(ext->oe_intree);
166 return rb_extent(rb_prev(&ext->oe_node));
169 static inline struct osc_extent *first_extent(struct osc_object *obj)
171 return rb_extent(rb_first(&obj->oo_root));
174 /* object must be locked by caller. */
175 static int osc_extent_sanity_check0(struct osc_extent *ext,
176 const char *func, const int line)
178 struct osc_object *obj = ext->oe_obj;
179 struct osc_async_page *oap;
183 if (!osc_object_is_locked(obj)) {
188 if (ext->oe_state >= OES_STATE_MAX) {
193 if (atomic_read(&ext->oe_refc) <= 0) {
198 if (atomic_read(&ext->oe_refc) < atomic_read(&ext->oe_users)) {
203 switch (ext->oe_state) {
205 if (ext->oe_nr_pages > 0 || !list_empty(&ext->oe_pages))
211 if (atomic_read(&ext->oe_users) == 0) {
219 if (ext->oe_fsync_wait && !ext->oe_urgent) {
225 if (ext->oe_grants == 0) {
229 if (ext->oe_fsync_wait && !ext->oe_urgent && !ext->oe_hp) {
234 if (atomic_read(&ext->oe_users) > 0) {
240 if (ext->oe_max_end < ext->oe_end || ext->oe_end < ext->oe_start) {
245 if (ext->oe_sync && ext->oe_grants > 0) {
250 if (ext->oe_dlmlock) {
251 struct ldlm_extent *extent;
253 extent = &ext->oe_dlmlock->l_policy_data.l_extent;
254 if (!(extent->start <= cl_offset(osc2cl(obj), ext->oe_start) &&
255 extent->end >= cl_offset(osc2cl(obj), ext->oe_max_end))) {
260 if (!(ext->oe_dlmlock->l_granted_mode & (LCK_PW | LCK_GROUP))) {
266 if (ext->oe_nr_pages > ext->oe_mppr) {
271 /* Do not verify page list if extent is in RPC. This is because an
272 * in-RPC extent is supposed to be exclusively accessible w/o lock.
274 if (ext->oe_state > OES_CACHE) {
285 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
286 pgoff_t index = osc_index(oap2osc(oap));
288 if (index > ext->oe_end || index < ext->oe_start) {
293 if (page_count != ext->oe_nr_pages) {
300 OSC_EXTENT_DUMP(D_ERROR, ext,
301 "%s:%d sanity check %p failed with rc = %d\n",
302 func, line, ext, rc);
306 #define sanity_check_nolock(ext) \
307 osc_extent_sanity_check0(ext, __func__, __LINE__)
309 #define sanity_check(ext) ({ \
311 osc_object_lock((ext)->oe_obj); \
312 __res = sanity_check_nolock(ext); \
313 osc_object_unlock((ext)->oe_obj); \
318 * sanity check - to make sure there is no overlapped extent in the tree.
320 static int osc_extent_is_overlapped(struct osc_object *obj,
321 struct osc_extent *ext)
323 struct osc_extent *tmp;
325 LASSERT(osc_object_is_locked(obj));
330 for (tmp = first_extent(obj); tmp; tmp = next_extent(tmp)) {
333 if (tmp->oe_end >= ext->oe_start &&
334 tmp->oe_start <= ext->oe_end)
340 static void osc_extent_state_set(struct osc_extent *ext, int state)
342 LASSERT(osc_object_is_locked(ext->oe_obj));
343 LASSERT(state >= OES_INV && state < OES_STATE_MAX);
345 /* Never try to sanity check a state changing extent :-) */
346 /* LASSERT(sanity_check_nolock(ext) == 0); */
348 /* TODO: validate the state machine */
349 ext->oe_state = state;
350 wake_up_all(&ext->oe_waitq);
353 static struct osc_extent *osc_extent_alloc(struct osc_object *obj)
355 struct osc_extent *ext;
357 ext = kmem_cache_zalloc(osc_extent_kmem, GFP_NOFS);
361 RB_CLEAR_NODE(&ext->oe_node);
363 atomic_set(&ext->oe_refc, 1);
364 atomic_set(&ext->oe_users, 0);
365 INIT_LIST_HEAD(&ext->oe_link);
366 ext->oe_state = OES_INV;
367 INIT_LIST_HEAD(&ext->oe_pages);
368 init_waitqueue_head(&ext->oe_waitq);
369 ext->oe_dlmlock = NULL;
374 static void osc_extent_free(struct osc_extent *ext)
376 kmem_cache_free(osc_extent_kmem, ext);
379 static struct osc_extent *osc_extent_get(struct osc_extent *ext)
381 LASSERT(atomic_read(&ext->oe_refc) >= 0);
382 atomic_inc(&ext->oe_refc);
386 static void osc_extent_put(const struct lu_env *env, struct osc_extent *ext)
388 LASSERT(atomic_read(&ext->oe_refc) > 0);
389 if (atomic_dec_and_test(&ext->oe_refc)) {
390 LASSERT(list_empty(&ext->oe_link));
391 LASSERT(atomic_read(&ext->oe_users) == 0);
392 LASSERT(ext->oe_state == OES_INV);
393 LASSERT(!ext->oe_intree);
395 if (ext->oe_dlmlock) {
396 lu_ref_add(&ext->oe_dlmlock->l_reference,
398 LDLM_LOCK_PUT(ext->oe_dlmlock);
399 ext->oe_dlmlock = NULL;
401 osc_extent_free(ext);
406 * osc_extent_put_trust() is a special version of osc_extent_put() when
407 * it's known that the caller is not the last user. This is to address the
408 * problem of lacking of lu_env ;-).
410 static void osc_extent_put_trust(struct osc_extent *ext)
412 LASSERT(atomic_read(&ext->oe_refc) > 1);
413 LASSERT(osc_object_is_locked(ext->oe_obj));
414 atomic_dec(&ext->oe_refc);
418 * Return the extent which includes pgoff @index, or return the greatest
419 * previous extent in the tree.
421 static struct osc_extent *osc_extent_search(struct osc_object *obj,
424 struct rb_node *n = obj->oo_root.rb_node;
425 struct osc_extent *tmp, *p = NULL;
427 LASSERT(osc_object_is_locked(obj));
430 if (index < tmp->oe_start) {
432 } else if (index > tmp->oe_end) {
443 * Return the extent covering @index, otherwise return NULL.
444 * caller must have held object lock.
446 static struct osc_extent *osc_extent_lookup(struct osc_object *obj,
449 struct osc_extent *ext;
451 ext = osc_extent_search(obj, index);
452 if (ext && ext->oe_start <= index && index <= ext->oe_end)
453 return osc_extent_get(ext);
457 /* caller must have held object lock. */
458 static void osc_extent_insert(struct osc_object *obj, struct osc_extent *ext)
460 struct rb_node **n = &obj->oo_root.rb_node;
461 struct rb_node *parent = NULL;
462 struct osc_extent *tmp;
464 LASSERT(ext->oe_intree == 0);
465 LASSERT(ext->oe_obj == obj);
466 LASSERT(osc_object_is_locked(obj));
471 if (ext->oe_end < tmp->oe_start)
473 else if (ext->oe_start > tmp->oe_end)
476 EASSERTF(0, tmp, EXTSTR"\n", EXTPARA(ext));
478 rb_link_node(&ext->oe_node, parent, n);
479 rb_insert_color(&ext->oe_node, &obj->oo_root);
484 /* caller must have held object lock. */
485 static void osc_extent_erase(struct osc_extent *ext)
487 struct osc_object *obj = ext->oe_obj;
489 LASSERT(osc_object_is_locked(obj));
490 if (ext->oe_intree) {
491 rb_erase(&ext->oe_node, &obj->oo_root);
493 /* rbtree held a refcount */
494 osc_extent_put_trust(ext);
498 static struct osc_extent *osc_extent_hold(struct osc_extent *ext)
500 struct osc_object *obj = ext->oe_obj;
502 LASSERT(osc_object_is_locked(obj));
503 LASSERT(ext->oe_state == OES_ACTIVE || ext->oe_state == OES_CACHE);
504 if (ext->oe_state == OES_CACHE) {
505 osc_extent_state_set(ext, OES_ACTIVE);
506 osc_update_pending(obj, OBD_BRW_WRITE, -ext->oe_nr_pages);
508 atomic_inc(&ext->oe_users);
509 list_del_init(&ext->oe_link);
510 return osc_extent_get(ext);
513 static void __osc_extent_remove(struct osc_extent *ext)
515 LASSERT(osc_object_is_locked(ext->oe_obj));
516 LASSERT(list_empty(&ext->oe_pages));
517 osc_extent_erase(ext);
518 list_del_init(&ext->oe_link);
519 osc_extent_state_set(ext, OES_INV);
520 OSC_EXTENT_DUMP(D_CACHE, ext, "destroyed.\n");
523 static void osc_extent_remove(struct osc_extent *ext)
525 struct osc_object *obj = ext->oe_obj;
527 osc_object_lock(obj);
528 __osc_extent_remove(ext);
529 osc_object_unlock(obj);
533 * This function is used to merge extents to get better performance. It checks
534 * if @cur and @victim are contiguous at chunk level.
536 static int osc_extent_merge(const struct lu_env *env, struct osc_extent *cur,
537 struct osc_extent *victim)
539 struct osc_object *obj = cur->oe_obj;
544 LASSERT(cur->oe_state == OES_CACHE);
545 LASSERT(osc_object_is_locked(obj));
549 if (victim->oe_state != OES_CACHE || victim->oe_fsync_wait)
552 if (cur->oe_max_end != victim->oe_max_end)
555 LASSERT(cur->oe_dlmlock == victim->oe_dlmlock);
556 ppc_bits = osc_cli(obj)->cl_chunkbits - PAGE_SHIFT;
557 chunk_start = cur->oe_start >> ppc_bits;
558 chunk_end = cur->oe_end >> ppc_bits;
559 if (chunk_start != (victim->oe_end >> ppc_bits) + 1 &&
560 chunk_end + 1 != victim->oe_start >> ppc_bits)
563 OSC_EXTENT_DUMP(D_CACHE, victim, "will be merged by %p.\n", cur);
565 cur->oe_start = min(cur->oe_start, victim->oe_start);
566 cur->oe_end = max(cur->oe_end, victim->oe_end);
567 cur->oe_grants += victim->oe_grants;
568 cur->oe_nr_pages += victim->oe_nr_pages;
569 /* only the following bits are needed to merge */
570 cur->oe_urgent |= victim->oe_urgent;
571 cur->oe_memalloc |= victim->oe_memalloc;
572 list_splice_init(&victim->oe_pages, &cur->oe_pages);
573 list_del_init(&victim->oe_link);
574 victim->oe_nr_pages = 0;
576 osc_extent_get(victim);
577 __osc_extent_remove(victim);
578 osc_extent_put(env, victim);
580 OSC_EXTENT_DUMP(D_CACHE, cur, "after merging %p.\n", victim);
585 * Drop user count of osc_extent, and unplug IO asynchronously.
587 void osc_extent_release(const struct lu_env *env, struct osc_extent *ext)
589 struct osc_object *obj = ext->oe_obj;
591 LASSERT(atomic_read(&ext->oe_users) > 0);
592 LASSERT(sanity_check(ext) == 0);
593 LASSERT(ext->oe_grants > 0);
595 if (atomic_dec_and_lock(&ext->oe_users, &obj->oo_lock)) {
596 LASSERT(ext->oe_state == OES_ACTIVE);
597 if (ext->oe_trunc_pending) {
598 /* a truncate process is waiting for this extent.
599 * This may happen due to a race, check
600 * osc_cache_truncate_start().
602 osc_extent_state_set(ext, OES_TRUNC);
603 ext->oe_trunc_pending = 0;
605 osc_extent_state_set(ext, OES_CACHE);
606 osc_update_pending(obj, OBD_BRW_WRITE,
609 /* try to merge the previous and next extent. */
610 osc_extent_merge(env, ext, prev_extent(ext));
611 osc_extent_merge(env, ext, next_extent(ext));
614 list_move_tail(&ext->oe_link,
615 &obj->oo_urgent_exts);
617 osc_object_unlock(obj);
619 osc_io_unplug_async(env, osc_cli(obj), obj);
621 osc_extent_put(env, ext);
624 static inline int overlapped(struct osc_extent *ex1, struct osc_extent *ex2)
626 return !(ex1->oe_end < ex2->oe_start || ex2->oe_end < ex1->oe_start);
630 * Find or create an extent which includes @index, core function to manage
633 static struct osc_extent *osc_extent_find(const struct lu_env *env,
634 struct osc_object *obj, pgoff_t index,
635 unsigned int *grants)
637 struct client_obd *cli = osc_cli(obj);
638 struct osc_lock *olck;
639 struct cl_lock_descr *descr;
640 struct osc_extent *cur;
641 struct osc_extent *ext;
642 struct osc_extent *conflict = NULL;
643 struct osc_extent *found = NULL;
646 unsigned int max_pages; /* max_pages_per_rpc */
647 unsigned int chunksize;
648 int ppc_bits; /* pages per chunk bits */
652 cur = osc_extent_alloc(obj);
654 return ERR_PTR(-ENOMEM);
656 olck = osc_env_io(env)->oi_write_osclock;
657 LASSERTF(olck, "page %lu is not covered by lock\n", index);
658 LASSERT(olck->ols_state == OLS_GRANTED);
660 descr = &olck->ols_cl.cls_lock->cll_descr;
661 LASSERT(descr->cld_mode >= CLM_WRITE);
663 LASSERT(cli->cl_chunkbits >= PAGE_SHIFT);
664 ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
665 chunk_mask = ~((1 << ppc_bits) - 1);
666 chunksize = 1 << cli->cl_chunkbits;
667 chunk = index >> ppc_bits;
669 /* align end to rpc edge, rpc size may not be a power 2 integer. */
670 max_pages = cli->cl_max_pages_per_rpc;
671 LASSERT((max_pages & ~chunk_mask) == 0);
672 max_end = index - (index % max_pages) + max_pages - 1;
673 max_end = min_t(pgoff_t, max_end, descr->cld_end);
675 /* initialize new extent by parameters so far */
676 cur->oe_max_end = max_end;
677 cur->oe_start = index & chunk_mask;
678 cur->oe_end = ((index + ~chunk_mask + 1) & chunk_mask) - 1;
679 if (cur->oe_start < descr->cld_start)
680 cur->oe_start = descr->cld_start;
681 if (cur->oe_end > max_end)
682 cur->oe_end = max_end;
684 cur->oe_mppr = max_pages;
685 if (olck->ols_dlmlock) {
686 LASSERT(olck->ols_hold);
687 cur->oe_dlmlock = LDLM_LOCK_GET(olck->ols_dlmlock);
688 lu_ref_add(&olck->ols_dlmlock->l_reference, "osc_extent", cur);
691 /* grants has been allocated by caller */
692 LASSERTF(*grants >= chunksize + cli->cl_extent_tax,
693 "%u/%u/%u.\n", *grants, chunksize, cli->cl_extent_tax);
694 LASSERTF((max_end - cur->oe_start) < max_pages, EXTSTR"\n",
698 osc_object_lock(obj);
699 ext = osc_extent_search(obj, cur->oe_start);
701 ext = first_extent(obj);
703 pgoff_t ext_chk_start = ext->oe_start >> ppc_bits;
704 pgoff_t ext_chk_end = ext->oe_end >> ppc_bits;
706 LASSERT(sanity_check_nolock(ext) == 0);
707 if (chunk > ext_chk_end + 1)
710 /* if covering by different locks, no chance to match */
711 if (olck->ols_dlmlock != ext->oe_dlmlock) {
712 EASSERTF(!overlapped(ext, cur), ext,
713 EXTSTR"\n", EXTPARA(cur));
715 ext = next_extent(ext);
719 /* discontiguous chunks? */
720 if (chunk + 1 < ext_chk_start) {
721 ext = next_extent(ext);
725 /* ok, from now on, ext and cur have these attrs:
726 * 1. covered by the same lock
727 * 2. contiguous at chunk level or overlapping.
730 if (overlapped(ext, cur)) {
731 /* cur is the minimum unit, so overlapping means
734 EASSERTF((ext->oe_start <= cur->oe_start &&
735 ext->oe_end >= cur->oe_end),
736 ext, EXTSTR"\n", EXTPARA(cur));
738 if (ext->oe_state > OES_CACHE || ext->oe_fsync_wait) {
739 /* for simplicity, we wait for this extent to
740 * finish before going forward.
742 conflict = osc_extent_get(ext);
746 found = osc_extent_hold(ext);
750 /* non-overlapped extent */
751 if (ext->oe_state != OES_CACHE || ext->oe_fsync_wait) {
752 /* we can't do anything for a non OES_CACHE extent, or
753 * if there is someone waiting for this extent to be
754 * flushed, try next one.
756 ext = next_extent(ext);
760 /* check if they belong to the same rpc slot before trying to
761 * merge. the extents are not overlapped and contiguous at
762 * chunk level to get here.
764 if (ext->oe_max_end != max_end) {
765 /* if they don't belong to the same RPC slot or
766 * max_pages_per_rpc has ever changed, do not merge.
768 ext = next_extent(ext);
772 /* it's required that an extent must be contiguous at chunk
773 * level so that we know the whole extent is covered by grant
774 * (the pages in the extent are NOT required to be contiguous).
775 * Otherwise, it will be too much difficult to know which
776 * chunks have grants allocated.
779 /* try to do front merge - extend ext's start */
780 if (chunk + 1 == ext_chk_start) {
781 /* ext must be chunk size aligned */
782 EASSERT((ext->oe_start & ~chunk_mask) == 0, ext);
784 /* pull ext's start back to cover cur */
785 ext->oe_start = cur->oe_start;
786 ext->oe_grants += chunksize;
787 *grants -= chunksize;
789 found = osc_extent_hold(ext);
790 } else if (chunk == ext_chk_end + 1) {
792 ext->oe_end = cur->oe_end;
793 ext->oe_grants += chunksize;
794 *grants -= chunksize;
796 /* try to merge with the next one because we just fill
799 if (osc_extent_merge(env, ext, next_extent(ext)) == 0)
800 /* we can save extent tax from next extent */
801 *grants += cli->cl_extent_tax;
803 found = osc_extent_hold(ext);
808 ext = next_extent(ext);
811 osc_extent_tree_dump(D_CACHE, obj);
814 if (!IS_ERR(found)) {
815 LASSERT(found->oe_dlmlock == cur->oe_dlmlock);
816 OSC_EXTENT_DUMP(D_CACHE, found,
817 "found caching ext for %lu.\n", index);
819 } else if (!conflict) {
820 /* create a new extent */
821 EASSERT(osc_extent_is_overlapped(obj, cur) == 0, cur);
822 cur->oe_grants = chunksize + cli->cl_extent_tax;
823 *grants -= cur->oe_grants;
824 LASSERT(*grants >= 0);
826 cur->oe_state = OES_CACHE;
827 found = osc_extent_hold(cur);
828 osc_extent_insert(obj, cur);
829 OSC_EXTENT_DUMP(D_CACHE, cur, "add into tree %lu/%lu.\n",
830 index, descr->cld_end);
832 osc_object_unlock(obj);
837 /* waiting for IO to finish. Please notice that it's impossible
838 * to be an OES_TRUNC extent.
840 rc = osc_extent_wait(env, conflict, OES_INV);
841 osc_extent_put(env, conflict);
852 osc_extent_put(env, cur);
853 LASSERT(*grants >= 0);
858 * Called when IO is finished to an extent.
860 int osc_extent_finish(const struct lu_env *env, struct osc_extent *ext,
863 struct client_obd *cli = osc_cli(ext->oe_obj);
864 struct osc_async_page *oap;
865 struct osc_async_page *tmp;
866 int nr_pages = ext->oe_nr_pages;
868 int blocksize = cli->cl_import->imp_obd->obd_osfs.os_bsize ? : 4096;
872 OSC_EXTENT_DUMP(D_CACHE, ext, "extent finished.\n");
874 ext->oe_rc = rc ?: ext->oe_nr_pages;
875 EASSERT(ergo(rc == 0, ext->oe_state == OES_RPC), ext);
877 osc_lru_add_batch(cli, &ext->oe_pages);
878 list_for_each_entry_safe(oap, tmp, &ext->oe_pages, oap_pending_item) {
879 list_del_init(&oap->oap_rpc_item);
880 list_del_init(&oap->oap_pending_item);
881 if (last_off <= oap->oap_obj_off) {
882 last_off = oap->oap_obj_off;
883 last_count = oap->oap_count;
887 osc_ap_completion(env, cli, oap, sent, rc);
889 EASSERT(ext->oe_nr_pages == 0, ext);
892 lost_grant = ext->oe_grants;
893 } else if (blocksize < PAGE_SIZE &&
894 last_count != PAGE_SIZE) {
895 /* For short writes we shouldn't count parts of pages that
896 * span a whole chunk on the OST side, or our accounting goes
897 * wrong. Should match the code in filter_grant_check.
899 int offset = last_off & ~PAGE_MASK;
900 int count = last_count + (offset & (blocksize - 1));
901 int end = (offset + last_count) & (blocksize - 1);
903 count += blocksize - end;
905 lost_grant = PAGE_SIZE - count;
907 if (ext->oe_grants > 0)
908 osc_free_grant(cli, nr_pages, lost_grant);
910 osc_extent_remove(ext);
911 /* put the refcount for RPC */
912 osc_extent_put(env, ext);
916 static int extent_wait_cb(struct osc_extent *ext, enum osc_extent_state state)
920 osc_object_lock(ext->oe_obj);
921 ret = ext->oe_state == state;
922 osc_object_unlock(ext->oe_obj);
928 * Wait for the extent's state to become @state.
930 static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
931 enum osc_extent_state state)
933 struct osc_object *obj = ext->oe_obj;
934 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL,
935 LWI_ON_SIGNAL_NOOP, NULL);
938 osc_object_lock(obj);
939 LASSERT(sanity_check_nolock(ext) == 0);
940 /* `Kick' this extent only if the caller is waiting for it to be
943 if (state == OES_INV && !ext->oe_urgent && !ext->oe_hp &&
944 !ext->oe_trunc_pending) {
945 if (ext->oe_state == OES_ACTIVE) {
947 } else if (ext->oe_state == OES_CACHE) {
949 osc_extent_hold(ext);
953 osc_object_unlock(obj);
955 osc_extent_release(env, ext);
957 /* wait for the extent until its state becomes @state */
958 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), &lwi);
959 if (rc == -ETIMEDOUT) {
960 OSC_EXTENT_DUMP(D_ERROR, ext,
961 "%s: wait ext to %u timedout, recovery in progress?\n",
962 osc_export(obj)->exp_obd->obd_name, state);
964 lwi = LWI_INTR(NULL, NULL);
965 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state),
968 if (rc == 0 && ext->oe_rc < 0)
974 * Discard pages with index greater than @size. If @ext is overlapped with
975 * @size, then partial truncate happens.
977 static int osc_extent_truncate(struct osc_extent *ext, pgoff_t trunc_index,
980 struct cl_env_nest nest;
983 struct osc_object *obj = ext->oe_obj;
984 struct client_obd *cli = osc_cli(obj);
985 struct osc_async_page *oap;
986 struct osc_async_page *tmp;
987 int pages_in_chunk = 0;
988 int ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
989 __u64 trunc_chunk = trunc_index >> ppc_bits;
994 LASSERT(sanity_check(ext) == 0);
995 EASSERT(ext->oe_state == OES_TRUNC, ext);
996 EASSERT(!ext->oe_urgent, ext);
998 /* Request new lu_env.
999 * We can't use that env from osc_cache_truncate_start() because
1000 * it's from lov_io_sub and not fully initialized.
1002 env = cl_env_nested_get(&nest);
1003 io = &osc_env_info(env)->oti_io;
1004 io->ci_obj = cl_object_top(osc2cl(obj));
1005 rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
1009 /* discard all pages with index greater then trunc_index */
1010 list_for_each_entry_safe(oap, tmp, &ext->oe_pages, oap_pending_item) {
1011 pgoff_t index = osc_index(oap2osc(oap));
1012 struct cl_page *page = oap2cl_page(oap);
1014 LASSERT(list_empty(&oap->oap_rpc_item));
1016 /* only discard the pages with their index greater than
1017 * trunc_index, and ...
1019 if (index < trunc_index ||
1020 (index == trunc_index && partial)) {
1021 /* accounting how many pages remaining in the chunk
1022 * so that we can calculate grants correctly. */
1023 if (index >> ppc_bits == trunc_chunk)
1028 list_del_init(&oap->oap_pending_item);
1031 lu_ref_add(&page->cp_reference, "truncate", current);
1033 if (cl_page_own(env, io, page) == 0) {
1034 cl_page_discard(env, io, page);
1035 cl_page_disown(env, io, page);
1037 LASSERT(page->cp_state == CPS_FREEING);
1041 lu_ref_del(&page->cp_reference, "truncate", current);
1042 cl_page_put(env, page);
1047 EASSERTF(ergo(ext->oe_start >= trunc_index + !!partial,
1048 ext->oe_nr_pages == 0),
1049 ext, "trunc_index %lu, partial %d\n", trunc_index, partial);
1051 osc_object_lock(obj);
1052 if (ext->oe_nr_pages == 0) {
1053 LASSERT(pages_in_chunk == 0);
1054 grants = ext->oe_grants;
1056 } else { /* calculate how many grants we can free */
1057 int chunks = (ext->oe_end >> ppc_bits) - trunc_chunk;
1060 /* if there is no pages in this chunk, we can also free grants
1061 * for the last chunk
1063 if (pages_in_chunk == 0) {
1064 /* if this is the 1st chunk and no pages in this chunk,
1065 * ext->oe_nr_pages must be zero, so we should be in
1066 * the other if-clause.
1068 LASSERT(trunc_chunk > 0);
1073 /* this is what we can free from this extent */
1074 grants = chunks << cli->cl_chunkbits;
1075 ext->oe_grants -= grants;
1076 last_index = ((trunc_chunk + 1) << ppc_bits) - 1;
1077 ext->oe_end = min(last_index, ext->oe_max_end);
1078 LASSERT(ext->oe_end >= ext->oe_start);
1079 LASSERT(ext->oe_grants > 0);
1081 osc_object_unlock(obj);
1083 if (grants > 0 || nr_pages > 0)
1084 osc_free_grant(cli, nr_pages, grants);
1087 cl_io_fini(env, io);
1088 cl_env_nested_put(&nest, env);
1093 * This function is used to make the extent prepared for transfer.
1094 * A race with flushing page - ll_writepage() has to be handled cautiously.
1096 static int osc_extent_make_ready(const struct lu_env *env,
1097 struct osc_extent *ext)
1099 struct osc_async_page *oap;
1100 struct osc_async_page *last = NULL;
1101 struct osc_object *obj = ext->oe_obj;
1102 unsigned int page_count = 0;
1105 /* we're going to grab page lock, so object lock must not be taken. */
1106 LASSERT(sanity_check(ext) == 0);
1107 /* in locking state, any process should not touch this extent. */
1108 EASSERT(ext->oe_state == OES_LOCKING, ext);
1109 EASSERT(ext->oe_owner, ext);
1111 OSC_EXTENT_DUMP(D_CACHE, ext, "make ready\n");
1113 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1115 if (!last || last->oap_obj_off < oap->oap_obj_off)
1118 /* checking ASYNC_READY is race safe */
1119 if ((oap->oap_async_flags & ASYNC_READY) != 0)
1122 rc = osc_make_ready(env, oap, OBD_BRW_WRITE);
1125 spin_lock(&oap->oap_lock);
1126 oap->oap_async_flags |= ASYNC_READY;
1127 spin_unlock(&oap->oap_lock);
1130 LASSERT((oap->oap_async_flags & ASYNC_READY) != 0);
1133 LASSERTF(0, "unknown return code: %d\n", rc);
1137 LASSERT(page_count == ext->oe_nr_pages);
1139 /* the last page is the only one we need to refresh its count by
1142 if (!(last->oap_async_flags & ASYNC_COUNT_STABLE)) {
1143 int last_oap_count = osc_refresh_count(env, last, OBD_BRW_WRITE);
1145 LASSERT(last_oap_count > 0);
1146 LASSERT(last->oap_page_off + last_oap_count <= PAGE_SIZE);
1147 last->oap_count = last_oap_count;
1148 spin_lock(&last->oap_lock);
1149 last->oap_async_flags |= ASYNC_COUNT_STABLE;
1150 spin_unlock(&last->oap_lock);
1153 /* for the rest of pages, we don't need to call osf_refresh_count()
1154 * because it's known they are not the last page
1156 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1157 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE)) {
1158 oap->oap_count = PAGE_SIZE - oap->oap_page_off;
1159 spin_lock(&last->oap_lock);
1160 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1161 spin_unlock(&last->oap_lock);
1165 osc_object_lock(obj);
1166 osc_extent_state_set(ext, OES_RPC);
1167 osc_object_unlock(obj);
1168 /* get a refcount for RPC. */
1169 osc_extent_get(ext);
1175 * Quick and simple version of osc_extent_find(). This function is frequently
1176 * called to expand the extent for the same IO. To expand the extent, the
1177 * page index must be in the same or next chunk of ext->oe_end.
1179 static int osc_extent_expand(struct osc_extent *ext, pgoff_t index,
1180 unsigned int *grants)
1182 struct osc_object *obj = ext->oe_obj;
1183 struct client_obd *cli = osc_cli(obj);
1184 struct osc_extent *next;
1185 int ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
1186 pgoff_t chunk = index >> ppc_bits;
1189 unsigned int chunksize = 1 << cli->cl_chunkbits;
1192 LASSERT(ext->oe_max_end >= index && ext->oe_start <= index);
1193 osc_object_lock(obj);
1194 LASSERT(sanity_check_nolock(ext) == 0);
1195 end_chunk = ext->oe_end >> ppc_bits;
1196 if (chunk > end_chunk + 1) {
1201 if (end_chunk >= chunk) {
1206 LASSERT(end_chunk + 1 == chunk);
1207 /* try to expand this extent to cover @index */
1208 end_index = min(ext->oe_max_end, ((chunk + 1) << ppc_bits) - 1);
1210 next = next_extent(ext);
1211 if (next && next->oe_start <= end_index) {
1212 /* complex mode - overlapped with the next extent,
1213 * this case will be handled by osc_extent_find()
1219 ext->oe_end = end_index;
1220 ext->oe_grants += chunksize;
1221 *grants -= chunksize;
1222 LASSERT(*grants >= 0);
1223 EASSERTF(osc_extent_is_overlapped(obj, ext) == 0, ext,
1224 "overlapped after expanding for %lu.\n", index);
1227 osc_object_unlock(obj);
1231 static void osc_extent_tree_dump0(int level, struct osc_object *obj,
1232 const char *func, int line)
1234 struct osc_extent *ext;
1237 CDEBUG(level, "Dump object %p extents at %s:%d, mppr: %u.\n",
1238 obj, func, line, osc_cli(obj)->cl_max_pages_per_rpc);
1240 /* osc_object_lock(obj); */
1242 for (ext = first_extent(obj); ext; ext = next_extent(ext))
1243 OSC_EXTENT_DUMP(level, ext, "in tree %d.\n", cnt++);
1246 list_for_each_entry(ext, &obj->oo_hp_exts, oe_link)
1247 OSC_EXTENT_DUMP(level, ext, "hp %d.\n", cnt++);
1250 list_for_each_entry(ext, &obj->oo_urgent_exts, oe_link)
1251 OSC_EXTENT_DUMP(level, ext, "urgent %d.\n", cnt++);
1254 list_for_each_entry(ext, &obj->oo_reading_exts, oe_link)
1255 OSC_EXTENT_DUMP(level, ext, "reading %d.\n", cnt++);
1256 /* osc_object_unlock(obj); */
1259 /* ------------------ osc extent end ------------------ */
1261 static inline int osc_is_ready(struct osc_object *osc)
1263 return !list_empty(&osc->oo_ready_item) ||
1264 !list_empty(&osc->oo_hp_ready_item);
1267 #define OSC_IO_DEBUG(OSC, STR, args...) \
1268 CDEBUG(D_CACHE, "obj %p ready %d|%c|%c wr %d|%c|%c rd %d|%c " STR, \
1269 (OSC), osc_is_ready(OSC), \
1270 list_empty_marker(&(OSC)->oo_hp_ready_item), \
1271 list_empty_marker(&(OSC)->oo_ready_item), \
1272 atomic_read(&(OSC)->oo_nr_writes), \
1273 list_empty_marker(&(OSC)->oo_hp_exts), \
1274 list_empty_marker(&(OSC)->oo_urgent_exts), \
1275 atomic_read(&(OSC)->oo_nr_reads), \
1276 list_empty_marker(&(OSC)->oo_reading_exts), \
1279 static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
1282 struct osc_page *opg = oap2osc_page(oap);
1283 struct cl_page *page = oap2cl_page(oap);
1286 LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
1288 result = cl_page_make_ready(env, page, CRT_WRITE);
1290 opg->ops_submit_time = cfs_time_current();
1294 static int osc_refresh_count(const struct lu_env *env,
1295 struct osc_async_page *oap, int cmd)
1297 struct osc_page *opg = oap2osc_page(oap);
1298 pgoff_t index = osc_index(oap2osc(oap));
1299 struct cl_object *obj;
1300 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
1305 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
1306 LASSERT(!(cmd & OBD_BRW_READ));
1307 obj = opg->ops_cl.cpl_obj;
1309 cl_object_attr_lock(obj);
1310 result = cl_object_attr_get(env, obj, attr);
1311 cl_object_attr_unlock(obj);
1314 kms = attr->cat_kms;
1315 if (cl_offset(obj, index) >= kms)
1316 /* catch race with truncate */
1318 else if (cl_offset(obj, index + 1) > kms)
1319 /* catch sub-page write at end of file */
1320 return kms % PAGE_SIZE;
1325 static int osc_completion(const struct lu_env *env, struct osc_async_page *oap,
1328 struct osc_page *opg = oap2osc_page(oap);
1329 struct cl_page *page = oap2cl_page(oap);
1330 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
1331 enum cl_req_type crt;
1334 cmd &= ~OBD_BRW_NOQUOTA;
1335 LASSERTF(equi(page->cp_state == CPS_PAGEIN, cmd == OBD_BRW_READ),
1336 "cp_state:%u, cmd:%d\n", page->cp_state, cmd);
1337 LASSERTF(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE),
1338 "cp_state:%u, cmd:%d\n", page->cp_state, cmd);
1339 LASSERT(opg->ops_transfer_pinned);
1342 * page->cp_req can be NULL if io submission failed before
1343 * cl_req was allocated.
1346 cl_req_page_done(env, page);
1347 LASSERT(!page->cp_req);
1349 crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
1350 /* Clear opg->ops_transfer_pinned before VM lock is released. */
1351 opg->ops_transfer_pinned = 0;
1353 spin_lock(&obj->oo_seatbelt);
1354 LASSERT(opg->ops_submitter);
1355 LASSERT(!list_empty(&opg->ops_inflight));
1356 list_del_init(&opg->ops_inflight);
1357 opg->ops_submitter = NULL;
1358 spin_unlock(&obj->oo_seatbelt);
1360 opg->ops_submit_time = 0;
1361 srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK;
1364 if (rc == 0 && srvlock) {
1365 struct lu_device *ld = opg->ops_cl.cpl_obj->co_lu.lo_dev;
1366 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
1367 size_t bytes = oap->oap_count;
1369 if (crt == CRT_READ)
1370 stats->os_lockless_reads += bytes;
1372 stats->os_lockless_writes += bytes;
1376 * This has to be the last operation with the page, as locks are
1377 * released in cl_page_completion() and nothing except for the
1378 * reference counter protects page from concurrent reclaim.
1380 lu_ref_del(&page->cp_reference, "transfer", page);
1382 cl_page_completion(env, page, crt, rc);
1387 #define OSC_DUMP_GRANT(lvl, cli, fmt, args...) do { \
1388 struct client_obd *__tmp = (cli); \
1389 CDEBUG(lvl, "%s: grant { dirty: %ld/%ld dirty_pages: %ld/%lu " \
1390 "dropped: %ld avail: %ld, reserved: %ld, flight: %d }" \
1391 "lru {in list: %ld, left: %ld, waiters: %d }" fmt "\n", \
1392 __tmp->cl_import->imp_obd->obd_name, \
1393 __tmp->cl_dirty_pages, __tmp->cl_dirty_max_pages, \
1394 atomic_long_read(&obd_dirty_pages), obd_max_dirty_pages, \
1395 __tmp->cl_lost_grant, __tmp->cl_avail_grant, \
1396 __tmp->cl_reserved_grant, __tmp->cl_w_in_flight, \
1397 atomic_long_read(&__tmp->cl_lru_in_list), \
1398 atomic_long_read(&__tmp->cl_lru_busy), \
1399 atomic_read(&__tmp->cl_lru_shrinkers), ##args); \
1402 /* caller must hold loi_list_lock */
1403 static void osc_consume_write_grant(struct client_obd *cli,
1404 struct brw_page *pga)
1406 assert_spin_locked(&cli->cl_loi_list_lock);
1407 LASSERT(!(pga->flag & OBD_BRW_FROM_GRANT));
1408 atomic_long_inc(&obd_dirty_pages);
1409 cli->cl_dirty_pages++;
1410 pga->flag |= OBD_BRW_FROM_GRANT;
1411 CDEBUG(D_CACHE, "using %lu grant credits for brw %p page %p\n",
1412 PAGE_SIZE, pga, pga->pg);
1413 osc_update_next_shrink(cli);
1416 /* the companion to osc_consume_write_grant, called when a brw has completed.
1417 * must be called with the loi lock held.
1419 static void osc_release_write_grant(struct client_obd *cli,
1420 struct brw_page *pga)
1422 assert_spin_locked(&cli->cl_loi_list_lock);
1423 if (!(pga->flag & OBD_BRW_FROM_GRANT)) {
1427 pga->flag &= ~OBD_BRW_FROM_GRANT;
1428 atomic_long_dec(&obd_dirty_pages);
1429 cli->cl_dirty_pages--;
1430 if (pga->flag & OBD_BRW_NOCACHE) {
1431 pga->flag &= ~OBD_BRW_NOCACHE;
1432 atomic_long_dec(&obd_dirty_transit_pages);
1433 cli->cl_dirty_transit--;
1438 * To avoid sleeping with object lock held, it's good for us allocate enough
1439 * grants before entering into critical section.
1441 * spin_lock held by caller
1443 static int osc_reserve_grant(struct client_obd *cli, unsigned int bytes)
1447 if (cli->cl_avail_grant >= bytes) {
1448 cli->cl_avail_grant -= bytes;
1449 cli->cl_reserved_grant += bytes;
1455 static void __osc_unreserve_grant(struct client_obd *cli,
1456 unsigned int reserved, unsigned int unused)
1458 /* it's quite normal for us to get more grant than reserved.
1459 * Thinking about a case that two extents merged by adding a new
1460 * chunk, we can save one extent tax. If extent tax is greater than
1461 * one chunk, we can save more grant by adding a new chunk
1463 cli->cl_reserved_grant -= reserved;
1464 if (unused > reserved) {
1465 cli->cl_avail_grant += reserved;
1466 cli->cl_lost_grant += unused - reserved;
1468 cli->cl_avail_grant += unused;
1472 static void osc_unreserve_grant(struct client_obd *cli,
1473 unsigned int reserved, unsigned int unused)
1475 spin_lock(&cli->cl_loi_list_lock);
1476 __osc_unreserve_grant(cli, reserved, unused);
1478 osc_wake_cache_waiters(cli);
1479 spin_unlock(&cli->cl_loi_list_lock);
1483 * Free grant after IO is finished or canceled.
1485 * @lost_grant is used to remember how many grants we have allocated but not
1486 * used, we should return these grants to OST. There're two cases where grants
1489 * 2. blocksize at OST is less than PAGE_SIZE and a partial page was
1490 * written. In this case OST may use less chunks to serve this partial
1491 * write. OSTs don't actually know the page size on the client side. so
1492 * clients have to calculate lost grant by the blocksize on the OST.
1493 * See filter_grant_check() for details.
1495 static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
1496 unsigned int lost_grant)
1498 unsigned long grant = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
1500 spin_lock(&cli->cl_loi_list_lock);
1501 atomic_long_sub(nr_pages, &obd_dirty_pages);
1502 cli->cl_dirty_pages -= nr_pages;
1503 cli->cl_lost_grant += lost_grant;
1504 if (cli->cl_avail_grant < grant && cli->cl_lost_grant >= grant) {
1505 /* borrow some grant from truncate to avoid the case that
1506 * truncate uses up all avail grant
1508 cli->cl_lost_grant -= grant;
1509 cli->cl_avail_grant += grant;
1511 osc_wake_cache_waiters(cli);
1512 spin_unlock(&cli->cl_loi_list_lock);
1513 CDEBUG(D_CACHE, "lost %u grant: %lu avail: %lu dirty: %lu\n",
1514 lost_grant, cli->cl_lost_grant,
1515 cli->cl_avail_grant, cli->cl_dirty_pages << PAGE_SHIFT);
1519 * The companion to osc_enter_cache(), called when @oap is no longer part of
1520 * the dirty accounting due to error.
1522 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1524 spin_lock(&cli->cl_loi_list_lock);
1525 osc_release_write_grant(cli, &oap->oap_brw_page);
1526 spin_unlock(&cli->cl_loi_list_lock);
1530 * Non-blocking version of osc_enter_cache() that consumes grant only when it
1533 static int osc_enter_cache_try(struct client_obd *cli,
1534 struct osc_async_page *oap,
1535 int bytes, int transient)
1539 OSC_DUMP_GRANT(D_CACHE, cli, "need:%d\n", bytes);
1541 rc = osc_reserve_grant(cli, bytes);
1545 if (cli->cl_dirty_pages <= cli->cl_dirty_max_pages &&
1546 atomic_long_read(&obd_dirty_pages) + 1 <= obd_max_dirty_pages) {
1547 osc_consume_write_grant(cli, &oap->oap_brw_page);
1549 cli->cl_dirty_transit++;
1550 atomic_long_inc(&obd_dirty_transit_pages);
1551 oap->oap_brw_flags |= OBD_BRW_NOCACHE;
1555 __osc_unreserve_grant(cli, bytes, bytes);
1561 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1565 spin_lock(&cli->cl_loi_list_lock);
1566 rc = list_empty(&ocw->ocw_entry);
1567 spin_unlock(&cli->cl_loi_list_lock);
1572 * The main entry to reserve dirty page accounting. Usually the grant reserved
1573 * in this function will be freed in bulk in osc_free_grant() unless it fails
1574 * to add osc cache, in that case, it will be freed in osc_exit_cache().
1576 * The process will be put into sleep if it's already run out of grant.
1578 static int osc_enter_cache(const struct lu_env *env, struct client_obd *cli,
1579 struct osc_async_page *oap, int bytes)
1581 struct osc_object *osc = oap->oap_obj;
1582 struct lov_oinfo *loi = osc->oo_oinfo;
1583 struct osc_cache_waiter ocw;
1584 struct l_wait_info lwi;
1587 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(AT_OFF ? obd_timeout : at_max),
1588 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1590 OSC_DUMP_GRANT(D_CACHE, cli, "need:%d\n", bytes);
1592 spin_lock(&cli->cl_loi_list_lock);
1594 /* force the caller to try sync io. this can jump the list
1595 * of queued writes and create a discontiguous rpc stream
1597 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_NO_GRANT) ||
1598 !cli->cl_dirty_max_pages || cli->cl_ar.ar_force_sync ||
1599 loi->loi_ar.ar_force_sync) {
1600 OSC_DUMP_GRANT(D_CACHE, cli, "forced sync i/o\n");
1605 /* Hopefully normal case - cache space and write credits available */
1606 if (osc_enter_cache_try(cli, oap, bytes, 0)) {
1607 OSC_DUMP_GRANT(D_CACHE, cli, "granted from cache\n");
1612 /* We can get here for two reasons: too many dirty pages in cache, or
1613 * run out of grants. In both cases we should write dirty pages out.
1614 * Adding a cache waiter will trigger urgent write-out no matter what
1616 * The exiting condition is no avail grants and no dirty pages caching,
1617 * that really means there is no space on the OST.
1619 init_waitqueue_head(&ocw.ocw_waitq);
1621 ocw.ocw_grant = bytes;
1622 while (cli->cl_dirty_pages > 0 || cli->cl_w_in_flight > 0) {
1623 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1625 spin_unlock(&cli->cl_loi_list_lock);
1627 osc_io_unplug_async(env, cli, NULL);
1629 CDEBUG(D_CACHE, "%s: sleeping for cache space @ %p for %p\n",
1630 cli->cl_import->imp_obd->obd_name, &ocw, oap);
1632 rc = l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1634 spin_lock(&cli->cl_loi_list_lock);
1637 /* l_wait_event is interrupted by signal, or timed out */
1638 list_del_init(&ocw.ocw_entry);
1641 LASSERT(list_empty(&ocw.ocw_entry));
1646 if (osc_enter_cache_try(cli, oap, bytes, 0)) {
1654 OSC_DUMP_GRANT(D_CACHE, cli, "finally got grant space\n");
1657 OSC_DUMP_GRANT(D_CACHE, cli,
1658 "timeout, fall back to sync i/o\n");
1659 osc_extent_tree_dump(D_CACHE, osc);
1660 /* fall back to synchronous I/O */
1664 /* Ensures restartability - LU-3581 */
1665 OSC_DUMP_GRANT(D_CACHE, cli, "interrupted\n");
1669 OSC_DUMP_GRANT(D_CACHE, cli,
1670 "no grant space, fall back to sync i/o\n");
1673 CDEBUG(D_CACHE, "%s: event for cache space @ %p never arrived due to %d, fall back to sync i/o\n",
1674 cli->cl_import->imp_obd->obd_name, &ocw, rc);
1678 spin_unlock(&cli->cl_loi_list_lock);
1682 /* caller must hold loi_list_lock */
1683 void osc_wake_cache_waiters(struct client_obd *cli)
1685 struct list_head *l, *tmp;
1686 struct osc_cache_waiter *ocw;
1688 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1689 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
1690 list_del_init(&ocw->ocw_entry);
1692 ocw->ocw_rc = -EDQUOT;
1693 /* we can't dirty more */
1694 if ((cli->cl_dirty_pages > cli->cl_dirty_max_pages) ||
1695 (atomic_long_read(&obd_dirty_pages) + 1 >
1696 obd_max_dirty_pages)) {
1697 CDEBUG(D_CACHE, "no dirty room: dirty: %ld osc max %ld, sys max %ld\n",
1698 cli->cl_dirty_pages, cli->cl_dirty_max_pages,
1699 obd_max_dirty_pages);
1703 if (osc_enter_cache_try(cli, ocw->ocw_oap, ocw->ocw_grant, 0))
1706 CDEBUG(D_CACHE, "wake up %p for oap %p, avail grant %ld, %d\n",
1707 ocw, ocw->ocw_oap, cli->cl_avail_grant, ocw->ocw_rc);
1709 wake_up(&ocw->ocw_waitq);
1713 static int osc_max_rpc_in_flight(struct client_obd *cli, struct osc_object *osc)
1715 int hprpc = !!list_empty(&osc->oo_hp_exts);
1717 return rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight + hprpc;
1720 /* This maintains the lists of pending pages to read/write for a given object
1721 * (lop). This is used by osc_check_rpcs->osc_next_obj() and osc_list_maint()
1722 * to quickly find objects that are ready to send an RPC.
1724 static int osc_makes_rpc(struct client_obd *cli, struct osc_object *osc,
1727 int invalid_import = 0;
1729 /* if we have an invalid import we want to drain the queued pages
1730 * by forcing them through rpcs that immediately fail and complete
1731 * the pages. recovery relies on this to empty the queued pages
1732 * before canceling the locks and evicting down the llite pages
1734 if (!cli->cl_import || cli->cl_import->imp_invalid)
1737 if (cmd & OBD_BRW_WRITE) {
1738 if (atomic_read(&osc->oo_nr_writes) == 0)
1740 if (invalid_import) {
1741 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1744 if (!list_empty(&osc->oo_hp_exts)) {
1745 CDEBUG(D_CACHE, "high prio request forcing RPC\n");
1748 if (!list_empty(&osc->oo_urgent_exts)) {
1749 CDEBUG(D_CACHE, "urgent request forcing RPC\n");
1752 /* trigger a write rpc stream as long as there are dirtiers
1753 * waiting for space. as they're waiting, they're not going to
1754 * create more pages to coalesce with what's waiting..
1756 if (!list_empty(&cli->cl_cache_waiters)) {
1757 CDEBUG(D_CACHE, "cache waiters forcing RPC\n");
1760 if (atomic_read(&osc->oo_nr_writes) >=
1761 cli->cl_max_pages_per_rpc)
1764 if (atomic_read(&osc->oo_nr_reads) == 0)
1766 if (invalid_import) {
1767 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1770 /* all read are urgent. */
1771 if (!list_empty(&osc->oo_reading_exts))
1778 static void osc_update_pending(struct osc_object *obj, int cmd, int delta)
1780 struct client_obd *cli = osc_cli(obj);
1782 if (cmd & OBD_BRW_WRITE) {
1783 atomic_add(delta, &obj->oo_nr_writes);
1784 atomic_add(delta, &cli->cl_pending_w_pages);
1785 LASSERT(atomic_read(&obj->oo_nr_writes) >= 0);
1787 atomic_add(delta, &obj->oo_nr_reads);
1788 atomic_add(delta, &cli->cl_pending_r_pages);
1789 LASSERT(atomic_read(&obj->oo_nr_reads) >= 0);
1791 OSC_IO_DEBUG(obj, "update pending cmd %d delta %d.\n", cmd, delta);
1794 static int osc_makes_hprpc(struct osc_object *obj)
1796 return !list_empty(&obj->oo_hp_exts);
1799 static void on_list(struct list_head *item, struct list_head *list, int should_be_on)
1801 if (list_empty(item) && should_be_on)
1802 list_add_tail(item, list);
1803 else if (!list_empty(item) && !should_be_on)
1804 list_del_init(item);
1807 /* maintain the osc's cli list membership invariants so that osc_send_oap_rpc
1808 * can find pages to build into rpcs quickly
1810 static int __osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1812 if (osc_makes_hprpc(osc)) {
1814 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, 0);
1815 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 1);
1817 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 0);
1818 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list,
1819 osc_makes_rpc(cli, osc, OBD_BRW_WRITE) ||
1820 osc_makes_rpc(cli, osc, OBD_BRW_READ));
1823 on_list(&osc->oo_write_item, &cli->cl_loi_write_list,
1824 atomic_read(&osc->oo_nr_writes) > 0);
1826 on_list(&osc->oo_read_item, &cli->cl_loi_read_list,
1827 atomic_read(&osc->oo_nr_reads) > 0);
1829 return osc_is_ready(osc);
1832 static int osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1836 spin_lock(&cli->cl_loi_list_lock);
1837 is_ready = __osc_list_maint(cli, osc);
1838 spin_unlock(&cli->cl_loi_list_lock);
1843 /* this is trying to propagate async writeback errors back up to the
1844 * application. As an async write fails we record the error code for later if
1845 * the app does an fsync. As long as errors persist we force future rpcs to be
1846 * sync so that the app can get a sync error and break the cycle of queueing
1847 * pages for which writeback will fail.
1849 static void osc_process_ar(struct osc_async_rc *ar, __u64 xid,
1856 ar->ar_force_sync = 1;
1857 ar->ar_min_xid = ptlrpc_sample_next_xid();
1861 if (ar->ar_force_sync && (xid >= ar->ar_min_xid))
1862 ar->ar_force_sync = 0;
1865 /* this must be called holding the loi list lock to give coverage to exit_cache,
1866 * async_flag maintenance, and oap_request
1868 static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
1869 struct osc_async_page *oap, int sent, int rc)
1871 struct osc_object *osc = oap->oap_obj;
1872 struct lov_oinfo *loi = osc->oo_oinfo;
1875 if (oap->oap_request) {
1876 xid = ptlrpc_req_xid(oap->oap_request);
1877 ptlrpc_req_finished(oap->oap_request);
1878 oap->oap_request = NULL;
1881 /* As the transfer for this page is being done, clear the flags */
1882 spin_lock(&oap->oap_lock);
1883 oap->oap_async_flags = 0;
1884 spin_unlock(&oap->oap_lock);
1885 oap->oap_interrupted = 0;
1887 if (oap->oap_cmd & OBD_BRW_WRITE && xid > 0) {
1888 spin_lock(&cli->cl_loi_list_lock);
1889 osc_process_ar(&cli->cl_ar, xid, rc);
1890 osc_process_ar(&loi->loi_ar, xid, rc);
1891 spin_unlock(&cli->cl_loi_list_lock);
1894 rc = osc_completion(env, oap, oap->oap_cmd, rc);
1896 CERROR("completion on oap %p obj %p returns %d.\n",
1901 * Try to add extent to one RPC. We need to think about the following things:
1902 * - # of pages must not be over max_pages_per_rpc
1903 * - extent must be compatible with previous ones
1905 static int try_to_add_extent_for_io(struct client_obd *cli,
1906 struct osc_extent *ext, struct list_head *rpclist,
1907 unsigned int *pc, unsigned int *max_pages)
1909 struct osc_extent *tmp;
1910 struct osc_async_page *oap = list_first_entry(&ext->oe_pages,
1911 struct osc_async_page,
1914 EASSERT((ext->oe_state == OES_CACHE || ext->oe_state == OES_LOCK_DONE),
1917 *max_pages = max(ext->oe_mppr, *max_pages);
1918 if (*pc + ext->oe_nr_pages > *max_pages)
1921 list_for_each_entry(tmp, rpclist, oe_link) {
1922 struct osc_async_page *oap2;
1924 oap2 = list_first_entry(&tmp->oe_pages, struct osc_async_page,
1926 EASSERT(tmp->oe_owner == current, tmp);
1927 if (oap2cl_page(oap)->cp_type != oap2cl_page(oap2)->cp_type) {
1928 CDEBUG(D_CACHE, "Do not permit different type of IO"
1929 " for a same RPC\n");
1933 if (tmp->oe_srvlock != ext->oe_srvlock ||
1934 !tmp->oe_grants != !ext->oe_grants)
1937 /* remove break for strict check */
1941 *pc += ext->oe_nr_pages;
1942 list_move_tail(&ext->oe_link, rpclist);
1943 ext->oe_owner = current;
1948 * In order to prevent multiple ptlrpcd from breaking contiguous extents,
1949 * get_write_extent() takes all appropriate extents in atomic.
1951 * The following policy is used to collect extents for IO:
1952 * 1. Add as many HP extents as possible;
1953 * 2. Add the first urgent extent in urgent extent list and take it out of
1955 * 3. Add subsequent extents of this urgent extent;
1956 * 4. If urgent list is not empty, goto 2;
1957 * 5. Traverse the extent tree from the 1st extent;
1958 * 6. Above steps exit if there is no space in this RPC.
1960 static unsigned int get_write_extents(struct osc_object *obj,
1961 struct list_head *rpclist)
1963 struct client_obd *cli = osc_cli(obj);
1964 struct osc_extent *ext;
1965 struct osc_extent *temp;
1966 unsigned int page_count = 0;
1967 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1969 LASSERT(osc_object_is_locked(obj));
1970 list_for_each_entry_safe(ext, temp, &obj->oo_hp_exts, oe_link) {
1971 LASSERT(ext->oe_state == OES_CACHE);
1972 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1975 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1977 if (page_count == max_pages)
1980 while (!list_empty(&obj->oo_urgent_exts)) {
1981 ext = list_entry(obj->oo_urgent_exts.next,
1982 struct osc_extent, oe_link);
1983 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1987 if (!ext->oe_intree)
1990 while ((ext = next_extent(ext)) != NULL) {
1991 if ((ext->oe_state != OES_CACHE) ||
1992 (!list_empty(&ext->oe_link) &&
1996 if (!try_to_add_extent_for_io(cli, ext, rpclist,
1997 &page_count, &max_pages))
2001 if (page_count == max_pages)
2004 ext = first_extent(obj);
2006 if ((ext->oe_state != OES_CACHE) ||
2007 /* this extent may be already in current rpclist */
2008 (!list_empty(&ext->oe_link) && ext->oe_owner)) {
2009 ext = next_extent(ext);
2013 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
2017 ext = next_extent(ext);
2023 osc_send_write_rpc(const struct lu_env *env, struct client_obd *cli,
2024 struct osc_object *osc)
2028 struct osc_extent *ext;
2029 struct osc_extent *tmp;
2030 struct osc_extent *first = NULL;
2035 LASSERT(osc_object_is_locked(osc));
2037 page_count = get_write_extents(osc, &rpclist);
2038 LASSERT(equi(page_count == 0, list_empty(&rpclist)));
2040 if (list_empty(&rpclist))
2043 osc_update_pending(osc, OBD_BRW_WRITE, -page_count);
2045 list_for_each_entry(ext, &rpclist, oe_link) {
2046 LASSERT(ext->oe_state == OES_CACHE ||
2047 ext->oe_state == OES_LOCK_DONE);
2048 if (ext->oe_state == OES_CACHE)
2049 osc_extent_state_set(ext, OES_LOCKING);
2051 osc_extent_state_set(ext, OES_RPC);
2054 /* we're going to grab page lock, so release object lock because
2055 * lock order is page lock -> object lock.
2057 osc_object_unlock(osc);
2059 list_for_each_entry_safe(ext, tmp, &rpclist, oe_link) {
2060 if (ext->oe_state == OES_LOCKING) {
2061 rc = osc_extent_make_ready(env, ext);
2062 if (unlikely(rc < 0)) {
2063 list_del_init(&ext->oe_link);
2064 osc_extent_finish(env, ext, 0, rc);
2070 srvlock = ext->oe_srvlock;
2072 LASSERT(srvlock == ext->oe_srvlock);
2076 if (!list_empty(&rpclist)) {
2077 LASSERT(page_count > 0);
2078 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_WRITE);
2079 LASSERT(list_empty(&rpclist));
2082 osc_object_lock(osc);
2087 * prepare pages for ASYNC io and put pages in send queue.
2089 * \param cmd OBD_BRW_* macroses
2090 * \param lop pending pages
2092 * \return zero if no page added to send queue.
2093 * \return 1 if pages successfully added to send queue.
2094 * \return negative on errors.
2097 osc_send_read_rpc(const struct lu_env *env, struct client_obd *cli,
2098 struct osc_object *osc)
2101 struct osc_extent *ext;
2102 struct osc_extent *next;
2104 unsigned int page_count = 0;
2105 unsigned int max_pages = cli->cl_max_pages_per_rpc;
2108 LASSERT(osc_object_is_locked(osc));
2109 list_for_each_entry_safe(ext, next, &osc->oo_reading_exts, oe_link) {
2110 EASSERT(ext->oe_state == OES_LOCK_DONE, ext);
2111 if (!try_to_add_extent_for_io(cli, ext, &rpclist, &page_count,
2114 osc_extent_state_set(ext, OES_RPC);
2115 EASSERT(ext->oe_nr_pages <= max_pages, ext);
2117 LASSERT(page_count <= max_pages);
2119 osc_update_pending(osc, OBD_BRW_READ, -page_count);
2121 if (!list_empty(&rpclist)) {
2122 osc_object_unlock(osc);
2124 LASSERT(page_count > 0);
2125 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_READ);
2126 LASSERT(list_empty(&rpclist));
2128 osc_object_lock(osc);
2133 #define list_to_obj(list, item) ({ \
2134 struct list_head *__tmp = (list)->next; \
2135 list_del_init(__tmp); \
2136 list_entry(__tmp, struct osc_object, oo_##item); \
2139 /* This is called by osc_check_rpcs() to find which objects have pages that
2140 * we could be sending. These lists are maintained by osc_makes_rpc().
2142 static struct osc_object *osc_next_obj(struct client_obd *cli)
2144 /* First return objects that have blocked locks so that they
2145 * will be flushed quickly and other clients can get the lock,
2146 * then objects which have pages ready to be stuffed into RPCs
2148 if (!list_empty(&cli->cl_loi_hp_ready_list))
2149 return list_to_obj(&cli->cl_loi_hp_ready_list, hp_ready_item);
2150 if (!list_empty(&cli->cl_loi_ready_list))
2151 return list_to_obj(&cli->cl_loi_ready_list, ready_item);
2153 /* then if we have cache waiters, return all objects with queued
2154 * writes. This is especially important when many small files
2155 * have filled up the cache and not been fired into rpcs because
2156 * they don't pass the nr_pending/object threshold
2158 if (!list_empty(&cli->cl_cache_waiters) &&
2159 !list_empty(&cli->cl_loi_write_list))
2160 return list_to_obj(&cli->cl_loi_write_list, write_item);
2162 /* then return all queued objects when we have an invalid import
2163 * so that they get flushed
2165 if (!cli->cl_import || cli->cl_import->imp_invalid) {
2166 if (!list_empty(&cli->cl_loi_write_list))
2167 return list_to_obj(&cli->cl_loi_write_list, write_item);
2168 if (!list_empty(&cli->cl_loi_read_list))
2169 return list_to_obj(&cli->cl_loi_read_list, read_item);
2174 /* called with the loi list lock held */
2175 static void osc_check_rpcs(const struct lu_env *env, struct client_obd *cli)
2176 __must_hold(&cli->cl_loi_list_lock)
2178 struct osc_object *osc;
2181 while ((osc = osc_next_obj(cli)) != NULL) {
2182 struct cl_object *obj = osc2cl(osc);
2183 struct lu_ref_link link;
2185 OSC_IO_DEBUG(osc, "%lu in flight\n", rpcs_in_flight(cli));
2187 if (osc_max_rpc_in_flight(cli, osc)) {
2188 __osc_list_maint(cli, osc);
2193 spin_unlock(&cli->cl_loi_list_lock);
2194 lu_object_ref_add_at(&obj->co_lu, &link, "check", current);
2196 /* attempt some read/write balancing by alternating between
2197 * reads and writes in an object. The makes_rpc checks here
2198 * would be redundant if we were getting read/write work items
2199 * instead of objects. we don't want send_oap_rpc to drain a
2200 * partial read pending queue when we're given this object to
2201 * do io on writes while there are cache waiters
2203 osc_object_lock(osc);
2204 if (osc_makes_rpc(cli, osc, OBD_BRW_WRITE)) {
2205 rc = osc_send_write_rpc(env, cli, osc);
2207 CERROR("Write request failed with %d\n", rc);
2209 /* osc_send_write_rpc failed, mostly because of
2212 * It can't break here, because if:
2213 * - a page was submitted by osc_io_submit, so
2215 * - no request in flight
2216 * - no subsequent request
2217 * The system will be in live-lock state,
2218 * because there is no chance to call
2219 * osc_io_unplug() and osc_check_rpcs() any
2220 * more. pdflush can't help in this case,
2221 * because it might be blocked at grabbing
2222 * the page lock as we mentioned.
2224 * Anyway, continue to drain pages.
2229 if (osc_makes_rpc(cli, osc, OBD_BRW_READ)) {
2230 rc = osc_send_read_rpc(env, cli, osc);
2232 CERROR("Read request failed with %d\n", rc);
2234 osc_object_unlock(osc);
2236 osc_list_maint(cli, osc);
2237 lu_object_ref_del_at(&obj->co_lu, &link, "check", current);
2238 cl_object_put(env, obj);
2240 spin_lock(&cli->cl_loi_list_lock);
2244 static int osc_io_unplug0(const struct lu_env *env, struct client_obd *cli,
2245 struct osc_object *osc, int async)
2249 if (osc && osc_list_maint(cli, osc) == 0)
2253 /* disable osc_lru_shrink() temporarily to avoid
2254 * potential stack overrun problem. LU-2859
2256 atomic_inc(&cli->cl_lru_shrinkers);
2257 spin_lock(&cli->cl_loi_list_lock);
2258 osc_check_rpcs(env, cli);
2259 spin_unlock(&cli->cl_loi_list_lock);
2260 atomic_dec(&cli->cl_lru_shrinkers);
2262 CDEBUG(D_CACHE, "Queue writeback work for client %p.\n", cli);
2263 LASSERT(cli->cl_writeback_work);
2264 rc = ptlrpcd_queue_work(cli->cl_writeback_work);
2269 static int osc_io_unplug_async(const struct lu_env *env,
2270 struct client_obd *cli, struct osc_object *osc)
2272 return osc_io_unplug0(env, cli, osc, 1);
2275 void osc_io_unplug(const struct lu_env *env, struct client_obd *cli,
2276 struct osc_object *osc)
2278 (void)osc_io_unplug0(env, cli, osc, 0);
2281 int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops,
2282 struct page *page, loff_t offset)
2284 struct obd_export *exp = osc_export(osc);
2285 struct osc_async_page *oap = &ops->ops_oap;
2288 return cfs_size_round(sizeof(*oap));
2290 oap->oap_magic = OAP_MAGIC;
2291 oap->oap_cli = &exp->exp_obd->u.cli;
2294 oap->oap_page = page;
2295 oap->oap_obj_off = offset;
2296 LASSERT(!(offset & ~PAGE_MASK));
2298 if (capable(CFS_CAP_SYS_RESOURCE))
2299 oap->oap_brw_flags = OBD_BRW_NOQUOTA;
2301 INIT_LIST_HEAD(&oap->oap_pending_item);
2302 INIT_LIST_HEAD(&oap->oap_rpc_item);
2304 spin_lock_init(&oap->oap_lock);
2305 CDEBUG(D_INFO, "oap %p page %p obj off %llu\n",
2306 oap, page, oap->oap_obj_off);
2310 int osc_queue_async_io(const struct lu_env *env, struct cl_io *io,
2311 struct osc_page *ops)
2313 struct osc_io *oio = osc_env_io(env);
2314 struct osc_extent *ext = NULL;
2315 struct osc_async_page *oap = &ops->ops_oap;
2316 struct client_obd *cli = oap->oap_cli;
2317 struct osc_object *osc = oap->oap_obj;
2319 unsigned int grants = 0, tmp;
2320 int brw_flags = OBD_BRW_ASYNC;
2321 int cmd = OBD_BRW_WRITE;
2322 int need_release = 0;
2325 if (oap->oap_magic != OAP_MAGIC)
2328 if (!cli->cl_import || cli->cl_import->imp_invalid)
2331 if (!list_empty(&oap->oap_pending_item) ||
2332 !list_empty(&oap->oap_rpc_item))
2335 /* Set the OBD_BRW_SRVLOCK before the page is queued. */
2336 brw_flags |= ops->ops_srvlock ? OBD_BRW_SRVLOCK : 0;
2337 if (capable(CFS_CAP_SYS_RESOURCE)) {
2338 brw_flags |= OBD_BRW_NOQUOTA;
2339 cmd |= OBD_BRW_NOQUOTA;
2342 /* check if the file's owner/group is over quota */
2343 if (!(cmd & OBD_BRW_NOQUOTA)) {
2344 struct cl_object *obj;
2345 struct cl_attr *attr;
2346 unsigned int qid[MAXQUOTAS];
2348 obj = cl_object_top(&osc->oo_cl);
2349 attr = &osc_env_info(env)->oti_attr;
2351 cl_object_attr_lock(obj);
2352 rc = cl_object_attr_get(env, obj, attr);
2353 cl_object_attr_unlock(obj);
2355 qid[USRQUOTA] = attr->cat_uid;
2356 qid[GRPQUOTA] = attr->cat_gid;
2357 if (rc == 0 && osc_quota_chkdq(cli, qid) == NO_QUOTA)
2364 oap->oap_page_off = ops->ops_from;
2365 oap->oap_count = ops->ops_to - ops->ops_from;
2367 * No need to hold a lock here,
2368 * since this page is not in any list yet.
2370 oap->oap_async_flags = 0;
2371 oap->oap_brw_flags = brw_flags;
2373 OSC_IO_DEBUG(osc, "oap %p page %p added for cmd %d\n",
2374 oap, oap->oap_page, oap->oap_cmd & OBD_BRW_RWMASK);
2376 index = osc_index(oap2osc(oap));
2378 /* Add this page into extent by the following steps:
2379 * 1. if there exists an active extent for this IO, mostly this page
2380 * can be added to the active extent and sometimes we need to
2381 * expand extent to accommodate this page;
2382 * 2. otherwise, a new extent will be allocated.
2385 ext = oio->oi_active;
2386 if (ext && ext->oe_start <= index && ext->oe_max_end >= index) {
2387 /* one chunk plus extent overhead must be enough to write this
2390 grants = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2391 if (ext->oe_end >= index)
2394 /* it doesn't need any grant to dirty this page */
2395 spin_lock(&cli->cl_loi_list_lock);
2396 rc = osc_enter_cache_try(cli, oap, grants, 0);
2397 spin_unlock(&cli->cl_loi_list_lock);
2398 if (rc == 0) { /* try failed */
2401 } else if (ext->oe_end < index) {
2403 /* try to expand this extent */
2404 rc = osc_extent_expand(ext, index, &tmp);
2407 /* don't free reserved grant */
2409 OSC_EXTENT_DUMP(D_CACHE, ext,
2410 "expanded for %lu.\n", index);
2411 osc_unreserve_grant(cli, grants, tmp);
2417 /* index is located outside of active extent */
2421 osc_extent_release(env, ext);
2422 oio->oi_active = NULL;
2427 tmp = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2429 /* try to find new extent to cover this page */
2430 LASSERT(!oio->oi_active);
2431 /* we may have allocated grant for this page if we failed
2432 * to expand the previous active extent.
2434 LASSERT(ergo(grants > 0, grants >= tmp));
2438 /* we haven't allocated grant for this page. */
2439 rc = osc_enter_cache(env, cli, oap, tmp);
2446 ext = osc_extent_find(env, osc, index, &tmp);
2448 LASSERT(tmp == grants);
2449 osc_exit_cache(cli, oap);
2453 oio->oi_active = ext;
2457 osc_unreserve_grant(cli, grants, tmp);
2460 LASSERT(ergo(rc == 0, ext));
2462 EASSERTF(ext->oe_end >= index && ext->oe_start <= index,
2463 ext, "index = %lu.\n", index);
2464 LASSERT((oap->oap_brw_flags & OBD_BRW_FROM_GRANT) != 0);
2466 osc_object_lock(osc);
2467 if (ext->oe_nr_pages == 0)
2468 ext->oe_srvlock = ops->ops_srvlock;
2470 LASSERT(ext->oe_srvlock == ops->ops_srvlock);
2472 list_add_tail(&oap->oap_pending_item, &ext->oe_pages);
2473 osc_object_unlock(osc);
2478 int osc_teardown_async_page(const struct lu_env *env,
2479 struct osc_object *obj, struct osc_page *ops)
2481 struct osc_async_page *oap = &ops->ops_oap;
2482 struct osc_extent *ext = NULL;
2485 LASSERT(oap->oap_magic == OAP_MAGIC);
2487 CDEBUG(D_INFO, "teardown oap %p page %p at index %lu.\n",
2488 oap, ops, osc_index(oap2osc(oap)));
2490 osc_object_lock(obj);
2491 if (!list_empty(&oap->oap_rpc_item)) {
2492 CDEBUG(D_CACHE, "oap %p is not in cache.\n", oap);
2494 } else if (!list_empty(&oap->oap_pending_item)) {
2495 ext = osc_extent_lookup(obj, osc_index(oap2osc(oap)));
2496 /* only truncated pages are allowed to be taken out.
2497 * See osc_extent_truncate() and osc_cache_truncate_start()
2500 if (ext && ext->oe_state != OES_TRUNC) {
2501 OSC_EXTENT_DUMP(D_ERROR, ext, "trunc at %lu.\n",
2502 osc_index(oap2osc(oap)));
2506 osc_object_unlock(obj);
2508 osc_extent_put(env, ext);
2513 * This is called when a page is picked up by kernel to write out.
2515 * We should find out the corresponding extent and add the whole extent
2516 * into urgent list. The extent may be being truncated or used, handle it
2519 int osc_flush_async_page(const struct lu_env *env, struct cl_io *io,
2520 struct osc_page *ops)
2522 struct osc_extent *ext = NULL;
2523 struct osc_object *obj = cl2osc(ops->ops_cl.cpl_obj);
2524 struct cl_page *cp = ops->ops_cl.cpl_page;
2525 pgoff_t index = osc_index(ops);
2526 struct osc_async_page *oap = &ops->ops_oap;
2527 bool unplug = false;
2530 osc_object_lock(obj);
2531 ext = osc_extent_lookup(obj, index);
2533 osc_extent_tree_dump(D_ERROR, obj);
2534 LASSERTF(0, "page index %lu is NOT covered.\n", index);
2537 switch (ext->oe_state) {
2540 CL_PAGE_DEBUG(D_ERROR, env, cp, "flush an in-rpc page?\n");
2544 /* If we know this extent is being written out, we should abort
2545 * so that the writer can make this page ready. Otherwise, there
2546 * exists a deadlock problem because other process can wait for
2547 * page writeback bit holding page lock; and meanwhile in
2548 * vvp_page_make_ready(), we need to grab page lock before
2549 * really sending the RPC.
2552 /* race with truncate, page will be redirtied */
2554 /* The extent is active so we need to abort and let the caller
2555 * re-dirty the page. If we continued on here, and we were the
2556 * one making the extent active, we could deadlock waiting for
2557 * the page writeback to clear but it won't because the extent
2558 * is active and won't be written out.
2566 rc = cl_page_prep(env, io, cp, CRT_WRITE);
2570 spin_lock(&oap->oap_lock);
2571 oap->oap_async_flags |= ASYNC_READY | ASYNC_URGENT;
2572 spin_unlock(&oap->oap_lock);
2574 if (memory_pressure_get())
2575 ext->oe_memalloc = 1;
2578 if (ext->oe_state == OES_CACHE) {
2579 OSC_EXTENT_DUMP(D_CACHE, ext,
2580 "flush page %p make it urgent.\n", oap);
2581 if (list_empty(&ext->oe_link))
2582 list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2588 osc_object_unlock(obj);
2589 osc_extent_put(env, ext);
2591 osc_io_unplug_async(env, osc_cli(obj), obj);
2596 * this is called when a sync waiter receives an interruption. Its job is to
2597 * get the caller woken as soon as possible. If its page hasn't been put in an
2598 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
2599 * desiring interruption which will forcefully complete the rpc once the rpc
2602 int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops)
2604 struct osc_async_page *oap = &ops->ops_oap;
2605 struct osc_object *obj = oap->oap_obj;
2606 struct client_obd *cli = osc_cli(obj);
2607 struct osc_extent *ext;
2608 struct osc_extent *found = NULL;
2609 struct list_head *plist;
2610 pgoff_t index = osc_index(ops);
2614 LASSERT(!oap->oap_interrupted);
2615 oap->oap_interrupted = 1;
2617 /* Find out the caching extent */
2618 osc_object_lock(obj);
2619 if (oap->oap_cmd & OBD_BRW_WRITE) {
2620 plist = &obj->oo_urgent_exts;
2621 cmd = OBD_BRW_WRITE;
2623 plist = &obj->oo_reading_exts;
2626 list_for_each_entry(ext, plist, oe_link) {
2627 if (ext->oe_start <= index && ext->oe_end >= index) {
2628 LASSERT(ext->oe_state == OES_LOCK_DONE);
2629 /* For OES_LOCK_DONE state extent, it has already held
2630 * a refcount for RPC.
2632 found = osc_extent_get(ext);
2637 list_del_init(&found->oe_link);
2638 osc_update_pending(obj, cmd, -found->oe_nr_pages);
2639 osc_object_unlock(obj);
2641 osc_extent_finish(env, found, 0, -EINTR);
2642 osc_extent_put(env, found);
2645 osc_object_unlock(obj);
2646 /* ok, it's been put in an rpc. only one oap gets a request
2649 if (oap->oap_request) {
2650 ptlrpc_mark_interrupted(oap->oap_request);
2651 ptlrpcd_wake(oap->oap_request);
2652 ptlrpc_req_finished(oap->oap_request);
2653 oap->oap_request = NULL;
2657 osc_list_maint(cli, obj);
2661 int osc_queue_sync_pages(const struct lu_env *env, struct osc_object *obj,
2662 struct list_head *list, int cmd, int brw_flags)
2664 struct client_obd *cli = osc_cli(obj);
2665 struct osc_extent *ext;
2666 struct osc_async_page *oap, *tmp;
2668 int mppr = cli->cl_max_pages_per_rpc;
2669 pgoff_t start = CL_PAGE_EOF;
2672 list_for_each_entry(oap, list, oap_pending_item) {
2673 pgoff_t index = osc_index(oap2osc(oap));
2680 mppr <<= (page_count > mppr);
2683 ext = osc_extent_alloc(obj);
2685 list_for_each_entry_safe(oap, tmp, list, oap_pending_item) {
2686 list_del_init(&oap->oap_pending_item);
2687 osc_ap_completion(env, cli, oap, 0, -ENOMEM);
2692 ext->oe_rw = !!(cmd & OBD_BRW_READ);
2695 ext->oe_start = start;
2697 ext->oe_max_end = end;
2699 ext->oe_srvlock = !!(brw_flags & OBD_BRW_SRVLOCK);
2700 ext->oe_nr_pages = page_count;
2701 ext->oe_mppr = mppr;
2702 list_splice_init(list, &ext->oe_pages);
2704 osc_object_lock(obj);
2705 /* Reuse the initial refcount for RPC, don't drop it */
2706 osc_extent_state_set(ext, OES_LOCK_DONE);
2707 if (cmd & OBD_BRW_WRITE) {
2708 list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2709 osc_update_pending(obj, OBD_BRW_WRITE, page_count);
2711 list_add_tail(&ext->oe_link, &obj->oo_reading_exts);
2712 osc_update_pending(obj, OBD_BRW_READ, page_count);
2714 osc_object_unlock(obj);
2716 osc_io_unplug_async(env, cli, obj);
2721 * Called by osc_io_setattr_start() to freeze and destroy covering extents.
2723 int osc_cache_truncate_start(const struct lu_env *env, struct osc_io *oio,
2724 struct osc_object *obj, __u64 size)
2726 struct client_obd *cli = osc_cli(obj);
2727 struct osc_extent *ext;
2728 struct osc_extent *temp;
2729 struct osc_extent *waiting = NULL;
2735 /* pages with index greater or equal to index will be truncated. */
2736 index = cl_index(osc2cl(obj), size);
2737 partial = size > cl_offset(osc2cl(obj), index);
2740 osc_object_lock(obj);
2741 ext = osc_extent_search(obj, index);
2743 ext = first_extent(obj);
2744 else if (ext->oe_end < index)
2745 ext = next_extent(ext);
2747 EASSERT(ext->oe_state != OES_TRUNC, ext);
2749 if (ext->oe_state > OES_CACHE || ext->oe_urgent) {
2750 /* if ext is in urgent state, it means there must exist
2751 * a page already having been flushed by write_page().
2752 * We have to wait for this extent because we can't
2753 * truncate that page.
2755 LASSERT(!ext->oe_hp);
2756 OSC_EXTENT_DUMP(D_CACHE, ext,
2757 "waiting for busy extent\n");
2758 waiting = osc_extent_get(ext);
2762 OSC_EXTENT_DUMP(D_CACHE, ext, "try to trunc:%llu.\n", size);
2764 osc_extent_get(ext);
2765 if (ext->oe_state == OES_ACTIVE) {
2766 /* though we grab inode mutex for write path, but we
2767 * release it before releasing extent(in osc_io_end()),
2768 * so there is a race window that an extent is still
2769 * in OES_ACTIVE when truncate starts.
2771 LASSERT(!ext->oe_trunc_pending);
2772 ext->oe_trunc_pending = 1;
2774 EASSERT(ext->oe_state == OES_CACHE, ext);
2775 osc_extent_state_set(ext, OES_TRUNC);
2776 osc_update_pending(obj, OBD_BRW_WRITE,
2779 EASSERT(list_empty(&ext->oe_link), ext);
2780 list_add_tail(&ext->oe_link, &list);
2782 ext = next_extent(ext);
2784 osc_object_unlock(obj);
2786 osc_list_maint(cli, obj);
2788 list_for_each_entry_safe(ext, temp, &list, oe_link) {
2791 list_del_init(&ext->oe_link);
2793 /* extent may be in OES_ACTIVE state because inode mutex
2794 * is released before osc_io_end() in file write case
2796 if (ext->oe_state != OES_TRUNC)
2797 osc_extent_wait(env, ext, OES_TRUNC);
2799 rc = osc_extent_truncate(ext, index, partial);
2804 OSC_EXTENT_DUMP(D_ERROR, ext,
2805 "truncate error %d\n", rc);
2806 } else if (ext->oe_nr_pages == 0) {
2807 osc_extent_remove(ext);
2809 /* this must be an overlapped extent which means only
2810 * part of pages in this extent have been truncated.
2812 EASSERTF(ext->oe_start <= index, ext,
2813 "trunc index = %lu/%d.\n", index, partial);
2814 /* fix index to skip this partially truncated extent */
2815 index = ext->oe_end + 1;
2818 /* we need to hold this extent in OES_TRUNC state so
2819 * that no writeback will happen. This is to avoid
2822 LASSERT(!oio->oi_trunc);
2823 oio->oi_trunc = osc_extent_get(ext);
2824 OSC_EXTENT_DUMP(D_CACHE, ext,
2825 "trunc at %llu\n", size);
2827 osc_extent_put(env, ext);
2832 /* ignore the result of osc_extent_wait the write initiator
2833 * should take care of it.
2835 rc = osc_extent_wait(env, waiting, OES_INV);
2837 OSC_EXTENT_DUMP(D_CACHE, waiting, "error: %d.\n", rc);
2839 osc_extent_put(env, waiting);
2847 * Called after osc_io_setattr_end to add oio->oi_trunc back to cache.
2849 void osc_cache_truncate_end(const struct lu_env *env, struct osc_io *oio,
2850 struct osc_object *obj)
2852 struct osc_extent *ext = oio->oi_trunc;
2854 oio->oi_trunc = NULL;
2856 bool unplug = false;
2858 EASSERT(ext->oe_nr_pages > 0, ext);
2859 EASSERT(ext->oe_state == OES_TRUNC, ext);
2860 EASSERT(!ext->oe_urgent, ext);
2862 OSC_EXTENT_DUMP(D_CACHE, ext, "trunc -> cache.\n");
2863 osc_object_lock(obj);
2864 osc_extent_state_set(ext, OES_CACHE);
2865 if (ext->oe_fsync_wait && !ext->oe_urgent) {
2867 list_move_tail(&ext->oe_link, &obj->oo_urgent_exts);
2870 osc_update_pending(obj, OBD_BRW_WRITE, ext->oe_nr_pages);
2871 osc_object_unlock(obj);
2872 osc_extent_put(env, ext);
2875 osc_io_unplug_async(env, osc_cli(obj), obj);
2880 * Wait for extents in a specific range to be written out.
2881 * The caller must have called osc_cache_writeback_range() to issue IO
2882 * otherwise it will take a long time for this function to finish.
2884 * Caller must hold inode_mutex , or cancel exclusive dlm lock so that
2885 * nobody else can dirty this range of file while we're waiting for
2886 * extents to be written.
2888 int osc_cache_wait_range(const struct lu_env *env, struct osc_object *obj,
2889 pgoff_t start, pgoff_t end)
2891 struct osc_extent *ext;
2892 pgoff_t index = start;
2896 osc_object_lock(obj);
2897 ext = osc_extent_search(obj, index);
2899 ext = first_extent(obj);
2900 else if (ext->oe_end < index)
2901 ext = next_extent(ext);
2905 if (ext->oe_start > end)
2908 if (!ext->oe_fsync_wait) {
2909 ext = next_extent(ext);
2913 EASSERT(ergo(ext->oe_state == OES_CACHE,
2914 ext->oe_hp || ext->oe_urgent), ext);
2915 EASSERT(ergo(ext->oe_state == OES_ACTIVE,
2916 !ext->oe_hp && ext->oe_urgent), ext);
2918 index = ext->oe_end + 1;
2919 osc_extent_get(ext);
2920 osc_object_unlock(obj);
2922 rc = osc_extent_wait(env, ext, OES_INV);
2925 osc_extent_put(env, ext);
2928 osc_object_unlock(obj);
2930 OSC_IO_DEBUG(obj, "sync file range.\n");
2935 * Called to write out a range of osc object.
2937 * @hp : should be set this is caused by lock cancel;
2938 * @discard: is set if dirty pages should be dropped - file will be deleted or
2939 * truncated, this implies there is no partially discarding extents.
2941 * Return how many pages will be issued, or error code if error occurred.
2943 int osc_cache_writeback_range(const struct lu_env *env, struct osc_object *obj,
2944 pgoff_t start, pgoff_t end, int hp, int discard)
2946 struct osc_extent *ext;
2947 LIST_HEAD(discard_list);
2948 bool unplug = false;
2951 osc_object_lock(obj);
2952 ext = osc_extent_search(obj, start);
2954 ext = first_extent(obj);
2955 else if (ext->oe_end < start)
2956 ext = next_extent(ext);
2958 if (ext->oe_start > end)
2961 ext->oe_fsync_wait = 1;
2962 switch (ext->oe_state) {
2964 result += ext->oe_nr_pages;
2966 struct list_head *list = NULL;
2969 EASSERT(!ext->oe_hp, ext);
2971 list = &obj->oo_hp_exts;
2972 } else if (!ext->oe_urgent) {
2974 list = &obj->oo_urgent_exts;
2977 list_move_tail(&ext->oe_link, list);
2980 /* the only discarder is lock cancelling, so
2981 * [start, end] must contain this extent
2983 EASSERT(ext->oe_start >= start &&
2984 ext->oe_max_end <= end, ext);
2985 osc_extent_state_set(ext, OES_LOCKING);
2986 ext->oe_owner = current;
2987 list_move_tail(&ext->oe_link, &discard_list);
2988 osc_update_pending(obj, OBD_BRW_WRITE,
2993 /* It's pretty bad to wait for ACTIVE extents, because
2994 * we don't know how long we will wait for it to be
2995 * flushed since it may be blocked at awaiting more
2996 * grants. We do this for the correctness of fsync.
2998 LASSERT(hp == 0 && discard == 0);
3002 /* this extent is being truncated, can't do anything
3003 * for it now. it will be set to urgent after truncate
3004 * is finished in osc_cache_truncate_end().
3009 ext = next_extent(ext);
3011 osc_object_unlock(obj);
3013 LASSERT(ergo(!discard, list_empty(&discard_list)));
3014 if (!list_empty(&discard_list)) {
3015 struct osc_extent *tmp;
3018 osc_list_maint(osc_cli(obj), obj);
3019 list_for_each_entry_safe(ext, tmp, &discard_list, oe_link) {
3020 list_del_init(&ext->oe_link);
3021 EASSERT(ext->oe_state == OES_LOCKING, ext);
3023 /* Discard caching pages. We don't actually write this
3024 * extent out but we complete it as if we did.
3026 rc = osc_extent_make_ready(env, ext);
3027 if (unlikely(rc < 0)) {
3028 OSC_EXTENT_DUMP(D_ERROR, ext,
3029 "make_ready returned %d\n", rc);
3034 /* finish the extent as if the pages were sent */
3035 osc_extent_finish(env, ext, 0, 0);
3040 osc_io_unplug(env, osc_cli(obj), obj);
3042 if (hp || discard) {
3045 rc = osc_cache_wait_range(env, obj, start, end);
3046 if (result >= 0 && rc < 0)
3050 OSC_IO_DEBUG(obj, "pageout [%lu, %lu], %d.\n", start, end, result);
3055 * Returns a list of pages by a given [start, end] of \a obj.
3057 * \param resched If not NULL, then we give up before hogging CPU for too
3058 * long and set *resched = 1, in that case caller should implement a retry
3061 * Gang tree lookup (radix_tree_gang_lookup()) optimization is absolutely
3062 * crucial in the face of [offset, EOF] locks.
3064 * Return at least one page in @queue unless there is no covered page.
3066 int osc_page_gang_lookup(const struct lu_env *env, struct cl_io *io,
3067 struct osc_object *osc, pgoff_t start, pgoff_t end,
3068 osc_page_gang_cbt cb, void *cbdata)
3070 struct osc_page *ops;
3076 int res = CLP_GANG_OKAY;
3077 bool tree_lock = true;
3080 pvec = osc_env_info(env)->oti_pvec;
3081 spin_lock(&osc->oo_tree_lock);
3082 while ((nr = radix_tree_gang_lookup(&osc->oo_tree, pvec,
3083 idx, OTI_PVEC_SIZE)) > 0) {
3084 struct cl_page *page;
3085 bool end_of_region = false;
3087 for (i = 0, j = 0; i < nr; ++i) {
3091 idx = osc_index(ops);
3093 end_of_region = true;
3097 page = ops->ops_cl.cpl_page;
3098 LASSERT(page->cp_type == CPT_CACHEABLE);
3099 if (page->cp_state == CPS_FREEING)
3103 lu_ref_add_atomic(&page->cp_reference,
3104 "gang_lookup", current);
3110 * Here a delicate locking dance is performed. Current thread
3111 * holds a reference to a page, but has to own it before it
3112 * can be placed into queue. Owning implies waiting, so
3113 * radix-tree lock is to be released. After a wait one has to
3114 * check that pages weren't truncated (cl_page_own() returns
3115 * error in the latter case).
3117 spin_unlock(&osc->oo_tree_lock);
3120 for (i = 0; i < j; ++i) {
3122 if (res == CLP_GANG_OKAY)
3123 res = (*cb)(env, io, ops, cbdata);
3125 page = ops->ops_cl.cpl_page;
3126 lu_ref_del(&page->cp_reference, "gang_lookup", current);
3127 cl_page_put(env, page);
3129 if (nr < OTI_PVEC_SIZE || end_of_region)
3132 if (res == CLP_GANG_OKAY && need_resched())
3133 res = CLP_GANG_RESCHED;
3134 if (res != CLP_GANG_OKAY)
3137 spin_lock(&osc->oo_tree_lock);
3141 spin_unlock(&osc->oo_tree_lock);
3146 * Check if page @page is covered by an extra lock or discard it.
3148 static int check_and_discard_cb(const struct lu_env *env, struct cl_io *io,
3149 struct osc_page *ops, void *cbdata)
3151 struct osc_thread_info *info = osc_env_info(env);
3152 struct osc_object *osc = cbdata;
3155 index = osc_index(ops);
3156 if (index >= info->oti_fn_index) {
3157 struct ldlm_lock *tmp;
3158 struct cl_page *page = ops->ops_cl.cpl_page;
3160 /* refresh non-overlapped index */
3161 tmp = osc_dlmlock_at_pgoff(env, osc, index, 0, 0);
3163 __u64 end = tmp->l_policy_data.l_extent.end;
3164 /* Cache the first-non-overlapped index so as to skip
3165 * all pages within [index, oti_fn_index). This is safe
3166 * because if tmp lock is canceled, it will discard
3169 info->oti_fn_index = cl_index(osc2cl(osc), end + 1);
3170 if (end == OBD_OBJECT_EOF)
3171 info->oti_fn_index = CL_PAGE_EOF;
3173 } else if (cl_page_own(env, io, page) == 0) {
3174 /* discard the page */
3175 cl_page_discard(env, io, page);
3176 cl_page_disown(env, io, page);
3178 LASSERT(page->cp_state == CPS_FREEING);
3182 info->oti_next_index = index + 1;
3183 return CLP_GANG_OKAY;
3186 static int discard_cb(const struct lu_env *env, struct cl_io *io,
3187 struct osc_page *ops, void *cbdata)
3189 struct osc_thread_info *info = osc_env_info(env);
3190 struct cl_page *page = ops->ops_cl.cpl_page;
3192 /* page is top page. */
3193 info->oti_next_index = osc_index(ops) + 1;
3194 if (cl_page_own(env, io, page) == 0) {
3195 KLASSERT(ergo(page->cp_type == CPT_CACHEABLE,
3196 !PageDirty(cl_page_vmpage(page))));
3198 /* discard the page */
3199 cl_page_discard(env, io, page);
3200 cl_page_disown(env, io, page);
3202 LASSERT(page->cp_state == CPS_FREEING);
3205 return CLP_GANG_OKAY;
3209 * Discard pages protected by the given lock. This function traverses radix
3210 * tree to find all covering pages and discard them. If a page is being covered
3211 * by other locks, it should remain in cache.
3213 * If error happens on any step, the process continues anyway (the reasoning
3214 * behind this being that lock cancellation cannot be delayed indefinitely).
3216 int osc_lock_discard_pages(const struct lu_env *env, struct osc_object *osc,
3217 pgoff_t start, pgoff_t end, enum cl_lock_mode mode)
3219 struct osc_thread_info *info = osc_env_info(env);
3220 struct cl_io *io = &info->oti_io;
3221 osc_page_gang_cbt cb;
3225 io->ci_obj = cl_object_top(osc2cl(osc));
3226 io->ci_ignore_layout = 1;
3227 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3231 cb = mode == CLM_READ ? check_and_discard_cb : discard_cb;
3232 info->oti_fn_index = start;
3233 info->oti_next_index = start;
3235 res = osc_page_gang_lookup(env, io, osc,
3236 info->oti_next_index, end, cb, osc);
3237 if (info->oti_next_index > end)
3240 if (res == CLP_GANG_RESCHED)
3242 } while (res != CLP_GANG_OKAY);
3244 cl_io_fini(env, io);