Whamcloud - gitweb
a42d12f84f04ac1fb76d32cb77cfd172af6da580
[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         cfs_atomic_inc(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
84 #define CS_PAGE_DEC(o, item) \
85         cfs_atomic_dec(&cl_object_site(o)->cs_pages.cs_stats[CS_##item])
86 #define CS_PAGESTATE_INC(o, state) \
87         cfs_atomic_inc(&cl_object_site(o)->cs_pages_state[state])
88 #define CS_PAGESTATE_DEC(o, state) \
89         cfs_atomic_dec(&cl_object_site(o)->cs_pages_state[state])
90 #else
91 #define CS_PAGE_INC(o, item)
92 #define CS_PAGE_DEC(o, item)
93 #define CS_PAGESTATE_INC(o, state)
94 #define CS_PAGESTATE_DEC(o, state)
95 #endif
96
97 /**
98  * Internal version of cl_page_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(cfs_atomic_read(&page->cp_ref) > 0);
110         cfs_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         cfs_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, cfs_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 (!cfs_list_empty(&page->cp_layers)) {
145                 struct cl_page_slice *slice;
146
147                 slice = cfs_list_entry(page->cp_layers.next,
148                                        struct cl_page_slice, cpl_linkage);
149                 cfs_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_IO);
183         if (page != NULL) {
184                 int result = 0;
185                 cfs_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                 CFS_INIT_LIST_HEAD(&page->cp_layers);
194                 CFS_INIT_LIST_HEAD(&page->cp_batch);
195                 CFS_INIT_LIST_HEAD(&page->cp_flight);
196                 mutex_init(&page->cp_mutex);
197                 lu_ref_init(&page->cp_reference);
198                 head = o->co_lu.lo_header;
199                 cfs_list_for_each_entry(o, &head->loh_layers,
200                                         co_lu.lo_linkage) {
201                         if (o->co_ops->coo_page_init != NULL) {
202                                 result = o->co_ops->coo_page_init(env, o, page,
203                                                                   ind);
204                                 if (result != 0) {
205                                         cl_page_delete0(env, page);
206                                         cl_page_free(env, page);
207                                         page = ERR_PTR(result);
208                                         break;
209                                 }
210                         }
211                 }
212                 if (result == 0) {
213                         CS_PAGE_INC(o, total);
214                         CS_PAGE_INC(o, create);
215                         CS_PAGESTATE_DEC(o, CPS_CACHED);
216                 }
217         } else {
218                 page = ERR_PTR(-ENOMEM);
219         }
220         RETURN(page);
221 }
222 EXPORT_SYMBOL(cl_page_alloc);
223
224 /**
225  * Returns a cl_page with index \a idx at the object \a o, and associated with
226  * the VM page \a vmpage.
227  *
228  * This is the main entry point into the cl_page caching interface. First, a
229  * cache (implemented as a per-object radix tree) is consulted. If page is
230  * found there, it is returned immediately. Otherwise new page is allocated
231  * and returned. In any case, additional reference to page is acquired.
232  *
233  * \see cl_object_find(), cl_lock_find()
234  */
235 struct cl_page *cl_page_find(const struct lu_env *env,
236                              struct cl_object *o,
237                              pgoff_t idx, struct page *vmpage,
238                              enum cl_page_type type)
239 {
240         struct cl_page          *page = NULL;
241         struct cl_object_header *hdr;
242
243         LASSERT(type == CPT_CACHEABLE || type == CPT_TRANSIENT);
244         might_sleep();
245
246         ENTRY;
247
248         hdr = cl_object_header(o);
249         CS_PAGE_INC(o, lookup);
250
251         CDEBUG(D_PAGE, "%lu@"DFID" %p %lx %d\n",
252                idx, PFID(&hdr->coh_lu.loh_fid), vmpage, vmpage->private, type);
253         /* fast path. */
254         if (type == CPT_CACHEABLE) {
255                 /* vmpage lock is used to protect the child/parent
256                  * relationship */
257                 KLASSERT(PageLocked(vmpage));
258                 /*
259                  * cl_vmpage_page() can be called here without any locks as
260                  *
261                  *     - "vmpage" is locked (which prevents ->private from
262                  *       concurrent updates), and
263                  *
264                  *     - "o" cannot be destroyed while current thread holds a
265                  *       reference on it.
266                  */
267                 page = cl_vmpage_page(vmpage, o);
268                 if (page != NULL) {
269                         CS_PAGE_INC(o, hit);
270                         RETURN(page);
271                 }
272         }
273
274         /* allocate and initialize cl_page */
275         page = cl_page_alloc(env, o, idx, vmpage, type);
276         RETURN(page);
277 }
278 EXPORT_SYMBOL(cl_page_find);
279
280 static inline int cl_page_invariant(const struct cl_page *pg)
281 {
282         /*
283          * Page invariant is protected by a VM lock.
284          */
285         LINVRNT(cl_page_is_vmlocked(NULL, pg));
286
287         return cl_page_in_use_noref(pg);
288 }
289
290 static void cl_page_state_set0(const struct lu_env *env,
291                                struct cl_page *page, enum cl_page_state state)
292 {
293         enum cl_page_state old;
294
295         /*
296          * Matrix of allowed state transitions [old][new], for sanity
297          * checking.
298          */
299         static const int allowed_transitions[CPS_NR][CPS_NR] = {
300                 [CPS_CACHED] = {
301                         [CPS_CACHED]  = 0,
302                         [CPS_OWNED]   = 1, /* io finds existing cached page */
303                         [CPS_PAGEIN]  = 0,
304                         [CPS_PAGEOUT] = 1, /* write-out from the cache */
305                         [CPS_FREEING] = 1, /* eviction on the memory pressure */
306                 },
307                 [CPS_OWNED] = {
308                         [CPS_CACHED]  = 1, /* release to the cache */
309                         [CPS_OWNED]   = 0,
310                         [CPS_PAGEIN]  = 1, /* start read immediately */
311                         [CPS_PAGEOUT] = 1, /* start write immediately */
312                         [CPS_FREEING] = 1, /* lock invalidation or truncate */
313                 },
314                 [CPS_PAGEIN] = {
315                         [CPS_CACHED]  = 1, /* io completion */
316                         [CPS_OWNED]   = 0,
317                         [CPS_PAGEIN]  = 0,
318                         [CPS_PAGEOUT] = 0,
319                         [CPS_FREEING] = 0,
320                 },
321                 [CPS_PAGEOUT] = {
322                         [CPS_CACHED]  = 1, /* io completion */
323                         [CPS_OWNED]   = 0,
324                         [CPS_PAGEIN]  = 0,
325                         [CPS_PAGEOUT] = 0,
326                         [CPS_FREEING] = 0,
327                 },
328                 [CPS_FREEING] = {
329                         [CPS_CACHED]  = 0,
330                         [CPS_OWNED]   = 0,
331                         [CPS_PAGEIN]  = 0,
332                         [CPS_PAGEOUT] = 0,
333                         [CPS_FREEING] = 0,
334                 }
335         };
336
337         ENTRY;
338         old = page->cp_state;
339         PASSERT(env, page, allowed_transitions[old][state]);
340         CL_PAGE_HEADER(D_TRACE, env, page, "%d -> %d\n", old, state);
341         PASSERT(env, page, page->cp_state == old);
342         PASSERT(env, page, equi(state == CPS_OWNED, page->cp_owner != NULL));
343
344         CS_PAGESTATE_DEC(page->cp_obj, page->cp_state);
345         CS_PAGESTATE_INC(page->cp_obj, state);
346         cl_page_state_set_trust(page, state);
347         EXIT;
348 }
349
350 static void cl_page_state_set(const struct lu_env *env,
351                               struct cl_page *page, enum cl_page_state state)
352 {
353         cl_page_state_set0(env, page, state);
354 }
355
356 /**
357  * Acquires an additional reference to a page.
358  *
359  * This can be called only by caller already possessing a reference to \a
360  * page.
361  *
362  * \see cl_object_get(), cl_lock_get().
363  */
364 void cl_page_get(struct cl_page *page)
365 {
366         ENTRY;
367         cl_page_get_trust(page);
368         EXIT;
369 }
370 EXPORT_SYMBOL(cl_page_get);
371
372 /**
373  * Releases a reference to a page.
374  *
375  * When last reference is released, page is returned to the cache, unless it
376  * is in cl_page_state::CPS_FREEING state, in which case it is immediately
377  * destroyed.
378  *
379  * \see cl_object_put(), cl_lock_put().
380  */
381 void cl_page_put(const struct lu_env *env, struct cl_page *page)
382 {
383         ENTRY;
384         CL_PAGE_HEADER(D_TRACE, env, page, "%d\n",
385                        cfs_atomic_read(&page->cp_ref));
386
387         if (cfs_atomic_dec_and_test(&page->cp_ref)) {
388                 LASSERT(page->cp_state == CPS_FREEING);
389
390                 LASSERT(cfs_atomic_read(&page->cp_ref) == 0);
391                 PASSERT(env, page, page->cp_owner == NULL);
392                 PASSERT(env, page, cfs_list_empty(&page->cp_batch));
393                 /*
394                  * Page is no longer reachable by other threads. Tear
395                  * it down.
396                  */
397                 cl_page_free(env, page);
398         }
399
400         EXIT;
401 }
402 EXPORT_SYMBOL(cl_page_put);
403
404 /**
405  * Returns a cl_page associated with a VM page, and given cl_object.
406  */
407 struct cl_page *cl_vmpage_page(struct page *vmpage, struct cl_object *obj)
408 {
409         struct cl_page *page;
410
411         ENTRY;
412         KLASSERT(PageLocked(vmpage));
413
414         /*
415          * NOTE: absence of races and liveness of data are guaranteed by page
416          *       lock on a "vmpage". That works because object destruction has
417          *       bottom-to-top pass.
418          */
419
420         page = (struct cl_page *)vmpage->private;
421         if (page != NULL) {
422                 cl_page_get_trust(page);
423                 LASSERT(page->cp_type == CPT_CACHEABLE);
424         }
425         RETURN(page);
426 }
427 EXPORT_SYMBOL(cl_vmpage_page);
428
429 const struct cl_page_slice *cl_page_at(const struct cl_page *page,
430                                        const struct lu_device_type *dtype)
431 {
432         return cl_page_at_trusted(page, dtype);
433 }
434 EXPORT_SYMBOL(cl_page_at);
435
436 #define CL_PAGE_OP(opname) offsetof(struct cl_page_operations, opname)
437
438 #define CL_PAGE_INVOKE(_env, _page, _op, _proto, ...)                   \
439 ({                                                                      \
440         const struct lu_env        *__env  = (_env);                    \
441         struct cl_page             *__page = (_page);                   \
442         const struct cl_page_slice *__scan;                             \
443         int                         __result;                           \
444         ptrdiff_t                   __op   = (_op);                     \
445         int                       (*__method)_proto;                    \
446                                                                         \
447         __result = 0;                                                   \
448         cfs_list_for_each_entry(__scan, &__page->cp_layers, cpl_linkage) {     \
449                 __method = *(void **)((char *)__scan->cpl_ops +  __op);        \
450                 if (__method != NULL) {                                        \
451                         __result = (*__method)(__env, __scan, ## __VA_ARGS__); \
452                         if (__result != 0)                              \
453                                 break;                                  \
454                 }                                                       \
455         }                                                               \
456         if (__result > 0)                                               \
457                 __result = 0;                                           \
458         __result;                                                       \
459 })
460
461 #define CL_PAGE_INVOID(_env, _page, _op, _proto, ...)                   \
462 do {                                                                    \
463         const struct lu_env        *__env  = (_env);                    \
464         struct cl_page             *__page = (_page);                   \
465         const struct cl_page_slice *__scan;                             \
466         ptrdiff_t                   __op   = (_op);                     \
467         void                      (*__method)_proto;                    \
468                                                                         \
469         cfs_list_for_each_entry(__scan, &__page->cp_layers,             \
470                                 cpl_linkage) {                          \
471                 __method = *(void **)((char *)__scan->cpl_ops +  __op); \
472                 if (__method != NULL)                                   \
473                         (*__method)(__env, __scan, ## __VA_ARGS__);     \
474         }                                                               \
475 } while (0)
476
477 #define CL_PAGE_INVOID_REVERSE(_env, _page, _op, _proto, ...)           \
478 do {                                                                    \
479         const struct lu_env        *__env  = (_env);                    \
480         struct cl_page             *__page = (_page);                   \
481         const struct cl_page_slice *__scan;                             \
482         ptrdiff_t                   __op   = (_op);                     \
483         void                      (*__method)_proto;                    \
484                                                                         \
485         /* get to the bottom page. */                                   \
486         cfs_list_for_each_entry_reverse(__scan, &__page->cp_layers,     \
487                                         cpl_linkage) {                  \
488                 __method = *(void **)((char *)__scan->cpl_ops + __op);  \
489                 if (__method != NULL)                                   \
490                         (*__method)(__env, __scan, ## __VA_ARGS__);     \
491         }                                                               \
492 } while (0)
493
494 static int cl_page_invoke(const struct lu_env *env,
495                           struct cl_io *io, struct cl_page *page, ptrdiff_t op)
496
497 {
498         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
499         ENTRY;
500         RETURN(CL_PAGE_INVOKE(env, page, op,
501                               (const struct lu_env *,
502                                const struct cl_page_slice *, struct cl_io *),
503                               io));
504 }
505
506 static void cl_page_invoid(const struct lu_env *env,
507                            struct cl_io *io, struct cl_page *page, ptrdiff_t op)
508
509 {
510         PINVRNT(env, page, cl_object_same(page->cp_obj, io->ci_obj));
511         ENTRY;
512         CL_PAGE_INVOID(env, page, op,
513                        (const struct lu_env *,
514                         const struct cl_page_slice *, struct cl_io *), io);
515         EXIT;
516 }
517
518 static void cl_page_owner_clear(struct cl_page *page)
519 {
520         ENTRY;
521         if (page->cp_owner != NULL) {
522                 LASSERT(page->cp_owner->ci_owned_nr > 0);
523                 page->cp_owner->ci_owned_nr--;
524                 page->cp_owner = NULL;
525                 page->cp_task = NULL;
526         }
527         EXIT;
528 }
529
530 static void cl_page_owner_set(struct cl_page *page)
531 {
532         ENTRY;
533         LASSERT(page->cp_owner != NULL);
534         page->cp_owner->ci_owned_nr++;
535         EXIT;
536 }
537
538 void cl_page_disown0(const struct lu_env *env,
539                      struct cl_io *io, struct cl_page *pg)
540 {
541         enum cl_page_state state;
542
543         ENTRY;
544         state = pg->cp_state;
545         PINVRNT(env, pg, state == CPS_OWNED || state == CPS_FREEING);
546         PINVRNT(env, pg, cl_page_invariant(pg) || state == CPS_FREEING);
547         cl_page_owner_clear(pg);
548
549         if (state == CPS_OWNED)
550                 cl_page_state_set(env, pg, CPS_CACHED);
551         /*
552          * Completion call-backs are executed in the bottom-up order, so that
553          * uppermost layer (llite), responsible for VFS/VM interaction runs
554          * last and can release locks safely.
555          */
556         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_disown),
557                                (const struct lu_env *,
558                                 const struct cl_page_slice *, struct cl_io *),
559                                io);
560         EXIT;
561 }
562
563 /**
564  * returns true, iff page is owned by the given io.
565  */
566 int cl_page_is_owned(const struct cl_page *pg, const struct cl_io *io)
567 {
568         struct cl_io *top = cl_io_top((struct cl_io *)io);
569         LINVRNT(cl_object_same(pg->cp_obj, io->ci_obj));
570         ENTRY;
571         RETURN(pg->cp_state == CPS_OWNED && pg->cp_owner == top);
572 }
573 EXPORT_SYMBOL(cl_page_is_owned);
574
575 /**
576  * Try to own a page by IO.
577  *
578  * Waits until page is in cl_page_state::CPS_CACHED state, and then switch it
579  * into cl_page_state::CPS_OWNED state.
580  *
581  * \pre  !cl_page_is_owned(pg, io)
582  * \post result == 0 iff cl_page_is_owned(pg, io)
583  *
584  * \retval 0   success
585  *
586  * \retval -ve failure, e.g., page was destroyed (and landed in
587  *             cl_page_state::CPS_FREEING instead of cl_page_state::CPS_CACHED).
588  *             or, page was owned by another thread, or in IO.
589  *
590  * \see cl_page_disown()
591  * \see cl_page_operations::cpo_own()
592  * \see cl_page_own_try()
593  * \see cl_page_own
594  */
595 static int cl_page_own0(const struct lu_env *env, struct cl_io *io,
596                         struct cl_page *pg, int nonblock)
597 {
598         int result;
599
600         PINVRNT(env, pg, !cl_page_is_owned(pg, io));
601
602         ENTRY;
603         io = cl_io_top(io);
604
605         if (pg->cp_state == CPS_FREEING) {
606                 result = -ENOENT;
607         } else {
608                 result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(cpo_own),
609                                         (const struct lu_env *,
610                                          const struct cl_page_slice *,
611                                          struct cl_io *, int),
612                                         io, nonblock);
613                 if (result == 0) {
614                         PASSERT(env, pg, pg->cp_owner == NULL);
615                         PASSERT(env, pg, pg->cp_req == NULL);
616                         pg->cp_owner = cl_io_top(io);;
617                         pg->cp_task  = current;
618                         cl_page_owner_set(pg);
619                         if (pg->cp_state != CPS_FREEING) {
620                                 cl_page_state_set(env, pg, CPS_OWNED);
621                         } else {
622                                 cl_page_disown0(env, io, pg);
623                                 result = -ENOENT;
624                         }
625                 }
626         }
627         PINVRNT(env, pg, ergo(result == 0, cl_page_invariant(pg)));
628         RETURN(result);
629 }
630
631 /**
632  * Own a page, might be blocked.
633  *
634  * \see cl_page_own0()
635  */
636 int cl_page_own(const struct lu_env *env, struct cl_io *io, struct cl_page *pg)
637 {
638         return cl_page_own0(env, io, pg, 0);
639 }
640 EXPORT_SYMBOL(cl_page_own);
641
642 /**
643  * Nonblock version of cl_page_own().
644  *
645  * \see cl_page_own0()
646  */
647 int cl_page_own_try(const struct lu_env *env, struct cl_io *io,
648                     struct cl_page *pg)
649 {
650         return cl_page_own0(env, io, pg, 1);
651 }
652 EXPORT_SYMBOL(cl_page_own_try);
653
654
655 /**
656  * Assume page ownership.
657  *
658  * Called when page is already locked by the hosting VM.
659  *
660  * \pre !cl_page_is_owned(pg, io)
661  * \post cl_page_is_owned(pg, io)
662  *
663  * \see cl_page_operations::cpo_assume()
664  */
665 void cl_page_assume(const struct lu_env *env,
666                     struct cl_io *io, struct cl_page *pg)
667 {
668         PINVRNT(env, pg, cl_object_same(pg->cp_obj, io->ci_obj));
669
670         ENTRY;
671         io = cl_io_top(io);
672
673         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_assume));
674         PASSERT(env, pg, pg->cp_owner == NULL);
675         pg->cp_owner = cl_io_top(io);
676         pg->cp_task = current;
677         cl_page_owner_set(pg);
678         cl_page_state_set(env, pg, CPS_OWNED);
679         EXIT;
680 }
681 EXPORT_SYMBOL(cl_page_assume);
682
683 /**
684  * Releases page ownership without unlocking the page.
685  *
686  * Moves page into cl_page_state::CPS_CACHED without releasing a lock on the
687  * underlying VM page (as VM is supposed to do this itself).
688  *
689  * \pre   cl_page_is_owned(pg, io)
690  * \post !cl_page_is_owned(pg, io)
691  *
692  * \see cl_page_assume()
693  */
694 void cl_page_unassume(const struct lu_env *env,
695                       struct cl_io *io, struct cl_page *pg)
696 {
697         PINVRNT(env, pg, cl_page_is_owned(pg, io));
698         PINVRNT(env, pg, cl_page_invariant(pg));
699
700         ENTRY;
701         io = cl_io_top(io);
702         cl_page_owner_clear(pg);
703         cl_page_state_set(env, pg, CPS_CACHED);
704         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_unassume),
705                                (const struct lu_env *,
706                                 const struct cl_page_slice *, struct cl_io *),
707                                io);
708         EXIT;
709 }
710 EXPORT_SYMBOL(cl_page_unassume);
711
712 /**
713  * Releases page ownership.
714  *
715  * Moves page into cl_page_state::CPS_CACHED.
716  *
717  * \pre   cl_page_is_owned(pg, io)
718  * \post !cl_page_is_owned(pg, io)
719  *
720  * \see cl_page_own()
721  * \see cl_page_operations::cpo_disown()
722  */
723 void cl_page_disown(const struct lu_env *env,
724                     struct cl_io *io, struct cl_page *pg)
725 {
726         PINVRNT(env, pg, cl_page_is_owned(pg, io) ||
727                 pg->cp_state == CPS_FREEING);
728
729         ENTRY;
730         io = cl_io_top(io);
731         cl_page_disown0(env, io, pg);
732         EXIT;
733 }
734 EXPORT_SYMBOL(cl_page_disown);
735
736 /**
737  * Called when page is to be removed from the object, e.g., as a result of
738  * truncate.
739  *
740  * Calls cl_page_operations::cpo_discard() top-to-bottom.
741  *
742  * \pre cl_page_is_owned(pg, io)
743  *
744  * \see cl_page_operations::cpo_discard()
745  */
746 void cl_page_discard(const struct lu_env *env,
747                      struct cl_io *io, struct cl_page *pg)
748 {
749         PINVRNT(env, pg, cl_page_is_owned(pg, io));
750         PINVRNT(env, pg, cl_page_invariant(pg));
751
752         cl_page_invoid(env, io, pg, CL_PAGE_OP(cpo_discard));
753 }
754 EXPORT_SYMBOL(cl_page_discard);
755
756 /**
757  * Version of cl_page_delete() that can be called for not fully constructed
758  * pages, e.g,. in a error handling cl_page_find()->cl_page_delete0()
759  * path. Doesn't check page invariant.
760  */
761 static void cl_page_delete0(const struct lu_env *env, struct cl_page *pg)
762 {
763         ENTRY;
764
765         PASSERT(env, pg, pg->cp_state != CPS_FREEING);
766
767         /*
768          * Severe all ways to obtain new pointers to @pg.
769          */
770         cl_page_owner_clear(pg);
771
772         cl_page_state_set0(env, pg, CPS_FREEING);
773
774         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(cpo_delete),
775                        (const struct lu_env *, const struct cl_page_slice *));
776
777         EXIT;
778 }
779
780 /**
781  * Called when a decision is made to throw page out of memory.
782  *
783  * Notifies all layers about page destruction by calling
784  * cl_page_operations::cpo_delete() method top-to-bottom.
785  *
786  * Moves page into cl_page_state::CPS_FREEING state (this is the only place
787  * where transition to this state happens).
788  *
789  * Eliminates all venues through which new references to the page can be
790  * obtained:
791  *
792  *     - removes page from the radix trees,
793  *
794  *     - breaks linkage from VM page to cl_page.
795  *
796  * Once page reaches cl_page_state::CPS_FREEING, all remaining references will
797  * drain after some time, at which point page will be recycled.
798  *
799  * \pre  VM page is locked
800  * \post pg->cp_state == CPS_FREEING
801  *
802  * \see cl_page_operations::cpo_delete()
803  */
804 void cl_page_delete(const struct lu_env *env, struct cl_page *pg)
805 {
806         PINVRNT(env, pg, cl_page_invariant(pg));
807         ENTRY;
808         cl_page_delete0(env, pg);
809         EXIT;
810 }
811 EXPORT_SYMBOL(cl_page_delete);
812
813 /**
814  * Marks page up-to-date.
815  *
816  * Call cl_page_operations::cpo_export() through all layers top-to-bottom. The
817  * layer responsible for VM interaction has to mark/clear page as up-to-date
818  * by the \a uptodate argument.
819  *
820  * \see cl_page_operations::cpo_export()
821  */
822 void cl_page_export(const struct lu_env *env, struct cl_page *pg, int uptodate)
823 {
824         PINVRNT(env, pg, cl_page_invariant(pg));
825         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_export),
826                        (const struct lu_env *,
827                         const struct cl_page_slice *, int), uptodate);
828 }
829 EXPORT_SYMBOL(cl_page_export);
830
831 /**
832  * Returns true, iff \a pg is VM locked in a suitable sense by the calling
833  * thread.
834  */
835 int cl_page_is_vmlocked(const struct lu_env *env, const struct cl_page *pg)
836 {
837         int result;
838         const struct cl_page_slice *slice;
839
840         ENTRY;
841         slice = container_of(pg->cp_layers.next,
842                              const struct cl_page_slice, cpl_linkage);
843         PASSERT(env, pg, slice->cpl_ops->cpo_is_vmlocked != NULL);
844         /*
845          * Call ->cpo_is_vmlocked() directly instead of going through
846          * CL_PAGE_INVOKE(), because cl_page_is_vmlocked() is used by
847          * cl_page_invariant().
848          */
849         result = slice->cpl_ops->cpo_is_vmlocked(env, slice);
850         PASSERT(env, pg, result == -EBUSY || result == -ENODATA);
851         RETURN(result == -EBUSY);
852 }
853 EXPORT_SYMBOL(cl_page_is_vmlocked);
854
855 static enum cl_page_state cl_req_type_state(enum cl_req_type crt)
856 {
857         ENTRY;
858         RETURN(crt == CRT_WRITE ? CPS_PAGEOUT : CPS_PAGEIN);
859 }
860
861 static void cl_page_io_start(const struct lu_env *env,
862                              struct cl_page *pg, enum cl_req_type crt)
863 {
864         /*
865          * Page is queued for IO, change its state.
866          */
867         ENTRY;
868         cl_page_owner_clear(pg);
869         cl_page_state_set(env, pg, cl_req_type_state(crt));
870         EXIT;
871 }
872
873 /**
874  * Prepares page for immediate transfer. cl_page_operations::cpo_prep() is
875  * called top-to-bottom. Every layer either agrees to submit this page (by
876  * returning 0), or requests to omit this page (by returning -EALREADY). Layer
877  * handling interactions with the VM also has to inform VM that page is under
878  * transfer now.
879  */
880 int cl_page_prep(const struct lu_env *env, struct cl_io *io,
881                  struct cl_page *pg, enum cl_req_type crt)
882 {
883         int result;
884
885         PINVRNT(env, pg, cl_page_is_owned(pg, io));
886         PINVRNT(env, pg, cl_page_invariant(pg));
887         PINVRNT(env, pg, crt < CRT_NR);
888
889         /*
890          * XXX this has to be called bottom-to-top, so that llite can set up
891          * PG_writeback without risking other layers deciding to skip this
892          * page.
893          */
894         if (crt >= CRT_NR)
895                 return -EINVAL;
896         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(io[crt].cpo_prep));
897         if (result == 0)
898                 cl_page_io_start(env, pg, crt);
899
900         KLASSERT(ergo(crt == CRT_WRITE && pg->cp_type == CPT_CACHEABLE,
901                       equi(result == 0,
902                            PageWriteback(cl_page_vmpage(pg)))));
903         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
904         return result;
905 }
906 EXPORT_SYMBOL(cl_page_prep);
907
908 /**
909  * Notify layers about transfer completion.
910  *
911  * Invoked by transfer sub-system (which is a part of osc) to notify layers
912  * that a transfer, of which this page is a part of has completed.
913  *
914  * Completion call-backs are executed in the bottom-up order, so that
915  * uppermost layer (llite), responsible for the VFS/VM interaction runs last
916  * and can release locks safely.
917  *
918  * \pre  pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
919  * \post pg->cp_state == CPS_CACHED
920  *
921  * \see cl_page_operations::cpo_completion()
922  */
923 void cl_page_completion(const struct lu_env *env,
924                         struct cl_page *pg, enum cl_req_type crt, int ioret)
925 {
926         struct cl_sync_io *anchor = pg->cp_sync_io;
927
928         PASSERT(env, pg, crt < CRT_NR);
929         /* cl_page::cp_req already cleared by the caller (osc_completion()) */
930         PASSERT(env, pg, pg->cp_req == NULL);
931         PASSERT(env, pg, pg->cp_state == cl_req_type_state(crt));
932
933         ENTRY;
934         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, ioret);
935         if (crt == CRT_READ && ioret == 0) {
936                 PASSERT(env, pg, !(pg->cp_flags & CPF_READ_COMPLETED));
937                 pg->cp_flags |= CPF_READ_COMPLETED;
938         }
939
940         cl_page_state_set(env, pg, CPS_CACHED);
941         if (crt >= CRT_NR)
942                 return;
943         CL_PAGE_INVOID_REVERSE(env, pg, CL_PAGE_OP(io[crt].cpo_completion),
944                                (const struct lu_env *,
945                                 const struct cl_page_slice *, int), ioret);
946         if (anchor) {
947                 LASSERT(cl_page_is_vmlocked(env, pg));
948                 LASSERT(pg->cp_sync_io == anchor);
949                 pg->cp_sync_io = NULL;
950         }
951         /*
952          * As page->cp_obj is pinned by a reference from page->cp_req, it is
953          * safe to call cl_page_put() without risking object destruction in a
954          * non-blocking context.
955          */
956         cl_page_put(env, pg);
957
958         if (anchor)
959                 cl_sync_io_note(anchor, ioret);
960
961         EXIT;
962 }
963 EXPORT_SYMBOL(cl_page_completion);
964
965 /**
966  * Notify layers that transfer formation engine decided to yank this page from
967  * the cache and to make it a part of a transfer.
968  *
969  * \pre  pg->cp_state == CPS_CACHED
970  * \post pg->cp_state == CPS_PAGEIN || pg->cp_state == CPS_PAGEOUT
971  *
972  * \see cl_page_operations::cpo_make_ready()
973  */
974 int cl_page_make_ready(const struct lu_env *env, struct cl_page *pg,
975                        enum cl_req_type crt)
976 {
977         int result;
978
979         PINVRNT(env, pg, crt < CRT_NR);
980
981         ENTRY;
982         if (crt >= CRT_NR)
983                 RETURN(-EINVAL);
984         result = CL_PAGE_INVOKE(env, pg, CL_PAGE_OP(io[crt].cpo_make_ready),
985                                 (const struct lu_env *,
986                                  const struct cl_page_slice *));
987         if (result == 0) {
988                 PASSERT(env, pg, pg->cp_state == CPS_CACHED);
989                 cl_page_io_start(env, pg, crt);
990         }
991         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", crt, result);
992         RETURN(result);
993 }
994 EXPORT_SYMBOL(cl_page_make_ready);
995
996 /**
997  * Called if a pge is being written back by kernel's intention.
998  *
999  * \pre  cl_page_is_owned(pg, io)
1000  * \post ergo(result == 0, pg->cp_state == CPS_PAGEOUT)
1001  *
1002  * \see cl_page_operations::cpo_flush()
1003  */
1004 int cl_page_flush(const struct lu_env *env, struct cl_io *io,
1005                   struct cl_page *pg)
1006 {
1007         int result;
1008
1009         PINVRNT(env, pg, cl_page_is_owned(pg, io));
1010         PINVRNT(env, pg, cl_page_invariant(pg));
1011
1012         ENTRY;
1013
1014         result = cl_page_invoke(env, io, pg, CL_PAGE_OP(cpo_flush));
1015
1016         CL_PAGE_HEADER(D_TRACE, env, pg, "%d\n", result);
1017         RETURN(result);
1018 }
1019 EXPORT_SYMBOL(cl_page_flush);
1020
1021 /**
1022  * Checks whether page is protected by any extent lock is at least required
1023  * mode.
1024  *
1025  * \return the same as in cl_page_operations::cpo_is_under_lock() method.
1026  * \see cl_page_operations::cpo_is_under_lock()
1027  */
1028 int cl_page_is_under_lock(const struct lu_env *env, struct cl_io *io,
1029                           struct cl_page *page)
1030 {
1031         int rc;
1032
1033         PINVRNT(env, page, cl_page_invariant(page));
1034
1035         ENTRY;
1036         rc = CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_is_under_lock),
1037                             (const struct lu_env *,
1038                              const struct cl_page_slice *, struct cl_io *),
1039                             io);
1040         PASSERT(env, page, rc != 0);
1041         RETURN(rc);
1042 }
1043 EXPORT_SYMBOL(cl_page_is_under_lock);
1044
1045 /**
1046  * Tells transfer engine that only part of a page is to be transmitted.
1047  *
1048  * \see cl_page_operations::cpo_clip()
1049  */
1050 void cl_page_clip(const struct lu_env *env, struct cl_page *pg,
1051                   int from, int to)
1052 {
1053         PINVRNT(env, pg, cl_page_invariant(pg));
1054
1055         CL_PAGE_HEADER(D_TRACE, env, pg, "%d %d\n", from, to);
1056         CL_PAGE_INVOID(env, pg, CL_PAGE_OP(cpo_clip),
1057                        (const struct lu_env *,
1058                         const struct cl_page_slice *,int, int),
1059                        from, to);
1060 }
1061 EXPORT_SYMBOL(cl_page_clip);
1062
1063 /**
1064  * Prints human readable representation of \a pg to the \a f.
1065  */
1066 void cl_page_header_print(const struct lu_env *env, void *cookie,
1067                           lu_printer_t printer, const struct cl_page *pg)
1068 {
1069         (*printer)(env, cookie,
1070                    "page@%p[%d %p %d %d %d %p %p %#x]\n",
1071                    pg, cfs_atomic_read(&pg->cp_ref), pg->cp_obj,
1072                    pg->cp_state, pg->cp_error, pg->cp_type,
1073                    pg->cp_owner, pg->cp_req, pg->cp_flags);
1074 }
1075 EXPORT_SYMBOL(cl_page_header_print);
1076
1077 /**
1078  * Prints human readable representation of \a pg to the \a f.
1079  */
1080 void cl_page_print(const struct lu_env *env, void *cookie,
1081                    lu_printer_t printer, const struct cl_page *pg)
1082 {
1083         cl_page_header_print(env, cookie, printer, pg);
1084         CL_PAGE_INVOKE(env, (struct cl_page *)pg, CL_PAGE_OP(cpo_print),
1085                        (const struct lu_env *env,
1086                         const struct cl_page_slice *slice,
1087                         void *cookie, lu_printer_t p), cookie, printer);
1088         (*printer)(env, cookie, "end page@%p\n", pg);
1089 }
1090 EXPORT_SYMBOL(cl_page_print);
1091
1092 /**
1093  * Cancel a page which is still in a transfer.
1094  */
1095 int cl_page_cancel(const struct lu_env *env, struct cl_page *page)
1096 {
1097         return CL_PAGE_INVOKE(env, page, CL_PAGE_OP(cpo_cancel),
1098                               (const struct lu_env *,
1099                                const struct cl_page_slice *));
1100 }
1101 EXPORT_SYMBOL(cl_page_cancel);
1102
1103 /**
1104  * Converts a byte offset within object \a obj into a page index.
1105  */
1106 loff_t cl_offset(const struct cl_object *obj, pgoff_t idx)
1107 {
1108         return (loff_t)idx << PAGE_CACHE_SHIFT;
1109 }
1110 EXPORT_SYMBOL(cl_offset);
1111
1112 /**
1113  * Converts a page index into a byte offset within object \a obj.
1114  */
1115 pgoff_t cl_index(const struct cl_object *obj, loff_t offset)
1116 {
1117         return offset >> PAGE_CACHE_SHIFT;
1118 }
1119 EXPORT_SYMBOL(cl_index);
1120
1121 int cl_page_size(const struct cl_object *obj)
1122 {
1123         return 1 << PAGE_CACHE_SHIFT;
1124 }
1125 EXPORT_SYMBOL(cl_page_size);
1126
1127 /**
1128  * Adds page slice to the compound page.
1129  *
1130  * This is called by cl_object_operations::coo_page_init() methods to add a
1131  * per-layer state to the page. New state is added at the end of
1132  * cl_page::cp_layers list, that is, it is at the bottom of the stack.
1133  *
1134  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_io_slice_add()
1135  */
1136 void cl_page_slice_add(struct cl_page *page, struct cl_page_slice *slice,
1137                        struct cl_object *obj,
1138                        const struct cl_page_operations *ops)
1139 {
1140         ENTRY;
1141         cfs_list_add_tail(&slice->cpl_linkage, &page->cp_layers);
1142         slice->cpl_obj  = obj;
1143         slice->cpl_ops  = ops;
1144         slice->cpl_page = page;
1145         EXIT;
1146 }
1147 EXPORT_SYMBOL(cl_page_slice_add);
1148
1149 int  cl_page_init(void)
1150 {
1151         return 0;
1152 }
1153
1154 void cl_page_fini(void)
1155 {
1156 }