blob: 0a0d0eccee4ef5782e3b2169be1fe4e8270d515b [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Josef Bacik5103e942007-11-16 11:45:54 -05002/*
3 * Copyright (C) 2007 Red Hat. All rights reserved.
Josef Bacik5103e942007-11-16 11:45:54 -05004 */
5
6#include <linux/fs.h>
7#include <linux/string.h>
8#include <linux/xattr.h>
9#include <linux/posix_acl_xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040010#include <linux/posix_acl.h>
Chris Masonc1e32da2008-01-22 12:46:56 -050011#include <linux/sched.h>
Filipe Mananaa0873492018-12-13 21:16:56 +000012#include <linux/sched/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040014
Josef Bacik5103e942007-11-16 11:45:54 -050015#include "ctree.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040016#include "btrfs_inode.h"
Josef Bacik5103e942007-11-16 11:45:54 -050017#include "xattr.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040018
Miklos Szeredi0cad6242021-08-18 22:08:24 +020019struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu)
Josef Bacik33268ea2008-07-24 12:16:36 -040020{
Christoph Hellwig95819c02008-08-28 06:21:17 -040021 int size;
22 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040023 char *value = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040024 struct posix_acl *acl;
25
Miklos Szeredi0cad6242021-08-18 22:08:24 +020026 if (rcu)
27 return ERR_PTR(-ECHILD);
28
Josef Bacik33268ea2008-07-24 12:16:36 -040029 switch (type) {
30 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010031 name = XATTR_NAME_POSIX_ACL_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040032 break;
33 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010034 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040035 break;
36 default:
Chengguang Xuab3629e2018-06-27 12:16:34 +080037 return ERR_PTR(-EINVAL);
Josef Bacik33268ea2008-07-24 12:16:36 -040038 }
39
Chengguang Xu7e35eab2018-06-27 12:16:35 +080040 size = btrfs_getxattr(inode, name, NULL, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -040041 if (size > 0) {
David Sterba39a27ec2015-12-03 12:49:48 +010042 value = kzalloc(size, GFP_KERNEL);
Josef Bacik33268ea2008-07-24 12:16:36 -040043 if (!value)
44 return ERR_PTR(-ENOMEM);
David Sterba78527812018-02-27 15:48:52 +010045 size = btrfs_getxattr(inode, name, value, size);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090046 }
Chengguang Xu4de426c2018-06-27 12:16:38 +080047 if (size > 0)
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070048 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Chengguang Xu4de426c2018-06-27 12:16:38 +080049 else if (size == -ENODATA || size == 0)
Josef Bacik33268ea2008-07-24 12:16:36 -040050 acl = NULL;
Chengguang Xu4de426c2018-06-27 12:16:38 +080051 else
Chengguang Xudc7789e2018-06-27 12:16:37 +080052 acl = ERR_PTR(size);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090053 kfree(value);
54
Josef Bacik33268ea2008-07-24 12:16:36 -040055 return acl;
56}
57
Christoph Hellwig996a7102013-12-20 05:16:43 -080058static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
Christian Brauner4a8b34a2021-07-27 12:48:58 +020059 struct user_namespace *mnt_userns,
60 struct inode *inode, struct posix_acl *acl, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040061{
Christoph Hellwig95819c02008-08-28 06:21:17 -040062 int ret, size = 0;
63 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040064 char *value = NULL;
Josef Bacik33268ea2008-07-24 12:16:36 -040065
Josef Bacik33268ea2008-07-24 12:16:36 -040066 switch (type) {
67 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010068 name = XATTR_NAME_POSIX_ACL_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040069 break;
70 case ACL_TYPE_DEFAULT:
71 if (!S_ISDIR(inode->i_mode))
72 return acl ? -EINVAL : 0;
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010073 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040074 break;
75 default:
76 return -EINVAL;
77 }
78
79 if (acl) {
Filipe Mananaa0873492018-12-13 21:16:56 +000080 unsigned int nofs_flag;
81
Josef Bacik33268ea2008-07-24 12:16:36 -040082 size = posix_acl_xattr_size(acl->a_count);
Filipe Mananaa0873492018-12-13 21:16:56 +000083 /*
84 * We're holding a transaction handle, so use a NOFS memory
85 * allocation context to avoid deadlock if reclaim happens.
86 */
87 nofs_flag = memalloc_nofs_save();
David Sterba39a27ec2015-12-03 12:49:48 +010088 value = kmalloc(size, GFP_KERNEL);
Filipe Mananaa0873492018-12-13 21:16:56 +000089 memalloc_nofs_restore(nofs_flag);
Josef Bacik33268ea2008-07-24 12:16:36 -040090 if (!value) {
91 ret = -ENOMEM;
92 goto out;
93 }
94
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070095 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -040096 if (ret < 0)
97 goto out;
98 }
99
Anand Jain04e68632019-04-12 16:02:58 +0800100 if (trans)
101 ret = btrfs_setxattr(trans, inode, name, value, size, 0);
102 else
Anand Jaine3de9b12019-04-12 16:02:59 +0800103 ret = btrfs_setxattr_trans(inode, name, value, size, 0);
Anand Jain04e68632019-04-12 16:02:58 +0800104
Josef Bacik33268ea2008-07-24 12:16:36 -0400105out:
Chris Masond3977122009-01-05 21:25:51 -0500106 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400107
108 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400109 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400110
111 return ret;
112}
Yanfb4bc1e2008-01-17 11:59:51 -0500113
Christian Brauner549c7292021-01-21 14:19:43 +0100114int btrfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
115 struct posix_acl *acl, int type)
Yan744f52f2008-01-14 13:26:08 -0500116{
Jan Karab7f8a092017-06-22 15:31:07 +0200117 int ret;
Ernesto A. Fernándezd7d82492017-08-02 03:18:27 -0300118 umode_t old_mode = inode->i_mode;
Jan Karab7f8a092017-06-22 15:31:07 +0200119
120 if (type == ACL_TYPE_ACCESS && acl) {
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200121 ret = posix_acl_update_mode(mnt_userns, inode,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100122 &inode->i_mode, &acl);
Jan Karab7f8a092017-06-22 15:31:07 +0200123 if (ret)
124 return ret;
125 }
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200126 ret = __btrfs_set_acl(NULL, mnt_userns, inode, acl, type);
Ernesto A. Fernándezd7d82492017-08-02 03:18:27 -0300127 if (ret)
128 inode->i_mode = old_mode;
129 return ret;
Yan744f52f2008-01-14 13:26:08 -0500130}
Josef Bacik1caf9342007-11-19 10:18:17 -0500131
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000132int btrfs_init_acl(struct btrfs_trans_handle *trans,
133 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400134{
Christoph Hellwig996a7102013-12-20 05:16:43 -0800135 struct posix_acl *default_acl, *acl;
Josef Bacik33268ea2008-07-24 12:16:36 -0400136 int ret = 0;
137
138 /* this happens with subvols */
139 if (!dir)
140 return 0;
141
Christoph Hellwig996a7102013-12-20 05:16:43 -0800142 ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400143 if (ret)
144 return ret;
Christoph Hellwig996a7102013-12-20 05:16:43 -0800145
146 if (default_acl) {
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200147 ret = __btrfs_set_acl(trans, &init_user_ns, inode, default_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -0800148 ACL_TYPE_DEFAULT);
149 posix_acl_release(default_acl);
150 }
151
152 if (acl) {
153 if (!ret)
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200154 ret = __btrfs_set_acl(trans, &init_user_ns, inode, acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -0800155 ACL_TYPE_ACCESS);
156 posix_acl_release(acl);
157 }
158
159 if (!default_acl && !acl)
160 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400161 return ret;
162}