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