Whamcloud - gitweb
branch: HEAD
[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                 }
841         }
842         EXIT;
843 }
844
845 static void cl_page_owner_set(struct cl_page *page)
846 {
847         ENTRY;
848         for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
849                 LASSERT(page->cp_owner != NULL);
850                 page->cp_owner->ci_owned_nr++;
851         }
852         EXIT;
853 }
854
855 void cl_page_disown0(const struct lu_env *env,
856                      struct cl_io *io, struct cl_page *pg)
857 {
858         enum cl_page_state state;
859
860         ENTRY;
861         state = pg->cp_state;
862         PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
863         PINVRNT(env, pg, cl_page_invariant(pg));
864         cl_page_owner_clear(pg);
865
866         if (state == CPS_OWNED)
867                 cl_page_state_set(env, pg, CPS_CACHED);
868         /*
869          * Completion call-backs are executed in the bottom-up order, so that
870          * uppermost layer (llite), responsible for VFS/VM interaction runs
871          * last and can release locks safely.
872          */
873         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
874                                (const struct lu_env *,
875                                 const struct cl_page_slice *, struct cl_io *),
876                                io);
877         EXIT;
878 }
879
880 /**
881  * returns true, iff page is owned by the given io.
882  */
883 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
884 {
885         LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
886         ENTRY;
887         RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == io);
888 }
889 EXPORT_SYMBOL(cl_page_is_owned);
890
891 /**
892  * Owns a page by IO.
893  *
894  * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
895  * into cl_page_state::CPS_OWNED state.
896  *
897  * \pre  !cl_page_is_owned(pg, io)
898  * \post result == 0 iff cl_page_is_owned(pg, io)
899  *
900  * \retval 0   success
901  *
902  * \retval -ve failure, e.g., page was destroyed (and landed in
903  *             cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
904  *
905  * \see cl_page_disown()
906  * \see cl_page_operations::cpo_own()
907  */
908 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
909 {
910         int result;
911
912         PINVRNT(env, pg, !cl_page_is_owned(pg, io));
913
914         ENTRY;
915         pg = cl_page_top(pg);
916         io = cl_io_top(io);
917
918         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_own));
919         PASSERT(env, pg, pg->cp_owner == NULL);
920         PASSERT(env, pg, pg->cp_req == NULL);
921         pg->cp_owner = io;
922         cl_page_owner_set(pg);
923         if (pg->cp_state != CPS_FREEING) {
924                 cl_page_state_set(env, pg, CPS_OWNED);
925                 result = 0;
926         } else {
927                 cl_page_disown0(env, io, pg);
928                 result = -EAGAIN;
929         }
930         PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
931         RETURN(result);
932 }
933 EXPORT_SYMBOL(cl_page_own);
934
935 /**
936  * Assume page ownership.
937  *
938  * Called when page is already locked by the hosting VM.
939  *
940  * \pre !cl_page_is_owned(pg, io)
941  * \post cl_page_is_owned(pg, io)
942  *
943  * \see cl_page_operations::cpo_assume()
944  */
945 void cl_page_assume(const struct lu_env *env,
946                     struct cl_io *io, struct cl_page *pg)
947 {
948         PASSERT(env, pg, pg->cp_state < CPS_OWNED);
949         PASSERT(env, pg, pg->cp_owner == NULL);
950         PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
951         PINVRNT(env, pg, cl_page_invariant(pg));
952
953         ENTRY;
954         pg = cl_page_top(pg);
955         io = cl_io_top(io);
956
957         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
958         pg->cp_owner = io;
959         cl_page_owner_set(pg);
960         cl_page_state_set(env, pg, CPS_OWNED);
961         EXIT;
962 }
963 EXPORT_SYMBOL(cl_page_assume);
964
965 /**
966  * Releases page ownership without unlocking the page.
967  *
968  * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
969  * underlying VM page (as VM is supposed to do this itself).
970  *
971  * \pre   cl_page_is_owned(pg, io)
972  * \post !cl_page_is_owned(pg, io)
973  *
974  * \see cl_page_assume()
975  */
976 void cl_page_unassume(const struct lu_env *env,
977                       struct cl_io *io, struct cl_page *pg)
978 {
979         PINVRNT(env, pg, cl_page_is_owned(pg, io));
980         PINVRNT(env, pg, cl_page_invariant(pg));
981
982         ENTRY;
983         pg = cl_page_top(pg);
984         io = cl_io_top(io);
985         cl_page_owner_clear(pg);
986         cl_page_state_set(env, pg, CPS_CACHED);
987         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
988                                (const struct lu_env *,
989                                 const struct cl_page_slice *, struct cl_io *),
990                                io);
991         EXIT;
992 }
993 EXPORT_SYMBOL(cl_page_unassume);
994
995 /**
996  * Releases page ownership.
997  *
998  * Moves page into cl_page_state::CPS_CACHED.
999  *
1000  * \pre   cl_page_is_owned(pg, io)
1001  * \post !cl_page_is_owned(pg, io)
1002  *
1003  * \see cl_page_own()
1004  * \see cl_page_operations::cpo_disown()
1005  */
1006 void cl_page_disown(const struct lu_env *env,
1007                     struct cl_io *io, struct cl_page *pg)
1008 {
1009         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1010
1011         ENTRY;
1012         pg = cl_page_top(pg);
1013         io = cl_io_top(io);
1014         cl_page_disown0(env, io, pg);
1015         EXIT;
1016 }
1017 EXPORT_SYMBOL(cl_page_disown);
1018
1019 /**
1020  * Called when page is to be removed from the object, e.g., as a result of
1021  * truncate.
1022  *
1023  * Calls cl_page_operations::cpo_discard() top-to-bottom.
1024  *
1025  * \pre cl_page_is_owned(pg, io)
1026  *
1027  * \see cl_page_operations::cpo_discard()
1028  */
1029 void cl_page_discard(const struct lu_env *env,
1030                      struct cl_io *io, struct cl_page *pg)
1031 {
1032         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1033         PINVRNT(env, pg, cl_page_invariant(pg));
1034
1035         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
1036 }
1037 EXPORT_SYMBOL(cl_page_discard);
1038
1039 /**
1040  * Version of cl_page_delete() that can be called for not fully constructed
1041  * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
1042  * path. Doesn't check page invariant.
1043  */
1044 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
1045                             int radix)
1046 {
1047         PASSERT(env, pg, pg == cl_page_top(pg));
1048         PASSERT(env, pg, pg->cp_state != CPS_FREEING);
1049
1050         ENTRY;
1051         /*
1052          * Severe all ways to obtain new pointers to @pg.
1053          */
1054         cl_page_owner_clear(pg);
1055         cl_page_state_set0(env, pg, CPS_FREEING);
1056         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_delete),
1057                        (const struct lu_env *, const struct cl_page_slice *));
1058         if (!radix)
1059                 /*
1060                  * !radix means that @pg is not yet in the radix tree, skip
1061                  * removing it.
1062                  */
1063                 pg = pg->cp_child;
1064         for (; pg != NULL; pg = pg->cp_child) {
1065                 void                    *value;
1066                 struct cl_object_header *hdr;
1067
1068                 hdr = cl_object_header(pg->cp_obj);
1069                 spin_lock(&hdr->coh_page_guard);
1070                 value = radix_tree_delete(&hdr->coh_tree, pg->cp_index);
1071                 PASSERT(env, pg, value == pg);
1072                 PASSERT(env, pg, hdr->coh_pages > 0);
1073                 hdr->coh_pages--;
1074                 spin_unlock(&hdr->coh_page_guard);
1075         }
1076         EXIT;
1077 }
1078
1079 /**
1080  * Called when a decision is made to throw page out of memory.
1081  *
1082  * Notifies all layers about page destruction by calling
1083  * cl_page_operations::cpo_delete() method top-to-bottom.
1084  *
1085  * Moves page into cl_page_state::CPS_FREEING state (this is the only place
1086  * where transition to this state happens).
1087  *
1088  * Eliminates all venues through which new references to the page can be
1089  * obtained:
1090  *
1091  *     - removes page from the radix trees,
1092  *
1093  *     - breaks linkage from VM page to cl_page.
1094  *
1095  * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
1096  * drain after some time, at which point page will be recycled.
1097  *
1098  * \pre  pg == cl_page_top(pg)
1099  * \pre  VM page is locked
1100  * \post pg->cp_state == CPS_FREEING
1101  *
1102  * \see cl_page_operations::cpo_delete()
1103  */
1104 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
1105 {
1106         PINVRNT(env, pg, cl_page_invariant(pg));
1107         ENTRY;
1108         cl_page_delete0(env, pg, 1);
1109         EXIT;
1110 }
1111 EXPORT_SYMBOL(cl_page_delete);
1112
1113 /**
1114  * Unmaps page from user virtual memory.
1115  *
1116  * Calls cl_page_operations::cpo_unmap() through all layers top-to-bottom. The
1117  * layer responsible for VM interaction has to unmap page from user space
1118  * virtual memory.
1119  *
1120  * \see cl_page_operations::cpo_unmap()
1121  */
1122 int cl_page_unmap(const struct lu_env *env,
1123                   struct cl_io *io, struct cl_page *pg)
1124 {
1125         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1126         PINVRNT(env, pg, cl_page_invariant(pg));
1127
1128         return cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_unmap));
1129 }
1130 EXPORT_SYMBOL(cl_page_unmap);
1131
1132 /**
1133  * Marks page up-to-date.
1134  *
1135  * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
1136  * layer responsible for VM interaction has to mark page as up-to-date. From
1137  * this moment on, page can be shown to the user space without Lustre being
1138  * notified, hence the name.
1139  *
1140  * \see cl_page_operations::cpo_export()
1141  */
1142 void cl_page_export(const struct lu_env *env, struct cl_page *pg)
1143 {
1144         PINVRNT(env, pg, cl_page_invariant(pg));
1145         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
1146                        (const struct lu_env *, const struct cl_page_slice *));
1147 }
1148 EXPORT_SYMBOL(cl_page_export);
1149
1150 /**
1151  * Returns true, iff \a pg is VM locked in a suitable sense by the calling
1152  * thread.
1153  */
1154 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
1155 {
1156         int result;
1157         const struct cl_page_slice *slice;
1158
1159         ENTRY;
1160         pg = cl_page_top_trusted((struct cl_page *)pg);
1161         slice = container_of(pg->cp_layers.next,
1162                              const struct cl_page_slice, cpl_linkage);
1163         PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
1164         /*
1165          * Call ->cpo_is_vmlocked() directly instead of going through
1166          * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
1167          * cl_page_invariant().
1168          */
1169         result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
1170         PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
1171         RETURN(result == -EBUSY);
1172 }
1173 EXPORT_SYMBOL(cl_page_is_vmlocked);
1174
1175 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
1176 {
1177         ENTRY;
1178         RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
1179 }
1180
1181 static void cl_page_io_start(const struct lu_env *env,
1182                              struct cl_page *pg, enum cl_req_type crt)
1183 {
1184         /*
1185          * Page is queued for IO, change its state.
1186          */
1187         ENTRY;
1188         cl_page_owner_clear(pg);
1189         cl_page_state_set(env, pg, cl_req_type_state(crt));
1190         EXIT;
1191 }
1192
1193 /**
1194  * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
1195  * called top-to-bottom. Every layer either agrees to submit this page (by
1196  * returning 0), or requests to omit this page (by returning -EALREADY). Layer
1197  * handling interactions with the VM also has to inform VM that page is under
1198  * transfer now.
1199  */
1200 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
1201                  struct cl_page *pg, enum cl_req_type crt)
1202 {
1203         int result;
1204
1205         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1206         PINVRNT(env, pg, cl_page_invariant(pg));
1207         PINVRNT(env, pg, crt < CRT_NR);
1208
1209         /*
1210          * XXX this has to be called bottom-to-top, so that llite can set up
1211          * PG_writeback without risking other layers deciding to skip this
1212          * page.
1213          */
1214         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
1215         if (result == 0)
1216                 cl_page_io_start(env, pg, crt);
1217
1218         KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
1219                       equi(result == 0,
1220                            PageWriteback(cl_page_vmpage(env, pg)))));
1221         CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, result);
1222         return result;
1223 }
1224 EXPORT_SYMBOL(cl_page_prep);
1225
1226 /**
1227  * Notify layers about transfer completion.
1228  *
1229  * Invoked by transfer sub-system (which is a part of osc) to notify layers
1230  * that a transfer, of which this page is a part of has completed.
1231  *
1232  * Completion call-backs are executed in the bottom-up order, so that
1233  * uppermost layer (llite), responsible for the VFS/VM interaction runs last
1234  * and can release locks safely.
1235  *
1236  * \pre  pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1237  * \post pg->cp_state == CPS_CACHED
1238  *
1239  * \see cl_page_operations::cpo_completion()
1240  */
1241 void cl_page_completion(const struct lu_env *env,
1242                         struct cl_page *pg, enum cl_req_type crt, int ioret)
1243 {
1244         PASSERT(env, pg, crt < CRT_NR);
1245         /* cl_page::cp_req already cleared by the caller (osc_completion()) */
1246         PASSERT(env, pg, pg->cp_req == NULL);
1247         PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
1248         PINVRNT(env, pg, cl_page_invariant(pg));
1249
1250         ENTRY;
1251         CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, ioret);
1252         if (crt == CRT_READ && ioret == 0) {
1253                 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
1254                 pg->cp_flags |= CPF_READ_COMPLETED;
1255         }
1256
1257         cl_page_state_set(env, pg, CPS_CACHED);
1258         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
1259                                (const struct lu_env *,
1260                                 const struct cl_page_slice *, int), ioret);
1261
1262         /* Don't assert the page writeback bit here because the lustre file
1263          * may be as a backend of swap space. in this case, the page writeback
1264          * is set by VM, and obvious we shouldn't clear it at all. Fortunately
1265          * this type of pages are all TRANSIENT pages. */
1266         KLASSERT(ergo(pg->cp_type == CPT_CACHEABLE,
1267                       !PageWriteback(cl_page_vmpage(env, pg))));
1268         EXIT;
1269 }
1270 EXPORT_SYMBOL(cl_page_completion);
1271
1272 /**
1273  * Notify layers that transfer formation engine decided to yank this page from
1274  * the cache and to make it a part of a transfer.
1275  *
1276  * \pre  pg->cp_state == CPS_CACHED
1277  * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1278  *
1279  * \see cl_page_operations::cpo_make_ready()
1280  */
1281 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1282                        enum cl_req_type crt)
1283 {
1284         int result;
1285
1286         PINVRNT(env, pg, crt < CRT_NR);
1287
1288         ENTRY;
1289         result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1290                                 (const struct lu_env *,
1291                                  const struct cl_page_slice *));
1292         if (result == 0) {
1293                 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1294                 cl_page_io_start(env, pg, crt);
1295         }
1296         CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, result);
1297         RETURN(result);
1298 }
1299 EXPORT_SYMBOL(cl_page_make_ready);
1300
1301 /**
1302  * Notify layers that high level io decided to place this page into a cache
1303  * for future transfer.
1304  *
1305  * The layer implementing transfer engine (osc) has to register this page in
1306  * its queues.
1307  *
1308  * \pre  cl_page_is_owned(pg, io)
1309  * \post ergo(result == 0,
1310  *            pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT)
1311  *
1312  * \see cl_page_operations::cpo_cache_add()
1313  */
1314 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1315                       struct cl_page *pg, enum cl_req_type crt)
1316 {
1317         int result;
1318
1319         PINVRNT(env, pg, crt < CRT_NR);
1320         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1321         PINVRNT(env, pg, cl_page_invariant(pg));
1322
1323         ENTRY;
1324         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_cache_add));
1325         if (result == 0) {
1326                 cl_page_owner_clear(pg);
1327                 cl_page_state_set(env, pg, CPS_CACHED);
1328         }
1329         CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", crt, result);
1330         RETURN(result);
1331 }
1332 EXPORT_SYMBOL(cl_page_cache_add);
1333
1334 /**
1335  * Checks whether page is protected by any extent lock is at least required
1336  * mode.
1337  *
1338  * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1339  * \see cl_page_operations::cpo_is_under_lock()
1340  */
1341 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1342                           struct cl_page *page)
1343 {
1344         int rc;
1345
1346         PINVRNT(env, page, cl_page_invariant(page));
1347
1348         ENTRY;
1349         rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1350                             (const struct lu_env *,
1351                              const struct cl_page_slice *, struct cl_io *),
1352                             io);
1353         PASSERT(env, page, rc != 0);
1354         RETURN(rc);
1355 }
1356 EXPORT_SYMBOL(cl_page_is_under_lock);
1357
1358 /**
1359  * Purges all cached pages belonging to the object \a obj.
1360  */
1361 int cl_pages_prune(const struct lu_env *env, struct cl_object *clobj)
1362 {
1363         struct cl_thread_info   *info;
1364         struct cl_object        *obj = cl_object_top(clobj);
1365         struct cl_io            *io;
1366         struct cl_page_list     *plist;
1367         int                      result;
1368
1369         ENTRY;
1370         info  = cl_env_info(env);
1371         plist = &info->clt_list;
1372         io    = &info->clt_io;
1373
1374         /*
1375          * initialize the io. This is ugly since we never do IO in this
1376          * function, we just make cl_page_list functions happy. -jay
1377          */
1378         io->ci_obj = obj;
1379         result = cl_io_init(env, io, CIT_MISC, obj);
1380         if (result != 0) {
1381                 cl_io_fini(env, io);
1382                 RETURN(io->ci_result);
1383         }
1384
1385         cl_page_list_init(plist);
1386         cl_page_gang_lookup(env, obj, io, 0, CL_PAGE_EOF, plist);
1387         /*
1388          * Since we're purging the pages of an object, we don't care
1389          * the possible outcomes of the following functions.
1390          */
1391         cl_page_list_unmap(env, io, plist);
1392         cl_page_list_discard(env, io, plist);
1393         cl_page_list_disown(env, io, plist);
1394         cl_page_list_fini(env, plist);
1395
1396         cl_io_fini(env, io);
1397         RETURN(result);
1398 }
1399 EXPORT_SYMBOL(cl_pages_prune);
1400
1401 /**
1402  * Tells transfer engine that only part of a page is to be transmitted.
1403  *
1404  * \see cl_page_operations::cpo_clip()
1405  */
1406 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1407                   int from, int to)
1408 {
1409         PINVRNT(env, pg, cl_page_invariant(pg));
1410
1411         CL_PAGE_HEADER(D_TRACE, env, pg, "%i %i\n", from, to);
1412         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1413                        (const struct lu_env *,
1414                         const struct cl_page_slice *,int, int),
1415                        from, to);
1416 }
1417 EXPORT_SYMBOL(cl_page_clip);
1418
1419 /**
1420  * Prints human readable representation of \a pg to the \a f.
1421  */
1422 void cl_page_header_print(const struct lu_env *env, void *cookie,
1423                           lu_printer_t printer, const struct cl_page *pg)
1424 {
1425         (*printer)(env, cookie,
1426                    "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1427                    pg, atomic_read(&pg->cp_ref), pg->cp_obj,
1428                    pg->cp_index, pg->cp_parent, pg->cp_child,
1429                    pg->cp_state, pg->cp_error, pg->cp_type,
1430                    pg->cp_owner, pg->cp_req, pg->cp_flags);
1431 }
1432 EXPORT_SYMBOL(cl_page_header_print);
1433
1434 /**
1435  * Prints human readable representation of \a pg to the \a f.
1436  */
1437 void cl_page_print(const struct lu_env *env, void *cookie,
1438                    lu_printer_t printer, const struct cl_page *pg)
1439 {
1440         struct cl_page *scan;
1441
1442         for (scan = cl_page_top((struct cl_page *)pg);
1443              scan != NULL; scan = scan->cp_child)
1444                 cl_page_header_print(env, cookie, printer, scan);
1445         CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1446                        (const struct lu_env *env,
1447                         const struct cl_page_slice *slice,
1448                         void *cookie, lu_printer_t p), cookie, printer);
1449         (*printer)(env, cookie, "end page@%p\n", pg);
1450 }
1451 EXPORT_SYMBOL(cl_page_print);
1452
1453 /**
1454  * Cancel a page which is still in a transfer.
1455  */
1456 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1457 {
1458         return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1459                               (const struct lu_env *,
1460                                const struct cl_page_slice *));
1461 }
1462 EXPORT_SYMBOL(cl_page_cancel);
1463
1464 /**
1465  * Converts a byte offset within object \a obj into a page index.
1466  */
1467 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1468 {
1469         /*
1470          * XXX for now.
1471          */
1472         return (loff_t)idx << CFS_PAGE_SHIFT;
1473 }
1474 EXPORT_SYMBOL(cl_offset);
1475
1476 /**
1477  * Converts a page index into a byte offset within object \a obj.
1478  */
1479 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1480 {
1481         /*
1482          * XXX for now.
1483          */
1484         return offset >> CFS_PAGE_SHIFT;
1485 }
1486 EXPORT_SYMBOL(cl_index);
1487
1488 int cl_page_size(const struct cl_object *obj)
1489 {
1490         return 1 << CFS_PAGE_SHIFT;
1491 }
1492 EXPORT_SYMBOL(cl_page_size);
1493
1494 /**
1495  * Adds page slice to the compound page.
1496  *
1497  * This is called by cl_object_operations::coo_page_init() methods to add a
1498  * per-layer state to the page. New state is added at the end of
1499  * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1500  *
1501  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1502  */
1503 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1504                        struct cl_object *obj,
1505                        const struct cl_page_operations *ops)
1506 {
1507         ENTRY;
1508         list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1509         slice->cpl_obj  = obj;
1510         slice->cpl_ops  = ops;
1511         slice->cpl_page = page;
1512         EXIT;
1513 }
1514 EXPORT_SYMBOL(cl_page_slice_add);
1515
1516 int  cl_page_init(void)
1517 {
1518         return lu_kmem_init(cl_page_caches);
1519 }
1520
1521 void cl_page_fini(void)
1522 {
1523         lu_kmem_fini(cl_page_caches);
1524 }