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