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> 23#include <linux/posix_acl.h> 24#include <linux/sched.h> 25#include <linux/slab.h> 26 27#include "ctree.h" 28#include "btrfs_inode.h" 29#include "xattr.h" 30 31#ifdef CONFIG_BTRFS_FS_POSIX_ACL 32 33static struct posix_acl *btrfs_get_acl(struct inode *inode, int type) 34{ 35 int size; 36 const char *name; 37 char *value = NULL; 38 struct posix_acl *acl; 39 40 acl = get_cached_acl(inode, type); 41 if (acl != ACL_NOT_CACHED) 42 return acl; 43 44 switch (type) { 45 case ACL_TYPE_ACCESS: 46 name = POSIX_ACL_XATTR_ACCESS; 47 break; 48 case ACL_TYPE_DEFAULT: 49 name = POSIX_ACL_XATTR_DEFAULT; 50 break; 51 default: 52 BUG(); 53 } 54 55 size = __btrfs_getxattr(inode, name, "", 0); 56 if (size > 0) { 57 value = kzalloc(size, GFP_NOFS); 58 if (!value) 59 return ERR_PTR(-ENOMEM); 60 size = __btrfs_getxattr(inode, name, value, size); 61 if (size > 0) { 62 acl = posix_acl_from_xattr(value, size); 63 if (IS_ERR(acl)) 64 return acl; 65 set_cached_acl(inode, type, acl); 66 } 67 kfree(value); 68 } else if (size == -ENOENT || size == -ENODATA || size == 0) { 69 acl = NULL; 70 set_cached_acl(inode, type, acl); 71 } else { 72 acl = ERR_PTR(-EIO); 73 } 74 75 return acl; 76} 77 78static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name, 79 void *value, size_t size, int type) 80{ 81 struct posix_acl *acl; 82 int ret = 0; 83 84 acl = btrfs_get_acl(dentry->d_inode, type); 85 86 if (IS_ERR(acl)) 87 return PTR_ERR(acl); 88 if (acl == NULL) 89 return -ENODATA; 90 ret = posix_acl_to_xattr(acl, value, size); 91 posix_acl_release(acl); 92 93 return ret; 94} 95 96/* 97 * Needs to be called with fs_mutex held 98 */ 99static int btrfs_set_acl(struct btrfs_trans_handle *trans, 100 struct inode *inode, struct posix_acl *acl, int type) 101{ 102 int ret, size = 0; 103 const char *name; 104 char *value = NULL; 105 mode_t mode; 106 107 if (acl) { 108 ret = posix_acl_valid(acl); 109 if (ret < 0) 110 return ret; 111 ret = 0; 112 } 113 114 switch (type) { 115 case ACL_TYPE_ACCESS: 116 mode = inode->i_mode; 117 name = POSIX_ACL_XATTR_ACCESS; 118 if (acl) { 119 ret = posix_acl_equiv_mode(acl, &mode); 120 if (ret < 0) 121 return ret; 122 inode->i_mode = mode; 123 } 124 ret = 0; 125 break; 126 case ACL_TYPE_DEFAULT: 127 if (!S_ISDIR(inode->i_mode)) 128 return acl ? -EINVAL : 0; 129 name = POSIX_ACL_XATTR_DEFAULT; 130 break; 131 default: 132 return -EINVAL; 133 } 134 135 if (acl) { 136 size = posix_acl_xattr_size(acl->a_count); 137 value = kmalloc(size, GFP_NOFS); 138 if (!value) { 139 ret = -ENOMEM; 140 goto out; 141 } 142 143 ret = posix_acl_to_xattr(acl, value, size); 144 if (ret < 0) 145 goto out; 146 } 147 148 ret = __btrfs_setxattr(trans, inode, name, value, size, 0); 149out: 150 kfree(value); 151 152 if (!ret) 153 set_cached_acl(inode, type, acl); 154 155 return ret; 156} 157 158static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name, 159 const void *value, size_t size, int flags, int type) 160{ 161 int ret; 162 struct posix_acl *acl = NULL; 163 164 if (!is_owner_or_cap(dentry->d_inode)) 165 return -EPERM; 166 167 if (!IS_POSIXACL(dentry->d_inode)) 168 return -EOPNOTSUPP; 169 170 if (value) { 171 acl = posix_acl_from_xattr(value, size); 172 if (acl == NULL) { 173 value = NULL; 174 size = 0; 175 } else if (IS_ERR(acl)) { 176 return PTR_ERR(acl); 177 } 178 } 179 180 ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type); 181 182 posix_acl_release(acl); 183 184 return ret; 185} 186 187int btrfs_check_acl(struct inode *inode, int mask) 188{ 189 struct posix_acl *acl; 190 int error = -EAGAIN; 191 192 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); 193 194 if (IS_ERR(acl)) 195 return PTR_ERR(acl); 196 if (acl) { 197 error = posix_acl_permission(inode, acl, mask); 198 posix_acl_release(acl); 199 } 200 201 return error; 202} 203 204/* 205 * btrfs_init_acl is already generally called under fs_mutex, so the locking 206 * stuff has been fixed to work with that. If the locking stuff changes, we 207 * need to re-evaluate the acl locking stuff. 208 */ 209int btrfs_init_acl(struct btrfs_trans_handle *trans, 210 struct inode *inode, struct inode *dir) 211{ 212 struct posix_acl *acl = NULL; 213 int ret = 0; 214 215 /* this happens with subvols */ 216 if (!dir) 217 return 0; 218 219 if (!S_ISLNK(inode->i_mode)) { 220 if (IS_POSIXACL(dir)) { 221 acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT); 222 if (IS_ERR(acl)) 223 return PTR_ERR(acl); 224 } 225 226 if (!acl) 227 inode->i_mode &= ~current_umask(); 228 } 229 230 if (IS_POSIXACL(dir) && acl) { 231 struct posix_acl *clone; 232 mode_t mode; 233 234 if (S_ISDIR(inode->i_mode)) { 235 ret = btrfs_set_acl(trans, inode, acl, 236 ACL_TYPE_DEFAULT); 237 if (ret) 238 goto failed; 239 } 240 clone = posix_acl_clone(acl, GFP_NOFS); 241 ret = -ENOMEM; 242 if (!clone) 243 goto failed; 244 245 mode = inode->i_mode; 246 ret = posix_acl_create_masq(clone, &mode); 247 if (ret >= 0) { 248 inode->i_mode = mode; 249 if (ret > 0) { 250 /* we need an acl */ 251 ret = btrfs_set_acl(trans, inode, clone, 252 ACL_TYPE_ACCESS); 253 } 254 } 255 posix_acl_release(clone); 256 } 257failed: 258 posix_acl_release(acl); 259 260 return ret; 261} 262 263int btrfs_acl_chmod(struct inode *inode) 264{ 265 struct posix_acl *acl, *clone; 266 int ret = 0; 267 268 if (S_ISLNK(inode->i_mode)) 269 return -EOPNOTSUPP; 270 271 if (!IS_POSIXACL(inode)) 272 return 0; 273 274 acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS); 275 if (IS_ERR(acl) || !acl) 276 return PTR_ERR(acl); 277 278 clone = posix_acl_clone(acl, GFP_KERNEL); 279 posix_acl_release(acl); 280 if (!clone) 281 return -ENOMEM; 282 283 ret = posix_acl_chmod_masq(clone, inode->i_mode); 284 if (!ret) 285 ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS); 286 287 posix_acl_release(clone); 288 289 return ret; 290} 291 292const struct xattr_handler btrfs_xattr_acl_default_handler = { 293 .prefix = POSIX_ACL_XATTR_DEFAULT, 294 .flags = ACL_TYPE_DEFAULT, 295 .get = btrfs_xattr_acl_get, 296 .set = btrfs_xattr_acl_set, 297}; 298 299const struct xattr_handler btrfs_xattr_acl_access_handler = { 300 .prefix = POSIX_ACL_XATTR_ACCESS, 301 .flags = ACL_TYPE_ACCESS, 302 .get = btrfs_xattr_acl_get, 303 .set = btrfs_xattr_acl_set, 304}; 305 306#else /* CONFIG_BTRFS_FS_POSIX_ACL */ 307 308int btrfs_acl_chmod(struct inode *inode) 309{ 310 return 0; 311} 312 313int btrfs_init_acl(struct btrfs_trans_handle *trans, 314 struct inode *inode, struct inode *dir) 315{ 316 return 0; 317} 318 319#endif /* CONFIG_BTRFS_FS_POSIX_ACL */ 320