David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Red Hat. All rights reserved. |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/fs.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/xattr.h> |
| 9 | #include <linux/posix_acl_xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10 | #include <linux/posix_acl.h> |
Chris Mason | c1e32da | 2008-01-22 12:46:56 -0500 | [diff] [blame] | 11 | #include <linux/sched.h> |
Filipe Manana | a087349 | 2018-12-13 21:16:56 +0000 | [diff] [blame] | 12 | #include <linux/sched/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 14 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 15 | #include "ctree.h" |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 16 | #include "btrfs_inode.h" |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 17 | #include "xattr.h" |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 18 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 19 | struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 20 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 21 | int size; |
| 22 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 23 | char *value = NULL; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 24 | struct posix_acl *acl; |
| 25 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 26 | if (rcu) |
| 27 | return ERR_PTR(-ECHILD); |
| 28 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 29 | switch (type) { |
| 30 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 31 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 32 | break; |
| 33 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 34 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 35 | break; |
| 36 | default: |
Chengguang Xu | ab3629e | 2018-06-27 12:16:34 +0800 | [diff] [blame] | 37 | return ERR_PTR(-EINVAL); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Chengguang Xu | 7e35eab | 2018-06-27 12:16:35 +0800 | [diff] [blame] | 40 | size = btrfs_getxattr(inode, name, NULL, 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 41 | if (size > 0) { |
David Sterba | 39a27ec | 2015-12-03 12:49:48 +0100 | [diff] [blame] | 42 | value = kzalloc(size, GFP_KERNEL); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 43 | if (!value) |
| 44 | return ERR_PTR(-ENOMEM); |
David Sterba | 7852781 | 2018-02-27 15:48:52 +0100 | [diff] [blame] | 45 | size = btrfs_getxattr(inode, name, value, size); |
Tsutomu Itoh | cfbffc3 | 2011-10-06 13:37:08 +0900 | [diff] [blame] | 46 | } |
Chengguang Xu | 4de426c | 2018-06-27 12:16:38 +0800 | [diff] [blame] | 47 | if (size > 0) |
Eric W. Biederman | 5f3a4a28 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 48 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
Chengguang Xu | 4de426c | 2018-06-27 12:16:38 +0800 | [diff] [blame] | 49 | else if (size == -ENODATA || size == 0) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 50 | acl = NULL; |
Chengguang Xu | 4de426c | 2018-06-27 12:16:38 +0800 | [diff] [blame] | 51 | else |
Chengguang Xu | dc7789e | 2018-06-27 12:16:37 +0800 | [diff] [blame] | 52 | acl = ERR_PTR(size); |
Tsutomu Itoh | cfbffc3 | 2011-10-06 13:37:08 +0900 | [diff] [blame] | 53 | kfree(value); |
| 54 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 55 | return acl; |
| 56 | } |
| 57 | |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 58 | static int __btrfs_set_acl(struct btrfs_trans_handle *trans, |
Christian Brauner | 4a8b34a | 2021-07-27 12:48:58 +0200 | [diff] [blame] | 59 | struct user_namespace *mnt_userns, |
| 60 | struct inode *inode, struct posix_acl *acl, int type) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 61 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 62 | int ret, size = 0; |
| 63 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 64 | char *value = NULL; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 65 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 66 | switch (type) { |
| 67 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 68 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 69 | break; |
| 70 | case ACL_TYPE_DEFAULT: |
| 71 | if (!S_ISDIR(inode->i_mode)) |
| 72 | return acl ? -EINVAL : 0; |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 73 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 74 | break; |
| 75 | default: |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | if (acl) { |
Filipe Manana | a087349 | 2018-12-13 21:16:56 +0000 | [diff] [blame] | 80 | unsigned int nofs_flag; |
| 81 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 82 | size = posix_acl_xattr_size(acl->a_count); |
Filipe Manana | a087349 | 2018-12-13 21:16:56 +0000 | [diff] [blame] | 83 | /* |
| 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 Sterba | 39a27ec | 2015-12-03 12:49:48 +0100 | [diff] [blame] | 88 | value = kmalloc(size, GFP_KERNEL); |
Filipe Manana | a087349 | 2018-12-13 21:16:56 +0000 | [diff] [blame] | 89 | memalloc_nofs_restore(nofs_flag); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 90 | if (!value) { |
| 91 | ret = -ENOMEM; |
| 92 | goto out; |
| 93 | } |
| 94 | |
Eric W. Biederman | 5f3a4a28 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 95 | ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 96 | if (ret < 0) |
| 97 | goto out; |
| 98 | } |
| 99 | |
Anand Jain | 04e6863 | 2019-04-12 16:02:58 +0800 | [diff] [blame] | 100 | if (trans) |
| 101 | ret = btrfs_setxattr(trans, inode, name, value, size, 0); |
| 102 | else |
Anand Jain | e3de9b1 | 2019-04-12 16:02:59 +0800 | [diff] [blame] | 103 | ret = btrfs_setxattr_trans(inode, name, value, size, 0); |
Anand Jain | 04e6863 | 2019-04-12 16:02:58 +0800 | [diff] [blame] | 104 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 105 | out: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 106 | kfree(value); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 107 | |
| 108 | if (!ret) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 109 | set_cached_acl(inode, type, acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 110 | |
| 111 | return ret; |
| 112 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 113 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 114 | int btrfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode, |
| 115 | struct posix_acl *acl, int type) |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 116 | { |
Jan Kara | b7f8a09 | 2017-06-22 15:31:07 +0200 | [diff] [blame] | 117 | int ret; |
Ernesto A. Fernández | d7d8249 | 2017-08-02 03:18:27 -0300 | [diff] [blame] | 118 | umode_t old_mode = inode->i_mode; |
Jan Kara | b7f8a09 | 2017-06-22 15:31:07 +0200 | [diff] [blame] | 119 | |
| 120 | if (type == ACL_TYPE_ACCESS && acl) { |
Christian Brauner | 4a8b34a | 2021-07-27 12:48:58 +0200 | [diff] [blame] | 121 | ret = posix_acl_update_mode(mnt_userns, inode, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 122 | &inode->i_mode, &acl); |
Jan Kara | b7f8a09 | 2017-06-22 15:31:07 +0200 | [diff] [blame] | 123 | if (ret) |
| 124 | return ret; |
| 125 | } |
Christian Brauner | 4a8b34a | 2021-07-27 12:48:58 +0200 | [diff] [blame] | 126 | ret = __btrfs_set_acl(NULL, mnt_userns, inode, acl, type); |
Ernesto A. Fernández | d7d8249 | 2017-08-02 03:18:27 -0300 | [diff] [blame] | 127 | if (ret) |
| 128 | inode->i_mode = old_mode; |
| 129 | return ret; |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 130 | } |
Josef Bacik | 1caf934 | 2007-11-19 10:18:17 -0500 | [diff] [blame] | 131 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 132 | int btrfs_init_acl(struct btrfs_trans_handle *trans, |
| 133 | struct inode *inode, struct inode *dir) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 134 | { |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 135 | struct posix_acl *default_acl, *acl; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 136 | int ret = 0; |
| 137 | |
| 138 | /* this happens with subvols */ |
| 139 | if (!dir) |
| 140 | return 0; |
| 141 | |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 142 | ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 143 | if (ret) |
| 144 | return ret; |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 145 | |
| 146 | if (default_acl) { |
Christian Brauner | 4a8b34a | 2021-07-27 12:48:58 +0200 | [diff] [blame] | 147 | ret = __btrfs_set_acl(trans, &init_user_ns, inode, default_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 148 | ACL_TYPE_DEFAULT); |
| 149 | posix_acl_release(default_acl); |
| 150 | } |
| 151 | |
| 152 | if (acl) { |
| 153 | if (!ret) |
Christian Brauner | 4a8b34a | 2021-07-27 12:48:58 +0200 | [diff] [blame] | 154 | ret = __btrfs_set_acl(trans, &init_user_ns, inode, acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 155 | ACL_TYPE_ACCESS); |
| 156 | posix_acl_release(acl); |
| 157 | } |
| 158 | |
| 159 | if (!default_acl && !acl) |
| 160 | cache_no_acl(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 161 | return ret; |
| 162 | } |