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 "../../include/linux/libcfs/libcfs.h"
44 #include "../include/obd_class.h"
45 #include "../include/obd_support.h"
46 #include <linux/list.h>
48 #include "../include/cl_object.h"
49 #include "cl_internal.h"
51 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
54 # define PASSERT(env, page, expr) \
56 if (unlikely(!(expr))) { \
57 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
62 # define PINVRNT(env, page, exp) \
63 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
66 * Internal version of cl_page_top, it should be called if the page is
67 * known to be not freed, says with page referenced, or radix tree lock held,
70 static struct cl_page *cl_page_top_trusted(struct cl_page *page)
72 while (page->cp_parent != NULL)
73 page = page->cp_parent;
78 * Internal version of cl_page_get().
80 * This function can be used to obtain initial reference to previously
81 * unreferenced cached object. It can be called only if concurrent page
82 * reclamation is somehow prevented, e.g., by locking page radix-tree
83 * (cl_object_header::hdr->coh_page_guard), or by keeping a lock on a VM page,
84 * associated with \a page.
86 * Use with care! Not exported.
88 static void cl_page_get_trust(struct cl_page *page)
90 LASSERT(atomic_read(&page->cp_ref) > 0);
91 atomic_inc(&page->cp_ref);
95 * Returns a slice within a page, corresponding to the given layer in the
100 static const struct cl_page_slice *
101 cl_page_at_trusted(const struct cl_page *page,
102 const struct lu_device_type *dtype)
104 const struct cl_page_slice *slice;
106 page = cl_page_top_trusted((struct cl_page *)page);
108 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
109 if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
112 page = page->cp_child;
113 } while (page != NULL);
118 * Returns a page with given index in the given object, or NULL if no page is
119 * found. Acquires a reference on \a page.
121 * Locking: called under cl_object_header::coh_page_guard spin-lock.
123 struct cl_page *cl_page_lookup(struct cl_object_header *hdr, pgoff_t index)
125 struct cl_page *page;
127 assert_spin_locked(&hdr->coh_page_guard);
129 page = radix_tree_lookup(&hdr->coh_tree, index);
131 cl_page_get_trust(page);
134 EXPORT_SYMBOL(cl_page_lookup);
137 * Returns a list of pages by a given [start, end] of \a obj.
139 * \param resched If not NULL, then we give up before hogging CPU for too
140 * long and set *resched = 1, in that case caller should implement a retry
143 * Gang tree lookup (radix_tree_gang_lookup()) optimization is absolutely
144 * crucial in the face of [offset, EOF] locks.
146 * Return at least one page in @queue unless there is no covered page.
148 int cl_page_gang_lookup(const struct lu_env *env, struct cl_object *obj,
149 struct cl_io *io, pgoff_t start, pgoff_t end,
150 cl_page_gang_cb_t cb, void *cbdata)
152 struct cl_object_header *hdr;
153 struct cl_page *page;
154 struct cl_page **pvec;
155 const struct cl_page_slice *slice;
156 const struct lu_device_type *dtype;
161 int res = CLP_GANG_OKAY;
165 hdr = cl_object_header(obj);
166 pvec = cl_env_info(env)->clt_pvec;
167 dtype = cl_object_top(obj)->co_lu.lo_dev->ld_type;
168 spin_lock(&hdr->coh_page_guard);
169 while ((nr = radix_tree_gang_lookup(&hdr->coh_tree, (void **)pvec,
170 idx, CLT_PVEC_SIZE)) > 0) {
171 int end_of_region = 0;
173 idx = pvec[nr - 1]->cp_index + 1;
174 for (i = 0, j = 0; i < nr; ++i) {
178 LASSERT(page->cp_type == CPT_CACHEABLE);
179 if (page->cp_index > end) {
183 if (page->cp_state == CPS_FREEING)
186 slice = cl_page_at_trusted(page, dtype);
188 * Pages for lsm-less file has no underneath sub-page
189 * for osc, in case of ...
191 PASSERT(env, page, slice != NULL);
193 page = slice->cpl_page;
195 * Can safely call cl_page_get_trust() under
196 * radix-tree spin-lock.
198 * XXX not true, because @page is from object another
199 * than @hdr and protected by different tree lock.
201 cl_page_get_trust(page);
202 lu_ref_add_atomic(&page->cp_reference,
203 "gang_lookup", current);
208 * Here a delicate locking dance is performed. Current thread
209 * holds a reference to a page, but has to own it before it
210 * can be placed into queue. Owning implies waiting, so
211 * radix-tree lock is to be released. After a wait one has to
212 * check that pages weren't truncated (cl_page_own() returns
213 * error in the latter case).
215 spin_unlock(&hdr->coh_page_guard);
218 for (i = 0; i < j; ++i) {
220 if (res == CLP_GANG_OKAY)
221 res = (*cb)(env, io, page, cbdata);
222 lu_ref_del(&page->cp_reference,
223 "gang_lookup", current);
224 cl_page_put(env, page);
226 if (nr < CLT_PVEC_SIZE || end_of_region)
229 if (res == CLP_GANG_OKAY && need_resched())
230 res = CLP_GANG_RESCHED;
231 if (res != CLP_GANG_OKAY)
234 spin_lock(&hdr->coh_page_guard);
238 spin_unlock(&hdr->coh_page_guard);
241 EXPORT_SYMBOL(cl_page_gang_lookup);
243 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
245 struct cl_object *obj = page->cp_obj;
247 PASSERT(env, page, list_empty(&page->cp_batch));
248 PASSERT(env, page, page->cp_owner == NULL);
249 PASSERT(env, page, page->cp_req == NULL);
250 PASSERT(env, page, page->cp_parent == NULL);
251 PASSERT(env, page, page->cp_state == CPS_FREEING);
254 while (!list_empty(&page->cp_layers)) {
255 struct cl_page_slice *slice;
257 slice = list_entry(page->cp_layers.next,
258 struct cl_page_slice, cpl_linkage);
259 list_del_init(page->cp_layers.next);
260 slice->cpl_ops->cpo_fini(env, slice);
262 lu_object_ref_del_at(&obj->co_lu, &page->cp_obj_ref, "cl_page", page);
263 cl_object_put(env, obj);
264 lu_ref_fini(&page->cp_reference);
269 * Helper function updating page state. This is the only place in the code
270 * where cl_page::cp_state field is mutated.
272 static inline void cl_page_state_set_trust(struct cl_page *page,
273 enum cl_page_state state)
276 *(enum cl_page_state *)&page->cp_state = state;
279 static struct cl_page *cl_page_alloc(const struct lu_env *env,
280 struct cl_object *o, pgoff_t ind, struct page *vmpage,
281 enum cl_page_type type)
283 struct cl_page *page;
284 struct lu_object_header *head;
286 OBD_ALLOC_GFP(page, cl_object_header(o)->coh_page_bufsize,
291 atomic_set(&page->cp_ref, 1);
292 if (type == CPT_CACHEABLE) /* for radix tree */
293 atomic_inc(&page->cp_ref);
296 lu_object_ref_add_at(&o->co_lu, &page->cp_obj_ref, "cl_page",
298 page->cp_index = ind;
299 cl_page_state_set_trust(page, CPS_CACHED);
300 page->cp_type = type;
301 INIT_LIST_HEAD(&page->cp_layers);
302 INIT_LIST_HEAD(&page->cp_batch);
303 INIT_LIST_HEAD(&page->cp_flight);
304 mutex_init(&page->cp_mutex);
305 lu_ref_init(&page->cp_reference);
306 head = o->co_lu.lo_header;
307 list_for_each_entry(o, &head->loh_layers,
309 if (o->co_ops->coo_page_init != NULL) {
310 result = o->co_ops->coo_page_init(env, o,
313 cl_page_delete0(env, page, 0);
314 cl_page_free(env, page);
315 page = ERR_PTR(result);
321 page = ERR_PTR(-ENOMEM);
327 * Returns a cl_page with index \a idx at the object \a o, and associated with
328 * the VM page \a vmpage.
330 * This is the main entry point into the cl_page caching interface. First, a
331 * cache (implemented as a per-object radix tree) is consulted. If page is
332 * found there, it is returned immediately. Otherwise new page is allocated
333 * and returned. In any case, additional reference to page is acquired.
335 * \see cl_object_find(), cl_lock_find()
337 static struct cl_page *cl_page_find0(const struct lu_env *env,
339 pgoff_t idx, struct page *vmpage,
340 enum cl_page_type type,
341 struct cl_page *parent)
343 struct cl_page *page = NULL;
344 struct cl_page *ghost = NULL;
345 struct cl_object_header *hdr;
348 LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
351 hdr = cl_object_header(o);
353 CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
354 idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
356 if (type == CPT_CACHEABLE) {
358 * vmpage lock is used to protect the child/parent
361 KLASSERT(PageLocked(vmpage));
363 * cl_vmpage_page() can be called here without any locks as
365 * - "vmpage" is locked (which prevents ->private from
366 * concurrent updates), and
368 * - "o" cannot be destroyed while current thread holds a
371 page = cl_vmpage_page(vmpage, o);
374 cl_page_vmpage(env, page) == vmpage &&
375 (void *)radix_tree_lookup(&hdr->coh_tree,
382 /* allocate and initialize cl_page */
383 page = cl_page_alloc(env, o, idx, vmpage, type);
387 if (type == CPT_TRANSIENT) {
389 LASSERT(page->cp_parent == NULL);
390 page->cp_parent = parent;
391 parent->cp_child = page;
397 * XXX optimization: use radix_tree_preload() here, and change tree
398 * gfp mask to GFP_KERNEL in cl_object_header_init().
400 spin_lock(&hdr->coh_page_guard);
401 err = radix_tree_insert(&hdr->coh_tree, idx, page);
405 * Noted by Jay: a lock on \a vmpage protects cl_page_find()
406 * from this race, but
408 * 0. it's better to have cl_page interface "locally
409 * consistent" so that its correctness can be reasoned
410 * about without appealing to the (obscure world of) VM
413 * 1. handling this race allows ->coh_tree to remain
414 * consistent even when VM locking is somehow busted,
415 * which is very useful during diagnosing and debugging.
418 CL_PAGE_DEBUG(D_ERROR, env, ghost,
419 "fail to insert into radix tree: %d\n", err);
422 LASSERT(page->cp_parent == NULL);
423 page->cp_parent = parent;
424 parent->cp_child = page;
428 spin_unlock(&hdr->coh_page_guard);
430 if (unlikely(ghost != NULL)) {
431 cl_page_delete0(env, ghost, 0);
432 cl_page_free(env, ghost);
437 struct cl_page *cl_page_find(const struct lu_env *env, struct cl_object *o,
438 pgoff_t idx, struct page *vmpage,
439 enum cl_page_type type)
441 return cl_page_find0(env, o, idx, vmpage, type, NULL);
443 EXPORT_SYMBOL(cl_page_find);
446 struct cl_page *cl_page_find_sub(const struct lu_env *env, struct cl_object *o,
447 pgoff_t idx, struct page *vmpage,
448 struct cl_page *parent)
450 return cl_page_find0(env, o, idx, vmpage, parent->cp_type, parent);
452 EXPORT_SYMBOL(cl_page_find_sub);
454 static inline int cl_page_invariant(const struct cl_page *pg)
456 struct cl_object_header *header;
457 struct cl_page *parent;
458 struct cl_page *child;
462 * Page invariant is protected by a VM lock.
464 LINVRNT(cl_page_is_vmlocked(NULL, pg));
466 header = cl_object_header(pg->cp_obj);
467 parent = pg->cp_parent;
468 child = pg->cp_child;
469 owner = pg->cp_owner;
471 return cl_page_in_use(pg) &&
472 ergo(parent != NULL, parent->cp_child == pg) &&
473 ergo(child != NULL, child->cp_parent == pg) &&
474 ergo(child != NULL, pg->cp_obj != child->cp_obj) &&
475 ergo(parent != NULL, pg->cp_obj != parent->cp_obj) &&
476 ergo(owner != NULL && parent != NULL,
477 parent->cp_owner == pg->cp_owner->ci_parent) &&
478 ergo(owner != NULL && child != NULL,
479 child->cp_owner->ci_parent == owner) &&
481 * Either page is early in initialization (has neither child
482 * nor parent yet), or it is in the object radix tree.
484 ergo(pg->cp_state < CPS_FREEING && pg->cp_type == CPT_CACHEABLE,
485 (void *)radix_tree_lookup(&header->coh_tree,
486 pg->cp_index) == pg ||
487 (child == NULL && parent == NULL));
490 static void cl_page_state_set0(const struct lu_env *env,
491 struct cl_page *page, enum cl_page_state state)
493 enum cl_page_state old;
496 * Matrix of allowed state transitions [old][new], for sanity
499 static const int allowed_transitions[CPS_NR][CPS_NR] = {
502 [CPS_OWNED] = 1, /* io finds existing cached page */
504 [CPS_PAGEOUT] = 1, /* write-out from the cache */
505 [CPS_FREEING] = 1, /* eviction on the memory pressure */
508 [CPS_CACHED] = 1, /* release to the cache */
510 [CPS_PAGEIN] = 1, /* start read immediately */
511 [CPS_PAGEOUT] = 1, /* start write immediately */
512 [CPS_FREEING] = 1, /* lock invalidation or truncate */
515 [CPS_CACHED] = 1, /* io completion */
522 [CPS_CACHED] = 1, /* io completion */
537 old = page->cp_state;
538 PASSERT(env, page, allowed_transitions[old][state]);
539 CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
540 for (; page != NULL; page = page->cp_child) {
541 PASSERT(env, page, page->cp_state == old);
543 equi(state == CPS_OWNED, page->cp_owner != NULL));
545 cl_page_state_set_trust(page, state);
549 static void cl_page_state_set(const struct lu_env *env,
550 struct cl_page *page, enum cl_page_state state)
552 cl_page_state_set0(env, page, state);
556 * Acquires an additional reference to a page.
558 * This can be called only by caller already possessing a reference to \a
561 * \see cl_object_get(), cl_lock_get().
563 void cl_page_get(struct cl_page *page)
565 cl_page_get_trust(page);
567 EXPORT_SYMBOL(cl_page_get);
570 * Releases a reference to a page.
572 * When last reference is released, page is returned to the cache, unless it
573 * is in cl_page_state::CPS_FREEING state, in which case it is immediately
576 * \see cl_object_put(), cl_lock_put().
578 void cl_page_put(const struct lu_env *env, struct cl_page *page)
580 PASSERT(env, page, atomic_read(&page->cp_ref) > !!page->cp_parent);
582 CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
583 atomic_read(&page->cp_ref));
585 if (atomic_dec_and_test(&page->cp_ref)) {
586 LASSERT(page->cp_state == CPS_FREEING);
588 LASSERT(atomic_read(&page->cp_ref) == 0);
589 PASSERT(env, page, page->cp_owner == NULL);
590 PASSERT(env, page, list_empty(&page->cp_batch));
592 * Page is no longer reachable by other threads. Tear
595 cl_page_free(env, page);
598 EXPORT_SYMBOL(cl_page_put);
601 * Returns a VM page associated with a given cl_page.
603 struct page *cl_page_vmpage(const struct lu_env *env, struct cl_page *page)
605 const struct cl_page_slice *slice;
608 * Find uppermost layer with ->cpo_vmpage() method, and return its
611 page = cl_page_top(page);
613 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
614 if (slice->cpl_ops->cpo_vmpage != NULL)
615 return slice->cpl_ops->cpo_vmpage(env, slice);
617 page = page->cp_child;
618 } while (page != NULL);
619 LBUG(); /* ->cpo_vmpage() has to be defined somewhere in the stack */
621 EXPORT_SYMBOL(cl_page_vmpage);
624 * Returns a cl_page associated with a VM page, and given cl_object.
626 struct cl_page *cl_vmpage_page(struct page *vmpage, struct cl_object *obj)
629 struct cl_page *page;
631 KLASSERT(PageLocked(vmpage));
634 * NOTE: absence of races and liveness of data are guaranteed by page
635 * lock on a "vmpage". That works because object destruction has
636 * bottom-to-top pass.
640 * This loop assumes that ->private points to the top-most page. This
641 * can be rectified easily.
643 top = (struct cl_page *)vmpage->private;
647 for (page = top; page != NULL; page = page->cp_child) {
648 if (cl_object_same(page->cp_obj, obj)) {
649 cl_page_get_trust(page);
653 LASSERT(ergo(page, page->cp_type == CPT_CACHEABLE));
656 EXPORT_SYMBOL(cl_vmpage_page);
659 * Returns the top-page for a given page.
661 * \see cl_object_top(), cl_io_top()
663 struct cl_page *cl_page_top(struct cl_page *page)
665 return cl_page_top_trusted(page);
667 EXPORT_SYMBOL(cl_page_top);
669 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
670 const struct lu_device_type *dtype)
672 return cl_page_at_trusted(page, dtype);
674 EXPORT_SYMBOL(cl_page_at);
676 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
678 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...) \
680 const struct lu_env *__env = (_env); \
681 struct cl_page *__page = (_page); \
682 const struct cl_page_slice *__scan; \
684 ptrdiff_t __op = (_op); \
685 int (*__method)_proto; \
688 __page = cl_page_top(__page); \
690 list_for_each_entry(__scan, &__page->cp_layers, \
692 __method = *(void **)((char *)__scan->cpl_ops + \
694 if (__method != NULL) { \
695 __result = (*__method)(__env, __scan, \
701 __page = __page->cp_child; \
702 } while (__page != NULL && __result == 0); \
708 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...) \
710 const struct lu_env *__env = (_env); \
711 struct cl_page *__page = (_page); \
712 const struct cl_page_slice *__scan; \
713 ptrdiff_t __op = (_op); \
714 void (*__method)_proto; \
716 __page = cl_page_top(__page); \
718 list_for_each_entry(__scan, &__page->cp_layers, \
720 __method = *(void **)((char *)__scan->cpl_ops + \
722 if (__method != NULL) \
723 (*__method)(__env, __scan, \
726 __page = __page->cp_child; \
727 } while (__page != NULL); \
730 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...) \
732 const struct lu_env *__env = (_env); \
733 struct cl_page *__page = (_page); \
734 const struct cl_page_slice *__scan; \
735 ptrdiff_t __op = (_op); \
736 void (*__method)_proto; \
738 /* get to the bottom page. */ \
739 while (__page->cp_child != NULL) \
740 __page = __page->cp_child; \
742 list_for_each_entry_reverse(__scan, &__page->cp_layers, \
744 __method = *(void **)((char *)__scan->cpl_ops + \
746 if (__method != NULL) \
747 (*__method)(__env, __scan, \
750 __page = __page->cp_parent; \
751 } while (__page != NULL); \
754 static int cl_page_invoke(const struct lu_env *env,
755 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
758 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
759 return CL_PAGE_INVOKE(env, page, op,
760 (const struct lu_env *,
761 const struct cl_page_slice *, struct cl_io *),
765 static void cl_page_invoid(const struct lu_env *env,
766 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
769 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
770 CL_PAGE_INVOID(env, page, op,
771 (const struct lu_env *,
772 const struct cl_page_slice *, struct cl_io *), io);
775 static void cl_page_owner_clear(struct cl_page *page)
777 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
778 if (page->cp_owner != NULL) {
779 LASSERT(page->cp_owner->ci_owned_nr > 0);
780 page->cp_owner->ci_owned_nr--;
781 page->cp_owner = NULL;
782 page->cp_task = NULL;
787 static void cl_page_owner_set(struct cl_page *page)
789 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
790 LASSERT(page->cp_owner != NULL);
791 page->cp_owner->ci_owned_nr++;
795 void cl_page_disown0(const struct lu_env *env,
796 struct cl_io *io, struct cl_page *pg)
798 enum cl_page_state state;
800 state = pg->cp_state;
801 PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
802 PINVRNT(env, pg, cl_page_invariant(pg));
803 cl_page_owner_clear(pg);
805 if (state == CPS_OWNED)
806 cl_page_state_set(env, pg, CPS_CACHED);
808 * Completion call-backs are executed in the bottom-up order, so that
809 * uppermost layer (llite), responsible for VFS/VM interaction runs
810 * last and can release locks safely.
812 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
813 (const struct lu_env *,
814 const struct cl_page_slice *, struct cl_io *),
819 * returns true, iff page is owned by the given io.
821 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
823 LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
824 return pg->cp_state == CPS_OWNED && pg->cp_owner == io;
826 EXPORT_SYMBOL(cl_page_is_owned);
829 * Try to own a page by IO.
831 * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
832 * into cl_page_state::CPS_OWNED state.
834 * \pre !cl_page_is_owned(pg, io)
835 * \post result == 0 iff cl_page_is_owned(pg, io)
839 * \retval -ve failure, e.g., page was destroyed (and landed in
840 * cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
841 * or, page was owned by another thread, or in IO.
843 * \see cl_page_disown()
844 * \see cl_page_operations::cpo_own()
845 * \see cl_page_own_try()
848 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
849 struct cl_page *pg, int nonblock)
853 PINVRNT(env, pg, !cl_page_is_owned(pg, io));
855 pg = cl_page_top(pg);
858 if (pg->cp_state == CPS_FREEING) {
861 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
862 (const struct lu_env *,
863 const struct cl_page_slice *,
864 struct cl_io *, int),
867 PASSERT(env, pg, pg->cp_owner == NULL);
868 PASSERT(env, pg, pg->cp_req == NULL);
870 pg->cp_task = current;
871 cl_page_owner_set(pg);
872 if (pg->cp_state != CPS_FREEING) {
873 cl_page_state_set(env, pg, CPS_OWNED);
875 cl_page_disown0(env, io, pg);
880 PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
885 * Own a page, might be blocked.
887 * \see cl_page_own0()
889 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
891 return cl_page_own0(env, io, pg, 0);
893 EXPORT_SYMBOL(cl_page_own);
896 * Nonblock version of cl_page_own().
898 * \see cl_page_own0()
900 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
903 return cl_page_own0(env, io, pg, 1);
905 EXPORT_SYMBOL(cl_page_own_try);
909 * Assume page ownership.
911 * Called when page is already locked by the hosting VM.
913 * \pre !cl_page_is_owned(pg, io)
914 * \post cl_page_is_owned(pg, io)
916 * \see cl_page_operations::cpo_assume()
918 void cl_page_assume(const struct lu_env *env,
919 struct cl_io *io, struct cl_page *pg)
921 PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
923 pg = cl_page_top(pg);
926 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
927 PASSERT(env, pg, pg->cp_owner == NULL);
929 pg->cp_task = current;
930 cl_page_owner_set(pg);
931 cl_page_state_set(env, pg, CPS_OWNED);
933 EXPORT_SYMBOL(cl_page_assume);
936 * Releases page ownership without unlocking the page.
938 * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
939 * underlying VM page (as VM is supposed to do this itself).
941 * \pre cl_page_is_owned(pg, io)
942 * \post !cl_page_is_owned(pg, io)
944 * \see cl_page_assume()
946 void cl_page_unassume(const struct lu_env *env,
947 struct cl_io *io, struct cl_page *pg)
949 PINVRNT(env, pg, cl_page_is_owned(pg, io));
950 PINVRNT(env, pg, cl_page_invariant(pg));
952 pg = cl_page_top(pg);
954 cl_page_owner_clear(pg);
955 cl_page_state_set(env, pg, CPS_CACHED);
956 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
957 (const struct lu_env *,
958 const struct cl_page_slice *, struct cl_io *),
961 EXPORT_SYMBOL(cl_page_unassume);
964 * Releases page ownership.
966 * Moves page into cl_page_state::CPS_CACHED.
968 * \pre cl_page_is_owned(pg, io)
969 * \post !cl_page_is_owned(pg, io)
972 * \see cl_page_operations::cpo_disown()
974 void cl_page_disown(const struct lu_env *env,
975 struct cl_io *io, struct cl_page *pg)
977 PINVRNT(env, pg, cl_page_is_owned(pg, io));
979 pg = cl_page_top(pg);
981 cl_page_disown0(env, io, pg);
983 EXPORT_SYMBOL(cl_page_disown);
986 * Called when page is to be removed from the object, e.g., as a result of
989 * Calls cl_page_operations::cpo_discard() top-to-bottom.
991 * \pre cl_page_is_owned(pg, io)
993 * \see cl_page_operations::cpo_discard()
995 void cl_page_discard(const struct lu_env *env,
996 struct cl_io *io, struct cl_page *pg)
998 PINVRNT(env, pg, cl_page_is_owned(pg, io));
999 PINVRNT(env, pg, cl_page_invariant(pg));
1001 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
1003 EXPORT_SYMBOL(cl_page_discard);
1006 * Version of cl_page_delete() that can be called for not fully constructed
1007 * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
1008 * path. Doesn't check page invariant.
1010 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
1013 struct cl_page *tmp = pg;
1015 PASSERT(env, pg, pg == cl_page_top(pg));
1016 PASSERT(env, pg, pg->cp_state != CPS_FREEING);
1019 * Severe all ways to obtain new pointers to @pg.
1021 cl_page_owner_clear(pg);
1024 * unexport the page firstly before freeing it so that
1025 * the page content is considered to be invalid.
1026 * We have to do this because a CPS_FREEING cl_page may
1027 * be NOT under the protection of a cl_lock.
1028 * Afterwards, if this page is found by other threads, then this
1029 * page will be forced to reread.
1031 cl_page_export(env, pg, 0);
1032 cl_page_state_set0(env, pg, CPS_FREEING);
1034 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_delete),
1035 (const struct lu_env *, const struct cl_page_slice *));
1037 if (tmp->cp_type == CPT_CACHEABLE) {
1039 /* !radix means that @pg is not yet in the radix tree,
1043 for (; tmp != NULL; tmp = tmp->cp_child) {
1045 struct cl_object_header *hdr;
1047 hdr = cl_object_header(tmp->cp_obj);
1048 spin_lock(&hdr->coh_page_guard);
1049 value = radix_tree_delete(&hdr->coh_tree,
1051 PASSERT(env, tmp, value == tmp);
1052 PASSERT(env, tmp, hdr->coh_pages > 0);
1054 spin_unlock(&hdr->coh_page_guard);
1055 cl_page_put(env, tmp);
1061 * Called when a decision is made to throw page out of memory.
1063 * Notifies all layers about page destruction by calling
1064 * cl_page_operations::cpo_delete() method top-to-bottom.
1066 * Moves page into cl_page_state::CPS_FREEING state (this is the only place
1067 * where transition to this state happens).
1069 * Eliminates all venues through which new references to the page can be
1072 * - removes page from the radix trees,
1074 * - breaks linkage from VM page to cl_page.
1076 * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
1077 * drain after some time, at which point page will be recycled.
1079 * \pre pg == cl_page_top(pg)
1080 * \pre VM page is locked
1081 * \post pg->cp_state == CPS_FREEING
1083 * \see cl_page_operations::cpo_delete()
1085 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
1087 PINVRNT(env, pg, cl_page_invariant(pg));
1088 cl_page_delete0(env, pg, 1);
1090 EXPORT_SYMBOL(cl_page_delete);
1093 * Unmaps page from user virtual memory.
1095 * Calls cl_page_operations::cpo_unmap() through all layers top-to-bottom. The
1096 * layer responsible for VM interaction has to unmap page from user space
1099 * \see cl_page_operations::cpo_unmap()
1101 int cl_page_unmap(const struct lu_env *env,
1102 struct cl_io *io, struct cl_page *pg)
1104 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1105 PINVRNT(env, pg, cl_page_invariant(pg));
1107 return cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_unmap));
1109 EXPORT_SYMBOL(cl_page_unmap);
1112 * Marks page up-to-date.
1114 * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
1115 * layer responsible for VM interaction has to mark/clear page as up-to-date
1116 * by the \a uptodate argument.
1118 * \see cl_page_operations::cpo_export()
1120 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
1122 PINVRNT(env, pg, cl_page_invariant(pg));
1123 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
1124 (const struct lu_env *,
1125 const struct cl_page_slice *, int), uptodate);
1127 EXPORT_SYMBOL(cl_page_export);
1130 * Returns true, iff \a pg is VM locked in a suitable sense by the calling
1133 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
1136 const struct cl_page_slice *slice;
1138 pg = cl_page_top_trusted((struct cl_page *)pg);
1139 slice = container_of(pg->cp_layers.next,
1140 const struct cl_page_slice, cpl_linkage);
1141 PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
1143 * Call ->cpo_is_vmlocked() directly instead of going through
1144 * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
1145 * cl_page_invariant().
1147 result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
1148 PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
1149 return result == -EBUSY;
1151 EXPORT_SYMBOL(cl_page_is_vmlocked);
1153 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
1155 return crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN;
1158 static void cl_page_io_start(const struct lu_env *env,
1159 struct cl_page *pg, enum cl_req_type crt)
1162 * Page is queued for IO, change its state.
1164 cl_page_owner_clear(pg);
1165 cl_page_state_set(env, pg, cl_req_type_state(crt));
1169 * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
1170 * called top-to-bottom. Every layer either agrees to submit this page (by
1171 * returning 0), or requests to omit this page (by returning -EALREADY). Layer
1172 * handling interactions with the VM also has to inform VM that page is under
1175 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
1176 struct cl_page *pg, enum cl_req_type crt)
1180 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1181 PINVRNT(env, pg, cl_page_invariant(pg));
1182 PINVRNT(env, pg, crt < CRT_NR);
1185 * XXX this has to be called bottom-to-top, so that llite can set up
1186 * PG_writeback without risking other layers deciding to skip this
1191 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
1193 cl_page_io_start(env, pg, crt);
1195 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1198 EXPORT_SYMBOL(cl_page_prep);
1201 * Notify layers about transfer completion.
1203 * Invoked by transfer sub-system (which is a part of osc) to notify layers
1204 * that a transfer, of which this page is a part of has completed.
1206 * Completion call-backs are executed in the bottom-up order, so that
1207 * uppermost layer (llite), responsible for the VFS/VM interaction runs last
1208 * and can release locks safely.
1210 * \pre pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1211 * \post pg->cp_state == CPS_CACHED
1213 * \see cl_page_operations::cpo_completion()
1215 void cl_page_completion(const struct lu_env *env,
1216 struct cl_page *pg, enum cl_req_type crt, int ioret)
1218 struct cl_sync_io *anchor = pg->cp_sync_io;
1220 PASSERT(env, pg, crt < CRT_NR);
1221 /* cl_page::cp_req already cleared by the caller (osc_completion()) */
1222 PASSERT(env, pg, pg->cp_req == NULL);
1223 PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
1225 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
1226 if (crt == CRT_READ && ioret == 0) {
1227 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
1228 pg->cp_flags |= CPF_READ_COMPLETED;
1231 cl_page_state_set(env, pg, CPS_CACHED);
1234 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
1235 (const struct lu_env *,
1236 const struct cl_page_slice *, int), ioret);
1238 LASSERT(cl_page_is_vmlocked(env, pg));
1239 LASSERT(pg->cp_sync_io == anchor);
1240 pg->cp_sync_io = NULL;
1243 * As page->cp_obj is pinned by a reference from page->cp_req, it is
1244 * safe to call cl_page_put() without risking object destruction in a
1245 * non-blocking context.
1247 cl_page_put(env, pg);
1250 cl_sync_io_note(anchor, ioret);
1252 EXPORT_SYMBOL(cl_page_completion);
1255 * Notify layers that transfer formation engine decided to yank this page from
1256 * the cache and to make it a part of a transfer.
1258 * \pre pg->cp_state == CPS_CACHED
1259 * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1261 * \see cl_page_operations::cpo_make_ready()
1263 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1264 enum cl_req_type crt)
1268 PINVRNT(env, pg, crt < CRT_NR);
1272 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1273 (const struct lu_env *,
1274 const struct cl_page_slice *));
1276 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1277 cl_page_io_start(env, pg, crt);
1279 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1282 EXPORT_SYMBOL(cl_page_make_ready);
1285 * Notify layers that high level io decided to place this page into a cache
1286 * for future transfer.
1288 * The layer implementing transfer engine (osc) has to register this page in
1291 * \pre cl_page_is_owned(pg, io)
1292 * \post cl_page_is_owned(pg, io)
1294 * \see cl_page_operations::cpo_cache_add()
1296 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1297 struct cl_page *pg, enum cl_req_type crt)
1299 const struct cl_page_slice *scan;
1302 PINVRNT(env, pg, crt < CRT_NR);
1303 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1304 PINVRNT(env, pg, cl_page_invariant(pg));
1309 list_for_each_entry(scan, &pg->cp_layers, cpl_linkage) {
1310 if (scan->cpl_ops->io[crt].cpo_cache_add == NULL)
1313 result = scan->cpl_ops->io[crt].cpo_cache_add(env, scan, io);
1317 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1320 EXPORT_SYMBOL(cl_page_cache_add);
1323 * Called if a pge is being written back by kernel's intention.
1325 * \pre cl_page_is_owned(pg, io)
1326 * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
1328 * \see cl_page_operations::cpo_flush()
1330 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
1335 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1336 PINVRNT(env, pg, cl_page_invariant(pg));
1338 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_flush));
1340 CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1343 EXPORT_SYMBOL(cl_page_flush);
1346 * Checks whether page is protected by any extent lock is at least required
1349 * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1350 * \see cl_page_operations::cpo_is_under_lock()
1352 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1353 struct cl_page *page)
1357 PINVRNT(env, page, cl_page_invariant(page));
1359 rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1360 (const struct lu_env *,
1361 const struct cl_page_slice *, struct cl_io *),
1363 PASSERT(env, page, rc != 0);
1366 EXPORT_SYMBOL(cl_page_is_under_lock);
1368 static int page_prune_cb(const struct lu_env *env, struct cl_io *io,
1369 struct cl_page *page, void *cbdata)
1371 cl_page_own(env, io, page);
1372 cl_page_unmap(env, io, page);
1373 cl_page_discard(env, io, page);
1374 cl_page_disown(env, io, page);
1375 return CLP_GANG_OKAY;
1379 * Purges all cached pages belonging to the object \a obj.
1381 int cl_pages_prune(const struct lu_env *env, struct cl_object *clobj)
1383 struct cl_thread_info *info;
1384 struct cl_object *obj = cl_object_top(clobj);
1388 info = cl_env_info(env);
1392 * initialize the io. This is ugly since we never do IO in this
1393 * function, we just make cl_page_list functions happy. -jay
1396 io->ci_ignore_layout = 1;
1397 result = cl_io_init(env, io, CIT_MISC, obj);
1399 cl_io_fini(env, io);
1400 return io->ci_result;
1404 result = cl_page_gang_lookup(env, obj, io, 0, CL_PAGE_EOF,
1405 page_prune_cb, NULL);
1406 if (result == CLP_GANG_RESCHED)
1408 } while (result != CLP_GANG_OKAY);
1410 cl_io_fini(env, io);
1413 EXPORT_SYMBOL(cl_pages_prune);
1416 * Tells transfer engine that only part of a page is to be transmitted.
1418 * \see cl_page_operations::cpo_clip()
1420 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1423 PINVRNT(env, pg, cl_page_invariant(pg));
1425 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1426 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1427 (const struct lu_env *,
1428 const struct cl_page_slice *, int, int),
1431 EXPORT_SYMBOL(cl_page_clip);
1434 * Prints human readable representation of \a pg to the \a f.
1436 void cl_page_header_print(const struct lu_env *env, void *cookie,
1437 lu_printer_t printer, const struct cl_page *pg)
1439 (*printer)(env, cookie,
1440 "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1441 pg, atomic_read(&pg->cp_ref), pg->cp_obj,
1442 pg->cp_index, pg->cp_parent, pg->cp_child,
1443 pg->cp_state, pg->cp_error, pg->cp_type,
1444 pg->cp_owner, pg->cp_req, pg->cp_flags);
1446 EXPORT_SYMBOL(cl_page_header_print);
1449 * Prints human readable representation of \a pg to the \a f.
1451 void cl_page_print(const struct lu_env *env, void *cookie,
1452 lu_printer_t printer, const struct cl_page *pg)
1454 struct cl_page *scan;
1456 for (scan = cl_page_top((struct cl_page *)pg);
1457 scan != NULL; scan = scan->cp_child)
1458 cl_page_header_print(env, cookie, printer, scan);
1459 CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1460 (const struct lu_env *env,
1461 const struct cl_page_slice *slice,
1462 void *cookie, lu_printer_t p), cookie, printer);
1463 (*printer)(env, cookie, "end page@%p\n", pg);
1465 EXPORT_SYMBOL(cl_page_print);
1468 * Cancel a page which is still in a transfer.
1470 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1472 return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1473 (const struct lu_env *,
1474 const struct cl_page_slice *));
1476 EXPORT_SYMBOL(cl_page_cancel);
1479 * Converts a byte offset within object \a obj into a page index.
1481 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1486 return (loff_t)idx << PAGE_CACHE_SHIFT;
1488 EXPORT_SYMBOL(cl_offset);
1491 * Converts a page index into a byte offset within object \a obj.
1493 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1498 return offset >> PAGE_CACHE_SHIFT;
1500 EXPORT_SYMBOL(cl_index);
1502 int cl_page_size(const struct cl_object *obj)
1504 return 1 << PAGE_CACHE_SHIFT;
1506 EXPORT_SYMBOL(cl_page_size);
1509 * Adds page slice to the compound page.
1511 * This is called by cl_object_operations::coo_page_init() methods to add a
1512 * per-layer state to the page. New state is added at the end of
1513 * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1515 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1517 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1518 struct cl_object *obj,
1519 const struct cl_page_operations *ops)
1521 list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1522 slice->cpl_obj = obj;
1523 slice->cpl_ops = ops;
1524 slice->cpl_page = page;
1526 EXPORT_SYMBOL(cl_page_slice_add);
1528 int cl_page_init(void)
1533 void cl_page_fini(void)