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