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 cfs_waitq_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 cfs_waitq_broadcast(&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 cfs_waitq_init(&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);
814 cfs_list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
816 cfs_list_del_init(&oap->oap_rpc_item);
817 cfs_list_del_init(&oap->oap_pending_item);
818 if (last_off <= oap->oap_obj_off) {
819 last_off = oap->oap_obj_off;
820 last_count = oap->oap_count;
824 osc_ap_completion(env, cli, oap, sent, rc);
826 EASSERT(ext->oe_nr_pages == 0, ext);
829 lost_grant = ext->oe_grants;
830 } else if (blocksize < PAGE_CACHE_SIZE &&
831 last_count != PAGE_CACHE_SIZE) {
832 /* For short writes we shouldn't count parts of pages that
833 * span a whole chunk on the OST side, or our accounting goes
834 * wrong. Should match the code in filter_grant_check. */
835 int offset = oap->oap_page_off & ~CFS_PAGE_MASK;
836 int count = oap->oap_count + (offset & (blocksize - 1));
837 int end = (offset + oap->oap_count) & (blocksize - 1);
839 count += blocksize - end;
841 lost_grant = PAGE_CACHE_SIZE - count;
843 if (ext->oe_grants > 0)
844 osc_free_grant(cli, nr_pages, lost_grant);
846 osc_extent_remove(ext);
847 /* put the refcount for RPC */
848 osc_extent_put(env, ext);
852 static int extent_wait_cb(struct osc_extent *ext, int state)
856 osc_object_lock(ext->oe_obj);
857 ret = ext->oe_state == state;
858 osc_object_unlock(ext->oe_obj);
864 * Wait for the extent's state to become @state.
866 static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
869 struct osc_object *obj = ext->oe_obj;
870 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL,
871 LWI_ON_SIGNAL_NOOP, NULL);
875 osc_object_lock(obj);
876 LASSERT(sanity_check_nolock(ext) == 0);
877 /* `Kick' this extent only if the caller is waiting for it to be
879 if (state == OES_INV && !ext->oe_urgent && !ext->oe_hp) {
880 if (ext->oe_state == OES_ACTIVE) {
882 } else if (ext->oe_state == OES_CACHE) {
884 osc_extent_hold(ext);
888 osc_object_unlock(obj);
890 osc_extent_release(env, ext);
892 /* wait for the extent until its state becomes @state */
893 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), &lwi);
894 if (rc == -ETIMEDOUT) {
895 OSC_EXTENT_DUMP(D_ERROR, ext,
896 "%s: wait ext to %d timedout, recovery in progress?\n",
897 osc_export(obj)->exp_obd->obd_name, state);
899 lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
900 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state),
903 if (rc == 0 && ext->oe_rc < 0)
909 * Discard pages with index greater than @size. If @ext is overlapped with
910 * @size, then partial truncate happens.
912 static int osc_extent_truncate(struct osc_extent *ext, pgoff_t trunc_index,
915 struct cl_env_nest nest;
918 struct osc_object *obj = ext->oe_obj;
919 struct client_obd *cli = osc_cli(obj);
920 struct osc_async_page *oap;
921 struct osc_async_page *tmp;
922 int pages_in_chunk = 0;
923 int ppc_bits = cli->cl_chunkbits -
925 __u64 trunc_chunk = trunc_index >> ppc_bits;
931 LASSERT(sanity_check(ext) == 0);
932 LASSERT(ext->oe_state == OES_TRUNC);
933 LASSERT(!ext->oe_urgent);
935 /* Request new lu_env.
936 * We can't use that env from osc_cache_truncate_start() because
937 * it's from lov_io_sub and not fully initialized. */
938 env = cl_env_nested_get(&nest);
939 io = &osc_env_info(env)->oti_io;
940 io->ci_obj = cl_object_top(osc2cl(obj));
941 rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
945 /* discard all pages with index greater then trunc_index */
946 cfs_list_for_each_entry_safe(oap, tmp, &ext->oe_pages,
948 struct cl_page *sub = oap2cl_page(oap);
949 struct cl_page *page = cl_page_top(sub);
951 LASSERT(cfs_list_empty(&oap->oap_rpc_item));
953 /* only discard the pages with their index greater than
954 * trunc_index, and ... */
955 if (sub->cp_index < trunc_index ||
956 (sub->cp_index == trunc_index && partial)) {
957 /* accounting how many pages remaining in the chunk
958 * so that we can calculate grants correctly. */
959 if (sub->cp_index >> ppc_bits == trunc_chunk)
964 cfs_list_del_init(&oap->oap_pending_item);
967 lu_ref_add(&page->cp_reference, "truncate", cfs_current());
969 if (cl_page_own(env, io, page) == 0) {
970 cl_page_unmap(env, io, page);
971 cl_page_discard(env, io, page);
972 cl_page_disown(env, io, page);
974 LASSERT(page->cp_state == CPS_FREEING);
978 lu_ref_del(&page->cp_reference, "truncate", cfs_current());
979 cl_page_put(env, page);
984 EASSERTF(ergo(ext->oe_start >= trunc_index + !!partial,
985 ext->oe_nr_pages == 0),
986 ext, "trunc_index %lu, partial %d\n", trunc_index, partial);
988 osc_object_lock(obj);
989 if (ext->oe_nr_pages == 0) {
990 LASSERT(pages_in_chunk == 0);
991 grants = ext->oe_grants;
993 } else { /* calculate how many grants we can free */
994 int chunks = (ext->oe_end >> ppc_bits) - trunc_chunk;
998 /* if there is no pages in this chunk, we can also free grants
999 * for the last chunk */
1000 if (pages_in_chunk == 0) {
1001 /* if this is the 1st chunk and no pages in this chunk,
1002 * ext->oe_nr_pages must be zero, so we should be in
1003 * the other if-clause. */
1004 LASSERT(trunc_chunk > 0);
1009 /* this is what we can free from this extent */
1010 grants = chunks << cli->cl_chunkbits;
1011 ext->oe_grants -= grants;
1012 last_index = ((trunc_chunk + 1) << ppc_bits) - 1;
1013 ext->oe_end = min(last_index, ext->oe_max_end);
1014 LASSERT(ext->oe_end >= ext->oe_start);
1015 LASSERT(ext->oe_grants > 0);
1017 osc_object_unlock(obj);
1019 if (grants > 0 || nr_pages > 0)
1020 osc_free_grant(cli, nr_pages, grants);
1023 cl_io_fini(env, io);
1024 cl_env_nested_put(&nest, env);
1029 * This function is used to make the extent prepared for transfer.
1030 * A race with flusing page - ll_writepage() has to be handled cautiously.
1032 static int osc_extent_make_ready(const struct lu_env *env,
1033 struct osc_extent *ext)
1035 struct osc_async_page *oap;
1036 struct osc_async_page *last = NULL;
1037 struct osc_object *obj = ext->oe_obj;
1042 /* we're going to grab page lock, so object lock must not be taken. */
1043 LASSERT(sanity_check(ext) == 0);
1044 /* in locking state, any process should not touch this extent. */
1045 EASSERT(ext->oe_state == OES_LOCKING, ext);
1046 EASSERT(ext->oe_owner != NULL, ext);
1048 OSC_EXTENT_DUMP(D_CACHE, ext, "make ready\n");
1050 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1052 if (last == NULL || last->oap_obj_off < oap->oap_obj_off)
1055 /* checking ASYNC_READY is race safe */
1056 if ((oap->oap_async_flags & ASYNC_READY) != 0)
1059 rc = osc_make_ready(env, oap, OBD_BRW_WRITE);
1062 spin_lock(&oap->oap_lock);
1063 oap->oap_async_flags |= ASYNC_READY;
1064 spin_unlock(&oap->oap_lock);
1067 LASSERT((oap->oap_async_flags & ASYNC_READY) != 0);
1070 LASSERTF(0, "unknown return code: %d\n", rc);
1074 LASSERT(page_count == ext->oe_nr_pages);
1075 LASSERT(last != NULL);
1076 /* the last page is the only one we need to refresh its count by
1077 * the size of file. */
1078 if (!(last->oap_async_flags & ASYNC_COUNT_STABLE)) {
1079 last->oap_count = osc_refresh_count(env, last, OBD_BRW_WRITE);
1080 LASSERT(last->oap_count > 0);
1081 LASSERT(last->oap_page_off + last->oap_count <= PAGE_CACHE_SIZE);
1082 last->oap_async_flags |= ASYNC_COUNT_STABLE;
1085 /* for the rest of pages, we don't need to call osf_refresh_count()
1086 * because it's known they are not the last page */
1087 cfs_list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1088 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE)) {
1089 oap->oap_count = PAGE_CACHE_SIZE - oap->oap_page_off;
1090 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1094 osc_object_lock(obj);
1095 osc_extent_state_set(ext, OES_RPC);
1096 osc_object_unlock(obj);
1097 /* get a refcount for RPC. */
1098 osc_extent_get(ext);
1104 * Quick and simple version of osc_extent_find(). This function is frequently
1105 * called to expand the extent for the same IO. To expand the extent, the
1106 * page index must be in the same or next chunk of ext->oe_end.
1108 static int osc_extent_expand(struct osc_extent *ext, pgoff_t index, int *grants)
1110 struct osc_object *obj = ext->oe_obj;
1111 struct client_obd *cli = osc_cli(obj);
1112 struct osc_extent *next;
1113 int ppc_bits = cli->cl_chunkbits - PAGE_CACHE_SHIFT;
1114 pgoff_t chunk = index >> ppc_bits;
1117 int chunksize = 1 << cli->cl_chunkbits;
1121 LASSERT(ext->oe_max_end >= index && ext->oe_start <= index);
1122 osc_object_lock(obj);
1123 LASSERT(sanity_check_nolock(ext) == 0);
1124 end_chunk = ext->oe_end >> ppc_bits;
1125 if (chunk > end_chunk + 1)
1126 GOTO(out, rc = -ERANGE);
1128 if (end_chunk >= chunk)
1131 LASSERT(end_chunk + 1 == chunk);
1132 /* try to expand this extent to cover @index */
1133 end_index = min(ext->oe_max_end, ((chunk + 1) << ppc_bits) - 1);
1135 next = next_extent(ext);
1136 if (next != NULL && next->oe_start <= end_index)
1137 /* complex mode - overlapped with the next extent,
1138 * this case will be handled by osc_extent_find() */
1139 GOTO(out, rc = -EAGAIN);
1141 ext->oe_end = end_index;
1142 ext->oe_grants += chunksize;
1143 *grants -= chunksize;
1144 LASSERT(*grants >= 0);
1145 EASSERTF(osc_extent_is_overlapped(obj, ext) == 0, ext,
1146 "overlapped after expanding for %lu.\n", index);
1150 osc_object_unlock(obj);
1154 static void osc_extent_tree_dump0(int level, struct osc_object *obj,
1155 const char *func, int line)
1157 struct osc_extent *ext;
1160 CDEBUG(level, "Dump object %p extents at %s:%d, mppr: %u.\n",
1161 obj, func, line, osc_cli(obj)->cl_max_pages_per_rpc);
1163 /* osc_object_lock(obj); */
1165 for (ext = first_extent(obj); ext != NULL; ext = next_extent(ext))
1166 OSC_EXTENT_DUMP(level, ext, "in tree %d.\n", cnt++);
1169 cfs_list_for_each_entry(ext, &obj->oo_hp_exts, oe_link)
1170 OSC_EXTENT_DUMP(level, ext, "hp %d.\n", cnt++);
1173 cfs_list_for_each_entry(ext, &obj->oo_urgent_exts, oe_link)
1174 OSC_EXTENT_DUMP(level, ext, "urgent %d.\n", cnt++);
1177 cfs_list_for_each_entry(ext, &obj->oo_reading_exts, oe_link)
1178 OSC_EXTENT_DUMP(level, ext, "reading %d.\n", cnt++);
1179 /* osc_object_unlock(obj); */
1182 /* ------------------ osc extent end ------------------ */
1184 static inline int osc_is_ready(struct osc_object *osc)
1186 return !cfs_list_empty(&osc->oo_ready_item) ||
1187 !cfs_list_empty(&osc->oo_hp_ready_item);
1190 #define OSC_IO_DEBUG(OSC, STR, args...) \
1191 CDEBUG(D_CACHE, "obj %p ready %d|%c|%c wr %d|%c|%c rd %d|%c " STR, \
1192 (OSC), osc_is_ready(OSC), \
1193 list_empty_marker(&(OSC)->oo_hp_ready_item), \
1194 list_empty_marker(&(OSC)->oo_ready_item), \
1195 cfs_atomic_read(&(OSC)->oo_nr_writes), \
1196 list_empty_marker(&(OSC)->oo_hp_exts), \
1197 list_empty_marker(&(OSC)->oo_urgent_exts), \
1198 cfs_atomic_read(&(OSC)->oo_nr_reads), \
1199 list_empty_marker(&(OSC)->oo_reading_exts), \
1202 static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
1205 struct osc_page *opg = oap2osc_page(oap);
1206 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1209 LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
1212 result = cl_page_make_ready(env, page, CRT_WRITE);
1214 opg->ops_submit_time = cfs_time_current();
1218 static int osc_refresh_count(const struct lu_env *env,
1219 struct osc_async_page *oap, int cmd)
1221 struct osc_page *opg = oap2osc_page(oap);
1222 struct cl_page *page = oap2cl_page(oap);
1223 struct cl_object *obj;
1224 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
1229 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
1230 LASSERT(!(cmd & OBD_BRW_READ));
1231 LASSERT(opg != NULL);
1232 obj = opg->ops_cl.cpl_obj;
1234 cl_object_attr_lock(obj);
1235 result = cl_object_attr_get(env, obj, attr);
1236 cl_object_attr_unlock(obj);
1239 kms = attr->cat_kms;
1240 if (cl_offset(obj, page->cp_index) >= kms)
1241 /* catch race with truncate */
1243 else if (cl_offset(obj, page->cp_index + 1) > kms)
1244 /* catch sub-page write at end of file */
1245 return kms % PAGE_CACHE_SIZE;
1247 return PAGE_CACHE_SIZE;
1250 static int osc_completion(const struct lu_env *env, struct osc_async_page *oap,
1253 struct osc_page *opg = oap2osc_page(oap);
1254 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1255 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
1256 enum cl_req_type crt;
1261 cmd &= ~OBD_BRW_NOQUOTA;
1262 LASSERT(equi(page->cp_state == CPS_PAGEIN, cmd == OBD_BRW_READ));
1263 LASSERT(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE));
1264 LASSERT(opg->ops_transfer_pinned);
1267 * page->cp_req can be NULL if io submission failed before
1268 * cl_req was allocated.
1270 if (page->cp_req != NULL)
1271 cl_req_page_done(env, page);
1272 LASSERT(page->cp_req == NULL);
1274 crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
1275 /* Clear opg->ops_transfer_pinned before VM lock is released. */
1276 opg->ops_transfer_pinned = 0;
1278 spin_lock(&obj->oo_seatbelt);
1279 LASSERT(opg->ops_submitter != NULL);
1280 LASSERT(!cfs_list_empty(&opg->ops_inflight));
1281 cfs_list_del_init(&opg->ops_inflight);
1282 opg->ops_submitter = NULL;
1283 spin_unlock(&obj->oo_seatbelt);
1285 opg->ops_submit_time = 0;
1286 srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK;
1289 if (rc == 0 && srvlock) {
1290 struct lu_device *ld = opg->ops_cl.cpl_obj->co_lu.lo_dev;
1291 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
1292 int bytes = oap->oap_count;
1294 if (crt == CRT_READ)
1295 stats->os_lockless_reads += bytes;
1297 stats->os_lockless_writes += bytes;
1301 * This has to be the last operation with the page, as locks are
1302 * released in cl_page_completion() and nothing except for the
1303 * reference counter protects page from concurrent reclaim.
1305 lu_ref_del(&page->cp_reference, "transfer", page);
1307 cl_page_completion(env, page, crt, rc);
1312 #define OSC_DUMP_GRANT(cli, fmt, args...) do { \
1313 struct client_obd *__tmp = (cli); \
1314 CDEBUG(D_CACHE, "%s: { dirty: %ld/%ld dirty_pages: %d/%d " \
1315 "dropped: %ld avail: %ld, reserved: %ld, flight: %d } " fmt, \
1316 __tmp->cl_import->imp_obd->obd_name, \
1317 __tmp->cl_dirty, __tmp->cl_dirty_max, \
1318 cfs_atomic_read(&obd_dirty_pages), obd_max_dirty_pages, \
1319 __tmp->cl_lost_grant, __tmp->cl_avail_grant, \
1320 __tmp->cl_reserved_grant, __tmp->cl_w_in_flight, ##args); \
1323 /* caller must hold loi_list_lock */
1324 static void osc_consume_write_grant(struct client_obd *cli,
1325 struct brw_page *pga)
1327 LASSERT_SPIN_LOCKED(&cli->cl_loi_list_lock.lock);
1328 LASSERT(!(pga->flag & OBD_BRW_FROM_GRANT));
1329 cfs_atomic_inc(&obd_dirty_pages);
1330 cli->cl_dirty += PAGE_CACHE_SIZE;
1331 pga->flag |= OBD_BRW_FROM_GRANT;
1332 CDEBUG(D_CACHE, "using %lu grant credits for brw %p page %p\n",
1333 PAGE_CACHE_SIZE, pga, pga->pg);
1334 osc_update_next_shrink(cli);
1337 /* the companion to osc_consume_write_grant, called when a brw has completed.
1338 * must be called with the loi lock held. */
1339 static void osc_release_write_grant(struct client_obd *cli,
1340 struct brw_page *pga)
1344 LASSERT_SPIN_LOCKED(&cli->cl_loi_list_lock.lock);
1345 if (!(pga->flag & OBD_BRW_FROM_GRANT)) {
1350 pga->flag &= ~OBD_BRW_FROM_GRANT;
1351 cfs_atomic_dec(&obd_dirty_pages);
1352 cli->cl_dirty -= PAGE_CACHE_SIZE;
1353 if (pga->flag & OBD_BRW_NOCACHE) {
1354 pga->flag &= ~OBD_BRW_NOCACHE;
1355 cfs_atomic_dec(&obd_dirty_transit_pages);
1356 cli->cl_dirty_transit -= PAGE_CACHE_SIZE;
1362 * To avoid sleeping with object lock held, it's good for us allocate enough
1363 * grants before entering into critical section.
1365 * client_obd_list_lock held by caller
1367 static int osc_reserve_grant(struct client_obd *cli, unsigned int bytes)
1371 if (cli->cl_avail_grant >= bytes) {
1372 cli->cl_avail_grant -= bytes;
1373 cli->cl_reserved_grant += bytes;
1379 static void __osc_unreserve_grant(struct client_obd *cli,
1380 unsigned int reserved, unsigned int unused)
1382 /* it's quite normal for us to get more grant than reserved.
1383 * Thinking about a case that two extents merged by adding a new
1384 * chunk, we can save one extent tax. If extent tax is greater than
1385 * one chunk, we can save more grant by adding a new chunk */
1386 cli->cl_reserved_grant -= reserved;
1387 if (unused > reserved) {
1388 cli->cl_avail_grant += reserved;
1389 cli->cl_lost_grant += unused - reserved;
1391 cli->cl_avail_grant += unused;
1395 void osc_unreserve_grant(struct client_obd *cli,
1396 unsigned int reserved, unsigned int unused)
1398 client_obd_list_lock(&cli->cl_loi_list_lock);
1399 __osc_unreserve_grant(cli, reserved, unused);
1401 osc_wake_cache_waiters(cli);
1402 client_obd_list_unlock(&cli->cl_loi_list_lock);
1406 * Free grant after IO is finished or canceled.
1408 * @lost_grant is used to remember how many grants we have allocated but not
1409 * used, we should return these grants to OST. There're two cases where grants
1412 * 2. blocksize at OST is less than PAGE_CACHE_SIZE and a partial page was
1413 * written. In this case OST may use less chunks to serve this partial
1414 * write. OSTs don't actually know the page size on the client side. so
1415 * clients have to calculate lost grant by the blocksize on the OST.
1416 * See filter_grant_check() for details.
1418 static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
1419 unsigned int lost_grant)
1421 int grant = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
1423 client_obd_list_lock(&cli->cl_loi_list_lock);
1424 cfs_atomic_sub(nr_pages, &obd_dirty_pages);
1425 cli->cl_dirty -= nr_pages << PAGE_CACHE_SHIFT;
1426 cli->cl_lost_grant += lost_grant;
1427 if (cli->cl_avail_grant < grant && cli->cl_lost_grant >= grant) {
1428 /* borrow some grant from truncate to avoid the case that
1429 * truncate uses up all avail grant */
1430 cli->cl_lost_grant -= grant;
1431 cli->cl_avail_grant += grant;
1433 osc_wake_cache_waiters(cli);
1434 client_obd_list_unlock(&cli->cl_loi_list_lock);
1435 CDEBUG(D_CACHE, "lost %u grant: %lu avail: %lu dirty: %lu\n",
1436 lost_grant, cli->cl_lost_grant,
1437 cli->cl_avail_grant, cli->cl_dirty);
1441 * The companion to osc_enter_cache(), called when @oap is no longer part of
1442 * the dirty accounting due to error.
1444 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1446 client_obd_list_lock(&cli->cl_loi_list_lock);
1447 osc_release_write_grant(cli, &oap->oap_brw_page);
1448 client_obd_list_unlock(&cli->cl_loi_list_lock);
1452 * Non-blocking version of osc_enter_cache() that consumes grant only when it
1455 static int osc_enter_cache_try(struct client_obd *cli,
1456 struct osc_async_page *oap,
1457 int bytes, int transient)
1461 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1463 rc = osc_reserve_grant(cli, bytes);
1467 if (cli->cl_dirty + PAGE_CACHE_SIZE <= cli->cl_dirty_max &&
1468 cfs_atomic_read(&obd_dirty_pages) + 1 <= obd_max_dirty_pages) {
1469 osc_consume_write_grant(cli, &oap->oap_brw_page);
1471 cli->cl_dirty_transit += PAGE_CACHE_SIZE;
1472 cfs_atomic_inc(&obd_dirty_transit_pages);
1473 oap->oap_brw_flags |= OBD_BRW_NOCACHE;
1477 __osc_unreserve_grant(cli, bytes, bytes);
1483 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1486 client_obd_list_lock(&cli->cl_loi_list_lock);
1487 rc = cfs_list_empty(&ocw->ocw_entry);
1488 client_obd_list_unlock(&cli->cl_loi_list_lock);
1493 * The main entry to reserve dirty page accounting. Usually the grant reserved
1494 * in this function will be freed in bulk in osc_free_grant() unless it fails
1495 * to add osc cache, in that case, it will be freed in osc_exit_cache().
1497 * The process will be put into sleep if it's already run out of grant.
1499 static int osc_enter_cache(const struct lu_env *env, struct client_obd *cli,
1500 struct osc_async_page *oap, int bytes)
1502 struct osc_object *osc = oap->oap_obj;
1503 struct lov_oinfo *loi = osc->oo_oinfo;
1504 struct osc_cache_waiter ocw;
1505 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1509 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1511 client_obd_list_lock(&cli->cl_loi_list_lock);
1513 /* force the caller to try sync io. this can jump the list
1514 * of queued writes and create a discontiguous rpc stream */
1515 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_NO_GRANT) ||
1516 cli->cl_dirty_max < PAGE_CACHE_SIZE ||
1517 cli->cl_ar.ar_force_sync || loi->loi_ar.ar_force_sync)
1518 GOTO(out, rc = -EDQUOT);
1520 /* Hopefully normal case - cache space and write credits available */
1521 if (osc_enter_cache_try(cli, oap, bytes, 0))
1524 /* We can get here for two reasons: too many dirty pages in cache, or
1525 * run out of grants. In both cases we should write dirty pages out.
1526 * Adding a cache waiter will trigger urgent write-out no matter what
1528 * The exiting condition is no avail grants and no dirty pages caching,
1529 * that really means there is no space on the OST. */
1530 cfs_waitq_init(&ocw.ocw_waitq);
1532 ocw.ocw_grant = bytes;
1533 while (cli->cl_dirty > 0 || cli->cl_w_in_flight > 0) {
1534 cfs_list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1536 client_obd_list_unlock(&cli->cl_loi_list_lock);
1538 osc_io_unplug_async(env, cli, NULL);
1540 CDEBUG(D_CACHE, "%s: sleeping for cache space @ %p for %p\n",
1541 cli->cl_import->imp_obd->obd_name, &ocw, oap);
1543 rc = l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1545 client_obd_list_lock(&cli->cl_loi_list_lock);
1547 /* l_wait_event is interrupted by signal */
1549 cfs_list_del_init(&ocw.ocw_entry);
1553 LASSERT(cfs_list_empty(&ocw.ocw_entry));
1558 if (osc_enter_cache_try(cli, oap, bytes, 0))
1563 client_obd_list_unlock(&cli->cl_loi_list_lock);
1564 OSC_DUMP_GRANT(cli, "returned %d.\n", rc);
1568 /* caller must hold loi_list_lock */
1569 void osc_wake_cache_waiters(struct client_obd *cli)
1571 cfs_list_t *l, *tmp;
1572 struct osc_cache_waiter *ocw;
1575 cfs_list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1576 ocw = cfs_list_entry(l, struct osc_cache_waiter, ocw_entry);
1577 cfs_list_del_init(&ocw->ocw_entry);
1579 ocw->ocw_rc = -EDQUOT;
1580 /* we can't dirty more */
1581 if ((cli->cl_dirty + PAGE_CACHE_SIZE > cli->cl_dirty_max) ||
1582 (cfs_atomic_read(&obd_dirty_pages) + 1 >
1583 obd_max_dirty_pages)) {
1584 CDEBUG(D_CACHE, "no dirty room: dirty: %ld "
1585 "osc max %ld, sys max %d\n", cli->cl_dirty,
1586 cli->cl_dirty_max, obd_max_dirty_pages);
1591 if (!osc_enter_cache_try(cli, ocw->ocw_oap, ocw->ocw_grant, 0))
1592 ocw->ocw_rc = -EDQUOT;
1595 CDEBUG(D_CACHE, "wake up %p for oap %p, avail grant %ld, %d\n",
1596 ocw, ocw->ocw_oap, cli->cl_avail_grant, ocw->ocw_rc);
1598 cfs_waitq_signal(&ocw->ocw_waitq);
1604 static int osc_max_rpc_in_flight(struct client_obd *cli, struct osc_object *osc)
1606 int hprpc = !!cfs_list_empty(&osc->oo_hp_exts);
1607 return rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight + hprpc;
1610 /* This maintains the lists of pending pages to read/write for a given object
1611 * (lop). This is used by osc_check_rpcs->osc_next_obj() and osc_list_maint()
1612 * to quickly find objects that are ready to send an RPC. */
1613 static int osc_makes_rpc(struct client_obd *cli, struct osc_object *osc,
1616 int invalid_import = 0;
1619 /* if we have an invalid import we want to drain the queued pages
1620 * by forcing them through rpcs that immediately fail and complete
1621 * the pages. recovery relies on this to empty the queued pages
1622 * before canceling the locks and evicting down the llite pages */
1623 if ((cli->cl_import == NULL || cli->cl_import->imp_invalid))
1626 if (cmd & OBD_BRW_WRITE) {
1627 if (cfs_atomic_read(&osc->oo_nr_writes) == 0)
1629 if (invalid_import) {
1630 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1633 if (!cfs_list_empty(&osc->oo_hp_exts)) {
1634 CDEBUG(D_CACHE, "high prio request forcing RPC\n");
1637 if (!cfs_list_empty(&osc->oo_urgent_exts)) {
1638 CDEBUG(D_CACHE, "urgent request forcing RPC\n");
1641 /* trigger a write rpc stream as long as there are dirtiers
1642 * waiting for space. as they're waiting, they're not going to
1643 * create more pages to coalesce with what's waiting.. */
1644 if (!cfs_list_empty(&cli->cl_cache_waiters)) {
1645 CDEBUG(D_CACHE, "cache waiters forcing RPC\n");
1648 if (cfs_atomic_read(&osc->oo_nr_writes) >=
1649 cli->cl_max_pages_per_rpc)
1652 if (cfs_atomic_read(&osc->oo_nr_reads) == 0)
1654 if (invalid_import) {
1655 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
1658 /* all read are urgent. */
1659 if (!cfs_list_empty(&osc->oo_reading_exts))
1666 static void osc_update_pending(struct osc_object *obj, int cmd, int delta)
1668 struct client_obd *cli = osc_cli(obj);
1669 if (cmd & OBD_BRW_WRITE) {
1670 cfs_atomic_add(delta, &obj->oo_nr_writes);
1671 cfs_atomic_add(delta, &cli->cl_pending_w_pages);
1672 LASSERT(cfs_atomic_read(&obj->oo_nr_writes) >= 0);
1674 cfs_atomic_add(delta, &obj->oo_nr_reads);
1675 cfs_atomic_add(delta, &cli->cl_pending_r_pages);
1676 LASSERT(cfs_atomic_read(&obj->oo_nr_reads) >= 0);
1678 OSC_IO_DEBUG(obj, "update pending cmd %d delta %d.\n", cmd, delta);
1681 static int osc_makes_hprpc(struct osc_object *obj)
1683 return !cfs_list_empty(&obj->oo_hp_exts);
1686 static void on_list(cfs_list_t *item, cfs_list_t *list, int should_be_on)
1688 if (cfs_list_empty(item) && should_be_on)
1689 cfs_list_add_tail(item, list);
1690 else if (!cfs_list_empty(item) && !should_be_on)
1691 cfs_list_del_init(item);
1694 /* maintain the osc's cli list membership invariants so that osc_send_oap_rpc
1695 * can find pages to build into rpcs quickly */
1696 static int __osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1698 if (osc_makes_hprpc(osc)) {
1700 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, 0);
1701 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 1);
1703 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 0);
1704 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list,
1705 osc_makes_rpc(cli, osc, OBD_BRW_WRITE) ||
1706 osc_makes_rpc(cli, osc, OBD_BRW_READ));
1709 on_list(&osc->oo_write_item, &cli->cl_loi_write_list,
1710 cfs_atomic_read(&osc->oo_nr_writes) > 0);
1712 on_list(&osc->oo_read_item, &cli->cl_loi_read_list,
1713 cfs_atomic_read(&osc->oo_nr_reads) > 0);
1715 return osc_is_ready(osc);
1718 static int osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1722 client_obd_list_lock(&cli->cl_loi_list_lock);
1723 is_ready = __osc_list_maint(cli, osc);
1724 client_obd_list_unlock(&cli->cl_loi_list_lock);
1729 /* this is trying to propogate async writeback errors back up to the
1730 * application. As an async write fails we record the error code for later if
1731 * the app does an fsync. As long as errors persist we force future rpcs to be
1732 * sync so that the app can get a sync error and break the cycle of queueing
1733 * pages for which writeback will fail. */
1734 static void osc_process_ar(struct osc_async_rc *ar, __u64 xid,
1741 ar->ar_force_sync = 1;
1742 ar->ar_min_xid = ptlrpc_sample_next_xid();
1747 if (ar->ar_force_sync && (xid >= ar->ar_min_xid))
1748 ar->ar_force_sync = 0;
1751 /* this must be called holding the loi list lock to give coverage to exit_cache,
1752 * async_flag maintenance, and oap_request */
1753 static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
1754 struct osc_async_page *oap, int sent, int rc)
1756 struct osc_object *osc = oap->oap_obj;
1757 struct lov_oinfo *loi = osc->oo_oinfo;
1761 if (oap->oap_request != NULL) {
1762 xid = ptlrpc_req_xid(oap->oap_request);
1763 ptlrpc_req_finished(oap->oap_request);
1764 oap->oap_request = NULL;
1767 /* As the transfer for this page is being done, clear the flags */
1768 spin_lock(&oap->oap_lock);
1769 oap->oap_async_flags = 0;
1770 spin_unlock(&oap->oap_lock);
1771 oap->oap_interrupted = 0;
1773 if (oap->oap_cmd & OBD_BRW_WRITE && xid > 0) {
1774 client_obd_list_lock(&cli->cl_loi_list_lock);
1775 osc_process_ar(&cli->cl_ar, xid, rc);
1776 osc_process_ar(&loi->loi_ar, xid, rc);
1777 client_obd_list_unlock(&cli->cl_loi_list_lock);
1780 rc = osc_completion(env, oap, oap->oap_cmd, rc);
1782 CERROR("completion on oap %p obj %p returns %d.\n",
1789 * Try to add extent to one RPC. We need to think about the following things:
1790 * - # of pages must not be over max_pages_per_rpc
1791 * - extent must be compatible with previous ones
1793 static int try_to_add_extent_for_io(struct client_obd *cli,
1794 struct osc_extent *ext, cfs_list_t *rpclist,
1795 int *pc, unsigned int *max_pages)
1797 struct osc_extent *tmp;
1800 EASSERT((ext->oe_state == OES_CACHE || ext->oe_state == OES_LOCK_DONE),
1803 *max_pages = max(ext->oe_mppr, *max_pages);
1804 if (*pc + ext->oe_nr_pages > *max_pages)
1807 cfs_list_for_each_entry(tmp, rpclist, oe_link) {
1808 EASSERT(tmp->oe_owner == cfs_current(), tmp);
1810 if (overlapped(tmp, ext)) {
1811 OSC_EXTENT_DUMP(D_ERROR, tmp, "overlapped %p.\n", ext);
1816 if (tmp->oe_srvlock != ext->oe_srvlock ||
1817 !tmp->oe_grants != !ext->oe_grants)
1820 /* remove break for strict check */
1824 *pc += ext->oe_nr_pages;
1825 cfs_list_move_tail(&ext->oe_link, rpclist);
1826 ext->oe_owner = cfs_current();
1831 * In order to prevent multiple ptlrpcd from breaking contiguous extents,
1832 * get_write_extent() takes all appropriate extents in atomic.
1834 * The following policy is used to collect extents for IO:
1835 * 1. Add as many HP extents as possible;
1836 * 2. Add the first urgent extent in urgent extent list and take it out of
1838 * 3. Add subsequent extents of this urgent extent;
1839 * 4. If urgent list is not empty, goto 2;
1840 * 5. Traverse the extent tree from the 1st extent;
1841 * 6. Above steps exit if there is no space in this RPC.
1843 static int get_write_extents(struct osc_object *obj, cfs_list_t *rpclist)
1845 struct client_obd *cli = osc_cli(obj);
1846 struct osc_extent *ext;
1848 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1850 LASSERT(osc_object_is_locked(obj));
1851 while (!cfs_list_empty(&obj->oo_hp_exts)) {
1852 ext = cfs_list_entry(obj->oo_hp_exts.next, struct osc_extent,
1854 LASSERT(ext->oe_state == OES_CACHE);
1855 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1858 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1860 if (page_count == max_pages)
1863 while (!cfs_list_empty(&obj->oo_urgent_exts)) {
1864 ext = cfs_list_entry(obj->oo_urgent_exts.next,
1865 struct osc_extent, oe_link);
1866 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1870 if (!ext->oe_intree)
1873 while ((ext = next_extent(ext)) != NULL) {
1874 if ((ext->oe_state != OES_CACHE) ||
1875 (!cfs_list_empty(&ext->oe_link) &&
1876 ext->oe_owner != NULL))
1879 if (!try_to_add_extent_for_io(cli, ext, rpclist,
1880 &page_count, &max_pages))
1884 if (page_count == max_pages)
1887 ext = first_extent(obj);
1888 while (ext != NULL) {
1889 if ((ext->oe_state != OES_CACHE) ||
1890 /* this extent may be already in current rpclist */
1891 (!cfs_list_empty(&ext->oe_link) && ext->oe_owner != NULL)) {
1892 ext = next_extent(ext);
1896 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1900 ext = next_extent(ext);
1906 osc_send_write_rpc(const struct lu_env *env, struct client_obd *cli,
1907 struct osc_object *osc, pdl_policy_t pol)
1909 CFS_LIST_HEAD(rpclist);
1910 struct osc_extent *ext;
1911 struct osc_extent *tmp;
1912 struct osc_extent *first = NULL;
1913 obd_count page_count = 0;
1918 LASSERT(osc_object_is_locked(osc));
1920 page_count = get_write_extents(osc, &rpclist);
1921 LASSERT(equi(page_count == 0, cfs_list_empty(&rpclist)));
1923 if (cfs_list_empty(&rpclist))
1926 osc_update_pending(osc, OBD_BRW_WRITE, -page_count);
1928 cfs_list_for_each_entry(ext, &rpclist, oe_link) {
1929 LASSERT(ext->oe_state == OES_CACHE ||
1930 ext->oe_state == OES_LOCK_DONE);
1931 if (ext->oe_state == OES_CACHE)
1932 osc_extent_state_set(ext, OES_LOCKING);
1934 osc_extent_state_set(ext, OES_RPC);
1937 /* we're going to grab page lock, so release object lock because
1938 * lock order is page lock -> object lock. */
1939 osc_object_unlock(osc);
1941 cfs_list_for_each_entry_safe(ext, tmp, &rpclist, oe_link) {
1942 if (ext->oe_state == OES_LOCKING) {
1943 rc = osc_extent_make_ready(env, ext);
1944 if (unlikely(rc < 0)) {
1945 cfs_list_del_init(&ext->oe_link);
1946 osc_extent_finish(env, ext, 0, rc);
1950 if (first == NULL) {
1952 srvlock = ext->oe_srvlock;
1954 LASSERT(srvlock == ext->oe_srvlock);
1958 if (!cfs_list_empty(&rpclist)) {
1959 LASSERT(page_count > 0);
1960 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_WRITE, pol);
1961 LASSERT(cfs_list_empty(&rpclist));
1964 osc_object_lock(osc);
1969 * prepare pages for ASYNC io and put pages in send queue.
1971 * \param cmd OBD_BRW_* macroses
1972 * \param lop pending pages
1974 * \return zero if no page added to send queue.
1975 * \return 1 if pages successfully added to send queue.
1976 * \return negative on errors.
1979 osc_send_read_rpc(const struct lu_env *env, struct client_obd *cli,
1980 struct osc_object *osc, pdl_policy_t pol)
1982 struct osc_extent *ext;
1983 struct osc_extent *next;
1984 CFS_LIST_HEAD(rpclist);
1986 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1990 LASSERT(osc_object_is_locked(osc));
1991 cfs_list_for_each_entry_safe(ext, next,
1992 &osc->oo_reading_exts, oe_link) {
1993 EASSERT(ext->oe_state == OES_LOCK_DONE, ext);
1994 if (!try_to_add_extent_for_io(cli, ext, &rpclist, &page_count,
1997 osc_extent_state_set(ext, OES_RPC);
1998 EASSERT(ext->oe_nr_pages <= max_pages, ext);
2000 LASSERT(page_count <= max_pages);
2002 osc_update_pending(osc, OBD_BRW_READ, -page_count);
2004 if (!cfs_list_empty(&rpclist)) {
2005 osc_object_unlock(osc);
2007 LASSERT(page_count > 0);
2008 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_READ, pol);
2009 LASSERT(cfs_list_empty(&rpclist));
2011 osc_object_lock(osc);
2016 #define list_to_obj(list, item) ({ \
2017 cfs_list_t *__tmp = (list)->next; \
2018 cfs_list_del_init(__tmp); \
2019 cfs_list_entry(__tmp, struct osc_object, oo_##item); \
2022 /* This is called by osc_check_rpcs() to find which objects have pages that
2023 * we could be sending. These lists are maintained by osc_makes_rpc(). */
2024 static struct osc_object *osc_next_obj(struct client_obd *cli)
2028 /* First return objects that have blocked locks so that they
2029 * will be flushed quickly and other clients can get the lock,
2030 * then objects which have pages ready to be stuffed into RPCs */
2031 if (!cfs_list_empty(&cli->cl_loi_hp_ready_list))
2032 RETURN(list_to_obj(&cli->cl_loi_hp_ready_list, hp_ready_item));
2033 if (!cfs_list_empty(&cli->cl_loi_ready_list))
2034 RETURN(list_to_obj(&cli->cl_loi_ready_list, ready_item));
2036 /* then if we have cache waiters, return all objects with queued
2037 * writes. This is especially important when many small files
2038 * have filled up the cache and not been fired into rpcs because
2039 * they don't pass the nr_pending/object threshhold */
2040 if (!cfs_list_empty(&cli->cl_cache_waiters) &&
2041 !cfs_list_empty(&cli->cl_loi_write_list))
2042 RETURN(list_to_obj(&cli->cl_loi_write_list, write_item));
2044 /* then return all queued objects when we have an invalid import
2045 * so that they get flushed */
2046 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
2047 if (!cfs_list_empty(&cli->cl_loi_write_list))
2048 RETURN(list_to_obj(&cli->cl_loi_write_list,
2050 if (!cfs_list_empty(&cli->cl_loi_read_list))
2051 RETURN(list_to_obj(&cli->cl_loi_read_list,
2057 /* called with the loi list lock held */
2058 static void osc_check_rpcs(const struct lu_env *env, struct client_obd *cli,
2061 struct osc_object *osc;
2065 while ((osc = osc_next_obj(cli)) != NULL) {
2066 struct cl_object *obj = osc2cl(osc);
2067 struct lu_ref_link link;
2069 OSC_IO_DEBUG(osc, "%lu in flight\n", rpcs_in_flight(cli));
2071 if (osc_max_rpc_in_flight(cli, osc)) {
2072 __osc_list_maint(cli, osc);
2077 client_obd_list_unlock(&cli->cl_loi_list_lock);
2078 lu_object_ref_add_at(&obj->co_lu, &link, "check",
2081 /* attempt some read/write balancing by alternating between
2082 * reads and writes in an object. The makes_rpc checks here
2083 * would be redundant if we were getting read/write work items
2084 * instead of objects. we don't want send_oap_rpc to drain a
2085 * partial read pending queue when we're given this object to
2086 * do io on writes while there are cache waiters */
2087 osc_object_lock(osc);
2088 if (osc_makes_rpc(cli, osc, OBD_BRW_WRITE)) {
2089 rc = osc_send_write_rpc(env, cli, osc, pol);
2091 CERROR("Write request failed with %d\n", rc);
2093 /* osc_send_write_rpc failed, mostly because of
2096 * It can't break here, because if:
2097 * - a page was submitted by osc_io_submit, so
2099 * - no request in flight
2100 * - no subsequent request
2101 * The system will be in live-lock state,
2102 * because there is no chance to call
2103 * osc_io_unplug() and osc_check_rpcs() any
2104 * more. pdflush can't help in this case,
2105 * because it might be blocked at grabbing
2106 * the page lock as we mentioned.
2108 * Anyway, continue to drain pages. */
2112 if (osc_makes_rpc(cli, osc, OBD_BRW_READ)) {
2113 rc = osc_send_read_rpc(env, cli, osc, pol);
2115 CERROR("Read request failed with %d\n", rc);
2117 osc_object_unlock(osc);
2119 osc_list_maint(cli, osc);
2120 lu_object_ref_del_at(&obj->co_lu, &link, "check",
2122 cl_object_put(env, obj);
2124 client_obd_list_lock(&cli->cl_loi_list_lock);
2128 static int osc_io_unplug0(const struct lu_env *env, struct client_obd *cli,
2129 struct osc_object *osc, pdl_policy_t pol, int async)
2133 if (osc != NULL && osc_list_maint(cli, osc) == 0)
2137 /* disable osc_lru_shrink() temporarily to avoid
2138 * potential stack overrun problem. LU-2859 */
2139 cfs_atomic_inc(&cli->cl_lru_shrinkers);
2140 client_obd_list_lock(&cli->cl_loi_list_lock);
2141 osc_check_rpcs(env, cli, pol);
2142 client_obd_list_unlock(&cli->cl_loi_list_lock);
2143 cfs_atomic_dec(&cli->cl_lru_shrinkers);
2145 CDEBUG(D_CACHE, "Queue writeback work for client %p.\n", cli);
2146 LASSERT(cli->cl_writeback_work != NULL);
2147 rc = ptlrpcd_queue_work(cli->cl_writeback_work);
2152 static int osc_io_unplug_async(const struct lu_env *env,
2153 struct client_obd *cli, struct osc_object *osc)
2155 /* XXX: policy is no use actually. */
2156 return osc_io_unplug0(env, cli, osc, PDL_POLICY_ROUND, 1);
2159 void osc_io_unplug(const struct lu_env *env, struct client_obd *cli,
2160 struct osc_object *osc, pdl_policy_t pol)
2162 (void)osc_io_unplug0(env, cli, osc, pol, 0);
2165 int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops,
2166 struct page *page, loff_t offset)
2168 struct obd_export *exp = osc_export(osc);
2169 struct osc_async_page *oap = &ops->ops_oap;
2173 return cfs_size_round(sizeof(*oap));
2175 oap->oap_magic = OAP_MAGIC;
2176 oap->oap_cli = &exp->exp_obd->u.cli;
2179 oap->oap_page = page;
2180 oap->oap_obj_off = offset;
2181 LASSERT(!(offset & ~CFS_PAGE_MASK));
2183 if (!client_is_remote(exp) && cfs_capable(CFS_CAP_SYS_RESOURCE))
2184 oap->oap_brw_flags = OBD_BRW_NOQUOTA;
2186 CFS_INIT_LIST_HEAD(&oap->oap_pending_item);
2187 CFS_INIT_LIST_HEAD(&oap->oap_rpc_item);
2189 spin_lock_init(&oap->oap_lock);
2190 CDEBUG(D_INFO, "oap %p page %p obj off "LPU64"\n",
2191 oap, page, oap->oap_obj_off);
2195 int osc_queue_async_io(const struct lu_env *env, struct cl_io *io,
2196 struct osc_page *ops)
2198 struct osc_io *oio = osc_env_io(env);
2199 struct osc_extent *ext = NULL;
2200 struct osc_async_page *oap = &ops->ops_oap;
2201 struct client_obd *cli = oap->oap_cli;
2202 struct osc_object *osc = oap->oap_obj;
2205 int brw_flags = OBD_BRW_ASYNC;
2206 int cmd = OBD_BRW_WRITE;
2207 int need_release = 0;
2211 if (oap->oap_magic != OAP_MAGIC)
2214 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2217 if (!cfs_list_empty(&oap->oap_pending_item) ||
2218 !cfs_list_empty(&oap->oap_rpc_item))
2221 /* Set the OBD_BRW_SRVLOCK before the page is queued. */
2222 brw_flags |= ops->ops_srvlock ? OBD_BRW_SRVLOCK : 0;
2223 if (!client_is_remote(osc_export(osc)) &&
2224 cfs_capable(CFS_CAP_SYS_RESOURCE)) {
2225 brw_flags |= OBD_BRW_NOQUOTA;
2226 cmd |= OBD_BRW_NOQUOTA;
2229 /* check if the file's owner/group is over quota */
2230 if (!(cmd & OBD_BRW_NOQUOTA)) {
2231 struct cl_object *obj;
2232 struct cl_attr *attr;
2233 unsigned int qid[MAXQUOTAS];
2235 obj = cl_object_top(&osc->oo_cl);
2236 attr = &osc_env_info(env)->oti_attr;
2238 cl_object_attr_lock(obj);
2239 rc = cl_object_attr_get(env, obj, attr);
2240 cl_object_attr_unlock(obj);
2242 qid[USRQUOTA] = attr->cat_uid;
2243 qid[GRPQUOTA] = attr->cat_gid;
2244 if (rc == 0 && osc_quota_chkdq(cli, qid) == NO_QUOTA)
2251 oap->oap_page_off = ops->ops_from;
2252 oap->oap_count = ops->ops_to - ops->ops_from;
2253 oap->oap_async_flags = 0;
2254 oap->oap_brw_flags = brw_flags;
2256 OSC_IO_DEBUG(osc, "oap %p page %p added for cmd %d\n",
2257 oap, oap->oap_page, oap->oap_cmd & OBD_BRW_RWMASK);
2259 index = oap2cl_page(oap)->cp_index;
2261 /* Add this page into extent by the following steps:
2262 * 1. if there exists an active extent for this IO, mostly this page
2263 * can be added to the active extent and sometimes we need to
2264 * expand extent to accomodate this page;
2265 * 2. otherwise, a new extent will be allocated. */
2267 ext = oio->oi_active;
2268 if (ext != NULL && ext->oe_start <= index && ext->oe_max_end >= index) {
2269 /* one chunk plus extent overhead must be enough to write this
2271 grants = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2272 if (ext->oe_end >= index)
2275 /* it doesn't need any grant to dirty this page */
2276 client_obd_list_lock(&cli->cl_loi_list_lock);
2277 rc = osc_enter_cache_try(cli, oap, grants, 0);
2278 client_obd_list_unlock(&cli->cl_loi_list_lock);
2279 if (rc == 0) { /* try failed */
2282 } else if (ext->oe_end < index) {
2284 /* try to expand this extent */
2285 rc = osc_extent_expand(ext, index, &tmp);
2288 /* don't free reserved grant */
2290 OSC_EXTENT_DUMP(D_CACHE, ext,
2291 "expanded for %lu.\n", index);
2292 osc_unreserve_grant(cli, grants, tmp);
2297 } else if (ext != NULL) {
2298 /* index is located outside of active extent */
2302 osc_extent_release(env, ext);
2303 oio->oi_active = NULL;
2308 int tmp = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2310 /* try to find new extent to cover this page */
2311 LASSERT(oio->oi_active == NULL);
2312 /* we may have allocated grant for this page if we failed
2313 * to expand the previous active extent. */
2314 LASSERT(ergo(grants > 0, grants >= tmp));
2318 /* we haven't allocated grant for this page. */
2319 rc = osc_enter_cache(env, cli, oap, tmp);
2326 ext = osc_extent_find(env, osc, index, &tmp);
2328 LASSERT(tmp == grants);
2329 osc_exit_cache(cli, oap);
2333 oio->oi_active = ext;
2337 osc_unreserve_grant(cli, grants, tmp);
2340 LASSERT(ergo(rc == 0, ext != NULL));
2342 EASSERTF(ext->oe_end >= index && ext->oe_start <= index,
2343 ext, "index = %lu.\n", index);
2344 LASSERT((oap->oap_brw_flags & OBD_BRW_FROM_GRANT) != 0);
2346 osc_object_lock(osc);
2347 if (ext->oe_nr_pages == 0)
2348 ext->oe_srvlock = ops->ops_srvlock;
2350 LASSERT(ext->oe_srvlock == ops->ops_srvlock);
2352 cfs_list_add_tail(&oap->oap_pending_item, &ext->oe_pages);
2353 osc_object_unlock(osc);
2358 int osc_teardown_async_page(const struct lu_env *env,
2359 struct osc_object *obj, struct osc_page *ops)
2361 struct osc_async_page *oap = &ops->ops_oap;
2362 struct osc_extent *ext = NULL;
2366 LASSERT(oap->oap_magic == OAP_MAGIC);
2368 CDEBUG(D_INFO, "teardown oap %p page %p at index %lu.\n",
2369 oap, ops, oap2cl_page(oap)->cp_index);
2371 osc_object_lock(obj);
2372 if (!cfs_list_empty(&oap->oap_rpc_item)) {
2373 CDEBUG(D_CACHE, "oap %p is not in cache.\n", oap);
2375 } else if (!cfs_list_empty(&oap->oap_pending_item)) {
2376 ext = osc_extent_lookup(obj, oap2cl_page(oap)->cp_index);
2377 /* only truncated pages are allowed to be taken out.
2378 * See osc_extent_truncate() and osc_cache_truncate_start()
2380 if (ext != NULL && ext->oe_state != OES_TRUNC) {
2381 OSC_EXTENT_DUMP(D_ERROR, ext, "trunc at %lu.\n",
2382 oap2cl_page(oap)->cp_index);
2386 osc_object_unlock(obj);
2388 osc_extent_put(env, ext);
2393 * This is called when a page is picked up by kernel to write out.
2395 * We should find out the corresponding extent and add the whole extent
2396 * into urgent list. The extent may be being truncated or used, handle it
2399 int osc_flush_async_page(const struct lu_env *env, struct cl_io *io,
2400 struct osc_page *ops)
2402 struct osc_extent *ext = NULL;
2403 struct osc_object *obj = cl2osc(ops->ops_cl.cpl_obj);
2404 struct cl_page *cp = ops->ops_cl.cpl_page;
2405 pgoff_t index = cp->cp_index;
2406 struct osc_async_page *oap = &ops->ops_oap;
2407 bool unplug = false;
2411 osc_object_lock(obj);
2412 ext = osc_extent_lookup(obj, index);
2414 osc_extent_tree_dump(D_ERROR, obj);
2415 LASSERTF(0, "page index %lu is NOT covered.\n", index);
2418 switch (ext->oe_state) {
2421 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(cp),
2422 "flush an in-rpc page?\n");
2426 /* If we know this extent is being written out, we should abort
2427 * so that the writer can make this page ready. Otherwise, there
2428 * exists a deadlock problem because other process can wait for
2429 * page writeback bit holding page lock; and meanwhile in
2430 * vvp_page_make_ready(), we need to grab page lock before
2431 * really sending the RPC. */
2433 /* race with truncate, page will be redirtied */
2434 GOTO(out, rc = -EAGAIN);
2439 rc = cl_page_prep(env, io, cl_page_top(cp), CRT_WRITE);
2443 spin_lock(&oap->oap_lock);
2444 oap->oap_async_flags |= ASYNC_READY|ASYNC_URGENT;
2445 spin_unlock(&oap->oap_lock);
2447 if (memory_pressure_get())
2448 ext->oe_memalloc = 1;
2451 if (ext->oe_state == OES_CACHE) {
2452 OSC_EXTENT_DUMP(D_CACHE, ext,
2453 "flush page %p make it urgent.\n", oap);
2454 if (cfs_list_empty(&ext->oe_link))
2455 cfs_list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2462 osc_object_unlock(obj);
2463 osc_extent_put(env, ext);
2465 osc_io_unplug_async(env, osc_cli(obj), obj);
2470 * this is called when a sync waiter receives an interruption. Its job is to
2471 * get the caller woken as soon as possible. If its page hasn't been put in an
2472 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
2473 * desiring interruption which will forcefully complete the rpc once the rpc
2476 int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops)
2478 struct osc_async_page *oap = &ops->ops_oap;
2479 struct osc_object *obj = oap->oap_obj;
2480 struct client_obd *cli = osc_cli(obj);
2481 struct osc_extent *ext;
2482 struct osc_extent *found = NULL;
2484 pgoff_t index = oap2cl_page(oap)->cp_index;
2489 LASSERT(!oap->oap_interrupted);
2490 oap->oap_interrupted = 1;
2492 /* Find out the caching extent */
2493 osc_object_lock(obj);
2494 if (oap->oap_cmd & OBD_BRW_WRITE) {
2495 plist = &obj->oo_urgent_exts;
2496 cmd = OBD_BRW_WRITE;
2498 plist = &obj->oo_reading_exts;
2501 cfs_list_for_each_entry(ext, plist, oe_link) {
2502 if (ext->oe_start <= index && ext->oe_end >= index) {
2503 LASSERT(ext->oe_state == OES_LOCK_DONE);
2504 /* For OES_LOCK_DONE state extent, it has already held
2505 * a refcount for RPC. */
2506 found = osc_extent_get(ext);
2510 if (found != NULL) {
2511 cfs_list_del_init(&found->oe_link);
2512 osc_update_pending(obj, cmd, -found->oe_nr_pages);
2513 osc_object_unlock(obj);
2515 osc_extent_finish(env, found, 0, -EINTR);
2516 osc_extent_put(env, found);
2519 osc_object_unlock(obj);
2520 /* ok, it's been put in an rpc. only one oap gets a request
2522 if (oap->oap_request != NULL) {
2523 ptlrpc_mark_interrupted(oap->oap_request);
2524 ptlrpcd_wake(oap->oap_request);
2525 ptlrpc_req_finished(oap->oap_request);
2526 oap->oap_request = NULL;
2530 osc_list_maint(cli, obj);
2534 int osc_queue_sync_pages(const struct lu_env *env, struct osc_object *obj,
2535 cfs_list_t *list, int cmd, int brw_flags)
2537 struct client_obd *cli = osc_cli(obj);
2538 struct osc_extent *ext;
2539 struct osc_async_page *oap;
2541 int mppr = cli->cl_max_pages_per_rpc;
2542 pgoff_t start = CL_PAGE_EOF;
2546 cfs_list_for_each_entry(oap, list, oap_pending_item) {
2547 struct cl_page *cp = oap2cl_page(oap);
2548 if (cp->cp_index > end)
2550 if (cp->cp_index < start)
2551 start = cp->cp_index;
2553 mppr <<= (page_count > mppr);
2556 ext = osc_extent_alloc(obj);
2558 cfs_list_for_each_entry(oap, list, oap_pending_item) {
2559 cfs_list_del_init(&oap->oap_pending_item);
2560 osc_ap_completion(env, cli, oap, 0, -ENOMEM);
2565 ext->oe_rw = !!(cmd & OBD_BRW_READ);
2567 ext->oe_start = start;
2568 ext->oe_end = ext->oe_max_end = end;
2570 ext->oe_srvlock = !!(brw_flags & OBD_BRW_SRVLOCK);
2571 ext->oe_nr_pages = page_count;
2572 ext->oe_mppr = mppr;
2573 cfs_list_splice_init(list, &ext->oe_pages);
2575 osc_object_lock(obj);
2576 /* Reuse the initial refcount for RPC, don't drop it */
2577 osc_extent_state_set(ext, OES_LOCK_DONE);
2578 if (cmd & OBD_BRW_WRITE) {
2579 cfs_list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2580 osc_update_pending(obj, OBD_BRW_WRITE, page_count);
2582 cfs_list_add_tail(&ext->oe_link, &obj->oo_reading_exts);
2583 osc_update_pending(obj, OBD_BRW_READ, page_count);
2585 osc_object_unlock(obj);
2587 osc_io_unplug(env, cli, obj, PDL_POLICY_ROUND);
2592 * Called by osc_io_setattr_start() to freeze and destroy covering extents.
2594 int osc_cache_truncate_start(const struct lu_env *env, struct osc_io *oio,
2595 struct osc_object *obj, __u64 size)
2597 struct client_obd *cli = osc_cli(obj);
2598 struct osc_extent *ext;
2599 struct osc_extent *waiting = NULL;
2601 CFS_LIST_HEAD(list);
2606 /* pages with index greater or equal to index will be truncated. */
2607 index = cl_index(osc2cl(obj), size);
2608 partial = size > cl_offset(osc2cl(obj), index);
2611 osc_object_lock(obj);
2612 ext = osc_extent_search(obj, index);
2614 ext = first_extent(obj);
2615 else if (ext->oe_end < index)
2616 ext = next_extent(ext);
2617 while (ext != NULL) {
2618 EASSERT(ext->oe_state != OES_TRUNC, ext);
2620 if (ext->oe_state > OES_CACHE || ext->oe_urgent) {
2621 /* if ext is in urgent state, it means there must exist
2622 * a page already having been flushed by write_page().
2623 * We have to wait for this extent because we can't
2624 * truncate that page. */
2625 LASSERT(!ext->oe_hp);
2626 OSC_EXTENT_DUMP(D_CACHE, ext,
2627 "waiting for busy extent\n");
2628 waiting = osc_extent_get(ext);
2632 OSC_EXTENT_DUMP(D_CACHE, ext, "try to trunc:"LPU64".\n", size);
2634 osc_extent_get(ext);
2635 if (ext->oe_state == OES_ACTIVE) {
2636 /* though we grab inode mutex for write path, but we
2637 * release it before releasing extent(in osc_io_end()),
2638 * so there is a race window that an extent is still
2639 * in OES_ACTIVE when truncate starts. */
2640 LASSERT(!ext->oe_trunc_pending);
2641 ext->oe_trunc_pending = 1;
2643 EASSERT(ext->oe_state == OES_CACHE, ext);
2644 osc_extent_state_set(ext, OES_TRUNC);
2645 osc_update_pending(obj, OBD_BRW_WRITE,
2648 EASSERT(cfs_list_empty(&ext->oe_link), ext);
2649 cfs_list_add_tail(&ext->oe_link, &list);
2651 ext = next_extent(ext);
2653 osc_object_unlock(obj);
2655 osc_list_maint(cli, obj);
2657 while (!cfs_list_empty(&list)) {
2660 ext = cfs_list_entry(list.next, struct osc_extent, oe_link);
2661 cfs_list_del_init(&ext->oe_link);
2663 /* extent may be in OES_ACTIVE state because inode mutex
2664 * is released before osc_io_end() in file write case */
2665 if (ext->oe_state != OES_TRUNC)
2666 osc_extent_wait(env, ext, OES_TRUNC);
2668 rc = osc_extent_truncate(ext, index, partial);
2673 OSC_EXTENT_DUMP(D_ERROR, ext,
2674 "truncate error %d\n", rc);
2675 } else if (ext->oe_nr_pages == 0) {
2676 osc_extent_remove(ext);
2678 /* this must be an overlapped extent which means only
2679 * part of pages in this extent have been truncated.
2681 EASSERTF(ext->oe_start <= index, ext,
2682 "trunc index = %lu/%d.\n", index, partial);
2683 /* fix index to skip this partially truncated extent */
2684 index = ext->oe_end + 1;
2687 /* we need to hold this extent in OES_TRUNC state so
2688 * that no writeback will happen. This is to avoid
2690 LASSERT(oio->oi_trunc == NULL);
2691 oio->oi_trunc = osc_extent_get(ext);
2692 OSC_EXTENT_DUMP(D_CACHE, ext,
2693 "trunc at "LPU64"\n", size);
2695 osc_extent_put(env, ext);
2697 if (waiting != NULL) {
2700 /* ignore the result of osc_extent_wait the write initiator
2701 * should take care of it. */
2702 rc = osc_extent_wait(env, waiting, OES_INV);
2704 OSC_EXTENT_DUMP(D_CACHE, waiting, "error: %d.\n", rc);
2706 osc_extent_put(env, waiting);
2714 * Called after osc_io_setattr_end to add oio->oi_trunc back to cache.
2716 void osc_cache_truncate_end(const struct lu_env *env, struct osc_io *oio,
2717 struct osc_object *obj)
2719 struct osc_extent *ext = oio->oi_trunc;
2721 oio->oi_trunc = NULL;
2723 bool unplug = false;
2725 EASSERT(ext->oe_nr_pages > 0, ext);
2726 EASSERT(ext->oe_state == OES_TRUNC, ext);
2727 EASSERT(!ext->oe_urgent, ext);
2729 OSC_EXTENT_DUMP(D_CACHE, ext, "trunc -> cache.\n");
2730 osc_object_lock(obj);
2731 osc_extent_state_set(ext, OES_CACHE);
2732 if (ext->oe_fsync_wait && !ext->oe_urgent) {
2734 cfs_list_move_tail(&ext->oe_link, &obj->oo_urgent_exts);
2737 osc_update_pending(obj, OBD_BRW_WRITE, ext->oe_nr_pages);
2738 osc_object_unlock(obj);
2739 osc_extent_put(env, ext);
2742 osc_io_unplug_async(env, osc_cli(obj), obj);
2747 * Wait for extents in a specific range to be written out.
2748 * The caller must have called osc_cache_writeback_range() to issue IO
2749 * otherwise it will take a long time for this function to finish.
2751 * Caller must hold inode_mutex , or cancel exclusive dlm lock so that
2752 * nobody else can dirty this range of file while we're waiting for
2753 * extents to be written.
2755 int osc_cache_wait_range(const struct lu_env *env, struct osc_object *obj,
2756 pgoff_t start, pgoff_t end)
2758 struct osc_extent *ext;
2759 pgoff_t index = start;
2764 osc_object_lock(obj);
2765 ext = osc_extent_search(obj, index);
2767 ext = first_extent(obj);
2768 else if (ext->oe_end < index)
2769 ext = next_extent(ext);
2770 while (ext != NULL) {
2773 if (ext->oe_start > end)
2776 if (!ext->oe_fsync_wait) {
2777 ext = next_extent(ext);
2781 EASSERT(ergo(ext->oe_state == OES_CACHE,
2782 ext->oe_hp || ext->oe_urgent), ext);
2783 EASSERT(ergo(ext->oe_state == OES_ACTIVE,
2784 !ext->oe_hp && ext->oe_urgent), ext);
2786 index = ext->oe_end + 1;
2787 osc_extent_get(ext);
2788 osc_object_unlock(obj);
2790 rc = osc_extent_wait(env, ext, OES_INV);
2793 osc_extent_put(env, ext);
2796 osc_object_unlock(obj);
2798 OSC_IO_DEBUG(obj, "sync file range.\n");
2803 * Called to write out a range of osc object.
2805 * @hp : should be set this is caused by lock cancel;
2806 * @discard: is set if dirty pages should be dropped - file will be deleted or
2807 * truncated, this implies there is no partially discarding extents.
2809 * Return how many pages will be issued, or error code if error occurred.
2811 int osc_cache_writeback_range(const struct lu_env *env, struct osc_object *obj,
2812 pgoff_t start, pgoff_t end, int hp, int discard)
2814 struct osc_extent *ext;
2815 CFS_LIST_HEAD(discard_list);
2816 bool unplug = false;
2820 osc_object_lock(obj);
2821 ext = osc_extent_search(obj, start);
2823 ext = first_extent(obj);
2824 else if (ext->oe_end < start)
2825 ext = next_extent(ext);
2826 while (ext != NULL) {
2827 if (ext->oe_start > end)
2830 ext->oe_fsync_wait = 1;
2831 switch (ext->oe_state) {
2833 result += ext->oe_nr_pages;
2835 cfs_list_t *list = NULL;
2837 EASSERT(!ext->oe_hp, ext);
2839 list = &obj->oo_hp_exts;
2840 } else if (!ext->oe_urgent) {
2842 list = &obj->oo_urgent_exts;
2845 cfs_list_move_tail(&ext->oe_link, list);
2848 /* the only discarder is lock cancelling, so
2849 * [start, end] must contain this extent */
2850 EASSERT(ext->oe_start >= start &&
2851 ext->oe_max_end <= end, ext);
2852 osc_extent_state_set(ext, OES_LOCKING);
2853 ext->oe_owner = cfs_current();
2854 cfs_list_move_tail(&ext->oe_link,
2856 osc_update_pending(obj, OBD_BRW_WRITE,
2861 /* It's pretty bad to wait for ACTIVE extents, because
2862 * we don't know how long we will wait for it to be
2863 * flushed since it may be blocked at awaiting more
2864 * grants. We do this for the correctness of fsync. */
2865 LASSERT(hp == 0 && discard == 0);
2869 /* this extent is being truncated, can't do anything
2870 * for it now. it will be set to urgent after truncate
2871 * is finished in osc_cache_truncate_end(). */
2875 ext = next_extent(ext);
2877 osc_object_unlock(obj);
2879 LASSERT(ergo(!discard, cfs_list_empty(&discard_list)));
2880 if (!cfs_list_empty(&discard_list)) {
2881 struct osc_extent *tmp;
2884 osc_list_maint(osc_cli(obj), obj);
2885 cfs_list_for_each_entry_safe(ext, tmp, &discard_list, oe_link) {
2886 cfs_list_del_init(&ext->oe_link);
2887 EASSERT(ext->oe_state == OES_LOCKING, ext);
2889 /* Discard caching pages. We don't actually write this
2890 * extent out but we complete it as if we did. */
2891 rc = osc_extent_make_ready(env, ext);
2892 if (unlikely(rc < 0)) {
2893 OSC_EXTENT_DUMP(D_ERROR, ext,
2894 "make_ready returned %d\n", rc);
2899 /* finish the extent as if the pages were sent */
2900 osc_extent_finish(env, ext, 0, 0);
2905 osc_io_unplug(env, osc_cli(obj), obj, PDL_POLICY_ROUND);
2907 if (hp || discard) {
2909 rc = osc_cache_wait_range(env, obj, start, end);
2910 if (result >= 0 && rc < 0)
2914 OSC_IO_DEBUG(obj, "cache page out.\n");