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 spin_lock(&inode->i_lock);
64 if (*p_acl != BTRFS_ACL_NOT_CACHED)
65 acl = posix_acl_dup(*p_acl);
66 spin_unlock(&inode->i_lock);
72 size = __btrfs_getxattr(inode, name, "", 0);
74 value = kzalloc(size, GFP_NOFS);
76 return ERR_PTR(-ENOMEM);
77 size = __btrfs_getxattr(inode, name, value, size);
79 acl = posix_acl_from_xattr(value, size);
80 btrfs_update_cached_acl(inode, p_acl, acl);
83 } else if (size == -ENOENT) {
85 btrfs_update_cached_acl(inode, p_acl, acl);
91 static int btrfs_xattr_get_acl(struct inode *inode, int type,
92 void *value, size_t size)
94 struct posix_acl *acl;
97 acl = btrfs_get_acl(inode, type);
103 ret = posix_acl_to_xattr(acl, value, size);
104 posix_acl_release(acl);
110 * Needs to be called with fs_mutex held
112 static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
116 struct posix_acl **p_acl;
121 ret = posix_acl_valid(acl);
128 case ACL_TYPE_ACCESS:
129 mode = inode->i_mode;
130 ret = posix_acl_equiv_mode(acl, &mode);
134 inode->i_mode = mode;
135 name = POSIX_ACL_XATTR_ACCESS;
136 p_acl = &BTRFS_I(inode)->i_acl;
138 case ACL_TYPE_DEFAULT:
139 if (!S_ISDIR(inode->i_mode))
140 return acl ? -EINVAL : 0;
141 name = POSIX_ACL_XATTR_DEFAULT;
142 p_acl = &BTRFS_I(inode)->i_default_acl;
149 size = posix_acl_xattr_size(acl->a_count);
150 value = kmalloc(size, GFP_NOFS);
156 ret = posix_acl_to_xattr(acl, value, size);
161 ret = __btrfs_setxattr(inode, name, value, size, 0);
167 btrfs_update_cached_acl(inode, p_acl, acl);
172 static int btrfs_xattr_set_acl(struct inode *inode, int type,
173 const void *value, size_t size)
176 struct posix_acl *acl = NULL;
179 acl = posix_acl_from_xattr(value, size);
183 } else if (IS_ERR(acl)) {
188 ret = btrfs_set_acl(inode, acl, type);
190 posix_acl_release(acl);
196 static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
197 void *value, size_t size)
199 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
202 static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
203 const void *value, size_t size, int flags)
205 return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
208 static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
209 void *value, size_t size)
211 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
214 static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
215 const void *value, size_t size, int flags)
217 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
220 int btrfs_check_acl(struct inode *inode, int mask)
222 struct posix_acl *acl;
225 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
230 error = posix_acl_permission(inode, acl, mask);
231 posix_acl_release(acl);
238 * btrfs_init_acl is already generally called under fs_mutex, so the locking
239 * stuff has been fixed to work with that. If the locking stuff changes, we
240 * need to re-evaluate the acl locking stuff.
242 int btrfs_init_acl(struct inode *inode, struct inode *dir)
244 struct posix_acl *acl = NULL;
247 /* this happens with subvols */
251 if (!S_ISLNK(inode->i_mode)) {
252 if (IS_POSIXACL(dir)) {
253 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
259 inode->i_mode &= ~current->fs->umask;
262 if (IS_POSIXACL(dir) && acl) {
263 struct posix_acl *clone;
266 if (S_ISDIR(inode->i_mode)) {
267 ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
271 clone = posix_acl_clone(acl, GFP_NOFS);
276 mode = inode->i_mode;
277 ret = posix_acl_create_masq(clone, &mode);
279 inode->i_mode = mode;
282 ret = btrfs_set_acl(inode, clone,
288 posix_acl_release(acl);
293 int btrfs_acl_chmod(struct inode *inode)
295 struct posix_acl *acl, *clone;
298 if (S_ISLNK(inode->i_mode))
301 if (!IS_POSIXACL(inode))
304 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
305 if (IS_ERR(acl) || !acl)
308 clone = posix_acl_clone(acl, GFP_KERNEL);
309 posix_acl_release(acl);
313 ret = posix_acl_chmod_masq(clone, inode->i_mode);
315 ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
317 posix_acl_release(clone);
322 struct xattr_handler btrfs_xattr_acl_default_handler = {
323 .prefix = POSIX_ACL_XATTR_DEFAULT,
324 .get = btrfs_xattr_acl_default_get,
325 .set = btrfs_xattr_acl_default_set,
328 struct xattr_handler btrfs_xattr_acl_access_handler = {
329 .prefix = POSIX_ACL_XATTR_ACCESS,
330 .get = btrfs_xattr_acl_access_get,
331 .set = btrfs_xattr_acl_access_set,
334 #else /* CONFIG_FS_POSIX_ACL */
336 int btrfs_acl_chmod(struct inode *inode)
341 int btrfs_init_acl(struct inode *inode, struct inode *dir)
346 int btrfs_check_acl(struct inode *inode, int mask)
351 #endif /* CONFIG_FS_POSIX_ACL */