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