1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
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 # define EXPORT_SYMTAB
46 #include <libcfs/libcfs.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <libcfs/list.h>
51 #include <cl_object.h>
52 #include "cl_internal.h"
54 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
57 static cfs_mem_cache_t *cl_page_kmem = NULL;
59 static struct lu_kmem_descr cl_page_caches[] = {
61 .ckd_cache = &cl_page_kmem,
62 .ckd_name = "cl_page_kmem",
63 .ckd_size = sizeof (struct cl_page)
71 # define PASSERT(env, page, expr) \
73 if (unlikely(!(expr))) { \
74 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
78 #else /* !LIBCFS_DEBUG */
79 # define PASSERT(env, page, exp) \
80 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
81 #endif /* !LIBCFS_DEBUG */
83 #ifdef INVARIANT_CHECK
84 # define PINVRNT(env, page, expr) \
86 if (unlikely(!(expr))) { \
87 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
91 #else /* !INVARIANT_CHECK */
92 # define PINVRNT(env, page, exp) \
93 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
94 #endif /* !INVARIANT_CHECK */
97 * Internal version of cl_page_top, it should be called with page referenced,
98 * or coh_page_guard held.
100 static struct cl_page *cl_page_top_trusted(struct cl_page *page)
102 LASSERT(cl_is_page(page));
103 while (page->cp_parent != NULL)
104 page = page->cp_parent;
109 * Internal version of cl_page_get().
111 * This function can be used to obtain initial reference to previously
112 * unreferenced cached object. It can be called only if concurrent page
113 * reclamation is somehow prevented, e.g., by locking page radix-tree
114 * (cl_object_header::hdr->coh_page_guard), or 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(cl_is_page(page));
123 * Checkless version for trusted users.
125 if (atomic_inc_return(&page->cp_ref) == 1)
126 atomic_inc(&cl_object_site(page->cp_obj)->cs_pages.cs_busy);
130 * Returns a slice within a page, corresponding to the given layer in the
135 static const struct cl_page_slice *
136 cl_page_at_trusted(const struct cl_page *page,
137 const struct lu_device_type *dtype)
139 const struct cl_page_slice *slice;
141 #ifdef INVARIANT_CHECK
142 struct cl_object_header *ch = cl_object_header(page->cp_obj);
144 if (!atomic_read(&page->cp_ref))
145 LASSERT_SPIN_LOCKED(&ch->coh_page_guard);
149 page = cl_page_top_trusted((struct cl_page *)page);
151 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
152 if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
155 page = page->cp_child;
156 } while (page != NULL);
161 * Returns a page with given index in the given object, or NULL if no page is
162 * found. Acquires a reference on \a page.
164 * Locking: called under cl_object_header::coh_page_guard spin-lock.
166 struct cl_page *cl_page_lookup(struct cl_object_header *hdr, pgoff_t index)
168 struct cl_page *page;
170 LASSERT_SPIN_LOCKED(&hdr->coh_page_guard);
172 page = radix_tree_lookup(&hdr->coh_tree, index);
174 LASSERT(cl_is_page(page));
175 cl_page_get_trust(page);
179 EXPORT_SYMBOL(cl_page_lookup);
182 * Returns a list of pages by a given [start, end] of @obj.
184 * Gang tree lookup (radix_tree_gang_lookup()) optimization is absolutely
185 * crucial in the face of [offset, EOF] locks.
187 void cl_page_gang_lookup(const struct lu_env *env, struct cl_object *obj,
188 struct cl_io *io, pgoff_t start, pgoff_t end,
189 struct cl_page_list *queue)
191 struct cl_object_header *hdr;
192 struct cl_page *page;
193 struct cl_page **pvec;
194 const struct cl_page_slice *slice;
195 const struct lu_device_type *dtype;
203 hdr = cl_object_header(obj);
204 pvec = cl_env_info(env)->clt_pvec;
205 dtype = cl_object_top(obj)->co_lu.lo_dev->ld_type;
206 spin_lock(&hdr->coh_page_guard);
207 while ((nr = radix_tree_gang_lookup(&hdr->coh_tree, (void **)pvec,
208 idx, CLT_PVEC_SIZE)) > 0) {
209 idx = pvec[nr - 1]->cp_index + 1;
210 for (i = 0, j = 0; i < nr; ++i) {
212 PASSERT(env, page, cl_is_page(page));
214 if (page->cp_index > end)
216 if (page->cp_state == CPS_FREEING)
218 if (page->cp_type == CPT_TRANSIENT) {
219 /* God, we found a transient page!*/
223 slice = cl_page_at_trusted(page, dtype);
225 * Pages for lsm-less file has no underneath sub-page
226 * for osc, in case of ...
228 PASSERT(env, page, slice != NULL);
229 page = slice->cpl_page;
231 * Can safely call cl_page_get_trust() under
232 * radix-tree spin-lock.
234 * XXX not true, because @page is from object another
235 * than @hdr and protected by different tree lock.
237 cl_page_get_trust(page);
238 lu_ref_add_atomic(&page->cp_reference,
239 "page_list", cfs_current());
244 * Here a delicate locking dance is performed. Current thread
245 * holds a reference to a page, but has to own it before it
246 * can be placed into queue. Owning implies waiting, so
247 * radix-tree lock is to be released. After a wait one has to
248 * check that pages weren't truncated (cl_page_own() returns
249 * error in the latter case).
251 spin_unlock(&hdr->coh_page_guard);
252 for (i = 0; i < j; ++i) {
254 if (cl_page_own(env, io, page) == 0)
255 cl_page_list_add(queue, page);
256 lu_ref_del(&page->cp_reference,
257 "page_list", cfs_current());
258 cl_page_put(env, page);
260 spin_lock(&hdr->coh_page_guard);
261 if (nr < CLT_PVEC_SIZE)
264 spin_unlock(&hdr->coh_page_guard);
267 EXPORT_SYMBOL(cl_page_gang_lookup);
269 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
271 struct cl_object *obj = page->cp_obj;
272 struct cl_site *site = cl_object_site(obj);
274 PASSERT(env, page, cl_is_page(page));
275 PASSERT(env, page, list_empty(&page->cp_batch));
276 PASSERT(env, page, page->cp_owner == NULL);
277 PASSERT(env, page, page->cp_req == NULL);
278 PASSERT(env, page, page->cp_parent == NULL);
279 PASSERT(env, page, page->cp_state == CPS_FREEING);
283 while (!list_empty(&page->cp_layers)) {
284 struct cl_page_slice *slice;
286 slice = list_entry(page->cp_layers.next, struct cl_page_slice,
288 list_del_init(page->cp_layers.next);
289 slice->cpl_ops->cpo_fini(env, slice);
291 atomic_dec(&site->cs_pages.cs_total);
292 atomic_dec(&site->cs_pages_state[page->cp_state]);
293 lu_object_ref_del_at(&obj->co_lu, page->cp_obj_ref, "cl_page", page);
294 cl_object_put(env, obj);
295 lu_ref_fini(&page->cp_reference);
296 OBD_SLAB_FREE_PTR(page, cl_page_kmem);
301 * Helper function updating page state. This is the only place in the code
302 * where cl_page::cp_state field is mutated.
304 static inline void cl_page_state_set_trust(struct cl_page *page,
305 enum cl_page_state state)
308 *(enum cl_page_state *)&page->cp_state = state;
311 static int cl_page_alloc(const struct lu_env *env, struct cl_object *o,
312 pgoff_t ind, struct page *vmpage,
313 enum cl_page_type type, struct cl_page **out)
315 struct cl_page *page;
316 struct cl_page *err = NULL;
317 struct lu_object_header *head;
318 struct cl_site *site = cl_object_site(o);
323 OBD_SLAB_ALLOC_PTR(page, cl_page_kmem);
325 atomic_set(&page->cp_ref, 1);
328 page->cp_obj_ref = lu_object_ref_add(&o->co_lu,
330 page->cp_index = ind;
331 cl_page_state_set_trust(page, CPS_CACHED);
332 page->cp_type = type;
333 CFS_INIT_LIST_HEAD(&page->cp_layers);
334 CFS_INIT_LIST_HEAD(&page->cp_batch);
335 CFS_INIT_LIST_HEAD(&page->cp_flight);
336 mutex_init(&page->cp_mutex);
337 lu_ref_init(&page->cp_reference);
338 head = o->co_lu.lo_header;
339 list_for_each_entry(o, &head->loh_layers, co_lu.lo_linkage) {
340 if (o->co_ops->coo_page_init != NULL) {
341 err = o->co_ops->coo_page_init(env, o,
344 cl_page_state_set_trust(page,
346 cl_page_free(env, page);
353 atomic_inc(&site->cs_pages.cs_busy);
354 atomic_inc(&site->cs_pages.cs_total);
355 atomic_inc(&site->cs_pages_state[CPS_CACHED]);
356 atomic_inc(&site->cs_pages.cs_created);
360 page = ERR_PTR(-ENOMEM);
366 * Returns a cl_page with index \a idx at the object \a o, and associated with
367 * the VM page \a vmpage.
369 * This is the main entry point into the cl_page caching interface. First, a
370 * cache (implemented as a per-object radix tree) is consulted. If page is
371 * found there, it is returned immediately. Otherwise new page is allocated
372 * and returned. In any case, additional reference to page is acquired.
374 * \see cl_object_find(), cl_lock_find()
376 struct cl_page *cl_page_find(const struct lu_env *env, struct cl_object *o,
377 pgoff_t idx, struct page *vmpage,
378 enum cl_page_type type)
380 struct cl_page *page;
381 struct cl_page *ghost = NULL;
382 struct cl_object_header *hdr;
383 struct cl_site *site = cl_object_site(o);
386 LINVRNT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
391 hdr = cl_object_header(o);
392 atomic_inc(&site->cs_pages.cs_lookup);
394 CDEBUG(D_PAGE, "%lu@"DFID" %p %lu %i\n",
395 idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
397 if (type == CPT_CACHEABLE) {
399 * cl_vmpage_page() can be called here without any locks as
401 * - "vmpage" is locked (which prevents ->private from
402 * concurrent updates), and
404 * - "o" cannot be destroyed while current thread holds a
407 page = cl_vmpage_page(vmpage, o);
410 cl_page_vmpage(env, page) == vmpage &&
411 (void *)radix_tree_lookup(&hdr->coh_tree,
414 spin_lock(&hdr->coh_page_guard);
415 page = cl_page_lookup(hdr, idx);
416 spin_unlock(&hdr->coh_page_guard);
419 atomic_inc(&site->cs_pages.cs_hit);
423 /* allocate and initialize cl_page */
424 err = cl_page_alloc(env, o, idx, vmpage, type, &page);
428 * XXX optimization: use radix_tree_preload() here, and change tree
429 * gfp mask to GFP_KERNEL in cl_object_header_init().
431 spin_lock(&hdr->coh_page_guard);
432 err = radix_tree_insert(&hdr->coh_tree, idx, page);
436 * Noted by Jay: a lock on \a vmpage protects cl_page_find()
437 * from this race, but
439 * 0. it's better to have cl_page interface "locally
440 * consistent" so that its correctness can be reasoned
441 * about without appealing to the (obscure world of) VM
444 * 1. handling this race allows ->coh_tree to remain
445 * consistent even when VM locking is somehow busted,
446 * which is very useful during diagnosing and debugging.
448 if (err == -EEXIST) {
450 * XXX in case of a lookup for CPT_TRANSIENT page,
451 * nothing protects a CPT_CACHEABLE page from being
452 * concurrently moved into CPS_FREEING state.
454 page = cl_page_lookup(hdr, idx);
455 PASSERT(env, page, page != NULL);
456 if (page->cp_type == CPT_TRANSIENT &&
457 type == CPT_CACHEABLE) {
458 /* XXX: We should make sure that inode sem
459 * keeps being held in the lifetime of
460 * transient pages, so it is impossible to
461 * have conflicting transient pages.
463 spin_unlock(&hdr->coh_page_guard);
464 cl_page_put(env, page);
465 spin_lock(&hdr->coh_page_guard);
466 page = ERR_PTR(-EBUSY);
472 spin_unlock(&hdr->coh_page_guard);
474 if (unlikely(ghost != NULL)) {
475 atomic_dec(&site->cs_pages.cs_busy);
476 cl_page_delete0(env, ghost, 0);
477 cl_page_free(env, ghost);
481 EXPORT_SYMBOL(cl_page_find);
483 static inline int cl_page_invariant(const struct cl_page *pg)
485 struct cl_object_header *header;
486 struct cl_page *parent;
487 struct cl_page *child;
490 LASSERT(cl_is_page(pg));
492 * Page invariant is protected by a VM lock.
494 LINVRNT(cl_page_is_vmlocked(NULL, pg));
496 header = cl_object_header(pg->cp_obj);
497 parent = pg->cp_parent;
498 child = pg->cp_child;
499 owner = pg->cp_owner;
501 return atomic_read(&pg->cp_ref) > 0 &&
502 ergo(parent != NULL, parent->cp_child == pg) &&
503 ergo(child != NULL, child->cp_parent == pg) &&
504 ergo(child != NULL, pg->cp_obj != child->cp_obj) &&
505 ergo(parent != NULL, pg->cp_obj != parent->cp_obj) &&
506 ergo(owner != NULL && parent != NULL,
507 parent->cp_owner == pg->cp_owner->ci_parent) &&
508 ergo(owner != NULL && child != NULL,
509 child->cp_owner->ci_parent == owner) &&
511 * Either page is early in initialization (has neither child
512 * nor parent yet), or it is in the object radix tree.
514 ergo(pg->cp_state < CPS_FREEING,
515 (void *)radix_tree_lookup(&header->coh_tree,
516 pg->cp_index) == pg ||
517 (child == NULL && parent == NULL));
520 static void cl_page_state_set0(const struct lu_env *env,
521 struct cl_page *page, enum cl_page_state state)
523 enum cl_page_state old;
524 struct cl_site *site = cl_object_site(page->cp_obj);
527 * Matrix of allowed state transitions [old][new], for sanity
530 static const int allowed_transitions[CPS_NR][CPS_NR] = {
533 [CPS_OWNED] = 1, /* io finds existing cached page */
535 [CPS_PAGEOUT] = 1, /* write-out from the cache */
536 [CPS_FREEING] = 1, /* eviction on the memory pressure */
539 [CPS_CACHED] = 1, /* release to the cache */
541 [CPS_PAGEIN] = 1, /* start read immediately */
542 [CPS_PAGEOUT] = 1, /* start write immediately */
543 [CPS_FREEING] = 1, /* lock invalidation or truncate */
546 [CPS_CACHED] = 1, /* io completion */
553 [CPS_CACHED] = 1, /* io completion */
569 old = page->cp_state;
570 PASSERT(env, page, allowed_transitions[old][state]);
571 CL_PAGE_HEADER(D_TRACE, env, page, "%i -> %i\n", old, state);
572 for (; page != NULL; page = page->cp_child) {
573 PASSERT(env, page, page->cp_state == old);
575 equi(state == CPS_OWNED, page->cp_owner != NULL));
577 atomic_dec(&site->cs_pages_state[page->cp_state]);
578 atomic_inc(&site->cs_pages_state[state]);
579 cl_page_state_set_trust(page, state);
584 static void cl_page_state_set(const struct lu_env *env,
585 struct cl_page *page, enum cl_page_state state)
587 PINVRNT(env, page, cl_page_invariant(page));
588 cl_page_state_set0(env, page, state);
592 * Acquires an additional reference to a page.
594 * This can be called only by caller already possessing a reference to \a
597 * \see cl_object_get(), cl_lock_get().
599 void cl_page_get(struct cl_page *page)
602 LASSERT(page->cp_state != CPS_FREEING);
603 cl_page_get_trust(page);
606 EXPORT_SYMBOL(cl_page_get);
609 * Releases a reference to a page.
611 * When last reference is released, page is returned to the cache, unless it
612 * is in cl_page_state::CPS_FREEING state, in which case it is immediately
615 * \see cl_object_put(), cl_lock_put().
617 void cl_page_put(const struct lu_env *env, struct cl_page *page)
619 struct cl_object_header *hdr;
620 struct cl_site *site = cl_object_site(page->cp_obj);
622 PASSERT(env, page, atomic_read(&page->cp_ref) > !!page->cp_parent);
625 CL_PAGE_HEADER(D_TRACE, env, page, "%i\n", atomic_read(&page->cp_ref));
626 hdr = cl_object_header(page->cp_obj);
627 if (atomic_dec_and_test(&page->cp_ref)) {
628 atomic_dec(&site->cs_pages.cs_busy);
629 if (page->cp_state == CPS_FREEING) {
630 PASSERT(env, page, page->cp_owner == NULL);
631 PASSERT(env, page, list_empty(&page->cp_batch));
633 * Page is no longer reachable by other threads. Tear
636 cl_page_free(env, page);
641 EXPORT_SYMBOL(cl_page_put);
644 * Returns a VM page associated with a given cl_page.
646 cfs_page_t *cl_page_vmpage(const struct lu_env *env, struct cl_page *page)
648 const struct cl_page_slice *slice;
651 * Find uppermost layer with ->cpo_vmpage() method, and return its
654 page = cl_page_top(page);
656 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
657 if (slice->cpl_ops->cpo_vmpage != NULL)
658 RETURN(slice->cpl_ops->cpo_vmpage(env, slice));
660 page = page->cp_child;
661 } while (page != NULL);
662 LBUG(); /* ->cpo_vmpage() has to be defined somewhere in the stack */
664 EXPORT_SYMBOL(cl_page_vmpage);
667 * Returns a cl_page associated with a VM page, and given cl_object.
669 struct cl_page *cl_vmpage_page(cfs_page_t *vmpage, struct cl_object *obj)
671 struct cl_page *page;
674 KLASSERT(PageLocked(vmpage));
677 * NOTE: absence of races and liveness of data are guaranteed by page
678 * lock on a "vmpage". That works because object destruction has
679 * bottom-to-top pass.
683 * This loop assumes that ->private points to the top-most page. This
684 * can be rectified easily.
686 for (page = (void *)vmpage->private;
687 page != NULL; page = page->cp_child) {
688 if (cl_object_same(page->cp_obj, obj)) {
689 cl_page_get_trust(page);
693 LASSERT(ergo(page, cl_is_page(page) && page->cp_type == CPT_CACHEABLE));
696 EXPORT_SYMBOL(cl_vmpage_page);
699 * Returns the top-page for a given page.
701 * \see cl_object_top(), cl_io_top()
703 struct cl_page *cl_page_top(struct cl_page *page)
705 return cl_page_top_trusted(page);
707 EXPORT_SYMBOL(cl_page_top);
710 * Returns true if \a addr is an address of an allocated cl_page. Used in
711 * assertions. This check is optimistically imprecise, i.e., it occasionally
712 * returns true for the incorrect addresses, but if it returns false, then the
713 * address is guaranteed to be incorrect. (Should be named cl_pagep().)
717 int cl_is_page(const void *addr)
719 return cfs_mem_is_in_cache(addr, cl_page_kmem);
721 EXPORT_SYMBOL(cl_is_page);
723 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
724 const struct lu_device_type *dtype)
726 return cl_page_at_trusted(page, dtype);
728 EXPORT_SYMBOL(cl_page_at);
730 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
732 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...) \
734 const struct lu_env *__env = (_env); \
735 struct cl_page *__page = (_page); \
736 const struct cl_page_slice *__scan; \
738 ptrdiff_t __op = (_op); \
739 int (*__method)_proto; \
742 __page = cl_page_top(__page); \
744 list_for_each_entry(__scan, &__page->cp_layers, \
746 __method = *(void **)((char *)__scan->cpl_ops + \
748 if (__method != NULL) { \
749 __result = (*__method)(__env, __scan, \
755 __page = __page->cp_child; \
756 } while (__page != NULL && __result == 0); \
762 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...) \
764 const struct lu_env *__env = (_env); \
765 struct cl_page *__page = (_page); \
766 const struct cl_page_slice *__scan; \
767 ptrdiff_t __op = (_op); \
768 void (*__method)_proto; \
770 __page = cl_page_top(__page); \
772 list_for_each_entry(__scan, &__page->cp_layers, \
774 __method = *(void **)((char *)__scan->cpl_ops + \
776 if (__method != NULL) \
777 (*__method)(__env, __scan, \
780 __page = __page->cp_child; \
781 } while (__page != NULL); \
784 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...) \
786 const struct lu_env *__env = (_env); \
787 struct cl_page *__page = (_page); \
788 const struct cl_page_slice *__scan; \
789 ptrdiff_t __op = (_op); \
790 void (*__method)_proto; \
792 /* get to the bottom page. */ \
793 while (__page->cp_child != NULL) \
794 __page = __page->cp_child; \
796 list_for_each_entry_reverse(__scan, &__page->cp_layers, \
798 __method = *(void **)((char *)__scan->cpl_ops + \
800 if (__method != NULL) \
801 (*__method)(__env, __scan, \
804 __page = __page->cp_parent; \
805 } while (__page != NULL); \
808 static int cl_page_invoke(const struct lu_env *env,
809 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
812 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
814 RETURN(CL_PAGE_INVOKE(env, page, op,
815 (const struct lu_env *,
816 const struct cl_page_slice *, struct cl_io *),
820 static void cl_page_invoid(const struct lu_env *env,
821 struct cl_io *io, struct cl_page *page, ptrdiff_t op)
824 PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
826 CL_PAGE_INVOID(env, page, op,
827 (const struct lu_env *,
828 const struct cl_page_slice *, struct cl_io *), io);
832 static void cl_page_owner_clear(struct cl_page *page)
835 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
836 if (page->cp_owner != NULL) {
837 LASSERT(page->cp_owner->ci_owned_nr > 0);
838 page->cp_owner->ci_owned_nr--;
839 page->cp_owner = NULL;
845 static void cl_page_owner_set(struct cl_page *page)
848 for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
849 LASSERT(page->cp_owner != NULL);
850 page->cp_owner->ci_owned_nr++;
855 void cl_page_disown0(const struct lu_env *env,
856 struct cl_io *io, struct cl_page *pg)
858 enum cl_page_state state;
861 state = pg->cp_state;
862 PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
863 PINVRNT(env, pg, cl_page_invariant(pg));
864 cl_page_owner_clear(pg);
866 if (state == CPS_OWNED)
867 cl_page_state_set(env, pg, CPS_CACHED);
869 * Completion call-backs are executed in the bottom-up order, so that
870 * uppermost layer (llite), responsible for VFS/VM interaction runs
871 * last and can release locks safely.
873 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
874 (const struct lu_env *,
875 const struct cl_page_slice *, struct cl_io *),
881 * returns true, iff page is owned by the given io.
883 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
885 LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
887 RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == io);
889 EXPORT_SYMBOL(cl_page_is_owned);
894 * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
895 * into cl_page_state::CPS_OWNED state.
897 * \pre !cl_page_is_owned(pg, io)
898 * \post result == 0 iff cl_page_is_owned(pg, io)
902 * \retval -ve failure, e.g., page was destroyed (and landed in
903 * cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
905 * \see cl_page_disown()
906 * \see cl_page_operations::cpo_own()
908 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
912 PINVRNT(env, pg, !cl_page_is_owned(pg, io));
915 pg = cl_page_top(pg);
918 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_own));
919 PASSERT(env, pg, pg->cp_owner == NULL);
920 PASSERT(env, pg, pg->cp_req == NULL);
922 cl_page_owner_set(pg);
923 if (pg->cp_state != CPS_FREEING) {
924 cl_page_state_set(env, pg, CPS_OWNED);
927 cl_page_disown0(env, io, pg);
930 PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
933 EXPORT_SYMBOL(cl_page_own);
936 * Assume page ownership.
938 * Called when page is already locked by the hosting VM.
940 * \pre !cl_page_is_owned(pg, io)
941 * \post cl_page_is_owned(pg, io)
943 * \see cl_page_operations::cpo_assume()
945 void cl_page_assume(const struct lu_env *env,
946 struct cl_io *io, struct cl_page *pg)
948 PASSERT(env, pg, pg->cp_state < CPS_OWNED);
949 PASSERT(env, pg, pg->cp_owner == NULL);
950 PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
951 PINVRNT(env, pg, cl_page_invariant(pg));
954 pg = cl_page_top(pg);
957 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
959 cl_page_owner_set(pg);
960 cl_page_state_set(env, pg, CPS_OWNED);
963 EXPORT_SYMBOL(cl_page_assume);
966 * Releases page ownership without unlocking the page.
968 * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
969 * underlying VM page (as VM is supposed to do this itself).
971 * \pre cl_page_is_owned(pg, io)
972 * \post !cl_page_is_owned(pg, io)
974 * \see cl_page_assume()
976 void cl_page_unassume(const struct lu_env *env,
977 struct cl_io *io, struct cl_page *pg)
979 PINVRNT(env, pg, cl_page_is_owned(pg, io));
980 PINVRNT(env, pg, cl_page_invariant(pg));
983 pg = cl_page_top(pg);
985 cl_page_owner_clear(pg);
986 cl_page_state_set(env, pg, CPS_CACHED);
987 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
988 (const struct lu_env *,
989 const struct cl_page_slice *, struct cl_io *),
993 EXPORT_SYMBOL(cl_page_unassume);
996 * Releases page ownership.
998 * Moves page into cl_page_state::CPS_CACHED.
1000 * \pre cl_page_is_owned(pg, io)
1001 * \post !cl_page_is_owned(pg, io)
1003 * \see cl_page_own()
1004 * \see cl_page_operations::cpo_disown()
1006 void cl_page_disown(const struct lu_env *env,
1007 struct cl_io *io, struct cl_page *pg)
1009 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1012 pg = cl_page_top(pg);
1014 cl_page_disown0(env, io, pg);
1017 EXPORT_SYMBOL(cl_page_disown);
1020 * Called when page is to be removed from the object, e.g., as a result of
1023 * Calls cl_page_operations::cpo_discard() top-to-bottom.
1025 * \pre cl_page_is_owned(pg, io)
1027 * \see cl_page_operations::cpo_discard()
1029 void cl_page_discard(const struct lu_env *env,
1030 struct cl_io *io, struct cl_page *pg)
1032 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1033 PINVRNT(env, pg, cl_page_invariant(pg));
1035 cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
1037 EXPORT_SYMBOL(cl_page_discard);
1040 * Version of cl_page_delete() that can be called for not fully constructed
1041 * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
1042 * path. Doesn't check page invariant.
1044 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
1047 PASSERT(env, pg, pg == cl_page_top(pg));
1048 PASSERT(env, pg, pg->cp_state != CPS_FREEING);
1052 * Severe all ways to obtain new pointers to @pg.
1054 cl_page_owner_clear(pg);
1055 cl_page_state_set0(env, pg, CPS_FREEING);
1056 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_delete),
1057 (const struct lu_env *, const struct cl_page_slice *));
1060 * !radix means that @pg is not yet in the radix tree, skip
1064 for (; pg != NULL; pg = pg->cp_child) {
1066 struct cl_object_header *hdr;
1068 hdr = cl_object_header(pg->cp_obj);
1069 spin_lock(&hdr->coh_page_guard);
1070 value = radix_tree_delete(&hdr->coh_tree, pg->cp_index);
1071 PASSERT(env, pg, value == pg);
1072 PASSERT(env, pg, hdr->coh_pages > 0);
1074 spin_unlock(&hdr->coh_page_guard);
1080 * Called when a decision is made to throw page out of memory.
1082 * Notifies all layers about page destruction by calling
1083 * cl_page_operations::cpo_delete() method top-to-bottom.
1085 * Moves page into cl_page_state::CPS_FREEING state (this is the only place
1086 * where transition to this state happens).
1088 * Eliminates all venues through which new references to the page can be
1091 * - removes page from the radix trees,
1093 * - breaks linkage from VM page to cl_page.
1095 * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
1096 * drain after some time, at which point page will be recycled.
1098 * \pre pg == cl_page_top(pg)
1099 * \pre VM page is locked
1100 * \post pg->cp_state == CPS_FREEING
1102 * \see cl_page_operations::cpo_delete()
1104 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
1106 PINVRNT(env, pg, cl_page_invariant(pg));
1108 cl_page_delete0(env, pg, 1);
1111 EXPORT_SYMBOL(cl_page_delete);
1114 * Unmaps page from user virtual memory.
1116 * Calls cl_page_operations::cpo_unmap() through all layers top-to-bottom. The
1117 * layer responsible for VM interaction has to unmap page from user space
1120 * \see cl_page_operations::cpo_unmap()
1122 int cl_page_unmap(const struct lu_env *env,
1123 struct cl_io *io, struct cl_page *pg)
1125 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1126 PINVRNT(env, pg, cl_page_invariant(pg));
1128 return cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_unmap));
1130 EXPORT_SYMBOL(cl_page_unmap);
1133 * Marks page up-to-date.
1135 * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
1136 * layer responsible for VM interaction has to mark page as up-to-date. From
1137 * this moment on, page can be shown to the user space without Lustre being
1138 * notified, hence the name.
1140 * \see cl_page_operations::cpo_export()
1142 void cl_page_export(const struct lu_env *env, struct cl_page *pg)
1144 PINVRNT(env, pg, cl_page_invariant(pg));
1145 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
1146 (const struct lu_env *, const struct cl_page_slice *));
1148 EXPORT_SYMBOL(cl_page_export);
1151 * Returns true, iff \a pg is VM locked in a suitable sense by the calling
1154 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
1157 const struct cl_page_slice *slice;
1160 pg = cl_page_top_trusted((struct cl_page *)pg);
1161 slice = container_of(pg->cp_layers.next,
1162 const struct cl_page_slice, cpl_linkage);
1163 PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
1165 * Call ->cpo_is_vmlocked() directly instead of going through
1166 * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
1167 * cl_page_invariant().
1169 result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
1170 PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
1171 RETURN(result == -EBUSY);
1173 EXPORT_SYMBOL(cl_page_is_vmlocked);
1175 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
1178 RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
1181 static void cl_page_io_start(const struct lu_env *env,
1182 struct cl_page *pg, enum cl_req_type crt)
1185 * Page is queued for IO, change its state.
1188 cl_page_owner_clear(pg);
1189 cl_page_state_set(env, pg, cl_req_type_state(crt));
1194 * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
1195 * called top-to-bottom. Every layer either agrees to submit this page (by
1196 * returning 0), or requests to omit this page (by returning -EALREADY). Layer
1197 * handling interactions with the VM also has to inform VM that page is under
1200 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
1201 struct cl_page *pg, enum cl_req_type crt)
1205 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1206 PINVRNT(env, pg, cl_page_invariant(pg));
1207 PINVRNT(env, pg, crt < CRT_NR);
1210 * XXX this has to be called bottom-to-top, so that llite can set up
1211 * PG_writeback without risking other layers deciding to skip this
1214 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
1216 cl_page_io_start(env, pg, crt);
1218 KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
1220 PageWriteback(cl_page_vmpage(env, pg)))));
1221 CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, result);
1224 EXPORT_SYMBOL(cl_page_prep);
1227 * Notify layers about transfer completion.
1229 * Invoked by transfer sub-system (which is a part of osc) to notify layers
1230 * that a transfer, of which this page is a part of has completed.
1232 * Completion call-backs are executed in the bottom-up order, so that
1233 * uppermost layer (llite), responsible for the VFS/VM interaction runs last
1234 * and can release locks safely.
1236 * \pre pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1237 * \post pg->cp_state == CPS_CACHED
1239 * \see cl_page_operations::cpo_completion()
1241 void cl_page_completion(const struct lu_env *env,
1242 struct cl_page *pg, enum cl_req_type crt, int ioret)
1244 PASSERT(env, pg, crt < CRT_NR);
1245 /* cl_page::cp_req already cleared by the caller (osc_completion()) */
1246 PASSERT(env, pg, pg->cp_req == NULL);
1247 PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
1248 PINVRNT(env, pg, cl_page_invariant(pg));
1251 CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, ioret);
1252 if (crt == CRT_READ) {
1253 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
1254 pg->cp_flags |= CPF_READ_COMPLETED;
1257 cl_page_state_set(env, pg, CPS_CACHED);
1258 CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
1259 (const struct lu_env *,
1260 const struct cl_page_slice *, int), ioret);
1262 KLASSERT(!PageWriteback(cl_page_vmpage(env, pg)));
1265 EXPORT_SYMBOL(cl_page_completion);
1268 * Notify layers that transfer formation engine decided to yank this page from
1269 * the cache and to make it a part of a transfer.
1271 * \pre pg->cp_state == CPS_CACHED
1272 * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1274 * \see cl_page_operations::cpo_make_ready()
1276 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1277 enum cl_req_type crt)
1281 PINVRNT(env, pg, crt < CRT_NR);
1284 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1285 (const struct lu_env *,
1286 const struct cl_page_slice *));
1288 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1289 cl_page_io_start(env, pg, crt);
1291 CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, result);
1294 EXPORT_SYMBOL(cl_page_make_ready);
1297 * Notify layers that high level io decided to place this page into a cache
1298 * for future transfer.
1300 * The layer implementing transfer engine (osc) has to register this page in
1303 * \pre cl_page_is_owned(pg, io)
1304 * \post ergo(result == 0,
1305 * pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT)
1307 * \see cl_page_operations::cpo_cache_add()
1309 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1310 struct cl_page *pg, enum cl_req_type crt)
1314 PINVRNT(env, pg, crt < CRT_NR);
1315 PINVRNT(env, pg, cl_page_is_owned(pg, io));
1316 PINVRNT(env, pg, cl_page_invariant(pg));
1319 result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_cache_add));
1321 cl_page_owner_clear(pg);
1322 cl_page_state_set(env, pg, CPS_CACHED);
1324 CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, result);
1327 EXPORT_SYMBOL(cl_page_cache_add);
1330 * Checks whether page is protected by any extent lock is at least required
1333 * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1334 * \see cl_page_operations::cpo_is_under_lock()
1336 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1337 struct cl_page *page)
1341 PINVRNT(env, page, cl_page_invariant(page));
1344 rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1345 (const struct lu_env *,
1346 const struct cl_page_slice *, struct cl_io *),
1348 PASSERT(env, page, rc != 0);
1351 EXPORT_SYMBOL(cl_page_is_under_lock);
1354 * Purges all cached pages belonging to the object \a obj.
1356 int cl_pages_prune(const struct lu_env *env, struct cl_object *clobj)
1358 struct cl_thread_info *info;
1359 struct cl_object *obj = cl_object_top(clobj);
1361 struct cl_page_list *plist;
1365 info = cl_env_info(env);
1366 plist = &info->clt_list;
1370 * initialize the io. This is ugly since we never do IO in this
1371 * function, we just make cl_page_list functions happy. -jay
1374 result = cl_io_init(env, io, CIT_MISC, obj);
1376 cl_io_fini(env, io);
1377 RETURN(io->ci_result);
1380 cl_page_list_init(plist);
1381 cl_page_gang_lookup(env, obj, io, 0, CL_PAGE_EOF, plist);
1383 * Since we're purging the pages of an object, we don't care
1384 * the possible outcomes of the following functions.
1386 cl_page_list_unmap(env, io, plist);
1387 cl_page_list_discard(env, io, plist);
1388 cl_page_list_disown(env, io, plist);
1389 cl_page_list_fini(env, plist);
1391 cl_io_fini(env, io);
1394 EXPORT_SYMBOL(cl_pages_prune);
1397 * Tells transfer engine that only part of a page is to be transmitted.
1399 * \see cl_page_operations::cpo_clip()
1401 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1404 PINVRNT(env, pg, cl_page_invariant(pg));
1406 CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", from, to);
1407 CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1408 (const struct lu_env *,
1409 const struct cl_page_slice *,int, int),
1412 EXPORT_SYMBOL(cl_page_clip);
1415 * Prints human readable representation of \a pg to the \a f.
1417 void cl_page_header_print(const struct lu_env *env, void *cookie,
1418 lu_printer_t printer, const struct cl_page *pg)
1420 (*printer)(env, cookie,
1421 "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1422 pg, atomic_read(&pg->cp_ref), pg->cp_obj,
1423 pg->cp_index, pg->cp_parent, pg->cp_child,
1424 pg->cp_state, pg->cp_error, pg->cp_type,
1425 pg->cp_owner, pg->cp_req, pg->cp_flags);
1427 EXPORT_SYMBOL(cl_page_header_print);
1430 * Prints human readable representation of \a pg to the \a f.
1432 void cl_page_print(const struct lu_env *env, void *cookie,
1433 lu_printer_t printer, const struct cl_page *pg)
1435 struct cl_page *scan;
1437 for (scan = cl_page_top((struct cl_page *)pg);
1438 scan != NULL; scan = scan->cp_child)
1439 cl_page_header_print(env, cookie, printer, scan);
1440 CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1441 (const struct lu_env *env,
1442 const struct cl_page_slice *slice,
1443 void *cookie, lu_printer_t p), cookie, printer);
1444 (*printer)(env, cookie, "end page@%p\n", pg);
1446 EXPORT_SYMBOL(cl_page_print);
1449 * Cancel a page which is still in a transfer.
1451 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1453 return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1454 (const struct lu_env *,
1455 const struct cl_page_slice *));
1457 EXPORT_SYMBOL(cl_page_cancel);
1460 * Converts a byte offset within object \a obj into a page index.
1462 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1467 return (loff_t)idx << CFS_PAGE_SHIFT;
1469 EXPORT_SYMBOL(cl_offset);
1472 * Converts a page index into a byte offset within object \a obj.
1474 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1479 return offset >> CFS_PAGE_SHIFT;
1481 EXPORT_SYMBOL(cl_index);
1483 int cl_page_size(const struct cl_object *obj)
1485 return 1 << CFS_PAGE_SHIFT;
1487 EXPORT_SYMBOL(cl_page_size);
1490 * Adds page slice to the compound page.
1492 * This is called by cl_object_operations::coo_page_init() methods to add a
1493 * per-layer state to the page. New state is added at the end of
1494 * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1496 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1498 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1499 struct cl_object *obj,
1500 const struct cl_page_operations *ops)
1503 list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1504 slice->cpl_obj = obj;
1505 slice->cpl_ops = ops;
1506 slice->cpl_page = page;
1509 EXPORT_SYMBOL(cl_page_slice_add);
1511 int cl_page_init(void)
1513 return lu_kmem_init(cl_page_caches);
1516 void cl_page_fini(void)
1518 lu_kmem_fini(cl_page_caches);