blob: 52cbe47022bf659c29b728aa602a7c60f4c570ca [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
Christoph Hellwig431547b2009-11-13 09:52:56 +000076static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
77 void *value, size_t size, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040078{
79 struct posix_acl *acl;
80 int ret = 0;
81
Christoph Hellwig431547b2009-11-13 09:52:56 +000082 acl = btrfs_get_acl(dentry->d_inode, type);
Josef Bacik33268ea2008-07-24 12:16:36 -040083
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 */
97static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
98{
Christoph Hellwig95819c02008-08-28 06:21:17 -040099 int ret, size = 0;
100 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -0400101 char *value = NULL;
102 mode_t mode;
103
104 if (acl) {
105 ret = posix_acl_valid(acl);
106 if (ret < 0)
107 return ret;
108 ret = 0;
109 }
110
111 switch (type) {
112 case ACL_TYPE_ACCESS:
113 mode = inode->i_mode;
114 ret = posix_acl_equiv_mode(acl, &mode);
115 if (ret < 0)
116 return ret;
117 ret = 0;
118 inode->i_mode = mode;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400119 name = POSIX_ACL_XATTR_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -0400120 break;
121 case ACL_TYPE_DEFAULT:
122 if (!S_ISDIR(inode->i_mode))
123 return acl ? -EINVAL : 0;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400124 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -0400125 break;
126 default:
127 return -EINVAL;
128 }
129
130 if (acl) {
131 size = posix_acl_xattr_size(acl->a_count);
132 value = kmalloc(size, GFP_NOFS);
133 if (!value) {
134 ret = -ENOMEM;
135 goto out;
136 }
137
138 ret = posix_acl_to_xattr(acl, value, size);
139 if (ret < 0)
140 goto out;
141 }
142
Christoph Hellwig95819c02008-08-28 06:21:17 -0400143 ret = __btrfs_setxattr(inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400144
145out:
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
Christoph Hellwig431547b2009-11-13 09:52:56 +0000154static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
155 const void *value, size_t size, int flags, int type)
Yan744f52f2008-01-14 13:26:08 -0500156{
157 int ret = 0;
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
Christoph Hellwig431547b2009-11-13 09:52:56 +0000170 ret = btrfs_set_acl(dentry->d_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 -0400177int btrfs_check_acl(struct inode *inode, int mask)
178{
179 struct posix_acl *acl;
180 int error = -EAGAIN;
181
182 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
183
184 if (IS_ERR(acl))
185 return PTR_ERR(acl);
186 if (acl) {
187 error = posix_acl_permission(inode, acl, mask);
188 posix_acl_release(acl);
189 }
190
191 return error;
192}
193
194/*
195 * btrfs_init_acl is already generally called under fs_mutex, so the locking
196 * stuff has been fixed to work with that. If the locking stuff changes, we
197 * need to re-evaluate the acl locking stuff.
198 */
199int btrfs_init_acl(struct inode *inode, struct inode *dir)
200{
201 struct posix_acl *acl = NULL;
202 int ret = 0;
203
204 /* this happens with subvols */
205 if (!dir)
206 return 0;
207
208 if (!S_ISLNK(inode->i_mode)) {
209 if (IS_POSIXACL(dir)) {
210 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
211 if (IS_ERR(acl))
212 return PTR_ERR(acl);
213 }
214
215 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400216 inode->i_mode &= ~current_umask();
Josef Bacik33268ea2008-07-24 12:16:36 -0400217 }
218
219 if (IS_POSIXACL(dir) && acl) {
220 struct posix_acl *clone;
221 mode_t mode;
222
223 if (S_ISDIR(inode->i_mode)) {
224 ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
225 if (ret)
226 goto failed;
227 }
228 clone = posix_acl_clone(acl, GFP_NOFS);
229 ret = -ENOMEM;
230 if (!clone)
231 goto failed;
232
233 mode = inode->i_mode;
234 ret = posix_acl_create_masq(clone, &mode);
235 if (ret >= 0) {
236 inode->i_mode = mode;
237 if (ret > 0) {
238 /* we need an acl */
239 ret = btrfs_set_acl(inode, clone,
240 ACL_TYPE_ACCESS);
241 }
242 }
243 }
244failed:
245 posix_acl_release(acl);
246
247 return ret;
248}
249
250int btrfs_acl_chmod(struct inode *inode)
251{
252 struct posix_acl *acl, *clone;
253 int ret = 0;
254
255 if (S_ISLNK(inode->i_mode))
256 return -EOPNOTSUPP;
257
258 if (!IS_POSIXACL(inode))
259 return 0;
260
261 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
262 if (IS_ERR(acl) || !acl)
263 return PTR_ERR(acl);
264
265 clone = posix_acl_clone(acl, GFP_KERNEL);
266 posix_acl_release(acl);
267 if (!clone)
268 return -ENOMEM;
269
270 ret = posix_acl_chmod_masq(clone, inode->i_mode);
271 if (!ret)
272 ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
273
274 posix_acl_release(clone);
275
276 return ret;
277}
278
Josef Bacik5103e942007-11-16 11:45:54 -0500279struct xattr_handler btrfs_xattr_acl_default_handler = {
280 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000281 .flags = ACL_TYPE_DEFAULT,
282 .get = btrfs_xattr_acl_get,
283 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500284};
285
286struct xattr_handler btrfs_xattr_acl_access_handler = {
287 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000288 .flags = ACL_TYPE_ACCESS,
289 .get = btrfs_xattr_acl_get,
290 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500291};
Josef Bacikeab922e2008-08-28 06:21:15 -0400292
Chris Mason0eda2942009-10-13 13:50:18 -0400293#else /* CONFIG_BTRFS_FS_POSIX_ACL */
Josef Bacikeab922e2008-08-28 06:21:15 -0400294
295int btrfs_acl_chmod(struct inode *inode)
296{
297 return 0;
298}
299
300int btrfs_init_acl(struct inode *inode, struct inode *dir)
301{
302 return 0;
303}
304
Chris Mason0eda2942009-10-13 13:50:18 -0400305#endif /* CONFIG_BTRFS_FS_POSIX_ACL */