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/
31 * lustre/obdclass/lu_object.c
34 * These are the only exported functions, they provide some generic
35 * infrastructure for managing object devices
37 * Author: Nikita Danilov <nikita.danilov@sun.com>
40 #define DEBUG_SUBSYSTEM S_CLASS
42 #include <linux/delay.h>
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/linux/linux-mem.h>
50 #include <libcfs/linux/linux-hash.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 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
88 #define LU_CACHE_NR_ZFS_LIMIT 10240
90 #define LU_CACHE_NR_MIN 4096
91 #define LU_CACHE_NR_MAX 0x80000000UL
94 * Max 256 buckets, we don't want too many buckets because:
95 * - consume too much memory (currently max 16K)
96 * - avoid unbalanced LRU list
97 * With few cpus there is little gain from extra buckets, so
98 * we treat this as a maximum in lu_site_init().
100 #define LU_SITE_BKT_BITS 8
102 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
103 module_param(lu_cache_percent, int, 0644);
104 MODULE_PARM_DESC(lu_cache_percent, "Percentage of memory to be used as lu_object cache");
106 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
107 module_param(lu_cache_nr, long, 0644);
108 MODULE_PARM_DESC(lu_cache_nr, "Maximum number of objects in lu_object cache");
110 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
111 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx);
113 static u32 lu_fid_hash(const void *data, u32 len, u32 seed)
115 const struct lu_fid *fid = data;
117 seed = cfs_hash_32(seed ^ fid->f_oid, 32);
118 seed ^= cfs_hash_64(fid->f_seq, 32);
122 static const struct rhashtable_params obj_hash_params = {
123 .key_len = sizeof(struct lu_fid),
124 .key_offset = offsetof(struct lu_object_header, loh_fid),
125 .head_offset = offsetof(struct lu_object_header, loh_hash),
126 .hashfn = lu_fid_hash,
127 .automatic_shrinking = true,
130 static inline int lu_bkt_hash(struct lu_site *s, const struct lu_fid *fid)
132 return lu_fid_hash(fid, sizeof(*fid), s->ls_bkt_seed) &
137 lu_site_wq_from_fid(struct lu_site *site, struct lu_fid *fid)
139 struct lu_site_bkt_data *bkt;
141 bkt = &site->ls_bkts[lu_bkt_hash(site, fid)];
142 return &bkt->lsb_waitq;
144 EXPORT_SYMBOL(lu_site_wq_from_fid);
147 * Decrease reference counter on object. If last reference is freed, return
148 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
149 * case, free object immediately.
151 void lu_object_put(const struct lu_env *env, struct lu_object *o)
153 struct lu_site_bkt_data *bkt;
154 struct lu_object_header *top = o->lo_header;
155 struct lu_site *site = o->lo_dev->ld_site;
156 struct lu_object *orig = o;
157 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(list_empty(&top->loh_lru));
166 if (!atomic_dec_and_test(&top->loh_ref))
168 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
169 if (o->lo_ops->loo_object_release != NULL)
170 o->lo_ops->loo_object_release(env, o);
172 lu_object_free(env, orig);
176 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
177 if (atomic_add_unless(&top->loh_ref, -1, 1)) {
180 * At this point the object reference is dropped and lock is
181 * not taken, so lu_object should not be touched because it
182 * can be freed by concurrent thread.
184 * Somebody may be waiting for this, currently only used for
185 * cl_object, see cl_object_put_last().
187 wake_up(&bkt->lsb_waitq);
192 spin_lock(&bkt->lsb_waitq.lock);
193 if (!atomic_dec_and_test(&top->loh_ref)) {
194 spin_unlock(&bkt->lsb_waitq.lock);
199 * Refcount is zero, and cannot be incremented without taking the bkt
200 * lock, so object is stable.
204 * When last reference is released, iterate over object layers, and
205 * notify them that object is no longer busy.
207 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
208 if (o->lo_ops->loo_object_release != NULL)
209 o->lo_ops->loo_object_release(env, o);
213 * Don't use local 'is_dying' here because if was taken without lock but
214 * here we need the latest actual value of it so check lu_object
217 if (!lu_object_is_dying(top) &&
218 (lu_object_exists(orig) || lu_object_is_cl(orig))) {
219 LASSERT(list_empty(&top->loh_lru));
220 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
221 spin_unlock(&bkt->lsb_waitq.lock);
222 percpu_counter_inc(&site->ls_lru_len_counter);
223 CDEBUG(D_INODE, "Add %p/%p to site lru. bkt: %p\n",
229 * If object is dying (will not be cached) then remove it from hash
230 * table (it is already not on the LRU).
232 * This is done with bucket lock held. As the only way to acquire first
233 * reference to previously unreferenced object is through hash-table
234 * lookup (lu_object_find()) which takes the lock for first reference,
235 * no race with concurrent object lookup is possible and we can safely
236 * destroy object below.
238 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
239 rhashtable_remove_fast(&site->ls_obj_hash, &top->loh_hash,
242 spin_unlock(&bkt->lsb_waitq.lock);
243 /* Object was already removed from hash above, can kill it. */
244 lu_object_free(env, orig);
246 EXPORT_SYMBOL(lu_object_put);
249 * Put object and don't keep in cache. This is temporary solution for
250 * multi-site objects when its layering is not constant.
252 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
254 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
255 return lu_object_put(env, o);
257 EXPORT_SYMBOL(lu_object_put_nocache);
260 * Kill the object and take it out of LRU cache.
261 * Currently used by client code for layout change.
263 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
265 struct lu_object_header *top;
268 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
269 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
270 struct lu_site *site = o->lo_dev->ld_site;
271 struct rhashtable *obj_hash = &site->ls_obj_hash;
272 struct lu_site_bkt_data *bkt;
274 bkt = &site->ls_bkts[lu_bkt_hash(site, &top->loh_fid)];
275 spin_lock(&bkt->lsb_waitq.lock);
276 if (!list_empty(&top->loh_lru)) {
277 list_del_init(&top->loh_lru);
278 percpu_counter_dec(&site->ls_lru_len_counter);
280 spin_unlock(&bkt->lsb_waitq.lock);
282 rhashtable_remove_fast(obj_hash, &top->loh_hash,
286 EXPORT_SYMBOL(lu_object_unhash);
289 * Allocate new object.
291 * This follows object creation protocol, described in the comment within
292 * struct lu_device_operations definition.
294 static struct lu_object *lu_object_alloc(const struct lu_env *env,
295 struct lu_device *dev,
296 const struct lu_fid *f)
298 struct lu_object *top;
301 * Create top-level object slice. This will also create
304 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
306 return ERR_PTR(-ENOMEM);
310 * This is the only place where object fid is assigned. It's constant
313 top->lo_header->loh_fid = *f;
321 * This is called after object hash insertion to avoid returning an object with
324 static int lu_object_start(const struct lu_env *env, struct lu_device *dev,
325 struct lu_object *top,
326 const struct lu_object_conf *conf)
328 struct lu_object *scan;
329 struct list_head *layers;
330 unsigned int init_mask = 0;
331 unsigned int init_flag;
335 layers = &top->lo_header->loh_layers;
339 * Call ->loo_object_init() repeatedly, until no more new
340 * object slices are created.
344 list_for_each_entry(scan, layers, lo_linkage) {
345 if (init_mask & init_flag)
348 scan->lo_header = top->lo_header;
349 result = scan->lo_ops->loo_object_init(env, scan, conf);
353 init_mask |= init_flag;
359 list_for_each_entry_reverse(scan, layers, lo_linkage) {
360 if (scan->lo_ops->loo_object_start != NULL) {
361 result = scan->lo_ops->loo_object_start(env, scan);
367 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
369 set_bit(LU_OBJECT_INITED, &top->lo_header->loh_flags);
377 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
379 wait_queue_head_t *wq;
380 struct lu_site *site;
381 struct lu_object *scan;
382 struct list_head *layers;
385 site = o->lo_dev->ld_site;
386 layers = &o->lo_header->loh_layers;
387 wq = lu_site_wq_from_fid(site, &o->lo_header->loh_fid);
389 * First call ->loo_object_delete() method to release all resources.
391 list_for_each_entry_reverse(scan, layers, lo_linkage) {
392 if (scan->lo_ops->loo_object_delete != NULL)
393 scan->lo_ops->loo_object_delete(env, scan);
397 * Then, splice object layers into stand-alone list, and call
398 * ->loo_object_free() on all layers to free memory. Splice is
399 * necessary, because lu_object_header is freed together with the
402 list_splice_init(layers, &splice);
403 while (!list_empty(&splice)) {
405 * Free layers in bottom-to-top order, so that object header
406 * lives as long as possible and ->loo_object_free() methods
407 * can look at its contents.
409 o = container_of(splice.prev, struct lu_object, lo_linkage);
410 list_del_init(&o->lo_linkage);
411 LASSERT(o->lo_ops->loo_object_free != NULL);
412 o->lo_ops->loo_object_free(env, o);
415 if (waitqueue_active(wq))
420 * Free \a nr objects from the cold end of the site LRU list.
421 * if canblock is 0, then don't block awaiting for another
422 * instance of lu_site_purge() to complete
424 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s,
425 int nr, int canblock)
427 struct lu_object_header *h;
428 struct lu_object_header *temp;
429 struct lu_site_bkt_data *bkt;
432 unsigned int start = 0;
437 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
441 * Under LRU list lock, scan LRU list and move unreferenced objects to
442 * the dispose list, removing them from LRU and hash table.
445 start = s->ls_purge_start;
446 bnr = (nr == ~0) ? -1 : nr / s->ls_bkt_cnt + 1;
449 * It doesn't make any sense to make purge threads parallel, that can
450 * only bring troubles to us. See LU-5331.
453 mutex_lock(&s->ls_purge_mutex);
454 else if (mutex_trylock(&s->ls_purge_mutex) == 0)
458 for (i = start; i < s->ls_bkt_cnt ; i++) {
460 bkt = &s->ls_bkts[i];
461 spin_lock(&bkt->lsb_waitq.lock);
463 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
464 LASSERT(atomic_read(&h->loh_ref) == 0);
466 LINVRNT(lu_bkt_hash(s, &h->loh_fid) == i);
468 set_bit(LU_OBJECT_UNHASHED, &h->loh_flags);
469 rhashtable_remove_fast(&s->ls_obj_hash, &h->loh_hash,
471 list_move(&h->loh_lru, &dispose);
472 percpu_counter_dec(&s->ls_lru_len_counter);
476 if (nr != ~0 && --nr == 0)
479 if (count > 0 && --count == 0)
483 spin_unlock(&bkt->lsb_waitq.lock);
486 * Free everything on the dispose list. This is safe against
487 * races due to the reasons described in lu_object_put().
489 while ((h = list_first_entry_or_null(&dispose,
490 struct lu_object_header,
492 list_del_init(&h->loh_lru);
493 lu_object_free(env, lu_object_top(h));
494 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
500 mutex_unlock(&s->ls_purge_mutex);
502 if (nr != 0 && did_sth && start != 0) {
503 start = 0; /* restart from the first bucket */
506 /* race on s->ls_purge_start, but nobody cares */
507 s->ls_purge_start = i & (s->ls_bkt_cnt - 1);
511 EXPORT_SYMBOL(lu_site_purge_objects);
516 * Code below has to jump through certain loops to output object description
517 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
518 * composes object description from strings that are parts of _lines_ of
519 * output (i.e., strings that are not terminated by newline). This doesn't fit
520 * very well into libcfs_debug_msg() interface that assumes that each message
521 * supplied to it is a self-contained output line.
523 * To work around this, strings are collected in a temporary buffer
524 * (implemented as a value of lu_cdebug_key key), until terminating newline
525 * character is detected.
533 * XXX overflow is not handled correctly.
538 struct lu_cdebug_data {
542 char lck_area[LU_CDEBUG_LINE];
545 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
546 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
549 * Key, holding temporary buffer. This key is registered very early by
552 static struct lu_context_key lu_global_key = {
553 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
554 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
555 .lct_init = lu_global_key_init,
556 .lct_fini = lu_global_key_fini
560 * Printer function emitting messages through libcfs_debug_msg().
562 int lu_cdebug_printer(const struct lu_env *env,
563 void *cookie, const char *format, ...)
565 struct libcfs_debug_msg_data *msgdata = cookie;
566 struct lu_cdebug_data *key;
571 va_start(args, format);
573 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
574 LASSERT(key != NULL);
576 used = strlen(key->lck_area);
577 complete = format[strlen(format) - 1] == '\n';
579 * Append new chunk to the buffer.
581 vsnprintf(key->lck_area + used,
582 ARRAY_SIZE(key->lck_area) - used, format, args);
584 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
585 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
586 key->lck_area[0] = 0;
591 EXPORT_SYMBOL(lu_cdebug_printer);
594 * Print object header.
596 void lu_object_header_print(const struct lu_env *env, void *cookie,
597 lu_printer_t printer,
598 const struct lu_object_header *hdr)
600 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
601 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
603 test_bit(LU_OBJECT_UNHASHED,
604 &hdr->loh_flags) ? "" : " hash",
605 list_empty(&hdr->loh_lru) ? "" : " lru",
606 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
608 EXPORT_SYMBOL(lu_object_header_print);
611 * Print human readable representation of the \a o to the \a printer.
613 void lu_object_print(const struct lu_env *env, void *cookie,
614 lu_printer_t printer, const struct lu_object *o)
616 static const char ruler[] = "........................................";
617 struct lu_object_header *top;
621 lu_object_header_print(env, cookie, printer, top);
622 (*printer)(env, cookie, "{\n");
624 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
626 * print `.' \a depth times followed by type name and address
628 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
629 o->lo_dev->ld_type->ldt_name, o);
631 if (o->lo_ops->loo_object_print != NULL)
632 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
634 (*printer)(env, cookie, "\n");
637 (*printer)(env, cookie, "} header@%p\n", top);
639 EXPORT_SYMBOL(lu_object_print);
642 * Check object consistency.
644 int lu_object_invariant(const struct lu_object *o)
646 struct lu_object_header *top;
649 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
650 if (o->lo_ops->loo_object_invariant != NULL &&
651 !o->lo_ops->loo_object_invariant(o))
658 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because the
659 * calculation for the number of objects to reclaim is not covered by a lock the
660 * maximum number of objects is capped by LU_CACHE_MAX_ADJUST. This ensures
661 * that many concurrent threads will not accidentally purge the entire cache.
663 static void lu_object_limit(const struct lu_env *env,
664 struct lu_device *dev)
668 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
671 size = atomic_read(&dev->ld_site->ls_obj_hash.nelems);
672 nr = (u64)lu_cache_nr;
676 lu_site_purge_objects(env, dev->ld_site,
677 min_t(u64, size - nr, LU_CACHE_NR_MAX_ADJUST),
681 static struct lu_object *htable_lookup(const struct lu_env *env,
682 struct lu_device *dev,
683 struct lu_site_bkt_data *bkt,
684 const struct lu_fid *f,
685 struct lu_object_header *new)
687 struct lu_site *s = dev->ld_site;
688 struct lu_object_header *h;
693 h = rhashtable_lookup_get_insert_fast(&s->ls_obj_hash,
697 h = rhashtable_lookup(&s->ls_obj_hash, f, obj_hash_params);
699 if (IS_ERR_OR_NULL(h)) {
702 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
704 if (PTR_ERR(h) == -ENOMEM) {
708 lu_object_limit(env, dev);
709 if (PTR_ERR(h) == -E2BIG)
712 return ERR_PTR(-ENOENT);
715 if (atomic_inc_not_zero(&h->loh_ref)) {
717 return lu_object_top(h);
720 spin_lock(&bkt->lsb_waitq.lock);
721 if (lu_object_is_dying(h) ||
722 test_bit(LU_OBJECT_UNHASHED, &h->loh_flags)) {
723 spin_unlock(&bkt->lsb_waitq.lock);
727 * Old object might have already been removed, or will
728 * be soon. We need to insert our new object, so
729 * remove the old one just in case it is still there.
731 rhashtable_remove_fast(&s->ls_obj_hash, &h->loh_hash,
735 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
736 return ERR_PTR(-ENOENT);
738 /* Now protected by spinlock */
741 if (!list_empty(&h->loh_lru)) {
742 list_del_init(&h->loh_lru);
743 percpu_counter_dec(&s->ls_lru_len_counter);
745 atomic_inc(&h->loh_ref);
746 spin_unlock(&bkt->lsb_waitq.lock);
747 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
748 return lu_object_top(h);
752 * Search cache for an object with the fid \a f. If such object is found,
753 * return it. Otherwise, create new object, insert it into cache and return
754 * it. In any case, additional reference is acquired on the returned object.
756 struct lu_object *lu_object_find(const struct lu_env *env,
757 struct lu_device *dev, const struct lu_fid *f,
758 const struct lu_object_conf *conf)
760 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
762 EXPORT_SYMBOL(lu_object_find);
765 * Get a 'first' reference to an object that was found while looking through the
768 struct lu_object *lu_object_get_first(struct lu_object_header *h,
769 struct lu_device *dev)
771 struct lu_site *s = dev->ld_site;
772 struct lu_object *ret;
774 if (IS_ERR_OR_NULL(h) || lu_object_is_dying(h))
777 ret = lu_object_locate(h, dev->ld_type);
781 if (!atomic_inc_not_zero(&h->loh_ref)) {
782 struct lu_site_bkt_data *bkt;
784 bkt = &s->ls_bkts[lu_bkt_hash(s, &h->loh_fid)];
785 spin_lock(&bkt->lsb_waitq.lock);
786 if (!lu_object_is_dying(h) &&
787 !test_bit(LU_OBJECT_UNHASHED, &h->loh_flags))
788 atomic_inc(&h->loh_ref);
791 spin_unlock(&bkt->lsb_waitq.lock);
795 EXPORT_SYMBOL(lu_object_get_first);
798 * Core logic of lu_object_find*() functions.
800 * Much like lu_object_find(), but top level device of object is specifically
801 * \a dev rather than top level device of the site. This interface allows
802 * objects of different "stacking" to be created within the same site.
804 struct lu_object *lu_object_find_at(const struct lu_env *env,
805 struct lu_device *dev,
806 const struct lu_fid *f,
807 const struct lu_object_conf *conf)
810 struct lu_object *shadow;
812 struct lu_site_bkt_data *bkt;
813 struct rhashtable *hs;
818 /* FID is from disk or network, zero FID is meaningless, return error
819 * early to avoid assertion in lu_object_put. If a zero FID is wanted,
820 * it should be allocated via lu_object_anon().
823 RETURN(ERR_PTR(-EINVAL));
826 * This uses standard index maintenance protocol:
828 * - search index under lock, and return object if found;
829 * - otherwise, unlock index, allocate new object;
830 * - lock index and search again;
831 * - if nothing is found (usual case), insert newly created
833 * - otherwise (race: other thread inserted object), free
834 * object just allocated.
838 * For "LOC_F_NEW" case, we are sure the object is new established.
839 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
840 * just alloc and insert directly.
844 hs = &s->ls_obj_hash;
846 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_OBD_ZERO_NLINK_RACE)))
847 lu_site_purge(env, s, -1);
849 bkt = &s->ls_bkts[lu_bkt_hash(s, f)];
850 if (!(conf && conf->loc_flags & LOC_F_NEW)) {
851 o = htable_lookup(env, dev, bkt, f, NULL);
854 if (likely(lu_object_is_inited(o->lo_header)))
857 wait_event_idle(bkt->lsb_waitq,
858 lu_object_is_inited(o->lo_header) ||
859 lu_object_is_dying(o->lo_header));
861 if (lu_object_is_dying(o->lo_header)) {
862 lu_object_put(env, o);
864 RETURN(ERR_PTR(-ENOENT));
870 if (PTR_ERR(o) != -ENOENT)
875 * Allocate new object, NB, object is unitialized in case object
876 * is changed between allocation and hash insertion, thus the object
877 * with stale attributes is returned.
879 o = lu_object_alloc(env, dev, f);
883 LASSERT(lu_fid_eq(lu_object_fid(o), f));
885 CFS_RACE_WAIT(OBD_FAIL_OBD_ZERO_NLINK_RACE);
887 if (conf && conf->loc_flags & LOC_F_NEW) {
888 int status = rhashtable_insert_fast(hs, &o->lo_header->loh_hash,
891 /* Strange error - go the slow way */
892 shadow = htable_lookup(env, dev, bkt, f, o->lo_header);
894 shadow = ERR_PTR(-ENOENT);
896 shadow = htable_lookup(env, dev, bkt, f, o->lo_header);
898 if (likely(PTR_ERR(shadow) == -ENOENT)) {
900 * The new object has been successfully inserted.
902 * This may result in rather complicated operations, including
903 * fld queries, inode loading, etc.
905 rc = lu_object_start(env, dev, o, conf);
907 lu_object_put_nocache(env, o);
911 wake_up(&bkt->lsb_waitq);
913 lu_object_limit(env, dev);
918 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
919 lu_object_free(env, o);
921 if (!(conf && conf->loc_flags & LOC_F_NEW) &&
923 !lu_object_is_inited(shadow->lo_header)) {
924 wait_event_idle(bkt->lsb_waitq,
925 lu_object_is_inited(shadow->lo_header) ||
926 lu_object_is_dying(shadow->lo_header));
928 if (lu_object_is_dying(shadow->lo_header)) {
929 lu_object_put(env, shadow);
931 RETURN(ERR_PTR(-ENOENT));
937 EXPORT_SYMBOL(lu_object_find_at);
940 * Find object with given fid, and return its slice belonging to given device.
942 struct lu_object *lu_object_find_slice(const struct lu_env *env,
943 struct lu_device *dev,
944 const struct lu_fid *f,
945 const struct lu_object_conf *conf)
947 struct lu_object *top;
948 struct lu_object *obj;
950 top = lu_object_find(env, dev, f, conf);
954 obj = lu_object_locate(top->lo_header, dev->ld_type);
955 if (unlikely(obj == NULL)) {
956 lu_object_put(env, top);
957 obj = ERR_PTR(-ENOENT);
962 EXPORT_SYMBOL(lu_object_find_slice);
964 int lu_device_type_init(struct lu_device_type *ldt)
968 atomic_set(&ldt->ldt_device_nr, 0);
969 if (ldt->ldt_ops->ldto_init)
970 result = ldt->ldt_ops->ldto_init(ldt);
974 EXPORT_SYMBOL(lu_device_type_init);
976 void lu_device_type_fini(struct lu_device_type *ldt)
978 if (ldt->ldt_ops->ldto_fini)
979 ldt->ldt_ops->ldto_fini(ldt);
981 EXPORT_SYMBOL(lu_device_type_fini);
984 * Global list of all sites on this node
986 static LIST_HEAD(lu_sites);
987 static DECLARE_RWSEM(lu_sites_guard);
990 * Global environment used by site shrinker.
992 static struct lu_env lu_shrink_env;
994 struct lu_site_print_arg {
995 struct lu_env *lsp_env;
997 lu_printer_t lsp_printer;
1001 lu_site_obj_print(struct lu_object_header *h, struct lu_site_print_arg *arg)
1003 if (!list_empty(&h->loh_layers)) {
1004 const struct lu_object *o;
1006 o = lu_object_top(h);
1007 lu_object_print(arg->lsp_env, arg->lsp_cookie,
1008 arg->lsp_printer, o);
1010 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
1011 arg->lsp_printer, h);
1016 * Print all objects in \a s.
1018 void lu_site_print(const struct lu_env *env, struct lu_site *s, atomic_t *ref,
1019 int msg_flag, lu_printer_t printer)
1021 struct lu_site_print_arg arg = {
1022 .lsp_env = (struct lu_env *)env,
1023 .lsp_printer = printer,
1025 struct rhashtable_iter iter;
1026 struct lu_object_header *h;
1027 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, msg_flag, NULL);
1029 if (!s || !atomic_read(ref))
1032 arg.lsp_cookie = (void *)&msgdata;
1034 rhashtable_walk_enter(&s->ls_obj_hash, &iter);
1035 rhashtable_walk_start(&iter);
1036 while ((h = rhashtable_walk_next(&iter)) != NULL) {
1039 lu_site_obj_print(h, &arg);
1041 rhashtable_walk_stop(&iter);
1042 rhashtable_walk_exit(&iter);
1044 EXPORT_SYMBOL(lu_site_print);
1047 * Return desired hash table order.
1049 static void lu_htable_limits(struct lu_device *top)
1051 unsigned long cache_size;
1054 * For ZFS based OSDs the cache should be disabled by default. This
1055 * allows the ZFS ARC maximum flexibility in determining what buffers
1056 * to cache. If Lustre has objects or buffer which it wants to ensure
1057 * always stay cached it must maintain a hold on them.
1059 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
1060 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
1065 * Calculate hash table size, assuming that we want reasonable
1066 * performance when 20% of total memory is occupied by cache of
1069 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
1071 cache_size = cfs_totalram_pages();
1073 #if BITS_PER_LONG == 32
1074 /* limit hashtable size for lowmem systems to low RAM */
1075 if (cache_size > 1 << (30 - PAGE_SHIFT))
1076 cache_size = 1 << (30 - PAGE_SHIFT) * 3 / 4;
1079 /* clear off unreasonable cache setting. */
1080 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
1081 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in the range of (0, %u]. Will use default value: %u.\n",
1082 lu_cache_percent, LU_CACHE_PERCENT_MAX,
1083 LU_CACHE_PERCENT_DEFAULT);
1085 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
1087 cache_size = cache_size / 100 * lu_cache_percent *
1090 lu_cache_nr = clamp_t(typeof(cache_size), cache_size,
1091 LU_CACHE_NR_MIN, LU_CACHE_NR_MAX);
1094 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1096 spin_lock(&s->ls_ld_lock);
1097 if (list_empty(&d->ld_linkage))
1098 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1099 spin_unlock(&s->ls_ld_lock);
1101 EXPORT_SYMBOL(lu_dev_add_linkage);
1103 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1105 spin_lock(&s->ls_ld_lock);
1106 list_del_init(&d->ld_linkage);
1107 spin_unlock(&s->ls_ld_lock);
1109 EXPORT_SYMBOL(lu_dev_del_linkage);
1112 * Initialize site \a s, with \a d as the top level device.
1114 int lu_site_init(struct lu_site *s, struct lu_device *top)
1116 struct lu_site_bkt_data *bkt;
1121 memset(s, 0, sizeof *s);
1122 mutex_init(&s->ls_purge_mutex);
1123 lu_htable_limits(top);
1125 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
1126 rc = percpu_counter_init(&s->ls_lru_len_counter, 0, GFP_NOFS);
1128 rc = percpu_counter_init(&s->ls_lru_len_counter, 0);
1133 if (rhashtable_init(&s->ls_obj_hash, &obj_hash_params) != 0) {
1134 CERROR("failed to create lu_site hash\n");
1138 s->ls_bkt_seed = prandom_u32();
1139 s->ls_bkt_cnt = max_t(long, 1 << LU_SITE_BKT_BITS,
1140 2 * num_possible_cpus());
1141 s->ls_bkt_cnt = roundup_pow_of_two(s->ls_bkt_cnt);
1142 OBD_ALLOC_PTR_ARRAY_LARGE(s->ls_bkts, s->ls_bkt_cnt);
1144 rhashtable_destroy(&s->ls_obj_hash);
1149 for (i = 0; i < s->ls_bkt_cnt; i++) {
1150 bkt = &s->ls_bkts[i];
1151 INIT_LIST_HEAD(&bkt->lsb_lru);
1152 init_waitqueue_head(&bkt->lsb_waitq);
1155 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1156 if (s->ls_stats == NULL) {
1157 OBD_FREE_PTR_ARRAY_LARGE(s->ls_bkts, s->ls_bkt_cnt);
1159 rhashtable_destroy(&s->ls_obj_hash);
1163 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1164 0, "created", "created");
1165 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1166 0, "cache_hit", "cache_hit");
1167 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1168 0, "cache_miss", "cache_miss");
1169 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1170 0, "cache_race", "cache_race");
1171 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1172 0, "cache_death_race", "cache_death_race");
1173 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1174 0, "lru_purged", "lru_purged");
1176 INIT_LIST_HEAD(&s->ls_linkage);
1177 s->ls_top_dev = top;
1180 lu_ref_add(&top->ld_reference, "site-top", s);
1182 INIT_LIST_HEAD(&s->ls_ld_linkage);
1183 spin_lock_init(&s->ls_ld_lock);
1185 lu_dev_add_linkage(s, top);
1189 EXPORT_SYMBOL(lu_site_init);
1192 * Finalize \a s and release its resources.
1194 void lu_site_fini(struct lu_site *s)
1196 down_write(&lu_sites_guard);
1197 list_del_init(&s->ls_linkage);
1198 up_write(&lu_sites_guard);
1200 percpu_counter_destroy(&s->ls_lru_len_counter);
1203 rhashtable_destroy(&s->ls_obj_hash);
1204 OBD_FREE_PTR_ARRAY_LARGE(s->ls_bkts, s->ls_bkt_cnt);
1208 if (s->ls_top_dev != NULL) {
1209 s->ls_top_dev->ld_site = NULL;
1210 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1211 lu_device_put(s->ls_top_dev);
1212 s->ls_top_dev = NULL;
1215 if (s->ls_stats != NULL)
1216 lprocfs_free_stats(&s->ls_stats);
1218 EXPORT_SYMBOL(lu_site_fini);
1221 * Called when initialization of stack for this site is completed.
1223 int lu_site_init_finish(struct lu_site *s)
1226 down_write(&lu_sites_guard);
1227 result = lu_context_refill(&lu_shrink_env.le_ctx);
1229 list_add(&s->ls_linkage, &lu_sites);
1230 up_write(&lu_sites_guard);
1233 EXPORT_SYMBOL(lu_site_init_finish);
1236 * Acquire additional reference on device \a d
1238 void lu_device_get(struct lu_device *d)
1240 atomic_inc(&d->ld_ref);
1242 EXPORT_SYMBOL(lu_device_get);
1245 * Release reference on device \a d.
1247 void lu_device_put(struct lu_device *d)
1249 LASSERT(atomic_read(&d->ld_ref) > 0);
1250 atomic_dec(&d->ld_ref);
1252 EXPORT_SYMBOL(lu_device_put);
1254 enum { /* Maximal number of tld slots. */
1255 LU_CONTEXT_KEY_NR = 40
1257 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1258 static DECLARE_RWSEM(lu_key_initing);
1261 * Initialize device \a d of type \a t.
1263 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1265 if (atomic_add_unless(&t->ldt_device_nr, 1, 0) == 0) {
1266 down_write(&lu_key_initing);
1267 if (t->ldt_ops->ldto_start &&
1268 atomic_read(&t->ldt_device_nr) == 0)
1269 t->ldt_ops->ldto_start(t);
1270 atomic_inc(&t->ldt_device_nr);
1271 up_write(&lu_key_initing);
1274 memset(d, 0, sizeof *d);
1276 lu_ref_init(&d->ld_reference);
1277 INIT_LIST_HEAD(&d->ld_linkage);
1281 EXPORT_SYMBOL(lu_device_init);
1284 * Finalize device \a d.
1286 void lu_device_fini(struct lu_device *d)
1288 struct lu_device_type *t = d->ld_type;
1290 if (d->ld_obd != NULL) {
1291 d->ld_obd->obd_lu_dev = NULL;
1295 lu_ref_fini(&d->ld_reference);
1296 LASSERTF(atomic_read(&d->ld_ref) == 0,
1297 "Refcount is %u\n", atomic_read(&d->ld_ref));
1298 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1300 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1301 t->ldt_ops->ldto_stop != NULL)
1302 t->ldt_ops->ldto_stop(t);
1304 EXPORT_SYMBOL(lu_device_fini);
1307 * Initialize object \a o that is part of compound object \a h and was created
1310 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1311 struct lu_device *d)
1313 memset(o, 0, sizeof(*o));
1317 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1318 INIT_LIST_HEAD(&o->lo_linkage);
1322 EXPORT_SYMBOL(lu_object_init);
1325 * Finalize object and release its resources.
1327 void lu_object_fini(struct lu_object *o)
1329 struct lu_device *dev = o->lo_dev;
1331 LASSERT(list_empty(&o->lo_linkage));
1334 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1340 EXPORT_SYMBOL(lu_object_fini);
1343 * Add object \a o as first layer of compound object \a h
1345 * This is typically called by the ->ldo_object_alloc() method of top-level
1348 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1350 list_move(&o->lo_linkage, &h->loh_layers);
1352 EXPORT_SYMBOL(lu_object_add_top);
1355 * Add object \a o as a layer of compound object, going after \a before.
1357 * This is typically called by the ->ldo_object_alloc() method of \a
1360 void lu_object_add(struct lu_object *before, struct lu_object *o)
1362 list_move(&o->lo_linkage, &before->lo_linkage);
1364 EXPORT_SYMBOL(lu_object_add);
1367 * Initialize compound object.
1369 int lu_object_header_init(struct lu_object_header *h)
1371 memset(h, 0, sizeof *h);
1372 atomic_set(&h->loh_ref, 1);
1373 INIT_LIST_HEAD(&h->loh_lru);
1374 INIT_LIST_HEAD(&h->loh_layers);
1375 lu_ref_init(&h->loh_reference);
1378 EXPORT_SYMBOL(lu_object_header_init);
1381 * Finalize compound object.
1383 void lu_object_header_fini(struct lu_object_header *h)
1385 LASSERT(list_empty(&h->loh_layers));
1386 LASSERT(list_empty(&h->loh_lru));
1387 lu_ref_fini(&h->loh_reference);
1389 EXPORT_SYMBOL(lu_object_header_fini);
1392 * Free lu_object_header with proper RCU handling
1394 void lu_object_header_free(struct lu_object_header *h)
1396 lu_object_header_fini(h);
1397 OBD_FREE_PRE(h, sizeof(*h), "kfreed");
1398 kfree_rcu(h, loh_rcu);
1400 EXPORT_SYMBOL(lu_object_header_free);
1403 * Given a compound object, find its slice, corresponding to the device type
1406 struct lu_object *lu_object_locate(struct lu_object_header *h,
1407 const struct lu_device_type *dtype)
1409 struct lu_object *o;
1411 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1412 if (o->lo_dev->ld_type == dtype)
1417 EXPORT_SYMBOL(lu_object_locate);
1420 * Finalize and free devices in the device stack.
1422 * Finalize device stack by purging object cache, and calling
1423 * lu_device_type_operations::ldto_device_fini() and
1424 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1426 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1428 struct lu_site *site = top->ld_site;
1429 struct lu_device *scan;
1430 struct lu_device *next;
1432 lu_site_purge(env, site, ~0);
1433 for (scan = top; scan != NULL; scan = next) {
1434 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1435 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1436 lu_device_put(scan);
1440 lu_site_purge(env, site, ~0);
1442 for (scan = top; scan != NULL; scan = next) {
1443 const struct lu_device_type *ldt = scan->ld_type;
1445 next = ldt->ldt_ops->ldto_device_free(env, scan);
1450 * Global counter incremented whenever key is registered, unregistered,
1451 * revived or quiesced. This is used to void unnecessary calls to
1452 * lu_context_refill(). No locking is provided, as initialization and shutdown
1453 * are supposed to be externally serialized.
1455 static atomic_t key_set_version = ATOMIC_INIT(0);
1460 int lu_context_key_register(struct lu_context_key *key)
1465 LASSERT(key->lct_init != NULL);
1466 LASSERT(key->lct_fini != NULL);
1467 LASSERT(key->lct_tags != 0);
1468 LASSERT(key->lct_owner != NULL);
1471 atomic_set(&key->lct_used, 1);
1472 lu_ref_init(&key->lct_reference);
1473 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1478 if (strncmp("osd_", module_name(key->lct_owner), 4) == 0)
1479 CFS_RACE_WAIT(OBD_FAIL_OBD_SETUP);
1481 if (cmpxchg(&lu_keys[i], NULL, key) != NULL)
1485 atomic_inc(&key_set_version);
1489 lu_ref_fini(&key->lct_reference);
1490 atomic_set(&key->lct_used, 0);
1494 EXPORT_SYMBOL(lu_context_key_register);
1496 static void key_fini(struct lu_context *ctx, int index)
1498 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1499 struct lu_context_key *key;
1501 key = lu_keys[index];
1502 LASSERT(key != NULL);
1503 LASSERT(key->lct_fini != NULL);
1504 LASSERT(atomic_read(&key->lct_used) > 0);
1506 key->lct_fini(ctx, key, ctx->lc_value[index]);
1507 lu_ref_del(&key->lct_reference, "ctx", ctx);
1508 if (atomic_dec_and_test(&key->lct_used))
1509 wake_up_var(&key->lct_used);
1511 LASSERT(key->lct_owner != NULL);
1512 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1513 LINVRNT(module_refcount(key->lct_owner) > 0);
1514 module_put(key->lct_owner);
1516 ctx->lc_value[index] = NULL;
1523 void lu_context_key_degister(struct lu_context_key *key)
1525 LASSERT(atomic_read(&key->lct_used) >= 1);
1526 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1528 lu_context_key_quiesce(NULL, key);
1530 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1533 * Wait until all transient contexts referencing this key have
1534 * run lu_context_key::lct_fini() method.
1536 atomic_dec(&key->lct_used);
1537 wait_var_event(&key->lct_used, atomic_read(&key->lct_used) == 0);
1539 if (!WARN_ON(lu_keys[key->lct_index] == NULL))
1540 lu_ref_fini(&key->lct_reference);
1542 smp_store_release(&lu_keys[key->lct_index], NULL);
1544 EXPORT_SYMBOL(lu_context_key_degister);
1547 * Register a number of keys. This has to be called after all keys have been
1548 * initialized by a call to LU_CONTEXT_KEY_INIT().
1550 int lu_context_key_register_many(struct lu_context_key *k, ...)
1552 struct lu_context_key *key = k;
1558 result = lu_context_key_register(key);
1561 key = va_arg(args, struct lu_context_key *);
1562 } while (key != NULL);
1568 lu_context_key_degister(k);
1569 k = va_arg(args, struct lu_context_key *);
1576 EXPORT_SYMBOL(lu_context_key_register_many);
1579 * De-register a number of keys. This is a dual to
1580 * lu_context_key_register_many().
1582 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1588 lu_context_key_degister(k);
1589 k = va_arg(args, struct lu_context_key*);
1590 } while (k != NULL);
1593 EXPORT_SYMBOL(lu_context_key_degister_many);
1596 * Revive a number of keys.
1598 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1604 lu_context_key_revive(k);
1605 k = va_arg(args, struct lu_context_key*);
1606 } while (k != NULL);
1609 EXPORT_SYMBOL(lu_context_key_revive_many);
1612 * Quiescent a number of keys.
1614 void lu_context_key_quiesce_many(struct lu_device_type *t,
1615 struct lu_context_key *k, ...)
1621 lu_context_key_quiesce(t, k);
1622 k = va_arg(args, struct lu_context_key*);
1623 } while (k != NULL);
1626 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1629 * Return value associated with key \a key in context \a ctx.
1631 void *lu_context_key_get(const struct lu_context *ctx,
1632 const struct lu_context_key *key)
1634 LINVRNT(ctx->lc_state == LCS_ENTERED);
1635 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1636 LASSERT(lu_keys[key->lct_index] == key);
1637 return ctx->lc_value[key->lct_index];
1639 EXPORT_SYMBOL(lu_context_key_get);
1642 * List of remembered contexts. XXX document me.
1644 static LIST_HEAD(lu_context_remembered);
1645 static DEFINE_SPINLOCK(lu_context_remembered_guard);
1648 * Destroy \a key in all remembered contexts. This is used to destroy key
1649 * values in "shared" contexts (like service threads), when a module owning
1650 * the key is about to be unloaded.
1652 void lu_context_key_quiesce(struct lu_device_type *t,
1653 struct lu_context_key *key)
1655 struct lu_context *ctx;
1657 if (key->lct_tags & LCT_QUIESCENT)
1660 * The write-lock on lu_key_initing will ensure that any
1661 * keys_fill() which didn't see LCT_QUIESCENT will have
1662 * finished before we call key_fini().
1664 down_write(&lu_key_initing);
1665 if (!(key->lct_tags & LCT_QUIESCENT)) {
1666 if (t == NULL || atomic_read(&t->ldt_device_nr) == 0)
1667 key->lct_tags |= LCT_QUIESCENT;
1668 up_write(&lu_key_initing);
1670 spin_lock(&lu_context_remembered_guard);
1671 list_for_each_entry(ctx, &lu_context_remembered, lc_remember) {
1672 spin_until_cond(READ_ONCE(ctx->lc_state) != LCS_LEAVING);
1673 key_fini(ctx, key->lct_index);
1675 spin_unlock(&lu_context_remembered_guard);
1679 up_write(&lu_key_initing);
1682 void lu_context_key_revive(struct lu_context_key *key)
1684 key->lct_tags &= ~LCT_QUIESCENT;
1685 atomic_inc(&key_set_version);
1688 static void keys_fini(struct lu_context *ctx)
1692 if (ctx->lc_value == NULL)
1695 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1698 OBD_FREE_PTR_ARRAY(ctx->lc_value, ARRAY_SIZE(lu_keys));
1699 ctx->lc_value = NULL;
1702 static int keys_fill(struct lu_context *ctx)
1708 * A serialisation with lu_context_key_quiesce() is needed, to
1709 * ensure we see LCT_QUIESCENT and don't allocate a new value
1710 * after it freed one. The rwsem provides this. As down_read()
1711 * does optimistic spinning while the writer is active, this is
1712 * unlikely to ever sleep.
1714 down_read(&lu_key_initing);
1715 ctx->lc_version = atomic_read(&key_set_version);
1717 LINVRNT(ctx->lc_value);
1718 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1719 struct lu_context_key *key;
1722 if (!ctx->lc_value[i] && key &&
1723 (key->lct_tags & ctx->lc_tags) &&
1725 * Don't create values for a LCT_QUIESCENT key, as this
1726 * will pin module owning a key.
1728 !(key->lct_tags & LCT_QUIESCENT)) {
1731 LINVRNT(key->lct_init != NULL);
1732 LINVRNT(key->lct_index == i);
1734 LASSERT(key->lct_owner != NULL);
1735 if (!(ctx->lc_tags & LCT_NOREF) &&
1736 try_module_get(key->lct_owner) == 0) {
1737 /* module is unloading, skip this key */
1741 value = key->lct_init(ctx, key);
1742 if (unlikely(IS_ERR(value))) {
1743 rc = PTR_ERR(value);
1747 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1748 atomic_inc(&key->lct_used);
1750 * This is the only place in the code, where an
1751 * element of ctx->lc_value[] array is set to non-NULL
1754 ctx->lc_value[i] = value;
1755 if (key->lct_exit != NULL)
1756 ctx->lc_tags |= LCT_HAS_EXIT;
1760 up_read(&lu_key_initing);
1764 static int keys_init(struct lu_context *ctx)
1766 OBD_ALLOC_PTR_ARRAY(ctx->lc_value, ARRAY_SIZE(lu_keys));
1767 if (likely(ctx->lc_value != NULL))
1768 return keys_fill(ctx);
1774 * Initialize context data-structure. Create values for all keys.
1776 int lu_context_init(struct lu_context *ctx, __u32 tags)
1780 memset(ctx, 0, sizeof *ctx);
1781 ctx->lc_state = LCS_INITIALIZED;
1782 ctx->lc_tags = tags;
1783 if (tags & LCT_REMEMBER) {
1784 spin_lock(&lu_context_remembered_guard);
1785 list_add(&ctx->lc_remember, &lu_context_remembered);
1786 spin_unlock(&lu_context_remembered_guard);
1788 INIT_LIST_HEAD(&ctx->lc_remember);
1791 rc = keys_init(ctx);
1793 lu_context_fini(ctx);
1797 EXPORT_SYMBOL(lu_context_init);
1800 * Finalize context data-structure. Destroy key values.
1802 void lu_context_fini(struct lu_context *ctx)
1804 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1805 ctx->lc_state = LCS_FINALIZED;
1807 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1808 LASSERT(list_empty(&ctx->lc_remember));
1810 /* could race with key degister */
1811 spin_lock(&lu_context_remembered_guard);
1812 list_del_init(&ctx->lc_remember);
1813 spin_unlock(&lu_context_remembered_guard);
1817 EXPORT_SYMBOL(lu_context_fini);
1820 * Called before entering context.
1822 void lu_context_enter(struct lu_context *ctx)
1824 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1825 ctx->lc_state = LCS_ENTERED;
1827 EXPORT_SYMBOL(lu_context_enter);
1830 * Called after exiting from \a ctx
1832 void lu_context_exit(struct lu_context *ctx)
1836 LINVRNT(ctx->lc_state == LCS_ENTERED);
1838 * Disable preempt to ensure we get a warning if
1839 * any lct_exit ever tries to sleep. That would hurt
1840 * lu_context_key_quiesce() which spins waiting for us.
1841 * This also ensure we aren't preempted while the state
1842 * is LCS_LEAVING, as that too would cause problems for
1843 * lu_context_key_quiesce().
1847 * Ensure lu_context_key_quiesce() sees LCS_LEAVING
1848 * or we see LCT_QUIESCENT
1850 smp_store_mb(ctx->lc_state, LCS_LEAVING);
1851 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value) {
1852 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1853 struct lu_context_key *key;
1856 if (ctx->lc_value[i] &&
1857 !(key->lct_tags & LCT_QUIESCENT) &&
1859 key->lct_exit(ctx, key, ctx->lc_value[i]);
1863 smp_store_release(&ctx->lc_state, LCS_LEFT);
1866 EXPORT_SYMBOL(lu_context_exit);
1869 * Allocate for context all missing keys that were registered after context
1870 * creation. key_set_version is only changed in rare cases when modules
1871 * are loaded and removed.
1873 int lu_context_refill(struct lu_context *ctx)
1875 if (likely(ctx->lc_version == atomic_read(&key_set_version)))
1878 return keys_fill(ctx);
1882 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1883 * obd being added. Currently, this is only used on client side, specifically
1884 * for echo device client, for other stack (like ptlrpc threads), context are
1885 * predefined when the lu_device type are registered, during the module probe
1888 u32 lu_context_tags_default = LCT_CL_THREAD;
1889 u32 lu_session_tags_default = LCT_SESSION;
1891 void lu_context_tags_update(__u32 tags)
1893 spin_lock(&lu_context_remembered_guard);
1894 lu_context_tags_default |= tags;
1895 atomic_inc(&key_set_version);
1896 spin_unlock(&lu_context_remembered_guard);
1898 EXPORT_SYMBOL(lu_context_tags_update);
1900 void lu_context_tags_clear(__u32 tags)
1902 spin_lock(&lu_context_remembered_guard);
1903 lu_context_tags_default &= ~tags;
1904 atomic_inc(&key_set_version);
1905 spin_unlock(&lu_context_remembered_guard);
1907 EXPORT_SYMBOL(lu_context_tags_clear);
1909 void lu_session_tags_update(__u32 tags)
1911 spin_lock(&lu_context_remembered_guard);
1912 lu_session_tags_default |= tags;
1913 atomic_inc(&key_set_version);
1914 spin_unlock(&lu_context_remembered_guard);
1916 EXPORT_SYMBOL(lu_session_tags_update);
1918 void lu_session_tags_clear(__u32 tags)
1920 spin_lock(&lu_context_remembered_guard);
1921 lu_session_tags_default &= ~tags;
1922 atomic_inc(&key_set_version);
1923 spin_unlock(&lu_context_remembered_guard);
1925 EXPORT_SYMBOL(lu_session_tags_clear);
1927 int lu_env_init(struct lu_env *env, __u32 tags)
1932 result = lu_context_init(&env->le_ctx, tags);
1933 if (likely(result == 0))
1934 lu_context_enter(&env->le_ctx);
1937 EXPORT_SYMBOL(lu_env_init);
1939 void lu_env_fini(struct lu_env *env)
1941 lu_context_exit(&env->le_ctx);
1942 lu_context_fini(&env->le_ctx);
1945 EXPORT_SYMBOL(lu_env_fini);
1947 int lu_env_refill(struct lu_env *env)
1951 result = lu_context_refill(&env->le_ctx);
1952 if (result == 0 && env->le_ses != NULL)
1953 result = lu_context_refill(env->le_ses);
1956 EXPORT_SYMBOL(lu_env_refill);
1959 * Currently, this API will only be used by echo client.
1960 * Because echo client and normal lustre client will share
1961 * same cl_env cache. So echo client needs to refresh
1962 * the env context after it get one from the cache, especially
1963 * when normal client and echo client co-exist in the same client.
1965 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1970 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1971 env->le_ctx.lc_version = 0;
1972 env->le_ctx.lc_tags |= ctags;
1975 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1976 env->le_ses->lc_version = 0;
1977 env->le_ses->lc_tags |= stags;
1980 result = lu_env_refill(env);
1984 EXPORT_SYMBOL(lu_env_refill_by_tags);
1987 struct lu_env_item {
1988 struct task_struct *lei_task; /* rhashtable key */
1989 struct rhash_head lei_linkage;
1990 struct lu_env *lei_env;
1991 struct rcu_head lei_rcu_head;
1994 static const struct rhashtable_params lu_env_rhash_params = {
1995 .key_len = sizeof(struct task_struct *),
1996 .key_offset = offsetof(struct lu_env_item, lei_task),
1997 .head_offset = offsetof(struct lu_env_item, lei_linkage),
2000 struct rhashtable lu_env_rhash;
2002 struct lu_env_percpu {
2003 struct task_struct *lep_task;
2004 struct lu_env *lep_env ____cacheline_aligned_in_smp;
2007 static struct lu_env_percpu lu_env_percpu[NR_CPUS];
2009 int lu_env_add_task(struct lu_env *env, struct task_struct *task)
2011 struct lu_env_item *lei, *old;
2019 lei->lei_task = task;
2022 old = rhashtable_lookup_get_insert_fast(&lu_env_rhash,
2024 lu_env_rhash_params);
2029 EXPORT_SYMBOL(lu_env_add_task);
2031 int lu_env_add(struct lu_env *env)
2033 return lu_env_add_task(env, current);
2035 EXPORT_SYMBOL(lu_env_add);
2037 static void lu_env_item_free(struct rcu_head *head)
2039 struct lu_env_item *lei;
2041 lei = container_of(head, struct lu_env_item, lei_rcu_head);
2045 void lu_env_remove(struct lu_env *env)
2047 struct lu_env_item *lei;
2048 const void *task = current;
2051 for_each_possible_cpu(i) {
2052 if (lu_env_percpu[i].lep_env == env) {
2053 LASSERT(lu_env_percpu[i].lep_task == task);
2054 lu_env_percpu[i].lep_task = NULL;
2055 lu_env_percpu[i].lep_env = NULL;
2059 /* The rcu_lock is not taking in this case since the key
2060 * used is the actual task_struct. This implies that each
2061 * object is only removed by the owning thread, so there
2062 * can never be a race on a particular object.
2064 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
2065 lu_env_rhash_params);
2066 if (lei && rhashtable_remove_fast(&lu_env_rhash, &lei->lei_linkage,
2067 lu_env_rhash_params) == 0)
2068 call_rcu(&lei->lei_rcu_head, lu_env_item_free);
2070 EXPORT_SYMBOL(lu_env_remove);
2072 struct lu_env *lu_env_find(void)
2074 struct lu_env *env = NULL;
2075 struct lu_env_item *lei;
2076 const void *task = current;
2079 if (lu_env_percpu[i].lep_task == current) {
2080 env = lu_env_percpu[i].lep_env;
2086 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
2087 lu_env_rhash_params);
2090 lu_env_percpu[i].lep_task = current;
2091 lu_env_percpu[i].lep_env = env;
2097 EXPORT_SYMBOL(lu_env_find);
2099 typedef struct lu_site_stats{
2100 unsigned lss_populated;
2101 unsigned lss_max_search;
2106 static void lu_site_stats_get(const struct lu_site *s,
2107 lu_site_stats_t *stats)
2109 int cnt = atomic_read(&s->ls_obj_hash.nelems);
2111 * percpu_counter_sum_positive() won't accept a const pointer
2112 * as it does modify the struct by taking a spinlock
2114 struct lu_site *s2 = (struct lu_site *)s;
2116 stats->lss_busy += cnt -
2117 percpu_counter_sum_positive(&s2->ls_lru_len_counter);
2119 stats->lss_total += cnt;
2120 stats->lss_max_search = 0;
2121 stats->lss_populated = 0;
2126 * lu_cache_shrink_count() returns an approximate number of cached objects
2127 * that can be freed by shrink_slab(). A counter, which tracks the
2128 * number of items in the site's lru, is maintained in a percpu_counter
2129 * for each site. The percpu values are incremented and decremented as
2130 * objects are added or removed from the lru. The percpu values are summed
2131 * and saved whenever a percpu value exceeds a threshold. Thus the saved,
2132 * summed value at any given time may not accurately reflect the current
2133 * lru length. But this value is sufficiently accurate for the needs of
2136 * Using a per cpu counter is a compromise solution to concurrent access:
2137 * lu_object_put() can update the counter without locking the site and
2138 * lu_cache_shrink_count can sum the counters without locking each
2139 * ls_obj_hash bucket.
2141 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
2142 struct shrink_control *sc)
2145 struct lu_site *tmp;
2146 unsigned long cached = 0;
2148 if (!(sc->gfp_mask & __GFP_FS))
2151 down_read(&lu_sites_guard);
2152 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage)
2153 cached += percpu_counter_read_positive(&s->ls_lru_len_counter);
2154 up_read(&lu_sites_guard);
2156 cached = (cached / 100) * sysctl_vfs_cache_pressure;
2157 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
2158 cached, sysctl_vfs_cache_pressure);
2163 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
2164 struct shrink_control *sc)
2167 struct lu_site *tmp;
2168 unsigned long remain = sc->nr_to_scan;
2171 if (!(sc->gfp_mask & __GFP_FS))
2172 /* We must not take the lu_sites_guard lock when
2173 * __GFP_FS is *not* set because of the deadlock
2174 * possibility detailed above. Additionally,
2175 * since we cannot determine the number of
2176 * objects in the cache without taking this
2177 * lock, we're in a particularly tough spot. As
2178 * a result, we'll just lie and say our cache is
2179 * empty. This _should_ be ok, as we can't
2180 * reclaim objects when __GFP_FS is *not* set
2185 down_write(&lu_sites_guard);
2186 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
2187 remain = lu_site_purge(&lu_shrink_env, s, remain);
2189 * Move just shrunk site to the tail of site list to
2190 * assure shrinking fairness.
2192 list_move_tail(&s->ls_linkage, &splice);
2194 list_splice(&splice, lu_sites.prev);
2195 up_write(&lu_sites_guard);
2197 return sc->nr_to_scan - remain;
2200 #ifdef HAVE_SHRINKER_COUNT
2201 static struct shrinker lu_site_shrinker = {
2202 .count_objects = lu_cache_shrink_count,
2203 .scan_objects = lu_cache_shrink_scan,
2204 .seeks = DEFAULT_SEEKS,
2209 * There exists a potential lock inversion deadlock scenario when using
2210 * Lustre on top of ZFS. This occurs between one of ZFS's
2211 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2212 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2213 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2214 * lock. Obviously neither thread will wake and drop their respective hold
2217 * To prevent this from happening we must ensure the lu_sites_guard lock is
2218 * not taken while down this code path. ZFS reliably does not set the
2219 * __GFP_FS bit in its code paths, so this can be used to determine if it
2220 * is safe to take the lu_sites_guard lock.
2222 * Ideally we should accurately return the remaining number of cached
2223 * objects without taking the lu_sites_guard lock, but this is not
2224 * possible in the current implementation.
2226 static int lu_cache_shrink(struct shrinker *shrinker,
2227 struct shrink_control *sc)
2231 CDEBUG(D_INODE, "Shrink %lu objects\n", sc->nr_to_scan);
2233 if (sc->nr_to_scan != 0)
2234 lu_cache_shrink_scan(shrinker, sc);
2236 cached = lu_cache_shrink_count(shrinker, sc);
2240 static struct shrinker lu_site_shrinker = {
2241 .shrink = lu_cache_shrink,
2242 .seeks = DEFAULT_SEEKS,
2245 #endif /* HAVE_SHRINKER_COUNT */
2253 * Environment to be used in debugger, contains all tags.
2255 static struct lu_env lu_debugging_env;
2258 * Debugging printer function using printk().
2260 int lu_printk_printer(const struct lu_env *env,
2261 void *unused, const char *format, ...)
2265 va_start(args, format);
2266 vprintk(format, args);
2271 int lu_debugging_setup(void)
2273 return lu_env_init(&lu_debugging_env, ~0);
2276 void lu_context_keys_dump(void)
2280 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2281 struct lu_context_key *key;
2285 CERROR("LU context keys [%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2286 i, key, key->lct_tags,
2287 key->lct_init, key->lct_fini, key->lct_exit,
2288 key->lct_index, atomic_read(&key->lct_used),
2289 key->lct_owner ? key->lct_owner->name : "",
2291 lu_ref_print(&key->lct_reference);
2297 * Initialization of global lu_* data.
2299 int lu_global_init(void)
2303 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2305 result = lu_ref_global_init();
2309 LU_CONTEXT_KEY_INIT(&lu_global_key);
2310 result = lu_context_key_register(&lu_global_key);
2315 * At this level, we don't know what tags are needed, so allocate them
2316 * conservatively. This should not be too bad, because this
2317 * environment is global.
2319 down_write(&lu_sites_guard);
2320 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2321 up_write(&lu_sites_guard);
2323 lu_context_key_degister(&lu_global_key);
2328 * seeks estimation: 3 seeks to read a record from oi, one to read
2329 * inode, one for ea. Unfortunately setting this high value results in
2330 * lu_object/inode cache consuming all the memory.
2332 result = register_shrinker(&lu_site_shrinker);
2336 result = rhashtable_init(&lu_env_rhash, &lu_env_rhash_params);
2344 unregister_shrinker(&lu_site_shrinker);
2346 /* ordering here is explained in lu_global_fini() */
2347 lu_context_key_degister(&lu_global_key);
2348 down_write(&lu_sites_guard);
2349 lu_env_fini(&lu_shrink_env);
2350 up_write(&lu_sites_guard);
2352 lu_ref_global_fini();
2357 * Dual to lu_global_init().
2359 void lu_global_fini(void)
2361 unregister_shrinker(&lu_site_shrinker);
2363 lu_context_key_degister(&lu_global_key);
2366 * Tear shrinker environment down _after_ de-registering
2367 * lu_global_key, because the latter has a value in the former.
2369 down_write(&lu_sites_guard);
2370 lu_env_fini(&lu_shrink_env);
2371 up_write(&lu_sites_guard);
2373 rhashtable_destroy(&lu_env_rhash);
2375 lu_ref_global_fini();
2378 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2380 #ifdef CONFIG_PROC_FS
2381 struct lprocfs_counter ret;
2383 lprocfs_stats_collect(stats, idx, &ret);
2384 return (__u32)ret.lc_count;
2391 * Output site statistical counters into a buffer. Suitable for
2392 * lprocfs_rd_*()-style functions.
2394 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2396 const struct bucket_table *tbl;
2397 lu_site_stats_t stats;
2398 unsigned int chains;
2400 memset(&stats, 0, sizeof(stats));
2401 lu_site_stats_get(s, &stats);
2404 tbl = rht_dereference_rcu(s->ls_obj_hash.tbl,
2405 &((struct lu_site *)s)->ls_obj_hash);
2408 seq_printf(m, "%d/%d %d/%u %d %d %d %d %d %d %d\n",
2411 stats.lss_populated,
2413 stats.lss_max_search,
2414 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2415 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2416 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2417 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2418 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2419 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2422 EXPORT_SYMBOL(lu_site_stats_seq_print);
2425 * Helper function to initialize a number of kmem slab caches at once.
2427 int lu_kmem_init(struct lu_kmem_descr *caches)
2430 struct lu_kmem_descr *iter = caches;
2432 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2433 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2436 if (*iter->ckd_cache == NULL) {
2438 /* free all previously allocated caches */
2439 lu_kmem_fini(caches);
2445 EXPORT_SYMBOL(lu_kmem_init);
2448 * Helper function to finalize a number of kmem slab cached at once. Dual to
2451 void lu_kmem_fini(struct lu_kmem_descr *caches)
2453 for (; caches->ckd_cache != NULL; ++caches) {
2454 if (*caches->ckd_cache != NULL) {
2455 kmem_cache_destroy(*caches->ckd_cache);
2456 *caches->ckd_cache = NULL;
2460 EXPORT_SYMBOL(lu_kmem_fini);
2463 * Temporary solution to be able to assign fid in ->do_create()
2464 * till we have fully-functional OST fids
2466 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2467 const struct lu_fid *fid)
2469 struct lu_site *s = o->lo_dev->ld_site;
2470 struct lu_fid *old = &o->lo_header->loh_fid;
2473 LASSERT(fid_is_zero(old));
2476 rc = rhashtable_lookup_insert_fast(&s->ls_obj_hash,
2477 &o->lo_header->loh_hash,
2479 /* supposed to be unique */
2480 LASSERT(rc != -EEXIST);
2481 /* handle hash table resizing */
2482 if (rc == -ENOMEM || rc == -EBUSY) {
2486 /* trim the hash if its growing to big */
2487 lu_object_limit(env, o->lo_dev);
2491 LASSERTF(rc == 0, "failed hashtable insertion: rc = %d\n", rc);
2493 EXPORT_SYMBOL(lu_object_assign_fid);
2496 * allocates object with 0 (non-assiged) fid
2497 * XXX: temporary solution to be able to assign fid in ->do_create()
2498 * till we have fully-functional OST fids
2500 struct lu_object *lu_object_anon(const struct lu_env *env,
2501 struct lu_device *dev,
2502 const struct lu_object_conf *conf)
2505 struct lu_object *o;
2509 o = lu_object_alloc(env, dev, &fid);
2511 rc = lu_object_start(env, dev, o, conf);
2513 lu_object_free(env, o);
2520 EXPORT_SYMBOL(lu_object_anon);
2522 struct lu_buf LU_BUF_NULL = {
2526 EXPORT_SYMBOL(LU_BUF_NULL);
2528 void lu_buf_free(struct lu_buf *buf)
2532 LASSERT(buf->lb_len > 0);
2533 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2538 EXPORT_SYMBOL(lu_buf_free);
2540 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2543 LASSERT(buf->lb_buf == NULL);
2544 LASSERT(buf->lb_len == 0);
2545 OBD_ALLOC_LARGE(buf->lb_buf, size);
2546 if (likely(buf->lb_buf))
2549 EXPORT_SYMBOL(lu_buf_alloc);
2551 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2554 lu_buf_alloc(buf, size);
2556 EXPORT_SYMBOL(lu_buf_realloc);
2558 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2560 if (buf->lb_buf == NULL && buf->lb_len == 0)
2561 lu_buf_alloc(buf, len);
2563 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2564 lu_buf_realloc(buf, len);
2568 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2571 * Increase the size of the \a buf.
2572 * preserves old data in buffer
2573 * old buffer remains unchanged on error
2574 * \retval 0 or -ENOMEM
2576 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2580 if (len <= buf->lb_len)
2583 OBD_ALLOC_LARGE(ptr, len);
2587 /* Free the old buf */
2588 if (buf->lb_buf != NULL) {
2589 memcpy(ptr, buf->lb_buf, buf->lb_len);
2590 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2597 EXPORT_SYMBOL(lu_buf_check_and_grow);