Whamcloud - gitweb
LU-3963 libcfs: remove cfs_hash_long
[fs/lustre-release.git] / lustre / obdclass / lu_object.c
index 117f607..489796a 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2011, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -57,6 +57,7 @@
 #include <lustre_disk.h>
 #include <lustre_fid.h>
 #include <lu_object.h>
+#include <lu_ref.h>
 #include <libcfs/list.h>
 
 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
@@ -89,7 +90,7 @@ void lu_object_put(const struct lu_env *env, struct lu_object *o)
                LASSERT(top->loh_hash.next == NULL
                        && top->loh_hash.pprev == NULL);
                LASSERT(cfs_list_empty(&top->loh_lru));
-               if (!cfs_atomic_dec_and_test(&top->loh_ref))
+               if (!atomic_dec_and_test(&top->loh_ref))
                        return;
                cfs_list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
                        if (o->lo_ops->loo_object_release != NULL)
@@ -102,17 +103,17 @@ void lu_object_put(const struct lu_env *env, struct lu_object *o)
         cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
         bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
 
-        if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
-                if (lu_object_is_dying(top)) {
+       if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
+               if (lu_object_is_dying(top)) {
 
-                        /*
-                         * somebody may be waiting for this, currently only
-                         * used for cl_object, see cl_object_put_last().
-                         */
-                        cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
-                }
-                return;
-        }
+                       /*
+                        * somebody may be waiting for this, currently only
+                        * used for cl_object, see cl_object_put_last().
+                        */
+                       wake_up_all(&bkt->lsb_marche_funebre);
+               }
+               return;
+       }
 
         LASSERT(bkt->lsb_busy > 0);
         bkt->lsb_busy--;
