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 #include <linux/processor.h>
46 #include <linux/random.h>
48 #include <libcfs/libcfs.h>
49 #include <libcfs/libcfs_hash.h> /* hash_long() */
50 #include <libcfs/linux/linux-mem.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_disk.h>
54 #include <lustre_fid.h>
55 #include <lu_object.h>
58 struct lu_site_bkt_data {
60 * LRU list, updated on each access to object. Protected by
63 * "Cold" end of LRU is lu_site::ls_lru.next. Accessed object are
64 * moved to the lu_site::ls_lru.prev
66 struct list_head lsb_lru;
68 * Wait-queue signaled when an object in this site is ultimately
69 * destroyed (lu_object_free()) or initialized (lu_object_start()).
70 * It is used by lu_object_find() to wait before re-trying when
71 * object in the process of destruction is found in the hash table;
72 * or wait object to be initialized by the allocator.
74 * \see htable_lookup().
76 wait_queue_head_t lsb_waitq;
80 LU_CACHE_PERCENT_MAX = 50,
81 LU_CACHE_PERCENT_DEFAULT = 20
84 #define LU_CACHE_NR_MAX_ADJUST 512
85 #define LU_CACHE_NR_UNLIMITED -1
86 #define LU_CACHE_NR_DEFAULT LU_CACHE_NR_UNLIMITED
87 #define LU_CACHE_NR_LDISKFS_LIMIT LU_CACHE_NR_UNLIMITED
88 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
89 #define LU_CACHE_NR_ZFS_LIMIT 10240
91 #define LU_SITE_BITS_MIN 12
92 #define LU_SITE_BITS_MAX 24
93 #define LU_SITE_BITS_MAX_CL 19
95 * Max 256 buckets, we don't want too many buckets because:
96 * - consume too much memory (currently max 16K)
97 * - avoid unbalanced LRU list
98 * With few cpus there is little gain from extra buckets, so
99 * we treat this as a maximum in lu_site_init().
101 #define LU_SITE_BKT_BITS 8
104 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
105 module_param(lu_cache_percent, int, 0644);
106 MODULE_PARM_DESC(lu_cache_percent, "Percentage of memory to be used as lu_object cache");
108 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
109 module_param(lu_cache_nr, long, 0644);
110 MODULE_PARM_DESC(lu_cache_nr, "Maximum number of objects in lu_object cache");
112 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
113 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx);
115 static u32 lu_fid_hash(const void *data, u32 seed)
117 const struct lu_fid *fid = data;
119 seed = cfs_hash_32(seed ^ fid->f_oid, 32);
120 seed ^= cfs_hash_64(fid->f_seq, 32);
124 static inline int lu_bkt_hash(struct lu_site *s, const struct lu_fid *fid)
126 return lu_fid_hash(fid, s->ls_bkt_seed) &
131 lu_site_wq_from_fid(struct lu_site *site, struct lu_fid *fid)
133 struct lu_site_bkt_data *bkt;
135 bkt = &site->ls_bkts[lu_bkt_hash(site, fid)];
136 return &bkt->lsb_waitq;
138 EXPORT_SYMBOL(lu_site_wq_from_fid);
141 * Decrease reference counter on object. If last reference is freed, return
142 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
143 * case, free object immediately.
145 void lu_object_put(const struct lu_env *env, struct lu_object *o)
147 struct lu_site_bkt_data *bkt;
148 struct lu_object_header *top = o->lo_header;
149 struct lu_site *site = o->lo_dev->ld_site;
150 struct lu_object *orig = o;
151 struct cfs_hash_bd bd;
152 const struct lu_fid *fid = lu_object_fid(o);
156 * till we have full fids-on-OST implemented anonymous objects
157 * are possible in OSP. such an object isn't listed in the site
158 * so we should not remove it from the site.
160 if (fid_is_zero(fid)) {
161 LASSERT(top->loh_hash.next == NULL
162 && top->loh_hash.pprev == NULL);
163 LASSERT(list_empty(&top->loh_lru));
164 if (!atomic_dec_and_test(&top->loh_ref))
166 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
167 if (o->lo_ops->loo_object_release != NULL)
168 o->lo_ops->loo_object_release(env, o);
170 lu_object_free(env, orig);
174 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
176 is_dying = lu_object_is_dying(top);
177 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
178 /* at this point the object reference is dropped and lock is
179 * not taken, so lu_object should not be touched because it
180 * can be freed by concurrent thread. Use local variable for
185 * somebody may be waiting for this, currently only
186 * used for cl_object, see cl_object_put_last().
188 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
189 wake_up_all(&bkt->lsb_waitq);
195 * When last reference is released, iterate over object
196 * layers, and notify them that object is no longer busy.
198 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
199 if (o->lo_ops->loo_object_release != NULL)
200 o->lo_ops->loo_object_release(env, o);
203 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
204 spin_lock(&bkt->lsb_waitq.lock);
206 /* don't use local 'is_dying' here because if was taken without lock
207 * but here we need the latest actual value of it so check lu_object
210 if (!lu_object_is_dying(top) &&
211 (lu_object_exists(orig) || lu_object_is_cl(orig))) {
212 LASSERT(list_empty(&top->loh_lru));
213 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
214 spin_unlock(&bkt->lsb_waitq.lock);
215 percpu_counter_inc(&site->ls_lru_len_counter);
216 CDEBUG(D_INODE, "Add %p/%p to site lru. hash: %p, bkt: %p\n",
217 orig, top, site->ls_obj_hash, bkt);
218 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
223 * If object is dying (will not be cached) then remove it
224 * from hash table (it is already not on the LRU).
226 * This is done with hash table lists locked. As the only
227 * way to acquire first reference to previously unreferenced
228 * object is through hash-table lookup (lu_object_find())
229 * which is done under hash-table, no race with concurrent
230 * object lookup is possible and we can safely destroy object below.
232 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
233 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
234 spin_unlock(&bkt->lsb_waitq.lock);
235 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
236 /* Object was already removed from hash above, can kill it. */
237 lu_object_free(env, orig);
239 EXPORT_SYMBOL(lu_object_put);
242 * Put object and don't keep in cache. This is temporary solution for
243 * multi-site objects when its layering is not constant.
245 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
247 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
248 return lu_object_put(env, o);
250 EXPORT_SYMBOL(lu_object_put_nocache);
253 * Kill the object and take it out of LRU cache.
254 * Currently used by client code for layout change.
256 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
258 struct lu_object_header *top;
261 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
262 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
263 struct lu_site *site = o->lo_dev->ld_site;
264 struct cfs_hash *obj_hash = site->ls_obj_hash;
265 struct cfs_hash_bd bd;
267 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
268 if (!list_empty(&top->loh_lru)) {
269 struct lu_site_bkt_data *bkt;
271 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
272 spin_lock(&bkt->lsb_waitq.lock);
273 list_del_init(&top->loh_lru);
274 spin_unlock(&bkt->lsb_waitq.lock);
275 percpu_counter_dec(&site->ls_lru_len_counter);
277 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
278 cfs_hash_bd_unlock(obj_hash, &bd, 1);
281 EXPORT_SYMBOL(lu_object_unhash);
284 * Allocate new object.
286 * This follows object creation protocol, described in the comment within
287 * struct lu_device_operations definition.
289 static struct lu_object *lu_object_alloc(const struct lu_env *env,
290 struct lu_device *dev,
291 const struct lu_fid *f)
293 struct lu_object *top;
296 * Create top-level object slice. This will also create
299 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
301 return ERR_PTR(-ENOMEM);
305 * This is the only place where object fid is assigned. It's constant
308 top->lo_header->loh_fid = *f;
316 * This is called after object hash insertion to avoid returning an object with
319 static int lu_object_start(const struct lu_env *env, struct lu_device *dev,
320 struct lu_object *top,
321 const struct lu_object_conf *conf)
323 struct lu_object *scan;
324 struct list_head *layers;
325 unsigned int init_mask = 0;
326 unsigned int init_flag;
330 layers = &top->lo_header->loh_layers;
334 * Call ->loo_object_init() repeatedly, until no more new
335 * object slices are created.
339 list_for_each_entry(scan, layers, lo_linkage) {
340 if (init_mask & init_flag)
343 scan->lo_header = top->lo_header;
344 result = scan->lo_ops->loo_object_init(env, scan, conf);
348 init_mask |= init_flag;
354 list_for_each_entry_reverse(scan, layers, lo_linkage) {
355 if (scan->lo_ops->loo_object_start != NULL) {
356 result = scan->lo_ops->loo_object_start(env, scan);
362 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
364 set_bit(LU_OBJECT_INITED, &top->lo_header->loh_flags);
372 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
374 wait_queue_head_t *wq;
375 struct lu_site *site;
376 struct lu_object *scan;
377 struct list_head *layers;
380 site = o->lo_dev->ld_site;
381 layers = &o->lo_header->loh_layers;
382 wq = lu_site_wq_from_fid(site, &o->lo_header->loh_fid);
384 * First call ->loo_object_delete() method to release all resources.
386 list_for_each_entry_reverse(scan, layers, lo_linkage) {
387 if (scan->lo_ops->loo_object_delete != NULL)
388 scan->lo_ops->loo_object_delete(env, scan);
392 * Then, splice object layers into stand-alone list, and call
393 * ->loo_object_free() on all layers to free memory. Splice is
394 * necessary, because lu_object_header is freed together with the
397 list_splice_init(layers, &splice);
398 while (!list_empty(&splice)) {
400 * Free layers in bottom-to-top order, so that object header
401 * lives as long as possible and ->loo_object_free() methods
402 * can look at its contents.
404 o = container_of0(splice.prev, struct lu_object, lo_linkage);
405 list_del_init(&o->lo_linkage);
406 LASSERT(o->lo_ops->loo_object_free != NULL);
407 o->lo_ops->loo_object_free(env, o);
410 if (waitqueue_active(wq))
415 * Free \a nr objects from the cold end of the site LRU list.
416 * if canblock is 0, then don't block awaiting for another
417 * instance of lu_site_purge() to complete
419 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s,
420 int nr, int canblock)
422 struct lu_object_header *h;
423 struct lu_object_header *temp;
424 struct lu_site_bkt_data *bkt;
427 unsigned int start = 0;
432 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
436 * Under LRU list lock, scan LRU list and move unreferenced objects to
437 * the dispose list, removing them from LRU and hash table.
440 start = s->ls_purge_start;
441 bnr = (nr == ~0) ? -1 : nr / s->ls_bkt_cnt + 1;
444 * It doesn't make any sense to make purge threads parallel, that can
445 * only bring troubles to us. See LU-5331.
448 mutex_lock(&s->ls_purge_mutex);
449 else if (mutex_trylock(&s->ls_purge_mutex) == 0)
453 for (i = start; i < s->ls_bkt_cnt ; i++) {
455 bkt = &s->ls_bkts[i];
456 spin_lock(&bkt->lsb_waitq.lock);
458 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
459 LASSERT(atomic_read(&h->loh_ref) == 0);
461 LINVRNT(lu_bkt_hash(s, &h->loh_fid) == i);
463 /* Cannot remove from hash under current spinlock,
464 * so set flag to stop object from being found
465 * by htable_lookup().
467 set_bit(LU_OBJECT_PURGING, &h->loh_flags);
468 list_move(&h->loh_lru, &dispose);
469 percpu_counter_dec(&s->ls_lru_len_counter);
473 if (nr != ~0 && --nr == 0)
476 if (count > 0 && --count == 0)
480 spin_unlock(&bkt->lsb_waitq.lock);
483 * Free everything on the dispose list. This is safe against
484 * races due to the reasons described in lu_object_put().
486 while ((h = list_first_entry_or_null(&dispose,
487 struct lu_object_header,
489 cfs_hash_del(s->ls_obj_hash, &h->loh_fid, &h->loh_hash);
490 list_del_init(&h->loh_lru);
491 lu_object_free(env, lu_object_top(h));
492 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
498 mutex_unlock(&s->ls_purge_mutex);
500 if (nr != 0 && did_sth && start != 0) {
501 start = 0; /* restart from the first bucket */
504 /* race on s->ls_purge_start, but nobody cares */
505 s->ls_purge_start = i & (s->ls_bkt_cnt - 1);
509 EXPORT_SYMBOL(lu_site_purge_objects);
514 * Code below has to jump through certain loops to output object description
515 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
516 * composes object description from strings that are parts of _lines_ of
517 * output (i.e., strings that are not terminated by newline). This doesn't fit
518 * very well into libcfs_debug_msg() interface that assumes that each message
519 * supplied to it is a self-contained output line.
521 * To work around this, strings are collected in a temporary buffer
522 * (implemented as a value of lu_cdebug_key key), until terminating newline
523 * character is detected.
531 * XXX overflow is not handled correctly.
536 struct lu_cdebug_data {
540 char lck_area[LU_CDEBUG_LINE];
543 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
544 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
547 * Key, holding temporary buffer. This key is registered very early by
550 static struct lu_context_key lu_global_key = {
551 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
552 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
553 .lct_init = lu_global_key_init,
554 .lct_fini = lu_global_key_fini
558 * Printer function emitting messages through libcfs_debug_msg().
560 int lu_cdebug_printer(const struct lu_env *env,
561 void *cookie, const char *format, ...)
563 struct libcfs_debug_msg_data *msgdata = cookie;
564 struct lu_cdebug_data *key;
569 va_start(args, format);
571 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
572 LASSERT(key != NULL);
574 used = strlen(key->lck_area);
575 complete = format[strlen(format) - 1] == '\n';
577 * Append new chunk to the buffer.
579 vsnprintf(key->lck_area + used,
580 ARRAY_SIZE(key->lck_area) - used, format, args);
582 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
583 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
584 key->lck_area[0] = 0;
589 EXPORT_SYMBOL(lu_cdebug_printer);
592 * Print object header.
594 void lu_object_header_print(const struct lu_env *env, void *cookie,
595 lu_printer_t printer,
596 const struct lu_object_header *hdr)
598 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
599 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
601 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
602 list_empty((struct list_head *)&hdr->loh_lru) ? \
604 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
606 EXPORT_SYMBOL(lu_object_header_print);
609 * Print human readable representation of the \a o to the \a printer.
611 void lu_object_print(const struct lu_env *env, void *cookie,
612 lu_printer_t printer, const struct lu_object *o)
614 static const char ruler[] = "........................................";
615 struct lu_object_header *top;
619 lu_object_header_print(env, cookie, printer, top);
620 (*printer)(env, cookie, "{\n");
622 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
624 * print `.' \a depth times followed by type name and address
626 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
627 o->lo_dev->ld_type->ldt_name, o);
629 if (o->lo_ops->loo_object_print != NULL)
630 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
632 (*printer)(env, cookie, "\n");
635 (*printer)(env, cookie, "} header@%p\n", top);
637 EXPORT_SYMBOL(lu_object_print);
640 * Check object consistency.
642 int lu_object_invariant(const struct lu_object *o)
644 struct lu_object_header *top;
647 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
648 if (o->lo_ops->loo_object_invariant != NULL &&
649 !o->lo_ops->loo_object_invariant(o))
655 static struct lu_object *htable_lookup(struct lu_site *s,
656 struct cfs_hash_bd *bd,
657 const struct lu_fid *f,
660 struct lu_object_header *h;
661 struct hlist_node *hnode;
662 __u64 ver = cfs_hash_bd_version_get(bd);
665 return ERR_PTR(-ENOENT);
668 /* cfs_hash_bd_peek_locked is a somehow "internal" function
669 * of cfs_hash, it doesn't add refcount on object. */
670 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
672 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
673 return ERR_PTR(-ENOENT);
676 h = container_of0(hnode, struct lu_object_header, loh_hash);
677 if (!list_empty(&h->loh_lru)) {
678 struct lu_site_bkt_data *bkt;
680 bkt = &s->ls_bkts[lu_bkt_hash(s, &h->loh_fid)];
681 spin_lock(&bkt->lsb_waitq.lock);
682 /* Might have just been moved to the dispose list, in which
683 * case LU_OBJECT_PURGING will be set. In that case,
684 * delete it from the hash table immediately.
685 * When lu_site_purge_objects() tried, it will find it
686 * isn't there, which is harmless.
688 if (test_bit(LU_OBJECT_PURGING, &h->loh_flags)) {
689 spin_unlock(&bkt->lsb_waitq.lock);
690 cfs_hash_bd_del_locked(s->ls_obj_hash, bd, hnode);
691 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
692 return ERR_PTR(-ENOENT);
694 list_del_init(&h->loh_lru);
695 spin_unlock(&bkt->lsb_waitq.lock);
696 percpu_counter_dec(&s->ls_lru_len_counter);
698 cfs_hash_get(s->ls_obj_hash, hnode);
699 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
700 return lu_object_top(h);
704 * Search cache for an object with the fid \a f. If such object is found,
705 * return it. Otherwise, create new object, insert it into cache and return
706 * it. In any case, additional reference is acquired on the returned object.
708 struct lu_object *lu_object_find(const struct lu_env *env,
709 struct lu_device *dev, const struct lu_fid *f,
710 const struct lu_object_conf *conf)
712 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
714 EXPORT_SYMBOL(lu_object_find);
717 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
718 * the calculation for the number of objects to reclaim is not covered by
719 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
720 * This ensures that many concurrent threads will not accidentally purge
723 static void lu_object_limit(const struct lu_env *env,
724 struct lu_device *dev)
728 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
731 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
732 nr = (__u64)lu_cache_nr;
736 lu_site_purge_objects(env, dev->ld_site,
737 min_t(__u64, size - nr, LU_CACHE_NR_MAX_ADJUST),
742 * Core logic of lu_object_find*() functions.
744 * Much like lu_object_find(), but top level device of object is specifically
745 * \a dev rather than top level device of the site. This interface allows
746 * objects of different "stacking" to be created within the same site.
748 struct lu_object *lu_object_find_at(const struct lu_env *env,
749 struct lu_device *dev,
750 const struct lu_fid *f,
751 const struct lu_object_conf *conf)
754 struct lu_object *shadow;
757 struct cfs_hash_bd bd;
758 struct lu_site_bkt_data *bkt;
765 * This uses standard index maintenance protocol:
767 * - search index under lock, and return object if found;
768 * - otherwise, unlock index, allocate new object;
769 * - lock index and search again;
770 * - if nothing is found (usual case), insert newly created
772 * - otherwise (race: other thread inserted object), free
773 * object just allocated.
777 * For "LOC_F_NEW" case, we are sure the object is new established.
778 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
779 * just alloc and insert directly.
785 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_OBD_ZERO_NLINK_RACE)))
786 lu_site_purge(env, s, -1);
788 bkt = &s->ls_bkts[lu_bkt_hash(s, f)];
789 cfs_hash_bd_get(hs, f, &bd);
790 if (!(conf && conf->loc_flags & LOC_F_NEW)) {
791 cfs_hash_bd_lock(hs, &bd, 1);
792 o = htable_lookup(s, &bd, f, &version);
793 cfs_hash_bd_unlock(hs, &bd, 1);
796 if (likely(lu_object_is_inited(o->lo_header)))
799 wait_event_idle(bkt->lsb_waitq,
800 lu_object_is_inited(o->lo_header) ||
801 lu_object_is_dying(o->lo_header));
803 if (lu_object_is_dying(o->lo_header)) {
804 lu_object_put(env, o);
806 RETURN(ERR_PTR(-ENOENT));
812 if (PTR_ERR(o) != -ENOENT)
817 * Allocate new object, NB, object is unitialized in case object
818 * is changed between allocation and hash insertion, thus the object
819 * with stale attributes is returned.
821 o = lu_object_alloc(env, dev, f);
825 LASSERT(lu_fid_eq(lu_object_fid(o), f));
827 CFS_RACE_WAIT(OBD_FAIL_OBD_ZERO_NLINK_RACE);
829 cfs_hash_bd_lock(hs, &bd, 1);
831 if (conf && conf->loc_flags & LOC_F_NEW)
832 shadow = ERR_PTR(-ENOENT);
834 shadow = htable_lookup(s, &bd, f, &version);
835 if (likely(PTR_ERR(shadow) == -ENOENT)) {
836 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
837 cfs_hash_bd_unlock(hs, &bd, 1);
840 * This may result in rather complicated operations, including
841 * fld queries, inode loading, etc.
843 rc = lu_object_start(env, dev, o, conf);
845 lu_object_put_nocache(env, o);
849 wake_up_all(&bkt->lsb_waitq);
851 lu_object_limit(env, dev);
856 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
857 cfs_hash_bd_unlock(hs, &bd, 1);
858 lu_object_free(env, o);
860 if (!(conf && conf->loc_flags & LOC_F_NEW) &&
861 !lu_object_is_inited(shadow->lo_header)) {
862 wait_event_idle(bkt->lsb_waitq,
863 lu_object_is_inited(shadow->lo_header) ||
864 lu_object_is_dying(shadow->lo_header));
866 if (lu_object_is_dying(shadow->lo_header)) {
867 lu_object_put(env, shadow);
869 RETURN(ERR_PTR(-ENOENT));
875 EXPORT_SYMBOL(lu_object_find_at);
878 * Find object with given fid, and return its slice belonging to given device.
880 struct lu_object *lu_object_find_slice(const struct lu_env *env,
881 struct lu_device *dev,
882 const struct lu_fid *f,
883 const struct lu_object_conf *conf)
885 struct lu_object *top;
886 struct lu_object *obj;
888 top = lu_object_find(env, dev, f, conf);
892 obj = lu_object_locate(top->lo_header, dev->ld_type);
893 if (unlikely(obj == NULL)) {
894 lu_object_put(env, top);
895 obj = ERR_PTR(-ENOENT);
900 EXPORT_SYMBOL(lu_object_find_slice);
902 int lu_device_type_init(struct lu_device_type *ldt)
906 atomic_set(&ldt->ldt_device_nr, 0);
907 if (ldt->ldt_ops->ldto_init)
908 result = ldt->ldt_ops->ldto_init(ldt);
912 EXPORT_SYMBOL(lu_device_type_init);
914 void lu_device_type_fini(struct lu_device_type *ldt)
916 if (ldt->ldt_ops->ldto_fini)
917 ldt->ldt_ops->ldto_fini(ldt);
919 EXPORT_SYMBOL(lu_device_type_fini);
922 * Global list of all sites on this node
924 static LIST_HEAD(lu_sites);
925 static DECLARE_RWSEM(lu_sites_guard);
928 * Global environment used by site shrinker.
930 static struct lu_env lu_shrink_env;
932 struct lu_site_print_arg {
933 struct lu_env *lsp_env;
935 lu_printer_t lsp_printer;
939 lu_site_obj_print(struct cfs_hash *hs, struct cfs_hash_bd *bd,
940 struct hlist_node *hnode, void *data)
942 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
943 struct lu_object_header *h;
945 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
946 if (!list_empty(&h->loh_layers)) {
947 const struct lu_object *o;
949 o = lu_object_top(h);
950 lu_object_print(arg->lsp_env, arg->lsp_cookie,
951 arg->lsp_printer, o);
953 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
954 arg->lsp_printer, h);
960 * Print all objects in \a s.
962 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
963 lu_printer_t printer)
965 struct lu_site_print_arg arg = {
966 .lsp_env = (struct lu_env *)env,
967 .lsp_cookie = cookie,
968 .lsp_printer = printer,
971 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
973 EXPORT_SYMBOL(lu_site_print);
976 * Return desired hash table order.
978 static unsigned long lu_htable_order(struct lu_device *top)
980 unsigned long cache_size;
982 unsigned long bits_max = LU_SITE_BITS_MAX;
985 * For ZFS based OSDs the cache should be disabled by default. This
986 * allows the ZFS ARC maximum flexibility in determining what buffers
987 * to cache. If Lustre has objects or buffer which it wants to ensure
988 * always stay cached it must maintain a hold on them.
990 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
991 lu_cache_percent = 1;
992 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
993 return LU_SITE_BITS_MIN;
996 if (strcmp(top->ld_type->ldt_name, LUSTRE_VVP_NAME) == 0)
997 bits_max = LU_SITE_BITS_MAX_CL;
1000 * Calculate hash table size, assuming that we want reasonable
1001 * performance when 20% of total memory is occupied by cache of
1004 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
1006 cache_size = cfs_totalram_pages();
1008 #if BITS_PER_LONG == 32
1009 /* limit hashtable size for lowmem systems to low RAM */
1010 if (cache_size > 1 << (30 - PAGE_SHIFT))
1011 cache_size = 1 << (30 - PAGE_SHIFT) * 3 / 4;
1014 /* clear off unreasonable cache setting. */
1015 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
1016 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
1017 " the range of (0, %u]. Will use default value: %u.\n",
1018 lu_cache_percent, LU_CACHE_PERCENT_MAX,
1019 LU_CACHE_PERCENT_DEFAULT);
1021 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
1023 cache_size = cache_size / 100 * lu_cache_percent *
1026 for (bits = 1; (1 << bits) < cache_size; ++bits) {
1030 return clamp_t(typeof(bits), bits, LU_SITE_BITS_MIN, bits_max);
1033 static unsigned lu_obj_hop_hash(struct cfs_hash *hs,
1034 const void *key, unsigned mask)
1036 struct lu_fid *fid = (struct lu_fid *)key;
1039 hash = fid_flatten32(fid);
1040 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
1041 hash = hash_long(hash, hs->hs_bkt_bits);
1043 /* give me another random factor */
1044 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
1046 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
1047 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
1052 static void *lu_obj_hop_object(struct hlist_node *hnode)
1054 return hlist_entry(hnode, struct lu_object_header, loh_hash);
1057 static void *lu_obj_hop_key(struct hlist_node *hnode)
1059 struct lu_object_header *h;
1061 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1065 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
1067 struct lu_object_header *h;
1069 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1070 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1073 static void lu_obj_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1075 struct lu_object_header *h;
1077 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1078 atomic_inc(&h->loh_ref);
1081 static void lu_obj_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
1083 LBUG(); /* we should never called it */
1086 static struct cfs_hash_ops lu_site_hash_ops = {
1087 .hs_hash = lu_obj_hop_hash,
1088 .hs_key = lu_obj_hop_key,
1089 .hs_keycmp = lu_obj_hop_keycmp,
1090 .hs_object = lu_obj_hop_object,
1091 .hs_get = lu_obj_hop_get,
1092 .hs_put_locked = lu_obj_hop_put_locked,
1095 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1097 spin_lock(&s->ls_ld_lock);
1098 if (list_empty(&d->ld_linkage))
1099 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1100 spin_unlock(&s->ls_ld_lock);
1102 EXPORT_SYMBOL(lu_dev_add_linkage);
1104 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1106 spin_lock(&s->ls_ld_lock);
1107 list_del_init(&d->ld_linkage);
1108 spin_unlock(&s->ls_ld_lock);
1110 EXPORT_SYMBOL(lu_dev_del_linkage);
1113 * Initialize site \a s, with \a d as the top level device.
1115 int lu_site_init(struct lu_site *s, struct lu_device *top)
1117 struct lu_site_bkt_data *bkt;
1124 memset(s, 0, sizeof *s);
1125 mutex_init(&s->ls_purge_mutex);
1127 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
1128 rc = percpu_counter_init(&s->ls_lru_len_counter, 0, GFP_NOFS);
1130 rc = percpu_counter_init(&s->ls_lru_len_counter, 0);
1135 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1136 for (bits = lu_htable_order(top);
1137 bits >= LU_SITE_BITS_MIN; bits--) {
1138 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1139 bits - LU_SITE_BKT_BITS,
1142 CFS_HASH_SPIN_BKTLOCK |
1143 CFS_HASH_NO_ITEMREF |
1145 CFS_HASH_ASSERT_EMPTY |
1147 if (s->ls_obj_hash != NULL)
1151 if (s->ls_obj_hash == NULL) {
1152 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1156 s->ls_bkt_seed = prandom_u32();
1157 s->ls_bkt_cnt = max_t(long, 1 << LU_SITE_BKT_BITS,
1158 2 * num_possible_cpus());
1159 s->ls_bkt_cnt = roundup_pow_of_two(s->ls_bkt_cnt);
1160 OBD_ALLOC_LARGE(s->ls_bkts, s->ls_bkt_cnt * sizeof(*bkt));
1162 cfs_hash_putref(s->ls_obj_hash);
1163 s->ls_obj_hash = NULL;
1168 for (i = 0; i < s->ls_bkt_cnt; i++) {
1169 bkt = &s->ls_bkts[i];
1170 INIT_LIST_HEAD(&bkt->lsb_lru);
1171 init_waitqueue_head(&bkt->lsb_waitq);
1174 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1175 if (s->ls_stats == NULL) {
1176 OBD_FREE_LARGE(s->ls_bkts, s->ls_bkt_cnt * sizeof(*bkt));
1177 cfs_hash_putref(s->ls_obj_hash);
1178 s->ls_obj_hash = NULL;
1183 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1184 0, "created", "created");
1185 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1186 0, "cache_hit", "cache_hit");
1187 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1188 0, "cache_miss", "cache_miss");
1189 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1190 0, "cache_race", "cache_race");
1191 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1192 0, "cache_death_race", "cache_death_race");
1193 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1194 0, "lru_purged", "lru_purged");
1196 INIT_LIST_HEAD(&s->ls_linkage);
1197 s->ls_top_dev = top;
1200 lu_ref_add(&top->ld_reference, "site-top", s);
1202 INIT_LIST_HEAD(&s->ls_ld_linkage);
1203 spin_lock_init(&s->ls_ld_lock);
1205 lu_dev_add_linkage(s, top);
1209 EXPORT_SYMBOL(lu_site_init);
1212 * Finalize \a s and release its resources.
1214 void lu_site_fini(struct lu_site *s)
1216 down_write(&lu_sites_guard);
1217 list_del_init(&s->ls_linkage);
1218 up_write(&lu_sites_guard);
1220 percpu_counter_destroy(&s->ls_lru_len_counter);
1222 if (s->ls_obj_hash != NULL) {
1223 cfs_hash_putref(s->ls_obj_hash);
1224 s->ls_obj_hash = NULL;
1227 OBD_FREE_LARGE(s->ls_bkts, s->ls_bkt_cnt * sizeof(*s->ls_bkts));
1229 if (s->ls_top_dev != NULL) {
1230 s->ls_top_dev->ld_site = NULL;
1231 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1232 lu_device_put(s->ls_top_dev);
1233 s->ls_top_dev = NULL;
1236 if (s->ls_stats != NULL)
1237 lprocfs_free_stats(&s->ls_stats);
1239 EXPORT_SYMBOL(lu_site_fini);
1242 * Called when initialization of stack for this site is completed.
1244 int lu_site_init_finish(struct lu_site *s)
1247 down_write(&lu_sites_guard);
1248 result = lu_context_refill(&lu_shrink_env.le_ctx);
1250 list_add(&s->ls_linkage, &lu_sites);
1251 up_write(&lu_sites_guard);
1254 EXPORT_SYMBOL(lu_site_init_finish);
1257 * Acquire additional reference on device \a d
1259 void lu_device_get(struct lu_device *d)
1261 atomic_inc(&d->ld_ref);
1263 EXPORT_SYMBOL(lu_device_get);
1266 * Release reference on device \a d.
1268 void lu_device_put(struct lu_device *d)
1270 LASSERT(atomic_read(&d->ld_ref) > 0);
1271 atomic_dec(&d->ld_ref);
1273 EXPORT_SYMBOL(lu_device_put);
1276 * Initialize device \a d of type \a t.
1278 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1280 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1281 t->ldt_ops->ldto_start != NULL)
1282 t->ldt_ops->ldto_start(t);
1284 memset(d, 0, sizeof *d);
1286 lu_ref_init(&d->ld_reference);
1287 INIT_LIST_HEAD(&d->ld_linkage);
1291 EXPORT_SYMBOL(lu_device_init);
1294 * Finalize device \a d.
1296 void lu_device_fini(struct lu_device *d)
1298 struct lu_device_type *t = d->ld_type;
1300 if (d->ld_obd != NULL) {
1301 d->ld_obd->obd_lu_dev = NULL;
1305 lu_ref_fini(&d->ld_reference);
1306 LASSERTF(atomic_read(&d->ld_ref) == 0,
1307 "Refcount is %u\n", atomic_read(&d->ld_ref));
1308 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1310 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1311 t->ldt_ops->ldto_stop != NULL)
1312 t->ldt_ops->ldto_stop(t);
1314 EXPORT_SYMBOL(lu_device_fini);
1317 * Initialize object \a o that is part of compound object \a h and was created
1320 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1321 struct lu_device *d)
1323 memset(o, 0, sizeof(*o));
1327 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1328 INIT_LIST_HEAD(&o->lo_linkage);
1332 EXPORT_SYMBOL(lu_object_init);
1335 * Finalize object and release its resources.
1337 void lu_object_fini(struct lu_object *o)
1339 struct lu_device *dev = o->lo_dev;
1341 LASSERT(list_empty(&o->lo_linkage));
1344 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1350 EXPORT_SYMBOL(lu_object_fini);
1353 * Add object \a o as first layer of compound object \a h
1355 * This is typically called by the ->ldo_object_alloc() method of top-level
1358 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1360 list_move(&o->lo_linkage, &h->loh_layers);
1362 EXPORT_SYMBOL(lu_object_add_top);
1365 * Add object \a o as a layer of compound object, going after \a before.
1367 * This is typically called by the ->ldo_object_alloc() method of \a
1370 void lu_object_add(struct lu_object *before, struct lu_object *o)
1372 list_move(&o->lo_linkage, &before->lo_linkage);
1374 EXPORT_SYMBOL(lu_object_add);
1377 * Initialize compound object.
1379 int lu_object_header_init(struct lu_object_header *h)
1381 memset(h, 0, sizeof *h);
1382 atomic_set(&h->loh_ref, 1);
1383 INIT_HLIST_NODE(&h->loh_hash);
1384 INIT_LIST_HEAD(&h->loh_lru);
1385 INIT_LIST_HEAD(&h->loh_layers);
1386 lu_ref_init(&h->loh_reference);
1389 EXPORT_SYMBOL(lu_object_header_init);
1392 * Finalize compound object.
1394 void lu_object_header_fini(struct lu_object_header *h)
1396 LASSERT(list_empty(&h->loh_layers));
1397 LASSERT(list_empty(&h->loh_lru));
1398 LASSERT(hlist_unhashed(&h->loh_hash));
1399 lu_ref_fini(&h->loh_reference);
1401 EXPORT_SYMBOL(lu_object_header_fini);
1404 * Given a compound object, find its slice, corresponding to the device type
1407 struct lu_object *lu_object_locate(struct lu_object_header *h,
1408 const struct lu_device_type *dtype)
1410 struct lu_object *o;
1412 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1413 if (o->lo_dev->ld_type == dtype)
1418 EXPORT_SYMBOL(lu_object_locate);
1421 * Finalize and free devices in the device stack.
1423 * Finalize device stack by purging object cache, and calling
1424 * lu_device_type_operations::ldto_device_fini() and
1425 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1427 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1429 struct lu_site *site = top->ld_site;
1430 struct lu_device *scan;
1431 struct lu_device *next;
1433 lu_site_purge(env, site, ~0);
1434 for (scan = top; scan != NULL; scan = next) {
1435 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1436 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1437 lu_device_put(scan);
1441 lu_site_purge(env, site, ~0);
1443 for (scan = top; scan != NULL; scan = next) {
1444 const struct lu_device_type *ldt = scan->ld_type;
1446 next = ldt->ldt_ops->ldto_device_free(env, scan);
1452 * Maximal number of tld slots.
1454 LU_CONTEXT_KEY_NR = 40
1457 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1459 static DECLARE_RWSEM(lu_key_initing);
1462 * Global counter incremented whenever key is registered, unregistered,
1463 * revived or quiesced. This is used to void unnecessary calls to
1464 * lu_context_refill(). No locking is provided, as initialization and shutdown
1465 * are supposed to be externally serialized.
1467 static atomic_t key_set_version = ATOMIC_INIT(0);
1472 int lu_context_key_register(struct lu_context_key *key)
1477 LASSERT(key->lct_init != NULL);
1478 LASSERT(key->lct_fini != NULL);
1479 LASSERT(key->lct_tags != 0);
1480 LASSERT(key->lct_owner != NULL);
1483 atomic_set(&key->lct_used, 1);
1484 lu_ref_init(&key->lct_reference);
1485 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1489 if (cmpxchg(&lu_keys[i], NULL, key) != NULL)
1493 atomic_inc(&key_set_version);
1497 lu_ref_fini(&key->lct_reference);
1498 atomic_set(&key->lct_used, 0);
1502 EXPORT_SYMBOL(lu_context_key_register);
1504 static void key_fini(struct lu_context *ctx, int index)
1506 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1507 struct lu_context_key *key;
1509 key = lu_keys[index];
1510 LASSERT(key != NULL);
1511 LASSERT(key->lct_fini != NULL);
1512 LASSERT(atomic_read(&key->lct_used) > 0);
1514 key->lct_fini(ctx, key, ctx->lc_value[index]);
1515 lu_ref_del(&key->lct_reference, "ctx", ctx);
1516 if (atomic_dec_and_test(&key->lct_used))
1517 wake_up_var(&key->lct_used);
1519 LASSERT(key->lct_owner != NULL);
1520 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1521 LINVRNT(module_refcount(key->lct_owner) > 0);
1522 module_put(key->lct_owner);
1524 ctx->lc_value[index] = NULL;
1531 void lu_context_key_degister(struct lu_context_key *key)
1533 LASSERT(atomic_read(&key->lct_used) >= 1);
1534 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1536 lu_context_key_quiesce(key);
1538 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1541 * Wait until all transient contexts referencing this key have
1542 * run lu_context_key::lct_fini() method.
1544 atomic_dec(&key->lct_used);
1545 wait_var_event(&key->lct_used, atomic_read(&key->lct_used) == 0);
1547 if (!WARN_ON(lu_keys[key->lct_index] == NULL))
1548 lu_ref_fini(&key->lct_reference);
1550 smp_store_release(&lu_keys[key->lct_index], NULL);
1552 EXPORT_SYMBOL(lu_context_key_degister);
1555 * Register a number of keys. This has to be called after all keys have been
1556 * initialized by a call to LU_CONTEXT_KEY_INIT().
1558 int lu_context_key_register_many(struct lu_context_key *k, ...)
1560 struct lu_context_key *key = k;
1566 result = lu_context_key_register(key);
1569 key = va_arg(args, struct lu_context_key *);
1570 } while (key != NULL);
1576 lu_context_key_degister(k);
1577 k = va_arg(args, struct lu_context_key *);
1584 EXPORT_SYMBOL(lu_context_key_register_many);
1587 * De-register a number of keys. This is a dual to
1588 * lu_context_key_register_many().
1590 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1596 lu_context_key_degister(k);
1597 k = va_arg(args, struct lu_context_key*);
1598 } while (k != NULL);
1601 EXPORT_SYMBOL(lu_context_key_degister_many);
1604 * Revive a number of keys.
1606 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1612 lu_context_key_revive(k);
1613 k = va_arg(args, struct lu_context_key*);
1614 } while (k != NULL);
1617 EXPORT_SYMBOL(lu_context_key_revive_many);
1620 * Quiescent a number of keys.
1622 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1628 lu_context_key_quiesce(k);
1629 k = va_arg(args, struct lu_context_key*);
1630 } while (k != NULL);
1633 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1636 * Return value associated with key \a key in context \a ctx.
1638 void *lu_context_key_get(const struct lu_context *ctx,
1639 const struct lu_context_key *key)
1641 LINVRNT(ctx->lc_state == LCS_ENTERED);
1642 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1643 LASSERT(lu_keys[key->lct_index] == key);
1644 return ctx->lc_value[key->lct_index];
1646 EXPORT_SYMBOL(lu_context_key_get);
1649 * List of remembered contexts. XXX document me.
1651 static LIST_HEAD(lu_context_remembered);
1652 static DEFINE_SPINLOCK(lu_context_remembered_guard);
1655 * Destroy \a key in all remembered contexts. This is used to destroy key
1656 * values in "shared" contexts (like service threads), when a module owning
1657 * the key is about to be unloaded.
1659 void lu_context_key_quiesce(struct lu_context_key *key)
1661 struct lu_context *ctx;
1663 if (!(key->lct_tags & LCT_QUIESCENT)) {
1665 * The write-lock on lu_key_initing will ensure that any
1666 * keys_fill() which didn't see LCT_QUIESCENT will have
1667 * finished before we call key_fini().
1669 down_write(&lu_key_initing);
1670 key->lct_tags |= LCT_QUIESCENT;
1671 up_write(&lu_key_initing);
1673 spin_lock(&lu_context_remembered_guard);
1674 list_for_each_entry(ctx, &lu_context_remembered, lc_remember) {
1675 spin_until_cond(READ_ONCE(ctx->lc_state) != LCS_LEAVING);
1676 key_fini(ctx, key->lct_index);
1679 spin_unlock(&lu_context_remembered_guard);
1683 void lu_context_key_revive(struct lu_context_key *key)
1685 key->lct_tags &= ~LCT_QUIESCENT;
1686 atomic_inc(&key_set_version);
1689 static void keys_fini(struct lu_context *ctx)
1693 if (ctx->lc_value == NULL)
1696 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1699 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1700 ctx->lc_value = NULL;
1703 static int keys_fill(struct lu_context *ctx)
1709 * A serialisation with lu_context_key_quiesce() is needed, to
1710 * ensure we see LCT_QUIESCENT and don't allocate a new value
1711 * after it freed one. The rwsem provides this. As down_read()
1712 * does optimistic spinning while the writer is active, this is
1713 * unlikely to ever sleep.
1715 down_read(&lu_key_initing);
1716 ctx->lc_version = atomic_read(&key_set_version);
1718 LINVRNT(ctx->lc_value);
1719 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1720 struct lu_context_key *key;
1723 if (!ctx->lc_value[i] && key &&
1724 (key->lct_tags & ctx->lc_tags) &&
1726 * Don't create values for a LCT_QUIESCENT key, as this
1727 * will pin module owning a key.
1729 !(key->lct_tags & LCT_QUIESCENT)) {
1732 LINVRNT(key->lct_init != NULL);
1733 LINVRNT(key->lct_index == i);
1735 LASSERT(key->lct_owner != NULL);
1736 if (!(ctx->lc_tags & LCT_NOREF) &&
1737 try_module_get(key->lct_owner) == 0) {
1738 /* module is unloading, skip this key */
1742 value = key->lct_init(ctx, key);
1743 if (unlikely(IS_ERR(value))) {
1744 rc = PTR_ERR(value);
1748 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1749 atomic_inc(&key->lct_used);
1751 * This is the only place in the code, where an
1752 * element of ctx->lc_value[] array is set to non-NULL
1755 ctx->lc_value[i] = value;
1756 if (key->lct_exit != NULL)
1757 ctx->lc_tags |= LCT_HAS_EXIT;
1761 up_read(&lu_key_initing);
1765 static int keys_init(struct lu_context *ctx)
1767 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1768 if (likely(ctx->lc_value != NULL))
1769 return keys_fill(ctx);
1775 * Initialize context data-structure. Create values for all keys.
1777 int lu_context_init(struct lu_context *ctx, __u32 tags)
1781 memset(ctx, 0, sizeof *ctx);
1782 ctx->lc_state = LCS_INITIALIZED;
1783 ctx->lc_tags = tags;
1784 if (tags & LCT_REMEMBER) {
1785 spin_lock(&lu_context_remembered_guard);
1786 list_add(&ctx->lc_remember, &lu_context_remembered);
1787 spin_unlock(&lu_context_remembered_guard);
1789 INIT_LIST_HEAD(&ctx->lc_remember);
1792 rc = keys_init(ctx);
1794 lu_context_fini(ctx);
1798 EXPORT_SYMBOL(lu_context_init);
1801 * Finalize context data-structure. Destroy key values.
1803 void lu_context_fini(struct lu_context *ctx)
1805 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1806 ctx->lc_state = LCS_FINALIZED;
1808 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1809 LASSERT(list_empty(&ctx->lc_remember));
1811 /* could race with key degister */
1812 spin_lock(&lu_context_remembered_guard);
1813 list_del_init(&ctx->lc_remember);
1814 spin_unlock(&lu_context_remembered_guard);
1818 EXPORT_SYMBOL(lu_context_fini);
1821 * Called before entering context.
1823 void lu_context_enter(struct lu_context *ctx)
1825 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1826 ctx->lc_state = LCS_ENTERED;
1828 EXPORT_SYMBOL(lu_context_enter);
1831 * Called after exiting from \a ctx
1833 void lu_context_exit(struct lu_context *ctx)
1837 LINVRNT(ctx->lc_state == LCS_ENTERED);
1839 * Disable preempt to ensure we get a warning if
1840 * any lct_exit ever tries to sleep. That would hurt
1841 * lu_context_key_quiesce() which spins waiting for us.
1842 * This also ensure we aren't preempted while the state
1843 * is LCS_LEAVING, as that too would cause problems for
1844 * lu_context_key_quiesce().
1848 * Ensure lu_context_key_quiesce() sees LCS_LEAVING
1849 * or we see LCT_QUIESCENT
1851 smp_store_mb(ctx->lc_state, LCS_LEAVING);
1852 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value) {
1853 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1854 struct lu_context_key *key;
1857 if (ctx->lc_value[i] &&
1858 !(key->lct_tags & LCT_QUIESCENT) &&
1860 key->lct_exit(ctx, key, ctx->lc_value[i]);
1864 smp_store_release(&ctx->lc_state, LCS_LEFT);
1867 EXPORT_SYMBOL(lu_context_exit);
1870 * Allocate for context all missing keys that were registered after context
1871 * creation. key_set_version is only changed in rare cases when modules
1872 * are loaded and removed.
1874 int lu_context_refill(struct lu_context *ctx)
1876 if (likely(ctx->lc_version == atomic_read(&key_set_version)))
1879 return keys_fill(ctx);
1883 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1884 * obd being added. Currently, this is only used on client side, specifically
1885 * for echo device client, for other stack (like ptlrpc threads), context are
1886 * predefined when the lu_device type are registered, during the module probe
1889 u32 lu_context_tags_default = LCT_CL_THREAD;
1890 u32 lu_session_tags_default = LCT_SESSION;
1892 void lu_context_tags_update(__u32 tags)
1894 spin_lock(&lu_context_remembered_guard);
1895 lu_context_tags_default |= tags;
1896 atomic_inc(&key_set_version);
1897 spin_unlock(&lu_context_remembered_guard);
1899 EXPORT_SYMBOL(lu_context_tags_update);
1901 void lu_context_tags_clear(__u32 tags)
1903 spin_lock(&lu_context_remembered_guard);
1904 lu_context_tags_default &= ~tags;
1905 atomic_inc(&key_set_version);
1906 spin_unlock(&lu_context_remembered_guard);
1908 EXPORT_SYMBOL(lu_context_tags_clear);
1910 void lu_session_tags_update(__u32 tags)
1912 spin_lock(&lu_context_remembered_guard);
1913 lu_session_tags_default |= tags;
1914 atomic_inc(&key_set_version);
1915 spin_unlock(&lu_context_remembered_guard);
1917 EXPORT_SYMBOL(lu_session_tags_update);
1919 void lu_session_tags_clear(__u32 tags)
1921 spin_lock(&lu_context_remembered_guard);
1922 lu_session_tags_default &= ~tags;
1923 atomic_inc(&key_set_version);
1924 spin_unlock(&lu_context_remembered_guard);
1926 EXPORT_SYMBOL(lu_session_tags_clear);
1928 int lu_env_init(struct lu_env *env, __u32 tags)
1933 result = lu_context_init(&env->le_ctx, tags);
1934 if (likely(result == 0))
1935 lu_context_enter(&env->le_ctx);
1938 EXPORT_SYMBOL(lu_env_init);
1940 void lu_env_fini(struct lu_env *env)
1942 lu_context_exit(&env->le_ctx);
1943 lu_context_fini(&env->le_ctx);
1946 EXPORT_SYMBOL(lu_env_fini);
1948 int lu_env_refill(struct lu_env *env)
1952 result = lu_context_refill(&env->le_ctx);
1953 if (result == 0 && env->le_ses != NULL)
1954 result = lu_context_refill(env->le_ses);
1957 EXPORT_SYMBOL(lu_env_refill);
1960 * Currently, this API will only be used by echo client.
1961 * Because echo client and normal lustre client will share
1962 * same cl_env cache. So echo client needs to refresh
1963 * the env context after it get one from the cache, especially
1964 * when normal client and echo client co-exist in the same client.
1966 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1971 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1972 env->le_ctx.lc_version = 0;
1973 env->le_ctx.lc_tags |= ctags;
1976 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1977 env->le_ses->lc_version = 0;
1978 env->le_ses->lc_tags |= stags;
1981 result = lu_env_refill(env);
1985 EXPORT_SYMBOL(lu_env_refill_by_tags);
1988 struct lu_env_item {
1989 struct task_struct *lei_task; /* rhashtable key */
1990 struct rhash_head lei_linkage;
1991 struct lu_env *lei_env;
1992 struct rcu_head lei_rcu_head;
1995 static const struct rhashtable_params lu_env_rhash_params = {
1996 .key_len = sizeof(struct task_struct *),
1997 .key_offset = offsetof(struct lu_env_item, lei_task),
1998 .head_offset = offsetof(struct lu_env_item, lei_linkage),
2001 struct rhashtable lu_env_rhash;
2003 struct lu_env_percpu {
2004 struct task_struct *lep_task;
2005 struct lu_env *lep_env ____cacheline_aligned_in_smp;
2008 static struct lu_env_percpu lu_env_percpu[NR_CPUS];
2010 int lu_env_add_task(struct lu_env *env, struct task_struct *task)
2012 struct lu_env_item *lei, *old;
2020 lei->lei_task = task;
2023 old = rhashtable_lookup_get_insert_fast(&lu_env_rhash,
2025 lu_env_rhash_params);
2030 EXPORT_SYMBOL(lu_env_add_task);
2032 int lu_env_add(struct lu_env *env)
2034 return lu_env_add_task(env, current);
2036 EXPORT_SYMBOL(lu_env_add);
2038 static void lu_env_item_free(struct rcu_head *head)
2040 struct lu_env_item *lei;
2042 lei = container_of(head, struct lu_env_item, lei_rcu_head);
2046 void lu_env_remove(struct lu_env *env)
2048 struct lu_env_item *lei;
2049 const void *task = current;
2052 for_each_possible_cpu(i) {
2053 if (lu_env_percpu[i].lep_env == env) {
2054 LASSERT(lu_env_percpu[i].lep_task == task);
2055 lu_env_percpu[i].lep_task = NULL;
2056 lu_env_percpu[i].lep_env = NULL;
2060 /* The rcu_lock is not taking in this case since the key
2061 * used is the actual task_struct. This implies that each
2062 * object is only removed by the owning thread, so there
2063 * can never be a race on a particular object.
2065 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
2066 lu_env_rhash_params);
2067 if (lei && rhashtable_remove_fast(&lu_env_rhash, &lei->lei_linkage,
2068 lu_env_rhash_params) == 0)
2069 call_rcu(&lei->lei_rcu_head, lu_env_item_free);
2071 EXPORT_SYMBOL(lu_env_remove);
2073 struct lu_env *lu_env_find(void)
2075 struct lu_env *env = NULL;
2076 struct lu_env_item *lei;
2077 const void *task = current;
2080 if (lu_env_percpu[i].lep_task == current) {
2081 env = lu_env_percpu[i].lep_env;
2087 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
2088 lu_env_rhash_params);
2091 lu_env_percpu[i].lep_task = current;
2092 lu_env_percpu[i].lep_env = env;
2098 EXPORT_SYMBOL(lu_env_find);
2100 static struct shrinker *lu_site_shrinker;
2102 typedef struct lu_site_stats{
2103 unsigned lss_populated;
2104 unsigned lss_max_search;
2109 static void lu_site_stats_get(const struct lu_site *s,
2110 lu_site_stats_t *stats)
2112 int cnt = cfs_hash_size_get(s->ls_obj_hash);
2114 * percpu_counter_sum_positive() won't accept a const pointer
2115 * as it does modify the struct by taking a spinlock
2117 struct lu_site *s2 = (struct lu_site *)s;
2119 stats->lss_busy += cnt -
2120 percpu_counter_sum_positive(&s2->ls_lru_len_counter);
2122 stats->lss_total += cnt;
2123 stats->lss_max_search = 0;
2124 stats->lss_populated = 0;
2129 * lu_cache_shrink_count() returns an approximate number of cached objects
2130 * that can be freed by shrink_slab(). A counter, which tracks the
2131 * number of items in the site's lru, is maintained in a percpu_counter
2132 * for each site. The percpu values are incremented and decremented as
2133 * objects are added or removed from the lru. The percpu values are summed
2134 * and saved whenever a percpu value exceeds a threshold. Thus the saved,
2135 * summed value at any given time may not accurately reflect the current
2136 * lru length. But this value is sufficiently accurate for the needs of
2139 * Using a per cpu counter is a compromise solution to concurrent access:
2140 * lu_object_put() can update the counter without locking the site and
2141 * lu_cache_shrink_count can sum the counters without locking each
2142 * ls_obj_hash bucket.
2144 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
2145 struct shrink_control *sc)
2148 struct lu_site *tmp;
2149 unsigned long cached = 0;
2151 if (!(sc->gfp_mask & __GFP_FS))
2154 down_read(&lu_sites_guard);
2155 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage)
2156 cached += percpu_counter_read_positive(&s->ls_lru_len_counter);
2157 up_read(&lu_sites_guard);
2159 cached = (cached / 100) * sysctl_vfs_cache_pressure;
2160 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
2161 cached, sysctl_vfs_cache_pressure);
2166 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
2167 struct shrink_control *sc)
2170 struct lu_site *tmp;
2171 unsigned long remain = sc->nr_to_scan;
2174 if (!(sc->gfp_mask & __GFP_FS))
2175 /* We must not take the lu_sites_guard lock when
2176 * __GFP_FS is *not* set because of the deadlock
2177 * possibility detailed above. Additionally,
2178 * since we cannot determine the number of
2179 * objects in the cache without taking this
2180 * lock, we're in a particularly tough spot. As
2181 * a result, we'll just lie and say our cache is
2182 * empty. This _should_ be ok, as we can't
2183 * reclaim objects when __GFP_FS is *not* set
2188 down_write(&lu_sites_guard);
2189 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
2190 remain = lu_site_purge(&lu_shrink_env, s, remain);
2192 * Move just shrunk site to the tail of site list to
2193 * assure shrinking fairness.
2195 list_move_tail(&s->ls_linkage, &splice);
2197 list_splice(&splice, lu_sites.prev);
2198 up_write(&lu_sites_guard);
2200 return sc->nr_to_scan - remain;
2203 #ifndef HAVE_SHRINKER_COUNT
2205 * There exists a potential lock inversion deadlock scenario when using
2206 * Lustre on top of ZFS. This occurs between one of ZFS's
2207 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2208 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2209 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2210 * lock. Obviously neither thread will wake and drop their respective hold
2213 * To prevent this from happening we must ensure the lu_sites_guard lock is
2214 * not taken while down this code path. ZFS reliably does not set the
2215 * __GFP_FS bit in its code paths, so this can be used to determine if it
2216 * is safe to take the lu_sites_guard lock.
2218 * Ideally we should accurately return the remaining number of cached
2219 * objects without taking the lu_sites_guard lock, but this is not
2220 * possible in the current implementation.
2222 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2225 struct shrink_control scv = {
2226 .nr_to_scan = shrink_param(sc, nr_to_scan),
2227 .gfp_mask = shrink_param(sc, gfp_mask)
2230 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2232 if (scv.nr_to_scan != 0)
2233 lu_cache_shrink_scan(shrinker, &scv);
2235 cached = lu_cache_shrink_count(shrinker, &scv);
2239 #endif /* HAVE_SHRINKER_COUNT */
2247 * Environment to be used in debugger, contains all tags.
2249 static struct lu_env lu_debugging_env;
2252 * Debugging printer function using printk().
2254 int lu_printk_printer(const struct lu_env *env,
2255 void *unused, const char *format, ...)
2259 va_start(args, format);
2260 vprintk(format, args);
2265 int lu_debugging_setup(void)
2267 return lu_env_init(&lu_debugging_env, ~0);
2270 void lu_context_keys_dump(void)
2274 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2275 struct lu_context_key *key;
2279 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2280 i, key, key->lct_tags,
2281 key->lct_init, key->lct_fini, key->lct_exit,
2282 key->lct_index, atomic_read(&key->lct_used),
2283 key->lct_owner ? key->lct_owner->name : "",
2285 lu_ref_print(&key->lct_reference);
2291 * Initialization of global lu_* data.
2293 int lu_global_init(void)
2296 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2297 lu_cache_shrink_count, lu_cache_shrink_scan);
2299 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2301 result = lu_ref_global_init();
2305 LU_CONTEXT_KEY_INIT(&lu_global_key);
2306 result = lu_context_key_register(&lu_global_key);
2311 * At this level, we don't know what tags are needed, so allocate them
2312 * conservatively. This should not be too bad, because this
2313 * environment is global.
2315 down_write(&lu_sites_guard);
2316 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2317 up_write(&lu_sites_guard);
2322 * seeks estimation: 3 seeks to read a record from oi, one to read
2323 * inode, one for ea. Unfortunately setting this high value results in
2324 * lu_object/inode cache consuming all the memory.
2326 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2327 if (lu_site_shrinker == NULL)
2330 result = rhashtable_init(&lu_env_rhash, &lu_env_rhash_params);
2336 * Dual to lu_global_init().
2338 void lu_global_fini(void)
2340 if (lu_site_shrinker != NULL) {
2341 remove_shrinker(lu_site_shrinker);
2342 lu_site_shrinker = NULL;
2345 lu_context_key_degister(&lu_global_key);
2348 * Tear shrinker environment down _after_ de-registering
2349 * lu_global_key, because the latter has a value in the former.
2351 down_write(&lu_sites_guard);
2352 lu_env_fini(&lu_shrink_env);
2353 up_write(&lu_sites_guard);
2355 rhashtable_destroy(&lu_env_rhash);
2357 lu_ref_global_fini();
2360 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2362 #ifdef CONFIG_PROC_FS
2363 struct lprocfs_counter ret;
2365 lprocfs_stats_collect(stats, idx, &ret);
2366 return (__u32)ret.lc_count;
2373 * Output site statistical counters into a buffer. Suitable for
2374 * lprocfs_rd_*()-style functions.
2376 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2378 lu_site_stats_t stats;
2380 memset(&stats, 0, sizeof(stats));
2381 lu_site_stats_get(s, &stats);
2383 seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2386 stats.lss_populated,
2387 CFS_HASH_NHLIST(s->ls_obj_hash),
2388 stats.lss_max_search,
2389 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2390 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2391 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2392 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2393 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2394 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2397 EXPORT_SYMBOL(lu_site_stats_seq_print);
2400 * Helper function to initialize a number of kmem slab caches at once.
2402 int lu_kmem_init(struct lu_kmem_descr *caches)
2405 struct lu_kmem_descr *iter = caches;
2407 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2408 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2411 if (*iter->ckd_cache == NULL) {
2413 /* free all previously allocated caches */
2414 lu_kmem_fini(caches);
2420 EXPORT_SYMBOL(lu_kmem_init);
2423 * Helper function to finalize a number of kmem slab cached at once. Dual to
2426 void lu_kmem_fini(struct lu_kmem_descr *caches)
2428 for (; caches->ckd_cache != NULL; ++caches) {
2429 if (*caches->ckd_cache != NULL) {
2430 kmem_cache_destroy(*caches->ckd_cache);
2431 *caches->ckd_cache = NULL;
2435 EXPORT_SYMBOL(lu_kmem_fini);
2438 * Temporary solution to be able to assign fid in ->do_create()
2439 * till we have fully-functional OST fids
2441 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2442 const struct lu_fid *fid)
2444 struct lu_site *s = o->lo_dev->ld_site;
2445 struct lu_fid *old = &o->lo_header->loh_fid;
2446 struct cfs_hash *hs;
2447 struct cfs_hash_bd bd;
2449 LASSERT(fid_is_zero(old));
2451 /* supposed to be unique */
2452 hs = s->ls_obj_hash;
2453 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2454 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
2457 struct lu_object *shadow;
2459 shadow = htable_lookup(s, &bd, fid, &version);
2460 /* supposed to be unique */
2461 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2465 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2466 cfs_hash_bd_unlock(hs, &bd, 1);
2468 EXPORT_SYMBOL(lu_object_assign_fid);
2471 * allocates object with 0 (non-assiged) fid
2472 * XXX: temporary solution to be able to assign fid in ->do_create()
2473 * till we have fully-functional OST fids
2475 struct lu_object *lu_object_anon(const struct lu_env *env,
2476 struct lu_device *dev,
2477 const struct lu_object_conf *conf)
2480 struct lu_object *o;
2484 o = lu_object_alloc(env, dev, &fid);
2486 rc = lu_object_start(env, dev, o, conf);
2488 lu_object_free(env, o);
2495 EXPORT_SYMBOL(lu_object_anon);
2497 struct lu_buf LU_BUF_NULL = {
2501 EXPORT_SYMBOL(LU_BUF_NULL);
2503 void lu_buf_free(struct lu_buf *buf)
2507 LASSERT(buf->lb_len > 0);
2508 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2513 EXPORT_SYMBOL(lu_buf_free);
2515 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2518 LASSERT(buf->lb_buf == NULL);
2519 LASSERT(buf->lb_len == 0);
2520 OBD_ALLOC_LARGE(buf->lb_buf, size);
2521 if (likely(buf->lb_buf))
2524 EXPORT_SYMBOL(lu_buf_alloc);
2526 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2529 lu_buf_alloc(buf, size);
2531 EXPORT_SYMBOL(lu_buf_realloc);
2533 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2535 if (buf->lb_buf == NULL && buf->lb_len == 0)
2536 lu_buf_alloc(buf, len);
2538 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2539 lu_buf_realloc(buf, len);
2543 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2546 * Increase the size of the \a buf.
2547 * preserves old data in buffer
2548 * old buffer remains unchanged on error
2549 * \retval 0 or -ENOMEM
2551 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2555 if (len <= buf->lb_len)
2558 OBD_ALLOC_LARGE(ptr, len);
2562 /* Free the old buf */
2563 if (buf->lb_buf != NULL) {
2564 memcpy(ptr, buf->lb_buf, buf->lb_len);
2565 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2572 EXPORT_SYMBOL(lu_buf_check_and_grow);