Whamcloud - gitweb
b60943f25afbbd943ee6ae9909dd2affd10a4a30
[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 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45
46 #include <libcfs/libcfs.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <libcfs/list.h>
50
51 #include <cl_object.h>
52 #include "cl_internal.h"
53
54 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg,
55                             int radix);
56
57 static cfs_mem_cache_t      *cl_page_kmem = NULL;
58
59 static struct lu_kmem_descr cl_page_caches[] = {
60         {
61                 .ckd_cache = &cl_page_kmem,
62                 .ckd_name  = "cl_page_kmem",
63                 .ckd_size  = sizeof (struct cl_page)
64         },
65         {
66                 .ckd_cache = NULL
67         }
68 };
69
70 #ifdef LIBCFS_DEBUG
71 # define PASSERT(env, page, expr)                                       \
72   do {                                                                    \
73           if (unlikely(!(expr))) {                                      \
74                   CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n");    \
75                   LASSERT(0);                                           \
76           }                                                             \
77   } while (0)
78 #else /* !LIBCFS_DEBUG */
79 # define PASSERT(env, page, exp) \
80         ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
81 #endif /* !LIBCFS_DEBUG */
82
83 #ifdef INVARIANT_CHECK
84 # define PINVRNT(env, page, expr)                                       \
85   do {                                                                    \
86           if (unlikely(!(expr))) {                                      \
87                   CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n");    \
88                   LINVRNT(0);                                           \
89           }                                                             \
90   } while (0)
91 #else /* !INVARIANT_CHECK */
92 # define PINVRNT(env, page, exp) \
93         ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
94 #endif /* !INVARIANT_CHECK */
95
96 /**
97  * Internal version of cl_page_top, it should be called with page referenced,
98  * or coh_page_guard held.
99  */
100 static struct cl_page *cl_page_top_trusted(struct cl_page *page)
101 {
102         while (page->cp_parent != NULL)
103                 page = page->cp_parent;
104         return page;
105 }
106
107 /**
108  * Internal version of cl_page_get().
109  *
110  * This function can be used to obtain initial reference to previously
111  * unreferenced cached object. It can be called only if concurrent page
112  * reclamation is somehow prevented, e.g., by locking page radix-tree
113  * (cl_object_header::hdr->coh_page_guard), or by keeping a lock on a VM page,
114  * associated with \a page.
115  *
116  * Use with care! Not exported.
117  */
118 static void cl_page_get_trust(struct cl_page *page)
119 {
120         /*
121          * Checkless version for trusted users.
122          */
123         if (cfs_atomic_inc_return(&page->cp_ref) == 1)
124                 cfs_atomic_inc(&cl_object_site(page->cp_obj)->cs_pages.cs_busy);
125 }
126
127 /**
128  * Returns a slice within a page, corresponding to the given layer in the
129  * device stack.
130  *
131  * \see cl_lock_at()
132  */
133 static const struct cl_page_slice *
134 cl_page_at_trusted(const struct cl_page *page,
135                    const struct lu_device_type *dtype)
136 {
137         const struct cl_page_slice *slice;
138
139 #ifdef INVARIANT_CHECK
140         struct cl_object_header *ch = cl_object_header(page->cp_obj);
141
142         if (!cfs_atomic_read(&page->cp_ref))
143                 LASSERT_SPIN_LOCKED(&ch->coh_page_guard);
144 #endif
145         ENTRY;
146
147         page = cl_page_top_trusted((struct cl_page *)page);
148         do {
149                 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
150                         if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
151                                 RETURN(slice);
152                 }
153                 page = page->cp_child;
154         } while (page != NULL);
155         RETURN(NULL);
156 }
157
158 /**
159  * Returns a page with given index in the given object, or NULL if no page is
160  * found. Acquires a reference on \a page.
161  *
162  * Locking: called under cl_object_header::coh_page_guard spin-lock.
163  */
164 struct cl_page *cl_page_lookup(struct cl_object_header *hdr, pgoff_t index)
165 {
166         struct cl_page *page;
167
168         LASSERT_SPIN_LOCKED(&hdr->coh_page_guard);
169
170         page = radix_tree_lookup(&hdr->coh_tree, index);
171         if (page != NULL) {
172                 cl_page_get_trust(page);
173         }
174         return page;
175 }
176 EXPORT_SYMBOL(cl_page_lookup);
177
178 /**
179  * Returns a list of pages by a given [start, end] of \a obj.
180  *
181  * \param resched If not NULL, then we give up before hogging CPU for too
182  * long and set *resched = 1, in that case caller should implement a retry
183  * logic.
184  *
185  * Gang tree lookup (radix_tree_gang_lookup()) optimization is absolutely
186  * crucial in the face of [offset, EOF] locks.
187  *
188  * Return at least one page in @queue unless there is no covered page.
189  */
190 int cl_page_gang_lookup(const struct lu_env *env, struct cl_object *obj,
191                         struct cl_io *io, pgoff_t start, pgoff_t end,
192                         cl_page_gang_cb_t cb, void *cbdata)
193 {
194         struct cl_object_header *hdr;
195         struct cl_page          *page;
196         struct cl_page         **pvec;
197         const struct cl_page_slice  *slice;
198         const struct lu_device_type *dtype;
199         pgoff_t                  idx;
200         unsigned int             nr;
201         unsigned int             i;
202         unsigned int             j;
203         int                      res = CLP_GANG_OKAY;
204         int                      tree_lock = 1;
205         ENTRY;
206
207         idx = start;
208         hdr = cl_object_header(obj);
209         pvec = cl_env_info(env)->clt_pvec;
210         dtype = cl_object_top(obj)->co_lu.lo_dev->ld_type;
211         cfs_spin_lock(&hdr->coh_page_guard);
212         while ((nr = radix_tree_gang_lookup(&hdr->coh_tree, (void **)pvec,
213                                             idx, CLT_PVEC_SIZE)) > 0) {
214                 int end_of_region = 0;
215                 idx = pvec[nr - 1]->cp_index + 1;
216                 for (i = 0, j = 0; i < nr; ++i) {
217                         page = pvec[i];
218                         pvec[i] = NULL;
219
220                         LASSERT(page->cp_type == CPT_CACHEABLE);
221                         if (page->cp_index > end) {
222                                 end_of_region = 1;
223                                 break;
224                         }
225                         if (page->cp_state == CPS_FREEING)
226                                 continue;
227
228                         slice = cl_page_at_trusted(page, dtype);
229                         /*
230                          * Pages for lsm-less file has no underneath sub-page
231                          * for osc, in case of ...
232                          */
233                         PASSERT(env, page, slice != NULL);
234
235                         page = slice->cpl_page;
236                         /*
237                          * Can safely call cl_page_get_trust() under
238                          * radix-tree spin-lock.
239                          *
240                          * XXX not true, because @page is from object another
241                          * than @hdr and protected by different tree lock.
242                          */
243                         cl_page_get_trust(page);
244                         lu_ref_add_atomic(&page->cp_reference,
245                                           "gang_lookup", cfs_current());
246                         pvec[j++] = page;
247                 }
248
249                 /*
250                  * Here a delicate locking dance is performed. Current thread
251                  * holds a reference to a page, but has to own it before it
252                  * can be placed into queue. Owning implies waiting, so
253                  * radix-tree lock is to be released. After a wait one has to
254                  * check that pages weren't truncated (cl_page_own() returns
255                  * error in the latter case).
256                  */
257                 cfs_spin_unlock(&hdr->coh_page_guard);
258                 tree_lock = 0;
259
260                 for (i = 0; i < j; ++i) {
261                         page = pvec[i];
262                         if (res == CLP_GANG_OKAY)
263                                 res = (*cb)(env, io, page, cbdata);
264                         lu_ref_del(&page->cp_reference,
265                                    "gang_lookup", cfs_current());
266                         cl_page_put(env, page);
267                 }
268                 if (nr < CLT_PVEC_SIZE || end_of_region)
269                         break;
270
271                 if (res == CLP_GANG_OKAY && cfs_need_resched())
272                         res = CLP_GANG_RESCHED;
273                 if (res != CLP_GANG_OKAY)
274                         break;
275
276                 cfs_spin_lock(&hdr->coh_page_guard);
277                 tree_lock = 1;
278         }
279         if (tree_lock)
280                 cfs_spin_unlock(&hdr->coh_page_guard);
281         RETURN(res);
282 }
283 EXPORT_SYMBOL(cl_page_gang_lookup);
284
285 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
286 {
287         struct cl_object *obj  = page->cp_obj;
288         struct cl_site   *site = cl_object_site(obj);
289
290         PASSERT(env, page, cfs_list_empty(&page->cp_batch));
291         PASSERT(env, page, page->cp_owner == NULL);
292         PASSERT(env, page, page->cp_req == NULL);
293         PASSERT(env, page, page->cp_parent == NULL);
294         PASSERT(env, page, page->cp_state == CPS_FREEING);
295
296         ENTRY;
297         cfs_might_sleep();
298         while (!cfs_list_empty(&page->cp_layers)) {
299                 struct cl_page_slice *slice;
300
301                 slice = cfs_list_entry(page->cp_layers.next,
302                                        struct cl_page_slice, cpl_linkage);
303                 cfs_list_del_init(page->cp_layers.next);
304                 slice->cpl_ops->cpo_fini(env, slice);
305         }
306         cfs_atomic_dec(&site->cs_pages.cs_total);
307
308 #ifdef LUSTRE_PAGESTATE_TRACKING
309         cfs_atomic_dec(&site->cs_pages_state[page->cp_state]);
310 #endif
311         lu_object_ref_del_at(&obj->co_lu, page->cp_obj_ref, "cl_page", page);
312         cl_object_put(env, obj);
313         lu_ref_fini(&page->cp_reference);
314         OBD_SLAB_FREE_PTR(page, cl_page_kmem);
315         EXIT;
316 }
317
318 /**
319  * Helper function updating page state. This is the only place in the code
320  * where cl_page::cp_state field is mutated.
321  */
322 static inline void cl_page_state_set_trust(struct cl_page *page,
323                                            enum cl_page_state state)
324 {
325         /* bypass const. */
326         *(enum cl_page_state *)&page->cp_state = state;
327 }
328
329 static int cl_page_alloc(const struct lu_env *env, struct cl_object *o,
330                          pgoff_t ind, struct page *vmpage,
331                          enum cl_page_type type, struct cl_page **out)
332 {
333         struct cl_page          *page;
334         struct cl_page          *err  = NULL;
335         struct lu_object_header *head;
336         struct cl_site          *site = cl_object_site(o);
337         int                      result;
338
339         ENTRY;
340         result = +1;
341         OBD_SLAB_ALLOC_PTR_GFP(page, cl_page_kmem, CFS_ALLOC_IO);
342         if (page != NULL) {
343                 cfs_atomic_set(&page->cp_ref, 1);
344                 page->cp_obj = o;
345                 cl_object_get(o);
346                 page->cp_obj_ref = lu_object_ref_add(&o->co_lu,
347                                                      "cl_page", page);
348                 page->cp_index = ind;
349                 cl_page_state_set_trust(page, CPS_CACHED);
350                 page->cp_type = type;
351                 CFS_INIT_LIST_HEAD(&page->cp_layers);
352                 CFS_INIT_LIST_HEAD(&page->cp_batch);
353                 CFS_INIT_LIST_HEAD(&page->cp_flight);
354                 cfs_mutex_init(&page->cp_mutex);
355                 lu_ref_init(&page->cp_reference);
356                 head = o->co_lu.lo_header;
357                 cfs_list_for_each_entry(o, &head->loh_layers,
358                                         co_lu.lo_linkage) {
359                         if (o->co_ops->coo_page_init != NULL) {
360                                 err = o->co_ops->coo_page_init(env, o,
361                                                                page, vmpage);
362                                 if (err != NULL) {
363                                         cl_page_delete0(env, page, 0);
364                                         cl_page_free(env, page);
365                                         page = err;
366                                         break;
367                                 }
368                         }
369                 }
370                 if (err == NULL) {
371                         cfs_atomic_inc(&site->cs_pages.cs_busy);
372                         cfs_atomic_inc(&site->cs_pages.cs_total);
373
374 #ifdef LUSTRE_PAGESTATE_TRACKING
375                         cfs_atomic_inc(&site->cs_pages_state[CPS_CACHED]);
376 #endif
377                         cfs_atomic_inc(&site->cs_pages.cs_created);
378                         result = 0;
379                 }
380         } else
381                 page = ERR_PTR(-ENOMEM);
382         *out = page;
383         RETURN(result);
384 }
385
386 /**
387  * Returns a cl_page with index \a idx at the object \a o, and associated with
388  * the VM page \a vmpage.
389  *
390  * This is the main entry point into the cl_page caching interface. First, a
391  * cache (implemented as a per-object radix tree) is consulted. If page is
392  * found there, it is returned immediately. Otherwise new page is allocated
393  * and returned. In any case, additional reference to page is acquired.
394  *
395  * \see cl_object_find(), cl_lock_find()
396  */
397 static struct cl_page *cl_page_find0(const struct lu_env *env,
398                                      struct cl_object *o,
399                                      pgoff_t idx, struct page *vmpage,
400                                      enum cl_page_type type,
401                                      struct cl_page *parent)
402 {
403         struct cl_page          *page = NULL;
404         struct cl_page          *ghost = NULL;
405         struct cl_object_header *hdr;
406         struct cl_site          *site = cl_object_site(o);
407         int err;
408
409         LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
410         cfs_might_sleep();
411
412         ENTRY;
413
414         hdr = cl_object_header(o);
415         cfs_atomic_inc(&site->cs_pages.cs_lookup);
416
417         CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
418                idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
419         /* fast path. */
420         if (type == CPT_CACHEABLE) {
421                 /*
422                  * cl_vmpage_page() can be called here without any locks as
423                  *
424                  *     - "vmpage" is locked (which prevents ->private from
425                  *       concurrent updates), and
426                  *
427                  *     - "o" cannot be destroyed while current thread holds a
428                  *       reference on it.
429                  */
430                 page = cl_vmpage_page(vmpage, o);
431                 PINVRNT(env, page,
432                         ergo(page != NULL,
433                              cl_page_vmpage(env, page) == vmpage &&
434                              (void *)radix_tree_lookup(&hdr->coh_tree,
435                                                        idx) == page));
436         }
437
438         if (page != NULL) {
439                 cfs_atomic_inc(&site->cs_pages.cs_hit);
440                 RETURN(page);
441         }
442
443         /* allocate and initialize cl_page */
444         err = cl_page_alloc(env, o, idx, vmpage, type, &page);
445         if (err != 0)
446                 RETURN(page);
447
448         if (type == CPT_TRANSIENT) {
449                 if (parent) {
450                         LASSERT(page->cp_parent == NULL);
451                         page->cp_parent = parent;
452                         parent->cp_child = page;
453                 }
454                 RETURN(page);
455         }
456
457         /*
458          * XXX optimization: use radix_tree_preload() here, and change tree
459          * gfp mask to GFP_KERNEL in cl_object_header_init().
460          */
461         cfs_spin_lock(&hdr->coh_page_guard);
462         err = radix_tree_insert(&hdr->coh_tree, idx, page);
463         if (err != 0) {
464                 ghost = page;
465                 /*
466                  * Noted by Jay: a lock on \a vmpage protects cl_page_find()
467                  * from this race, but
468                  *
469                  *     0. it's better to have cl_page interface "locally
470                  *     consistent" so that its correctness can be reasoned
471                  *     about without appealing to the (obscure world of) VM
472                  *     locking.
473                  *
474                  *     1. handling this race allows ->coh_tree to remain
475                  *     consistent even when VM locking is somehow busted,
476                  *     which is very useful during diagnosing and debugging.
477                  */
478                 page = ERR_PTR(err);
479                 CL_PAGE_DEBUG(D_ERROR, env, ghost,
480                               "fail to insert into radix tree: %d\n", err);
481         } else {
482                 if (parent) {
483                         LASSERT(page->cp_parent == NULL);
484                         page->cp_parent = parent;
485                         parent->cp_child = page;
486                 }
487                 hdr->coh_pages++;
488         }
489         cfs_spin_unlock(&hdr->coh_page_guard);
490
491         if (unlikely(ghost != NULL)) {
492                 cfs_atomic_dec(&site->cs_pages.cs_busy);
493                 cl_page_delete0(env, ghost, 0);
494                 cl_page_free(env, ghost);
495         }
496         RETURN(page);
497 }
498
499 struct cl_page *cl_page_find(const struct lu_env *env, struct cl_object *o,
500                              pgoff_t idx, struct page *vmpage,
501                              enum cl_page_type type)
502 {
503         return cl_page_find0(env, o, idx, vmpage, type, NULL);
504 }
505 EXPORT_SYMBOL(cl_page_find);
506
507
508 struct cl_page *cl_page_find_sub(const struct lu_env *env, struct cl_object *o,
509                                  pgoff_t idx, struct page *vmpage,
510                                  struct cl_page *parent)
511 {
512         return cl_page_find0(env, o, idx, vmpage, parent->cp_type, parent);
513 }
514 EXPORT_SYMBOL(cl_page_find_sub);
515
516 static inline int cl_page_invariant(const struct cl_page *pg)
517 {
518         struct cl_object_header *header;
519         struct cl_page          *parent;
520         struct cl_page          *child;
521         struct cl_io            *owner;
522
523         /*
524          * Page invariant is protected by a VM lock.
525          */
526         LINVRNT(cl_page_is_vmlocked(NULL, pg));
527
528         header = cl_object_header(pg->cp_obj);
529         parent = pg->cp_parent;
530         child  = pg->cp_child;
531         owner  = pg->cp_owner;
532
533         return cfs_atomic_read(&pg->cp_ref) > 0 &&
534                 ergo(parent != NULL, parent->cp_child == pg) &&
535                 ergo(child != NULL, child->cp_parent == pg) &&
536                 ergo(child != NULL, pg->cp_obj != child->cp_obj) &&
537                 ergo(parent != NULL, pg->cp_obj != parent->cp_obj) &&
538                 ergo(owner != NULL && parent != NULL,
539                      parent->cp_owner == pg->cp_owner->ci_parent) &&
540                 ergo(owner != NULL && child != NULL,
541                      child->cp_owner->ci_parent == owner) &&
542                 /*
543                  * Either page is early in initialization (has neither child
544                  * nor parent yet), or it is in the object radix tree.
545                  */
546                 ergo(pg->cp_state < CPS_FREEING && pg->cp_type == CPT_CACHEABLE,
547                      (void *)radix_tree_lookup(&header->coh_tree,
548                                                pg->cp_index) == pg ||
549                      (child == NULL && parent == NULL));
550 }
551
552 static void cl_page_state_set0(const struct lu_env *env,
553                                struct cl_page *page, enum cl_page_state state)
554 {
555         enum cl_page_state old;
556 #ifdef LUSTRE_PAGESTATE_TRACKING
557         struct cl_site *site = cl_object_site(page->cp_obj);
558 #endif
559
560         /*
561          * Matrix of allowed state transitions [old][new], for sanity
562          * checking.
563          */
564         static const int allowed_transitions[CPS_NR][CPS_NR] = {
565                 [CPS_CACHED] = {
566                         [CPS_CACHED]  = 0,
567                         [CPS_OWNED]   = 1, /* io finds existing cached page */
568                         [CPS_PAGEIN]  = 0,
569                         [CPS_PAGEOUT] = 1, /* write-out from the cache */
570                         [CPS_FREEING] = 1, /* eviction on the memory pressure */
571                 },
572                 [CPS_OWNED] = {
573                         [CPS_CACHED]  = 1, /* release to the cache */
574                         [CPS_OWNED]   = 0,
575                         [CPS_PAGEIN]  = 1, /* start read immediately */
576                         [CPS_PAGEOUT] = 1, /* start write immediately */
577                         [CPS_FREEING] = 1, /* lock invalidation or truncate */
578                 },
579                 [CPS_PAGEIN] = {
580                         [CPS_CACHED]  = 1, /* io completion */
581                         [CPS_OWNED]   = 0,
582                         [CPS_PAGEIN]  = 0,
583                         [CPS_PAGEOUT] = 0,
584                         [CPS_FREEING] = 0,
585                 },
586                 [CPS_PAGEOUT] = {
587                         [CPS_CACHED]  = 1, /* io completion */
588                         [CPS_OWNED]   = 0,
589                         [CPS_PAGEIN]  = 0,
590                         [CPS_PAGEOUT] = 0,
591                         [CPS_FREEING] = 0,
592                 },
593                 [CPS_FREEING] = {
594                         [CPS_CACHED]  = 0,
595                         [CPS_OWNED]   = 0,
596                         [CPS_PAGEIN]  = 0,
597                         [CPS_PAGEOUT] = 0,
598                         [CPS_FREEING] = 0,
599                 }
600         };
601
602         ENTRY;
603         old = page->cp_state;
604         PASSERT(env, page, allowed_transitions[old][state]);
605         CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
606         for (; page != NULL; page = page->cp_child) {
607                 PASSERT(env, page, page->cp_state == old);
608                 PASSERT(env, page,
609                         equi(state == CPS_OWNED, page->cp_owner != NULL));
610
611 #ifdef LUSTRE_PAGESTATE_TRACKING
612                 cfs_atomic_dec(&site->cs_pages_state[page->cp_state]);
613                 cfs_atomic_inc(&site->cs_pages_state[state]);
614 #endif
615                 cl_page_state_set_trust(page, state);
616         }
617         EXIT;
618 }
619
620 static void cl_page_state_set(const struct lu_env *env,
621                               struct cl_page *page, enum cl_page_state state)
622 {
623         cl_page_state_set0(env, page, state);
624 }
625
626 /**
627  * Acquires an additional reference to a page.
628  *
629  * This can be called only by caller already possessing a reference to \a
630  * page.
631  *
632  * \see cl_object_get(), cl_lock_get().
633  */
634 void cl_page_get(struct cl_page *page)
635 {
636         ENTRY;
637         LASSERT(page->cp_state != CPS_FREEING);
638         cl_page_get_trust(page);
639         EXIT;
640 }
641 EXPORT_SYMBOL(cl_page_get);
642
643 /**
644  * Releases a reference to a page.
645  *
646  * When last reference is released, page is returned to the cache, unless it
647  * is in cl_page_state::CPS_FREEING state, in which case it is immediately
648  * destroyed.
649  *
650  * \see cl_object_put(), cl_lock_put().
651  */
652 void cl_page_put(const struct lu_env *env, struct cl_page *page)
653 {
654         struct cl_object_header *hdr;
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         hdr = cl_object_header(cl_object_top(page->cp_obj));
664         if (cfs_atomic_dec_and_lock(&page->cp_ref, &hdr->coh_page_guard)) {
665                 cfs_atomic_dec(&site->cs_pages.cs_busy);
666                 /* We're going to access the page w/o a reference, but it's
667                  * ok because we have grabbed the lock coh_page_guard, which
668                  * means nobody is able to free this page behind us.
669                  */
670                 if (page->cp_state == CPS_FREEING) {
671                         /* We drop the page reference and check the page state
672                          * inside the coh_page_guard. So that if it gets here,
673                          * it is the REALLY last reference to this page.
674                          */
675                         cfs_spin_unlock(&hdr->coh_page_guard);
676
677                         LASSERT(cfs_atomic_read(&page->cp_ref) == 0);
678                         PASSERT(env, page, page->cp_owner == NULL);
679                         PASSERT(env, page, cfs_list_empty(&page->cp_batch));
680                         /*
681                          * Page is no longer reachable by other threads. Tear
682                          * it down.
683                          */
684                         cl_page_free(env, page);
685
686                         EXIT;
687                         return;
688                 }
689                 cfs_spin_unlock(&hdr->coh_page_guard);
690         }
691
692         EXIT;
693 }
694 EXPORT_SYMBOL(cl_page_put);
695
696 /**
697  * Returns a VM page associated with a given cl_page.
698  */
699 cfs_page_t *cl_page_vmpage(const struct lu_env *env, struct cl_page *page)
700 {
701         const struct cl_page_slice *slice;
702
703         /*
704          * Find uppermost layer with ->cpo_vmpage() method, and return its
705          * result.
706          */
707         page = cl_page_top(page);
708         do {
709                 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
710                         if (slice->cpl_ops->cpo_vmpage != NULL)
711                                 RETURN(slice->cpl_ops->cpo_vmpage(env, slice));
712                 }
713                 page = page->cp_child;
714         } while (page != NULL);
715         LBUG(); /* ->cpo_vmpage() has to be defined somewhere in the stack */
716 }
717 EXPORT_SYMBOL(cl_page_vmpage);
718
719 /**
720  * Returns a cl_page associated with a VM page, and given cl_object.
721  */
722 struct cl_page *cl_vmpage_page(cfs_page_t *vmpage, struct cl_object *obj)
723 {
724         struct cl_page *page;
725         struct cl_object_header *hdr;
726
727         ENTRY;
728         KLASSERT(PageLocked(vmpage));
729
730         /*
731          * NOTE: absence of races and liveness of data are guaranteed by page
732          *       lock on a "vmpage". That works because object destruction has
733          *       bottom-to-top pass.
734          */
735
736         /*
737          * This loop assumes that ->private points to the top-most page. This
738          * can be rectified easily.
739          */
740         hdr = cl_object_header(cl_object_top(obj));
741         cfs_spin_lock(&hdr->coh_page_guard);
742         for (page = (void *)vmpage->private;
743              page != NULL; page = page->cp_child) {
744                 if (cl_object_same(page->cp_obj, obj)) {
745                         cl_page_get_trust(page);
746                         break;
747                 }
748         }
749         cfs_spin_unlock(&hdr->coh_page_guard);
750         LASSERT(ergo(page, page->cp_type == CPT_CACHEABLE));
751         RETURN(page);
752 }
753 EXPORT_SYMBOL(cl_vmpage_page);
754
755 /**
756  * Returns the top-page for a given page.
757  *
758  * \see cl_object_top(), cl_io_top()
759  */
760 struct cl_page *cl_page_top(struct cl_page *page)
761 {
762         return cl_page_top_trusted(page);
763 }
764 EXPORT_SYMBOL(cl_page_top);
765
766 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
767                                        const struct lu_device_type *dtype)
768 {
769         return cl_page_at_trusted(page, dtype);
770 }
771 EXPORT_SYMBOL(cl_page_at);
772
773 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
774
775 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...)                   \
776 ({                                                                      \
777         const struct lu_env        *__env  = (_env);                    \
778         struct cl_page             *__page = (_page);                   \
779         const struct cl_page_slice *__scan;                             \
780         int                         __result;                           \
781         ptrdiff_t                   __op   = (_op);                     \
782         int                       (*__method)_proto;                    \
783                                                                         \
784         __result = 0;                                                   \
785         __page = cl_page_top(__page);                                   \
786         do {                                                            \
787                 cfs_list_for_each_entry(__scan, &__page->cp_layers,     \
788                                         cpl_linkage) {                  \
789                         __method = *(void **)((char *)__scan->cpl_ops + \
790                                               __op);                    \
791                         if (__method != NULL) {                         \
792                                 __result = (*__method)(__env, __scan,   \
793                                                        ## __VA_ARGS__); \
794                                 if (__result != 0)                      \
795                                         break;                          \
796                         }                                               \
797                 }                                                       \
798                 __page = __page->cp_child;                              \
799         } while (__page != NULL && __result == 0);                      \
800         if (__result > 0)                                               \
801                 __result = 0;                                           \
802         __result;                                                       \
803 })
804
805 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...)                   \
806 do {                                                                    \
807         const struct lu_env        *__env  = (_env);                    \
808         struct cl_page             *__page = (_page);                   \
809         const struct cl_page_slice *__scan;                             \
810         ptrdiff_t                   __op   = (_op);                     \
811         void                      (*__method)_proto;                    \
812                                                                         \
813         __page = cl_page_top(__page);                                   \
814         do {                                                            \
815                 cfs_list_for_each_entry(__scan, &__page->cp_layers,     \
816                                         cpl_linkage) {                  \
817                         __method = *(void **)((char *)__scan->cpl_ops + \
818                                               __op);                    \
819                         if (__method != NULL)                           \
820                                 (*__method)(__env, __scan,              \
821                                             ## __VA_ARGS__);            \
822                 }                                                       \
823                 __page = __page->cp_child;                              \
824         } while (__page != NULL);                                       \
825 } while (0)
826
827 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...)               \
828 do {                                                                        \
829         const struct lu_env        *__env  = (_env);                        \
830         struct cl_page             *__page = (_page);                       \
831         const struct cl_page_slice *__scan;                                 \
832         ptrdiff_t                   __op   = (_op);                         \
833         void                      (*__method)_proto;                        \
834                                                                             \
835         /* get to the bottom page. */                                       \
836         while (__page->cp_child != NULL)                                    \
837                 __page = __page->cp_child;                                  \
838         do {                                                                \
839                 cfs_list_for_each_entry_reverse(__scan, &__page->cp_layers, \
840                                                 cpl_linkage) {              \
841                         __method = *(void **)((char *)__scan->cpl_ops +     \
842                                               __op);                        \
843                         if (__method != NULL)                               \
844                                 (*__method)(__env, __scan,                  \
845                                             ## __VA_ARGS__);                \
846                 }                                                           \
847                 __page = __page->cp_parent;                                 \
848         } while (__page != NULL);                                           \
849 } while (0)
850
851 static int cl_page_invoke(const struct lu_env *env,
852                           struct cl_io *io, struct cl_page *page, ptrdiff_t op)
853
854 {
855         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
856         ENTRY;
857         RETURN(CL_PAGE_INVOKE(env, page, op,
858                               (const struct lu_env *,
859                                const struct cl_page_slice *, struct cl_io *),
860                               io));
861 }
862
863 static void cl_page_invoid(const struct lu_env *env,
864                            struct cl_io *io, struct cl_page *page, ptrdiff_t op)
865
866 {
867         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
868         ENTRY;
869         CL_PAGE_INVOID(env, page, op,
870                        (const struct lu_env *,
871                         const struct cl_page_slice *, struct cl_io *), io);
872         EXIT;
873 }
874
875 static void cl_page_owner_clear(struct cl_page *page)
876 {
877         ENTRY;
878         for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
879                 if (page->cp_owner != NULL) {
880                         LASSERT(page->cp_owner->ci_owned_nr > 0);
881                         page->cp_owner->ci_owned_nr--;
882                         page->cp_owner = NULL;
883                         page->cp_task = NULL;
884                 }
885         }
886         EXIT;
887 }
888
889 static void cl_page_owner_set(struct cl_page *page)
890 {
891         ENTRY;
892         for (page = cl_page_top(page); page != NULL; page = page->cp_child) {
893                 LASSERT(page->cp_owner != NULL);
894                 page->cp_owner->ci_owned_nr++;
895         }
896         EXIT;
897 }
898
899 void cl_page_disown0(const struct lu_env *env,
900                      struct cl_io *io, struct cl_page *pg)
901 {
902         enum cl_page_state state;
903
904         ENTRY;
905         state = pg->cp_state;
906         PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
907         PINVRNT(env, pg, cl_page_invariant(pg));
908         cl_page_owner_clear(pg);
909
910         if (state == CPS_OWNED)
911                 cl_page_state_set(env, pg, CPS_CACHED);
912         /*
913          * Completion call-backs are executed in the bottom-up order, so that
914          * uppermost layer (llite), responsible for VFS/VM interaction runs
915          * last and can release locks safely.
916          */
917         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
918                                (const struct lu_env *,
919                                 const struct cl_page_slice *, struct cl_io *),
920                                io);
921         EXIT;
922 }
923
924 /**
925  * returns true, iff page is owned by the given io.
926  */
927 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
928 {
929         LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
930         ENTRY;
931         RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == io);
932 }
933 EXPORT_SYMBOL(cl_page_is_owned);
934
935 /**
936  * Try to own a page by IO.
937  *
938  * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
939  * into cl_page_state::CPS_OWNED state.
940  *
941  * \pre  !cl_page_is_owned(pg, io)
942  * \post result == 0 iff cl_page_is_owned(pg, io)
943  *
944  * \retval 0   success
945  *
946  * \retval -ve failure, e.g., page was destroyed (and landed in
947  *             cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
948  *             or, page was owned by another thread, or in IO.
949  *
950  * \see cl_page_disown()
951  * \see cl_page_operations::cpo_own()
952  * \see cl_page_own_try()
953  * \see cl_page_own
954  */
955 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
956                         struct cl_page *pg, int nonblock)
957 {
958         int result;
959
960         PINVRNT(env, pg, !cl_page_is_owned(pg, io));
961
962         ENTRY;
963         pg = cl_page_top(pg);
964         io = cl_io_top(io);
965
966         if (pg->cp_state == CPS_FREEING) {
967                 result = -ENOENT;
968         } else {
969                 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
970                                         (const struct lu_env *,
971                                          const struct cl_page_slice *,
972                                          struct cl_io *, int),
973                                         io, nonblock);
974                 if (result == 0) {
975                         PASSERT(env, pg, pg->cp_owner == NULL);
976                         PASSERT(env, pg, pg->cp_req == NULL);
977                         pg->cp_owner = io;
978                         pg->cp_task  = current;
979                         cl_page_owner_set(pg);
980                         if (pg->cp_state != CPS_FREEING) {
981                                 cl_page_state_set(env, pg, CPS_OWNED);
982                         } else {
983                                 cl_page_disown0(env, io, pg);
984                                 result = -ENOENT;
985                         }
986                 }
987         }
988         PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
989         RETURN(result);
990 }
991
992 /**
993  * Own a page, might be blocked.
994  *
995  * \see cl_page_own0()
996  */
997 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
998 {
999         return cl_page_own0(env, io, pg, 0);
1000 }
1001 EXPORT_SYMBOL(cl_page_own);
1002
1003 /**
1004  * Nonblock version of cl_page_own().
1005  *
1006  * \see cl_page_own0()
1007  */
1008 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
1009                     struct cl_page *pg)
1010 {
1011         return cl_page_own0(env, io, pg, 1);
1012 }
1013 EXPORT_SYMBOL(cl_page_own_try);
1014
1015
1016 /**
1017  * Assume page ownership.
1018  *
1019  * Called when page is already locked by the hosting VM.
1020  *
1021  * \pre !cl_page_is_owned(pg, io)
1022  * \post cl_page_is_owned(pg, io)
1023  *
1024  * \see cl_page_operations::cpo_assume()
1025  */
1026 void cl_page_assume(const struct lu_env *env,
1027                     struct cl_io *io, struct cl_page *pg)
1028 {
1029         PASSERT(env, pg, pg->cp_owner == NULL);
1030         PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
1031         PINVRNT(env, pg, cl_page_invariant(pg));
1032
1033         ENTRY;
1034         pg = cl_page_top(pg);
1035         io = cl_io_top(io);
1036
1037         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
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
1366         /* Don't assert the page writeback bit here because the lustre file
1367          * may be as a backend of swap space. in this case, the page writeback
1368          * is set by VM, and obvious we shouldn't clear it at all. Fortunately
1369          * this type of pages are all TRANSIENT pages. */
1370         KLASSERT(ergo(pg->cp_type == CPT_CACHEABLE,
1371                       !PageWriteback(cl_page_vmpage(env, pg))));
1372         EXIT;
1373 }
1374 EXPORT_SYMBOL(cl_page_completion);
1375
1376 /**
1377  * Notify layers that transfer formation engine decided to yank this page from
1378  * the cache and to make it a part of a transfer.
1379  *
1380  * \pre  pg->cp_state == CPS_CACHED
1381  * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
1382  *
1383  * \see cl_page_operations::cpo_make_ready()
1384  */
1385 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
1386                        enum cl_req_type crt)
1387 {
1388         int result;
1389
1390         PINVRNT(env, pg, crt < CRT_NR);
1391
1392         ENTRY;
1393         result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
1394                                 (const struct lu_env *,
1395                                  const struct cl_page_slice *));
1396         if (result == 0) {
1397                 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1398                 cl_page_io_start(env, pg, crt);
1399         }
1400         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1401         RETURN(result);
1402 }
1403 EXPORT_SYMBOL(cl_page_make_ready);
1404
1405 /**
1406  * Notify layers that high level io decided to place this page into a cache
1407  * for future transfer.
1408  *
1409  * The layer implementing transfer engine (osc) has to register this page in
1410  * its queues.
1411  *
1412  * \pre  cl_page_is_owned(pg, io)
1413  * \post ergo(result == 0,
1414  *            pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT)
1415  *
1416  * \see cl_page_operations::cpo_cache_add()
1417  */
1418 int cl_page_cache_add(const struct lu_env *env, struct cl_io *io,
1419                       struct cl_page *pg, enum cl_req_type crt)
1420 {
1421         int result;
1422
1423         PINVRNT(env, pg, crt < CRT_NR);
1424         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1425         PINVRNT(env, pg, cl_page_invariant(pg));
1426
1427         ENTRY;
1428         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_cache_add));
1429         if (result == 0) {
1430                 cl_page_owner_clear(pg);
1431                 cl_page_state_set(env, pg, CPS_CACHED);
1432         }
1433         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1434         RETURN(result);
1435 }
1436 EXPORT_SYMBOL(cl_page_cache_add);
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         result = cl_io_init(env, io, CIT_MISC, obj);
1492         if (result != 0) {
1493                 cl_io_fini(env, io);
1494                 RETURN(io->ci_result);
1495         }
1496
1497         do {
1498                 result = cl_page_gang_lookup(env, obj, io, 0, CL_PAGE_EOF,
1499                                              page_prune_cb, NULL);
1500                 if (result == CLP_GANG_RESCHED)
1501                         cfs_cond_resched();
1502         } while (result != CLP_GANG_OKAY);
1503
1504         cl_io_fini(env, io);
1505         RETURN(result);
1506 }
1507 EXPORT_SYMBOL(cl_pages_prune);
1508
1509 /**
1510  * Tells transfer engine that only part of a page is to be transmitted.
1511  *
1512  * \see cl_page_operations::cpo_clip()
1513  */
1514 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1515                   int from, int to)
1516 {
1517         PINVRNT(env, pg, cl_page_invariant(pg));
1518
1519         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1520         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1521                        (const struct lu_env *,
1522                         const struct cl_page_slice *,int, int),
1523                        from, to);
1524 }
1525 EXPORT_SYMBOL(cl_page_clip);
1526
1527 /**
1528  * Prints human readable representation of \a pg to the \a f.
1529  */
1530 void cl_page_header_print(const struct lu_env *env, void *cookie,
1531                           lu_printer_t printer, const struct cl_page *pg)
1532 {
1533         (*printer)(env, cookie,
1534                    "page@%p[%d %p:%lu ^%p_%p %d %d %d %p %p %#x]\n",
1535                    pg, cfs_atomic_read(&pg->cp_ref), pg->cp_obj,
1536                    pg->cp_index, pg->cp_parent, pg->cp_child,
1537                    pg->cp_state, pg->cp_error, pg->cp_type,
1538                    pg->cp_owner, pg->cp_req, pg->cp_flags);
1539 }
1540 EXPORT_SYMBOL(cl_page_header_print);
1541
1542 /**
1543  * Prints human readable representation of \a pg to the \a f.
1544  */
1545 void cl_page_print(const struct lu_env *env, void *cookie,
1546                    lu_printer_t printer, const struct cl_page *pg)
1547 {
1548         struct cl_page *scan;
1549
1550         for (scan = cl_page_top((struct cl_page *)pg);
1551              scan != NULL; scan = scan->cp_child)
1552                 cl_page_header_print(env, cookie, printer, scan);
1553         CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1554                        (const struct lu_env *env,
1555                         const struct cl_page_slice *slice,
1556                         void *cookie, lu_printer_t p), cookie, printer);
1557         (*printer)(env, cookie, "end page@%p\n", pg);
1558 }
1559 EXPORT_SYMBOL(cl_page_print);
1560
1561 /**
1562  * Cancel a page which is still in a transfer.
1563  */
1564 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1565 {
1566         return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1567                               (const struct lu_env *,
1568                                const struct cl_page_slice *));
1569 }
1570 EXPORT_SYMBOL(cl_page_cancel);
1571
1572 /**
1573  * Converts a byte offset within object \a obj into a page index.
1574  */
1575 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1576 {
1577         /*
1578          * XXX for now.
1579          */
1580         return (loff_t)idx << CFS_PAGE_SHIFT;
1581 }
1582 EXPORT_SYMBOL(cl_offset);
1583
1584 /**
1585  * Converts a page index into a byte offset within object \a obj.
1586  */
1587 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1588 {
1589         /*
1590          * XXX for now.
1591          */
1592         return offset >> CFS_PAGE_SHIFT;
1593 }
1594 EXPORT_SYMBOL(cl_index);
1595
1596 int cl_page_size(const struct cl_object *obj)
1597 {
1598         return 1 << CFS_PAGE_SHIFT;
1599 }
1600 EXPORT_SYMBOL(cl_page_size);
1601
1602 /**
1603  * Adds page slice to the compound page.
1604  *
1605  * This is called by cl_object_operations::coo_page_init() methods to add a
1606  * per-layer state to the page. New state is added at the end of
1607  * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1608  *
1609  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1610  */
1611 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1612                        struct cl_object *obj,
1613                        const struct cl_page_operations *ops)
1614 {
1615         ENTRY;
1616         cfs_list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1617         slice->cpl_obj  = obj;
1618         slice->cpl_ops  = ops;
1619         slice->cpl_page = page;
1620         EXIT;
1621 }
1622 EXPORT_SYMBOL(cl_page_slice_add);
1623
1624 int  cl_page_init(void)
1625 {
1626         return lu_kmem_init(cl_page_caches);
1627 }
1628
1629 void cl_page_fini(void)
1630 {
1631         lu_kmem_fini(cl_page_caches);
1632 }