kill boilerplate around posix_acl_chmod_masq()
[pandora-kernel.git] / include / linux / posix_acl.h
1 /*
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
34 struct posix_acl_entry {
35         short                   e_tag;
36         unsigned short          e_perm;
37         unsigned int            e_id;
38 };
39
40 struct 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  */
53 static inline struct posix_acl *
54 posix_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  */
64 static inline void
65 posix_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
74 extern void posix_acl_init(struct posix_acl *, int);
75 extern struct posix_acl *posix_acl_alloc(int, gfp_t);
76 extern struct posix_acl *posix_acl_clone(const struct posix_acl *, gfp_t);
77 extern int posix_acl_valid(const struct posix_acl *);
78 extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
79 extern struct posix_acl *posix_acl_from_mode(mode_t, gfp_t);
80 extern int posix_acl_equiv_mode(const struct posix_acl *, mode_t *);
81 extern int posix_acl_create_masq(struct posix_acl *, mode_t *);
82 extern int posix_acl_chmod_masq(struct posix_acl *, mode_t);
83 extern int posix_acl_chmod(struct posix_acl **, gfp_t, mode_t);
84
85 extern struct posix_acl *get_posix_acl(struct inode *, int);
86 extern int set_posix_acl(struct inode *, int, struct posix_acl *);
87
88 #ifdef CONFIG_FS_POSIX_ACL
89 static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
90 {
91         struct posix_acl **p, *acl;
92         switch (type) {
93         case ACL_TYPE_ACCESS:
94                 p = &inode->i_acl;
95                 break;
96         case ACL_TYPE_DEFAULT:
97                 p = &inode->i_default_acl;
98                 break;
99         default:
100                 return ERR_PTR(-EINVAL);
101         }
102         acl = ACCESS_ONCE(*p);
103         if (acl) {
104                 spin_lock(&inode->i_lock);
105                 acl = *p;
106                 if (acl != ACL_NOT_CACHED)
107                         acl = posix_acl_dup(acl);
108                 spin_unlock(&inode->i_lock);
109         }
110         return acl;
111 }
112
113 static inline int negative_cached_acl(struct inode *inode, int type)
114 {
115         struct posix_acl **p, *acl;
116         switch (type) {
117         case ACL_TYPE_ACCESS:
118                 p = &inode->i_acl;
119                 break;
120         case ACL_TYPE_DEFAULT:
121                 p = &inode->i_default_acl;
122                 break;
123         default:
124                 BUG();
125         }
126         acl = ACCESS_ONCE(*p);
127         if (acl)
128                 return 0;
129         return 1;
130 }
131
132 static inline void set_cached_acl(struct inode *inode,
133                                   int type,
134                                   struct posix_acl *acl)
135 {
136         struct posix_acl *old = NULL;
137         spin_lock(&inode->i_lock);
138         switch (type) {
139         case ACL_TYPE_ACCESS:
140                 old = inode->i_acl;
141                 inode->i_acl = posix_acl_dup(acl);
142                 break;
143         case ACL_TYPE_DEFAULT:
144                 old = inode->i_default_acl;
145                 inode->i_default_acl = posix_acl_dup(acl);
146                 break;
147         }
148         spin_unlock(&inode->i_lock);
149         if (old != ACL_NOT_CACHED)
150                 posix_acl_release(old);
151 }
152
153 static inline void forget_cached_acl(struct inode *inode, int type)
154 {
155         struct posix_acl *old = NULL;
156         spin_lock(&inode->i_lock);
157         switch (type) {
158         case ACL_TYPE_ACCESS:
159                 old = inode->i_acl;
160                 inode->i_acl = ACL_NOT_CACHED;
161                 break;
162         case ACL_TYPE_DEFAULT:
163                 old = inode->i_default_acl;
164                 inode->i_default_acl = ACL_NOT_CACHED;
165                 break;
166         }
167         spin_unlock(&inode->i_lock);
168         if (old != ACL_NOT_CACHED)
169                 posix_acl_release(old);
170 }
171
172 static inline void forget_all_cached_acls(struct inode *inode)
173 {
174         struct posix_acl *old_access, *old_default;
175         spin_lock(&inode->i_lock);
176         old_access = inode->i_acl;
177         old_default = inode->i_default_acl;
178         inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
179         spin_unlock(&inode->i_lock);
180         if (old_access != ACL_NOT_CACHED)
181                 posix_acl_release(old_access);
182         if (old_default != ACL_NOT_CACHED)
183                 posix_acl_release(old_default);
184 }
185 #endif
186
187 static inline void cache_no_acl(struct inode *inode)
188 {
189 #ifdef CONFIG_FS_POSIX_ACL
190         inode->i_acl = NULL;
191         inode->i_default_acl = NULL;
192 #endif
193 }
194
195 #endif  /* __LINUX_POSIX_ACL_H */