blob: 867eaf1f8efb8a9abc3d442012c3ecef6121b7d4 [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{
Christoph Hellwig95819c02008-08-28 06:21:17 -040045 int size;
46 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040047 char *value = NULL;
48 struct posix_acl *acl = NULL, **p_acl;
49
50 switch (type) {
51 case ACL_TYPE_ACCESS:
Christoph Hellwig95819c02008-08-28 06:21:17 -040052 name = POSIX_ACL_XATTR_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040053 p_acl = &BTRFS_I(inode)->i_acl;
54 break;
55 case ACL_TYPE_DEFAULT:
Christoph Hellwig95819c02008-08-28 06:21:17 -040056 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040057 p_acl = &BTRFS_I(inode)->i_default_acl;
58 break;
59 default:
60 return ERR_PTR(-EINVAL);
61 }
62
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);
67
68 if (acl)
69 return acl;
70
71
Christoph Hellwig95819c02008-08-28 06:21:17 -040072 size = __btrfs_getxattr(inode, name, "", 0);
Josef Bacik33268ea2008-07-24 12:16:36 -040073 if (size > 0) {
74 value = kzalloc(size, GFP_NOFS);
75 if (!value)
76 return ERR_PTR(-ENOMEM);
Christoph Hellwig95819c02008-08-28 06:21:17 -040077 size = __btrfs_getxattr(inode, name, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -040078 if (size > 0) {
79 acl = posix_acl_from_xattr(value, size);
80 btrfs_update_cached_acl(inode, p_acl, acl);
81 }
82 kfree(value);
83 } else if (size == -ENOENT) {
84 acl = NULL;
85 btrfs_update_cached_acl(inode, p_acl, acl);
86 }
87
88 return acl;
89}
90
91static int btrfs_xattr_get_acl(struct inode *inode, int type,
92 void *value, size_t size)
93{
94 struct posix_acl *acl;
95 int ret = 0;
96
97 acl = btrfs_get_acl(inode, type);
98
99 if (IS_ERR(acl))
100 return PTR_ERR(acl);
101 if (acl == NULL)
102 return -ENODATA;
103 ret = posix_acl_to_xattr(acl, value, size);
104 posix_acl_release(acl);
105
106 return ret;
107}
108
109/*
110 * Needs to be called with fs_mutex held
111 */
112static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
113{
Christoph Hellwig95819c02008-08-28 06:21:17 -0400114 int ret, size = 0;
115 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -0400116 struct posix_acl **p_acl;
117 char *value = NULL;
118 mode_t mode;
119
120 if (acl) {
121 ret = posix_acl_valid(acl);
122 if (ret < 0)
123 return ret;
124 ret = 0;
125 }
126
127 switch (type) {
128 case ACL_TYPE_ACCESS:
129 mode = inode->i_mode;
130 ret = posix_acl_equiv_mode(acl, &mode);
131 if (ret < 0)
132 return ret;
133 ret = 0;
134 inode->i_mode = mode;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400135 name = POSIX_ACL_XATTR_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -0400136 p_acl = &BTRFS_I(inode)->i_acl;
137 break;
138 case ACL_TYPE_DEFAULT:
139 if (!S_ISDIR(inode->i_mode))
140 return acl ? -EINVAL : 0;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400141 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -0400142 p_acl = &BTRFS_I(inode)->i_default_acl;
143 break;
144 default:
145 return -EINVAL;
146 }
147
148 if (acl) {
149 size = posix_acl_xattr_size(acl->a_count);
150 value = kmalloc(size, GFP_NOFS);
151 if (!value) {
152 ret = -ENOMEM;
153 goto out;
154 }
155
156 ret = posix_acl_to_xattr(acl, value, size);
157 if (ret < 0)
158 goto out;
159 }
160
Christoph Hellwig95819c02008-08-28 06:21:17 -0400161 ret = __btrfs_setxattr(inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400162
163out:
164 if (value)
165 kfree(value);
166
167 if (!ret)
168 btrfs_update_cached_acl(inode, p_acl, acl);
169
170 return ret;
171}
Yanfb4bc1e2008-01-17 11:59:51 -0500172
Yan744f52f2008-01-14 13:26:08 -0500173static int btrfs_xattr_set_acl(struct inode *inode, int type,
174 const void *value, size_t size)
175{
176 int ret = 0;
Josef Bacik33268ea2008-07-24 12:16:36 -0400177 struct posix_acl *acl = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500178
Yan744f52f2008-01-14 13:26:08 -0500179 if (value) {
180 acl = posix_acl_from_xattr(value, size);
181 if (acl == NULL) {
182 value = NULL;
183 size = 0;
184 } else if (IS_ERR(acl)) {
Josef Bacik33268ea2008-07-24 12:16:36 -0400185 return PTR_ERR(acl);
Yan744f52f2008-01-14 13:26:08 -0500186 }
Yan744f52f2008-01-14 13:26:08 -0500187 }
Josef Bacik33268ea2008-07-24 12:16:36 -0400188
189 ret = btrfs_set_acl(inode, acl, type);
190
191 posix_acl_release(acl);
192
193 return ret;
Yan744f52f2008-01-14 13:26:08 -0500194}
Josef Bacik1caf9342007-11-19 10:18:17 -0500195
Josef Bacik33268ea2008-07-24 12:16:36 -0400196
Josef Bacik5103e942007-11-16 11:45:54 -0500197static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
198 void *value, size_t size)
199{
Josef Bacik33268ea2008-07-24 12:16:36 -0400200 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500201}
Josef Bacik33268ea2008-07-24 12:16:36 -0400202
Josef Bacik5103e942007-11-16 11:45:54 -0500203static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
204 const void *value, size_t size, int flags)
205{
Josef Bacik33268ea2008-07-24 12:16:36 -0400206 return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500207}
Josef Bacik33268ea2008-07-24 12:16:36 -0400208
Josef Bacik5103e942007-11-16 11:45:54 -0500209static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
210 void *value, size_t size)
211{
Josef Bacik33268ea2008-07-24 12:16:36 -0400212 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500213}
Josef Bacik33268ea2008-07-24 12:16:36 -0400214
Josef Bacik5103e942007-11-16 11:45:54 -0500215static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
216 const void *value, size_t size, int flags)
217{
Josef Bacik33268ea2008-07-24 12:16:36 -0400218 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500219}
Josef Bacik33268ea2008-07-24 12:16:36 -0400220
221int btrfs_check_acl(struct inode *inode, int mask)
222{
223 struct posix_acl *acl;
224 int error = -EAGAIN;
225
226 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
227
228 if (IS_ERR(acl))
229 return PTR_ERR(acl);
230 if (acl) {
231 error = posix_acl_permission(inode, acl, mask);
232 posix_acl_release(acl);
233 }
234
235 return error;
236}
237
238/*
239 * btrfs_init_acl is already generally called under fs_mutex, so the locking
240 * stuff has been fixed to work with that. If the locking stuff changes, we
241 * need to re-evaluate the acl locking stuff.
242 */
243int btrfs_init_acl(struct inode *inode, struct inode *dir)
244{
245 struct posix_acl *acl = NULL;
246 int ret = 0;
247
248 /* this happens with subvols */
249 if (!dir)
250 return 0;
251
252 if (!S_ISLNK(inode->i_mode)) {
253 if (IS_POSIXACL(dir)) {
254 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
255 if (IS_ERR(acl))
256 return PTR_ERR(acl);
257 }
258
259 if (!acl)
260 inode->i_mode &= ~current->fs->umask;
261 }
262
263 if (IS_POSIXACL(dir) && acl) {
264 struct posix_acl *clone;
265 mode_t mode;
266
267 if (S_ISDIR(inode->i_mode)) {
268 ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
269 if (ret)
270 goto failed;
271 }
272 clone = posix_acl_clone(acl, GFP_NOFS);
273 ret = -ENOMEM;
274 if (!clone)
275 goto failed;
276
277 mode = inode->i_mode;
278 ret = posix_acl_create_masq(clone, &mode);
279 if (ret >= 0) {
280 inode->i_mode = mode;
281 if (ret > 0) {
282 /* we need an acl */
283 ret = btrfs_set_acl(inode, clone,
284 ACL_TYPE_ACCESS);
285 }
286 }
287 }
288failed:
289 posix_acl_release(acl);
290
291 return ret;
292}
293
294int btrfs_acl_chmod(struct inode *inode)
295{
296 struct posix_acl *acl, *clone;
297 int ret = 0;
298
299 if (S_ISLNK(inode->i_mode))
300 return -EOPNOTSUPP;
301
302 if (!IS_POSIXACL(inode))
303 return 0;
304
305 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
306 if (IS_ERR(acl) || !acl)
307 return PTR_ERR(acl);
308
309 clone = posix_acl_clone(acl, GFP_KERNEL);
310 posix_acl_release(acl);
311 if (!clone)
312 return -ENOMEM;
313
314 ret = posix_acl_chmod_masq(clone, inode->i_mode);
315 if (!ret)
316 ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
317
318 posix_acl_release(clone);
319
320 return ret;
321}
322
Josef Bacik5103e942007-11-16 11:45:54 -0500323struct xattr_handler btrfs_xattr_acl_default_handler = {
324 .prefix = POSIX_ACL_XATTR_DEFAULT,
Josef Bacik5103e942007-11-16 11:45:54 -0500325 .get = btrfs_xattr_acl_default_get,
326 .set = btrfs_xattr_acl_default_set,
327};
328
329struct xattr_handler btrfs_xattr_acl_access_handler = {
330 .prefix = POSIX_ACL_XATTR_ACCESS,
Josef Bacik5103e942007-11-16 11:45:54 -0500331 .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 */