1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/include/lustre_capa.h
38 * Author: Lai Siyao <lsy@clusterfs.com>
41 #ifndef __LINUX_CAPA_H_
42 #define __LINUX_CAPA_H_
48 #include <linux/crypto.h>
50 #include <lustre/lustre_idl.h>
52 #define CAPA_TIMEOUT 1800 /* sec, == 30 min */
53 #define CAPA_KEY_TIMEOUT (24 * 60 * 60) /* sec, == 1 days */
55 struct capa_hmac_alg {
61 #define DEF_CAPA_HMAC_ALG(name, type, len, keylen) \
62 [CAPA_HMAC_ALG_ ## type] = { \
65 .ha_keylen = keylen, \
70 struct list_head lli_list; /* link to lli_oss_capas */
74 struct hlist_node c_hash; /* link to capa hash */
78 struct list_head c_list; /* link to capa_list */
80 struct lustre_capa c_capa; /* capa */
81 atomic_t c_refc; /* ref count */
82 cfs_time_t c_expiry; /* jiffies */
83 spinlock_t c_lock; /* protect capa content */
87 struct client_capa cli;
88 struct target_capa tgt;
98 static inline struct lu_fid *capa_fid(struct lustre_capa *capa)
100 return &capa->lc_fid;
103 static inline __u64 capa_opc(struct lustre_capa *capa)
108 static inline __u64 capa_uid(struct lustre_capa *capa)
113 static inline __u64 capa_gid(struct lustre_capa *capa)
118 static inline __u32 capa_flags(struct lustre_capa *capa)
120 return capa->lc_flags & 0xffffff;
123 static inline __u32 capa_alg(struct lustre_capa *capa)
125 return (capa->lc_flags >> 24);
128 static inline __u32 capa_keyid(struct lustre_capa *capa)
130 return capa->lc_keyid;
133 static inline __u64 capa_key_mdsid(struct lustre_capa_key *key)
135 return key->lk_mdsid;
138 static inline __u32 capa_key_keyid(struct lustre_capa_key *key)
140 return key->lk_keyid;
143 static inline __u32 capa_timeout(struct lustre_capa *capa)
145 return capa->lc_timeout;
148 static inline __u32 capa_expiry(struct lustre_capa *capa)
150 return capa->lc_expiry;
153 #define DEBUG_CAPA(level, c, fmt, args...) \
155 CDEBUG(level, fmt " capability@%p fid "DFID" opc "LPX64" uid "LPU64" gid " \
156 LPU64" flags %u alg %d keyid %u timeout %u expiry %u\n", \
157 ##args, c, PFID(capa_fid(c)), capa_opc(c), capa_uid(c), capa_gid(c), \
158 capa_flags(c), capa_alg(c), capa_keyid(c), capa_timeout(c), \
162 #define DEBUG_CAPA_KEY(level, k, fmt, args...) \
164 CDEBUG(level, fmt " capability key@%p mdsid "LPU64" keyid %u\n", \
165 ##args, k, capa_key_mdsid(k), capa_key_keyid(k)); \
168 typedef int (* renew_capa_cb_t)(struct obd_capa *, struct lustre_capa *);
170 /* obdclass/capa.c */
171 extern struct list_head capa_list[];
172 extern spinlock_t capa_lock;
173 extern int capa_count[];
174 extern cfs_mem_cache_t *capa_cachep;
176 struct hlist_head *init_capa_hash(void);
177 void cleanup_capa_hash(struct hlist_head *hash);
179 struct obd_capa *capa_add(struct hlist_head *hash, struct lustre_capa *capa);
180 struct obd_capa *capa_lookup(struct hlist_head *hash, struct lustre_capa *capa,
183 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key);
184 int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen);
185 int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen);
186 void capa_cpy(void *dst, struct obd_capa *ocapa);
187 static inline struct obd_capa *alloc_capa(int site)
190 struct obd_capa *ocapa;
192 if (unlikely(site != CAPA_SITE_CLIENT && site != CAPA_SITE_SERVER))
193 return ERR_PTR(-EINVAL);
195 OBD_SLAB_ALLOC_PTR(ocapa, capa_cachep);
196 if (unlikely(!ocapa))
197 return ERR_PTR(-ENOMEM);
199 CFS_INIT_LIST_HEAD(&ocapa->c_list);
200 atomic_set(&ocapa->c_refc, 1);
201 spin_lock_init(&ocapa->c_lock);
202 ocapa->c_site = site;
203 if (ocapa->c_site == CAPA_SITE_CLIENT)
204 CFS_INIT_LIST_HEAD(&ocapa->u.cli.lli_list);
206 CFS_INIT_HLIST_NODE(&ocapa->u.tgt.c_hash);
210 return ERR_PTR(-EOPNOTSUPP);
214 static inline struct obd_capa *capa_get(struct obd_capa *ocapa)
219 atomic_inc(&ocapa->c_refc);
223 static inline void capa_put(struct obd_capa *ocapa)
228 if (atomic_read(&ocapa->c_refc) == 0) {
229 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc is 0 for");
233 if (atomic_dec_and_test(&ocapa->c_refc)) {
234 LASSERT(list_empty(&ocapa->c_list));
235 if (ocapa->c_site == CAPA_SITE_CLIENT) {
236 LASSERT(list_empty(&ocapa->u.cli.lli_list));
238 struct hlist_node *hnode;
240 hnode = &ocapa->u.tgt.c_hash;
241 LASSERT(!hnode->next && !hnode->pprev);
243 OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
247 static inline int open_flags_to_accmode(int flags)
251 if ((mode + 1) & O_ACCMODE)
259 static inline __u64 capa_open_opc(int mode)
261 return mode & FMODE_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_READ;
264 static inline void set_capa_expiry(struct obd_capa *ocapa)
266 cfs_time_t expiry = cfs_time_sub((cfs_time_t)ocapa->c_capa.lc_expiry,
267 cfs_time_current_sec());
268 ocapa->c_expiry = cfs_time_add(cfs_time_current(),
269 cfs_time_seconds(expiry));
272 static inline int capa_is_expired_sec(struct lustre_capa *capa)
274 return (capa->lc_expiry - cfs_time_current_sec() <= 0);
277 static inline int capa_is_expired(struct obd_capa *ocapa)
279 return cfs_time_beforeq(ocapa->c_expiry, cfs_time_current());
282 static inline int capa_opc_supported(struct lustre_capa *capa, __u64 opc)
284 return (capa_opc(capa) & opc) == opc;
287 static inline struct lustre_capa *
288 lustre_unpack_capa(struct lustre_msg *msg, unsigned int offset)
290 struct lustre_capa *capa;
292 capa = lustre_swab_buf(msg, offset, sizeof(*capa),
293 lustre_swab_lustre_capa);
295 CERROR("bufcount %u, bufsize %u\n",
296 lustre_msg_bufcount(msg),
297 (lustre_msg_bufcount(msg) <= offset) ?
298 -1 : lustre_msg_buflen(msg, offset));
303 struct filter_capa_key {
304 struct list_head k_list;
305 struct lustre_capa_key k_key;
314 #define BYPASS_CAPA (struct lustre_capa *)ERR_PTR(-ENOENT)
315 #endif /* __LINUX_CAPA_H_ */