blob: e15d2b0d8d3b20f3085c18348e9d711682fedc24 [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);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090062 }
63 if (size > 0) {
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070064 acl = posix_acl_from_xattr(&init_user_ns, value, size);
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;
Chris Mason7b1a14b2009-04-27 10:49:53 -040068 } else {
69 acl = ERR_PTR(-EIO);
Josef Bacik33268ea2008-07-24 12:16:36 -040070 }
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090071 kfree(value);
72
73 if (!IS_ERR(acl))
74 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -040075
76 return acl;
77}
78
Christoph Hellwig431547b2009-11-13 09:52:56 +000079static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
80 void *value, size_t size, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040081{
82 struct posix_acl *acl;
83 int ret = 0;
84
Miao Xied0f69682011-01-25 15:46:17 +080085 if (!IS_POSIXACL(dentry->d_inode))
86 return -EOPNOTSUPP;
87
Christoph Hellwig431547b2009-11-13 09:52:56 +000088 acl = btrfs_get_acl(dentry->d_inode, type);
Josef Bacik33268ea2008-07-24 12:16:36 -040089
90 if (IS_ERR(acl))
91 return PTR_ERR(acl);
92 if (acl == NULL)
93 return -ENODATA;
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070094 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -040095 posix_acl_release(acl);
96
97 return ret;
98}
99
100/*
101 * Needs to be called with fs_mutex held
102 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103static int btrfs_set_acl(struct btrfs_trans_handle *trans,
104 struct inode *inode, struct posix_acl *acl, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -0400105{
Christoph Hellwig95819c02008-08-28 06:21:17 -0400106 int ret, size = 0;
107 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -0400108 char *value = NULL;
Josef Bacik33268ea2008-07-24 12:16:36 -0400109
110 if (acl) {
111 ret = posix_acl_valid(acl);
112 if (ret < 0)
113 return ret;
114 ret = 0;
115 }
116
117 switch (type) {
118 case ACL_TYPE_ACCESS:
Christoph Hellwig95819c02008-08-28 06:21:17 -0400119 name = POSIX_ACL_XATTR_ACCESS;
Chris Masona9cc71a2010-01-17 20:36:18 -0500120 if (acl) {
Al Virod6952122011-07-23 18:56:36 -0400121 ret = posix_acl_equiv_mode(acl, &inode->i_mode);
Chris Masona9cc71a2010-01-17 20:36:18 -0500122 if (ret < 0)
123 return ret;
Liu Bo755ac672012-11-28 10:43:11 +0000124 if (ret == 0)
125 acl = NULL;
Chris Masona9cc71a2010-01-17 20:36:18 -0500126 }
127 ret = 0;
Josef Bacik33268ea2008-07-24 12:16:36 -0400128 break;
129 case ACL_TYPE_DEFAULT:
130 if (!S_ISDIR(inode->i_mode))
131 return acl ? -EINVAL : 0;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400132 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -0400133 break;
134 default:
135 return -EINVAL;
136 }
137
138 if (acl) {
139 size = posix_acl_xattr_size(acl->a_count);
140 value = kmalloc(size, GFP_NOFS);
141 if (!value) {
142 ret = -ENOMEM;
143 goto out;
144 }
145
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700146 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -0400147 if (ret < 0)
148 goto out;
149 }
150
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000151 ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400152out:
Chris Masond3977122009-01-05 21:25:51 -0500153 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400154
155 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400156 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400157
158 return ret;
159}
Yanfb4bc1e2008-01-17 11:59:51 -0500160
Christoph Hellwig431547b2009-11-13 09:52:56 +0000161static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
162 const void *value, size_t size, int flags, int type)
Yan744f52f2008-01-14 13:26:08 -0500163{
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000164 int ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400165 struct posix_acl *acl = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500166
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700167 if (!inode_owner_or_capable(dentry->d_inode))
Shi Weihua2f26afb2010-05-18 00:50:32 +0000168 return -EPERM;
169
Shi Weihua731e3d12010-05-18 00:51:54 +0000170 if (!IS_POSIXACL(dentry->d_inode))
171 return -EOPNOTSUPP;
172
Yan744f52f2008-01-14 13:26:08 -0500173 if (value) {
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700174 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Daniel J Bluemanf5de9392011-05-03 16:44:13 +0000175 if (IS_ERR(acl))
176 return PTR_ERR(acl);
177
Miao Xie329c5052011-04-13 14:07:59 +0800178 if (acl) {
179 ret = posix_acl_valid(acl);
180 if (ret)
181 goto out;
Yan744f52f2008-01-14 13:26:08 -0500182 }
Yan744f52f2008-01-14 13:26:08 -0500183 }
Josef Bacik33268ea2008-07-24 12:16:36 -0400184
Chris Masonebfee3d2009-12-17 15:02:22 -0500185 ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
Miao Xie329c5052011-04-13 14:07:59 +0800186out:
Josef Bacik33268ea2008-07-24 12:16:36 -0400187 posix_acl_release(acl);
188
189 return ret;
Yan744f52f2008-01-14 13:26:08 -0500190}
Josef Bacik1caf9342007-11-19 10:18:17 -0500191
Josef Bacik33268ea2008-07-24 12:16:36 -0400192/*
193 * btrfs_init_acl is already generally called under fs_mutex, so the locking
194 * stuff has been fixed to work with that. If the locking stuff changes, we
195 * need to re-evaluate the acl locking stuff.
196 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000197int btrfs_init_acl(struct btrfs_trans_handle *trans,
198 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400199{
200 struct posix_acl *acl = NULL;
201 int ret = 0;
202
203 /* this happens with subvols */
204 if (!dir)
205 return 0;
206
207 if (!S_ISLNK(inode->i_mode)) {
208 if (IS_POSIXACL(dir)) {
209 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
210 if (IS_ERR(acl))
211 return PTR_ERR(acl);
212 }
213
214 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400215 inode->i_mode &= ~current_umask();
Josef Bacik33268ea2008-07-24 12:16:36 -0400216 }
217
218 if (IS_POSIXACL(dir) && acl) {
Josef Bacik33268ea2008-07-24 12:16:36 -0400219 if (S_ISDIR(inode->i_mode)) {
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000220 ret = btrfs_set_acl(trans, inode, acl,
221 ACL_TYPE_DEFAULT);
Josef Bacik33268ea2008-07-24 12:16:36 -0400222 if (ret)
223 goto failed;
224 }
Al Virod3fb6122011-07-23 18:37:50 -0400225 ret = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
Al Viro826cae22011-07-23 03:10:32 -0400226 if (ret < 0)
227 return ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400228
Al Viro826cae22011-07-23 03:10:32 -0400229 if (ret > 0) {
230 /* we need an acl */
231 ret = btrfs_set_acl(trans, inode, acl, ACL_TYPE_ACCESS);
Josef Bacik30f8fe32012-04-23 13:55:30 -0400232 } else {
233 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400234 }
Josef Bacik30f8fe32012-04-23 13:55:30 -0400235 } else {
236 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400237 }
238failed:
239 posix_acl_release(acl);
240
241 return ret;
242}
243
244int btrfs_acl_chmod(struct inode *inode)
245{
Al Virobc26ab52011-07-23 00:18:02 -0400246 struct posix_acl *acl;
Josef Bacik33268ea2008-07-24 12:16:36 -0400247 int ret = 0;
248
249 if (S_ISLNK(inode->i_mode))
250 return -EOPNOTSUPP;
251
252 if (!IS_POSIXACL(inode))
253 return 0;
254
255 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
David Sterbac7040052011-04-19 18:00:01 +0200256 if (IS_ERR_OR_NULL(acl))
Josef Bacik33268ea2008-07-24 12:16:36 -0400257 return PTR_ERR(acl);
258
Al Virobc26ab52011-07-23 00:18:02 -0400259 ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
260 if (ret)
261 return ret;
262 ret = btrfs_set_acl(NULL, inode, acl, ACL_TYPE_ACCESS);
Josef Bacik33268ea2008-07-24 12:16:36 -0400263 posix_acl_release(acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400264 return ret;
265}
266
Stephen Hemmingerf01cbd32010-05-13 17:53:15 -0700267const struct xattr_handler btrfs_xattr_acl_default_handler = {
Josef Bacik5103e942007-11-16 11:45:54 -0500268 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000269 .flags = ACL_TYPE_DEFAULT,
270 .get = btrfs_xattr_acl_get,
271 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500272};
273
Stephen Hemmingerf01cbd32010-05-13 17:53:15 -0700274const struct xattr_handler btrfs_xattr_acl_access_handler = {
Josef Bacik5103e942007-11-16 11:45:54 -0500275 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000276 .flags = ACL_TYPE_ACCESS,
277 .get = btrfs_xattr_acl_get,
278 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500279};