Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Red Hat. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/xattr.h> |
| 22 | #include <linux/posix_acl_xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 23 | #include <linux/posix_acl.h> |
Chris Mason | c1e32da | 2008-01-22 12:46:56 -0500 | [diff] [blame] | 24 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 26 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 27 | #include "ctree.h" |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 28 | #include "btrfs_inode.h" |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 29 | #include "xattr.h" |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 30 | |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 31 | struct posix_acl *btrfs_get_acl(struct inode *inode, int type) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 32 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 33 | int size; |
| 34 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 35 | char *value = NULL; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 36 | struct posix_acl *acl; |
| 37 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 38 | switch (type) { |
| 39 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 40 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 41 | break; |
| 42 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 43 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 44 | break; |
| 45 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 46 | BUG(); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 49 | size = __btrfs_getxattr(inode, name, "", 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 50 | if (size > 0) { |
David Sterba | 39a27ec | 2015-12-03 12:49:48 +0100 | [diff] [blame] | 51 | value = kzalloc(size, GFP_KERNEL); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 52 | if (!value) |
| 53 | return ERR_PTR(-ENOMEM); |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 54 | size = __btrfs_getxattr(inode, name, value, size); |
Tsutomu Itoh | cfbffc3 | 2011-10-06 13:37:08 +0900 | [diff] [blame] | 55 | } |
| 56 | if (size > 0) { |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 57 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
Salah Triki | a60617d | 2016-07-03 05:40:10 +0100 | [diff] [blame] | 58 | } else if (size == -ERANGE || size == -ENODATA || size == 0) { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 59 | acl = NULL; |
Chris Mason | 7b1a14b | 2009-04-27 10:49:53 -0400 | [diff] [blame] | 60 | } else { |
| 61 | acl = ERR_PTR(-EIO); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 62 | } |
Tsutomu Itoh | cfbffc3 | 2011-10-06 13:37:08 +0900 | [diff] [blame] | 63 | kfree(value); |
| 64 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 65 | return acl; |
| 66 | } |
| 67 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 68 | /* |
| 69 | * Needs to be called with fs_mutex held |
| 70 | */ |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 71 | static int __btrfs_set_acl(struct btrfs_trans_handle *trans, |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 72 | struct inode *inode, struct posix_acl *acl, int type) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 73 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 74 | int ret, size = 0; |
| 75 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 76 | char *value = NULL; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 77 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 78 | switch (type) { |
| 79 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 80 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
Chris Mason | a9cc71a | 2010-01-17 20:36:18 -0500 | [diff] [blame] | 81 | if (acl) { |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 82 | ret = posix_acl_update_mode(inode, &inode->i_mode, &acl); |
| 83 | if (ret) |
Chris Mason | a9cc71a | 2010-01-17 20:36:18 -0500 | [diff] [blame] | 84 | return ret; |
Chris Mason | a9cc71a | 2010-01-17 20:36:18 -0500 | [diff] [blame] | 85 | } |
| 86 | ret = 0; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 87 | break; |
| 88 | case ACL_TYPE_DEFAULT: |
| 89 | if (!S_ISDIR(inode->i_mode)) |
| 90 | return acl ? -EINVAL : 0; |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 91 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 92 | break; |
| 93 | default: |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | if (acl) { |
| 98 | size = posix_acl_xattr_size(acl->a_count); |
David Sterba | 39a27ec | 2015-12-03 12:49:48 +0100 | [diff] [blame] | 99 | value = kmalloc(size, GFP_KERNEL); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 100 | if (!value) { |
| 101 | ret = -ENOMEM; |
| 102 | goto out; |
| 103 | } |
| 104 | |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 105 | ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 106 | if (ret < 0) |
| 107 | goto out; |
| 108 | } |
| 109 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 110 | ret = __btrfs_setxattr(trans, inode, name, value, size, 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 111 | out: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 112 | kfree(value); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 113 | |
| 114 | if (!ret) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 115 | set_cached_acl(inode, type, acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 116 | |
| 117 | return ret; |
| 118 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 119 | |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 120 | int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 121 | { |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 122 | return __btrfs_set_acl(NULL, inode, acl, type); |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 123 | } |
Josef Bacik | 1caf934 | 2007-11-19 10:18:17 -0500 | [diff] [blame] | 124 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 125 | /* |
| 126 | * btrfs_init_acl is already generally called under fs_mutex, so the locking |
| 127 | * stuff has been fixed to work with that. If the locking stuff changes, we |
| 128 | * need to re-evaluate the acl locking stuff. |
| 129 | */ |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 130 | int btrfs_init_acl(struct btrfs_trans_handle *trans, |
| 131 | struct inode *inode, struct inode *dir) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 132 | { |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 133 | struct posix_acl *default_acl, *acl; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 134 | int ret = 0; |
| 135 | |
| 136 | /* this happens with subvols */ |
| 137 | if (!dir) |
| 138 | return 0; |
| 139 | |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 140 | ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 141 | if (ret) |
| 142 | return ret; |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 143 | |
| 144 | if (default_acl) { |
| 145 | ret = __btrfs_set_acl(trans, inode, default_acl, |
| 146 | ACL_TYPE_DEFAULT); |
| 147 | posix_acl_release(default_acl); |
| 148 | } |
| 149 | |
| 150 | if (acl) { |
| 151 | if (!ret) |
| 152 | ret = __btrfs_set_acl(trans, inode, acl, |
| 153 | ACL_TYPE_ACCESS); |
| 154 | posix_acl_release(acl); |
| 155 | } |
| 156 | |
| 157 | if (!default_acl && !acl) |
| 158 | cache_no_acl(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 159 | return ret; |
| 160 | } |