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>
27 static struct posix_acl
*__v9fs_get_acl(struct p9_fid
*fid
, char *name
)
31 struct posix_acl
*acl
= NULL
;
33 size
= v9fs_fid_xattr_get(fid
, name
, NULL
, 0);
35 value
= kzalloc(size
, GFP_NOFS
);
37 return ERR_PTR(-ENOMEM
);
38 size
= v9fs_fid_xattr_get(fid
, name
, value
, size
);
40 acl
= posix_acl_from_xattr(value
, size
);
44 } else if (size
== -ENODATA
|| size
== 0 ||
45 size
== -ENOSYS
|| size
== -EOPNOTSUPP
) {
55 int v9fs_get_acl(struct inode
*inode
, struct p9_fid
*fid
)
58 struct posix_acl
*pacl
, *dacl
;
59 struct v9fs_session_info
*v9ses
;
61 v9ses
= v9fs_inode2v9ses(inode
);
62 if (((v9ses
->flags
& V9FS_ACCESS_MASK
) != V9FS_ACCESS_CLIENT
) ||
63 ((v9ses
->flags
& V9FS_ACL_MASK
) != V9FS_POSIX_ACL
)) {
64 set_cached_acl(inode
, ACL_TYPE_DEFAULT
, NULL
);
65 set_cached_acl(inode
, ACL_TYPE_ACCESS
, NULL
);
68 /* get the default/access acl values and cache them */
69 dacl
= __v9fs_get_acl(fid
, POSIX_ACL_XATTR_DEFAULT
);
70 pacl
= __v9fs_get_acl(fid
, POSIX_ACL_XATTR_ACCESS
);
72 if (!IS_ERR(dacl
) && !IS_ERR(pacl
)) {
73 set_cached_acl(inode
, ACL_TYPE_DEFAULT
, dacl
);
74 set_cached_acl(inode
, ACL_TYPE_ACCESS
, pacl
);
79 posix_acl_release(dacl
);
82 posix_acl_release(pacl
);
87 static struct posix_acl
*v9fs_get_cached_acl(struct inode
*inode
, int type
)
89 struct posix_acl
*acl
;
91 * 9p Always cache the acl value when
92 * instantiating the inode (v9fs_inode_from_fid)
94 acl
= get_cached_acl(inode
, type
);
95 BUG_ON(acl
== ACL_NOT_CACHED
);
99 int v9fs_check_acl(struct inode
*inode
, int mask
, unsigned int flags
)
101 struct posix_acl
*acl
;
102 struct v9fs_session_info
*v9ses
;
104 if (flags
& IPERM_FLAG_RCU
)
107 v9ses
= v9fs_inode2v9ses(inode
);
108 if (((v9ses
->flags
& V9FS_ACCESS_MASK
) != V9FS_ACCESS_CLIENT
) ||
109 ((v9ses
->flags
& V9FS_ACL_MASK
) != V9FS_POSIX_ACL
)) {
111 * On access = client and acl = on mode get the acl
112 * values from the server
116 acl
= v9fs_get_cached_acl(inode
, ACL_TYPE_ACCESS
);
121 int error
= posix_acl_permission(inode
, acl
, mask
);
122 posix_acl_release(acl
);
128 static int v9fs_set_acl(struct dentry
*dentry
, int type
, struct posix_acl
*acl
)
134 struct inode
*inode
= dentry
->d_inode
;
136 set_cached_acl(inode
, type
, acl
);
141 /* Set a setxattr request to server */
142 size
= posix_acl_xattr_size(acl
->a_count
);
143 buffer
= kmalloc(size
, GFP_KERNEL
);
146 retval
= posix_acl_to_xattr(acl
, buffer
, size
);
150 case ACL_TYPE_ACCESS
:
151 name
= POSIX_ACL_XATTR_ACCESS
;
153 case ACL_TYPE_DEFAULT
:
154 name
= POSIX_ACL_XATTR_DEFAULT
;
159 retval
= v9fs_xattr_set(dentry
, name
, buffer
, size
, 0);
165 int v9fs_acl_chmod(struct dentry
*dentry
)
168 struct posix_acl
*acl
, *clone
;
169 struct inode
*inode
= dentry
->d_inode
;
171 if (S_ISLNK(inode
->i_mode
))
173 acl
= v9fs_get_cached_acl(inode
, ACL_TYPE_ACCESS
);
175 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
176 posix_acl_release(acl
);
179 retval
= posix_acl_chmod_masq(clone
, inode
->i_mode
);
181 retval
= v9fs_set_acl(dentry
, ACL_TYPE_ACCESS
, clone
);
182 posix_acl_release(clone
);
187 int v9fs_set_create_acl(struct dentry
*dentry
,
188 struct posix_acl
**dpacl
, struct posix_acl
**pacl
)
191 v9fs_set_acl(dentry
, ACL_TYPE_DEFAULT
, *dpacl
);
192 v9fs_set_acl(dentry
, ACL_TYPE_ACCESS
, *pacl
);
194 posix_acl_release(*dpacl
);
195 posix_acl_release(*pacl
);
196 *dpacl
= *pacl
= NULL
;
200 int v9fs_acl_mode(struct inode
*dir
, mode_t
*modep
,
201 struct posix_acl
**dpacl
, struct posix_acl
**pacl
)
204 mode_t mode
= *modep
;
205 struct posix_acl
*acl
= NULL
;
207 if (!S_ISLNK(mode
)) {
208 acl
= v9fs_get_cached_acl(dir
, ACL_TYPE_DEFAULT
);
212 mode
&= ~current_umask();
215 struct posix_acl
*clone
;
218 *dpacl
= posix_acl_dup(acl
);
219 clone
= posix_acl_clone(acl
, GFP_NOFS
);
220 posix_acl_release(acl
);
224 retval
= posix_acl_create_masq(clone
, &mode
);
226 posix_acl_release(clone
);
232 posix_acl_release(clone
);
241 static int v9fs_remote_get_acl(struct dentry
*dentry
, const char *name
,
242 void *buffer
, size_t size
, int type
)
247 case ACL_TYPE_ACCESS
:
248 full_name
= POSIX_ACL_XATTR_ACCESS
;
250 case ACL_TYPE_DEFAULT
:
251 full_name
= POSIX_ACL_XATTR_DEFAULT
;
256 return v9fs_xattr_get(dentry
, full_name
, buffer
, size
);
259 static int v9fs_xattr_get_acl(struct dentry
*dentry
, const char *name
,
260 void *buffer
, size_t size
, int type
)
262 struct v9fs_session_info
*v9ses
;
263 struct posix_acl
*acl
;
266 if (strcmp(name
, "") != 0)
269 v9ses
= v9fs_dentry2v9ses(dentry
);
271 * We allow set/get/list of acl when access=client is not specified
273 if ((v9ses
->flags
& V9FS_ACCESS_MASK
) != V9FS_ACCESS_CLIENT
)
274 return v9fs_remote_get_acl(dentry
, name
, buffer
, size
, type
);
276 acl
= v9fs_get_cached_acl(dentry
->d_inode
, type
);
281 error
= posix_acl_to_xattr(acl
, buffer
, size
);
282 posix_acl_release(acl
);
287 static int v9fs_remote_set_acl(struct dentry
*dentry
, const char *name
,
288 const void *value
, size_t size
,
294 case ACL_TYPE_ACCESS
:
295 full_name
= POSIX_ACL_XATTR_ACCESS
;
297 case ACL_TYPE_DEFAULT
:
298 full_name
= POSIX_ACL_XATTR_DEFAULT
;
303 return v9fs_xattr_set(dentry
, full_name
, value
, size
, flags
);
307 static int v9fs_xattr_set_acl(struct dentry
*dentry
, const char *name
,
308 const void *value
, size_t size
,
312 struct posix_acl
*acl
;
313 struct v9fs_session_info
*v9ses
;
314 struct inode
*inode
= dentry
->d_inode
;
316 if (strcmp(name
, "") != 0)
319 v9ses
= v9fs_dentry2v9ses(dentry
);
321 * set the attribute on the remote. Without even looking at the
322 * xattr value. We leave it to the server to validate
324 if ((v9ses
->flags
& V9FS_ACCESS_MASK
) != V9FS_ACCESS_CLIENT
)
325 return v9fs_remote_set_acl(dentry
, name
,
326 value
, size
, flags
, type
);
328 if (S_ISLNK(inode
->i_mode
))
330 if (!inode_owner_or_capable(inode
))
333 /* update the cached acl value */
334 acl
= posix_acl_from_xattr(value
, size
);
338 retval
= posix_acl_valid(acl
);
346 case ACL_TYPE_ACCESS
:
347 name
= POSIX_ACL_XATTR_ACCESS
;
349 mode_t mode
= inode
->i_mode
;
350 retval
= posix_acl_equiv_mode(acl
, &mode
);
357 * ACL can be represented
358 * by the mode bits. So don't
365 /* Updte the mode bits */
366 iattr
.ia_mode
= ((mode
& S_IALLUGO
) |
367 (inode
->i_mode
& ~S_IALLUGO
));
368 iattr
.ia_valid
= ATTR_MODE
;
369 /* FIXME should we update ctime ?
370 * What is the following setxattr update the
373 v9fs_vfs_setattr_dotl(dentry
, &iattr
);
377 case ACL_TYPE_DEFAULT
:
378 name
= POSIX_ACL_XATTR_DEFAULT
;
379 if (!S_ISDIR(inode
->i_mode
)) {
380 retval
= acl
? -EINVAL
: 0;
387 retval
= v9fs_xattr_set(dentry
, name
, value
, size
, flags
);
389 set_cached_acl(inode
, type
, acl
);
391 posix_acl_release(acl
);
395 const struct xattr_handler v9fs_xattr_acl_access_handler
= {
396 .prefix
= POSIX_ACL_XATTR_ACCESS
,
397 .flags
= ACL_TYPE_ACCESS
,
398 .get
= v9fs_xattr_get_acl
,
399 .set
= v9fs_xattr_set_acl
,
402 const struct xattr_handler v9fs_xattr_acl_default_handler
= {
403 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
404 .flags
= ACL_TYPE_DEFAULT
,
405 .get
= v9fs_xattr_get_acl
,
406 .set
= v9fs_xattr_set_acl
,