[PATCH] Time: Let user request precision from current_tick_length()
[linux-2.6/verdex.git] / fs / posix_acl.c
blob6c8dcf7613fd89bf903a95b311af8a1eee2bf8ce
1 /*
2 * linux/fs/posix_acl.c
4 * Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
6 * Fixes from William Schumacher incorporated on 15 March 2001.
7 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
8 */
11 * This file contains generic functions for manipulating
12 * POSIX 1003.1e draft standard 17 ACLs.
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <asm/atomic.h>
18 #include <linux/fs.h>
19 #include <linux/sched.h>
20 #include <linux/posix_acl.h>
21 #include <linux/module.h>
23 #include <linux/errno.h>
25 EXPORT_SYMBOL(posix_acl_alloc);
26 EXPORT_SYMBOL(posix_acl_clone);
27 EXPORT_SYMBOL(posix_acl_valid);
28 EXPORT_SYMBOL(posix_acl_equiv_mode);
29 EXPORT_SYMBOL(posix_acl_from_mode);
30 EXPORT_SYMBOL(posix_acl_create_masq);
31 EXPORT_SYMBOL(posix_acl_chmod_masq);
32 EXPORT_SYMBOL(posix_acl_permission);
35 * Allocate a new ACL with the specified number of entries.
37 struct posix_acl *
38 posix_acl_alloc(int count, gfp_t flags)
40 const size_t size = sizeof(struct posix_acl) +
41 count * sizeof(struct posix_acl_entry);
42 struct posix_acl *acl = kmalloc(size, flags);
43 if (acl) {
44 atomic_set(&acl->a_refcount, 1);
45 acl->a_count = count;
47 return acl;
51 * Clone an ACL.
53 struct posix_acl *
54 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
56 struct posix_acl *clone = NULL;
58 if (acl) {
59 int size = sizeof(struct posix_acl) + acl->a_count *
60 sizeof(struct posix_acl_entry);
61 clone = kmalloc(size, flags);
62 if (clone) {
63 memcpy(clone, acl, size);
64 atomic_set(&clone->a_refcount, 1);
67 return clone;
71 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
73 int
74 posix_acl_valid(const struct posix_acl *acl)
76 const struct posix_acl_entry *pa, *pe;
77 int state = ACL_USER_OBJ;
78 unsigned int id = 0; /* keep gcc happy */
79 int needs_mask = 0;
81 FOREACH_ACL_ENTRY(pa, acl, pe) {
82 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
83 return -EINVAL;
84 switch (pa->e_tag) {
85 case ACL_USER_OBJ:
86 if (state == ACL_USER_OBJ) {
87 id = 0;
88 state = ACL_USER;
89 break;
91 return -EINVAL;
93 case ACL_USER:
94 if (state != ACL_USER)
95 return -EINVAL;
96 if (pa->e_id == ACL_UNDEFINED_ID ||
97 pa->e_id < id)
98 return -EINVAL;
99 id = pa->e_id + 1;
100 needs_mask = 1;
101 break;
103 case ACL_GROUP_OBJ:
104 if (state == ACL_USER) {
105 id = 0;
106 state = ACL_GROUP;
107 break;
109 return -EINVAL;
111 case ACL_GROUP:
112 if (state != ACL_GROUP)
113 return -EINVAL;
114 if (pa->e_id == ACL_UNDEFINED_ID ||
115 pa->e_id < id)
116 return -EINVAL;
117 id = pa->e_id + 1;
118 needs_mask = 1;
119 break;
121 case ACL_MASK:
122 if (state != ACL_GROUP)
123 return -EINVAL;
124 state = ACL_OTHER;
125 break;
127 case ACL_OTHER:
128 if (state == ACL_OTHER ||
129 (state == ACL_GROUP && !needs_mask)) {
130 state = 0;
131 break;
133 return -EINVAL;
135 default:
136 return -EINVAL;
139 if (state == 0)
140 return 0;
141 return -EINVAL;
145 * Returns 0 if the acl can be exactly represented in the traditional
146 * file mode permission bits, or else 1. Returns -E... on error.
149 posix_acl_equiv_mode(const struct posix_acl *acl, mode_t *mode_p)
151 const struct posix_acl_entry *pa, *pe;
152 mode_t mode = 0;
153 int not_equiv = 0;
155 FOREACH_ACL_ENTRY(pa, acl, pe) {
156 switch (pa->e_tag) {
157 case ACL_USER_OBJ:
158 mode |= (pa->e_perm & S_IRWXO) << 6;
159 break;
160 case ACL_GROUP_OBJ:
161 mode |= (pa->e_perm & S_IRWXO) << 3;
162 break;
163 case ACL_OTHER:
164 mode |= pa->e_perm & S_IRWXO;
165 break;
166 case ACL_MASK:
167 mode = (mode & ~S_IRWXG) |
168 ((pa->e_perm & S_IRWXO) << 3);
169 not_equiv = 1;
170 break;
171 case ACL_USER:
172 case ACL_GROUP:
173 not_equiv = 1;
174 break;
175 default:
176 return -EINVAL;
179 if (mode_p)
180 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
181 return not_equiv;
185 * Create an ACL representing the file mode permission bits of an inode.
187 struct posix_acl *
188 posix_acl_from_mode(mode_t mode, gfp_t flags)
190 struct posix_acl *acl = posix_acl_alloc(3, flags);
191 if (!acl)
192 return ERR_PTR(-ENOMEM);
194 acl->a_entries[0].e_tag = ACL_USER_OBJ;
195 acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
196 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
198 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
199 acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
200 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
202 acl->a_entries[2].e_tag = ACL_OTHER;
203 acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
204 acl->a_entries[2].e_perm = (mode & S_IRWXO);
205 return acl;
209 * Return 0 if current is granted want access to the inode
210 * by the acl. Returns -E... otherwise.
213 posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
215 const struct posix_acl_entry *pa, *pe, *mask_obj;
216 int found = 0;
218 FOREACH_ACL_ENTRY(pa, acl, pe) {
219 switch(pa->e_tag) {
220 case ACL_USER_OBJ:
221 /* (May have been checked already) */
222 if (inode->i_uid == current->fsuid)
223 goto check_perm;
224 break;
225 case ACL_USER:
226 if (pa->e_id == current->fsuid)
227 goto mask;
228 break;
229 case ACL_GROUP_OBJ:
230 if (in_group_p(inode->i_gid)) {
231 found = 1;
232 if ((pa->e_perm & want) == want)
233 goto mask;
235 break;
236 case ACL_GROUP:
237 if (in_group_p(pa->e_id)) {
238 found = 1;
239 if ((pa->e_perm & want) == want)
240 goto mask;
242 break;
243 case ACL_MASK:
244 break;
245 case ACL_OTHER:
246 if (found)
247 return -EACCES;
248 else
249 goto check_perm;
250 default:
251 return -EIO;
254 return -EIO;
256 mask:
257 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
258 if (mask_obj->e_tag == ACL_MASK) {
259 if ((pa->e_perm & mask_obj->e_perm & want) == want)
260 return 0;
261 return -EACCES;
265 check_perm:
266 if ((pa->e_perm & want) == want)
267 return 0;
268 return -EACCES;
272 * Modify acl when creating a new inode. The caller must ensure the acl is
273 * only referenced once.
275 * mode_p initially must contain the mode parameter to the open() / creat()
276 * system calls. All permissions that are not granted by the acl are removed.
277 * The permissions in the acl are changed to reflect the mode_p parameter.
280 posix_acl_create_masq(struct posix_acl *acl, mode_t *mode_p)
282 struct posix_acl_entry *pa, *pe;
283 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
284 mode_t mode = *mode_p;
285 int not_equiv = 0;
287 /* assert(atomic_read(acl->a_refcount) == 1); */
289 FOREACH_ACL_ENTRY(pa, acl, pe) {
290 switch(pa->e_tag) {
291 case ACL_USER_OBJ:
292 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
293 mode &= (pa->e_perm << 6) | ~S_IRWXU;
294 break;
296 case ACL_USER:
297 case ACL_GROUP:
298 not_equiv = 1;
299 break;
301 case ACL_GROUP_OBJ:
302 group_obj = pa;
303 break;
305 case ACL_OTHER:
306 pa->e_perm &= mode | ~S_IRWXO;
307 mode &= pa->e_perm | ~S_IRWXO;
308 break;
310 case ACL_MASK:
311 mask_obj = pa;
312 not_equiv = 1;
313 break;
315 default:
316 return -EIO;
320 if (mask_obj) {
321 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
322 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
323 } else {
324 if (!group_obj)
325 return -EIO;
326 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
327 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
330 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
331 return not_equiv;
335 * Modify the ACL for the chmod syscall.
338 posix_acl_chmod_masq(struct posix_acl *acl, mode_t mode)
340 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
341 struct posix_acl_entry *pa, *pe;
343 /* assert(atomic_read(acl->a_refcount) == 1); */
345 FOREACH_ACL_ENTRY(pa, acl, pe) {
346 switch(pa->e_tag) {
347 case ACL_USER_OBJ:
348 pa->e_perm = (mode & S_IRWXU) >> 6;
349 break;
351 case ACL_USER:
352 case ACL_GROUP:
353 break;
355 case ACL_GROUP_OBJ:
356 group_obj = pa;
357 break;
359 case ACL_MASK:
360 mask_obj = pa;
361 break;
363 case ACL_OTHER:
364 pa->e_perm = (mode & S_IRWXO);
365 break;
367 default:
368 return -EIO;
372 if (mask_obj) {
373 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
374 } else {
375 if (!group_obj)
376 return -EIO;
377 group_obj->e_perm = (mode & S_IRWXG) >> 3;
380 return 0;