@@ -194,16 +195,18 @@ EXPORT_SYMBOL(lu_object_unhash);
  * struct lu_device_operations definition.
  */
 static struct lu_object *lu_object_alloc(const struct lu_env *env,
-                                         struct lu_device *dev,
-                                         const struct lu_fid *f,
-                                         const struct lu_object_conf *conf)
-{
-        struct lu_object *scan;
-        struct lu_object *top;
-        cfs_list_t *layers;
-        int clean;
-        int result;
-        ENTRY;
+                                        struct lu_device *dev,
+                                        const struct lu_fid *f,
+                                        const struct lu_object_conf *conf)
+{
+       struct lu_object *scan;
+       struct lu_object *top;
+       cfs_list_t *layers;
+       unsigned int init_mask = 0;
+       unsigned int init_flag;
+       int clean;
+       int result;
+       ENTRY;
 
        /*
         * Create top-level object slice. This will also create
@@ -220,25 +223,29 @@ static struct lu_object *lu_object_alloc(const struct lu_env *env,
          */
         top->lo_header->loh_fid = *f;
         layers = &top->lo_header->loh_layers;
-        do {
-                /*
-                 * Call ->loo_object_init() repeatedly, until no more new
-                 * object slices are created.
-                 */
-                clean = 1;
-                cfs_list_for_each_entry(scan, layers, lo_linkage) {
-                        if (scan->lo_flags & LU_OBJECT_ALLOCATED)
-                                continue;
-                        clean = 0;
-                        scan->lo_header = top->lo_header;
-                        result = scan->lo_ops->loo_object_init(env, scan, conf);
-                        if (result != 0) {
-                                lu_object_free(env, top);
-                                RETURN(ERR_PTR(result));
-                        }
-                        scan->lo_flags |= LU_OBJECT_ALLOCATED;
-                }
-        } while (!clean);
+
+       do {
+               /*
+                * Call ->loo_object_init() repeatedly, until no more new
+                * object slices are created.
+                */
+               clean = 1;
+               init_flag = 1;
+               cfs_list_for_each_entry(scan, layers, lo_linkage) {
+                       if (init_mask & init_flag)
+                               goto next;
+                       clean = 0;
+                       scan->lo_header = top->lo_header;
+                       result = scan->lo_ops->loo_object_init(env, scan, conf);
+                       if (result != 0) {
+                               lu_object_free(env, top);
+                               RETURN(ERR_PTR(result));
+                       }
+                       init_mask |= init_flag;
+next:
+                       init_flag <<= 1;
+               }
+       } while (!clean);
 
         cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
                 if (scan->lo_ops->loo_object_start != NULL) {
@@ -284,20 +291,20 @@ static void lu_object_free(const struct lu_env *env, struct lu_object *o)
          */
         CFS_INIT_LIST_HEAD(&splice);
         cfs_list_splice_init(layers, &splice);
-        while (!cfs_list_empty(&splice)) {
-                /*
-                 * Free layers in bottom-to-top order, so that object header
-                 * lives as long as possible and ->loo_object_free() methods
-                 * can look at its contents.
-                 */
-                o = container_of0(splice.prev, struct lu_object, lo_linkage);
-                cfs_list_del_init(&o->lo_linkage);
-                LASSERT(o->lo_ops->loo_object_free != NULL);
-                o->lo_ops->loo_object_free(env, o);
-        }
+       while (!cfs_list_empty(&splice)) {
+               /*
+                * Free layers in bottom-to-top order, so that object header
+                * lives as long as possible and ->loo_object_free() methods
+                * can look at its contents.
+                */
+               o = container_of0(splice.prev, struct lu_object, lo_linkage);
+               cfs_list_del_init(&o->lo_linkage);
+               LASSERT(o->lo_ops->loo_object_free != NULL);
+               o->lo_ops->loo_object_free(env, o);
+       }
 
-        if (cfs_waitq_active(&bkt->lsb_marche_funebre))
-                cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
+       if (waitqueue_active(&bkt->lsb_marche_funebre))
+               wake_up_all(&bkt->lsb_marche_funebre);
 }
 
 /**
@@ -317,6 +324,9 @@ int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
         int                      bnr;
         int                      i;
 
+       if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
+               RETURN(0);
+
         CFS_INIT_LIST_HEAD(&dispose);
         /*
          * Under LRU list lock, scan LRU list and move unreferenced objects to
@@ -334,7 +344,7 @@ int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
                 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
 
                 cfs_list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
-                        LASSERT(cfs_atomic_read(&h->loh_ref) == 0);
+                       LASSERT(atomic_read(&h->loh_ref) == 0);
 
                         cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
                         LASSERT(bd.bd_bucket == bd2.bd_bucket);
@@ -351,13 +361,13 @@ int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
                         if (count > 0 && --count == 0)
                                 break;
 
-                }
-                cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
-                cfs_cond_resched();
-                /*
-                 * Free everything on the dispose list. This is safe against
-                 * races due to the reasons described in lu_object_put().
-                 */
+               }
+               cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
+               cond_resched();
+               /*
+                * Free everything on the dispose list. This is safe against
+                * races due to the reasons described in lu_object_put().
+                */
                 while (!cfs_list_empty(&dispose)) {
                         h = container_of0(dispose.next,
                                           struct lu_object_header, loh_lru);
@@ -421,10 +431,10 @@ LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
  * lu_global_init().
  */
 struct lu_context_key lu_global_key = {
-        .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
-                    LCT_MG_THREAD | LCT_CL_THREAD,
-        .lct_init = lu_global_key_init,
-        .lct_fini = lu_global_key_fini
+       .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
+                   LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
+       .lct_init = lu_global_key_init,
+       .lct_fini = lu_global_key_fini
 };
 
 /**
@@ -469,7 +479,7 @@ void lu_object_header_print(const struct lu_env *env, void *cookie,
                             const struct lu_object_header *hdr)
 {
         (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
-                   hdr, hdr->loh_flags, cfs_atomic_read(&hdr->loh_ref),
+                  hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
                    PFID(&hdr->loh_fid),
                    cfs_hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
                    cfs_list_empty((cfs_list_t *)&hdr->loh_lru) ? \
@@ -482,28 +492,30 @@ EXPORT_SYMBOL(lu_object_header_print);
  * Print human readable representation of the \a o to the \a printer.
  */
 void lu_object_print(const struct lu_env *env, void *cookie,
-                     lu_printer_t printer, const struct lu_object *o)
+                    lu_printer_t printer, const struct lu_object *o)
 {
-        static const char ruler[] = "........................................";
-        struct lu_object_header *top;
-        int depth;
+       static const char ruler[] = "........................................";
+       struct lu_object_header *top;
+       int depth = 4;
 
-        top = o->lo_header;
-        lu_object_header_print(env, cookie, printer, top);
-        (*printer)(env, cookie, "{ \n");
-        cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
-                depth = o->lo_depth + 4;
+       top = o->lo_header;
+       lu_object_header_print(env, cookie, printer, top);
+       (*printer)(env, cookie, "{\n");
 
-                /*
-                 * print `.' \a depth times followed by type name and address
-                 */
-                (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
-                           o->lo_dev->ld_type->ldt_name, o);
-                if (o->lo_ops->loo_object_print != NULL)
-                        o->lo_ops->loo_object_print(env, cookie, printer, o);
-                (*printer)(env, cookie, "\n");
-        }
-        (*printer)(env, cookie, "} header@%p\n", top);
+       cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
+               /*
+                * print `.' \a depth times followed by type name and address
+                */
+               (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
+                          o->lo_dev->ld_type->ldt_name, o);
+
+               if (o->lo_ops->loo_object_print != NULL)
+                       (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
+
+               (*printer)(env, cookie, "\n");
+       }
+
+       (*printer)(env, cookie, "} header@%p\n", top);
 }
 EXPORT_SYMBOL(lu_object_print);
 
