2 * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
4 * This file is released under the GPL.
6 * Generic ACL support for in-memory filesystems.
9 #include <linux/sched.h>
10 #include <linux/gfp.h>
12 #include <linux/generic_acl.h>
13 #include <linux/posix_acl.h>
14 #include <linux/posix_acl_xattr.h>
18 generic_acl_list(struct dentry
*dentry
, char *list
, size_t list_size
,
19 const char *name
, size_t name_len
, int type
)
21 struct posix_acl
*acl
;
25 acl
= get_cached_acl(dentry
->d_inode
, type
);
28 posix_acl_release(acl
);
32 xname
= POSIX_ACL_XATTR_ACCESS
;
34 case ACL_TYPE_DEFAULT
:
35 xname
= POSIX_ACL_XATTR_DEFAULT
;
40 size
= strlen(xname
) + 1;
41 if (list
&& size
<= list_size
)
42 memcpy(list
, xname
, size
);
47 generic_acl_get(struct dentry
*dentry
, const char *name
, void *buffer
,
48 size_t size
, int type
)
50 struct posix_acl
*acl
;
53 if (strcmp(name
, "") != 0)
56 acl
= get_cached_acl(dentry
->d_inode
, type
);
59 error
= posix_acl_to_xattr(acl
, buffer
, size
);
60 posix_acl_release(acl
);
66 generic_acl_set(struct dentry
*dentry
, const char *name
, const void *value
,
67 size_t size
, int flags
, int type
)
69 struct inode
*inode
= dentry
->d_inode
;
70 struct posix_acl
*acl
= NULL
;
73 if (strcmp(name
, "") != 0)
75 if (S_ISLNK(inode
->i_mode
))
77 if (!inode_owner_or_capable(inode
))
80 acl
= posix_acl_from_xattr(value
, size
);
87 error
= posix_acl_valid(acl
);
93 error
= posix_acl_equiv_mode(acl
, &mode
);
97 inode
->i_ctime
= CURRENT_TIME
;
99 posix_acl_release(acl
);
103 case ACL_TYPE_DEFAULT
:
104 if (!S_ISDIR(inode
->i_mode
)) {
111 set_cached_acl(inode
, type
, acl
);
114 posix_acl_release(acl
);
119 * generic_acl_init - Take care of acl inheritance at @inode create time
121 * Files created inside a directory with a default ACL inherit the
122 * directory's default ACL.
125 generic_acl_init(struct inode
*inode
, struct inode
*dir
)
127 struct posix_acl
*acl
= NULL
;
128 mode_t mode
= inode
->i_mode
;
131 inode
->i_mode
= mode
& ~current_umask();
132 if (!S_ISLNK(inode
->i_mode
))
133 acl
= get_cached_acl(dir
, ACL_TYPE_DEFAULT
);
135 struct posix_acl
*clone
;
137 if (S_ISDIR(inode
->i_mode
)) {
138 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
142 set_cached_acl(inode
, ACL_TYPE_DEFAULT
, clone
);
143 posix_acl_release(clone
);
145 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
149 error
= posix_acl_create_masq(clone
, &mode
);
151 inode
->i_mode
= mode
;
153 set_cached_acl(inode
, ACL_TYPE_ACCESS
, clone
);
155 posix_acl_release(clone
);
160 posix_acl_release(acl
);
165 * generic_acl_chmod - change the access acl of @inode upon chmod()
167 * A chmod also changes the permissions of the owner, group/mask, and
171 generic_acl_chmod(struct inode
*inode
)
173 struct posix_acl
*acl
, *clone
;
176 if (S_ISLNK(inode
->i_mode
))
178 acl
= get_cached_acl(inode
, ACL_TYPE_ACCESS
);
180 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
181 posix_acl_release(acl
);
184 error
= posix_acl_chmod_masq(clone
, inode
->i_mode
);
186 set_cached_acl(inode
, ACL_TYPE_ACCESS
, clone
);
187 posix_acl_release(clone
);
193 generic_check_acl(struct inode
*inode
, int mask
)
195 if (mask
& MAY_NOT_BLOCK
) {
196 if (!negative_cached_acl(inode
, ACL_TYPE_ACCESS
))
199 struct posix_acl
*acl
;
201 acl
= get_cached_acl(inode
, ACL_TYPE_ACCESS
);
203 int error
= posix_acl_permission(inode
, acl
, mask
);
204 posix_acl_release(acl
);
211 const struct xattr_handler generic_acl_access_handler
= {
212 .prefix
= POSIX_ACL_XATTR_ACCESS
,
213 .flags
= ACL_TYPE_ACCESS
,
214 .list
= generic_acl_list
,
215 .get
= generic_acl_get
,
216 .set
= generic_acl_set
,
219 const struct xattr_handler generic_acl_default_handler
= {
220 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
221 .flags
= ACL_TYPE_DEFAULT
,
222 .list
= generic_acl_list
,
223 .get
= generic_acl_get
,
224 .set
= generic_acl_set
,