blob: 761e2cd8fed16e6046951e50504b8bb9e7acd3e4 [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) {
64 acl = posix_acl_from_xattr(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;
94 ret = posix_acl_to_xattr(acl, value, size);
95 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;
Chris Masona9cc71a2010-01-17 20:36:18 -0500124 }
125 ret = 0;
Josef Bacik33268ea2008-07-24 12:16:36 -0400126 break;
127 case ACL_TYPE_DEFAULT:
128 if (!S_ISDIR(inode->i_mode))
129 return acl ? -EINVAL : 0;
Christoph Hellwig95819c02008-08-28 06:21:17 -0400130 name = POSIX_ACL_XATTR_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -0400131 break;
132 default:
133 return -EINVAL;
134 }
135
136 if (acl) {
137 size = posix_acl_xattr_size(acl->a_count);
138 value = kmalloc(size, GFP_NOFS);
139 if (!value) {
140 ret = -ENOMEM;
141 goto out;
142 }
143
144 ret = posix_acl_to_xattr(acl, value, size);
145 if (ret < 0)
146 goto out;
147 }
148
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000149 ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400150out:
Chris Masond3977122009-01-05 21:25:51 -0500151 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400152
153 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400154 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400155
156 return ret;
157}
Yanfb4bc1e2008-01-17 11:59:51 -0500158
Christoph Hellwig431547b2009-11-13 09:52:56 +0000159static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
160 const void *value, size_t size, int flags, int type)
Yan744f52f2008-01-14 13:26:08 -0500161{
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000162 int ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400163 struct posix_acl *acl = NULL;
Josef Bacik5103e942007-11-16 11:45:54 -0500164
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700165 if (!inode_owner_or_capable(dentry->d_inode))
Shi Weihua2f26afb2010-05-18 00:50:32 +0000166 return -EPERM;
167
Shi Weihua731e3d12010-05-18 00:51:54 +0000168 if (!IS_POSIXACL(dentry->d_inode))
169 return -EOPNOTSUPP;
170
Yan744f52f2008-01-14 13:26:08 -0500171 if (value) {
172 acl = posix_acl_from_xattr(value, size);
Daniel J Bluemanf5de9392011-05-03 16:44:13 +0000173 if (IS_ERR(acl))
174 return PTR_ERR(acl);
175
Miao Xie329c5052011-04-13 14:07:59 +0800176 if (acl) {
177 ret = posix_acl_valid(acl);
178 if (ret)
179 goto out;
Yan744f52f2008-01-14 13:26:08 -0500180 }
Yan744f52f2008-01-14 13:26:08 -0500181 }
Josef Bacik33268ea2008-07-24 12:16:36 -0400182
Chris Masonebfee3d2009-12-17 15:02:22 -0500183 ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
Miao Xie329c5052011-04-13 14:07:59 +0800184out:
Josef Bacik33268ea2008-07-24 12:16:36 -0400185 posix_acl_release(acl);
186
187 return ret;
Yan744f52f2008-01-14 13:26:08 -0500188}
Josef Bacik1caf9342007-11-19 10:18:17 -0500189
Josef Bacik33268ea2008-07-24 12:16:36 -0400190/*
191 * btrfs_init_acl is already generally called under fs_mutex, so the locking
192 * stuff has been fixed to work with that. If the locking stuff changes, we
193 * need to re-evaluate the acl locking stuff.
194 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000195int btrfs_init_acl(struct btrfs_trans_handle *trans,
196 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400197{
198 struct posix_acl *acl = NULL;
199 int ret = 0;
200
201 /* this happens with subvols */
202 if (!dir)
203 return 0;
204
205 if (!S_ISLNK(inode->i_mode)) {
206 if (IS_POSIXACL(dir)) {
207 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
208 if (IS_ERR(acl))
209 return PTR_ERR(acl);
210 }
211
212 if (!acl)
Al Viroce3b0f82009-03-29 19:08:22 -0400213 inode->i_mode &= ~current_umask();
Josef Bacik33268ea2008-07-24 12:16:36 -0400214 }
215
216 if (IS_POSIXACL(dir) && acl) {
Josef Bacik33268ea2008-07-24 12:16:36 -0400217 if (S_ISDIR(inode->i_mode)) {
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000218 ret = btrfs_set_acl(trans, inode, acl,
219 ACL_TYPE_DEFAULT);
Josef Bacik33268ea2008-07-24 12:16:36 -0400220 if (ret)
221 goto failed;
222 }
Al Virod3fb6122011-07-23 18:37:50 -0400223 ret = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
Al Viro826cae22011-07-23 03:10:32 -0400224 if (ret < 0)
225 return ret;
Josef Bacik33268ea2008-07-24 12:16:36 -0400226
Al Viro826cae22011-07-23 03:10:32 -0400227 if (ret > 0) {
228 /* we need an acl */
229 ret = btrfs_set_acl(trans, inode, acl, ACL_TYPE_ACCESS);
Josef Bacik30f8fe32012-04-23 13:55:30 -0400230 } else {
231 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400232 }
Josef Bacik30f8fe32012-04-23 13:55:30 -0400233 } else {
234 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400235 }
236failed:
237 posix_acl_release(acl);
238
239 return ret;
240}
241
242int btrfs_acl_chmod(struct inode *inode)
243{
Al Virobc26ab52011-07-23 00:18:02 -0400244 struct posix_acl *acl;
Josef Bacik33268ea2008-07-24 12:16:36 -0400245 int ret = 0;
246
247 if (S_ISLNK(inode->i_mode))
248 return -EOPNOTSUPP;
249
250 if (!IS_POSIXACL(inode))
251 return 0;
252
253 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
David Sterbac7040052011-04-19 18:00:01 +0200254 if (IS_ERR_OR_NULL(acl))
Josef Bacik33268ea2008-07-24 12:16:36 -0400255 return PTR_ERR(acl);
256
Al Virobc26ab52011-07-23 00:18:02 -0400257 ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
258 if (ret)
259 return ret;
260 ret = btrfs_set_acl(NULL, inode, acl, ACL_TYPE_ACCESS);
Josef Bacik33268ea2008-07-24 12:16:36 -0400261 posix_acl_release(acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400262 return ret;
263}
264
Stephen Hemmingerf01cbd32010-05-13 17:53:15 -0700265const struct xattr_handler btrfs_xattr_acl_default_handler = {
Josef Bacik5103e942007-11-16 11:45:54 -0500266 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000267 .flags = ACL_TYPE_DEFAULT,
268 .get = btrfs_xattr_acl_get,
269 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500270};
271
Stephen Hemmingerf01cbd32010-05-13 17:53:15 -0700272const struct xattr_handler btrfs_xattr_acl_access_handler = {
Josef Bacik5103e942007-11-16 11:45:54 -0500273 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000274 .flags = ACL_TYPE_ACCESS,
275 .get = btrfs_xattr_acl_get,
276 .set = btrfs_xattr_acl_set,
Josef Bacik5103e942007-11-16 11:45:54 -0500277};