@@ -525,10 +537,10 @@ int lu_object_invariant(const struct lu_object *o)
 EXPORT_SYMBOL(lu_object_invariant);
 
 static struct lu_object *htable_lookup(struct lu_site *s,
-                                       cfs_hash_bd_t *bd,
-                                       const struct lu_fid *f,
-                                       cfs_waitlink_t *waiter,
-                                       __u64 *version)
+                                      cfs_hash_bd_t *bd,
+                                      const struct lu_fid *f,
+                                      wait_queue_t *waiter,
+                                      __u64 *version)
 {
         struct lu_site_bkt_data *bkt;
         struct lu_object_header *h;
@@ -536,7 +548,7 @@ static struct lu_object *htable_lookup(struct lu_site *s,
         __u64  ver = cfs_hash_bd_version_get(bd);
 
         if (*version == ver)
-                return NULL;
+               return ERR_PTR(-ENOENT);
 
         *version = ver;
         bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
@@ -545,7 +557,7 @@ static struct lu_object *htable_lookup(struct lu_site *s,
        hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
         if (hnode == NULL) {
                 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
-                return NULL;
+               return ERR_PTR(-ENOENT);
         }
 
         h = container_of0(hnode, struct lu_object_header, loh_hash);
@@ -562,11 +574,36 @@ static struct lu_object *htable_lookup(struct lu_site *s,
          * drained), and moreover, lookup has to wait until object is freed.
          */
 
-        cfs_waitlink_init(waiter);
-        cfs_waitq_add(&bkt->lsb_marche_funebre, waiter);
-        cfs_set_current_state(CFS_TASK_UNINT);
-        lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
-        return ERR_PTR(-EAGAIN);
+       init_waitqueue_entry_current(waiter);
+       add_wait_queue(&bkt->lsb_marche_funebre, waiter);
+       set_current_state(TASK_UNINTERRUPTIBLE);
+       lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
+       return ERR_PTR(-EAGAIN);
+}
+
+static struct lu_object *htable_lookup_nowait(struct lu_site *s,
+                                             cfs_hash_bd_t *bd,
+                                             const struct lu_fid *f)
+{
+       cfs_hlist_node_t        *hnode;
+       struct lu_object_header *h;
+
+       /* cfs_hash_bd_peek_locked is a somehow "internal" function
+        * of cfs_hash, it doesn't add refcount on object. */
+       hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
+       if (hnode == NULL) {
+               lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
+               return ERR_PTR(-ENOENT);
+       }
+
+       h = container_of0(hnode, struct lu_object_header, loh_hash);
+       if (unlikely(lu_object_is_dying(h)))
+               return ERR_PTR(-ENOENT);
+
+       cfs_hash_get(s->ls_obj_hash, hnode);
+       lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
+       cfs_list_del_init(&h->loh_lru);
+       return lu_object_top(h);
 }
 
 /**
@@ -609,17 +646,17 @@ static struct lu_object *lu_object_new(const struct lu_env *env,
  * Core logic of lu_object_find*() functions.
  */
 static struct lu_object *lu_object_find_try(const struct lu_env *env,
-                                            struct lu_device *dev,
-                                            const struct lu_fid *f,
-                                            const struct lu_object_conf *conf,
-                                            cfs_waitlink_t *waiter)
-{
-        struct lu_object      *o;
-        struct lu_object      *shadow;
-        struct lu_site        *s;
-        cfs_hash_t            *hs;
-        cfs_hash_bd_t          bd;
-        __u64                  version = 0;
+                                           struct lu_device *dev,
+                                           const struct lu_fid *f,
+                                           const struct lu_object_conf *conf,
+                                           wait_queue_t *waiter)
+{
+       struct lu_object      *o;
+       struct lu_object      *shadow;
+       struct lu_site        *s;
+       cfs_hash_t            *hs;
+       cfs_hash_bd_t          bd;
+       __u64                  version = 0;
 
         /*
          * This uses standard index maintenance protocol:
@@ -649,7 +686,7 @@ static struct lu_object *lu_object_find_try(const struct lu_env *env,
         cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
         o = htable_lookup(s, &bd, f, waiter, &version);
         cfs_hash_bd_unlock(hs, &bd, 1);
-        if (o != NULL)
+       if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
                 return o;
 
         /*
@@ -665,7 +702,7 @@ static struct lu_object *lu_object_find_try(const struct lu_env *env,
         cfs_hash_bd_lock(hs, &bd, 1);
 
         shadow = htable_lookup(s, &bd, f, waiter, &version);
-        if (likely(shadow == NULL)) {
+       if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
                 struct lu_site_bkt_data *bkt;
 
                 bkt = cfs_hash_bd_extra_get(hs, &bd);
@@ -687,28 +724,52 @@ static struct lu_object *lu_object_find_try(const struct lu_env *env,
  * objects of different "stacking" to be created within the same site.
  */
 struct lu_object *lu_object_find_at(const struct lu_env *env,
-                                    struct lu_device *dev,
-                                    const struct lu_fid *f,
-                                    const struct lu_object_conf *conf)
+                                   struct lu_device *dev,
+                                   const struct lu_fid *f,
+                                   const struct lu_object_conf *conf)
+{
+       struct lu_site_bkt_data *bkt;
+       struct lu_object        *obj;
+       wait_queue_t           wait;
+
+       while (1) {
+               obj = lu_object_find_try(env, dev, f, conf, &wait);
+               if (obj != ERR_PTR(-EAGAIN))
+                       return obj;
+               /*
+                * lu_object_find_try() already added waiter into the
+                * wait queue.
+                */
+               waitq_wait(&wait, TASK_UNINTERRUPTIBLE);
+               bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
+               remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
+       }
+}
+EXPORT_SYMBOL(lu_object_find_at);
+
+/**
+ * Try to find the object in cache without waiting for the dead object
+ * to be released nor allocating object if no cached one was found.
+ *
+ * The found object will be set as LU_OBJECT_HEARD_BANSHEE for purging.
+ */
+void lu_object_purge(const struct lu_env *env, struct lu_device *dev,
+                    const struct lu_fid *f)
 {
-        struct lu_site_bkt_data *bkt;
-        struct lu_object        *obj;
-        cfs_waitlink_t           wait;
+       struct lu_site          *s  = dev->ld_site;
+       cfs_hash_t              *hs = s->ls_obj_hash;
+       cfs_hash_bd_t            bd;
+       struct lu_object        *o;
 
-        while (1) {
-                obj = lu_object_find_try(env, dev, f, conf, &wait);
-                if (obj != ERR_PTR(-EAGAIN))
-                        return obj;
-                /*
-                 * lu_object_find_try() already added waiter into the
-                 * wait queue.
-                 */
-                cfs_waitq_wait(&wait, CFS_TASK_UNINT);
-                bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
-                cfs_waitq_del(&bkt->lsb_marche_funebre, &wait);
-        }
+       cfs_hash_bd_get_and_lock(hs, f, &bd, 1);
+       o = htable_lookup_nowait(s, &bd, f);
+       cfs_hash_bd_unlock(hs, &bd, 1);
+       if (!IS_ERR(o)) {
+               set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
+               lu_object_put(env, o);
+       }
 }
-EXPORT_SYMBOL(lu_object_find_at);
+EXPORT_SYMBOL(lu_object_purge);
 
 /**
  * Find object with given fid, and return its slice belonging to given device.
@@ -847,12 +908,12 @@ static int lu_htable_order(void)
          *
          * Size of lu_object is (arbitrary) taken as 1K (together with inode).
          */
-        cache_size = cfs_num_physpages;
+       cache_size = totalram_pages;
 
 #if BITS_PER_LONG == 32
         /* limit hashtable size for lowmem systems to low RAM */
-        if (cache_size > 1 << (30 - CFS_PAGE_SHIFT))
-                cache_size = 1 << (30 - CFS_PAGE_SHIFT) * 3 / 4;
+       if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
+               cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
 #endif
 
         /* clear off unreasonable cache setting. */
@@ -865,7 +926,7 @@ static int lu_htable_order(void)
                 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
         }
         cache_size = cache_size / 100 * lu_cache_percent *
-                (CFS_PAGE_SIZE / 1024);
+               (PAGE_CACHE_SIZE / 1024);
 
         for (bits = 1; (1 << bits) < cache_size; ++bits) {
                 ;
@@ -874,22 +935,22 @@ static int lu_htable_order(void)
 }
 
 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
-                                const void *key, unsigned mask)
+                               const void *key, unsigned mask)
 {
-        struct lu_fid  *fid = (struct lu_fid *)key;
-        __u32           hash;
+       struct lu_fid  *fid = (struct lu_fid *)key;
+       __u32           hash;
 
-        hash = fid_flatten32(fid);
-        hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
-        hash = cfs_hash_long(hash, hs->hs_bkt_bits);
+       hash = fid_flatten32(fid);
+       hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
+       hash = hash_long(hash, hs->hs_bkt_bits);
 
-        /* give me another random factor */
-        hash -= cfs_hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
+       /* give me another random factor */
+       hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
 
-        hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
-        hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
+       hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
+       hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
 
-        return hash & mask;
+       return hash & mask;
 }
 
 static void *lu_obj_hop_object(cfs_hlist_node_t *hnode)
