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, 2013, 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 CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
68 # define PINVRNT(env, page, expr) \
70 if (unlikely(!(expr))) { \
71 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
75 #else /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK */
76 # define PINVRNT(env, page, exp) \
77 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
78 #endif /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_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 lu_object_ref_add_at(&o->co_lu, &page->cp_obj_ref, "cl_page",
336 page->cp_index = ind;
337 cl_page_state_set_trust(page, CPS_CACHED);
338 page->cp_type = type;
339 CFS_INIT_LIST_HEAD(&page->cp_layers);
340 CFS_INIT_LIST_HEAD(&page->cp_batch);
341 CFS_INIT_LIST_HEAD(&page->cp_flight);
342 mutex_init(&page->cp_mutex);
343 lu_ref_init(&page->cp_reference);
344 head = o->co_lu.lo_header;
345 cfs_list_for_each_entry(o, &head->loh_layers,
347 if (o->co_ops->coo_page_init != NULL) {
348 result = o->co_ops->coo_page_init(env, o,
351 cl_page_delete0(env, page, 0);
352 cl_page_free(env, page);
353 page = ERR_PTR(result);
359 CS_PAGE_INC(o, total);
360 CS_PAGE_INC(o, create);
361 CS_PAGESTATE_DEC(o, CPS_CACHED);
364 page = ERR_PTR(-ENOMEM);
370 * Returns a cl_page with index \a idx at the object \a o, and associated with
371 * the VM page \a vmpage.
373 * This is the main entry point into the cl_page caching interface. First, a
374 * cache (implemented as a per-object radix tree) is consulted. If page is
375 * found there, it is returned immediately. Otherwise new page is allocated
376 * and returned. In any case, additional reference to page is acquired.
378 * \see cl_object_find(), cl_lock_find()
380 static struct cl_page *cl_page_find0(const struct lu_env *env,
382 pgoff_t idx, struct page *vmpage,
383 enum cl_page_type type,
384 struct cl_page *parent)
386 struct cl_page *page = NULL;
387 struct cl_page *ghost = NULL;
388 struct cl_object_header *hdr;
391 LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
396 hdr = cl_object_header(o);
397 CS_PAGE_INC(o, lookup);
399 CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
400 idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
402 if (type == CPT_CACHEABLE) {
403 /* vmpage lock is used to protect the child/parent
405 KLASSERT(PageLocked(vmpage));
407 * cl_vmpage_page() can be called here without any locks as
409 * - "vmpage" is locked (which prevents ->private from
410 * concurrent updates), and
412 * - "o" cannot be destroyed while current thread holds a
415 page = cl_vmpage_page(vmpage, o);
418 cl_page_vmpage(env, page) == vmpage &&
419 (void *)radix_tree_lookup(&hdr->coh_tree,
428 /* allocate and initialize cl_page */
429 page = cl_page_alloc(env, o, idx, vmpage, type);
433 if (type == CPT_TRANSIENT) {
435 LASSERT(page->cp_parent == NULL);
436 page->cp_parent = parent;
437 parent->cp_child = page;
443 * XXX optimization: use radix_tree_preload() here, and change tree
444 * gfp mask to GFP_KERNEL in cl_object_header_init().
446 spin_lock(&hdr->coh_page_guard);
447 err = radix_tree_insert(&hdr->coh_tree, idx, page);
451 * Noted by Jay: a lock on \a vmpage protects cl_page_find()
452 * from this race, but
454 * 0. it's better to have cl_page interface "locally
455 * consistent" so that its correctness can be reasoned
456 * about without appealing to the (obscure world of) VM
459 * 1. handling this race allows ->coh_tree to remain
460 * consistent even when VM locking is somehow busted,
461 * which is very useful during diagnosing and debugging.
464 CL_PAGE_DEBUG(D_ERROR, env, ghost,
465 "fail to insert into radix tree: %d\n", err);
468 LASSERT(page->cp_parent == NULL);
469 page->cp_parent = parent;
470 parent->cp_child = page;
474 spin_unlock(&hdr->coh_page_guard);
476 if (unlikely(ghost != NULL)) {
477 cl_page_delete0(env, ghost, 0);
478 cl_page_free(env, ghost);
483 struct cl_page *cl_page_find(const struct lu_env *env, struct cl_object *o,
484 pgoff_t idx, struct page *vmpage,
485 enum cl_page_type type)
487 return cl_page_find0(env, o, idx, vmpage, type, NULL);
489 EXPORT_SYMBOL(cl_page_find);
492 struct cl_page *cl_page_find_sub(const struct lu_env *env, struct cl_object *o,
493 pgoff_t idx, struct page *vmpage,
494 struct cl_page *parent)
496 return cl_page_find0(env, o, idx, vmpage, parent->cp_type, parent);
498 EXPORT_SYMBOL(cl_page_find_sub);
500 static inline int cl_page_invariant(const struct cl_page *pg)
502 struct cl_object_header *header;
503 struct cl_page *parent;
504 struct cl_page *child;
508 * Page invariant is protected by a VM lock.
510 LINVRNT(cl_page_is_vmlocked(NULL, pg));
512 header = cl_object_header(pg->cp_obj);
513 parent = pg->cp_parent;
514 child = pg->cp_child;
515 owner = pg->cp_owner;
517 return cl_page_in_use(pg) &&
518 ergo(parent != NULL, parent->cp_child == pg) &&
519 ergo(child != NULL, child->cp_parent == pg) &&
520 ergo(child != NULL, pg->cp_obj != child->cp_obj) &&
521 ergo(parent != NULL, pg->cp_obj != parent->cp_obj) &&
522 ergo(owner != NULL && parent != NULL,
523 parent->cp_owner == pg->cp_owner->ci_parent) &&
524 ergo(owner != NULL && child != NULL,
525 child->cp_owner->ci_parent == owner) &&
527 * Either page is early in initialization (has neither child
528 * nor parent yet), or it is in the object radix tree.
530 ergo(pg->cp_state < CPS_FREEING && pg->cp_type == CPT_CACHEABLE,
531 (void *)radix_tree_lookup(&header->coh_tree,
532 pg->cp_index) == pg ||
533 (child == NULL && parent == NULL));
536 static void cl_page_state_set0(const struct lu_env *env,
537 struct cl_page *page, enum cl_page_state state)
539 enum cl_page_state old;
542 * Matrix of allowed state transitions [old][new], for sanity
545 static const int allowed_transitions[CPS_NR][CPS_NR] = {
548 [CPS_OWNED] = 1, /* io finds existing cached page */
550 [CPS_PAGEOUT] = 1, /* write-out from the cache */
551 [CPS_FREEING] = 1, /* eviction on the memory pressure */
554 [CPS_CACHED] = 1, /* release to the cache */
556 [CPS_PAGEIN] = 1, /* start read immediately */
557 [CPS_PAGEOUT] = 1, /* start write immediately */
558 [CPS_FREEING] = 1, /* lock invalidation or truncate */
561 [CPS_CACHED] = 1, /* io completion */
568 [CPS_CACHED] = 1, /* io completion */
584 old = page->cp_state;
585 PASSERT(env, page, allowed_transitions[old][state]);
586 CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
587 for (; page != NULL; page = page->cp_child) {
588 PASSERT(env, page, page->cp_state == old);
590 equi(state == CPS_OWNED, page->cp_owner != NULL));
592 CS_PAGESTATE_DEC(page->cp_obj, page->cp_state);
593 CS_PAGESTATE_INC(page->cp_obj, state);
594 cl_page_state_set_trust(page, state);
599 static void cl_page_state_set(const struct lu_env *env,
600 struct cl_page *page, enum cl_page_state state)
602 cl_page_state_set0(env, page, state);
606 * Acquires an additional reference to a page.
608 * This can be called only by caller already possessing a reference to \a
611 * \see cl_object_get(), cl_lock_get().
613 void cl_page_get(struct cl_page *page)
616 cl_page_get_trust(page);
619 EXPORT_SYMBOL(cl_page_get);
622 * Releases a reference to a page.
624 * When last reference is released, page is returned to the cache, unless it
625 * is in cl_page_state::CPS_FREEING state, in which case it is immediately
628 * \see cl_object_put(), cl_lock_put().
630 void cl_page_put(const struct lu_env *env, struct cl_page *page)
632 PASSERT(env, page, cfs_atomic_read(&page->cp_ref) > !!page->cp_parent);
635 CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
636 cfs_atomic_read(&page->cp_ref));
638 if (cfs_atomic_dec_and_test(&page->cp_ref)) {
639 LASSERT(page->cp_state == CPS_FREEING);
641 LASSERT(cfs_atomic_read(&page->cp_ref) == 0);
642 PASSERT(env, page, page->cp_owner == NULL);
643 PASSERT(env, page, cfs_list_empty(&page->cp_batch));
645 * Page is no longer reachable by other threads. Tear
648 cl_page_free(env, page);
653 EXPORT_SYMBOL(cl_page_put);
656 * Returns a VM page associated with a given cl_page.
658 struct page *cl_page_vmpage(const struct lu_env *env, struct cl_page *page)
660 const struct cl_page_slice *slice;
663 * Find uppermost layer with ->cpo_vmpage() method, and return its
666 page = cl_page_top(page);
668 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
669 if (slice->cpl_ops->cpo_vmpage != NULL)
670 RETURN(slice->cpl_ops->cpo_vmpage(env, slice));
672 page = page->cp_child;
673 } while (page != NULL);
674 LBUG(); /* ->cpo_vmpage() has to be defined somewhere in the stack */
676 EXPORT_SYMBOL(cl_page_vmpage);
679 * Returns a cl_page associated with a VM page, and given cl_object.
681 struct cl_page *cl_vmpage_page(struct page *vmpage, struct cl_object *obj)
684 struct cl_page *page;
687 KLASSERT(PageLocked(vmpage));
690 * NOTE: absence of races and liveness of data are guaranteed by page
691 * lock on a "vmpage". That works because object destruction has
692 * bottom-to-top pass.
696 * This loop assumes that ->private points to the top-most page. This
697 * can be rectified easily.
699 top = (struct cl_page *)vmpage->private;
703 for (page = top; page != NULL; page = page->cp_child) {
704 if (cl_object_same(page->cp_obj, obj)) {
705 cl_page_get_trust(page);
709 LASSERT(ergo(page, page->cp_type == CPT_CACHEABLE));
712 EXPORT_SYMBOL(cl_vmpage_page);
715 * Returns the top-page for a given page.
717 * \see cl_object_top(), cl_io_top()
719 struct cl_page *cl_page_top(struct cl_page *page)
721 return cl_page_top_trusted(page);
723 EXPORT_SYMBOL(cl_page_top);
725 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
726 const struct lu_device_type *dtype)
728 return cl_page_at_trusted(page, dtype);
730 EXPORT_SYMBOL(cl_page_at);
732 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
734 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...) \
736 const struct lu_env *__env = (_env); \
737 struct cl_page *__page = (_page); \
738 const struct cl_page_slice *__scan; \
740 ptrdiff_t __op = (_op); \
741 int (*__method)_proto; \
744 __page = cl_page_top(__page); \
746 cfs_list_for_each_entry(__scan, &__page->cp_layers, \
748 __method = *(void **)((char *)__scan->cpl_ops + \
750 if (__method != NULL) { \
751 __result = (*__method)(__env, __scan, \
757 __page = __page->cp_child; \
758 } while (__page != NULL && __result == 0); \
764 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...) \
766 const struct lu_env *__env = (_env); \
767 struct cl_page *__page = (_page); \
768 const struct cl_page_slice *__scan; \
769 ptrdiff_t __op = (_op); \
770 void (*__method)_proto; \
772 __page = cl_page_top(__page); \
774 cfs_list_for_each_entry(__scan, &__page->cp_layers, \
776 __method = *(void **)((char *)__scan->cpl_ops + \
778 if (__method != NULL) \
779 (*__method)(__env, __scan, \
782 __page = __page->cp_child; \
783 } while (__page != NULL); \
786 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...) \
788 const struct lu_env *__env = (_env); \
789 struct cl_page *__page = (_page); \
790 const struct cl_page_slice *__scan; \
791 ptrdiff_t __op = (_op); \
792 void (*__method)_proto; \
794 /* get to the bottom page. */ \
795 while (__page->cp_child != NULL) \
796 __page = __page->cp_child; \
798 cfs_list_for_each_entry_reverse(__scan, &__page->cp_layers, \
800 __method = *(void **)((char *)__scan->cpl_ops + \
802 if (__method != NULL) \
803 (*__method)(__env, __scan, \
806 __page = __page->cp_parent; \
807 } while (__page != NULL); \
810 static int cl_page_invoke(const struct lu_env *env,
811 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
814 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
816 RETURN(CL_PAGE_INVOKE(env, page, op,
817 (const struct lu_env *,
818 const struct cl_page_slice *, struct cl_io *),
822 static void cl_page_invoid(const struct lu_env *env,
823 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
826 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
828 CL_PAGE_INVOID(env, page, op,
829 (const struct lu_env *,
830 const struct cl_page_slice *, struct cl_io *), io);
834 static void cl_page_owner_clear(struct cl_page *page)
837 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
838 if (page->cp_owner != NULL) {
839 LASSERT(page->cp_owner->ci_owned_nr > 0);
840 page->cp_owner->ci_owned_nr--;
841 page->cp_owner = NULL;
842 page->cp_task = NULL;
848 static void cl_page_owner_set(struct cl_page *page)
851 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
852 LASSERT(page->cp_owner != NULL);
853 page->cp_owner->ci_owned_nr++;
858 void cl_page_disown0(const struct lu_env *env,
859 struct cl_io *io, struct cl_page *pg)
861 enum cl_page_state state;
864 state = pg->cp_state;
865 PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
866 PINVRNT(env, pg, cl_page_invariant(pg));
867 cl_page_owner_clear(pg);
869 if (state == CPS_OWNED)
870 cl_page_state_set(env, pg, CPS_CACHED);
872 * Completion call-backs are executed in the bottom-up order, so that
873 * uppermost layer (llite), responsible for VFS/VM interaction runs
874 * last and can release locks safely.
876 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
877 (const struct lu_env *,
878 const struct cl_page_slice *, struct cl_io *),
884 * returns true, iff page is owned by the given io.
886 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
888 LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
890 RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == io);
892 EXPORT_SYMBOL(cl_page_is_owned);
895 * Try to own a page by IO.
897 * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
898 * into cl_page_state::CPS_OWNED state.
900 * \pre !cl_page_is_owned(pg, io)
901 * \post result == 0 iff cl_page_is_owned(pg, io)
905 * \retval -ve failure, e.g., page was destroyed (and landed in
906 * cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
907 * or, page was owned by another thread, or in IO.
909 * \see cl_page_disown()
910 * \see cl_page_operations::cpo_own()
911 * \see cl_page_own_try()
914 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
915 struct cl_page *pg, int nonblock)
919 PINVRNT(env, pg, !cl_page_is_owned(pg, io));
922 pg = cl_page_top(pg);
925 if (pg->cp_state == CPS_FREEING) {
928 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
929 (const struct lu_env *,
930 const struct cl_page_slice *,
931 struct cl_io *, int),
934 PASSERT(env, pg, pg->cp_owner == NULL);
935 PASSERT(env, pg, pg->cp_req == NULL);
937 pg->cp_task = current;
938 cl_page_owner_set(pg);
939 if (pg->cp_state != CPS_FREEING) {
940 cl_page_state_set(env, pg, CPS_OWNED);
942 cl_page_disown0(env, io, pg);
947 PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
952 * Own a page, might be blocked.
954 * \see cl_page_own0()
956 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
958 return cl_page_own0(env, io, pg, 0);
960 EXPORT_SYMBOL(cl_page_own);
963 * Nonblock version of cl_page_own().
965 * \see cl_page_own0()
967 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
970 return cl_page_own0(env, io, pg, 1);
972 EXPORT_SYMBOL(cl_page_own_try);
976 * Assume page ownership.
978 * Called when page is already locked by the hosting VM.
980 * \pre !cl_page_is_owned(pg, io)
981 * \post cl_page_is_owned(pg, io)
983 * \see cl_page_operations::cpo_assume()
985 void cl_page_assume(const struct lu_env *env,
986 struct cl_io *io, struct cl_page *pg)
988 PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
991 pg = cl_page_top(pg);
994 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
995 PASSERT(env, pg, pg->cp_owner == NULL);
997 pg->cp_task = current;
998 cl_page_owner_set(pg);
999 cl_page_state_set(env, pg, CPS_OWNED);
1002 EXPORT_SYMBOL(cl_page_assume);
1005 * Releases page ownership without unlocking the page.
1007 * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
1008 * underlying VM page (as VM is supposed to do this itself).
1010 * \pre cl_page_is_owned(pg, io)
1011 * \post !cl_page_is_owned(pg, io)
1013 * \see cl_page_assume()
1015 void cl_page_unassume(const struct lu_env *env,
1016 struct cl_io *io, struct cl_page *pg)
1018 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1019 PINVRNT(env, pg, cl_page_invariant(pg));
1022 pg = cl_page_top(pg);
1024 cl_page_owner_clear(pg);
1025 cl_page_state_set(env, pg, CPS_CACHED);
1026 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
1027 (const struct lu_env *,
1028 const struct cl_page_slice *, struct cl_io *),
1032 EXPORT_SYMBOL(cl_page_unassume);
1035 * Releases page ownership.
1037 * Moves page into cl_page_state::CPS_CACHED.
1039 * \pre cl_page_is_owned(pg, io)
1040 * \post !cl_page_is_owned(pg, io)
1042 * \see cl_page_own()
1043 * \see cl_page_operations::cpo_disown()
1045 void cl_page_disown(const struct lu_env *env,
1046 struct cl_io *io, struct cl_page *pg)
1048 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1051 pg = cl_page_top(pg);
1053 cl_page_disown0(env, io, pg);
1056 EXPORT_SYMBOL(cl_page_disown);
1059 * Called when page is to be removed from the object, e.g., as a result of
1062 * Calls cl_page_operations::cpo_discard() top-to-bottom.
1064 * \pre cl_page_is_owned(pg, io)
1066 * \see cl_page_operations::cpo_discard()
1068 void cl_page_discard(const struct lu_env *env,
1069 struct cl_io *io, struct cl_page *pg)
1071 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1072 PINVRNT(env, pg, cl_page_invariant(pg));
1074 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
1076 EXPORT_SYMBOL(cl_page_discard);
1079 * Version of cl_page_delete() that can be called for not fully constructed
1080 * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
1081 * path. Doesn't check page invariant.
1083 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
1086 struct cl_page *tmp = pg;
1089 PASSERT(env, pg, pg == cl_page_top(pg));
1090 PASSERT(env, pg, pg->cp_state != CPS_FREEING);
1093 * Severe all ways to obtain new pointers to @pg.
1095 cl_page_owner_clear(pg);
1098 * unexport the page firstly before freeing it so that
1099 * the page content is considered to be invalid.
1100 * We have to do this because a CPS_FREEING cl_page may
1101 * be NOT under the protection of a cl_lock.
1102 * Afterwards, if this page is found by other threads, then this
1103 * page will be forced to reread.
1105 cl_page_export(env, pg, 0);
1106 cl_page_state_set0(env, pg, CPS_FREEING);
1108 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_delete),
1109 (const struct lu_env *, const struct cl_page_slice *));
1111 if (tmp->cp_type == CPT_CACHEABLE) {
1113 /* !radix means that @pg is not yet in the radix tree,
1117 for (; tmp != NULL; tmp = tmp->cp_child) {
1119 struct cl_object_header *hdr;
1121 hdr = cl_object_header(tmp->cp_obj);
1122 spin_lock(&hdr->coh_page_guard);
1123 value = radix_tree_delete(&hdr->coh_tree,
1125 PASSERT(env, tmp, value == tmp);
1126 PASSERT(env, tmp, hdr->coh_pages > 0);
1128 spin_unlock(&hdr->coh_page_guard);
1129 cl_page_put(env, tmp);
1137 * Called when a decision is made to throw page out of memory.
1139 * Notifies all layers about page destruction by calling
1140 * cl_page_operations::cpo_delete() method top-to-bottom.
1142 * Moves page into cl_page_state::CPS_FREEING state (this is the only place
1143 * where transition to this state happens).
1145 * Eliminates all venues through which new references to the page can be
1148 * - removes page from the radix trees,
1150 * - breaks linkage from VM page to cl_page.
1152 * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
1153 * drain after some time, at which point page will be recycled.
1155 * \pre pg == cl_page_top(pg)
1156 * \pre VM page is locked
1157 * \post pg->cp_state == CPS_FREEING
1159 * \see cl_page_operations::cpo_delete()
1161 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
1163 PINVRNT(env, pg, cl_page_invariant(pg));
1165 cl_page_delete0(env, pg, 1);
1168 EXPORT_SYMBOL(cl_page_delete);
1171 * Unmaps page from user virtual memory.
1173 * Calls cl_page_operations::cpo_unmap() through all layers top-to-bottom. The
1174 * layer responsible for VM interaction has to unmap page from user space
1177 * \see cl_page_operations::cpo_unmap()
1179 int cl_page_unmap(const struct lu_env *env,
1180 struct cl_io *io, struct cl_page *pg)
1182 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1183 PINVRNT(env, pg, cl_page_invariant(pg));
1185 return cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_unmap));
1187 EXPORT_SYMBOL(cl_page_unmap);
1190 * Marks page up-to-date.
1192 * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
1193 * layer responsible for VM interaction has to mark/clear page as up-to-date
1194 * by the \a uptodate argument.
1196 * \see cl_page_operations::cpo_export()
1198 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
1200 PINVRNT(env, pg, cl_page_invariant(pg));
1201 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
1202 (const struct lu_env *,
1203 const struct cl_page_slice *, int), uptodate);
1205 EXPORT_SYMBOL(cl_page_export);
1208 * Returns true, iff \a pg is VM locked in a suitable sense by the calling
1211 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
1214 const struct cl_page_slice *slice;
1217 pg = cl_page_top_trusted((struct cl_page *)pg);
1218 slice = container_of(pg->cp_layers.next,
1219 const struct cl_page_slice, cpl_linkage);
1220 PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
1222 * Call ->cpo_is_vmlocked() directly instead of going through
1223 * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
1224 * cl_page_invariant().
1226 result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
1227 PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
1228 RETURN(result == -EBUSY);
1230 EXPORT_SYMBOL(cl_page_is_vmlocked);
1232 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
1235 RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
1238 static void cl_page_io_start(const struct lu_env *env,
1239 struct cl_page *pg, enum cl_req_type crt)
1242 * Page is queued for IO, change its state.
1245 cl_page_owner_clear(pg);
1246 cl_page_state_set(env, pg, cl_req_type_state(crt));
1251 * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
1252 * called top-to-bottom. Every layer either agrees to submit this page (by
1253 * returning 0), or requests to omit this page (by returning -EALREADY). Layer
1254 * handling interactions with the VM also has to inform VM that page is under
1257 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
1258 struct cl_page *pg, enum cl_req_type crt)
1262 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1263 PINVRNT(env, pg, cl_page_invariant(pg));
1264 PINVRNT(env, pg, crt < CRT_NR);
1267 * XXX this has to be called bottom-to-top, so that llite can set up
1268 * PG_writeback without risking other layers deciding to skip this
1273 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
1275 cl_page_io_start(env, pg, crt);
1277 KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
1279 PageWriteback(cl_page_vmpage(env, pg)))));
1280 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1283 EXPORT_SYMBOL(cl_page_prep);
1286 * Notify layers about transfer completion.
1288 * Invoked by transfer sub-system (which is a part of osc) to notify layers
1289 * that a transfer, of which this page is a part of has completed.
1291 * Completion call-backs are executed in the bottom-up order, so that
1292 * uppermost layer (llite), responsible for the VFS/VM interaction runs last
1293 * and can release locks safely.
1295 * \pre pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1296 * \post pg->cp_state == CPS_CACHED
1298 * \see cl_page_operations::cpo_completion()
1300 void cl_page_completion(const struct lu_env *env,
1301 struct cl_page *pg, enum cl_req_type crt, int ioret)
1303 struct cl_sync_io *anchor = pg->cp_sync_io;
1305 PASSERT(env, pg, crt < CRT_NR);
1306 /* cl_page::cp_req already cleared by the caller (osc_completion()) */
1307 PASSERT(env, pg, pg->cp_req == NULL);
1308 PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
1311 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
1312 if (crt == CRT_READ && ioret == 0) {
1313 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
1314 pg->cp_flags |= CPF_READ_COMPLETED;
1317 cl_page_state_set(env, pg, CPS_CACHED);
1320 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
1321 (const struct lu_env *,
1322 const struct cl_page_slice *, int), ioret);
1324 LASSERT(cl_page_is_vmlocked(env, pg));
1325 LASSERT(pg->cp_sync_io == anchor);
1326 pg->cp_sync_io = NULL;
1329 * As page->cp_obj is pinned by a reference from page->cp_req, it is
1330 * safe to call cl_page_put() without risking object destruction in a
1331 * non-blocking context.
1333 cl_page_put(env, pg);
1336 cl_sync_io_note(anchor, ioret);
1340 EXPORT_SYMBOL(cl_page_completion);
1343 * Notify layers that transfer formation engine decided to yank this page from
1344 * the cache and to make it a part of a transfer.
1346 * \pre pg->cp_state == CPS_CACHED
1347 * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1349 * \see cl_page_operations::cpo_make_ready()
1351 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1352 enum cl_req_type crt)
1356 PINVRNT(env, pg, crt < CRT_NR);
1361 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1362 (const struct lu_env *,
1363 const struct cl_page_slice *));
1365 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1366 cl_page_io_start(env, pg, crt);
1368 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1371 EXPORT_SYMBOL(cl_page_make_ready);
1374 * Notify layers that high level io decided to place this page into a cache
1375 * for future transfer.
1377 * The layer implementing transfer engine (osc) has to register this page in
1380 * \pre cl_page_is_owned(pg, io)
1381 * \post cl_page_is_owned(pg, io)
1383 * \see cl_page_operations::cpo_cache_add()
1385 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1386 struct cl_page *pg, enum cl_req_type crt)
1388 const struct cl_page_slice *scan;
1391 PINVRNT(env, pg, crt < CRT_NR);
1392 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1393 PINVRNT(env, pg, cl_page_invariant(pg));
1400 cfs_list_for_each_entry(scan, &pg->cp_layers, cpl_linkage) {
1401 if (scan->cpl_ops->io[crt].cpo_cache_add == NULL)
1404 result = scan->cpl_ops->io[crt].cpo_cache_add(env, scan, io);
1408 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1411 EXPORT_SYMBOL(cl_page_cache_add);
1414 * Called if a pge is being written back by kernel's intention.
1416 * \pre cl_page_is_owned(pg, io)
1417 * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
1419 * \see cl_page_operations::cpo_flush()
1421 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
1426 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1427 PINVRNT(env, pg, cl_page_invariant(pg));
1431 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_flush));
1433 CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1436 EXPORT_SYMBOL(cl_page_flush);
1439 * Checks whether page is protected by any extent lock is at least required
1442 * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1443 * \see cl_page_operations::cpo_is_under_lock()
1445 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1446 struct cl_page *page)
1450 PINVRNT(env, page, cl_page_invariant(page));
1453 rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1454 (const struct lu_env *,
1455 const struct cl_page_slice *, struct cl_io *),
1457 PASSERT(env, page, rc != 0);
1460 EXPORT_SYMBOL(cl_page_is_under_lock);
1462 static int page_prune_cb(const struct lu_env *env, struct cl_io *io,
1463 struct cl_page *page, void *cbdata)
1465 cl_page_own(env, io, page);
1466 cl_page_unmap(env, io, page);
1467 cl_page_discard(env, io, page);
1468 cl_page_disown(env, io, page);
1469 return CLP_GANG_OKAY;
1473 * Purges all cached pages belonging to the object \a obj.
1475 int cl_pages_prune(const struct lu_env *env, struct cl_object *clobj)
1477 struct cl_thread_info *info;
1478 struct cl_object *obj = cl_object_top(clobj);
1483 info = cl_env_info(env);
1487 * initialize the io. This is ugly since we never do IO in this
1488 * function, we just make cl_page_list functions happy. -jay
1491 io->ci_ignore_layout = 1;
1492 result = cl_io_init(env, io, CIT_MISC, obj);
1494 cl_io_fini(env, io);
1495 RETURN(io->ci_result);
1499 result = cl_page_gang_lookup(env, obj, io, 0, CL_PAGE_EOF,
1500 page_prune_cb, NULL);
1501 if (result == CLP_GANG_RESCHED)
1503 } while (result != CLP_GANG_OKAY);
1505 cl_io_fini(env, io);
1508 EXPORT_SYMBOL(cl_pages_prune);
1511 * Tells transfer engine that only part of a page is to be transmitted.
1513 * \see cl_page_operations::cpo_clip()
1515 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1518 PINVRNT(env, pg, cl_page_invariant(pg));
1520 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1521 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1522 (const struct lu_env *,
1523 const struct cl_page_slice *,int, int),
1526 EXPORT_SYMBOL(cl_page_clip);
1529 * Prints human readable representation of \a pg to the \a f.
1531 void cl_page_header_print(const struct lu_env *env, void *cookie,
1532 lu_printer_t printer, const struct cl_page *pg)
1534 (*printer)(env, cookie,
1535 "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1536 pg, cfs_atomic_read(&pg->cp_ref), pg->cp_obj,
1537 pg->cp_index, pg->cp_parent, pg->cp_child,
1538 pg->cp_state, pg->cp_error, pg->cp_type,
1539 pg->cp_owner, pg->cp_req, pg->cp_flags);
1541 EXPORT_SYMBOL(cl_page_header_print);
1544 * Prints human readable representation of \a pg to the \a f.
1546 void cl_page_print(const struct lu_env *env, void *cookie,
1547 lu_printer_t printer, const struct cl_page *pg)
1549 struct cl_page *scan;
1551 for (scan = cl_page_top((struct cl_page *)pg);
1552 scan != NULL; scan = scan->cp_child)
1553 cl_page_header_print(env, cookie, printer, scan);
1554 CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1555 (const struct lu_env *env,
1556 const struct cl_page_slice *slice,
1557 void *cookie, lu_printer_t p), cookie, printer);
1558 (*printer)(env, cookie, "end page@%p\n", pg);
1560 EXPORT_SYMBOL(cl_page_print);
1563 * Cancel a page which is still in a transfer.
1565 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1567 return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1568 (const struct lu_env *,
1569 const struct cl_page_slice *));
1571 EXPORT_SYMBOL(cl_page_cancel);
1574 * Converts a byte offset within object \a obj into a page index.
1576 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1578 return (loff_t)idx << PAGE_CACHE_SHIFT;
1580 EXPORT_SYMBOL(cl_offset);
1583 * Converts a page index into a byte offset within object \a obj.
1585 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1587 return offset >> PAGE_CACHE_SHIFT;
1589 EXPORT_SYMBOL(cl_index);
1591 int cl_page_size(const struct cl_object *obj)
1593 return 1 << PAGE_CACHE_SHIFT;
1595 EXPORT_SYMBOL(cl_page_size);
1598 * Adds page slice to the compound page.
1600 * This is called by cl_object_operations::coo_page_init() methods to add a
1601 * per-layer state to the page. New state is added at the end of
1602 * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1604 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1606 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1607 struct cl_object *obj,
1608 const struct cl_page_operations *ops)
1611 cfs_list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1612 slice->cpl_obj = obj;
1613 slice->cpl_ops = ops;
1614 slice->cpl_page = page;
1617 EXPORT_SYMBOL(cl_page_slice_add);
1619 int cl_page_init(void)
1624 void cl_page_fini(void)