Whamcloud - gitweb
LU-3556 osd-ldiskfs: remove dependency on mdd module
[fs/lustre-release.git] / lustre / obdclass / capa.c
index 74149f7..b81351f 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
  * Author: Lai Siyao<lsy@clusterfs.com>
  */
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
-
 #define DEBUG_SUBSYSTEM S_SEC
 
 #ifdef __KERNEL__
 #define NR_CAPAHASH 32
 #define CAPA_HASH_SIZE 3000              /* for MDS & OSS */
 
-cfs_mem_cache_t *capa_cachep = NULL;
+struct kmem_cache *capa_cachep;
 
 #ifdef __KERNEL__
 /* lock for capa hash/capa_list/fo_capa_keys */
-cfs_spinlock_t capa_lock = CFS_SPIN_LOCK_UNLOCKED;
+DEFINE_SPINLOCK(capa_lock);
 
 cfs_list_t capa_list[CAPA_SITE_MAX];
 
@@ -89,20 +85,21 @@ EXPORT_SYMBOL(capa_count);
 
 cfs_hlist_head_t *init_capa_hash(void)
 {
-        cfs_hlist_head_t *hash;
-        int nr_hash, i;
+       cfs_hlist_head_t *hash;
+       int nr_hash, i;
 
-        OBD_ALLOC(hash, CFS_PAGE_SIZE);
-        if (!hash)
-                return NULL;
+       OBD_ALLOC(hash, PAGE_CACHE_SIZE);
+       if (!hash)
+               return NULL;
 
-        nr_hash = CFS_PAGE_SIZE / sizeof(cfs_hlist_head_t);
-        LASSERT(nr_hash > NR_CAPAHASH);
+       nr_hash = PAGE_CACHE_SIZE / sizeof(cfs_hlist_head_t);
+       LASSERT(nr_hash > NR_CAPAHASH);
 
-        for (i = 0; i < NR_CAPAHASH; i++)
-                CFS_INIT_HLIST_HEAD(hash + i);
-        return hash;
+       for (i = 0; i < NR_CAPAHASH; i++)
+               CFS_INIT_HLIST_HEAD(hash + i);
+       return hash;
 }
+EXPORT_SYMBOL(init_capa_hash);
 
 #ifdef __KERNEL__
 static inline int capa_on_server(struct obd_capa *ocapa)
@@ -122,20 +119,21 @@ static inline void capa_delete(struct obd_capa *ocapa)
 
 void cleanup_capa_hash(cfs_hlist_head_t *hash)
 {
-        int i;
-        cfs_hlist_node_t *pos, *next;
-        struct obd_capa *oc;
-
-        cfs_spin_lock(&capa_lock);
-        for (i = 0; i < NR_CAPAHASH; i++) {
-                cfs_hlist_for_each_entry_safe(oc, pos, next, hash + i,
-                                              u.tgt.c_hash)
-                        capa_delete(oc);
-        }
-        cfs_spin_unlock(&capa_lock);
-
-        OBD_FREE(hash, CFS_PAGE_SIZE);
+       int i;
+       cfs_hlist_node_t *pos, *next;
+       struct obd_capa *oc;
+
+       spin_lock(&capa_lock);
+       for (i = 0; i < NR_CAPAHASH; i++) {
+               cfs_hlist_for_each_entry_safe(oc, pos, next, hash + i,
+                                             u.tgt.c_hash)
+                       capa_delete(oc);
+       }
+       spin_unlock(&capa_lock);
+
+       OBD_FREE(hash, PAGE_CACHE_SIZE);
 }
+EXPORT_SYMBOL(cleanup_capa_hash);
 
 static inline int capa_hashfn(struct lu_fid *fid)
 {
@@ -205,7 +203,7 @@ struct obd_capa *capa_add(cfs_hlist_head_t *hash, struct lustre_capa *capa)
         if (IS_ERR(ocapa))
                 return NULL;
 
-        cfs_spin_lock(&capa_lock);
+       spin_lock(&capa_lock);
         old = find_capa(capa, head, 0);
         if (!old) {
                 ocapa->c_capa = *capa;
@@ -216,32 +214,34 @@ struct obd_capa *capa_add(cfs_hlist_head_t *hash, struct lustre_capa *capa)
                 capa_count[CAPA_SITE_SERVER]++;
                 if (capa_count[CAPA_SITE_SERVER] > CAPA_HASH_SIZE)
                         capa_delete_lru(list);
-                cfs_spin_unlock(&capa_lock);
-                return ocapa;
-        } else {
-                capa_get(old);
-                cfs_spin_unlock(&capa_lock);
-                capa_put(ocapa);
-                return old;
-        }
+               spin_unlock(&capa_lock);
+               return ocapa;
+       } else {
+               capa_get(old);
+               spin_unlock(&capa_lock);
+               capa_put(ocapa);
+               return old;
+       }
 }