@@ -918,7 +979,7 @@ static void lu_obj_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
         struct lu_object_header *h;
 
         h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
-        if (cfs_atomic_add_return(1, &h->loh_ref) == 1) {
+       if (atomic_add_return(1, &h->loh_ref) == 1) {
                 struct lu_site_bkt_data *bkt;
                 cfs_hash_bd_t            bd;
 
@@ -1002,11 +1063,11 @@ int lu_site_init(struct lu_site *s, struct lu_device *top)
                 return -ENOMEM;
         }
 
-        cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
-                bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
-                CFS_INIT_LIST_HEAD(&bkt->lsb_lru);
-                cfs_waitq_init(&bkt->lsb_marche_funebre);
-        }
+       cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
+               bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
+               CFS_INIT_LIST_HEAD(&bkt->lsb_lru);
+               init_waitqueue_head(&bkt->lsb_marche_funebre);
+       }
 
         s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
         if (s->ls_stats == NULL) {
@@ -1089,7 +1150,7 @@ EXPORT_SYMBOL(lu_site_init_finish);
  */
 void lu_device_get(struct lu_device *d)
 {
-        cfs_atomic_inc(&d->ld_ref);
+       atomic_inc(&d->ld_ref);
 }
 EXPORT_SYMBOL(lu_device_get);
 
@@ -1098,8 +1159,8 @@ EXPORT_SYMBOL(lu_device_get);
  */
 void lu_device_put(struct lu_device *d)
 {
-        LASSERT(cfs_atomic_read(&d->ld_ref) > 0);
-        cfs_atomic_dec(&d->ld_ref);
+       LASSERT(atomic_read(&d->ld_ref) > 0);
+       atomic_dec(&d->ld_ref);
 }
 EXPORT_SYMBOL(lu_device_put);
 
@@ -1111,7 +1172,7 @@ int lu_device_init(struct lu_device *d, struct lu_device_type *t)
         if (t->ldt_device_nr++ == 0 && t->ldt_ops->ldto_start != NULL)
                 t->ldt_ops->ldto_start(t);
         memset(d, 0, sizeof *d);
-        cfs_atomic_set(&d->ld_ref, 0);
+       atomic_set(&d->ld_ref, 0);
         d->ld_type = t;
         lu_ref_init(&d->ld_reference);
         CFS_INIT_LIST_HEAD(&d->ld_linkage);
@@ -1133,8 +1194,8 @@ void lu_device_fini(struct lu_device *d)
         }
 
         lu_ref_fini(&d->ld_reference);
