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