+EXPORT_SYMBOL(capa_add);
 
 struct obd_capa *capa_lookup(cfs_hlist_head_t *hash, struct lustre_capa *capa,
-                             int alive)
+                            int alive)
 {
-        struct obd_capa *ocapa;
-
-        cfs_spin_lock(&capa_lock);
-        ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
-        if (ocapa) {
-                cfs_list_move_tail(&ocapa->c_list,
-                                   &capa_list[CAPA_SITE_SERVER]);
-                capa_get(ocapa);
-        }
-        cfs_spin_unlock(&capa_lock);
-
-        return ocapa;
+       struct obd_capa *ocapa;
+
+       spin_lock(&capa_lock);
+       ocapa = find_capa(capa, hash + capa_hashfn(&capa->lc_fid), alive);
+       if (ocapa) {
+               cfs_list_move_tail(&ocapa->c_list,
+                                  &capa_list[CAPA_SITE_SERVER]);
+               capa_get(ocapa);
+       }
+       spin_unlock(&capa_lock);
+
+       return ocapa;
 }
+EXPORT_SYMBOL(capa_lookup);
 
 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
 {
@@ -267,13 +267,14 @@ int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key)
 
         sg_set_page(&sl, virt_to_page(capa),
                     offsetof(struct lustre_capa, lc_hmac),
-                    (unsigned long)(capa) % CFS_PAGE_SIZE);
+                   (unsigned long)(capa) % PAGE_CACHE_SIZE);
 
         ll_crypto_hmac(tfm, key, &keylen, &sl, sl.length, hmac);
         ll_crypto_free_hash(tfm);
 
         return 0;
 }
+EXPORT_SYMBOL(capa_hmac);
 
 int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
 {
@@ -286,13 +287,13 @@ int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
         char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
         ENTRY;
 
-        /* passing "aes" in a variable instead of a constant string keeps gcc
-         * 4.3.2 happy */
-        tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
-        if (tfm == NULL) {
-                CERROR("failed to load transform for aes\n");
-                RETURN(-EFAULT);
-        }
+       /* passing "aes" in a variable instead of a constant string keeps gcc
+        * 4.3.2 happy */
+       tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
+       if (IS_ERR(tfm)) {
+               CERROR("failed to load transform for aes\n");
+               RETURN(PTR_ERR(tfm));
+       }
 
         min = ll_crypto_tfm_alg_min_keysize(tfm);
         if (keylen < min) {
@@ -306,11 +307,11 @@ int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
                 GOTO(out, rc);
         }
 
-        sg_set_page(&sd, virt_to_page(d), 16,
-                    (unsigned long)(d) % CFS_PAGE_SIZE);
+       sg_set_page(&sd, virt_to_page(d), 16,
+                   (unsigned long)(d) % PAGE_CACHE_SIZE);
 
-        sg_set_page(&ss, virt_to_page(s), 16,
-                    (unsigned long)(s) % CFS_PAGE_SIZE);
+       sg_set_page(&ss, virt_to_page(s), 16,
+                   (unsigned long)(s) % PAGE_CACHE_SIZE);
         desc.tfm   = tfm;
         desc.info  = NULL;
         desc.flags = 0;
@@ -326,6 +327,7 @@ out:
         ll_crypto_free_blkcipher(tfm);
         return rc;
 }
+EXPORT_SYMBOL(capa_encrypt_id);
 
 int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
 {
@@ -338,13 +340,13 @@ int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
         char alg[CRYPTO_MAX_ALG_NAME+1] = "aes";
         ENTRY;
 
-        /* passing "aes" in a variable instead of a constant string keeps gcc
-         * 4.3.2 happy */
-        tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
-        if (tfm == NULL) {
-                CERROR("failed to load transform for aes\n");
-                RETURN(-EFAULT);
-        }
+       /* passing "aes" in a variable instead of a constant string keeps gcc
+        * 4.3.2 happy */
+       tfm = ll_crypto_alloc_blkcipher(alg, 0, 0 );
+       if (IS_ERR(tfm)) {
+               CERROR("failed to load transform for aes\n");
+               RETURN(PTR_ERR(tfm));
+       }
 
         min = ll_crypto_tfm_alg_min_keysize(tfm);
         if (keylen < min) {
@@ -358,11 +360,11 @@ int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen)
                 GOTO(out, rc);
         }
 
