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 static void btrfs_update_cached_acl(struct inode *inode,
31 struct posix_acl **p_acl,
32 struct posix_acl *acl)
34 spin_lock(&inode->i_lock);
35 if (*p_acl && *p_acl != BTRFS_ACL_NOT_CACHED)
36 posix_acl_release(*p_acl);
37 *p_acl = posix_acl_dup(acl);
38 spin_unlock(&inode->i_lock);
41 static struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
45 struct posix_acl *acl = NULL, **p_acl;
49 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS;
50 p_acl = &BTRFS_I(inode)->i_acl;
52 case ACL_TYPE_DEFAULT:
53 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
54 p_acl = &BTRFS_I(inode)->i_default_acl;
57 return ERR_PTR(-EINVAL);
60 spin_lock(&inode->i_lock);
61 if (*p_acl != BTRFS_ACL_NOT_CACHED)
62 acl = posix_acl_dup(*p_acl);
63 spin_unlock(&inode->i_lock);
69 size = btrfs_xattr_get(inode, name_index, "", NULL, 0);
71 value = kzalloc(size, GFP_NOFS);
73 return ERR_PTR(-ENOMEM);
74 size = btrfs_xattr_get(inode, name_index, "", value, size);
76 acl = posix_acl_from_xattr(value, size);
77 btrfs_update_cached_acl(inode, p_acl, acl);
80 } else if (size == -ENOENT) {
82 btrfs_update_cached_acl(inode, p_acl, acl);
88 static int btrfs_xattr_get_acl(struct inode *inode, int type,
89 void *value, size_t size)
91 struct posix_acl *acl;
94 acl = btrfs_get_acl(inode, type);
100 ret = posix_acl_to_xattr(acl, value, size);
101 posix_acl_release(acl);
107 * Needs to be called with fs_mutex held
109 static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
111 int ret, name_index = 0, size = 0;
112 struct posix_acl **p_acl;
117 ret = posix_acl_valid(acl);
124 case ACL_TYPE_ACCESS:
125 mode = inode->i_mode;
126 ret = posix_acl_equiv_mode(acl, &mode);
130 inode->i_mode = mode;
131 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS;
132 p_acl = &BTRFS_I(inode)->i_acl;
134 case ACL_TYPE_DEFAULT:
135 if (!S_ISDIR(inode->i_mode))
136 return acl ? -EINVAL : 0;
137 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
138 p_acl = &BTRFS_I(inode)->i_default_acl;
145 size = posix_acl_xattr_size(acl->a_count);
146 value = kmalloc(size, GFP_NOFS);
152 ret = posix_acl_to_xattr(acl, value, size);
157 ret = btrfs_xattr_set(inode, name_index, "", value, size, 0);
164 btrfs_update_cached_acl(inode, p_acl, acl);
169 static int btrfs_xattr_set_acl(struct inode *inode, int type,
170 const void *value, size_t size)
173 struct posix_acl *acl = NULL;
176 acl = posix_acl_from_xattr(value, size);
180 } else if (IS_ERR(acl)) {
185 ret = btrfs_set_acl(inode, acl, type);
187 posix_acl_release(acl);
193 static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
194 void *value, size_t size)
196 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
199 static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
200 const void *value, size_t size, int flags)
202 return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
205 static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
206 void *value, size_t size)
208 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
211 static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
212 const void *value, size_t size, int flags)
214 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
217 int btrfs_check_acl(struct inode *inode, int mask)
219 struct posix_acl *acl;
222 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
227 error = posix_acl_permission(inode, acl, mask);
228 posix_acl_release(acl);
235 * btrfs_init_acl is already generally called under fs_mutex, so the locking
236 * stuff has been fixed to work with that. If the locking stuff changes, we
237 * need to re-evaluate the acl locking stuff.
239 int btrfs_init_acl(struct inode *inode, struct inode *dir)
241 struct posix_acl *acl = NULL;
244 /* this happens with subvols */
248 if (!S_ISLNK(inode->i_mode)) {
249 if (IS_POSIXACL(dir)) {
250 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
256 inode->i_mode &= ~current->fs->umask;
259 if (IS_POSIXACL(dir) && acl) {
260 struct posix_acl *clone;
263 if (S_ISDIR(inode->i_mode)) {
264 ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
268 clone = posix_acl_clone(acl, GFP_NOFS);
273 mode = inode->i_mode;
274 ret = posix_acl_create_masq(clone, &mode);
276 inode->i_mode = mode;
279 ret = btrfs_set_acl(inode, clone,
285 posix_acl_release(acl);
290 int btrfs_acl_chmod(struct inode *inode)
292 struct posix_acl *acl, *clone;
295 if (S_ISLNK(inode->i_mode))
298 if (!IS_POSIXACL(inode))
301 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
302 if (IS_ERR(acl) || !acl)
305 clone = posix_acl_clone(acl, GFP_KERNEL);
306 posix_acl_release(acl);
310 ret = posix_acl_chmod_masq(clone, inode->i_mode);
312 ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
314 posix_acl_release(clone);
319 struct xattr_handler btrfs_xattr_acl_default_handler = {
320 .prefix = POSIX_ACL_XATTR_DEFAULT,
321 .list = btrfs_xattr_generic_list,
322 .get = btrfs_xattr_acl_default_get,
323 .set = btrfs_xattr_acl_default_set,
326 struct xattr_handler btrfs_xattr_acl_access_handler = {
327 .prefix = POSIX_ACL_XATTR_ACCESS,
328 .list = btrfs_xattr_generic_list,
329 .get = btrfs_xattr_acl_access_get,
330 .set = btrfs_xattr_acl_access_set,