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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
38 #define DEBUG_SUBSYSTEM S_CLASS
40 #include <linux/list.h>
41 #include <libcfs/libcfs.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
45 #include <cl_object.h>
46 #include "cl_internal.h"
48 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg);
51 # define PASSERT(env, page, expr) \
53 if (unlikely(!(expr))) { \
54 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
58 #else /* !LIBCFS_DEBUG */
59 # define PASSERT(env, page, exp) \
60 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
61 #endif /* !LIBCFS_DEBUG */
63 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
64 # define PINVRNT(env, page, expr) \
66 if (unlikely(!(expr))) { \
67 CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n"); \
71 #else /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK */
72 # define PINVRNT(env, page, exp) \
73 ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
74 #endif /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK */
76 /* Disable page statistic by default due to huge performance penalty. */
77 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
78 #define CS_PAGE_INC(o, item) \
79 atomic_inc(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
80 #define CS_PAGE_DEC(o, item) \
81 atomic_dec(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
82 #define CS_PAGESTATE_INC(o, state) \
83 atomic_inc(&cl_object_site(o)->cs_pages_state[state])
84 #define CS_PAGESTATE_DEC(o, state) \
85 atomic_dec(&cl_object_site(o)->cs_pages_state[state])
87 #define CS_PAGE_INC(o, item)
88 #define CS_PAGE_DEC(o, item)
89 #define CS_PAGESTATE_INC(o, state)
90 #define CS_PAGESTATE_DEC(o, state)
94 * Internal version of cl_page_get().
96 * This function can be used to obtain initial reference to previously
97 * unreferenced cached object. It can be called only if concurrent page
98 * reclamation is somehow prevented, e.g., by keeping a lock on a VM page,
99 * associated with \a page.
101 * Use with care! Not exported.
103 static void cl_page_get_trust(struct cl_page *page)
105 LASSERT(atomic_read(&page->cp_ref) > 0);
106 atomic_inc(&page->cp_ref);
110 * Returns a slice within a page, corresponding to the given layer in the
115 static const struct cl_page_slice *
116 cl_page_at_trusted(const struct cl_page *page,
117 const struct lu_device_type *dtype)
119 const struct cl_page_slice *slice;
122 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
123 if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
129 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
131 struct cl_object *obj = page->cp_obj;
132 int pagesize = cl_object_header(obj)->coh_page_bufsize;
134 PASSERT(env, page, list_empty(&page->cp_batch));
135 PASSERT(env, page, page->cp_owner == NULL);
136 PASSERT(env, page, page->cp_state == CPS_FREEING);
139 while (!list_empty(&page->cp_layers)) {
140 struct cl_page_slice *slice;
142 slice = list_entry(page->cp_layers.next,
143 struct cl_page_slice, cpl_linkage);
144 list_del_init(page->cp_layers.next);
145 if (unlikely(slice->cpl_ops->cpo_fini != NULL))
146 slice->cpl_ops->cpo_fini(env, slice);
148 CS_PAGE_DEC(obj, total);
149 CS_PAGESTATE_DEC(obj, page->cp_state);
150 lu_object_ref_del_at(&obj->co_lu, &page->cp_obj_ref, "cl_page", page);
151 cl_object_put(env, obj);
152 lu_ref_fini(&page->cp_reference);
153 OBD_FREE(page, pagesize);
158 * Helper function updating page state. This is the only place in the code
159 * where cl_page::cp_state field is mutated.
161 static inline void cl_page_state_set_trust(struct cl_page *page,
162 enum cl_page_state state)
165 *(enum cl_page_state *)&page->cp_state = state;
168 struct cl_page *cl_page_alloc(const struct lu_env *env,
169 struct cl_object *o, pgoff_t ind, struct page *vmpage,
170 enum cl_page_type type)
172 struct cl_page *page;
173 struct lu_object_header *head;
176 OBD_ALLOC_GFP(page, cl_object_header(o)->coh_page_bufsize,
180 atomic_set(&page->cp_ref, 1);
183 lu_object_ref_add_at(&o->co_lu, &page->cp_obj_ref, "cl_page",
185 page->cp_vmpage = vmpage;
186 cl_page_state_set_trust(page, CPS_CACHED);
187 page->cp_type = type;
188 INIT_LIST_HEAD(&page->cp_layers);
189 INIT_LIST_HEAD(&page->cp_batch);
190 lu_ref_init(&page->cp_reference);
191 head = o->co_lu.lo_header;
192 list_for_each_entry(o, &head->loh_layers,
194 if (o->co_ops->coo_page_init != NULL) {
195 result = o->co_ops->coo_page_init(env, o, page,
198 cl_page_delete0(env, page);
199 cl_page_free(env, page);
200 page = ERR_PTR(result);
206 CS_PAGE_INC(o, total);
207 CS_PAGE_INC(o, create);
208 CS_PAGESTATE_DEC(o, CPS_CACHED);
211 page = ERR_PTR(-ENOMEM);
217 * Returns a cl_page with index \a idx at the object \a o, and associated with
218 * the VM page \a vmpage.
220 * This is the main entry point into the cl_page caching interface. First, a
221 * cache (implemented as a per-object radix tree) is consulted. If page is
222 * found there, it is returned immediately. Otherwise new page is allocated
223 * and returned. In any case, additional reference to page is acquired.
225 * \see cl_object_find(), cl_lock_find()
227 struct cl_page *cl_page_find(const struct lu_env *env,
229 pgoff_t idx, struct page *vmpage,
230 enum cl_page_type type)
232 struct cl_page *page = NULL;
233 struct cl_object_header *hdr;
235 LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
240 hdr = cl_object_header(o);
241 CS_PAGE_INC(o, lookup);
243 CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
244 idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
246 if (type == CPT_CACHEABLE) {
247 /* vmpage lock is used to protect the child/parent
249 KLASSERT(PageLocked(vmpage));
251 * cl_vmpage_page() can be called here without any locks as
253 * - "vmpage" is locked (which prevents ->private from
254 * concurrent updates), and
256 * - "o" cannot be destroyed while current thread holds a
259 page = cl_vmpage_page(vmpage, o);
266 /* allocate and initialize cl_page */
267 page = cl_page_alloc(env, o, idx, vmpage, type);
270 EXPORT_SYMBOL(cl_page_find);
272 static inline int cl_page_invariant(const struct cl_page *pg)
274 return cl_page_in_use_noref(pg);
277 static void cl_page_state_set0(const struct lu_env *env,
278 struct cl_page *page, enum cl_page_state state)
280 enum cl_page_state old;
283 * Matrix of allowed state transitions [old][new], for sanity
286 static const int allowed_transitions[CPS_NR][CPS_NR] = {
289 [CPS_OWNED] = 1, /* io finds existing cached page */
291 [CPS_PAGEOUT] = 1, /* write-out from the cache */
292 [CPS_FREEING] = 1, /* eviction on the memory pressure */
295 [CPS_CACHED] = 1, /* release to the cache */
297 [CPS_PAGEIN] = 1, /* start read immediately */
298 [CPS_PAGEOUT] = 1, /* start write immediately */
299 [CPS_FREEING] = 1, /* lock invalidation or truncate */
302 [CPS_CACHED] = 1, /* io completion */
309 [CPS_CACHED] = 1, /* io completion */
325 old = page->cp_state;
326 PASSERT(env, page, allowed_transitions[old][state]);
327 CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
328 PASSERT(env, page, page->cp_state == old);
329 PASSERT(env, page, equi(state == CPS_OWNED, page->cp_owner != NULL));
331 CS_PAGESTATE_DEC(page->cp_obj, page->cp_state);
332 CS_PAGESTATE_INC(page->cp_obj, state);
333 cl_page_state_set_trust(page, state);
337 static void cl_page_state_set(const struct lu_env *env,
338 struct cl_page *page, enum cl_page_state state)
340 cl_page_state_set0(env, page, state);
344 * Acquires an additional reference to a page.
346 * This can be called only by caller already possessing a reference to \a
349 * \see cl_object_get(), cl_lock_get().
351 void cl_page_get(struct cl_page *page)
354 cl_page_get_trust(page);
357 EXPORT_SYMBOL(cl_page_get);
360 * Releases a reference to a page.
362 * When last reference is released, page is returned to the cache, unless it
363 * is in cl_page_state::CPS_FREEING state, in which case it is immediately
366 * \see cl_object_put(), cl_lock_put().
368 void cl_page_put(const struct lu_env *env, struct cl_page *page)
371 CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
372 atomic_read(&page->cp_ref));
374 if (atomic_dec_and_test(&page->cp_ref)) {
375 LASSERT(page->cp_state == CPS_FREEING);
377 LASSERT(atomic_read(&page->cp_ref) == 0);
378 PASSERT(env, page, page->cp_owner == NULL);
379 PASSERT(env, page, list_empty(&page->cp_batch));
381 * Page is no longer reachable by other threads. Tear
384 cl_page_free(env, page);
389 EXPORT_SYMBOL(cl_page_put);
392 * Returns a cl_page associated with a VM page, and given cl_object.
394 struct cl_page *cl_vmpage_page(struct page *vmpage, struct cl_object *obj)
396 struct cl_page *page;
399 KLASSERT(PageLocked(vmpage));
402 * NOTE: absence of races and liveness of data are guaranteed by page
403 * lock on a "vmpage". That works because object destruction has
404 * bottom-to-top pass.
407 page = (struct cl_page *)vmpage->private;
409 cl_page_get_trust(page);
410 LASSERT(page->cp_type == CPT_CACHEABLE);
414 EXPORT_SYMBOL(cl_vmpage_page);
416 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
417 const struct lu_device_type *dtype)
419 return cl_page_at_trusted(page, dtype);
421 EXPORT_SYMBOL(cl_page_at);
423 static void cl_page_owner_clear(struct cl_page *page)
426 if (page->cp_owner != NULL) {
427 LASSERT(page->cp_owner->ci_owned_nr > 0);
428 page->cp_owner->ci_owned_nr--;
429 page->cp_owner = NULL;
434 static void cl_page_owner_set(struct cl_page *page)
437 LASSERT(page->cp_owner != NULL);
438 page->cp_owner->ci_owned_nr++;
442 void cl_page_disown0(const struct lu_env *env,
443 struct cl_io *io, struct cl_page *pg)
445 const struct cl_page_slice *slice;
446 enum cl_page_state state;
449 state = pg->cp_state;
450 PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
451 PINVRNT(env, pg, cl_page_invariant(pg) || state == CPS_FREEING);
452 cl_page_owner_clear(pg);
454 if (state == CPS_OWNED)
455 cl_page_state_set(env, pg, CPS_CACHED);
457 * Completion call-backs are executed in the bottom-up order, so that
458 * uppermost layer (llite), responsible for VFS/VM interaction runs
459 * last and can release locks safely.
461 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
462 if (slice->cpl_ops->cpo_disown != NULL)
463 (*slice->cpl_ops->cpo_disown)(env, slice, io);
470 * returns true, iff page is owned by the given io.
472 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
474 struct cl_io *top = cl_io_top((struct cl_io *)io);
475 LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
477 RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == top);
479 EXPORT_SYMBOL(cl_page_is_owned);
482 * Try to own a page by IO.
484 * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
485 * into cl_page_state::CPS_OWNED state.
487 * \pre !cl_page_is_owned(pg, io)
488 * \post result == 0 iff cl_page_is_owned(pg, io)
492 * \retval -ve failure, e.g., page was destroyed (and landed in
493 * cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
494 * or, page was owned by another thread, or in IO.
496 * \see cl_page_disown()
497 * \see cl_page_operations::cpo_own()
498 * \see cl_page_own_try()
501 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
502 struct cl_page *pg, int nonblock)
505 const struct cl_page_slice *slice;
507 PINVRNT(env, pg, !cl_page_is_owned(pg, io));
512 if (pg->cp_state == CPS_FREEING) {
517 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
518 if (slice->cpl_ops->cpo_own)
519 result = (*slice->cpl_ops->cpo_own)(env, slice,
530 PASSERT(env, pg, pg->cp_owner == NULL);
531 pg->cp_owner = cl_io_top(io);
532 cl_page_owner_set(pg);
533 if (pg->cp_state != CPS_FREEING) {
534 cl_page_state_set(env, pg, CPS_OWNED);
536 cl_page_disown0(env, io, pg);
542 PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
547 * Own a page, might be blocked.
549 * \see cl_page_own0()
551 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
553 return cl_page_own0(env, io, pg, 0);
555 EXPORT_SYMBOL(cl_page_own);
558 * Nonblock version of cl_page_own().
560 * \see cl_page_own0()
562 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
565 return cl_page_own0(env, io, pg, 1);
567 EXPORT_SYMBOL(cl_page_own_try);
571 * Assume page ownership.
573 * Called when page is already locked by the hosting VM.
575 * \pre !cl_page_is_owned(pg, io)
576 * \post cl_page_is_owned(pg, io)
578 * \see cl_page_operations::cpo_assume()
580 void cl_page_assume(const struct lu_env *env,
581 struct cl_io *io, struct cl_page *pg)
583 const struct cl_page_slice *slice;
585 PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
590 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
591 if (slice->cpl_ops->cpo_assume != NULL)
592 (*slice->cpl_ops->cpo_assume)(env, slice, io);
595 PASSERT(env, pg, pg->cp_owner == NULL);
596 pg->cp_owner = cl_io_top(io);
597 cl_page_owner_set(pg);
598 cl_page_state_set(env, pg, CPS_OWNED);
601 EXPORT_SYMBOL(cl_page_assume);
604 * Releases page ownership without unlocking the page.
606 * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
607 * underlying VM page (as VM is supposed to do this itself).
609 * \pre cl_page_is_owned(pg, io)
610 * \post !cl_page_is_owned(pg, io)
612 * \see cl_page_assume()
614 void cl_page_unassume(const struct lu_env *env,
615 struct cl_io *io, struct cl_page *pg)
617 const struct cl_page_slice *slice;
619 PINVRNT(env, pg, cl_page_is_owned(pg, io));
620 PINVRNT(env, pg, cl_page_invariant(pg));
624 cl_page_owner_clear(pg);
625 cl_page_state_set(env, pg, CPS_CACHED);
627 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
628 if (slice->cpl_ops->cpo_unassume != NULL)
629 (*slice->cpl_ops->cpo_unassume)(env, slice, io);
634 EXPORT_SYMBOL(cl_page_unassume);
637 * Releases page ownership.
639 * Moves page into cl_page_state::CPS_CACHED.
641 * \pre cl_page_is_owned(pg, io)
642 * \post !cl_page_is_owned(pg, io)
645 * \see cl_page_operations::cpo_disown()
647 void cl_page_disown(const struct lu_env *env,
648 struct cl_io *io, struct cl_page *pg)
650 PINVRNT(env, pg, cl_page_is_owned(pg, io) ||
651 pg->cp_state == CPS_FREEING);
655 cl_page_disown0(env, io, pg);
658 EXPORT_SYMBOL(cl_page_disown);
661 * Called when page is to be removed from the object, e.g., as a result of
664 * Calls cl_page_operations::cpo_discard() top-to-bottom.
666 * \pre cl_page_is_owned(pg, io)
668 * \see cl_page_operations::cpo_discard()
670 void cl_page_discard(const struct lu_env *env,
671 struct cl_io *io, struct cl_page *pg)
673 const struct cl_page_slice *slice;
675 PINVRNT(env, pg, cl_page_is_owned(pg, io));
676 PINVRNT(env, pg, cl_page_invariant(pg));
678 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
679 if (slice->cpl_ops->cpo_discard != NULL)
680 (*slice->cpl_ops->cpo_discard)(env, slice, io);
683 EXPORT_SYMBOL(cl_page_discard);
686 * Version of cl_page_delete() that can be called for not fully constructed
687 * pages, e.g. in an error handling cl_page_find()->cl_page_delete0()
688 * path. Doesn't check page invariant.
690 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg)
692 const struct cl_page_slice *slice;
696 PASSERT(env, pg, pg->cp_state != CPS_FREEING);
699 * Severe all ways to obtain new pointers to @pg.
701 cl_page_owner_clear(pg);
702 cl_page_state_set0(env, pg, CPS_FREEING);
704 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
705 if (slice->cpl_ops->cpo_delete != NULL)
706 (*slice->cpl_ops->cpo_delete)(env, slice);
713 * Called when a decision is made to throw page out of memory.
715 * Notifies all layers about page destruction by calling
716 * cl_page_operations::cpo_delete() method top-to-bottom.
718 * Moves page into cl_page_state::CPS_FREEING state (this is the only place
719 * where transition to this state happens).
721 * Eliminates all venues through which new references to the page can be
724 * - removes page from the radix trees,
726 * - breaks linkage from VM page to cl_page.
728 * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
729 * drain after some time, at which point page will be recycled.
731 * \pre VM page is locked
732 * \post pg->cp_state == CPS_FREEING
734 * \see cl_page_operations::cpo_delete()
736 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
738 PINVRNT(env, pg, cl_page_invariant(pg));
740 cl_page_delete0(env, pg);
743 EXPORT_SYMBOL(cl_page_delete);
746 * Marks page up-to-date.
748 * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
749 * layer responsible for VM interaction has to mark/clear page as up-to-date
750 * by the \a uptodate argument.
752 * \see cl_page_operations::cpo_export()
754 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
756 const struct cl_page_slice *slice;
758 PINVRNT(env, pg, cl_page_invariant(pg));
760 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
761 if (slice->cpl_ops->cpo_export != NULL)
762 (*slice->cpl_ops->cpo_export)(env, slice, uptodate);
765 EXPORT_SYMBOL(cl_page_export);
768 * Returns true, iff \a pg is VM locked in a suitable sense by the calling
771 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
773 const struct cl_page_slice *slice;
777 slice = container_of(pg->cp_layers.next,
778 const struct cl_page_slice, cpl_linkage);
779 PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
781 * Call ->cpo_is_vmlocked() directly instead of going through
782 * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
783 * cl_page_invariant().
785 result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
786 PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
787 RETURN(result == -EBUSY);
789 EXPORT_SYMBOL(cl_page_is_vmlocked);
791 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
794 RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
797 static void cl_page_io_start(const struct lu_env *env,
798 struct cl_page *pg, enum cl_req_type crt)
801 * Page is queued for IO, change its state.
804 cl_page_owner_clear(pg);
805 cl_page_state_set(env, pg, cl_req_type_state(crt));
810 * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
811 * called top-to-bottom. Every layer either agrees to submit this page (by
812 * returning 0), or requests to omit this page (by returning -EALREADY). Layer
813 * handling interactions with the VM also has to inform VM that page is under
816 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
817 struct cl_page *pg, enum cl_req_type crt)
819 const struct cl_page_slice *slice;
822 PINVRNT(env, pg, cl_page_is_owned(pg, io));
823 PINVRNT(env, pg, cl_page_invariant(pg));
824 PINVRNT(env, pg, crt < CRT_NR);
827 * XXX this has to be called bottom-to-top, so that llite can set up
828 * PG_writeback without risking other layers deciding to skip this
834 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
835 if (slice->cpl_ops->cpo_own)
836 result = (*slice->cpl_ops->io[crt].cpo_prep)(env,
847 cl_page_io_start(env, pg, crt);
850 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
853 EXPORT_SYMBOL(cl_page_prep);
856 * Notify layers about transfer completion.
858 * Invoked by transfer sub-system (which is a part of osc) to notify layers
859 * that a transfer, of which this page is a part of has completed.
861 * Completion call-backs are executed in the bottom-up order, so that
862 * uppermost layer (llite), responsible for the VFS/VM interaction runs last
863 * and can release locks safely.
865 * \pre pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
866 * \post pg->cp_state == CPS_CACHED
868 * \see cl_page_operations::cpo_completion()
870 void cl_page_completion(const struct lu_env *env,
871 struct cl_page *pg, enum cl_req_type crt, int ioret)
873 const struct cl_page_slice *slice;
874 struct cl_sync_io *anchor = pg->cp_sync_io;
876 PASSERT(env, pg, crt < CRT_NR);
877 PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
880 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
881 cl_page_state_set(env, pg, CPS_CACHED);
885 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
886 if (slice->cpl_ops->io[crt].cpo_completion != NULL)
887 (*slice->cpl_ops->io[crt].cpo_completion)(env, slice,
891 if (anchor != NULL) {
892 LASSERT(pg->cp_sync_io == anchor);
893 pg->cp_sync_io = NULL;
894 cl_sync_io_note(env, anchor, ioret);
898 EXPORT_SYMBOL(cl_page_completion);
901 * Notify layers that transfer formation engine decided to yank this page from
902 * the cache and to make it a part of a transfer.
904 * \pre pg->cp_state == CPS_CACHED
905 * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
907 * \see cl_page_operations::cpo_make_ready()
909 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
910 enum cl_req_type crt)
912 const struct cl_page_slice *sli;
915 PINVRNT(env, pg, crt < CRT_NR);
921 list_for_each_entry(sli, &pg->cp_layers, cpl_linkage) {
922 if (sli->cpl_ops->io[crt].cpo_make_ready != NULL)
923 result = (*sli->cpl_ops->io[crt].cpo_make_ready)(env,
931 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
932 cl_page_io_start(env, pg, crt);
934 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
937 EXPORT_SYMBOL(cl_page_make_ready);
940 * Called if a pge is being written back by kernel's intention.
942 * \pre cl_page_is_owned(pg, io)
943 * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
945 * \see cl_page_operations::cpo_flush()
947 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
950 const struct cl_page_slice *slice;
953 PINVRNT(env, pg, cl_page_is_owned(pg, io));
954 PINVRNT(env, pg, cl_page_invariant(pg));
958 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
959 if (slice->cpl_ops->cpo_flush != NULL)
960 result = (*slice->cpl_ops->cpo_flush)(env, slice, io);
967 CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
970 EXPORT_SYMBOL(cl_page_flush);
973 * Tells transfer engine that only part of a page is to be transmitted.
975 * \see cl_page_operations::cpo_clip()
977 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
980 const struct cl_page_slice *slice;
982 PINVRNT(env, pg, cl_page_invariant(pg));
984 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
985 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
986 if (slice->cpl_ops->cpo_clip != NULL)
987 (*slice->cpl_ops->cpo_clip)(env, slice, from, to);
990 EXPORT_SYMBOL(cl_page_clip);
993 * Prints human readable representation of \a pg to the \a f.
995 void cl_page_header_print(const struct lu_env *env, void *cookie,
996 lu_printer_t printer, const struct cl_page *pg)
998 (*printer)(env, cookie,
999 "page@%p[%d %p %d %d %p]\n",
1000 pg, atomic_read(&pg->cp_ref), pg->cp_obj,
1001 pg->cp_state, pg->cp_type,
1004 EXPORT_SYMBOL(cl_page_header_print);
1007 * Prints human readable representation of \a pg to the \a f.
1009 void cl_page_print(const struct lu_env *env, void *cookie,
1010 lu_printer_t printer, const struct cl_page *pg)
1012 const struct cl_page_slice *slice;
1015 cl_page_header_print(env, cookie, printer, pg);
1016 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
1017 if (slice->cpl_ops->cpo_print != NULL)
1018 result = (*slice->cpl_ops->cpo_print)(env, slice,
1023 (*printer)(env, cookie, "end page@%p\n", pg);
1025 EXPORT_SYMBOL(cl_page_print);
1028 * Cancel a page which is still in a transfer.
1030 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1032 const struct cl_page_slice *slice;
1035 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1036 if (slice->cpl_ops->cpo_cancel != NULL)
1037 result = (*slice->cpl_ops->cpo_cancel)(env, slice);
1048 * Converts a byte offset within object \a obj into a page index.
1050 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1052 return (loff_t)idx << PAGE_SHIFT;
1054 EXPORT_SYMBOL(cl_offset);
1057 * Converts a page index into a byte offset within object \a obj.
1059 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1061 return offset >> PAGE_SHIFT;
1063 EXPORT_SYMBOL(cl_index);
1065 size_t cl_page_size(const struct cl_object *obj)
1067 return 1UL << PAGE_SHIFT;
1069 EXPORT_SYMBOL(cl_page_size);
1072 * Adds page slice to the compound page.
1074 * This is called by cl_object_operations::coo_page_init() methods to add a
1075 * per-layer state to the page. New state is added at the end of
1076 * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1078 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1080 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1081 struct cl_object *obj, pgoff_t index,
1082 const struct cl_page_operations *ops)
1085 list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1086 slice->cpl_obj = obj;
1087 slice->cpl_index = index;
1088 slice->cpl_ops = ops;
1089 slice->cpl_page = page;
1092 EXPORT_SYMBOL(cl_page_slice_add);
1095 * Allocate and initialize cl_cache, called by ll_init_sbi().
1097 struct cl_client_cache *cl_cache_init(unsigned long lru_page_max)
1099 struct cl_client_cache *cache = NULL;
1102 OBD_ALLOC(cache, sizeof(*cache));
1106 /* Initialize cache data */
1107 atomic_set(&cache->ccc_users, 1);
1108 cache->ccc_lru_max = lru_page_max;
1109 atomic_long_set(&cache->ccc_lru_left, lru_page_max);
1110 spin_lock_init(&cache->ccc_lru_lock);
1111 INIT_LIST_HEAD(&cache->ccc_lru);
1113 /* turn unstable check off by default as it impacts performance */
1114 cache->ccc_unstable_check = 0;
1115 atomic_long_set(&cache->ccc_unstable_nr, 0);
1116 init_waitqueue_head(&cache->ccc_unstable_waitq);
1120 EXPORT_SYMBOL(cl_cache_init);
1123 * Increase cl_cache refcount
1125 void cl_cache_incref(struct cl_client_cache *cache)
1127 atomic_inc(&cache->ccc_users);
1129 EXPORT_SYMBOL(cl_cache_incref);
1132 * Decrease cl_cache refcount and free the cache if refcount=0.
1133 * Since llite, lov and osc all hold cl_cache refcount,
1134 * the free will not cause race. (LU-6173)
1136 void cl_cache_decref(struct cl_client_cache *cache)
1138 if (atomic_dec_and_test(&cache->ccc_users))
1139 OBD_FREE(cache, sizeof(*cache));
1141 EXPORT_SYMBOL(cl_cache_decref);