Whamcloud - gitweb
Revert "LU-1013 obdclass: lu_object_find miss to unlink object from LRU"
[fs/lustre-release.git] / lustre / obdclass / lu_object.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/obdclass/lu_object.c
40  *
41  * Lustre Object.
42  * These are the only exported functions, they provide some generic
43  * infrastructure for managing object devices
44  *
45  *   Author: Nikita Danilov <nikita.danilov@sun.com>
46  */
47
48 #define DEBUG_SUBSYSTEM S_CLASS
49 #ifndef EXPORT_SYMTAB
50 # define EXPORT_SYMTAB
51 #endif
52
53 #include <libcfs/libcfs.h>
54
55 #ifdef __KERNEL__
56 # include <linux/module.h>
57 #endif
58
59 /* hash_long() */
60 #include <libcfs/libcfs_hash.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre_disk.h>
64 #include <lustre_fid.h>
65 #include <lu_object.h>
66 #include <libcfs/list.h>
67 /* lu_time_global_{init,fini}() */
68 #include <lu_time.h>
69
70 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
71
72 /**
73  * Decrease reference counter on object. If last reference is freed, return
74  * object to the cache, unless lu_object_is_dying(o) holds. In the latter
75  * case, free object immediately.
76  */
77 void lu_object_put(const struct lu_env *env, struct lu_object *o)
78 {
79         struct lu_site_bkt_data *bkt;
80         struct lu_object_header *top;
81         struct lu_site          *site;
82         struct lu_object        *orig;
83         cfs_hash_bd_t            bd;
84
85         top  = o->lo_header;
86         site = o->lo_dev->ld_site;
87         orig = o;
88
89         cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
90         bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
91
92         if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
93                 if (lu_object_is_dying(top)) {
94
95                         /*
96                          * somebody may be waiting for this, currently only
97                          * used for cl_object, see cl_object_put_last().
98                          */
99                         cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
100                 }
101                 return;
102         }
103
104         LASSERT(bkt->lsb_busy > 0);
105         bkt->lsb_busy--;
106         /*
107          * When last reference is released, iterate over object
108          * layers, and notify them that object is no longer busy.
109          */
110         cfs_list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
111                 if (o->lo_ops->loo_object_release != NULL)
112                         o->lo_ops->loo_object_release(env, o);
113         }
114
115         if (!lu_object_is_dying(top)) {
116                 LASSERT(cfs_list_empty(&top->loh_lru));
117                 cfs_list_add_tail(&top->loh_lru, &bkt->lsb_lru);
118                 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
119                 return;
120         }
121
122         /*
123          * If object is dying (will not be cached), removed it
124          * from hash table and LRU.
125          *
126          * This is done with hash table and LRU lists locked. As the only
127          * way to acquire first reference to previously unreferenced
128          * object is through hash-table lookup (lu_object_find()),
129          * or LRU scanning (lu_site_purge()), that are done under hash-table
130          * and LRU lock, no race with concurrent object lookup is possible
131          * and we can safely destroy object below.
132          */
133         cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
134         cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
135         /*
136          * Object was already removed from hash and lru above, can
137          * kill it.
138          */
139         lu_object_free(env, orig);
140 }
141 EXPORT_SYMBOL(lu_object_put);
142
143 /**
144  * Allocate new object.
145  *
146  * This follows object creation protocol, described in the comment within
147  * struct lu_device_operations definition.
148  */
149 static struct lu_object *lu_object_alloc(const struct lu_env *env,
150                                          struct lu_device *dev,
151                                          const struct lu_fid *f,
152                                          const struct lu_object_conf *conf)
153 {
154         struct lu_object *scan;
155         struct lu_object *top;
156         cfs_list_t *layers;
157         int clean;
158         int result;
159         ENTRY;
160
161         /*
162          * Create top-level object slice. This will also create
163          * lu_object_header.
164          */
165         top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
166         if (top == NULL)
167                 RETURN(ERR_PTR(-ENOMEM));
168         /*
169          * This is the only place where object fid is assigned. It's constant
170          * after this point.
171          */
172         LASSERT(fid_is_igif(f) || fid_ver(f) == 0);
173         top->lo_header->loh_fid = *f;
174         layers = &top->lo_header->loh_layers;
175         do {
176                 /*
177                  * Call ->loo_object_init() repeatedly, until no more new
178                  * object slices are created.
179                  */
180                 clean = 1;
181                 cfs_list_for_each_entry(scan, layers, lo_linkage) {
182                         if (scan->lo_flags & LU_OBJECT_ALLOCATED)
183                                 continue;
184                         clean = 0;
185                         scan->lo_header = top->lo_header;
186                         result = scan->lo_ops->loo_object_init(env, scan, conf);
187                         if (result != 0) {
188                                 lu_object_free(env, top);
189                                 RETURN(ERR_PTR(result));
190                         }
191                         scan->lo_flags |= LU_OBJECT_ALLOCATED;
192                 }
193         } while (!clean);
194
195         cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
196                 if (scan->lo_ops->loo_object_start != NULL) {
197                         result = scan->lo_ops->loo_object_start(env, scan);
198                         if (result != 0) {
199                                 lu_object_free(env, top);
200                                 RETURN(ERR_PTR(result));
201                         }
202                 }
203         }
204
205         lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
206         RETURN(top);
207 }
208
209 /**
210  * Free an object.
211  */
212 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
213 {
214         struct lu_site_bkt_data *bkt;
215         struct lu_site          *site;
216         struct lu_object        *scan;
217         cfs_list_t              *layers;
218         cfs_list_t               splice;
219
220         site   = o->lo_dev->ld_site;
221         layers = &o->lo_header->loh_layers;
222         bkt    = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
223         /*
224          * First call ->loo_object_delete() method to release all resources.
225          */
226         cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
227                 if (scan->lo_ops->loo_object_delete != NULL)
228                         scan->lo_ops->loo_object_delete(env, scan);
229         }
230
231         /*
232          * Then, splice object layers into stand-alone list, and call
233          * ->loo_object_free() on all layers to free memory. Splice is
234          * necessary, because lu_object_header is freed together with the
235          * top-level slice.
236          */
237         CFS_INIT_LIST_HEAD(&splice);
238         cfs_list_splice_init(layers, &splice);
239         while (!cfs_list_empty(&splice)) {
240                 /*
241                  * Free layers in bottom-to-top order, so that object header
242                  * lives as long as possible and ->loo_object_free() methods
243                  * can look at its contents.
244                  */
245                 o = container_of0(splice.prev, struct lu_object, lo_linkage);
246                 cfs_list_del_init(&o->lo_linkage);
247                 LASSERT(o->lo_ops->loo_object_free != NULL);
248                 o->lo_ops->loo_object_free(env, o);
249         }
250
251         if (cfs_waitq_active(&bkt->lsb_marche_funebre))
252                 cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
253 }
254
255 /**
256  * Free \a nr objects from the cold end of the site LRU list.
257  */
258 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
259 {
260         struct lu_object_header *h;
261         struct lu_object_header *temp;
262         struct lu_site_bkt_data *bkt;
263         cfs_hash_bd_t            bd;
264         cfs_hash_bd_t            bd2;
265         cfs_list_t               dispose;
266         int                      did_sth;
267         int                      start;
268         int                      count;
269         int                      bnr;
270         int                      i;
271
272         CFS_INIT_LIST_HEAD(&dispose);
273         /*
274          * Under LRU list lock, scan LRU list and move unreferenced objects to
275          * the dispose list, removing them from LRU and hash table.
276          */
277         start = s->ls_purge_start;
278         bnr = (nr == ~0) ? -1 : nr / CFS_HASH_NBKT(s->ls_obj_hash) + 1;
279  again:
280         did_sth = 0;
281         cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
282                 if (i < start)
283                         continue;
284                 count = bnr;
285                 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
286                 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
287
288                 cfs_list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
289                         LASSERT(cfs_atomic_read(&h->loh_ref) == 0);
290
291                         cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
292                         LASSERT(bd.bd_bucket == bd2.bd_bucket);
293
294                         cfs_hash_bd_del_locked(s->ls_obj_hash,
295                                                &bd2, &h->loh_hash);
296                         cfs_list_move(&h->loh_lru, &dispose);
297                         if (did_sth == 0)
298                                 did_sth = 1;
299
300                         if (nr != ~0 && --nr == 0)
301                                 break;
302
303                         if (count > 0 && --count == 0)
304                                 break;
305
306                 }
307                 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
308                 cfs_cond_resched();
309                 /*
310                  * Free everything on the dispose list. This is safe against
311                  * races due to the reasons described in lu_object_put().
312                  */
313                 while (!cfs_list_empty(&dispose)) {
314                         h = container_of0(dispose.next,
315                                           struct lu_object_header, loh_lru);
316                         cfs_list_del_init(&h->loh_lru);
317                         lu_object_free(env, lu_object_top(h));
318                         lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
319                 }
320
321                 if (nr == 0)
322                         break;
323         }
324
325         if (nr != 0 && did_sth && start != 0) {
326                 start = 0; /* restart from the first bucket */
327                 goto again;
328         }
329         /* race on s->ls_purge_start, but nobody cares */
330         s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
331
332         return nr;
333 }
334 EXPORT_SYMBOL(lu_site_purge);
335
336 /*
337  * Object printing.
338  *
339  * Code below has to jump through certain loops to output object description
340  * into libcfs_debug_msg-based log. The problem is that lu_object_print()
341  * composes object description from strings that are parts of _lines_ of
342  * output (i.e., strings that are not terminated by newline). This doesn't fit
343  * very well into libcfs_debug_msg() interface that assumes that each message
344  * supplied to it is a self-contained output line.
345  *
346  * To work around this, strings are collected in a temporary buffer
347  * (implemented as a value of lu_cdebug_key key), until terminating newline
348  * character is detected.
349  *
350  */
351
352 enum {
353         /**
354          * Maximal line size.
355          *
356          * XXX overflow is not handled correctly.
357          */
358         LU_CDEBUG_LINE = 512
359 };
360
361 struct lu_cdebug_data {
362         /**
363          * Temporary buffer.
364          */
365         char lck_area[LU_CDEBUG_LINE];
366 };
367
368 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
369 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
370
371 /**
372  * Key, holding temporary buffer. This key is registered very early by
373  * lu_global_init().
374  */
375 struct lu_context_key lu_global_key = {
376         .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
377         .lct_init = lu_global_key_init,
378         .lct_fini = lu_global_key_fini
379 };
380
381 /**
382  * Printer function emitting messages through libcfs_debug_msg().
383  */
384 int lu_cdebug_printer(const struct lu_env *env,
385                       void *cookie, const char *format, ...)
386 {
387         struct lu_cdebug_print_info *info = cookie;
388         struct lu_cdebug_data       *key;
389         int used;
390         int complete;
391         va_list args;
392
393         va_start(args, format);
394
395         key = lu_context_key_get(&env->le_ctx, &lu_global_key);
396         LASSERT(key != NULL);
397
398         used = strlen(key->lck_area);
399         complete = format[strlen(format) - 1] == '\n';
400         /*
401          * Append new chunk to the buffer.
402          */
403         vsnprintf(key->lck_area + used,
404                   ARRAY_SIZE(key->lck_area) - used, format, args);
405         if (complete) {
406                 if (cfs_cdebug_show(info->lpi_mask, info->lpi_subsys))
407                         libcfs_debug_msg(NULL, info->lpi_subsys, info->lpi_mask,
408                                          (char *)info->lpi_file, info->lpi_fn,
409                                          info->lpi_line, "%s", key->lck_area);
410                 key->lck_area[0] = 0;
411         }
412         va_end(args);
413         return 0;
414 }
415 EXPORT_SYMBOL(lu_cdebug_printer);
416
417 /**
418  * Print object header.
419  */
420 void lu_object_header_print(const struct lu_env *env, void *cookie,
421                             lu_printer_t printer,
422                             const struct lu_object_header *hdr)
423 {
424         (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
425                    hdr, hdr->loh_flags, cfs_atomic_read(&hdr->loh_ref),
426                    PFID(&hdr->loh_fid),
427                    cfs_hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
428                    cfs_list_empty((cfs_list_t *)&hdr->loh_lru) ? \
429                    "" : " lru",
430                    hdr->loh_attr & LOHA_EXISTS ? " exist":"");
431 }
432 EXPORT_SYMBOL(lu_object_header_print);
433
434 /**
435  * Print human readable representation of the \a o to the \a printer.
436  */
437 void lu_object_print(const struct lu_env *env, void *cookie,
438                      lu_printer_t printer, const struct lu_object *o)
439 {
440         static const char ruler[] = "........................................";
441         struct lu_object_header *top;
442         int depth;
443
444         top = o->lo_header;
445         lu_object_header_print(env, cookie, printer, top);
446         (*printer)(env, cookie, "{ \n");
447         cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
448                 depth = o->lo_depth + 4;
449
450                 /*
451                  * print `.' \a depth times followed by type name and address
452                  */
453                 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
454                            o->lo_dev->ld_type->ldt_name, o);
455                 if (o->lo_ops->loo_object_print != NULL)
456                         o->lo_ops->loo_object_print(env, cookie, printer, o);
457                 (*printer)(env, cookie, "\n");
458         }
459         (*printer)(env, cookie, "} header@%p\n", top);
460 }
461 EXPORT_SYMBOL(lu_object_print);
462
463 /**
464  * Check object consistency.
465  */
466 int lu_object_invariant(const struct lu_object *o)
467 {
468         struct lu_object_header *top;
469
470         top = o->lo_header;
471         cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
472                 if (o->lo_ops->loo_object_invariant != NULL &&
473                     !o->lo_ops->loo_object_invariant(o))
474                         return 0;
475         }
476         return 1;
477 }
478 EXPORT_SYMBOL(lu_object_invariant);
479
480 static struct lu_object *htable_lookup(struct lu_site *s,
481                                        cfs_hash_bd_t *bd,
482                                        const struct lu_fid *f,
483                                        cfs_waitlink_t *waiter,
484                                        __u64 *version)
485 {
486         struct lu_site_bkt_data *bkt;
487         struct lu_object_header *h;
488         cfs_hlist_node_t        *hnode;
489         __u64  ver = cfs_hash_bd_version_get(bd);
490
491         if (*version == ver)
492                 return NULL;
493
494         *version = ver;
495         bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
496         /* cfs_hash_bd_lookup_intent is a somehow "internal" function
497          * of cfs_hash, but we don't want refcount on object right now */
498         hnode = cfs_hash_bd_lookup_locked(s->ls_obj_hash, bd, (void *)f);
499         if (hnode == NULL) {
500                 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
501                 return NULL;
502         }
503
504         h = container_of0(hnode, struct lu_object_header, loh_hash);
505         if (likely(!lu_object_is_dying(h))) {
506                 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
507                 cfs_list_del_init(&h->loh_lru);
508                 return lu_object_top(h);
509         }
510
511         /*
512          * Lookup found an object being destroyed this object cannot be
513          * returned (to assure that references to dying objects are eventually
514          * drained), and moreover, lookup has to wait until object is freed.
515          */
516         cfs_atomic_dec(&h->loh_ref);
517
518         cfs_waitlink_init(waiter);
519         cfs_waitq_add(&bkt->lsb_marche_funebre, waiter);
520         cfs_set_current_state(CFS_TASK_UNINT);
521         lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
522         return ERR_PTR(-EAGAIN);
523 }
524
525 /**
526  * Search cache for an object with the fid \a f. If such object is found,
527  * return it. Otherwise, create new object, insert it into cache and return
528  * it. In any case, additional reference is acquired on the returned object.
529  */
530 struct lu_object *lu_object_find(const struct lu_env *env,
531                                  struct lu_device *dev, const struct lu_fid *f,
532                                  const struct lu_object_conf *conf)
533 {
534         return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
535 }
536 EXPORT_SYMBOL(lu_object_find);
537
538 static struct lu_object *lu_object_new(const struct lu_env *env,
539                                        struct lu_device *dev,
540                                        const struct lu_fid *f,
541                                        const struct lu_object_conf *conf)
542 {
543         struct lu_object        *o;
544         cfs_hash_t              *hs;
545         cfs_hash_bd_t            bd;
546         struct lu_site_bkt_data *bkt;
547
548         o = lu_object_alloc(env, dev, f, conf);
549         if (unlikely(IS_ERR(o)))
550                 return o;
551
552         hs = dev->ld_site->ls_obj_hash;
553         cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
554         bkt = cfs_hash_bd_extra_get(hs, &bd);
555         cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
556         bkt->lsb_busy++;
557         cfs_hash_bd_unlock(hs, &bd, 1);
558         return o;
559 }
560
561 /**
562  * Core logic of lu_object_find*() functions.
563  */
564 static struct lu_object *lu_object_find_try(const struct lu_env *env,
565                                             struct lu_device *dev,
566                                             const struct lu_fid *f,
567                                             const struct lu_object_conf *conf,
568                                             cfs_waitlink_t *waiter)
569 {
570         struct lu_object      *o;
571         struct lu_object      *shadow;
572         struct lu_site        *s;
573         cfs_hash_t            *hs;
574         cfs_hash_bd_t          bd;
575         __u64                  version = 0;
576
577         /*
578          * This uses standard index maintenance protocol:
579          *
580          *     - search index under lock, and return object if found;
581          *     - otherwise, unlock index, allocate new object;
582          *     - lock index and search again;
583          *     - if nothing is found (usual case), insert newly created
584          *       object into index;
585          *     - otherwise (race: other thread inserted object), free
586          *       object just allocated.
587          *     - unlock index;
588          *     - return object.
589          *
590          * For "LOC_F_NEW" case, we are sure the object is new established.
591          * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
592          * just alloc and insert directly.
593          *
594          * If dying object is found during index search, add @waiter to the
595          * site wait-queue and return ERR_PTR(-EAGAIN).
596          */
597         if (conf != NULL && conf->loc_flags & LOC_F_NEW)
598                 return lu_object_new(env, dev, f, conf);
599
600         s  = dev->ld_site;
601         hs = s->ls_obj_hash;
602         cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
603         o = htable_lookup(s, &bd, f, waiter, &version);
604         cfs_hash_bd_unlock(hs, &bd, 1);
605         if (o != NULL)
606                 return o;
607
608         /*
609          * Allocate new object. This may result in rather complicated
610          * operations, including fld queries, inode loading, etc.
611          */
612         o = lu_object_alloc(env, dev, f, conf);
613         if (unlikely(IS_ERR(o)))
614                 return o;
615
616         LASSERT(lu_fid_eq(lu_object_fid(o), f));
617
618         cfs_hash_bd_lock(hs, &bd, 1);
619
620         shadow = htable_lookup(s, &bd, f, waiter, &version);
621         if (likely(shadow == NULL)) {
622                 struct lu_site_bkt_data *bkt;
623
624                 bkt = cfs_hash_bd_extra_get(hs, &bd);
625                 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
626                 bkt->lsb_busy++;
627                 cfs_hash_bd_unlock(hs, &bd, 1);
628                 return o;
629         }
630
631         lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
632         cfs_hash_bd_unlock(hs, &bd, 1);
633         lu_object_free(env, o);
634         return shadow;
635 }
636
637 /**
638  * Much like lu_object_find(), but top level device of object is specifically
639  * \a dev rather than top level device of the site. This interface allows
640  * objects of different "stacking" to be created within the same site.
641  */
642 struct lu_object *lu_object_find_at(const struct lu_env *env,
643                                     struct lu_device *dev,
644                                     const struct lu_fid *f,
645                                     const struct lu_object_conf *conf)
646 {
647         struct lu_site_bkt_data *bkt;
648         struct lu_object        *obj;
649         cfs_waitlink_t           wait;
650
651         while (1) {
652                 obj = lu_object_find_try(env, dev, f, conf, &wait);
653                 if (obj != ERR_PTR(-EAGAIN))
654                         return obj;
655                 /*
656                  * lu_object_find_try() already added waiter into the
657                  * wait queue.
658                  */
659                 cfs_waitq_wait(&wait, CFS_TASK_UNINT);
660                 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
661                 cfs_waitq_del(&bkt->lsb_marche_funebre, &wait);
662         }
663 }
664 EXPORT_SYMBOL(lu_object_find_at);
665
666 /**
667  * Find object with given fid, and return its slice belonging to given device.
668  */
669 struct lu_object *lu_object_find_slice(const struct lu_env *env,
670                                        struct lu_device *dev,
671                                        const struct lu_fid *f,
672                                        const struct lu_object_conf *conf)
673 {
674         struct lu_object *top;
675         struct lu_object *obj;
676
677         top = lu_object_find(env, dev, f, conf);
678         if (!IS_ERR(top)) {
679                 obj = lu_object_locate(top->lo_header, dev->ld_type);
680                 if (obj == NULL)
681                         lu_object_put(env, top);
682         } else
683                 obj = top;
684         return obj;
685 }
686 EXPORT_SYMBOL(lu_object_find_slice);
687
688 /**
689  * Global list of all device types.
690  */
691 static CFS_LIST_HEAD(lu_device_types);
692
693 int lu_device_type_init(struct lu_device_type *ldt)
694 {
695         int result;
696
697         CFS_INIT_LIST_HEAD(&ldt->ldt_linkage);
698         result = ldt->ldt_ops->ldto_init(ldt);
699         if (result == 0)
700                 cfs_list_add(&ldt->ldt_linkage, &lu_device_types);
701         return result;
702 }
703 EXPORT_SYMBOL(lu_device_type_init);
704
705 void lu_device_type_fini(struct lu_device_type *ldt)
706 {
707         cfs_list_del_init(&ldt->ldt_linkage);
708         ldt->ldt_ops->ldto_fini(ldt);
709 }
710 EXPORT_SYMBOL(lu_device_type_fini);
711
712 void lu_types_stop(void)
713 {
714         struct lu_device_type *ldt;
715
716         cfs_list_for_each_entry(ldt, &lu_device_types, ldt_linkage) {
717                 if (ldt->ldt_device_nr == 0)
718                         ldt->ldt_ops->ldto_stop(ldt);
719         }
720 }
721 EXPORT_SYMBOL(lu_types_stop);
722
723 /**
724  * Global list of all sites on this node
725  */
726 static CFS_LIST_HEAD(lu_sites);
727 static CFS_DECLARE_MUTEX(lu_sites_guard);
728
729 /**
730  * Global environment used by site shrinker.
731  */
732 static struct lu_env lu_shrink_env;
733
734 struct lu_site_print_arg {
735         struct lu_env   *lsp_env;
736         void            *lsp_cookie;
737         lu_printer_t     lsp_printer;
738 };
739
740 static int
741 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
742                   cfs_hlist_node_t *hnode, void *data)
743 {
744         struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
745         struct lu_object_header  *h;
746
747         h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
748         if (!cfs_list_empty(&h->loh_layers)) {
749                 const struct lu_object *o;
750
751                 o = lu_object_top(h);
752                 lu_object_print(arg->lsp_env, arg->lsp_cookie,
753                                 arg->lsp_printer, o);
754         } else {
755                 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
756                                        arg->lsp_printer, h);
757         }
758         return 0;
759 }
760
761 /**
762  * Print all objects in \a s.
763  */
764 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
765                    lu_printer_t printer)
766 {
767         struct lu_site_print_arg arg = {
768                 .lsp_env     = (struct lu_env *)env,
769                 .lsp_cookie  = cookie,
770                 .lsp_printer = printer,
771         };
772
773         cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
774 }
775 EXPORT_SYMBOL(lu_site_print);
776
777 enum {
778         LU_CACHE_PERCENT_MAX     = 50,
779         LU_CACHE_PERCENT_DEFAULT = 20
780 };
781
782 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
783 CFS_MODULE_PARM(lu_cache_percent, "i", int, 0644,
784                 "Percentage of memory to be used as lu_object cache");
785
786 /**
787  * Return desired hash table order.
788  */
789 static int lu_htable_order(void)
790 {
791         unsigned long cache_size;
792         int bits;
793
794         /*
795          * Calculate hash table size, assuming that we want reasonable
796          * performance when 20% of total memory is occupied by cache of
797          * lu_objects.
798          *
799          * Size of lu_object is (arbitrary) taken as 1K (together with inode).
800          */
801         cache_size = cfs_num_physpages;
802
803 #if BITS_PER_LONG == 32
804         /* limit hashtable size for lowmem systems to low RAM */
805         if (cache_size > 1 << (30 - CFS_PAGE_SHIFT))
806                 cache_size = 1 << (30 - CFS_PAGE_SHIFT) * 3 / 4;
807 #endif
808
809         /* clear off unreasonable cache setting. */
810         if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
811                 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
812                       " the range of (0, %u]. Will use default value: %u.\n",
813                       lu_cache_percent, LU_CACHE_PERCENT_MAX,
814                       LU_CACHE_PERCENT_DEFAULT);
815
816                 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
817         }
818         cache_size = cache_size / 100 * lu_cache_percent *
819                 (CFS_PAGE_SIZE / 1024);
820
821         for (bits = 1; (1 << bits) < cache_size; ++bits) {
822                 ;
823         }
824         return bits;
825 }
826
827 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
828                                 const void *key, unsigned mask)
829 {
830         struct lu_fid  *fid = (struct lu_fid *)key;
831         __u32           hash;
832
833         hash = fid_flatten32(fid);
834         hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
835         hash = cfs_hash_long(hash, hs->hs_bkt_bits);
836
837         /* give me another random factor */
838         hash -= cfs_hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
839
840         hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
841         hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
842
843         return hash & mask;
844 }
845
846 static void *lu_obj_hop_object(cfs_hlist_node_t *hnode)
847 {
848         return cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
849 }
850
851 static void *lu_obj_hop_key(cfs_hlist_node_t *hnode)
852 {
853         struct lu_object_header *h;
854
855         h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
856         return &h->loh_fid;
857 }
858
859 static int lu_obj_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
860 {
861         struct lu_object_header *h;
862
863         h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
864         return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
865 }
866
867 static void lu_obj_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
868 {
869         struct lu_object_header *h;
870
871         h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
872         if (cfs_atomic_add_return(1, &h->loh_ref) == 1) {
873                 struct lu_site_bkt_data *bkt;
874                 cfs_hash_bd_t            bd;
875
876                 cfs_hash_bd_get(hs, &h->loh_fid, &bd);
877                 bkt = cfs_hash_bd_extra_get(hs, &bd);
878                 bkt->lsb_busy++;
879         }
880 }
881
882 static void lu_obj_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
883 {
884         LBUG(); /* we should never called it */
885 }
886
887 cfs_hash_ops_t lu_site_hash_ops = {
888         .hs_hash        = lu_obj_hop_hash,
889         .hs_key         = lu_obj_hop_key,
890         .hs_keycmp      = lu_obj_hop_keycmp,
891         .hs_object      = lu_obj_hop_object,
892         .hs_get         = lu_obj_hop_get,
893         .hs_put_locked  = lu_obj_hop_put_locked,
894 };
895
896 /**
897  * Initialize site \a s, with \a d as the top level device.
898  */
899 #define LU_SITE_BITS_MIN    12
900 #define LU_SITE_BITS_MAX    24
901 /**
902  * total 256 buckets, we don't want too many buckets because:
903  * - consume too much memory
904  * - avoid unbalanced LRU list
905  */
906 #define LU_SITE_BKT_BITS    8
907
908 int lu_site_init(struct lu_site *s, struct lu_device *top)
909 {
910         struct lu_site_bkt_data *bkt;
911         cfs_hash_bd_t bd;
912         char name[16];
913         int bits;
914         int i;
915         ENTRY;
916
917         memset(s, 0, sizeof *s);
918         bits = lu_htable_order();
919         snprintf(name, 16, "lu_site_%s", top->ld_type->ldt_name);
920         for (bits = min(max(LU_SITE_BITS_MIN, bits), LU_SITE_BITS_MAX);
921              bits >= LU_SITE_BITS_MIN; bits--) {
922                 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
923                                                  bits - LU_SITE_BKT_BITS,
924                                                  sizeof(*bkt), 0, 0,
925                                                  &lu_site_hash_ops,
926                                                  CFS_HASH_SPIN_BKTLOCK |
927                                                  CFS_HASH_NO_ITEMREF |
928                                                  CFS_HASH_DEPTH |
929                                                  CFS_HASH_ASSERT_EMPTY);
930                 if (s->ls_obj_hash != NULL)
931                         break;
932         }
933
934         if (s->ls_obj_hash == NULL) {
935                 CERROR("failed to create lu_site hash with bits: %d\n", bits);
936                 return -ENOMEM;
937         }
938
939         cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
940                 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
941                 CFS_INIT_LIST_HEAD(&bkt->lsb_lru);
942                 cfs_waitq_init(&bkt->lsb_marche_funebre);
943         }
944
945         s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
946         if (s->ls_stats == NULL) {
947                 cfs_hash_putref(s->ls_obj_hash);
948                 s->ls_obj_hash = NULL;
949                 return -ENOMEM;
950         }
951
952         lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
953                              0, "created", "created");
954         lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
955                              0, "cache_hit", "cache_hit");
956         lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
957                              0, "cache_miss", "cache_miss");
958         lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
959                              0, "cache_race", "cache_race");
960         lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
961                              0, "cache_death_race", "cache_death_race");
962         lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
963                              0, "lru_purged", "lru_purged");
964
965         CFS_INIT_LIST_HEAD(&s->ls_linkage);
966         s->ls_top_dev = top;
967         top->ld_site = s;
968         lu_device_get(top);
969         lu_ref_add(&top->ld_reference, "site-top", s);
970
971         CFS_INIT_LIST_HEAD(&s->ls_ld_linkage);
972         cfs_spin_lock_init(&s->ls_ld_lock);
973
974         cfs_spin_lock(&s->ls_ld_lock);
975         cfs_list_add(&top->ld_linkage, &s->ls_ld_linkage);
976         cfs_spin_unlock(&s->ls_ld_lock);
977
978         RETURN(0);
979 }
980 EXPORT_SYMBOL(lu_site_init);
981
982 /**
983  * Finalize \a s and release its resources.
984  */
985 void lu_site_fini(struct lu_site *s)
986 {
987         cfs_down(&lu_sites_guard);
988         cfs_list_del_init(&s->ls_linkage);
989         cfs_up(&lu_sites_guard);
990
991         if (s->ls_obj_hash != NULL) {
992                 cfs_hash_putref(s->ls_obj_hash);
993                 s->ls_obj_hash = NULL;
994         }
995
996         if (s->ls_top_dev != NULL) {
997                 s->ls_top_dev->ld_site = NULL;
998                 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
999                 lu_device_put(s->ls_top_dev);
1000                 s->ls_top_dev = NULL;
1001         }
1002
1003         if (s->ls_stats != NULL)
1004                 lprocfs_free_stats(&s->ls_stats);
1005 }
1006 EXPORT_SYMBOL(lu_site_fini);
1007
1008 /**
1009  * Called when initialization of stack for this site is completed.
1010  */
1011 int lu_site_init_finish(struct lu_site *s)
1012 {
1013         int result;
1014         cfs_down(&lu_sites_guard);
1015         result = lu_context_refill(&lu_shrink_env.le_ctx);
1016         if (result == 0)
1017                 cfs_list_add(&s->ls_linkage, &lu_sites);
1018         cfs_up(&lu_sites_guard);
1019         return result;
1020 }
1021 EXPORT_SYMBOL(lu_site_init_finish);
1022
1023 /**
1024  * Acquire additional reference on device \a d
1025  */
1026 void lu_device_get(struct lu_device *d)
1027 {
1028         cfs_atomic_inc(&d->ld_ref);
1029 }
1030 EXPORT_SYMBOL(lu_device_get);
1031
1032 /**
1033  * Release reference on device \a d.
1034  */
1035 void lu_device_put(struct lu_device *d)
1036 {
1037         LASSERT(cfs_atomic_read(&d->ld_ref) > 0);
1038         cfs_atomic_dec(&d->ld_ref);
1039 }
1040 EXPORT_SYMBOL(lu_device_put);
1041
1042 /**
1043  * Initialize device \a d of type \a t.
1044  */
1045 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1046 {
1047         if (t->ldt_device_nr++ == 0 && t->ldt_ops->ldto_start != NULL)
1048                 t->ldt_ops->ldto_start(t);
1049         memset(d, 0, sizeof *d);
1050         cfs_atomic_set(&d->ld_ref, 0);
1051         d->ld_type = t;
1052         lu_ref_init(&d->ld_reference);
1053         CFS_INIT_LIST_HEAD(&d->ld_linkage);
1054         return 0;
1055 }
1056 EXPORT_SYMBOL(lu_device_init);
1057
1058 /**
1059  * Finalize device \a d.
1060  */
1061 void lu_device_fini(struct lu_device *d)
1062 {
1063         struct lu_device_type *t;
1064
1065         t = d->ld_type;
1066         if (d->ld_obd != NULL) {
1067                 d->ld_obd->obd_lu_dev = NULL;
1068                 d->ld_obd = NULL;
1069         }
1070
1071         lu_ref_fini(&d->ld_reference);
1072         LASSERTF(cfs_atomic_read(&d->ld_ref) == 0,
1073                  "Refcount is %u\n", cfs_atomic_read(&d->ld_ref));
1074         LASSERT(t->ldt_device_nr > 0);
1075         if (--t->ldt_device_nr == 0 && t->ldt_ops->ldto_stop != NULL)
1076                 t->ldt_ops->ldto_stop(t);
1077 }
1078 EXPORT_SYMBOL(lu_device_fini);
1079
1080 /**
1081  * Initialize object \a o that is part of compound object \a h and was created
1082  * by device \a d.
1083  */
1084 int lu_object_init(struct lu_object *o,
1085                    struct lu_object_header *h, struct lu_device *d)
1086 {
1087         memset(o, 0, sizeof *o);
1088         o->lo_header = h;
1089         o->lo_dev    = d;
1090         lu_device_get(d);
1091         o->lo_dev_ref = lu_ref_add(&d->ld_reference, "lu_object", o);
1092         CFS_INIT_LIST_HEAD(&o->lo_linkage);
1093         return 0;
1094 }
1095 EXPORT_SYMBOL(lu_object_init);
1096
1097 /**
1098  * Finalize object and release its resources.
1099  */
1100 void lu_object_fini(struct lu_object *o)
1101 {
1102         struct lu_device *dev = o->lo_dev;
1103
1104         LASSERT(cfs_list_empty(&o->lo_linkage));
1105
1106         if (dev != NULL) {
1107                 lu_ref_del_at(&dev->ld_reference,
1108                               o->lo_dev_ref , "lu_object", o);
1109                 lu_device_put(dev);
1110                 o->lo_dev = NULL;
1111         }
1112 }
1113 EXPORT_SYMBOL(lu_object_fini);
1114
1115 /**
1116  * Add object \a o as first layer of compound object \a h
1117  *
1118  * This is typically called by the ->ldo_object_alloc() method of top-level
1119  * device.
1120  */
1121 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1122 {
1123         cfs_list_move(&o->lo_linkage, &h->loh_layers);
1124 }
1125 EXPORT_SYMBOL(lu_object_add_top);
1126
1127 /**
1128  * Add object \a o as a layer of compound object, going after \a before.
1129  *
1130  * This is typically called by the ->ldo_object_alloc() method of \a
1131  * before->lo_dev.
1132  */
1133 void lu_object_add(struct lu_object *before, struct lu_object *o)
1134 {
1135         cfs_list_move(&o->lo_linkage, &before->lo_linkage);
1136 }
1137 EXPORT_SYMBOL(lu_object_add);
1138
1139 /**
1140  * Initialize compound object.
1141  */
1142 int lu_object_header_init(struct lu_object_header *h)
1143 {
1144         memset(h, 0, sizeof *h);
1145         cfs_atomic_set(&h->loh_ref, 1);
1146         CFS_INIT_HLIST_NODE(&h->loh_hash);
1147         CFS_INIT_LIST_HEAD(&h->loh_lru);
1148         CFS_INIT_LIST_HEAD(&h->loh_layers);
1149         lu_ref_init(&h->loh_reference);
1150         return 0;
1151 }
1152 EXPORT_SYMBOL(lu_object_header_init);
1153
1154 /**
1155  * Finalize compound object.
1156  */
1157 void lu_object_header_fini(struct lu_object_header *h)
1158 {
1159         LASSERT(cfs_list_empty(&h->loh_layers));
1160         LASSERT(cfs_list_empty(&h->loh_lru));
1161         LASSERT(cfs_hlist_unhashed(&h->loh_hash));
1162         lu_ref_fini(&h->loh_reference);
1163 }
1164 EXPORT_SYMBOL(lu_object_header_fini);
1165
1166 /**
1167  * Given a compound object, find its slice, corresponding to the device type
1168  * \a dtype.
1169  */
1170 struct lu_object *lu_object_locate(struct lu_object_header *h,
1171                                    const struct lu_device_type *dtype)
1172 {
1173         struct lu_object *o;
1174
1175         cfs_list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1176                 if (o->lo_dev->ld_type == dtype)
1177                         return o;
1178         }
1179         return NULL;
1180 }
1181 EXPORT_SYMBOL(lu_object_locate);
1182
1183
1184
1185 /**
1186  * Finalize and free devices in the device stack.
1187  *
1188  * Finalize device stack by purging object cache, and calling
1189  * lu_device_type_operations::ldto_device_fini() and
1190  * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1191  */
1192 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1193 {
1194         struct lu_site   *site = top->ld_site;
1195         struct lu_device *scan;
1196         struct lu_device *next;
1197
1198         lu_site_purge(env, site, ~0);
1199         for (scan = top; scan != NULL; scan = next) {
1200                 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1201                 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1202                 lu_device_put(scan);
1203         }
1204
1205         /* purge again. */
1206         lu_site_purge(env, site, ~0);
1207
1208         if (!cfs_hash_is_empty(site->ls_obj_hash)) {
1209                 /*
1210                  * Uh-oh, objects still exist.
1211                  */
1212                 static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
1213
1214                 lu_site_print(env, site, &cookie, lu_cdebug_printer);
1215         }
1216
1217         for (scan = top; scan != NULL; scan = next) {
1218                 const struct lu_device_type *ldt = scan->ld_type;
1219                 struct obd_type             *type;
1220
1221                 next = ldt->ldt_ops->ldto_device_free(env, scan);
1222                 type = ldt->ldt_obd_type;
1223                 if (type != NULL) {
1224                         type->typ_refcnt--;
1225                         class_put_type(type);
1226                 }
1227         }
1228 }
1229 EXPORT_SYMBOL(lu_stack_fini);
1230
1231 enum {
1232         /**
1233          * Maximal number of tld slots.
1234          */
1235         LU_CONTEXT_KEY_NR = 32
1236 };
1237
1238 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1239
1240 static cfs_spinlock_t lu_keys_guard = CFS_SPIN_LOCK_UNLOCKED;
1241
1242 /**
1243  * Global counter incremented whenever key is registered, unregistered,
1244  * revived or quiesced. This is used to void unnecessary calls to
1245  * lu_context_refill(). No locking is provided, as initialization and shutdown
1246  * are supposed to be externally serialized.
1247  */
1248 static unsigned key_set_version = 0;
1249
1250 /**
1251  * Register new key.
1252  */
1253 int lu_context_key_register(struct lu_context_key *key)
1254 {
1255         int result;
1256         int i;
1257
1258         LASSERT(key->lct_init != NULL);
1259         LASSERT(key->lct_fini != NULL);
1260         LASSERT(key->lct_tags != 0);
1261         LASSERT(key->lct_owner != NULL);
1262
1263         result = -ENFILE;
1264         cfs_spin_lock(&lu_keys_guard);
1265         for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1266                 if (lu_keys[i] == NULL) {
1267                         key->lct_index = i;
1268                         cfs_atomic_set(&key->lct_used, 1);
1269                         lu_keys[i] = key;
1270                         lu_ref_init(&key->lct_reference);
1271                         result = 0;
1272                         ++key_set_version;
1273                         break;
1274                 }
1275         }
1276         cfs_spin_unlock(&lu_keys_guard);
1277         return result;
1278 }
1279 EXPORT_SYMBOL(lu_context_key_register);
1280
1281 static void key_fini(struct lu_context *ctx, int index)
1282 {
1283         if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1284                 struct lu_context_key *key;
1285
1286                 key = lu_keys[index];
1287                 LASSERT(key != NULL);
1288                 LASSERT(key->lct_fini != NULL);
1289                 LASSERT(cfs_atomic_read(&key->lct_used) > 1);
1290
1291                 key->lct_fini(ctx, key, ctx->lc_value[index]);
1292                 lu_ref_del(&key->lct_reference, "ctx", ctx);
1293                 cfs_atomic_dec(&key->lct_used);
1294                 LASSERT(key->lct_owner != NULL);
1295                 if (!(ctx->lc_tags & LCT_NOREF)) {
1296                         LASSERT(cfs_module_refcount(key->lct_owner) > 0);
1297                         cfs_module_put(key->lct_owner);
1298                 }
1299                 ctx->lc_value[index] = NULL;
1300         }
1301 }
1302
1303 /**
1304  * Deregister key.
1305  */
1306 void lu_context_key_degister(struct lu_context_key *key)
1307 {
1308         LASSERT(cfs_atomic_read(&key->lct_used) >= 1);
1309         LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1310
1311         lu_context_key_quiesce(key);
1312
1313         ++key_set_version;
1314         cfs_spin_lock(&lu_keys_guard);
1315         key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1316         if (lu_keys[key->lct_index]) {
1317                 lu_keys[key->lct_index] = NULL;
1318                 lu_ref_fini(&key->lct_reference);
1319         }
1320         cfs_spin_unlock(&lu_keys_guard);
1321
1322         LASSERTF(cfs_atomic_read(&key->lct_used) == 1,
1323                  "key has instances: %d\n",
1324                  cfs_atomic_read(&key->lct_used));
1325 }
1326 EXPORT_SYMBOL(lu_context_key_degister);
1327
1328 /**
1329  * Register a number of keys. This has to be called after all keys have been
1330  * initialized by a call to LU_CONTEXT_KEY_INIT().
1331  */
1332 int lu_context_key_register_many(struct lu_context_key *k, ...)
1333 {
1334         struct lu_context_key *key = k;
1335         va_list args;
1336         int result;
1337
1338         va_start(args, k);
1339         do {
1340                 result = lu_context_key_register(key);
1341                 if (result)
1342                         break;
1343                 key = va_arg(args, struct lu_context_key *);
1344         } while (key != NULL);
1345         va_end(args);
1346
1347         if (result != 0) {
1348                 va_start(args, k);
1349                 while (k != key) {
1350                         lu_context_key_degister(k);
1351                         k = va_arg(args, struct lu_context_key *);
1352                 }
1353                 va_end(args);
1354         }
1355
1356         return result;
1357 }
1358 EXPORT_SYMBOL(lu_context_key_register_many);
1359
1360 /**
1361  * De-register a number of keys. This is a dual to
1362  * lu_context_key_register_many().
1363  */
1364 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1365 {
1366         va_list args;
1367
1368         va_start(args, k);
1369         do {
1370                 lu_context_key_degister(k);
1371                 k = va_arg(args, struct lu_context_key*);
1372         } while (k != NULL);
1373         va_end(args);
1374 }
1375 EXPORT_SYMBOL(lu_context_key_degister_many);
1376
1377 /**
1378  * Revive a number of keys.
1379  */
1380 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1381 {
1382         va_list args;
1383
1384         va_start(args, k);
1385         do {
1386                 lu_context_key_revive(k);
1387                 k = va_arg(args, struct lu_context_key*);
1388         } while (k != NULL);
1389         va_end(args);
1390 }
1391 EXPORT_SYMBOL(lu_context_key_revive_many);
1392
1393 /**
1394  * Quiescent a number of keys.
1395  */
1396 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1397 {
1398         va_list args;
1399
1400         va_start(args, k);
1401         do {
1402                 lu_context_key_quiesce(k);
1403                 k = va_arg(args, struct lu_context_key*);
1404         } while (k != NULL);
1405         va_end(args);
1406 }
1407 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1408
1409 /**
1410  * Return value associated with key \a key in context \a ctx.
1411  */
1412 void *lu_context_key_get(const struct lu_context *ctx,
1413                          const struct lu_context_key *key)
1414 {
1415         LINVRNT(ctx->lc_state == LCS_ENTERED);
1416         LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1417         LASSERT(lu_keys[key->lct_index] == key);
1418         return ctx->lc_value[key->lct_index];
1419 }
1420 EXPORT_SYMBOL(lu_context_key_get);
1421
1422 /**
1423  * List of remembered contexts. XXX document me.
1424  */
1425 static CFS_LIST_HEAD(lu_context_remembered);
1426
1427 /**
1428  * Destroy \a key in all remembered contexts. This is used to destroy key
1429  * values in "shared" contexts (like service threads), when a module owning
1430  * the key is about to be unloaded.
1431  */
1432 void lu_context_key_quiesce(struct lu_context_key *key)
1433 {
1434         struct lu_context *ctx;
1435         extern unsigned cl_env_cache_purge(unsigned nr);
1436
1437         if (!(key->lct_tags & LCT_QUIESCENT)) {
1438                 /*
1439                  * XXX layering violation.
1440                  */
1441                 cl_env_cache_purge(~0);
1442                 key->lct_tags |= LCT_QUIESCENT;
1443                 /*
1444                  * XXX memory barrier has to go here.
1445                  */
1446                 cfs_spin_lock(&lu_keys_guard);
1447                 cfs_list_for_each_entry(ctx, &lu_context_remembered,
1448                                         lc_remember)
1449                         key_fini(ctx, key->lct_index);
1450                 cfs_spin_unlock(&lu_keys_guard);
1451                 ++key_set_version;
1452         }
1453 }
1454 EXPORT_SYMBOL(lu_context_key_quiesce);
1455
1456 void lu_context_key_revive(struct lu_context_key *key)
1457 {
1458         key->lct_tags &= ~LCT_QUIESCENT;
1459         ++key_set_version;
1460 }
1461 EXPORT_SYMBOL(lu_context_key_revive);
1462
1463 static void keys_fini(struct lu_context *ctx)
1464 {
1465         int i;
1466
1467         cfs_spin_lock(&lu_keys_guard);
1468         if (ctx->lc_value != NULL) {
1469                 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1470                         key_fini(ctx, i);
1471                 OBD_FREE(ctx->lc_value,
1472                          ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1473                 ctx->lc_value = NULL;
1474         }
1475         cfs_spin_unlock(&lu_keys_guard);
1476 }
1477
1478 static int keys_fill(struct lu_context *ctx)
1479 {
1480         int i;
1481
1482         for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1483                 struct lu_context_key *key;
1484
1485                 key = lu_keys[i];
1486                 if (ctx->lc_value[i] == NULL && key != NULL &&
1487                     (key->lct_tags & ctx->lc_tags) &&
1488                     /*
1489                      * Don't create values for a LCT_QUIESCENT key, as this
1490                      * will pin module owning a key.
1491                      */
1492                     !(key->lct_tags & LCT_QUIESCENT)) {
1493                         void *value;
1494
1495                         LINVRNT(key->lct_init != NULL);
1496                         LINVRNT(key->lct_index == i);
1497
1498                         value = key->lct_init(ctx, key);
1499                         if (unlikely(IS_ERR(value)))
1500                                 return PTR_ERR(value);
1501
1502                         LASSERT(key->lct_owner != NULL);
1503                         if (!(ctx->lc_tags & LCT_NOREF))
1504                                 cfs_try_module_get(key->lct_owner);
1505                         lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1506                         cfs_atomic_inc(&key->lct_used);
1507                         /*
1508                          * This is the only place in the code, where an
1509                          * element of ctx->lc_value[] array is set to non-NULL
1510                          * value.
1511                          */
1512                         ctx->lc_value[i] = value;
1513                         if (key->lct_exit != NULL)
1514                                 ctx->lc_tags |= LCT_HAS_EXIT;
1515                 }
1516                 ctx->lc_version = key_set_version;
1517         }
1518         return 0;
1519 }
1520
1521 static int keys_init(struct lu_context *ctx)
1522 {
1523         int result;
1524
1525         OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1526         if (likely(ctx->lc_value != NULL))
1527                 result = keys_fill(ctx);
1528         else
1529                 result = -ENOMEM;
1530
1531         if (result != 0)
1532                 keys_fini(ctx);
1533         return result;
1534 }
1535
1536 /**
1537  * Initialize context data-structure. Create values for all keys.
1538  */
1539 int lu_context_init(struct lu_context *ctx, __u32 tags)
1540 {
1541         memset(ctx, 0, sizeof *ctx);
1542         ctx->lc_state = LCS_INITIALIZED;
1543         ctx->lc_tags = tags;
1544         if (tags & LCT_REMEMBER) {
1545                 cfs_spin_lock(&lu_keys_guard);
1546                 cfs_list_add(&ctx->lc_remember, &lu_context_remembered);
1547                 cfs_spin_unlock(&lu_keys_guard);
1548         } else
1549                 CFS_INIT_LIST_HEAD(&ctx->lc_remember);
1550         return keys_init(ctx);
1551 }
1552 EXPORT_SYMBOL(lu_context_init);
1553
1554 /**
1555  * Finalize context data-structure. Destroy key values.
1556  */
1557 void lu_context_fini(struct lu_context *ctx)
1558 {
1559         LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1560         ctx->lc_state = LCS_FINALIZED;
1561         keys_fini(ctx);
1562         cfs_spin_lock(&lu_keys_guard);
1563         cfs_list_del_init(&ctx->lc_remember);
1564         cfs_spin_unlock(&lu_keys_guard);
1565 }
1566 EXPORT_SYMBOL(lu_context_fini);
1567
1568 /**
1569  * Called before entering context.
1570  */
1571 void lu_context_enter(struct lu_context *ctx)
1572 {
1573         LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1574         ctx->lc_state = LCS_ENTERED;
1575 }
1576 EXPORT_SYMBOL(lu_context_enter);
1577
1578 /**
1579  * Called after exiting from \a ctx
1580  */
1581 void lu_context_exit(struct lu_context *ctx)
1582 {
1583         int i;
1584
1585         LINVRNT(ctx->lc_state == LCS_ENTERED);
1586         ctx->lc_state = LCS_LEFT;
1587         if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1588                 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1589                         if (ctx->lc_value[i] != NULL) {
1590                                 struct lu_context_key *key;
1591
1592                                 key = lu_keys[i];
1593                                 LASSERT(key != NULL);
1594                                 if (key->lct_exit != NULL)
1595                                         key->lct_exit(ctx,
1596                                                       key, ctx->lc_value[i]);
1597                         }
1598                 }
1599         }
1600 }
1601 EXPORT_SYMBOL(lu_context_exit);
1602
1603 /**
1604  * Allocate for context all missing keys that were registered after context
1605  * creation.
1606  */
1607 int lu_context_refill(struct lu_context *ctx)
1608 {
1609         LINVRNT(ctx->lc_value != NULL);
1610         return ctx->lc_version == key_set_version ? 0 : keys_fill(ctx);
1611 }
1612 EXPORT_SYMBOL(lu_context_refill);
1613
1614 /**
1615  * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1616  * obd being added. Currently, this is only used on client side, specifically
1617  * for echo device client, for other stack (like ptlrpc threads), context are
1618  * predefined when the lu_device type are registered, during the module probe
1619  * phase.
1620  */
1621 __u32 lu_context_tags_default = 0;
1622 __u32 lu_session_tags_default = 0;
1623
1624 void lu_context_tags_update(__u32 tags)
1625 {
1626         cfs_spin_lock(&lu_keys_guard);
1627         lu_context_tags_default |= tags;
1628         key_set_version ++;
1629         cfs_spin_unlock(&lu_keys_guard);
1630 }
1631 EXPORT_SYMBOL(lu_context_tags_update);
1632
1633 void lu_context_tags_clear(__u32 tags)
1634 {
1635         cfs_spin_lock(&lu_keys_guard);
1636         lu_context_tags_default &= ~tags;
1637         key_set_version ++;
1638         cfs_spin_unlock(&lu_keys_guard);
1639 }
1640 EXPORT_SYMBOL(lu_context_tags_clear);
1641
1642 void lu_session_tags_update(__u32 tags)
1643 {
1644         cfs_spin_lock(&lu_keys_guard);
1645         lu_session_tags_default |= tags;
1646         key_set_version ++;
1647         cfs_spin_unlock(&lu_keys_guard);
1648 }
1649 EXPORT_SYMBOL(lu_session_tags_update);
1650
1651 void lu_session_tags_clear(__u32 tags)
1652 {
1653         cfs_spin_lock(&lu_keys_guard);
1654         lu_session_tags_default &= ~tags;
1655         key_set_version ++;
1656         cfs_spin_unlock(&lu_keys_guard);
1657 }
1658 EXPORT_SYMBOL(lu_session_tags_clear);
1659
1660 int lu_env_init(struct lu_env *env, __u32 tags)
1661 {
1662         int result;
1663
1664         env->le_ses = NULL;
1665         result = lu_context_init(&env->le_ctx, tags);
1666         if (likely(result == 0))
1667                 lu_context_enter(&env->le_ctx);
1668         return result;
1669 }
1670 EXPORT_SYMBOL(lu_env_init);
1671
1672 void lu_env_fini(struct lu_env *env)
1673 {
1674         lu_context_exit(&env->le_ctx);
1675         lu_context_fini(&env->le_ctx);
1676         env->le_ses = NULL;
1677 }
1678 EXPORT_SYMBOL(lu_env_fini);
1679
1680 int lu_env_refill(struct lu_env *env)
1681 {
1682         int result;
1683
1684         result = lu_context_refill(&env->le_ctx);
1685         if (result == 0 && env->le_ses != NULL)
1686                 result = lu_context_refill(env->le_ses);
1687         return result;
1688 }
1689 EXPORT_SYMBOL(lu_env_refill);
1690
1691 /**
1692  * Currently, this API will only be used by echo client.
1693  * Because echo client and normal lustre client will share
1694  * same cl_env cache. So echo client needs to refresh
1695  * the env context after it get one from the cache, especially
1696  * when normal client and echo client co-exist in the same client.
1697  */
1698 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1699                           __u32 stags)
1700 {
1701         int    result;
1702
1703         if ((env->le_ctx.lc_tags & ctags) != ctags) {
1704                 env->le_ctx.lc_version = 0;
1705                 env->le_ctx.lc_tags |= ctags;
1706         }
1707
1708         if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1709                 env->le_ses->lc_version = 0;
1710                 env->le_ses->lc_tags |= stags;
1711         }
1712
1713         result = lu_env_refill(env);
1714
1715         return result;
1716 }
1717 EXPORT_SYMBOL(lu_env_refill_by_tags);
1718
1719 static struct cfs_shrinker *lu_site_shrinker = NULL;
1720
1721 typedef struct lu_site_stats{
1722         unsigned        lss_populated;
1723         unsigned        lss_max_search;
1724         unsigned        lss_total;
1725         unsigned        lss_busy;
1726 } lu_site_stats_t;
1727
1728 static void lu_site_stats_get(cfs_hash_t *hs,
1729                               lu_site_stats_t *stats, int populated)
1730 {
1731         cfs_hash_bd_t bd;
1732         int           i;
1733
1734         cfs_hash_for_each_bucket(hs, &bd, i) {
1735                 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1736                 cfs_hlist_head_t        *hhead;
1737
1738                 cfs_hash_bd_lock(hs, &bd, 1);
1739                 stats->lss_busy  += bkt->lsb_busy;
1740                 stats->lss_total += cfs_hash_bd_count_get(&bd);
1741                 stats->lss_max_search = max((int)stats->lss_max_search,
1742                                             cfs_hash_bd_depmax_get(&bd));
1743                 if (!populated) {
1744                         cfs_hash_bd_unlock(hs, &bd, 1);
1745                         continue;
1746                 }
1747
1748                 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1749                         if (!cfs_hlist_empty(hhead))
1750                                 stats->lss_populated++;
1751                 }
1752                 cfs_hash_bd_unlock(hs, &bd, 1);
1753         }
1754 }
1755
1756 #ifdef __KERNEL__
1757
1758 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1759 {
1760         lu_site_stats_t stats;
1761         struct lu_site *s;
1762         struct lu_site *tmp;
1763         int cached = 0;
1764         int remain = shrink_param(sc, nr_to_scan);
1765         CFS_LIST_HEAD(splice);
1766
1767         if (remain != 0) {
1768                 if (!(shrink_param(sc, gfp_mask) & __GFP_FS))
1769                         return -1;
1770                 CDEBUG(D_INODE, "Shrink %d objects\n", remain);
1771         }
1772
1773         cfs_down(&lu_sites_guard);
1774         cfs_list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1775                 if (shrink_param(sc, nr_to_scan) != 0) {
1776                         remain = lu_site_purge(&lu_shrink_env, s, remain);
1777                         /*
1778                          * Move just shrunk site to the tail of site list to
1779                          * assure shrinking fairness.
1780                          */
1781                         cfs_list_move_tail(&s->ls_linkage, &splice);
1782                 }
1783
1784                 memset(&stats, 0, sizeof(stats));
1785                 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1786                 cached += stats.lss_total - stats.lss_busy;
1787                 if (shrink_param(sc, nr_to_scan) && remain <= 0)
1788                         break;
1789         }
1790         cfs_list_splice(&splice, lu_sites.prev);
1791         cfs_up(&lu_sites_guard);
1792
1793         cached = (cached / 100) * sysctl_vfs_cache_pressure;
1794         if (shrink_param(sc, nr_to_scan) == 0)
1795                 CDEBUG(D_INODE, "%d objects cached\n", cached);
1796         return cached;
1797 }
1798
1799 /*
1800  * Debugging stuff.
1801  */
1802
1803 /**
1804  * Environment to be used in debugger, contains all tags.
1805  */
1806 struct lu_env lu_debugging_env;
1807
1808 /**
1809  * Debugging printer function using printk().
1810  */
1811 int lu_printk_printer(const struct lu_env *env,
1812                       void *unused, const char *format, ...)
1813 {
1814         va_list args;
1815
1816         va_start(args, format);
1817         vprintk(format, args);
1818         va_end(args);
1819         return 0;
1820 }
1821
1822 void lu_debugging_setup(void)
1823 {
1824         lu_env_init(&lu_debugging_env, ~0);
1825 }
1826
1827 void lu_context_keys_dump(void)
1828 {
1829         int i;
1830
1831         for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1832                 struct lu_context_key *key;
1833
1834                 key = lu_keys[i];
1835                 if (key != NULL) {
1836                         CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
1837                                i, key, key->lct_tags,
1838                                key->lct_init, key->lct_fini, key->lct_exit,
1839                                key->lct_index, cfs_atomic_read(&key->lct_used),
1840                                key->lct_owner ? key->lct_owner->name : "",
1841                                key->lct_owner);
1842                         lu_ref_print(&key->lct_reference);
1843                 }
1844         }
1845 }
1846 EXPORT_SYMBOL(lu_context_keys_dump);
1847 #else  /* !__KERNEL__ */
1848 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1849 {
1850         return 0;
1851 }
1852 #endif /* __KERNEL__ */
1853
1854 int  cl_global_init(void);
1855 void cl_global_fini(void);
1856 int  lu_ref_global_init(void);
1857 void lu_ref_global_fini(void);
1858
1859 int dt_global_init(void);
1860 void dt_global_fini(void);
1861
1862 int llo_global_init(void);
1863 void llo_global_fini(void);
1864
1865 /**
1866  * Initialization of global lu_* data.
1867  */
1868 int lu_global_init(void)
1869 {
1870         int result;
1871
1872         CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
1873
1874         result = lu_ref_global_init();
1875         if (result != 0)
1876                 return result;
1877
1878         LU_CONTEXT_KEY_INIT(&lu_global_key);
1879         result = lu_context_key_register(&lu_global_key);
1880         if (result != 0)
1881                 return result;
1882         /*
1883          * At this level, we don't know what tags are needed, so allocate them
1884          * conservatively. This should not be too bad, because this
1885          * environment is global.
1886          */
1887         cfs_down(&lu_sites_guard);
1888         result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
1889         cfs_up(&lu_sites_guard);
1890         if (result != 0)
1891                 return result;
1892
1893         /*
1894          * seeks estimation: 3 seeks to read a record from oi, one to read
1895          * inode, one for ea. Unfortunately setting this high value results in
1896          * lu_object/inode cache consuming all the memory.
1897          */
1898         lu_site_shrinker = cfs_set_shrinker(CFS_DEFAULT_SEEKS, lu_cache_shrink);
1899         if (lu_site_shrinker == NULL)
1900                 return -ENOMEM;
1901
1902         result = lu_time_global_init();
1903         if (result)
1904                 GOTO(out, result);
1905
1906 #ifdef __KERNEL__
1907         result = dt_global_init();
1908         if (result)
1909                 GOTO(out, result);
1910
1911         result = llo_global_init();
1912         if (result)
1913                 GOTO(out, result);
1914 #endif
1915         result = cl_global_init();
1916 out:
1917
1918         return result;
1919 }
1920
1921 /**
1922  * Dual to lu_global_init().
1923  */
1924 void lu_global_fini(void)
1925 {
1926         cl_global_fini();
1927 #ifdef __KERNEL__
1928         llo_global_fini();
1929         dt_global_fini();
1930 #endif
1931         lu_time_global_fini();
1932         if (lu_site_shrinker != NULL) {
1933                 cfs_remove_shrinker(lu_site_shrinker);
1934                 lu_site_shrinker = NULL;
1935         }
1936
1937         lu_context_key_degister(&lu_global_key);
1938
1939         /*
1940          * Tear shrinker environment down _after_ de-registering
1941          * lu_global_key, because the latter has a value in the former.
1942          */
1943         cfs_down(&lu_sites_guard);
1944         lu_env_fini(&lu_shrink_env);
1945         cfs_up(&lu_sites_guard);
1946
1947         lu_ref_global_fini();
1948 }
1949
1950 struct lu_buf LU_BUF_NULL = {
1951         .lb_buf = NULL,
1952         .lb_len = 0
1953 };
1954 EXPORT_SYMBOL(LU_BUF_NULL);
1955
1956 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
1957 {
1958 #ifdef LPROCFS
1959         struct lprocfs_counter ret;
1960
1961         lprocfs_stats_collect(stats, idx, &ret);
1962         return (__u32)ret.lc_count;
1963 #else
1964         return 0;
1965 #endif
1966 }
1967
1968 /**
1969  * Output site statistical counters into a buffer. Suitable for
1970  * lprocfs_rd_*()-style functions.
1971  */
1972 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
1973 {
1974         lu_site_stats_t stats;
1975
1976         memset(&stats, 0, sizeof(stats));
1977         lu_site_stats_get(s->ls_obj_hash, &stats, 1);
1978
1979         return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
1980                         stats.lss_busy,
1981                         stats.lss_total,
1982                         stats.lss_populated,
1983                         CFS_HASH_NHLIST(s->ls_obj_hash),
1984                         stats.lss_max_search,
1985                         ls_stats_read(s->ls_stats, LU_SS_CREATED),
1986                         ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
1987                         ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
1988                         ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
1989                         ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
1990                         ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
1991 }
1992 EXPORT_SYMBOL(lu_site_stats_print);
1993
1994 const char *lu_time_names[LU_TIME_NR] = {
1995         [LU_TIME_FIND_LOOKUP] = "find_lookup",
1996         [LU_TIME_FIND_ALLOC]  = "find_alloc",
1997         [LU_TIME_FIND_INSERT] = "find_insert"
1998 };
1999 EXPORT_SYMBOL(lu_time_names);
2000
2001 /**
2002  * Helper function to initialize a number of kmem slab caches at once.
2003  */
2004 int lu_kmem_init(struct lu_kmem_descr *caches)
2005 {
2006         int result;
2007         struct lu_kmem_descr *iter = caches;
2008
2009         for (result = 0; iter->ckd_cache != NULL; ++iter) {
2010                 *iter->ckd_cache = cfs_mem_cache_create(iter->ckd_name,
2011                                                         iter->ckd_size,
2012                                                         0, 0);
2013                 if (*iter->ckd_cache == NULL) {
2014                         result = -ENOMEM;
2015                         /* free all previously allocated caches */
2016                         lu_kmem_fini(caches);
2017                         break;
2018                 }
2019         }
2020         return result;
2021 }
2022 EXPORT_SYMBOL(lu_kmem_init);
2023
2024 /**
2025  * Helper function to finalize a number of kmem slab cached at once. Dual to
2026  * lu_kmem_init().
2027  */
2028 void lu_kmem_fini(struct lu_kmem_descr *caches)
2029 {
2030         int rc;
2031
2032         for (; caches->ckd_cache != NULL; ++caches) {
2033                 if (*caches->ckd_cache != NULL) {
2034                         rc = cfs_mem_cache_destroy(*caches->ckd_cache);
2035                         LASSERTF(rc == 0, "couldn't destroy %s slab\n",
2036                                  caches->ckd_name);
2037                         *caches->ckd_cache = NULL;
2038                 }
2039         }
2040 }
2041 EXPORT_SYMBOL(lu_kmem_fini);