]>
Commit | Line | Data |
---|---|---|
024b7d6a | 1 | // SPDX-License-Identifier: LGPL-2.1 |
85ff872d AK |
2 | /* |
3 | * Copyright IBM Corporation, 2010 | |
4 | * Author Aneesh Kumar K.V <[email protected]> | |
85ff872d AK |
5 | */ |
6 | ||
7 | #include <linux/module.h> | |
8 | #include <linux/fs.h> | |
9 | #include <net/9p/9p.h> | |
10 | #include <net/9p/client.h> | |
11 | #include <linux/slab.h> | |
22d8dcdf | 12 | #include <linux/sched.h> |
85ff872d AK |
13 | #include <linux/posix_acl_xattr.h> |
14 | #include "xattr.h" | |
15 | #include "acl.h" | |
76381a42 | 16 | #include "v9fs.h" |
5ffc0cb3 | 17 | #include "v9fs_vfs.h" |
0f235cae | 18 | #include "fid.h" |
85ff872d AK |
19 | |
20 | static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name) | |
21 | { | |
22 | ssize_t size; | |
23 | void *value = NULL; | |
009ca389 | 24 | struct posix_acl *acl = NULL; |
85ff872d AK |
25 | |
26 | size = v9fs_fid_xattr_get(fid, name, NULL, 0); | |
27 | if (size > 0) { | |
28 | value = kzalloc(size, GFP_NOFS); | |
29 | if (!value) | |
30 | return ERR_PTR(-ENOMEM); | |
31 | size = v9fs_fid_xattr_get(fid, name, value, size); | |
32 | if (size > 0) { | |
5f3a4a28 | 33 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
85ff872d AK |
34 | if (IS_ERR(acl)) |
35 | goto err_out; | |
36 | } | |
37 | } else if (size == -ENODATA || size == 0 || | |
38 | size == -ENOSYS || size == -EOPNOTSUPP) { | |
39 | acl = NULL; | |
40 | } else | |
41 | acl = ERR_PTR(-EIO); | |
42 | ||
43 | err_out: | |
44 | kfree(value); | |
45 | return acl; | |
46 | } | |
47 | ||
48 | int v9fs_get_acl(struct inode *inode, struct p9_fid *fid) | |
49 | { | |
50 | int retval = 0; | |
51 | struct posix_acl *pacl, *dacl; | |
76381a42 | 52 | struct v9fs_session_info *v9ses; |
85ff872d | 53 | |
76381a42 | 54 | v9ses = v9fs_inode2v9ses(inode); |
e782ef71 VJJ |
55 | if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) || |
56 | ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) { | |
76381a42 AK |
57 | set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL); |
58 | set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); | |
59 | return 0; | |
60 | } | |
85ff872d | 61 | /* get the default/access acl values and cache them */ |
97d79299 AG |
62 | dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT); |
63 | pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS); | |
85ff872d AK |
64 | |
65 | if (!IS_ERR(dacl) && !IS_ERR(pacl)) { | |
66 | set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); | |
67 | set_cached_acl(inode, ACL_TYPE_ACCESS, pacl); | |
85ff872d AK |
68 | } else |
69 | retval = -EIO; | |
70 | ||
c61fa0d6 VJJ |
71 | if (!IS_ERR(dacl)) |
72 | posix_acl_release(dacl); | |
73 | ||
74 | if (!IS_ERR(pacl)) | |
75 | posix_acl_release(pacl); | |
76 | ||
85ff872d AK |
77 | return retval; |
78 | } | |
79 | ||
80 | static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type) | |
81 | { | |
82 | struct posix_acl *acl; | |
83 | /* | |
84 | * 9p Always cache the acl value when | |
85 | * instantiating the inode (v9fs_inode_from_fid) | |
86 | */ | |
87 | acl = get_cached_acl(inode, type); | |
b8a7a3a6 | 88 | BUG_ON(is_uncached_acl(acl)); |
85ff872d AK |
89 | return acl; |
90 | } | |
91 | ||
0cad6246 | 92 | struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type, bool rcu) |
85ff872d | 93 | { |
76381a42 AK |
94 | struct v9fs_session_info *v9ses; |
95 | ||
0cad6246 MS |
96 | if (rcu) |
97 | return ERR_PTR(-ECHILD); | |
98 | ||
76381a42 | 99 | v9ses = v9fs_inode2v9ses(inode); |
e782ef71 VJJ |
100 | if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) || |
101 | ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) { | |
76381a42 | 102 | /* |
e782ef71 | 103 | * On access = client and acl = on mode get the acl |
76381a42 AK |
104 | * values from the server |
105 | */ | |
4e34e719 | 106 | return NULL; |
76381a42 | 107 | } |
4e34e719 CH |
108 | return v9fs_get_cached_acl(inode, type); |
109 | ||
85ff872d | 110 | } |
7a4566b0 | 111 | |
0f235cae | 112 | static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl) |
6e8dc555 AK |
113 | { |
114 | int retval; | |
115 | char *name; | |
116 | size_t size; | |
117 | void *buffer; | |
6e195b0f | 118 | |
d344b0fb VJJ |
119 | if (!acl) |
120 | return 0; | |
121 | ||
6e8dc555 AK |
122 | /* Set a setxattr request to server */ |
123 | size = posix_acl_xattr_size(acl->a_count); | |
124 | buffer = kmalloc(size, GFP_KERNEL); | |
125 | if (!buffer) | |
126 | return -ENOMEM; | |
5f3a4a28 | 127 | retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); |
6e8dc555 AK |
128 | if (retval < 0) |
129 | goto err_free_out; | |
130 | switch (type) { | |
131 | case ACL_TYPE_ACCESS: | |
97d79299 | 132 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
6e8dc555 AK |
133 | break; |
134 | case ACL_TYPE_DEFAULT: | |
97d79299 | 135 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
6e8dc555 AK |
136 | break; |
137 | default: | |
138 | BUG(); | |
139 | } | |
0f235cae | 140 | retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0); |
6e8dc555 AK |
141 | err_free_out: |
142 | kfree(buffer); | |
143 | return retval; | |
144 | } | |
145 | ||
be308f07 | 146 | int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid) |
6e8dc555 AK |
147 | { |
148 | int retval = 0; | |
bc26ab5f | 149 | struct posix_acl *acl; |
6e8dc555 AK |
150 | |
151 | if (S_ISLNK(inode->i_mode)) | |
152 | return -EOPNOTSUPP; | |
153 | acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); | |
154 | if (acl) { | |
5bf3258f | 155 | retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); |
bc26ab5f AV |
156 | if (retval) |
157 | return retval; | |
7f165aaa | 158 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); |
0f235cae | 159 | retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl); |
6e8dc555 | 160 | posix_acl_release(acl); |
6e8dc555 AK |
161 | } |
162 | return retval; | |
163 | } | |
164 | ||
3592ac44 | 165 | int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid, |
5fa6300a | 166 | struct posix_acl *dacl, struct posix_acl *acl) |
ad77dbce | 167 | { |
3592ac44 AV |
168 | set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); |
169 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); | |
170 | v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl); | |
171 | v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl); | |
ad77dbce AK |
172 | return 0; |
173 | } | |
174 | ||
5fa6300a AV |
175 | void v9fs_put_acl(struct posix_acl *dacl, |
176 | struct posix_acl *acl) | |
177 | { | |
178 | posix_acl_release(dacl); | |
179 | posix_acl_release(acl); | |
180 | } | |
181 | ||
d3fb6120 | 182 | int v9fs_acl_mode(struct inode *dir, umode_t *modep, |
ad77dbce AK |
183 | struct posix_acl **dpacl, struct posix_acl **pacl) |
184 | { | |
185 | int retval = 0; | |
d3fb6120 | 186 | umode_t mode = *modep; |
ad77dbce AK |
187 | struct posix_acl *acl = NULL; |
188 | ||
189 | if (!S_ISLNK(mode)) { | |
190 | acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT); | |
191 | if (IS_ERR(acl)) | |
192 | return PTR_ERR(acl); | |
193 | if (!acl) | |
194 | mode &= ~current_umask(); | |
195 | } | |
196 | if (acl) { | |
ad77dbce | 197 | if (S_ISDIR(mode)) |
1ec95bf3 | 198 | *dpacl = posix_acl_dup(acl); |
37bc1539 | 199 | retval = __posix_acl_create(&acl, GFP_NOFS, &mode); |
826cae2f AV |
200 | if (retval < 0) |
201 | return retval; | |
ad77dbce | 202 | if (retval > 0) |
826cae2f | 203 | *pacl = acl; |
1ec95bf3 | 204 | else |
826cae2f | 205 | posix_acl_release(acl); |
ad77dbce AK |
206 | } |
207 | *modep = mode; | |
208 | return 0; | |
ad77dbce AK |
209 | } |
210 | ||
d9a82a04 | 211 | static int v9fs_xattr_get_acl(const struct xattr_handler *handler, |
b296821a AV |
212 | struct dentry *dentry, struct inode *inode, |
213 | const char *name, void *buffer, size_t size) | |
7a4566b0 | 214 | { |
76381a42 | 215 | struct v9fs_session_info *v9ses; |
7a4566b0 AK |
216 | struct posix_acl *acl; |
217 | int error; | |
218 | ||
42869c8a | 219 | v9ses = v9fs_dentry2v9ses(dentry); |
76381a42 AK |
220 | /* |
221 | * We allow set/get/list of acl when access=client is not specified | |
222 | */ | |
223 | if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) | |
98e9cb57 | 224 | return v9fs_xattr_get(dentry, handler->name, buffer, size); |
76381a42 | 225 | |
b296821a | 226 | acl = v9fs_get_cached_acl(inode, handler->flags); |
7a4566b0 AK |
227 | if (IS_ERR(acl)) |
228 | return PTR_ERR(acl); | |
229 | if (acl == NULL) | |
230 | return -ENODATA; | |
5f3a4a28 | 231 | error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); |
7a4566b0 AK |
232 | posix_acl_release(acl); |
233 | ||
234 | return error; | |
235 | } | |
236 | ||
d9a82a04 | 237 | static int v9fs_xattr_set_acl(const struct xattr_handler *handler, |
e65ce2a5 | 238 | struct user_namespace *mnt_userns, |
59301226 AV |
239 | struct dentry *dentry, struct inode *inode, |
240 | const char *name, const void *value, | |
241 | size_t size, int flags) | |
7a4566b0 | 242 | { |
22d8dcdf AK |
243 | int retval; |
244 | struct posix_acl *acl; | |
76381a42 | 245 | struct v9fs_session_info *v9ses; |
22d8dcdf | 246 | |
42869c8a | 247 | v9ses = v9fs_dentry2v9ses(dentry); |
76381a42 AK |
248 | /* |
249 | * set the attribute on the remote. Without even looking at the | |
250 | * xattr value. We leave it to the server to validate | |
251 | */ | |
252 | if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) | |
98e9cb57 | 253 | return v9fs_xattr_set(dentry, handler->name, value, size, |
e409de99 | 254 | flags); |
76381a42 | 255 | |
22d8dcdf AK |
256 | if (S_ISLNK(inode->i_mode)) |
257 | return -EOPNOTSUPP; | |
21cb47be | 258 | if (!inode_owner_or_capable(&init_user_ns, inode)) |
22d8dcdf AK |
259 | return -EPERM; |
260 | if (value) { | |
261 | /* update the cached acl value */ | |
5f3a4a28 | 262 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
22d8dcdf AK |
263 | if (IS_ERR(acl)) |
264 | return PTR_ERR(acl); | |
265 | else if (acl) { | |
0d4d717f | 266 | retval = posix_acl_valid(inode->i_sb->s_user_ns, acl); |
22d8dcdf AK |
267 | if (retval) |
268 | goto err_out; | |
269 | } | |
270 | } else | |
271 | acl = NULL; | |
272 | ||
d9a82a04 | 273 | switch (handler->flags) { |
22d8dcdf | 274 | case ACL_TYPE_ACCESS: |
22d8dcdf | 275 | if (acl) { |
e02a53d9 | 276 | struct iattr iattr = { 0 }; |
b5c66bab | 277 | struct posix_acl *old_acl = acl; |
07393101 | 278 | |
549c7297 | 279 | retval = posix_acl_update_mode(&init_user_ns, inode, |
e65ce2a5 | 280 | &iattr.ia_mode, &acl); |
07393101 | 281 | if (retval) |
22d8dcdf | 282 | goto err_out; |
07393101 JK |
283 | if (!acl) { |
284 | /* | |
285 | * ACL can be represented | |
286 | * by the mode bits. So don't | |
287 | * update ACL. | |
22d8dcdf | 288 | */ |
b5c66bab | 289 | posix_acl_release(old_acl); |
07393101 JK |
290 | value = NULL; |
291 | size = 0; | |
22d8dcdf | 292 | } |
07393101 JK |
293 | iattr.ia_valid = ATTR_MODE; |
294 | /* FIXME should we update ctime ? | |
295 | * What is the following setxattr update the | |
296 | * mode ? | |
297 | */ | |
549c7297 | 298 | v9fs_vfs_setattr_dotl(&init_user_ns, dentry, &iattr); |
22d8dcdf AK |
299 | } |
300 | break; | |
301 | case ACL_TYPE_DEFAULT: | |
22d8dcdf | 302 | if (!S_ISDIR(inode->i_mode)) { |
6f81c115 | 303 | retval = acl ? -EINVAL : 0; |
22d8dcdf AK |
304 | goto err_out; |
305 | } | |
306 | break; | |
307 | default: | |
308 | BUG(); | |
309 | } | |
98e9cb57 | 310 | retval = v9fs_xattr_set(dentry, handler->name, value, size, flags); |
22d8dcdf | 311 | if (!retval) |
d9a82a04 | 312 | set_cached_acl(inode, handler->flags, acl); |
22d8dcdf AK |
313 | err_out: |
314 | posix_acl_release(acl); | |
315 | return retval; | |
7a4566b0 AK |
316 | } |
317 | ||
318 | const struct xattr_handler v9fs_xattr_acl_access_handler = { | |
98e9cb57 | 319 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
7a4566b0 AK |
320 | .flags = ACL_TYPE_ACCESS, |
321 | .get = v9fs_xattr_get_acl, | |
322 | .set = v9fs_xattr_set_acl, | |
323 | }; | |
324 | ||
325 | const struct xattr_handler v9fs_xattr_acl_default_handler = { | |
98e9cb57 | 326 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
7a4566b0 AK |
327 | .flags = ACL_TYPE_DEFAULT, |
328 | .get = v9fs_xattr_get_acl, | |
329 | .set = v9fs_xattr_set_acl, | |
330 | }; |