Whamcloud - gitweb
LU-3963 obdclass: convert to linux list api
[fs/lustre-release.git] / lustre / obdclass / cl_page.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Client Lustre Page.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_CLASS
43
44 #include <libcfs/libcfs.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <libcfs/list.h>
48
49 #include <cl_object.h>
50 #include "cl_internal.h"
51
52 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg);
53
54 #ifdef LIBCFS_DEBUG
55 # define PASSERT(env, page, expr)                                       \
56   do {                                                                    \
57           if (unlikely(!(expr))) {                                      \
58                   CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n");    \
59                   LASSERT(0);                                           \
60           }                                                             \
61   } while (0)
62 #else /* !LIBCFS_DEBUG */
63 # define PASSERT(env, page, exp) \
64         ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
65 #endif /* !LIBCFS_DEBUG */
66
67 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
68 # define PINVRNT(env, page, expr)                                       \
69   do {                                                                    \
70           if (unlikely(!(expr))) {                                      \
71                   CL_PAGE_DEBUG(D_ERROR, (env), (page), #expr "\n");    \
72                   LINVRNT(0);                                           \
73           }                                                             \
74   } while (0)
75 #else /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK */
76 # define PINVRNT(env, page, exp) \
77          ((void)sizeof(env), (void)sizeof(page), (void)sizeof !!(exp))
78 #endif /* !CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK */
79
80 /* Disable page statistic by default due to huge performance penalty. */
81 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
82 #define CS_PAGE_INC(o, item) \
83         atomic_inc(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
84 #define CS_PAGE_DEC(o, item) \
85         atomic_dec(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
86 #define CS_PAGESTATE_INC(o, state) \
87         atomic_inc(&cl_object_site(o)->cs_pages_state[state])
88 #define CS_PAGESTATE_DEC(o, state) \
89         atomic_dec(&cl_object_site(o)->cs_pages_state[state])
90 #else
91 #define CS_PAGE_INC(o, item)
92 #define CS_PAGE_DEC(o, item)
93 #define CS_PAGESTATE_INC(o, state)
94 #define CS_PAGESTATE_DEC(o, state)
95 #endif
96
97 /**
98  * Internal version of cl_page_get().
99  *
100  * This function can be used to obtain initial reference to previously
101  * unreferenced cached object. It can be called only if concurrent page
102  * reclamation is somehow prevented, e.g., by keeping a lock on a VM page,
103  * associated with \a page.
104  *
105  * Use with care! Not exported.
106  */
107 static void cl_page_get_trust(struct cl_page *page)
108 {
109         LASSERT(atomic_read(&page->cp_ref) > 0);
110         atomic_inc(&page->cp_ref);
111 }
112
113 /**
114  * Returns a slice within a page, corresponding to the given layer in the
115  * device stack.
116  *
117  * \see cl_lock_at()
118  */
119 static const struct cl_page_slice *
120 cl_page_at_trusted(const struct cl_page *page,
121                    const struct lu_device_type *dtype)
122 {
123         const struct cl_page_slice *slice;
124         ENTRY;
125
126         list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
127                 if (slice->cpl_obj->co_lu.lo_dev->ld_type == dtype)
128                         RETURN(slice);
129         }
130         RETURN(NULL);
131 }
132
133 static void cl_page_free(const struct lu_env *env, struct cl_page *page)
134 {
135         struct cl_object *obj  = page->cp_obj;
136         int pagesize = cl_object_header(obj)->coh_page_bufsize;
137
138         PASSERT(env, page, list_empty(&page->cp_batch));
139         PASSERT(env, page, page->cp_owner == NULL);
140         PASSERT(env, page, page->cp_req == NULL);
141         PASSERT(env, page, page->cp_state == CPS_FREEING);
142
143         ENTRY;
144         while (!list_empty(&page->cp_layers)) {
145                 struct cl_page_slice *slice;
146
147                 slice = list_entry(page->cp_layers.next,
148                                    struct cl_page_slice, cpl_linkage);
149                 list_del_init(page->cp_layers.next);
150                 if (unlikely(slice->cpl_ops->cpo_fini != NULL))
151                         slice->cpl_ops->cpo_fini(env, slice);
152         }
153         CS_PAGE_DEC(obj, total);
154         CS_PAGESTATE_DEC(obj, page->cp_state);
155         lu_object_ref_del_at(&obj->co_lu, &page->cp_obj_ref, "cl_page", page);
156         cl_object_put(env, obj);
157         lu_ref_fini(&page->cp_reference);
158         OBD_FREE(page, pagesize);
159         EXIT;
160 }
161
162 /**
163  * Helper function updating page state. This is the only place in the code
164  * where cl_page::cp_state field is mutated.
165  */
166 static inline void cl_page_state_set_trust(struct cl_page *page,
167                                            enum cl_page_state state)
168 {
169         /* bypass const. */
170         *(enum cl_page_state *)&page->cp_state = state;
171 }
172
173 struct cl_page *cl_page_alloc(const struct lu_env *env,
174                 struct cl_object *o, pgoff_t ind, struct page *vmpage,
175                 enum cl_page_type type)
176 {
177         struct cl_page          *page;
178         struct lu_object_header *head;
179
180         ENTRY;
181         OBD_ALLOC_GFP(page, cl_object_header(o)->coh_page_bufsize,
182                         GFP_NOFS);
183         if (page != NULL) {
184                 int result = 0;
185                 atomic_set(&page->cp_ref, 1);
186                 page->cp_obj = o;
187                 cl_object_get(o);
188                 lu_object_ref_add_at(&o->co_lu, &page->cp_obj_ref, "cl_page",
189                                      page);
190                 page->cp_vmpage = vmpage;
191                 cl_page_state_set_trust(page, CPS_CACHED);
192                 page->cp_type = type;
193                 INIT_LIST_HEAD(&page->cp_layers);
194                 INIT_LIST_HEAD(&page->cp_batch);
195                 INIT_LIST_HEAD(&page->cp_flight);
196                 lu_ref_init(&page->cp_reference);
197                 head = o->co_lu.lo_header;
198                 list_for_each_entry(o, &head->loh_layers,
199                                     co_lu.lo_linkage) {
200                         if (o->co_ops->coo_page_init != NULL) {
201                                 result = o->co_ops->coo_page_init(env, o, page,
202                                                                   ind);
203                                 if (result != 0) {
204                                         cl_page_delete0(env, page);
205                                         cl_page_free(env, page);
206                                         page = ERR_PTR(result);
207                                         break;
208                                 }
209                         }
210                 }
211                 if (result == 0) {
212                         CS_PAGE_INC(o, total);
213                         CS_PAGE_INC(o, create);
214                         CS_PAGESTATE_DEC(o, CPS_CACHED);
215                 }
216         } else {
217                 page = ERR_PTR(-ENOMEM);
218         }
219         RETURN(page);
220 }
221 EXPORT_SYMBOL(cl_page_alloc);
222
223 /**
224  * Returns a cl_page with index \a idx at the object \a o, and associated with
225  * the VM page \a vmpage.
226  *
227  * This is the main entry point into the cl_page caching interface. First, a
228  * cache (implemented as a per-object radix tree) is consulted. If page is
229  * found there, it is returned immediately. Otherwise new page is allocated
230  * and returned. In any case, additional reference to page is acquired.
231  *
232  * \see cl_object_find(), cl_lock_find()
233  */
234 struct cl_page *cl_page_find(const struct lu_env *env,
235                              struct cl_object *o,
236                              pgoff_t idx, struct page *vmpage,
237                              enum cl_page_type type)
238 {
239         struct cl_page          *page = NULL;
240         struct cl_object_header *hdr;
241
242         LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
243         might_sleep();
244
245         ENTRY;
246
247         hdr = cl_object_header(o);
248         CS_PAGE_INC(o, lookup);
249
250         CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
251                idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
252         /* fast path. */
253         if (type == CPT_CACHEABLE) {
254                 /* vmpage lock is used to protect the child/parent
255                  * relationship */
256                 KLASSERT(PageLocked(vmpage));
257                 /*
258                  * cl_vmpage_page() can be called here without any locks as
259                  *
260                  *     - "vmpage" is locked (which prevents ->private from
261                  *       concurrent updates), and
262                  *
263                  *     - "o" cannot be destroyed while current thread holds a
264                  *       reference on it.
265                  */
266                 page = cl_vmpage_page(vmpage, o);
267                 if (page != NULL) {
268                         CS_PAGE_INC(o, hit);
269                         RETURN(page);
270                 }
271         }
272
273         /* allocate and initialize cl_page */
274         page = cl_page_alloc(env, o, idx, vmpage, type);
275         RETURN(page);
276 }
277 EXPORT_SYMBOL(cl_page_find);
278
279 static inline int cl_page_invariant(const struct cl_page *pg)
280 {
281         /*
282          * Page invariant is protected by a VM lock.
283          */
284         LINVRNT(cl_page_is_vmlocked(NULL, pg));
285
286         return cl_page_in_use_noref(pg);
287 }
288
289 static void cl_page_state_set0(const struct lu_env *env,
290                                struct cl_page *page, enum cl_page_state state)
291 {
292         enum cl_page_state old;
293
294         /*
295          * Matrix of allowed state transitions [old][new], for sanity
296          * checking.
297          */
298         static const int allowed_transitions[CPS_NR][CPS_NR] = {
299                 [CPS_CACHED] = {
300                         [CPS_CACHED]  = 0,
301                         [CPS_OWNED]   = 1, /* io finds existing cached page */
302                         [CPS_PAGEIN]  = 0,
303                         [CPS_PAGEOUT] = 1, /* write-out from the cache */
304                         [CPS_FREEING] = 1, /* eviction on the memory pressure */
305                 },
306                 [CPS_OWNED] = {
307                         [CPS_CACHED]  = 1, /* release to the cache */
308                         [CPS_OWNED]   = 0,
309                         [CPS_PAGEIN]  = 1, /* start read immediately */
310                         [CPS_PAGEOUT] = 1, /* start write immediately */
311                         [CPS_FREEING] = 1, /* lock invalidation or truncate */
312                 },
313                 [CPS_PAGEIN] = {
314                         [CPS_CACHED]  = 1, /* io completion */
315                         [CPS_OWNED]   = 0,
316                         [CPS_PAGEIN]  = 0,
317                         [CPS_PAGEOUT] = 0,
318                         [CPS_FREEING] = 0,
319                 },
320                 [CPS_PAGEOUT] = {
321                         [CPS_CACHED]  = 1, /* io completion */
322                         [CPS_OWNED]   = 0,
323                         [CPS_PAGEIN]  = 0,
324                         [CPS_PAGEOUT] = 0,
325                         [CPS_FREEING] = 0,
326                 },
327                 [CPS_FREEING] = {
328                         [CPS_CACHED]  = 0,
329                         [CPS_OWNED]   = 0,
330                         [CPS_PAGEIN]  = 0,
331                         [CPS_PAGEOUT] = 0,
332                         [CPS_FREEING] = 0,
333                 }
334         };
335
336         ENTRY;
337         old = page->cp_state;
338         PASSERT(env, page, allowed_transitions[old][state]);
339         CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
340         PASSERT(env, page, page->cp_state == old);
341         PASSERT(env, page, equi(state == CPS_OWNED, page->cp_owner != NULL));
342
343         CS_PAGESTATE_DEC(page->cp_obj, page->cp_state);
344         CS_PAGESTATE_INC(page->cp_obj, state);
345         cl_page_state_set_trust(page, state);
346         EXIT;
347 }
348
349 static void cl_page_state_set(const struct lu_env *env,
350                               struct cl_page *page, enum cl_page_state state)
351 {
352         cl_page_state_set0(env, page, state);
353 }
354
355 /**
356  * Acquires an additional reference to a page.
357  *
358  * This can be called only by caller already possessing a reference to \a
359  * page.
360  *
361  * \see cl_object_get(), cl_lock_get().
362  */
363 void cl_page_get(struct cl_page *page)
364 {
365         ENTRY;
366         cl_page_get_trust(page);
367         EXIT;
368 }
369 EXPORT_SYMBOL(cl_page_get);
370
371 /**
372  * Releases a reference to a page.
373  *
374  * When last reference is released, page is returned to the cache, unless it
375  * is in cl_page_state::CPS_FREEING state, in which case it is immediately
376  * destroyed.
377  *
378  * \see cl_object_put(), cl_lock_put().
379  */
380 void cl_page_put(const struct lu_env *env, struct cl_page *page)
381 {
382         ENTRY;
383         CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
384                        atomic_read(&page->cp_ref));
385
386         if (atomic_dec_and_test(&page->cp_ref)) {
387                 LASSERT(page->cp_state == CPS_FREEING);
388
389                 LASSERT(atomic_read(&page->cp_ref) == 0);
390                 PASSERT(env, page, page->cp_owner == NULL);
391                 PASSERT(env, page, list_empty(&page->cp_batch));
392                 /*
393                  * Page is no longer reachable by other threads. Tear
394                  * it down.
395                  */
396                 cl_page_free(env, page);
397         }
398
399         EXIT;
400 }
401 EXPORT_SYMBOL(cl_page_put);
402
403 /**
404  * Returns a cl_page associated with a VM page, and given cl_object.
405  */
406 struct cl_page *cl_vmpage_page(struct page *vmpage, struct cl_object *obj)
407 {
408         struct cl_page *page;
409
410         ENTRY;
411         KLASSERT(PageLocked(vmpage));
412
413         /*
414          * NOTE: absence of races and liveness of data are guaranteed by page
415          *       lock on a "vmpage". That works because object destruction has
416          *       bottom-to-top pass.
417          */
418
419         page = (struct cl_page *)vmpage->private;
420         if (page != NULL) {
421                 cl_page_get_trust(page);
422                 LASSERT(page->cp_type == CPT_CACHEABLE);
423         }
424         RETURN(page);
425 }
426 EXPORT_SYMBOL(cl_vmpage_page);
427
428 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
429                                        const struct lu_device_type *dtype)
430 {
431         return cl_page_at_trusted(page, dtype);
432 }
433 EXPORT_SYMBOL(cl_page_at);
434
435 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
436
437 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...)                   \
438 ({                                                                      \
439         const struct lu_env        *__env  = (_env);                    \
440         struct cl_page             *__page = (_page);                   \
441         const struct cl_page_slice *__scan;                             \
442         int                         __result;                           \
443         ptrdiff_t                   __op   = (_op);                     \
444         int                        (*__method)_proto;                   \
445                                                                         \
446         __result = 0;                                                   \
447         list_for_each_entry(__scan, &__page->cp_layers, cpl_linkage) {  \
448                 __method = *(void **)((char *)__scan->cpl_ops +  __op); \
449                 if (__method != NULL) {                                 \
450                         __result = (*__method)(__env, __scan, ## __VA_ARGS__); \
451                         if (__result != 0)                              \
452                                 break;                                  \
453                 }                                                       \
454         }                                                               \
455         if (__result > 0)                                               \
456                 __result = 0;                                           \
457         __result;                                                       \
458 })
459
460 #define CL_PAGE_INVOKE_REVERSE(_env, _page, _op, _proto, ...)           \
461 ({                                                                      \
462         const struct lu_env        *__env  = (_env);                    \
463         struct cl_page             *__page = (_page);                   \
464         const struct cl_page_slice *__scan;                             \
465         int                         __result;                           \
466         ptrdiff_t                   __op   = (_op);                     \
467         int                       (*__method)_proto;                    \
468                                                                         \
469         __result = 0;                                                   \
470         list_for_each_entry_reverse(__scan, &__page->cp_layers,         \
471                                     cpl_linkage) {                      \
472                 __method = *(void **)((char *)__scan->cpl_ops +  __op); \
473                 if (__method != NULL) {                                 \
474                         __result = (*__method)(__env, __scan, ## __VA_ARGS__); \
475                         if (__result != 0)                              \
476                                 break;                                  \
477                 }                                                       \
478         }                                                               \
479         if (__result > 0)                                               \
480                 __result = 0;                                           \
481         __result;                                                       \
482 })
483
484 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...)                   \
485 do {                                                                    \
486         const struct lu_env        *__env  = (_env);                    \
487         struct cl_page             *__page = (_page);                   \
488         const struct cl_page_slice *__scan;                             \
489         ptrdiff_t                   __op   = (_op);                     \
490         void                      (*__method)_proto;                    \
491                                                                         \
492         list_for_each_entry(__scan, &__page->cp_layers, cpl_linkage) {  \
493                 __method = *(void **)((char *)__scan->cpl_ops +  __op); \
494                 if (__method != NULL)                                   \
495                         (*__method)(__env, __scan, ## __VA_ARGS__);     \
496         }                                                               \
497 } while (0)
498
499 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...)           \
500 do {                                                                    \
501         const struct lu_env        *__env  = (_env);                    \
502         struct cl_page             *__page = (_page);                   \
503         const struct cl_page_slice *__scan;                             \
504         ptrdiff_t                   __op   = (_op);                     \
505         void                      (*__method)_proto;                    \
506                                                                         \
507         /* get to the bottom page. */                                   \
508         list_for_each_entry_reverse(__scan, &__page->cp_layers,         \
509                                     cpl_linkage) {                      \
510                 __method = *(void **)((char *)__scan->cpl_ops + __op);  \
511                 if (__method != NULL)                                   \
512                         (*__method)(__env, __scan, ## __VA_ARGS__);     \
513         }                                                               \
514 } while (0)
515
516 static int cl_page_invoke(const struct lu_env *env,
517                           struct cl_io *io, struct cl_page *page, ptrdiff_t op)
518
519 {
520         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
521         ENTRY;
522         RETURN(CL_PAGE_INVOKE(env, page, op,
523                               (const struct lu_env *,
524                                const struct cl_page_slice *, struct cl_io *),
525                               io));
526 }
527
528 static void cl_page_invoid(const struct lu_env *env,
529                            struct cl_io *io, struct cl_page *page, ptrdiff_t op)
530
531 {
532         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
533         ENTRY;
534         CL_PAGE_INVOID(env, page, op,
535                        (const struct lu_env *,
536                         const struct cl_page_slice *, struct cl_io *), io);
537         EXIT;
538 }
539
540 static void cl_page_owner_clear(struct cl_page *page)
541 {
542         ENTRY;
543         if (page->cp_owner != NULL) {
544                 LASSERT(page->cp_owner->ci_owned_nr > 0);
545                 page->cp_owner->ci_owned_nr--;
546                 page->cp_owner = NULL;
547         }
548         EXIT;
549 }
550
551 static void cl_page_owner_set(struct cl_page *page)
552 {
553         ENTRY;
554         LASSERT(page->cp_owner != NULL);
555         page->cp_owner->ci_owned_nr++;
556         EXIT;
557 }
558
559 void cl_page_disown0(const struct lu_env *env,
560                      struct cl_io *io, struct cl_page *pg)
561 {
562         enum cl_page_state state;
563
564         ENTRY;
565         state = pg->cp_state;
566         PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
567         PINVRNT(env, pg, cl_page_invariant(pg) || state == CPS_FREEING);
568         cl_page_owner_clear(pg);
569
570         if (state == CPS_OWNED)
571                 cl_page_state_set(env, pg, CPS_CACHED);
572         /*
573          * Completion call-backs are executed in the bottom-up order, so that
574          * uppermost layer (llite), responsible for VFS/VM interaction runs
575          * last and can release locks safely.
576          */
577         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
578                                (const struct lu_env *,
579                                 const struct cl_page_slice *, struct cl_io *),
580                                io);
581         EXIT;
582 }
583
584 /**
585  * returns true, iff page is owned by the given io.
586  */
587 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
588 {
589         struct cl_io *top = cl_io_top((struct cl_io *)io);
590         LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
591         ENTRY;
592         RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == top);
593 }
594 EXPORT_SYMBOL(cl_page_is_owned);
595
596 /**
597  * Try to own a page by IO.
598  *
599  * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
600  * into cl_page_state::CPS_OWNED state.
601  *
602  * \pre  !cl_page_is_owned(pg, io)
603  * \post result == 0 iff cl_page_is_owned(pg, io)
604  *
605  * \retval 0   success
606  *
607  * \retval -ve failure, e.g., page was destroyed (and landed in
608  *             cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
609  *             or, page was owned by another thread, or in IO.
610  *
611  * \see cl_page_disown()
612  * \see cl_page_operations::cpo_own()
613  * \see cl_page_own_try()
614  * \see cl_page_own
615  */
616 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
617                         struct cl_page *pg, int nonblock)
618 {
619         int result;
620
621         PINVRNT(env, pg, !cl_page_is_owned(pg, io));
622
623         ENTRY;
624         io = cl_io_top(io);
625
626         if (pg->cp_state == CPS_FREEING) {
627                 result = -ENOENT;
628         } else {
629                 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
630                                         (const struct lu_env *,
631                                          const struct cl_page_slice *,
632                                          struct cl_io *, int),
633                                         io, nonblock);
634                 if (result == 0) {
635                         PASSERT(env, pg, pg->cp_owner == NULL);
636                         PASSERT(env, pg, pg->cp_req == NULL);
637                         pg->cp_owner = cl_io_top(io);;
638                         cl_page_owner_set(pg);
639                         if (pg->cp_state != CPS_FREEING) {
640                                 cl_page_state_set(env, pg, CPS_OWNED);
641                         } else {
642                                 cl_page_disown0(env, io, pg);
643                                 result = -ENOENT;
644                         }
645                 }
646         }
647         PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
648         RETURN(result);
649 }
650
651 /**
652  * Own a page, might be blocked.
653  *
654  * \see cl_page_own0()
655  */
656 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
657 {
658         return cl_page_own0(env, io, pg, 0);
659 }
660 EXPORT_SYMBOL(cl_page_own);
661
662 /**
663  * Nonblock version of cl_page_own().
664  *
665  * \see cl_page_own0()
666  */
667 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
668                     struct cl_page *pg)
669 {
670         return cl_page_own0(env, io, pg, 1);
671 }
672 EXPORT_SYMBOL(cl_page_own_try);
673
674
675 /**
676  * Assume page ownership.
677  *
678  * Called when page is already locked by the hosting VM.
679  *
680  * \pre !cl_page_is_owned(pg, io)
681  * \post cl_page_is_owned(pg, io)
682  *
683  * \see cl_page_operations::cpo_assume()
684  */
685 void cl_page_assume(const struct lu_env *env,
686                     struct cl_io *io, struct cl_page *pg)
687 {
688         PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
689
690         ENTRY;
691         io = cl_io_top(io);
692
693         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
694         PASSERT(env, pg, pg->cp_owner == NULL);
695         pg->cp_owner = cl_io_top(io);
696         cl_page_owner_set(pg);
697         cl_page_state_set(env, pg, CPS_OWNED);
698         EXIT;
699 }
700 EXPORT_SYMBOL(cl_page_assume);
701
702 /**
703  * Releases page ownership without unlocking the page.
704  *
705  * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
706  * underlying VM page (as VM is supposed to do this itself).
707  *
708  * \pre   cl_page_is_owned(pg, io)
709  * \post !cl_page_is_owned(pg, io)
710  *
711  * \see cl_page_assume()
712  */
713 void cl_page_unassume(const struct lu_env *env,
714                       struct cl_io *io, struct cl_page *pg)
715 {
716         PINVRNT(env, pg, cl_page_is_owned(pg, io));
717         PINVRNT(env, pg, cl_page_invariant(pg));
718
719         ENTRY;
720         io = cl_io_top(io);
721         cl_page_owner_clear(pg);
722         cl_page_state_set(env, pg, CPS_CACHED);
723         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
724                                (const struct lu_env *,
725                                 const struct cl_page_slice *, struct cl_io *),
726                                io);
727         EXIT;
728 }
729 EXPORT_SYMBOL(cl_page_unassume);
730
731 /**
732  * Releases page ownership.
733  *
734  * Moves page into cl_page_state::CPS_CACHED.
735  *
736  * \pre   cl_page_is_owned(pg, io)
737  * \post !cl_page_is_owned(pg, io)
738  *
739  * \see cl_page_own()
740  * \see cl_page_operations::cpo_disown()
741  */
742 void cl_page_disown(const struct lu_env *env,
743                     struct cl_io *io, struct cl_page *pg)
744 {
745         PINVRNT(env, pg, cl_page_is_owned(pg, io) ||
746                 pg->cp_state == CPS_FREEING);
747
748         ENTRY;
749         io = cl_io_top(io);
750         cl_page_disown0(env, io, pg);
751         EXIT;
752 }
753 EXPORT_SYMBOL(cl_page_disown);
754
755 /**
756  * Called when page is to be removed from the object, e.g., as a result of
757  * truncate.
758  *
759  * Calls cl_page_operations::cpo_discard() top-to-bottom.
760  *
761  * \pre cl_page_is_owned(pg, io)
762  *
763  * \see cl_page_operations::cpo_discard()
764  */
765 void cl_page_discard(const struct lu_env *env,
766                      struct cl_io *io, struct cl_page *pg)
767 {
768         PINVRNT(env, pg, cl_page_is_owned(pg, io));
769         PINVRNT(env, pg, cl_page_invariant(pg));
770
771         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
772 }
773 EXPORT_SYMBOL(cl_page_discard);
774
775 /**
776  * Version of cl_page_delete() that can be called for not fully constructed
777  * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
778  * path. Doesn't check page invariant.
779  */
780 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg)
781 {
782         ENTRY;
783
784         PASSERT(env, pg, pg->cp_state != CPS_FREEING);
785
786         /*
787          * Severe all ways to obtain new pointers to @pg.
788          */
789         cl_page_owner_clear(pg);
790
791         cl_page_state_set0(env, pg, CPS_FREEING);
792
793         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_delete),
794                        (const struct lu_env *, const struct cl_page_slice *));
795
796         EXIT;
797 }
798
799 /**
800  * Called when a decision is made to throw page out of memory.
801  *
802  * Notifies all layers about page destruction by calling
803  * cl_page_operations::cpo_delete() method top-to-bottom.
804  *
805  * Moves page into cl_page_state::CPS_FREEING state (this is the only place
806  * where transition to this state happens).
807  *
808  * Eliminates all venues through which new references to the page can be
809  * obtained:
810  *
811  *     - removes page from the radix trees,
812  *
813  *     - breaks linkage from VM page to cl_page.
814  *
815  * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
816  * drain after some time, at which point page will be recycled.
817  *
818  * \pre  VM page is locked
819  * \post pg->cp_state == CPS_FREEING
820  *
821  * \see cl_page_operations::cpo_delete()
822  */
823 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
824 {
825         PINVRNT(env, pg, cl_page_invariant(pg));
826         ENTRY;
827         cl_page_delete0(env, pg);
828         EXIT;
829 }
830 EXPORT_SYMBOL(cl_page_delete);
831
832 /**
833  * Marks page up-to-date.
834  *
835  * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
836  * layer responsible for VM interaction has to mark/clear page as up-to-date
837  * by the \a uptodate argument.
838  *
839  * \see cl_page_operations::cpo_export()
840  */
841 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
842 {
843         PINVRNT(env, pg, cl_page_invariant(pg));
844         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
845                        (const struct lu_env *,
846                         const struct cl_page_slice *, int), uptodate);
847 }
848 EXPORT_SYMBOL(cl_page_export);
849
850 /**
851  * Returns true, iff \a pg is VM locked in a suitable sense by the calling
852  * thread.
853  */
854 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
855 {
856         int result;
857         const struct cl_page_slice *slice;
858
859         ENTRY;
860         slice = container_of(pg->cp_layers.next,
861                              const struct cl_page_slice, cpl_linkage);
862         PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
863         /*
864          * Call ->cpo_is_vmlocked() directly instead of going through
865          * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
866          * cl_page_invariant().
867          */
868         result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
869         PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
870         RETURN(result == -EBUSY);
871 }
872 EXPORT_SYMBOL(cl_page_is_vmlocked);
873
874 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
875 {
876         ENTRY;
877         RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
878 }
879
880 static void cl_page_io_start(const struct lu_env *env,
881                              struct cl_page *pg, enum cl_req_type crt)
882 {
883         /*
884          * Page is queued for IO, change its state.
885          */
886         ENTRY;
887         cl_page_owner_clear(pg);
888         cl_page_state_set(env, pg, cl_req_type_state(crt));
889         EXIT;
890 }
891
892 /**
893  * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
894  * called top-to-bottom. Every layer either agrees to submit this page (by
895  * returning 0), or requests to omit this page (by returning -EALREADY). Layer
896  * handling interactions with the VM also has to inform VM that page is under
897  * transfer now.
898  */
899 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
900                  struct cl_page *pg, enum cl_req_type crt)
901 {
902         int result;
903
904         PINVRNT(env, pg, cl_page_is_owned(pg, io));
905         PINVRNT(env, pg, cl_page_invariant(pg));
906         PINVRNT(env, pg, crt < CRT_NR);
907
908         /*
909          * XXX this has to be called bottom-to-top, so that llite can set up
910          * PG_writeback without risking other layers deciding to skip this
911          * page.
912          */
913         if (crt >= CRT_NR)
914                 return -EINVAL;
915         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
916         if (result == 0)
917                 cl_page_io_start(env, pg, crt);
918
919         KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
920                       equi(result == 0,
921                            PageWriteback(cl_page_vmpage(pg)))));
922         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
923         return result;
924 }
925 EXPORT_SYMBOL(cl_page_prep);
926
927 /**
928  * Notify layers about transfer completion.
929  *
930  * Invoked by transfer sub-system (which is a part of osc) to notify layers
931  * that a transfer, of which this page is a part of has completed.
932  *
933  * Completion call-backs are executed in the bottom-up order, so that
934  * uppermost layer (llite), responsible for the VFS/VM interaction runs last
935  * and can release locks safely.
936  *
937  * \pre  pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
938  * \post pg->cp_state == CPS_CACHED
939  *
940  * \see cl_page_operations::cpo_completion()
941  */
942 void cl_page_completion(const struct lu_env *env,
943                         struct cl_page *pg, enum cl_req_type crt, int ioret)
944 {
945         struct cl_sync_io *anchor = pg->cp_sync_io;
946
947         PASSERT(env, pg, crt < CRT_NR);
948         /* cl_page::cp_req already cleared by the caller (osc_completion()) */
949         PASSERT(env, pg, pg->cp_req == NULL);
950         PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
951
952         ENTRY;
953         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
954         cl_page_state_set(env, pg, CPS_CACHED);
955         if (crt >= CRT_NR)
956                 return;
957         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
958                                (const struct lu_env *,
959                                 const struct cl_page_slice *, int), ioret);
960         if (anchor) {
961                 LASSERT(cl_page_is_vmlocked(env, pg));
962                 LASSERT(pg->cp_sync_io == anchor);
963                 pg->cp_sync_io = NULL;
964         }
965         /*
966          * As page->cp_obj is pinned by a reference from page->cp_req, it is
967          * safe to call cl_page_put() without risking object destruction in a
968          * non-blocking context.
969          */
970         cl_page_put(env, pg);
971
972         if (anchor)
973                 cl_sync_io_note(anchor, ioret);
974
975         EXIT;
976 }
977 EXPORT_SYMBOL(cl_page_completion);
978
979 /**
980  * Notify layers that transfer formation engine decided to yank this page from
981  * the cache and to make it a part of a transfer.
982  *
983  * \pre  pg->cp_state == CPS_CACHED
984  * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
985  *
986  * \see cl_page_operations::cpo_make_ready()
987  */
988 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
989                        enum cl_req_type crt)
990 {
991         int result;
992
993         PINVRNT(env, pg, crt < CRT_NR);
994
995         ENTRY;
996         if (crt >= CRT_NR)
997                 RETURN(-EINVAL);
998         result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
999                                 (const struct lu_env *,
1000                                  const struct cl_page_slice *));
1001         if (result == 0) {
1002                 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
1003                 cl_page_io_start(env, pg, crt);
1004         }
1005         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
1006         RETURN(result);
1007 }
1008 EXPORT_SYMBOL(cl_page_make_ready);
1009
1010 /**
1011  * Called if a pge is being written back by kernel's intention.
1012  *
1013  * \pre  cl_page_is_owned(pg, io)
1014  * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
1015  *
1016  * \see cl_page_operations::cpo_flush()
1017  */
1018 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
1019                   struct cl_page *pg)
1020 {
1021         int result;
1022
1023         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1024         PINVRNT(env, pg, cl_page_invariant(pg));
1025
1026         ENTRY;
1027
1028         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_flush));
1029
1030         CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1031         RETURN(result);
1032 }
1033 EXPORT_SYMBOL(cl_page_flush);
1034
1035 /**
1036  * Checks whether page is protected by any extent lock is at least required
1037  * mode.
1038  *
1039  * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1040  * \see cl_page_operations::cpo_is_under_lock()
1041  */
1042 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1043                           struct cl_page *page, pgoff_t *max_index)
1044 {
1045         int rc;
1046
1047         PINVRNT(env, page, cl_page_invariant(page));
1048
1049         ENTRY;
1050         rc = CL_PAGE_INVOKE_REVERSE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1051                                     (const struct lu_env *,
1052                                      const struct cl_page_slice *,
1053                                      struct cl_io *, pgoff_t *),
1054                                     io, max_index);
1055         RETURN(rc);
1056 }
1057 EXPORT_SYMBOL(cl_page_is_under_lock);
1058
1059 /**
1060  * Tells transfer engine that only part of a page is to be transmitted.
1061  *
1062  * \see cl_page_operations::cpo_clip()
1063  */
1064 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1065                   int from, int to)
1066 {
1067         PINVRNT(env, pg, cl_page_invariant(pg));
1068
1069         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1070         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1071                        (const struct lu_env *,
1072                         const struct cl_page_slice *,int, int),
1073                        from, to);
1074 }
1075 EXPORT_SYMBOL(cl_page_clip);
1076
1077 /**
1078  * Prints human readable representation of \a pg to the \a f.
1079  */
1080 void cl_page_header_print(const struct lu_env *env, void *cookie,
1081                           lu_printer_t printer, const struct cl_page *pg)
1082 {
1083         (*printer)(env, cookie,
1084                    "page@%p[%d %p %d %d %d %p %p]\n",
1085                    pg, atomic_read(&pg->cp_ref), pg->cp_obj,
1086                    pg->cp_state, pg->cp_error, pg->cp_type,
1087                    pg->cp_owner, pg->cp_req);
1088 }
1089 EXPORT_SYMBOL(cl_page_header_print);
1090
1091 /**
1092  * Prints human readable representation of \a pg to the \a f.
1093  */
1094 void cl_page_print(const struct lu_env *env, void *cookie,
1095                    lu_printer_t printer, const struct cl_page *pg)
1096 {
1097         cl_page_header_print(env, cookie, printer, pg);
1098         CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1099                        (const struct lu_env *env,
1100                         const struct cl_page_slice *slice,
1101                         void *cookie, lu_printer_t p), cookie, printer);
1102         (*printer)(env, cookie, "end page@%p\n", pg);
1103 }
1104 EXPORT_SYMBOL(cl_page_print);
1105
1106 /**
1107  * Cancel a page which is still in a transfer.
1108  */
1109 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1110 {
1111         return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1112                               (const struct lu_env *,
1113                                const struct cl_page_slice *));
1114 }
1115 EXPORT_SYMBOL(cl_page_cancel);
1116
1117 /**
1118  * Converts a byte offset within object \a obj into a page index.
1119  */
1120 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1121 {
1122         return (loff_t)idx << PAGE_CACHE_SHIFT;
1123 }
1124 EXPORT_SYMBOL(cl_offset);
1125
1126 /**
1127  * Converts a page index into a byte offset within object \a obj.
1128  */
1129 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1130 {
1131         return offset >> PAGE_CACHE_SHIFT;
1132 }
1133 EXPORT_SYMBOL(cl_index);
1134
1135 int cl_page_size(const struct cl_object *obj)
1136 {
1137         return 1 << PAGE_CACHE_SHIFT;
1138 }
1139 EXPORT_SYMBOL(cl_page_size);
1140
1141 /**
1142  * Adds page slice to the compound page.
1143  *
1144  * This is called by cl_object_operations::coo_page_init() methods to add a
1145  * per-layer state to the page. New state is added at the end of
1146  * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1147  *
1148  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1149  */
1150 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1151                        struct cl_object *obj, pgoff_t index,
1152                        const struct cl_page_operations *ops)
1153 {
1154         ENTRY;
1155         list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1156         slice->cpl_obj  = obj;
1157         slice->cpl_index = index;
1158         slice->cpl_ops  = ops;
1159         slice->cpl_page = page;
1160         EXIT;
1161 }
1162 EXPORT_SYMBOL(cl_page_slice_add);
1163
1164 int  cl_page_init(void)
1165 {
1166         return 0;
1167 }
1168
1169 void cl_page_fini(void)
1170 {
1171 }