-        LASSERTF(cfs_atomic_read(&d->ld_ref) == 0,
-                 "Refcount is %u\n", cfs_atomic_read(&d->ld_ref));
+       LASSERTF(atomic_read(&d->ld_ref) == 0,
+                "Refcount is %u\n", atomic_read(&d->ld_ref));
         LASSERT(t->ldt_device_nr > 0);
         if (--t->ldt_device_nr == 0 && t->ldt_ops->ldto_stop != NULL)
                 t->ldt_ops->ldto_stop(t);
@@ -1145,16 +1206,17 @@ EXPORT_SYMBOL(lu_device_fini);
  * Initialize object \a o that is part of compound object \a h and was created
  * by device \a d.
  */
-int lu_object_init(struct lu_object *o,
-                   struct lu_object_header *h, struct lu_device *d)
+int lu_object_init(struct lu_object *o, struct lu_object_header *h,
+                  struct lu_device *d)
 {
-        memset(o, 0, sizeof *o);
-        o->lo_header = h;
-        o->lo_dev    = d;
-        lu_device_get(d);
-        o->lo_dev_ref = lu_ref_add(&d->ld_reference, "lu_object", o);
-        CFS_INIT_LIST_HEAD(&o->lo_linkage);
-        return 0;
+       memset(o, 0, sizeof(*o));
+       o->lo_header = h;
+       o->lo_dev = d;
+       lu_device_get(d);
+       lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
+       CFS_INIT_LIST_HEAD(&o->lo_linkage);
+
+       return 0;
 }
 EXPORT_SYMBOL(lu_object_init);
 
