1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/llite/llite_rmtacl.c
5 * Lustre Remote User Access Control List.
6 * Author: Fan Yong <fanyong@clusterfs.com>
8 * Copyright (c) 2004-2007 Cluster File Systems, Inc.
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DEBUG_SUBSYSTEM S_LLITE
28 #ifdef CONFIG_FS_POSIX_ACL
30 #include <lustre_lite.h>
31 #include "llite_internal.h"
33 static inline __u32 rce_hashfunc(uid_t id)
35 return id & (RCE_HASHES - 1);
38 static inline __u32 ee_hashfunc(uid_t id)
40 return id & (EE_HASHES - 1);
43 obd_valid rce_ops2valid(int ops)
47 return OBD_MD_FLRMTLSETFACL;
49 return OBD_MD_FLRMTLGETFACL;
51 return OBD_MD_FLRMTRSETFACL;
53 return OBD_MD_FLRMTRGETFACL;
59 static struct rmtacl_ctl_entry *rce_alloc(pid_t key, int ops)
61 struct rmtacl_ctl_entry *rce;
67 CFS_INIT_LIST_HEAD(&rce->rce_list);
74 static void rce_free(struct rmtacl_ctl_entry *rce)
76 if (!list_empty(&rce->rce_list))
77 list_del(&rce->rce_list);
82 static struct rmtacl_ctl_entry *__rct_search(struct rmtacl_ctl_table *rct,
85 struct rmtacl_ctl_entry *rce;
86 struct list_head *head = &rct->rct_entries[rce_hashfunc(key)];
88 list_for_each_entry(rce, head, rce_list)
89 if (rce->rce_key == key)
95 struct rmtacl_ctl_entry *rct_search(struct rmtacl_ctl_table *rct, pid_t key)
97 struct rmtacl_ctl_entry *rce;
99 spin_lock(&rct->rct_lock);
100 rce = __rct_search(rct, key);
101 spin_unlock(&rct->rct_lock);
105 int rct_add(struct rmtacl_ctl_table *rct, pid_t key, int ops)
107 struct rmtacl_ctl_entry *rce, *e;
109 rce = rce_alloc(key, ops);
113 spin_lock(&rct->rct_lock);
114 e = __rct_search(rct, key);
115 if (unlikely(e != NULL)) {
116 CWARN("Unexpected stale rmtacl_entry found: "
117 "[key: %d] [ops: %d]\n", (int)key, ops);
120 list_add_tail(&rce->rce_list, &rct->rct_entries[rce_hashfunc(key)]);
121 spin_unlock(&rct->rct_lock);
126 int rct_del(struct rmtacl_ctl_table *rct, pid_t key)
128 struct rmtacl_ctl_entry *rce;
130 spin_lock(&rct->rct_lock);
131 rce = __rct_search(rct, key);
134 spin_unlock(&rct->rct_lock);
136 return rce ? 0 : -ENOENT;
139 void rct_init(struct rmtacl_ctl_table *rct)
143 spin_lock_init(&rct->rct_lock);
144 for (i = 0; i < RCE_HASHES; i++)
145 CFS_INIT_LIST_HEAD(&rct->rct_entries[i]);
148 void rct_fini(struct rmtacl_ctl_table *rct)
150 struct rmtacl_ctl_entry *rce;
153 spin_lock(&rct->rct_lock);
154 for (i = 0; i < RCE_HASHES; i++)
155 while (!list_empty(&rct->rct_entries[i])) {
156 rce = list_entry(rct->rct_entries[i].next,
157 struct rmtacl_ctl_entry, rce_list);
160 spin_unlock(&rct->rct_lock);
164 static struct eacl_entry *ee_alloc(pid_t key, struct lu_fid *fid, int type,
165 ext_acl_xattr_header *header)
167 struct eacl_entry *ee;
173 CFS_INIT_LIST_HEAD(&ee->ee_list);
182 void ee_free(struct eacl_entry *ee)
184 if (!list_empty(&ee->ee_list))
185 list_del(&ee->ee_list);
188 lustre_ext_acl_xattr_free(ee->ee_acl);
193 static struct eacl_entry *__et_search_del(struct eacl_table *et, pid_t key,
194 struct lu_fid *fid, int type)
196 struct eacl_entry *ee;
197 struct list_head *head = &et->et_entries[ee_hashfunc(key)];
199 LASSERT(fid != NULL);
200 list_for_each_entry(ee, head, ee_list)
201 if (ee->ee_key == key) {
202 if (lu_fid_eq(&ee->ee_fid, fid) &&
203 ee->ee_type == type) {
204 list_del_init(&ee->ee_list);
212 struct eacl_entry *et_search_del(struct eacl_table *et, pid_t key,
213 struct lu_fid *fid, int type)
215 struct eacl_entry *ee;
217 spin_lock(&et->et_lock);
218 ee = __et_search_del(et, key, fid, type);
219 spin_unlock(&et->et_lock);
223 void et_search_free(struct eacl_table *et, pid_t key)
225 struct eacl_entry *ee, *next;
226 struct list_head *head = &et->et_entries[ee_hashfunc(key)];
228 spin_lock(&et->et_lock);
229 list_for_each_entry_safe(ee, next, head, ee_list)
230 if (ee->ee_key == key)
233 spin_unlock(&et->et_lock);
236 int ee_add(struct eacl_table *et, pid_t key, struct lu_fid *fid, int type,
237 ext_acl_xattr_header *header)
239 struct eacl_entry *ee, *e;
241 ee = ee_alloc(key, fid, type, header);
245 spin_lock(&et->et_lock);
246 e = __et_search_del(et, key, fid, type);
247 if (unlikely(e != NULL)) {
248 CWARN("Unexpected stale eacl_entry found: "
249 "[key: %d] [fid: "DFID"] [type: %d]\n",
250 (int)key, PFID(fid), type);
253 list_add_tail(&ee->ee_list, &et->et_entries[ee_hashfunc(key)]);
254 spin_unlock(&et->et_lock);
259 void et_init(struct eacl_table *et)
263 spin_lock_init(&et->et_lock);
264 for (i = 0; i < EE_HASHES; i++)
265 CFS_INIT_LIST_HEAD(&et->et_entries[i]);
268 void et_fini(struct eacl_table *et)
270 struct eacl_entry *ee;
273 spin_lock(&et->et_lock);
274 for (i = 0; i < EE_HASHES; i++)
275 while (!list_empty(&et->et_entries[i])) {
276 ee = list_entry(et->et_entries[i].next,
277 struct eacl_entry, ee_list);
280 spin_unlock(&et->et_lock);