2 * Copyright (C) 2007 Red Hat. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <linux/string.h>
21 #include <linux/xattr.h>
22 #include <linux/posix_acl_xattr.h>
23 #include <linux/posix_acl.h>
24 #include <linux/sched.h>
27 #include "btrfs_inode.h"
30 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
32 static struct posix_acl
*btrfs_get_acl(struct inode
*inode
, int type
)
37 struct posix_acl
*acl
;
39 acl
= get_cached_acl(inode
, type
);
40 if (acl
!= ACL_NOT_CACHED
)
45 name
= POSIX_ACL_XATTR_ACCESS
;
47 case ACL_TYPE_DEFAULT
:
48 name
= POSIX_ACL_XATTR_DEFAULT
;
54 size
= __btrfs_getxattr(inode
, name
, "", 0);
56 value
= kzalloc(size
, GFP_NOFS
);
58 return ERR_PTR(-ENOMEM
);
59 size
= __btrfs_getxattr(inode
, name
, value
, size
);
61 acl
= posix_acl_from_xattr(value
, size
);
62 set_cached_acl(inode
, type
, acl
);
65 } else if (size
== -ENOENT
|| size
== -ENODATA
|| size
== 0) {
66 /* FIXME, who returns -ENOENT? I think nobody */
68 set_cached_acl(inode
, type
, acl
);
76 static int btrfs_xattr_get_acl(struct inode
*inode
, int type
,
77 void *value
, size_t size
)
79 struct posix_acl
*acl
;
82 acl
= btrfs_get_acl(inode
, type
);
88 ret
= posix_acl_to_xattr(acl
, value
, size
);
89 posix_acl_release(acl
);
95 * Needs to be called with fs_mutex held
97 static int btrfs_set_acl(struct inode
*inode
, struct posix_acl
*acl
, int type
)
105 ret
= posix_acl_valid(acl
);
112 case ACL_TYPE_ACCESS
:
113 mode
= inode
->i_mode
;
114 ret
= posix_acl_equiv_mode(acl
, &mode
);
118 inode
->i_mode
= mode
;
119 name
= POSIX_ACL_XATTR_ACCESS
;
121 case ACL_TYPE_DEFAULT
:
122 if (!S_ISDIR(inode
->i_mode
))
123 return acl
? -EINVAL
: 0;
124 name
= POSIX_ACL_XATTR_DEFAULT
;
131 size
= posix_acl_xattr_size(acl
->a_count
);
132 value
= kmalloc(size
, GFP_NOFS
);
138 ret
= posix_acl_to_xattr(acl
, value
, size
);
143 ret
= __btrfs_setxattr(inode
, name
, value
, size
, 0);
149 set_cached_acl(inode
, type
, acl
);
154 static int btrfs_xattr_set_acl(struct inode
*inode
, int type
,
155 const void *value
, size_t size
)
158 struct posix_acl
*acl
= NULL
;
161 acl
= posix_acl_from_xattr(value
, size
);
165 } else if (IS_ERR(acl
)) {
170 ret
= btrfs_set_acl(inode
, acl
, type
);
172 posix_acl_release(acl
);
178 static int btrfs_xattr_acl_access_get(struct inode
*inode
, const char *name
,
179 void *value
, size_t size
)
181 return btrfs_xattr_get_acl(inode
, ACL_TYPE_ACCESS
, value
, size
);
184 static int btrfs_xattr_acl_access_set(struct inode
*inode
, const char *name
,
185 const void *value
, size_t size
, int flags
)
187 return btrfs_xattr_set_acl(inode
, ACL_TYPE_ACCESS
, value
, size
);
190 static int btrfs_xattr_acl_default_get(struct inode
*inode
, const char *name
,
191 void *value
, size_t size
)
193 return btrfs_xattr_get_acl(inode
, ACL_TYPE_DEFAULT
, value
, size
);
196 static int btrfs_xattr_acl_default_set(struct inode
*inode
, const char *name
,
197 const void *value
, size_t size
, int flags
)
199 return btrfs_xattr_set_acl(inode
, ACL_TYPE_DEFAULT
, value
, size
);
202 int btrfs_check_acl(struct inode
*inode
, int mask
)
204 struct posix_acl
*acl
;
207 acl
= btrfs_get_acl(inode
, ACL_TYPE_ACCESS
);
212 error
= posix_acl_permission(inode
, acl
, mask
);
213 posix_acl_release(acl
);
220 * btrfs_init_acl is already generally called under fs_mutex, so the locking
221 * stuff has been fixed to work with that. If the locking stuff changes, we
222 * need to re-evaluate the acl locking stuff.
224 int btrfs_init_acl(struct inode
*inode
, struct inode
*dir
)
226 struct posix_acl
*acl
= NULL
;
229 /* this happens with subvols */
233 if (!S_ISLNK(inode
->i_mode
)) {
234 if (IS_POSIXACL(dir
)) {
235 acl
= btrfs_get_acl(dir
, ACL_TYPE_DEFAULT
);
241 inode
->i_mode
&= ~current_umask();
244 if (IS_POSIXACL(dir
) && acl
) {
245 struct posix_acl
*clone
;
248 if (S_ISDIR(inode
->i_mode
)) {
249 ret
= btrfs_set_acl(inode
, acl
, ACL_TYPE_DEFAULT
);
253 clone
= posix_acl_clone(acl
, GFP_NOFS
);
258 mode
= inode
->i_mode
;
259 ret
= posix_acl_create_masq(clone
, &mode
);
261 inode
->i_mode
= mode
;
264 ret
= btrfs_set_acl(inode
, clone
,
270 posix_acl_release(acl
);
275 int btrfs_acl_chmod(struct inode
*inode
)
277 struct posix_acl
*acl
, *clone
;
280 if (S_ISLNK(inode
->i_mode
))
283 if (!IS_POSIXACL(inode
))
286 acl
= btrfs_get_acl(inode
, ACL_TYPE_ACCESS
);
287 if (IS_ERR(acl
) || !acl
)
290 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
291 posix_acl_release(acl
);
295 ret
= posix_acl_chmod_masq(clone
, inode
->i_mode
);
297 ret
= btrfs_set_acl(inode
, clone
, ACL_TYPE_ACCESS
);
299 posix_acl_release(clone
);
304 struct xattr_handler btrfs_xattr_acl_default_handler
= {
305 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
306 .get
= btrfs_xattr_acl_default_get
,
307 .set
= btrfs_xattr_acl_default_set
,
310 struct xattr_handler btrfs_xattr_acl_access_handler
= {
311 .prefix
= POSIX_ACL_XATTR_ACCESS
,
312 .get
= btrfs_xattr_acl_access_get
,
313 .set
= btrfs_xattr_acl_access_set
,
316 #else /* CONFIG_BTRFS_FS_POSIX_ACL */
318 int btrfs_acl_chmod(struct inode
*inode
)
323 int btrfs_init_acl(struct inode
*inode
, struct inode
*dir
)
328 #endif /* CONFIG_BTRFS_FS_POSIX_ACL */