Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[pandora-kernel.git] / fs / btrfs / acl.c
1 /*
2  * Copyright (C) 2007 Red Hat.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
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>
25 #include <linux/slab.h>
26
27 #include "ctree.h"
28 #include "btrfs_inode.h"
29 #include "xattr.h"
30
31 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
32
33 struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
34 {
35         int size;
36         const char *name;
37         char *value = NULL;
38         struct posix_acl *acl;
39
40         if (!IS_POSIXACL(inode))
41                 return NULL;
42
43         acl = get_cached_acl(inode, type);
44         if (acl != ACL_NOT_CACHED)
45                 return acl;
46
47         switch (type) {
48         case ACL_TYPE_ACCESS:
49                 name = POSIX_ACL_XATTR_ACCESS;
50                 break;
51         case ACL_TYPE_DEFAULT:
52                 name = POSIX_ACL_XATTR_DEFAULT;
53                 break;
54         default:
55                 BUG();
56         }
57
58         size = __btrfs_getxattr(inode, name, "", 0);
59         if (size > 0) {
60                 value = kzalloc(size, GFP_NOFS);
61                 if (!value)
62                         return ERR_PTR(-ENOMEM);
63                 size = __btrfs_getxattr(inode, name, value, size);
64                 if (size > 0) {
65                         acl = posix_acl_from_xattr(value, size);
66                         if (IS_ERR(acl)) {
67                                 kfree(value);
68                                 return acl;
69                         }
70                         set_cached_acl(inode, type, acl);
71                 }
72                 kfree(value);
73         } else if (size == -ENOENT || size == -ENODATA || size == 0) {
74                 /* FIXME, who returns -ENOENT?  I think nobody */
75                 acl = NULL;
76                 set_cached_acl(inode, type, acl);
77         } else {
78                 acl = ERR_PTR(-EIO);
79         }
80
81         return acl;
82 }
83
84 static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
85                 void *value, size_t size, int type)
86 {
87         struct posix_acl *acl;
88         int ret = 0;
89
90         if (!IS_POSIXACL(dentry->d_inode))
91                 return -EOPNOTSUPP;
92
93         acl = btrfs_get_acl(dentry->d_inode, type);
94
95         if (IS_ERR(acl))
96                 return PTR_ERR(acl);
97         if (acl == NULL)
98                 return -ENODATA;
99         ret = posix_acl_to_xattr(acl, value, size);
100         posix_acl_release(acl);
101
102         return ret;
103 }
104
105 /*
106  * Needs to be called with fs_mutex held
107  */
108 static int btrfs_set_acl(struct btrfs_trans_handle *trans,
109                          struct inode *inode, struct posix_acl *acl, int type)
110 {
111         int ret, size = 0;
112         const char *name;
113         char *value = NULL;
114
115         if (acl) {
116                 ret = posix_acl_valid(acl);
117                 if (ret < 0)
118                         return ret;
119                 ret = 0;
120         }
121
122         switch (type) {
123         case ACL_TYPE_ACCESS:
124                 name = POSIX_ACL_XATTR_ACCESS;
125                 if (acl) {
126                         ret = posix_acl_equiv_mode(acl, &inode->i_mode);
127                         if (ret < 0)
128                                 return ret;
129                 }
130                 ret = 0;
131                 break;
132         case ACL_TYPE_DEFAULT:
133                 if (!S_ISDIR(inode->i_mode))
134                         return acl ? -EINVAL : 0;
135                 name = POSIX_ACL_XATTR_DEFAULT;
136                 break;
137         default:
138                 return -EINVAL;
139         }
140
141         if (acl) {
142                 size = posix_acl_xattr_size(acl->a_count);
143                 value = kmalloc(size, GFP_NOFS);
144                 if (!value) {
145                         ret = -ENOMEM;
146                         goto out;
147                 }
148
149                 ret = posix_acl_to_xattr(acl, value, size);
150                 if (ret < 0)
151                         goto out;
152         }
153
154         ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
155 out:
156         kfree(value);
157
158         if (!ret)
159                 set_cached_acl(inode, type, acl);
160
161         return ret;
162 }
163
164 static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
165                 const void *value, size_t size, int flags, int type)
166 {
167         int ret;
168         struct posix_acl *acl = NULL;
169
170         if (!inode_owner_or_capable(dentry->d_inode))
171                 return -EPERM;
172
173         if (!IS_POSIXACL(dentry->d_inode))
174                 return -EOPNOTSUPP;
175
176         if (value) {
177                 acl = posix_acl_from_xattr(value, size);
178                 if (IS_ERR(acl))
179                         return PTR_ERR(acl);
180
181                 if (acl) {
182                         ret = posix_acl_valid(acl);
183                         if (ret)
184                                 goto out;
185                 }
186         }
187
188         ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
189 out:
190         posix_acl_release(acl);
191
192         return ret;
193 }
194
195 /*
196  * btrfs_init_acl is already generally called under fs_mutex, so the locking
197  * stuff has been fixed to work with that.  If the locking stuff changes, we
198  * need to re-evaluate the acl locking stuff.
199  */
200 int btrfs_init_acl(struct btrfs_trans_handle *trans,
201                    struct inode *inode, struct inode *dir)
202 {
203         struct posix_acl *acl = NULL;
204         int ret = 0;
205
206         /* this happens with subvols */
207         if (!dir)
208                 return 0;
209
210         if (!S_ISLNK(inode->i_mode)) {
211                 if (IS_POSIXACL(dir)) {
212                         acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
213                         if (IS_ERR(acl))
214                                 return PTR_ERR(acl);
215                 }
216
217                 if (!acl)
218                         inode->i_mode &= ~current_umask();
219         }
220
221         if (IS_POSIXACL(dir) && acl) {
222                 if (S_ISDIR(inode->i_mode)) {
223                         ret = btrfs_set_acl(trans, inode, acl,
224                                             ACL_TYPE_DEFAULT);
225                         if (ret)
226                                 goto failed;
227                 }
228                 ret = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
229                 if (ret < 0)
230                         return ret;
231
232                 if (ret > 0) {
233                         /* we need an acl */
234                         ret = btrfs_set_acl(trans, inode, acl, ACL_TYPE_ACCESS);
235                 }
236         }
237 failed:
238         posix_acl_release(acl);
239
240         return ret;
241 }
242
243 int btrfs_acl_chmod(struct inode *inode)
244 {
245         struct posix_acl *acl;
246         int ret = 0;
247
248         if (S_ISLNK(inode->i_mode))
249                 return -EOPNOTSUPP;
250
251         if (!IS_POSIXACL(inode))
252                 return 0;
253
254         acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
255         if (IS_ERR_OR_NULL(acl))
256                 return PTR_ERR(acl);
257
258         ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
259         if (ret)
260                 return ret;
261         ret = btrfs_set_acl(NULL, inode, acl, ACL_TYPE_ACCESS);
262         posix_acl_release(acl);
263         return ret;
264 }
265
266 const struct xattr_handler btrfs_xattr_acl_default_handler = {
267         .prefix = POSIX_ACL_XATTR_DEFAULT,
268         .flags  = ACL_TYPE_DEFAULT,
269         .get    = btrfs_xattr_acl_get,
270         .set    = btrfs_xattr_acl_set,
271 };
272
273 const struct xattr_handler btrfs_xattr_acl_access_handler = {
274         .prefix = POSIX_ACL_XATTR_ACCESS,
275         .flags  = ACL_TYPE_ACCESS,
276         .get    = btrfs_xattr_acl_get,
277         .set    = btrfs_xattr_acl_set,
278 };
279
280 #else /* CONFIG_BTRFS_FS_POSIX_ACL */
281
282 int btrfs_acl_chmod(struct inode *inode)
283 {
284         return 0;
285 }
286
287 int btrfs_init_acl(struct btrfs_trans_handle *trans,
288                    struct inode *inode, struct inode *dir)
289 {
290         return 0;
291 }
292
293 #endif /* CONFIG_BTRFS_FS_POSIX_ACL */