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