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 btrfs_trans_handle
*trans
,
98 struct inode
*inode
, struct posix_acl
*acl
, int type
)
106 ret
= posix_acl_valid(acl
);
113 case ACL_TYPE_ACCESS
:
114 mode
= inode
->i_mode
;
115 name
= POSIX_ACL_XATTR_ACCESS
;
117 ret
= posix_acl_equiv_mode(acl
, &mode
);
120 inode
->i_mode
= mode
;
124 case ACL_TYPE_DEFAULT
:
125 if (!S_ISDIR(inode
->i_mode
))
126 return acl
? -EINVAL
: 0;
127 name
= POSIX_ACL_XATTR_DEFAULT
;
134 size
= posix_acl_xattr_size(acl
->a_count
);
135 value
= kmalloc(size
, GFP_NOFS
);
141 ret
= posix_acl_to_xattr(acl
, value
, size
);
146 ret
= __btrfs_setxattr(trans
, inode
, name
, value
, size
, 0);
151 set_cached_acl(inode
, type
, acl
);
156 static int btrfs_xattr_set_acl(struct inode
*inode
, int type
,
157 const void *value
, size_t size
)
160 struct posix_acl
*acl
= NULL
;
162 if (!is_owner_or_cap(inode
))
166 acl
= posix_acl_from_xattr(value
, size
);
170 } else if (IS_ERR(acl
)) {
175 ret
= btrfs_set_acl(NULL
, inode
, acl
, type
);
177 posix_acl_release(acl
);
183 static int btrfs_xattr_acl_access_get(struct inode
*inode
, const char *name
,
184 void *value
, size_t size
)
186 return btrfs_xattr_get_acl(inode
, ACL_TYPE_ACCESS
, value
, size
);
189 static int btrfs_xattr_acl_access_set(struct inode
*inode
, const char *name
,
190 const void *value
, size_t size
, int flags
)
192 return btrfs_xattr_set_acl(inode
, ACL_TYPE_ACCESS
, value
, size
);
195 static int btrfs_xattr_acl_default_get(struct inode
*inode
, const char *name
,
196 void *value
, size_t size
)
198 return btrfs_xattr_get_acl(inode
, ACL_TYPE_DEFAULT
, value
, size
);
201 static int btrfs_xattr_acl_default_set(struct inode
*inode
, const char *name
,
202 const void *value
, size_t size
, int flags
)
204 return btrfs_xattr_set_acl(inode
, ACL_TYPE_DEFAULT
, value
, size
);
207 int btrfs_check_acl(struct inode
*inode
, int mask
)
209 struct posix_acl
*acl
;
212 acl
= btrfs_get_acl(inode
, ACL_TYPE_ACCESS
);
217 error
= posix_acl_permission(inode
, acl
, mask
);
218 posix_acl_release(acl
);
225 * btrfs_init_acl is already generally called under fs_mutex, so the locking
226 * stuff has been fixed to work with that. If the locking stuff changes, we
227 * need to re-evaluate the acl locking stuff.
229 int btrfs_init_acl(struct btrfs_trans_handle
*trans
,
230 struct inode
*inode
, struct inode
*dir
)
232 struct posix_acl
*acl
= NULL
;
235 /* this happens with subvols */
239 if (!S_ISLNK(inode
->i_mode
)) {
240 if (IS_POSIXACL(dir
)) {
241 acl
= btrfs_get_acl(dir
, ACL_TYPE_DEFAULT
);
247 inode
->i_mode
&= ~current_umask();
250 if (IS_POSIXACL(dir
) && acl
) {
251 struct posix_acl
*clone
;
254 if (S_ISDIR(inode
->i_mode
)) {
255 ret
= btrfs_set_acl(trans
, inode
, acl
,
260 clone
= posix_acl_clone(acl
, GFP_NOFS
);
265 mode
= inode
->i_mode
;
266 ret
= posix_acl_create_masq(clone
, &mode
);
268 inode
->i_mode
= mode
;
271 ret
= btrfs_set_acl(trans
, inode
, clone
,
275 posix_acl_release(clone
);
278 posix_acl_release(acl
);
283 int btrfs_acl_chmod(struct inode
*inode
)
285 struct posix_acl
*acl
, *clone
;
288 if (S_ISLNK(inode
->i_mode
))
291 if (!IS_POSIXACL(inode
))
294 acl
= btrfs_get_acl(inode
, ACL_TYPE_ACCESS
);
295 if (IS_ERR(acl
) || !acl
)
298 clone
= posix_acl_clone(acl
, GFP_KERNEL
);
299 posix_acl_release(acl
);
303 ret
= posix_acl_chmod_masq(clone
, inode
->i_mode
);
305 ret
= btrfs_set_acl(NULL
, inode
, clone
, ACL_TYPE_ACCESS
);
307 posix_acl_release(clone
);
312 struct xattr_handler btrfs_xattr_acl_default_handler
= {
313 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
314 .get
= btrfs_xattr_acl_default_get
,
315 .set
= btrfs_xattr_acl_default_set
,
318 struct xattr_handler btrfs_xattr_acl_access_handler
= {
319 .prefix
= POSIX_ACL_XATTR_ACCESS
,
320 .get
= btrfs_xattr_acl_access_get
,
321 .set
= btrfs_xattr_acl_access_set
,
324 #else /* CONFIG_BTRFS_FS_POSIX_ACL */
326 int btrfs_acl_chmod(struct inode
*inode
)
331 int btrfs_init_acl(struct btrfs_trans_handle
*trans
,
332 struct inode
*inode
, struct inode
*dir
)
337 #endif /* CONFIG_BTRFS_FS_POSIX_ACL */