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)
47 struct posix_acl *acl = NULL, **p_acl;
51 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS;
52 p_acl = &BTRFS_I(inode)->i_acl;
54 case ACL_TYPE_DEFAULT:
55 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
56 p_acl = &BTRFS_I(inode)->i_default_acl;
59 return ERR_PTR(-EINVAL);
62 spin_lock(&inode->i_lock);
63 if (*p_acl != BTRFS_ACL_NOT_CACHED)
64 acl = posix_acl_dup(*p_acl);
65 spin_unlock(&inode->i_lock);
71 size = btrfs_xattr_get(inode, name_index, "", NULL, 0);
73 value = kzalloc(size, GFP_NOFS);
75 return ERR_PTR(-ENOMEM);
76 size = btrfs_xattr_get(inode, name_index, "", value, size);
78 acl = posix_acl_from_xattr(value, size);
79 btrfs_update_cached_acl(inode, p_acl, acl);
82 } else if (size == -ENOENT) {
84 btrfs_update_cached_acl(inode, p_acl, acl);
90 static int btrfs_xattr_get_acl(struct inode *inode, int type,
91 void *value, size_t size)
93 struct posix_acl *acl;
96 acl = btrfs_get_acl(inode, type);
102 ret = posix_acl_to_xattr(acl, value, size);
103 posix_acl_release(acl);
109 * Needs to be called with fs_mutex held
111 static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
113 int ret, name_index = 0, size = 0;
114 struct posix_acl **p_acl;
119 ret = posix_acl_valid(acl);
126 case ACL_TYPE_ACCESS:
127 mode = inode->i_mode;
128 ret = posix_acl_equiv_mode(acl, &mode);
132 inode->i_mode = mode;
133 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS;
134 p_acl = &BTRFS_I(inode)->i_acl;
136 case ACL_TYPE_DEFAULT:
137 if (!S_ISDIR(inode->i_mode))
138 return acl ? -EINVAL : 0;
139 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
140 p_acl = &BTRFS_I(inode)->i_default_acl;
147 size = posix_acl_xattr_size(acl->a_count);
148 value = kmalloc(size, GFP_NOFS);
154 ret = posix_acl_to_xattr(acl, value, size);
159 ret = btrfs_xattr_set(inode, name_index, "", value, size, 0);
166 btrfs_update_cached_acl(inode, p_acl, acl);
171 static int btrfs_xattr_set_acl(struct inode *inode, int type,
172 const void *value, size_t size)
175 struct posix_acl *acl = NULL;
178 acl = posix_acl_from_xattr(value, size);
182 } else if (IS_ERR(acl)) {
187 ret = btrfs_set_acl(inode, acl, type);
189 posix_acl_release(acl);
195 static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
196 void *value, size_t size)
198 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
201 static int btrfs_xattr_acl_access_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_ACCESS, value, size);
207 static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
208 void *value, size_t size)
210 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
213 static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
214 const void *value, size_t size, int flags)
216 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
219 int btrfs_check_acl(struct inode *inode, int mask)
221 struct posix_acl *acl;
224 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
229 error = posix_acl_permission(inode, acl, mask);
230 posix_acl_release(acl);
237 * btrfs_init_acl is already generally called under fs_mutex, so the locking
238 * stuff has been fixed to work with that. If the locking stuff changes, we
239 * need to re-evaluate the acl locking stuff.
241 int btrfs_init_acl(struct inode *inode, struct inode *dir)
243 struct posix_acl *acl = NULL;
246 /* this happens with subvols */
250 if (!S_ISLNK(inode->i_mode)) {
251 if (IS_POSIXACL(dir)) {
252 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
258 inode->i_mode &= ~current->fs->umask;
261 if (IS_POSIXACL(dir) && acl) {
262 struct posix_acl *clone;
265 if (S_ISDIR(inode->i_mode)) {
266 ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
270 clone = posix_acl_clone(acl, GFP_NOFS);
275 mode = inode->i_mode;
276 ret = posix_acl_create_masq(clone, &mode);
278 inode->i_mode = mode;
281 ret = btrfs_set_acl(inode, clone,
287 posix_acl_release(acl);
292 int btrfs_acl_chmod(struct inode *inode)
294 struct posix_acl *acl, *clone;
297 if (S_ISLNK(inode->i_mode))
300 if (!IS_POSIXACL(inode))
303 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
304 if (IS_ERR(acl) || !acl)
307 clone = posix_acl_clone(acl, GFP_KERNEL);
308 posix_acl_release(acl);
312 ret = posix_acl_chmod_masq(clone, inode->i_mode);
314 ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
316 posix_acl_release(clone);
321 struct xattr_handler btrfs_xattr_acl_default_handler = {
322 .prefix = POSIX_ACL_XATTR_DEFAULT,
323 .get = btrfs_xattr_acl_default_get,
324 .set = btrfs_xattr_acl_default_set,
327 struct xattr_handler btrfs_xattr_acl_access_handler = {
328 .prefix = POSIX_ACL_XATTR_ACCESS,
329 .get = btrfs_xattr_acl_access_get,
330 .set = btrfs_xattr_acl_access_set,
333 #else /* CONFIG_FS_POSIX_ACL */
335 int btrfs_acl_chmod(struct inode *inode)
340 int btrfs_init_acl(struct inode *inode, struct inode *dir)
345 int btrfs_check_acl(struct inode *inode, int mask)
350 #endif /* CONFIG_FS_POSIX_ACL */