1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright IBM Corporation, 2010
4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
7 #include <linux/module.h>
10 #include <net/9p/client.h>
11 #include <linux/slab.h>
12 #include <linux/sched.h>
13 #include <linux/posix_acl_xattr.h>
20 static struct posix_acl *v9fs_fid_get_acl(struct p9_fid *fid, const char *name)
24 struct posix_acl *acl = NULL;
26 size = v9fs_fid_xattr_get(fid, name, NULL, 0);
30 return ERR_PTR(-ENODATA);
32 value = kzalloc(size, GFP_NOFS);
34 return ERR_PTR(-ENOMEM);
36 size = v9fs_fid_xattr_get(fid, name, value, size);
40 acl = ERR_PTR(-ENODATA);
42 acl = posix_acl_from_xattr(&init_user_ns, value, size);
47 static struct posix_acl *v9fs_acl_get(struct dentry *dentry, const char *name)
50 struct posix_acl *acl = NULL;
52 fid = v9fs_fid_lookup(dentry);
56 acl = v9fs_fid_get_acl(fid, name);
61 static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, const char *name)
64 struct posix_acl *acl = NULL;
66 acl = v9fs_fid_get_acl(fid, name);
70 retval = PTR_ERR(acl);
71 if (retval == -ENODATA || retval == -ENOSYS || retval == -EOPNOTSUPP)
74 /* map everything else to -EIO */
78 int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
81 struct posix_acl *pacl, *dacl;
82 struct v9fs_session_info *v9ses;
84 v9ses = v9fs_inode2v9ses(inode);
85 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
86 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
87 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
88 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
91 /* get the default/access acl values and cache them */
92 dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
93 pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
95 if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
96 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
97 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
102 posix_acl_release(dacl);
105 posix_acl_release(pacl);
110 static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
112 struct posix_acl *acl;
114 * 9p Always cache the acl value when
115 * instantiating the inode (v9fs_inode_from_fid)
117 acl = get_cached_acl(inode, type);
118 BUG_ON(is_uncached_acl(acl));
122 struct posix_acl *v9fs_iop_get_inode_acl(struct inode *inode, int type, bool rcu)
124 struct v9fs_session_info *v9ses;
127 return ERR_PTR(-ECHILD);
129 v9ses = v9fs_inode2v9ses(inode);
130 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
131 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
133 * On access = client and acl = on mode get the acl
134 * values from the server
138 return v9fs_get_cached_acl(inode, type);
142 struct posix_acl *v9fs_iop_get_acl(struct user_namespace *mnt_userns,
143 struct dentry *dentry, int type)
145 struct v9fs_session_info *v9ses;
147 v9ses = v9fs_dentry2v9ses(dentry);
148 /* We allow set/get/list of acl when access=client is not specified. */
149 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
150 return v9fs_acl_get(dentry, posix_acl_xattr_name(type));
151 return v9fs_get_cached_acl(d_inode(dentry), type);
154 static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
164 /* Set a setxattr request to server */
165 size = posix_acl_xattr_size(acl->a_count);
166 buffer = kmalloc(size, GFP_KERNEL);
169 retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
173 case ACL_TYPE_ACCESS:
174 name = XATTR_NAME_POSIX_ACL_ACCESS;
176 case ACL_TYPE_DEFAULT:
177 name = XATTR_NAME_POSIX_ACL_DEFAULT;
182 retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
188 int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
191 struct posix_acl *acl;
193 if (S_ISLNK(inode->i_mode))
195 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
197 retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
200 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
201 retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
202 posix_acl_release(acl);
207 int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
208 struct posix_acl *dacl, struct posix_acl *acl)
210 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
211 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
212 v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
213 v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
217 void v9fs_put_acl(struct posix_acl *dacl,
218 struct posix_acl *acl)
220 posix_acl_release(dacl);
221 posix_acl_release(acl);
224 int v9fs_acl_mode(struct inode *dir, umode_t *modep,
225 struct posix_acl **dpacl, struct posix_acl **pacl)
228 umode_t mode = *modep;
229 struct posix_acl *acl = NULL;
231 if (!S_ISLNK(mode)) {
232 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
236 mode &= ~current_umask();
240 *dpacl = posix_acl_dup(acl);
241 retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
247 posix_acl_release(acl);
253 static int v9fs_xattr_get_acl(const struct xattr_handler *handler,
254 struct dentry *dentry, struct inode *inode,
255 const char *name, void *buffer, size_t size)
257 struct v9fs_session_info *v9ses;
258 struct posix_acl *acl;
261 v9ses = v9fs_dentry2v9ses(dentry);
263 * We allow set/get/list of acl when access=client is not specified
265 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
266 return v9fs_xattr_get(dentry, handler->name, buffer, size);
268 acl = v9fs_get_cached_acl(inode, handler->flags);
273 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
274 posix_acl_release(acl);
279 static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
280 struct user_namespace *mnt_userns,
281 struct dentry *dentry, struct inode *inode,
282 const char *name, const void *value,
283 size_t size, int flags)
286 struct posix_acl *acl;
287 struct v9fs_session_info *v9ses;
289 v9ses = v9fs_dentry2v9ses(dentry);
291 * set the attribute on the remote. Without even looking at the
292 * xattr value. We leave it to the server to validate
294 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
295 return v9fs_xattr_set(dentry, handler->name, value, size,
298 if (S_ISLNK(inode->i_mode))
300 if (!inode_owner_or_capable(&init_user_ns, inode))
303 /* update the cached acl value */
304 acl = posix_acl_from_xattr(&init_user_ns, value, size);
308 retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
315 switch (handler->flags) {
316 case ACL_TYPE_ACCESS:
318 struct iattr iattr = { 0 };
319 struct posix_acl *old_acl = acl;
321 retval = posix_acl_update_mode(&init_user_ns, inode,
322 &iattr.ia_mode, &acl);
327 * ACL can be represented
328 * by the mode bits. So don't
331 posix_acl_release(old_acl);
335 iattr.ia_valid = ATTR_MODE;
336 /* FIXME should we update ctime ?
337 * What is the following setxattr update the
340 v9fs_vfs_setattr_dotl(&init_user_ns, dentry, &iattr);
343 case ACL_TYPE_DEFAULT:
344 if (!S_ISDIR(inode->i_mode)) {
345 retval = acl ? -EINVAL : 0;
352 retval = v9fs_xattr_set(dentry, handler->name, value, size, flags);
354 set_cached_acl(inode, handler->flags, acl);
356 posix_acl_release(acl);
360 const struct xattr_handler v9fs_xattr_acl_access_handler = {
361 .name = XATTR_NAME_POSIX_ACL_ACCESS,
362 .flags = ACL_TYPE_ACCESS,
363 .get = v9fs_xattr_get_acl,
364 .set = v9fs_xattr_set_acl,
367 const struct xattr_handler v9fs_xattr_acl_default_handler = {
368 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
369 .flags = ACL_TYPE_DEFAULT,
370 .get = v9fs_xattr_get_acl,
371 .set = v9fs_xattr_set_acl,