Whamcloud - gitweb
land b_colibri_devel on HEAD:
[fs/lustre-release.git] / lustre / llite / llite_rmtacl.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *   lustre/llite/llite_rmtacl.c
5  *   Lustre Remote User Access Control List.
6  *   Author: Fan Yong <fanyong@clusterfs.com>
7  *
8  * Copyright (c) 2004-2007 Cluster File Systems, Inc.
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
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.
15  *
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.
20  *
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.
24  */
25
26 #define DEBUG_SUBSYSTEM S_LLITE
27
28 #ifdef CONFIG_FS_POSIX_ACL
29
30 #include <lustre_lite.h>
31 #include "llite_internal.h"
32
33 static inline __u32 rce_hashfunc(uid_t id)
34 {
35         return id & (RCE_HASHES - 1);
36 }
37
38 static inline __u32 ee_hashfunc(uid_t id)
39 {
40         return id & (EE_HASHES - 1);
41 }
42
43 obd_valid rce_ops2valid(int ops)
44 {
45         switch (ops) {
46         case RMT_LSETFACL:
47                 return OBD_MD_FLRMTLSETFACL;
48         case RMT_LGETFACL:
49                 return OBD_MD_FLRMTLGETFACL;
50         case RMT_RSETFACL:
51                 return OBD_MD_FLRMTRSETFACL;
52         case RMT_RGETFACL:
53                 return OBD_MD_FLRMTRGETFACL;
54         default:
55                 return 0;
56         }
57 }
58
59 static struct rmtacl_ctl_entry *rce_alloc(pid_t key, int ops)
60 {
61         struct rmtacl_ctl_entry *rce;
62
63         OBD_ALLOC_PTR(rce);
64         if (!rce)
65                 return NULL;
66
67         CFS_INIT_LIST_HEAD(&rce->rce_list);
68         rce->rce_key = key;
69         rce->rce_ops = ops;
70
71         return rce;
72 }
73
74 static void rce_free(struct rmtacl_ctl_entry *rce)
75 {
76         if (!list_empty(&rce->rce_list))
77                 list_del(&rce->rce_list);
78
79         OBD_FREE_PTR(rce);
80 }
81
82 static struct rmtacl_ctl_entry *__rct_search(struct rmtacl_ctl_table *rct,
83                                            pid_t key)
84 {
85         struct rmtacl_ctl_entry *rce;
86         struct list_head *head = &rct->rct_entries[rce_hashfunc(key)];
87
88         list_for_each_entry(rce, head, rce_list)
89                 if (rce->rce_key == key)
90                         return rce;
91
92         return NULL;
93 }
94
95 struct rmtacl_ctl_entry *rct_search(struct rmtacl_ctl_table *rct, pid_t key)
96 {
97         struct rmtacl_ctl_entry *rce;
98
99         spin_lock(&rct->rct_lock);
100         rce = __rct_search(rct, key);
101         spin_unlock(&rct->rct_lock);
102         return rce;
103 }
104
105 int rct_add(struct rmtacl_ctl_table *rct, pid_t key, int ops)
106 {
107         struct rmtacl_ctl_entry *rce, *e;
108
109         rce = rce_alloc(key, ops);
110         if (rce == NULL)
111                 return -ENOMEM;
112
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);
118                 rce_free(e);
119         }
120         list_add_tail(&rce->rce_list, &rct->rct_entries[rce_hashfunc(key)]);
121         spin_unlock(&rct->rct_lock);
122
123         return 0;
124 }
125
126 int rct_del(struct rmtacl_ctl_table *rct, pid_t key)
127 {
128         struct rmtacl_ctl_entry *rce;
129
130         spin_lock(&rct->rct_lock);
131         rce = __rct_search(rct, key);
132         if (rce)
133                 rce_free(rce);
134         spin_unlock(&rct->rct_lock);
135
136         return rce ? 0 : -ENOENT;
137 }
138
139 void rct_init(struct rmtacl_ctl_table *rct)
140 {
141         int i;
142
143         spin_lock_init(&rct->rct_lock);
144         for (i = 0; i < RCE_HASHES; i++)
145                 CFS_INIT_LIST_HEAD(&rct->rct_entries[i]);
146 }
147
148 void rct_fini(struct rmtacl_ctl_table *rct)
149 {
150         struct rmtacl_ctl_entry *rce;
151         int i;
152
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);
158                         rce_free(rce);
159                 }
160         spin_unlock(&rct->rct_lock);
161 }
162
163
164 static struct eacl_entry *ee_alloc(pid_t key, struct lu_fid *fid, int type,
165                                    ext_acl_xattr_header *header)
166 {
167         struct eacl_entry *ee;
168
169         OBD_ALLOC_PTR(ee);
170         if (!ee)
171                 return NULL;
172
173         CFS_INIT_LIST_HEAD(&ee->ee_list);
174         ee->ee_key = key;
175         ee->ee_fid = *fid;
176         ee->ee_type = type;
177         ee->ee_acl = header;
178
179         return ee;
180 }
181
182 void ee_free(struct eacl_entry *ee)
183 {
184         if (!list_empty(&ee->ee_list))
185                 list_del(&ee->ee_list);
186
187         if (ee->ee_acl)
188                 lustre_ext_acl_xattr_free(ee->ee_acl);
189
190         OBD_FREE_PTR(ee);
191 }
192
193 static struct eacl_entry *__et_search_del(struct eacl_table *et, pid_t key,
194                                         struct lu_fid *fid, int type)
195 {
196         struct eacl_entry *ee;
197         struct list_head *head = &et->et_entries[ee_hashfunc(key)];
198
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);
205                                 return ee;
206                         }
207                 }
208
209         return NULL;
210 }
211
212 struct eacl_entry *et_search_del(struct eacl_table *et, pid_t key,
213                                  struct lu_fid *fid, int type)
214 {
215         struct eacl_entry *ee;
216
217         spin_lock(&et->et_lock);
218         ee = __et_search_del(et, key, fid, type);
219         spin_unlock(&et->et_lock);
220         return ee;
221 }
222
223 void et_search_free(struct eacl_table *et, pid_t key)
224 {
225         struct eacl_entry *ee, *next;
226         struct list_head *head = &et->et_entries[ee_hashfunc(key)];
227
228         spin_lock(&et->et_lock);
229         list_for_each_entry_safe(ee, next, head, ee_list)
230                 if (ee->ee_key == key)
231                         ee_free(ee);
232
233         spin_unlock(&et->et_lock);
234 }
235
236 int ee_add(struct eacl_table *et, pid_t key, struct lu_fid *fid, int type,
237            ext_acl_xattr_header *header)
238 {
239         struct eacl_entry *ee, *e;
240
241         ee = ee_alloc(key, fid, type, header);
242         if (ee == NULL)
243                 return -ENOMEM;
244
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);
251                 ee_free(e);
252         }
253         list_add_tail(&ee->ee_list, &et->et_entries[ee_hashfunc(key)]);
254         spin_unlock(&et->et_lock);
255
256         return 0;
257 }
258
259 void et_init(struct eacl_table *et)
260 {
261         int i;
262
263         spin_lock_init(&et->et_lock);
264         for (i = 0; i < EE_HASHES; i++)
265                 CFS_INIT_LIST_HEAD(&et->et_entries[i]);
266 }
267
268 void et_fini(struct eacl_table *et)
269 {
270         struct eacl_entry *ee;
271         int i;
272
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);
278                         ee_free(ee);
279                 }
280         spin_unlock(&et->et_lock);
281 }
282
283 #endif