Whamcloud - gitweb
LU-6142 lustre: mark strings in char arrays as const
[fs/lustre-release.git] / lustre / obdclass / cl_object.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 Object.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 /*
39  * Locking.
40  *
41  *  i_mutex
42  *      PG_locked
43  *          ->coh_attr_guard
44  *          ->ls_guard
45  */
46
47 #define DEBUG_SUBSYSTEM S_CLASS
48
49 #include <linux/list.h>
50 #include <libcfs/libcfs.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_fid.h>
54 #include <libcfs/libcfs_hash.h> /* for cfs_hash stuff */
55 #include <cl_object.h>
56 #include <lu_object.h>
57 #include "cl_internal.h"
58
59 static struct kmem_cache *cl_env_kmem;
60 struct kmem_cache *cl_dio_aio_kmem;
61 struct kmem_cache *cl_page_kmem_array[16];
62 unsigned short cl_page_kmem_size_array[16];
63
64 /** Lock class of cl_object_header::coh_attr_guard */
65 static struct lock_class_key cl_attr_guard_class;
66
67 /**
68  * Initialize cl_object_header.
69  */
70 int cl_object_header_init(struct cl_object_header *h)
71 {
72         int result;
73
74         ENTRY;
75         result = lu_object_header_init(&h->coh_lu);
76         if (result == 0) {
77                 spin_lock_init(&h->coh_attr_guard);
78                 lockdep_set_class(&h->coh_attr_guard, &cl_attr_guard_class);
79                 h->coh_page_bufsize = 0;
80         }
81         RETURN(result);
82 }
83 EXPORT_SYMBOL(cl_object_header_init);
84
85 /**
86  * Finalize cl_object_header.
87  */
88 void cl_object_header_fini(struct cl_object_header *h)
89 {
90         lu_object_header_fini(&h->coh_lu);
91 }
92
93 /**
94  * Returns a cl_object with a given \a fid.
95  *
96  * Returns either cached or newly created object. Additional reference on the
97  * returned object is acquired.
98  *
99  * \see lu_object_find(), cl_page_find(), cl_lock_find()
100  */
101 struct cl_object *cl_object_find(const struct lu_env *env,
102                                  struct cl_device *cd, const struct lu_fid *fid,
103                                  const struct cl_object_conf *c)
104 {
105         might_sleep();
106         return lu2cl(lu_object_find_slice(env, cl2lu_dev(cd), fid, &c->coc_lu));
107 }
108 EXPORT_SYMBOL(cl_object_find);
109
110 /**
111  * Releases a reference on \a o.
112  *
113  * When last reference is released object is returned to the cache, unless
114  * lu_object_header_flags::LU_OBJECT_HEARD_BANSHEE bit is set in its header.
115  *
116  * \see cl_page_put(), cl_lock_put().
117  */
118 void cl_object_put(const struct lu_env *env, struct cl_object *o)
119 {
120         lu_object_put(env, &o->co_lu);
121 }
122 EXPORT_SYMBOL(cl_object_put);
123
124 /**
125  * Acquire an additional reference to the object \a o.
126  *
127  * This can only be used to acquire _additional_ reference, i.e., caller
128  * already has to possess at least one reference to \a o before calling this.
129  *
130  * \see cl_page_get(), cl_lock_get().
131  */
132 void cl_object_get(struct cl_object *o)
133 {
134         lu_object_get(&o->co_lu);
135 }
136 EXPORT_SYMBOL(cl_object_get);
137
138 /**
139  * Returns the top-object for a given \a o.
140  *
141  * \see cl_io_top()
142  */
143 struct cl_object *cl_object_top(struct cl_object *o)
144 {
145         struct cl_object_header *hdr = cl_object_header(o);
146         struct cl_object *top;
147
148         while (hdr->coh_parent != NULL)
149                 hdr = hdr->coh_parent;
150
151         top = lu2cl(lu_object_top(&hdr->coh_lu));
152         CDEBUG(D_TRACE, "%p -> %p\n", o, top);
153         return top;
154 }
155 EXPORT_SYMBOL(cl_object_top);
156
157 /**
158  * Returns pointer to the lock protecting data-attributes for the given object
159  * \a o.
160  *
161  * Data-attributes are protected by the cl_object_header::coh_attr_guard
162  * spin-lock in the top-object.
163  *
164  * \see cl_attr, cl_object_attr_lock(), cl_object_operations::coo_attr_get().
165  */
166 static spinlock_t *cl_object_attr_guard(struct cl_object *o)
167 {
168         return &cl_object_header(cl_object_top(o))->coh_attr_guard;
169 }
170
171 /**
172  * Locks data-attributes.
173  *
174  * Prevents data-attributes from changing, until lock is released by
175  * cl_object_attr_unlock(). This has to be called before calls to
176  * cl_object_attr_get(), cl_object_attr_update().
177  */
178 void cl_object_attr_lock(struct cl_object *o)
179 __acquires(cl_object_attr_guard(o))
180 {
181         spin_lock(cl_object_attr_guard(o));
182 }
183 EXPORT_SYMBOL(cl_object_attr_lock);
184
185 /**
186  * Releases data-attributes lock, acquired by cl_object_attr_lock().
187  */
188 void cl_object_attr_unlock(struct cl_object *o)
189 __releases(cl_object_attr_guard(o))
190 {
191         spin_unlock(cl_object_attr_guard(o));
192 }
193 EXPORT_SYMBOL(cl_object_attr_unlock);
194
195 /**
196  * Returns data-attributes of an object \a obj.
197  *
198  * Every layer is asked (by calling cl_object_operations::coo_attr_get())
199  * top-to-bottom to fill in parts of \a attr that this layer is responsible
200  * for.
201  */
202 int cl_object_attr_get(const struct lu_env *env, struct cl_object *top,
203                         struct cl_attr *attr)
204 {
205         struct cl_object *obj;
206         int result = 0;
207
208         assert_spin_locked(cl_object_attr_guard(top));
209         ENTRY;
210
211         cl_object_for_each(obj, top) {
212                 if (obj->co_ops->coo_attr_get != NULL) {
213                         result = obj->co_ops->coo_attr_get(env, obj, attr);
214                         if (result != 0) {
215                                 if (result > 0)
216                                         result = 0;
217                                 break;
218                         }
219                 }
220         }
221         RETURN(result);
222 }
223 EXPORT_SYMBOL(cl_object_attr_get);
224
225 /**
226  * Updates data-attributes of an object \a obj.
227  *
228  * Only attributes, mentioned in a validness bit-mask \a v are
229  * updated. Calls cl_object_operations::coo_upd_attr() on every layer, bottom
230  * to top.
231  */
232 int cl_object_attr_update(const struct lu_env *env, struct cl_object *top,
233                           const struct cl_attr *attr, unsigned v)
234 {
235         struct cl_object *obj;
236         int result = 0;
237
238         assert_spin_locked(cl_object_attr_guard(top));
239         ENTRY;
240
241         cl_object_for_each_reverse(obj, top) {
242                 if (obj->co_ops->coo_attr_update != NULL) {
243                         result = obj->co_ops->coo_attr_update(env, obj, attr,
244                                                               v);
245                         if (result != 0) {
246                                 if (result > 0)
247                                         result = 0;
248                                 break;
249                         }
250                 }
251         }
252         RETURN(result);
253 }
254 EXPORT_SYMBOL(cl_object_attr_update);
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 /**
433  * Helper function removing all object locks, and marking object for
434  * deletion. All object pages must have been deleted at this point.
435  *
436  * This is called by cl_inode_fini() and lov_object_delete() to destroy top-
437  * and sub- objects respectively.
438  */
439 void cl_object_kill(const struct lu_env *env, struct cl_object *obj)
440 {
441         struct cl_object_header *hdr = cl_object_header(obj);
442
443         set_bit(LU_OBJECT_HEARD_BANSHEE, &hdr->coh_lu.loh_flags);
444 }
445 EXPORT_SYMBOL(cl_object_kill);
446
447 void cache_stats_init(struct cache_stats *cs, const char *name)
448 {
449         int i;
450
451         cs->cs_name = name;
452         for (i = 0; i < CS_NR; i++)
453                 atomic_set(&cs->cs_stats[i], 0);
454 }
455
456 static int cache_stats_print(const struct cache_stats *cs,
457                              struct seq_file *m, int h)
458 {
459         int i;
460
461         /*
462          *   lookup    hit    total  cached create
463          * env: ...... ...... ...... ...... ......
464          */
465         if (h) {
466                 const char *names[CS_NR] = CS_NAMES;
467
468                 seq_printf(m, "%6s", " ");
469                 for (i = 0; i < CS_NR; i++)
470                         seq_printf(m, "%8s", names[i]);
471                 seq_printf(m, "\n");
472         }
473
474         seq_printf(m, "%5.5s:", cs->cs_name);
475         for (i = 0; i < CS_NR; i++)
476                 seq_printf(m, "%8u", atomic_read(&cs->cs_stats[i]));
477         return 0;
478 }
479
480 static void cl_env_percpu_refill(void);
481
482 /**
483  * Initialize client site.
484  *
485  * Perform common initialization (lu_site_init()), and initialize statistical
486  * counters. Also perform global initializations on the first call.
487  */
488 int cl_site_init(struct cl_site *s, struct cl_device *d)
489 {
490         size_t i;
491         int result;
492
493         result = lu_site_init(&s->cs_lu, &d->cd_lu_dev);
494         if (result == 0) {
495                 cache_stats_init(&s->cs_pages, "pages");
496                 for (i = 0; i < ARRAY_SIZE(s->cs_pages_state); ++i)
497                         atomic_set(&s->cs_pages_state[0], 0);
498                 cl_env_percpu_refill();
499         }
500         return result;
501 }
502 EXPORT_SYMBOL(cl_site_init);
503
504 /**
505  * Finalize client site. Dual to cl_site_init().
506  */
507 void cl_site_fini(struct cl_site *s)
508 {
509         lu_site_fini(&s->cs_lu);
510 }
511 EXPORT_SYMBOL(cl_site_fini);
512
513 static struct cache_stats cl_env_stats = {
514         .cs_name    = "envs",
515         .cs_stats = { ATOMIC_INIT(0), }
516 };
517
518 /**
519  * Outputs client site statistical counters into a buffer. Suitable for
520  * ll_rd_*()-style functions.
521  */
522 int cl_site_stats_print(const struct cl_site *site, struct seq_file *m)
523 {
524         static const char *const pstate[] = {
525                 [CPS_CACHED]    = "c",
526                 [CPS_OWNED]     = "o",
527                 [CPS_PAGEOUT]   = "w",
528                 [CPS_PAGEIN]    = "r",
529                 [CPS_FREEING]   = "f"
530         };
531         size_t i;
532
533 /*
534        lookup    hit  total   busy create
535 pages: ...... ...... ...... ...... ...... [...... ...... ...... ......]
536 locks: ...... ...... ...... ...... ...... [...... ...... ...... ...... ......]
537   env: ...... ...... ...... ...... ......
538  */
539         lu_site_stats_seq_print(&site->cs_lu, m);
540         cache_stats_print(&site->cs_pages, m, 1);
541         seq_printf(m, " [");
542         for (i = 0; i < ARRAY_SIZE(site->cs_pages_state); ++i)
543                 seq_printf(m, "%s: %u ", pstate[i],
544                            atomic_read(&site->cs_pages_state[i]));
545         seq_printf(m, "]\n");
546         cache_stats_print(&cl_env_stats, m, 0);
547         seq_printf(m, "\n");
548         return 0;
549 }
550 EXPORT_SYMBOL(cl_site_stats_print);
551
552 /*****************************************************************************
553  *
554  * lu_env handling on client.
555  *
556  */
557
558 /**
559  * The most efficient way is to store cl_env pointer in task specific
560  * structures. On Linux, it isn't easy to use task_struct->journal_info
561  * because Lustre code may call into other fs during memory reclaim, which
562  * has certain assumptions about journal_info. There are not currently any
563  * fields in task_struct that can be used for this purpose.
564  * \note As long as we use task_struct to store cl_env, we assume that once
565  * called into Lustre, we'll never call into the other part of the kernel
566  * which will use those fields in task_struct without explicitly exiting
567  * Lustre.
568  *
569  * Since there's no space in task_struct is available, hash will be used.
570  * bz20044, bz22683.
571  */
572
573 static unsigned cl_envs_cached_max = 32; /* XXX: prototype: arbitrary limit
574                                           * for now. */
575 static struct cl_env_cache {
576         rwlock_t                cec_guard;
577         unsigned                cec_count;
578         struct list_head        cec_envs;
579 } *cl_envs = NULL;
580
581 struct cl_env {
582         void             *ce_magic;
583         struct lu_env     ce_lu;
584         struct lu_context ce_ses;
585
586         /*
587          * Linkage into global list of all client environments. Used for
588          * garbage collection.
589          */
590         struct list_head  ce_linkage;
591         /*
592          *
593          */
594         int               ce_ref;
595         /*
596          * Debugging field: address of the caller who made original
597          * allocation.
598          */
599         void             *ce_debug;
600 };
601
602 static void cl_env_inc(enum cache_stats_item item)
603 {
604 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
605         atomic_inc(&cl_env_stats.cs_stats[item]);
606 #endif
607 }
608
609 static void cl_env_dec(enum cache_stats_item item)
610 {
611 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
612         LASSERT(atomic_read(&cl_env_stats.cs_stats[item]) > 0);
613         atomic_dec(&cl_env_stats.cs_stats[item]);
614 #endif
615 }
616
617 static void cl_env_init0(struct cl_env *cle, void *debug)
618 {
619         LASSERT(cle->ce_ref == 0);
620         LASSERT(cle->ce_magic == &cl_env_init0);
621         LASSERT(cle->ce_debug == NULL);
622
623         cle->ce_ref = 1;
624         cle->ce_debug = debug;
625         cl_env_inc(CS_busy);
626 }
627
628 static struct lu_env *cl_env_new(__u32 ctx_tags, __u32 ses_tags, void *debug)
629 {
630         struct lu_env *env;
631         struct cl_env *cle;
632
633         OBD_SLAB_ALLOC_PTR_GFP(cle, cl_env_kmem, GFP_NOFS);
634         if (cle != NULL) {
635                 int rc;
636
637                 INIT_LIST_HEAD(&cle->ce_linkage);
638                 cle->ce_magic = &cl_env_init0;
639                 env = &cle->ce_lu;
640                 rc = lu_env_init(env, LCT_CL_THREAD|ctx_tags);
641                 if (rc == 0) {
642                         rc = lu_context_init(&cle->ce_ses,
643                                              LCT_SESSION | ses_tags);
644                         if (rc == 0) {
645                                 lu_context_enter(&cle->ce_ses);
646                                 env->le_ses = &cle->ce_ses;
647                                 cl_env_init0(cle, debug);
648                         } else
649                                 lu_env_fini(env);
650                 }
651                 if (rc != 0) {
652                         OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
653                         env = ERR_PTR(rc);
654                 } else {
655                         cl_env_inc(CS_create);
656                         cl_env_inc(CS_total);
657                 }
658         } else
659                 env = ERR_PTR(-ENOMEM);
660         return env;
661 }
662
663 static void cl_env_fini(struct cl_env *cle)
664 {
665         cl_env_dec(CS_total);
666         lu_context_fini(&cle->ce_lu.le_ctx);
667         lu_context_fini(&cle->ce_ses);
668         OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
669 }
670
671 static struct lu_env *cl_env_obtain(void *debug)
672 {
673         struct cl_env *cle;
674         struct lu_env *env;
675         int cpu = get_cpu();
676
677         ENTRY;
678
679         read_lock(&cl_envs[cpu].cec_guard);
680         LASSERT(equi(cl_envs[cpu].cec_count == 0,
681                 list_empty(&cl_envs[cpu].cec_envs)));
682         if (cl_envs[cpu].cec_count > 0) {
683                 int rc;
684
685                 cle = container_of(cl_envs[cpu].cec_envs.next, struct cl_env,
686                                    ce_linkage);
687                 list_del_init(&cle->ce_linkage);
688                 cl_envs[cpu].cec_count--;
689                 read_unlock(&cl_envs[cpu].cec_guard);
690                 put_cpu();
691
692                 env = &cle->ce_lu;
693                 rc = lu_env_refill(env);
694                 if (rc == 0) {
695                         cl_env_init0(cle, debug);
696                         lu_context_enter(&env->le_ctx);
697                         lu_context_enter(&cle->ce_ses);
698                 } else {
699                         cl_env_fini(cle);
700                         env = ERR_PTR(rc);
701                 }
702         } else {
703                 read_unlock(&cl_envs[cpu].cec_guard);
704                 put_cpu();
705                 env = cl_env_new(lu_context_tags_default,
706                                  lu_session_tags_default, debug);
707         }
708         RETURN(env);
709 }
710
711 static inline struct cl_env *cl_env_container(struct lu_env *env)
712 {
713         return container_of(env, struct cl_env, ce_lu);
714 }
715
716 /**
717  * Returns lu_env: if there already is an environment associated with the
718  * current thread, it is returned, otherwise, new environment is allocated.
719  *
720  * Allocations are amortized through the global cache of environments.
721  *
722  * \param refcheck pointer to a counter used to detect environment leaks. In
723  * the usual case cl_env_get() and cl_env_put() are called in the same lexical
724  * scope and pointer to the same integer is passed as \a refcheck. This is
725  * used to detect missed cl_env_put().
726  *
727  * \see cl_env_put()
728  */
729 struct lu_env *cl_env_get(__u16 *refcheck)
730 {
731         struct lu_env *env;
732
733         env = cl_env_obtain(__builtin_return_address(0));
734         if (!IS_ERR(env)) {
735                 struct cl_env *cle;
736
737                 cle = cl_env_container(env);
738                 *refcheck = cle->ce_ref;
739                 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
740         }
741         return env;
742 }
743 EXPORT_SYMBOL(cl_env_get);
744
745 /**
746  * Forces an allocation of a fresh environment with given tags.
747  *
748  * \see cl_env_get()
749  */
750 struct lu_env *cl_env_alloc(__u16 *refcheck, __u32 tags)
751 {
752         struct lu_env *env;
753
754         env = cl_env_new(tags, tags, __builtin_return_address(0));
755         if (!IS_ERR(env)) {
756                 struct cl_env *cle;
757
758                 cle = cl_env_container(env);
759                 *refcheck = cle->ce_ref;
760                 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
761         }
762         return env;
763 }
764 EXPORT_SYMBOL(cl_env_alloc);
765
766 static void cl_env_exit(struct cl_env *cle)
767 {
768         lu_context_exit(&cle->ce_lu.le_ctx);
769         lu_context_exit(&cle->ce_ses);
770 }
771
772 /**
773  * Finalizes and frees a given number of cached environments. This is done to
774  * (1) free some memory (not currently hooked into VM), or (2) release
775  * references to modules.
776  */
777 unsigned cl_env_cache_purge(unsigned nr)
778 {
779         struct cl_env *cle;
780         unsigned i;
781
782         ENTRY;
783         for_each_possible_cpu(i) {
784                 write_lock(&cl_envs[i].cec_guard);
785                 for (; !list_empty(&cl_envs[i].cec_envs) && nr > 0; --nr) {
786                         cle = container_of(cl_envs[i].cec_envs.next,
787                                            struct cl_env, ce_linkage);
788                         list_del_init(&cle->ce_linkage);
789                         LASSERT(cl_envs[i].cec_count > 0);
790                         cl_envs[i].cec_count--;
791                         write_unlock(&cl_envs[i].cec_guard);
792
793                         cl_env_fini(cle);
794                         write_lock(&cl_envs[i].cec_guard);
795                 }
796                 LASSERT(equi(cl_envs[i].cec_count == 0,
797                         list_empty(&cl_envs[i].cec_envs)));
798                 write_unlock(&cl_envs[i].cec_guard);
799         }
800         RETURN(nr);
801 }
802 EXPORT_SYMBOL(cl_env_cache_purge);
803
804 /**
805  * Release an environment.
806  *
807  * Decrement \a env reference counter. When counter drops to 0, nothing in
808  * this thread is using environment and it is returned to the allocation
809  * cache, or freed straight away, if cache is large enough.
810  */
811 void cl_env_put(struct lu_env *env, __u16 *refcheck)
812 {
813         struct cl_env *cle;
814
815         cle = cl_env_container(env);
816
817         LASSERT(cle->ce_ref > 0);
818         LASSERT(ergo(refcheck != NULL, cle->ce_ref == *refcheck));
819
820         CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
821         if (--cle->ce_ref == 0) {
822                 int cpu = get_cpu();
823
824                 cl_env_dec(CS_busy);
825                 cle->ce_debug = NULL;
826                 cl_env_exit(cle);
827                 /*
828                  * Don't bother to take a lock here.
829                  *
830                  * Return environment to the cache only when it was allocated
831                  * with the standard tags.
832                  */
833                 if (cl_envs[cpu].cec_count < cl_envs_cached_max &&
834                     (env->le_ctx.lc_tags & ~LCT_HAS_EXIT) == lu_context_tags_default &&
835                     (env->le_ses->lc_tags & ~LCT_HAS_EXIT) == lu_session_tags_default) {
836                         read_lock(&cl_envs[cpu].cec_guard);
837                         list_add(&cle->ce_linkage, &cl_envs[cpu].cec_envs);
838                         cl_envs[cpu].cec_count++;
839                         read_unlock(&cl_envs[cpu].cec_guard);
840                 } else
841                         cl_env_fini(cle);
842                 put_cpu();
843         }
844 }
845 EXPORT_SYMBOL(cl_env_put);
846
847 /**
848  * Converts struct cl_attr to struct ost_lvb.
849  *
850  * \see cl_lvb2attr
851  */
852 void cl_attr2lvb(struct ost_lvb *lvb, const struct cl_attr *attr)
853 {
854         lvb->lvb_size   = attr->cat_size;
855         lvb->lvb_mtime  = attr->cat_mtime;
856         lvb->lvb_atime  = attr->cat_atime;
857         lvb->lvb_ctime  = attr->cat_ctime;
858         lvb->lvb_blocks = attr->cat_blocks;
859 }
860
861 /**
862  * Converts struct ost_lvb to struct cl_attr.
863  *
864  * \see cl_attr2lvb
865  */
866 void cl_lvb2attr(struct cl_attr *attr, const struct ost_lvb *lvb)
867 {
868         attr->cat_size   = lvb->lvb_size;
869         attr->cat_mtime  = lvb->lvb_mtime;
870         attr->cat_atime  = lvb->lvb_atime;
871         attr->cat_ctime  = lvb->lvb_ctime;
872         attr->cat_blocks = lvb->lvb_blocks;
873 }
874 EXPORT_SYMBOL(cl_lvb2attr);
875
876 static struct cl_env cl_env_percpu[NR_CPUS];
877
878 static int cl_env_percpu_init(void)
879 {
880         struct cl_env *cle;
881         int tags = LCT_REMEMBER | LCT_NOREF;
882         int i, j;
883         int rc = 0;
884
885         for_each_possible_cpu(i) {
886                 struct lu_env *env;
887
888                 rwlock_init(&cl_envs[i].cec_guard);
889                 INIT_LIST_HEAD(&cl_envs[i].cec_envs);
890                 cl_envs[i].cec_count = 0;
891
892                 cle = &cl_env_percpu[i];
893                 env = &cle->ce_lu;
894
895                 INIT_LIST_HEAD(&cle->ce_linkage);
896                 cle->ce_magic = &cl_env_init0;
897                 rc = lu_env_init(env, LCT_CL_THREAD | tags);
898                 if (rc == 0) {
899                         rc = lu_context_init(&cle->ce_ses, LCT_SESSION | tags);
900                         if (rc == 0) {
901                                 lu_context_enter(&cle->ce_ses);
902                                 env->le_ses = &cle->ce_ses;
903                         } else {
904                                 lu_env_fini(env);
905                         }
906                 }
907                 if (rc != 0)
908                         break;
909         }
910         if (rc != 0) {
911                 /* Indices 0 to i (excluding i) were correctly initialized,
912                  * thus we must uninitialize up to i, the rest are undefined. */
913                 for (j = 0; j < i; j++) {
914                         cle = &cl_env_percpu[j];
915                         lu_context_exit(&cle->ce_ses);
916                         lu_context_fini(&cle->ce_ses);
917                         lu_env_fini(&cle->ce_lu);
918                 }
919         }
920
921         return rc;
922 }
923
924 static void cl_env_percpu_fini(void)
925 {
926         int i;
927
928         for_each_possible_cpu(i) {
929                 struct cl_env *cle = &cl_env_percpu[i];
930
931                 lu_context_exit(&cle->ce_ses);
932                 lu_context_fini(&cle->ce_ses);
933                 lu_env_fini(&cle->ce_lu);
934         }
935 }
936
937 static void cl_env_percpu_refill(void)
938 {
939         int i;
940
941         for_each_possible_cpu(i)
942                 lu_env_refill(&cl_env_percpu[i].ce_lu);
943 }
944
945 void cl_env_percpu_put(struct lu_env *env)
946 {
947         struct cl_env *cle;
948         int cpu;
949
950         cpu = smp_processor_id();
951         cle = cl_env_container(env);
952         LASSERT(cle == &cl_env_percpu[cpu]);
953
954         cle->ce_ref--;
955         LASSERT(cle->ce_ref == 0);
956
957         cl_env_dec(CS_busy);
958         cle->ce_debug = NULL;
959
960         put_cpu();
961 }
962 EXPORT_SYMBOL(cl_env_percpu_put);
963
964 struct lu_env *cl_env_percpu_get(void)
965 {
966         struct cl_env *cle;
967
968         cle = &cl_env_percpu[get_cpu()];
969         cl_env_init0(cle, __builtin_return_address(0));
970
971         return &cle->ce_lu;
972 }
973 EXPORT_SYMBOL(cl_env_percpu_get);
974
975 /*****************************************************************************
976  *
977  * Temporary prototype thing: mirror obd-devices into cl devices.
978  *
979  */
980
981 struct cl_device *cl_type_setup(const struct lu_env *env, struct lu_site *site,
982                                 struct lu_device_type *ldt,
983                                 struct lu_device *next)
984 {
985         const char       *typename;
986         struct lu_device *d;
987
988         LASSERT(ldt != NULL);
989
990         typename = ldt->ldt_name;
991         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, NULL);
992         if (!IS_ERR(d)) {
993                 int rc;
994
995                 if (site != NULL)
996                         d->ld_site = site;
997                 rc = ldt->ldt_ops->ldto_device_init(env, d, typename, next);
998                 if (rc == 0) {
999                         lu_device_get(d);
1000                         lu_ref_add(&d->ld_reference,
1001                                    "lu-stack", &lu_site_init);
1002                 } else {
1003                         ldt->ldt_ops->ldto_device_free(env, d);
1004                         CERROR("can't init device '%s', %d\n", typename, rc);
1005                         d = ERR_PTR(rc);
1006                 }
1007         } else
1008                 CERROR("Cannot allocate device: '%s'\n", typename);
1009         return lu2cl_dev(d);
1010 }
1011 EXPORT_SYMBOL(cl_type_setup);
1012
1013 /**
1014  * Finalize device stack by calling lu_stack_fini().
1015  */
1016 void cl_stack_fini(const struct lu_env *env, struct cl_device *cl)
1017 {
1018         lu_stack_fini(env, cl2lu_dev(cl));
1019 }
1020 EXPORT_SYMBOL(cl_stack_fini);
1021
1022 static struct lu_context_key cl_key;
1023
1024 struct cl_thread_info *cl_env_info(const struct lu_env *env)
1025 {
1026         return lu_context_key_get(&env->le_ctx, &cl_key);
1027 }
1028
1029 /* defines cl_key_{init,fini}() */
1030 LU_KEY_INIT_FINI(cl, struct cl_thread_info);
1031
1032 static struct lu_context_key cl_key = {
1033         .lct_tags = LCT_CL_THREAD,
1034         .lct_init = cl_key_init,
1035         .lct_fini = cl_key_fini,
1036 };
1037
1038 static struct lu_kmem_descr cl_object_caches[] = {
1039         {
1040                 .ckd_cache = &cl_env_kmem,
1041                 .ckd_name  = "cl_env_kmem",
1042                 .ckd_size  = sizeof(struct cl_env)
1043         },
1044         {
1045                 .ckd_cache = &cl_dio_aio_kmem,
1046                 .ckd_name  = "cl_dio_aio_kmem",
1047                 .ckd_size  = sizeof(struct cl_dio_aio)
1048         },
1049         {
1050                 .ckd_cache = NULL
1051         }
1052 };
1053
1054 /**
1055  * Global initialization of cl-data. Create kmem caches, register
1056  * lu_context_key's, etc.
1057  *
1058  * \see cl_global_fini()
1059  */
1060 int cl_global_init(void)
1061 {
1062         int result;
1063
1064         OBD_ALLOC_PTR_ARRAY(cl_envs, num_possible_cpus());
1065         if (cl_envs == NULL)
1066                 GOTO(out, result = -ENOMEM);
1067
1068         result = lu_kmem_init(cl_object_caches);
1069         if (result)
1070                 GOTO(out_envs, result);
1071
1072         LU_CONTEXT_KEY_INIT(&cl_key);
1073         result = lu_context_key_register(&cl_key);
1074         if (result)
1075                 GOTO(out_kmem, result);
1076
1077         result = cl_env_percpu_init();
1078         if (result) /* no cl_env_percpu_fini on error */
1079                 GOTO(out_keys, result);
1080
1081         return 0;
1082
1083 out_keys:
1084         lu_context_key_degister(&cl_key);
1085 out_kmem:
1086         lu_kmem_fini(cl_object_caches);
1087 out_envs:
1088         OBD_FREE_PTR_ARRAY(cl_envs, num_possible_cpus());
1089 out:
1090         return result;
1091 }
1092
1093 /**
1094  * Finalization of global cl-data. Dual to cl_global_init().
1095  */
1096 void cl_global_fini(void)
1097 {
1098         int i;
1099
1100         for (i = 0; i < ARRAY_SIZE(cl_page_kmem_array); i++) {
1101                 if (cl_page_kmem_array[i]) {
1102                         kmem_cache_destroy(cl_page_kmem_array[i]);
1103                         cl_page_kmem_array[i] = NULL;
1104                 }
1105         }
1106         cl_env_percpu_fini();
1107         lu_context_key_degister(&cl_key);
1108         lu_kmem_fini(cl_object_caches);
1109         OBD_FREE_PTR_ARRAY(cl_envs, num_possible_cpus());
1110 }