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