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