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 | |
Miao Xie | d0f6968 | 2011-01-25 15:46:17 +0800 | [diff] [blame] | 38 | if (!IS_POSIXACL(inode)) |
| 39 | return NULL; |
| 40 | |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 41 | acl = get_cached_acl(inode, type); |
| 42 | if (acl != ACL_NOT_CACHED) |
| 43 | return acl; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 44 | |
| 45 | switch (type) { |
| 46 | case ACL_TYPE_ACCESS: |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 47 | name = POSIX_ACL_XATTR_ACCESS; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 48 | break; |
| 49 | case ACL_TYPE_DEFAULT: |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 50 | name = POSIX_ACL_XATTR_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 51 | break; |
| 52 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 53 | BUG(); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 56 | size = __btrfs_getxattr(inode, name, "", 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 57 | if (size > 0) { |
| 58 | value = kzalloc(size, GFP_NOFS); |
| 59 | if (!value) |
| 60 | return ERR_PTR(-ENOMEM); |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 61 | size = __btrfs_getxattr(inode, name, value, size); |
Tsutomu Itoh | cfbffc3 | 2011-10-06 13:37:08 +0900 | [diff] [blame] | 62 | } |
| 63 | if (size > 0) { |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 64 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
Chris Mason | 7b1a14b | 2009-04-27 10:49:53 -0400 | [diff] [blame] | 65 | } else if (size == -ENOENT || size == -ENODATA || size == 0) { |
| 66 | /* FIXME, who returns -ENOENT? I think nobody */ |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 67 | acl = NULL; |
Chris Mason | 7b1a14b | 2009-04-27 10:49:53 -0400 | [diff] [blame] | 68 | } else { |
| 69 | acl = ERR_PTR(-EIO); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 70 | } |
Tsutomu Itoh | cfbffc3 | 2011-10-06 13:37:08 +0900 | [diff] [blame] | 71 | kfree(value); |
| 72 | |
| 73 | if (!IS_ERR(acl)) |
| 74 | set_cached_acl(inode, type, acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 75 | |
| 76 | return acl; |
| 77 | } |
| 78 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 79 | static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name, |
| 80 | void *value, size_t size, int type) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 81 | { |
| 82 | struct posix_acl *acl; |
| 83 | int ret = 0; |
| 84 | |
Miao Xie | d0f6968 | 2011-01-25 15:46:17 +0800 | [diff] [blame] | 85 | if (!IS_POSIXACL(dentry->d_inode)) |
| 86 | return -EOPNOTSUPP; |
| 87 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 88 | acl = btrfs_get_acl(dentry->d_inode, type); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 89 | |
| 90 | if (IS_ERR(acl)) |
| 91 | return PTR_ERR(acl); |
| 92 | if (acl == NULL) |
| 93 | return -ENODATA; |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 94 | ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 95 | posix_acl_release(acl); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Needs to be called with fs_mutex held |
| 102 | */ |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 103 | static int btrfs_set_acl(struct btrfs_trans_handle *trans, |
| 104 | struct inode *inode, struct posix_acl *acl, int type) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 105 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 106 | int ret, size = 0; |
| 107 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 108 | char *value = NULL; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 109 | |
| 110 | if (acl) { |
| 111 | ret = posix_acl_valid(acl); |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | ret = 0; |
| 115 | } |
| 116 | |
| 117 | switch (type) { |
| 118 | case ACL_TYPE_ACCESS: |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 119 | name = POSIX_ACL_XATTR_ACCESS; |
Chris Mason | a9cc71a | 2010-01-17 20:36:18 -0500 | [diff] [blame] | 120 | if (acl) { |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 121 | ret = posix_acl_equiv_mode(acl, &inode->i_mode); |
Chris Mason | a9cc71a | 2010-01-17 20:36:18 -0500 | [diff] [blame] | 122 | if (ret < 0) |
| 123 | return ret; |
Liu Bo | 755ac67 | 2012-11-28 10:43:11 +0000 | [diff] [blame] | 124 | if (ret == 0) |
| 125 | acl = NULL; |
Chris Mason | a9cc71a | 2010-01-17 20:36:18 -0500 | [diff] [blame] | 126 | } |
| 127 | ret = 0; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 128 | break; |
| 129 | case ACL_TYPE_DEFAULT: |
| 130 | if (!S_ISDIR(inode->i_mode)) |
| 131 | return acl ? -EINVAL : 0; |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 132 | name = POSIX_ACL_XATTR_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 133 | break; |
| 134 | default: |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | if (acl) { |
| 139 | size = posix_acl_xattr_size(acl->a_count); |
| 140 | value = kmalloc(size, GFP_NOFS); |
| 141 | if (!value) { |
| 142 | ret = -ENOMEM; |
| 143 | goto out; |
| 144 | } |
| 145 | |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 146 | ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 147 | if (ret < 0) |
| 148 | goto out; |
| 149 | } |
| 150 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 151 | ret = __btrfs_setxattr(trans, inode, name, value, size, 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 152 | out: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 153 | kfree(value); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 154 | |
| 155 | if (!ret) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 156 | set_cached_acl(inode, type, acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 157 | |
| 158 | return ret; |
| 159 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 160 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 161 | static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name, |
| 162 | const void *value, size_t size, int flags, int type) |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 163 | { |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 164 | int ret; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 165 | struct posix_acl *acl = NULL; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 166 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 167 | if (!inode_owner_or_capable(dentry->d_inode)) |
Shi Weihua | 2f26afb | 2010-05-18 00:50:32 +0000 | [diff] [blame] | 168 | return -EPERM; |
| 169 | |
Shi Weihua | 731e3d1 | 2010-05-18 00:51:54 +0000 | [diff] [blame] | 170 | if (!IS_POSIXACL(dentry->d_inode)) |
| 171 | return -EOPNOTSUPP; |
| 172 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 173 | if (value) { |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 174 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
Daniel J Blueman | f5de939 | 2011-05-03 16:44:13 +0000 | [diff] [blame] | 175 | if (IS_ERR(acl)) |
| 176 | return PTR_ERR(acl); |
| 177 | |
Miao Xie | 329c505 | 2011-04-13 14:07:59 +0800 | [diff] [blame] | 178 | if (acl) { |
| 179 | ret = posix_acl_valid(acl); |
| 180 | if (ret) |
| 181 | goto out; |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 182 | } |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 183 | } |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 184 | |
Chris Mason | ebfee3d | 2009-12-17 15:02:22 -0500 | [diff] [blame] | 185 | ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type); |
Miao Xie | 329c505 | 2011-04-13 14:07:59 +0800 | [diff] [blame] | 186 | out: |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 187 | posix_acl_release(acl); |
| 188 | |
| 189 | return ret; |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 190 | } |
Josef Bacik | 1caf934 | 2007-11-19 10:18:17 -0500 | [diff] [blame] | 191 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 192 | /* |
| 193 | * btrfs_init_acl is already generally called under fs_mutex, so the locking |
| 194 | * stuff has been fixed to work with that. If the locking stuff changes, we |
| 195 | * need to re-evaluate the acl locking stuff. |
| 196 | */ |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 197 | int btrfs_init_acl(struct btrfs_trans_handle *trans, |
| 198 | struct inode *inode, struct inode *dir) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 199 | { |
| 200 | struct posix_acl *acl = NULL; |
| 201 | int ret = 0; |
| 202 | |
| 203 | /* this happens with subvols */ |
| 204 | if (!dir) |
| 205 | return 0; |
| 206 | |
| 207 | if (!S_ISLNK(inode->i_mode)) { |
| 208 | if (IS_POSIXACL(dir)) { |
| 209 | acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT); |
| 210 | if (IS_ERR(acl)) |
| 211 | return PTR_ERR(acl); |
| 212 | } |
| 213 | |
| 214 | if (!acl) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 215 | inode->i_mode &= ~current_umask(); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (IS_POSIXACL(dir) && acl) { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 219 | if (S_ISDIR(inode->i_mode)) { |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 220 | ret = btrfs_set_acl(trans, inode, acl, |
| 221 | ACL_TYPE_DEFAULT); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 222 | if (ret) |
| 223 | goto failed; |
| 224 | } |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 225 | ret = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode); |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 226 | if (ret < 0) |
| 227 | return ret; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 228 | |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 229 | if (ret > 0) { |
| 230 | /* we need an acl */ |
| 231 | ret = btrfs_set_acl(trans, inode, acl, ACL_TYPE_ACCESS); |
Josef Bacik | 30f8fe3 | 2012-04-23 13:55:30 -0400 | [diff] [blame] | 232 | } else { |
| 233 | cache_no_acl(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 234 | } |
Josef Bacik | 30f8fe3 | 2012-04-23 13:55:30 -0400 | [diff] [blame] | 235 | } else { |
| 236 | cache_no_acl(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 237 | } |
| 238 | failed: |
| 239 | posix_acl_release(acl); |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | int btrfs_acl_chmod(struct inode *inode) |
| 245 | { |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 246 | struct posix_acl *acl; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 247 | int ret = 0; |
| 248 | |
| 249 | if (S_ISLNK(inode->i_mode)) |
| 250 | return -EOPNOTSUPP; |
| 251 | |
| 252 | if (!IS_POSIXACL(inode)) |
| 253 | return 0; |
| 254 | |
| 255 | acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 256 | if (IS_ERR_OR_NULL(acl)) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 257 | return PTR_ERR(acl); |
| 258 | |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 259 | ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); |
| 260 | if (ret) |
| 261 | return ret; |
| 262 | ret = btrfs_set_acl(NULL, inode, acl, ACL_TYPE_ACCESS); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 263 | posix_acl_release(acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
Stephen Hemminger | f01cbd3 | 2010-05-13 17:53:15 -0700 | [diff] [blame] | 267 | const struct xattr_handler btrfs_xattr_acl_default_handler = { |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 268 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 269 | .flags = ACL_TYPE_DEFAULT, |
| 270 | .get = btrfs_xattr_acl_get, |
| 271 | .set = btrfs_xattr_acl_set, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 272 | }; |
| 273 | |
Stephen Hemminger | f01cbd3 | 2010-05-13 17:53:15 -0700 | [diff] [blame] | 274 | const struct xattr_handler btrfs_xattr_acl_access_handler = { |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 275 | .prefix = POSIX_ACL_XATTR_ACCESS, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 276 | .flags = ACL_TYPE_ACCESS, |
| 277 | .get = btrfs_xattr_acl_get, |
| 278 | .set = btrfs_xattr_acl_set, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 279 | }; |