@@ -1163,16 +1225,16 @@ EXPORT_SYMBOL(lu_object_init);
  */
 void lu_object_fini(struct lu_object *o)
 {
-        struct lu_device *dev = o->lo_dev;
+       struct lu_device *dev = o->lo_dev;
 
-        LASSERT(cfs_list_empty(&o->lo_linkage));
+       LASSERT(cfs_list_empty(&o->lo_linkage));
 
-        if (dev != NULL) {
-                lu_ref_del_at(&dev->ld_reference,
-                              o->lo_dev_ref , "lu_object", o);
-                lu_device_put(dev);
-                o->lo_dev = NULL;
-        }
+       if (dev != NULL) {
+               lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
+                             "lu_object", o);
+               lu_device_put(dev);
+               o->lo_dev = NULL;
+       }
 }
 EXPORT_SYMBOL(lu_object_fini);
 
@@ -1206,7 +1268,7 @@ EXPORT_SYMBOL(lu_object_add);
 int lu_object_header_init(struct lu_object_header *h)
 {
         memset(h, 0, sizeof *h);
-        cfs_atomic_set(&h->loh_ref, 1);
+       atomic_set(&h->loh_ref, 1);
         CFS_INIT_HLIST_NODE(&h->loh_hash);
         CFS_INIT_LIST_HEAD(&h->loh_lru);
         CFS_INIT_LIST_HEAD(&h->loh_layers);
@@ -1320,7 +1382,7 @@ int lu_context_key_register(struct lu_context_key *key)
         for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
                 if (lu_keys[i] == NULL) {
                         key->lct_index = i;
-                        cfs_atomic_set(&key->lct_used, 1);
+                       atomic_set(&key->lct_used, 1);
                         lu_keys[i] = key;
                         lu_ref_init(&key->lct_reference);
                         result = 0;
@@ -1341,16 +1403,16 @@ static void key_fini(struct lu_context *ctx, int index)
                 key = lu_keys[index];
                 LASSERT(key != NULL);
                 LASSERT(key->lct_fini != NULL);
-                LASSERT(cfs_atomic_read(&key->lct_used) > 1);
+               LASSERT(atomic_read(&key->lct_used) > 1);
 
                 key->lct_fini(ctx, key, ctx->lc_value[index]);
                 lu_ref_del(&key->lct_reference, "ctx", ctx);
-                cfs_atomic_dec(&key->lct_used);
+               atomic_dec(&key->lct_used);
 
                LASSERT(key->lct_owner != NULL);
                if ((ctx->lc_tags & LCT_NOREF) == 0) {
-                       LINVRNT(cfs_module_refcount(key->lct_owner) > 0);
-                       cfs_module_put(key->lct_owner);
+                       LINVRNT(module_refcount(key->lct_owner) > 0);
+                       module_put(key->lct_owner);
                }
                ctx->lc_value[index] = NULL;
        }
@@ -1361,7 +1423,7 @@ static void key_fini(struct lu_context *ctx, int index)
  */
 void lu_context_key_degister(struct lu_context_key *key)
 {
-       LASSERT(cfs_atomic_read(&key->lct_used) >= 1);
+       LASSERT(atomic_read(&key->lct_used) >= 1);
        LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
 
        lu_context_key_quiesce(key);
@@ -1375,9 +1437,9 @@ void lu_context_key_degister(struct lu_context_key *key)
        }
        spin_unlock(&lu_keys_guard);
 
-       LASSERTF(cfs_atomic_read(&key->lct_used) == 1,
+       LASSERTF(atomic_read(&key->lct_used) == 1,
                 "key has instances: %d\n",
-                cfs_atomic_read(&key->lct_used));
+                atomic_read(&key->lct_used));
 }
 EXPORT_SYMBOL(lu_context_key_degister);
 
@@ -1555,11 +1617,11 @@ static int keys_fill(struct lu_context *ctx)
                         if (unlikely(IS_ERR(value)))
                                 return PTR_ERR(value);
 
-                        LASSERT(key->lct_owner != NULL);
-                        if (!(ctx->lc_tags & LCT_NOREF))
-                                cfs_try_module_get(key->lct_owner);
-                        lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
-                        cfs_atomic_inc(&key->lct_used);
+                       LASSERT(key->lct_owner != NULL);
+                       if (!(ctx->lc_tags & LCT_NOREF))
+                               try_module_get(key->lct_owner);
+                       lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
+                       atomic_inc(&key->lct_used);
                         /*
                          * This is the only place in the code, where an
                          * element of ctx->lc_value[] array is set to non-NULL
@@ -1781,7 +1843,7 @@ int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
 }
 EXPORT_SYMBOL(lu_env_refill_by_tags);
 
-static struct cfs_shrinker *lu_site_shrinker = NULL;
+static struct shrinker *lu_site_shrinker;
 
 typedef struct lu_site_stats{
         unsigned        lss_populated;
@@ -1933,7 +1995,7 @@ void lu_context_keys_dump(void)
                         CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
                                i, key, key->lct_tags,
                                key->lct_init, key->lct_fini, key->lct_exit,
-                               key->lct_index, cfs_atomic_read(&key->lct_used),
+                              key->lct_index, atomic_read(&key->lct_used),
                                key->lct_owner ? key->lct_owner->name : "",
                                key->lct_owner);
                         lu_ref_print(&key->lct_reference);
@@ -1948,63 +2010,6 @@ static int lu_cache_shrink(int nr, unsigned int gfp_mask)
 }
 #endif /* __KERNEL__ */
 
