Commit | Line | Data |
---|---|---|
f0c8bd16 | 1 | /* |
f0c8bd16 AG |
2 | * (C) 2005 Andreas Gruenbacher <agruen@suse.de> |
3 | * | |
4 | * This file is released under the GPL. | |
1c7c474c CH |
5 | * |
6 | * Generic ACL support for in-memory filesystems. | |
f0c8bd16 AG |
7 | */ |
8 | ||
9 | #include <linux/sched.h> | |
5a0e3ad6 | 10 | #include <linux/gfp.h> |
f0c8bd16 AG |
11 | #include <linux/fs.h> |
12 | #include <linux/generic_acl.h> | |
1c7c474c CH |
13 | #include <linux/posix_acl.h> |
14 | #include <linux/posix_acl_xattr.h> | |
f0c8bd16 | 15 | |
1c7c474c CH |
16 | |
17 | static size_t | |
18 | generic_acl_list(struct dentry *dentry, char *list, size_t list_size, | |
19 | const char *name, size_t name_len, int type) | |
f0c8bd16 AG |
20 | { |
21 | struct posix_acl *acl; | |
1c7c474c | 22 | const char *xname; |
f0c8bd16 AG |
23 | size_t size; |
24 | ||
1c7c474c | 25 | acl = get_cached_acl(dentry->d_inode, type); |
f0c8bd16 AG |
26 | if (!acl) |
27 | return 0; | |
28 | posix_acl_release(acl); | |
29 | ||
1c7c474c CH |
30 | switch (type) { |
31 | case ACL_TYPE_ACCESS: | |
32 | xname = POSIX_ACL_XATTR_ACCESS; | |
33 | break; | |
34 | case ACL_TYPE_DEFAULT: | |
35 | xname = POSIX_ACL_XATTR_DEFAULT; | |
36 | break; | |
37 | default: | |
38 | return 0; | |
f0c8bd16 | 39 | } |
1c7c474c | 40 | size = strlen(xname) + 1; |
f0c8bd16 | 41 | if (list && size <= list_size) |
1c7c474c | 42 | memcpy(list, xname, size); |
f0c8bd16 AG |
43 | return size; |
44 | } | |
45 | ||
1c7c474c CH |
46 | static int |
47 | generic_acl_get(struct dentry *dentry, const char *name, void *buffer, | |
48 | size_t size, int type) | |
f0c8bd16 AG |
49 | { |
50 | struct posix_acl *acl; | |
51 | int error; | |
52 | ||
1c7c474c CH |
53 | if (strcmp(name, "") != 0) |
54 | return -EINVAL; | |
55 | ||
56 | acl = get_cached_acl(dentry->d_inode, type); | |
f0c8bd16 AG |
57 | if (!acl) |
58 | return -ENODATA; | |
5f3a4a28 | 59 | error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); |
f0c8bd16 AG |
60 | posix_acl_release(acl); |
61 | ||
62 | return error; | |
63 | } | |
64 | ||
1c7c474c CH |
65 | static int |
66 | generic_acl_set(struct dentry *dentry, const char *name, const void *value, | |
67 | size_t size, int flags, int type) | |
f0c8bd16 | 68 | { |
1c7c474c | 69 | struct inode *inode = dentry->d_inode; |
f0c8bd16 AG |
70 | struct posix_acl *acl = NULL; |
71 | int error; | |
72 | ||
1c7c474c CH |
73 | if (strcmp(name, "") != 0) |
74 | return -EINVAL; | |
f0c8bd16 AG |
75 | if (S_ISLNK(inode->i_mode)) |
76 | return -EOPNOTSUPP; | |
2e149670 | 77 | if (!inode_owner_or_capable(inode)) |
f0c8bd16 AG |
78 | return -EPERM; |
79 | if (value) { | |
5f3a4a28 | 80 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
f0c8bd16 AG |
81 | if (IS_ERR(acl)) |
82 | return PTR_ERR(acl); | |
83 | } | |
84 | if (acl) { | |
1026a8ce GZ |
85 | struct posix_acl *old_acl; |
86 | ||
f0c8bd16 AG |
87 | error = posix_acl_valid(acl); |
88 | if (error) | |
89 | goto failed; | |
1c7c474c CH |
90 | switch (type) { |
91 | case ACL_TYPE_ACCESS: | |
1026a8ce GZ |
92 | old_acl = acl; |
93 | error = posix_acl_update_mode(inode, &inode->i_mode, | |
94 | &acl); | |
1c7c474c CH |
95 | if (error < 0) |
96 | goto failed; | |
1026a8ce GZ |
97 | if (!acl) |
98 | posix_acl_release(old_acl); | |
dad5eb6d | 99 | inode->i_ctime = CURRENT_TIME; |
1c7c474c CH |
100 | break; |
101 | case ACL_TYPE_DEFAULT: | |
102 | if (!S_ISDIR(inode->i_mode)) { | |
103 | error = -EINVAL; | |
104 | goto failed; | |
105 | } | |
106 | break; | |
f0c8bd16 AG |
107 | } |
108 | } | |
1c7c474c | 109 | set_cached_acl(inode, type, acl); |
f0c8bd16 AG |
110 | error = 0; |
111 | failed: | |
112 | posix_acl_release(acl); | |
113 | return error; | |
114 | } | |
115 | ||
116 | /** | |
117 | * generic_acl_init - Take care of acl inheritance at @inode create time | |
f0c8bd16 AG |
118 | * |
119 | * Files created inside a directory with a default ACL inherit the | |
120 | * directory's default ACL. | |
121 | */ | |
122 | int | |
1c7c474c | 123 | generic_acl_init(struct inode *inode, struct inode *dir) |
f0c8bd16 AG |
124 | { |
125 | struct posix_acl *acl = NULL; | |
f0c8bd16 AG |
126 | int error; |
127 | ||
f0c8bd16 | 128 | if (!S_ISLNK(inode->i_mode)) |
1c7c474c | 129 | acl = get_cached_acl(dir, ACL_TYPE_DEFAULT); |
f0c8bd16 | 130 | if (acl) { |
95203bef AV |
131 | if (S_ISDIR(inode->i_mode)) |
132 | set_cached_acl(inode, ACL_TYPE_DEFAULT, acl); | |
d3fb6120 | 133 | error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode); |
826cae2f AV |
134 | if (error < 0) |
135 | return error; | |
826cae2f AV |
136 | if (error > 0) |
137 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); | |
d3fb6120 AV |
138 | } else { |
139 | inode->i_mode &= ~current_umask(); | |
f0c8bd16 AG |
140 | } |
141 | error = 0; | |
142 | ||
f0c8bd16 AG |
143 | posix_acl_release(acl); |
144 | return error; | |
145 | } | |
146 | ||
147 | /** | |
148 | * generic_acl_chmod - change the access acl of @inode upon chmod() | |
f0c8bd16 AG |
149 | * |
150 | * A chmod also changes the permissions of the owner, group/mask, and | |
151 | * other ACL entries. | |
152 | */ | |
153 | int | |
1c7c474c | 154 | generic_acl_chmod(struct inode *inode) |
f0c8bd16 | 155 | { |
bc26ab5f | 156 | struct posix_acl *acl; |
f0c8bd16 AG |
157 | int error = 0; |
158 | ||
159 | if (S_ISLNK(inode->i_mode)) | |
160 | return -EOPNOTSUPP; | |
1c7c474c | 161 | acl = get_cached_acl(inode, ACL_TYPE_ACCESS); |
f0c8bd16 | 162 | if (acl) { |
bc26ab5f AV |
163 | error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); |
164 | if (error) | |
165 | return error; | |
166 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); | |
f0c8bd16 | 167 | posix_acl_release(acl); |
f0c8bd16 AG |
168 | } |
169 | return error; | |
170 | } | |
1c7c474c | 171 | |
bb435453 | 172 | const struct xattr_handler generic_acl_access_handler = { |
1c7c474c CH |
173 | .prefix = POSIX_ACL_XATTR_ACCESS, |
174 | .flags = ACL_TYPE_ACCESS, | |
175 | .list = generic_acl_list, | |
176 | .get = generic_acl_get, | |
177 | .set = generic_acl_set, | |
178 | }; | |
179 | ||
bb435453 | 180 | const struct xattr_handler generic_acl_default_handler = { |
1c7c474c CH |
181 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
182 | .flags = ACL_TYPE_DEFAULT, | |
183 | .list = generic_acl_list, | |
184 | .get = generic_acl_get, | |
185 | .set = generic_acl_set, | |
186 | }; |