Whamcloud - gitweb
LU-19098 hsm: don't print progname twice with lhsmtool
[fs/lustre-release.git] / lustre / obdclass / cl_object.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2011, 2017, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  *
13  * Client Lustre Object.
14  *
15  * Author: Nikita Danilov <nikita.danilov@sun.com>
16  * Author: Jinshan Xiong <jinshan.xiong@intel.com>
17  */
18
19 /*
20  * Locking.
21  *
22  *  i_mutex
23  *      PG_locked
24  *          ->coh_attr_guard
25  *          ->ls_guard
26  */
27
28 #define DEBUG_SUBSYSTEM S_CLASS
29
30 #include <linux/list.h>
31 #include <libcfs/libcfs.h>
32 #include <obd_class.h>
33 #include <obd_support.h>
34 #include <lustre_fid.h>
35 #include <cl_object.h>
36 #include <lu_object.h>
37 #include "cl_internal.h"
38
39 static struct kmem_cache *cl_env_kmem;
40 struct kmem_cache *cl_dio_aio_kmem;
41 struct kmem_cache *cl_sub_dio_kmem;
42 struct kmem_cache *cl_page_kmem_array[16];
43 unsigned short cl_page_kmem_size_array[16];
44
45 /** Lock class of cl_object_header::coh_attr_guard */
46 static struct lock_class_key cl_attr_guard_class;
47
48 /**
49  * Initialize cl_object_header.
50  */
51 int cl_object_header_init(struct cl_object_header *h)
52 {
53         int result;
54
55         ENTRY;
56         result = lu_object_header_init(&h->coh_lu);
57         if (result == 0) {
58                 spin_lock_init(&h->coh_attr_guard);
59                 lockdep_set_class(&h->coh_attr_guard, &cl_attr_guard_class);
60                 h->coh_page_bufsize = 0;
61         }
62         RETURN(result);
63 }
64 EXPORT_SYMBOL(cl_object_header_init);
65
66 /**
67  * Finalize cl_object_header.
68  */
69 void cl_object_header_fini(struct cl_object_header *h)
70 {
71         lu_object_header_fini(&h->coh_lu);
72 }
73
74 /**
75  * Returns a cl_object with a given \a fid.
76  *
77  * Returns either cached or newly created object. Additional reference on the
78  * returned object is acquired.
79  *
80  * \see lu_object_find(), cl_page_find(), cl_lock_find()
81  */
82 struct cl_object *cl_object_find(const struct lu_env *env,
83                                  struct cl_device *cd, const struct lu_fid *fid,
84                                  const struct cl_object_conf *c)
85 {
86         might_sleep();
87         return lu2cl(lu_object_find_slice(env, cl2lu_dev(cd), fid, &c->coc_lu));
88 }
89 EXPORT_SYMBOL(cl_object_find);
90
91 /**
92  * Releases a reference on \a o.
93  *
94  * When last reference is released object is returned to the cache, unless
95  * lu_object_header_flags::LU_OBJECT_HEARD_BANSHEE bit is set in its header.
96  *
97  * \see cl_page_put(), cl_lock_put().
98  */
99 void cl_object_put(const struct lu_env *env, struct cl_object *o)
100 {
101         lu_object_put(env, &o->co_lu);
102 }
103 EXPORT_SYMBOL(cl_object_put);
104
105 /**
106  * Acquire an additional reference to the object \a o.
107  *
108  * This can only be used to acquire _additional_ reference, i.e., caller
109  * already has to possess at least one reference to \a o before calling this.
110  *
111  * \see cl_page_get(), cl_lock_get().
112  */
113 void cl_object_get(struct cl_object *o)
114 {
115         lu_object_get(&o->co_lu);
116 }
117 EXPORT_SYMBOL(cl_object_get);
118
119 /**
120  * Returns the top-object for a given \a o.
121  *
122  * \see cl_io_top()
123  */
124 struct cl_object *cl_object_top(struct cl_object *o)
125 {
126         struct cl_object_header *hdr = cl_object_header(o);
127         struct cl_object *top;
128
129         while (hdr->coh_parent != NULL)
130                 hdr = hdr->coh_parent;
131
132         top = lu2cl(lu_object_top(&hdr->coh_lu));
133         CDEBUG(D_TRACE, "%p -> %p\n", o, top);
134         return top;
135 }
136 EXPORT_SYMBOL(cl_object_top);
137
138 /**
139  * Returns pointer to the lock protecting data-attributes for the given object
140  * \a o.
141  *
142  * Data-attributes are protected by the cl_object_header::coh_attr_guard
143  * spin-lock in the top-object.
144  *
145  * \see cl_attr, cl_object_attr_lock(), cl_object_operations::coo_attr_get().
146  */
147 static spinlock_t *cl_object_attr_guard(struct cl_object *o)
148 {
149         return &cl_object_header(cl_object_top(o))->coh_attr_guard;
150 }
151
152 /**
153  * Locks data-attributes.
154  *
155  * Prevents data-attributes from changing, until lock is released by
156  * cl_object_attr_unlock(). This has to be called before calls to
157  * cl_object_attr_get(), cl_object_attr_update().
158  */
159 void cl_object_attr_lock(struct cl_object *o)
160 __acquires(cl_object_attr_guard(o))
161 {
162         spin_lock(cl_object_attr_guard(o));
163 }
164 EXPORT_SYMBOL(cl_object_attr_lock);
165
166 /**
167  * Releases data-attributes lock, acquired by cl_object_attr_lock().
168  */
169 void cl_object_attr_unlock(struct cl_object *o)
170 __releases(cl_object_attr_guard(o))
171 {
172         spin_unlock(cl_object_attr_guard(o));
173 }
174 EXPORT_SYMBOL(cl_object_attr_unlock);
175
176 /**
177  * Returns data-attributes of an object \a obj.
178  *
179  * Every layer is asked (by calling cl_object_operations::coo_attr_get())
180  * top-to-bottom to fill in parts of \a attr that this layer is responsible
181  * for.
182  */
183 int cl_object_attr_get(const struct lu_env *env, struct cl_object *top,
184                         struct cl_attr *attr)
185 {
186         struct cl_object *obj;
187         int result = 0;
188
189         assert_spin_locked(cl_object_attr_guard(top));
190         ENTRY;
191
192         cl_object_for_each(obj, top) {
193                 if (obj->co_ops->coo_attr_get != NULL) {
194                         result = obj->co_ops->coo_attr_get(env, obj, attr);
195                         if (result != 0) {
196                                 if (result > 0)
197                                         result = 0;
198                                 break;
199                         }
200                 }
201         }
202         RETURN(result);
203 }
204 EXPORT_SYMBOL(cl_object_attr_get);
205
206 /**
207  * Updates data-attributes of an object \a obj.
208  *
209  * Only attributes, mentioned in a validness bit-mask \a v are
210  * updated. Calls cl_object_operations::coo_upd_attr() on every layer, bottom
211  * to top.
212  */
213 int cl_object_attr_update(const struct lu_env *env, struct cl_object *top,
214                           const struct cl_attr *attr, enum cl_attr_valid v)
215 {
216         struct cl_object *obj;
217         int result = 0;
218
219         assert_spin_locked(cl_object_attr_guard(top));
220         ENTRY;
221
222         cl_object_for_each_reverse(obj, top) {
223                 if (obj->co_ops->coo_attr_update != NULL) {
224                         result = obj->co_ops->coo_attr_update(env, obj, attr,
225                                                               v);
226                         if (result != 0) {
227                                 if (result > 0)
228                                         result = 0;
229                                 break;
230                         }
231                 }
232         }
233         RETURN(result);
234 }
235 EXPORT_SYMBOL(cl_object_attr_update);
236
237 /**
238  * Mark the inode as dirty when the inode has uncommitted (unstable) pages.
239  * Thus when the system is under momory pressure, it will trigger writeback
240  * on background to commit and unpin the pages.
241  */
242 void cl_object_dirty_for_sync(const struct lu_env *env, struct cl_object *top)
243 {
244         struct cl_object *obj;
245
246         ENTRY;
247
248         cl_object_for_each(obj, top) {
249                 if (obj->co_ops->coo_dirty_for_sync != NULL)
250                         obj->co_ops->coo_dirty_for_sync(env, obj);
251         }
252         EXIT;
253 }
254 EXPORT_SYMBOL(cl_object_dirty_for_sync);
255
256 /**
257  * Notifies layers (bottom-to-top) that glimpse AST was received.
258  *
259  * Layers have to fill \a lvb fields with information that will be shipped
260  * back to glimpse issuer.
261  *
262  * \see cl_lock_operations::clo_glimpse()
263  */
264 int cl_object_glimpse(const struct lu_env *env, struct cl_object *top,
265                       struct ost_lvb *lvb)
266 {
267         struct cl_object *obj;
268         int result = 0;
269
270         ENTRY;
271         cl_object_for_each_reverse(obj, top) {
272                 if (obj->co_ops->coo_glimpse != NULL) {
273                         result = obj->co_ops->coo_glimpse(env, obj, lvb);
274                         if (result != 0)
275                                 break;
276                 }
277         }
278         LU_OBJECT_HEADER(D_DLMTRACE, env, lu_object_top(top->co_lu.lo_header),
279                          "size: %llu mtime: %llu atime: %llu "
280                          "ctime: %llu blocks: %llu\n",
281                          lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
282                          lvb->lvb_ctime, lvb->lvb_blocks);
283         RETURN(result);
284 }
285 EXPORT_SYMBOL(cl_object_glimpse);
286
287 /**
288  * Updates a configuration of an object \a obj.
289  */
290 int cl_conf_set(const struct lu_env *env, struct cl_object *top,
291                 const struct cl_object_conf *conf)
292 {
293         struct cl_object *obj;
294         int result = 0;
295
296         ENTRY;
297         cl_object_for_each(obj, top) {
298                 if (obj->co_ops->coo_conf_set != NULL) {
299                         result = obj->co_ops->coo_conf_set(env, obj, conf);
300                         if (result)
301                                 break;
302                 }
303         }
304         RETURN(result);
305 }
306 EXPORT_SYMBOL(cl_conf_set);
307
308 /**
309  * Prunes caches of pages and locks for this object.
310  */
311 int cl_object_prune(const struct lu_env *env, struct cl_object *top)
312 {
313         struct cl_object *obj;
314         int result = 0;
315         ENTRY;
316
317         cl_object_for_each(obj, top) {
318                 if (obj->co_ops->coo_prune != NULL) {
319                         result = obj->co_ops->coo_prune(env, obj);
320                         if (result)
321                                 break;
322                 }
323         }
324
325         RETURN(result);
326 }
327 EXPORT_SYMBOL(cl_object_prune);
328
329 /**
330  * Get stripe information of this object.
331  */
332 int cl_object_getstripe(const struct lu_env *env, struct cl_object *top,
333                         struct lov_user_md __user *uarg, size_t size)
334 {
335         struct cl_object *obj;
336         int result = 0;
337         ENTRY;
338
339         cl_object_for_each(obj, top) {
340                 if (obj->co_ops->coo_getstripe) {
341                         result = obj->co_ops->coo_getstripe(env, obj, uarg,
342                                                             size);
343                         if (result)
344                                 break;
345                 }
346         }
347         RETURN(result);
348 }
349 EXPORT_SYMBOL(cl_object_getstripe);
350
351 /**
352  * Get fiemap extents from file object.
353  *
354  * \param env [in]      lustre environment
355  * \param obj [in]      file object
356  * \param key [in]      fiemap request argument
357  * \param fiemap [out]  fiemap extents mapping retrived
358  * \param buflen [in]   max buffer length of @fiemap
359  *
360  * \retval 0    success
361  * \retval < 0  error
362  */
363 int cl_object_fiemap(const struct lu_env *env, struct cl_object *top,
364                      struct ll_fiemap_info_key *key,
365                      struct fiemap *fiemap, size_t *buflen)
366 {
367         struct cl_object *obj;
368         int result = 0;
369         ENTRY;
370
371         cl_object_for_each(obj, top) {
372                 if (obj->co_ops->coo_fiemap) {
373                         result = obj->co_ops->coo_fiemap(env, obj, key, fiemap,
374                                                          buflen);
375                         if (result)
376                                 break;
377                 }
378         }
379         RETURN(result);
380 }
381 EXPORT_SYMBOL(cl_object_fiemap);
382
383 int cl_object_layout_get(const struct lu_env *env, struct cl_object *top,
384                          struct cl_layout *cl)
385 {
386         struct cl_object *obj;
387         ENTRY;
388
389         cl_object_for_each(obj, top) {
390                 if (obj->co_ops->coo_layout_get)
391                         return obj->co_ops->coo_layout_get(env, obj, cl);
392         }
393
394         RETURN(-EOPNOTSUPP);
395 }
396 EXPORT_SYMBOL(cl_object_layout_get);
397
398 loff_t cl_object_maxbytes(struct cl_object *top)
399 {
400         struct cl_object *obj;
401         loff_t maxbytes = LLONG_MAX;
402         ENTRY;
403
404         cl_object_for_each(obj, top) {
405                 if (obj->co_ops->coo_maxbytes)
406                         maxbytes = min_t(loff_t, obj->co_ops->coo_maxbytes(obj),
407                                          maxbytes);
408         }
409
410         RETURN(maxbytes);
411 }
412 EXPORT_SYMBOL(cl_object_maxbytes);
413
414 int cl_object_flush(const struct lu_env *env, struct cl_object *top,
415                          struct ldlm_lock *lock)
416 {
417         struct cl_object *obj;
418         int rc = 0;
419         ENTRY;
420
421         cl_object_for_each(obj, top) {
422                 if (obj->co_ops->coo_object_flush) {
423                         rc = obj->co_ops->coo_object_flush(env, obj, lock);
424                         if (rc)
425                                 break;
426                 }
427         }
428         RETURN(rc);
429 }
430 EXPORT_SYMBOL(cl_object_flush);
431
432 int cl_object_inode_ops(const struct lu_env *env, struct cl_object *top,
433                         enum coo_inode_opc opc, void *data)
434 {
435         struct cl_object *obj;
436         int rc = 0;
437
438         ENTRY;
439
440         cl_object_for_each(obj, top) {
441                 if (obj->co_ops->coo_inode_ops) {
442                         rc = obj->co_ops->coo_inode_ops(env, obj, opc, data);
443                         if (rc)
444                                 break;
445                 }
446         }
447         RETURN(rc);
448 }
449 EXPORT_SYMBOL(cl_object_inode_ops);
450
451 void cl_req_projid_set(const struct lu_env *env, struct cl_object *top,
452                        __u32 *projid)
453 {
454         struct cl_object *obj;
455
456         ENTRY;
457
458         cl_object_for_each(obj, top) {
459                 if (obj->co_ops->coo_req_projid_set)
460                         obj->co_ops->coo_req_projid_set(env, obj, projid);
461         }
462         EXIT;
463 }
464 EXPORT_SYMBOL(cl_req_projid_set);
465
466 /**
467  * Helper function removing all object locks, and marking object for
468  * deletion. All object pages must have been deleted at this point.
469  *
470  * This is called by cl_inode_fini() and lov_object_delete() to destroy top-
471  * and sub- objects respectively.
472  */
473 void cl_object_kill(const struct lu_env *env, struct cl_object *obj)
474 {
475         struct cl_object_header *hdr = cl_object_header(obj);
476
477         set_bit(LU_OBJECT_HEARD_BANSHEE, &hdr->coh_lu.loh_flags);
478 }
479 EXPORT_SYMBOL(cl_object_kill);
480
481 void cache_stats_init(struct cache_stats *cs, const char *name)
482 {
483         int i;
484
485         cs->cs_name = name;
486         for (i = 0; i < CS_NR; i++)
487                 atomic_set(&cs->cs_stats[i], 0);
488 }
489
490 static int cache_stats_print(const struct cache_stats *cs,
491                              struct seq_file *m, int h)
492 {
493         int i;
494
495         /*
496          *   lookup    hit    total  cached create
497          * env: ...... ...... ...... ...... ......
498          */
499         if (h) {
500                 const char *names[CS_NR] = CS_NAMES;
501
502                 seq_printf(m, "%6s", " ");
503                 for (i = 0; i < CS_NR; i++)
504                         seq_printf(m, "%8s", names[i]);
505                 seq_printf(m, "\n");
506         }
507
508         seq_printf(m, "%5.5s:", cs->cs_name);
509         for (i = 0; i < CS_NR; i++)
510                 seq_printf(m, "%8u", atomic_read(&cs->cs_stats[i]));
511         return 0;
512 }
513
514 static void cl_env_percpu_refill(void);
515
516 /**
517  * Initialize client site.
518  *
519  * Perform common initialization (lu_site_init()), and initialize statistical
520  * counters. Also perform global initializations on the first call.
521  */
522 int cl_site_init(struct cl_site *s, struct cl_device *d)
523 {
524         size_t i;
525         int result;
526
527         result = lu_site_init(&s->cs_lu, &d->cd_lu_dev);
528         if (result == 0) {
529                 cache_stats_init(&s->cs_pages, "pages");
530                 for (i = 0; i < ARRAY_SIZE(s->cs_pages_state); ++i)
531                         atomic_set(&s->cs_pages_state[0], 0);
532                 cl_env_percpu_refill();
533         }
534         return result;
535 }
536 EXPORT_SYMBOL(cl_site_init);
537
538 /**
539  * Finalize client site. Dual to cl_site_init().
540  */
541 void cl_site_fini(struct cl_site *s)
542 {
543         lu_site_fini(&s->cs_lu);
544 }
545 EXPORT_SYMBOL(cl_site_fini);
546
547 static struct cache_stats cl_env_stats = {
548         .cs_name    = "envs",
549         .cs_stats = { ATOMIC_INIT(0), }
550 };
551
552 /**
553  * Outputs client site statistical counters into a buffer. Suitable for
554  * ll_rd_*()-style functions.
555  */
556 int cl_site_stats_print(const struct cl_site *site, struct seq_file *m)
557 {
558         static const char *const pstate[] = {
559                 [CPS_CACHED]    = "c",
560                 [CPS_OWNED]     = "o",
561                 [CPS_PAGEOUT]   = "w",
562                 [CPS_PAGEIN]    = "r",
563                 [CPS_FREEING]   = "f"
564         };
565         size_t i;
566
567 /*
568        lookup    hit  total   busy create
569 pages: ...... ...... ...... ...... ...... [...... ...... ...... ......]
570 locks: ...... ...... ...... ...... ...... [...... ...... ...... ...... ......]
571   env: ...... ...... ...... ...... ......
572  */
573         lu_site_stats_seq_print(&site->cs_lu, m);
574         cache_stats_print(&site->cs_pages, m, 1);
575         seq_printf(m, " [");
576         for (i = 0; i < ARRAY_SIZE(site->cs_pages_state); ++i)
577                 seq_printf(m, "%s: %u ", pstate[i],
578                            atomic_read(&site->cs_pages_state[i]));
579         seq_printf(m, "]\n");
580         cache_stats_print(&cl_env_stats, m, 0);
581         seq_printf(m, "\n");
582         return 0;
583 }
584 EXPORT_SYMBOL(cl_site_stats_print);
585
586 /*****************************************************************************
587  *
588  * lu_env handling on client.
589  *
590  */
591
592 static unsigned cl_envs_cached_max = 32; /* XXX: prototype: arbitrary limit
593                                           * for now. */
594 static struct cl_env_cache {
595         rwlock_t                cec_guard;
596         unsigned                cec_count;
597         struct list_head        cec_envs;
598 } *cl_envs = NULL;
599
600 struct cl_env {
601         void             *ce_magic;
602         struct lu_env     ce_lu;
603         struct lu_context ce_ses;
604
605         /*
606          * Linkage into global list of all client environments. Used for
607          * garbage collection.
608          */
609         struct list_head  ce_linkage;
610         /*
611          *
612          */
613         int               ce_ref;
614         /*
615          * Debugging field: address of the caller who made original
616          * allocation.
617          */
618         void             *ce_debug;
619 };
620
621 static void cl_env_inc(enum cache_stats_item item)
622 {
623 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
624         atomic_inc(&cl_env_stats.cs_stats[item]);
625 #endif
626 }
627
628 static void cl_env_dec(enum cache_stats_item item)
629 {
630 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
631         LASSERT(atomic_read(&cl_env_stats.cs_stats[item]) > 0);
632         atomic_dec(&cl_env_stats.cs_stats[item]);
633 #endif
634 }
635
636 static void cl_env_init0(struct cl_env *cle, void *debug)
637 {
638         LASSERT(cle->ce_ref == 0);
639         LASSERT(cle->ce_magic == &cl_env_init0);
640         LASSERT(cle->ce_debug == NULL);
641
642         cle->ce_ref = 1;
643         cle->ce_debug = debug;
644         cl_env_inc(CS_busy);
645 }
646
647 static struct lu_env *cl_env_new(__u32 ctx_tags, __u32 ses_tags, void *debug)
648 {
649         struct lu_env *env;
650         struct cl_env *cle;
651
652         OBD_SLAB_ALLOC_PTR_GFP(cle, cl_env_kmem, GFP_NOFS);
653         if (cle != NULL) {
654                 int rc;
655
656                 INIT_LIST_HEAD(&cle->ce_linkage);
657                 cle->ce_magic = &cl_env_init0;
658                 env = &cle->ce_lu;
659                 rc = lu_env_init(env, LCT_CL_THREAD|ctx_tags);
660                 if (rc == 0) {
661                         rc = lu_context_init(&cle->ce_ses,
662                                              LCT_SESSION | ses_tags);
663                         if (rc == 0) {
664                                 lu_context_enter(&cle->ce_ses);
665                                 env->le_ses = &cle->ce_ses;
666                                 cl_env_init0(cle, debug);
667                         } else
668                                 lu_env_fini(env);
669                 }
670                 if (rc != 0) {
671                         OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
672                         env = ERR_PTR(rc);
673                 } else {
674                         cl_env_inc(CS_create);
675                         cl_env_inc(CS_total);
676                 }
677         } else
678                 env = ERR_PTR(-ENOMEM);
679         return env;
680 }
681
682 static void cl_env_fini(struct cl_env *cle)
683 {
684         cl_env_dec(CS_total);
685         lu_context_fini(&cle->ce_lu.le_ctx);
686         lu_context_fini(&cle->ce_ses);
687         OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
688 }
689
690 /* Get a cl_env, either from the per-CPU cache for the current CPU, or by
691  * allocating a new one.
692  */
693 static struct lu_env *cl_env_obtain(void *debug)
694 {
695         struct cl_env *cle;
696         struct lu_env *env;
697         int cpu = get_cpu();
698
699         ENTRY;
700
701         read_lock(&cl_envs[cpu].cec_guard);
702         LASSERT(equi(cl_envs[cpu].cec_count == 0,
703                 list_empty(&cl_envs[cpu].cec_envs)));
704         if (cl_envs[cpu].cec_count > 0) {
705                 int rc;
706
707                 cle = container_of(cl_envs[cpu].cec_envs.next, struct cl_env,
708                                    ce_linkage);
709                 list_del_init(&cle->ce_linkage);
710                 cl_envs[cpu].cec_count--;
711                 read_unlock(&cl_envs[cpu].cec_guard);
712                 put_cpu();
713
714                 env = &cle->ce_lu;
715                 rc = lu_env_refill(env);
716                 if (rc == 0) {
717                         cl_env_init0(cle, debug);
718                         lu_context_enter(&env->le_ctx);
719                         lu_context_enter(&cle->ce_ses);
720                 } else {
721                         cl_env_fini(cle);
722                         env = ERR_PTR(rc);
723                 }
724         } else {
725                 read_unlock(&cl_envs[cpu].cec_guard);
726                 put_cpu();
727                 env = cl_env_new(lu_context_tags_default,
728                                  lu_session_tags_default, debug);
729         }
730         RETURN(env);
731 }
732
733 static inline struct cl_env *cl_env_container(struct lu_env *env)
734 {
735         return container_of(env, struct cl_env, ce_lu);
736 }
737
738 /**
739  * Returns an lu_env.
740  *
741  * No link to thread, this returns an env from the cache or
742  * allocates a new one.
743  *
744  * If you need to get the specific environment you created for this thread,
745  * you must either pass the pointer directly or store it in the file/inode
746  * private data and retrieve it from there using ll_cl_add/ll_cl_find.
747  *
748  * \param refcheck pointer to a counter used to detect environment leaks. In
749  * the usual case cl_env_get() and cl_env_put() are called in the same lexical
750  * scope and pointer to the same integer is passed as \a refcheck. This is
751  * used to detect missed cl_env_put().
752  *
753  * \see cl_env_put()
754  */
755 struct lu_env *cl_env_get(__u16 *refcheck)
756 {
757         struct lu_env *env;
758
759         env = cl_env_obtain(__builtin_return_address(0));
760         if (!IS_ERR(env)) {
761                 struct cl_env *cle;
762
763                 cle = cl_env_container(env);
764                 *refcheck = cle->ce_ref;
765                 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
766         }
767         return env;
768 }
769 EXPORT_SYMBOL(cl_env_get);
770
771 /**
772  * Forces an allocation of a fresh environment with given tags.
773  *
774  * \see cl_env_get()
775  */
776 struct lu_env *cl_env_alloc(__u16 *refcheck, __u32 tags)
777 {
778         struct lu_env *env;
779
780         env = cl_env_new(tags, tags, __builtin_return_address(0));
781         if (!IS_ERR(env)) {
782                 struct cl_env *cle;
783
784                 cle = cl_env_container(env);
785                 *refcheck = cle->ce_ref;
786                 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
787         }
788         return env;
789 }
790 EXPORT_SYMBOL(cl_env_alloc);
791
792 static void cl_env_exit(struct cl_env *cle)
793 {
794         lu_context_exit(&cle->ce_lu.le_ctx);
795         lu_context_exit(&cle->ce_ses);
796 }
797
798 /**
799  * Finalizes and frees a given number of cached environments. This is done to
800  * (1) free some memory (not currently hooked into VM), or (2) release
801  * references to modules.
802  */
803 unsigned cl_env_cache_purge(unsigned nr)
804 {
805         struct cl_env *cle;
806         unsigned i;
807
808         ENTRY;
809         for_each_possible_cpu(i) {
810                 write_lock(&cl_envs[i].cec_guard);
811                 for (; !list_empty(&cl_envs[i].cec_envs) && nr > 0; --nr) {
812                         cle = container_of(cl_envs[i].cec_envs.next,
813                                            struct cl_env, ce_linkage);
814                         list_del_init(&cle->ce_linkage);
815                         LASSERT(cl_envs[i].cec_count > 0);
816                         cl_envs[i].cec_count--;
817                         write_unlock(&cl_envs[i].cec_guard);
818
819                         cl_env_fini(cle);
820                         write_lock(&cl_envs[i].cec_guard);
821                 }
822                 LASSERT(equi(cl_envs[i].cec_count == 0,
823                         list_empty(&cl_envs[i].cec_envs)));
824                 write_unlock(&cl_envs[i].cec_guard);
825         }
826         RETURN(nr);
827 }
828 EXPORT_SYMBOL(cl_env_cache_purge);
829
830 /**
831  * Release an environment.
832  *
833  * Decrement \a env reference counter. When counter drops to 0, nothing in
834  * this thread is using environment and it is returned to the per-CPU cache or
835  * freed immediately if the cache is full.
836  */
837 void cl_env_put(struct lu_env *env, __u16 *refcheck)
838 {
839         struct cl_env *cle;
840
841         cle = cl_env_container(env);
842
843         LASSERT(cle->ce_ref > 0);
844         LASSERT(ergo(refcheck != NULL, cle->ce_ref == *refcheck));
845
846         CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
847         if (--cle->ce_ref == 0) {
848                 int cpu = get_cpu();
849
850                 cl_env_dec(CS_busy);
851                 cle->ce_debug = NULL;
852                 cl_env_exit(cle);
853                 /*
854                  * Don't bother to take a lock here.
855                  *
856                  * Return environment to the cache only when it was allocated
857                  * with the standard tags.
858                  */
859                 if (cl_envs[cpu].cec_count < cl_envs_cached_max &&
860                     (env->le_ctx.lc_tags & ~LCT_HAS_EXIT) == lu_context_tags_default &&
861                     (env->le_ses->lc_tags & ~LCT_HAS_EXIT) == lu_session_tags_default) {
862                         read_lock(&cl_envs[cpu].cec_guard);
863                         list_add(&cle->ce_linkage, &cl_envs[cpu].cec_envs);
864                         cl_envs[cpu].cec_count++;
865                         read_unlock(&cl_envs[cpu].cec_guard);
866                 } else
867                         cl_env_fini(cle);
868                 put_cpu();
869         }
870 }
871 EXPORT_SYMBOL(cl_env_put);
872
873 /**
874  * Converts struct cl_attr to struct ost_lvb.
875  *
876  * \see cl_lvb2attr
877  */
878 void cl_attr2lvb(struct ost_lvb *lvb, const struct cl_attr *attr)
879 {
880         lvb->lvb_size   = attr->cat_size;
881         lvb->lvb_mtime  = attr->cat_mtime;
882         lvb->lvb_atime  = attr->cat_atime;
883         lvb->lvb_ctime  = attr->cat_ctime;
884         lvb->lvb_blocks = attr->cat_blocks;
885 }
886
887 /**
888  * Converts struct ost_lvb to struct cl_attr.
889  *
890  * \see cl_attr2lvb
891  */
892 void cl_lvb2attr(struct cl_attr *attr, const struct ost_lvb *lvb)
893 {
894         attr->cat_size   = lvb->lvb_size;
895         attr->cat_mtime  = lvb->lvb_mtime;
896         attr->cat_atime  = lvb->lvb_atime;
897         attr->cat_ctime  = lvb->lvb_ctime;
898         attr->cat_blocks = lvb->lvb_blocks;
899 }
900 EXPORT_SYMBOL(cl_lvb2attr);
901
902 static struct cl_env cl_env_percpu[NR_CPUS];
903 static DEFINE_MUTEX(cl_env_percpu_mutex);
904
905 static int cl_env_percpu_init(void)
906 {
907         struct cl_env *cle;
908         int tags = LCT_REMEMBER | LCT_NOREF;
909         int i, j;
910         int rc = 0;
911
912         for_each_possible_cpu(i) {
913                 struct lu_env *env;
914
915                 rwlock_init(&cl_envs[i].cec_guard);
916                 INIT_LIST_HEAD(&cl_envs[i].cec_envs);
917                 cl_envs[i].cec_count = 0;
918
919                 cle = &cl_env_percpu[i];
920                 env = &cle->ce_lu;
921
922                 INIT_LIST_HEAD(&cle->ce_linkage);
923                 cle->ce_magic = &cl_env_init0;
924                 rc = lu_env_init(env, LCT_CL_THREAD | tags);
925                 if (rc == 0) {
926                         rc = lu_context_init(&cle->ce_ses, LCT_SESSION | tags);
927                         if (rc == 0) {
928                                 lu_context_enter(&cle->ce_ses);
929                                 env->le_ses = &cle->ce_ses;
930                         } else {
931                                 lu_env_fini(env);
932                         }
933                 }
934                 if (rc != 0)
935                         break;
936         }
937         if (rc != 0) {
938                 /* Indices 0 to i (excluding i) were correctly initialized,
939                  * thus we must uninitialize up to i, the rest are undefined. */
940                 for (j = 0; j < i; j++) {
941                         cle = &cl_env_percpu[j];
942                         lu_context_exit(&cle->ce_ses);
943                         lu_context_fini(&cle->ce_ses);
944                         lu_env_fini(&cle->ce_lu);
945                 }
946         }
947
948         return rc;
949 }
950
951 static void cl_env_percpu_fini(void)
952 {
953         int i;
954
955         for_each_possible_cpu(i) {
956                 struct cl_env *cle = &cl_env_percpu[i];
957
958                 lu_context_exit(&cle->ce_ses);
959                 lu_context_fini(&cle->ce_ses);
960                 lu_env_fini(&cle->ce_lu);
961         }
962 }
963
964 static void cl_env_percpu_refill(void)
965 {
966         int i;
967
968         mutex_lock(&cl_env_percpu_mutex);
969         for_each_possible_cpu(i)
970                 lu_env_refill(&cl_env_percpu[i].ce_lu);
971         mutex_unlock(&cl_env_percpu_mutex);
972 }
973
974 void cl_env_percpu_put(struct lu_env *env)
975 {
976         struct cl_env *cle;
977         int cpu;
978
979         cpu = smp_processor_id();
980         cle = cl_env_container(env);
981         LASSERT(cle == &cl_env_percpu[cpu]);
982
983         cle->ce_ref--;
984         LASSERT(cle->ce_ref == 0);
985
986         cl_env_dec(CS_busy);
987         cle->ce_debug = NULL;
988
989         put_cpu();
990 }
991 EXPORT_SYMBOL(cl_env_percpu_put);
992
993 struct lu_env *cl_env_percpu_get(void)
994 {
995         struct cl_env *cle;
996
997         cle = &cl_env_percpu[get_cpu()];
998         cl_env_init0(cle, __builtin_return_address(0));
999
1000         return &cle->ce_lu;
1001 }
1002 EXPORT_SYMBOL(cl_env_percpu_get);
1003
1004 /*****************************************************************************
1005  *
1006  * Temporary prototype thing: mirror obd-devices into cl devices.
1007  *
1008  */
1009
1010 struct cl_device *cl_type_setup(const struct lu_env *env, struct lu_site *site,
1011                                 struct lu_device_type *ldt,
1012                                 struct lu_device *next)
1013 {
1014         const char *typename;
1015         struct lu_device *d;
1016
1017         LASSERT(ldt);
1018
1019         typename = ldt->ldt_name;
1020         d = ldto_device_alloc(env, ldt, NULL);
1021         if (!IS_ERR(d)) {
1022                 int rc;
1023
1024                 if (site)
1025                         d->ld_site = site;
1026
1027                 rc = ldto_device_init(env, d, typename, next);
1028                 if (rc == 0) {
1029                         lu_device_get(d);
1030                 } else {
1031                         ldto_device_free(env, d);
1032                         CERROR("can't init device '%s', %d\n", typename, rc);
1033                         d = ERR_PTR(rc);
1034                 }
1035         } else {
1036                 CERROR("Cannot allocate device: '%s'\n", typename);
1037         }
1038
1039         return lu2cl_dev(d);
1040 }
1041 EXPORT_SYMBOL(cl_type_setup);
1042
1043 static struct lu_context_key cl_key;
1044
1045 struct cl_thread_info *cl_env_info(const struct lu_env *env)
1046 {
1047         return lu_context_key_get(&env->le_ctx, &cl_key);
1048 }
1049
1050 /* defines cl_key_{init,fini}() */
1051 LU_KEY_INIT_FINI(cl, struct cl_thread_info);
1052
1053 static struct lu_context_key cl_key = {
1054         .lct_tags = LCT_CL_THREAD,
1055         .lct_init = cl_key_init,
1056         .lct_fini = cl_key_fini,
1057 };
1058
1059 static struct lu_kmem_descr cl_object_caches[] = {
1060         {
1061                 .ckd_cache = &cl_env_kmem,
1062                 .ckd_name  = "cl_env_kmem",
1063                 .ckd_size  = sizeof(struct cl_env)
1064         },
1065         {
1066                 .ckd_cache = &cl_dio_aio_kmem,
1067                 .ckd_name  = "cl_dio_aio_kmem",
1068                 .ckd_size  = sizeof(struct cl_dio_aio)
1069         },
1070         {
1071                 .ckd_cache = &cl_sub_dio_kmem,
1072                 .ckd_name  = "cl_sub_dio_kmem",
1073                 .ckd_size  = sizeof(struct cl_sub_dio)
1074         },
1075         {
1076                 .ckd_cache = NULL
1077         }
1078 };
1079
1080 /**
1081  * Global initialization of cl-data. Create kmem caches, register
1082  * lu_context_key's, etc.
1083  *
1084  * \see cl_global_fini()
1085  */
1086 int cl_global_init(void)
1087 {
1088         int result;
1089
1090         OBD_ALLOC_PTR_ARRAY(cl_envs, num_possible_cpus());
1091         if (cl_envs == NULL)
1092                 GOTO(out, result = -ENOMEM);
1093
1094         result = lu_kmem_init(cl_object_caches);
1095         if (result)
1096                 GOTO(out_envs, result);
1097
1098         LU_CONTEXT_KEY_INIT(&cl_key);
1099         result = lu_context_key_register(&cl_key);
1100         if (result)
1101                 GOTO(out_kmem, result);
1102
1103         result = cl_env_percpu_init();
1104         if (result) /* no cl_env_percpu_fini on error */
1105                 GOTO(out_keys, result);
1106
1107         return 0;
1108
1109 out_keys:
1110         lu_context_key_degister(&cl_key);
1111 out_kmem:
1112         lu_kmem_fini(cl_object_caches);
1113 out_envs:
1114         OBD_FREE_PTR_ARRAY(cl_envs, num_possible_cpus());
1115 out:
1116         return result;
1117 }
1118
1119 /**
1120  * Finalization of global cl-data. Dual to cl_global_init().
1121  */
1122 void cl_global_fini(void)
1123 {
1124         int i;
1125
1126         for (i = 0; i < ARRAY_SIZE(cl_page_kmem_array); i++) {
1127                 if (cl_page_kmem_array[i]) {
1128                         kmem_cache_destroy(cl_page_kmem_array[i]);
1129                         cl_page_kmem_array[i] = NULL;
1130                 }
1131         }
1132         cl_env_percpu_fini();
1133         lu_context_key_degister(&cl_key);
1134         lu_kmem_fini(cl_object_caches);
1135         OBD_FREE_PTR_ARRAY(cl_envs, num_possible_cpus());
1136 }