blob: 1898f8555f06cf3284bd9f7317cb75618b97ab53 [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
Chris Mason0eda2942009-10-13 13:50:18 -040030#ifdef CONFIG_BTRFS_FS_POSIX_ACL
Josef Bacikeab922e2008-08-28 06:21:15 -040031
Josef Bacik33268ea2008-07-24 12:16:36 -040032static struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
33{
Christoph Hellwig95819c02008-08-28 06:21:17 -040034 int size;
35 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040036 char *value = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040037 struct posix_acl *acl;
38
39 acl = get_cached_acl(inode, type);
40 if (acl != ACL_NOT_CACHED)
41 return acl;
Josef Bacik33268ea2008-07-24 12:16:36 -040042
43 switch (type) {
44 case ACL_TYPE_ACCESS:
Christoph Hellwig95819c02008-08-28 06:21:17 -040045 name = POSIX_ACL_XATTR_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040046 break;
47 case ACL_TYPE_DEFAULT:
Christoph Hellwig95819c02008-08-28 06:21:17 -040048 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040049 break;
50 default:
Al Viro073aaa12009-06-09 12:11:54 -040051 BUG();
Josef Bacik33268ea2008-07-24 12:16:36 -040052 }
53
Christoph Hellwig95819c02008-08-28 06:21:17 -040054 size = __btrfs_getxattr(inode, name, "", 0);
Josef Bacik33268ea2008-07-24 12:16:36 -040055 if (size > 0) {
56 value = kzalloc(size, GFP_NOFS);
57 if (!value)
58 return ERR_PTR(-ENOMEM);
Christoph Hellwig95819c02008-08-28 06:21:17 -040059 size = __btrfs_getxattr(inode, name, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -040060 if (size > 0) {
61 acl = posix_acl_from_xattr(value, size);
Al Viro073aaa12009-06-09 12:11:54 -040062 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -040063 }
64 kfree(value);
Chris Mason7b1a14b2009-04-27 10:49:53 -040065 } else if (size == -ENOENT || size == -ENODATA || size == 0) {
66 /* FIXME, who returns -ENOENT? I think nobody */
Josef Bacik33268ea2008-07-24 12:16:36 -040067 acl = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040068 set_cached_acl(inode, type, acl);
Chris Mason7b1a14b2009-04-27 10:49:53 -040069 } else {
70 acl = ERR_PTR(-EIO);
Josef Bacik33268ea2008-07-24 12:16:36 -040071 }
72
73 return acl;
74}
75
76static int btrfs_xattr_get_acl(struct inode *inode, int type,
77 void *value, size_t size)
78{
79 struct posix_acl *acl;
80 int ret = 0;
81
82 acl = btrfs_get_acl(inode, type);
83
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);
90
91 return ret;
92}
93
94/*
95 * Needs to be called with fs_mutex held
96 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +000097static int btrfs_set_acl(struct btrfs_trans_handle *trans,
98 struct inode *inode, struct posix_acl *acl, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040099{
Christoph Hellwig95819c02008-08-28 06:21:17 -0400100 int ret, size = 0;
101 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -0400102 char *value = NULL;
103 mode_t mode;
104
105 if (acl) {
106 ret = posix_acl_valid(acl);
107 if (ret < 0)
108 return ret;
109 ret = 0;
110 }
111
112 switch (type) {
113 case ACL_TYPE_ACCESS:
114 mode = inode->i_mode;
115 ret = posix_acl_equiv_mode(acl, &mode);
116 if (ret < 0)
117 return ret;
118 ret = 0;
119 inode->i_mode = mode;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400120 name = POSIX_ACL_XATTR_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -0400121 break;
122 case ACL_TYPE_DEFAULT:
123 if (!S_ISDIR(inode->i_mode))
124 return acl ? -EINVAL : 0;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400125 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -0400126 break;
127 default:
128 return -EINVAL;
129 }
130
131 if (acl) {
132 size = posix_acl_xattr_size(acl->a_count);
133 value = kmalloc(size, GFP_NOFS);
134 if (!value) {
135 ret = -ENOMEM;
136 goto out;
137 }
138
139 ret = posix_acl_to_xattr(acl, value, size);
140 if (ret < 0)
141 goto out;
142 }
143
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000144 ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400145out:
Chris Masond3977122009-01-05 21:25:51 -0500146 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400147
148 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400149 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400150
151 return ret;
152}
Yanfb4bc1e2008-01-17 11:59:51 -0500153
Yan744f52f2008-01-14 13:26:08 -0500154static int btrfs_xattr_set_acl(struct inode *inode, int type,
155 const void *value, size_t size)
156{
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157 int ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400158 struct posix_acl *acl = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500159
Yan744f52f2008-01-14 13:26:08 -0500160 if (value) {
161 acl = posix_acl_from_xattr(value, size);
162 if (acl == NULL) {
163 value = NULL;
164 size = 0;
165 } else if (IS_ERR(acl)) {
Josef Bacik33268ea2008-07-24 12:16:36 -0400166 return PTR_ERR(acl);
Yan744f52f2008-01-14 13:26:08 -0500167 }
Yan744f52f2008-01-14 13:26:08 -0500168 }
Josef Bacik33268ea2008-07-24 12:16:36 -0400169
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000170 ret = btrfs_set_acl(NULL, inode, acl, type);
Josef Bacik33268ea2008-07-24 12:16:36 -0400171
172 posix_acl_release(acl);
173
174 return ret;
Yan744f52f2008-01-14 13:26:08 -0500175}
Josef Bacik1caf9342007-11-19 10:18:17 -0500176
Josef Bacik33268ea2008-07-24 12:16:36 -0400177
Josef Bacik5103e942007-11-16 11:45:54 -0500178static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
179 void *value, size_t size)
180{
Josef Bacik33268ea2008-07-24 12:16:36 -0400181 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500182}
Josef Bacik33268ea2008-07-24 12:16:36 -0400183
Josef Bacik5103e942007-11-16 11:45:54 -0500184static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
185 const void *value, size_t size, int flags)
186{
Josef Bacik33268ea2008-07-24 12:16:36 -0400187 return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500188}
Josef Bacik33268ea2008-07-24 12:16:36 -0400189
Josef Bacik5103e942007-11-16 11:45:54 -0500190static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
191 void *value, size_t size)
192{
Josef Bacik33268ea2008-07-24 12:16:36 -0400193 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500194}
Josef Bacik33268ea2008-07-24 12:16:36 -0400195
Josef Bacik5103e942007-11-16 11:45:54 -0500196static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
Chris Masond3977122009-01-05 21:25:51 -0500197 const void *value, size_t size, int flags)
Josef Bacik5103e942007-11-16 11:45:54 -0500198{
Josef Bacik33268ea2008-07-24 12:16:36 -0400199 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500200}
Josef Bacik33268ea2008-07-24 12:16:36 -0400201
202int btrfs_check_acl(struct inode *inode, int mask)
203{
204 struct posix_acl *acl;
205 int error = -EAGAIN;
206
207 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
208
209 if (IS_ERR(acl))
210 return PTR_ERR(acl);
211 if (acl) {
212 error = posix_acl_permission(inode, acl, mask);
213 posix_acl_release(acl);
214 }
215
216 return error;
217}
218
219/*
220 * btrfs_init_acl is already generally called under fs_mutex, so the locking
221 * stuff has been fixed to work with that. If the locking stuff changes, we
222 * need to re-evaluate the acl locking stuff.
223 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000224int btrfs_init_acl(struct btrfs_trans_handle *trans,
225 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400226{
227 struct posix_acl *acl = NULL;
228 int ret = 0;
229
230 /* this happens with subvols */
231 if (!dir)
232 return 0;
233
234 if (!S_ISLNK(inode->i_mode)) {
235 if (IS_POSIXACL(dir)) {
236 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
237 if (IS_ERR(acl))
238 return PTR_ERR(acl);
239 }
240
241 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400242 inode->i_mode &= ~current_umask();
Josef Bacik33268ea2008-07-24 12:16:36 -0400243 }
244
245 if (IS_POSIXACL(dir) && acl) {
246 struct posix_acl *clone;
247 mode_t mode;
248
249 if (S_ISDIR(inode->i_mode)) {
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000250 ret = btrfs_set_acl(trans, inode, acl,
251 ACL_TYPE_DEFAULT);
Josef Bacik33268ea2008-07-24 12:16:36 -0400252 if (ret)
253 goto failed;
254 }
255 clone = posix_acl_clone(acl, GFP_NOFS);
256 ret = -ENOMEM;
257 if (!clone)
258 goto failed;
259
260 mode = inode->i_mode;
261 ret = posix_acl_create_masq(clone, &mode);
262 if (ret >= 0) {
263 inode->i_mode = mode;
264 if (ret > 0) {
265 /* we need an acl */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000266 ret = btrfs_set_acl(trans, inode, clone,
Josef Bacik33268ea2008-07-24 12:16:36 -0400267 ACL_TYPE_ACCESS);
268 }
269 }
270 }
271failed:
272 posix_acl_release(acl);
273
274 return ret;
275}
276
277int btrfs_acl_chmod(struct inode *inode)
278{
279 struct posix_acl *acl, *clone;
280 int ret = 0;
281
282 if (S_ISLNK(inode->i_mode))
283 return -EOPNOTSUPP;
284
285 if (!IS_POSIXACL(inode))
286 return 0;
287
288 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
289 if (IS_ERR(acl) || !acl)
290 return PTR_ERR(acl);
291
292 clone = posix_acl_clone(acl, GFP_KERNEL);
293 posix_acl_release(acl);
294 if (!clone)
295 return -ENOMEM;
296
297 ret = posix_acl_chmod_masq(clone, inode->i_mode);
298 if (!ret)
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000299 ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
Josef Bacik33268ea2008-07-24 12:16:36 -0400300
301 posix_acl_release(clone);
302
303 return ret;
304}
305
Josef Bacik5103e942007-11-16 11:45:54 -0500306struct xattr_handler btrfs_xattr_acl_default_handler = {
307 .prefix = POSIX_ACL_XATTR_DEFAULT,
Josef Bacik5103e942007-11-16 11:45:54 -0500308 .get = btrfs_xattr_acl_default_get,
309 .set = btrfs_xattr_acl_default_set,
310};
311
312struct xattr_handler btrfs_xattr_acl_access_handler = {
313 .prefix = POSIX_ACL_XATTR_ACCESS,
Josef Bacik5103e942007-11-16 11:45:54 -0500314 .get = btrfs_xattr_acl_access_get,
315 .set = btrfs_xattr_acl_access_set,
316};
Josef Bacikeab922e2008-08-28 06:21:15 -0400317
Chris Mason0eda2942009-10-13 13:50:18 -0400318#else /* CONFIG_BTRFS_FS_POSIX_ACL */
Josef Bacikeab922e2008-08-28 06:21:15 -0400319
320int btrfs_acl_chmod(struct inode *inode)
321{
322 return 0;
323}
324
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000325int btrfs_init_acl(struct btrfs_trans_handle *trans,
326 struct inode *inode, struct inode *dir)
Josef Bacikeab922e2008-08-28 06:21:15 -0400327{
328 return 0;
329}
330
Chris Mason0eda2942009-10-13 13:50:18 -0400331#endif /* CONFIG_BTRFS_FS_POSIX_ACL */