-int  cl_global_init(void);
-void cl_global_fini(void);
-int  lu_ref_global_init(void);
-void lu_ref_global_fini(void);
-
-int dt_global_init(void);
-void dt_global_fini(void);
-
-int llo_global_init(void);
-void llo_global_fini(void);
-
-/* context key constructor/destructor: lu_ucred_key_init, lu_ucred_key_fini */
-LU_KEY_INIT_FINI(lu_ucred, struct lu_ucred);
-
-static struct lu_context_key lu_ucred_key = {
-       .lct_tags = LCT_SESSION,
-       .lct_init = lu_ucred_key_init,
-       .lct_fini = lu_ucred_key_fini
-};
-
-/**
- * Get ucred key if session exists and ucred key is allocated on it.
- * Return NULL otherwise.
- */
-struct lu_ucred *lu_ucred(const struct lu_env *env)
-{
-       if (!env->le_ses)
-               return NULL;
-       return lu_context_key_get(env->le_ses, &lu_ucred_key);
-}
-EXPORT_SYMBOL(lu_ucred);
-
-/**
- * Get ucred key and check if it is properly initialized.
- * Return NULL otherwise.
- */
-struct lu_ucred *lu_ucred_check(const struct lu_env *env)
-{
-       struct lu_ucred *uc = lu_ucred(env);
-       if (uc && uc->uc_valid != UCRED_OLD && uc->uc_valid != UCRED_NEW)
-               return NULL;
-       return uc;
-}
-EXPORT_SYMBOL(lu_ucred_check);
-
-/**
- * Get ucred key, which must exist and must be properly initialized.
- * Assert otherwise.
- */
-struct lu_ucred *lu_ucred_assert(const struct lu_env *env)
-{
-       struct lu_ucred *uc = lu_ucred_check(env);
-       LASSERT(uc != NULL);
-       return uc;
-}
-EXPORT_SYMBOL(lu_ucred_assert);
-
 /**
  * Initialization of global lu_* data.
  */
@@ -2023,11 +2028,6 @@ int lu_global_init(void)
         if (result != 0)
                 return result;
 
-       LU_CONTEXT_KEY_INIT(&lu_ucred_key);
-       result = lu_context_key_register(&lu_ucred_key);
-       if (result != 0)
-               return result;
-
         /*
          * At this level, we don't know what tags are needed, so allocate them
          * conservatively. This should not be too bad, because this
@@ -2044,21 +2044,10 @@ int lu_global_init(void)
          * inode, one for ea. Unfortunately setting this high value results in
          * lu_object/inode cache consuming all the memory.
          */
-        lu_site_shrinker = cfs_set_shrinker(CFS_DEFAULT_SEEKS, lu_cache_shrink);
+       lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, lu_cache_shrink);
         if (lu_site_shrinker == NULL)
                 return -ENOMEM;
 
-#ifdef __KERNEL__
-       result = dt_global_init();
-       if (result != 0)
-               return result;
-
-       result = llo_global_init();
-       if (result != 0)
-               return result;
-#endif
-        result = cl_global_init();
-
         return result;
 }
 
