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