Whamcloud - gitweb
b=16437
[fs/lustre-release.git] / lustre / obdfilter / filter_capa.c
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  *
6  * Author: Lai Siyao <lsy@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_FILTER
25
26 #include <linux/fs.h>
27 #include <linux/version.h>
28 #include <asm/uaccess.h>
29 #include <linux/file.h>
30 #include <linux/kmod.h>
31
32 #include <lustre_fsfilt.h>
33 #include <lustre_capa.h>
34
35 #include "filter_internal.h"
36
37 static inline __u32 filter_ck_keyid(struct filter_capa_key *key)
38 {
39         return key->k_key.lk_keyid;
40 }
41
42 int filter_update_capa_key(struct obd_device *obd, struct lustre_capa_key *new)
43 {
44         struct filter_obd *filter = &obd->u.filter;
45         struct filter_capa_key *k, *keys[2] = { NULL, NULL };
46         int i;
47
48         spin_lock(&capa_lock);
49         list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
50                 if (k->k_key.lk_mdsid != new->lk_mdsid)
51                         continue;
52
53                 if (keys[0]) {
54                         keys[1] = k;
55                         if (filter_ck_keyid(keys[1]) > filter_ck_keyid(keys[0]))
56                                 keys[1] = keys[0], keys[0] = k;
57                 } else {
58                         keys[0] = k;
59                 }
60         }
61         spin_unlock(&capa_lock);
62
63         for (i = 0; i < 2; i++) {
64                 if (!keys[i])
65                         continue;
66                 if (filter_ck_keyid(keys[i]) != new->lk_keyid)
67                         continue;
68                 /* maybe because of recovery or other reasons, MDS sent the
69                  * the old capability key again.
70                  */
71                 spin_lock(&capa_lock);
72                 keys[i]->k_key = *new;
73                 spin_unlock(&capa_lock);
74
75                 RETURN(0);
76         }
77
78         if (keys[1]) {
79                 /* if OSS already have two keys, update the old one */
80                 k = keys[1];
81         } else {
82                 OBD_ALLOC_PTR(k);
83                 if (!k)
84                         RETURN(-ENOMEM);
85                 CFS_INIT_LIST_HEAD(&k->k_list);
86         }
87
88         spin_lock(&capa_lock);
89         k->k_key = *new;
90         if (list_empty(&k->k_list))
91                 list_add(&k->k_list, &filter->fo_capa_keys);
92         spin_unlock(&capa_lock);
93
94         DEBUG_CAPA_KEY(D_SEC, new, "new");
95         RETURN(0);
96 }
97
98 int filter_auth_capa(struct obd_export *exp, struct lu_fid *fid, __u64 mdsid,
99                      struct lustre_capa *capa, __u64 opc)
100 {
101         struct obd_device *obd = exp->exp_obd;
102         struct filter_obd *filter = &obd->u.filter;
103         struct filter_capa_key *k;
104         struct lustre_capa_key key;
105         struct obd_capa *oc;
106         __u8 *hmac;
107         int keys_ready = 0, key_found = 0, rc = 0;
108         ENTRY;
109
110         /* capability is disabled */
111         if (!filter->fo_fl_oss_capa)
112                 RETURN(0);
113
114         if (capa == NULL) {
115                 if (fid)
116                         CERROR("mdsno/fid/opc "LPU64"/"DFID"/"LPX64
117                                ": no capability has been passed\n",
118                                mdsid, PFID(fid), opc);
119                 else
120                         CERROR("mdsno/opc "LPU64"/"LPX64
121                                ": no capability has been passed\n",
122                                mdsid, opc);
123                 RETURN(-EACCES);
124         }
125
126         if (opc == CAPA_OPC_OSS_READ) {
127                 if (!(capa->lc_opc & CAPA_OPC_OSS_RW))
128                         rc = -EACCES;
129         } else if (!capa_opc_supported(capa, opc)) {
130                 rc = -EACCES;
131         }
132         if (rc) {
133                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
134                 RETURN(rc);
135         }
136
137         oc = capa_lookup(filter->fo_capa_hash, capa, 0);
138         if (oc) {
139                 spin_lock(&oc->c_lock);
140                 if (capa_is_expired(oc)) {
141                         DEBUG_CAPA(D_ERROR, capa, "expired");
142                         rc = -ESTALE;
143                 }
144                 spin_unlock(&oc->c_lock);
145
146                 capa_put(oc);
147                 RETURN(rc);
148         }
149
150         spin_lock(&capa_lock);
151         list_for_each_entry(k, &filter->fo_capa_keys, k_list)
152                 if (k->k_key.lk_mdsid == mdsid) {
153                         keys_ready = 1;
154                         if (k->k_key.lk_keyid == capa_keyid(capa)) {
155                                 key = k->k_key;
156                                 key_found = 1;
157                                 break;
158                         }
159                 }
160         spin_unlock(&capa_lock);
161
162         if (!keys_ready) {
163                 CDEBUG(D_SEC, "MDS hasn't propagated capability keys yet, "
164                        "ignore check!\n");
165                 RETURN(0);
166         }
167
168        if (!key_found) {
169                 DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
170                 RETURN(-ESTALE);
171         }
172
173         OBD_ALLOC(hmac, CAPA_HMAC_MAX_LEN);
174         if (hmac == NULL)
175                 RETURN(-ENOMEM);
176
177         rc = capa_hmac(hmac, capa, key.lk_key);
178         if (rc) {
179                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: rc %d", rc);
180                 OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
181                 RETURN(rc);
182         }
183
184         rc = memcmp(hmac, capa->lc_hmac, CAPA_HMAC_MAX_LEN);
185         OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
186         if (rc) {
187                 DEBUG_CAPA_KEY(D_ERROR, &key, "calculate HMAC with ");
188                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
189                 RETURN(-EACCES);
190         }
191
192         /* store in capa hash */
193         oc = capa_add(filter->fo_capa_hash, capa);
194         capa_put(oc);
195         RETURN(0);
196 }
197
198 void filter_free_capa_keys(struct filter_obd *filter)
199 {
200         struct filter_capa_key *key, *n;
201
202         spin_lock(&capa_lock);
203         list_for_each_entry_safe(key, n, &filter->fo_capa_keys, k_list) {
204                 list_del_init(&key->k_list);
205                 OBD_FREE(key, sizeof(*key));
206         }
207         spin_unlock(&capa_lock);
208 }