Whamcloud - gitweb
LU-1866 osd: ancillary work for initial OI scrub
[fs/lustre-release.git] / lustre / ofd / ofd_capa.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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/ofd/ofd_capa.c
37  *
38  * Author: Lai Siyao <laisiyao@whamcloud.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include "ofd_internal.h"
44
45 static inline __u32 ofd_ck_keyid(struct filter_capa_key *key)
46 {
47         return key->k_key.lk_keyid;
48 }
49
50 int ofd_update_capa_key(struct ofd_device *ofd, struct lustre_capa_key *new)
51 {
52         struct obd_device       *obd = ofd_obd(ofd);
53         struct filter_capa_key  *k, *keys[2] = { NULL, NULL };
54         int                      i;
55
56         spin_lock(&capa_lock);
57         cfs_list_for_each_entry(k, &obd->u.filter.fo_capa_keys, k_list) {
58                 if (k->k_key.lk_seq != new->lk_seq)
59                         continue;
60
61                 if (keys[0]) {
62                         keys[1] = k;
63                         if (ofd_ck_keyid(keys[1]) > ofd_ck_keyid(keys[0]))
64                                 keys[1] = keys[0], keys[0] = k;
65                 } else {
66                         keys[0] = k;
67                 }
68         }
69         spin_unlock(&capa_lock);
70
71         for (i = 0; i < 2; i++) {
72                 if (!keys[i])
73                         continue;
74                 if (ofd_ck_keyid(keys[i]) != new->lk_keyid)
75                         continue;
76                 /* maybe because of recovery or other reasons, MDS sent the
77                  * the old capability key again.
78                  */
79                 spin_lock(&capa_lock);
80                 keys[i]->k_key = *new;
81                 spin_unlock(&capa_lock);
82
83                 RETURN(0);
84         }
85
86         if (keys[1]) {
87                 /* if OSS already have two keys, update the old one */
88                 k = keys[1];
89         } else {
90                 OBD_ALLOC_PTR(k);
91                 if (!k)
92                         RETURN(-ENOMEM);
93                 CFS_INIT_LIST_HEAD(&k->k_list);
94         }
95
96         spin_lock(&capa_lock);
97         k->k_key = *new;
98         if (cfs_list_empty(&k->k_list))
99                 cfs_list_add(&k->k_list, &obd->u.filter.fo_capa_keys);
100         spin_unlock(&capa_lock);
101
102         DEBUG_CAPA_KEY(D_SEC, new, "new");
103         RETURN(0);
104 }
105
106 int ofd_auth_capa(struct obd_export *exp, struct lu_fid *fid, obd_seq seq,
107                   struct lustre_capa *capa, __u64 opc)
108 {
109         struct filter_obd       *filter = &exp->exp_obd->u.filter;
110         struct filter_capa_key  *k;
111         struct lustre_capa_key   key;
112         struct obd_capa         *oc;
113         __u8                    *hmac;
114         int                      keys_ready = 0, key_found = 0, rc = 0;
115
116         ENTRY;
117
118         /* skip capa check for llog and obdecho */
119         if (!fid_seq_is_mdt(seq))
120                 RETURN(0);
121
122         /* capability is disabled */
123         if (!filter->fo_fl_oss_capa)
124                 RETURN(0);
125
126         if (!(exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA))
127                 RETURN(0);
128
129         if (capa == NULL) {
130                 if (fid)
131                         CERROR("seq/fid/opc "LPU64"/"DFID"/"LPX64
132                                ": no capability has been passed\n",
133                                seq, PFID(fid), opc);
134                 else
135                         CERROR("seq/opc "LPU64"/"LPX64
136                                ": no capability has been passed\n",
137                                seq, opc);
138                 RETURN(-EACCES);
139         }
140
141         if (opc == CAPA_OPC_OSS_READ) {
142                 if (!(capa->lc_opc & CAPA_OPC_OSS_RW))
143                         rc = -EACCES;
144         } else if (!capa_opc_supported(capa, opc)) {
145                 rc = -EACCES;
146         }
147
148         if (rc) {
149                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
150                 RETURN(rc);
151         }
152
153         oc = capa_lookup(filter->fo_capa_hash, capa, 0);
154         if (oc) {
155                 spin_lock(&oc->c_lock);
156                 if (capa_is_expired(oc)) {
157                         DEBUG_CAPA(D_ERROR, capa, "expired");
158                         rc = -ESTALE;
159                 }
160                 spin_unlock(&oc->c_lock);
161
162                 capa_put(oc);
163                 RETURN(rc);
164         }
165
166         if (capa_is_expired_sec(capa)) {
167                 DEBUG_CAPA(D_ERROR, capa, "expired");
168                 RETURN(-ESTALE);
169         }
170
171         spin_lock(&capa_lock);
172         cfs_list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
173                 if (k->k_key.lk_seq == seq) {
174                         keys_ready = 1;
175                         if (k->k_key.lk_keyid == capa_keyid(capa)) {
176                                 key = k->k_key;
177                                 key_found = 1;
178                                 break;
179                         }
180                 }
181         }
182         spin_unlock(&capa_lock);
183
184         if (!keys_ready) {
185                 CDEBUG(D_SEC, "MDS hasn't propagated capability keys yet, "
186                        "ignore check!\n");
187                 RETURN(0);
188         }
189
190         if (!key_found) {
191                 DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
192                 RETURN(-ESTALE);
193         }
194
195         OBD_ALLOC(hmac, CAPA_HMAC_MAX_LEN);
196         if (hmac == NULL)
197                 RETURN(-ENOMEM);
198
199         rc = capa_hmac(hmac, capa, key.lk_key);
200         if (rc) {
201                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: rc %d", rc);
202                 OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
203                 RETURN(rc);
204         }
205
206         rc = memcmp(hmac, capa->lc_hmac, CAPA_HMAC_MAX_LEN);
207         OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
208         if (rc) {
209                 DEBUG_CAPA_KEY(D_ERROR, &key, "calculate HMAC with ");
210                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
211                 RETURN(-EACCES);
212         }
213
214         /* store in capa hash */
215         oc = capa_add(filter->fo_capa_hash, capa);
216         capa_put(oc);
217         RETURN(0);
218 }
219
220 void ofd_free_capa_keys(struct ofd_device *ofd)
221 {
222         struct obd_device       *obd = ofd_obd(ofd);
223         struct filter_capa_key  *key, *n;
224
225         spin_lock(&capa_lock);
226         cfs_list_for_each_entry_safe(key, n, &obd->u.filter.fo_capa_keys, k_list) {
227                 cfs_list_del_init(&key->k_list);
228                 OBD_FREE_PTR(key);
229         }
230         spin_unlock(&capa_lock);
231 }