blob: 247b8dfaf6e5e53b2f8a898f036375819a1ac225 [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
Josef Bacik33268ea2008-07-24 12:16:36 -040038 switch (type) {
39 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010040 name = XATTR_NAME_POSIX_ACL_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040041 break;
42 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010043 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040044 break;
45 default:
Al Viro073aaa12009-06-09 12:11:54 -040046 BUG();
Josef Bacik33268ea2008-07-24 12:16:36 -040047 }
48
Christoph Hellwig95819c02008-08-28 06:21:17 -040049 size = __btrfs_getxattr(inode, name, "", 0);
Josef Bacik33268ea2008-07-24 12:16:36 -040050 if (size > 0) {
David Sterba39a27ec2015-12-03 12:49:48 +010051 value = kzalloc(size, GFP_KERNEL);
Josef Bacik33268ea2008-07-24 12:16:36 -040052 if (!value)
53 return ERR_PTR(-ENOMEM);
Christoph Hellwig95819c02008-08-28 06:21:17 -040054 size = __btrfs_getxattr(inode, name, value, size);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090055 }
56 if (size > 0) {
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070057 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Salah Trikia60617d2016-07-03 05:40:10 +010058 } else if (size == -ERANGE || size == -ENODATA || size == 0) {
Josef Bacik33268ea2008-07-24 12:16:36 -040059 acl = NULL;
Chris Mason7b1a14b2009-04-27 10:49:53 -040060 } else {
61 acl = ERR_PTR(-EIO);
Josef Bacik33268ea2008-07-24 12:16:36 -040062 }
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090063 kfree(value);
64
Josef Bacik33268ea2008-07-24 12:16:36 -040065 return acl;
66}
67
Josef Bacik33268ea2008-07-24 12:16:36 -040068/*
69 * Needs to be called with fs_mutex held
70 */
Christoph Hellwig996a7102013-12-20 05:16:43 -080071static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
Yan, Zhengf34f57a2009-11-12 09:35:27 +000072 struct inode *inode, struct posix_acl *acl, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040073{
Christoph Hellwig95819c02008-08-28 06:21:17 -040074 int ret, size = 0;
75 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040076 char *value = NULL;
Josef Bacik33268ea2008-07-24 12:16:36 -040077
Josef Bacik33268ea2008-07-24 12:16:36 -040078 switch (type) {
79 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010080 name = XATTR_NAME_POSIX_ACL_ACCESS;
Chris Masona9cc71a2010-01-17 20:36:18 -050081 if (acl) {
Jan Kara07393102016-09-19 17:39:09 +020082 ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
83 if (ret)
Chris Masona9cc71a2010-01-17 20:36:18 -050084 return ret;
Chris Masona9cc71a2010-01-17 20:36:18 -050085 }
86 ret = 0;
Josef Bacik33268ea2008-07-24 12:16:36 -040087 break;
88 case ACL_TYPE_DEFAULT:
89 if (!S_ISDIR(inode->i_mode))
90 return acl ? -EINVAL : 0;
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010091 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040092 break;
93 default:
94 return -EINVAL;
95 }
96
97 if (acl) {
98 size = posix_acl_xattr_size(acl->a_count);
David Sterba39a27ec2015-12-03 12:49:48 +010099 value = kmalloc(size, GFP_KERNEL);
Josef Bacik33268ea2008-07-24 12:16:36 -0400100 if (!value) {
101 ret = -ENOMEM;
102 goto out;
103 }
104
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700105 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -0400106 if (ret < 0)
107 goto out;
108 }
109
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000110 ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -0400111out:
Chris Masond3977122009-01-05 21:25:51 -0500112 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400113
114 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400115 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400116
117 return ret;
118}
Yanfb4bc1e2008-01-17 11:59:51 -0500119
Christoph Hellwig996a7102013-12-20 05:16:43 -0800120int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Yan744f52f2008-01-14 13:26:08 -0500121{
Christoph Hellwig996a7102013-12-20 05:16:43 -0800122 return __btrfs_set_acl(NULL, inode, acl, type);
Yan744f52f2008-01-14 13:26:08 -0500123}
Josef Bacik1caf9342007-11-19 10:18:17 -0500124
Josef Bacik33268ea2008-07-24 12:16:36 -0400125/*
126 * btrfs_init_acl is already generally called under fs_mutex, so the locking
127 * stuff has been fixed to work with that. If the locking stuff changes, we
128 * need to re-evaluate the acl locking stuff.
129 */
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000130int btrfs_init_acl(struct btrfs_trans_handle *trans,
131 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400132{
Christoph Hellwig996a7102013-12-20 05:16:43 -0800133 struct posix_acl *default_acl, *acl;
Josef Bacik33268ea2008-07-24 12:16:36 -0400134 int ret = 0;
135
136 /* this happens with subvols */
137 if (!dir)
138 return 0;
139
Christoph Hellwig996a7102013-12-20 05:16:43 -0800140 ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400141 if (ret)
142 return ret;
Christoph Hellwig996a7102013-12-20 05:16:43 -0800143
144 if (default_acl) {
145 ret = __btrfs_set_acl(trans, inode, default_acl,
146 ACL_TYPE_DEFAULT);
147 posix_acl_release(default_acl);
148 }
149
150 if (acl) {
151 if (!ret)
152 ret = __btrfs_set_acl(trans, inode, acl,
153 ACL_TYPE_ACCESS);
154 posix_acl_release(acl);
155 }
156
157 if (!default_acl && !acl)
158 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400159 return ret;
160}