Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / include / lustre_capa.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/include/lustre_capa.h
37  *
38  * Author: Lai Siyao <lsy@clusterfs.com>
39  */
40
41 #ifndef __LINUX_CAPA_H_
42 #define __LINUX_CAPA_H_
43
44 /*
45  * capability
46  */
47 #ifdef __KERNEL__
48 #include <linux/crypto.h>
49 #endif
50 #include <lustre/lustre_idl.h>
51
52 #define CAPA_TIMEOUT 1800                /* sec, == 30 min */
53 #define CAPA_KEY_TIMEOUT (24 * 60 * 60)  /* sec, == 1 days */
54
55 struct capa_hmac_alg {
56         const char     *ha_name;
57         int             ha_len;
58         int             ha_keylen;
59 };
60
61 #define DEF_CAPA_HMAC_ALG(name, type, len, keylen)      \
62 [CAPA_HMAC_ALG_ ## type] = {                            \
63         .ha_name         = name,                        \
64         .ha_len          = len,                         \
65         .ha_keylen       = keylen,                      \
66 }
67
68 struct client_capa {
69         struct inode             *inode;      
70         struct list_head          lli_list;     /* link to lli_oss_capas */
71 };
72
73 struct target_capa {
74         struct hlist_node         c_hash;       /* link to capa hash */
75 };
76
77 struct obd_capa {
78         struct list_head          c_list;       /* link to capa_list */
79
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 */
84         int                       c_site;
85
86         union {
87                 struct client_capa      cli;
88                 struct target_capa      tgt;
89         } u;
90 };
91
92 enum {
93         CAPA_SITE_CLIENT = 0,
94         CAPA_SITE_SERVER,
95         CAPA_SITE_MAX
96 };
97
98 static inline __u64 capa_opc(struct lustre_capa *capa)
99 {
100         return capa->lc_opc;
101 }
102
103 static inline __u32 capa_uid(struct lustre_capa *capa)
104 {
105         return capa->lc_uid;
106 }
107
108 static inline struct lu_fid *capa_fid(struct lustre_capa *capa)
109 {
110         return &capa->lc_fid;
111 }
112
113 static inline __u32 capa_keyid(struct lustre_capa *capa)
114 {
115         return capa->lc_keyid;
116 }
117
118 static inline __u64 capa_expiry(struct lustre_capa *capa)
119 {
120         return capa->lc_expiry;
121 }
122
123 static inline __u32 capa_flags(struct lustre_capa *capa)
124 {
125         return capa->lc_flags & 0xffffff;
126 }
127
128 static inline __u32 capa_alg(struct lustre_capa *capa)
129 {
130         __u32 alg = capa->lc_flags;
131
132         return alg >> 24;
133 }
134
135 static inline __u64 capa_key_mdsid(struct lustre_capa_key *key)
136 {
137         return key->lk_mdsid;
138 }
139
140 static inline __u32 capa_key_keyid(struct lustre_capa_key *key)
141 {
142         return key->lk_keyid;
143 }
144
145 #define DEBUG_CAPA(level, c, fmt, args...)                                     \
146 do {                                                                           \
147 CDEBUG(level, fmt " capability@%p uid %u opc "LPX64" fid "DFID" keyid %u "     \
148        "expiry "LPU64" flags %u alg %d\n",                                     \
149        ##args, c, capa_uid(c), capa_opc(c), PFID(capa_fid(c)), capa_keyid(c),  \
150        capa_expiry(c), capa_flags(c), capa_alg(c));                            \
151 } while (0)
152
153 #define DEBUG_CAPA_KEY(level, k, fmt, args...)                                 \
154 do {                                                                           \
155 CDEBUG(level, fmt " capability key@%p mdsid "LPU64" keyid %u\n",               \
156        ##args, k, capa_key_mdsid(k), capa_key_keyid(k));                       \
157 } while (0)
158
159 typedef int (* renew_capa_cb_t)(struct obd_capa *, struct lustre_capa *);
160
161 /* obdclass/capa.c */
162 extern struct list_head capa_list[];
163 extern spinlock_t capa_lock;
164 extern int capa_count[];
165 extern cfs_mem_cache_t *capa_cachep;
166
167 struct hlist_head *init_capa_hash(void);
168 void cleanup_capa_hash(struct hlist_head *hash);
169
170 struct obd_capa *capa_add(struct hlist_head *hash, struct lustre_capa *capa);
171 struct obd_capa *capa_lookup(struct hlist_head *hash, struct lustre_capa *capa,
172                              int alive);
173
174 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key);
175 void capa_cpy(void *dst, struct obd_capa *ocapa);
176
177 char *dump_capa_content(char *buf, char *key, int len);
178
179 static inline struct obd_capa *alloc_capa(int site)
180 {
181 #ifdef __KERNEL__
182         struct obd_capa *ocapa;
183
184         OBD_SLAB_ALLOC(ocapa, capa_cachep, GFP_KERNEL, sizeof(*ocapa));
185         if (ocapa) {
186                 atomic_set(&ocapa->c_refc, 0);
187                 spin_lock_init(&ocapa->c_lock);
188                 CFS_INIT_LIST_HEAD(&ocapa->c_list);
189                 ocapa->c_site = site;
190         }
191         return ocapa;
192 #else
193         return NULL;
194 #endif
195 }
196
197 static inline void free_capa(struct obd_capa *ocapa)
198 {
199 #ifdef __KERNEL__
200         if (atomic_read(&ocapa->c_refc)) {
201                 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc %d for",
202                            atomic_read(&ocapa->c_refc));
203                 LBUG();
204         }
205         OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
206 #else
207 #endif
208 }
209
210 static inline struct obd_capa *capa_get(struct obd_capa *ocapa)
211 {
212         if (!ocapa)
213                 return NULL;
214
215         atomic_inc(&ocapa->c_refc);
216         return ocapa;
217 }
218
219 static inline void capa_put(struct obd_capa *ocapa)
220 {
221         if (!ocapa)
222                 return;
223
224         if (atomic_read(&ocapa->c_refc) == 0) {
225                 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc is 0 for");
226                 LBUG();
227         }
228         atomic_dec(&ocapa->c_refc);
229 }
230
231 static inline int open_flags_to_accmode(int flags)
232 {
233         int mode = flags;
234
235         if ((mode + 1) & O_ACCMODE)
236                 mode++;
237         if (mode & O_TRUNC)
238                 mode |= 2;
239
240         return mode;
241 }
242
243 static inline __u64 capa_open_opc(int mode)
244 {
245         return mode & FMODE_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_READ;
246 }
247
248 static inline void set_capa_expiry(struct obd_capa *ocapa)
249 {
250         cfs_time_t expiry = cfs_time_sub((cfs_time_t)ocapa->c_capa.lc_expiry,
251                                          cfs_time_current_sec());
252         ocapa->c_expiry = cfs_time_add(cfs_time_current(),
253                                        cfs_time_seconds(expiry));
254 }
255
256 static inline int capa_is_expired(struct obd_capa *ocapa)
257 {
258         return cfs_time_beforeq(ocapa->c_expiry, cfs_time_current());
259 }
260
261 static inline int capa_opc_supported(struct lustre_capa *capa, __u64 opc)
262 {
263         return (capa_opc(capa) & opc) == opc;
264 }
265
266 static inline struct lustre_capa *
267 lustre_unpack_capa(struct lustre_msg *msg, unsigned int offset)
268 {
269         struct lustre_capa *capa;
270
271         capa = lustre_swab_buf(msg, offset, sizeof(*capa),
272                                lustre_swab_lustre_capa);
273         if (capa == NULL)
274                 CERROR("bufcount %u, bufsize %u\n",
275                        lustre_msg_bufcount(msg),
276                        (lustre_msg_bufcount(msg) <= offset) ?
277                                 -1 : lustre_msg_buflen(msg, offset));
278
279         return capa;
280 }
281
282 struct filter_capa_key {
283         struct list_head        k_list;
284         struct lustre_capa_key  k_key;
285 };
286
287 #define BYPASS_CAPA (struct lustre_capa *)ERR_PTR(-ENOENT)
288 #endif /* __LINUX_CAPA_H_ */