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> 25996a710dSChristoph Hellwig #include <linux/posix_acl_xattr.h> 265103e947SJosef Bacik #include "ctree.h" 275103e947SJosef Bacik #include "btrfs_inode.h" 285103e947SJosef Bacik #include "transaction.h" 295103e947SJosef Bacik #include "xattr.h" 305103e947SJosef Bacik #include "disk-io.h" 3163541927SFilipe David Borba Manana #include "props.h" 325f5bc6b1SFilipe Manana #include "locking.h" 3333268eafSJosef Bacik 3433268eafSJosef Bacik 3595819c05SChristoph Hellwig ssize_t __btrfs_getxattr(struct inode *inode, const char *name, 3695819c05SChristoph Hellwig void *buffer, size_t size) 375103e947SJosef Bacik { 385103e947SJosef Bacik struct btrfs_dir_item *di; 395103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 405103e947SJosef Bacik struct btrfs_path *path; 415103e947SJosef Bacik struct extent_buffer *leaf; 425103e947SJosef Bacik int ret = 0; 435103e947SJosef Bacik unsigned long data_ptr; 445103e947SJosef Bacik 455103e947SJosef Bacik path = btrfs_alloc_path(); 4695819c05SChristoph Hellwig if (!path) 475103e947SJosef Bacik return -ENOMEM; 485103e947SJosef Bacik 495103e947SJosef Bacik /* lookup the xattr by name */ 5033345d01SLi Zefan di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name, 515103e947SJosef Bacik strlen(name), 0); 5207060404SJosef Bacik if (!di) { 535103e947SJosef Bacik ret = -ENODATA; 545103e947SJosef Bacik goto out; 5507060404SJosef Bacik } else if (IS_ERR(di)) { 5607060404SJosef Bacik ret = PTR_ERR(di); 5707060404SJosef Bacik goto out; 585103e947SJosef Bacik } 595103e947SJosef Bacik 605103e947SJosef Bacik leaf = path->nodes[0]; 615103e947SJosef Bacik /* if size is 0, that means we want the size of the attr */ 625103e947SJosef Bacik if (!size) { 635103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 645103e947SJosef Bacik goto out; 655103e947SJosef Bacik } 665103e947SJosef Bacik 675103e947SJosef Bacik /* now get the data out of our dir_item */ 685103e947SJosef Bacik if (btrfs_dir_data_len(leaf, di) > size) { 695103e947SJosef Bacik ret = -ERANGE; 705103e947SJosef Bacik goto out; 715103e947SJosef Bacik } 7207060404SJosef Bacik 7307060404SJosef Bacik /* 7407060404SJosef Bacik * The way things are packed into the leaf is like this 7507060404SJosef Bacik * |struct btrfs_dir_item|name|data| 7607060404SJosef Bacik * where name is the xattr name, so security.foo, and data is the 7707060404SJosef Bacik * content of the xattr. data_ptr points to the location in memory 7807060404SJosef Bacik * where the data starts in the in memory leaf 7907060404SJosef Bacik */ 805103e947SJosef Bacik data_ptr = (unsigned long)((char *)(di + 1) + 815103e947SJosef Bacik btrfs_dir_name_len(leaf, di)); 825103e947SJosef Bacik read_extent_buffer(leaf, buffer, data_ptr, 833acd7ee8SJosef Bacik btrfs_dir_data_len(leaf, di)); 845103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 855103e947SJosef Bacik 865103e947SJosef Bacik out: 875103e947SJosef Bacik btrfs_free_path(path); 885103e947SJosef Bacik return ret; 895103e947SJosef Bacik } 905103e947SJosef Bacik 91f34f57a3SYan, Zheng static int do_setxattr(struct btrfs_trans_handle *trans, 92f34f57a3SYan, Zheng struct inode *inode, const char *name, 9395819c05SChristoph Hellwig const void *value, size_t size, int flags) 945103e947SJosef Bacik { 955f5bc6b1SFilipe Manana struct btrfs_dir_item *di = NULL; 965103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 975103e947SJosef Bacik struct btrfs_path *path; 98f34f57a3SYan, Zheng size_t name_len = strlen(name); 99f34f57a3SYan, Zheng int ret = 0; 100f34f57a3SYan, Zheng 101f34f57a3SYan, Zheng if (name_len + size > BTRFS_MAX_XATTR_SIZE(root)) 102f34f57a3SYan, Zheng return -ENOSPC; 1035103e947SJosef Bacik 1045103e947SJosef Bacik path = btrfs_alloc_path(); 10595819c05SChristoph Hellwig if (!path) 1065103e947SJosef Bacik return -ENOMEM; 1075f5bc6b1SFilipe Manana path->skip_release_on_error = 1; 1085103e947SJosef Bacik 1095f5bc6b1SFilipe Manana if (!value) { 1105f5bc6b1SFilipe Manana di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), 1115f5bc6b1SFilipe Manana name, name_len, -1); 1125f5bc6b1SFilipe Manana if (!di && (flags & XATTR_REPLACE)) 1135f5bc6b1SFilipe Manana ret = -ENODATA; 1145cdf83edSFilipe Manana else if (IS_ERR(di)) 1155cdf83edSFilipe Manana ret = PTR_ERR(di); 1165f5bc6b1SFilipe Manana else if (di) 1175f5bc6b1SFilipe Manana ret = btrfs_delete_one_dir_name(trans, root, path, di); 1185103e947SJosef Bacik goto out; 1195f5bc6b1SFilipe Manana } 1205f5bc6b1SFilipe Manana 1215f5bc6b1SFilipe Manana /* 1225f5bc6b1SFilipe Manana * For a replace we can't just do the insert blindly. 1235f5bc6b1SFilipe Manana * Do a lookup first (read-only btrfs_search_slot), and return if xattr 1245f5bc6b1SFilipe Manana * doesn't exist. If it exists, fall down below to the insert/replace 1255f5bc6b1SFilipe Manana * path - we can't race with a concurrent xattr delete, because the VFS 1265f5bc6b1SFilipe Manana * locks the inode's i_mutex before calling setxattr or removexattr. 1275f5bc6b1SFilipe Manana */ 1285f5bc6b1SFilipe Manana if (flags & XATTR_REPLACE) { 1295f5bc6b1SFilipe Manana ASSERT(mutex_is_locked(&inode->i_mutex)); 1305f5bc6b1SFilipe Manana di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), 1315f5bc6b1SFilipe Manana name, name_len, 0); 1325cdf83edSFilipe Manana if (!di) 1335103e947SJosef Bacik ret = -ENODATA; 1345cdf83edSFilipe Manana else if (IS_ERR(di)) 1355cdf83edSFilipe Manana ret = PTR_ERR(di); 1365cdf83edSFilipe Manana if (ret) 1375103e947SJosef Bacik goto out; 138fa09200bSJosef Bacik btrfs_release_path(path); 1395f5bc6b1SFilipe Manana di = NULL; 14033268eafSJosef Bacik } 1415103e947SJosef Bacik 14233345d01SLi Zefan ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode), 143f34f57a3SYan, Zheng name, name_len, value, size); 1445f5bc6b1SFilipe Manana if (ret == -EOVERFLOW) { 145ed3ee9f4SJosef Bacik /* 1465f5bc6b1SFilipe Manana * We have an existing item in a leaf, split_leaf couldn't 1475f5bc6b1SFilipe Manana * expand it. That item might have or not a dir_item that 1485f5bc6b1SFilipe Manana * matches our target xattr, so lets check. 149ed3ee9f4SJosef Bacik */ 1505f5bc6b1SFilipe Manana ret = 0; 1515f5bc6b1SFilipe Manana btrfs_assert_tree_locked(path->nodes[0]); 1525f5bc6b1SFilipe Manana di = btrfs_match_dir_item_name(root, path, name, name_len); 1535f5bc6b1SFilipe Manana if (!di && !(flags & XATTR_REPLACE)) { 1545f5bc6b1SFilipe Manana ret = -ENOSPC; 155fa09200bSJosef Bacik goto out; 1565f5bc6b1SFilipe Manana } 1575f5bc6b1SFilipe Manana } else if (ret == -EEXIST) { 1585f5bc6b1SFilipe Manana ret = 0; 1595f5bc6b1SFilipe Manana di = btrfs_match_dir_item_name(root, path, name, name_len); 1605f5bc6b1SFilipe Manana ASSERT(di); /* logic error */ 1615f5bc6b1SFilipe Manana } else if (ret) { 162fa09200bSJosef Bacik goto out; 163fa09200bSJosef Bacik } 164fa09200bSJosef Bacik 1655f5bc6b1SFilipe Manana if (di && (flags & XATTR_CREATE)) { 1665f5bc6b1SFilipe Manana ret = -EEXIST; 1675f5bc6b1SFilipe Manana goto out; 1685f5bc6b1SFilipe Manana } 1695f5bc6b1SFilipe Manana 1705f5bc6b1SFilipe Manana if (di) { 1715f5bc6b1SFilipe Manana /* 1725f5bc6b1SFilipe Manana * We're doing a replace, and it must be atomic, that is, at 1735f5bc6b1SFilipe Manana * any point in time we have either the old or the new xattr 1745f5bc6b1SFilipe Manana * value in the tree. We don't want readers (getxattr and 1755f5bc6b1SFilipe Manana * listxattrs) to miss a value, this is specially important 1765f5bc6b1SFilipe Manana * for ACLs. 1775f5bc6b1SFilipe Manana */ 1785f5bc6b1SFilipe Manana const int slot = path->slots[0]; 1795f5bc6b1SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 1805f5bc6b1SFilipe Manana const u16 old_data_len = btrfs_dir_data_len(leaf, di); 1815f5bc6b1SFilipe Manana const u32 item_size = btrfs_item_size_nr(leaf, slot); 1825f5bc6b1SFilipe Manana const u32 data_size = sizeof(*di) + name_len + size; 1835f5bc6b1SFilipe Manana struct btrfs_item *item; 1845f5bc6b1SFilipe Manana unsigned long data_ptr; 1855f5bc6b1SFilipe Manana char *ptr; 1865f5bc6b1SFilipe Manana 1875f5bc6b1SFilipe Manana if (size > old_data_len) { 1885f5bc6b1SFilipe Manana if (btrfs_leaf_free_space(root, leaf) < 1895f5bc6b1SFilipe Manana (size - old_data_len)) { 1905f5bc6b1SFilipe Manana ret = -ENOSPC; 1915f5bc6b1SFilipe Manana goto out; 1925f5bc6b1SFilipe Manana } 1935f5bc6b1SFilipe Manana } 1945f5bc6b1SFilipe Manana 1955f5bc6b1SFilipe Manana if (old_data_len + name_len + sizeof(*di) == item_size) { 1965f5bc6b1SFilipe Manana /* No other xattrs packed in the same leaf item. */ 1975f5bc6b1SFilipe Manana if (size > old_data_len) 1985f5bc6b1SFilipe Manana btrfs_extend_item(root, path, 1995f5bc6b1SFilipe Manana size - old_data_len); 2005f5bc6b1SFilipe Manana else if (size < old_data_len) 2015f5bc6b1SFilipe Manana btrfs_truncate_item(root, path, data_size, 1); 2025f5bc6b1SFilipe Manana } else { 2035f5bc6b1SFilipe Manana /* There are other xattrs packed in the same item. */ 204fa09200bSJosef Bacik ret = btrfs_delete_one_dir_name(trans, root, path, di); 205fa09200bSJosef Bacik if (ret) 206fa09200bSJosef Bacik goto out; 2075f5bc6b1SFilipe Manana btrfs_extend_item(root, path, data_size); 208fa09200bSJosef Bacik } 2095f5bc6b1SFilipe Manana 2105f5bc6b1SFilipe Manana item = btrfs_item_nr(slot); 2115f5bc6b1SFilipe Manana ptr = btrfs_item_ptr(leaf, slot, char); 2125f5bc6b1SFilipe Manana ptr += btrfs_item_size(leaf, item) - data_size; 2135f5bc6b1SFilipe Manana di = (struct btrfs_dir_item *)ptr; 2145f5bc6b1SFilipe Manana btrfs_set_dir_data_len(leaf, di, size); 2155f5bc6b1SFilipe Manana data_ptr = ((unsigned long)(di + 1)) + name_len; 2165f5bc6b1SFilipe Manana write_extent_buffer(leaf, value, data_ptr, size); 2175f5bc6b1SFilipe Manana btrfs_mark_buffer_dirty(leaf); 2185f5bc6b1SFilipe Manana } else { 2195f5bc6b1SFilipe Manana /* 2205f5bc6b1SFilipe Manana * Insert, and we had space for the xattr, so path->slots[0] is 2215f5bc6b1SFilipe Manana * where our xattr dir_item is and btrfs_insert_xattr_item() 2225f5bc6b1SFilipe Manana * filled it. 2235f5bc6b1SFilipe Manana */ 224fa09200bSJosef Bacik } 2255103e947SJosef Bacik out: 226f34f57a3SYan, Zheng btrfs_free_path(path); 227f34f57a3SYan, Zheng return ret; 2285103e947SJosef Bacik } 2295103e947SJosef Bacik 2304815053aSDavid Sterba /* 2314815053aSDavid Sterba * @value: "" makes the attribute to empty, NULL removes it 2324815053aSDavid Sterba */ 233f34f57a3SYan, Zheng int __btrfs_setxattr(struct btrfs_trans_handle *trans, 234f34f57a3SYan, Zheng struct inode *inode, const char *name, 235f34f57a3SYan, Zheng const void *value, size_t size, int flags) 236f34f57a3SYan, Zheng { 237f34f57a3SYan, Zheng struct btrfs_root *root = BTRFS_I(inode)->root; 238f34f57a3SYan, Zheng int ret; 239f34f57a3SYan, Zheng 240f34f57a3SYan, Zheng if (trans) 241f34f57a3SYan, Zheng return do_setxattr(trans, inode, name, value, size, flags); 242f34f57a3SYan, Zheng 243a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 2); 244a22285a6SYan, Zheng if (IS_ERR(trans)) 245a22285a6SYan, Zheng return PTR_ERR(trans); 246f34f57a3SYan, Zheng 247f34f57a3SYan, Zheng ret = do_setxattr(trans, inode, name, value, size, flags); 248f34f57a3SYan, Zheng if (ret) 249f34f57a3SYan, Zheng goto out; 250f34f57a3SYan, Zheng 2510c4d2d95SJosef Bacik inode_inc_iversion(inode); 252f34f57a3SYan, Zheng inode->i_ctime = CURRENT_TIME; 253e9976151SJosef Bacik set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); 254f34f57a3SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 255f34f57a3SYan, Zheng BUG_ON(ret); 256f34f57a3SYan, Zheng out: 2577ad85bb7SJosef Bacik btrfs_end_transaction(trans, root); 2585103e947SJosef Bacik return ret; 2595103e947SJosef Bacik } 2605103e947SJosef Bacik 2615103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 2625103e947SJosef Bacik { 2635103e947SJosef Bacik struct btrfs_key key, found_key; 2642b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 2655103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 2665103e947SJosef Bacik struct btrfs_path *path; 2675103e947SJosef Bacik struct extent_buffer *leaf; 2685103e947SJosef Bacik struct btrfs_dir_item *di; 2692e6a0035SLi Zefan int ret = 0, slot; 270eaa47d86SChristoph Hellwig size_t total_size = 0, size_left = size; 2715103e947SJosef Bacik unsigned long name_ptr; 272eaa47d86SChristoph Hellwig size_t name_len; 2735103e947SJosef Bacik 2745103e947SJosef Bacik /* 2755103e947SJosef Bacik * ok we want all objects associated with this id. 2765103e947SJosef Bacik * NOTE: we set key.offset = 0; because we want to start with the 2775103e947SJosef Bacik * first xattr that we find and walk forward 2785103e947SJosef Bacik */ 27933345d01SLi Zefan key.objectid = btrfs_ino(inode); 280962a298fSDavid Sterba key.type = BTRFS_XATTR_ITEM_KEY; 2815103e947SJosef Bacik key.offset = 0; 2825103e947SJosef Bacik 2835103e947SJosef Bacik path = btrfs_alloc_path(); 2845103e947SJosef Bacik if (!path) 2855103e947SJosef Bacik return -ENOMEM; 2861caf9342SJosef Bacik path->reada = 2; 2875103e947SJosef Bacik 2885103e947SJosef Bacik /* search for our xattrs */ 2895103e947SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2905103e947SJosef Bacik if (ret < 0) 2915103e947SJosef Bacik goto err; 2922e6a0035SLi Zefan 2935103e947SJosef Bacik while (1) { 2945103e947SJosef Bacik leaf = path->nodes[0]; 2955103e947SJosef Bacik slot = path->slots[0]; 2965103e947SJosef Bacik 2975103e947SJosef Bacik /* this is where we start walking through the path */ 2982e6a0035SLi Zefan if (slot >= btrfs_header_nritems(leaf)) { 2995103e947SJosef Bacik /* 3005103e947SJosef Bacik * if we've reached the last slot in this leaf we need 3015103e947SJosef Bacik * to go to the next leaf and reset everything 3025103e947SJosef Bacik */ 3035103e947SJosef Bacik ret = btrfs_next_leaf(root, path); 3042e6a0035SLi Zefan if (ret < 0) 3052e6a0035SLi Zefan goto err; 3062e6a0035SLi Zefan else if (ret > 0) 3075103e947SJosef Bacik break; 3082e6a0035SLi Zefan continue; 3095103e947SJosef Bacik } 3105103e947SJosef Bacik 3115103e947SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 3125103e947SJosef Bacik 3135103e947SJosef Bacik /* check to make sure this item is what we want */ 3145103e947SJosef Bacik if (found_key.objectid != key.objectid) 3155103e947SJosef Bacik break; 316f1cd1f0bSFilipe Manana if (found_key.type > BTRFS_XATTR_ITEM_KEY) 3175103e947SJosef Bacik break; 318f1cd1f0bSFilipe Manana if (found_key.type < BTRFS_XATTR_ITEM_KEY) 319f1cd1f0bSFilipe Manana goto next; 3205103e947SJosef Bacik 3215103e947SJosef Bacik di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 32222a94d44SJosef Bacik if (verify_dir_item(root, leaf, di)) 323db2254bcSLiu Bo goto next; 3245103e947SJosef Bacik 325eaa47d86SChristoph Hellwig name_len = btrfs_dir_name_len(leaf, di); 326eaa47d86SChristoph Hellwig total_size += name_len + 1; 3275103e947SJosef Bacik 3285103e947SJosef Bacik /* we are just looking for how big our buffer needs to be */ 3295103e947SJosef Bacik if (!size) 3302e6a0035SLi Zefan goto next; 3315103e947SJosef Bacik 332eaa47d86SChristoph Hellwig if (!buffer || (name_len + 1) > size_left) { 3335103e947SJosef Bacik ret = -ERANGE; 334b16281c3SYehuda Sadeh Weinraub goto err; 3355103e947SJosef Bacik } 3365103e947SJosef Bacik 337eaa47d86SChristoph Hellwig name_ptr = (unsigned long)(di + 1); 338eaa47d86SChristoph Hellwig read_extent_buffer(leaf, buffer, name_ptr, name_len); 339eaa47d86SChristoph Hellwig buffer[name_len] = '\0'; 340eaa47d86SChristoph Hellwig 341eaa47d86SChristoph Hellwig size_left -= name_len + 1; 342eaa47d86SChristoph Hellwig buffer += name_len + 1; 3432e6a0035SLi Zefan next: 3442e6a0035SLi Zefan path->slots[0]++; 3455103e947SJosef Bacik } 3465103e947SJosef Bacik ret = total_size; 3475103e947SJosef Bacik 3485103e947SJosef Bacik err: 3495103e947SJosef Bacik btrfs_free_path(path); 3505103e947SJosef Bacik 3515103e947SJosef Bacik return ret; 3525103e947SJosef Bacik } 3535103e947SJosef Bacik 3545103e947SJosef Bacik /* 35595819c05SChristoph Hellwig * List of handlers for synthetic system.* attributes. All real ondisk 35695819c05SChristoph Hellwig * attributes are handled directly. 3575103e947SJosef Bacik */ 358f01cbd3fSStephen Hemminger const struct xattr_handler *btrfs_xattr_handlers[] = { 3590eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 360996a710dSChristoph Hellwig &posix_acl_access_xattr_handler, 361996a710dSChristoph Hellwig &posix_acl_default_xattr_handler, 36295819c05SChristoph Hellwig #endif 36395819c05SChristoph Hellwig NULL, 36495819c05SChristoph Hellwig }; 36595819c05SChristoph Hellwig 36695819c05SChristoph Hellwig /* 36795819c05SChristoph Hellwig * Check if the attribute is in a supported namespace. 36895819c05SChristoph Hellwig * 3693c3b04d1SDavid Sterba * This is applied after the check for the synthetic attributes in the system 37095819c05SChristoph Hellwig * namespace. 37195819c05SChristoph Hellwig */ 3723c3b04d1SDavid Sterba static int btrfs_is_valid_xattr(const char *name) 37395819c05SChristoph Hellwig { 3743c3b04d1SDavid Sterba int len = strlen(name); 3753c3b04d1SDavid Sterba int prefixlen = 0; 3763c3b04d1SDavid Sterba 3773c3b04d1SDavid Sterba if (!strncmp(name, XATTR_SECURITY_PREFIX, 3783c3b04d1SDavid Sterba XATTR_SECURITY_PREFIX_LEN)) 3793c3b04d1SDavid Sterba prefixlen = XATTR_SECURITY_PREFIX_LEN; 3803c3b04d1SDavid Sterba else if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 3813c3b04d1SDavid Sterba prefixlen = XATTR_SYSTEM_PREFIX_LEN; 3823c3b04d1SDavid Sterba else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) 3833c3b04d1SDavid Sterba prefixlen = XATTR_TRUSTED_PREFIX_LEN; 3843c3b04d1SDavid Sterba else if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) 3853c3b04d1SDavid Sterba prefixlen = XATTR_USER_PREFIX_LEN; 3863c3b04d1SDavid Sterba else if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) 3873c3b04d1SDavid Sterba prefixlen = XATTR_BTRFS_PREFIX_LEN; 3883c3b04d1SDavid Sterba else 3893c3b04d1SDavid Sterba return -EOPNOTSUPP; 3903c3b04d1SDavid Sterba 3913c3b04d1SDavid Sterba /* 3923c3b04d1SDavid Sterba * The name cannot consist of just prefix 3933c3b04d1SDavid Sterba */ 3943c3b04d1SDavid Sterba if (len <= prefixlen) 3953c3b04d1SDavid Sterba return -EINVAL; 3963c3b04d1SDavid Sterba 3973c3b04d1SDavid Sterba return 0; 39869a32ac5SChris Mason } 39969a32ac5SChris Mason 40095819c05SChristoph Hellwig ssize_t btrfs_getxattr(struct dentry *dentry, const char *name, 40195819c05SChristoph Hellwig void *buffer, size_t size) 40295819c05SChristoph Hellwig { 4033c3b04d1SDavid Sterba int ret; 4043c3b04d1SDavid Sterba 40595819c05SChristoph Hellwig /* 40695819c05SChristoph Hellwig * If this is a request for a synthetic attribute in the system.* 40795819c05SChristoph Hellwig * namespace use the generic infrastructure to resolve a handler 40895819c05SChristoph Hellwig * for it via sb->s_xattr. 40995819c05SChristoph Hellwig */ 41095819c05SChristoph Hellwig if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 41195819c05SChristoph Hellwig return generic_getxattr(dentry, name, buffer, size); 4125103e947SJosef Bacik 4133c3b04d1SDavid Sterba ret = btrfs_is_valid_xattr(name); 4143c3b04d1SDavid Sterba if (ret) 4153c3b04d1SDavid Sterba return ret; 4162b0143b5SDavid Howells return __btrfs_getxattr(d_inode(dentry), name, buffer, size); 41795819c05SChristoph Hellwig } 4185103e947SJosef Bacik 41995819c05SChristoph Hellwig int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value, 42095819c05SChristoph Hellwig size_t size, int flags) 42195819c05SChristoph Hellwig { 4222b0143b5SDavid Howells struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root; 4233c3b04d1SDavid Sterba int ret; 424b83cc969SLi Zefan 425b83cc969SLi Zefan /* 426b83cc969SLi Zefan * The permission on security.* and system.* is not checked 427b83cc969SLi Zefan * in permission(). 428b83cc969SLi Zefan */ 429b83cc969SLi Zefan if (btrfs_root_readonly(root)) 430b83cc969SLi Zefan return -EROFS; 431b83cc969SLi Zefan 43295819c05SChristoph Hellwig /* 43395819c05SChristoph Hellwig * If this is a request for a synthetic attribute in the system.* 43495819c05SChristoph Hellwig * namespace use the generic infrastructure to resolve a handler 43595819c05SChristoph Hellwig * for it via sb->s_xattr. 43695819c05SChristoph Hellwig */ 43795819c05SChristoph Hellwig if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 43895819c05SChristoph Hellwig return generic_setxattr(dentry, name, value, size, flags); 4395103e947SJosef Bacik 4403c3b04d1SDavid Sterba ret = btrfs_is_valid_xattr(name); 4413c3b04d1SDavid Sterba if (ret) 4423c3b04d1SDavid Sterba return ret; 4435103e947SJosef Bacik 44463541927SFilipe David Borba Manana if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) 4452b0143b5SDavid Howells return btrfs_set_prop(d_inode(dentry), name, 44663541927SFilipe David Borba Manana value, size, flags); 44763541927SFilipe David Borba Manana 44895819c05SChristoph Hellwig if (size == 0) 44995819c05SChristoph Hellwig value = ""; /* empty EA, do not remove */ 450f34f57a3SYan, Zheng 4512b0143b5SDavid Howells return __btrfs_setxattr(NULL, d_inode(dentry), name, value, size, 452f34f57a3SYan, Zheng flags); 45395819c05SChristoph Hellwig } 45495819c05SChristoph Hellwig 45595819c05SChristoph Hellwig int btrfs_removexattr(struct dentry *dentry, const char *name) 45695819c05SChristoph Hellwig { 4572b0143b5SDavid Howells struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root; 4583c3b04d1SDavid Sterba int ret; 459b83cc969SLi Zefan 460b83cc969SLi Zefan /* 461b83cc969SLi Zefan * The permission on security.* and system.* is not checked 462b83cc969SLi Zefan * in permission(). 463b83cc969SLi Zefan */ 464b83cc969SLi Zefan if (btrfs_root_readonly(root)) 465b83cc969SLi Zefan return -EROFS; 466b83cc969SLi Zefan 46795819c05SChristoph Hellwig /* 46895819c05SChristoph Hellwig * If this is a request for a synthetic attribute in the system.* 46995819c05SChristoph Hellwig * namespace use the generic infrastructure to resolve a handler 47095819c05SChristoph Hellwig * for it via sb->s_xattr. 47195819c05SChristoph Hellwig */ 47295819c05SChristoph Hellwig if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 47395819c05SChristoph Hellwig return generic_removexattr(dentry, name); 47495819c05SChristoph Hellwig 4753c3b04d1SDavid Sterba ret = btrfs_is_valid_xattr(name); 4763c3b04d1SDavid Sterba if (ret) 4773c3b04d1SDavid Sterba return ret; 478f34f57a3SYan, Zheng 47963541927SFilipe David Borba Manana if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN)) 4802b0143b5SDavid Howells return btrfs_set_prop(d_inode(dentry), name, 48163541927SFilipe David Borba Manana NULL, 0, XATTR_REPLACE); 48263541927SFilipe David Borba Manana 4832b0143b5SDavid Howells return __btrfs_setxattr(NULL, d_inode(dentry), name, NULL, 0, 484f34f57a3SYan, Zheng XATTR_REPLACE); 48595819c05SChristoph Hellwig } 4860279b4cdSJim Owens 48748a3b636SEric Sandeen static int btrfs_initxattrs(struct inode *inode, 48848a3b636SEric Sandeen const struct xattr *xattr_array, void *fs_info) 4899d8f13baSMimi Zohar { 4909d8f13baSMimi Zohar const struct xattr *xattr; 4919d8f13baSMimi Zohar struct btrfs_trans_handle *trans = fs_info; 4929d8f13baSMimi Zohar char *name; 4939d8f13baSMimi Zohar int err = 0; 4949d8f13baSMimi Zohar 4959d8f13baSMimi Zohar for (xattr = xattr_array; xattr->name != NULL; xattr++) { 4969d8f13baSMimi Zohar name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 497*39a27ec1SDavid Sterba strlen(xattr->name) + 1, GFP_KERNEL); 4989d8f13baSMimi Zohar if (!name) { 4999d8f13baSMimi Zohar err = -ENOMEM; 5009d8f13baSMimi Zohar break; 5019d8f13baSMimi Zohar } 5029d8f13baSMimi Zohar strcpy(name, XATTR_SECURITY_PREFIX); 5039d8f13baSMimi Zohar strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); 5049d8f13baSMimi Zohar err = __btrfs_setxattr(trans, inode, name, 5059d8f13baSMimi Zohar xattr->value, xattr->value_len, 0); 5069d8f13baSMimi Zohar kfree(name); 5079d8f13baSMimi Zohar if (err < 0) 5089d8f13baSMimi Zohar break; 5099d8f13baSMimi Zohar } 5109d8f13baSMimi Zohar return err; 5119d8f13baSMimi Zohar } 5129d8f13baSMimi Zohar 513f34f57a3SYan, Zheng int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, 5142a7dba39SEric Paris struct inode *inode, struct inode *dir, 5152a7dba39SEric Paris const struct qstr *qstr) 5160279b4cdSJim Owens { 5179d8f13baSMimi Zohar return security_inode_init_security(inode, dir, qstr, 5189d8f13baSMimi Zohar &btrfs_initxattrs, trans); 5190279b4cdSJim Owens } 520