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
)
190 v9fs_set_acl(dentry
, ACL_TYPE_DEFAULT
, dpacl
);
191 v9fs_set_acl(dentry
, ACL_TYPE_ACCESS
, pacl
);
192 posix_acl_release(dpacl
);
193 posix_acl_release(pacl
);
197 int v9fs_acl_mode(struct inode
*dir
, mode_t
*modep
,
198 struct posix_acl
**dpacl
, struct posix_acl
**pacl
)
201 mode_t mode
= *modep
;
202 struct posix_acl
*acl
= NULL
;
204 if (!S_ISLNK(mode
)) {
205 acl
= v9fs_get_cached_acl(dir
, ACL_TYPE_DEFAULT
);
209 mode
&= ~current_umask();
212 struct posix_acl
*clone
;
216 clone
= posix_acl_clone(acl
, GFP_NOFS
);
221 retval
= posix_acl_create_masq(clone
, &mode
);
223 posix_acl_release(clone
);
232 posix_acl_release(acl
);
237 static int v9fs_remote_get_acl(struct dentry
*dentry
, const char *name
,
238 void *buffer
, size_t size
, int type
)
243 case ACL_TYPE_ACCESS
:
244 full_name
= POSIX_ACL_XATTR_ACCESS
;
246 case ACL_TYPE_DEFAULT
:
247 full_name
= POSIX_ACL_XATTR_DEFAULT
;
252 return v9fs_xattr_get(dentry
, full_name
, buffer
, size
);
255 static int v9fs_xattr_get_acl(struct dentry
*dentry
, const char *name
,
256 void *buffer
, size_t size
, int type
)
258 struct v9fs_session_info
*v9ses
;
259 struct posix_acl
*acl
;
262 if (strcmp(name
, "") != 0)
265 v9ses
= v9fs_dentry2v9ses(dentry
);
267 * We allow set/get/list of acl when access=client is not specified
269 if ((v9ses
->flags
& V9FS_ACCESS_MASK
) != V9FS_ACCESS_CLIENT
)
270 return v9fs_remote_get_acl(dentry
, name
, buffer
, size
, type
);
272 acl
= v9fs_get_cached_acl(dentry
->d_inode
, type
);
277 error
= posix_acl_to_xattr(acl
, buffer
, size
);
278 posix_acl_release(acl
);
283 static int v9fs_remote_set_acl(struct dentry
*dentry
, const char *name
,
284 const void *value
, size_t size
,
290 case ACL_TYPE_ACCESS
:
291 full_name
= POSIX_ACL_XATTR_ACCESS
;
293 case ACL_TYPE_DEFAULT
:
294 full_name
= POSIX_ACL_XATTR_DEFAULT
;
299 return v9fs_xattr_set(dentry
, full_name
, value
, size
, flags
);
303 static int v9fs_xattr_set_acl(struct dentry
*dentry
, const char *name
,
304 const void *value
, size_t size
,
308 struct posix_acl
*acl
;
309 struct v9fs_session_info
*v9ses
;
310 struct inode
*inode
= dentry
->d_inode
;
312 if (strcmp(name
, "") != 0)
315 v9ses
= v9fs_dentry2v9ses(dentry
);
317 * set the attribute on the remote. Without even looking at the
318 * xattr value. We leave it to the server to validate
320 if ((v9ses
->flags
& V9FS_ACCESS_MASK
) != V9FS_ACCESS_CLIENT
)
321 return v9fs_remote_set_acl(dentry
, name
,
322 value
, size
, flags
, type
);
324 if (S_ISLNK(inode
->i_mode
))
326 if (!inode_owner_or_capable(inode
))
329 /* update the cached acl value */
330 acl
= posix_acl_from_xattr(value
, size
);
334 retval
= posix_acl_valid(acl
);
342 case ACL_TYPE_ACCESS
:
343 name
= POSIX_ACL_XATTR_ACCESS
;
345 mode_t mode
= inode
->i_mode
;
346 retval
= posix_acl_equiv_mode(acl
, &mode
);
353 * ACL can be represented
354 * by the mode bits. So don't
361 /* Updte the mode bits */
362 iattr
.ia_mode
= ((mode
& S_IALLUGO
) |
363 (inode
->i_mode
& ~S_IALLUGO
));
364 iattr
.ia_valid
= ATTR_MODE
;
365 /* FIXME should we update ctime ?
366 * What is the following setxattr update the
369 v9fs_vfs_setattr_dotl(dentry
, &iattr
);
373 case ACL_TYPE_DEFAULT
:
374 name
= POSIX_ACL_XATTR_DEFAULT
;
375 if (!S_ISDIR(inode
->i_mode
)) {
376 retval
= acl
? -EINVAL
: 0;
383 retval
= v9fs_xattr_set(dentry
, name
, value
, size
, flags
);
385 set_cached_acl(inode
, type
, acl
);
387 posix_acl_release(acl
);
391 const struct xattr_handler v9fs_xattr_acl_access_handler
= {
392 .prefix
= POSIX_ACL_XATTR_ACCESS
,
393 .flags
= ACL_TYPE_ACCESS
,
394 .get
= v9fs_xattr_get_acl
,
395 .set
= v9fs_xattr_set_acl
,
398 const struct xattr_handler v9fs_xattr_acl_default_handler
= {
399 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
400 .flags
= ACL_TYPE_DEFAULT
,
401 .get
= v9fs_xattr_get_acl
,
402 .set
= v9fs_xattr_set_acl
,