15103e947SJosef Bacik /* 25103e947SJosef Bacik * Copyright (C) 2007 Red Hat. All rights reserved. 35103e947SJosef Bacik * 45103e947SJosef Bacik * This program is free software; you can redistribute it and/or 55103e947SJosef Bacik * modify it under the terms of the GNU General Public 65103e947SJosef Bacik * License v2 as published by the Free Software Foundation. 75103e947SJosef Bacik * 85103e947SJosef Bacik * This program is distributed in the hope that it will be useful, 95103e947SJosef Bacik * but WITHOUT ANY WARRANTY; without even the implied warranty of 105103e947SJosef Bacik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 115103e947SJosef Bacik * General Public License for more details. 125103e947SJosef Bacik * 135103e947SJosef Bacik * You should have received a copy of the GNU General Public 145103e947SJosef Bacik * License along with this program; if not, write to the 155103e947SJosef Bacik * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 165103e947SJosef Bacik * Boston, MA 021110-1307, USA. 175103e947SJosef Bacik */ 185103e947SJosef Bacik 195103e947SJosef Bacik #include <linux/init.h> 205103e947SJosef Bacik #include <linux/fs.h> 215103e947SJosef Bacik #include <linux/slab.h> 225103e947SJosef Bacik #include <linux/rwsem.h> 235103e947SJosef Bacik #include <linux/xattr.h> 240279b4cdSJim Owens #include <linux/security.h> 255103e947SJosef Bacik #include "ctree.h" 265103e947SJosef Bacik #include "btrfs_inode.h" 275103e947SJosef Bacik #include "transaction.h" 285103e947SJosef Bacik #include "xattr.h" 295103e947SJosef Bacik #include "disk-io.h" 3033268eafSJosef Bacik 3133268eafSJosef Bacik 3295819c05SChristoph Hellwig ssize_t __btrfs_getxattr(struct inode *inode, const char *name, 3395819c05SChristoph Hellwig void *buffer, size_t size) 345103e947SJosef Bacik { 355103e947SJosef Bacik struct btrfs_dir_item *di; 365103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 375103e947SJosef Bacik struct btrfs_path *path; 385103e947SJosef Bacik struct extent_buffer *leaf; 395103e947SJosef Bacik int ret = 0; 405103e947SJosef Bacik unsigned long data_ptr; 415103e947SJosef Bacik 425103e947SJosef Bacik path = btrfs_alloc_path(); 4395819c05SChristoph Hellwig if (!path) 445103e947SJosef Bacik return -ENOMEM; 455103e947SJosef Bacik 465103e947SJosef Bacik /* lookup the xattr by name */ 4733345d01SLi Zefan di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name, 485103e947SJosef Bacik strlen(name), 0); 4907060404SJosef Bacik if (!di) { 505103e947SJosef Bacik ret = -ENODATA; 515103e947SJosef Bacik goto out; 5207060404SJosef Bacik } else if (IS_ERR(di)) { 5307060404SJosef Bacik ret = PTR_ERR(di); 5407060404SJosef Bacik goto out; 555103e947SJosef Bacik } 565103e947SJosef Bacik 575103e947SJosef Bacik leaf = path->nodes[0]; 585103e947SJosef Bacik /* if size is 0, that means we want the size of the attr */ 595103e947SJosef Bacik if (!size) { 605103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 615103e947SJosef Bacik goto out; 625103e947SJosef Bacik } 635103e947SJosef Bacik 645103e947SJosef Bacik /* now get the data out of our dir_item */ 655103e947SJosef Bacik if (btrfs_dir_data_len(leaf, di) > size) { 665103e947SJosef Bacik ret = -ERANGE; 675103e947SJosef Bacik goto out; 685103e947SJosef Bacik } 6907060404SJosef Bacik 7007060404SJosef Bacik /* 7107060404SJosef Bacik * The way things are packed into the leaf is like this 7207060404SJosef Bacik * |struct btrfs_dir_item|name|data| 7307060404SJosef Bacik * where name is the xattr name, so security.foo, and data is the 7407060404SJosef Bacik * content of the xattr. data_ptr points to the location in memory 7507060404SJosef Bacik * where the data starts in the in memory leaf 7607060404SJosef Bacik */ 775103e947SJosef Bacik data_ptr = (unsigned long)((char *)(di + 1) + 785103e947SJosef Bacik btrfs_dir_name_len(leaf, di)); 795103e947SJosef Bacik read_extent_buffer(leaf, buffer, data_ptr, 803acd7ee8SJosef Bacik btrfs_dir_data_len(leaf, di)); 815103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 825103e947SJosef Bacik 835103e947SJosef Bacik out: 845103e947SJosef Bacik btrfs_free_path(path); 855103e947SJosef Bacik return ret; 865103e947SJosef Bacik } 875103e947SJosef Bacik 88f34f57a3SYan, Zheng static int do_setxattr(struct btrfs_trans_handle *trans, 89f34f57a3SYan, Zheng struct inode *inode, const char *name, 9095819c05SChristoph Hellwig const void *value, size_t size, int flags) 915103e947SJosef Bacik { 925103e947SJosef Bacik struct btrfs_dir_item *di; 935103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 945103e947SJosef Bacik struct btrfs_path *path; 95f34f57a3SYan, Zheng size_t name_len = strlen(name); 96f34f57a3SYan, Zheng int ret = 0; 97f34f57a3SYan, Zheng 98f34f57a3SYan, Zheng if (name_len + size > BTRFS_MAX_XATTR_SIZE(root)) 99f34f57a3SYan, Zheng return -ENOSPC; 1005103e947SJosef Bacik 1015103e947SJosef Bacik path = btrfs_alloc_path(); 10295819c05SChristoph Hellwig if (!path) 1035103e947SJosef Bacik return -ENOMEM; 1045103e947SJosef Bacik 1055103e947SJosef Bacik /* first lets see if we already have this xattr */ 10633345d01SLi Zefan di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name, 1075103e947SJosef Bacik strlen(name), -1); 1085103e947SJosef Bacik if (IS_ERR(di)) { 1095103e947SJosef Bacik ret = PTR_ERR(di); 1105103e947SJosef Bacik goto out; 1115103e947SJosef Bacik } 1125103e947SJosef Bacik 1135103e947SJosef Bacik /* ok we already have this xattr, lets remove it */ 1145103e947SJosef Bacik if (di) { 1155103e947SJosef Bacik /* if we want create only exit */ 1165103e947SJosef Bacik if (flags & XATTR_CREATE) { 1175103e947SJosef Bacik ret = -EEXIST; 1185103e947SJosef Bacik goto out; 1195103e947SJosef Bacik } 1205103e947SJosef Bacik 1215103e947SJosef Bacik ret = btrfs_delete_one_dir_name(trans, root, path, di); 122f34f57a3SYan, Zheng BUG_ON(ret); 123b3b4aa74SDavid Sterba btrfs_release_path(path); 1245103e947SJosef Bacik 1255103e947SJosef Bacik /* if we don't have a value then we are removing the xattr */ 126f34f57a3SYan, Zheng if (!value) 1275103e947SJosef Bacik goto out; 12833268eafSJosef Bacik } else { 129b3b4aa74SDavid Sterba btrfs_release_path(path); 13033268eafSJosef Bacik 13133268eafSJosef Bacik if (flags & XATTR_REPLACE) { 13233268eafSJosef Bacik /* we couldn't find the attr to replace */ 1335103e947SJosef Bacik ret = -ENODATA; 1345103e947SJosef Bacik goto out; 1355103e947SJosef Bacik } 13633268eafSJosef Bacik } 1375103e947SJosef Bacik 1385103e947SJosef Bacik /* ok we have to create a completely new xattr */ 13933345d01SLi Zefan ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode), 140f34f57a3SYan, Zheng name, name_len, value, size); 141f34f57a3SYan, Zheng BUG_ON(ret); 1425103e947SJosef Bacik out: 143f34f57a3SYan, Zheng btrfs_free_path(path); 144f34f57a3SYan, Zheng return ret; 1455103e947SJosef Bacik } 1465103e947SJosef Bacik 147f34f57a3SYan, Zheng int __btrfs_setxattr(struct btrfs_trans_handle *trans, 148f34f57a3SYan, Zheng struct inode *inode, const char *name, 149f34f57a3SYan, Zheng const void *value, size_t size, int flags) 150f34f57a3SYan, Zheng { 151f34f57a3SYan, Zheng struct btrfs_root *root = BTRFS_I(inode)->root; 152f34f57a3SYan, Zheng int ret; 153f34f57a3SYan, Zheng 154f34f57a3SYan, Zheng if (trans) 155f34f57a3SYan, Zheng return do_setxattr(trans, inode, name, value, size, flags); 156f34f57a3SYan, Zheng 157a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 2); 158a22285a6SYan, Zheng if (IS_ERR(trans)) 159a22285a6SYan, Zheng return PTR_ERR(trans); 160f34f57a3SYan, Zheng 161f34f57a3SYan, Zheng ret = do_setxattr(trans, inode, name, value, size, flags); 162f34f57a3SYan, Zheng if (ret) 163f34f57a3SYan, Zheng goto out; 164f34f57a3SYan, Zheng 165f34f57a3SYan, Zheng inode->i_ctime = CURRENT_TIME; 166f34f57a3SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 167f34f57a3SYan, Zheng BUG_ON(ret); 168f34f57a3SYan, Zheng out: 169f34f57a3SYan, Zheng btrfs_end_transaction_throttle(trans, root); 1705103e947SJosef Bacik return ret; 1715103e947SJosef Bacik } 1725103e947SJosef Bacik 1735103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 1745103e947SJosef Bacik { 1755103e947SJosef Bacik struct btrfs_key key, found_key; 1765103e947SJosef Bacik struct inode *inode = dentry->d_inode; 1775103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 1785103e947SJosef Bacik struct btrfs_path *path; 1795103e947SJosef Bacik struct extent_buffer *leaf; 1805103e947SJosef Bacik struct btrfs_dir_item *di; 1812e6a0035SLi Zefan int ret = 0, slot; 182eaa47d86SChristoph Hellwig size_t total_size = 0, size_left = size; 1835103e947SJosef Bacik unsigned long name_ptr; 184eaa47d86SChristoph Hellwig size_t name_len; 1855103e947SJosef Bacik 1865103e947SJosef Bacik /* 1875103e947SJosef Bacik * ok we want all objects associated with this id. 1885103e947SJosef Bacik * NOTE: we set key.offset = 0; because we want to start with the 1895103e947SJosef Bacik * first xattr that we find and walk forward 1905103e947SJosef Bacik */ 19133345d01SLi Zefan key.objectid = btrfs_ino(inode); 1925103e947SJosef Bacik btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); 1935103e947SJosef Bacik key.offset = 0; 1945103e947SJosef Bacik 1955103e947SJosef Bacik path = btrfs_alloc_path(); 1965103e947SJosef Bacik if (!path) 1975103e947SJosef Bacik return -ENOMEM; 1981caf9342SJosef Bacik path->reada = 2; 1995103e947SJosef Bacik 2005103e947SJosef Bacik /* search for our xattrs */ 2015103e947SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2025103e947SJosef Bacik if (ret < 0) 2035103e947SJosef Bacik goto err; 2042e6a0035SLi Zefan 2055103e947SJosef Bacik while (1) { 2065103e947SJosef Bacik leaf = path->nodes[0]; 2075103e947SJosef Bacik slot = path->slots[0]; 2085103e947SJosef Bacik 2095103e947SJosef Bacik /* this is where we start walking through the path */ 2102e6a0035SLi Zefan if (slot >= btrfs_header_nritems(leaf)) { 2115103e947SJosef Bacik /* 2125103e947SJosef Bacik * if we've reached the last slot in this leaf we need 2135103e947SJosef Bacik * to go to the next leaf and reset everything 2145103e947SJosef Bacik */ 2155103e947SJosef Bacik ret = btrfs_next_leaf(root, path); 2162e6a0035SLi Zefan if (ret < 0) 2172e6a0035SLi Zefan goto err; 2182e6a0035SLi Zefan else if (ret > 0) 2195103e947SJosef Bacik break; 2202e6a0035SLi Zefan continue; 2215103e947SJosef Bacik } 2225103e947SJosef Bacik 2235103e947SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 2245103e947SJosef Bacik 2255103e947SJosef Bacik /* check to make sure this item is what we want */ 2265103e947SJosef Bacik if (found_key.objectid != key.objectid) 2275103e947SJosef Bacik break; 2285103e947SJosef Bacik if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY) 2295103e947SJosef Bacik break; 2305103e947SJosef Bacik 2315103e947SJosef Bacik di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 23222a94d44SJosef Bacik if (verify_dir_item(root, leaf, di)) 23322a94d44SJosef Bacik continue; 2345103e947SJosef Bacik 235eaa47d86SChristoph Hellwig name_len = btrfs_dir_name_len(leaf, di); 236eaa47d86SChristoph Hellwig total_size += name_len + 1; 2375103e947SJosef Bacik 2385103e947SJosef Bacik /* we are just looking for how big our buffer needs to be */ 2395103e947SJosef Bacik if (!size) 2402e6a0035SLi Zefan goto next; 2415103e947SJosef Bacik 242eaa47d86SChristoph Hellwig if (!buffer || (name_len + 1) > size_left) { 2435103e947SJosef Bacik ret = -ERANGE; 244b16281c3SYehuda Sadeh Weinraub goto err; 2455103e947SJosef Bacik } 2465103e947SJosef Bacik 247eaa47d86SChristoph Hellwig name_ptr = (unsigned long)(di + 1); 248eaa47d86SChristoph Hellwig read_extent_buffer(leaf, buffer, name_ptr, name_len); 249eaa47d86SChristoph Hellwig buffer[name_len] = '\0'; 250eaa47d86SChristoph Hellwig 251eaa47d86SChristoph Hellwig size_left -= name_len + 1; 252eaa47d86SChristoph Hellwig buffer += name_len + 1; 2532e6a0035SLi Zefan next: 2542e6a0035SLi Zefan path->slots[0]++; 2555103e947SJosef Bacik } 2565103e947SJosef Bacik ret = total_size; 2575103e947SJosef Bacik 2585103e947SJosef Bacik err: 2595103e947SJosef Bacik btrfs_free_path(path); 2605103e947SJosef Bacik 2615103e947SJosef Bacik return ret; 2625103e947SJosef Bacik } 2635103e947SJosef Bacik 2645103e947SJosef Bacik /* 26595819c05SChristoph Hellwig * List of handlers for synthetic system.* attributes. All real ondisk 26695819c05SChristoph Hellwig * attributes are handled directly. 2675103e947SJosef Bacik */ 268f01cbd3fSStephen Hemminger const struct xattr_handler *btrfs_xattr_handlers[] = { 2690eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 27095819c05SChristoph Hellwig &btrfs_xattr_acl_access_handler, 27195819c05SChristoph Hellwig &btrfs_xattr_acl_default_handler, 27295819c05SChristoph Hellwig #endif 27395819c05SChristoph Hellwig NULL, 27495819c05SChristoph Hellwig }; 27595819c05SChristoph Hellwig 27695819c05SChristoph Hellwig /* 27795819c05SChristoph Hellwig * Check if the attribute is in a supported namespace. 27895819c05SChristoph Hellwig * 27995819c05SChristoph Hellwig * This applied after the check for the synthetic attributes in the system 28095819c05SChristoph Hellwig * namespace. 28195819c05SChristoph Hellwig */ 28295819c05SChristoph Hellwig static bool btrfs_is_valid_xattr(const char *name) 28395819c05SChristoph Hellwig { 284d397712bSChris Mason return !strncmp(name, XATTR_SECURITY_PREFIX, 285d397712bSChris Mason XATTR_SECURITY_PREFIX_LEN) || 28695819c05SChristoph Hellwig !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) || 28795819c05SChristoph Hellwig !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || 28895819c05SChristoph Hellwig !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); 28969a32ac5SChris Mason } 29069a32ac5SChris Mason 29195819c05SChristoph Hellwig ssize_t btrfs_getxattr(struct dentry *dentry, const char *name, 29295819c05SChristoph Hellwig void *buffer, size_t size) 29395819c05SChristoph Hellwig { 29495819c05SChristoph Hellwig /* 29595819c05SChristoph Hellwig * If this is a request for a synthetic attribute in the system.* 29695819c05SChristoph Hellwig * namespace use the generic infrastructure to resolve a handler 29795819c05SChristoph Hellwig * for it via sb->s_xattr. 29895819c05SChristoph Hellwig */ 29995819c05SChristoph Hellwig if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 30095819c05SChristoph Hellwig return generic_getxattr(dentry, name, buffer, size); 3015103e947SJosef Bacik 30295819c05SChristoph Hellwig if (!btrfs_is_valid_xattr(name)) 30395819c05SChristoph Hellwig return -EOPNOTSUPP; 30495819c05SChristoph Hellwig return __btrfs_getxattr(dentry->d_inode, name, buffer, size); 30595819c05SChristoph Hellwig } 3065103e947SJosef Bacik 30795819c05SChristoph Hellwig int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value, 30895819c05SChristoph Hellwig size_t size, int flags) 30995819c05SChristoph Hellwig { 310b83cc969SLi Zefan struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root; 311b83cc969SLi Zefan 312b83cc969SLi Zefan /* 313b83cc969SLi Zefan * The permission on security.* and system.* is not checked 314b83cc969SLi Zefan * in permission(). 315b83cc969SLi Zefan */ 316b83cc969SLi Zefan if (btrfs_root_readonly(root)) 317b83cc969SLi Zefan return -EROFS; 318b83cc969SLi Zefan 31995819c05SChristoph Hellwig /* 32095819c05SChristoph Hellwig * If this is a request for a synthetic attribute in the system.* 32195819c05SChristoph Hellwig * namespace use the generic infrastructure to resolve a handler 32295819c05SChristoph Hellwig * for it via sb->s_xattr. 32395819c05SChristoph Hellwig */ 32495819c05SChristoph Hellwig if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 32595819c05SChristoph Hellwig return generic_setxattr(dentry, name, value, size, flags); 3265103e947SJosef Bacik 32795819c05SChristoph Hellwig if (!btrfs_is_valid_xattr(name)) 32895819c05SChristoph Hellwig return -EOPNOTSUPP; 3295103e947SJosef Bacik 33095819c05SChristoph Hellwig if (size == 0) 33195819c05SChristoph Hellwig value = ""; /* empty EA, do not remove */ 332f34f57a3SYan, Zheng 333f34f57a3SYan, Zheng return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size, 334f34f57a3SYan, Zheng flags); 33595819c05SChristoph Hellwig } 33695819c05SChristoph Hellwig 33795819c05SChristoph Hellwig int btrfs_removexattr(struct dentry *dentry, const char *name) 33895819c05SChristoph Hellwig { 339b83cc969SLi Zefan struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root; 340b83cc969SLi Zefan 341b83cc969SLi Zefan /* 342b83cc969SLi Zefan * The permission on security.* and system.* is not checked 343b83cc969SLi Zefan * in permission(). 344b83cc969SLi Zefan */ 345b83cc969SLi Zefan if (btrfs_root_readonly(root)) 346b83cc969SLi Zefan return -EROFS; 347b83cc969SLi Zefan 34895819c05SChristoph Hellwig /* 34995819c05SChristoph Hellwig * If this is a request for a synthetic attribute in the system.* 35095819c05SChristoph Hellwig * namespace use the generic infrastructure to resolve a handler 35195819c05SChristoph Hellwig * for it via sb->s_xattr. 35295819c05SChristoph Hellwig */ 35395819c05SChristoph Hellwig if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 35495819c05SChristoph Hellwig return generic_removexattr(dentry, name); 35595819c05SChristoph Hellwig 35695819c05SChristoph Hellwig if (!btrfs_is_valid_xattr(name)) 35795819c05SChristoph Hellwig return -EOPNOTSUPP; 358f34f57a3SYan, Zheng 359f34f57a3SYan, Zheng return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0, 360f34f57a3SYan, Zheng XATTR_REPLACE); 36195819c05SChristoph Hellwig } 3620279b4cdSJim Owens 3639d8f13baSMimi Zohar int btrfs_initxattrs(struct inode *inode, const struct xattr *xattr_array, 3649d8f13baSMimi Zohar void *fs_info) 3659d8f13baSMimi Zohar { 3669d8f13baSMimi Zohar const struct xattr *xattr; 3679d8f13baSMimi Zohar struct btrfs_trans_handle *trans = fs_info; 3689d8f13baSMimi Zohar char *name; 3699d8f13baSMimi Zohar int err = 0; 3709d8f13baSMimi Zohar 3719d8f13baSMimi Zohar for (xattr = xattr_array; xattr->name != NULL; xattr++) { 3729d8f13baSMimi Zohar name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 3739d8f13baSMimi Zohar strlen(xattr->name) + 1, GFP_NOFS); 3749d8f13baSMimi Zohar if (!name) { 3759d8f13baSMimi Zohar err = -ENOMEM; 3769d8f13baSMimi Zohar break; 3779d8f13baSMimi Zohar } 3789d8f13baSMimi Zohar strcpy(name, XATTR_SECURITY_PREFIX); 3799d8f13baSMimi Zohar strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); 3809d8f13baSMimi Zohar err = __btrfs_setxattr(trans, inode, name, 3819d8f13baSMimi Zohar xattr->value, xattr->value_len, 0); 3829d8f13baSMimi Zohar kfree(name); 3839d8f13baSMimi Zohar if (err < 0) 3849d8f13baSMimi Zohar break; 3859d8f13baSMimi Zohar } 3869d8f13baSMimi Zohar return err; 3879d8f13baSMimi Zohar } 3889d8f13baSMimi Zohar 389f34f57a3SYan, Zheng int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, 3902a7dba39SEric Paris struct inode *inode, struct inode *dir, 3912a7dba39SEric Paris const struct qstr *qstr) 3920279b4cdSJim Owens { 3939d8f13baSMimi Zohar return security_inode_init_security(inode, dir, qstr, 3949d8f13baSMimi Zohar &btrfs_initxattrs, trans); 3950279b4cdSJim Owens } 396