Whamcloud - gitweb
LU-2675 mdt: add mbo_ prefix to members of struct mdt_body
[fs/lustre-release.git] / lustre / llite / llite_rmtacl.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) 2008, 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/llite/llite_rmtacl.c
37  *
38  * Lustre Remote User Access Control List.
39  *
40  * Author: Fan Yong <fanyong@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44
45 #ifdef CONFIG_FS_POSIX_ACL
46
47 #include <lustre_lite.h>
48 #include <lustre_eacl.h>
49 #include "llite_internal.h"
50
51 static inline __u32 rce_hashfunc(uid_t id)
52 {
53         return id & (RCE_HASHES - 1);
54 }
55
56 static inline __u32 ee_hashfunc(uid_t id)
57 {
58         return id & (EE_HASHES - 1);
59 }
60
61 obd_valid rce_ops2valid(int ops)
62 {
63         switch (ops) {
64         case RMT_LSETFACL:
65                 return OBD_MD_FLRMTLSETFACL;
66         case RMT_LGETFACL:
67                 return OBD_MD_FLRMTLGETFACL;
68         case RMT_RSETFACL:
69                 return OBD_MD_FLRMTRSETFACL;
70         case RMT_RGETFACL:
71                 return OBD_MD_FLRMTRGETFACL;
72         default:
73                 return 0;
74         }
75 }
76
77 static struct rmtacl_ctl_entry *rce_alloc(pid_t key, int ops)
78 {
79         struct rmtacl_ctl_entry *rce;
80
81         OBD_ALLOC_PTR(rce);
82         if (!rce)
83                 return NULL;
84
85         INIT_LIST_HEAD(&rce->rce_list);
86         rce->rce_key = key;
87         rce->rce_ops = ops;
88
89         return rce;
90 }
91
92 static void rce_free(struct rmtacl_ctl_entry *rce)
93 {
94         if (!list_empty(&rce->rce_list))
95                 list_del(&rce->rce_list);
96
97         OBD_FREE_PTR(rce);
98 }
99
100 static struct rmtacl_ctl_entry *__rct_search(struct rmtacl_ctl_table *rct,
101                                            pid_t key)
102 {
103         struct rmtacl_ctl_entry *rce;
104         struct list_head *head = &rct->rct_entries[rce_hashfunc(key)];
105
106         list_for_each_entry(rce, head, rce_list)
107                 if (rce->rce_key == key)
108                         return rce;
109
110         return NULL;
111 }
112
113 struct rmtacl_ctl_entry *rct_search(struct rmtacl_ctl_table *rct, pid_t key)
114 {
115         struct rmtacl_ctl_entry *rce;
116
117         spin_lock(&rct->rct_lock);
118         rce = __rct_search(rct, key);
119         spin_unlock(&rct->rct_lock);
120         return rce;
121 }
122
123 int rct_add(struct rmtacl_ctl_table *rct, pid_t key, int ops)
124 {
125         struct rmtacl_ctl_entry *rce, *e;
126
127         rce = rce_alloc(key, ops);
128         if (rce == NULL)
129                 return -ENOMEM;
130
131         spin_lock(&rct->rct_lock);
132         e = __rct_search(rct, key);
133         if (unlikely(e != NULL)) {
134                 CWARN("Unexpected stale rmtacl_entry found: "
135                       "[key: %d] [ops: %d]\n", (int)key, ops);
136                 rce_free(e);
137         }
138         list_add_tail(&rce->rce_list, &rct->rct_entries[rce_hashfunc(key)]);
139         spin_unlock(&rct->rct_lock);
140
141         return 0;
142 }
143
144 int rct_del(struct rmtacl_ctl_table *rct, pid_t key)
145 {
146         struct rmtacl_ctl_entry *rce;
147
148         spin_lock(&rct->rct_lock);
149         rce = __rct_search(rct, key);
150         if (rce)
151                 rce_free(rce);
152         spin_unlock(&rct->rct_lock);
153
154         return rce ? 0 : -ENOENT;
155 }
156
157 void rct_init(struct rmtacl_ctl_table *rct)
158 {
159         int i;
160
161         spin_lock_init(&rct->rct_lock);
162         for (i = 0; i < RCE_HASHES; i++)
163                 INIT_LIST_HEAD(&rct->rct_entries[i]);
164 }
165
166 void rct_fini(struct rmtacl_ctl_table *rct)
167 {
168         struct rmtacl_ctl_entry *rce;
169         int i;
170
171         spin_lock(&rct->rct_lock);
172         for (i = 0; i < RCE_HASHES; i++)
173                 while (!list_empty(&rct->rct_entries[i])) {
174                         rce = list_entry(rct->rct_entries[i].next,
175                                              struct rmtacl_ctl_entry, rce_list);
176                         rce_free(rce);
177                 }
178         spin_unlock(&rct->rct_lock);
179 }
180
181
182 static struct eacl_entry *ee_alloc(pid_t key, struct lu_fid *fid, int type,
183                                    ext_acl_xattr_header *header)
184 {
185         struct eacl_entry *ee;
186
187         OBD_ALLOC_PTR(ee);
188         if (!ee)
189                 return NULL;
190
191         INIT_LIST_HEAD(&ee->ee_list);
192         ee->ee_key = key;
193         ee->ee_fid = *fid;
194         ee->ee_type = type;
195         ee->ee_acl = header;
196
197         return ee;
198 }
199
200 void ee_free(struct eacl_entry *ee)
201 {
202         if (!list_empty(&ee->ee_list))
203                 list_del(&ee->ee_list);
204
205         if (ee->ee_acl)
206                 lustre_ext_acl_xattr_free(ee->ee_acl);
207
208         OBD_FREE_PTR(ee);
209 }
210
211 static struct eacl_entry *__et_search_del(struct eacl_table *et, pid_t key,
212                                         struct lu_fid *fid, int type)
213 {
214         struct eacl_entry *ee;
215         struct list_head *head = &et->et_entries[ee_hashfunc(key)];
216
217         LASSERT(fid != NULL);
218         list_for_each_entry(ee, head, ee_list)
219                 if (ee->ee_key == key) {
220                         if (lu_fid_eq(&ee->ee_fid, fid) &&
221                             ee->ee_type == type) {
222                                 list_del_init(&ee->ee_list);
223                                 return ee;
224                         }
225                 }
226
227         return NULL;
228 }
229
230 struct eacl_entry *et_search_del(struct eacl_table *et, pid_t key,
231                                  struct lu_fid *fid, int type)
232 {
233         struct eacl_entry *ee;
234
235         spin_lock(&et->et_lock);
236         ee = __et_search_del(et, key, fid, type);
237         spin_unlock(&et->et_lock);
238         return ee;
239 }
240
241 void et_search_free(struct eacl_table *et, pid_t key)
242 {
243         struct eacl_entry *ee, *next;
244         struct list_head *head = &et->et_entries[ee_hashfunc(key)];
245
246         spin_lock(&et->et_lock);
247         list_for_each_entry_safe(ee, next, head, ee_list)
248                 if (ee->ee_key == key)
249                         ee_free(ee);
250
251         spin_unlock(&et->et_lock);
252 }
253
254 int ee_add(struct eacl_table *et, pid_t key, struct lu_fid *fid, int type,
255            ext_acl_xattr_header *header)
256 {
257         struct eacl_entry *ee, *e;
258
259         ee = ee_alloc(key, fid, type, header);
260         if (ee == NULL)
261                 return -ENOMEM;
262
263         spin_lock(&et->et_lock);
264         e = __et_search_del(et, key, fid, type);
265         if (unlikely(e != NULL)) {
266                 CWARN("Unexpected stale eacl_entry found: "
267                       "[key: %d] [fid: "DFID"] [type: %d]\n",
268                       (int)key, PFID(fid), type);
269                 ee_free(e);
270         }
271         list_add_tail(&ee->ee_list, &et->et_entries[ee_hashfunc(key)]);
272         spin_unlock(&et->et_lock);
273
274         return 0;
275 }
276
277 void et_init(struct eacl_table *et)
278 {
279         int i;
280
281         spin_lock_init(&et->et_lock);
282         for (i = 0; i < EE_HASHES; i++)
283                 INIT_LIST_HEAD(&et->et_entries[i]);
284 }
285
286 void et_fini(struct eacl_table *et)
287 {
288         struct eacl_entry *ee;
289         int i;
290
291         spin_lock(&et->et_lock);
292         for (i = 0; i < EE_HASHES; i++)
293                 while (!list_empty(&et->et_entries[i])) {
294                         ee = list_entry(et->et_entries[i].next,
295                                         struct eacl_entry, ee_list);
296                         ee_free(ee);
297                 }
298         spin_unlock(&et->et_lock);
299 }
300
301 #endif