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> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 25 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 26 | #include "ctree.h" |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 27 | #include "btrfs_inode.h" |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 28 | #include "xattr.h" |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 29 | |
Chris Mason | 0eda294 | 2009-10-13 13:50:18 -0400 | [diff] [blame] | 30 | #ifdef CONFIG_BTRFS_FS_POSIX_ACL |
Josef Bacik | eab922e | 2008-08-28 06:21:15 -0400 | [diff] [blame] | 31 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 32 | static struct posix_acl *btrfs_get_acl(struct inode *inode, int type) |
| 33 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 34 | int size; |
| 35 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 36 | char *value = NULL; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 37 | struct posix_acl *acl; |
| 38 | |
| 39 | acl = get_cached_acl(inode, type); |
| 40 | if (acl != ACL_NOT_CACHED) |
| 41 | return acl; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 42 | |
| 43 | switch (type) { |
| 44 | case ACL_TYPE_ACCESS: |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 45 | name = POSIX_ACL_XATTR_ACCESS; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 46 | break; |
| 47 | case ACL_TYPE_DEFAULT: |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 48 | name = POSIX_ACL_XATTR_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 49 | break; |
| 50 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 51 | BUG(); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 54 | size = __btrfs_getxattr(inode, name, "", 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 55 | if (size > 0) { |
| 56 | value = kzalloc(size, GFP_NOFS); |
| 57 | if (!value) |
| 58 | return ERR_PTR(-ENOMEM); |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 59 | size = __btrfs_getxattr(inode, name, value, size); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 60 | if (size > 0) { |
| 61 | acl = posix_acl_from_xattr(value, size); |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 62 | set_cached_acl(inode, type, acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 63 | } |
| 64 | kfree(value); |
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; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 68 | set_cached_acl(inode, type, acl); |
Chris Mason | 7b1a14b | 2009-04-27 10:49:53 -0400 | [diff] [blame] | 69 | } else { |
| 70 | acl = ERR_PTR(-EIO); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return acl; |
| 74 | } |
| 75 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 76 | static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name, |
| 77 | void *value, size_t size, int type) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 78 | { |
| 79 | struct posix_acl *acl; |
| 80 | int ret = 0; |
| 81 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 82 | acl = btrfs_get_acl(dentry->d_inode, type); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 83 | |
| 84 | if (IS_ERR(acl)) |
| 85 | return PTR_ERR(acl); |
| 86 | if (acl == NULL) |
| 87 | return -ENODATA; |
| 88 | ret = posix_acl_to_xattr(acl, value, size); |
| 89 | posix_acl_release(acl); |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Needs to be called with fs_mutex held |
| 96 | */ |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 97 | static int btrfs_set_acl(struct btrfs_trans_handle *trans, |
| 98 | struct inode *inode, struct posix_acl *acl, int type) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 99 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 100 | int ret, size = 0; |
| 101 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 102 | char *value = NULL; |
| 103 | mode_t mode; |
| 104 | |
| 105 | if (acl) { |
| 106 | ret = posix_acl_valid(acl); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | ret = 0; |
| 110 | } |
| 111 | |
| 112 | switch (type) { |
| 113 | case ACL_TYPE_ACCESS: |
| 114 | mode = inode->i_mode; |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 115 | name = POSIX_ACL_XATTR_ACCESS; |
Chris Mason | a9cc71a | 2010-01-17 20:36:18 -0500 | [diff] [blame] | 116 | if (acl) { |
| 117 | ret = posix_acl_equiv_mode(acl, &mode); |
| 118 | if (ret < 0) |
| 119 | return ret; |
| 120 | inode->i_mode = mode; |
| 121 | } |
| 122 | ret = 0; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 123 | break; |
| 124 | case ACL_TYPE_DEFAULT: |
| 125 | if (!S_ISDIR(inode->i_mode)) |
| 126 | return acl ? -EINVAL : 0; |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 127 | name = POSIX_ACL_XATTR_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 128 | break; |
| 129 | default: |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | if (acl) { |
| 134 | size = posix_acl_xattr_size(acl->a_count); |
| 135 | value = kmalloc(size, GFP_NOFS); |
| 136 | if (!value) { |
| 137 | ret = -ENOMEM; |
| 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | ret = posix_acl_to_xattr(acl, value, size); |
| 142 | if (ret < 0) |
| 143 | goto out; |
| 144 | } |
| 145 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 146 | ret = __btrfs_setxattr(trans, inode, name, value, size, 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 147 | out: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 148 | kfree(value); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 149 | |
| 150 | if (!ret) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 151 | set_cached_acl(inode, type, acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 152 | |
| 153 | return ret; |
| 154 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 155 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 156 | static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name, |
| 157 | const void *value, size_t size, int flags, int type) |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 158 | { |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 159 | int ret; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 160 | struct posix_acl *acl = NULL; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 161 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 162 | if (value) { |
| 163 | acl = posix_acl_from_xattr(value, size); |
| 164 | if (acl == NULL) { |
| 165 | value = NULL; |
| 166 | size = 0; |
| 167 | } else if (IS_ERR(acl)) { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 168 | return PTR_ERR(acl); |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 169 | } |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 170 | } |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 171 | |
Chris Mason | ebfee3d | 2009-12-17 15:02:22 -0500 | [diff] [blame] | 172 | ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 173 | |
| 174 | posix_acl_release(acl); |
| 175 | |
| 176 | return ret; |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 177 | } |
Josef Bacik | 1caf934 | 2007-11-19 10:18:17 -0500 | [diff] [blame] | 178 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 179 | int btrfs_check_acl(struct inode *inode, int mask) |
| 180 | { |
| 181 | struct posix_acl *acl; |
| 182 | int error = -EAGAIN; |
| 183 | |
| 184 | acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); |
| 185 | |
| 186 | if (IS_ERR(acl)) |
| 187 | return PTR_ERR(acl); |
| 188 | if (acl) { |
| 189 | error = posix_acl_permission(inode, acl, mask); |
| 190 | posix_acl_release(acl); |
| 191 | } |
| 192 | |
| 193 | return error; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * btrfs_init_acl is already generally called under fs_mutex, so the locking |
| 198 | * stuff has been fixed to work with that. If the locking stuff changes, we |
| 199 | * need to re-evaluate the acl locking stuff. |
| 200 | */ |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 201 | int btrfs_init_acl(struct btrfs_trans_handle *trans, |
| 202 | struct inode *inode, struct inode *dir) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 203 | { |
| 204 | struct posix_acl *acl = NULL; |
| 205 | int ret = 0; |
| 206 | |
| 207 | /* this happens with subvols */ |
| 208 | if (!dir) |
| 209 | return 0; |
| 210 | |
| 211 | if (!S_ISLNK(inode->i_mode)) { |
| 212 | if (IS_POSIXACL(dir)) { |
| 213 | acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT); |
| 214 | if (IS_ERR(acl)) |
| 215 | return PTR_ERR(acl); |
| 216 | } |
| 217 | |
| 218 | if (!acl) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 219 | inode->i_mode &= ~current_umask(); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | if (IS_POSIXACL(dir) && acl) { |
| 223 | struct posix_acl *clone; |
| 224 | mode_t mode; |
| 225 | |
| 226 | if (S_ISDIR(inode->i_mode)) { |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 227 | ret = btrfs_set_acl(trans, inode, acl, |
| 228 | ACL_TYPE_DEFAULT); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 229 | if (ret) |
| 230 | goto failed; |
| 231 | } |
| 232 | clone = posix_acl_clone(acl, GFP_NOFS); |
| 233 | ret = -ENOMEM; |
| 234 | if (!clone) |
| 235 | goto failed; |
| 236 | |
| 237 | mode = inode->i_mode; |
| 238 | ret = posix_acl_create_masq(clone, &mode); |
| 239 | if (ret >= 0) { |
| 240 | inode->i_mode = mode; |
| 241 | if (ret > 0) { |
| 242 | /* we need an acl */ |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 243 | ret = btrfs_set_acl(trans, inode, clone, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 244 | ACL_TYPE_ACCESS); |
| 245 | } |
| 246 | } |
Yang Hongyang | f858153 | 2010-01-26 00:48:23 +0000 | [diff] [blame] | 247 | posix_acl_release(clone); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 248 | } |
| 249 | failed: |
| 250 | posix_acl_release(acl); |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | int btrfs_acl_chmod(struct inode *inode) |
| 256 | { |
| 257 | struct posix_acl *acl, *clone; |
| 258 | int ret = 0; |
| 259 | |
| 260 | if (S_ISLNK(inode->i_mode)) |
| 261 | return -EOPNOTSUPP; |
| 262 | |
| 263 | if (!IS_POSIXACL(inode)) |
| 264 | return 0; |
| 265 | |
| 266 | acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); |
| 267 | if (IS_ERR(acl) || !acl) |
| 268 | return PTR_ERR(acl); |
| 269 | |
| 270 | clone = posix_acl_clone(acl, GFP_KERNEL); |
| 271 | posix_acl_release(acl); |
| 272 | if (!clone) |
| 273 | return -ENOMEM; |
| 274 | |
| 275 | ret = posix_acl_chmod_masq(clone, inode->i_mode); |
| 276 | if (!ret) |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 277 | ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 278 | |
| 279 | posix_acl_release(clone); |
| 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 284 | struct xattr_handler btrfs_xattr_acl_default_handler = { |
| 285 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 286 | .flags = ACL_TYPE_DEFAULT, |
| 287 | .get = btrfs_xattr_acl_get, |
| 288 | .set = btrfs_xattr_acl_set, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | struct xattr_handler btrfs_xattr_acl_access_handler = { |
| 292 | .prefix = POSIX_ACL_XATTR_ACCESS, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 293 | .flags = ACL_TYPE_ACCESS, |
| 294 | .get = btrfs_xattr_acl_get, |
| 295 | .set = btrfs_xattr_acl_set, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 296 | }; |
Josef Bacik | eab922e | 2008-08-28 06:21:15 -0400 | [diff] [blame] | 297 | |
Chris Mason | 0eda294 | 2009-10-13 13:50:18 -0400 | [diff] [blame] | 298 | #else /* CONFIG_BTRFS_FS_POSIX_ACL */ |
Josef Bacik | eab922e | 2008-08-28 06:21:15 -0400 | [diff] [blame] | 299 | |
| 300 | int btrfs_acl_chmod(struct inode *inode) |
| 301 | { |
| 302 | return 0; |
| 303 | } |
| 304 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 305 | int btrfs_init_acl(struct btrfs_trans_handle *trans, |
| 306 | struct inode *inode, struct inode *dir) |
Josef Bacik | eab922e | 2008-08-28 06:21:15 -0400 | [diff] [blame] | 307 | { |
| 308 | return 0; |
| 309 | } |
| 310 | |
Chris Mason | 0eda294 | 2009-10-13 13:50:18 -0400 | [diff] [blame] | 311 | #endif /* CONFIG_BTRFS_FS_POSIX_ACL */ |