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