blob: c9f9789e828fd9bc90ce8fdffd4d507c064ab537 [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
Christoph Hellwig4e34e712011-07-23 17:37:31 +020019struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
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
Josef Bacik33268ea2008-07-24 12:16:36 -040026 switch (type) {
27 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010028 name = XATTR_NAME_POSIX_ACL_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040029 break;
30 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010031 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040032 break;
33 default:
Chengguang Xuab3629e2018-06-27 12:16:34 +080034 return ERR_PTR(-EINVAL);
Josef Bacik33268ea2008-07-24 12:16:36 -040035 }
36
Chengguang Xu7e35eab2018-06-27 12:16:35 +080037 size = btrfs_getxattr(inode, name, NULL, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -040038 if (size > 0) {
David Sterba39a27ec2015-12-03 12:49:48 +010039 value = kzalloc(size, GFP_KERNEL);
Josef Bacik33268ea2008-07-24 12:16:36 -040040 if (!value)
41 return ERR_PTR(-ENOMEM);
David Sterba78527812018-02-27 15:48:52 +010042 size = btrfs_getxattr(inode, name, value, size);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090043 }
Chengguang Xu4de426c2018-06-27 12:16:38 +080044 if (size > 0)
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070045 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Chengguang Xu4de426c2018-06-27 12:16:38 +080046 else if (size == -ENODATA || size == 0)
Josef Bacik33268ea2008-07-24 12:16:36 -040047 acl = NULL;
Chengguang Xu4de426c2018-06-27 12:16:38 +080048 else
Chengguang Xudc7789e2018-06-27 12:16:37 +080049 acl = ERR_PTR(size);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090050 kfree(value);
51
Josef Bacik33268ea2008-07-24 12:16:36 -040052 return acl;
53}
54
Christoph Hellwig996a7102013-12-20 05:16:43 -080055static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
Christian Brauner4a8b34a2021-07-27 12:48:58 +020056 struct user_namespace *mnt_userns,
57 struct inode *inode, struct posix_acl *acl, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040058{
Christoph Hellwig95819c02008-08-28 06:21:17 -040059 int ret, size = 0;
60 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040061 char *value = NULL;
Josef Bacik33268ea2008-07-24 12:16:36 -040062
Josef Bacik33268ea2008-07-24 12:16:36 -040063 switch (type) {
64 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010065 name = XATTR_NAME_POSIX_ACL_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040066 break;
67 case ACL_TYPE_DEFAULT:
68 if (!S_ISDIR(inode->i_mode))
69 return acl ? -EINVAL : 0;
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010070 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040071 break;
72 default:
73 return -EINVAL;
74 }
75
76 if (acl) {
Filipe Mananaa0873492018-12-13 21:16:56 +000077 unsigned int nofs_flag;
78
Josef Bacik33268ea2008-07-24 12:16:36 -040079 size = posix_acl_xattr_size(acl->a_count);
Filipe Mananaa0873492018-12-13 21:16:56 +000080 /*
81 * We're holding a transaction handle, so use a NOFS memory
82 * allocation context to avoid deadlock if reclaim happens.
83 */
84 nofs_flag = memalloc_nofs_save();
David Sterba39a27ec2015-12-03 12:49:48 +010085 value = kmalloc(size, GFP_KERNEL);
Filipe Mananaa0873492018-12-13 21:16:56 +000086 memalloc_nofs_restore(nofs_flag);
Josef Bacik33268ea2008-07-24 12:16:36 -040087 if (!value) {
88 ret = -ENOMEM;
89 goto out;
90 }
91
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070092 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -040093 if (ret < 0)
94 goto out;
95 }
96
Anand Jain04e68632019-04-12 16:02:58 +080097 if (trans)
98 ret = btrfs_setxattr(trans, inode, name, value, size, 0);
99 else
Anand Jaine3de9b12019-04-12 16:02:59 +0800100 ret = btrfs_setxattr_trans(inode, name, value, size, 0);
Anand Jain04e68632019-04-12 16:02:58 +0800101
Josef Bacik33268ea2008-07-24 12:16:36 -0400102out:
Chris Masond3977122009-01-05 21:25:51 -0500103 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400104
105 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400106 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400107
108 return ret;
109}
Yanfb4bc1e2008-01-17 11:59:51 -0500110
Christian Brauner549c7292021-01-21 14:19:43 +0100111int btrfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
112 struct posix_acl *acl, int type)
Yan744f52f2008-01-14 13:26:08 -0500113{
Jan Karab7f8a092017-06-22 15:31:07 +0200114 int ret;
Ernesto A. Fernándezd7d82492017-08-02 03:18:27 -0300115 umode_t old_mode = inode->i_mode;
Jan Karab7f8a092017-06-22 15:31:07 +0200116
117 if (type == ACL_TYPE_ACCESS && acl) {
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200118 ret = posix_acl_update_mode(mnt_userns, inode,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100119 &inode->i_mode, &acl);
Jan Karab7f8a092017-06-22 15:31:07 +0200120 if (ret)
121 return ret;
122 }
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200123 ret = __btrfs_set_acl(NULL, mnt_userns, inode, acl, type);
Ernesto A. Fernándezd7d82492017-08-02 03:18:27 -0300124 if (ret)
125 inode->i_mode = old_mode;
126 return ret;
Yan744f52f2008-01-14 13:26:08 -0500127}
Josef Bacik1caf9342007-11-19 10:18:17 -0500128
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000129int btrfs_init_acl(struct btrfs_trans_handle *trans,
130 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400131{
Christoph Hellwig996a7102013-12-20 05:16:43 -0800132 struct posix_acl *default_acl, *acl;
Josef Bacik33268ea2008-07-24 12:16:36 -0400133 int ret = 0;
134
135 /* this happens with subvols */
136 if (!dir)
137 return 0;
138
Christoph Hellwig996a7102013-12-20 05:16:43 -0800139 ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400140 if (ret)
141 return ret;
Christoph Hellwig996a7102013-12-20 05:16:43 -0800142
143 if (default_acl) {
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200144 ret = __btrfs_set_acl(trans, &init_user_ns, inode, default_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -0800145 ACL_TYPE_DEFAULT);
146 posix_acl_release(default_acl);
147 }
148
149 if (acl) {
150 if (!ret)
Christian Brauner4a8b34a2021-07-27 12:48:58 +0200151 ret = __btrfs_set_acl(trans, &init_user_ns, inode, acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -0800152 ACL_TYPE_ACCESS);
153 posix_acl_release(acl);
154 }
155
156 if (!default_acl && !acl)
157 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400158 return ret;
159}