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, 2017, 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 static void cs_page_inc(const struct cl_object *obj,
78 enum cache_stats_item item)
80 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
81 atomic_inc(&cl_object_site(obj)->cs_pages.cs_stats[item]);
85 static void cs_page_dec(const struct cl_object *obj,
86 enum cache_stats_item item)
88 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
89 atomic_dec(&cl_object_site(obj)->cs_pages.cs_stats[item]);
93 static void cs_pagestate_inc(const struct cl_object *obj,
94 enum cl_page_state state)
96 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
97 atomic_inc(&cl_object_site(obj)->cs_pages_state[state]);
101 static void cs_pagestate_dec(const struct cl_object *obj,
102 enum cl_page_state state)
104 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
105 atomic_dec(&cl_object_site(obj)->cs_pages_state[state]);
110 * Internal version of cl_page_get().
112 * This function can be used to obtain initial reference to previously
113 * unreferenced cached object. It can be called only if concurrent page
114 * reclamation is somehow prevented, e.g., by keeping a lock on a VM page,
115 * associated with \a page.
117 * Use with care! Not exported.
119 static void cl_page_get_trust(struct cl_page *page)
121 LASSERT(atomic_read(&page->cp_ref) > 0);
122 atomic_inc(&page->cp_ref);
126 * Returns a slice within a page, corresponding to the given layer in the
131 static const struct cl_page_slice *
132 cl_page_at_trusted(const struct cl_page *page,
133 const struct lu_device_type *dtype)
135 const struct cl_page_slice *slice;
138 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
139 if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
145 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
147 struct cl_object *obj = page->cp_obj;
148 int pagesize = cl_object_header(obj)->coh_page_bufsize;
150 PASSERT(env, page, list_empty(&page->cp_batch));
151 PASSERT(env, page, page->cp_owner == NULL);
152 PASSERT(env, page, page->cp_state == CPS_FREEING);
155 while (!list_empty(&page->cp_layers)) {
156 struct cl_page_slice *slice;
158 slice = list_entry(page->cp_layers.next,
159 struct cl_page_slice, cpl_linkage);
160 list_del_init(page->cp_layers.next);
161 if (unlikely(slice->cpl_ops->cpo_fini != NULL))
162 slice->cpl_ops->cpo_fini(env, slice);
164 cs_page_dec(obj, CS_total);
165 cs_pagestate_dec(obj, page->cp_state);
166 lu_object_ref_del_at(&obj->co_lu, &page->cp_obj_ref, "cl_page", page);
167 cl_object_put(env, obj);
168 lu_ref_fini(&page->cp_reference);
169 OBD_FREE(page, pagesize);
174 * Helper function updating page state. This is the only place in the code
175 * where cl_page::cp_state field is mutated.
177 static inline void cl_page_state_set_trust(struct cl_page *page,
178 enum cl_page_state state)
181 *(enum cl_page_state *)&page->cp_state = state;
184 struct cl_page *cl_page_alloc(const struct lu_env *env,
185 struct cl_object *o, pgoff_t ind, struct page *vmpage,
186 enum cl_page_type type)
188 struct cl_page *page;
189 struct lu_object_header *head;
192 OBD_ALLOC_GFP(page, cl_object_header(o)->coh_page_bufsize,
196 atomic_set(&page->cp_ref, 1);
199 lu_object_ref_add_at(&o->co_lu, &page->cp_obj_ref, "cl_page",
201 page->cp_vmpage = vmpage;
202 cl_page_state_set_trust(page, CPS_CACHED);
203 page->cp_type = type;
204 INIT_LIST_HEAD(&page->cp_layers);
205 INIT_LIST_HEAD(&page->cp_batch);
206 lu_ref_init(&page->cp_reference);
207 head = o->co_lu.lo_header;
208 list_for_each_entry(o, &head->loh_layers,
210 if (o->co_ops->coo_page_init != NULL) {
211 result = o->co_ops->coo_page_init(env, o, page,
214 cl_page_delete0(env, page);
215 cl_page_free(env, page);
216 page = ERR_PTR(result);
222 cs_page_inc(o, CS_total);
223 cs_page_inc(o, CS_create);
224 cs_pagestate_dec(o, CPS_CACHED);
227 page = ERR_PTR(-ENOMEM);
233 * Returns a cl_page with index \a idx at the object \a o, and associated with
234 * the VM page \a vmpage.
236 * This is the main entry point into the cl_page caching interface. First, a
237 * cache (implemented as a per-object radix tree) is consulted. If page is
238 * found there, it is returned immediately. Otherwise new page is allocated
239 * and returned. In any case, additional reference to page is acquired.
241 * \see cl_object_find(), cl_lock_find()
243 struct cl_page *cl_page_find(const struct lu_env *env,
245 pgoff_t idx, struct page *vmpage,
246 enum cl_page_type type)
248 struct cl_page *page = NULL;
249 struct cl_object_header *hdr;
251 LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
256 hdr = cl_object_header(o);
257 cs_page_inc(o, CS_lookup);
259 CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
260 idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
262 if (type == CPT_CACHEABLE) {
263 /* vmpage lock is used to protect the child/parent
265 KLASSERT(PageLocked(vmpage));
267 * cl_vmpage_page() can be called here without any locks as
269 * - "vmpage" is locked (which prevents ->private from
270 * concurrent updates), and
272 * - "o" cannot be destroyed while current thread holds a
275 page = cl_vmpage_page(vmpage, o);
277 cs_page_inc(o, CS_hit);
282 /* allocate and initialize cl_page */
283 page = cl_page_alloc(env, o, idx, vmpage, type);
286 EXPORT_SYMBOL(cl_page_find);
288 static inline int cl_page_invariant(const struct cl_page *pg)
290 return cl_page_in_use_noref(pg);
293 static void cl_page_state_set0(const struct lu_env *env,
294 struct cl_page *page, enum cl_page_state state)
296 enum cl_page_state old;
299 * Matrix of allowed state transitions [old][new], for sanity
302 static const int allowed_transitions[CPS_NR][CPS_NR] = {
305 [CPS_OWNED] = 1, /* io finds existing cached page */
307 [CPS_PAGEOUT] = 1, /* write-out from the cache */
308 [CPS_FREEING] = 1, /* eviction on the memory pressure */
311 [CPS_CACHED] = 1, /* release to the cache */
313 [CPS_PAGEIN] = 1, /* start read immediately */
314 [CPS_PAGEOUT] = 1, /* start write immediately */
315 [CPS_FREEING] = 1, /* lock invalidation or truncate */
318 [CPS_CACHED] = 1, /* io completion */
325 [CPS_CACHED] = 1, /* io completion */
341 old = page->cp_state;
342 PASSERT(env, page, allowed_transitions[old][state]);
343 CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
344 PASSERT(env, page, page->cp_state == old);
345 PASSERT(env, page, equi(state == CPS_OWNED, page->cp_owner != NULL));
347 cs_pagestate_dec(page->cp_obj, page->cp_state);
348 cs_pagestate_inc(page->cp_obj, state);
349 cl_page_state_set_trust(page, state);
353 static void cl_page_state_set(const struct lu_env *env,
354 struct cl_page *page, enum cl_page_state state)
356 cl_page_state_set0(env, page, state);
360 * Acquires an additional reference to a page.
362 * This can be called only by caller already possessing a reference to \a
365 * \see cl_object_get(), cl_lock_get().
367 void cl_page_get(struct cl_page *page)
370 cl_page_get_trust(page);
373 EXPORT_SYMBOL(cl_page_get);
376 * Releases a reference to a page.
378 * When last reference is released, page is returned to the cache, unless it
379 * is in cl_page_state::CPS_FREEING state, in which case it is immediately
382 * \see cl_object_put(), cl_lock_put().
384 void cl_page_put(const struct lu_env *env, struct cl_page *page)
387 CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
388 atomic_read(&page->cp_ref));
390 if (atomic_dec_and_test(&page->cp_ref)) {
391 LASSERT(page->cp_state == CPS_FREEING);
393 LASSERT(atomic_read(&page->cp_ref) == 0);
394 PASSERT(env, page, page->cp_owner == NULL);
395 PASSERT(env, page, list_empty(&page->cp_batch));
397 * Page is no longer reachable by other threads. Tear
400 cl_page_free(env, page);
405 EXPORT_SYMBOL(cl_page_put);
408 * Returns a cl_page associated with a VM page, and given cl_object.
410 struct cl_page *cl_vmpage_page(struct page *vmpage, struct cl_object *obj)
412 struct cl_page *page;
415 KLASSERT(PageLocked(vmpage));
418 * NOTE: absence of races and liveness of data are guaranteed by page
419 * lock on a "vmpage". That works because object destruction has
420 * bottom-to-top pass.
423 page = (struct cl_page *)vmpage->private;
425 cl_page_get_trust(page);
426 LASSERT(page->cp_type == CPT_CACHEABLE);
430 EXPORT_SYMBOL(cl_vmpage_page);
432 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
433 const struct lu_device_type *dtype)
435 return cl_page_at_trusted(page, dtype);
437 EXPORT_SYMBOL(cl_page_at);
439 static void cl_page_owner_clear(struct cl_page *page)
442 if (page->cp_owner != NULL) {
443 LASSERT(page->cp_owner->ci_owned_nr > 0);
444 page->cp_owner->ci_owned_nr--;
445 page->cp_owner = NULL;
450 static void cl_page_owner_set(struct cl_page *page)
453 LASSERT(page->cp_owner != NULL);
454 page->cp_owner->ci_owned_nr++;
458 void cl_page_disown0(const struct lu_env *env,
459 struct cl_io *io, struct cl_page *pg)
461 const struct cl_page_slice *slice;
462 enum cl_page_state state;
465 state = pg->cp_state;
466 PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
467 PINVRNT(env, pg, cl_page_invariant(pg) || state == CPS_FREEING);
468 cl_page_owner_clear(pg);
470 if (state == CPS_OWNED)
471 cl_page_state_set(env, pg, CPS_CACHED);
473 * Completion call-backs are executed in the bottom-up order, so that
474 * uppermost layer (llite), responsible for VFS/VM interaction runs
475 * last and can release locks safely.
477 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
478 if (slice->cpl_ops->cpo_disown != NULL)
479 (*slice->cpl_ops->cpo_disown)(env, slice, io);
486 * returns true, iff page is owned by the given io.
488 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
490 struct cl_io *top = cl_io_top((struct cl_io *)io);
491 LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
493 RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == top);
495 EXPORT_SYMBOL(cl_page_is_owned);
498 * Try to own a page by IO.
500 * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
501 * into cl_page_state::CPS_OWNED state.
503 * \pre !cl_page_is_owned(pg, io)
504 * \post result == 0 iff cl_page_is_owned(pg, io)
508 * \retval -ve failure, e.g., page was destroyed (and landed in
509 * cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
510 * or, page was owned by another thread, or in IO.
512 * \see cl_page_disown()
513 * \see cl_page_operations::cpo_own()
514 * \see cl_page_own_try()
517 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
518 struct cl_page *pg, int nonblock)
521 const struct cl_page_slice *slice;
523 PINVRNT(env, pg, !cl_page_is_owned(pg, io));
528 if (pg->cp_state == CPS_FREEING) {
533 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
534 if (slice->cpl_ops->cpo_own)
535 result = (*slice->cpl_ops->cpo_own)(env, slice,
546 PASSERT(env, pg, pg->cp_owner == NULL);
547 pg->cp_owner = cl_io_top(io);
548 cl_page_owner_set(pg);
549 if (pg->cp_state != CPS_FREEING) {
550 cl_page_state_set(env, pg, CPS_OWNED);
552 cl_page_disown0(env, io, pg);
558 PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
563 * Own a page, might be blocked.
565 * \see cl_page_own0()
567 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
569 return cl_page_own0(env, io, pg, 0);
571 EXPORT_SYMBOL(cl_page_own);
574 * Nonblock version of cl_page_own().
576 * \see cl_page_own0()
578 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
581 return cl_page_own0(env, io, pg, 1);
583 EXPORT_SYMBOL(cl_page_own_try);
587 * Assume page ownership.
589 * Called when page is already locked by the hosting VM.
591 * \pre !cl_page_is_owned(pg, io)
592 * \post cl_page_is_owned(pg, io)
594 * \see cl_page_operations::cpo_assume()
596 void cl_page_assume(const struct lu_env *env,
597 struct cl_io *io, struct cl_page *pg)
599 const struct cl_page_slice *slice;
601 PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
606 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
607 if (slice->cpl_ops->cpo_assume != NULL)
608 (*slice->cpl_ops->cpo_assume)(env, slice, io);
611 PASSERT(env, pg, pg->cp_owner == NULL);
612 pg->cp_owner = cl_io_top(io);
613 cl_page_owner_set(pg);
614 cl_page_state_set(env, pg, CPS_OWNED);
617 EXPORT_SYMBOL(cl_page_assume);
620 * Releases page ownership without unlocking the page.
622 * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
623 * underlying VM page (as VM is supposed to do this itself).
625 * \pre cl_page_is_owned(pg, io)
626 * \post !cl_page_is_owned(pg, io)
628 * \see cl_page_assume()
630 void cl_page_unassume(const struct lu_env *env,
631 struct cl_io *io, struct cl_page *pg)
633 const struct cl_page_slice *slice;
635 PINVRNT(env, pg, cl_page_is_owned(pg, io));
636 PINVRNT(env, pg, cl_page_invariant(pg));
640 cl_page_owner_clear(pg);
641 cl_page_state_set(env, pg, CPS_CACHED);
643 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
644 if (slice->cpl_ops->cpo_unassume != NULL)
645 (*slice->cpl_ops->cpo_unassume)(env, slice, io);
650 EXPORT_SYMBOL(cl_page_unassume);
653 * Releases page ownership.
655 * Moves page into cl_page_state::CPS_CACHED.
657 * \pre cl_page_is_owned(pg, io)
658 * \post !cl_page_is_owned(pg, io)
661 * \see cl_page_operations::cpo_disown()
663 void cl_page_disown(const struct lu_env *env,
664 struct cl_io *io, struct cl_page *pg)
666 PINVRNT(env, pg, cl_page_is_owned(pg, io) ||
667 pg->cp_state == CPS_FREEING);
671 cl_page_disown0(env, io, pg);
674 EXPORT_SYMBOL(cl_page_disown);
677 * Called when page is to be removed from the object, e.g., as a result of
680 * Calls cl_page_operations::cpo_discard() top-to-bottom.
682 * \pre cl_page_is_owned(pg, io)
684 * \see cl_page_operations::cpo_discard()
686 void cl_page_discard(const struct lu_env *env,
687 struct cl_io *io, struct cl_page *pg)
689 const struct cl_page_slice *slice;
691 PINVRNT(env, pg, cl_page_is_owned(pg, io));
692 PINVRNT(env, pg, cl_page_invariant(pg));
694 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
695 if (slice->cpl_ops->cpo_discard != NULL)
696 (*slice->cpl_ops->cpo_discard)(env, slice, io);
699 EXPORT_SYMBOL(cl_page_discard);
702 * Version of cl_page_delete() that can be called for not fully constructed
703 * pages, e.g. in an error handling cl_page_find()->cl_page_delete0()
704 * path. Doesn't check page invariant.
706 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg)
708 const struct cl_page_slice *slice;
712 PASSERT(env, pg, pg->cp_state != CPS_FREEING);
715 * Severe all ways to obtain new pointers to @pg.
717 cl_page_owner_clear(pg);
718 cl_page_state_set0(env, pg, CPS_FREEING);
720 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
721 if (slice->cpl_ops->cpo_delete != NULL)
722 (*slice->cpl_ops->cpo_delete)(env, slice);
729 * Called when a decision is made to throw page out of memory.
731 * Notifies all layers about page destruction by calling
732 * cl_page_operations::cpo_delete() method top-to-bottom.
734 * Moves page into cl_page_state::CPS_FREEING state (this is the only place
735 * where transition to this state happens).
737 * Eliminates all venues through which new references to the page can be
740 * - removes page from the radix trees,
742 * - breaks linkage from VM page to cl_page.
744 * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
745 * drain after some time, at which point page will be recycled.
747 * \pre VM page is locked
748 * \post pg->cp_state == CPS_FREEING
750 * \see cl_page_operations::cpo_delete()
752 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
754 PINVRNT(env, pg, cl_page_invariant(pg));
756 cl_page_delete0(env, pg);
759 EXPORT_SYMBOL(cl_page_delete);
762 * Marks page up-to-date.
764 * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
765 * layer responsible for VM interaction has to mark/clear page as up-to-date
766 * by the \a uptodate argument.
768 * \see cl_page_operations::cpo_export()
770 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
772 const struct cl_page_slice *slice;
774 PINVRNT(env, pg, cl_page_invariant(pg));
776 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
777 if (slice->cpl_ops->cpo_export != NULL)
778 (*slice->cpl_ops->cpo_export)(env, slice, uptodate);
781 EXPORT_SYMBOL(cl_page_export);
784 * Returns true, iff \a pg is VM locked in a suitable sense by the calling
787 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
789 const struct cl_page_slice *slice;
793 slice = container_of(pg->cp_layers.next,
794 const struct cl_page_slice, cpl_linkage);
795 PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
797 * Call ->cpo_is_vmlocked() directly instead of going through
798 * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
799 * cl_page_invariant().
801 result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
802 PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
803 RETURN(result == -EBUSY);
805 EXPORT_SYMBOL(cl_page_is_vmlocked);
807 void cl_page_touch(const struct lu_env *env, const struct cl_page *pg,
810 const struct cl_page_slice *slice;
814 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
815 if (slice->cpl_ops->cpo_page_touch != NULL)
816 (*slice->cpl_ops->cpo_page_touch)(env, slice, to);
821 EXPORT_SYMBOL(cl_page_touch);
823 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
826 RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
829 static void cl_page_io_start(const struct lu_env *env,
830 struct cl_page *pg, enum cl_req_type crt)
833 * Page is queued for IO, change its state.
836 cl_page_owner_clear(pg);
837 cl_page_state_set(env, pg, cl_req_type_state(crt));
842 * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
843 * called top-to-bottom. Every layer either agrees to submit this page (by
844 * returning 0), or requests to omit this page (by returning -EALREADY). Layer
845 * handling interactions with the VM also has to inform VM that page is under
848 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
849 struct cl_page *pg, enum cl_req_type crt)
851 const struct cl_page_slice *slice;
854 PINVRNT(env, pg, cl_page_is_owned(pg, io));
855 PINVRNT(env, pg, cl_page_invariant(pg));
856 PINVRNT(env, pg, crt < CRT_NR);
859 * XXX this has to be called bottom-to-top, so that llite can set up
860 * PG_writeback without risking other layers deciding to skip this
866 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
867 if (slice->cpl_ops->cpo_own)
868 result = (*slice->cpl_ops->io[crt].cpo_prep)(env,
879 cl_page_io_start(env, pg, crt);
882 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
885 EXPORT_SYMBOL(cl_page_prep);
888 * Notify layers about transfer completion.
890 * Invoked by transfer sub-system (which is a part of osc) to notify layers
891 * that a transfer, of which this page is a part of has completed.
893 * Completion call-backs are executed in the bottom-up order, so that
894 * uppermost layer (llite), responsible for the VFS/VM interaction runs last
895 * and can release locks safely.
897 * \pre pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
898 * \post pg->cp_state == CPS_CACHED
900 * \see cl_page_operations::cpo_completion()
902 void cl_page_completion(const struct lu_env *env,
903 struct cl_page *pg, enum cl_req_type crt, int ioret)
905 const struct cl_page_slice *slice;
906 struct cl_sync_io *anchor = pg->cp_sync_io;
908 PASSERT(env, pg, crt < CRT_NR);
909 PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
912 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
913 cl_page_state_set(env, pg, CPS_CACHED);
917 list_for_each_entry_reverse(slice, &pg->cp_layers, cpl_linkage) {
918 if (slice->cpl_ops->io[crt].cpo_completion != NULL)
919 (*slice->cpl_ops->io[crt].cpo_completion)(env, slice,
923 if (anchor != NULL) {
924 LASSERT(pg->cp_sync_io == anchor);
925 pg->cp_sync_io = NULL;
926 cl_sync_io_note(env, anchor, ioret);
930 EXPORT_SYMBOL(cl_page_completion);
933 * Notify layers that transfer formation engine decided to yank this page from
934 * the cache and to make it a part of a transfer.
936 * \pre pg->cp_state == CPS_CACHED
937 * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
939 * \see cl_page_operations::cpo_make_ready()
941 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
942 enum cl_req_type crt)
944 const struct cl_page_slice *sli;
947 PINVRNT(env, pg, crt < CRT_NR);
953 list_for_each_entry(sli, &pg->cp_layers, cpl_linkage) {
954 if (sli->cpl_ops->io[crt].cpo_make_ready != NULL)
955 result = (*sli->cpl_ops->io[crt].cpo_make_ready)(env,
963 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
964 cl_page_io_start(env, pg, crt);
966 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
969 EXPORT_SYMBOL(cl_page_make_ready);
972 * Called if a pge is being written back by kernel's intention.
974 * \pre cl_page_is_owned(pg, io)
975 * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
977 * \see cl_page_operations::cpo_flush()
979 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
982 const struct cl_page_slice *slice;
985 PINVRNT(env, pg, cl_page_is_owned(pg, io));
986 PINVRNT(env, pg, cl_page_invariant(pg));
990 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
991 if (slice->cpl_ops->cpo_flush != NULL)
992 result = (*slice->cpl_ops->cpo_flush)(env, slice, io);
999 CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1002 EXPORT_SYMBOL(cl_page_flush);
1005 * Tells transfer engine that only part of a page is to be transmitted.
1007 * \see cl_page_operations::cpo_clip()
1009 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1012 const struct cl_page_slice *slice;
1014 PINVRNT(env, pg, cl_page_invariant(pg));
1016 CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1017 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
1018 if (slice->cpl_ops->cpo_clip != NULL)
1019 (*slice->cpl_ops->cpo_clip)(env, slice, from, to);
1022 EXPORT_SYMBOL(cl_page_clip);
1025 * Prints human readable representation of \a pg to the \a f.
1027 void cl_page_header_print(const struct lu_env *env, void *cookie,
1028 lu_printer_t printer, const struct cl_page *pg)
1030 (*printer)(env, cookie,
1031 "page@%p[%d %p %d %d %p]\n",
1032 pg, atomic_read(&pg->cp_ref), pg->cp_obj,
1033 pg->cp_state, pg->cp_type,
1036 EXPORT_SYMBOL(cl_page_header_print);
1039 * Prints human readable representation of \a pg to the \a f.
1041 void cl_page_print(const struct lu_env *env, void *cookie,
1042 lu_printer_t printer, const struct cl_page *pg)
1044 const struct cl_page_slice *slice;
1047 cl_page_header_print(env, cookie, printer, pg);
1048 list_for_each_entry(slice, &pg->cp_layers, cpl_linkage) {
1049 if (slice->cpl_ops->cpo_print != NULL)
1050 result = (*slice->cpl_ops->cpo_print)(env, slice,
1055 (*printer)(env, cookie, "end page@%p\n", pg);
1057 EXPORT_SYMBOL(cl_page_print);
1060 * Cancel a page which is still in a transfer.
1062 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1064 const struct cl_page_slice *slice;
1067 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1068 if (slice->cpl_ops->cpo_cancel != NULL)
1069 result = (*slice->cpl_ops->cpo_cancel)(env, slice);
1080 * Converts a byte offset within object \a obj into a page index.
1082 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1084 return (loff_t)idx << PAGE_SHIFT;
1086 EXPORT_SYMBOL(cl_offset);
1089 * Converts a page index into a byte offset within object \a obj.
1091 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1093 return offset >> PAGE_SHIFT;
1095 EXPORT_SYMBOL(cl_index);
1097 size_t cl_page_size(const struct cl_object *obj)
1099 return 1UL << PAGE_SHIFT;
1101 EXPORT_SYMBOL(cl_page_size);
1104 * Adds page slice to the compound page.
1106 * This is called by cl_object_operations::coo_page_init() methods to add a
1107 * per-layer state to the page. New state is added at the end of
1108 * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1110 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1112 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1113 struct cl_object *obj, pgoff_t index,
1114 const struct cl_page_operations *ops)
1117 list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1118 slice->cpl_obj = obj;
1119 slice->cpl_index = index;
1120 slice->cpl_ops = ops;
1121 slice->cpl_page = page;
1124 EXPORT_SYMBOL(cl_page_slice_add);
1127 * Allocate and initialize cl_cache, called by ll_init_sbi().
1129 struct cl_client_cache *cl_cache_init(unsigned long lru_page_max)
1131 struct cl_client_cache *cache = NULL;
1134 OBD_ALLOC(cache, sizeof(*cache));
1138 /* Initialize cache data */
1139 atomic_set(&cache->ccc_users, 1);
1140 cache->ccc_lru_max = lru_page_max;
1141 atomic_long_set(&cache->ccc_lru_left, lru_page_max);
1142 spin_lock_init(&cache->ccc_lru_lock);
1143 INIT_LIST_HEAD(&cache->ccc_lru);
1145 /* turn unstable check off by default as it impacts performance */
1146 cache->ccc_unstable_check = 0;
1147 atomic_long_set(&cache->ccc_unstable_nr, 0);
1148 init_waitqueue_head(&cache->ccc_unstable_waitq);
1152 EXPORT_SYMBOL(cl_cache_init);
1155 * Increase cl_cache refcount
1157 void cl_cache_incref(struct cl_client_cache *cache)
1159 atomic_inc(&cache->ccc_users);
1161 EXPORT_SYMBOL(cl_cache_incref);
1164 * Decrease cl_cache refcount and free the cache if refcount=0.
1165 * Since llite, lov and osc all hold cl_cache refcount,
1166 * the free will not cause race. (LU-6173)
1168 void cl_cache_decref(struct cl_client_cache *cache)
1170 if (atomic_dec_and_test(&cache->ccc_users))
1171 OBD_FREE(cache, sizeof(*cache));
1173 EXPORT_SYMBOL(cl_cache_decref);