1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004, 2005 Cluster File Systems, Inc.
6 * Author: Lai Siyao <lsy@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
24 #define DEBUG_SUBSYSTEM S_FILTER
27 #include <linux/version.h>
28 #include <asm/uaccess.h>
29 #include <linux/file.h>
30 #include <linux/kmod.h>
32 #include <linux/lustre_fsfilt.h>
33 #include <linux/lustre_sec.h>
35 #include "filter_internal.h"
37 void filter_free_capa_keys(struct filter_obd *filter)
39 struct filter_capa_key *key, *n;
41 spin_lock(&filter->fo_capa_lock);
42 list_for_each_entry_safe(key, n, &filter->fo_capa_keys, k_list) {
43 list_del_init(&key->k_list);
44 OBD_FREE(key, sizeof(*key));
46 spin_unlock(&filter->fo_capa_lock);
49 int filter_update_capa_key(struct obd_device *obd, struct lustre_capa_key *key)
51 struct filter_obd *filter = &obd->u.filter;
52 struct filter_capa_key *tmp = NULL, *rkey = NULL, *bkey = NULL;
56 spin_lock(&filter->fo_capa_lock);
57 list_for_each_entry(tmp, &filter->fo_capa_keys, k_list) {
58 if (tmp->k_key.lk_mdsid != le32_to_cpu(key->lk_mdsid))
66 spin_unlock(&filter->fo_capa_lock);
68 if (rkey && bkey && capa_key_cmp(&rkey->k_key, &bkey->k_key) < 0) {
77 OBD_ALLOC(tmp, sizeof(*tmp));
79 GOTO(out, rc = -ENOMEM);
82 /* fields in lustre_capa_key are in cpu order */
83 spin_lock(&filter->fo_capa_lock);
84 tmp->k_key.lk_mdsid = le32_to_cpu(key->lk_mdsid);
85 tmp->k_key.lk_keyid = le32_to_cpu(key->lk_keyid);
86 tmp->k_key.lk_expiry = le64_to_cpu(key->lk_expiry);
87 memcpy(&tmp->k_key.lk_key, key->lk_key, sizeof(key->lk_key));
90 list_add_tail(&tmp->k_list, &filter->fo_capa_keys);
91 spin_unlock(&filter->fo_capa_lock);
93 DEBUG_CAPA_KEY(D_INFO, &tmp->k_key, "filter_update_capa_key");
98 int filter_verify_capa(int cmd, struct obd_export *exp, struct inode *inode,
99 struct lustre_capa *capa)
101 struct obd_device *obd = exp->exp_obd;
102 struct filter_obd *filter = &obd->u.filter;
103 struct obd_capa *ocapa;
104 struct lustre_capa tcapa;
105 struct lustre_id fid;
106 struct filter_capa_key *rkey = NULL, *bkey = NULL, *tmp;
107 __u8 hmac_key[CAPA_KEY_LEN];
111 /* capability is disabled */
112 if (filter->fo_capa_stat == 0)
118 if (cmd == OBD_BRW_WRITE && capa->lc_op != MAY_WRITE)
120 if (cmd == OBD_BRW_READ && !(capa->lc_op & (MAY_WRITE | MAY_READ)))
123 if (OBD_FAIL_CHECK(OBD_FAIL_FILTER_VERIFY_CAPA))
126 rc = fsfilt_get_md(obd, inode, &fid, sizeof(fid), EA_SID);
128 CERROR("get fid from object failed! rc:%d\n", rc);
131 if (capa->lc_mdsid != id_group(&fid) ||
132 capa->lc_ino != id_ino(&fid))
136 if (capa_expired(capa))
139 ocapa = capa_get(capa->lc_uid, capa->lc_op, capa->lc_mdsid,
140 capa->lc_ino, FILTER_CAPA, NULL, NULL, NULL);
143 /* fo_capa_lock protects capa too */
144 spin_lock(&filter->fo_capa_lock);
145 if (capa->lc_keyid == ocapa->c_capa.lc_keyid) {
146 rc = memcmp(capa, &ocapa->c_capa, sizeof(*capa));
147 } else if (ocapa->c_bvalid &&
148 capa->lc_keyid == ocapa->c_bkeyid) {
149 rc = memcmp(capa->lc_hmac, ocapa->c_bhmac,
150 sizeof(capa->lc_hmac));
152 /* ocapa is obsolete */
153 capa_put(ocapa, FILTER_CAPA);
154 spin_unlock(&filter->fo_capa_lock);
157 spin_unlock(&filter->fo_capa_lock);
159 capa_put(ocapa, FILTER_CAPA);
160 RETURN(rc ? -EACCES : 0);
164 spin_lock(&filter->fo_capa_lock);
165 list_for_each_entry(tmp, &filter->fo_capa_keys, k_list) {
166 if (tmp->k_key.lk_mdsid == capa->lc_mdsid) {
174 if (rkey && bkey && capa_key_cmp(&rkey->k_key, &bkey->k_key) < 0) {
180 if ((!rkey || rkey->k_key.lk_keyid != capa->lc_keyid) &&
181 (!bkey || bkey->k_key.lk_keyid != capa->lc_keyid)) {
182 spin_unlock(&filter->fo_capa_lock);
183 GOTO(out, rc = -ESTALE);
188 memcpy(&tcapa, capa, sizeof(tcapa));
189 tcapa.lc_keyid = rkey->k_key.lk_keyid;
190 memcpy(hmac_key, rkey->k_key.lk_key, sizeof(hmac_key));
191 spin_unlock(&filter->fo_capa_lock);
193 capa_hmac(filter->fo_capa_hmac, hmac_key, &tcapa);
195 /* store in capa cache */
196 ocapa = capa_get(capa->lc_uid, capa->lc_op, capa->lc_mdsid,
197 capa->lc_ino, FILTER_CAPA, capa, NULL, NULL);
199 GOTO(out, rc = -ENOMEM);
202 spin_lock(&filter->fo_capa_lock);
203 tcapa.lc_keyid = bkey->k_key.lk_keyid;
204 memcpy(hmac_key, bkey->k_key.lk_key, sizeof(hmac_key));
205 ocapa->c_bkeyid = bkey->k_key.lk_keyid;
206 spin_unlock(&filter->fo_capa_lock);
208 capa_hmac(filter->fo_capa_hmac, bkey->k_key.lk_key, &tcapa);
210 spin_lock(&filter->fo_capa_lock);
211 memcpy(ocapa->c_bhmac, tcapa.lc_hmac, sizeof(ocapa->c_bhmac));
212 spin_unlock(&filter->fo_capa_lock);