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>
39 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
42 #define DEBUG_SUBSYSTEM S_CLASS
44 #include <libcfs/libcfs.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <libcfs/list.h>
49 #include <cl_object.h>
50 #include "cl_internal.h"
52 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 keeping a lock on a VM page,
115 * associated with \a page.
117 * Use with care! Not exported.
119 static void cl_page_get_trust(struct cl_page *page)
121 LASSERT(cfs_atomic_read(&page->cp_ref) > 0);
122 cfs_atomic_inc(&page->cp_ref);
126 * Returns a slice within a page, corresponding to the given layer in the
131 static const struct cl_page_slice *
132 cl_page_at_trusted(const struct cl_page *page,
133 const struct lu_device_type *dtype)
135 const struct cl_page_slice *slice;
138 page = cl_page_top_trusted((struct cl_page *)page);
140 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
141 if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
144 page = page->cp_child;
145 } while (page != NULL);
149 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
151 struct cl_object *obj = page->cp_obj;
152 int pagesize = cl_object_header(obj)->coh_page_bufsize;
154 PASSERT(env, page, cfs_list_empty(&page->cp_batch));
155 PASSERT(env, page, page->cp_owner == NULL);
156 PASSERT(env, page, page->cp_req == NULL);
157 PASSERT(env, page, page->cp_parent == NULL);
158 PASSERT(env, page, page->cp_state == CPS_FREEING);
161 while (!cfs_list_empty(&page->cp_layers)) {
162 struct cl_page_slice *slice;
164 slice = cfs_list_entry(page->cp_layers.next,
165 struct cl_page_slice, cpl_linkage);
166 cfs_list_del_init(page->cp_layers.next);
167 slice->cpl_ops->cpo_fini(env, slice);
169 CS_PAGE_DEC(obj, total);
170 CS_PAGESTATE_DEC(obj, page->cp_state);
171 lu_object_ref_del_at(&obj->co_lu, &page->cp_obj_ref, "cl_page", page);
172 cl_object_put(env, obj);
173 lu_ref_fini(&page->cp_reference);
174 OBD_FREE(page, pagesize);
179 * Helper function updating page state. This is the only place in the code
180 * where cl_page::cp_state field is mutated.
182 static inline void cl_page_state_set_trust(struct cl_page *page,
183 enum cl_page_state state)
186 *(enum cl_page_state *)&page->cp_state = state;
189 struct cl_page *cl_page_alloc(const struct lu_env *env,
190 struct cl_object *o, pgoff_t ind, struct page *vmpage,
191 enum cl_page_type type)
193 struct cl_page *page;
194 struct lu_object_header *head;
197 OBD_ALLOC_GFP(page, cl_object_header(o)->coh_page_bufsize,
201 cfs_atomic_set(&page->cp_ref, 1);
204 lu_object_ref_add_at(&o->co_lu, &page->cp_obj_ref, "cl_page",
206 page->cp_index = ind;
207 cl_page_state_set_trust(page, CPS_CACHED);
208 page->cp_type = type;
209 CFS_INIT_LIST_HEAD(&page->cp_layers);
210 CFS_INIT_LIST_HEAD(&page->cp_batch);
211 CFS_INIT_LIST_HEAD(&page->cp_flight);
212 mutex_init(&page->cp_mutex);
213 lu_ref_init(&page->cp_reference);
214 head = o->co_lu.lo_header;
215 cfs_list_for_each_entry(o, &head->loh_layers,
217 if (o->co_ops->coo_page_init != NULL) {
218 result = o->co_ops->coo_page_init(env, o,
221 cl_page_delete0(env, page);
222 cl_page_free(env, page);
223 page = ERR_PTR(result);
229 CS_PAGE_INC(o, total);
230 CS_PAGE_INC(o, create);
231 CS_PAGESTATE_DEC(o, CPS_CACHED);
234 page = ERR_PTR(-ENOMEM);
238 EXPORT_SYMBOL(cl_page_alloc);
241 * Returns a cl_page with index \a idx at the object \a o, and associated with
242 * the VM page \a vmpage.
244 * This is the main entry point into the cl_page caching interface. First, a
245 * cache (implemented as a per-object radix tree) is consulted. If page is
246 * found there, it is returned immediately. Otherwise new page is allocated
247 * and returned. In any case, additional reference to page is acquired.
249 * \see cl_object_find(), cl_lock_find()
251 struct cl_page *cl_page_find(const struct lu_env *env,
253 pgoff_t idx, struct page *vmpage,
254 enum cl_page_type type)
256 struct cl_page *page = NULL;
257 struct cl_object_header *hdr;
259 LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
264 hdr = cl_object_header(o);
265 CS_PAGE_INC(o, lookup);
267 CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
268 idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
270 if (type == CPT_CACHEABLE) {
271 /* vmpage lock is used to protect the child/parent
273 KLASSERT(PageLocked(vmpage));
275 * cl_vmpage_page() can be called here without any locks as
277 * - "vmpage" is locked (which prevents ->private from
278 * concurrent updates), and
280 * - "o" cannot be destroyed while current thread holds a
283 page = cl_vmpage_page(vmpage, o);
290 /* allocate and initialize cl_page */
291 page = cl_page_alloc(env, o, idx, vmpage, type);
294 EXPORT_SYMBOL(cl_page_find);
296 static inline int cl_page_invariant(const struct cl_page *pg)
298 struct cl_page *parent;
299 struct cl_page *child;
303 * Page invariant is protected by a VM lock.
305 LINVRNT(cl_page_is_vmlocked(NULL, pg));
307 parent = pg->cp_parent;
308 child = pg->cp_child;
309 owner = pg->cp_owner;
311 return cl_page_in_use_noref(pg) &&
312 ergo(parent != NULL, parent->cp_child == pg) &&
313 ergo(child != NULL, child->cp_parent == pg) &&
314 ergo(child != NULL, pg->cp_obj != child->cp_obj) &&
315 ergo(parent != NULL, pg->cp_obj != parent->cp_obj) &&
316 ergo(owner != NULL && parent != NULL,
317 parent->cp_owner == pg->cp_owner->ci_parent) &&
318 ergo(owner != NULL && child != NULL,
319 child->cp_owner->ci_parent == owner);
322 static void cl_page_state_set0(const struct lu_env *env,
323 struct cl_page *page, enum cl_page_state state)
325 enum cl_page_state old;
328 * Matrix of allowed state transitions [old][new], for sanity
331 static const int allowed_transitions[CPS_NR][CPS_NR] = {
334 [CPS_OWNED] = 1, /* io finds existing cached page */
336 [CPS_PAGEOUT] = 1, /* write-out from the cache */
337 [CPS_FREEING] = 1, /* eviction on the memory pressure */
340 [CPS_CACHED] = 1, /* release to the cache */
342 [CPS_PAGEIN] = 1, /* start read immediately */
343 [CPS_PAGEOUT] = 1, /* start write immediately */
344 [CPS_FREEING] = 1, /* lock invalidation or truncate */
347 [CPS_CACHED] = 1, /* io completion */
354 [CPS_CACHED] = 1, /* io completion */
370 old = page->cp_state;
371 PASSERT(env, page, allowed_transitions[old][state]);
372 CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
373 for (; page != NULL; page = page->cp_child) {
374 PASSERT(env, page, page->cp_state == old);
376 equi(state == CPS_OWNED, page->cp_owner != NULL));
378 CS_PAGESTATE_DEC(page->cp_obj, page->cp_state);
379 CS_PAGESTATE_INC(page->cp_obj, state);
380 cl_page_state_set_trust(page, state);
385 static void cl_page_state_set(const struct lu_env *env,
386 struct cl_page *page, enum cl_page_state state)
388 cl_page_state_set0(env, page, state);
392 * Acquires an additional reference to a page.
394 * This can be called only by caller already possessing a reference to \a
397 * \see cl_object_get(), cl_lock_get().
399 void cl_page_get(struct cl_page *page)
402 cl_page_get_trust(page);
405 EXPORT_SYMBOL(cl_page_get);
408 * Releases a reference to a page.
410 * When last reference is released, page is returned to the cache, unless it
411 * is in cl_page_state::CPS_FREEING state, in which case it is immediately
414 * \see cl_object_put(), cl_lock_put().
416 void cl_page_put(const struct lu_env *env, struct cl_page *page)
418 PASSERT(env, page, cfs_atomic_read(&page->cp_ref) > !!page->cp_parent);
421 CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
422 cfs_atomic_read(&page->cp_ref));
424 if (cfs_atomic_dec_and_test(&page->cp_ref)) {
425 LASSERT(page->cp_state == CPS_FREEING);
427 LASSERT(cfs_atomic_read(&page->cp_ref) == 0);
428 PASSERT(env, page, page->cp_owner == NULL);
429 PASSERT(env, page, cfs_list_empty(&page->cp_batch));
431 * Page is no longer reachable by other threads. Tear
434 cl_page_free(env, page);
439 EXPORT_SYMBOL(cl_page_put);
442 * Returns a VM page associated with a given cl_page.
444 struct page *cl_page_vmpage(const struct lu_env *env, struct cl_page *page)
446 const struct cl_page_slice *slice;
449 * Find uppermost layer with ->cpo_vmpage() method, and return its
452 page = cl_page_top(page);
454 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
455 if (slice->cpl_ops->cpo_vmpage != NULL)
456 RETURN(slice->cpl_ops->cpo_vmpage(env, slice));
458 page = page->cp_child;
459 } while (page != NULL);
460 LBUG(); /* ->cpo_vmpage() has to be defined somewhere in the stack */
462 EXPORT_SYMBOL(cl_page_vmpage);
465 * Returns a cl_page associated with a VM page, and given cl_object.
467 struct cl_page *cl_vmpage_page(struct page *vmpage, struct cl_object *obj)
470 struct cl_page *page;
473 KLASSERT(PageLocked(vmpage));
476 * NOTE: absence of races and liveness of data are guaranteed by page
477 * lock on a "vmpage". That works because object destruction has
478 * bottom-to-top pass.
482 * This loop assumes that ->private points to the top-most page. This
483 * can be rectified easily.
485 top = (struct cl_page *)vmpage->private;
489 for (page = top; page != NULL; page = page->cp_child) {
490 if (cl_object_same(page->cp_obj, obj)) {
491 cl_page_get_trust(page);
495 LASSERT(ergo(page, page->cp_type == CPT_CACHEABLE));
498 EXPORT_SYMBOL(cl_vmpage_page);
501 * Returns the top-page for a given page.
503 * \see cl_object_top(), cl_io_top()
505 struct cl_page *cl_page_top(struct cl_page *page)
507 return cl_page_top_trusted(page);
509 EXPORT_SYMBOL(cl_page_top);
511 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
512 const struct lu_device_type *dtype)
514 return cl_page_at_trusted(page, dtype);
516 EXPORT_SYMBOL(cl_page_at);
518 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
520 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...) \
522 const struct lu_env *__env = (_env); \
523 struct cl_page *__page = (_page); \
524 const struct cl_page_slice *__scan; \
526 ptrdiff_t __op = (_op); \
527 int (*__method)_proto; \
530 __page = cl_page_top(__page); \
532 cfs_list_for_each_entry(__scan, &__page->cp_layers, \
534 __method = *(void **)((char *)__scan->cpl_ops + \
536 if (__method != NULL) { \
537 __result = (*__method)(__env, __scan, \
543 __page = __page->cp_child; \
544 } while (__page != NULL && __result == 0); \
550 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...) \
552 const struct lu_env *__env = (_env); \
553 struct cl_page *__page = (_page); \
554 const struct cl_page_slice *__scan; \
555 ptrdiff_t __op = (_op); \
556 void (*__method)_proto; \
558 __page = cl_page_top(__page); \
560 cfs_list_for_each_entry(__scan, &__page->cp_layers, \
562 __method = *(void **)((char *)__scan->cpl_ops + \
564 if (__method != NULL) \
565 (*__method)(__env, __scan, \
568 __page = __page->cp_child; \
569 } while (__page != NULL); \
572 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...) \
574 const struct lu_env *__env = (_env); \
575 struct cl_page *__page = (_page); \
576 const struct cl_page_slice *__scan; \
577 ptrdiff_t __op = (_op); \
578 void (*__method)_proto; \
580 /* get to the bottom page. */ \
581 while (__page->cp_child != NULL) \
582 __page = __page->cp_child; \
584 cfs_list_for_each_entry_reverse(__scan, &__page->cp_layers, \
586 __method = *(void **)((char *)__scan->cpl_ops + \
588 if (__method != NULL) \
589 (*__method)(__env, __scan, \
592 __page = __page->cp_parent; \
593 } while (__page != NULL); \
596 static int cl_page_invoke(const struct lu_env *env,
597 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
600 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
602 RETURN(CL_PAGE_INVOKE(env, page, op,
603 (const struct lu_env *,
604 const struct cl_page_slice *, struct cl_io *),
608 static void cl_page_invoid(const struct lu_env *env,
609 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
612 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
614 CL_PAGE_INVOID(env, page, op,
615 (const struct lu_env *,
616 const struct cl_page_slice *, struct cl_io *), io);
620 static void cl_page_owner_clear(struct cl_page *page)
623 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
624 if (page->cp_owner != NULL) {
625 LASSERT(page->cp_owner->ci_owned_nr > 0);
626 page->cp_owner->ci_owned_nr--;
627 page->cp_owner = NULL;
628 page->cp_task = NULL;
634 static void cl_page_owner_set(struct cl_page *page)
637 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
638 LASSERT(page->cp_owner != NULL);
639 page->cp_owner->ci_owned_nr++;
644 void cl_page_disown0(const struct lu_env *env,
645 struct cl_io *io, struct cl_page *pg)
647 enum cl_page_state state;
650 state = pg->cp_state;
651 PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
652 PINVRNT(env, pg, cl_page_invariant(pg) || state == CPS_FREEING);
653 cl_page_owner_clear(pg);
655 if (state == CPS_OWNED)
656 cl_page_state_set(env, pg, CPS_CACHED);
658 * Completion call-backs are executed in the bottom-up order, so that
659 * uppermost layer (llite), responsible for VFS/VM interaction runs
660 * last and can release locks safely.
662 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
663 (const struct lu_env *,
664 const struct cl_page_slice *, struct cl_io *),
670 * returns true, iff page is owned by the given io.
672 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
674 LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
676 RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == io);
678 EXPORT_SYMBOL(cl_page_is_owned);
681 * Try to own a page by IO.
683 * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
684 * into cl_page_state::CPS_OWNED state.
686 * \pre !cl_page_is_owned(pg, io)
687 * \post result == 0 iff cl_page_is_owned(pg, io)
691 * \retval -ve failure, e.g., page was destroyed (and landed in
692 * cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
693 * or, page was owned by another thread, or in IO.
695 * \see cl_page_disown()
696 * \see cl_page_operations::cpo_own()
697 * \see cl_page_own_try()
700 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
701 struct cl_page *pg, int nonblock)
705 PINVRNT(env, pg, !cl_page_is_owned(pg, io));
708 pg = cl_page_top(pg);
711 if (pg->cp_state == CPS_FREEING) {
714 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
715 (const struct lu_env *,
716 const struct cl_page_slice *,
717 struct cl_io *, int),
720 PASSERT(env, pg, pg->cp_owner == NULL);
721 PASSERT(env, pg, pg->cp_req == NULL);
723 pg->cp_task = current;
724 cl_page_owner_set(pg);
725 if (pg->cp_state != CPS_FREEING) {
726 cl_page_state_set(env, pg, CPS_OWNED);
728 cl_page_disown0(env, io, pg);
733 PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
738 * Own a page, might be blocked.
740 * \see cl_page_own0()
742 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
744 return cl_page_own0(env, io, pg, 0);
746 EXPORT_SYMBOL(cl_page_own);
749 * Nonblock version of cl_page_own().
751 * \see cl_page_own0()
753 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
756 return cl_page_own0(env, io, pg, 1);
758 EXPORT_SYMBOL(cl_page_own_try);
762 * Assume page ownership.
764 * Called when page is already locked by the hosting VM.
766 * \pre !cl_page_is_owned(pg, io)
767 * \post cl_page_is_owned(pg, io)
769 * \see cl_page_operations::cpo_assume()
771 void cl_page_assume(const struct lu_env *env,
772 struct cl_io *io, struct cl_page *pg)
774 PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
777 pg = cl_page_top(pg);
780 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
781 PASSERT(env, pg, pg->cp_owner == NULL);
783 pg->cp_task = current;
784 cl_page_owner_set(pg);
785 cl_page_state_set(env, pg, CPS_OWNED);
788 EXPORT_SYMBOL(cl_page_assume);
791 * Releases page ownership without unlocking the page.
793 * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
794 * underlying VM page (as VM is supposed to do this itself).
796 * \pre cl_page_is_owned(pg, io)
797 * \post !cl_page_is_owned(pg, io)
799 * \see cl_page_assume()
801 void cl_page_unassume(const struct lu_env *env,
802 struct cl_io *io, struct cl_page *pg)
804 PINVRNT(env, pg, cl_page_is_owned(pg, io));
805 PINVRNT(env, pg, cl_page_invariant(pg));
808 pg = cl_page_top(pg);
810 cl_page_owner_clear(pg);
811 cl_page_state_set(env, pg, CPS_CACHED);
812 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
813 (const struct lu_env *,
814 const struct cl_page_slice *, struct cl_io *),
818 EXPORT_SYMBOL(cl_page_unassume);
821 * Releases page ownership.
823 * Moves page into cl_page_state::CPS_CACHED.
825 * \pre cl_page_is_owned(pg, io)
826 * \post !cl_page_is_owned(pg, io)
829 * \see cl_page_operations::cpo_disown()
831 void cl_page_disown(const struct lu_env *env,
832 struct cl_io *io, struct cl_page *pg)
834 PINVRNT(env, pg, cl_page_is_owned(pg, io) ||
835 pg->cp_state == CPS_FREEING);
838 pg = cl_page_top(pg);
840 cl_page_disown0(env, io, pg);
843 EXPORT_SYMBOL(cl_page_disown);
846 * Called when page is to be removed from the object, e.g., as a result of
849 * Calls cl_page_operations::cpo_discard() top-to-bottom.
851 * \pre cl_page_is_owned(pg, io)
853 * \see cl_page_operations::cpo_discard()
855 void cl_page_discard(const struct lu_env *env,
856 struct cl_io *io, struct cl_page *pg)
858 PINVRNT(env, pg, cl_page_is_owned(pg, io));
859 PINVRNT(env, pg, cl_page_invariant(pg));
861 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
863 EXPORT_SYMBOL(cl_page_discard);
866 * Version of cl_page_delete() that can be called for not fully constructed
867 * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
868 * path. Doesn't check page invariant.
870 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg)
874 PASSERT(env, pg, pg == cl_page_top(pg));
875 PASSERT(env, pg, pg->cp_state != CPS_FREEING);
878 * Severe all ways to obtain new pointers to @pg.
880 cl_page_owner_clear(pg);
882 cl_page_state_set0(env, pg, CPS_FREEING);
884 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_delete),
885 (const struct lu_env *, const struct cl_page_slice *));
891 * Called when a decision is made to throw page out of memory.
893 * Notifies all layers about page destruction by calling
894 * cl_page_operations::cpo_delete() method top-to-bottom.
896 * Moves page into cl_page_state::CPS_FREEING state (this is the only place
897 * where transition to this state happens).
899 * Eliminates all venues through which new references to the page can be
902 * - removes page from the radix trees,
904 * - breaks linkage from VM page to cl_page.
906 * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
907 * drain after some time, at which point page will be recycled.
909 * \pre pg == cl_page_top(pg)
910 * \pre VM page is locked
911 * \post pg->cp_state == CPS_FREEING
913 * \see cl_page_operations::cpo_delete()
915 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
917 PINVRNT(env, pg, cl_page_invariant(pg));
919 cl_page_delete0(env, pg);
922 EXPORT_SYMBOL(cl_page_delete);
925 * Marks page up-to-date.
927 * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
928 * layer responsible for VM interaction has to mark/clear page as up-to-date
929 * by the \a uptodate argument.
931 * \see cl_page_operations::cpo_export()
933 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
935 PINVRNT(env, pg, cl_page_invariant(pg));
936 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
937 (const struct lu_env *,
938 const struct cl_page_slice *, int), uptodate);
940 EXPORT_SYMBOL(cl_page_export);
943 * Returns true, iff \a pg is VM locked in a suitable sense by the calling
946 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
949 const struct cl_page_slice *slice;
952 pg = cl_page_top_trusted((struct cl_page *)pg);
953 slice = container_of(pg->cp_layers.next,
954 const struct cl_page_slice, cpl_linkage);
955 PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
957 * Call ->cpo_is_vmlocked() directly instead of going through
958 * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
959 * cl_page_invariant().
961 result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
962 PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
963 RETURN(result == -EBUSY);
965 EXPORT_SYMBOL(cl_page_is_vmlocked);
967 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
970 RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
973 static void cl_page_io_start(const struct lu_env *env,
974 struct cl_page *pg, enum cl_req_type crt)
977 * Page is queued for IO, change its state.
980 cl_page_owner_clear(pg);
981 cl_page_state_set(env, pg, cl_req_type_state(crt));
986 * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
987 * called top-to-bottom. Every layer either agrees to submit this page (by
988 * returning 0), or requests to omit this page (by returning -EALREADY). Layer
989 * handling interactions with the VM also has to inform VM that page is under
992 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
993 struct cl_page *pg, enum cl_req_type crt)
997 PINVRNT(env, pg, cl_page_is_owned(pg, io));
998 PINVRNT(env, pg, cl_page_invariant(pg));
999 PINVRNT(env, pg, crt < CRT_NR);
1002 * XXX this has to be called bottom-to-top, so that llite can set up
1003 * PG_writeback without risking other layers deciding to skip this
1008 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
1010 cl_page_io_start(env, pg, crt);
1012 KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
1014 PageWriteback(cl_page_vmpage(env, pg)))));
1015 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1018 EXPORT_SYMBOL(cl_page_prep);
1021 * Notify layers about transfer completion.
1023 * Invoked by transfer sub-system (which is a part of osc) to notify layers
1024 * that a transfer, of which this page is a part of has completed.
1026 * Completion call-backs are executed in the bottom-up order, so that
1027 * uppermost layer (llite), responsible for the VFS/VM interaction runs last
1028 * and can release locks safely.
1030 * \pre pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1031 * \post pg->cp_state == CPS_CACHED
1033 * \see cl_page_operations::cpo_completion()
1035 void cl_page_completion(const struct lu_env *env,
1036 struct cl_page *pg, enum cl_req_type crt, int ioret)
1038 struct cl_sync_io *anchor = pg->cp_sync_io;
1040 PASSERT(env, pg, crt < CRT_NR);
1041 /* cl_page::cp_req already cleared by the caller (osc_completion()) */
1042 PASSERT(env, pg, pg->cp_req == NULL);
1043 PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
1046 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
1047 if (crt == CRT_READ && ioret == 0) {
1048 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
1049 pg->cp_flags |= CPF_READ_COMPLETED;
1052 cl_page_state_set(env, pg, CPS_CACHED);
1055 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
1056 (const struct lu_env *,
1057 const struct cl_page_slice *, int), ioret);
1059 LASSERT(cl_page_is_vmlocked(env, pg));
1060 LASSERT(pg->cp_sync_io == anchor);
1061 pg->cp_sync_io = NULL;
1064 * As page->cp_obj is pinned by a reference from page->cp_req, it is
1065 * safe to call cl_page_put() without risking object destruction in a
1066 * non-blocking context.
1068 cl_page_put(env, pg);
1071 cl_sync_io_note(anchor, ioret);
1075 EXPORT_SYMBOL(cl_page_completion);
1078 * Notify layers that transfer formation engine decided to yank this page from
1079 * the cache and to make it a part of a transfer.
1081 * \pre pg->cp_state == CPS_CACHED
1082 * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1084 * \see cl_page_operations::cpo_make_ready()
1086 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1087 enum cl_req_type crt)
1091 PINVRNT(env, pg, crt < CRT_NR);
1096 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1097 (const struct lu_env *,
1098 const struct cl_page_slice *));
1100 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1101 cl_page_io_start(env, pg, crt);
1103 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1106 EXPORT_SYMBOL(cl_page_make_ready);
1109 * Notify layers that high level io decided to place this page into a cache
1110 * for future transfer.
1112 * The layer implementing transfer engine (osc) has to register this page in
1115 * \pre cl_page_is_owned(pg, io)
1116 * \post cl_page_is_owned(pg, io)
1118 * \see cl_page_operations::cpo_cache_add()
1120 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1121 struct cl_page *pg, enum cl_req_type crt)
1123 const struct cl_page_slice *scan;
1126 PINVRNT(env, pg, crt < CRT_NR);
1127 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1128 PINVRNT(env, pg, cl_page_invariant(pg));
1135 cfs_list_for_each_entry(scan, &pg->cp_layers, cpl_linkage) {
1136 if (scan->cpl_ops->io[crt].cpo_cache_add == NULL)
1139 result = scan->cpl_ops->io[crt].cpo_cache_add(env, scan, io);
1143 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1146 EXPORT_SYMBOL(cl_page_cache_add);
1149 * Called if a pge is being written back by kernel's intention.
1151 * \pre cl_page_is_owned(pg, io)
1152 * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
1154 * \see cl_page_operations::cpo_flush()
1156 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
1161 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1162 PINVRNT(env, pg, cl_page_invariant(pg));
1166 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_flush));
1168 CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1171 EXPORT_SYMBOL(cl_page_flush);
1174 * Checks whether page is protected by any extent lock is at least required
1177 * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1178 * \see cl_page_operations::cpo_is_under_lock()
1180 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1181 struct cl_page *page)
1185 PINVRNT(env, page, cl_page_invariant(page));
1188 rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1189 (const struct lu_env *,
1190 const struct cl_page_slice *, struct cl_io *),
1192 PASSERT(env, page, rc != 0);
1195 EXPORT_SYMBOL(cl_page_is_under_lock);
1198 * Tells transfer engine that only part of a page is to be transmitted.
1200 * \see cl_page_operations::cpo_clip()
1202 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1205 PINVRNT(env, pg, cl_page_invariant(pg));
1207 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1208 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1209 (const struct lu_env *,
1210 const struct cl_page_slice *,int, int),
1213 EXPORT_SYMBOL(cl_page_clip);
1216 * Prints human readable representation of \a pg to the \a f.
1218 void cl_page_header_print(const struct lu_env *env, void *cookie,
1219 lu_printer_t printer, const struct cl_page *pg)
1221 (*printer)(env, cookie,
1222 "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1223 pg, cfs_atomic_read(&pg->cp_ref), pg->cp_obj,
1224 pg->cp_index, pg->cp_parent, pg->cp_child,
1225 pg->cp_state, pg->cp_error, pg->cp_type,
1226 pg->cp_owner, pg->cp_req, pg->cp_flags);
1228 EXPORT_SYMBOL(cl_page_header_print);
1231 * Prints human readable representation of \a pg to the \a f.
1233 void cl_page_print(const struct lu_env *env, void *cookie,
1234 lu_printer_t printer, const struct cl_page *pg)
1236 struct cl_page *scan;
1238 for (scan = cl_page_top((struct cl_page *)pg);
1239 scan != NULL; scan = scan->cp_child)
1240 cl_page_header_print(env, cookie, printer, scan);
1241 CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1242 (const struct lu_env *env,
1243 const struct cl_page_slice *slice,
1244 void *cookie, lu_printer_t p), cookie, printer);
1245 (*printer)(env, cookie, "end page@%p\n", pg);
1247 EXPORT_SYMBOL(cl_page_print);
1250 * Cancel a page which is still in a transfer.
1252 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1254 return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1255 (const struct lu_env *,
1256 const struct cl_page_slice *));
1258 EXPORT_SYMBOL(cl_page_cancel);
1261 * Converts a byte offset within object \a obj into a page index.
1263 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1265 return (loff_t)idx << PAGE_CACHE_SHIFT;
1267 EXPORT_SYMBOL(cl_offset);
1270 * Converts a page index into a byte offset within object \a obj.
1272 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1274 return offset >> PAGE_CACHE_SHIFT;
1276 EXPORT_SYMBOL(cl_index);
1278 int cl_page_size(const struct cl_object *obj)
1280 return 1 << PAGE_CACHE_SHIFT;
1282 EXPORT_SYMBOL(cl_page_size);
1285 * Adds page slice to the compound page.
1287 * This is called by cl_object_operations::coo_page_init() methods to add a
1288 * per-layer state to the page. New state is added at the end of
1289 * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1291 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1293 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1294 struct cl_object *obj,
1295 const struct cl_page_operations *ops)
1298 cfs_list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1299 slice->cpl_obj = obj;
1300 slice->cpl_ops = ops;
1301 slice->cpl_page = page;
1304 EXPORT_SYMBOL(cl_page_slice_add);
1306 int cl_page_init(void)
1311 void cl_page_fini(void)