1*c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 25103e947SJosef Bacik /* 35103e947SJosef Bacik * Copyright (C) 2007 Red Hat. All rights reserved. 45103e947SJosef Bacik */ 55103e947SJosef Bacik 65103e947SJosef Bacik #include <linux/init.h> 75103e947SJosef Bacik #include <linux/fs.h> 85103e947SJosef Bacik #include <linux/slab.h> 95103e947SJosef Bacik #include <linux/rwsem.h> 105103e947SJosef Bacik #include <linux/xattr.h> 110279b4cdSJim Owens #include <linux/security.h> 12996a710dSChristoph Hellwig #include <linux/posix_acl_xattr.h> 13ae5e165dSJeff Layton #include <linux/iversion.h> 145103e947SJosef Bacik #include "ctree.h" 155103e947SJosef Bacik #include "btrfs_inode.h" 165103e947SJosef Bacik #include "transaction.h" 175103e947SJosef Bacik #include "xattr.h" 185103e947SJosef Bacik #include "disk-io.h" 1963541927SFilipe David Borba Manana #include "props.h" 205f5bc6b1SFilipe Manana #include "locking.h" 2133268eafSJosef Bacik 22bcadd705SDavid Sterba int btrfs_getxattr(struct inode *inode, const char *name, 2395819c05SChristoph Hellwig void *buffer, size_t size) 245103e947SJosef Bacik { 255103e947SJosef Bacik struct btrfs_dir_item *di; 265103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 275103e947SJosef Bacik struct btrfs_path *path; 285103e947SJosef Bacik struct extent_buffer *leaf; 295103e947SJosef Bacik int ret = 0; 305103e947SJosef Bacik unsigned long data_ptr; 315103e947SJosef Bacik 325103e947SJosef Bacik path = btrfs_alloc_path(); 3395819c05SChristoph Hellwig if (!path) 345103e947SJosef Bacik return -ENOMEM; 355103e947SJosef Bacik 365103e947SJosef Bacik /* lookup the xattr by name */ 37f85b7379SDavid Sterba di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)), 38f85b7379SDavid Sterba name, strlen(name), 0); 3907060404SJosef Bacik if (!di) { 405103e947SJosef Bacik ret = -ENODATA; 415103e947SJosef Bacik goto out; 4207060404SJosef Bacik } else if (IS_ERR(di)) { 4307060404SJosef Bacik ret = PTR_ERR(di); 4407060404SJosef Bacik goto out; 455103e947SJosef Bacik } 465103e947SJosef Bacik 475103e947SJosef Bacik leaf = path->nodes[0]; 485103e947SJosef Bacik /* if size is 0, that means we want the size of the attr */ 495103e947SJosef Bacik if (!size) { 505103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 515103e947SJosef Bacik goto out; 525103e947SJosef Bacik } 535103e947SJosef Bacik 545103e947SJosef Bacik /* now get the data out of our dir_item */ 555103e947SJosef Bacik if (btrfs_dir_data_len(leaf, di) > size) { 565103e947SJosef Bacik ret = -ERANGE; 575103e947SJosef Bacik goto out; 585103e947SJosef Bacik } 5907060404SJosef Bacik 6007060404SJosef Bacik /* 6107060404SJosef Bacik * The way things are packed into the leaf is like this 6207060404SJosef Bacik * |struct btrfs_dir_item|name|data| 6307060404SJosef Bacik * where name is the xattr name, so security.foo, and data is the 6407060404SJosef Bacik * content of the xattr. data_ptr points to the location in memory 6507060404SJosef Bacik * where the data starts in the in memory leaf 6607060404SJosef Bacik */ 675103e947SJosef Bacik data_ptr = (unsigned long)((char *)(di + 1) + 685103e947SJosef Bacik btrfs_dir_name_len(leaf, di)); 695103e947SJosef Bacik read_extent_buffer(leaf, buffer, data_ptr, 703acd7ee8SJosef Bacik btrfs_dir_data_len(leaf, di)); 715103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 725103e947SJosef Bacik 735103e947SJosef Bacik out: 745103e947SJosef Bacik btrfs_free_path(path); 755103e947SJosef Bacik return ret; 765103e947SJosef Bacik } 775103e947SJosef Bacik 78f34f57a3SYan, Zheng static int do_setxattr(struct btrfs_trans_handle *trans, 79f34f57a3SYan, Zheng struct inode *inode, const char *name, 8095819c05SChristoph Hellwig const void *value, size_t size, int flags) 815103e947SJosef Bacik { 825f5bc6b1SFilipe Manana struct btrfs_dir_item *di = NULL; 835103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 842ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 855103e947SJosef Bacik struct btrfs_path *path; 86f34f57a3SYan, Zheng size_t name_len = strlen(name); 87f34f57a3SYan, Zheng int ret = 0; 88f34f57a3SYan, Zheng 89da17066cSJeff Mahoney if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info)) 90f34f57a3SYan, Zheng return -ENOSPC; 915103e947SJosef Bacik 925103e947SJosef Bacik path = btrfs_alloc_path(); 9395819c05SChristoph Hellwig if (!path) 945103e947SJosef Bacik return -ENOMEM; 955f5bc6b1SFilipe Manana path->skip_release_on_error = 1; 965103e947SJosef Bacik 975f5bc6b1SFilipe Manana if (!value) { 98f85b7379SDavid Sterba di = btrfs_lookup_xattr(trans, root, path, 99f85b7379SDavid Sterba btrfs_ino(BTRFS_I(inode)), name, name_len, -1); 1005f5bc6b1SFilipe Manana if (!di && (flags & XATTR_REPLACE)) 1015f5bc6b1SFilipe Manana ret = -ENODATA; 1025cdf83edSFilipe Manana else if (IS_ERR(di)) 1035cdf83edSFilipe Manana ret = PTR_ERR(di); 1045f5bc6b1SFilipe Manana else if (di) 1055f5bc6b1SFilipe Manana ret = btrfs_delete_one_dir_name(trans, root, path, di); 1065103e947SJosef Bacik goto out; 1075f5bc6b1SFilipe Manana } 1085f5bc6b1SFilipe Manana 1095f5bc6b1SFilipe Manana /* 1105f5bc6b1SFilipe Manana * For a replace we can't just do the insert blindly. 1115f5bc6b1SFilipe Manana * Do a lookup first (read-only btrfs_search_slot), and return if xattr 1125f5bc6b1SFilipe Manana * doesn't exist. If it exists, fall down below to the insert/replace 1135f5bc6b1SFilipe Manana * path - we can't race with a concurrent xattr delete, because the VFS 1145f5bc6b1SFilipe Manana * locks the inode's i_mutex before calling setxattr or removexattr. 1155f5bc6b1SFilipe Manana */ 1165f5bc6b1SFilipe Manana if (flags & XATTR_REPLACE) { 1175955102cSAl Viro ASSERT(inode_is_locked(inode)); 118f85b7379SDavid Sterba di = btrfs_lookup_xattr(NULL, root, path, 119f85b7379SDavid Sterba btrfs_ino(BTRFS_I(inode)), name, name_len, 0); 1205cdf83edSFilipe Manana if (!di) 1215103e947SJosef Bacik ret = -ENODATA; 1225cdf83edSFilipe Manana else if (IS_ERR(di)) 1235cdf83edSFilipe Manana ret = PTR_ERR(di); 1245cdf83edSFilipe Manana if (ret) 1255103e947SJosef Bacik goto out; 126fa09200bSJosef Bacik btrfs_release_path(path); 1275f5bc6b1SFilipe Manana di = NULL; 12833268eafSJosef Bacik } 1295103e947SJosef Bacik 1304a0cc7caSNikolay Borisov ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)), 131f34f57a3SYan, Zheng name, name_len, value, size); 1325f5bc6b1SFilipe Manana if (ret == -EOVERFLOW) { 133ed3ee9f4SJosef Bacik /* 1345f5bc6b1SFilipe Manana * We have an existing item in a leaf, split_leaf couldn't 1355f5bc6b1SFilipe Manana * expand it. That item might have or not a dir_item that 1365f5bc6b1SFilipe Manana * matches our target xattr, so lets check. 137ed3ee9f4SJosef Bacik */ 1385f5bc6b1SFilipe Manana ret = 0; 1395f5bc6b1SFilipe Manana btrfs_assert_tree_locked(path->nodes[0]); 1402ff7e61eSJeff Mahoney di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 1415f5bc6b1SFilipe Manana if (!di && !(flags & XATTR_REPLACE)) { 1425f5bc6b1SFilipe Manana ret = -ENOSPC; 143fa09200bSJosef Bacik goto out; 1445f5bc6b1SFilipe Manana } 1455f5bc6b1SFilipe Manana } else if (ret == -EEXIST) { 1465f5bc6b1SFilipe Manana ret = 0; 1472ff7e61eSJeff Mahoney di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 1485f5bc6b1SFilipe Manana ASSERT(di); /* logic error */ 1495f5bc6b1SFilipe Manana } else if (ret) { 150fa09200bSJosef Bacik goto out; 151fa09200bSJosef Bacik } 152fa09200bSJosef Bacik 1535f5bc6b1SFilipe Manana if (di && (flags & XATTR_CREATE)) { 1545f5bc6b1SFilipe Manana ret = -EEXIST; 1555f5bc6b1SFilipe Manana goto out; 1565f5bc6b1SFilipe Manana } 1575f5bc6b1SFilipe Manana 1585f5bc6b1SFilipe Manana if (di) { 1595f5bc6b1SFilipe Manana /* 1605f5bc6b1SFilipe Manana * We're doing a replace, and it must be atomic, that is, at 1615f5bc6b1SFilipe Manana * any point in time we have either the old or the new xattr 1625f5bc6b1SFilipe Manana * value in the tree. We don't want readers (getxattr and 1635f5bc6b1SFilipe Manana * listxattrs) to miss a value, this is specially important 1645f5bc6b1SFilipe Manana * for ACLs. 1655f5bc6b1SFilipe Manana */ 1665f5bc6b1SFilipe Manana const int slot = path->slots[0]; 1675f5bc6b1SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 1685f5bc6b1SFilipe Manana const u16 old_data_len = btrfs_dir_data_len(leaf, di); 1695f5bc6b1SFilipe Manana const u32 item_size = btrfs_item_size_nr(leaf, slot); 1705f5bc6b1SFilipe Manana const u32 data_size = sizeof(*di) + name_len + size; 1715f5bc6b1SFilipe Manana struct btrfs_item *item; 1725f5bc6b1SFilipe Manana unsigned long data_ptr; 1735f5bc6b1SFilipe Manana char *ptr; 1745f5bc6b1SFilipe Manana 1755f5bc6b1SFilipe Manana if (size > old_data_len) { 1762ff7e61eSJeff Mahoney if (btrfs_leaf_free_space(fs_info, leaf) < 1775f5bc6b1SFilipe Manana (size - old_data_len)) { 1785f5bc6b1SFilipe Manana ret = -ENOSPC; 1795f5bc6b1SFilipe Manana goto out; 1805f5bc6b1SFilipe Manana } 1815f5bc6b1SFilipe Manana } 1825f5bc6b1SFilipe Manana 1835f5bc6b1SFilipe Manana if (old_data_len + name_len + sizeof(*di) == item_size) { 1845f5bc6b1SFilipe Manana /* No other xattrs packed in the same leaf item. */ 1855f5bc6b1SFilipe Manana if (size > old_data_len) 1862ff7e61eSJeff Mahoney btrfs_extend_item(fs_info, path, 1875f5bc6b1SFilipe Manana size - old_data_len); 1885f5bc6b1SFilipe Manana else if (size < old_data_len) 1892ff7e61eSJeff Mahoney btrfs_truncate_item(fs_info, path, 1902ff7e61eSJeff Mahoney data_size, 1); 1915f5bc6b1SFilipe Manana } else { 1925f5bc6b1SFilipe Manana /* There are other xattrs packed in the same item. */ 193fa09200bSJosef Bacik ret = btrfs_delete_one_dir_name(trans, root, path, di); 194fa09200bSJosef Bacik if (ret) 195fa09200bSJosef Bacik goto out; 1962ff7e61eSJeff Mahoney btrfs_extend_item(fs_info, path, data_size); 197fa09200bSJosef Bacik } 1985f5bc6b1SFilipe Manana 1995f5bc6b1SFilipe Manana item = btrfs_item_nr(slot); 2005f5bc6b1SFilipe Manana ptr = btrfs_item_ptr(leaf, slot, char); 2015f5bc6b1SFilipe Manana ptr += btrfs_item_size(leaf, item) - data_size; 2025f5bc6b1SFilipe Manana di = (struct btrfs_dir_item *)ptr; 2035f5bc6b1SFilipe Manana btrfs_set_dir_data_len(leaf, di, size); 2045f5bc6b1SFilipe Manana data_ptr = ((unsigned long)(di + 1)) + name_len; 2055f5bc6b1SFilipe Manana write_extent_buffer(leaf, value, data_ptr, size); 2065f5bc6b1SFilipe Manana btrfs_mark_buffer_dirty(leaf); 2075f5bc6b1SFilipe Manana } else { 2085f5bc6b1SFilipe Manana /* 2095f5bc6b1SFilipe Manana * Insert, and we had space for the xattr, so path->slots[0] is 2105f5bc6b1SFilipe Manana * where our xattr dir_item is and btrfs_insert_xattr_item() 2115f5bc6b1SFilipe Manana * filled it. 2125f5bc6b1SFilipe Manana */ 213fa09200bSJosef Bacik } 2145103e947SJosef Bacik out: 215f34f57a3SYan, Zheng btrfs_free_path(path); 216f34f57a3SYan, Zheng return ret; 2175103e947SJosef Bacik } 2185103e947SJosef Bacik 2194815053aSDavid Sterba /* 2204815053aSDavid Sterba * @value: "" makes the attribute to empty, NULL removes it 2214815053aSDavid Sterba */ 2227852781dSDavid Sterba int btrfs_setxattr(struct btrfs_trans_handle *trans, 223f34f57a3SYan, Zheng struct inode *inode, const char *name, 224f34f57a3SYan, Zheng const void *value, size_t size, int flags) 225f34f57a3SYan, Zheng { 226f34f57a3SYan, Zheng struct btrfs_root *root = BTRFS_I(inode)->root; 227f34f57a3SYan, Zheng int ret; 228f34f57a3SYan, Zheng 229e0d46f5cSAndreas Gruenbacher if (btrfs_root_readonly(root)) 230e0d46f5cSAndreas Gruenbacher return -EROFS; 231e0d46f5cSAndreas Gruenbacher 232f34f57a3SYan, Zheng if (trans) 233f34f57a3SYan, Zheng return do_setxattr(trans, inode, name, value, size, flags); 234f34f57a3SYan, Zheng 235a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 2); 236a22285a6SYan, Zheng if (IS_ERR(trans)) 237a22285a6SYan, Zheng return PTR_ERR(trans); 238f34f57a3SYan, Zheng 239f34f57a3SYan, Zheng ret = do_setxattr(trans, inode, name, value, size, flags); 240f34f57a3SYan, Zheng if (ret) 241f34f57a3SYan, Zheng goto out; 242f34f57a3SYan, Zheng 2430c4d2d95SJosef Bacik inode_inc_iversion(inode); 244c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 245e9976151SJosef Bacik set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); 246f34f57a3SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 247f34f57a3SYan, Zheng BUG_ON(ret); 248f34f57a3SYan, Zheng out: 2493a45bb20SJeff Mahoney btrfs_end_transaction(trans); 2505103e947SJosef Bacik return ret; 2515103e947SJosef Bacik } 2525103e947SJosef Bacik 2535103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 2545103e947SJosef Bacik { 255daac7ba6SFilipe Manana struct btrfs_key key; 2562b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 2575103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 2585103e947SJosef Bacik struct btrfs_path *path; 259daac7ba6SFilipe Manana int ret = 0; 260eaa47d86SChristoph Hellwig size_t total_size = 0, size_left = size; 2615103e947SJosef Bacik 2625103e947SJosef Bacik /* 2635103e947SJosef Bacik * ok we want all objects associated with this id. 2645103e947SJosef Bacik * NOTE: we set key.offset = 0; because we want to start with the 2655103e947SJosef Bacik * first xattr that we find and walk forward 2665103e947SJosef Bacik */ 2674a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(inode)); 268962a298fSDavid Sterba key.type = BTRFS_XATTR_ITEM_KEY; 2695103e947SJosef Bacik key.offset = 0; 2705103e947SJosef Bacik 2715103e947SJosef Bacik path = btrfs_alloc_path(); 2725103e947SJosef Bacik if (!path) 2735103e947SJosef Bacik return -ENOMEM; 274e4058b54SDavid Sterba path->reada = READA_FORWARD; 2755103e947SJosef Bacik 2765103e947SJosef Bacik /* search for our xattrs */ 2775103e947SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2785103e947SJosef Bacik if (ret < 0) 2795103e947SJosef Bacik goto err; 2802e6a0035SLi Zefan 2815103e947SJosef Bacik while (1) { 282daac7ba6SFilipe Manana struct extent_buffer *leaf; 283daac7ba6SFilipe Manana int slot; 284daac7ba6SFilipe Manana struct btrfs_dir_item *di; 285daac7ba6SFilipe Manana struct btrfs_key found_key; 286daac7ba6SFilipe Manana u32 item_size; 287daac7ba6SFilipe Manana u32 cur; 288daac7ba6SFilipe Manana 2895103e947SJosef Bacik leaf = path->nodes[0]; 2905103e947SJosef Bacik slot = path->slots[0]; 2915103e947SJosef Bacik 2925103e947SJosef Bacik /* this is where we start walking through the path */ 2932e6a0035SLi Zefan if (slot >= btrfs_header_nritems(leaf)) { 2945103e947SJosef Bacik /* 2955103e947SJosef Bacik * if we've reached the last slot in this leaf we need 2965103e947SJosef Bacik * to go to the next leaf and reset everything 2975103e947SJosef Bacik */ 2985103e947SJosef Bacik ret = btrfs_next_leaf(root, path); 2992e6a0035SLi Zefan if (ret < 0) 3002e6a0035SLi Zefan goto err; 3012e6a0035SLi Zefan else if (ret > 0) 3025103e947SJosef Bacik break; 3032e6a0035SLi Zefan continue; 3045103e947SJosef Bacik } 3055103e947SJosef Bacik 3065103e947SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 3075103e947SJosef Bacik 3085103e947SJosef Bacik /* check to make sure this item is what we want */ 3095103e947SJosef Bacik if (found_key.objectid != key.objectid) 3105103e947SJosef Bacik break; 311f1cd1f0bSFilipe Manana if (found_key.type > BTRFS_XATTR_ITEM_KEY) 3125103e947SJosef Bacik break; 313f1cd1f0bSFilipe Manana if (found_key.type < BTRFS_XATTR_ITEM_KEY) 314daac7ba6SFilipe Manana goto next_item; 3155103e947SJosef Bacik 3165103e947SJosef Bacik di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 317daac7ba6SFilipe Manana item_size = btrfs_item_size_nr(leaf, slot); 318daac7ba6SFilipe Manana cur = 0; 319daac7ba6SFilipe Manana while (cur < item_size) { 320daac7ba6SFilipe Manana u16 name_len = btrfs_dir_name_len(leaf, di); 321daac7ba6SFilipe Manana u16 data_len = btrfs_dir_data_len(leaf, di); 322daac7ba6SFilipe Manana u32 this_len = sizeof(*di) + name_len + data_len; 323daac7ba6SFilipe Manana unsigned long name_ptr = (unsigned long)(di + 1); 3245103e947SJosef Bacik 325eaa47d86SChristoph Hellwig total_size += name_len + 1; 326daac7ba6SFilipe Manana /* 327daac7ba6SFilipe Manana * We are just looking for how big our buffer needs to 328daac7ba6SFilipe Manana * be. 329daac7ba6SFilipe Manana */ 3305103e947SJosef Bacik if (!size) 3312e6a0035SLi Zefan goto next; 3325103e947SJosef Bacik 333eaa47d86SChristoph Hellwig if (!buffer || (name_len + 1) > size_left) { 3345103e947SJosef Bacik ret = -ERANGE; 335b16281c3SYehuda Sadeh Weinraub goto err; 3365103e947SJosef Bacik } 3375103e947SJosef Bacik 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: 344daac7ba6SFilipe Manana cur += this_len; 345daac7ba6SFilipe Manana di = (struct btrfs_dir_item *)((char *)di + this_len); 346daac7ba6SFilipe Manana } 347daac7ba6SFilipe Manana next_item: 3482e6a0035SLi Zefan path->slots[0]++; 3495103e947SJosef Bacik } 3505103e947SJosef Bacik ret = total_size; 3515103e947SJosef Bacik 3525103e947SJosef Bacik err: 3535103e947SJosef Bacik btrfs_free_path(path); 3545103e947SJosef Bacik 3555103e947SJosef Bacik return ret; 3565103e947SJosef Bacik } 3575103e947SJosef Bacik 3589172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_get(const struct xattr_handler *handler, 359b296821aSAl Viro struct dentry *unused, struct inode *inode, 360b296821aSAl Viro const char *name, void *buffer, size_t size) 3619172abbcSAndreas Gruenbacher { 3629172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 3637852781dSDavid Sterba return btrfs_getxattr(inode, name, buffer, size); 3649172abbcSAndreas Gruenbacher } 3659172abbcSAndreas Gruenbacher 3669172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_set(const struct xattr_handler *handler, 36759301226SAl Viro struct dentry *unused, struct inode *inode, 36859301226SAl Viro const char *name, const void *buffer, 36959301226SAl Viro size_t size, int flags) 3709172abbcSAndreas Gruenbacher { 3719172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 3727852781dSDavid Sterba return btrfs_setxattr(NULL, inode, name, buffer, size, flags); 3739172abbcSAndreas Gruenbacher } 3749172abbcSAndreas Gruenbacher 3759172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, 37659301226SAl Viro struct dentry *unused, struct inode *inode, 3779172abbcSAndreas Gruenbacher const char *name, const void *value, 3789172abbcSAndreas Gruenbacher size_t size, int flags) 3799172abbcSAndreas Gruenbacher { 3809172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 38159301226SAl Viro return btrfs_set_prop(inode, name, value, size, flags); 3829172abbcSAndreas Gruenbacher } 3839172abbcSAndreas Gruenbacher 3849172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_security_xattr_handler = { 3859172abbcSAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3869172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 3879172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 3889172abbcSAndreas Gruenbacher }; 3899172abbcSAndreas Gruenbacher 3909172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_trusted_xattr_handler = { 3919172abbcSAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3929172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 3939172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 3949172abbcSAndreas Gruenbacher }; 3959172abbcSAndreas Gruenbacher 3969172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_user_xattr_handler = { 3979172abbcSAndreas Gruenbacher .prefix = XATTR_USER_PREFIX, 3989172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 3999172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 4009172abbcSAndreas Gruenbacher }; 4019172abbcSAndreas Gruenbacher 4029172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_btrfs_xattr_handler = { 4039172abbcSAndreas Gruenbacher .prefix = XATTR_BTRFS_PREFIX, 4049172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 4059172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set_prop, 4069172abbcSAndreas Gruenbacher }; 4079172abbcSAndreas Gruenbacher 408f01cbd3fSStephen Hemminger const struct xattr_handler *btrfs_xattr_handlers[] = { 4099172abbcSAndreas Gruenbacher &btrfs_security_xattr_handler, 4100eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 411996a710dSChristoph Hellwig &posix_acl_access_xattr_handler, 412996a710dSChristoph Hellwig &posix_acl_default_xattr_handler, 41395819c05SChristoph Hellwig #endif 4149172abbcSAndreas Gruenbacher &btrfs_trusted_xattr_handler, 4159172abbcSAndreas Gruenbacher &btrfs_user_xattr_handler, 4169172abbcSAndreas Gruenbacher &btrfs_btrfs_xattr_handler, 41795819c05SChristoph Hellwig NULL, 41895819c05SChristoph Hellwig }; 41995819c05SChristoph Hellwig 42048a3b636SEric Sandeen static int btrfs_initxattrs(struct inode *inode, 42148a3b636SEric Sandeen const struct xattr *xattr_array, void *fs_info) 4229d8f13baSMimi Zohar { 4239d8f13baSMimi Zohar const struct xattr *xattr; 4249d8f13baSMimi Zohar struct btrfs_trans_handle *trans = fs_info; 4259d8f13baSMimi Zohar char *name; 4269d8f13baSMimi Zohar int err = 0; 4279d8f13baSMimi Zohar 4289d8f13baSMimi Zohar for (xattr = xattr_array; xattr->name != NULL; xattr++) { 4299d8f13baSMimi Zohar name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 43039a27ec1SDavid Sterba strlen(xattr->name) + 1, GFP_KERNEL); 4319d8f13baSMimi Zohar if (!name) { 4329d8f13baSMimi Zohar err = -ENOMEM; 4339d8f13baSMimi Zohar break; 4349d8f13baSMimi Zohar } 4359d8f13baSMimi Zohar strcpy(name, XATTR_SECURITY_PREFIX); 4369d8f13baSMimi Zohar strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); 4377852781dSDavid Sterba err = btrfs_setxattr(trans, inode, name, xattr->value, 4387852781dSDavid Sterba xattr->value_len, 0); 4399d8f13baSMimi Zohar kfree(name); 4409d8f13baSMimi Zohar if (err < 0) 4419d8f13baSMimi Zohar break; 4429d8f13baSMimi Zohar } 4439d8f13baSMimi Zohar return err; 4449d8f13baSMimi Zohar } 4459d8f13baSMimi Zohar 446f34f57a3SYan, Zheng int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, 4472a7dba39SEric Paris struct inode *inode, struct inode *dir, 4482a7dba39SEric Paris const struct qstr *qstr) 4490279b4cdSJim Owens { 4509d8f13baSMimi Zohar return security_inode_init_security(inode, dir, qstr, 4519d8f13baSMimi Zohar &btrfs_initxattrs, trans); 4520279b4cdSJim Owens } 453