Whamcloud - gitweb
c7e6732f0b4cedbc5b1d4c624ddc082e9842fff5
[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  * Copyright (C) 2005 Cluster File Systems, Inc.
5  *   Author: Lai Siyao <lsy@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
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.
12  *
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.
17  *
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.
21  *
22  *   Lustre capability support.
23  */
24
25 #ifndef __LINUX_CAPA_H_
26 #define __LINUX_CAPA_H_
27
28 /*
29  * capability
30  */
31 #ifdef __KERNEL__
32 #include <linux/crypto.h>
33 #endif
34 #include <lustre/lustre_idl.h>
35
36 #define CAPA_TIMEOUT 1800                /* sec, == 30 min */
37 #define CAPA_KEY_TIMEOUT (24 * 60 * 60)  /* sec, == 1 days */
38
39 struct capa_hmac_alg {
40         const char     *ha_name;
41         int             ha_len;
42         int             ha_keylen;
43 };
44
45 #define DEF_CAPA_HMAC_ALG(name, type, len, keylen)      \
46 [CAPA_HMAC_ALG_ ## type] = {                            \
47         .ha_name         = name,                        \
48         .ha_len          = len,                         \
49         .ha_keylen       = keylen,                      \
50 }
51
52 struct client_capa {
53         struct inode             *inode;      
54         struct list_head          lli_list;     /* link to lli_oss_capas */
55 };
56
57 struct target_capa {
58         struct hlist_node         c_hash;       /* link to capa hash */
59 };
60
61 struct obd_capa {
62         struct list_head          c_list;       /* link to capa_list */
63
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 */
68         int                       c_site;
69
70         union {
71                 struct client_capa      cli;
72                 struct target_capa      tgt;
73         } u;
74 };
75
76 enum {
77         CAPA_SITE_CLIENT = 0,
78         CAPA_SITE_SERVER,
79         CAPA_SITE_MAX
80 };
81
82 static inline __u64 capa_opc(struct lustre_capa *capa)
83 {
84         return capa->lc_opc;
85 }
86
87 static inline __u32 capa_uid(struct lustre_capa *capa)
88 {
89         return capa->lc_uid;
90 }
91
92 static inline struct lu_fid *capa_fid(struct lustre_capa *capa)
93 {
94         return &capa->lc_fid;
95 }
96
97 static inline __u32 capa_keyid(struct lustre_capa *capa)
98 {
99         return capa->lc_keyid;
100 }
101
102 static inline __u64 capa_expiry(struct lustre_capa *capa)
103 {
104         return capa->lc_expiry;
105 }
106
107 static inline __u32 capa_flags(struct lustre_capa *capa)
108 {
109         return capa->lc_flags & 0xffffff;
110 }
111
112 static inline __u32 capa_alg(struct lustre_capa *capa)
113 {
114         __u32 alg = capa->lc_flags;
115
116         return alg >> 24;
117 }
118
119 static inline __u64 capa_key_mdsid(struct lustre_capa_key *key)
120 {
121         return key->lk_mdsid;
122 }
123
124 static inline __u32 capa_key_keyid(struct lustre_capa_key *key)
125 {
126         return key->lk_keyid;
127 }
128
129 #define DEBUG_CAPA(level, c, fmt, args...)                                     \
130 do {                                                                           \
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));                            \
135 } while (0)
136
137 #define DEBUG_CAPA_KEY(level, k, fmt, args...)                                 \
138 do {                                                                           \
139 CDEBUG(level, fmt " capability key@%p mdsid "LPU64" keyid %u\n",               \
140        ##args, k, capa_key_mdsid(k), capa_key_keyid(k));                       \
141 } while (0)
142
143 typedef int (* renew_capa_cb_t)(struct obd_capa *, struct lustre_capa *);
144
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;
150
151 struct hlist_head *init_capa_hash(void);
152 void cleanup_capa_hash(struct hlist_head *hash);
153
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,
156                              int alive);
157
158 int capa_hmac(__u8 *hmac, struct lustre_capa *capa, __u8 *key);
159 void capa_cpy(void *dst, struct obd_capa *ocapa);
160
161 char *dump_capa_content(char *buf, char *key, int len);
162
163 static inline struct obd_capa *alloc_capa(int site)
164 {
165 #ifdef __KERNEL__
166         struct obd_capa *ocapa;
167
168         OBD_SLAB_ALLOC(ocapa, capa_cachep, GFP_KERNEL, sizeof(*ocapa));
169         if (ocapa) {
170                 atomic_set(&ocapa->c_refc, 0);
171                 spin_lock_init(&ocapa->c_lock);
172                 CFS_INIT_LIST_HEAD(&ocapa->c_list);
173                 ocapa->c_site = site;
174         }
175         return ocapa;
176 #else
177         return NULL;
178 #endif
179 }
180
181 static inline void free_capa(struct obd_capa *ocapa)
182 {
183 #ifdef __KERNEL__
184         if (atomic_read(&ocapa->c_refc)) {
185                 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc %d for",
186                            atomic_read(&ocapa->c_refc));
187                 LBUG();
188         }
189         OBD_SLAB_FREE(ocapa, capa_cachep, sizeof(*ocapa));
190 #else
191 #endif
192 }
193
194 static inline struct obd_capa *capa_get(struct obd_capa *ocapa)
195 {
196         if (!ocapa)
197                 return NULL;
198
199         atomic_inc(&ocapa->c_refc);
200         return ocapa;
201 }
202
203 static inline void capa_put(struct obd_capa *ocapa)
204 {
205         if (!ocapa)
206                 return;
207
208         if (atomic_read(&ocapa->c_refc) == 0) {
209                 DEBUG_CAPA(D_ERROR, &ocapa->c_capa, "refc is 0 for");
210                 LBUG();
211         }
212         atomic_dec(&ocapa->c_refc);
213 }
214
215 static inline int open_flags_to_accmode(int flags)
216 {
217         int mode = flags;
218
219         if ((mode + 1) & O_ACCMODE)
220                 mode++;
221         if (mode & O_TRUNC)
222                 mode |= 2;
223
224         return mode;
225 }
226
227 static inline __u64 capa_open_opc(int mode)
228 {
229         return mode & FMODE_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_READ;
230 }
231
232 static inline void set_capa_expiry(struct obd_capa *ocapa)
233 {
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));
238 }
239
240 static inline int capa_is_expired(struct obd_capa *ocapa)
241 {
242         return cfs_time_beforeq(ocapa->c_expiry, cfs_time_current());
243 }
244
245 static inline int capa_opc_supported(struct lustre_capa *capa, __u64 opc)
246 {
247         return (capa_opc(capa) & opc) == opc;
248 }
249
250 static inline struct lustre_capa *
251 lustre_unpack_capa(struct lustre_msg *msg, unsigned int offset)
252 {
253         struct lustre_capa *capa;
254
255         capa = lustre_swab_buf(msg, offset, sizeof(*capa),
256                                lustre_swab_lustre_capa);
257         if (capa == NULL)
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));
262
263         return capa;
264 }
265
266 struct filter_capa_key {
267         struct list_head        k_list;
268         struct lustre_capa_key  k_key;
269 };
270
271 #define BYPASS_CAPA (struct lustre_capa *)ERR_PTR(-ENOENT)
272 #endif /* __LINUX_CAPA_H_ */