Fix gcc 4.5.1 miscompiling drivers/char/i8k.c (again)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / acl.c
blob9be949a231e1986c438459b7a1ddcb7168c662a0
1 /*
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.
19 #include <linux/fs.h>
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>
26 #include "ctree.h"
27 #include "btrfs_inode.h"
28 #include "xattr.h"
30 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
32 static struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
34 int size;
35 const char *name;
36 char *value = NULL;
37 struct posix_acl *acl;
39 acl = get_cached_acl(inode, type);
40 if (acl != ACL_NOT_CACHED)
41 return acl;
43 switch (type) {
44 case ACL_TYPE_ACCESS:
45 name = POSIX_ACL_XATTR_ACCESS;
46 break;
47 case ACL_TYPE_DEFAULT:
48 name = POSIX_ACL_XATTR_DEFAULT;
49 break;
50 default:
51 BUG();
54 size = __btrfs_getxattr(inode, name, "", 0);
55 if (size > 0) {
56 value = kzalloc(size, GFP_NOFS);
57 if (!value)
58 return ERR_PTR(-ENOMEM);
59 size = __btrfs_getxattr(inode, name, value, size);
60 if (size > 0) {
61 acl = posix_acl_from_xattr(value, size);
62 set_cached_acl(inode, type, acl);
64 kfree(value);
65 } else if (size == -ENOENT || size == -ENODATA || size == 0) {
66 /* FIXME, who returns -ENOENT? I think nobody */
67 acl = NULL;
68 set_cached_acl(inode, type, acl);
69 } else {
70 acl = ERR_PTR(-EIO);
73 return acl;
76 static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
77 void *value, size_t size, int type)
79 struct posix_acl *acl;
80 int ret = 0;
82 acl = btrfs_get_acl(dentry->d_inode, type);
84 if (IS_ERR(acl))
85 return PTR_ERR(acl);
86 if (acl == NULL)
87 return -ENODATA;
88 ret = posix_acl_to_xattr(acl, value, size);
89 posix_acl_release(acl);
91 return ret;
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)
100 int ret, size = 0;
101 const char *name;
102 char *value = NULL;
103 mode_t mode;
105 if (acl) {
106 ret = posix_acl_valid(acl);
107 if (ret < 0)
108 return ret;
109 ret = 0;
112 switch (type) {
113 case ACL_TYPE_ACCESS:
114 mode = inode->i_mode;
115 name = POSIX_ACL_XATTR_ACCESS;
116 if (acl) {
117 ret = posix_acl_equiv_mode(acl, &mode);
118 if (ret < 0)
119 return ret;
120 inode->i_mode = mode;
122 ret = 0;
123 break;
124 case ACL_TYPE_DEFAULT:
125 if (!S_ISDIR(inode->i_mode))
126 return acl ? -EINVAL : 0;
127 name = POSIX_ACL_XATTR_DEFAULT;
128 break;
129 default:
130 return -EINVAL;
133 if (acl) {
134 size = posix_acl_xattr_size(acl->a_count);
135 value = kmalloc(size, GFP_NOFS);
136 if (!value) {
137 ret = -ENOMEM;
138 goto out;
141 ret = posix_acl_to_xattr(acl, value, size);
142 if (ret < 0)
143 goto out;
146 ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
147 out:
148 kfree(value);
150 if (!ret)
151 set_cached_acl(inode, type, acl);
153 return ret;
156 static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
157 const void *value, size_t size, int flags, int type)
159 int ret;
160 struct posix_acl *acl = NULL;
162 if (!is_owner_or_cap(dentry->d_inode))
163 return -EPERM;
165 if (value) {
166 acl = posix_acl_from_xattr(value, size);
167 if (acl == NULL) {
168 value = NULL;
169 size = 0;
170 } else if (IS_ERR(acl)) {
171 return PTR_ERR(acl);
175 ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
177 posix_acl_release(acl);
179 return ret;
182 int btrfs_check_acl(struct inode *inode, int mask)
184 struct posix_acl *acl;
185 int error = -EAGAIN;
187 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
189 if (IS_ERR(acl))
190 return PTR_ERR(acl);
191 if (acl) {
192 error = posix_acl_permission(inode, acl, mask);
193 posix_acl_release(acl);
196 return error;
200 * btrfs_init_acl is already generally called under fs_mutex, so the locking
201 * stuff has been fixed to work with that. If the locking stuff changes, we
202 * need to re-evaluate the acl locking stuff.
204 int btrfs_init_acl(struct btrfs_trans_handle *trans,
205 struct inode *inode, struct inode *dir)
207 struct posix_acl *acl = NULL;
208 int ret = 0;
210 /* this happens with subvols */
211 if (!dir)
212 return 0;
214 if (!S_ISLNK(inode->i_mode)) {
215 if (IS_POSIXACL(dir)) {
216 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
217 if (IS_ERR(acl))
218 return PTR_ERR(acl);
221 if (!acl)
222 inode->i_mode &= ~current_umask();
225 if (IS_POSIXACL(dir) && acl) {
226 struct posix_acl *clone;
227 mode_t mode;
229 if (S_ISDIR(inode->i_mode)) {
230 ret = btrfs_set_acl(trans, inode, acl,
231 ACL_TYPE_DEFAULT);
232 if (ret)
233 goto failed;
235 clone = posix_acl_clone(acl, GFP_NOFS);
236 ret = -ENOMEM;
237 if (!clone)
238 goto failed;
240 mode = inode->i_mode;
241 ret = posix_acl_create_masq(clone, &mode);
242 if (ret >= 0) {
243 inode->i_mode = mode;
244 if (ret > 0) {
245 /* we need an acl */
246 ret = btrfs_set_acl(trans, inode, clone,
247 ACL_TYPE_ACCESS);
250 posix_acl_release(clone);
252 failed:
253 posix_acl_release(acl);
255 return ret;
258 int btrfs_acl_chmod(struct inode *inode)
260 struct posix_acl *acl, *clone;
261 int ret = 0;
263 if (S_ISLNK(inode->i_mode))
264 return -EOPNOTSUPP;
266 if (!IS_POSIXACL(inode))
267 return 0;
269 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
270 if (IS_ERR(acl) || !acl)
271 return PTR_ERR(acl);
273 clone = posix_acl_clone(acl, GFP_KERNEL);
274 posix_acl_release(acl);
275 if (!clone)
276 return -ENOMEM;
278 ret = posix_acl_chmod_masq(clone, inode->i_mode);
279 if (!ret)
280 ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
282 posix_acl_release(clone);
284 return ret;
287 struct xattr_handler btrfs_xattr_acl_default_handler = {
288 .prefix = POSIX_ACL_XATTR_DEFAULT,
289 .flags = ACL_TYPE_DEFAULT,
290 .get = btrfs_xattr_acl_get,
291 .set = btrfs_xattr_acl_set,
294 struct xattr_handler btrfs_xattr_acl_access_handler = {
295 .prefix = POSIX_ACL_XATTR_ACCESS,
296 .flags = ACL_TYPE_ACCESS,
297 .get = btrfs_xattr_acl_get,
298 .set = btrfs_xattr_acl_set,
301 #else /* CONFIG_BTRFS_FS_POSIX_ACL */
303 int btrfs_acl_chmod(struct inode *inode)
305 return 0;
308 int btrfs_init_acl(struct btrfs_trans_handle *trans,
309 struct inode *inode, struct inode *dir)
311 return 0;
314 #endif /* CONFIG_BTRFS_FS_POSIX_ACL */