blob: fa44e92e9b8fa437c6d18fc4643fdbc66bde7905 [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;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400115 name = POSIX_ACL_XATTR_ACCESS;
Chris Masona9cc71a2010-01-17 20:36:18 -0500116 if (acl) {
117 ret = posix_acl_equiv_mode(acl, &mode);
118 if (ret < 0)
119 return ret;
120 inode->i_mode = mode;
121 }
122 ret = 0;
Josef Bacik33268ea2008-07-24 12:16:36 -0400123 break;
124 case ACL_TYPE_DEFAULT:
125 if (!S_ISDIR(inode->i_mode))
126 return acl ? -EINVAL : 0;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400127 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -0400128 break;
129 default:
130 return -EINVAL;
131 }
132
133 if (acl) {
134 size = posix_acl_xattr_size(acl->a_count);
135 value = kmalloc(size, GFP_NOFS);
136 if (!value) {
137 ret = -ENOMEM;
138 goto out;
139 }
140
141 ret = posix_acl_to_xattr(acl, value, size);
142 if (ret < 0)
143 goto out;
144 }
145
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000146 ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400147out:
Chris Masond3977122009-01-05 21:25:51 -0500148 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400149
150 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400151 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400152
153 return ret;
154}
Yanfb4bc1e2008-01-17 11:59:51 -0500155
Yan744f52f2008-01-14 13:26:08 -0500156static int btrfs_xattr_set_acl(struct inode *inode, int type,
157 const void *value, size_t size)
158{
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000159 int ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400160 struct posix_acl *acl = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500161
Yan744f52f2008-01-14 13:26:08 -0500162 if (value) {
163 acl = posix_acl_from_xattr(value, size);
164 if (acl == NULL) {
165 value = NULL;
166 size = 0;
167 } else if (IS_ERR(acl)) {
Josef Bacik33268ea2008-07-24 12:16:36 -0400168 return PTR_ERR(acl);
Yan744f52f2008-01-14 13:26:08 -0500169 }
Yan744f52f2008-01-14 13:26:08 -0500170 }
Josef Bacik33268ea2008-07-24 12:16:36 -0400171
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000172 ret = btrfs_set_acl(NULL, inode, acl, type);
Josef Bacik33268ea2008-07-24 12:16:36 -0400173
174 posix_acl_release(acl);
175
176 return ret;
Yan744f52f2008-01-14 13:26:08 -0500177}
Josef Bacik1caf9342007-11-19 10:18:17 -0500178
Josef Bacik33268ea2008-07-24 12:16:36 -0400179
Josef Bacik5103e942007-11-16 11:45:54 -0500180static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
181 void *value, size_t size)
182{
Josef Bacik33268ea2008-07-24 12:16:36 -0400183 return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500184}
Josef Bacik33268ea2008-07-24 12:16:36 -0400185
Josef Bacik5103e942007-11-16 11:45:54 -0500186static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
187 const void *value, size_t size, int flags)
188{
Josef Bacik33268ea2008-07-24 12:16:36 -0400189 return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500190}
Josef Bacik33268ea2008-07-24 12:16:36 -0400191
Josef Bacik5103e942007-11-16 11:45:54 -0500192static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
193 void *value, size_t size)
194{
Josef Bacik33268ea2008-07-24 12:16:36 -0400195 return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500196}
Josef Bacik33268ea2008-07-24 12:16:36 -0400197
Josef Bacik5103e942007-11-16 11:45:54 -0500198static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
Chris Masond3977122009-01-05 21:25:51 -0500199 const void *value, size_t size, int flags)
Josef Bacik5103e942007-11-16 11:45:54 -0500200{
Josef Bacik33268ea2008-07-24 12:16:36 -0400201 return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
Josef Bacik5103e942007-11-16 11:45:54 -0500202}
Josef Bacik33268ea2008-07-24 12:16:36 -0400203
204int btrfs_check_acl(struct inode *inode, int mask)
205{
206 struct posix_acl *acl;
207 int error = -EAGAIN;
208
209 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
210
211 if (IS_ERR(acl))
212 return PTR_ERR(acl);
213 if (acl) {
214 error = posix_acl_permission(inode, acl, mask);
215 posix_acl_release(acl);
216 }
217
218 return error;
219}
220
221/*
222 * btrfs_init_acl is already generally called under fs_mutex, so the locking
223 * stuff has been fixed to work with that. If the locking stuff changes, we
224 * need to re-evaluate the acl locking stuff.
225 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000226int btrfs_init_acl(struct btrfs_trans_handle *trans,
227 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400228{
229 struct posix_acl *acl = NULL;
230 int ret = 0;
231
232 /* this happens with subvols */
233 if (!dir)
234 return 0;
235
236 if (!S_ISLNK(inode->i_mode)) {
237 if (IS_POSIXACL(dir)) {
238 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
239 if (IS_ERR(acl))
240 return PTR_ERR(acl);
241 }
242
243 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400244 inode->i_mode &= ~current_umask();
Josef Bacik33268ea2008-07-24 12:16:36 -0400245 }
246
247 if (IS_POSIXACL(dir) && acl) {
248 struct posix_acl *clone;
249 mode_t mode;
250
251 if (S_ISDIR(inode->i_mode)) {
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000252 ret = btrfs_set_acl(trans, inode, acl,
253 ACL_TYPE_DEFAULT);
Josef Bacik33268ea2008-07-24 12:16:36 -0400254 if (ret)
255 goto failed;
256 }
257 clone = posix_acl_clone(acl, GFP_NOFS);
258 ret = -ENOMEM;
259 if (!clone)
260 goto failed;
261
262 mode = inode->i_mode;
263 ret = posix_acl_create_masq(clone, &mode);
264 if (ret >= 0) {
265 inode->i_mode = mode;
266 if (ret > 0) {
267 /* we need an acl */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000268 ret = btrfs_set_acl(trans, inode, clone,
Josef Bacik33268ea2008-07-24 12:16:36 -0400269 ACL_TYPE_ACCESS);
270 }
271 }
272 }
273failed:
274 posix_acl_release(acl);
275
276 return ret;
277}
278
279int btrfs_acl_chmod(struct inode *inode)
280{
281 struct posix_acl *acl, *clone;
282 int ret = 0;
283
284 if (S_ISLNK(inode->i_mode))
285 return -EOPNOTSUPP;
286
287 if (!IS_POSIXACL(inode))
288 return 0;
289
290 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
291 if (IS_ERR(acl) || !acl)
292 return PTR_ERR(acl);
293
294 clone = posix_acl_clone(acl, GFP_KERNEL);
295 posix_acl_release(acl);
296 if (!clone)
297 return -ENOMEM;
298
299 ret = posix_acl_chmod_masq(clone, inode->i_mode);
300 if (!ret)
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000301 ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
Josef Bacik33268ea2008-07-24 12:16:36 -0400302
303 posix_acl_release(clone);
304
305 return ret;
306}
307
Josef Bacik5103e942007-11-16 11:45:54 -0500308struct xattr_handler btrfs_xattr_acl_default_handler = {
309 .prefix = POSIX_ACL_XATTR_DEFAULT,
Josef Bacik5103e942007-11-16 11:45:54 -0500310 .get = btrfs_xattr_acl_default_get,
311 .set = btrfs_xattr_acl_default_set,
312};
313
314struct xattr_handler btrfs_xattr_acl_access_handler = {
315 .prefix = POSIX_ACL_XATTR_ACCESS,
Josef Bacik5103e942007-11-16 11:45:54 -0500316 .get = btrfs_xattr_acl_access_get,
317 .set = btrfs_xattr_acl_access_set,
318};
Josef Bacikeab922e2008-08-28 06:21:15 -0400319
Chris Mason0eda2942009-10-13 13:50:18 -0400320#else /* CONFIG_BTRFS_FS_POSIX_ACL */
Josef Bacikeab922e2008-08-28 06:21:15 -0400321
322int btrfs_acl_chmod(struct inode *inode)
323{
324 return 0;
325}
326
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000327int btrfs_init_acl(struct btrfs_trans_handle *trans,
328 struct inode *inode, struct inode *dir)
Josef Bacikeab922e2008-08-28 06:21:15 -0400329{
330 return 0;
331}
332
Chris Mason0eda2942009-10-13 13:50:18 -0400333#endif /* CONFIG_BTRFS_FS_POSIX_ACL */