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