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