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