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
102 static const char *oes_strings[] = {
103 "inv", "active", "cache", "locking", "lockdone", "rpc", "trunc", NULL };
105 #define OSC_EXTENT_DUMP(lvl, extent, fmt, ...) do { \
106 struct osc_extent *__ext = (extent); \
110 "extent %p@{" EXTSTR ", " \
111 "[%d|%d|%c|%s|%s|%p], [%d|%d|%c|%c|%p|%u|%p]} " fmt, \
112 /* ----- extent part 0 ----- */ \
113 __ext, EXTPARA(__ext), \
114 /* ----- part 1 ----- */ \
115 cfs_atomic_read(&__ext->oe_refc), \
116 cfs_atomic_read(&__ext->oe_users), \
117 list_empty_marker(&__ext->oe_link), \
118 oes_strings[__ext->oe_state], ext_flags(__ext, __buf), \
120 /* ----- part 2 ----- */ \
121 __ext->oe_grants, __ext->oe_nr_pages, \
122 list_empty_marker(&__ext->oe_pages), \
123 waitqueue_active(&__ext->oe_waitq) ? '+' : '-', \
124 __ext->oe_osclock, __ext->oe_mppr, __ext->oe_owner, \
125 /* ----- part 4 ----- */ \
130 #define EASSERTF(expr, ext, fmt, args...) do { \
132 OSC_EXTENT_DUMP(D_ERROR, (ext), fmt, ##args); \
133 osc_extent_tree_dump(D_ERROR, (ext)->oe_obj); \
139 #define EASSERT(expr, ext) EASSERTF(expr, ext, "\n")
141 static inline struct osc_extent *rb_extent(struct rb_node *n)
146 return container_of(n, struct osc_extent, oe_node);
149 static inline struct osc_extent *next_extent(struct osc_extent *ext)
154 LASSERT(ext->oe_intree);
155 return rb_extent(rb_next(&ext->oe_node));
158 static inline struct osc_extent *prev_extent(struct osc_extent *ext)
163 LASSERT(ext->oe_intree);
164 return rb_extent(rb_prev(&ext->oe_node));
167 static inline struct osc_extent *first_extent(struct osc_object *obj)
169 return rb_extent(rb_first(&obj->oo_root));
172 /* object must be locked by caller. */
173 static int osc_extent_sanity_check0(struct osc_extent *ext,
174 const char *func, const int line)
176 struct osc_object *obj = ext->oe_obj;
177 struct osc_async_page *oap;
181 if (!osc_object_is_locked(obj))
184 if (ext->oe_state >= OES_STATE_MAX)
187 if (cfs_atomic_read(&ext->oe_refc) <= 0)
190 if (cfs_atomic_read(&ext->oe_refc) < cfs_atomic_read(&ext->oe_users))
193 switch (ext->oe_state) {
195 if (ext->oe_nr_pages > 0 || !cfs_list_empty(&ext->oe_pages))
200 if (cfs_atomic_read(&ext->oe_users) == 0)
204 if (ext->oe_fsync_wait && !ext->oe_urgent)
208 if (ext->oe_grants == 0)
210 if (ext->oe_fsync_wait && !ext->oe_urgent && !ext->oe_hp)
213 if (cfs_atomic_read(&ext->oe_users) > 0)
217 if (ext->oe_max_end < ext->oe_end || ext->oe_end < ext->oe_start)
220 if (ext->oe_osclock == NULL && ext->oe_grants > 0)
223 if (ext->oe_osclock) {
224 struct cl_lock_descr *descr;
225 descr = &ext->oe_osclock->cll_descr;
226 if (!(descr->cld_start <= ext->oe_start &&
227 descr->cld_end >= ext->oe_max_end))
231 if (ext->oe_nr_pages > ext->oe_mppr)
234 /* Do not verify page list if extent is in RPC. This is because an
235 * in-RPC extent is supposed to be exclusively accessible w/o lock. */
236 if (ext->oe_state > OES_CACHE)
243 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
244 pgoff_t index = oap2cl_page(oap)->cp_index;
246 if (index > ext->oe_end || index < ext->oe_start)
249 if (page_count != ext->oe_nr_pages)
254 OSC_EXTENT_DUMP(D_ERROR, ext,
255 "%s:%d sanity check %p failed with rc = %d\n",
256 func, line, ext, rc);
260 #define sanity_check_nolock(ext) \
261 osc_extent_sanity_check0(ext, __func__, __LINE__)
263 #define sanity_check(ext) ({ \
265 osc_object_lock((ext)->oe_obj); \
266 __res = sanity_check_nolock(ext); \
267 osc_object_unlock((ext)->oe_obj); \
273 * sanity check - to make sure there is no overlapped extent in the tree.
275 static int osc_extent_is_overlapped(struct osc_object *obj,
276 struct osc_extent *ext)
278 struct osc_extent *tmp;
280 LASSERT(osc_object_is_locked(obj));
285 for (tmp = first_extent(obj); tmp != NULL; tmp = next_extent(tmp)) {
288 if (tmp->oe_end >= ext->oe_start &&
289 tmp->oe_start <= ext->oe_end)
295 static void osc_extent_state_set(struct osc_extent *ext, int state)
297 LASSERT(osc_object_is_locked(ext->oe_obj));
298 LASSERT(state >= OES_INV && state < OES_STATE_MAX);
300 /* Never try to sanity check a state changing extent :-) */
301 /* LASSERT(sanity_check_nolock(ext) == 0); */
303 /* TODO: validate the state machine */
304 ext->oe_state = state;
305 wake_up_all(&ext->oe_waitq);
308 static struct osc_extent *osc_extent_alloc(struct osc_object *obj)
310 struct osc_extent *ext;
312 OBD_SLAB_ALLOC_PTR_GFP(ext, osc_extent_kmem, GFP_IOFS);
316 RB_CLEAR_NODE(&ext->oe_node);
318 cfs_atomic_set(&ext->oe_refc, 1);
319 cfs_atomic_set(&ext->oe_users, 0);
320 CFS_INIT_LIST_HEAD(&ext->oe_link);
321 ext->oe_state = OES_INV;
322 CFS_INIT_LIST_HEAD(&ext->oe_pages);
323 init_waitqueue_head(&ext->oe_waitq);
324 ext->oe_osclock = NULL;
329 static void osc_extent_free(struct osc_extent *ext)
331 OBD_SLAB_FREE_PTR(ext, osc_extent_kmem);
334 static struct osc_extent *osc_extent_get(struct osc_extent *ext)
336 LASSERT(cfs_atomic_read(&ext->oe_refc) >= 0);
337 cfs_atomic_inc(&ext->oe_refc);
341 static void osc_extent_put(const struct lu_env *env, struct osc_extent *ext)
343 LASSERT(cfs_atomic_read(&ext->oe_refc) > 0);
344 if (cfs_atomic_dec_and_test(&ext->oe_refc)) {
345 LASSERT(cfs_list_empty(&ext->oe_link));
346 LASSERT(cfs_atomic_read(&ext->oe_users) == 0);
347 LASSERT(ext->oe_state == OES_INV);
348 LASSERT(!ext->oe_intree);
350 if (ext->oe_osclock) {
351 cl_lock_put(env, ext->oe_osclock);
352 ext->oe_osclock = NULL;
354 osc_extent_free(ext);
359 * osc_extent_put_trust() is a special version of osc_extent_put() when
360 * it's known that the caller is not the last user. This is to address the
361 * problem of lacking of lu_env ;-).
363 static void osc_extent_put_trust(struct osc_extent *ext)
365 LASSERT(cfs_atomic_read(&ext->oe_refc) > 1);
366 LASSERT(osc_object_is_locked(ext->oe_obj));
367 cfs_atomic_dec(&ext->oe_refc);
371 * Return the extent which includes pgoff @index, or return the greatest
372 * previous extent in the tree.
374 static struct osc_extent *osc_extent_search(struct osc_object *obj,
377 struct rb_node *n = obj->oo_root.rb_node;
378 struct osc_extent *tmp, *p = NULL;
380 LASSERT(osc_object_is_locked(obj));
383 if (index < tmp->oe_start) {
385 } else if (index > tmp->oe_end) {
396 * Return the extent covering @index, otherwise return NULL.
397 * caller must have held object lock.
399 static struct osc_extent *osc_extent_lookup(struct osc_object *obj,
402 struct osc_extent *ext;
404 ext = osc_extent_search(obj, index);
405 if (ext != NULL && ext->oe_start <= index && index <= ext->oe_end)
406 return osc_extent_get(ext);
410 /* caller must have held object lock. */
411 static void osc_extent_insert(struct osc_object *obj, struct osc_extent *ext)
413 struct rb_node **n = &obj->oo_root.rb_node;
414 struct rb_node *parent = NULL;
415 struct osc_extent *tmp;
417 LASSERT(ext->oe_intree == 0);
418 LASSERT(ext->oe_obj == obj);
419 LASSERT(osc_object_is_locked(obj));
424 if (ext->oe_end < tmp->oe_start)
426 else if (ext->oe_start > tmp->oe_end)
429 EASSERTF(0, tmp, EXTSTR, EXTPARA(ext));
431 rb_link_node(&ext->oe_node, parent, n);
432 rb_insert_color(&ext->oe_node, &obj->oo_root);
437 /* caller must have held object lock. */
438 static void osc_extent_erase(struct osc_extent *ext)
440 struct osc_object *obj = ext->oe_obj;
441 LASSERT(osc_object_is_locked(obj));
442 if (ext->oe_intree) {
443 rb_erase(&ext->oe_node, &obj->oo_root);
445 /* rbtree held a refcount */
446 osc_extent_put_trust(ext);
450 static struct osc_extent *osc_extent_hold(struct osc_extent *ext)
452 struct osc_object *obj = ext->oe_obj;
454 LASSERT(osc_object_is_locked(obj));
455 LASSERT(ext->oe_state == OES_ACTIVE || ext->oe_state == OES_CACHE);
456 if (ext->oe_state == OES_CACHE) {
457 osc_extent_state_set(ext, OES_ACTIVE);
458 osc_update_pending(obj, OBD_BRW_WRITE, -ext->oe_nr_pages);
460 cfs_atomic_inc(&ext->oe_users);
461 cfs_list_del_init(&ext->oe_link);
462 return osc_extent_get(ext);
465 static void __osc_extent_remove(struct osc_extent *ext)
467 LASSERT(osc_object_is_locked(ext->oe_obj));
468 LASSERT(cfs_list_empty(&ext->oe_pages));
469 osc_extent_erase(ext);
470 cfs_list_del_init(&ext->oe_link);
471 osc_extent_state_set(ext, OES_INV);
472 OSC_EXTENT_DUMP(D_CACHE, ext, "destroyed.\n");
475 static void osc_extent_remove(struct osc_extent *ext)
477 struct osc_object *obj = ext->oe_obj;
479 osc_object_lock(obj);
480 __osc_extent_remove(ext);
481 osc_object_unlock(obj);
485 * This function is used to merge extents to get better performance. It checks
486 * if @cur and @victim are contiguous at chunk level.
488 static int osc_extent_merge(const struct lu_env *env, struct osc_extent *cur,
489 struct osc_extent *victim)
491 struct osc_object *obj = cur->oe_obj;
496 LASSERT(cur->oe_state == OES_CACHE);
497 LASSERT(osc_object_is_locked(obj));
501 if (victim->oe_state != OES_CACHE || victim->oe_fsync_wait)
504 if (cur->oe_max_end != victim->oe_max_end)
507 LASSERT(cur->oe_osclock == victim->oe_osclock);
508 ppc_bits = osc_cli(obj)->cl_chunkbits - PAGE_CACHE_SHIFT;
509 chunk_start = cur->oe_start >> ppc_bits;
510 chunk_end = cur->oe_end >> ppc_bits;
511 if (chunk_start != (victim->oe_end >> ppc_bits) + 1 &&
512 chunk_end + 1 != victim->oe_start >> ppc_bits)
515 OSC_EXTENT_DUMP(D_CACHE, victim, "will be merged by %p.\n", cur);
517 cur->oe_start = min(cur->oe_start, victim->oe_start);
518 cur->oe_end = max(cur->oe_end, victim->oe_end);
519 cur->oe_grants += victim->oe_grants;
520 cur->oe_nr_pages += victim->oe_nr_pages;
521 /* only the following bits are needed to merge */
522 cur->oe_urgent |= victim->oe_urgent;
523 cur->oe_memalloc |= victim->oe_memalloc;
524 cfs_list_splice_init(&victim->oe_pages, &cur->oe_pages);
525 cfs_list_del_init(&victim->oe_link);
526 victim->oe_nr_pages = 0;
528 osc_extent_get(victim);
529 __osc_extent_remove(victim);
530 osc_extent_put(env, victim);
532 OSC_EXTENT_DUMP(D_CACHE, cur, "after merging %p.\n", victim);
537 * Drop user count of osc_extent, and unplug IO asynchronously.
539 int osc_extent_release(const struct lu_env *env, struct osc_extent *ext)
541 struct osc_object *obj = ext->oe_obj;
545 LASSERT(cfs_atomic_read(&ext->oe_users) > 0);
546 LASSERT(sanity_check(ext) == 0);
547 LASSERT(ext->oe_grants > 0);
549 if (cfs_atomic_dec_and_lock(&ext->oe_users, &obj->oo_lock)) {
550 LASSERT(ext->oe_state == OES_ACTIVE);
551 if (ext->oe_trunc_pending) {
552 /* a truncate process is waiting for this extent.
553 * This may happen due to a race, check
554 * osc_cache_truncate_start(). */
555 osc_extent_state_set(ext, OES_TRUNC);
556 ext->oe_trunc_pending = 0;
558 osc_extent_state_set(ext, OES_CACHE);
559 osc_update_pending(obj, OBD_BRW_WRITE,
562 /* try to merge the previous and next extent. */
563 osc_extent_merge(env, ext, prev_extent(ext));
564 osc_extent_merge(env, ext, next_extent(ext));
567 cfs_list_move_tail(&ext->oe_link,
568 &obj->oo_urgent_exts);
570 osc_object_unlock(obj);
572 osc_io_unplug_async(env, osc_cli(obj), obj);
574 osc_extent_put(env, ext);
578 static inline int overlapped(struct osc_extent *ex1, struct osc_extent *ex2)
580 return !(ex1->oe_end < ex2->oe_start || ex2->oe_end < ex1->oe_start);
584 * Find or create an extent which includes @index, core function to manage
587 struct osc_extent *osc_extent_find(const struct lu_env *env,
588 struct osc_object *obj, pgoff_t index,
592 struct client_obd *cli = osc_cli(obj);
593 struct cl_lock *lock;
594 struct osc_extent *cur;
595 struct osc_extent *ext;
596 struct osc_extent *conflict = NULL;
597 struct osc_extent *found = NULL;
600 int max_pages; /* max_pages_per_rpc */
602 int ppc_bits; /* pages per chunk bits */
607 cur = osc_extent_alloc(obj);
609 RETURN(ERR_PTR(-ENOMEM));
611 lock = cl_lock_at_pgoff(env, osc2cl(obj), index, NULL, 1, 0);
612 LASSERT(lock != NULL);
613 LASSERT(lock->cll_descr.cld_mode >= CLM_WRITE);
615 LASSERT(cli->cl_chunkbits >= PAGE_CACHE_SHIFT);
616 ppc_bits = cli->cl_chunkbits - PAGE_CACHE_SHIFT;
617 chunk_mask = ~((1 << ppc_bits) - 1);
618 chunksize = 1 << cli->cl_chunkbits;
619 chunk = index >> ppc_bits;
621 /* align end to rpc edge, rpc size may not be a power 2 integer. */
622 max_pages = cli->cl_max_pages_per_rpc;
623 LASSERT((max_pages & ~chunk_mask) == 0);
624 max_end = index - (index % max_pages) + max_pages - 1;
625 max_end = min_t(pgoff_t, max_end, lock->cll_descr.cld_end);
627 /* initialize new extent by parameters so far */
628 cur->oe_max_end = max_end;
629 cur->oe_start = index & chunk_mask;
630 cur->oe_end = ((index + ~chunk_mask + 1) & chunk_mask) - 1;
631 if (cur->oe_start < lock->cll_descr.cld_start)
632 cur->oe_start = lock->cll_descr.cld_start;
633 if (cur->oe_end > max_end)
634 cur->oe_end = max_end;
635 cur->oe_osclock = lock;
637 cur->oe_mppr = max_pages;
639 /* grants has been allocated by caller */
640 LASSERTF(*grants >= chunksize + cli->cl_extent_tax,
641 "%u/%u/%u.\n", *grants, chunksize, cli->cl_extent_tax);
642 LASSERTF((max_end - cur->oe_start) < max_pages, EXTSTR, EXTPARA(cur));
645 osc_object_lock(obj);
646 ext = osc_extent_search(obj, cur->oe_start);
648 ext = first_extent(obj);
649 while (ext != NULL) {
650 loff_t ext_chk_start = ext->oe_start >> ppc_bits;
651 loff_t ext_chk_end = ext->oe_end >> ppc_bits;
653 LASSERT(sanity_check_nolock(ext) == 0);
654 if (chunk > ext_chk_end + 1)
657 /* if covering by different locks, no chance to match */
658 if (lock != ext->oe_osclock) {
659 EASSERTF(!overlapped(ext, cur), ext,
660 EXTSTR, EXTPARA(cur));
662 ext = next_extent(ext);
666 /* discontiguous chunks? */
667 if (chunk + 1 < ext_chk_start) {
668 ext = next_extent(ext);
672 /* ok, from now on, ext and cur have these attrs:
673 * 1. covered by the same lock
674 * 2. contiguous at chunk level or overlapping. */
676 if (overlapped(ext, cur)) {
677 /* cur is the minimum unit, so overlapping means
679 EASSERTF((ext->oe_start <= cur->oe_start &&
680 ext->oe_end >= cur->oe_end),
681 ext, EXTSTR, EXTPARA(cur));
683 if (ext->oe_state > OES_CACHE || ext->oe_fsync_wait) {
684 /* for simplicity, we wait for this extent to
685 * finish before going forward. */
686 conflict = osc_extent_get(ext);
690 found = osc_extent_hold(ext);
694 /* non-overlapped extent */
695 if (ext->oe_state != OES_CACHE || ext->oe_fsync_wait) {
696 /* we can't do anything for a non OES_CACHE extent, or
697 * if there is someone waiting for this extent to be
698 * flushed, try next one. */
699 ext = next_extent(ext);
703 /* check if they belong to the same rpc slot before trying to
704 * merge. the extents are not overlapped and contiguous at
705 * chunk level to get here. */
706 if (ext->oe_max_end != max_end) {
707 /* if they don't belong to the same RPC slot or
708 * max_pages_per_rpc has ever changed, do not merge. */
709 ext = next_extent(ext);
713 /* it's required that an extent must be contiguous at chunk
714 * level so that we know the whole extent is covered by grant
715 * (the pages in the extent are NOT required to be contiguous).
716 * Otherwise, it will be too much difficult to know which
717 * chunks have grants allocated. */
719 /* try to do front merge - extend ext's start */
720 if (chunk + 1 == ext_chk_start) {
721 /* ext must be chunk size aligned */
722 EASSERT((ext->oe_start & ~chunk_mask) == 0, ext);
724 /* pull ext's start back to cover cur */
725 ext->oe_start = cur->oe_start;
726 ext->oe_grants += chunksize;
727 *grants -= chunksize;
729 found = osc_extent_hold(ext);
730 } else if (chunk == ext_chk_end + 1) {
732 ext->oe_end = cur->oe_end;
733 ext->oe_grants += chunksize;
734 *grants -= chunksize;
736 /* try to merge with the next one because we just fill
738 if (osc_extent_merge(env, ext, next_extent(ext)) == 0)
739 /* we can save extent tax from next extent */
740 *grants += cli->cl_extent_tax;
742 found = osc_extent_hold(ext);
747 ext = next_extent(ext);
750 osc_extent_tree_dump(D_CACHE, obj);
752 LASSERT(conflict == NULL);
753 if (!IS_ERR(found)) {
754 LASSERT(found->oe_osclock == cur->oe_osclock);
755 OSC_EXTENT_DUMP(D_CACHE, found,
756 "found caching ext for %lu.\n", index);
758 } else if (conflict == NULL) {
759 /* create a new extent */
760 EASSERT(osc_extent_is_overlapped(obj, cur) == 0, cur);
761 cur->oe_grants = chunksize + cli->cl_extent_tax;
762 *grants -= cur->oe_grants;
763 LASSERT(*grants >= 0);
765 cur->oe_state = OES_CACHE;
766 found = osc_extent_hold(cur);
767 osc_extent_insert(obj, cur);
768 OSC_EXTENT_DUMP(D_CACHE, cur, "add into tree %lu/%lu.\n",
769 index, lock->cll_descr.cld_end);
771 osc_object_unlock(obj);
773 if (conflict != NULL) {
774 LASSERT(found == NULL);
776 /* waiting for IO to finish. Please notice that it's impossible
777 * to be an OES_TRUNC extent. */
778 rc = osc_extent_wait(env, conflict, OES_INV);
779 osc_extent_put(env, conflict);
782 GOTO(out, found = ERR_PTR(rc));
789 osc_extent_put(env, cur);
790 LASSERT(*grants >= 0);
795 * Called when IO is finished to an extent.
797 int osc_extent_finish(const struct lu_env *env, struct osc_extent *ext,
800 struct client_obd *cli = osc_cli(ext->oe_obj);
801 struct osc_async_page *oap;
802 struct osc_async_page *tmp;
803 int nr_pages = ext->oe_nr_pages;
805 int blocksize = cli->cl_import->imp_obd->obd_osfs.os_bsize ? : 4096;
810 OSC_EXTENT_DUMP(D_CACHE, ext, "extent finished.\n");
812 ext->oe_rc = rc ?: ext->oe_nr_pages;
813 EASSERT(ergo(rc == 0, ext->oe_state == OES_RPC), ext);
815 osc_lru_add_batch(cli, &ext->oe_pages);
816 cfs_list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
818 cfs_list_del_init(&oap->oap_rpc_item);
819 cfs_list_del_init(&oap->oap_pending_item);
820 if (last_off <= oap->oap_obj_off) {
821 last_off = oap->oap_obj_off;
822 last_count = oap->oap_count;
826 osc_ap_completion(env, cli, oap, sent, rc);
828 EASSERT(ext->oe_nr_pages == 0, ext);
831 lost_grant = ext->oe_grants;
832 } else if (blocksize < PAGE_CACHE_SIZE &&
833 last_count != PAGE_CACHE_SIZE) {
834 /* For short writes we shouldn't count parts of pages that
835 * span a whole chunk on the OST side, or our accounting goes
836 * wrong. Should match the code in filter_grant_check. */
837 int offset = oap->oap_page_off & ~CFS_PAGE_MASK;
838 int count = oap->oap_count + (offset & (blocksize - 1));
839 int end = (offset + oap->oap_count) & (blocksize - 1);
841 count += blocksize - end;
843 lost_grant = PAGE_CACHE_SIZE - count;
845 if (ext->oe_grants > 0)
846 osc_free_grant(cli, nr_pages, lost_grant);
848 osc_extent_remove(ext);
849 /* put the refcount for RPC */
850 osc_extent_put(env, ext);
854 static int extent_wait_cb(struct osc_extent *ext, int state)
858 osc_object_lock(ext->oe_obj);
859 ret = ext->oe_state == state;
860 osc_object_unlock(ext->oe_obj);
866 * Wait for the extent's state to become @state.
868 static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
871 struct osc_object *obj = ext->oe_obj;
872 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL,
873 LWI_ON_SIGNAL_NOOP, NULL);
877 osc_object_lock(obj);
878 LASSERT(sanity_check_nolock(ext) == 0);
879 /* `Kick' this extent only if the caller is waiting for it to be
881 if (state == OES_INV && !ext->oe_urgent && !ext->oe_hp) {
882 if (ext->oe_state == OES_ACTIVE) {
884 } else if (ext->oe_state == OES_CACHE) {
886 osc_extent_hold(ext);
890 osc_object_unlock(obj);
892 osc_extent_release(env, ext);
894 /* wait for the extent until its state becomes @state */
895 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), &lwi);
896 if (rc == -ETIMEDOUT) {
897 OSC_EXTENT_DUMP(D_ERROR, ext,
898 "%s: wait ext to %d timedout, recovery in progress?\n",
899 osc_export(obj)->exp_obd->obd_name, state);
901 lwi = LWI_INTR(NULL, NULL);
902 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state),
905 if (rc == 0 && ext->oe_rc < 0)
911 * Discard pages with index greater than @size. If @ext is overlapped with
912 * @size, then partial truncate happens.
914 static int osc_extent_truncate(struct osc_extent *ext, pgoff_t trunc_index,
917 struct cl_env_nest nest;
920 struct osc_object *obj = ext->oe_obj;
921 struct client_obd *cli = osc_cli(obj);
922 struct osc_async_page *oap;
923 struct osc_async_page *tmp;
924 int pages_in_chunk = 0;
925 int ppc_bits = cli->cl_chunkbits -
927 __u64 trunc_chunk = trunc_index >> ppc_bits;
933 LASSERT(sanity_check(ext) == 0);
934 LASSERT(ext->oe_state == OES_TRUNC);
935 LASSERT(!ext->oe_urgent);
937 /* Request new lu_env.
938 * We can't use that env from osc_cache_truncate_start() because
939 * it's from lov_io_sub and not fully initialized. */
940 env = cl_env_nested_get(&nest);
941 io = &osc_env_info(env)->oti_io;
942 io->ci_obj = cl_object_top(osc2cl(obj));
943 rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
947 /* discard all pages with index greater then trunc_index */
948 cfs_list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
950 struct cl_page *sub = oap2cl_page(oap);
951 struct cl_page *page = cl_page_top(sub);
953 LASSERT(cfs_list_empty(&oap->oap_rpc_item));
955 /* only discard the pages with their index greater than
956 * trunc_index, and ... */
957 if (sub->cp_index < trunc_index ||
958 (sub->cp_index == trunc_index && partial)) {
959 /* accounting how many pages remaining in the chunk
960 * so that we can calculate grants correctly. */
961 if (sub->cp_index >> ppc_bits == trunc_chunk)
966 cfs_list_del_init(&oap->oap_pending_item);
969 lu_ref_add(&page->cp_reference, "truncate", current);
971 if (cl_page_own(env, io, page) == 0) {
972 cl_page_unmap(env, io, page);
973 cl_page_discard(env, io, page);
974 cl_page_disown(env, io, page);
976 LASSERT(page->cp_state == CPS_FREEING);
980 lu_ref_del(&page->cp_reference, "truncate", current);
981 cl_page_put(env, page);
986 EASSERTF(ergo(ext->oe_start >= trunc_index + !!partial,
987 ext->oe_nr_pages == 0),
988 ext, "trunc_index %lu, partial %d\n", trunc_index, partial);
990 osc_object_lock(obj);
991 if (ext->oe_nr_pages == 0) {
992 LASSERT(pages_in_chunk == 0);
993 grants = ext->oe_grants;
995 } else { /* calculate how many grants we can free */
996 int chunks = (ext->oe_end >> ppc_bits) - trunc_chunk;
1000 /* if there is no pages in this chunk, we can also free grants
1001 * for the last chunk */
1002 if (pages_in_chunk == 0) {
1003 /* if this is the 1st chunk and no pages in this chunk,
1004 * ext->oe_nr_pages must be zero, so we should be in
1005 * the other if-clause. */
1006 LASSERT(trunc_chunk > 0);
1011 /* this is what we can free from this extent */
1012 grants = chunks << cli->cl_chunkbits;
1013 ext->oe_grants -= grants;
1014 last_index = ((trunc_chunk + 1) << ppc_bits) - 1;
1015 ext->oe_end = min(last_index, ext->oe_max_end);
1016 LASSERT(ext->oe_end >= ext->oe_start);
1017 LASSERT(ext->oe_grants > 0);
1019 osc_object_unlock(obj);
1021 if (grants > 0 || nr_pages > 0)
1022 osc_free_grant(cli, nr_pages, grants);
1025 cl_io_fini(env, io);
1026 cl_env_nested_put(&nest, env);
1031 * This function is used to make the extent prepared for transfer.
1032 * A race with flusing page - ll_writepage() has to be handled cautiously.
1034 static int osc_extent_make_ready(const struct lu_env *env,
1035 struct osc_extent *ext)
1037 struct osc_async_page *oap;
1038 struct osc_async_page *last = NULL;
1039 struct osc_object *obj = ext->oe_obj;
1044 /* we're going to grab page lock, so object lock must not be taken. */
1045 LASSERT(sanity_check(ext) == 0);
1046 /* in locking state, any process should not touch this extent. */
1047 EASSERT(ext->oe_state == OES_LOCKING, ext);
1048 EASSERT(ext->oe_owner != NULL, ext);
1050 OSC_EXTENT_DUMP(D_CACHE, ext, "make ready\n");
1052 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1054 if (last == NULL || last->oap_obj_off < oap->oap_obj_off)
1057 /* checking ASYNC_READY is race safe */
1058 if ((oap->oap_async_flags & ASYNC_READY) != 0)
1061 rc = osc_make_ready(env, oap, OBD_BRW_WRITE);
1064 spin_lock(&oap->oap_lock);
1065 oap->oap_async_flags |= ASYNC_READY;
1066 spin_unlock(&oap->oap_lock);
1069 LASSERT((oap->oap_async_flags & ASYNC_READY) != 0);
1072 LASSERTF(0, "unknown return code: %d\n", rc);
1076 LASSERT(page_count == ext->oe_nr_pages);
1077 LASSERT(last != NULL);
1078 /* the last page is the only one we need to refresh its count by
1079 * the size of file. */
1080 if (!(last->oap_async_flags & ASYNC_COUNT_STABLE)) {
1081 last->oap_count = osc_refresh_count(env, last, OBD_BRW_WRITE);
1082 LASSERT(last->oap_count > 0);
1083 LASSERT(last->oap_page_off + last->oap_count <= PAGE_CACHE_SIZE);
1084 spin_lock(&last->oap_lock);
1085 last->oap_async_flags |= ASYNC_COUNT_STABLE;
1086 spin_unlock(&last->oap_lock);
1089 /* for the rest of pages, we don't need to call osf_refresh_count()
1090 * because it's known they are not the last page */
1091 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1092 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE)) {
1093 oap->oap_count = PAGE_CACHE_SIZE - oap->oap_page_off;
1094 spin_lock(&oap->oap_lock);
1095 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1096 spin_unlock(&oap->oap_lock);
1100 osc_object_lock(obj);
1101 osc_extent_state_set(ext, OES_RPC);
1102 osc_object_unlock(obj);
1103 /* get a refcount for RPC. */
1104 osc_extent_get(ext);
1110 * Quick and simple version of osc_extent_find(). This function is frequently
1111 * called to expand the extent for the same IO. To expand the extent, the
1112 * page index must be in the same or next chunk of ext->oe_end.
1114 static int osc_extent_expand(struct osc_extent *ext, pgoff_t index, int *grants)
1116 struct osc_object *obj = ext->oe_obj;
1117 struct client_obd *cli = osc_cli(obj);
1118 struct osc_extent *next;
1119 int ppc_bits = cli->cl_chunkbits - PAGE_CACHE_SHIFT;
1120 pgoff_t chunk = index >> ppc_bits;
1123 int chunksize = 1 << cli->cl_chunkbits;
1127 LASSERT(ext->oe_max_end >= index && ext->oe_start <= index);
1128 osc_object_lock(obj);
1129 LASSERT(sanity_check_nolock(ext) == 0);
1130 end_chunk = ext->oe_end >> ppc_bits;
1131 if (chunk > end_chunk + 1)
1132 GOTO(out, rc = -ERANGE);
1134 if (end_chunk >= chunk)
1137 LASSERT(end_chunk + 1 == chunk);
1138 /* try to expand this extent to cover @index */
1139 end_index = min(ext->oe_max_end, ((chunk + 1) << ppc_bits) - 1);
1141 next = next_extent(ext);
1142 if (next != NULL && next->oe_start <= end_index)
1143 /* complex mode - overlapped with the next extent,
1144 * this case will be handled by osc_extent_find() */
1145 GOTO(out, rc = -EAGAIN);
1147 ext->oe_end = end_index;
1148 ext->oe_grants += chunksize;
1149 *grants -= chunksize;
1150 LASSERT(*grants >= 0);
1151 EASSERTF(osc_extent_is_overlapped(obj, ext) == 0, ext,
1152 "overlapped after expanding for %lu.\n", index);
1156 osc_object_unlock(obj);
1160 static void osc_extent_tree_dump0(int level, struct osc_object *obj,
1161 const char *func, int line)
1163 struct osc_extent *ext;
1166 CDEBUG(level, "Dump object %p extents at %s:%d, mppr: %u.\n",
1167 obj, func, line, osc_cli(obj)->cl_max_pages_per_rpc);
1169 /* osc_object_lock(obj); */
1171 for (ext = first_extent(obj); ext != NULL; ext = next_extent(ext))
1172 OSC_EXTENT_DUMP(level, ext, "in tree %d.\n", cnt++);
1175 cfs_list_for_each_entry(ext, &obj->oo_hp_exts, oe_link)
1176 OSC_EXTENT_DUMP(level, ext, "hp %d.\n", cnt++);
1179 cfs_list_for_each_entry(ext, &obj->oo_urgent_exts, oe_link)
1180 OSC_EXTENT_DUMP(level, ext, "urgent %d.\n", cnt++);
1183 cfs_list_for_each_entry(ext, &obj->oo_reading_exts, oe_link)
1184 OSC_EXTENT_DUMP(level, ext, "reading %d.\n", cnt++);
1185 /* osc_object_unlock(obj); */
1188 /* ------------------ osc extent end ------------------ */
1190 static inline int osc_is_ready(struct osc_object *osc)
1192 return !cfs_list_empty(&osc->oo_ready_item) ||
1193 !cfs_list_empty(&osc->oo_hp_ready_item);
1196 #define OSC_IO_DEBUG(OSC, STR, args...) \
1197 CDEBUG(D_CACHE, "obj %p ready %d|%c|%c wr %d|%c|%c rd %d|%c " STR, \
1198 (OSC), osc_is_ready(OSC), \
1199 list_empty_marker(&(OSC)->oo_hp_ready_item), \
1200 list_empty_marker(&(OSC)->oo_ready_item), \
1201 cfs_atomic_read(&(OSC)->oo_nr_writes), \
1202 list_empty_marker(&(OSC)->oo_hp_exts), \
1203 list_empty_marker(&(OSC)->oo_urgent_exts), \
1204 cfs_atomic_read(&(OSC)->oo_nr_reads), \
1205 list_empty_marker(&(OSC)->oo_reading_exts), \
1208 static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
1211 struct osc_page *opg = oap2osc_page(oap);
1212 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1215 LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
1218 result = cl_page_make_ready(env, page, CRT_WRITE);
1220 opg->ops_submit_time = cfs_time_current();
1224 static int osc_refresh_count(const struct lu_env *env,
1225 struct osc_async_page *oap, int cmd)
1227 struct osc_page *opg = oap2osc_page(oap);
1228 struct cl_page *page = oap2cl_page(oap);
1229 struct cl_object *obj;
1230 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
1235 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
1236 LASSERT(!(cmd & OBD_BRW_READ));
1237 LASSERT(opg != NULL);
1238 obj = opg->ops_cl.cpl_obj;
1240 cl_object_attr_lock(obj);
1241 result = cl_object_attr_get(env, obj, attr);
1242 cl_object_attr_unlock(obj);
1245 kms = attr->cat_kms;
1246 if (cl_offset(obj, page->cp_index) >= kms)
1247 /* catch race with truncate */
1249 else if (cl_offset(obj, page->cp_index + 1) > kms)
1250 /* catch sub-page write at end of file */
1251 return kms % PAGE_CACHE_SIZE;
1253 return PAGE_CACHE_SIZE;
1256 static int osc_completion(const struct lu_env *env, struct osc_async_page *oap,
1259 struct osc_page *opg = oap2osc_page(oap);
1260 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1261 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
1262 enum cl_req_type crt;
1267 cmd &= ~OBD_BRW_NOQUOTA;
1268 LASSERTF(equi(page->cp_state == CPS_PAGEIN, cmd == OBD_BRW_READ),
1269 "cp_state:%u, cmd:%d\n", page->cp_state, cmd);
1270 LASSERTF(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE),
1271 "cp_state:%u, cmd:%d\n", page->cp_state, cmd);
1272 LASSERT(opg->ops_transfer_pinned);
1275 * page->cp_req can be NULL if io submission failed before
1276 * cl_req was allocated.
1278 if (page->cp_req != NULL)
1279 cl_req_page_done(env, page);
1280 LASSERT(page->cp_req == NULL);
1282 crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
1283 /* Clear opg->ops_transfer_pinned before VM lock is released. */
1284 opg->ops_transfer_pinned = 0;
1286 spin_lock(&obj->oo_seatbelt);
1287 LASSERT(opg->ops_submitter != NULL);
1288 LASSERT(!cfs_list_empty(&opg->ops_inflight));
1289 cfs_list_del_init(&opg->ops_inflight);
1290 opg->ops_submitter = NULL;
1291 spin_unlock(&obj->oo_seatbelt);
1293 opg->ops_submit_time = 0;
1294 srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK;
1297 if (rc == 0 && srvlock) {
1298 struct lu_device *ld = opg->ops_cl.cpl_obj->co_lu.lo_dev;
1299 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
1300 int bytes = oap->oap_count;
1302 if (crt == CRT_READ)
1303 stats->os_lockless_reads += bytes;
1305 stats->os_lockless_writes += bytes;
1309 * This has to be the last operation with the page, as locks are
1310 * released in cl_page_completion() and nothing except for the
1311 * reference counter protects page from concurrent reclaim.
1313 lu_ref_del(&page->cp_reference, "transfer", page);
1315 cl_page_completion(env, page, crt, rc);
1320 #define OSC_DUMP_GRANT(lvl, cli, fmt, args...) do { \
1321 struct client_obd *__tmp = (cli); \
1322 CDEBUG(lvl, "%s: grant { dirty: %ld/%ld dirty_pages: %d/%d " \
1323 "unstable_pages: %d/%d dropped: %ld avail: %ld, " \
1324 "reserved: %ld, flight: %d } lru {in list: %d, " \
1325 "left: %d, waiters: %d }" fmt, \
1326 __tmp->cl_import->imp_obd->obd_name, \
1327 __tmp->cl_dirty, __tmp->cl_dirty_max, \
1328 cfs_atomic_read(&obd_dirty_pages), obd_max_dirty_pages, \
1329 cfs_atomic_read(&obd_unstable_pages), obd_max_dirty_pages, \
1330 __tmp->cl_lost_grant, __tmp->cl_avail_grant, \
1331 __tmp->cl_reserved_grant, __tmp->cl_w_in_flight, \
1332 cfs_atomic_read(&__tmp->cl_lru_in_list), \
1333 cfs_atomic_read(&__tmp->cl_lru_busy), \
1334 cfs_atomic_read(&__tmp->cl_lru_shrinkers), ##args); \
1337 /* caller must hold loi_list_lock */
1338 static void osc_consume_write_grant(struct client_obd *cli,
1339 struct brw_page *pga)
1341 LASSERT(spin_is_locked(&cli->cl_loi_list_lock.lock));
1342 LASSERT(!(pga->flag & OBD_BRW_FROM_GRANT));
1343 cfs_atomic_inc(&obd_dirty_pages);
1344 cli->cl_dirty += PAGE_CACHE_SIZE;
1345 pga->flag |= OBD_BRW_FROM_GRANT;
1346 CDEBUG(D_CACHE, "using %lu grant credits for brw %p page %p\n",
1347 PAGE_CACHE_SIZE, pga, pga->pg);
1348 osc_update_next_shrink(cli);
1351 /* the companion to osc_consume_write_grant, called when a brw has completed.
1352 * must be called with the loi lock held. */
1353 static void osc_release_write_grant(struct client_obd *cli,
1354 struct brw_page *pga)
1358 LASSERT(spin_is_locked(&cli->cl_loi_list_lock.lock));
1359 if (!(pga->flag & OBD_BRW_FROM_GRANT)) {
1364 pga->flag &= ~OBD_BRW_FROM_GRANT;
1365 cfs_atomic_dec(&obd_dirty_pages);
1366 cli->cl_dirty -= PAGE_CACHE_SIZE;
1367 if (pga->flag & OBD_BRW_NOCACHE) {
1368 pga->flag &= ~OBD_BRW_NOCACHE;
1369 cfs_atomic_dec(&obd_dirty_transit_pages);
1370 cli->cl_dirty_transit -= PAGE_CACHE_SIZE;
1376 * To avoid sleeping with object lock held, it's good for us allocate enough
1377 * grants before entering into critical section.
1379 * client_obd_list_lock held by caller
1381 static int osc_reserve_grant(struct client_obd *cli, unsigned int bytes)
1385 if (cli->cl_avail_grant >= bytes) {
1386 cli->cl_avail_grant -= bytes;
1387 cli->cl_reserved_grant += bytes;
1393 static void __osc_unreserve_grant(struct client_obd *cli,
1394 unsigned int reserved, unsigned int unused)
1396 /* it's quite normal for us to get more grant than reserved.
1397 * Thinking about a case that two extents merged by adding a new
1398 * chunk, we can save one extent tax. If extent tax is greater than
1399 * one chunk, we can save more grant by adding a new chunk */
1400 cli->cl_reserved_grant -= reserved;
1401 if (unused > reserved) {
1402 cli->cl_avail_grant += reserved;
1403 cli->cl_lost_grant += unused - reserved;
1405 cli->cl_avail_grant += unused;
1409 void osc_unreserve_grant(struct client_obd *cli,
1410 unsigned int reserved, unsigned int unused)
1412 client_obd_list_lock(&cli->cl_loi_list_lock);
1413 __osc_unreserve_grant(cli, reserved, unused);
1415 osc_wake_cache_waiters(cli);
1416 client_obd_list_unlock(&cli->cl_loi_list_lock);
1420 * Free grant after IO is finished or canceled.
1422 * @lost_grant is used to remember how many grants we have allocated but not
1423 * used, we should return these grants to OST. There're two cases where grants
1426 * 2. blocksize at OST is less than PAGE_CACHE_SIZE and a partial page was
1427 * written. In this case OST may use less chunks to serve this partial
1428 * write. OSTs don't actually know the page size on the client side. so
1429 * clients have to calculate lost grant by the blocksize on the OST.
1430 * See filter_grant_check() for details.
1432 static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
1433 unsigned int lost_grant)
1435 int grant = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
1437 client_obd_list_lock(&cli->cl_loi_list_lock);
1438 cfs_atomic_sub(nr_pages, &obd_dirty_pages);
1439 cli->cl_dirty -= nr_pages << PAGE_CACHE_SHIFT;
1440 cli->cl_lost_grant += lost_grant;
1441 if (cli->cl_avail_grant < grant && cli->cl_lost_grant >= grant) {
1442 /* borrow some grant from truncate to avoid the case that
1443 * truncate uses up all avail grant */
1444 cli->cl_lost_grant -= grant;
1445 cli->cl_avail_grant += grant;
1447 osc_wake_cache_waiters(cli);
1448 client_obd_list_unlock(&cli->cl_loi_list_lock);
1449 CDEBUG(D_CACHE, "lost %u grant: %lu avail: %lu dirty: %lu\n",
1450 lost_grant, cli->cl_lost_grant,
1451 cli->cl_avail_grant, cli->cl_dirty);
1455 * The companion to osc_enter_cache(), called when @oap is no longer part of
1456 * the dirty accounting due to error.
1458 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1460 client_obd_list_lock(&cli->cl_loi_list_lock);
1461 osc_release_write_grant(cli, &oap->oap_brw_page);
1462 client_obd_list_unlock(&cli->cl_loi_list_lock);
1466 * Non-blocking version of osc_enter_cache() that consumes grant only when it
1469 static int osc_enter_cache_try(struct client_obd *cli,
1470 struct osc_async_page *oap,
1471 int bytes, int transient)
1475 OSC_DUMP_GRANT(D_CACHE, cli, "need:%d.\n", bytes);
1477 rc = osc_reserve_grant(cli, bytes);
1481 if (cli->cl_dirty + PAGE_CACHE_SIZE <= cli->cl_dirty_max &&
1482 cfs_atomic_read(&obd_unstable_pages) + 1 +
1483 cfs_atomic_read(&obd_dirty_pages) <= obd_max_dirty_pages) {
1484 osc_consume_write_grant(cli, &oap->oap_brw_page);
1486 cli->cl_dirty_transit += PAGE_CACHE_SIZE;
1487 cfs_atomic_inc(&obd_dirty_transit_pages);
1488 oap->oap_brw_flags |= OBD_BRW_NOCACHE;
1492 __osc_unreserve_grant(cli, bytes, bytes);
1498 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1501 client_obd_list_lock(&cli->cl_loi_list_lock);
1502 rc = cfs_list_empty(&ocw->ocw_entry);
1503 client_obd_list_unlock(&cli->cl_loi_list_lock);
1508 * The main entry to reserve dirty page accounting. Usually the grant reserved
1509 * in this function will be freed in bulk in osc_free_grant() unless it fails
1510 * to add osc cache, in that case, it will be freed in osc_exit_cache().
1512 * The process will be put into sleep if it's already run out of grant.
1514 static int osc_enter_cache(const struct lu_env *env, struct client_obd *cli,
1515 struct osc_async_page *oap, int bytes)
1517 struct osc_object *osc = oap->oap_obj;
1518 struct lov_oinfo *loi = osc->oo_oinfo;
1519 struct osc_cache_waiter ocw;
1520 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL,
1521 LWI_ON_SIGNAL_NOOP, NULL);
1525 OSC_DUMP_GRANT(D_CACHE, cli, "need:%d.\n", bytes);
1527 client_obd_list_lock(&cli->cl_loi_list_lock);
1529 /* force the caller to try sync io. this can jump the list
1530 * of queued writes and create a discontiguous rpc stream */
1531 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_NO_GRANT) ||
1532 cli->cl_dirty_max < PAGE_CACHE_SIZE ||
1533 cli->cl_ar.ar_force_sync || loi->loi_ar.ar_force_sync)
1534 GOTO(out, rc = -EDQUOT);
1536 /* Hopefully normal case - cache space and write credits available */
1537 if (osc_enter_cache_try(cli, oap, bytes, 0))
1540 /* We can get here for two reasons: too many dirty pages in cache, or
1541 * run out of grants. In both cases we should write dirty pages out.
1542 * Adding a cache waiter will trigger urgent write-out no matter what
1544 * The exiting condition is no avail grants and no dirty pages caching,
1545 * that really means there is no space on the OST. */
1546 init_waitqueue_head(&ocw.ocw_waitq);
1548 ocw.ocw_grant = bytes;
1549 while (cli->cl_dirty > 0 || cli->cl_w_in_flight > 0) {
1550 cfs_list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1552 client_obd_list_unlock(&cli->cl_loi_list_lock);
1554 osc_io_unplug_async(env, cli, NULL);
1556 CDEBUG(D_CACHE, "%s: sleeping for cache space @ %p for %p\n",
1557 cli->cl_import->imp_obd->obd_name, &ocw, oap);
1559 rc = l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1561 client_obd_list_lock(&cli->cl_loi_list_lock);
1563 /* l_wait_event is interrupted by signal, or timed out */
1567 OSC_DUMP_GRANT(D_ERROR, cli,
1568 "try to reserve %d.\n", bytes);
1569 osc_extent_tree_dump(D_ERROR, osc);
1573 /* Ensures restartability - LU-3581 */
1577 CDEBUG(D_CACHE, "%s: event for cache space @"
1578 " %p never arrived due to %d\n",
1579 cli->cl_import->imp_obd->obd_name,
1583 cfs_list_del_init(&ocw.ocw_entry);
1587 LASSERT(cfs_list_empty(&ocw.ocw_entry));
1592 if (osc_enter_cache_try(cli, oap, bytes, 0))
1597 client_obd_list_unlock(&cli->cl_loi_list_lock);
1598 OSC_DUMP_GRANT(D_CACHE, cli, "returned %d.\n", rc);
1602 /* caller must hold loi_list_lock */
1603 void osc_wake_cache_waiters(struct client_obd *cli)
1605 cfs_list_t *l, *tmp;
1606 struct osc_cache_waiter *ocw;
1609 cfs_list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1610 ocw = cfs_list_entry(l, struct osc_cache_waiter, ocw_entry);
1611 cfs_list_del_init(&ocw->ocw_entry);
1613 ocw->ocw_rc = -EDQUOT;
1614 /* we can't dirty more */
1615 if ((cli->cl_dirty + PAGE_CACHE_SIZE > cli->cl_dirty_max) ||
1616 (cfs_atomic_read(&obd_unstable_pages) + 1 +
1617 cfs_atomic_read(&obd_dirty_pages) > obd_max_dirty_pages)) {
1618 CDEBUG(D_CACHE, "no dirty room: dirty: %ld "
1619 "osc max %ld, sys max %d\n", cli->cl_dirty,
1620 cli->cl_dirty_max, obd_max_dirty_pages);
1625 if (!osc_enter_cache_try(cli, ocw->ocw_oap, ocw->ocw_grant, 0))
1626 ocw->ocw_rc = -EDQUOT;
1629 CDEBUG(D_CACHE, "wake up %p for oap %p, avail grant %ld, %d\n",
1630 ocw, ocw->ocw_oap, cli->cl_avail_grant, ocw->ocw_rc);
1632 wake_up(&ocw->ocw_waitq);
1638 static int osc_max_rpc_in_flight(struct client_obd *cli, struct osc_object *osc)
1640 int hprpc = !!cfs_list_empty(&osc->oo_hp_exts);
1641 return rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight + hprpc;
1644 /* This maintains the lists of pending pages to read/write for a given object
1645 * (lop). This is used by osc_check_rpcs->osc_next_obj() and osc_list_maint()
1646 * to quickly find objects that are ready to send an RPC. */
1647 static int osc_makes_rpc(struct client_obd *cli, struct osc_object *osc,
1650 int invalid_import = 0;
1653 /* if we have an invalid import we want to drain the queued pages
1654 * by forcing them through rpcs that immediately fail and complete
1655 * the pages. recovery relies on this to empty the queued pages
1656 * before canceling the locks and evicting down the llite pages */
1657 if ((cli->cl_import == NULL || cli->cl_import->imp_invalid))
1660 if (cmd & OBD_BRW_WRITE) {
1661 if (cfs_atomic_read(&osc->oo_nr_writes) == 0)
1663 if (invalid_import) {
1664 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1667 if (!cfs_list_empty(&osc->oo_hp_exts)) {
1668 CDEBUG(D_CACHE, "high prio request forcing RPC\n");
1671 if (!cfs_list_empty(&osc->oo_urgent_exts)) {
1672 CDEBUG(D_CACHE, "urgent request forcing RPC\n");
1675 /* trigger a write rpc stream as long as there are dirtiers
1676 * waiting for space. as they're waiting, they're not going to
1677 * create more pages to coalesce with what's waiting.. */
1678 if (!cfs_list_empty(&cli->cl_cache_waiters)) {
1679 CDEBUG(D_CACHE, "cache waiters forcing RPC\n");
1682 if (cfs_atomic_read(&osc->oo_nr_writes) >=
1683 cli->cl_max_pages_per_rpc)
1686 if (cfs_atomic_read(&osc->oo_nr_reads) == 0)
1688 if (invalid_import) {
1689 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1692 /* all read are urgent. */
1693 if (!cfs_list_empty(&osc->oo_reading_exts))
1700 static void osc_update_pending(struct osc_object *obj, int cmd, int delta)
1702 struct client_obd *cli = osc_cli(obj);
1703 if (cmd & OBD_BRW_WRITE) {
1704 cfs_atomic_add(delta, &obj->oo_nr_writes);
1705 cfs_atomic_add(delta, &cli->cl_pending_w_pages);
1706 LASSERT(cfs_atomic_read(&obj->oo_nr_writes) >= 0);
1708 cfs_atomic_add(delta, &obj->oo_nr_reads);
1709 cfs_atomic_add(delta, &cli->cl_pending_r_pages);
1710 LASSERT(cfs_atomic_read(&obj->oo_nr_reads) >= 0);
1712 OSC_IO_DEBUG(obj, "update pending cmd %d delta %d.\n", cmd, delta);
1715 static int osc_makes_hprpc(struct osc_object *obj)
1717 return !cfs_list_empty(&obj->oo_hp_exts);
1720 static void on_list(cfs_list_t *item, cfs_list_t *list, int should_be_on)
1722 if (cfs_list_empty(item) && should_be_on)
1723 cfs_list_add_tail(item, list);
1724 else if (!cfs_list_empty(item) && !should_be_on)
1725 cfs_list_del_init(item);
1728 /* maintain the osc's cli list membership invariants so that osc_send_oap_rpc
1729 * can find pages to build into rpcs quickly */
1730 static int __osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1732 if (osc_makes_hprpc(osc)) {
1734 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, 0);
1735 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 1);
1737 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 0);
1738 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list,
1739 osc_makes_rpc(cli, osc, OBD_BRW_WRITE) ||
1740 osc_makes_rpc(cli, osc, OBD_BRW_READ));
1743 on_list(&osc->oo_write_item, &cli->cl_loi_write_list,
1744 cfs_atomic_read(&osc->oo_nr_writes) > 0);
1746 on_list(&osc->oo_read_item, &cli->cl_loi_read_list,
1747 cfs_atomic_read(&osc->oo_nr_reads) > 0);
1749 return osc_is_ready(osc);
1752 static int osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1756 client_obd_list_lock(&cli->cl_loi_list_lock);
1757 is_ready = __osc_list_maint(cli, osc);
1758 client_obd_list_unlock(&cli->cl_loi_list_lock);
1763 /* this is trying to propogate async writeback errors back up to the
1764 * application. As an async write fails we record the error code for later if
1765 * the app does an fsync. As long as errors persist we force future rpcs to be
1766 * sync so that the app can get a sync error and break the cycle of queueing
1767 * pages for which writeback will fail. */
1768 static void osc_process_ar(struct osc_async_rc *ar, __u64 xid,
1775 ar->ar_force_sync = 1;
1776 ar->ar_min_xid = ptlrpc_sample_next_xid();
1781 if (ar->ar_force_sync && (xid >= ar->ar_min_xid))
1782 ar->ar_force_sync = 0;
1785 /* Performs "unstable" page accounting. This function balances the
1786 * increment operations performed in osc_inc_unstable_pages. It is
1787 * registered as the RPC request callback, and is executed when the
1788 * bulk RPC is committed on the server. Thus at this point, the pages
1789 * involved in the bulk transfer are no longer considered unstable. */
1790 void osc_dec_unstable_pages(struct ptlrpc_request *req)
1792 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
1793 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
1794 obd_count page_count = desc->bd_iov_count;
1797 /* No unstable page tracking */
1798 if (cli->cl_cache == NULL)
1801 LASSERT(page_count >= 0);
1803 for (i = 0; i < page_count; i++)
1804 dec_zone_page_state(desc->bd_iov[i].kiov_page, NR_UNSTABLE_NFS);
1806 cfs_atomic_sub(page_count, &cli->cl_cache->ccc_unstable_nr);
1807 LASSERT(cfs_atomic_read(&cli->cl_cache->ccc_unstable_nr) >= 0);
1809 cfs_atomic_sub(page_count, &cli->cl_unstable_count);
1810 LASSERT(cfs_atomic_read(&cli->cl_unstable_count) >= 0);
1812 cfs_atomic_sub(page_count, &obd_unstable_pages);
1813 LASSERT(cfs_atomic_read(&obd_unstable_pages) >= 0);
1815 spin_lock(&req->rq_lock);
1816 req->rq_committed = 1;
1817 req->rq_unstable = 0;
1818 spin_unlock(&req->rq_lock);
1820 wake_up_all(&cli->cl_cache->ccc_unstable_waitq);
1823 /* "unstable" page accounting. See: osc_dec_unstable_pages. */
1824 void osc_inc_unstable_pages(struct ptlrpc_request *req)
1826 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
1827 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
1828 obd_count page_count = desc->bd_iov_count;
1831 /* No unstable page tracking */
1832 if (cli->cl_cache == NULL)
1835 LASSERT(page_count >= 0);
1837 for (i = 0; i < page_count; i++)
1838 inc_zone_page_state(desc->bd_iov[i].kiov_page, NR_UNSTABLE_NFS);
1840 LASSERT(cfs_atomic_read(&cli->cl_cache->ccc_unstable_nr) >= 0);
1841 cfs_atomic_add(page_count, &cli->cl_cache->ccc_unstable_nr);
1843 LASSERT(cfs_atomic_read(&cli->cl_unstable_count) >= 0);
1844 cfs_atomic_add(page_count, &cli->cl_unstable_count);
1846 LASSERT(cfs_atomic_read(&obd_unstable_pages) >= 0);
1847 cfs_atomic_add(page_count, &obd_unstable_pages);
1849 spin_lock(&req->rq_lock);
1851 /* If the request has already been committed (i.e. brw_commit
1852 * called via rq_commit_cb), we need to undo the unstable page
1853 * increments we just performed because rq_commit_cb wont be
1854 * called again. Otherwise, just set the commit callback so the
1855 * unstable page accounting is properly updated when the request
1857 if (req->rq_committed) {
1858 /* Drop lock before calling osc_dec_unstable_pages */
1859 spin_unlock(&req->rq_lock);
1860 osc_dec_unstable_pages(req);
1861 spin_lock(&req->rq_lock);
1863 req->rq_unstable = 1;
1864 req->rq_commit_cb = osc_dec_unstable_pages;
1867 spin_unlock(&req->rq_lock);
1870 /* this must be called holding the loi list lock to give coverage to exit_cache,
1871 * async_flag maintenance, and oap_request */
1872 static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
1873 struct osc_async_page *oap, int sent, int rc)
1875 struct osc_object *osc = oap->oap_obj;
1876 struct lov_oinfo *loi = osc->oo_oinfo;
1880 if (oap->oap_request != NULL) {
1882 osc_inc_unstable_pages(oap->oap_request);
1884 xid = ptlrpc_req_xid(oap->oap_request);
1885 ptlrpc_req_finished(oap->oap_request);
1886 oap->oap_request = NULL;
1889 /* As the transfer for this page is being done, clear the flags */
1890 spin_lock(&oap->oap_lock);
1891 oap->oap_async_flags = 0;
1892 spin_unlock(&oap->oap_lock);
1893 oap->oap_interrupted = 0;
1895 if (oap->oap_cmd & OBD_BRW_WRITE && xid > 0) {
1896 client_obd_list_lock(&cli->cl_loi_list_lock);
1897 osc_process_ar(&cli->cl_ar, xid, rc);
1898 osc_process_ar(&loi->loi_ar, xid, rc);
1899 client_obd_list_unlock(&cli->cl_loi_list_lock);
1902 rc = osc_completion(env, oap, oap->oap_cmd, rc);
1904 CERROR("completion on oap %p obj %p returns %d.\n",
1911 * Try to add extent to one RPC. We need to think about the following things:
1912 * - # of pages must not be over max_pages_per_rpc
1913 * - extent must be compatible with previous ones
1915 static int try_to_add_extent_for_io(struct client_obd *cli,
1916 struct osc_extent *ext, cfs_list_t *rpclist,
1917 int *pc, unsigned int *max_pages)
1919 struct osc_extent *tmp;
1922 EASSERT((ext->oe_state == OES_CACHE || ext->oe_state == OES_LOCK_DONE),
1925 *max_pages = max(ext->oe_mppr, *max_pages);
1926 if (*pc + ext->oe_nr_pages > *max_pages)
1929 cfs_list_for_each_entry(tmp, rpclist, oe_link) {
1930 EASSERT(tmp->oe_owner == current, tmp);
1932 if (overlapped(tmp, ext)) {
1933 OSC_EXTENT_DUMP(D_ERROR, tmp, "overlapped %p.\n", ext);
1938 if (tmp->oe_srvlock != ext->oe_srvlock ||
1939 !tmp->oe_grants != !ext->oe_grants)
1942 /* remove break for strict check */
1946 *pc += ext->oe_nr_pages;
1947 cfs_list_move_tail(&ext->oe_link, rpclist);
1948 ext->oe_owner = current;
1953 * In order to prevent multiple ptlrpcd from breaking contiguous extents,
1954 * get_write_extent() takes all appropriate extents in atomic.
1956 * The following policy is used to collect extents for IO:
1957 * 1. Add as many HP extents as possible;
1958 * 2. Add the first urgent extent in urgent extent list and take it out of
1960 * 3. Add subsequent extents of this urgent extent;
1961 * 4. If urgent list is not empty, goto 2;
1962 * 5. Traverse the extent tree from the 1st extent;
1963 * 6. Above steps exit if there is no space in this RPC.
1965 static int get_write_extents(struct osc_object *obj, cfs_list_t *rpclist)
1967 struct client_obd *cli = osc_cli(obj);
1968 struct osc_extent *ext;
1970 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1972 LASSERT(osc_object_is_locked(obj));
1973 while (!cfs_list_empty(&obj->oo_hp_exts)) {
1974 ext = cfs_list_entry(obj->oo_hp_exts.next, struct osc_extent,
1976 LASSERT(ext->oe_state == OES_CACHE);
1977 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1980 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1982 if (page_count == max_pages)
1985 while (!cfs_list_empty(&obj->oo_urgent_exts)) {
1986 ext = cfs_list_entry(obj->oo_urgent_exts.next,
1987 struct osc_extent, oe_link);
1988 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1992 if (!ext->oe_intree)
1995 while ((ext = next_extent(ext)) != NULL) {
1996 if ((ext->oe_state != OES_CACHE) ||
1997 (!cfs_list_empty(&ext->oe_link) &&
1998 ext->oe_owner != NULL))
2001 if (!try_to_add_extent_for_io(cli, ext, rpclist,
2002 &page_count, &max_pages))
2006 if (page_count == max_pages)
2009 ext = first_extent(obj);
2010 while (ext != NULL) {
2011 if ((ext->oe_state != OES_CACHE) ||
2012 /* this extent may be already in current rpclist */
2013 (!cfs_list_empty(&ext->oe_link) && ext->oe_owner != NULL)) {
2014 ext = next_extent(ext);
2018 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
2022 ext = next_extent(ext);
2028 osc_send_write_rpc(const struct lu_env *env, struct client_obd *cli,
2029 struct osc_object *osc, pdl_policy_t pol)
2031 CFS_LIST_HEAD(rpclist);
2032 struct osc_extent *ext;
2033 struct osc_extent *tmp;
2034 struct osc_extent *first = NULL;
2035 obd_count page_count = 0;
2040 LASSERT(osc_object_is_locked(osc));
2042 page_count = get_write_extents(osc, &rpclist);
2043 LASSERT(equi(page_count == 0, cfs_list_empty(&rpclist)));
2045 if (cfs_list_empty(&rpclist))
2048 osc_update_pending(osc, OBD_BRW_WRITE, -page_count);
2050 cfs_list_for_each_entry(ext, &rpclist, oe_link) {
2051 LASSERT(ext->oe_state == OES_CACHE ||
2052 ext->oe_state == OES_LOCK_DONE);
2053 if (ext->oe_state == OES_CACHE)
2054 osc_extent_state_set(ext, OES_LOCKING);
2056 osc_extent_state_set(ext, OES_RPC);
2059 /* we're going to grab page lock, so release object lock because
2060 * lock order is page lock -> object lock. */
2061 osc_object_unlock(osc);
2063 cfs_list_for_each_entry_safe(ext, tmp, &rpclist, oe_link) {
2064 if (ext->oe_state == OES_LOCKING) {
2065 rc = osc_extent_make_ready(env, ext);
2066 if (unlikely(rc < 0)) {
2067 cfs_list_del_init(&ext->oe_link);
2068 osc_extent_finish(env, ext, 0, rc);
2072 if (first == NULL) {
2074 srvlock = ext->oe_srvlock;
2076 LASSERT(srvlock == ext->oe_srvlock);
2080 if (!cfs_list_empty(&rpclist)) {
2081 LASSERT(page_count > 0);
2082 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_WRITE, pol);
2083 LASSERT(cfs_list_empty(&rpclist));
2086 osc_object_lock(osc);
2091 * prepare pages for ASYNC io and put pages in send queue.
2093 * \param cmd OBD_BRW_* macroses
2094 * \param lop pending pages
2096 * \return zero if no page added to send queue.
2097 * \return 1 if pages successfully added to send queue.
2098 * \return negative on errors.
2101 osc_send_read_rpc(const struct lu_env *env, struct client_obd *cli,
2102 struct osc_object *osc, pdl_policy_t pol)
2104 struct osc_extent *ext;
2105 struct osc_extent *next;
2106 CFS_LIST_HEAD(rpclist);
2108 unsigned int max_pages = cli->cl_max_pages_per_rpc;
2112 LASSERT(osc_object_is_locked(osc));
2113 cfs_list_for_each_entry_safe(ext, next,
2114 &osc->oo_reading_exts, oe_link) {
2115 EASSERT(ext->oe_state == OES_LOCK_DONE, ext);
2116 if (!try_to_add_extent_for_io(cli, ext, &rpclist, &page_count,
2119 osc_extent_state_set(ext, OES_RPC);
2120 EASSERT(ext->oe_nr_pages <= max_pages, ext);
2122 LASSERT(page_count <= max_pages);
2124 osc_update_pending(osc, OBD_BRW_READ, -page_count);
2126 if (!cfs_list_empty(&rpclist)) {
2127 osc_object_unlock(osc);
2129 LASSERT(page_count > 0);
2130 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_READ, pol);
2131 LASSERT(cfs_list_empty(&rpclist));
2133 osc_object_lock(osc);
2138 #define list_to_obj(list, item) ({ \
2139 cfs_list_t *__tmp = (list)->next; \
2140 cfs_list_del_init(__tmp); \
2141 cfs_list_entry(__tmp, struct osc_object, oo_##item); \
2144 /* This is called by osc_check_rpcs() to find which objects have pages that
2145 * we could be sending. These lists are maintained by osc_makes_rpc(). */
2146 static struct osc_object *osc_next_obj(struct client_obd *cli)
2150 /* First return objects that have blocked locks so that they
2151 * will be flushed quickly and other clients can get the lock,
2152 * then objects which have pages ready to be stuffed into RPCs */
2153 if (!cfs_list_empty(&cli->cl_loi_hp_ready_list))
2154 RETURN(list_to_obj(&cli->cl_loi_hp_ready_list, hp_ready_item));
2155 if (!cfs_list_empty(&cli->cl_loi_ready_list))
2156 RETURN(list_to_obj(&cli->cl_loi_ready_list, ready_item));
2158 /* then if we have cache waiters, return all objects with queued
2159 * writes. This is especially important when many small files
2160 * have filled up the cache and not been fired into rpcs because
2161 * they don't pass the nr_pending/object threshhold */
2162 if (!cfs_list_empty(&cli->cl_cache_waiters) &&
2163 !cfs_list_empty(&cli->cl_loi_write_list))
2164 RETURN(list_to_obj(&cli->cl_loi_write_list, write_item));
2166 /* then return all queued objects when we have an invalid import
2167 * so that they get flushed */
2168 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
2169 if (!cfs_list_empty(&cli->cl_loi_write_list))
2170 RETURN(list_to_obj(&cli->cl_loi_write_list,
2172 if (!cfs_list_empty(&cli->cl_loi_read_list))
2173 RETURN(list_to_obj(&cli->cl_loi_read_list,
2179 /* called with the loi list lock held */
2180 static void osc_check_rpcs(const struct lu_env *env, struct client_obd *cli,
2183 struct osc_object *osc;
2187 while ((osc = osc_next_obj(cli)) != NULL) {
2188 struct cl_object *obj = osc2cl(osc);
2189 struct lu_ref_link link;
2191 OSC_IO_DEBUG(osc, "%lu in flight\n", rpcs_in_flight(cli));
2193 if (osc_max_rpc_in_flight(cli, osc)) {
2194 __osc_list_maint(cli, osc);
2199 client_obd_list_unlock(&cli->cl_loi_list_lock);
2200 lu_object_ref_add_at(&obj->co_lu, &link, "check",
2203 /* attempt some read/write balancing by alternating between
2204 * reads and writes in an object. The makes_rpc checks here
2205 * would be redundant if we were getting read/write work items
2206 * instead of objects. we don't want send_oap_rpc to drain a
2207 * partial read pending queue when we're given this object to
2208 * do io on writes while there are cache waiters */
2209 osc_object_lock(osc);
2210 if (osc_makes_rpc(cli, osc, OBD_BRW_WRITE)) {
2211 rc = osc_send_write_rpc(env, cli, osc, pol);
2213 CERROR("Write request failed with %d\n", rc);
2215 /* osc_send_write_rpc failed, mostly because of
2218 * It can't break here, because if:
2219 * - a page was submitted by osc_io_submit, so
2221 * - no request in flight
2222 * - no subsequent request
2223 * The system will be in live-lock state,
2224 * because there is no chance to call
2225 * osc_io_unplug() and osc_check_rpcs() any
2226 * more. pdflush can't help in this case,
2227 * because it might be blocked at grabbing
2228 * the page lock as we mentioned.
2230 * Anyway, continue to drain pages. */
2234 if (osc_makes_rpc(cli, osc, OBD_BRW_READ)) {
2235 rc = osc_send_read_rpc(env, cli, osc, pol);
2237 CERROR("Read request failed with %d\n", rc);
2239 osc_object_unlock(osc);
2241 osc_list_maint(cli, osc);
2242 lu_object_ref_del_at(&obj->co_lu, &link, "check",
2244 cl_object_put(env, obj);
2246 client_obd_list_lock(&cli->cl_loi_list_lock);
2250 static int osc_io_unplug0(const struct lu_env *env, struct client_obd *cli,
2251 struct osc_object *osc, pdl_policy_t pol, int async)
2255 if (osc != NULL && osc_list_maint(cli, osc) == 0)
2259 /* disable osc_lru_shrink() temporarily to avoid
2260 * potential stack overrun problem. LU-2859 */
2261 cfs_atomic_inc(&cli->cl_lru_shrinkers);
2262 client_obd_list_lock(&cli->cl_loi_list_lock);
2263 osc_check_rpcs(env, cli, pol);
2264 client_obd_list_unlock(&cli->cl_loi_list_lock);
2265 cfs_atomic_dec(&cli->cl_lru_shrinkers);
2267 CDEBUG(D_CACHE, "Queue writeback work for client %p.\n", cli);
2268 LASSERT(cli->cl_writeback_work != NULL);
2269 rc = ptlrpcd_queue_work(cli->cl_writeback_work);
2274 static int osc_io_unplug_async(const struct lu_env *env,
2275 struct client_obd *cli, struct osc_object *osc)
2277 /* XXX: policy is no use actually. */
2278 return osc_io_unplug0(env, cli, osc, PDL_POLICY_ROUND, 1);
2281 void osc_io_unplug(const struct lu_env *env, struct client_obd *cli,
2282 struct osc_object *osc, pdl_policy_t pol)
2284 (void)osc_io_unplug0(env, cli, osc, pol, 0);
2287 int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops,
2288 struct page *page, loff_t offset)
2290 struct obd_export *exp = osc_export(osc);
2291 struct osc_async_page *oap = &ops->ops_oap;
2295 return cfs_size_round(sizeof(*oap));
2297 oap->oap_magic = OAP_MAGIC;
2298 oap->oap_cli = &exp->exp_obd->u.cli;
2301 oap->oap_page = page;
2302 oap->oap_obj_off = offset;
2303 LASSERT(!(offset & ~CFS_PAGE_MASK));
2305 if (!client_is_remote(exp) && cfs_capable(CFS_CAP_SYS_RESOURCE))
2306 oap->oap_brw_flags = OBD_BRW_NOQUOTA;
2308 CFS_INIT_LIST_HEAD(&oap->oap_pending_item);
2309 CFS_INIT_LIST_HEAD(&oap->oap_rpc_item);
2311 spin_lock_init(&oap->oap_lock);
2312 CDEBUG(D_INFO, "oap %p page %p obj off "LPU64"\n",
2313 oap, page, oap->oap_obj_off);
2317 int osc_queue_async_io(const struct lu_env *env, struct cl_io *io,
2318 struct osc_page *ops)
2320 struct osc_io *oio = osc_env_io(env);
2321 struct osc_extent *ext = NULL;
2322 struct osc_async_page *oap = &ops->ops_oap;
2323 struct client_obd *cli = oap->oap_cli;
2324 struct osc_object *osc = oap->oap_obj;
2327 int brw_flags = OBD_BRW_ASYNC;
2328 int cmd = OBD_BRW_WRITE;
2329 int need_release = 0;
2333 if (oap->oap_magic != OAP_MAGIC)
2336 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2339 if (!cfs_list_empty(&oap->oap_pending_item) ||
2340 !cfs_list_empty(&oap->oap_rpc_item))
2343 /* Set the OBD_BRW_SRVLOCK before the page is queued. */
2344 brw_flags |= ops->ops_srvlock ? OBD_BRW_SRVLOCK : 0;
2345 if (!client_is_remote(osc_export(osc)) &&
2346 cfs_capable(CFS_CAP_SYS_RESOURCE)) {
2347 brw_flags |= OBD_BRW_NOQUOTA;
2348 cmd |= OBD_BRW_NOQUOTA;
2351 /* check if the file's owner/group is over quota */
2352 if (!(cmd & OBD_BRW_NOQUOTA)) {
2353 struct cl_object *obj;
2354 struct cl_attr *attr;
2355 unsigned int qid[MAXQUOTAS];
2357 obj = cl_object_top(&osc->oo_cl);
2358 attr = &osc_env_info(env)->oti_attr;
2360 cl_object_attr_lock(obj);
2361 rc = cl_object_attr_get(env, obj, attr);
2362 cl_object_attr_unlock(obj);
2364 qid[USRQUOTA] = attr->cat_uid;
2365 qid[GRPQUOTA] = attr->cat_gid;
2366 if (rc == 0 && osc_quota_chkdq(cli, qid) == NO_QUOTA)
2372 if (osc_over_unstable_soft_limit(cli))
2373 brw_flags |= OBD_BRW_SOFT_SYNC;
2376 oap->oap_page_off = ops->ops_from;
2377 oap->oap_count = ops->ops_to - ops->ops_from;
2378 /* No need to hold a lock here,
2379 * since this page is not in any list yet. */
2380 oap->oap_async_flags = 0;
2381 oap->oap_brw_flags = brw_flags;
2383 OSC_IO_DEBUG(osc, "oap %p page %p added for cmd %d\n",
2384 oap, oap->oap_page, oap->oap_cmd & OBD_BRW_RWMASK);
2386 index = oap2cl_page(oap)->cp_index;
2388 /* Add this page into extent by the following steps:
2389 * 1. if there exists an active extent for this IO, mostly this page
2390 * can be added to the active extent and sometimes we need to
2391 * expand extent to accomodate this page;
2392 * 2. otherwise, a new extent will be allocated. */
2394 ext = oio->oi_active;
2395 if (ext != NULL && ext->oe_start <= index && ext->oe_max_end >= index) {
2396 /* one chunk plus extent overhead must be enough to write this
2398 grants = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2399 if (ext->oe_end >= index)
2402 /* it doesn't need any grant to dirty this page */
2403 client_obd_list_lock(&cli->cl_loi_list_lock);
2404 rc = osc_enter_cache_try(cli, oap, grants, 0);
2405 client_obd_list_unlock(&cli->cl_loi_list_lock);
2406 if (rc == 0) { /* try failed */
2409 } else if (ext->oe_end < index) {
2411 /* try to expand this extent */
2412 rc = osc_extent_expand(ext, index, &tmp);
2415 /* don't free reserved grant */
2417 OSC_EXTENT_DUMP(D_CACHE, ext,
2418 "expanded for %lu.\n", index);
2419 osc_unreserve_grant(cli, grants, tmp);
2424 } else if (ext != NULL) {
2425 /* index is located outside of active extent */
2429 osc_extent_release(env, ext);
2430 oio->oi_active = NULL;
2435 int tmp = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2437 /* try to find new extent to cover this page */
2438 LASSERT(oio->oi_active == NULL);
2439 /* we may have allocated grant for this page if we failed
2440 * to expand the previous active extent. */
2441 LASSERT(ergo(grants > 0, grants >= tmp));
2445 /* we haven't allocated grant for this page. */
2446 rc = osc_enter_cache(env, cli, oap, tmp);
2453 ext = osc_extent_find(env, osc, index, &tmp);
2455 LASSERT(tmp == grants);
2456 osc_exit_cache(cli, oap);
2460 oio->oi_active = ext;
2464 osc_unreserve_grant(cli, grants, tmp);
2467 LASSERT(ergo(rc == 0, ext != NULL));
2469 EASSERTF(ext->oe_end >= index && ext->oe_start <= index,
2470 ext, "index = %lu.\n", index);
2471 LASSERT((oap->oap_brw_flags & OBD_BRW_FROM_GRANT) != 0);
2473 osc_object_lock(osc);
2474 if (ext->oe_nr_pages == 0)
2475 ext->oe_srvlock = ops->ops_srvlock;
2477 LASSERT(ext->oe_srvlock == ops->ops_srvlock);
2479 cfs_list_add_tail(&oap->oap_pending_item, &ext->oe_pages);
2480 osc_object_unlock(osc);
2485 int osc_teardown_async_page(const struct lu_env *env,
2486 struct osc_object *obj, struct osc_page *ops)
2488 struct osc_async_page *oap = &ops->ops_oap;
2489 struct osc_extent *ext = NULL;
2493 LASSERT(oap->oap_magic == OAP_MAGIC);
2495 CDEBUG(D_INFO, "teardown oap %p page %p at index %lu.\n",
2496 oap, ops, oap2cl_page(oap)->cp_index);
2498 osc_object_lock(obj);
2499 if (!cfs_list_empty(&oap->oap_rpc_item)) {
2500 CDEBUG(D_CACHE, "oap %p is not in cache.\n", oap);
2502 } else if (!cfs_list_empty(&oap->oap_pending_item)) {
2503 ext = osc_extent_lookup(obj, oap2cl_page(oap)->cp_index);
2504 /* only truncated pages are allowed to be taken out.
2505 * See osc_extent_truncate() and osc_cache_truncate_start()
2507 if (ext != NULL && ext->oe_state != OES_TRUNC) {
2508 OSC_EXTENT_DUMP(D_ERROR, ext, "trunc at %lu.\n",
2509 oap2cl_page(oap)->cp_index);
2513 osc_object_unlock(obj);
2515 osc_extent_put(env, ext);
2520 * This is called when a page is picked up by kernel to write out.
2522 * We should find out the corresponding extent and add the whole extent
2523 * into urgent list. The extent may be being truncated or used, handle it
2526 int osc_flush_async_page(const struct lu_env *env, struct cl_io *io,
2527 struct osc_page *ops)
2529 struct osc_extent *ext = NULL;
2530 struct osc_object *obj = cl2osc(ops->ops_cl.cpl_obj);
2531 struct cl_page *cp = ops->ops_cl.cpl_page;
2532 pgoff_t index = cp->cp_index;
2533 struct osc_async_page *oap = &ops->ops_oap;
2534 bool unplug = false;
2538 osc_object_lock(obj);
2539 ext = osc_extent_lookup(obj, index);
2541 osc_extent_tree_dump(D_ERROR, obj);
2542 LASSERTF(0, "page index %lu is NOT covered.\n", index);
2545 switch (ext->oe_state) {
2548 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(cp),
2549 "flush an in-rpc page?\n");
2553 /* If we know this extent is being written out, we should abort
2554 * so that the writer can make this page ready. Otherwise, there
2555 * exists a deadlock problem because other process can wait for
2556 * page writeback bit holding page lock; and meanwhile in
2557 * vvp_page_make_ready(), we need to grab page lock before
2558 * really sending the RPC. */
2560 /* race with truncate, page will be redirtied */
2561 GOTO(out, rc = -EAGAIN);
2566 rc = cl_page_prep(env, io, cl_page_top(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 (cfs_list_empty(&ext->oe_link))
2582 cfs_list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2589 osc_object_unlock(obj);
2590 osc_extent_put(env, ext);
2592 osc_io_unplug_async(env, osc_cli(obj), obj);
2597 * this is called when a sync waiter receives an interruption. Its job is to
2598 * get the caller woken as soon as possible. If its page hasn't been put in an
2599 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
2600 * desiring interruption which will forcefully complete the rpc once the rpc
2603 int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops)
2605 struct osc_async_page *oap = &ops->ops_oap;
2606 struct osc_object *obj = oap->oap_obj;
2607 struct client_obd *cli = osc_cli(obj);
2608 struct osc_extent *ext;
2609 struct osc_extent *found = NULL;
2611 pgoff_t index = oap2cl_page(oap)->cp_index;
2616 LASSERT(!oap->oap_interrupted);
2617 oap->oap_interrupted = 1;
2619 /* Find out the caching extent */
2620 osc_object_lock(obj);
2621 if (oap->oap_cmd & OBD_BRW_WRITE) {
2622 plist = &obj->oo_urgent_exts;
2623 cmd = OBD_BRW_WRITE;
2625 plist = &obj->oo_reading_exts;
2628 cfs_list_for_each_entry(ext, plist, oe_link) {
2629 if (ext->oe_start <= index && ext->oe_end >= index) {
2630 LASSERT(ext->oe_state == OES_LOCK_DONE);
2631 /* For OES_LOCK_DONE state extent, it has already held
2632 * a refcount for RPC. */
2633 found = osc_extent_get(ext);
2637 if (found != NULL) {
2638 cfs_list_del_init(&found->oe_link);
2639 osc_update_pending(obj, cmd, -found->oe_nr_pages);
2640 osc_object_unlock(obj);
2642 osc_extent_finish(env, found, 0, -EINTR);
2643 osc_extent_put(env, found);
2646 osc_object_unlock(obj);
2647 /* ok, it's been put in an rpc. only one oap gets a request
2649 if (oap->oap_request != NULL) {
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 cfs_list_t *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;
2668 int mppr = cli->cl_max_pages_per_rpc;
2669 pgoff_t start = CL_PAGE_EOF;
2673 cfs_list_for_each_entry(oap, list, oap_pending_item) {
2674 struct cl_page *cp = oap2cl_page(oap);
2675 if (cp->cp_index > end)
2677 if (cp->cp_index < start)
2678 start = cp->cp_index;
2680 mppr <<= (page_count > mppr);
2683 ext = osc_extent_alloc(obj);
2685 cfs_list_for_each_entry(oap, list, oap_pending_item) {
2686 cfs_list_del_init(&oap->oap_pending_item);
2687 osc_ap_completion(env, cli, oap, 0, -ENOMEM);
2692 ext->oe_rw = !!(cmd & OBD_BRW_READ);
2694 ext->oe_start = start;
2695 ext->oe_end = ext->oe_max_end = end;
2697 ext->oe_srvlock = !!(brw_flags & OBD_BRW_SRVLOCK);
2698 ext->oe_nr_pages = page_count;
2699 ext->oe_mppr = mppr;
2700 cfs_list_splice_init(list, &ext->oe_pages);
2702 osc_object_lock(obj);
2703 /* Reuse the initial refcount for RPC, don't drop it */
2704 osc_extent_state_set(ext, OES_LOCK_DONE);
2705 if (cmd & OBD_BRW_WRITE) {
2706 cfs_list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2707 osc_update_pending(obj, OBD_BRW_WRITE, page_count);
2709 cfs_list_add_tail(&ext->oe_link, &obj->oo_reading_exts);
2710 osc_update_pending(obj, OBD_BRW_READ, page_count);
2712 osc_object_unlock(obj);
2714 osc_io_unplug(env, cli, obj, PDL_POLICY_ROUND);
2719 * Called by osc_io_setattr_start() to freeze and destroy covering extents.
2721 int osc_cache_truncate_start(const struct lu_env *env, struct osc_io *oio,
2722 struct osc_object *obj, __u64 size)
2724 struct client_obd *cli = osc_cli(obj);
2725 struct osc_extent *ext;
2726 struct osc_extent *waiting = NULL;
2728 CFS_LIST_HEAD(list);
2733 /* pages with index greater or equal to index will be truncated. */
2734 index = cl_index(osc2cl(obj), size);
2735 partial = size > cl_offset(osc2cl(obj), index);
2738 osc_object_lock(obj);
2739 ext = osc_extent_search(obj, index);
2741 ext = first_extent(obj);
2742 else if (ext->oe_end < index)
2743 ext = next_extent(ext);
2744 while (ext != NULL) {
2745 EASSERT(ext->oe_state != OES_TRUNC, ext);
2747 if (ext->oe_state > OES_CACHE || ext->oe_urgent) {
2748 /* if ext is in urgent state, it means there must exist
2749 * a page already having been flushed by write_page().
2750 * We have to wait for this extent because we can't
2751 * truncate that page. */
2752 LASSERT(!ext->oe_hp);
2753 OSC_EXTENT_DUMP(D_CACHE, ext,
2754 "waiting for busy extent\n");
2755 waiting = osc_extent_get(ext);
2759 OSC_EXTENT_DUMP(D_CACHE, ext, "try to trunc:"LPU64".\n", size);
2761 osc_extent_get(ext);
2762 if (ext->oe_state == OES_ACTIVE) {
2763 /* though we grab inode mutex for write path, but we
2764 * release it before releasing extent(in osc_io_end()),
2765 * so there is a race window that an extent is still
2766 * in OES_ACTIVE when truncate starts. */
2767 LASSERT(!ext->oe_trunc_pending);
2768 ext->oe_trunc_pending = 1;
2770 EASSERT(ext->oe_state == OES_CACHE, ext);
2771 osc_extent_state_set(ext, OES_TRUNC);
2772 osc_update_pending(obj, OBD_BRW_WRITE,
2775 EASSERT(cfs_list_empty(&ext->oe_link), ext);
2776 cfs_list_add_tail(&ext->oe_link, &list);
2778 ext = next_extent(ext);
2780 osc_object_unlock(obj);
2782 osc_list_maint(cli, obj);
2784 while (!cfs_list_empty(&list)) {
2787 ext = cfs_list_entry(list.next, struct osc_extent, oe_link);
2788 cfs_list_del_init(&ext->oe_link);
2790 /* extent may be in OES_ACTIVE state because inode mutex
2791 * is released before osc_io_end() in file write case */
2792 if (ext->oe_state != OES_TRUNC)
2793 osc_extent_wait(env, ext, OES_TRUNC);
2795 rc = osc_extent_truncate(ext, index, partial);
2800 OSC_EXTENT_DUMP(D_ERROR, ext,
2801 "truncate error %d\n", rc);
2802 } else if (ext->oe_nr_pages == 0) {
2803 osc_extent_remove(ext);
2805 /* this must be an overlapped extent which means only
2806 * part of pages in this extent have been truncated.
2808 EASSERTF(ext->oe_start <= index, ext,
2809 "trunc index = %lu/%d.\n", index, partial);
2810 /* fix index to skip this partially truncated extent */
2811 index = ext->oe_end + 1;
2814 /* we need to hold this extent in OES_TRUNC state so
2815 * that no writeback will happen. This is to avoid
2817 LASSERT(oio->oi_trunc == NULL);
2818 oio->oi_trunc = osc_extent_get(ext);
2819 OSC_EXTENT_DUMP(D_CACHE, ext,
2820 "trunc at "LPU64"\n", size);
2822 osc_extent_put(env, ext);
2824 if (waiting != NULL) {
2827 /* ignore the result of osc_extent_wait the write initiator
2828 * should take care of it. */
2829 rc = osc_extent_wait(env, waiting, OES_INV);
2831 OSC_EXTENT_DUMP(D_CACHE, waiting, "error: %d.\n", rc);
2833 osc_extent_put(env, waiting);
2841 * Called after osc_io_setattr_end to add oio->oi_trunc back to cache.
2843 void osc_cache_truncate_end(const struct lu_env *env, struct osc_io *oio,
2844 struct osc_object *obj)
2846 struct osc_extent *ext = oio->oi_trunc;
2848 oio->oi_trunc = NULL;
2850 bool unplug = false;
2852 EASSERT(ext->oe_nr_pages > 0, ext);
2853 EASSERT(ext->oe_state == OES_TRUNC, ext);
2854 EASSERT(!ext->oe_urgent, ext);
2856 OSC_EXTENT_DUMP(D_CACHE, ext, "trunc -> cache.\n");
2857 osc_object_lock(obj);
2858 osc_extent_state_set(ext, OES_CACHE);
2859 if (ext->oe_fsync_wait && !ext->oe_urgent) {
2861 cfs_list_move_tail(&ext->oe_link, &obj->oo_urgent_exts);
2864 osc_update_pending(obj, OBD_BRW_WRITE, ext->oe_nr_pages);
2865 osc_object_unlock(obj);
2866 osc_extent_put(env, ext);
2869 osc_io_unplug_async(env, osc_cli(obj), obj);
2874 * Wait for extents in a specific range to be written out.
2875 * The caller must have called osc_cache_writeback_range() to issue IO
2876 * otherwise it will take a long time for this function to finish.
2878 * Caller must hold inode_mutex , or cancel exclusive dlm lock so that
2879 * nobody else can dirty this range of file while we're waiting for
2880 * extents to be written.
2882 int osc_cache_wait_range(const struct lu_env *env, struct osc_object *obj,
2883 pgoff_t start, pgoff_t end)
2885 struct osc_extent *ext;
2886 pgoff_t index = start;
2891 osc_object_lock(obj);
2892 ext = osc_extent_search(obj, index);
2894 ext = first_extent(obj);
2895 else if (ext->oe_end < index)
2896 ext = next_extent(ext);
2897 while (ext != NULL) {
2900 if (ext->oe_start > end)
2903 if (!ext->oe_fsync_wait) {
2904 ext = next_extent(ext);
2908 EASSERT(ergo(ext->oe_state == OES_CACHE,
2909 ext->oe_hp || ext->oe_urgent), ext);
2910 EASSERT(ergo(ext->oe_state == OES_ACTIVE,
2911 !ext->oe_hp && ext->oe_urgent), ext);
2913 index = ext->oe_end + 1;
2914 osc_extent_get(ext);
2915 osc_object_unlock(obj);
2917 rc = osc_extent_wait(env, ext, OES_INV);
2920 osc_extent_put(env, ext);
2923 osc_object_unlock(obj);
2925 OSC_IO_DEBUG(obj, "sync file range.\n");
2930 * Called to write out a range of osc object.
2932 * @hp : should be set this is caused by lock cancel;
2933 * @discard: is set if dirty pages should be dropped - file will be deleted or
2934 * truncated, this implies there is no partially discarding extents.
2936 * Return how many pages will be issued, or error code if error occurred.
2938 int osc_cache_writeback_range(const struct lu_env *env, struct osc_object *obj,
2939 pgoff_t start, pgoff_t end, int hp, int discard)
2941 struct osc_extent *ext;
2942 CFS_LIST_HEAD(discard_list);
2943 bool unplug = false;
2947 osc_object_lock(obj);
2948 ext = osc_extent_search(obj, start);
2950 ext = first_extent(obj);
2951 else if (ext->oe_end < start)
2952 ext = next_extent(ext);
2953 while (ext != NULL) {
2954 if (ext->oe_start > end)
2957 ext->oe_fsync_wait = 1;
2958 switch (ext->oe_state) {
2960 result += ext->oe_nr_pages;
2962 cfs_list_t *list = NULL;
2964 EASSERT(!ext->oe_hp, ext);
2966 list = &obj->oo_hp_exts;
2967 } else if (!ext->oe_urgent) {
2969 list = &obj->oo_urgent_exts;
2972 cfs_list_move_tail(&ext->oe_link, list);
2975 /* the only discarder is lock cancelling, so
2976 * [start, end] must contain this extent */
2977 EASSERT(ext->oe_start >= start &&
2978 ext->oe_max_end <= end, ext);
2979 osc_extent_state_set(ext, OES_LOCKING);
2980 ext->oe_owner = current;
2981 cfs_list_move_tail(&ext->oe_link,
2983 osc_update_pending(obj, OBD_BRW_WRITE,
2988 /* It's pretty bad to wait for ACTIVE extents, because
2989 * we don't know how long we will wait for it to be
2990 * flushed since it may be blocked at awaiting more
2991 * grants. We do this for the correctness of fsync. */
2992 LASSERT(hp == 0 && discard == 0);
2996 /* this extent is being truncated, can't do anything
2997 * for it now. it will be set to urgent after truncate
2998 * is finished in osc_cache_truncate_end(). */
3002 ext = next_extent(ext);
3004 osc_object_unlock(obj);
3006 LASSERT(ergo(!discard, cfs_list_empty(&discard_list)));
3007 if (!cfs_list_empty(&discard_list)) {
3008 struct osc_extent *tmp;
3011 osc_list_maint(osc_cli(obj), obj);
3012 cfs_list_for_each_entry_safe(ext, tmp, &discard_list, oe_link) {
3013 cfs_list_del_init(&ext->oe_link);
3014 EASSERT(ext->oe_state == OES_LOCKING, ext);
3016 /* Discard caching pages. We don't actually write this
3017 * extent out but we complete it as if we did. */
3018 rc = osc_extent_make_ready(env, ext);
3019 if (unlikely(rc < 0)) {
3020 OSC_EXTENT_DUMP(D_ERROR, ext,
3021 "make_ready returned %d\n", rc);
3026 /* finish the extent as if the pages were sent */
3027 osc_extent_finish(env, ext, 0, 0);
3032 osc_io_unplug(env, osc_cli(obj), obj, PDL_POLICY_ROUND);
3034 if (hp || discard) {
3036 rc = osc_cache_wait_range(env, obj, start, end);
3037 if (result >= 0 && rc < 0)
3041 OSC_IO_DEBUG(obj, "pageout [%lu, %lu], %d.\n", start, end, result);