1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc.
5 * Author: Lai Siyao <lsy@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Lustre capability support.
25 #ifndef __LINUX_CAPA_H_
26 #define __LINUX_CAPA_H_
32 #include <linux/crypto.h>
34 #include <lustre/lustre_idl.h>
36 #define CAPA_TIMEOUT 1800 /* sec, == 30 min */
37 #define CAPA_KEY_TIMEOUT (24 * 60 * 60) /* sec, == 1 days */
39 struct capa_hmac_alg {
45 #define DEF_CAPA_HMAC_ALG(name, type, len, keylen) \
46 [CAPA_HMAC_ALG_ ## type] = { \
49 .ha_keylen = keylen, \
54 struct list_head lli_list; /* link to lli_oss_capas */
58 struct hlist_node c_hash; /* link to capa hash */
62 struct list_head c_list; /* link to capa_list */
64 struct lustre_capa c_capa; /* capa */
65 atomic_t c_refc; /* ref count */
66 cfs_time_t c_expiry; /* jiffies */
67 spinlock_t c_lock; /* protect capa content */
71 struct client_capa cli;
72 struct target_capa tgt;
82 static inline __u64 capa_opc(struct lustre_capa *capa)
87 static inline __u32 capa_uid(struct lustre_capa *capa)
92 static inline struct lu_fid *capa_fid(struct lustre_capa *capa)
97 static inline __u32 capa_keyid(struct lustre_capa *capa)
99 return capa->lc_keyid;
102 static inline __u64 capa_expiry(struct lustre_capa *capa)
104 return capa->lc_expiry;
107 static inline __u32 capa_flags(struct lustre_capa *capa)
109 return capa->lc_flags & 0xffffff;
112 static inline __u32 capa_alg(struct lustre_capa *capa)
114 __u32 alg = capa->lc_flags;
119 static inline __u64 capa_key_mdsid(struct lustre_capa_key *key)
121 return key->lk_mdsid;
124 static inline __u32 capa_key_keyid(struct lustre_capa_key *key)
126 return key->lk_keyid;
129 #define DEBUG_CAPA(level, c, fmt, args...) \
131 CDEBUG(level, fmt " capability@%p uid %u opc "LPX64" fid "DFID" keyid %u " \
132 "expiry "LPU64" flags %u alg %d\n", \
133 ##args, c, capa_uid(c), capa_opc(c), PFID(capa_fid(c)), capa_keyid(c), \
134 capa_expiry(c), capa_flags(c), capa_alg(c)); \
137 #define DEBUG_CAPA_KEY(level, k, fmt, args...) \
139 CDEBUG(level, fmt " capability key@%p mdsid "LPU64" keyid %u\n", \
140 ##args, k, capa_key_mdsid(k), capa_key_keyid(k)); \
143 typedef int (* renew_capa_cb_t)(struct obd_capa *, struct lustre_capa *);
145 /* obdclass/capa.c */
146 extern struct list_head capa_list[];
147 extern spinlock_t capa_lock;
148 extern int capa_count[];
149 extern cfs_mem_cache_t *capa_cachep;
151 struct hlist_head *init_capa_hash(void);
152 void cleanup_capa_hash(struct hlist_head *hash);
154 struct obd_capa *capa_add(struct hlist_head *hash, struct lustre_capa *capa);
155 struct obd_capa *capa_lookup(struct hlist_head *hash, struct lustre_capa *capa,
158 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key);
159 void capa_cpy(void *dst, struct obd_capa *ocapa);
161 char *dump_capa_content(char *buf, char *key, int len);
163 static inline struct obd_capa *alloc_capa(int site)
166 struct obd_capa *ocapa;
168 OBD_SLAB_ALLOC(ocapa, capa_cachep, GFP_KERNEL, sizeof(*ocapa));
170 atomic_set(&ocapa->c_refc, 0);
171 spin_lock_init(&ocapa->c_lock);
172 INIT_LIST_HEAD(&ocapa->c_list);
173 ocapa->c_site = site;
181 static inline void free_capa(struct obd_capa *ocapa)
184 if (atomic_read(&ocapa->c_refc)) {
185 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc %d for",
186 atomic_read(&ocapa->c_refc));
189 OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
194 static inline struct obd_capa *capa_get(struct obd_capa *ocapa)
199 atomic_inc(&ocapa->c_refc);
203 static inline void capa_put(struct obd_capa *ocapa)
208 if (atomic_read(&ocapa->c_refc) == 0) {
209 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc is 0 for");
212 atomic_dec(&ocapa->c_refc);
215 static inline int open_flags_to_accmode(int flags)
219 if ((mode + 1) & O_ACCMODE)
227 static inline __u64 capa_open_opc(int mode)
229 return mode & FMODE_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_READ;
232 static inline void set_capa_expiry(struct obd_capa *ocapa)
234 cfs_time_t expiry = cfs_time_sub((cfs_time_t)ocapa->c_capa.lc_expiry,
235 cfs_time_current_sec());
236 ocapa->c_expiry = cfs_time_add(cfs_time_current(),
237 cfs_time_seconds(expiry));
240 static inline int capa_is_expired(struct obd_capa *ocapa)
242 return cfs_time_beforeq(ocapa->c_expiry, cfs_time_current());
245 static inline int capa_opc_supported(struct lustre_capa *capa, __u64 opc)
247 return (capa_opc(capa) & opc) == opc;
250 static inline struct lustre_capa *
251 lustre_unpack_capa(struct lustre_msg *msg, unsigned int offset)
253 struct lustre_capa *capa;
255 capa = lustre_swab_buf(msg, offset, sizeof(*capa),
256 lustre_swab_lustre_capa);
258 CERROR("bufcount %u, bufsize %u\n",
259 lustre_msg_bufcount(msg),
260 (lustre_msg_bufcount(msg) <= offset) ?
261 -1 : lustre_msg_buflen(msg, offset));
266 struct filter_capa_key {
267 struct list_head k_list;
268 struct lustre_capa_key k_key;
271 #define BYPASS_CAPA (struct lustre_capa *)ERR_PTR(-ENOENT)
272 #endif /* __LINUX_CAPA_H_ */