Whamcloud - gitweb
LU-744 obdclass: revise stats for cl_object cache
[fs/lustre-release.git] / lustre / obdclass / cl_page.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Client Lustre Page.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include <libcfs/libcfs.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <libcfs/list.h>
47
48 #include <cl_object.h>
49 #include "cl_internal.h"
50
51 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
52                             int radix);
53
54 static cfs_mem_cache_t      *cl_page_kmem = NULL;
55
56 static struct lu_kmem_descr cl_page_caches[] = {
57         {
58                 .ckd_cache = &cl_page_kmem,
59                 .ckd_name  = "cl_page_kmem",
60                 .ckd_size  = sizeof (struct cl_page)
61         },
62         {
63                 .ckd_cache = NULL
64         }
65 };
66
67 #ifdef LIBCFS_DEBUG
68 # define PASSERT(env, page, expr)                                       \
69   do {                                                                    \
70           if (unlikely(!(expr))) {                                      \
71                   CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n");    \
72                   LASSERT(0);                                           \
73           }                                                             \
74   } while (0)
75 #else /* !LIBCFS_DEBUG */
76 # define PASSERT(env, page, exp) \
77         ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
78 #endif /* !LIBCFS_DEBUG */
79
80 #ifdef INVARIANT_CHECK
81 # define PINVRNT(env, page, expr)                                       \
82   do {                                                                    \
83           if (unlikely(!(expr))) {                                      \
84                   CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n");    \
85                   LINVRNT(0);                                           \
86           }                                                             \
87   } while (0)
88 #else /* !INVARIANT_CHECK */
89 # define PINVRNT(env, page, exp) \
90         ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
91 #endif /* !INVARIANT_CHECK */
92
93 /* Disable page statistic by default due to huge performance penalty. */
94 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
95 #define CS_PAGE_INC(o, item) \
96         cfs_atomic_inc(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
97 #define CS_PAGE_DEC(o, item) \
98         cfs_atomic_dec(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
99 #define CS_PAGESTATE_INC(o, state) \
100         cfs_atomic_inc(&cl_object_site(o)->cs_pages_state[state])
101 #define CS_PAGESTATE_DEC(o, state) \
102         cfs_atomic_dec(&cl_object_site(o)->cs_pages_state[state])
103 #else
104 #define CS_PAGE_INC(o, item)
105 #define CS_PAGE_DEC(o, item)
106 #define CS_PAGESTATE_INC(o, state)
107 #define CS_PAGESTATE_DEC(o, state)
108 #endif
109
110 /**
111  * Internal version of cl_page_top, it should be called with page referenced,
112  * or cp_lock held.
113  */
114 static struct cl_page *cl_page_top_trusted(struct cl_page *page)
115 {
116         while (page->cp_parent != NULL)
117                 page = page->cp_parent;
118         return page;
119 }
120
121 /**
122  * Internal version of cl_page_get().
123  *
124  * This function can be used to obtain initial reference to previously
125  * unreferenced cached object. It can be called only if concurrent page
126  * reclamation is somehow prevented, e.g., by locking page radix-tree
127  * (cl_object_header::hdr->coh_page_guard), or by keeping a lock on a VM page,
128  * associated with \a page.
129  *
130  * Use with care! Not exported.
131  */
132 static void cl_page_get_trust(struct cl_page *page)
133 {
134         /*
135          * Checkless version for trusted users.
136          */
137         if (cfs_atomic_inc_return(&page->cp_ref) == 1)
138                 CS_PAGE_INC(page->cp_obj, busy);
139 }
140
141 /**
142  * Returns a slice within a page, corresponding to the given layer in the
143  * device stack.
144  *
145  * \see cl_lock_at()
146  */
147 static const struct cl_page_slice *
148 cl_page_at_trusted(const struct cl_page *page,
149                    const struct lu_device_type *dtype)
150 {
151         const struct cl_page_slice *slice;
152
153 #ifdef INVARIANT_CHECK
154         if (!cfs_atomic_read(&page->cp_ref))
155                 LASSERT_SPIN_LOCKED(&page->cp_lock);
156 #endif
157         ENTRY;
158
159         page = cl_page_top_trusted((struct cl_page *)page);
160         do {
161                 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
162                         if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
163                                 RETURN(slice);
164                 }
165                 page = page->cp_child;
166         } while (page != NULL);
167         RETURN(NULL);
168 }
169
170 /**
171  * Returns a page with given index in the given object, or NULL if no page is
172  * found. Acquires a reference on \a page.
173  *
174  * Locking: called under cl_object_header::coh_page_guard spin-lock.
175  */
176 struct cl_page *cl_page_lookup(struct cl_object_header *hdr, pgoff_t index)
177 {
178         struct cl_page *page;
179
180         LASSERT_SPIN_LOCKED(&hdr->coh_page_guard);
181
182         page = radix_tree_lookup(&hdr->coh_tree, index);
183         if (page != NULL) {
184                 cl_page_get_trust(page);
185         }
186         return page;
187 }
188 EXPORT_SYMBOL(cl_page_lookup);
189
190 /**
191  * Returns a list of pages by a given [start, end] of \a obj.
192  *
193  * \param resched If not NULL, then we give up before hogging CPU for too
194  * long and set *resched = 1, in that case caller should implement a retry
195  * logic.
196  *
197  * Gang tree lookup (radix_tree_gang_lookup()) optimization is absolutely
198  * crucial in the face of [offset, EOF] locks.
199  *
200  * Return at least one page in @queue unless there is no covered page.
201  */
202 int cl_page_gang_lookup(const struct lu_env *env, struct cl_object *obj,
203                         struct cl_io *io, pgoff_t start, pgoff_t end,
204                         cl_page_gang_cb_t cb, void *cbdata)
205 {
206         struct cl_object_header *hdr;
207         struct cl_page          *page;
208         struct cl_page         **pvec;
209         const struct cl_page_slice  *slice;
210         const struct lu_device_type *dtype;
211         pgoff_t                  idx;
212         unsigned int             nr;
213         unsigned int             i;
214         unsigned int             j;
215         int                      res = CLP_GANG_OKAY;
216         int                      tree_lock = 1;
217         ENTRY;
218
219         idx = start;
220         hdr = cl_object_header(obj);
221         pvec = cl_env_info(env)->clt_pvec;
222         dtype = cl_object_top(obj)->co_lu.lo_dev->ld_type;
223         spin_lock(&hdr->coh_page_guard);
224         while ((nr = radix_tree_gang_lookup(&hdr->coh_tree, (void **)pvec,
225                                             idx, CLT_PVEC_SIZE)) > 0) {
226                 int end_of_region = 0;
227                 idx = pvec[nr - 1]->cp_index + 1;
228                 for (i = 0, j = 0; i < nr; ++i) {
229                         page = pvec[i];
230                         pvec[i] = NULL;
231
232                         LASSERT(page->cp_type == CPT_CACHEABLE);
233                         if (page->cp_index > end) {
234                                 end_of_region = 1;
235                                 break;
236                         }
237                         if (page->cp_state == CPS_FREEING)
238                                 continue;
239
240                         slice = cl_page_at_trusted(page, dtype);
241                         /*
242                          * Pages for lsm-less file has no underneath sub-page
243                          * for osc, in case of ...
244                          */
245                         PASSERT(env, page, slice != NULL);
246
247                         page = slice->cpl_page;
248                         /*
249                          * Can safely call cl_page_get_trust() under
250                          * radix-tree spin-lock.
251                          *
252                          * XXX not true, because @page is from object another
253                          * than @hdr and protected by different tree lock.
254                          */
255                         cl_page_get_trust(page);
256                         lu_ref_add_atomic(&page->cp_reference,
257                                           "gang_lookup", cfs_current());
258                         pvec[j++] = page;
259                 }
260
261                 /*
262                  * Here a delicate locking dance is performed. Current thread
263                  * holds a reference to a page, but has to own it before it
264                  * can be placed into queue. Owning implies waiting, so
265                  * radix-tree lock is to be released. After a wait one has to
266                  * check that pages weren't truncated (cl_page_own() returns
267                  * error in the latter case).
268                  */
269                 spin_unlock(&hdr->coh_page_guard);
270                 tree_lock = 0;
271
272                 for (i = 0; i < j; ++i) {
273                         page = pvec[i];
274                         if (res == CLP_GANG_OKAY)
275                                 res = (*cb)(env, io, page, cbdata);
276                         lu_ref_del(&page->cp_reference,
277                                    "gang_lookup", cfs_current());
278                         cl_page_put(env, page);
279                 }
280                 if (nr < CLT_PVEC_SIZE || end_of_region)
281                         break;
282
283                 if (res == CLP_GANG_OKAY && cfs_need_resched())
284                         res = CLP_GANG_RESCHED;
285                 if (res != CLP_GANG_OKAY)
286                         break;
287
288                 spin_lock(&hdr->coh_page_guard);
289                 tree_lock = 1;
290         }
291         if (tree_lock)
292                 spin_unlock(&hdr->coh_page_guard);
293         RETURN(res);
294 }
295 EXPORT_SYMBOL(cl_page_gang_lookup);
296
297 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
298 {
299         struct cl_object *obj  = page->cp_obj;
300
301         PASSERT(env, page, cfs_list_empty(&page->cp_batch));
302         PASSERT(env, page, page->cp_owner == NULL);
303         PASSERT(env, page, page->cp_req == NULL);
304         PASSERT(env, page, page->cp_parent == NULL);
305         PASSERT(env, page, page->cp_state == CPS_FREEING);
306
307         ENTRY;
308         cfs_might_sleep();
309         while (!cfs_list_empty(&page->cp_layers)) {
310                 struct cl_page_slice *slice;
311
312                 slice = cfs_list_entry(page->cp_layers.next,
313                                        struct cl_page_slice, cpl_linkage);
314                 cfs_list_del_init(page->cp_layers.next);
315                 slice->cpl_ops->cpo_fini(env, slice);
316         }
317         CS_PAGE_DEC(obj, total);
318         CS_PAGESTATE_DEC(obj, page->cp_state);
319         lu_object_ref_del_at(&obj->co_lu, page->cp_obj_ref, "cl_page", page);
320         cl_object_put(env, obj);
321         lu_ref_fini(&page->cp_reference);
322         OBD_SLAB_FREE_PTR(page, cl_page_kmem);
323         EXIT;
324 }
325
326 /**
327  * Helper function updating page state. This is the only place in the code
328  * where cl_page::cp_state field is mutated.
329  */
330 static inline void cl_page_state_set_trust(struct cl_page *page,
331                                            enum cl_page_state state)
332 {
333         /* bypass const. */
334         *(enum cl_page_state *)&page->cp_state = state;
335 }
336
337 static int cl_page_alloc(const struct lu_env *env, struct cl_object *o,
338                          pgoff_t ind, struct page *vmpage,
339                          enum cl_page_type type, struct cl_page **out)
340 {
341         struct cl_page          *page;
342         struct cl_page          *err  = NULL;
343         struct lu_object_header *head;
344         int                      result;
345
346         ENTRY;
347         result = +1;
348         OBD_SLAB_ALLOC_PTR_GFP(page, cl_page_kmem, CFS_ALLOC_IO);
349         if (page != NULL) {
350                 cfs_atomic_set(&page->cp_ref, 1);
351                 page->cp_obj = o;
352                 cl_object_get(o);
353                 page->cp_obj_ref = lu_object_ref_add(&o->co_lu,
354                                                      "cl_page", page);
355                 page->cp_index = ind;
356                 cl_page_state_set_trust(page, CPS_CACHED);
357                 spin_lock_init(&page->cp_lock);
358                 page->cp_type = type;
359                 CFS_INIT_LIST_HEAD(&page->cp_layers);
360                 CFS_INIT_LIST_HEAD(&page->cp_batch);
361                 CFS_INIT_LIST_HEAD(&page->cp_flight);
362                 mutex_init(&page->cp_mutex);
363                 lu_ref_init(&page->cp_reference);
364                 head = o->co_lu.lo_header;
365                 cfs_list_for_each_entry(o, &head->loh_layers,
366                                         co_lu.lo_linkage) {
367                         if (o->co_ops->coo_page_init != NULL) {
368                                 err = o->co_ops->coo_page_init(env, o,
369                                                                page, vmpage);
370                                 if (err != NULL) {
371                                         cl_page_delete0(env, page, 0);
372                                         cl_page_free(env, page);
373                                         page = err;
374                                         break;
375                                 }
376                         }
377                 }
378                 if (err == NULL) {
379                         CS_PAGE_INC(o, busy);
380                         CS_PAGE_INC(o, total);
381                         CS_PAGE_INC(o, create);
382                         CS_PAGESTATE_DEC(o, CPS_CACHED);
383                         result = 0;
384                 }
385         } else
386                 page = ERR_PTR(-ENOMEM);
387         *out = page;
388         RETURN(result);
389 }
390
391 /**
392  * Returns a cl_page with index \a idx at the object \a o, and associated with
393  * the VM page \a vmpage.
394  *
395  * This is the main entry point into the cl_page caching interface. First, a
396  * cache (implemented as a per-object radix tree) is consulted. If page is
397  * found there, it is returned immediately. Otherwise new page is allocated
398  * and returned. In any case, additional reference to page is acquired.
399  *
400  * \see cl_object_find(), cl_lock_find()
401  */
402 static struct cl_page *cl_page_find0(const struct lu_env *env,
403                                      struct cl_object *o,
404                                      pgoff_t idx, struct page *vmpage,
405                                      enum cl_page_type type,
406                                      struct cl_page *parent)
407 {
408         struct cl_page          *page = NULL;
409         struct cl_page          *ghost = NULL;
410         struct cl_object_header *hdr;
411         int err;
412
413         LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
414         cfs_might_sleep();
415
416         ENTRY;
417
418         hdr = cl_object_header(o);
419         CS_PAGE_INC(o, lookup);
420
421         CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
422                idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
423         /* fast path. */
424         if (type == CPT_CACHEABLE) {
425                 /* cl_page::cp_lock is used to protect the page state and
426                  * refcount, but need an external lock to protect the
427                  * child/parent relationship, so vmpage lock must be held for
428                  * this purpose. */
429                 KLASSERT(PageLocked(vmpage));
430                 /*
431                  * cl_vmpage_page() can be called here without any locks as
432                  *
433                  *     - "vmpage" is locked (which prevents ->private from
434                  *       concurrent updates), and
435                  *
436                  *     - "o" cannot be destroyed while current thread holds a
437                  *       reference on it.
438                  */
439                 page = cl_vmpage_page(vmpage, o);
440                 PINVRNT(env, page,
441                         ergo(page != NULL,
442                              cl_page_vmpage(env, page) == vmpage &&
443                              (void *)radix_tree_lookup(&hdr->coh_tree,
444                                                        idx) == page));
445         }
446
447         if (page != NULL) {
448                 CS_PAGE_INC(o, hit);
449                 RETURN(page);
450         }
451
452         /* allocate and initialize cl_page */
453         err = cl_page_alloc(env, o, idx, vmpage, type, &page);
454         if (err != 0)
455                 RETURN(page);
456
457         if (type == CPT_TRANSIENT) {
458                 if (parent) {
459                         LASSERT(page->cp_parent == NULL);
460                         page->cp_parent = parent;
461                         parent->cp_child = page;
462                 }
463                 RETURN(page);
464         }
465
466         /*
467          * XXX optimization: use radix_tree_preload() here, and change tree
468          * gfp mask to GFP_KERNEL in cl_object_header_init().
469          */
470         spin_lock(&hdr->coh_page_guard);
471         err = radix_tree_insert(&hdr->coh_tree, idx, page);
472         if (err != 0) {
473                 ghost = page;
474                 /*
475                  * Noted by Jay: a lock on \a vmpage protects cl_page_find()
476                  * from this race, but
477                  *
478                  *     0. it's better to have cl_page interface "locally
479                  *     consistent" so that its correctness can be reasoned
480                  *     about without appealing to the (obscure world of) VM
481                  *     locking.
482                  *
483                  *     1. handling this race allows ->coh_tree to remain
484                  *     consistent even when VM locking is somehow busted,
485                  *     which is very useful during diagnosing and debugging.
486                  */
487                 page = ERR_PTR(err);
488                 CL_PAGE_DEBUG(D_ERROR, env, ghost,
489                               "fail to insert into radix tree: %d\n", err);
490         } else {
491                 if (parent) {
492                         LASSERT(page->cp_parent == NULL);
493                         page->cp_parent = parent;
494                         parent->cp_child = page;
495                 }
496                 hdr->coh_pages++;
497         }
498         spin_unlock(&hdr->coh_page_guard);
499
500         if (unlikely(ghost != NULL)) {
501                 CS_PAGE_DEC(o, busy);
502                 cl_page_delete0(env, ghost, 0);
503                 cl_page_free(env, ghost);
504         }
505         RETURN(page);
506 }
507
508 struct cl_page *cl_page_find(const struct lu_env *env, struct cl_object *o,
509                              pgoff_t idx, struct page *vmpage,
510                              enum cl_page_type type)
511 {
512         return cl_page_find0(env, o, idx, vmpage, type, NULL);
513 }
514 EXPORT_SYMBOL(cl_page_find);
515
516
517 struct cl_page *cl_page_find_sub(const struct lu_env *env, struct cl_object *o,
518                                  pgoff_t idx, struct page *vmpage,
519                                  struct cl_page *parent)
520 {
521         return cl_page_find0(env, o, idx, vmpage, parent->cp_type, parent);
522 }
523 EXPORT_SYMBOL(cl_page_find_sub);
524
525 static inline int cl_page_invariant(const struct cl_page *pg)
526 {
527         struct cl_object_header *header;
528         struct cl_page          *parent;
529         struct cl_page          *child;
530         struct cl_io            *owner;
531
532         /*
533          * Page invariant is protected by a VM lock.
534          */
535         LINVRNT(cl_page_is_vmlocked(NULL, pg));
536
537         header = cl_object_header(pg->cp_obj);
538         parent = pg->cp_parent;
539         child  = pg->cp_child;
540         owner  = pg->cp_owner;
541
542         return cfs_atomic_read(&pg->cp_ref) > 0 &&
543                 ergo(parent != NULL, parent->cp_child == pg) &&
544                 ergo(child != NULL, child->cp_parent == pg) &&
545                 ergo(child != NULL, pg->cp_obj != child->cp_obj) &&
546                 ergo(parent != NULL, pg->cp_obj != parent->cp_obj) &&
547                 ergo(owner != NULL && parent != NULL,
548                      parent->cp_owner == pg->cp_owner->ci_parent) &&
549                 ergo(owner != NULL && child != NULL,
550                      child->cp_owner->ci_parent == owner) &&
551                 /*
552                  * Either page is early in initialization (has neither child
553                  * nor parent yet), or it is in the object radix tree.
554                  */
555                 ergo(pg->cp_state < CPS_FREEING && pg->cp_type == CPT_CACHEABLE,
556                      (void *)radix_tree_lookup(&header->coh_tree,
557                                                pg->cp_index) == pg ||
558                      (child == NULL && parent == NULL));
559 }
560
561 static void cl_page_state_set0(const struct lu_env *env,
562                                struct cl_page *page, enum cl_page_state state)
563 {
564         enum cl_page_state old;
565
566         /*
567          * Matrix of allowed state transitions [old][new], for sanity
568          * checking.
569          */
570         static const int allowed_transitions[CPS_NR][CPS_NR] = {
571                 [CPS_CACHED] = {
572                         [CPS_CACHED]  = 0,
573                         [CPS_OWNED]   = 1, /* io finds existing cached page */
574                         [CPS_PAGEIN]  = 0,
575                         [CPS_PAGEOUT] = 1, /* write-out from the cache */
576                         [CPS_FREEING] = 1, /* eviction on the memory pressure */
577                 },
578                 [CPS_OWNED] = {
579                         [CPS_CACHED]  = 1, /* release to the cache */
580                         [CPS_OWNED]   = 0,
581                         [CPS_PAGEIN]  = 1, /* start read immediately */
582                         [CPS_PAGEOUT] = 1, /* start write immediately */
583                         [CPS_FREEING] = 1, /* lock invalidation or truncate */
584                 },
585                 [CPS_PAGEIN] = {
586                         [CPS_CACHED]  = 1, /* io completion */
587                         [CPS_OWNED]   = 0,
588                         [CPS_PAGEIN]  = 0,
589                         [CPS_PAGEOUT] = 0,
590                         [CPS_FREEING] = 0,
591                 },
592                 [CPS_PAGEOUT] = {
593                         [CPS_CACHED]  = 1, /* io completion */
594                         [CPS_OWNED]   = 0,
595                         [CPS_PAGEIN]  = 0,
596                         [CPS_PAGEOUT] = 0,
597                         [CPS_FREEING] = 0,
598                 },
599                 [CPS_FREEING] = {
600                         [CPS_CACHED]  = 0,
601                         [CPS_OWNED]   = 0,
602                         [CPS_PAGEIN]  = 0,
603                         [CPS_PAGEOUT] = 0,
604                         [CPS_FREEING] = 0,
605                 }
606         };
607
608         ENTRY;
609         old = page->cp_state;
610         PASSERT(env, page, allowed_transitions[old][state]);
611         CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
612         for (; page != NULL; page = page->cp_child) {
613                 PASSERT(env, page, page->cp_state == old);
614                 PASSERT(env, page,
615                         equi(state == CPS_OWNED, page->cp_owner != NULL));
616
617                 CS_PAGESTATE_DEC(page->cp_obj, page->cp_state);
618                 CS_PAGESTATE_INC(page->cp_obj, state);
619                 cl_page_state_set_trust(page, state);
620         }
621         EXIT;
622 }
623
624 static void cl_page_state_set(const struct lu_env *env,
625                               struct cl_page *page, enum cl_page_state state)
626 {
627         cl_page_state_set0(env, page, state);
628 }
629
630 /**
631  * Acquires an additional reference to a page.
632  *
633  * This can be called only by caller already possessing a reference to \a
634  * page.
635  *
636  * \see cl_object_get(), cl_lock_get().
637  */
638 void cl_page_get(struct cl_page *page)
639 {
640         ENTRY;
641         cl_page_get_trust(page);
642         EXIT;
643 }
644 EXPORT_SYMBOL(cl_page_get);
645
646 /**
647  * Releases a reference to a page.
648  *
649  * When last reference is released, page is returned to the cache, unless it
650  * is in cl_page_state::CPS_FREEING state, in which case it is immediately
651  * destroyed.
652  *
653  * \see cl_object_put(), cl_lock_put().
654  */
655 void cl_page_put(const struct lu_env *env, struct cl_page *page)
656 {
657         PASSERT(env, page, cfs_atomic_read(&page->cp_ref) > !!page->cp_parent);
658
659         ENTRY;
660         CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
661                        cfs_atomic_read(&page->cp_ref));
662
663         if (cfs_atomic_dec_and_lock(&page->cp_ref, &page->cp_lock)) {
664                 CS_PAGE_DEC(page->cp_obj, busy);
665                 /* We're going to access the page w/o a reference, but it's
666                  * ok because we have grabbed the lock cp_lock, which
667                  * means nobody is able to free this page behind us.
668                  */
669                 if (page->cp_state == CPS_FREEING) {
670                         /* We drop the page reference and check the page state
671                          * inside the cp_lock. So that if it gets here,
672                          * it is the REALLY last reference to this page.
673                          */
674                         spin_unlock(&page->cp_lock);
675
676                         LASSERT(cfs_atomic_read(&page->cp_ref) == 0);
677                         PASSERT(env, page, page->cp_owner == NULL);
678                         PASSERT(env, page, cfs_list_empty(&page->cp_batch));
679                         /*
680                          * Page is no longer reachable by other threads. Tear
681                          * it down.
682                          */
683                         cl_page_free(env, page);
684
685                         EXIT;
686                         return;
687                 }
688                 spin_unlock(&page->cp_lock);
689         }
690
691         EXIT;
692 }
693 EXPORT_SYMBOL(cl_page_put);
694
695 /**
696  * Returns a VM page associated with a given cl_page.
697  */
698 cfs_page_t *cl_page_vmpage(const struct lu_env *env, struct cl_page *page)
699 {
700         const struct cl_page_slice *slice;
701
702         /*
703          * Find uppermost layer with ->cpo_vmpage() method, and return its
704          * result.
705          */
706         page = cl_page_top(page);
707         do {
708                 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
709                         if (slice->cpl_ops->cpo_vmpage != NULL)
710                                 RETURN(slice->cpl_ops->cpo_vmpage(env, slice));
711                 }
712                 page = page->cp_child;
713         } while (page != NULL);
714         LBUG(); /* ->cpo_vmpage() has to be defined somewhere in the stack */
715 }
716 EXPORT_SYMBOL(cl_page_vmpage);
717
718 /**
719  * Returns a cl_page associated with a VM page, and given cl_object.
720  */
721 struct cl_page *cl_vmpage_page(cfs_page_t *vmpage, struct cl_object *obj)
722 {
723         struct cl_page *top;
724         struct cl_page *page;
725
726         ENTRY;
727         KLASSERT(PageLocked(vmpage));
728
729         /*
730          * NOTE: absence of races and liveness of data are guaranteed by page
731          *       lock on a "vmpage". That works because object destruction has
732          *       bottom-to-top pass.
733          */
734
735         /*
736          * This loop assumes that ->private points to the top-most page. This
737          * can be rectified easily.
738          */
739         top = (struct cl_page *)vmpage->private;
740         if (top == NULL)
741                 RETURN(NULL);
742
743         spin_lock(&top->cp_lock);
744         for (page = top; page != NULL; page = page->cp_child) {
745                 if (cl_object_same(page->cp_obj, obj)) {
746                         cl_page_get_trust(page);
747                         break;
748                 }
749         }
750         spin_unlock(&top->cp_lock);
751         LASSERT(ergo(page, page->cp_type == CPT_CACHEABLE));
752         RETURN(page);
753 }
754 EXPORT_SYMBOL(cl_vmpage_page);
755
756 /**
757  * Returns the top-page for a given page.
758  *
759  * \see cl_object_top(), cl_io_top()
760  */
761 struct cl_page *cl_page_top(struct cl_page *page)
762 {
763         return cl_page_top_trusted(page);
764 }
765 EXPORT_SYMBOL(cl_page_top);
766
767 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
768                                        const struct lu_device_type *dtype)
769 {
770         return cl_page_at_trusted(page, dtype);
771 }
772 EXPORT_SYMBOL(cl_page_at);
773
774 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
775
776 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...)                   \
777 ({                                                                      \
778         const struct lu_env        *__env  = (_env);                    \
779         struct cl_page             *__page = (_page);                   \
780         const struct cl_page_slice *__scan;                             \
781         int                         __result;                           \
782         ptrdiff_t                   __op   = (_op);                     \
783         int                       (*__method)_proto;                    \
784                                                                         \
785         __result = 0;                                                   \
786         __page = cl_page_top(__page);                                   \
787         do {                                                            \
788                 cfs_list_for_each_entry(__scan, &__page->cp_layers,     \
789                                         cpl_linkage) {                  \
790                         __method = *(void **)((char *)__scan->cpl_ops + \
791                                               __op);                    \
792                         if (__method != NULL) {                         \
793                                 __result = (*__method)(__env, __scan,   \
794                                                        ## __VA_ARGS__); \
795                                 if (__result != 0)                      \
796                                         break;                          \
797                         }                                               \
798                 }                                                       \
799                 __page = __page->cp_child;                              \
800         } while (__page != NULL && __result == 0);                      \
801         if (__result > 0)                                               \
802                 __result = 0;                                           \
803         __result;                                                       \
804 })
805
806 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...)                   \
807 do {                                                                    \
808         const struct lu_env        *__env  = (_env);                    \
809         struct cl_page             *__page = (_page);                   \
810         const struct cl_page_slice *__scan;                             \
811         ptrdiff_t                   __op   = (_op);                     \
812         void                      (*__method)_proto;                    \
813                                                                         \
814         __page = cl_page_top(__page);                                   \
815         do {                                                            \
816                 cfs_list_for_each_entry(__scan, &__page->cp_layers,     \
817                                         cpl_linkage) {                  \
818                         __method = *(void **)((char *)__scan->cpl_ops + \
819                                               __op);                    \
820                         if (__method != NULL)                           \
821                                 (*__method)(__env, __scan,              \
822                                             ## __VA_ARGS__);            \
823                 }                                                       \
824                 __page = __page->cp_child;                              \
825         } while (__page != NULL);                                       \
826 } while (0)
827
828 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...)               \
829 do {                                                                        \
830         const struct lu_env        *__env  = (_env);                        \
831         struct cl_page             *__page = (_page);                       \
832         const struct cl_page_slice *__scan;                                 \
833         ptrdiff_t                   __op   = (_op);                         \
834         void                      (*__method)_proto;                        \
835                                                                             \
836         /* get to the bottom page. */                                       \
837         while (__page->cp_child != NULL)                                    \
838                 __page = __page->cp_child;                                  \
839         do {                                                                \
840                 cfs_list_for_each_entry_reverse(__scan, &__page->cp_layers, \
841                                                 cpl_linkage) {              \
842                         __method = *(void **)((char *)__scan->cpl_ops +     \
843                                               __op);                        \
844                         if (__method != NULL)                               \
845                                 (*__method)(__env, __scan,                  \
846                                             ## __VA_ARGS__);                \
847                 }                                                           \
848                 __page = __page->cp_parent;                                 \
849         } while (__page != NULL);                                           \
850 } while (0)
851
852 static int cl_page_invoke(const struct lu_env *env,
853                           struct cl_io *io, struct cl_page *page, ptrdiff_t op)
854
855 {
856         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
857         ENTRY;
858         RETURN(CL_PAGE_INVOKE(env, page, op,
859                               (const struct lu_env *,
860                                const struct cl_page_slice *, struct cl_io *),
861                               io));
862 }
863
864 static void cl_page_invoid(const struct lu_env *env,
865                            struct cl_io *io, struct cl_page *page, ptrdiff_t op)
866
867 {
868         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
869         ENTRY;
870         CL_PAGE_INVOID(env, page, op,
871                        (const struct lu_env *,
872                         const struct cl_page_slice *, struct cl_io *), io);
873         EXIT;
874 }
875
876 static void cl_page_owner_clear(struct cl_page *page)
877 {
878         ENTRY;
879         for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
880                 if (page->cp_owner != NULL) {
881                         LASSERT(page->cp_owner->ci_owned_nr > 0);
882                         page->cp_owner->ci_owned_nr--;
883                         page->cp_owner = NULL;
884                         page->cp_task = NULL;
885                 }
886         }
887         EXIT;
888 }
889
890 static void cl_page_owner_set(struct cl_page *page)
891 {
892         ENTRY;
893         for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
894                 LASSERT(page->cp_owner != NULL);
895                 page->cp_owner->ci_owned_nr++;
896         }
897         EXIT;
898 }
899
900 void cl_page_disown0(const struct lu_env *env,
901                      struct cl_io *io, struct cl_page *pg)
902 {
903         enum cl_page_state state;
904
905         ENTRY;
906         state = pg->cp_state;
907         PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
908         PINVRNT(env, pg, cl_page_invariant(pg));
909         cl_page_owner_clear(pg);
910
911         if (state == CPS_OWNED)
912                 cl_page_state_set(env, pg, CPS_CACHED);
913         /*
914          * Completion call-backs are executed in the bottom-up order, so that
915          * uppermost layer (llite), responsible for VFS/VM interaction runs
916          * last and can release locks safely.
917          */
918         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
919                                (const struct lu_env *,
920                                 const struct cl_page_slice *, struct cl_io *),
921                                io);
922         EXIT;
923 }
924
925 /**
926  * returns true, iff page is owned by the given io.
927  */
928 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
929 {
930         LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
931         ENTRY;
932         RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == io);
933 }
934 EXPORT_SYMBOL(cl_page_is_owned);
935
936 /**
937  * Try to own a page by IO.
938  *
939  * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
940  * into cl_page_state::CPS_OWNED state.
941  *
942  * \pre  !cl_page_is_owned(pg, io)
943  * \post result == 0 iff cl_page_is_owned(pg, io)
944  *
945  * \retval 0   success
946  *
947  * \retval -ve failure, e.g., page was destroyed (and landed in
948  *             cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
949  *             or, page was owned by another thread, or in IO.
950  *
951  * \see cl_page_disown()
952  * \see cl_page_operations::cpo_own()
953  * \see cl_page_own_try()
954  * \see cl_page_own
955  */
956 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
957                         struct cl_page *pg, int nonblock)
958 {
959         int result;
960
961         PINVRNT(env, pg, !cl_page_is_owned(pg, io));
962
963         ENTRY;
964         pg = cl_page_top(pg);
965         io = cl_io_top(io);
966
967         if (pg->cp_state == CPS_FREEING) {
968                 result = -ENOENT;
969         } else {
970                 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
971                                         (const struct lu_env *,
972                                          const struct cl_page_slice *,
973                                          struct cl_io *, int),
974                                         io, nonblock);
975                 if (result == 0) {
976                         PASSERT(env, pg, pg->cp_owner == NULL);
977                         PASSERT(env, pg, pg->cp_req == NULL);
978                         pg->cp_owner = io;
979                         pg->cp_task  = current;
980                         cl_page_owner_set(pg);
981                         if (pg->cp_state != CPS_FREEING) {
982                                 cl_page_state_set(env, pg, CPS_OWNED);
983                         } else {
984                                 cl_page_disown0(env, io, pg);
985                                 result = -ENOENT;
986                         }
987                 }
988         }
989         PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
990         RETURN(result);
991 }
992
993 /**
994  * Own a page, might be blocked.
995  *
996  * \see cl_page_own0()
997  */
998 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
999 {
1000         return cl_page_own0(env, io, pg, 0);
1001 }
1002 EXPORT_SYMBOL(cl_page_own);
1003
1004 /**
1005  * Nonblock version of cl_page_own().
1006  *
1007  * \see cl_page_own0()
1008  */
1009 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
1010                     struct cl_page *pg)
1011 {
1012         return cl_page_own0(env, io, pg, 1);
1013 }
1014 EXPORT_SYMBOL(cl_page_own_try);
1015
1016
1017 /**
1018  * Assume page ownership.
1019  *
1020  * Called when page is already locked by the hosting VM.
1021  *
1022  * \pre !cl_page_is_owned(pg, io)
1023  * \post cl_page_is_owned(pg, io)
1024  *
1025  * \see cl_page_operations::cpo_assume()
1026  */
1027 void cl_page_assume(const struct lu_env *env,
1028                     struct cl_io *io, struct cl_page *pg)
1029 {
1030         PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
1031
1032         ENTRY;
1033         pg = cl_page_top(pg);
1034         io = cl_io_top(io);
1035
1036         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
1037         PASSERT(env, pg, pg->cp_owner == NULL);
1038         pg->cp_owner = io;
1039         pg->cp_task = current;
1040         cl_page_owner_set(pg);
1041         cl_page_state_set(env, pg, CPS_OWNED);
1042         EXIT;
1043 }
1044 EXPORT_SYMBOL(cl_page_assume);
1045
1046 /**
1047  * Releases page ownership without unlocking the page.
1048  *
1049  * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
1050  * underlying VM page (as VM is supposed to do this itself).
1051  *
1052  * \pre   cl_page_is_owned(pg, io)
1053  * \post !cl_page_is_owned(pg, io)
1054  *
1055  * \see cl_page_assume()
1056  */
1057 void cl_page_unassume(const struct lu_env *env,
1058                       struct cl_io *io, struct cl_page *pg)
1059 {
1060         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1061         PINVRNT(env, pg, cl_page_invariant(pg));
1062
1063         ENTRY;
1064         pg = cl_page_top(pg);
1065         io = cl_io_top(io);
1066         cl_page_owner_clear(pg);
1067         cl_page_state_set(env, pg, CPS_CACHED);
1068         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
1069                                (const struct lu_env *,
1070                                 const struct cl_page_slice *, struct cl_io *),
1071                                io);
1072         EXIT;
1073 }
1074 EXPORT_SYMBOL(cl_page_unassume);
1075
1076 /**
1077  * Releases page ownership.
1078  *
1079  * Moves page into cl_page_state::CPS_CACHED.
1080  *
1081  * \pre   cl_page_is_owned(pg, io)
1082  * \post !cl_page_is_owned(pg, io)
1083  *
1084  * \see cl_page_own()
1085  * \see cl_page_operations::cpo_disown()
1086  */
1087 void cl_page_disown(const struct lu_env *env,
1088                     struct cl_io *io, struct cl_page *pg)
1089 {
1090         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1091
1092         ENTRY;
1093         pg = cl_page_top(pg);
1094         io = cl_io_top(io);
1095         cl_page_disown0(env, io, pg);
1096         EXIT;
1097 }
1098 EXPORT_SYMBOL(cl_page_disown);
1099
1100 /**
1101  * Called when page is to be removed from the object, e.g., as a result of
1102  * truncate.
1103  *
1104  * Calls cl_page_operations::cpo_discard() top-to-bottom.
1105  *
1106  * \pre cl_page_is_owned(pg, io)
1107  *
1108  * \see cl_page_operations::cpo_discard()
1109  */
1110 void cl_page_discard(const struct lu_env *env,
1111                      struct cl_io *io, struct cl_page *pg)
1112 {
1113         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1114         PINVRNT(env, pg, cl_page_invariant(pg));
1115
1116         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
1117 }
1118 EXPORT_SYMBOL(cl_page_discard);
1119
1120 /**
1121  * Version of cl_page_delete() that can be called for not fully constructed
1122  * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
1123  * path. Doesn't check page invariant.
1124  */
1125 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
1126                             int radix)
1127 {
1128         struct cl_page *tmp = pg;
1129         ENTRY;
1130
1131         PASSERT(env, pg, pg == cl_page_top(pg));
1132         PASSERT(env, pg, pg->cp_state != CPS_FREEING);
1133
1134         /*
1135          * Severe all ways to obtain new pointers to @pg.
1136          */
1137         cl_page_owner_clear(pg);
1138
1139         /* 
1140          * unexport the page firstly before freeing it so that
1141          * the page content is considered to be invalid.
1142          * We have to do this because a CPS_FREEING cl_page may
1143          * be NOT under the protection of a cl_lock.
1144          * Afterwards, if this page is found by other threads, then this
1145          * page will be forced to reread.
1146          */
1147         cl_page_export(env, pg, 0);
1148         cl_page_state_set0(env, pg, CPS_FREEING);
1149
1150         if (tmp->cp_type == CPT_CACHEABLE) {
1151                 if (!radix)
1152                         /* !radix means that @pg is not yet in the radix tree,
1153                          * skip removing it.
1154                          */
1155                         tmp = pg->cp_child;
1156                 for (; tmp != NULL; tmp = tmp->cp_child) {
1157                         void                    *value;
1158                         struct cl_object_header *hdr;
1159
1160                         hdr = cl_object_header(tmp->cp_obj);
1161                         spin_lock(&hdr->coh_page_guard);
1162                         value = radix_tree_delete(&hdr->coh_tree,
1163                                                   tmp->cp_index);
1164                         PASSERT(env, tmp, value == tmp);
1165                         PASSERT(env, tmp, hdr->coh_pages > 0);
1166                         hdr->coh_pages--;
1167                         spin_unlock(&hdr->coh_page_guard);
1168                 }
1169         }
1170
1171         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_delete),
1172                        (const struct lu_env *, const struct cl_page_slice *));
1173         EXIT;
1174 }
1175
1176 /**
1177  * Called when a decision is made to throw page out of memory.
1178  *
1179  * Notifies all layers about page destruction by calling
1180  * cl_page_operations::cpo_delete() method top-to-bottom.
1181  *
1182  * Moves page into cl_page_state::CPS_FREEING state (this is the only place
1183  * where transition to this state happens).
1184  *
1185  * Eliminates all venues through which new references to the page can be
1186  * obtained:
1187  *
1188  *     - removes page from the radix trees,
1189  *
1190  *     - breaks linkage from VM page to cl_page.
1191  *
1192  * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
1193  * drain after some time, at which point page will be recycled.
1194  *
1195  * \pre  pg == cl_page_top(pg)
1196  * \pre  VM page is locked
1197  * \post pg->cp_state == CPS_FREEING
1198  *
1199  * \see cl_page_operations::cpo_delete()
1200  */
1201 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
1202 {
1203         PINVRNT(env, pg, cl_page_invariant(pg));
1204         ENTRY;
1205         cl_page_delete0(env, pg, 1);
1206         EXIT;
1207 }
1208 EXPORT_SYMBOL(cl_page_delete);
1209
1210 /**
1211  * Unmaps page from user virtual memory.
1212  *
1213  * Calls cl_page_operations::cpo_unmap() through all layers top-to-bottom. The
1214  * layer responsible for VM interaction has to unmap page from user space
1215  * virtual memory.
1216  *
1217  * \see cl_page_operations::cpo_unmap()
1218  */
1219 int cl_page_unmap(const struct lu_env *env,
1220                   struct cl_io *io, struct cl_page *pg)
1221 {
1222         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1223         PINVRNT(env, pg, cl_page_invariant(pg));
1224
1225         return cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_unmap));
1226 }
1227 EXPORT_SYMBOL(cl_page_unmap);
1228
1229 /**
1230  * Marks page up-to-date.
1231  *
1232  * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
1233  * layer responsible for VM interaction has to mark/clear page as up-to-date
1234  * by the \a uptodate argument.
1235  *
1236  * \see cl_page_operations::cpo_export()
1237  */
1238 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
1239 {
1240         PINVRNT(env, pg, cl_page_invariant(pg));
1241         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
1242                        (const struct lu_env *,
1243                         const struct cl_page_slice *, int), uptodate);
1244 }
1245 EXPORT_SYMBOL(cl_page_export);
1246
1247 /**
1248  * Returns true, iff \a pg is VM locked in a suitable sense by the calling
1249  * thread.
1250  */
1251 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
1252 {
1253         int result;
1254         const struct cl_page_slice *slice;
1255
1256         ENTRY;
1257         pg = cl_page_top_trusted((struct cl_page *)pg);
1258         slice = container_of(pg->cp_layers.next,
1259                              const struct cl_page_slice, cpl_linkage);
1260         PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
1261         /*
1262          * Call ->cpo_is_vmlocked() directly instead of going through
1263          * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
1264          * cl_page_invariant().
1265          */
1266         result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
1267         PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
1268         RETURN(result == -EBUSY);
1269 }
1270 EXPORT_SYMBOL(cl_page_is_vmlocked);
1271
1272 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
1273 {
1274         ENTRY;
1275         RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
1276 }
1277
1278 static void cl_page_io_start(const struct lu_env *env,
1279                              struct cl_page *pg, enum cl_req_type crt)
1280 {
1281         /*
1282          * Page is queued for IO, change its state.
1283          */
1284         ENTRY;
1285         cl_page_owner_clear(pg);
1286         cl_page_state_set(env, pg, cl_req_type_state(crt));
1287         EXIT;
1288 }
1289
1290 /**
1291  * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
1292  * called top-to-bottom. Every layer either agrees to submit this page (by
1293  * returning 0), or requests to omit this page (by returning -EALREADY). Layer
1294  * handling interactions with the VM also has to inform VM that page is under
1295  * transfer now.
1296  */
1297 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
1298                  struct cl_page *pg, enum cl_req_type crt)
1299 {
1300         int result;
1301
1302         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1303         PINVRNT(env, pg, cl_page_invariant(pg));
1304         PINVRNT(env, pg, crt < CRT_NR);
1305
1306         /*
1307          * XXX this has to be called bottom-to-top, so that llite can set up
1308          * PG_writeback without risking other layers deciding to skip this
1309          * page.
1310          */
1311         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
1312         if (result == 0)
1313                 cl_page_io_start(env, pg, crt);
1314
1315         KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
1316                       equi(result == 0,
1317                            PageWriteback(cl_page_vmpage(env, pg)))));
1318         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1319         return result;
1320 }
1321 EXPORT_SYMBOL(cl_page_prep);
1322
1323 /**
1324  * Notify layers about transfer completion.
1325  *
1326  * Invoked by transfer sub-system (which is a part of osc) to notify layers
1327  * that a transfer, of which this page is a part of has completed.
1328  *
1329  * Completion call-backs are executed in the bottom-up order, so that
1330  * uppermost layer (llite), responsible for the VFS/VM interaction runs last
1331  * and can release locks safely.
1332  *
1333  * \pre  pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1334  * \post pg->cp_state == CPS_CACHED
1335  *
1336  * \see cl_page_operations::cpo_completion()
1337  */
1338 void cl_page_completion(const struct lu_env *env,
1339                         struct cl_page *pg, enum cl_req_type crt, int ioret)
1340 {
1341         struct cl_sync_io *anchor = pg->cp_sync_io;
1342
1343         PASSERT(env, pg, crt < CRT_NR);
1344         /* cl_page::cp_req already cleared by the caller (osc_completion()) */
1345         PASSERT(env, pg, pg->cp_req == NULL);
1346         PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
1347
1348         ENTRY;
1349         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
1350         if (crt == CRT_READ && ioret == 0) {
1351                 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
1352                 pg->cp_flags |= CPF_READ_COMPLETED;
1353         }
1354
1355         cl_page_state_set(env, pg, CPS_CACHED);
1356         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
1357                                (const struct lu_env *,
1358                                 const struct cl_page_slice *, int), ioret);
1359         if (anchor) {
1360                 LASSERT(cl_page_is_vmlocked(env, pg));
1361                 LASSERT(pg->cp_sync_io == anchor);
1362                 pg->cp_sync_io = NULL;
1363                 cl_sync_io_note(anchor, ioret);
1364         }
1365         EXIT;
1366 }
1367 EXPORT_SYMBOL(cl_page_completion);
1368
1369 /**
1370  * Notify layers that transfer formation engine decided to yank this page from
1371  * the cache and to make it a part of a transfer.
1372  *
1373  * \pre  pg->cp_state == CPS_CACHED
1374  * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1375  *
1376  * \see cl_page_operations::cpo_make_ready()
1377  */
1378 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1379                        enum cl_req_type crt)
1380 {
1381         int result;
1382
1383         PINVRNT(env, pg, crt < CRT_NR);
1384
1385         ENTRY;
1386         result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1387                                 (const struct lu_env *,
1388                                  const struct cl_page_slice *));
1389         if (result == 0) {
1390                 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1391                 cl_page_io_start(env, pg, crt);
1392         }
1393         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1394         RETURN(result);
1395 }
1396 EXPORT_SYMBOL(cl_page_make_ready);
1397
1398 /**
1399  * Notify layers that high level io decided to place this page into a cache
1400  * for future transfer.
1401  *
1402  * The layer implementing transfer engine (osc) has to register this page in
1403  * its queues.
1404  *
1405  * \pre  cl_page_is_owned(pg, io)
1406  * \post cl_page_is_owned(pg, io)
1407  *
1408  * \see cl_page_operations::cpo_cache_add()
1409  */
1410 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1411                       struct cl_page *pg, enum cl_req_type crt)
1412 {
1413         const struct cl_page_slice *scan;
1414         int result = 0;
1415
1416         PINVRNT(env, pg, crt < CRT_NR);
1417         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1418         PINVRNT(env, pg, cl_page_invariant(pg));
1419
1420         ENTRY;
1421
1422         cfs_list_for_each_entry(scan, &pg->cp_layers, cpl_linkage) {
1423                 if (scan->cpl_ops->io[crt].cpo_cache_add == NULL)
1424                         continue;
1425
1426                 result = scan->cpl_ops->io[crt].cpo_cache_add(env, scan, io);
1427                 if (result != 0)
1428                         break;
1429         }
1430         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1431         RETURN(result);
1432 }
1433 EXPORT_SYMBOL(cl_page_cache_add);
1434
1435 /**
1436  * Called if a pge is being written back by kernel's intention.
1437  *
1438  * \pre  cl_page_is_owned(pg, io)
1439  * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
1440  *
1441  * \see cl_page_operations::cpo_flush()
1442  */
1443 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
1444                   struct cl_page *pg)
1445 {
1446         int result;
1447
1448         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1449         PINVRNT(env, pg, cl_page_invariant(pg));
1450
1451         ENTRY;
1452
1453         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_flush));
1454
1455         CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1456         RETURN(result);
1457 }
1458 EXPORT_SYMBOL(cl_page_flush);
1459
1460 /**
1461  * Checks whether page is protected by any extent lock is at least required
1462  * mode.
1463  *
1464  * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1465  * \see cl_page_operations::cpo_is_under_lock()
1466  */
1467 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1468                           struct cl_page *page)
1469 {
1470         int rc;
1471
1472         PINVRNT(env, page, cl_page_invariant(page));
1473
1474         ENTRY;
1475         rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1476                             (const struct lu_env *,
1477                              const struct cl_page_slice *, struct cl_io *),
1478                             io);
1479         PASSERT(env, page, rc != 0);
1480         RETURN(rc);
1481 }
1482 EXPORT_SYMBOL(cl_page_is_under_lock);
1483
1484 static int page_prune_cb(const struct lu_env *env, struct cl_io *io,
1485                          struct cl_page *page, void *cbdata)
1486 {
1487         cl_page_own(env, io, page);
1488         cl_page_unmap(env, io, page);
1489         cl_page_discard(env, io, page);
1490         cl_page_disown(env, io, page);
1491         return CLP_GANG_OKAY;
1492 }
1493
1494 /**
1495  * Purges all cached pages belonging to the object \a obj.
1496  */
1497 int cl_pages_prune(const struct lu_env *env, struct cl_object *clobj)
1498 {
1499         struct cl_thread_info   *info;
1500         struct cl_object        *obj = cl_object_top(clobj);
1501         struct cl_io            *io;
1502         int                      result;
1503
1504         ENTRY;
1505         info  = cl_env_info(env);
1506         io    = &info->clt_io;
1507
1508         /*
1509          * initialize the io. This is ugly since we never do IO in this
1510          * function, we just make cl_page_list functions happy. -jay
1511          */
1512         io->ci_obj = obj;
1513         io->ci_ignore_layout = 1;
1514         result = cl_io_init(env, io, CIT_MISC, obj);
1515         if (result != 0) {
1516                 cl_io_fini(env, io);
1517                 RETURN(io->ci_result);
1518         }
1519
1520         do {
1521                 result = cl_page_gang_lookup(env, obj, io, 0, CL_PAGE_EOF,
1522                                              page_prune_cb, NULL);
1523                 if (result == CLP_GANG_RESCHED)
1524                         cfs_cond_resched();
1525         } while (result != CLP_GANG_OKAY);
1526
1527         cl_io_fini(env, io);
1528         RETURN(result);
1529 }
1530 EXPORT_SYMBOL(cl_pages_prune);
1531
1532 /**
1533  * Tells transfer engine that only part of a page is to be transmitted.
1534  *
1535  * \see cl_page_operations::cpo_clip()
1536  */
1537 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1538                   int from, int to)
1539 {
1540         PINVRNT(env, pg, cl_page_invariant(pg));
1541
1542         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1543         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1544                        (const struct lu_env *,
1545                         const struct cl_page_slice *,int, int),
1546                        from, to);
1547 }
1548 EXPORT_SYMBOL(cl_page_clip);
1549
1550 /**
1551  * Prints human readable representation of \a pg to the \a f.
1552  */
1553 void cl_page_header_print(const struct lu_env *env, void *cookie,
1554                           lu_printer_t printer, const struct cl_page *pg)
1555 {
1556         (*printer)(env, cookie,
1557                    "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1558                    pg, cfs_atomic_read(&pg->cp_ref), pg->cp_obj,
1559                    pg->cp_index, pg->cp_parent, pg->cp_child,
1560                    pg->cp_state, pg->cp_error, pg->cp_type,
1561                    pg->cp_owner, pg->cp_req, pg->cp_flags);
1562 }
1563 EXPORT_SYMBOL(cl_page_header_print);
1564
1565 /**
1566  * Prints human readable representation of \a pg to the \a f.
1567  */
1568 void cl_page_print(const struct lu_env *env, void *cookie,
1569                    lu_printer_t printer, const struct cl_page *pg)
1570 {
1571         struct cl_page *scan;
1572
1573         for (scan = cl_page_top((struct cl_page *)pg);
1574              scan != NULL; scan = scan->cp_child)
1575                 cl_page_header_print(env, cookie, printer, scan);
1576         CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1577                        (const struct lu_env *env,
1578                         const struct cl_page_slice *slice,
1579                         void *cookie, lu_printer_t p), cookie, printer);
1580         (*printer)(env, cookie, "end page@%p\n", pg);
1581 }
1582 EXPORT_SYMBOL(cl_page_print);
1583
1584 /**
1585  * Cancel a page which is still in a transfer.
1586  */
1587 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1588 {
1589         return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1590                               (const struct lu_env *,
1591                                const struct cl_page_slice *));
1592 }
1593 EXPORT_SYMBOL(cl_page_cancel);
1594
1595 /**
1596  * Converts a byte offset within object \a obj into a page index.
1597  */
1598 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1599 {
1600         /*
1601          * XXX for now.
1602          */
1603         return (loff_t)idx << CFS_PAGE_SHIFT;
1604 }
1605 EXPORT_SYMBOL(cl_offset);
1606
1607 /**
1608  * Converts a page index into a byte offset within object \a obj.
1609  */
1610 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1611 {
1612         /*
1613          * XXX for now.
1614          */
1615         return offset >> CFS_PAGE_SHIFT;
1616 }
1617 EXPORT_SYMBOL(cl_index);
1618
1619 int cl_page_size(const struct cl_object *obj)
1620 {
1621         return 1 << CFS_PAGE_SHIFT;
1622 }
1623 EXPORT_SYMBOL(cl_page_size);
1624
1625 /**
1626  * Adds page slice to the compound page.
1627  *
1628  * This is called by cl_object_operations::coo_page_init() methods to add a
1629  * per-layer state to the page. New state is added at the end of
1630  * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1631  *
1632  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1633  */
1634 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1635                        struct cl_object *obj,
1636                        const struct cl_page_operations *ops)
1637 {
1638         ENTRY;
1639         cfs_list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1640         slice->cpl_obj  = obj;
1641         slice->cpl_ops  = ops;
1642         slice->cpl_page = page;
1643         EXIT;
1644 }
1645 EXPORT_SYMBOL(cl_page_slice_add);
1646
1647 int  cl_page_init(void)
1648 {
1649         return lu_kmem_init(cl_page_caches);
1650 }
1651
1652 void cl_page_fini(void)
1653 {
1654         lu_kmem_fini(cl_page_caches);
1655 }