blob: eb159aaa5a1139204e2d24333bc32eae8433261d [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040026
Josef Bacik5103e942007-11-16 11:45:54 -050027#include "ctree.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040028#include "btrfs_inode.h"
Josef Bacik5103e942007-11-16 11:45:54 -050029#include "xattr.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040030
Christoph Hellwig4e34e712011-07-23 17:37:31 +020031struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040032{
Christoph Hellwig95819c02008-08-28 06:21:17 -040033 int size;
34 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040035 char *value = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040036 struct posix_acl *acl;
37
Miao Xied0f69682011-01-25 15:46:17 +080038 if (!IS_POSIXACL(inode))
39 return NULL;
40
Al Viro073aaa12009-06-09 12:11:54 -040041 acl = get_cached_acl(inode, type);
42 if (acl != ACL_NOT_CACHED)
43 return acl;
Josef Bacik33268ea2008-07-24 12:16:36 -040044
45 switch (type) {
46 case ACL_TYPE_ACCESS:
Christoph Hellwig95819c02008-08-28 06:21:17 -040047 name = POSIX_ACL_XATTR_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040048 break;
49 case ACL_TYPE_DEFAULT:
Christoph Hellwig95819c02008-08-28 06:21:17 -040050 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040051 break;
52 default:
Al Viro073aaa12009-06-09 12:11:54 -040053 BUG();
Josef Bacik33268ea2008-07-24 12:16:36 -040054 }
55
Christoph Hellwig95819c02008-08-28 06:21:17 -040056 size = __btrfs_getxattr(inode, name, "", 0);
Josef Bacik33268ea2008-07-24 12:16:36 -040057 if (size > 0) {
58 value = kzalloc(size, GFP_NOFS);
59 if (!value)
60 return ERR_PTR(-ENOMEM);
Christoph Hellwig95819c02008-08-28 06:21:17 -040061 size = __btrfs_getxattr(inode, name, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -040062 if (size > 0) {
63 acl = posix_acl_from_xattr(value, size);
Jesper Juhl42838bb2011-01-06 21:45:21 +000064 if (IS_ERR(acl)) {
65 kfree(value);
Dan Carpenter834e7472010-05-29 09:48:35 +000066 return acl;
Jesper Juhl42838bb2011-01-06 21:45:21 +000067 }
Al Viro073aaa12009-06-09 12:11:54 -040068 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -040069 }
70 kfree(value);
Chris Mason7b1a14b2009-04-27 10:49:53 -040071 } else if (size == -ENOENT || size == -ENODATA || size == 0) {
72 /* FIXME, who returns -ENOENT? I think nobody */
Josef Bacik33268ea2008-07-24 12:16:36 -040073 acl = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040074 set_cached_acl(inode, type, acl);
Chris Mason7b1a14b2009-04-27 10:49:53 -040075 } else {
76 acl = ERR_PTR(-EIO);
Josef Bacik33268ea2008-07-24 12:16:36 -040077 }
78
79 return acl;
80}
81
Christoph Hellwig431547b2009-11-13 09:52:56 +000082static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
83 void *value, size_t size, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040084{
85 struct posix_acl *acl;
86 int ret = 0;
87
Miao Xied0f69682011-01-25 15:46:17 +080088 if (!IS_POSIXACL(dentry->d_inode))
89 return -EOPNOTSUPP;
90
Christoph Hellwig431547b2009-11-13 09:52:56 +000091 acl = btrfs_get_acl(dentry->d_inode, type);
Josef Bacik33268ea2008-07-24 12:16:36 -040092
93 if (IS_ERR(acl))
94 return PTR_ERR(acl);
95 if (acl == NULL)
96 return -ENODATA;
97 ret = posix_acl_to_xattr(acl, value, size);
98 posix_acl_release(acl);
99
100 return ret;
101}
102
103/*
104 * Needs to be called with fs_mutex held
105 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000106static int btrfs_set_acl(struct btrfs_trans_handle *trans,
107 struct inode *inode, struct posix_acl *acl, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -0400108{
Christoph Hellwig95819c02008-08-28 06:21:17 -0400109 int ret, size = 0;
110 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -0400111 char *value = NULL;
Josef Bacik33268ea2008-07-24 12:16:36 -0400112
113 if (acl) {
114 ret = posix_acl_valid(acl);
115 if (ret < 0)
116 return ret;
117 ret = 0;
118 }
119
120 switch (type) {
121 case ACL_TYPE_ACCESS:
Christoph Hellwig95819c02008-08-28 06:21:17 -0400122 name = POSIX_ACL_XATTR_ACCESS;
Chris Masona9cc71a2010-01-17 20:36:18 -0500123 if (acl) {
Al Virod6952122011-07-23 18:56:36 -0400124 ret = posix_acl_equiv_mode(acl, &inode->i_mode);
Chris Masona9cc71a2010-01-17 20:36:18 -0500125 if (ret < 0)
126 return ret;
Chris Masona9cc71a2010-01-17 20:36:18 -0500127 }
128 ret = 0;
Josef Bacik33268ea2008-07-24 12:16:36 -0400129 break;
130 case ACL_TYPE_DEFAULT:
131 if (!S_ISDIR(inode->i_mode))
132 return acl ? -EINVAL : 0;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400133 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -0400134 break;
135 default:
136 return -EINVAL;
137 }
138
139 if (acl) {
140 size = posix_acl_xattr_size(acl->a_count);
141 value = kmalloc(size, GFP_NOFS);
142 if (!value) {
143 ret = -ENOMEM;
144 goto out;
145 }
146
147 ret = posix_acl_to_xattr(acl, value, size);
148 if (ret < 0)
149 goto out;
150 }
151
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000152 ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400153out:
Chris Masond3977122009-01-05 21:25:51 -0500154 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400155
156 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400157 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400158
159 return ret;
160}
Yanfb4bc1e2008-01-17 11:59:51 -0500161
Christoph Hellwig431547b2009-11-13 09:52:56 +0000162static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
163 const void *value, size_t size, int flags, int type)
Yan744f52f2008-01-14 13:26:08 -0500164{
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000165 int ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400166 struct posix_acl *acl = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500167
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700168 if (!inode_owner_or_capable(dentry->d_inode))
Shi Weihua2f26afb2010-05-18 00:50:32 +0000169 return -EPERM;
170
Shi Weihua731e3d12010-05-18 00:51:54 +0000171 if (!IS_POSIXACL(dentry->d_inode))
172 return -EOPNOTSUPP;
173
Yan744f52f2008-01-14 13:26:08 -0500174 if (value) {
175 acl = posix_acl_from_xattr(value, size);
Daniel J Bluemanf5de9392011-05-03 16:44:13 +0000176 if (IS_ERR(acl))
177 return PTR_ERR(acl);
178
Miao Xie329c5052011-04-13 14:07:59 +0800179 if (acl) {
180 ret = posix_acl_valid(acl);
181 if (ret)
182 goto out;
Yan744f52f2008-01-14 13:26:08 -0500183 }
Yan744f52f2008-01-14 13:26:08 -0500184 }
Josef Bacik33268ea2008-07-24 12:16:36 -0400185
Chris Masonebfee3d2009-12-17 15:02:22 -0500186 ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
Miao Xie329c5052011-04-13 14:07:59 +0800187out:
Josef Bacik33268ea2008-07-24 12:16:36 -0400188 posix_acl_release(acl);
189
190 return ret;
Yan744f52f2008-01-14 13:26:08 -0500191}
Josef Bacik1caf9342007-11-19 10:18:17 -0500192
Josef Bacik33268ea2008-07-24 12:16:36 -0400193/*
194 * btrfs_init_acl is already generally called under fs_mutex, so the locking
195 * stuff has been fixed to work with that. If the locking stuff changes, we
196 * need to re-evaluate the acl locking stuff.
197 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000198int btrfs_init_acl(struct btrfs_trans_handle *trans,
199 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400200{
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) {
Josef Bacik33268ea2008-07-24 12:16:36 -0400220 if (S_ISDIR(inode->i_mode)) {
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000221 ret = btrfs_set_acl(trans, inode, acl,
222 ACL_TYPE_DEFAULT);
Josef Bacik33268ea2008-07-24 12:16:36 -0400223 if (ret)
224 goto failed;
225 }
Al Virod3fb6122011-07-23 18:37:50 -0400226 ret = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
Al Viro826cae22011-07-23 03:10:32 -0400227 if (ret < 0)
228 return ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400229
Al Viro826cae22011-07-23 03:10:32 -0400230 if (ret > 0) {
231 /* we need an acl */
232 ret = btrfs_set_acl(trans, inode, acl, ACL_TYPE_ACCESS);
Josef Bacik33268ea2008-07-24 12:16:36 -0400233 }
234 }
235failed:
236 posix_acl_release(acl);
237
238 return ret;
239}
240
241int btrfs_acl_chmod(struct inode *inode)
242{
Al Virobc26ab52011-07-23 00:18:02 -0400243 struct posix_acl *acl;
Josef Bacik33268ea2008-07-24 12:16:36 -0400244 int ret = 0;
245
246 if (S_ISLNK(inode->i_mode))
247 return -EOPNOTSUPP;
248
249 if (!IS_POSIXACL(inode))
250 return 0;
251
252 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
David Sterbac7040052011-04-19 18:00:01 +0200253 if (IS_ERR_OR_NULL(acl))
Josef Bacik33268ea2008-07-24 12:16:36 -0400254 return PTR_ERR(acl);
255
Al Virobc26ab52011-07-23 00:18:02 -0400256 ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
257 if (ret)
258 return ret;
259 ret = btrfs_set_acl(NULL, inode, acl, ACL_TYPE_ACCESS);
Josef Bacik33268ea2008-07-24 12:16:36 -0400260 posix_acl_release(acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400261 return ret;
262}
263
Stephen Hemmingerf01cbd32010-05-13 17:53:15 -0700264const struct xattr_handler btrfs_xattr_acl_default_handler = {
Josef Bacik5103e942007-11-16 11:45:54 -0500265 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000266 .flags = ACL_TYPE_DEFAULT,
267 .get = btrfs_xattr_acl_get,
268 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500269};
270
Stephen Hemmingerf01cbd32010-05-13 17:53:15 -0700271const struct xattr_handler btrfs_xattr_acl_access_handler = {
Josef Bacik5103e942007-11-16 11:45:54 -0500272 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000273 .flags = ACL_TYPE_ACCESS,
274 .get = btrfs_xattr_acl_get,
275 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500276};