4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/lu_object.c
35 * These are the only exported functions, they provide some generic
36 * infrastructure for managing object devices
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <linux/module.h>
44 #include <linux/list.h>
45 #ifdef HAVE_PROCESSOR_H
46 #include <linux/processor.h>
48 #include <libcfs/linux/processor.h>
50 #include <linux/random.h>
52 #include <libcfs/libcfs.h>
53 #include <libcfs/libcfs_hash.h> /* hash_long() */
54 #include <libcfs/linux/linux-mem.h>
55 #include <obd_class.h>
56 #include <obd_support.h>
57 #include <lustre_disk.h>
58 #include <lustre_fid.h>
59 #include <lu_object.h>
62 struct lu_site_bkt_data {
64 * LRU list, updated on each access to object. Protected by
67 * "Cold" end of LRU is lu_site::ls_lru.next. Accessed object are
68 * moved to the lu_site::ls_lru.prev
70 struct list_head lsb_lru;
72 * Wait-queue signaled when an object in this site is ultimately
73 * destroyed (lu_object_free()) or initialized (lu_object_start()).
74 * It is used by lu_object_find() to wait before re-trying when
75 * object in the process of destruction is found in the hash table;
76 * or wait object to be initialized by the allocator.
78 * \see htable_lookup().
80 wait_queue_head_t lsb_waitq;
84 LU_CACHE_PERCENT_MAX = 50,
85 LU_CACHE_PERCENT_DEFAULT = 20
88 #define LU_CACHE_NR_MAX_ADJUST 512
89 #define LU_CACHE_NR_UNLIMITED -1
90 #define LU_CACHE_NR_DEFAULT LU_CACHE_NR_UNLIMITED
91 #define LU_CACHE_NR_LDISKFS_LIMIT LU_CACHE_NR_UNLIMITED
92 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
93 #define LU_CACHE_NR_ZFS_LIMIT 10240
95 #define LU_SITE_BITS_MIN 12
96 #define LU_SITE_BITS_MAX 24
97 #define LU_SITE_BITS_MAX_CL 19
99 * Max 256 buckets, we don't want too many buckets because:
100 * - consume too much memory (currently max 16K)
101 * - avoid unbalanced LRU list
102 * With few cpus there is little gain from extra buckets, so
103 * we treat this as a maximum in lu_site_init().
105 #define LU_SITE_BKT_BITS 8
108 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
109 module_param(lu_cache_percent, int, 0644);
110 MODULE_PARM_DESC(lu_cache_percent, "Percentage of memory to be used as lu_object cache");
112 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
113 module_param(lu_cache_nr, long, 0644);
114 MODULE_PARM_DESC(lu_cache_nr, "Maximum number of objects in lu_object cache");
116 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
117 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx);
119 static u32 lu_fid_hash(const void *data, u32 seed)
121 const struct lu_fid *fid = data;
123 seed = cfs_hash_32(seed ^ fid->f_oid, 32);
124 seed ^= cfs_hash_64(fid->f_seq, 32);
128 static inline int lu_bkt_hash(struct lu_site *s, const struct lu_fid *fid)
130 return lu_fid_hash(fid, s->ls_bkt_seed) &
135 lu_site_wq_from_fid(struct lu_site *site, struct lu_fid *fid)
137 struct lu_site_bkt_data *bkt;
139 bkt = &site->ls_bkts[lu_bkt_hash(site, fid)];
140 return &bkt->lsb_waitq;
142 EXPORT_SYMBOL(lu_site_wq_from_fid);
145 * Decrease reference counter on object. If last reference is freed, return
146 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
147 * case, free object immediately.
149 void lu_object_put(const struct lu_env *env, struct lu_object *o)
151 struct lu_site_bkt_data *bkt;
152 struct lu_object_header *top = o->lo_header;
153 struct lu_site *site = o->lo_dev->ld_site;
154 struct lu_object *orig = o;
155 struct cfs_hash_bd bd;
156 const struct lu_fid *fid = lu_object_fid(o);
160 * till we have full fids-on-OST implemented anonymous objects
161 * are possible in OSP. such an object isn't listed in the site
162 * so we should not remove it from the site.
164 if (fid_is_zero(fid)) {
165 LASSERT(top->loh_hash.next == NULL
166 && top->loh_hash.pprev == NULL);
167 LASSERT(list_empty(&top->loh_lru));
168 if (!atomic_dec_and_test(&top->loh_ref))
170 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
171 if (o->lo_ops->loo_object_release != NULL)
172 o->lo_ops->loo_object_release(env, o);
174 lu_object_free(env, orig);
178 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
180 is_dying = lu_object_is_dying(top);
181 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
182 /* at this point the object reference is dropped and lock is
183 * not taken, so lu_object should not be touched because it
184 * can be freed by concurrent thread. Use local variable for
189 * somebody may be waiting for this, currently only
190 * used for cl_object, see cl_object_put_last().
192 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
193 wake_up_all(&bkt->lsb_waitq);
199 * When last reference is released, iterate over object
200 * layers, and notify them that object is no longer busy.
202 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
203 if (o->lo_ops->loo_object_release != NULL)
204 o->lo_ops->loo_object_release(env, o);
207 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
208 spin_lock(&bkt->lsb_waitq.lock);
210 /* don't use local 'is_dying' here because if was taken without lock
211 * but here we need the latest actual value of it so check lu_object
214 if (!lu_object_is_dying(top) &&
215 (lu_object_exists(orig) || lu_object_is_cl(orig))) {
216 LASSERT(list_empty(&top->loh_lru));
217 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
218 spin_unlock(&bkt->lsb_waitq.lock);
219 percpu_counter_inc(&site->ls_lru_len_counter);
220 CDEBUG(D_INODE, "Add %p/%p to site lru. hash: %p, bkt: %p\n",
221 orig, top, site->ls_obj_hash, bkt);
222 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
227 * If object is dying (will not be cached) then remove it
228 * from hash table (it is already not on the LRU).
230 * This is done with hash table lists locked. As the only
231 * way to acquire first reference to previously unreferenced
232 * object is through hash-table lookup (lu_object_find())
233 * which is done under hash-table, no race with concurrent
234 * object lookup is possible and we can safely destroy object below.
236 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
237 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
238 spin_unlock(&bkt->lsb_waitq.lock);
239 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
240 /* Object was already removed from hash above, can kill it. */
241 lu_object_free(env, orig);
243 EXPORT_SYMBOL(lu_object_put);
246 * Put object and don't keep in cache. This is temporary solution for
247 * multi-site objects when its layering is not constant.
249 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
251 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
252 return lu_object_put(env, o);
254 EXPORT_SYMBOL(lu_object_put_nocache);
257 * Kill the object and take it out of LRU cache.
258 * Currently used by client code for layout change.
260 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
262 struct lu_object_header *top;
265 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
266 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
267 struct lu_site *site = o->lo_dev->ld_site;
268 struct cfs_hash *obj_hash = site->ls_obj_hash;
269 struct cfs_hash_bd bd;
271 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
272 if (!list_empty(&top->loh_lru)) {
273 struct lu_site_bkt_data *bkt;
275 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
276 spin_lock(&bkt->lsb_waitq.lock);
277 list_del_init(&top->loh_lru);
278 spin_unlock(&bkt->lsb_waitq.lock);
279 percpu_counter_dec(&site->ls_lru_len_counter);
281 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
282 cfs_hash_bd_unlock(obj_hash, &bd, 1);
285 EXPORT_SYMBOL(lu_object_unhash);
288 * Allocate new object.
290 * This follows object creation protocol, described in the comment within
291 * struct lu_device_operations definition.
293 static struct lu_object *lu_object_alloc(const struct lu_env *env,
294 struct lu_device *dev,
295 const struct lu_fid *f)
297 struct lu_object *top;
300 * Create top-level object slice. This will also create
303 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
305 return ERR_PTR(-ENOMEM);
309 * This is the only place where object fid is assigned. It's constant
312 top->lo_header->loh_fid = *f;
320 * This is called after object hash insertion to avoid returning an object with
323 static int lu_object_start(const struct lu_env *env, struct lu_device *dev,
324 struct lu_object *top,
325 const struct lu_object_conf *conf)
327 struct lu_object *scan;
328 struct list_head *layers;
329 unsigned int init_mask = 0;
330 unsigned int init_flag;
334 layers = &top->lo_header->loh_layers;
338 * Call ->loo_object_init() repeatedly, until no more new
339 * object slices are created.
343 list_for_each_entry(scan, layers, lo_linkage) {
344 if (init_mask & init_flag)
347 scan->lo_header = top->lo_header;
348 result = scan->lo_ops->loo_object_init(env, scan, conf);
352 init_mask |= init_flag;
358 list_for_each_entry_reverse(scan, layers, lo_linkage) {
359 if (scan->lo_ops->loo_object_start != NULL) {
360 result = scan->lo_ops->loo_object_start(env, scan);
366 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
368 set_bit(LU_OBJECT_INITED, &top->lo_header->loh_flags);
376 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
378 wait_queue_head_t *wq;
379 struct lu_site *site;
380 struct lu_object *scan;
381 struct list_head *layers;
384 site = o->lo_dev->ld_site;
385 layers = &o->lo_header->loh_layers;
386 wq = lu_site_wq_from_fid(site, &o->lo_header->loh_fid);
388 * First call ->loo_object_delete() method to release all resources.
390 list_for_each_entry_reverse(scan, layers, lo_linkage) {
391 if (scan->lo_ops->loo_object_delete != NULL)
392 scan->lo_ops->loo_object_delete(env, scan);
396 * Then, splice object layers into stand-alone list, and call
397 * ->loo_object_free() on all layers to free memory. Splice is
398 * necessary, because lu_object_header is freed together with the
401 list_splice_init(layers, &splice);
402 while (!list_empty(&splice)) {
404 * Free layers in bottom-to-top order, so that object header
405 * lives as long as possible and ->loo_object_free() methods
406 * can look at its contents.
408 o = container_of0(splice.prev, struct lu_object, lo_linkage);
409 list_del_init(&o->lo_linkage);
410 LASSERT(o->lo_ops->loo_object_free != NULL);
411 o->lo_ops->loo_object_free(env, o);
414 if (waitqueue_active(wq))
419 * Free \a nr objects from the cold end of the site LRU list.
420 * if canblock is 0, then don't block awaiting for another
421 * instance of lu_site_purge() to complete
423 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s,
424 int nr, int canblock)
426 struct lu_object_header *h;
427 struct lu_object_header *temp;
428 struct lu_site_bkt_data *bkt;
431 unsigned int start = 0;
436 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
440 * Under LRU list lock, scan LRU list and move unreferenced objects to
441 * the dispose list, removing them from LRU and hash table.
444 start = s->ls_purge_start;
445 bnr = (nr == ~0) ? -1 : nr / s->ls_bkt_cnt + 1;
448 * It doesn't make any sense to make purge threads parallel, that can
449 * only bring troubles to us. See LU-5331.
452 mutex_lock(&s->ls_purge_mutex);
453 else if (mutex_trylock(&s->ls_purge_mutex) == 0)
457 for (i = start; i < s->ls_bkt_cnt ; i++) {
459 bkt = &s->ls_bkts[i];
460 spin_lock(&bkt->lsb_waitq.lock);
462 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
463 LASSERT(atomic_read(&h->loh_ref) == 0);
465 LINVRNT(lu_bkt_hash(s, &h->loh_fid) == i);
467 /* Cannot remove from hash under current spinlock,
468 * so set flag to stop object from being found
469 * by htable_lookup().
471 set_bit(LU_OBJECT_PURGING, &h->loh_flags);
472 list_move(&h->loh_lru, &dispose);
473 percpu_counter_dec(&s->ls_lru_len_counter);
477 if (nr != ~0 && --nr == 0)
480 if (count > 0 && --count == 0)
484 spin_unlock(&bkt->lsb_waitq.lock);
487 * Free everything on the dispose list. This is safe against
488 * races due to the reasons described in lu_object_put().
490 while ((h = list_first_entry_or_null(&dispose,
491 struct lu_object_header,
493 cfs_hash_del(s->ls_obj_hash, &h->loh_fid, &h->loh_hash);
494 list_del_init(&h->loh_lru);
495 lu_object_free(env, lu_object_top(h));
496 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
502 mutex_unlock(&s->ls_purge_mutex);
504 if (nr != 0 && did_sth && start != 0) {
505 start = 0; /* restart from the first bucket */
508 /* race on s->ls_purge_start, but nobody cares */
509 s->ls_purge_start = i & (s->ls_bkt_cnt - 1);
513 EXPORT_SYMBOL(lu_site_purge_objects);
518 * Code below has to jump through certain loops to output object description
519 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
520 * composes object description from strings that are parts of _lines_ of
521 * output (i.e., strings that are not terminated by newline). This doesn't fit
522 * very well into libcfs_debug_msg() interface that assumes that each message
523 * supplied to it is a self-contained output line.
525 * To work around this, strings are collected in a temporary buffer
526 * (implemented as a value of lu_cdebug_key key), until terminating newline
527 * character is detected.
535 * XXX overflow is not handled correctly.
540 struct lu_cdebug_data {
544 char lck_area[LU_CDEBUG_LINE];
547 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
548 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
551 * Key, holding temporary buffer. This key is registered very early by
554 static struct lu_context_key lu_global_key = {
555 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
556 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
557 .lct_init = lu_global_key_init,
558 .lct_fini = lu_global_key_fini
562 * Printer function emitting messages through libcfs_debug_msg().
564 int lu_cdebug_printer(const struct lu_env *env,
565 void *cookie, const char *format, ...)
567 struct libcfs_debug_msg_data *msgdata = cookie;
568 struct lu_cdebug_data *key;
573 va_start(args, format);
575 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
576 LASSERT(key != NULL);
578 used = strlen(key->lck_area);
579 complete = format[strlen(format) - 1] == '\n';
581 * Append new chunk to the buffer.
583 vsnprintf(key->lck_area + used,
584 ARRAY_SIZE(key->lck_area) - used, format, args);
586 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
587 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
588 key->lck_area[0] = 0;
593 EXPORT_SYMBOL(lu_cdebug_printer);
596 * Print object header.
598 void lu_object_header_print(const struct lu_env *env, void *cookie,
599 lu_printer_t printer,
600 const struct lu_object_header *hdr)
602 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
603 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
605 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
606 list_empty((struct list_head *)&hdr->loh_lru) ? \
608 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
610 EXPORT_SYMBOL(lu_object_header_print);
613 * Print human readable representation of the \a o to the \a printer.
615 void lu_object_print(const struct lu_env *env, void *cookie,
616 lu_printer_t printer, const struct lu_object *o)
618 static const char ruler[] = "........................................";
619 struct lu_object_header *top;
623 lu_object_header_print(env, cookie, printer, top);
624 (*printer)(env, cookie, "{\n");
626 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
628 * print `.' \a depth times followed by type name and address
630 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
631 o->lo_dev->ld_type->ldt_name, o);
633 if (o->lo_ops->loo_object_print != NULL)
634 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
636 (*printer)(env, cookie, "\n");
639 (*printer)(env, cookie, "} header@%p\n", top);
641 EXPORT_SYMBOL(lu_object_print);
644 * Check object consistency.
646 int lu_object_invariant(const struct lu_object *o)
648 struct lu_object_header *top;
651 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
652 if (o->lo_ops->loo_object_invariant != NULL &&
653 !o->lo_ops->loo_object_invariant(o))
659 static struct lu_object *htable_lookup(struct lu_site *s,
660 struct cfs_hash_bd *bd,
661 const struct lu_fid *f,
664 struct lu_object_header *h;
665 struct hlist_node *hnode;
666 __u64 ver = cfs_hash_bd_version_get(bd);
669 return ERR_PTR(-ENOENT);
672 /* cfs_hash_bd_peek_locked is a somehow "internal" function
673 * of cfs_hash, it doesn't add refcount on object. */
674 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
676 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
677 return ERR_PTR(-ENOENT);
680 h = container_of0(hnode, struct lu_object_header, loh_hash);
681 if (!list_empty(&h->loh_lru)) {
682 struct lu_site_bkt_data *bkt;
684 bkt = &s->ls_bkts[lu_bkt_hash(s, &h->loh_fid)];
685 spin_lock(&bkt->lsb_waitq.lock);
686 /* Might have just been moved to the dispose list, in which
687 * case LU_OBJECT_PURGING will be set. In that case,
688 * delete it from the hash table immediately.
689 * When lu_site_purge_objects() tried, it will find it
690 * isn't there, which is harmless.
692 if (test_bit(LU_OBJECT_PURGING, &h->loh_flags)) {
693 spin_unlock(&bkt->lsb_waitq.lock);
694 cfs_hash_bd_del_locked(s->ls_obj_hash, bd, hnode);
695 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
696 return ERR_PTR(-ENOENT);
698 list_del_init(&h->loh_lru);
699 spin_unlock(&bkt->lsb_waitq.lock);
700 percpu_counter_dec(&s->ls_lru_len_counter);
702 cfs_hash_get(s->ls_obj_hash, hnode);
703 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
704 return lu_object_top(h);
708 * Search cache for an object with the fid \a f. If such object is found,
709 * return it. Otherwise, create new object, insert it into cache and return
710 * it. In any case, additional reference is acquired on the returned object.
712 struct lu_object *lu_object_find(const struct lu_env *env,
713 struct lu_device *dev, const struct lu_fid *f,
714 const struct lu_object_conf *conf)
716 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
718 EXPORT_SYMBOL(lu_object_find);
721 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
722 * the calculation for the number of objects to reclaim is not covered by
723 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
724 * This ensures that many concurrent threads will not accidentally purge
727 static void lu_object_limit(const struct lu_env *env,
728 struct lu_device *dev)
732 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
735 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
736 nr = (__u64)lu_cache_nr;
740 lu_site_purge_objects(env, dev->ld_site,
741 min_t(__u64, size - nr, LU_CACHE_NR_MAX_ADJUST),
746 * Core logic of lu_object_find*() functions.
748 * Much like lu_object_find(), but top level device of object is specifically
749 * \a dev rather than top level device of the site. This interface allows
750 * objects of different "stacking" to be created within the same site.
752 struct lu_object *lu_object_find_at(const struct lu_env *env,
753 struct lu_device *dev,
754 const struct lu_fid *f,
755 const struct lu_object_conf *conf)
758 struct lu_object *shadow;
761 struct cfs_hash_bd bd;
762 struct lu_site_bkt_data *bkt;
769 * This uses standard index maintenance protocol:
771 * - search index under lock, and return object if found;
772 * - otherwise, unlock index, allocate new object;
773 * - lock index and search again;
774 * - if nothing is found (usual case), insert newly created
776 * - otherwise (race: other thread inserted object), free
777 * object just allocated.
781 * For "LOC_F_NEW" case, we are sure the object is new established.
782 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
783 * just alloc and insert directly.
789 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_OBD_ZERO_NLINK_RACE)))
790 lu_site_purge(env, s, -1);
792 bkt = &s->ls_bkts[lu_bkt_hash(s, f)];
793 cfs_hash_bd_get(hs, f, &bd);
794 if (!(conf && conf->loc_flags & LOC_F_NEW)) {
795 cfs_hash_bd_lock(hs, &bd, 1);
796 o = htable_lookup(s, &bd, f, &version);
797 cfs_hash_bd_unlock(hs, &bd, 1);
800 if (likely(lu_object_is_inited(o->lo_header)))
803 wait_event_idle(bkt->lsb_waitq,
804 lu_object_is_inited(o->lo_header) ||
805 lu_object_is_dying(o->lo_header));
807 if (lu_object_is_dying(o->lo_header)) {
808 lu_object_put(env, o);
810 RETURN(ERR_PTR(-ENOENT));
816 if (PTR_ERR(o) != -ENOENT)
821 * Allocate new object, NB, object is unitialized in case object
822 * is changed between allocation and hash insertion, thus the object
823 * with stale attributes is returned.
825 o = lu_object_alloc(env, dev, f);
829 LASSERT(lu_fid_eq(lu_object_fid(o), f));
831 CFS_RACE_WAIT(OBD_FAIL_OBD_ZERO_NLINK_RACE);
833 cfs_hash_bd_lock(hs, &bd, 1);
835 if (conf && conf->loc_flags & LOC_F_NEW)
836 shadow = ERR_PTR(-ENOENT);
838 shadow = htable_lookup(s, &bd, f, &version);
839 if (likely(PTR_ERR(shadow) == -ENOENT)) {
840 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
841 cfs_hash_bd_unlock(hs, &bd, 1);
844 * This may result in rather complicated operations, including
845 * fld queries, inode loading, etc.
847 rc = lu_object_start(env, dev, o, conf);
849 lu_object_put_nocache(env, o);
853 wake_up_all(&bkt->lsb_waitq);
855 lu_object_limit(env, dev);
860 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
861 cfs_hash_bd_unlock(hs, &bd, 1);
862 lu_object_free(env, o);
864 if (!(conf && conf->loc_flags & LOC_F_NEW) &&
865 !lu_object_is_inited(shadow->lo_header)) {
866 wait_event_idle(bkt->lsb_waitq,
867 lu_object_is_inited(shadow->lo_header) ||
868 lu_object_is_dying(shadow->lo_header));
870 if (lu_object_is_dying(shadow->lo_header)) {
871 lu_object_put(env, shadow);
873 RETURN(ERR_PTR(-ENOENT));
879 EXPORT_SYMBOL(lu_object_find_at);
882 * Find object with given fid, and return its slice belonging to given device.
884 struct lu_object *lu_object_find_slice(const struct lu_env *env,
885 struct lu_device *dev,
886 const struct lu_fid *f,
887 const struct lu_object_conf *conf)
889 struct lu_object *top;
890 struct lu_object *obj;
892 top = lu_object_find(env, dev, f, conf);
896 obj = lu_object_locate(top->lo_header, dev->ld_type);
897 if (unlikely(obj == NULL)) {
898 lu_object_put(env, top);
899 obj = ERR_PTR(-ENOENT);
904 EXPORT_SYMBOL(lu_object_find_slice);
906 int lu_device_type_init(struct lu_device_type *ldt)
910 atomic_set(&ldt->ldt_device_nr, 0);
911 if (ldt->ldt_ops->ldto_init)
912 result = ldt->ldt_ops->ldto_init(ldt);
916 EXPORT_SYMBOL(lu_device_type_init);
918 void lu_device_type_fini(struct lu_device_type *ldt)
920 if (ldt->ldt_ops->ldto_fini)
921 ldt->ldt_ops->ldto_fini(ldt);
923 EXPORT_SYMBOL(lu_device_type_fini);
926 * Global list of all sites on this node
928 static LIST_HEAD(lu_sites);
929 static DECLARE_RWSEM(lu_sites_guard);
932 * Global environment used by site shrinker.
934 static struct lu_env lu_shrink_env;
936 struct lu_site_print_arg {
937 struct lu_env *lsp_env;
939 lu_printer_t lsp_printer;
943 lu_site_obj_print(struct cfs_hash *hs, struct cfs_hash_bd *bd,
944 struct hlist_node *hnode, void *data)
946 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
947 struct lu_object_header *h;
949 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
950 if (!list_empty(&h->loh_layers)) {
951 const struct lu_object *o;
953 o = lu_object_top(h);
954 lu_object_print(arg->lsp_env, arg->lsp_cookie,
955 arg->lsp_printer, o);
957 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
958 arg->lsp_printer, h);
964 * Print all objects in \a s.
966 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
967 lu_printer_t printer)
969 struct lu_site_print_arg arg = {
970 .lsp_env = (struct lu_env *)env,
971 .lsp_cookie = cookie,
972 .lsp_printer = printer,
975 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
977 EXPORT_SYMBOL(lu_site_print);
980 * Return desired hash table order.
982 static unsigned long lu_htable_order(struct lu_device *top)
984 unsigned long cache_size;
986 unsigned long bits_max = LU_SITE_BITS_MAX;
989 * For ZFS based OSDs the cache should be disabled by default. This
990 * allows the ZFS ARC maximum flexibility in determining what buffers
991 * to cache. If Lustre has objects or buffer which it wants to ensure
992 * always stay cached it must maintain a hold on them.
994 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
995 lu_cache_percent = 1;
996 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
997 return LU_SITE_BITS_MIN;
1000 if (strcmp(top->ld_type->ldt_name, LUSTRE_VVP_NAME) == 0)
1001 bits_max = LU_SITE_BITS_MAX_CL;
1004 * Calculate hash table size, assuming that we want reasonable
1005 * performance when 20% of total memory is occupied by cache of
1008 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
1010 cache_size = cfs_totalram_pages();
1012 #if BITS_PER_LONG == 32
1013 /* limit hashtable size for lowmem systems to low RAM */
1014 if (cache_size > 1 << (30 - PAGE_SHIFT))
1015 cache_size = 1 << (30 - PAGE_SHIFT) * 3 / 4;
1018 /* clear off unreasonable cache setting. */
1019 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
1020 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
1021 " the range of (0, %u]. Will use default value: %u.\n",
1022 lu_cache_percent, LU_CACHE_PERCENT_MAX,
1023 LU_CACHE_PERCENT_DEFAULT);
1025 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
1027 cache_size = cache_size / 100 * lu_cache_percent *
1030 for (bits = 1; (1 << bits) < cache_size; ++bits) {
1034 return clamp_t(typeof(bits), bits, LU_SITE_BITS_MIN, bits_max);
1037 static unsigned lu_obj_hop_hash(struct cfs_hash *hs,
1038 const void *key, unsigned mask)
1040 struct lu_fid *fid = (struct lu_fid *)key;
1043 hash = fid_flatten32(fid);
1044 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
1045 hash = hash_long(hash, hs->hs_bkt_bits);
1047 /* give me another random factor */
1048 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
1050 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
1051 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
1056 static void *lu_obj_hop_object(struct hlist_node *hnode)
1058 return hlist_entry(hnode, struct lu_object_header, loh_hash);
1061 static void *lu_obj_hop_key(struct hlist_node *hnode)
1063 struct lu_object_header *h;
1065 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1069 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
1071 struct lu_object_header *h;
1073 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1074 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1077 static void lu_obj_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1079 struct lu_object_header *h;
1081 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1082 atomic_inc(&h->loh_ref);
1085 static void lu_obj_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
1087 LBUG(); /* we should never called it */
1090 static struct cfs_hash_ops lu_site_hash_ops = {
1091 .hs_hash = lu_obj_hop_hash,
1092 .hs_key = lu_obj_hop_key,
1093 .hs_keycmp = lu_obj_hop_keycmp,
1094 .hs_object = lu_obj_hop_object,
1095 .hs_get = lu_obj_hop_get,
1096 .hs_put_locked = lu_obj_hop_put_locked,
1099 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1101 spin_lock(&s->ls_ld_lock);
1102 if (list_empty(&d->ld_linkage))
1103 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1104 spin_unlock(&s->ls_ld_lock);
1106 EXPORT_SYMBOL(lu_dev_add_linkage);
1108 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1110 spin_lock(&s->ls_ld_lock);
1111 list_del_init(&d->ld_linkage);
1112 spin_unlock(&s->ls_ld_lock);
1114 EXPORT_SYMBOL(lu_dev_del_linkage);
1117 * Initialize site \a s, with \a d as the top level device.
1119 int lu_site_init(struct lu_site *s, struct lu_device *top)
1121 struct lu_site_bkt_data *bkt;
1128 memset(s, 0, sizeof *s);
1129 mutex_init(&s->ls_purge_mutex);
1131 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
1132 rc = percpu_counter_init(&s->ls_lru_len_counter, 0, GFP_NOFS);
1134 rc = percpu_counter_init(&s->ls_lru_len_counter, 0);
1139 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1140 for (bits = lu_htable_order(top);
1141 bits >= LU_SITE_BITS_MIN; bits--) {
1142 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1143 bits - LU_SITE_BKT_BITS,
1146 CFS_HASH_SPIN_BKTLOCK |
1147 CFS_HASH_NO_ITEMREF |
1149 CFS_HASH_ASSERT_EMPTY |
1151 if (s->ls_obj_hash != NULL)
1155 if (s->ls_obj_hash == NULL) {
1156 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1160 s->ls_bkt_seed = prandom_u32();
1161 s->ls_bkt_cnt = max_t(long, 1 << LU_SITE_BKT_BITS,
1162 2 * num_possible_cpus());
1163 s->ls_bkt_cnt = roundup_pow_of_two(s->ls_bkt_cnt);
1164 OBD_ALLOC_LARGE(s->ls_bkts, s->ls_bkt_cnt * sizeof(*bkt));
1166 cfs_hash_putref(s->ls_obj_hash);
1167 s->ls_obj_hash = NULL;
1172 for (i = 0; i < s->ls_bkt_cnt; i++) {
1173 bkt = &s->ls_bkts[i];
1174 INIT_LIST_HEAD(&bkt->lsb_lru);
1175 init_waitqueue_head(&bkt->lsb_waitq);
1178 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1179 if (s->ls_stats == NULL) {
1180 OBD_FREE_LARGE(s->ls_bkts, s->ls_bkt_cnt * sizeof(*bkt));
1181 cfs_hash_putref(s->ls_obj_hash);
1182 s->ls_obj_hash = NULL;
1187 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1188 0, "created", "created");
1189 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1190 0, "cache_hit", "cache_hit");
1191 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1192 0, "cache_miss", "cache_miss");
1193 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1194 0, "cache_race", "cache_race");
1195 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1196 0, "cache_death_race", "cache_death_race");
1197 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1198 0, "lru_purged", "lru_purged");
1200 INIT_LIST_HEAD(&s->ls_linkage);
1201 s->ls_top_dev = top;
1204 lu_ref_add(&top->ld_reference, "site-top", s);
1206 INIT_LIST_HEAD(&s->ls_ld_linkage);
1207 spin_lock_init(&s->ls_ld_lock);
1209 lu_dev_add_linkage(s, top);
1213 EXPORT_SYMBOL(lu_site_init);
1216 * Finalize \a s and release its resources.
1218 void lu_site_fini(struct lu_site *s)
1220 down_write(&lu_sites_guard);
1221 list_del_init(&s->ls_linkage);
1222 up_write(&lu_sites_guard);
1224 percpu_counter_destroy(&s->ls_lru_len_counter);
1226 if (s->ls_obj_hash != NULL) {
1227 cfs_hash_putref(s->ls_obj_hash);
1228 s->ls_obj_hash = NULL;
1231 OBD_FREE_LARGE(s->ls_bkts, s->ls_bkt_cnt * sizeof(*s->ls_bkts));
1233 if (s->ls_top_dev != NULL) {
1234 s->ls_top_dev->ld_site = NULL;
1235 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1236 lu_device_put(s->ls_top_dev);
1237 s->ls_top_dev = NULL;
1240 if (s->ls_stats != NULL)
1241 lprocfs_free_stats(&s->ls_stats);
1243 EXPORT_SYMBOL(lu_site_fini);
1246 * Called when initialization of stack for this site is completed.
1248 int lu_site_init_finish(struct lu_site *s)
1251 down_write(&lu_sites_guard);
1252 result = lu_context_refill(&lu_shrink_env.le_ctx);
1254 list_add(&s->ls_linkage, &lu_sites);
1255 up_write(&lu_sites_guard);
1258 EXPORT_SYMBOL(lu_site_init_finish);
1261 * Acquire additional reference on device \a d
1263 void lu_device_get(struct lu_device *d)
1265 atomic_inc(&d->ld_ref);
1267 EXPORT_SYMBOL(lu_device_get);
1270 * Release reference on device \a d.
1272 void lu_device_put(struct lu_device *d)
1274 LASSERT(atomic_read(&d->ld_ref) > 0);
1275 atomic_dec(&d->ld_ref);
1277 EXPORT_SYMBOL(lu_device_put);
1280 * Initialize device \a d of type \a t.
1282 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1284 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1285 t->ldt_ops->ldto_start != NULL)
1286 t->ldt_ops->ldto_start(t);
1288 memset(d, 0, sizeof *d);
1290 lu_ref_init(&d->ld_reference);
1291 INIT_LIST_HEAD(&d->ld_linkage);
1295 EXPORT_SYMBOL(lu_device_init);
1298 * Finalize device \a d.
1300 void lu_device_fini(struct lu_device *d)
1302 struct lu_device_type *t = d->ld_type;
1304 if (d->ld_obd != NULL) {
1305 d->ld_obd->obd_lu_dev = NULL;
1309 lu_ref_fini(&d->ld_reference);
1310 LASSERTF(atomic_read(&d->ld_ref) == 0,
1311 "Refcount is %u\n", atomic_read(&d->ld_ref));
1312 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1314 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1315 t->ldt_ops->ldto_stop != NULL)
1316 t->ldt_ops->ldto_stop(t);
1318 EXPORT_SYMBOL(lu_device_fini);
1321 * Initialize object \a o that is part of compound object \a h and was created
1324 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1325 struct lu_device *d)
1327 memset(o, 0, sizeof(*o));
1331 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1332 INIT_LIST_HEAD(&o->lo_linkage);
1336 EXPORT_SYMBOL(lu_object_init);
1339 * Finalize object and release its resources.
1341 void lu_object_fini(struct lu_object *o)
1343 struct lu_device *dev = o->lo_dev;
1345 LASSERT(list_empty(&o->lo_linkage));
1348 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1354 EXPORT_SYMBOL(lu_object_fini);
1357 * Add object \a o as first layer of compound object \a h
1359 * This is typically called by the ->ldo_object_alloc() method of top-level
1362 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1364 list_move(&o->lo_linkage, &h->loh_layers);
1366 EXPORT_SYMBOL(lu_object_add_top);
1369 * Add object \a o as a layer of compound object, going after \a before.
1371 * This is typically called by the ->ldo_object_alloc() method of \a
1374 void lu_object_add(struct lu_object *before, struct lu_object *o)
1376 list_move(&o->lo_linkage, &before->lo_linkage);
1378 EXPORT_SYMBOL(lu_object_add);
1381 * Initialize compound object.
1383 int lu_object_header_init(struct lu_object_header *h)
1385 memset(h, 0, sizeof *h);
1386 atomic_set(&h->loh_ref, 1);
1387 INIT_HLIST_NODE(&h->loh_hash);
1388 INIT_LIST_HEAD(&h->loh_lru);
1389 INIT_LIST_HEAD(&h->loh_layers);
1390 lu_ref_init(&h->loh_reference);
1393 EXPORT_SYMBOL(lu_object_header_init);
1396 * Finalize compound object.
1398 void lu_object_header_fini(struct lu_object_header *h)
1400 LASSERT(list_empty(&h->loh_layers));
1401 LASSERT(list_empty(&h->loh_lru));
1402 LASSERT(hlist_unhashed(&h->loh_hash));
1403 lu_ref_fini(&h->loh_reference);
1405 EXPORT_SYMBOL(lu_object_header_fini);
1408 * Given a compound object, find its slice, corresponding to the device type
1411 struct lu_object *lu_object_locate(struct lu_object_header *h,
1412 const struct lu_device_type *dtype)
1414 struct lu_object *o;
1416 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1417 if (o->lo_dev->ld_type == dtype)
1422 EXPORT_SYMBOL(lu_object_locate);
1425 * Finalize and free devices in the device stack.
1427 * Finalize device stack by purging object cache, and calling
1428 * lu_device_type_operations::ldto_device_fini() and
1429 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1431 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1433 struct lu_site *site = top->ld_site;
1434 struct lu_device *scan;
1435 struct lu_device *next;
1437 lu_site_purge(env, site, ~0);
1438 for (scan = top; scan != NULL; scan = next) {
1439 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1440 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1441 lu_device_put(scan);
1445 lu_site_purge(env, site, ~0);
1447 for (scan = top; scan != NULL; scan = next) {
1448 const struct lu_device_type *ldt = scan->ld_type;
1450 next = ldt->ldt_ops->ldto_device_free(env, scan);
1456 * Maximal number of tld slots.
1458 LU_CONTEXT_KEY_NR = 40
1461 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1463 static DECLARE_RWSEM(lu_key_initing);
1466 * Global counter incremented whenever key is registered, unregistered,
1467 * revived or quiesced. This is used to void unnecessary calls to
1468 * lu_context_refill(). No locking is provided, as initialization and shutdown
1469 * are supposed to be externally serialized.
1471 static atomic_t key_set_version = ATOMIC_INIT(0);
1476 int lu_context_key_register(struct lu_context_key *key)
1481 LASSERT(key->lct_init != NULL);
1482 LASSERT(key->lct_fini != NULL);
1483 LASSERT(key->lct_tags != 0);
1484 LASSERT(key->lct_owner != NULL);
1487 atomic_set(&key->lct_used, 1);
1488 lu_ref_init(&key->lct_reference);
1489 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1493 if (cmpxchg(&lu_keys[i], NULL, key) != NULL)
1497 atomic_inc(&key_set_version);
1501 lu_ref_fini(&key->lct_reference);
1502 atomic_set(&key->lct_used, 0);
1506 EXPORT_SYMBOL(lu_context_key_register);
1508 static void key_fini(struct lu_context *ctx, int index)
1510 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1511 struct lu_context_key *key;
1513 key = lu_keys[index];
1514 LASSERT(key != NULL);
1515 LASSERT(key->lct_fini != NULL);
1516 LASSERT(atomic_read(&key->lct_used) > 0);
1518 key->lct_fini(ctx, key, ctx->lc_value[index]);
1519 lu_ref_del(&key->lct_reference, "ctx", ctx);
1520 if (atomic_dec_and_test(&key->lct_used))
1521 wake_up_var(&key->lct_used);
1523 LASSERT(key->lct_owner != NULL);
1524 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1525 LINVRNT(module_refcount(key->lct_owner) > 0);
1526 module_put(key->lct_owner);
1528 ctx->lc_value[index] = NULL;
1535 void lu_context_key_degister(struct lu_context_key *key)
1537 LASSERT(atomic_read(&key->lct_used) >= 1);
1538 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1540 lu_context_key_quiesce(key);
1542 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1545 * Wait until all transient contexts referencing this key have
1546 * run lu_context_key::lct_fini() method.
1548 atomic_dec(&key->lct_used);
1549 wait_var_event(&key->lct_used, atomic_read(&key->lct_used) == 0);
1551 if (!WARN_ON(lu_keys[key->lct_index] == NULL))
1552 lu_ref_fini(&key->lct_reference);
1554 smp_store_release(&lu_keys[key->lct_index], NULL);
1556 EXPORT_SYMBOL(lu_context_key_degister);
1559 * Register a number of keys. This has to be called after all keys have been
1560 * initialized by a call to LU_CONTEXT_KEY_INIT().
1562 int lu_context_key_register_many(struct lu_context_key *k, ...)
1564 struct lu_context_key *key = k;
1570 result = lu_context_key_register(key);
1573 key = va_arg(args, struct lu_context_key *);
1574 } while (key != NULL);
1580 lu_context_key_degister(k);
1581 k = va_arg(args, struct lu_context_key *);
1588 EXPORT_SYMBOL(lu_context_key_register_many);
1591 * De-register a number of keys. This is a dual to
1592 * lu_context_key_register_many().
1594 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1600 lu_context_key_degister(k);
1601 k = va_arg(args, struct lu_context_key*);
1602 } while (k != NULL);
1605 EXPORT_SYMBOL(lu_context_key_degister_many);
1608 * Revive a number of keys.
1610 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1616 lu_context_key_revive(k);
1617 k = va_arg(args, struct lu_context_key*);
1618 } while (k != NULL);
1621 EXPORT_SYMBOL(lu_context_key_revive_many);
1624 * Quiescent a number of keys.
1626 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1632 lu_context_key_quiesce(k);
1633 k = va_arg(args, struct lu_context_key*);
1634 } while (k != NULL);
1637 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1640 * Return value associated with key \a key in context \a ctx.
1642 void *lu_context_key_get(const struct lu_context *ctx,
1643 const struct lu_context_key *key)
1645 LINVRNT(ctx->lc_state == LCS_ENTERED);
1646 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1647 LASSERT(lu_keys[key->lct_index] == key);
1648 return ctx->lc_value[key->lct_index];
1650 EXPORT_SYMBOL(lu_context_key_get);
1653 * List of remembered contexts. XXX document me.
1655 static LIST_HEAD(lu_context_remembered);
1656 static DEFINE_SPINLOCK(lu_context_remembered_guard);
1659 * Destroy \a key in all remembered contexts. This is used to destroy key
1660 * values in "shared" contexts (like service threads), when a module owning
1661 * the key is about to be unloaded.
1663 void lu_context_key_quiesce(struct lu_context_key *key)
1665 struct lu_context *ctx;
1667 if (!(key->lct_tags & LCT_QUIESCENT)) {
1669 * The write-lock on lu_key_initing will ensure that any
1670 * keys_fill() which didn't see LCT_QUIESCENT will have
1671 * finished before we call key_fini().
1673 down_write(&lu_key_initing);
1674 key->lct_tags |= LCT_QUIESCENT;
1675 up_write(&lu_key_initing);
1677 spin_lock(&lu_context_remembered_guard);
1678 list_for_each_entry(ctx, &lu_context_remembered, lc_remember) {
1679 spin_until_cond(READ_ONCE(ctx->lc_state) != LCS_LEAVING);
1680 key_fini(ctx, key->lct_index);
1683 spin_unlock(&lu_context_remembered_guard);
1687 void lu_context_key_revive(struct lu_context_key *key)
1689 key->lct_tags &= ~LCT_QUIESCENT;
1690 atomic_inc(&key_set_version);
1693 static void keys_fini(struct lu_context *ctx)
1697 if (ctx->lc_value == NULL)
1700 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1703 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1704 ctx->lc_value = NULL;
1707 static int keys_fill(struct lu_context *ctx)
1713 * A serialisation with lu_context_key_quiesce() is needed, to
1714 * ensure we see LCT_QUIESCENT and don't allocate a new value
1715 * after it freed one. The rwsem provides this. As down_read()
1716 * does optimistic spinning while the writer is active, this is
1717 * unlikely to ever sleep.
1719 down_read(&lu_key_initing);
1720 ctx->lc_version = atomic_read(&key_set_version);
1722 LINVRNT(ctx->lc_value);
1723 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1724 struct lu_context_key *key;
1727 if (!ctx->lc_value[i] && key &&
1728 (key->lct_tags & ctx->lc_tags) &&
1730 * Don't create values for a LCT_QUIESCENT key, as this
1731 * will pin module owning a key.
1733 !(key->lct_tags & LCT_QUIESCENT)) {
1736 LINVRNT(key->lct_init != NULL);
1737 LINVRNT(key->lct_index == i);
1739 LASSERT(key->lct_owner != NULL);
1740 if (!(ctx->lc_tags & LCT_NOREF) &&
1741 try_module_get(key->lct_owner) == 0) {
1742 /* module is unloading, skip this key */
1746 value = key->lct_init(ctx, key);
1747 if (unlikely(IS_ERR(value))) {
1748 rc = PTR_ERR(value);
1752 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1753 atomic_inc(&key->lct_used);
1755 * This is the only place in the code, where an
1756 * element of ctx->lc_value[] array is set to non-NULL
1759 ctx->lc_value[i] = value;
1760 if (key->lct_exit != NULL)
1761 ctx->lc_tags |= LCT_HAS_EXIT;
1765 up_read(&lu_key_initing);
1769 static int keys_init(struct lu_context *ctx)
1771 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1772 if (likely(ctx->lc_value != NULL))
1773 return keys_fill(ctx);
1779 * Initialize context data-structure. Create values for all keys.
1781 int lu_context_init(struct lu_context *ctx, __u32 tags)
1785 memset(ctx, 0, sizeof *ctx);
1786 ctx->lc_state = LCS_INITIALIZED;
1787 ctx->lc_tags = tags;
1788 if (tags & LCT_REMEMBER) {
1789 spin_lock(&lu_context_remembered_guard);
1790 list_add(&ctx->lc_remember, &lu_context_remembered);
1791 spin_unlock(&lu_context_remembered_guard);
1793 INIT_LIST_HEAD(&ctx->lc_remember);
1796 rc = keys_init(ctx);
1798 lu_context_fini(ctx);
1802 EXPORT_SYMBOL(lu_context_init);
1805 * Finalize context data-structure. Destroy key values.
1807 void lu_context_fini(struct lu_context *ctx)
1809 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1810 ctx->lc_state = LCS_FINALIZED;
1812 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1813 LASSERT(list_empty(&ctx->lc_remember));
1815 /* could race with key degister */
1816 spin_lock(&lu_context_remembered_guard);
1817 list_del_init(&ctx->lc_remember);
1818 spin_unlock(&lu_context_remembered_guard);
1822 EXPORT_SYMBOL(lu_context_fini);
1825 * Called before entering context.
1827 void lu_context_enter(struct lu_context *ctx)
1829 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1830 ctx->lc_state = LCS_ENTERED;
1832 EXPORT_SYMBOL(lu_context_enter);
1835 * Called after exiting from \a ctx
1837 void lu_context_exit(struct lu_context *ctx)
1841 LINVRNT(ctx->lc_state == LCS_ENTERED);
1843 * Disable preempt to ensure we get a warning if
1844 * any lct_exit ever tries to sleep. That would hurt
1845 * lu_context_key_quiesce() which spins waiting for us.
1846 * This also ensure we aren't preempted while the state
1847 * is LCS_LEAVING, as that too would cause problems for
1848 * lu_context_key_quiesce().
1852 * Ensure lu_context_key_quiesce() sees LCS_LEAVING
1853 * or we see LCT_QUIESCENT
1855 smp_store_mb(ctx->lc_state, LCS_LEAVING);
1856 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value) {
1857 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1858 struct lu_context_key *key;
1861 if (ctx->lc_value[i] &&
1862 !(key->lct_tags & LCT_QUIESCENT) &&
1864 key->lct_exit(ctx, key, ctx->lc_value[i]);
1868 smp_store_release(&ctx->lc_state, LCS_LEFT);
1871 EXPORT_SYMBOL(lu_context_exit);
1874 * Allocate for context all missing keys that were registered after context
1875 * creation. key_set_version is only changed in rare cases when modules
1876 * are loaded and removed.
1878 int lu_context_refill(struct lu_context *ctx)
1880 if (likely(ctx->lc_version == atomic_read(&key_set_version)))
1883 return keys_fill(ctx);
1887 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1888 * obd being added. Currently, this is only used on client side, specifically
1889 * for echo device client, for other stack (like ptlrpc threads), context are
1890 * predefined when the lu_device type are registered, during the module probe
1893 u32 lu_context_tags_default = LCT_CL_THREAD;
1894 u32 lu_session_tags_default = LCT_SESSION;
1896 void lu_context_tags_update(__u32 tags)
1898 spin_lock(&lu_context_remembered_guard);
1899 lu_context_tags_default |= tags;
1900 atomic_inc(&key_set_version);
1901 spin_unlock(&lu_context_remembered_guard);
1903 EXPORT_SYMBOL(lu_context_tags_update);
1905 void lu_context_tags_clear(__u32 tags)
1907 spin_lock(&lu_context_remembered_guard);
1908 lu_context_tags_default &= ~tags;
1909 atomic_inc(&key_set_version);
1910 spin_unlock(&lu_context_remembered_guard);
1912 EXPORT_SYMBOL(lu_context_tags_clear);
1914 void lu_session_tags_update(__u32 tags)
1916 spin_lock(&lu_context_remembered_guard);
1917 lu_session_tags_default |= tags;
1918 atomic_inc(&key_set_version);
1919 spin_unlock(&lu_context_remembered_guard);
1921 EXPORT_SYMBOL(lu_session_tags_update);
1923 void lu_session_tags_clear(__u32 tags)
1925 spin_lock(&lu_context_remembered_guard);
1926 lu_session_tags_default &= ~tags;
1927 atomic_inc(&key_set_version);
1928 spin_unlock(&lu_context_remembered_guard);
1930 EXPORT_SYMBOL(lu_session_tags_clear);
1932 int lu_env_init(struct lu_env *env, __u32 tags)
1937 result = lu_context_init(&env->le_ctx, tags);
1938 if (likely(result == 0))
1939 lu_context_enter(&env->le_ctx);
1942 EXPORT_SYMBOL(lu_env_init);
1944 void lu_env_fini(struct lu_env *env)
1946 lu_context_exit(&env->le_ctx);
1947 lu_context_fini(&env->le_ctx);
1950 EXPORT_SYMBOL(lu_env_fini);
1952 int lu_env_refill(struct lu_env *env)
1956 result = lu_context_refill(&env->le_ctx);
1957 if (result == 0 && env->le_ses != NULL)
1958 result = lu_context_refill(env->le_ses);
1961 EXPORT_SYMBOL(lu_env_refill);
1964 * Currently, this API will only be used by echo client.
1965 * Because echo client and normal lustre client will share
1966 * same cl_env cache. So echo client needs to refresh
1967 * the env context after it get one from the cache, especially
1968 * when normal client and echo client co-exist in the same client.
1970 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1975 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1976 env->le_ctx.lc_version = 0;
1977 env->le_ctx.lc_tags |= ctags;
1980 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1981 env->le_ses->lc_version = 0;
1982 env->le_ses->lc_tags |= stags;
1985 result = lu_env_refill(env);
1989 EXPORT_SYMBOL(lu_env_refill_by_tags);
1992 struct lu_env_item {
1993 struct task_struct *lei_task; /* rhashtable key */
1994 struct rhash_head lei_linkage;
1995 struct lu_env *lei_env;
1996 struct rcu_head lei_rcu_head;
1999 static const struct rhashtable_params lu_env_rhash_params = {
2000 .key_len = sizeof(struct task_struct *),
2001 .key_offset = offsetof(struct lu_env_item, lei_task),
2002 .head_offset = offsetof(struct lu_env_item, lei_linkage),
2005 struct rhashtable lu_env_rhash;
2007 struct lu_env_percpu {
2008 struct task_struct *lep_task;
2009 struct lu_env *lep_env ____cacheline_aligned_in_smp;
2012 static struct lu_env_percpu lu_env_percpu[NR_CPUS];
2014 int lu_env_add(struct lu_env *env)
2016 struct lu_env_item *lei, *old;
2024 lei->lei_task = current;
2027 old = rhashtable_lookup_get_insert_fast(&lu_env_rhash,
2029 lu_env_rhash_params);
2034 EXPORT_SYMBOL(lu_env_add);
2036 static void lu_env_item_free(struct rcu_head *head)
2038 struct lu_env_item *lei;
2040 lei = container_of(head, struct lu_env_item, lei_rcu_head);
2044 void lu_env_remove(struct lu_env *env)
2046 struct lu_env_item *lei;
2047 const void *task = current;
2050 for_each_possible_cpu(i) {
2051 if (lu_env_percpu[i].lep_env == env) {
2052 LASSERT(lu_env_percpu[i].lep_task == task);
2053 lu_env_percpu[i].lep_task = NULL;
2054 lu_env_percpu[i].lep_env = NULL;
2058 /* The rcu_lock is not taking in this case since the key
2059 * used is the actual task_struct. This implies that each
2060 * object is only removed by the owning thread, so there
2061 * can never be a race on a particular object.
2063 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
2064 lu_env_rhash_params);
2065 if (lei && rhashtable_remove_fast(&lu_env_rhash, &lei->lei_linkage,
2066 lu_env_rhash_params) == 0)
2067 call_rcu(&lei->lei_rcu_head, lu_env_item_free);
2069 EXPORT_SYMBOL(lu_env_remove);
2071 struct lu_env *lu_env_find(void)
2073 struct lu_env *env = NULL;
2074 struct lu_env_item *lei;
2075 const void *task = current;
2078 if (lu_env_percpu[i].lep_task == current) {
2079 env = lu_env_percpu[i].lep_env;
2085 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
2086 lu_env_rhash_params);
2089 lu_env_percpu[i].lep_task = current;
2090 lu_env_percpu[i].lep_env = env;
2096 EXPORT_SYMBOL(lu_env_find);
2098 static struct shrinker *lu_site_shrinker;
2100 typedef struct lu_site_stats{
2101 unsigned lss_populated;
2102 unsigned lss_max_search;
2107 static void lu_site_stats_get(const struct lu_site *s,
2108 lu_site_stats_t *stats)
2110 int cnt = cfs_hash_size_get(s->ls_obj_hash);
2112 * percpu_counter_sum_positive() won't accept a const pointer
2113 * as it does modify the struct by taking a spinlock
2115 struct lu_site *s2 = (struct lu_site *)s;
2117 stats->lss_busy += cnt -
2118 percpu_counter_sum_positive(&s2->ls_lru_len_counter);
2120 stats->lss_total += cnt;
2121 stats->lss_max_search = 0;
2122 stats->lss_populated = 0;
2127 * lu_cache_shrink_count() returns an approximate number of cached objects
2128 * that can be freed by shrink_slab(). A counter, which tracks the
2129 * number of items in the site's lru, is maintained in a percpu_counter
2130 * for each site. The percpu values are incremented and decremented as
2131 * objects are added or removed from the lru. The percpu values are summed
2132 * and saved whenever a percpu value exceeds a threshold. Thus the saved,
2133 * summed value at any given time may not accurately reflect the current
2134 * lru length. But this value is sufficiently accurate for the needs of
2137 * Using a per cpu counter is a compromise solution to concurrent access:
2138 * lu_object_put() can update the counter without locking the site and
2139 * lu_cache_shrink_count can sum the counters without locking each
2140 * ls_obj_hash bucket.
2142 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
2143 struct shrink_control *sc)
2146 struct lu_site *tmp;
2147 unsigned long cached = 0;
2149 if (!(sc->gfp_mask & __GFP_FS))
2152 down_read(&lu_sites_guard);
2153 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage)
2154 cached += percpu_counter_read_positive(&s->ls_lru_len_counter);
2155 up_read(&lu_sites_guard);
2157 cached = (cached / 100) * sysctl_vfs_cache_pressure;
2158 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
2159 cached, sysctl_vfs_cache_pressure);
2164 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
2165 struct shrink_control *sc)
2168 struct lu_site *tmp;
2169 unsigned long remain = sc->nr_to_scan;
2172 if (!(sc->gfp_mask & __GFP_FS))
2173 /* We must not take the lu_sites_guard lock when
2174 * __GFP_FS is *not* set because of the deadlock
2175 * possibility detailed above. Additionally,
2176 * since we cannot determine the number of
2177 * objects in the cache without taking this
2178 * lock, we're in a particularly tough spot. As
2179 * a result, we'll just lie and say our cache is
2180 * empty. This _should_ be ok, as we can't
2181 * reclaim objects when __GFP_FS is *not* set
2186 down_write(&lu_sites_guard);
2187 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
2188 remain = lu_site_purge(&lu_shrink_env, s, remain);
2190 * Move just shrunk site to the tail of site list to
2191 * assure shrinking fairness.
2193 list_move_tail(&s->ls_linkage, &splice);
2195 list_splice(&splice, lu_sites.prev);
2196 up_write(&lu_sites_guard);
2198 return sc->nr_to_scan - remain;
2201 #ifndef HAVE_SHRINKER_COUNT
2203 * There exists a potential lock inversion deadlock scenario when using
2204 * Lustre on top of ZFS. This occurs between one of ZFS's
2205 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2206 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2207 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2208 * lock. Obviously neither thread will wake and drop their respective hold
2211 * To prevent this from happening we must ensure the lu_sites_guard lock is
2212 * not taken while down this code path. ZFS reliably does not set the
2213 * __GFP_FS bit in its code paths, so this can be used to determine if it
2214 * is safe to take the lu_sites_guard lock.
2216 * Ideally we should accurately return the remaining number of cached
2217 * objects without taking the lu_sites_guard lock, but this is not
2218 * possible in the current implementation.
2220 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2223 struct shrink_control scv = {
2224 .nr_to_scan = shrink_param(sc, nr_to_scan),
2225 .gfp_mask = shrink_param(sc, gfp_mask)
2228 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2230 if (scv.nr_to_scan != 0)
2231 lu_cache_shrink_scan(shrinker, &scv);
2233 cached = lu_cache_shrink_count(shrinker, &scv);
2237 #endif /* HAVE_SHRINKER_COUNT */
2245 * Environment to be used in debugger, contains all tags.
2247 static struct lu_env lu_debugging_env;
2250 * Debugging printer function using printk().
2252 int lu_printk_printer(const struct lu_env *env,
2253 void *unused, const char *format, ...)
2257 va_start(args, format);
2258 vprintk(format, args);
2263 int lu_debugging_setup(void)
2265 return lu_env_init(&lu_debugging_env, ~0);
2268 void lu_context_keys_dump(void)
2272 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2273 struct lu_context_key *key;
2277 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2278 i, key, key->lct_tags,
2279 key->lct_init, key->lct_fini, key->lct_exit,
2280 key->lct_index, atomic_read(&key->lct_used),
2281 key->lct_owner ? key->lct_owner->name : "",
2283 lu_ref_print(&key->lct_reference);
2289 * Initialization of global lu_* data.
2291 int lu_global_init(void)
2294 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2295 lu_cache_shrink_count, lu_cache_shrink_scan);
2297 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2299 result = lu_ref_global_init();
2303 LU_CONTEXT_KEY_INIT(&lu_global_key);
2304 result = lu_context_key_register(&lu_global_key);
2309 * At this level, we don't know what tags are needed, so allocate them
2310 * conservatively. This should not be too bad, because this
2311 * environment is global.
2313 down_write(&lu_sites_guard);
2314 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2315 up_write(&lu_sites_guard);
2320 * seeks estimation: 3 seeks to read a record from oi, one to read
2321 * inode, one for ea. Unfortunately setting this high value results in
2322 * lu_object/inode cache consuming all the memory.
2324 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2325 if (lu_site_shrinker == NULL)
2328 result = rhashtable_init(&lu_env_rhash, &lu_env_rhash_params);
2334 * Dual to lu_global_init().
2336 void lu_global_fini(void)
2338 if (lu_site_shrinker != NULL) {
2339 remove_shrinker(lu_site_shrinker);
2340 lu_site_shrinker = NULL;
2343 lu_context_key_degister(&lu_global_key);
2346 * Tear shrinker environment down _after_ de-registering
2347 * lu_global_key, because the latter has a value in the former.
2349 down_write(&lu_sites_guard);
2350 lu_env_fini(&lu_shrink_env);
2351 up_write(&lu_sites_guard);
2353 rhashtable_destroy(&lu_env_rhash);
2355 lu_ref_global_fini();
2358 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2360 #ifdef CONFIG_PROC_FS
2361 struct lprocfs_counter ret;
2363 lprocfs_stats_collect(stats, idx, &ret);
2364 return (__u32)ret.lc_count;
2371 * Output site statistical counters into a buffer. Suitable for
2372 * lprocfs_rd_*()-style functions.
2374 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2376 lu_site_stats_t stats;
2378 memset(&stats, 0, sizeof(stats));
2379 lu_site_stats_get(s, &stats);
2381 seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2384 stats.lss_populated,
2385 CFS_HASH_NHLIST(s->ls_obj_hash),
2386 stats.lss_max_search,
2387 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2388 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2389 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2390 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2391 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2392 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2395 EXPORT_SYMBOL(lu_site_stats_seq_print);
2398 * Helper function to initialize a number of kmem slab caches at once.
2400 int lu_kmem_init(struct lu_kmem_descr *caches)
2403 struct lu_kmem_descr *iter = caches;
2405 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2406 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2409 if (*iter->ckd_cache == NULL) {
2411 /* free all previously allocated caches */
2412 lu_kmem_fini(caches);
2418 EXPORT_SYMBOL(lu_kmem_init);
2421 * Helper function to finalize a number of kmem slab cached at once. Dual to
2424 void lu_kmem_fini(struct lu_kmem_descr *caches)
2426 for (; caches->ckd_cache != NULL; ++caches) {
2427 if (*caches->ckd_cache != NULL) {
2428 kmem_cache_destroy(*caches->ckd_cache);
2429 *caches->ckd_cache = NULL;
2433 EXPORT_SYMBOL(lu_kmem_fini);
2436 * Temporary solution to be able to assign fid in ->do_create()
2437 * till we have fully-functional OST fids
2439 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2440 const struct lu_fid *fid)
2442 struct lu_site *s = o->lo_dev->ld_site;
2443 struct lu_fid *old = &o->lo_header->loh_fid;
2444 struct cfs_hash *hs;
2445 struct cfs_hash_bd bd;
2447 LASSERT(fid_is_zero(old));
2449 /* supposed to be unique */
2450 hs = s->ls_obj_hash;
2451 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2452 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
2455 struct lu_object *shadow;
2457 shadow = htable_lookup(s, &bd, fid, &version);
2458 /* supposed to be unique */
2459 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2463 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2464 cfs_hash_bd_unlock(hs, &bd, 1);
2466 EXPORT_SYMBOL(lu_object_assign_fid);
2469 * allocates object with 0 (non-assiged) fid
2470 * XXX: temporary solution to be able to assign fid in ->do_create()
2471 * till we have fully-functional OST fids
2473 struct lu_object *lu_object_anon(const struct lu_env *env,
2474 struct lu_device *dev,
2475 const struct lu_object_conf *conf)
2478 struct lu_object *o;
2482 o = lu_object_alloc(env, dev, &fid);
2484 rc = lu_object_start(env, dev, o, conf);
2486 lu_object_free(env, o);
2493 EXPORT_SYMBOL(lu_object_anon);
2495 struct lu_buf LU_BUF_NULL = {
2499 EXPORT_SYMBOL(LU_BUF_NULL);
2501 void lu_buf_free(struct lu_buf *buf)
2505 LASSERT(buf->lb_len > 0);
2506 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2511 EXPORT_SYMBOL(lu_buf_free);
2513 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2516 LASSERT(buf->lb_buf == NULL);
2517 LASSERT(buf->lb_len == 0);
2518 OBD_ALLOC_LARGE(buf->lb_buf, size);
2519 if (likely(buf->lb_buf))
2522 EXPORT_SYMBOL(lu_buf_alloc);
2524 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2527 lu_buf_alloc(buf, size);
2529 EXPORT_SYMBOL(lu_buf_realloc);
2531 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2533 if (buf->lb_buf == NULL && buf->lb_len == 0)
2534 lu_buf_alloc(buf, len);
2536 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2537 lu_buf_realloc(buf, len);
2541 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2544 * Increase the size of the \a buf.
2545 * preserves old data in buffer
2546 * old buffer remains unchanged on error
2547 * \retval 0 or -ENOMEM
2549 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2553 if (len <= buf->lb_len)
2556 OBD_ALLOC_LARGE(ptr, len);
2560 /* Free the old buf */
2561 if (buf->lb_buf != NULL) {
2562 memcpy(ptr, buf->lb_buf, buf->lb_len);
2563 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2570 EXPORT_SYMBOL(lu_buf_check_and_grow);