Whamcloud - gitweb
LU-2059 llog: MGC to use OSD API for backup logs
[fs/lustre-release.git] / lustre / obdclass / lu_object.c
index 38f29c0..ba45a0d 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);
@@ -424,10 +425,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
 };
 
 /**
@@ -539,7 +540,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);
@@ -548,7 +549,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);
@@ -572,6 +573,31 @@ static struct lu_object *htable_lookup(struct lu_site *s,
         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);
+}
+
 /**
  * Search cache for an object with the fid \a f. If such object is found,
  * return it. Otherwise, create new object, insert it into cache and return
@@ -652,7 +678,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;
 
         /*
@@ -668,7 +694,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);
@@ -714,6 +740,30 @@ struct lu_object *lu_object_find_at(const struct lu_env *env,
 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          *s  = dev->ld_site;
+       cfs_hash_t              *hs = s->ls_obj_hash;
+       cfs_hash_bd_t            bd;
+       struct lu_object        *o;
+
+       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_purge);
+
+/**
  * Find object with given fid, and return its slice belonging to given device.
  */
 struct lu_object *lu_object_find_slice(const struct lu_env *env,
@@ -850,12 +900,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 = num_physpages;
 
 #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. */
@@ -868,7 +918,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) {
                 ;
@@ -1148,16 +1198,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);
 
@@ -1166,16 +1217,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);
 
@@ -1782,7 +1833,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;
@@ -1949,63 +2000,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.
  */
@@ -2024,11 +2018,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
@@ -2045,21 +2034,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;
 }
 
@@ -2068,18 +2046,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
@@ -2139,9 +2111,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 */
@@ -2159,13 +2131,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;
                 }
         }
@@ -2194,7 +2162,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);