2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 #include <linux/module.h>
17 #include <net/9p/9p.h>
18 #include <net/9p/client.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/posix_acl_xattr.h>
26 static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
30 struct posix_acl *acl = NULL;;
32 size = v9fs_fid_xattr_get(fid, name, NULL, 0);
34 value = kzalloc(size, GFP_NOFS);
36 return ERR_PTR(-ENOMEM);
37 size = v9fs_fid_xattr_get(fid, name, value, size);
39 acl = posix_acl_from_xattr(value, size);
43 } else if (size == -ENODATA || size == 0 ||
44 size == -ENOSYS || size == -EOPNOTSUPP) {
54 int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
57 struct posix_acl *pacl, *dacl;
59 /* get the default/access acl values and cache them */
60 dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
61 pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
63 if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
64 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
65 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
66 posix_acl_release(dacl);
67 posix_acl_release(pacl);
74 static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
76 struct posix_acl *acl;
78 * 9p Always cache the acl value when
79 * instantiating the inode (v9fs_inode_from_fid)
81 acl = get_cached_acl(inode, type);
82 BUG_ON(acl == ACL_NOT_CACHED);
86 int v9fs_check_acl(struct inode *inode, int mask)
88 struct posix_acl *acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
93 int error = posix_acl_permission(inode, acl, mask);
94 posix_acl_release(acl);
100 static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
106 struct inode *inode = dentry->d_inode;
108 set_cached_acl(inode, type, acl);
109 /* Set a setxattr request to server */
110 size = posix_acl_xattr_size(acl->a_count);
111 buffer = kmalloc(size, GFP_KERNEL);
114 retval = posix_acl_to_xattr(acl, buffer, size);
118 case ACL_TYPE_ACCESS:
119 name = POSIX_ACL_XATTR_ACCESS;
121 case ACL_TYPE_DEFAULT:
122 name = POSIX_ACL_XATTR_DEFAULT;
127 retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
133 int v9fs_acl_chmod(struct dentry *dentry)
136 struct posix_acl *acl, *clone;
137 struct inode *inode = dentry->d_inode;
139 if (S_ISLNK(inode->i_mode))
141 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
143 clone = posix_acl_clone(acl, GFP_KERNEL);
144 posix_acl_release(acl);
147 retval = posix_acl_chmod_masq(clone, inode->i_mode);
149 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
150 posix_acl_release(clone);
155 static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
156 void *buffer, size_t size, int type)
158 struct posix_acl *acl;
161 if (strcmp(name, "") != 0)
164 acl = v9fs_get_cached_acl(dentry->d_inode, type);
169 error = posix_acl_to_xattr(acl, buffer, size);
170 posix_acl_release(acl);
175 static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
176 const void *value, size_t size,
180 struct posix_acl *acl;
181 struct inode *inode = dentry->d_inode;
183 if (strcmp(name, "") != 0)
185 if (S_ISLNK(inode->i_mode))
187 if (!is_owner_or_cap(inode))
190 /* update the cached acl value */
191 acl = posix_acl_from_xattr(value, size);
195 retval = posix_acl_valid(acl);
203 case ACL_TYPE_ACCESS:
204 name = POSIX_ACL_XATTR_ACCESS;
206 mode_t mode = inode->i_mode;
207 retval = posix_acl_equiv_mode(acl, &mode);
214 * ACL can be represented
215 * by the mode bits. So don't
222 /* Updte the mode bits */
223 iattr.ia_mode = ((mode & S_IALLUGO) |
224 (inode->i_mode & ~S_IALLUGO));
225 iattr.ia_valid = ATTR_MODE;
226 /* FIXME should we update ctime ?
227 * What is the following setxattr update the
230 v9fs_vfs_setattr_dotl(dentry, &iattr);
234 case ACL_TYPE_DEFAULT:
235 name = POSIX_ACL_XATTR_DEFAULT;
236 if (!S_ISDIR(inode->i_mode)) {
244 retval = v9fs_xattr_set(dentry, name, value, size, flags);
246 set_cached_acl(inode, type, acl);
248 posix_acl_release(acl);
252 const struct xattr_handler v9fs_xattr_acl_access_handler = {
253 .prefix = POSIX_ACL_XATTR_ACCESS,
254 .flags = ACL_TYPE_ACCESS,
255 .get = v9fs_xattr_get_acl,
256 .set = v9fs_xattr_set_acl,
259 const struct xattr_handler v9fs_xattr_acl_default_handler = {
260 .prefix = POSIX_ACL_XATTR_DEFAULT,
261 .flags = ACL_TYPE_DEFAULT,
262 .get = v9fs_xattr_get_acl,
263 .set = v9fs_xattr_set_acl,