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