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