@@ -2067,18 +2056,12 @@ int lu_global_init(void)
  */
 void lu_global_fini(void)
 {
-        cl_global_fini();
-#ifdef __KERNEL__
-        llo_global_fini();
-        dt_global_fini();
-#endif
         if (lu_site_shrinker != NULL) {
-                cfs_remove_shrinker(lu_site_shrinker);
+               remove_shrinker(lu_site_shrinker);
                 lu_site_shrinker = NULL;
         }
 
-        lu_context_key_degister(&lu_global_key);
-       lu_context_key_degister(&lu_ucred_key);
+       lu_context_key_degister(&lu_global_key);
 
         /*
          * Tear shrinker environment down _after_ de-registering
@@ -2091,12 +2074,6 @@ void lu_global_fini(void)
         lu_ref_global_fini();
 }
 
-struct lu_buf LU_BUF_NULL = {
-        .lb_buf = NULL,
-        .lb_len = 0
-};
-EXPORT_SYMBOL(LU_BUF_NULL);
-
 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
 {
 #ifdef LPROCFS
@@ -2113,6 +2090,28 @@ static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
  * Output site statistical counters into a buffer. Suitable for
  * lprocfs_rd_*()-style functions.
  */
+int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
+{
+       lu_site_stats_t stats;
+
+       memset(&stats, 0, sizeof(stats));
+       lu_site_stats_get(s->ls_obj_hash, &stats, 1);
+
+       return seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
+                         stats.lss_busy,
+                         stats.lss_total,
+                         stats.lss_populated,
+                         CFS_HASH_NHLIST(s->ls_obj_hash),
+                         stats.lss_max_search,
+                         ls_stats_read(s->ls_stats, LU_SS_CREATED),
+                         ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
+                         ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
+                         ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
+                         ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
+                         ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
+}
+EXPORT_SYMBOL(lu_site_stats_seq_print);
+
 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
 {
         lu_site_stats_t stats;
@@ -2144,9 +2143,9 @@ int lu_kmem_init(struct lu_kmem_descr *caches)
         struct lu_kmem_descr *iter = caches;
 
         for (result = 0; iter->ckd_cache != NULL; ++iter) {
-                *iter->ckd_cache = cfs_mem_cache_create(iter->ckd_name,
-                                                        iter->ckd_size,
-                                                        0, 0);
+               *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
+                                                    iter->ckd_size,
+                                                    0, 0, NULL);
                 if (*iter->ckd_cache == NULL) {
                         result = -ENOMEM;
                         /* free all previously allocated caches */
@@ -2164,13 +2163,9 @@ EXPORT_SYMBOL(lu_kmem_init);
  */
 void lu_kmem_fini(struct lu_kmem_descr *caches)
 {
-        int rc;
-
         for (; caches->ckd_cache != NULL; ++caches) {
                 if (*caches->ckd_cache != NULL) {
-                        rc = cfs_mem_cache_destroy(*caches->ckd_cache);
-                        LASSERTF(rc == 0, "couldn't destroy %s slab\n",
-                                 caches->ckd_name);
+                       kmem_cache_destroy(*caches->ckd_cache);
                         *caches->ckd_cache = NULL;
                 }
         }
@@ -2188,7 +2183,7 @@ void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
        struct lu_fid           *old = &o->lo_header->loh_fid;
        struct lu_site_bkt_data *bkt;
        struct lu_object        *shadow;
-       cfs_waitlink_t           waiter;
+       wait_queue_t             waiter;
        cfs_hash_t              *hs;
        cfs_hash_bd_t            bd;
        __u64                    version = 0;
@@ -2199,7 +2194,7 @@ void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
        cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
        shadow = htable_lookup(s, &bd, fid, &waiter, &version);
        /* supposed to be unique */
-       LASSERT(shadow == NULL);
+       LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
        *old = *fid;
        bkt = cfs_hash_bd_extra_get(hs, &bd);
        cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
@@ -2227,6 +2222,12 @@ struct lu_object *lu_object_anon(const struct lu_env *env,
 }
 EXPORT_SYMBOL(lu_object_anon);
 
+struct lu_buf LU_BUF_NULL = {
+       .lb_buf = NULL,
+       .lb_len = 0
+};
+EXPORT_SYMBOL(LU_BUF_NULL);
+
 void lu_buf_free(struct lu_buf *buf)
 {
        LASSERT(buf);
@@ -2256,3 +2257,45 @@ void lu_buf_realloc(struct lu_buf *buf, int size)
        lu_buf_alloc(buf, size);
 }
 EXPORT_SYMBOL(lu_buf_realloc);
+
+struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, int len)
+{
+       if (buf->lb_buf == NULL && buf->lb_len == 0)
+               lu_buf_alloc(buf, len);
+
+       if ((len > buf->lb_len) && (buf->lb_buf != NULL))
+               lu_buf_realloc(buf, len);
+
+       return buf;
+}
+EXPORT_SYMBOL(lu_buf_check_and_alloc);
+
+/**
+ * Increase the size of the \a buf.
+ * preserves old data in buffer
+ * old buffer remains unchanged on error
+ * \retval 0 or -ENOMEM
+ */
+int lu_buf_check_and_grow(struct lu_buf *buf, int len)
+{
+       char *ptr;
+
+       if (len <= buf->lb_len)
+               return 0;
+
+       OBD_ALLOC_LARGE(ptr, len);
+       if (ptr == NULL)
+               return -ENOMEM;
+
+       /* Free the old buf */
+       if (buf->lb_buf != NULL) {
+               memcpy(ptr, buf->lb_buf, buf->lb_len);
+               OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
+       }
+
+       buf->lb_buf = ptr;
+       buf->lb_len = len;
+       return 0;
+}
+EXPORT_SYMBOL(lu_buf_check_and_grow);
+