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_FS_POSIX_ACL
32 static void btrfs_update_cached_acl(struct inode *inode,
33 struct posix_acl **p_acl,
34 struct posix_acl *acl)
36 spin_lock(&inode->i_lock);
37 if (*p_acl && *p_acl != BTRFS_ACL_NOT_CACHED)
38 posix_acl_release(*p_acl);
39 *p_acl = posix_acl_dup(acl);
40 spin_unlock(&inode->i_lock);
43 static struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
48 struct posix_acl *acl = NULL, **p_acl;
52 name = POSIX_ACL_XATTR_ACCESS;
53 p_acl = &BTRFS_I(inode)->i_acl;
55 case ACL_TYPE_DEFAULT:
56 name = POSIX_ACL_XATTR_DEFAULT;
57 p_acl = &BTRFS_I(inode)->i_default_acl;
60 return ERR_PTR(-EINVAL);
63 /* Handle the cached NULL acl case without locking */
64 acl = ACCESS_ONCE(*p_acl);
68 spin_lock(&inode->i_lock);
70 if (acl != BTRFS_ACL_NOT_CACHED)
71 acl = posix_acl_dup(acl);
72 spin_unlock(&inode->i_lock);
74 if (acl != BTRFS_ACL_NOT_CACHED)
77 size = __btrfs_getxattr(inode, name, "", 0);
79 value = kzalloc(size, GFP_NOFS);
81 return ERR_PTR(-ENOMEM);
82 size = __btrfs_getxattr(inode, name, value, size);
84 acl = posix_acl_from_xattr(value, size);
85 btrfs_update_cached_acl(inode, p_acl, acl);
88 } else if (size == -ENOENT || size == -ENODATA || size == 0) {
89 /* FIXME, who returns -ENOENT? I think nobody */
91 btrfs_update_cached_acl(inode, p_acl, acl);
99 static int btrfs_xattr_get_acl(struct inode *inode, int type,
100 void *value, size_t size)
102 struct posix_acl *acl;
105 acl = btrfs_get_acl(inode, type);
111 ret = posix_acl_to_xattr(acl, value, size);
112 posix_acl_release(acl);
118 * Needs to be called with fs_mutex held
120 static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
124 struct posix_acl **p_acl;
129 ret = posix_acl_valid(acl);
136 case ACL_TYPE_ACCESS:
137 mode = inode->i_mode;
138 ret = posix_acl_equiv_mode(acl, &mode);
142 inode->i_mode = mode;
143 name = POSIX_ACL_XATTR_ACCESS;
144 p_acl = &BTRFS_I(inode)->i_acl;
146 case ACL_TYPE_DEFAULT:
147 if (!S_ISDIR(inode->i_mode))
148 return acl ? -EINVAL : 0;
149 name = POSIX_ACL_XATTR_DEFAULT;
150 p_acl = &BTRFS_I(inode)->i_default_acl;
157 size = posix_acl_xattr_size(acl->a_count);
158 value = kmalloc(size, GFP_NOFS);
164 ret = posix_acl_to_xattr(acl, value, size);
169 ret = __btrfs_setxattr(inode, name, value, size, 0);
175 btrfs_update_cached_acl(inode, p_acl, acl);
180 static int btrfs_xattr_set_acl(struct inode *inode, int type,
181 const void *value, size_t size)
184 struct posix_acl *acl = NULL;
187 acl = posix_acl_from_xattr(value, size);
191 } else if (IS_ERR(acl)) {
196 ret = btrfs_set_acl(inode, acl, type);
198 posix_acl_release(acl);
204 static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
205 void *value, size_t size)
207 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
210 static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
211 const void *value, size_t size, int flags)
213 return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
216 static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
217 void *value, size_t size)
219 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
222 static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
223 const void *value, size_t size, int flags)
225 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
228 int btrfs_check_acl(struct inode *inode, int mask)
230 struct posix_acl *acl;
233 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
238 error = posix_acl_permission(inode, acl, mask);
239 posix_acl_release(acl);
246 * btrfs_init_acl is already generally called under fs_mutex, so the locking
247 * stuff has been fixed to work with that. If the locking stuff changes, we
248 * need to re-evaluate the acl locking stuff.
250 int btrfs_init_acl(struct inode *inode, struct inode *dir)
252 struct posix_acl *acl = NULL;
255 /* this happens with subvols */
259 if (!S_ISLNK(inode->i_mode)) {
260 if (IS_POSIXACL(dir)) {
261 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
267 inode->i_mode &= ~current_umask();
270 if (IS_POSIXACL(dir) && acl) {
271 struct posix_acl *clone;
274 if (S_ISDIR(inode->i_mode)) {
275 ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
279 clone = posix_acl_clone(acl, GFP_NOFS);
284 mode = inode->i_mode;
285 ret = posix_acl_create_masq(clone, &mode);
287 inode->i_mode = mode;
290 ret = btrfs_set_acl(inode, clone,
296 posix_acl_release(acl);
301 int btrfs_acl_chmod(struct inode *inode)
303 struct posix_acl *acl, *clone;
306 if (S_ISLNK(inode->i_mode))
309 if (!IS_POSIXACL(inode))
312 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
313 if (IS_ERR(acl) || !acl)
316 clone = posix_acl_clone(acl, GFP_KERNEL);
317 posix_acl_release(acl);
321 ret = posix_acl_chmod_masq(clone, inode->i_mode);
323 ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
325 posix_acl_release(clone);
330 struct xattr_handler btrfs_xattr_acl_default_handler = {
331 .prefix = POSIX_ACL_XATTR_DEFAULT,
332 .get = btrfs_xattr_acl_default_get,
333 .set = btrfs_xattr_acl_default_set,
336 struct xattr_handler btrfs_xattr_acl_access_handler = {
337 .prefix = POSIX_ACL_XATTR_ACCESS,
338 .get = btrfs_xattr_acl_access_get,
339 .set = btrfs_xattr_acl_access_set,
342 #else /* CONFIG_FS_POSIX_ACL */
344 int btrfs_acl_chmod(struct inode *inode)
349 int btrfs_init_acl(struct inode *inode, struct inode *dir)
354 int btrfs_check_acl(struct inode *inode, int mask)
359 #endif /* CONFIG_FS_POSIX_ACL */