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