blob: b95147ef1c777dd349839951e76528f0d7eb39a1 [file] [log] [blame]
Josef Bacik5103e942007-11-16 11:45:54 -05001/*
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>
Josef Bacik33268ea2008-07-24 12:16:36 -040023#include <linux/posix_acl.h>
Chris Masonc1e32da2008-01-22 12:46:56 -050024#include <linux/sched.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040025
Josef Bacik5103e942007-11-16 11:45:54 -050026#include "ctree.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040027#include "btrfs_inode.h"
Josef Bacik5103e942007-11-16 11:45:54 -050028#include "xattr.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040029
Josef Bacikeab922e2008-08-28 06:21:15 -040030#ifdef CONFIG_FS_POSIX_ACL
31
Josef Bacik33268ea2008-07-24 12:16:36 -040032static void btrfs_update_cached_acl(struct inode *inode,
33 struct posix_acl **p_acl,
34 struct posix_acl *acl)
35{
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);
41}
42
43static struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
44{
45 int size, name_index;
46 char *value = NULL;
47 struct posix_acl *acl = NULL, **p_acl;
48
49 switch (type) {
50 case ACL_TYPE_ACCESS:
51 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS;
52 p_acl = &BTRFS_I(inode)->i_acl;
53 break;
54 case ACL_TYPE_DEFAULT:
55 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
56 p_acl = &BTRFS_I(inode)->i_default_acl;
57 break;
58 default:
59 return ERR_PTR(-EINVAL);
60 }
61
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);
66
67 if (acl)
68 return acl;
69
70
71 size = btrfs_xattr_get(inode, name_index, "", NULL, 0);
72 if (size > 0) {
73 value = kzalloc(size, GFP_NOFS);
74 if (!value)
75 return ERR_PTR(-ENOMEM);
76 size = btrfs_xattr_get(inode, name_index, "", value, size);
77 if (size > 0) {
78 acl = posix_acl_from_xattr(value, size);
79 btrfs_update_cached_acl(inode, p_acl, acl);
80 }
81 kfree(value);
82 } else if (size == -ENOENT) {
83 acl = NULL;
84 btrfs_update_cached_acl(inode, p_acl, acl);
85 }
86
87 return acl;
88}
89
90static int btrfs_xattr_get_acl(struct inode *inode, int type,
91 void *value, size_t size)
92{
93 struct posix_acl *acl;
94 int ret = 0;
95
96 acl = btrfs_get_acl(inode, type);
97
98 if (IS_ERR(acl))
99 return PTR_ERR(acl);
100 if (acl == NULL)
101 return -ENODATA;
102 ret = posix_acl_to_xattr(acl, value, size);
103 posix_acl_release(acl);
104
105 return ret;
106}
107
108/*
109 * Needs to be called with fs_mutex held
110 */
111static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
112{
113 int ret, name_index = 0, size = 0;
114 struct posix_acl **p_acl;
115 char *value = NULL;
116 mode_t mode;
117
118 if (acl) {
119 ret = posix_acl_valid(acl);
120 if (ret < 0)
121 return ret;
122 ret = 0;
123 }
124
125 switch (type) {
126 case ACL_TYPE_ACCESS:
127 mode = inode->i_mode;
128 ret = posix_acl_equiv_mode(acl, &mode);
129 if (ret < 0)
130 return ret;
131 ret = 0;
132 inode->i_mode = mode;
133 name_index = BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS;
134 p_acl = &BTRFS_I(inode)->i_acl;
135 break;
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;
141 break;
142 default:
143 return -EINVAL;
144 }
145
146 if (acl) {
147 size = posix_acl_xattr_size(acl->a_count);
148 value = kmalloc(size, GFP_NOFS);
149 if (!value) {
150 ret = -ENOMEM;
151 goto out;
152 }
153
154 ret = posix_acl_to_xattr(acl, value, size);
155 if (ret < 0)
156 goto out;
157 }
158
159 ret = btrfs_xattr_set(inode, name_index, "", value, size, 0);
160
161out:
162 if (value)
163 kfree(value);
164
165 if (!ret)
166 btrfs_update_cached_acl(inode, p_acl, acl);
167
168 return ret;
169}
Yanfb4bc1e2008-01-17 11:59:51 -0500170
Yan744f52f2008-01-14 13:26:08 -0500171static int btrfs_xattr_set_acl(struct inode *inode, int type,
172 const void *value, size_t size)
173{
174 int ret = 0;
Josef Bacik33268ea2008-07-24 12:16:36 -0400175 struct posix_acl *acl = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500176
Yan744f52f2008-01-14 13:26:08 -0500177 if (value) {
178 acl = posix_acl_from_xattr(value, size);
179 if (acl == NULL) {
180 value = NULL;
181 size = 0;
182 } else if (IS_ERR(acl)) {
Josef Bacik33268ea2008-07-24 12:16:36 -0400183 return PTR_ERR(acl);
Yan744f52f2008-01-14 13:26:08 -0500184 }
Yan744f52f2008-01-14 13:26:08 -0500185 }
Josef Bacik33268ea2008-07-24 12:16:36 -0400186
187 ret = btrfs_set_acl(inode, acl, type);
188
189 posix_acl_release(acl);
190
191 return ret;
Yan744f52f2008-01-14 13:26:08 -0500192}
Josef Bacik1caf9342007-11-19 10:18:17 -0500193
Josef Bacik33268ea2008-07-24 12:16:36 -0400194
Josef Bacik5103e942007-11-16 11:45:54 -0500195static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
196 void *value, size_t size)
197{
Josef Bacik33268ea2008-07-24 12:16:36 -0400198 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500199}
Josef Bacik33268ea2008-07-24 12:16:36 -0400200
Josef Bacik5103e942007-11-16 11:45:54 -0500201static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
202 const void *value, size_t size, int flags)
203{
Josef Bacik33268ea2008-07-24 12:16:36 -0400204 return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500205}
Josef Bacik33268ea2008-07-24 12:16:36 -0400206
Josef Bacik5103e942007-11-16 11:45:54 -0500207static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
208 void *value, size_t size)
209{
Josef Bacik33268ea2008-07-24 12:16:36 -0400210 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500211}
Josef Bacik33268ea2008-07-24 12:16:36 -0400212
Josef Bacik5103e942007-11-16 11:45:54 -0500213static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
214 const void *value, size_t size, int flags)
215{
Josef Bacik33268ea2008-07-24 12:16:36 -0400216 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500217}
Josef Bacik33268ea2008-07-24 12:16:36 -0400218
219int btrfs_check_acl(struct inode *inode, int mask)
220{
221 struct posix_acl *acl;
222 int error = -EAGAIN;
223
224 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
225
226 if (IS_ERR(acl))
227 return PTR_ERR(acl);
228 if (acl) {
229 error = posix_acl_permission(inode, acl, mask);
230 posix_acl_release(acl);
231 }
232
233 return error;
234}
235
236/*
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.
240 */
241int btrfs_init_acl(struct inode *inode, struct inode *dir)
242{
243 struct posix_acl *acl = NULL;
244 int ret = 0;
245
246 /* this happens with subvols */
247 if (!dir)
248 return 0;
249
250 if (!S_ISLNK(inode->i_mode)) {
251 if (IS_POSIXACL(dir)) {
252 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
253 if (IS_ERR(acl))
254 return PTR_ERR(acl);
255 }
256
257 if (!acl)
258 inode->i_mode &= ~current->fs->umask;
259 }
260
261 if (IS_POSIXACL(dir) && acl) {
262 struct posix_acl *clone;
263 mode_t mode;
264
265 if (S_ISDIR(inode->i_mode)) {
266 ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
267 if (ret)
268 goto failed;
269 }
270 clone = posix_acl_clone(acl, GFP_NOFS);
271 ret = -ENOMEM;
272 if (!clone)
273 goto failed;
274
275 mode = inode->i_mode;
276 ret = posix_acl_create_masq(clone, &mode);
277 if (ret >= 0) {
278 inode->i_mode = mode;
279 if (ret > 0) {
280 /* we need an acl */
281 ret = btrfs_set_acl(inode, clone,
282 ACL_TYPE_ACCESS);
283 }
284 }
285 }
286failed:
287 posix_acl_release(acl);
288
289 return ret;
290}
291
292int btrfs_acl_chmod(struct inode *inode)
293{
294 struct posix_acl *acl, *clone;
295 int ret = 0;
296
297 if (S_ISLNK(inode->i_mode))
298 return -EOPNOTSUPP;
299
300 if (!IS_POSIXACL(inode))
301 return 0;
302
303 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
304 if (IS_ERR(acl) || !acl)
305 return PTR_ERR(acl);
306
307 clone = posix_acl_clone(acl, GFP_KERNEL);
308 posix_acl_release(acl);
309 if (!clone)
310 return -ENOMEM;
311
312 ret = posix_acl_chmod_masq(clone, inode->i_mode);
313 if (!ret)
314 ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
315
316 posix_acl_release(clone);
317
318 return ret;
319}
320
Josef Bacik5103e942007-11-16 11:45:54 -0500321struct xattr_handler btrfs_xattr_acl_default_handler = {
322 .prefix = POSIX_ACL_XATTR_DEFAULT,
323 .list = btrfs_xattr_generic_list,
324 .get = btrfs_xattr_acl_default_get,
325 .set = btrfs_xattr_acl_default_set,
326};
327
328struct xattr_handler btrfs_xattr_acl_access_handler = {
329 .prefix = POSIX_ACL_XATTR_ACCESS,
330 .list = btrfs_xattr_generic_list,
331 .get = btrfs_xattr_acl_access_get,
332 .set = btrfs_xattr_acl_access_set,
333};
Josef Bacikeab922e2008-08-28 06:21:15 -0400334
335#else /* CONFIG_FS_POSIX_ACL */
336
337int btrfs_acl_chmod(struct inode *inode)
338{
339 return 0;
340}
341
342int btrfs_init_acl(struct inode *inode, struct inode *dir)
343{
344 return 0;
345}
346
347int btrfs_check_acl(struct inode *inode, int mask)
348{
349 return 0;
350}
351
352#endif /* CONFIG_FS_POSIX_ACL */