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