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 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 | |
| 76 | static int btrfs_xattr_get_acl(struct inode *inode, int type, |
| 77 | void *value, size_t size) |
| 78 | { |
| 79 | struct posix_acl *acl; |
| 80 | int ret = 0; |
| 81 | |
| 82 | acl = btrfs_get_acl(inode, type); |
| 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 | */ |
| 97 | static int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
| 98 | { |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 99 | int ret, size = 0; |
| 100 | const char *name; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 101 | char *value = NULL; |
| 102 | mode_t mode; |
| 103 | |
| 104 | if (acl) { |
| 105 | ret = posix_acl_valid(acl); |
| 106 | if (ret < 0) |
| 107 | return ret; |
| 108 | ret = 0; |
| 109 | } |
| 110 | |
| 111 | switch (type) { |
| 112 | case ACL_TYPE_ACCESS: |
| 113 | mode = inode->i_mode; |
| 114 | ret = posix_acl_equiv_mode(acl, &mode); |
| 115 | if (ret < 0) |
| 116 | return ret; |
| 117 | ret = 0; |
| 118 | inode->i_mode = mode; |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 119 | name = POSIX_ACL_XATTR_ACCESS; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 120 | break; |
| 121 | case ACL_TYPE_DEFAULT: |
| 122 | if (!S_ISDIR(inode->i_mode)) |
| 123 | return acl ? -EINVAL : 0; |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 124 | name = POSIX_ACL_XATTR_DEFAULT; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 125 | break; |
| 126 | default: |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | |
| 130 | if (acl) { |
| 131 | size = posix_acl_xattr_size(acl->a_count); |
| 132 | value = kmalloc(size, GFP_NOFS); |
| 133 | if (!value) { |
| 134 | ret = -ENOMEM; |
| 135 | goto out; |
| 136 | } |
| 137 | |
| 138 | ret = posix_acl_to_xattr(acl, value, size); |
| 139 | if (ret < 0) |
| 140 | goto out; |
| 141 | } |
| 142 | |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 143 | ret = __btrfs_setxattr(inode, name, value, size, 0); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 144 | |
| 145 | out: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 146 | kfree(value); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 147 | |
| 148 | if (!ret) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame^] | 149 | set_cached_acl(inode, type, acl); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 150 | |
| 151 | return ret; |
| 152 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 153 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 154 | static int btrfs_xattr_set_acl(struct inode *inode, int type, |
| 155 | const void *value, size_t size) |
| 156 | { |
| 157 | int ret = 0; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 158 | struct posix_acl *acl = NULL; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 159 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 160 | if (value) { |
| 161 | acl = posix_acl_from_xattr(value, size); |
| 162 | if (acl == NULL) { |
| 163 | value = NULL; |
| 164 | size = 0; |
| 165 | } else if (IS_ERR(acl)) { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 166 | return PTR_ERR(acl); |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 167 | } |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 168 | } |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 169 | |
| 170 | ret = btrfs_set_acl(inode, acl, type); |
| 171 | |
| 172 | posix_acl_release(acl); |
| 173 | |
| 174 | return ret; |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 175 | } |
Josef Bacik | 1caf934 | 2007-11-19 10:18:17 -0500 | [diff] [blame] | 176 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 177 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 178 | static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name, |
| 179 | void *value, size_t size) |
| 180 | { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 181 | return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 182 | } |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 183 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 184 | static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name, |
| 185 | const void *value, size_t size, int flags) |
| 186 | { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 187 | return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 188 | } |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 189 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 190 | static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name, |
| 191 | void *value, size_t size) |
| 192 | { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 193 | return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 194 | } |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 195 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 196 | 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] | 197 | const void *value, size_t size, int flags) |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 198 | { |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 199 | return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 200 | } |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 201 | |
| 202 | int btrfs_check_acl(struct inode *inode, int mask) |
| 203 | { |
| 204 | struct posix_acl *acl; |
| 205 | int error = -EAGAIN; |
| 206 | |
| 207 | acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); |
| 208 | |
| 209 | if (IS_ERR(acl)) |
| 210 | return PTR_ERR(acl); |
| 211 | if (acl) { |
| 212 | error = posix_acl_permission(inode, acl, mask); |
| 213 | posix_acl_release(acl); |
| 214 | } |
| 215 | |
| 216 | return error; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * btrfs_init_acl is already generally called under fs_mutex, so the locking |
| 221 | * stuff has been fixed to work with that. If the locking stuff changes, we |
| 222 | * need to re-evaluate the acl locking stuff. |
| 223 | */ |
| 224 | int btrfs_init_acl(struct inode *inode, struct inode *dir) |
| 225 | { |
| 226 | struct posix_acl *acl = NULL; |
| 227 | int ret = 0; |
| 228 | |
| 229 | /* this happens with subvols */ |
| 230 | if (!dir) |
| 231 | return 0; |
| 232 | |
| 233 | if (!S_ISLNK(inode->i_mode)) { |
| 234 | if (IS_POSIXACL(dir)) { |
| 235 | acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT); |
| 236 | if (IS_ERR(acl)) |
| 237 | return PTR_ERR(acl); |
| 238 | } |
| 239 | |
| 240 | if (!acl) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 241 | inode->i_mode &= ~current_umask(); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if (IS_POSIXACL(dir) && acl) { |
| 245 | struct posix_acl *clone; |
| 246 | mode_t mode; |
| 247 | |
| 248 | if (S_ISDIR(inode->i_mode)) { |
| 249 | ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT); |
| 250 | if (ret) |
| 251 | goto failed; |
| 252 | } |
| 253 | clone = posix_acl_clone(acl, GFP_NOFS); |
| 254 | ret = -ENOMEM; |
| 255 | if (!clone) |
| 256 | goto failed; |
| 257 | |
| 258 | mode = inode->i_mode; |
| 259 | ret = posix_acl_create_masq(clone, &mode); |
| 260 | if (ret >= 0) { |
| 261 | inode->i_mode = mode; |
| 262 | if (ret > 0) { |
| 263 | /* we need an acl */ |
| 264 | ret = btrfs_set_acl(inode, clone, |
| 265 | ACL_TYPE_ACCESS); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | failed: |
| 270 | posix_acl_release(acl); |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | int btrfs_acl_chmod(struct inode *inode) |
| 276 | { |
| 277 | struct posix_acl *acl, *clone; |
| 278 | int ret = 0; |
| 279 | |
| 280 | if (S_ISLNK(inode->i_mode)) |
| 281 | return -EOPNOTSUPP; |
| 282 | |
| 283 | if (!IS_POSIXACL(inode)) |
| 284 | return 0; |
| 285 | |
| 286 | acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); |
| 287 | if (IS_ERR(acl) || !acl) |
| 288 | return PTR_ERR(acl); |
| 289 | |
| 290 | clone = posix_acl_clone(acl, GFP_KERNEL); |
| 291 | posix_acl_release(acl); |
| 292 | if (!clone) |
| 293 | return -ENOMEM; |
| 294 | |
| 295 | ret = posix_acl_chmod_masq(clone, inode->i_mode); |
| 296 | if (!ret) |
| 297 | ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS); |
| 298 | |
| 299 | posix_acl_release(clone); |
| 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 304 | struct xattr_handler btrfs_xattr_acl_default_handler = { |
| 305 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 306 | .get = btrfs_xattr_acl_default_get, |
| 307 | .set = btrfs_xattr_acl_default_set, |
| 308 | }; |
| 309 | |
| 310 | struct xattr_handler btrfs_xattr_acl_access_handler = { |
| 311 | .prefix = POSIX_ACL_XATTR_ACCESS, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 312 | .get = btrfs_xattr_acl_access_get, |
| 313 | .set = btrfs_xattr_acl_access_set, |
| 314 | }; |
Josef Bacik | eab922e | 2008-08-28 06:21:15 -0400 | [diff] [blame] | 315 | |
| 316 | #else /* CONFIG_FS_POSIX_ACL */ |
| 317 | |
| 318 | int btrfs_acl_chmod(struct inode *inode) |
| 319 | { |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | int btrfs_init_acl(struct inode *inode, struct inode *dir) |
| 324 | { |
| 325 | return 0; |
| 326 | } |
| 327 | |
Josef Bacik | eab922e | 2008-08-28 06:21:15 -0400 | [diff] [blame] | 328 | #endif /* CONFIG_FS_POSIX_ACL */ |