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 #include <linux/rcupdate.h>
13 
14 #define ACL_UNDEFINED_ID	(-1)
15 
16 /* a_type field in acl_user_posix_entry_t */
17 #define ACL_TYPE_ACCESS		(0x8000)
18 #define ACL_TYPE_DEFAULT	(0x4000)
19 
20 /* e_tag entry in struct posix_acl_entry */
21 #define ACL_USER_OBJ		(0x01)
22 #define ACL_USER		(0x02)
23 #define ACL_GROUP_OBJ		(0x04)
24 #define ACL_GROUP		(0x08)
25 #define ACL_MASK		(0x10)
26 #define ACL_OTHER		(0x20)
27 
28 /* permissions in the e_perm field */
29 #define ACL_READ		(0x04)
30 #define ACL_WRITE		(0x02)
31 #define ACL_EXECUTE		(0x01)
32 //#define ACL_ADD		(0x08)
33 //#define ACL_DELETE		(0x10)
34 
35 struct posix_acl_entry {
36 	short			e_tag;
37 	unsigned short		e_perm;
38 	unsigned int		e_id;
39 };
40 
41 struct posix_acl {
42 	union {
43 		atomic_t		a_refcount;
44 		struct rcu_head		a_rcu;
45 	};
46 	unsigned int		a_count;
47 	struct posix_acl_entry	a_entries[0];
48 };
49 
50 #define FOREACH_ACL_ENTRY(pa, acl, pe) \
51 	for(pa=(acl)->a_entries, pe=pa+(acl)->a_count; pa<pe; pa++)
52 
53 
54 /*
55  * Duplicate an ACL handle.
56  */
57 static inline struct posix_acl *
posix_acl_dup(struct posix_acl * acl)58 posix_acl_dup(struct posix_acl *acl)
59 {
60 	if (acl)
61 		atomic_inc(&acl->a_refcount);
62 	return acl;
63 }
64 
65 /*
66  * Free an ACL handle.
67  */
68 static inline void
posix_acl_release(struct posix_acl * acl)69 posix_acl_release(struct posix_acl *acl)
70 {
71 	if (acl && atomic_dec_and_test(&acl->a_refcount))
72 		kfree_rcu(acl, a_rcu);
73 }
74 
75 
76 /* posix_acl.c */
77 
78 extern void posix_acl_init(struct posix_acl *, int);
79 extern struct posix_acl *posix_acl_alloc(int, gfp_t);
80 extern int posix_acl_valid(const struct posix_acl *);
81 extern int posix_acl_permission(struct inode *, const struct posix_acl *, int);
82 extern struct posix_acl *posix_acl_from_mode(umode_t, gfp_t);
83 extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *);
84 extern int posix_acl_create(struct posix_acl **, gfp_t, umode_t *);
85 extern int posix_acl_chmod(struct posix_acl **, gfp_t, umode_t);
86 
87 extern struct posix_acl *get_posix_acl(struct inode *, int);
88 extern int set_posix_acl(struct inode *, int, struct posix_acl *);
89 
90 #ifdef CONFIG_FS_POSIX_ACL
acl_by_type(struct inode * inode,int type)91 static inline struct posix_acl **acl_by_type(struct inode *inode, int type)
92 {
93 	switch (type) {
94 	case ACL_TYPE_ACCESS:
95 		return &inode->i_acl;
96 	case ACL_TYPE_DEFAULT:
97 		return &inode->i_default_acl;
98 	default:
99 		BUG();
100 	}
101 }
102 
get_cached_acl(struct inode * inode,int type)103 static inline struct posix_acl *get_cached_acl(struct inode *inode, int type)
104 {
105 	struct posix_acl **p = acl_by_type(inode, type);
106 	struct posix_acl *acl = ACCESS_ONCE(*p);
107 	if (acl) {
108 		spin_lock(&inode->i_lock);
109 		acl = *p;
110 		if (acl != ACL_NOT_CACHED)
111 			acl = posix_acl_dup(acl);
112 		spin_unlock(&inode->i_lock);
113 	}
114 	return acl;
115 }
116 
get_cached_acl_rcu(struct inode * inode,int type)117 static inline struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
118 {
119 	return rcu_dereference(*acl_by_type(inode, type));
120 }
121 
set_cached_acl(struct inode * inode,int type,struct posix_acl * acl)122 static inline void set_cached_acl(struct inode *inode,
123 				  int type,
124 				  struct posix_acl *acl)
125 {
126 	struct posix_acl **p = acl_by_type(inode, type);
127 	struct posix_acl *old;
128 	spin_lock(&inode->i_lock);
129 	old = *p;
130 	rcu_assign_pointer(*p, posix_acl_dup(acl));
131 	spin_unlock(&inode->i_lock);
132 	if (old != ACL_NOT_CACHED)
133 		posix_acl_release(old);
134 }
135 
forget_cached_acl(struct inode * inode,int type)136 static inline void forget_cached_acl(struct inode *inode, int type)
137 {
138 	struct posix_acl **p = acl_by_type(inode, type);
139 	struct posix_acl *old;
140 	spin_lock(&inode->i_lock);
141 	old = *p;
142 	*p = ACL_NOT_CACHED;
143 	spin_unlock(&inode->i_lock);
144 	if (old != ACL_NOT_CACHED)
145 		posix_acl_release(old);
146 }
147 
forget_all_cached_acls(struct inode * inode)148 static inline void forget_all_cached_acls(struct inode *inode)
149 {
150 	struct posix_acl *old_access, *old_default;
151 	spin_lock(&inode->i_lock);
152 	old_access = inode->i_acl;
153 	old_default = inode->i_default_acl;
154 	inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
155 	spin_unlock(&inode->i_lock);
156 	if (old_access != ACL_NOT_CACHED)
157 		posix_acl_release(old_access);
158 	if (old_default != ACL_NOT_CACHED)
159 		posix_acl_release(old_default);
160 }
161 #endif
162 
cache_no_acl(struct inode * inode)163 static inline void cache_no_acl(struct inode *inode)
164 {
165 #ifdef CONFIG_FS_POSIX_ACL
166 	inode->i_acl = NULL;
167 	inode->i_default_acl = NULL;
168 #endif
169 }
170 
171 #endif  /* __LINUX_POSIX_ACL_H */
172