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_get_acl(struct p9_fid *fid, char *name)
24 struct posix_acl *acl = NULL;
26 size = v9fs_fid_xattr_get(fid, name, NULL, 0);
28 value = kzalloc(size, GFP_NOFS);
30 return ERR_PTR(-ENOMEM);
31 size = v9fs_fid_xattr_get(fid, name, value, size);
33 acl = posix_acl_from_xattr(&init_user_ns, value, size);
37 } else if (size == -ENODATA || size == 0 ||
38 size == -ENOSYS || size == -EOPNOTSUPP) {
48 int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
51 struct posix_acl *pacl, *dacl;
52 struct v9fs_session_info *v9ses;
54 v9ses = v9fs_inode2v9ses(inode);
55 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
56 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
57 set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
58 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
61 /* get the default/access acl values and cache them */
62 dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
63 pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
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);
72 posix_acl_release(dacl);
75 posix_acl_release(pacl);
80 static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
82 struct posix_acl *acl;
84 * 9p Always cache the acl value when
85 * instantiating the inode (v9fs_inode_from_fid)
87 acl = get_cached_acl(inode, type);
88 BUG_ON(is_uncached_acl(acl));
92 struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type, bool rcu)
94 struct v9fs_session_info *v9ses;
97 return ERR_PTR(-ECHILD);
99 v9ses = v9fs_inode2v9ses(inode);
100 if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
101 ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
103 * On access = client and acl = on mode get the acl
104 * values from the server
108 return v9fs_get_cached_acl(inode, type);
112 static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
122 /* Set a setxattr request to server */
123 size = posix_acl_xattr_size(acl->a_count);
124 buffer = kmalloc(size, GFP_KERNEL);
127 retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
131 case ACL_TYPE_ACCESS:
132 name = XATTR_NAME_POSIX_ACL_ACCESS;
134 case ACL_TYPE_DEFAULT:
135 name = XATTR_NAME_POSIX_ACL_DEFAULT;
140 retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
146 int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
149 struct posix_acl *acl;
151 if (S_ISLNK(inode->i_mode))
153 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
155 retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
158 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
159 retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
160 posix_acl_release(acl);
165 int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
166 struct posix_acl *dacl, struct posix_acl *acl)
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);
175 void v9fs_put_acl(struct posix_acl *dacl,
176 struct posix_acl *acl)
178 posix_acl_release(dacl);
179 posix_acl_release(acl);
182 int v9fs_acl_mode(struct inode *dir, umode_t *modep,
183 struct posix_acl **dpacl, struct posix_acl **pacl)
186 umode_t mode = *modep;
187 struct posix_acl *acl = NULL;
189 if (!S_ISLNK(mode)) {
190 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
194 mode &= ~current_umask();
198 *dpacl = posix_acl_dup(acl);
199 retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
205 posix_acl_release(acl);
211 static int v9fs_xattr_get_acl(const struct xattr_handler *handler,
212 struct dentry *dentry, struct inode *inode,
213 const char *name, void *buffer, size_t size)
215 struct v9fs_session_info *v9ses;
216 struct posix_acl *acl;
219 v9ses = v9fs_dentry2v9ses(dentry);
221 * We allow set/get/list of acl when access=client is not specified
223 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
224 return v9fs_xattr_get(dentry, handler->name, buffer, size);
226 acl = v9fs_get_cached_acl(inode, handler->flags);
231 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
232 posix_acl_release(acl);
237 static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
238 struct user_namespace *mnt_userns,
239 struct dentry *dentry, struct inode *inode,
240 const char *name, const void *value,
241 size_t size, int flags)
244 struct posix_acl *acl;
245 struct v9fs_session_info *v9ses;
247 v9ses = v9fs_dentry2v9ses(dentry);
249 * set the attribute on the remote. Without even looking at the
250 * xattr value. We leave it to the server to validate
252 if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
253 return v9fs_xattr_set(dentry, handler->name, value, size,
256 if (S_ISLNK(inode->i_mode))
258 if (!inode_owner_or_capable(&init_user_ns, inode))
261 /* update the cached acl value */
262 acl = posix_acl_from_xattr(&init_user_ns, value, size);
266 retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
273 switch (handler->flags) {
274 case ACL_TYPE_ACCESS:
276 struct iattr iattr = { 0 };
277 struct posix_acl *old_acl = acl;
279 retval = posix_acl_update_mode(&init_user_ns, inode,
280 &iattr.ia_mode, &acl);
285 * ACL can be represented
286 * by the mode bits. So don't
289 posix_acl_release(old_acl);
293 iattr.ia_valid = ATTR_MODE;
294 /* FIXME should we update ctime ?
295 * What is the following setxattr update the
298 v9fs_vfs_setattr_dotl(&init_user_ns, dentry, &iattr);
301 case ACL_TYPE_DEFAULT:
302 if (!S_ISDIR(inode->i_mode)) {
303 retval = acl ? -EINVAL : 0;
310 retval = v9fs_xattr_set(dentry, handler->name, value, size, flags);
312 set_cached_acl(inode, handler->flags, acl);
314 posix_acl_release(acl);
318 const struct xattr_handler v9fs_xattr_acl_access_handler = {
319 .name = XATTR_NAME_POSIX_ACL_ACCESS,
320 .flags = ACL_TYPE_ACCESS,
321 .get = v9fs_xattr_get_acl,
322 .set = v9fs_xattr_set_acl,
325 const struct xattr_handler v9fs_xattr_acl_default_handler = {
326 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
327 .flags = ACL_TYPE_DEFAULT,
328 .get = v9fs_xattr_get_acl,
329 .set = v9fs_xattr_set_acl,