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) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <libcfs/libcfs.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <libcfs/list.h>
48 #include <cl_object.h>
49 #include "cl_internal.h"
51 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
55 # define PASSERT(env, page, expr) \
57 if (unlikely(!(expr))) { \
58 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
62 #else /* !LIBCFS_DEBUG */
63 # define PASSERT(env, page, exp) \
64 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
65 #endif /* !LIBCFS_DEBUG */
67 #ifdef INVARIANT_CHECK
68 # define PINVRNT(env, page, expr) \
70 if (unlikely(!(expr))) { \
71 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
75 #else /* !INVARIANT_CHECK */
76 # define PINVRNT(env, page, exp) \
77 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
78 #endif /* !INVARIANT_CHECK */
80 /* Disable page statistic by default due to huge performance penalty. */
81 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
82 #define CS_PAGE_INC(o, item) \
83 cfs_atomic_inc(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
84 #define CS_PAGE_DEC(o, item) \
85 cfs_atomic_dec(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
86 #define CS_PAGESTATE_INC(o, state) \
87 cfs_atomic_inc(&cl_object_site(o)->cs_pages_state[state])
88 #define CS_PAGESTATE_DEC(o, state) \
89 cfs_atomic_dec(&cl_object_site(o)->cs_pages_state[state])
91 #define CS_PAGE_INC(o, item)
92 #define CS_PAGE_DEC(o, item)
93 #define CS_PAGESTATE_INC(o, state)
94 #define CS_PAGESTATE_DEC(o, state)
98 * Internal version of cl_page_top, it should be called if the page is
99 * known to be not freed, says with page referenced, or radix tree lock held,
102 static struct cl_page *cl_page_top_trusted(struct cl_page *page)
104 while (page->cp_parent != NULL)
105 page = page->cp_parent;
110 * Internal version of cl_page_get().
112 * This function can be used to obtain initial reference to previously
113 * unreferenced cached object. It can be called only if concurrent page
114 * reclamation is somehow prevented, e.g., by locking page radix-tree
115 * (cl_object_header::hdr->coh_page_guard), or by keeping a lock on a VM page,
116 * associated with \a page.
118 * Use with care! Not exported.
120 static void cl_page_get_trust(struct cl_page *page)
122 LASSERT(cfs_atomic_read(&page->cp_ref) > 0);
123 cfs_atomic_inc(&page->cp_ref);
127 * Returns a slice within a page, corresponding to the given layer in the
132 static const struct cl_page_slice *
133 cl_page_at_trusted(const struct cl_page *page,
134 const struct lu_device_type *dtype)
136 const struct cl_page_slice *slice;
139 page = cl_page_top_trusted((struct cl_page *)page);
141 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
142 if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
145 page = page->cp_child;
146 } while (page != NULL);
151 * Returns a page with given index in the given object, or NULL if no page is
152 * found. Acquires a reference on \a page.
154 * Locking: called under cl_object_header::coh_page_guard spin-lock.
156 struct cl_page *cl_page_lookup(struct cl_object_header *hdr, pgoff_t index)
158 struct cl_page *page;
160 LASSERT_SPIN_LOCKED(&hdr->coh_page_guard);
162 page = radix_tree_lookup(&hdr->coh_tree, index);
164 cl_page_get_trust(page);
167 EXPORT_SYMBOL(cl_page_lookup);
170 * Returns a list of pages by a given [start, end] of \a obj.
172 * \param resched If not NULL, then we give up before hogging CPU for too
173 * long and set *resched = 1, in that case caller should implement a retry
176 * Gang tree lookup (radix_tree_gang_lookup()) optimization is absolutely
177 * crucial in the face of [offset, EOF] locks.
179 * Return at least one page in @queue unless there is no covered page.
181 int cl_page_gang_lookup(const struct lu_env *env, struct cl_object *obj,
182 struct cl_io *io, pgoff_t start, pgoff_t end,
183 cl_page_gang_cb_t cb, void *cbdata)
185 struct cl_object_header *hdr;
186 struct cl_page *page;
187 struct cl_page **pvec;
188 const struct cl_page_slice *slice;
189 const struct lu_device_type *dtype;
194 int res = CLP_GANG_OKAY;
199 hdr = cl_object_header(obj);
200 pvec = cl_env_info(env)->clt_pvec;
201 dtype = cl_object_top(obj)->co_lu.lo_dev->ld_type;
202 spin_lock(&hdr->coh_page_guard);
203 while ((nr = radix_tree_gang_lookup(&hdr->coh_tree, (void **)pvec,
204 idx, CLT_PVEC_SIZE)) > 0) {
205 int end_of_region = 0;
206 idx = pvec[nr - 1]->cp_index + 1;
207 for (i = 0, j = 0; i < nr; ++i) {
211 LASSERT(page->cp_type == CPT_CACHEABLE);
212 if (page->cp_index > end) {
216 if (page->cp_state == CPS_FREEING)
219 slice = cl_page_at_trusted(page, dtype);
221 * Pages for lsm-less file has no underneath sub-page
222 * for osc, in case of ...
224 PASSERT(env, page, slice != NULL);
226 page = slice->cpl_page;
228 * Can safely call cl_page_get_trust() under
229 * radix-tree spin-lock.
231 * XXX not true, because @page is from object another
232 * than @hdr and protected by different tree lock.
234 cl_page_get_trust(page);
235 lu_ref_add_atomic(&page->cp_reference,
236 "gang_lookup", cfs_current());
241 * Here a delicate locking dance is performed. Current thread
242 * holds a reference to a page, but has to own it before it
243 * can be placed into queue. Owning implies waiting, so
244 * radix-tree lock is to be released. After a wait one has to
245 * check that pages weren't truncated (cl_page_own() returns
246 * error in the latter case).
248 spin_unlock(&hdr->coh_page_guard);
251 for (i = 0; i < j; ++i) {
253 if (res == CLP_GANG_OKAY)
254 res = (*cb)(env, io, page, cbdata);
255 lu_ref_del(&page->cp_reference,
256 "gang_lookup", cfs_current());
257 cl_page_put(env, page);
259 if (nr < CLT_PVEC_SIZE || end_of_region)
262 if (res == CLP_GANG_OKAY && cfs_need_resched())
263 res = CLP_GANG_RESCHED;
264 if (res != CLP_GANG_OKAY)
267 spin_lock(&hdr->coh_page_guard);
271 spin_unlock(&hdr->coh_page_guard);
274 EXPORT_SYMBOL(cl_page_gang_lookup);
276 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
278 struct cl_object *obj = page->cp_obj;
279 int pagesize = cl_object_header(obj)->coh_page_bufsize;
281 PASSERT(env, page, cfs_list_empty(&page->cp_batch));
282 PASSERT(env, page, page->cp_owner == NULL);
283 PASSERT(env, page, page->cp_req == NULL);
284 PASSERT(env, page, page->cp_parent == NULL);
285 PASSERT(env, page, page->cp_state == CPS_FREEING);
289 while (!cfs_list_empty(&page->cp_layers)) {
290 struct cl_page_slice *slice;
292 slice = cfs_list_entry(page->cp_layers.next,
293 struct cl_page_slice, cpl_linkage);
294 cfs_list_del_init(page->cp_layers.next);
295 slice->cpl_ops->cpo_fini(env, slice);
297 CS_PAGE_DEC(obj, total);
298 CS_PAGESTATE_DEC(obj, page->cp_state);
299 lu_object_ref_del_at(&obj->co_lu, page->cp_obj_ref, "cl_page", page);
300 cl_object_put(env, obj);
301 lu_ref_fini(&page->cp_reference);
302 OBD_FREE(page, pagesize);
307 * Helper function updating page state. This is the only place in the code
308 * where cl_page::cp_state field is mutated.
310 static inline void cl_page_state_set_trust(struct cl_page *page,
311 enum cl_page_state state)
314 *(enum cl_page_state *)&page->cp_state = state;
317 static struct cl_page *cl_page_alloc(const struct lu_env *env,
318 struct cl_object *o, pgoff_t ind, struct page *vmpage,
319 enum cl_page_type type)
321 struct cl_page *page;
322 struct lu_object_header *head;
325 OBD_ALLOC_GFP(page, cl_object_header(o)->coh_page_bufsize,
329 cfs_atomic_set(&page->cp_ref, 1);
330 if (type == CPT_CACHEABLE) /* for radix tree */
331 cfs_atomic_inc(&page->cp_ref);
334 page->cp_obj_ref = lu_object_ref_add(&o->co_lu, "cl_page",page);
335 page->cp_index = ind;
336 cl_page_state_set_trust(page, CPS_CACHED);
337 page->cp_type = type;
338 CFS_INIT_LIST_HEAD(&page->cp_layers);
339 CFS_INIT_LIST_HEAD(&page->cp_batch);
340 CFS_INIT_LIST_HEAD(&page->cp_flight);
341 mutex_init(&page->cp_mutex);
342 lu_ref_init(&page->cp_reference);
343 head = o->co_lu.lo_header;
344 cfs_list_for_each_entry(o, &head->loh_layers,
346 if (o->co_ops->coo_page_init != NULL) {
347 result = o->co_ops->coo_page_init(env, o,
350 cl_page_delete0(env, page, 0);
351 cl_page_free(env, page);
352 page = ERR_PTR(result);
358 CS_PAGE_INC(o, total);
359 CS_PAGE_INC(o, create);
360 CS_PAGESTATE_DEC(o, CPS_CACHED);
363 page = ERR_PTR(-ENOMEM);
369 * Returns a cl_page with index \a idx at the object \a o, and associated with
370 * the VM page \a vmpage.
372 * This is the main entry point into the cl_page caching interface. First, a
373 * cache (implemented as a per-object radix tree) is consulted. If page is
374 * found there, it is returned immediately. Otherwise new page is allocated
375 * and returned. In any case, additional reference to page is acquired.
377 * \see cl_object_find(), cl_lock_find()
379 static struct cl_page *cl_page_find0(const struct lu_env *env,
381 pgoff_t idx, struct page *vmpage,
382 enum cl_page_type type,
383 struct cl_page *parent)
385 struct cl_page *page = NULL;
386 struct cl_page *ghost = NULL;
387 struct cl_object_header *hdr;
390 LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
395 hdr = cl_object_header(o);
396 CS_PAGE_INC(o, lookup);
398 CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
399 idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
401 if (type == CPT_CACHEABLE) {
402 /* vmpage lock is used to protect the child/parent
404 KLASSERT(PageLocked(vmpage));
406 * cl_vmpage_page() can be called here without any locks as
408 * - "vmpage" is locked (which prevents ->private from
409 * concurrent updates), and
411 * - "o" cannot be destroyed while current thread holds a
414 page = cl_vmpage_page(vmpage, o);
417 cl_page_vmpage(env, page) == vmpage &&
418 (void *)radix_tree_lookup(&hdr->coh_tree,
427 /* allocate and initialize cl_page */
428 page = cl_page_alloc(env, o, idx, vmpage, type);
432 if (type == CPT_TRANSIENT) {
434 LASSERT(page->cp_parent == NULL);
435 page->cp_parent = parent;
436 parent->cp_child = page;
442 * XXX optimization: use radix_tree_preload() here, and change tree
443 * gfp mask to GFP_KERNEL in cl_object_header_init().
445 spin_lock(&hdr->coh_page_guard);
446 err = radix_tree_insert(&hdr->coh_tree, idx, page);
450 * Noted by Jay: a lock on \a vmpage protects cl_page_find()
451 * from this race, but
453 * 0. it's better to have cl_page interface "locally
454 * consistent" so that its correctness can be reasoned
455 * about without appealing to the (obscure world of) VM
458 * 1. handling this race allows ->coh_tree to remain
459 * consistent even when VM locking is somehow busted,
460 * which is very useful during diagnosing and debugging.
463 CL_PAGE_DEBUG(D_ERROR, env, ghost,
464 "fail to insert into radix tree: %d\n", err);
467 LASSERT(page->cp_parent == NULL);
468 page->cp_parent = parent;
469 parent->cp_child = page;
473 spin_unlock(&hdr->coh_page_guard);
475 if (unlikely(ghost != NULL)) {
476 cl_page_delete0(env, ghost, 0);
477 cl_page_free(env, ghost);
482 struct cl_page *cl_page_find(const struct lu_env *env, struct cl_object *o,
483 pgoff_t idx, struct page *vmpage,
484 enum cl_page_type type)
486 return cl_page_find0(env, o, idx, vmpage, type, NULL);
488 EXPORT_SYMBOL(cl_page_find);
491 struct cl_page *cl_page_find_sub(const struct lu_env *env, struct cl_object *o,
492 pgoff_t idx, struct page *vmpage,
493 struct cl_page *parent)
495 return cl_page_find0(env, o, idx, vmpage, parent->cp_type, parent);
497 EXPORT_SYMBOL(cl_page_find_sub);
499 static inline int cl_page_invariant(const struct cl_page *pg)
501 struct cl_object_header *header;
502 struct cl_page *parent;
503 struct cl_page *child;
507 * Page invariant is protected by a VM lock.
509 LINVRNT(cl_page_is_vmlocked(NULL, pg));
511 header = cl_object_header(pg->cp_obj);
512 parent = pg->cp_parent;
513 child = pg->cp_child;
514 owner = pg->cp_owner;
516 return cl_page_in_use(pg) &&
517 ergo(parent != NULL, parent->cp_child == pg) &&
518 ergo(child != NULL, child->cp_parent == pg) &&
519 ergo(child != NULL, pg->cp_obj != child->cp_obj) &&
520 ergo(parent != NULL, pg->cp_obj != parent->cp_obj) &&
521 ergo(owner != NULL && parent != NULL,
522 parent->cp_owner == pg->cp_owner->ci_parent) &&
523 ergo(owner != NULL && child != NULL,
524 child->cp_owner->ci_parent == owner) &&
526 * Either page is early in initialization (has neither child
527 * nor parent yet), or it is in the object radix tree.
529 ergo(pg->cp_state < CPS_FREEING && pg->cp_type == CPT_CACHEABLE,
530 (void *)radix_tree_lookup(&header->coh_tree,
531 pg->cp_index) == pg ||
532 (child == NULL && parent == NULL));
535 static void cl_page_state_set0(const struct lu_env *env,
536 struct cl_page *page, enum cl_page_state state)
538 enum cl_page_state old;
541 * Matrix of allowed state transitions [old][new], for sanity
544 static const int allowed_transitions[CPS_NR][CPS_NR] = {
547 [CPS_OWNED] = 1, /* io finds existing cached page */
549 [CPS_PAGEOUT] = 1, /* write-out from the cache */
550 [CPS_FREEING] = 1, /* eviction on the memory pressure */
553 [CPS_CACHED] = 1, /* release to the cache */
555 [CPS_PAGEIN] = 1, /* start read immediately */
556 [CPS_PAGEOUT] = 1, /* start write immediately */
557 [CPS_FREEING] = 1, /* lock invalidation or truncate */
560 [CPS_CACHED] = 1, /* io completion */
567 [CPS_CACHED] = 1, /* io completion */
583 old = page->cp_state;
584 PASSERT(env, page, allowed_transitions[old][state]);
585 CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
586 for (; page != NULL; page = page->cp_child) {
587 PASSERT(env, page, page->cp_state == old);
589 equi(state == CPS_OWNED, page->cp_owner != NULL));
591 CS_PAGESTATE_DEC(page->cp_obj, page->cp_state);
592 CS_PAGESTATE_INC(page->cp_obj, state);
593 cl_page_state_set_trust(page, state);
598 static void cl_page_state_set(const struct lu_env *env,
599 struct cl_page *page, enum cl_page_state state)
601 cl_page_state_set0(env, page, state);
605 * Acquires an additional reference to a page.
607 * This can be called only by caller already possessing a reference to \a
610 * \see cl_object_get(), cl_lock_get().
612 void cl_page_get(struct cl_page *page)
615 cl_page_get_trust(page);
618 EXPORT_SYMBOL(cl_page_get);
621 * Releases a reference to a page.
623 * When last reference is released, page is returned to the cache, unless it
624 * is in cl_page_state::CPS_FREEING state, in which case it is immediately
627 * \see cl_object_put(), cl_lock_put().
629 void cl_page_put(const struct lu_env *env, struct cl_page *page)
631 PASSERT(env, page, cfs_atomic_read(&page->cp_ref) > !!page->cp_parent);
634 CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
635 cfs_atomic_read(&page->cp_ref));
637 if (cfs_atomic_dec_and_test(&page->cp_ref)) {
638 LASSERT(page->cp_state == CPS_FREEING);
640 LASSERT(cfs_atomic_read(&page->cp_ref) == 0);
641 PASSERT(env, page, page->cp_owner == NULL);
642 PASSERT(env, page, cfs_list_empty(&page->cp_batch));
644 * Page is no longer reachable by other threads. Tear
647 cl_page_free(env, page);
652 EXPORT_SYMBOL(cl_page_put);
655 * Returns a VM page associated with a given cl_page.
657 cfs_page_t *cl_page_vmpage(const struct lu_env *env, struct cl_page *page)
659 const struct cl_page_slice *slice;
662 * Find uppermost layer with ->cpo_vmpage() method, and return its
665 page = cl_page_top(page);
667 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
668 if (slice->cpl_ops->cpo_vmpage != NULL)
669 RETURN(slice->cpl_ops->cpo_vmpage(env, slice));
671 page = page->cp_child;
672 } while (page != NULL);
673 LBUG(); /* ->cpo_vmpage() has to be defined somewhere in the stack */
675 EXPORT_SYMBOL(cl_page_vmpage);
678 * Returns a cl_page associated with a VM page, and given cl_object.
680 struct cl_page *cl_vmpage_page(cfs_page_t *vmpage, struct cl_object *obj)
683 struct cl_page *page;
686 KLASSERT(PageLocked(vmpage));
689 * NOTE: absence of races and liveness of data are guaranteed by page
690 * lock on a "vmpage". That works because object destruction has
691 * bottom-to-top pass.
695 * This loop assumes that ->private points to the top-most page. This
696 * can be rectified easily.
698 top = (struct cl_page *)vmpage->private;
702 for (page = top; page != NULL; page = page->cp_child) {
703 if (cl_object_same(page->cp_obj, obj)) {
704 cl_page_get_trust(page);
708 LASSERT(ergo(page, page->cp_type == CPT_CACHEABLE));
711 EXPORT_SYMBOL(cl_vmpage_page);
714 * Returns the top-page for a given page.
716 * \see cl_object_top(), cl_io_top()
718 struct cl_page *cl_page_top(struct cl_page *page)
720 return cl_page_top_trusted(page);
722 EXPORT_SYMBOL(cl_page_top);
724 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
725 const struct lu_device_type *dtype)
727 return cl_page_at_trusted(page, dtype);
729 EXPORT_SYMBOL(cl_page_at);
731 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
733 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...) \
735 const struct lu_env *__env = (_env); \
736 struct cl_page *__page = (_page); \
737 const struct cl_page_slice *__scan; \
739 ptrdiff_t __op = (_op); \
740 int (*__method)_proto; \
743 __page = cl_page_top(__page); \
745 cfs_list_for_each_entry(__scan, &__page->cp_layers, \
747 __method = *(void **)((char *)__scan->cpl_ops + \
749 if (__method != NULL) { \
750 __result = (*__method)(__env, __scan, \
756 __page = __page->cp_child; \
757 } while (__page != NULL && __result == 0); \
763 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...) \
765 const struct lu_env *__env = (_env); \
766 struct cl_page *__page = (_page); \
767 const struct cl_page_slice *__scan; \
768 ptrdiff_t __op = (_op); \
769 void (*__method)_proto; \
771 __page = cl_page_top(__page); \
773 cfs_list_for_each_entry(__scan, &__page->cp_layers, \
775 __method = *(void **)((char *)__scan->cpl_ops + \
777 if (__method != NULL) \
778 (*__method)(__env, __scan, \
781 __page = __page->cp_child; \
782 } while (__page != NULL); \
785 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...) \
787 const struct lu_env *__env = (_env); \
788 struct cl_page *__page = (_page); \
789 const struct cl_page_slice *__scan; \
790 ptrdiff_t __op = (_op); \
791 void (*__method)_proto; \
793 /* get to the bottom page. */ \
794 while (__page->cp_child != NULL) \
795 __page = __page->cp_child; \
797 cfs_list_for_each_entry_reverse(__scan, &__page->cp_layers, \
799 __method = *(void **)((char *)__scan->cpl_ops + \
801 if (__method != NULL) \
802 (*__method)(__env, __scan, \
805 __page = __page->cp_parent; \
806 } while (__page != NULL); \
809 static int cl_page_invoke(const struct lu_env *env,
810 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
813 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
815 RETURN(CL_PAGE_INVOKE(env, page, op,
816 (const struct lu_env *,
817 const struct cl_page_slice *, struct cl_io *),
821 static void cl_page_invoid(const struct lu_env *env,
822 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
825 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
827 CL_PAGE_INVOID(env, page, op,
828 (const struct lu_env *,
829 const struct cl_page_slice *, struct cl_io *), io);
833 static void cl_page_owner_clear(struct cl_page *page)
836 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
837 if (page->cp_owner != NULL) {
838 LASSERT(page->cp_owner->ci_owned_nr > 0);
839 page->cp_owner->ci_owned_nr--;
840 page->cp_owner = NULL;
841 page->cp_task = NULL;
847 static void cl_page_owner_set(struct cl_page *page)
850 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
851 LASSERT(page->cp_owner != NULL);
852 page->cp_owner->ci_owned_nr++;
857 void cl_page_disown0(const struct lu_env *env,
858 struct cl_io *io, struct cl_page *pg)
860 enum cl_page_state state;
863 state = pg->cp_state;
864 PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
865 PINVRNT(env, pg, cl_page_invariant(pg));
866 cl_page_owner_clear(pg);
868 if (state == CPS_OWNED)
869 cl_page_state_set(env, pg, CPS_CACHED);
871 * Completion call-backs are executed in the bottom-up order, so that
872 * uppermost layer (llite), responsible for VFS/VM interaction runs
873 * last and can release locks safely.
875 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
876 (const struct lu_env *,
877 const struct cl_page_slice *, struct cl_io *),
883 * returns true, iff page is owned by the given io.
885 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
887 LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
889 RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == io);
891 EXPORT_SYMBOL(cl_page_is_owned);
894 * Try to own a page by IO.
896 * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
897 * into cl_page_state::CPS_OWNED state.
899 * \pre !cl_page_is_owned(pg, io)
900 * \post result == 0 iff cl_page_is_owned(pg, io)
904 * \retval -ve failure, e.g., page was destroyed (and landed in
905 * cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
906 * or, page was owned by another thread, or in IO.
908 * \see cl_page_disown()
909 * \see cl_page_operations::cpo_own()
910 * \see cl_page_own_try()
913 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
914 struct cl_page *pg, int nonblock)
918 PINVRNT(env, pg, !cl_page_is_owned(pg, io));
921 pg = cl_page_top(pg);
924 if (pg->cp_state == CPS_FREEING) {
927 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
928 (const struct lu_env *,
929 const struct cl_page_slice *,
930 struct cl_io *, int),
933 PASSERT(env, pg, pg->cp_owner == NULL);
934 PASSERT(env, pg, pg->cp_req == NULL);
936 pg->cp_task = current;
937 cl_page_owner_set(pg);
938 if (pg->cp_state != CPS_FREEING) {
939 cl_page_state_set(env, pg, CPS_OWNED);
941 cl_page_disown0(env, io, pg);
946 PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
951 * Own a page, might be blocked.
953 * \see cl_page_own0()
955 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
957 return cl_page_own0(env, io, pg, 0);
959 EXPORT_SYMBOL(cl_page_own);
962 * Nonblock version of cl_page_own().
964 * \see cl_page_own0()
966 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
969 return cl_page_own0(env, io, pg, 1);
971 EXPORT_SYMBOL(cl_page_own_try);
975 * Assume page ownership.
977 * Called when page is already locked by the hosting VM.
979 * \pre !cl_page_is_owned(pg, io)
980 * \post cl_page_is_owned(pg, io)
982 * \see cl_page_operations::cpo_assume()
984 void cl_page_assume(const struct lu_env *env,
985 struct cl_io *io, struct cl_page *pg)
987 PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
990 pg = cl_page_top(pg);
993 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
994 PASSERT(env, pg, pg->cp_owner == NULL);
996 pg->cp_task = current;
997 cl_page_owner_set(pg);
998 cl_page_state_set(env, pg, CPS_OWNED);
1001 EXPORT_SYMBOL(cl_page_assume);
1004 * Releases page ownership without unlocking the page.
1006 * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
1007 * underlying VM page (as VM is supposed to do this itself).
1009 * \pre cl_page_is_owned(pg, io)
1010 * \post !cl_page_is_owned(pg, io)
1012 * \see cl_page_assume()
1014 void cl_page_unassume(const struct lu_env *env,
1015 struct cl_io *io, struct cl_page *pg)
1017 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1018 PINVRNT(env, pg, cl_page_invariant(pg));
1021 pg = cl_page_top(pg);
1023 cl_page_owner_clear(pg);
1024 cl_page_state_set(env, pg, CPS_CACHED);
1025 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
1026 (const struct lu_env *,
1027 const struct cl_page_slice *, struct cl_io *),
1031 EXPORT_SYMBOL(cl_page_unassume);
1034 * Releases page ownership.
1036 * Moves page into cl_page_state::CPS_CACHED.
1038 * \pre cl_page_is_owned(pg, io)
1039 * \post !cl_page_is_owned(pg, io)
1041 * \see cl_page_own()
1042 * \see cl_page_operations::cpo_disown()
1044 void cl_page_disown(const struct lu_env *env,
1045 struct cl_io *io, struct cl_page *pg)
1047 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1050 pg = cl_page_top(pg);
1052 cl_page_disown0(env, io, pg);
1055 EXPORT_SYMBOL(cl_page_disown);
1058 * Called when page is to be removed from the object, e.g., as a result of
1061 * Calls cl_page_operations::cpo_discard() top-to-bottom.
1063 * \pre cl_page_is_owned(pg, io)
1065 * \see cl_page_operations::cpo_discard()
1067 void cl_page_discard(const struct lu_env *env,
1068 struct cl_io *io, struct cl_page *pg)
1070 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1071 PINVRNT(env, pg, cl_page_invariant(pg));
1073 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
1075 EXPORT_SYMBOL(cl_page_discard);
1078 * Version of cl_page_delete() that can be called for not fully constructed
1079 * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
1080 * path. Doesn't check page invariant.
1082 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
1085 struct cl_page *tmp = pg;
1088 PASSERT(env, pg, pg == cl_page_top(pg));
1089 PASSERT(env, pg, pg->cp_state != CPS_FREEING);
1092 * Severe all ways to obtain new pointers to @pg.
1094 cl_page_owner_clear(pg);
1097 * unexport the page firstly before freeing it so that
1098 * the page content is considered to be invalid.
1099 * We have to do this because a CPS_FREEING cl_page may
1100 * be NOT under the protection of a cl_lock.
1101 * Afterwards, if this page is found by other threads, then this
1102 * page will be forced to reread.
1104 cl_page_export(env, pg, 0);
1105 cl_page_state_set0(env, pg, CPS_FREEING);
1107 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_delete),
1108 (const struct lu_env *, const struct cl_page_slice *));
1110 if (tmp->cp_type == CPT_CACHEABLE) {
1112 /* !radix means that @pg is not yet in the radix tree,
1116 for (; tmp != NULL; tmp = tmp->cp_child) {
1118 struct cl_object_header *hdr;
1120 hdr = cl_object_header(tmp->cp_obj);
1121 spin_lock(&hdr->coh_page_guard);
1122 value = radix_tree_delete(&hdr->coh_tree,
1124 PASSERT(env, tmp, value == tmp);
1125 PASSERT(env, tmp, hdr->coh_pages > 0);
1127 spin_unlock(&hdr->coh_page_guard);
1128 cl_page_put(env, tmp);
1136 * Called when a decision is made to throw page out of memory.
1138 * Notifies all layers about page destruction by calling
1139 * cl_page_operations::cpo_delete() method top-to-bottom.
1141 * Moves page into cl_page_state::CPS_FREEING state (this is the only place
1142 * where transition to this state happens).
1144 * Eliminates all venues through which new references to the page can be
1147 * - removes page from the radix trees,
1149 * - breaks linkage from VM page to cl_page.
1151 * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
1152 * drain after some time, at which point page will be recycled.
1154 * \pre pg == cl_page_top(pg)
1155 * \pre VM page is locked
1156 * \post pg->cp_state == CPS_FREEING
1158 * \see cl_page_operations::cpo_delete()
1160 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
1162 PINVRNT(env, pg, cl_page_invariant(pg));
1164 cl_page_delete0(env, pg, 1);
1167 EXPORT_SYMBOL(cl_page_delete);
1170 * Unmaps page from user virtual memory.
1172 * Calls cl_page_operations::cpo_unmap() through all layers top-to-bottom. The
1173 * layer responsible for VM interaction has to unmap page from user space
1176 * \see cl_page_operations::cpo_unmap()
1178 int cl_page_unmap(const struct lu_env *env,
1179 struct cl_io *io, struct cl_page *pg)
1181 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1182 PINVRNT(env, pg, cl_page_invariant(pg));
1184 return cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_unmap));
1186 EXPORT_SYMBOL(cl_page_unmap);
1189 * Marks page up-to-date.
1191 * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
1192 * layer responsible for VM interaction has to mark/clear page as up-to-date
1193 * by the \a uptodate argument.
1195 * \see cl_page_operations::cpo_export()
1197 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
1199 PINVRNT(env, pg, cl_page_invariant(pg));
1200 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
1201 (const struct lu_env *,
1202 const struct cl_page_slice *, int), uptodate);
1204 EXPORT_SYMBOL(cl_page_export);
1207 * Returns true, iff \a pg is VM locked in a suitable sense by the calling
1210 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
1213 const struct cl_page_slice *slice;
1216 pg = cl_page_top_trusted((struct cl_page *)pg);
1217 slice = container_of(pg->cp_layers.next,
1218 const struct cl_page_slice, cpl_linkage);
1219 PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
1221 * Call ->cpo_is_vmlocked() directly instead of going through
1222 * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
1223 * cl_page_invariant().
1225 result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
1226 PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
1227 RETURN(result == -EBUSY);
1229 EXPORT_SYMBOL(cl_page_is_vmlocked);
1231 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
1234 RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
1237 static void cl_page_io_start(const struct lu_env *env,
1238 struct cl_page *pg, enum cl_req_type crt)
1241 * Page is queued for IO, change its state.
1244 cl_page_owner_clear(pg);
1245 cl_page_state_set(env, pg, cl_req_type_state(crt));
1250 * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
1251 * called top-to-bottom. Every layer either agrees to submit this page (by
1252 * returning 0), or requests to omit this page (by returning -EALREADY). Layer
1253 * handling interactions with the VM also has to inform VM that page is under
1256 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
1257 struct cl_page *pg, enum cl_req_type crt)
1261 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1262 PINVRNT(env, pg, cl_page_invariant(pg));
1263 PINVRNT(env, pg, crt < CRT_NR);
1266 * XXX this has to be called bottom-to-top, so that llite can set up
1267 * PG_writeback without risking other layers deciding to skip this
1272 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
1274 cl_page_io_start(env, pg, crt);
1276 KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
1278 PageWriteback(cl_page_vmpage(env, pg)))));
1279 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1282 EXPORT_SYMBOL(cl_page_prep);
1285 * Notify layers about transfer completion.
1287 * Invoked by transfer sub-system (which is a part of osc) to notify layers
1288 * that a transfer, of which this page is a part of has completed.
1290 * Completion call-backs are executed in the bottom-up order, so that
1291 * uppermost layer (llite), responsible for the VFS/VM interaction runs last
1292 * and can release locks safely.
1294 * \pre pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1295 * \post pg->cp_state == CPS_CACHED
1297 * \see cl_page_operations::cpo_completion()
1299 void cl_page_completion(const struct lu_env *env,
1300 struct cl_page *pg, enum cl_req_type crt, int ioret)
1302 struct cl_sync_io *anchor = pg->cp_sync_io;
1304 PASSERT(env, pg, crt < CRT_NR);
1305 /* cl_page::cp_req already cleared by the caller (osc_completion()) */
1306 PASSERT(env, pg, pg->cp_req == NULL);
1307 PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
1310 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
1311 if (crt == CRT_READ && ioret == 0) {
1312 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
1313 pg->cp_flags |= CPF_READ_COMPLETED;
1316 cl_page_state_set(env, pg, CPS_CACHED);
1319 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
1320 (const struct lu_env *,
1321 const struct cl_page_slice *, int), ioret);
1323 LASSERT(cl_page_is_vmlocked(env, pg));
1324 LASSERT(pg->cp_sync_io == anchor);
1325 pg->cp_sync_io = NULL;
1328 * As page->cp_obj is pinned by a reference from page->cp_req, it is
1329 * safe to call cl_page_put() without risking object destruction in a
1330 * non-blocking context.
1332 cl_page_put(env, pg);
1335 cl_sync_io_note(anchor, ioret);
1339 EXPORT_SYMBOL(cl_page_completion);
1342 * Notify layers that transfer formation engine decided to yank this page from
1343 * the cache and to make it a part of a transfer.
1345 * \pre pg->cp_state == CPS_CACHED
1346 * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1348 * \see cl_page_operations::cpo_make_ready()
1350 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1351 enum cl_req_type crt)
1355 PINVRNT(env, pg, crt < CRT_NR);
1360 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1361 (const struct lu_env *,
1362 const struct cl_page_slice *));
1364 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1365 cl_page_io_start(env, pg, crt);
1367 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1370 EXPORT_SYMBOL(cl_page_make_ready);
1373 * Notify layers that high level io decided to place this page into a cache
1374 * for future transfer.
1376 * The layer implementing transfer engine (osc) has to register this page in
1379 * \pre cl_page_is_owned(pg, io)
1380 * \post cl_page_is_owned(pg, io)
1382 * \see cl_page_operations::cpo_cache_add()
1384 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1385 struct cl_page *pg, enum cl_req_type crt)
1387 const struct cl_page_slice *scan;
1390 PINVRNT(env, pg, crt < CRT_NR);
1391 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1392 PINVRNT(env, pg, cl_page_invariant(pg));
1399 cfs_list_for_each_entry(scan, &pg->cp_layers, cpl_linkage) {
1400 if (scan->cpl_ops->io[crt].cpo_cache_add == NULL)
1403 result = scan->cpl_ops->io[crt].cpo_cache_add(env, scan, io);
1407 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1410 EXPORT_SYMBOL(cl_page_cache_add);
1413 * Called if a pge is being written back by kernel's intention.
1415 * \pre cl_page_is_owned(pg, io)
1416 * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
1418 * \see cl_page_operations::cpo_flush()
1420 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
1425 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1426 PINVRNT(env, pg, cl_page_invariant(pg));
1430 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_flush));
1432 CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1435 EXPORT_SYMBOL(cl_page_flush);
1438 * Checks whether page is protected by any extent lock is at least required
1441 * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1442 * \see cl_page_operations::cpo_is_under_lock()
1444 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1445 struct cl_page *page)
1449 PINVRNT(env, page, cl_page_invariant(page));
1452 rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1453 (const struct lu_env *,
1454 const struct cl_page_slice *, struct cl_io *),
1456 PASSERT(env, page, rc != 0);
1459 EXPORT_SYMBOL(cl_page_is_under_lock);
1461 static int page_prune_cb(const struct lu_env *env, struct cl_io *io,
1462 struct cl_page *page, void *cbdata)
1464 cl_page_own(env, io, page);
1465 cl_page_unmap(env, io, page);
1466 cl_page_discard(env, io, page);
1467 cl_page_disown(env, io, page);
1468 return CLP_GANG_OKAY;
1472 * Purges all cached pages belonging to the object \a obj.
1474 int cl_pages_prune(const struct lu_env *env, struct cl_object *clobj)
1476 struct cl_thread_info *info;
1477 struct cl_object *obj = cl_object_top(clobj);
1482 info = cl_env_info(env);
1486 * initialize the io. This is ugly since we never do IO in this
1487 * function, we just make cl_page_list functions happy. -jay
1490 io->ci_ignore_layout = 1;
1491 result = cl_io_init(env, io, CIT_MISC, obj);
1493 cl_io_fini(env, io);
1494 RETURN(io->ci_result);
1498 result = cl_page_gang_lookup(env, obj, io, 0, CL_PAGE_EOF,
1499 page_prune_cb, NULL);
1500 if (result == CLP_GANG_RESCHED)
1502 } while (result != CLP_GANG_OKAY);
1504 cl_io_fini(env, io);
1507 EXPORT_SYMBOL(cl_pages_prune);
1510 * Tells transfer engine that only part of a page is to be transmitted.
1512 * \see cl_page_operations::cpo_clip()
1514 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1517 PINVRNT(env, pg, cl_page_invariant(pg));
1519 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1520 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1521 (const struct lu_env *,
1522 const struct cl_page_slice *,int, int),
1525 EXPORT_SYMBOL(cl_page_clip);
1528 * Prints human readable representation of \a pg to the \a f.
1530 void cl_page_header_print(const struct lu_env *env, void *cookie,
1531 lu_printer_t printer, const struct cl_page *pg)
1533 (*printer)(env, cookie,
1534 "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1535 pg, cfs_atomic_read(&pg->cp_ref), pg->cp_obj,
1536 pg->cp_index, pg->cp_parent, pg->cp_child,
1537 pg->cp_state, pg->cp_error, pg->cp_type,
1538 pg->cp_owner, pg->cp_req, pg->cp_flags);
1540 EXPORT_SYMBOL(cl_page_header_print);
1543 * Prints human readable representation of \a pg to the \a f.
1545 void cl_page_print(const struct lu_env *env, void *cookie,
1546 lu_printer_t printer, const struct cl_page *pg)
1548 struct cl_page *scan;
1550 for (scan = cl_page_top((struct cl_page *)pg);
1551 scan != NULL; scan = scan->cp_child)
1552 cl_page_header_print(env, cookie, printer, scan);
1553 CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1554 (const struct lu_env *env,
1555 const struct cl_page_slice *slice,
1556 void *cookie, lu_printer_t p), cookie, printer);
1557 (*printer)(env, cookie, "end page@%p\n", pg);
1559 EXPORT_SYMBOL(cl_page_print);
1562 * Cancel a page which is still in a transfer.
1564 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1566 return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1567 (const struct lu_env *,
1568 const struct cl_page_slice *));
1570 EXPORT_SYMBOL(cl_page_cancel);
1573 * Converts a byte offset within object \a obj into a page index.
1575 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1580 return (loff_t)idx << CFS_PAGE_SHIFT;
1582 EXPORT_SYMBOL(cl_offset);
1585 * Converts a page index into a byte offset within object \a obj.
1587 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1592 return offset >> CFS_PAGE_SHIFT;
1594 EXPORT_SYMBOL(cl_index);
1596 int cl_page_size(const struct cl_object *obj)
1598 return 1 << CFS_PAGE_SHIFT;
1600 EXPORT_SYMBOL(cl_page_size);
1603 * Adds page slice to the compound page.
1605 * This is called by cl_object_operations::coo_page_init() methods to add a
1606 * per-layer state to the page. New state is added at the end of
1607 * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1609 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1611 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1612 struct cl_object *obj,
1613 const struct cl_page_operations *ops)
1616 cfs_list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1617 slice->cpl_obj = obj;
1618 slice->cpl_ops = ops;
1619 slice->cpl_page = page;
1622 EXPORT_SYMBOL(cl_page_slice_add);
1624 int cl_page_init(void)
1629 void cl_page_fini(void)