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