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