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 *
posix_acl_dup(struct posix_acl * 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
posix_acl_release(struct posix_acl * acl)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 
84 extern struct posix_acl *get_posix_acl(struct inode *, int);
85 extern int set_posix_acl(struct inode *, int, struct posix_acl *);
86 
87 #ifdef CONFIG_FS_POSIX_ACL
get_cached_acl(struct inode * inode,int type)88 static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
89 {
90 	struct posix_acl **p, *acl;
91 	switch (type) {
92 	case ACL_TYPE_ACCESS:
93 		p = &inode->i_acl;
94 		break;
95 	case ACL_TYPE_DEFAULT:
96 		p = &inode->i_default_acl;
97 		break;
98 	default:
99 		return ERR_PTR(-EINVAL);
100 	}
101 	acl = ACCESS_ONCE(*p);
102 	if (acl) {
103 		spin_lock(&inode->i_lock);
104 		acl = *p;
105 		if (acl != ACL_NOT_CACHED)
106 			acl = posix_acl_dup(acl);
107 		spin_unlock(&inode->i_lock);
108 	}
109 	return acl;
110 }
111 
negative_cached_acl(struct inode * inode,int type)112 static inline int negative_cached_acl(struct inode *inode, int type)
113 {
114 	struct posix_acl **p, *acl;
115 	switch (type) {
116 	case ACL_TYPE_ACCESS:
117 		p = &inode->i_acl;
118 		break;
119 	case ACL_TYPE_DEFAULT:
120 		p = &inode->i_default_acl;
121 		break;
122 	default:
123 		BUG();
124 	}
125 	acl = ACCESS_ONCE(*p);
126 	if (acl)
127 		return 0;
128 	return 1;
129 }
130 
set_cached_acl(struct inode * inode,int type,struct posix_acl * acl)131 static inline void set_cached_acl(struct inode *inode,
132 				  int type,
133 				  struct posix_acl *acl)
134 {
135 	struct posix_acl *old = NULL;
136 	spin_lock(&inode->i_lock);
137 	switch (type) {
138 	case ACL_TYPE_ACCESS:
139 		old = inode->i_acl;
140 		inode->i_acl = posix_acl_dup(acl);
141 		break;
142 	case ACL_TYPE_DEFAULT:
143 		old = inode->i_default_acl;
144 		inode->i_default_acl = posix_acl_dup(acl);
145 		break;
146 	}
147 	spin_unlock(&inode->i_lock);
148 	if (old != ACL_NOT_CACHED)
149 		posix_acl_release(old);
150 }
151 
forget_cached_acl(struct inode * inode,int type)152 static inline void forget_cached_acl(struct inode *inode, int type)
153 {
154 	struct posix_acl *old = NULL;
155 	spin_lock(&inode->i_lock);
156 	switch (type) {
157 	case ACL_TYPE_ACCESS:
158 		old = inode->i_acl;
159 		inode->i_acl = ACL_NOT_CACHED;
160 		break;
161 	case ACL_TYPE_DEFAULT:
162 		old = inode->i_default_acl;
163 		inode->i_default_acl = ACL_NOT_CACHED;
164 		break;
165 	}
166 	spin_unlock(&inode->i_lock);
167 	if (old != ACL_NOT_CACHED)
168 		posix_acl_release(old);
169 }
170 
forget_all_cached_acls(struct inode * inode)171 static inline void forget_all_cached_acls(struct inode *inode)
172 {
173 	struct posix_acl *old_access, *old_default;
174 	spin_lock(&inode->i_lock);
175 	old_access = inode->i_acl;
176 	old_default = inode->i_default_acl;
177 	inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
178 	spin_unlock(&inode->i_lock);
179 	if (old_access != ACL_NOT_CACHED)
180 		posix_acl_release(old_access);
181 	if (old_default != ACL_NOT_CACHED)
182 		posix_acl_release(old_default);
183 }
184 #endif
185 
cache_no_acl(struct inode * inode)186 static inline void cache_no_acl(struct inode *inode)
187 {
188 #ifdef CONFIG_FS_POSIX_ACL
189 	inode->i_acl = NULL;
190 	inode->i_default_acl = NULL;
191 #endif
192 }
193 
194 #endif  /* __LINUX_POSIX_ACL_H */
195