Whamcloud - gitweb
b=24188 rehash patch makes oops in quota_search_lqs
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 (c) 2007, 2010, Oracle and/or its affiliates. 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 /** \defgroup capa capa
45  *
46  * @{
47  */
48
49 /*
50  * capability
51  */
52 #ifdef __KERNEL__
53 #include <linux/crypto.h>
54 #endif
55 #include <lustre/lustre_idl.h>
56
57 #define CAPA_TIMEOUT 1800                /* sec, == 30 min */
58 #define CAPA_KEY_TIMEOUT (24 * 60 * 60)  /* sec, == 1 days */
59
60 struct capa_hmac_alg {
61         const char     *ha_name;
62         int             ha_len;
63         int             ha_keylen;
64 };
65
66 #define DEF_CAPA_HMAC_ALG(name, type, len, keylen)      \
67 [CAPA_HMAC_ALG_ ## type] = {                            \
68         .ha_name         = name,                        \
69         .ha_len          = len,                         \
70         .ha_keylen       = keylen,                      \
71 }
72
73 struct client_capa {
74         struct inode             *inode;
75         cfs_list_t                lli_list;     /* link to lli_oss_capas */
76 };
77
78 struct target_capa {
79         cfs_hlist_node_t          c_hash;       /* link to capa hash */
80 };
81
82 struct obd_capa {
83         cfs_list_t                c_list;       /* link to capa_list */
84
85         struct lustre_capa        c_capa;       /* capa */
86         cfs_atomic_t              c_refc;       /* ref count */
87         cfs_time_t                c_expiry;     /* jiffies */
88         cfs_spinlock_t            c_lock;       /* protect capa content */
89         int                       c_site;
90
91         union {
92                 struct client_capa      cli;
93                 struct target_capa      tgt;
94         } u;
95 };
96
97 enum {
98         CAPA_SITE_CLIENT = 0,
99         CAPA_SITE_SERVER,
100         CAPA_SITE_MAX
101 };
102
103 static inline struct lu_fid *capa_fid(struct lustre_capa *capa)
104 {
105         return &capa->lc_fid;
106 }
107
108 static inline __u64 capa_opc(struct lustre_capa *capa)
109 {
110         return capa->lc_opc;
111 }
112
113 static inline __u64 capa_uid(struct lustre_capa *capa)
114 {
115         return capa->lc_uid;
116 }
117
118 static inline __u64 capa_gid(struct lustre_capa *capa)
119 {
120         return capa->lc_gid;
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         return (capa->lc_flags >> 24);
131 }
132
133 static inline __u32 capa_keyid(struct lustre_capa *capa)
134 {
135         return capa->lc_keyid;
136 }
137
138 static inline __u64 capa_key_seq(struct lustre_capa_key *key)
139 {
140         return key->lk_seq;
141 }
142
143 static inline __u32 capa_key_keyid(struct lustre_capa_key *key)
144 {
145         return key->lk_keyid;
146 }
147
148 static inline __u32 capa_timeout(struct lustre_capa *capa)
149 {
150         return capa->lc_timeout;
151 }
152
153 static inline __u32 capa_expiry(struct lustre_capa *capa)
154 {
155         return capa->lc_expiry;
156 }
157
158 #define DEBUG_CAPA(level, c, fmt, args...)                                     \
159 do {                                                                           \
160 CDEBUG(level, fmt " capability@%p fid "DFID" opc "LPX64" uid "LPU64" gid "     \
161        LPU64" flags %u alg %d keyid %u timeout %u expiry %u\n",                \
162        ##args, c, PFID(capa_fid(c)), capa_opc(c), capa_uid(c), capa_gid(c),    \
163        capa_flags(c), capa_alg(c), capa_keyid(c), capa_timeout(c),             \
164        capa_expiry(c));                                                        \
165 } while (0)
166
167 #define DEBUG_CAPA_KEY(level, k, fmt, args...)                                 \
168 do {                                                                           \
169 CDEBUG(level, fmt " capability key@%p seq "LPU64" keyid %u\n",                 \
170        ##args, k, capa_key_seq(k), capa_key_keyid(k));                         \
171 } while (0)
172
173 typedef int (* renew_capa_cb_t)(struct obd_capa *, struct lustre_capa *);
174
175 /* obdclass/capa.c */
176 extern cfs_list_t capa_list[];
177 extern cfs_spinlock_t capa_lock;
178 extern int capa_count[];
179 extern cfs_mem_cache_t *capa_cachep;
180
181 cfs_hlist_head_t *init_capa_hash(void);
182 void cleanup_capa_hash(cfs_hlist_head_t *hash);
183
184 struct obd_capa *capa_add(cfs_hlist_head_t *hash,
185                           struct lustre_capa *capa);
186 struct obd_capa *capa_lookup(cfs_hlist_head_t *hash,
187                              struct lustre_capa *capa, int alive);
188
189 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key);
190 int capa_encrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen);
191 int capa_decrypt_id(__u32 *d, __u32 *s, __u8 *key, int keylen);
192 void capa_cpy(void *dst, struct obd_capa *ocapa);
193 static inline struct obd_capa *alloc_capa(int site)
194 {
195 #ifdef __KERNEL__
196         struct obd_capa *ocapa;
197
198         if (unlikely(site != CAPA_SITE_CLIENT && site != CAPA_SITE_SERVER))
199                 return ERR_PTR(-EINVAL);
200
201         OBD_SLAB_ALLOC_PTR(ocapa, capa_cachep);
202         if (unlikely(!ocapa))
203                 return ERR_PTR(-ENOMEM);
204
205         CFS_INIT_LIST_HEAD(&ocapa->c_list);
206         cfs_atomic_set(&ocapa->c_refc, 1);
207         cfs_spin_lock_init(&ocapa->c_lock);
208         ocapa->c_site = site;
209         if (ocapa->c_site == CAPA_SITE_CLIENT)
210                 CFS_INIT_LIST_HEAD(&ocapa->u.cli.lli_list);
211         else
212                 CFS_INIT_HLIST_NODE(&ocapa->u.tgt.c_hash);
213
214         return ocapa;
215 #else
216         return ERR_PTR(-EOPNOTSUPP);
217 #endif
218 }
219
220 static inline struct obd_capa *capa_get(struct obd_capa *ocapa)
221 {
222         if (!ocapa)
223                 return NULL;
224
225         cfs_atomic_inc(&ocapa->c_refc);
226         return ocapa;
227 }
228
229 static inline void capa_put(struct obd_capa *ocapa)
230 {
231         if (!ocapa)
232                 return;
233
234         if (cfs_atomic_read(&ocapa->c_refc) == 0) {
235                 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc is 0 for");
236                 LBUG();
237         }
238
239         if (cfs_atomic_dec_and_test(&ocapa->c_refc)) {
240                 LASSERT(cfs_list_empty(&ocapa->c_list));
241                 if (ocapa->c_site == CAPA_SITE_CLIENT) {
242                         LASSERT(cfs_list_empty(&ocapa->u.cli.lli_list));
243                 } else {
244                         cfs_hlist_node_t *hnode;
245
246                         hnode = &ocapa->u.tgt.c_hash;
247                         LASSERT(!hnode->next && !hnode->pprev);
248                 }
249                 OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
250         }
251 }
252
253 static inline int open_flags_to_accmode(int flags)
254 {
255         int mode = flags;
256
257         if ((mode + 1) & O_ACCMODE)
258                 mode++;
259         if (mode & O_TRUNC)
260                 mode |= 2;
261
262         return mode;
263 }
264
265 static inline __u64 capa_open_opc(int mode)
266 {
267         return mode & FMODE_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_READ;
268 }
269
270 static inline void set_capa_expiry(struct obd_capa *ocapa)
271 {
272         cfs_time_t expiry = cfs_time_sub((cfs_time_t)ocapa->c_capa.lc_expiry,
273                                          cfs_time_current_sec());
274         ocapa->c_expiry = cfs_time_add(cfs_time_current(),
275                                        cfs_time_seconds(expiry));
276 }
277
278 static inline int capa_is_expired_sec(struct lustre_capa *capa)
279 {
280         return (capa->lc_expiry - cfs_time_current_sec() <= 0);
281 }
282
283 static inline int capa_is_expired(struct obd_capa *ocapa)
284 {
285         return cfs_time_beforeq(ocapa->c_expiry, cfs_time_current());
286 }
287
288 static inline int capa_opc_supported(struct lustre_capa *capa, __u64 opc)
289 {
290         return (capa_opc(capa) & opc) == opc;
291 }
292
293 struct filter_capa_key {
294         cfs_list_t              k_list;
295         struct lustre_capa_key  k_key;
296 };
297
298 enum {
299         LC_ID_NONE      = 0,
300         LC_ID_PLAIN     = 1,
301         LC_ID_CONVERT   = 2
302 };
303
304 #define BYPASS_CAPA (struct lustre_capa *)ERR_PTR(-ENOENT)
305
306 /** @} capa */
307
308 #endif /* __LINUX_CAPA_H_ */