-        sg_set_page(&sd, virt_to_page(d), 16,
-                    (unsigned long)(d) % CFS_PAGE_SIZE);
+       sg_set_page(&sd, virt_to_page(d), 16,
+                   (unsigned long)(d) % PAGE_CACHE_SIZE);
 
-        sg_set_page(&ss, virt_to_page(s), 16,
-                    (unsigned long)(s) % CFS_PAGE_SIZE);
+       sg_set_page(&ss, virt_to_page(s), 16,
+                   (unsigned long)(s) % PAGE_CACHE_SIZE);
 
         desc.tfm   = tfm;
         desc.info  = NULL;
@@ -379,20 +381,71 @@ out:
         ll_crypto_free_blkcipher(tfm);
         return rc;
 }
+EXPORT_SYMBOL(capa_decrypt_id);
 #endif
 
 void capa_cpy(void *capa, struct obd_capa *ocapa)
 {
-        cfs_spin_lock(&ocapa->c_lock);
-        *(struct lustre_capa *)capa = ocapa->c_capa;
-        cfs_spin_unlock(&ocapa->c_lock);
+       spin_lock(&ocapa->c_lock);
+       *(struct lustre_capa *)capa = ocapa->c_capa;
+       spin_unlock(&ocapa->c_lock);
 }
-
-EXPORT_SYMBOL(init_capa_hash);
-EXPORT_SYMBOL(cleanup_capa_hash);
-EXPORT_SYMBOL(capa_add);
-EXPORT_SYMBOL(capa_lookup);
-EXPORT_SYMBOL(capa_hmac);
-EXPORT_SYMBOL(capa_encrypt_id);
-EXPORT_SYMBOL(capa_decrypt_id);
 EXPORT_SYMBOL(capa_cpy);
+
+void _debug_capa(struct lustre_capa *c,
+                 struct libcfs_debug_msg_data *msgdata,
+                 const char *fmt, ... )
+{
+        va_list args;
+        va_start(args, fmt);
+        libcfs_debug_vmsg2(msgdata, fmt, args,
+                           " capability@%p fid "DFID" opc "LPX64" uid "LPU64
+                           " gid "LPU64" flags %u alg %d keyid %u timeout %u "
+                           "expiry %u\n", c, PFID(capa_fid(c)), capa_opc(c),
+                           capa_uid(c), capa_gid(c), capa_flags(c),
+                           capa_alg(c), capa_keyid(c), capa_timeout(c),
+                           capa_expiry(c));
+        va_end(args);
+}
+EXPORT_SYMBOL(_debug_capa);
+
+/*
+ * context key constructor/destructor:
+ * lu_capainfo_key_init, lu_capainfo_key_fini
+ */
+LU_KEY_INIT_FINI(lu_capainfo, struct lu_capainfo);
+
+struct lu_context_key lu_capainfo_key = {
+       .lct_tags = LCT_SESSION,
+       .lct_init = lu_capainfo_key_init,
+       .lct_fini = lu_capainfo_key_fini
+};
+
+struct lu_capainfo *lu_capainfo_get(const struct lu_env *env)
+{
+       /* NB, in mdt_init0 */
+       if (env->le_ses == NULL)
+               return NULL;
+       return lu_context_key_get(env->le_ses, &lu_capainfo_key);
+}
+EXPORT_SYMBOL(lu_capainfo_get);
+
+/**
+ * Initialization of lu_capainfo_key data.
+ */
+int lu_capainfo_init(void)
+{
+       int rc;
+
+       LU_CONTEXT_KEY_INIT(&lu_capainfo_key);
+       rc = lu_context_key_register(&lu_capainfo_key);
+       return rc;
+}
+
+/**
+ * Dual to lu_capainfo_init().
+ */
+void lu_capainfo_fini(void)
+{
+       lu_context_key_degister(&lu_capainfo_key);
+}