blob: 0ae0e4a3e20c4f5602e7046c0d6cd438d7657270 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 File: linux/posix_acl.h
3
4 (C) 2002 Andreas Gruenbacher, <a.gruenbacher@computer.org>
5*/
6
7
8#ifndef __LINUX_POSIX_ACL_H
9#define __LINUX_POSIX_ACL_H
10
11#include <linux/slab.h>
12
13#define ACL_UNDEFINED_ID (-1)
14
15/* a_type field in acl_user_posix_entry_t */
16#define ACL_TYPE_ACCESS (0x8000)
17#define ACL_TYPE_DEFAULT (0x4000)
18
19/* e_tag entry in struct posix_acl_entry */
20#define ACL_USER_OBJ (0x01)
21#define ACL_USER (0x02)
22#define ACL_GROUP_OBJ (0x04)
23#define ACL_GROUP (0x08)
24#define ACL_MASK (0x10)
25#define ACL_OTHER (0x20)
26
27/* permissions in the e_perm field */
28#define ACL_READ (0x04)
29#define ACL_WRITE (0x02)
30#define ACL_EXECUTE (0x01)
31//#define ACL_ADD (0x08)
32//#define ACL_DELETE (0x10)
33
34struct posix_acl_entry {
35 short e_tag;
36 unsigned short e_perm;
37 unsigned int e_id;
38};
39
40struct posix_acl {
41 atomic_t a_refcount;
42 unsigned int a_count;
43 struct posix_acl_entry a_entries[0];
44};
45
46#define FOREACH_ACL_ENTRY(pa, acl, pe) \
47 for(pa=(acl)->a_entries, pe=pa+(acl)->a_count; pa<pe; pa++)
48
49
50/*
51 * Duplicate an ACL handle.
52 */
53static inline struct posix_acl *
54posix_acl_dup(struct posix_acl *acl)
55{
56 if (acl)
57 atomic_inc(&acl->a_refcount);
58 return acl;
59}
60
61/*
62 * Free an ACL handle.
63 */
64static inline void
65posix_acl_release(struct posix_acl *acl)
66{
67 if (acl && atomic_dec_and_test(&acl->a_refcount))
68 kfree(acl);
69}
70
71
72/* posix_acl.c */
73
Chuck Leverf61f6da2011-01-21 03:05:38 +000074extern void posix_acl_init(struct posix_acl *, int);
Al Virodd0fc662005-10-07 07:46:04 +010075extern struct posix_acl *posix_acl_alloc(int, gfp_t);
76extern struct posix_acl *posix_acl_clone(const struct posix_acl *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077extern int posix_acl_valid(const struct posix_acl *);
78extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
Al Virodd0fc662005-10-07 07:46:04 +010079extern struct posix_acl *posix_acl_from_mode(mode_t, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080extern int posix_acl_equiv_mode(const struct posix_acl *, mode_t *);
81extern int posix_acl_create_masq(struct posix_acl *, mode_t *);
82extern int posix_acl_chmod_masq(struct posix_acl *, mode_t);
Al Viro826cae22011-07-23 03:10:32 -040083extern int posix_acl_create(struct posix_acl **, gfp_t, mode_t *);
Al Virobc26ab52011-07-23 00:18:02 -040084extern int posix_acl_chmod(struct posix_acl **, gfp_t, mode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86extern struct posix_acl *get_posix_acl(struct inode *, int);
87extern int set_posix_acl(struct inode *, int, struct posix_acl *);
88
Markus Trippelsdorf641cf4a2009-06-24 22:28:52 +020089#ifdef CONFIG_FS_POSIX_ACL
Al Viro073aaa12009-06-09 12:11:54 -040090static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
91{
92 struct posix_acl **p, *acl;
93 switch (type) {
94 case ACL_TYPE_ACCESS:
95 p = &inode->i_acl;
96 break;
97 case ACL_TYPE_DEFAULT:
98 p = &inode->i_default_acl;
99 break;
100 default:
101 return ERR_PTR(-EINVAL);
102 }
103 acl = ACCESS_ONCE(*p);
104 if (acl) {
105 spin_lock(&inode->i_lock);
106 acl = *p;
107 if (acl != ACL_NOT_CACHED)
108 acl = posix_acl_dup(acl);
109 spin_unlock(&inode->i_lock);
110 }
111 return acl;
112}
113
Nick Piggin1e1743e2011-01-07 17:49:59 +1100114static inline int negative_cached_acl(struct inode *inode, int type)
115{
116 struct posix_acl **p, *acl;
117 switch (type) {
118 case ACL_TYPE_ACCESS:
119 p = &inode->i_acl;
120 break;
121 case ACL_TYPE_DEFAULT:
122 p = &inode->i_default_acl;
123 break;
124 default:
125 BUG();
126 }
127 acl = ACCESS_ONCE(*p);
128 if (acl)
129 return 0;
130 return 1;
131}
132
Al Viro073aaa12009-06-09 12:11:54 -0400133static inline void set_cached_acl(struct inode *inode,
134 int type,
135 struct posix_acl *acl)
136{
137 struct posix_acl *old = NULL;
138 spin_lock(&inode->i_lock);
139 switch (type) {
140 case ACL_TYPE_ACCESS:
141 old = inode->i_acl;
142 inode->i_acl = posix_acl_dup(acl);
143 break;
144 case ACL_TYPE_DEFAULT:
145 old = inode->i_default_acl;
146 inode->i_default_acl = posix_acl_dup(acl);
147 break;
148 }
149 spin_unlock(&inode->i_lock);
150 if (old != ACL_NOT_CACHED)
151 posix_acl_release(old);
152}
153
154static inline void forget_cached_acl(struct inode *inode, int type)
155{
156 struct posix_acl *old = NULL;
157 spin_lock(&inode->i_lock);
158 switch (type) {
159 case ACL_TYPE_ACCESS:
160 old = inode->i_acl;
161 inode->i_acl = ACL_NOT_CACHED;
162 break;
163 case ACL_TYPE_DEFAULT:
164 old = inode->i_default_acl;
165 inode->i_default_acl = ACL_NOT_CACHED;
166 break;
167 }
168 spin_unlock(&inode->i_lock);
169 if (old != ACL_NOT_CACHED)
170 posix_acl_release(old);
171}
Steven Whitehouse796bd952009-09-29 12:27:23 +0100172
173static inline void forget_all_cached_acls(struct inode *inode)
174{
175 struct posix_acl *old_access, *old_default;
176 spin_lock(&inode->i_lock);
177 old_access = inode->i_acl;
178 old_default = inode->i_default_acl;
179 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
180 spin_unlock(&inode->i_lock);
181 if (old_access != ACL_NOT_CACHED)
182 posix_acl_release(old_access);
183 if (old_default != ACL_NOT_CACHED)
184 posix_acl_release(old_default);
185}
Markus Trippelsdorf641cf4a2009-06-24 22:28:52 +0200186#endif
Al Viro72c04902009-06-24 16:58:48 -0400187
188static inline void cache_no_acl(struct inode *inode)
189{
190#ifdef CONFIG_FS_POSIX_ACL
191 inode->i_acl = NULL;
192 inode->i_default_acl = NULL;
193#endif
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#endif /* __LINUX_POSIX_ACL_H */