1c1d7c514SDavid 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> 14827aa18eSFilipe Manana #include <linux/sched/mm.h> 155103e947SJosef Bacik #include "ctree.h" 165103e947SJosef Bacik #include "btrfs_inode.h" 175103e947SJosef Bacik #include "transaction.h" 185103e947SJosef Bacik #include "xattr.h" 195103e947SJosef Bacik #include "disk-io.h" 2063541927SFilipe David Borba Manana #include "props.h" 215f5bc6b1SFilipe Manana #include "locking.h" 2233268eafSJosef Bacik 23bcadd705SDavid Sterba int btrfs_getxattr(struct inode *inode, const char *name, 2495819c05SChristoph Hellwig void *buffer, size_t size) 255103e947SJosef Bacik { 265103e947SJosef Bacik struct btrfs_dir_item *di; 275103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 285103e947SJosef Bacik struct btrfs_path *path; 295103e947SJosef Bacik struct extent_buffer *leaf; 305103e947SJosef Bacik int ret = 0; 315103e947SJosef Bacik unsigned long data_ptr; 325103e947SJosef Bacik 335103e947SJosef Bacik path = btrfs_alloc_path(); 3495819c05SChristoph Hellwig if (!path) 355103e947SJosef Bacik return -ENOMEM; 365103e947SJosef Bacik 375103e947SJosef Bacik /* lookup the xattr by name */ 38f85b7379SDavid Sterba di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)), 39f85b7379SDavid Sterba name, strlen(name), 0); 4007060404SJosef Bacik if (!di) { 415103e947SJosef Bacik ret = -ENODATA; 425103e947SJosef Bacik goto out; 4307060404SJosef Bacik } else if (IS_ERR(di)) { 4407060404SJosef Bacik ret = PTR_ERR(di); 4507060404SJosef Bacik goto out; 465103e947SJosef Bacik } 475103e947SJosef Bacik 485103e947SJosef Bacik leaf = path->nodes[0]; 495103e947SJosef Bacik /* if size is 0, that means we want the size of the attr */ 505103e947SJosef Bacik if (!size) { 515103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 525103e947SJosef Bacik goto out; 535103e947SJosef Bacik } 545103e947SJosef Bacik 555103e947SJosef Bacik /* now get the data out of our dir_item */ 565103e947SJosef Bacik if (btrfs_dir_data_len(leaf, di) > size) { 575103e947SJosef Bacik ret = -ERANGE; 585103e947SJosef Bacik goto out; 595103e947SJosef Bacik } 6007060404SJosef Bacik 6107060404SJosef Bacik /* 6207060404SJosef Bacik * The way things are packed into the leaf is like this 6307060404SJosef Bacik * |struct btrfs_dir_item|name|data| 6407060404SJosef Bacik * where name is the xattr name, so security.foo, and data is the 6507060404SJosef Bacik * content of the xattr. data_ptr points to the location in memory 6607060404SJosef Bacik * where the data starts in the in memory leaf 6707060404SJosef Bacik */ 685103e947SJosef Bacik data_ptr = (unsigned long)((char *)(di + 1) + 695103e947SJosef Bacik btrfs_dir_name_len(leaf, di)); 705103e947SJosef Bacik read_extent_buffer(leaf, buffer, data_ptr, 713acd7ee8SJosef Bacik btrfs_dir_data_len(leaf, di)); 725103e947SJosef Bacik ret = btrfs_dir_data_len(leaf, di); 735103e947SJosef Bacik 745103e947SJosef Bacik out: 755103e947SJosef Bacik btrfs_free_path(path); 765103e947SJosef Bacik return ret; 775103e947SJosef Bacik } 785103e947SJosef Bacik 79f34f57a3SYan, Zheng static int do_setxattr(struct btrfs_trans_handle *trans, 80f34f57a3SYan, Zheng struct inode *inode, const char *name, 8195819c05SChristoph Hellwig const void *value, size_t size, int flags) 825103e947SJosef Bacik { 835f5bc6b1SFilipe Manana struct btrfs_dir_item *di = NULL; 845103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 852ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 865103e947SJosef Bacik struct btrfs_path *path; 87f34f57a3SYan, Zheng size_t name_len = strlen(name); 88f34f57a3SYan, Zheng int ret = 0; 89f34f57a3SYan, Zheng 90da17066cSJeff Mahoney if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info)) 91f34f57a3SYan, Zheng return -ENOSPC; 925103e947SJosef Bacik 935103e947SJosef Bacik path = btrfs_alloc_path(); 9495819c05SChristoph Hellwig if (!path) 955103e947SJosef Bacik return -ENOMEM; 965f5bc6b1SFilipe Manana path->skip_release_on_error = 1; 975103e947SJosef Bacik 985f5bc6b1SFilipe Manana if (!value) { 99f85b7379SDavid Sterba di = btrfs_lookup_xattr(trans, root, path, 100f85b7379SDavid Sterba btrfs_ino(BTRFS_I(inode)), name, name_len, -1); 1015f5bc6b1SFilipe Manana if (!di && (flags & XATTR_REPLACE)) 1025f5bc6b1SFilipe Manana ret = -ENODATA; 1035cdf83edSFilipe Manana else if (IS_ERR(di)) 1045cdf83edSFilipe Manana ret = PTR_ERR(di); 1055f5bc6b1SFilipe Manana else if (di) 1065f5bc6b1SFilipe Manana ret = btrfs_delete_one_dir_name(trans, root, path, di); 1075103e947SJosef Bacik goto out; 1085f5bc6b1SFilipe Manana } 1095f5bc6b1SFilipe Manana 1105f5bc6b1SFilipe Manana /* 1115f5bc6b1SFilipe Manana * For a replace we can't just do the insert blindly. 1125f5bc6b1SFilipe Manana * Do a lookup first (read-only btrfs_search_slot), and return if xattr 1135f5bc6b1SFilipe Manana * doesn't exist. If it exists, fall down below to the insert/replace 1145f5bc6b1SFilipe Manana * path - we can't race with a concurrent xattr delete, because the VFS 1155f5bc6b1SFilipe Manana * locks the inode's i_mutex before calling setxattr or removexattr. 1165f5bc6b1SFilipe Manana */ 1175f5bc6b1SFilipe Manana if (flags & XATTR_REPLACE) { 1185955102cSAl Viro ASSERT(inode_is_locked(inode)); 119f85b7379SDavid Sterba di = btrfs_lookup_xattr(NULL, root, path, 120f85b7379SDavid Sterba btrfs_ino(BTRFS_I(inode)), name, name_len, 0); 1215cdf83edSFilipe Manana if (!di) 1225103e947SJosef Bacik ret = -ENODATA; 1235cdf83edSFilipe Manana else if (IS_ERR(di)) 1245cdf83edSFilipe Manana ret = PTR_ERR(di); 1255cdf83edSFilipe Manana if (ret) 1265103e947SJosef Bacik goto out; 127fa09200bSJosef Bacik btrfs_release_path(path); 1285f5bc6b1SFilipe Manana di = NULL; 12933268eafSJosef Bacik } 1305103e947SJosef Bacik 1314a0cc7caSNikolay Borisov ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)), 132f34f57a3SYan, Zheng name, name_len, value, size); 1335f5bc6b1SFilipe Manana if (ret == -EOVERFLOW) { 134ed3ee9f4SJosef Bacik /* 1355f5bc6b1SFilipe Manana * We have an existing item in a leaf, split_leaf couldn't 1365f5bc6b1SFilipe Manana * expand it. That item might have or not a dir_item that 1375f5bc6b1SFilipe Manana * matches our target xattr, so lets check. 138ed3ee9f4SJosef Bacik */ 1395f5bc6b1SFilipe Manana ret = 0; 1405f5bc6b1SFilipe Manana btrfs_assert_tree_locked(path->nodes[0]); 1412ff7e61eSJeff Mahoney di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 1425f5bc6b1SFilipe Manana if (!di && !(flags & XATTR_REPLACE)) { 1435f5bc6b1SFilipe Manana ret = -ENOSPC; 144fa09200bSJosef Bacik goto out; 1455f5bc6b1SFilipe Manana } 1465f5bc6b1SFilipe Manana } else if (ret == -EEXIST) { 1475f5bc6b1SFilipe Manana ret = 0; 1482ff7e61eSJeff Mahoney di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 1495f5bc6b1SFilipe Manana ASSERT(di); /* logic error */ 1505f5bc6b1SFilipe Manana } else if (ret) { 151fa09200bSJosef Bacik goto out; 152fa09200bSJosef Bacik } 153fa09200bSJosef Bacik 1545f5bc6b1SFilipe Manana if (di && (flags & XATTR_CREATE)) { 1555f5bc6b1SFilipe Manana ret = -EEXIST; 1565f5bc6b1SFilipe Manana goto out; 1575f5bc6b1SFilipe Manana } 1585f5bc6b1SFilipe Manana 1595f5bc6b1SFilipe Manana if (di) { 1605f5bc6b1SFilipe Manana /* 1615f5bc6b1SFilipe Manana * We're doing a replace, and it must be atomic, that is, at 1625f5bc6b1SFilipe Manana * any point in time we have either the old or the new xattr 1635f5bc6b1SFilipe Manana * value in the tree. We don't want readers (getxattr and 1645f5bc6b1SFilipe Manana * listxattrs) to miss a value, this is specially important 1655f5bc6b1SFilipe Manana * for ACLs. 1665f5bc6b1SFilipe Manana */ 1675f5bc6b1SFilipe Manana const int slot = path->slots[0]; 1685f5bc6b1SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 1695f5bc6b1SFilipe Manana const u16 old_data_len = btrfs_dir_data_len(leaf, di); 1705f5bc6b1SFilipe Manana const u32 item_size = btrfs_item_size_nr(leaf, slot); 1715f5bc6b1SFilipe Manana const u32 data_size = sizeof(*di) + name_len + size; 1725f5bc6b1SFilipe Manana struct btrfs_item *item; 1735f5bc6b1SFilipe Manana unsigned long data_ptr; 1745f5bc6b1SFilipe Manana char *ptr; 1755f5bc6b1SFilipe Manana 1765f5bc6b1SFilipe Manana if (size > old_data_len) { 1772ff7e61eSJeff Mahoney if (btrfs_leaf_free_space(fs_info, leaf) < 1785f5bc6b1SFilipe Manana (size - old_data_len)) { 1795f5bc6b1SFilipe Manana ret = -ENOSPC; 1805f5bc6b1SFilipe Manana goto out; 1815f5bc6b1SFilipe Manana } 1825f5bc6b1SFilipe Manana } 1835f5bc6b1SFilipe Manana 1845f5bc6b1SFilipe Manana if (old_data_len + name_len + sizeof(*di) == item_size) { 1855f5bc6b1SFilipe Manana /* No other xattrs packed in the same leaf item. */ 1865f5bc6b1SFilipe Manana if (size > old_data_len) 1872ff7e61eSJeff Mahoney btrfs_extend_item(fs_info, path, 1885f5bc6b1SFilipe Manana size - old_data_len); 1895f5bc6b1SFilipe Manana else if (size < old_data_len) 1902ff7e61eSJeff Mahoney btrfs_truncate_item(fs_info, path, 1912ff7e61eSJeff Mahoney data_size, 1); 1925f5bc6b1SFilipe Manana } else { 1935f5bc6b1SFilipe Manana /* There are other xattrs packed in the same item. */ 194fa09200bSJosef Bacik ret = btrfs_delete_one_dir_name(trans, root, path, di); 195fa09200bSJosef Bacik if (ret) 196fa09200bSJosef Bacik goto out; 1972ff7e61eSJeff Mahoney btrfs_extend_item(fs_info, path, data_size); 198fa09200bSJosef Bacik } 1995f5bc6b1SFilipe Manana 2005f5bc6b1SFilipe Manana item = btrfs_item_nr(slot); 2015f5bc6b1SFilipe Manana ptr = btrfs_item_ptr(leaf, slot, char); 2025f5bc6b1SFilipe Manana ptr += btrfs_item_size(leaf, item) - data_size; 2035f5bc6b1SFilipe Manana di = (struct btrfs_dir_item *)ptr; 2045f5bc6b1SFilipe Manana btrfs_set_dir_data_len(leaf, di, size); 2055f5bc6b1SFilipe Manana data_ptr = ((unsigned long)(di + 1)) + name_len; 2065f5bc6b1SFilipe Manana write_extent_buffer(leaf, value, data_ptr, size); 2075f5bc6b1SFilipe Manana btrfs_mark_buffer_dirty(leaf); 2085f5bc6b1SFilipe Manana } else { 2095f5bc6b1SFilipe Manana /* 2105f5bc6b1SFilipe Manana * Insert, and we had space for the xattr, so path->slots[0] is 2115f5bc6b1SFilipe Manana * where our xattr dir_item is and btrfs_insert_xattr_item() 2125f5bc6b1SFilipe Manana * filled it. 2135f5bc6b1SFilipe Manana */ 214fa09200bSJosef Bacik } 2155103e947SJosef Bacik out: 216f34f57a3SYan, Zheng btrfs_free_path(path); 217f34f57a3SYan, Zheng return ret; 2185103e947SJosef Bacik } 2195103e947SJosef Bacik 2204815053aSDavid Sterba /* 2214815053aSDavid Sterba * @value: "" makes the attribute to empty, NULL removes it 2224815053aSDavid Sterba */ 2237852781dSDavid Sterba int btrfs_setxattr(struct btrfs_trans_handle *trans, 224f34f57a3SYan, Zheng struct inode *inode, const char *name, 225f34f57a3SYan, Zheng const void *value, size_t size, int flags) 226f34f57a3SYan, Zheng { 227f34f57a3SYan, Zheng struct btrfs_root *root = BTRFS_I(inode)->root; 228f34f57a3SYan, Zheng int ret; 229f34f57a3SYan, Zheng 230e0d46f5cSAndreas Gruenbacher if (btrfs_root_readonly(root)) 231e0d46f5cSAndreas Gruenbacher return -EROFS; 232e0d46f5cSAndreas Gruenbacher 233f34f57a3SYan, Zheng if (trans) 234f34f57a3SYan, Zheng return do_setxattr(trans, inode, name, value, size, flags); 235f34f57a3SYan, Zheng 236a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 2); 237a22285a6SYan, Zheng if (IS_ERR(trans)) 238a22285a6SYan, Zheng return PTR_ERR(trans); 239f34f57a3SYan, Zheng 240f34f57a3SYan, Zheng ret = do_setxattr(trans, inode, name, value, size, flags); 241f34f57a3SYan, Zheng if (ret) 242f34f57a3SYan, Zheng goto out; 243f34f57a3SYan, Zheng 2440c4d2d95SJosef Bacik inode_inc_iversion(inode); 245c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 246e9976151SJosef Bacik set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); 247f34f57a3SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 248f34f57a3SYan, Zheng BUG_ON(ret); 249f34f57a3SYan, Zheng out: 2503a45bb20SJeff Mahoney btrfs_end_transaction(trans); 2515103e947SJosef Bacik return ret; 2525103e947SJosef Bacik } 2535103e947SJosef Bacik 2545103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 2555103e947SJosef Bacik { 256daac7ba6SFilipe Manana struct btrfs_key key; 2572b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 2585103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 2595103e947SJosef Bacik struct btrfs_path *path; 260daac7ba6SFilipe Manana int ret = 0; 261eaa47d86SChristoph Hellwig size_t total_size = 0, size_left = size; 2625103e947SJosef Bacik 2635103e947SJosef Bacik /* 2645103e947SJosef Bacik * ok we want all objects associated with this id. 2655103e947SJosef Bacik * NOTE: we set key.offset = 0; because we want to start with the 2665103e947SJosef Bacik * first xattr that we find and walk forward 2675103e947SJosef Bacik */ 2684a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(inode)); 269962a298fSDavid Sterba key.type = BTRFS_XATTR_ITEM_KEY; 2705103e947SJosef Bacik key.offset = 0; 2715103e947SJosef Bacik 2725103e947SJosef Bacik path = btrfs_alloc_path(); 2735103e947SJosef Bacik if (!path) 2745103e947SJosef Bacik return -ENOMEM; 275e4058b54SDavid Sterba path->reada = READA_FORWARD; 2765103e947SJosef Bacik 2775103e947SJosef Bacik /* search for our xattrs */ 2785103e947SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2795103e947SJosef Bacik if (ret < 0) 2805103e947SJosef Bacik goto err; 2812e6a0035SLi Zefan 2825103e947SJosef Bacik while (1) { 283daac7ba6SFilipe Manana struct extent_buffer *leaf; 284daac7ba6SFilipe Manana int slot; 285daac7ba6SFilipe Manana struct btrfs_dir_item *di; 286daac7ba6SFilipe Manana struct btrfs_key found_key; 287daac7ba6SFilipe Manana u32 item_size; 288daac7ba6SFilipe Manana u32 cur; 289daac7ba6SFilipe Manana 2905103e947SJosef Bacik leaf = path->nodes[0]; 2915103e947SJosef Bacik slot = path->slots[0]; 2925103e947SJosef Bacik 2935103e947SJosef Bacik /* this is where we start walking through the path */ 2942e6a0035SLi Zefan if (slot >= btrfs_header_nritems(leaf)) { 2955103e947SJosef Bacik /* 2965103e947SJosef Bacik * if we've reached the last slot in this leaf we need 2975103e947SJosef Bacik * to go to the next leaf and reset everything 2985103e947SJosef Bacik */ 2995103e947SJosef Bacik ret = btrfs_next_leaf(root, path); 3002e6a0035SLi Zefan if (ret < 0) 3012e6a0035SLi Zefan goto err; 3022e6a0035SLi Zefan else if (ret > 0) 3035103e947SJosef Bacik break; 3042e6a0035SLi Zefan continue; 3055103e947SJosef Bacik } 3065103e947SJosef Bacik 3075103e947SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 3085103e947SJosef Bacik 3095103e947SJosef Bacik /* check to make sure this item is what we want */ 3105103e947SJosef Bacik if (found_key.objectid != key.objectid) 3115103e947SJosef Bacik break; 312f1cd1f0bSFilipe Manana if (found_key.type > BTRFS_XATTR_ITEM_KEY) 3135103e947SJosef Bacik break; 314f1cd1f0bSFilipe Manana if (found_key.type < BTRFS_XATTR_ITEM_KEY) 315daac7ba6SFilipe Manana goto next_item; 3165103e947SJosef Bacik 3175103e947SJosef Bacik di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 318daac7ba6SFilipe Manana item_size = btrfs_item_size_nr(leaf, slot); 319daac7ba6SFilipe Manana cur = 0; 320daac7ba6SFilipe Manana while (cur < item_size) { 321daac7ba6SFilipe Manana u16 name_len = btrfs_dir_name_len(leaf, di); 322daac7ba6SFilipe Manana u16 data_len = btrfs_dir_data_len(leaf, di); 323daac7ba6SFilipe Manana u32 this_len = sizeof(*di) + name_len + data_len; 324daac7ba6SFilipe Manana unsigned long name_ptr = (unsigned long)(di + 1); 3255103e947SJosef Bacik 326eaa47d86SChristoph Hellwig total_size += name_len + 1; 327daac7ba6SFilipe Manana /* 328daac7ba6SFilipe Manana * We are just looking for how big our buffer needs to 329daac7ba6SFilipe Manana * be. 330daac7ba6SFilipe Manana */ 3315103e947SJosef Bacik if (!size) 3322e6a0035SLi Zefan goto next; 3335103e947SJosef Bacik 334eaa47d86SChristoph Hellwig if (!buffer || (name_len + 1) > size_left) { 3355103e947SJosef Bacik ret = -ERANGE; 336b16281c3SYehuda Sadeh Weinraub goto err; 3375103e947SJosef Bacik } 3385103e947SJosef Bacik 339eaa47d86SChristoph Hellwig read_extent_buffer(leaf, buffer, name_ptr, name_len); 340eaa47d86SChristoph Hellwig buffer[name_len] = '\0'; 341eaa47d86SChristoph Hellwig 342eaa47d86SChristoph Hellwig size_left -= name_len + 1; 343eaa47d86SChristoph Hellwig buffer += name_len + 1; 3442e6a0035SLi Zefan next: 345daac7ba6SFilipe Manana cur += this_len; 346daac7ba6SFilipe Manana di = (struct btrfs_dir_item *)((char *)di + this_len); 347daac7ba6SFilipe Manana } 348daac7ba6SFilipe Manana next_item: 3492e6a0035SLi Zefan path->slots[0]++; 3505103e947SJosef Bacik } 3515103e947SJosef Bacik ret = total_size; 3525103e947SJosef Bacik 3535103e947SJosef Bacik err: 3545103e947SJosef Bacik btrfs_free_path(path); 3555103e947SJosef Bacik 3565103e947SJosef Bacik return ret; 3575103e947SJosef Bacik } 3585103e947SJosef Bacik 3599172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_get(const struct xattr_handler *handler, 360b296821aSAl Viro struct dentry *unused, struct inode *inode, 361b296821aSAl Viro const char *name, void *buffer, size_t size) 3629172abbcSAndreas Gruenbacher { 3639172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 3647852781dSDavid Sterba return btrfs_getxattr(inode, name, buffer, size); 3659172abbcSAndreas Gruenbacher } 3669172abbcSAndreas Gruenbacher 3679172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_set(const struct xattr_handler *handler, 36859301226SAl Viro struct dentry *unused, struct inode *inode, 36959301226SAl Viro const char *name, const void *buffer, 37059301226SAl Viro size_t size, int flags) 3719172abbcSAndreas Gruenbacher { 3729172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 3737852781dSDavid Sterba return btrfs_setxattr(NULL, inode, name, buffer, size, flags); 3749172abbcSAndreas Gruenbacher } 3759172abbcSAndreas Gruenbacher 3769172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, 37759301226SAl Viro struct dentry *unused, struct inode *inode, 3789172abbcSAndreas Gruenbacher const char *name, const void *value, 3799172abbcSAndreas Gruenbacher size_t size, int flags) 3809172abbcSAndreas Gruenbacher { 3819172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 38259301226SAl Viro return btrfs_set_prop(inode, name, value, size, flags); 3839172abbcSAndreas Gruenbacher } 3849172abbcSAndreas Gruenbacher 3859172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_security_xattr_handler = { 3869172abbcSAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3879172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 3889172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 3899172abbcSAndreas Gruenbacher }; 3909172abbcSAndreas Gruenbacher 3919172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_trusted_xattr_handler = { 3929172abbcSAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3939172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 3949172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 3959172abbcSAndreas Gruenbacher }; 3969172abbcSAndreas Gruenbacher 3979172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_user_xattr_handler = { 3989172abbcSAndreas Gruenbacher .prefix = XATTR_USER_PREFIX, 3999172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 4009172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 4019172abbcSAndreas Gruenbacher }; 4029172abbcSAndreas Gruenbacher 4039172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_btrfs_xattr_handler = { 4049172abbcSAndreas Gruenbacher .prefix = XATTR_BTRFS_PREFIX, 4059172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 4069172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set_prop, 4079172abbcSAndreas Gruenbacher }; 4089172abbcSAndreas Gruenbacher 409f01cbd3fSStephen Hemminger const struct xattr_handler *btrfs_xattr_handlers[] = { 4109172abbcSAndreas Gruenbacher &btrfs_security_xattr_handler, 4110eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 412996a710dSChristoph Hellwig &posix_acl_access_xattr_handler, 413996a710dSChristoph Hellwig &posix_acl_default_xattr_handler, 41495819c05SChristoph Hellwig #endif 4159172abbcSAndreas Gruenbacher &btrfs_trusted_xattr_handler, 4169172abbcSAndreas Gruenbacher &btrfs_user_xattr_handler, 4179172abbcSAndreas Gruenbacher &btrfs_btrfs_xattr_handler, 41895819c05SChristoph Hellwig NULL, 41995819c05SChristoph Hellwig }; 42095819c05SChristoph Hellwig 42148a3b636SEric Sandeen static int btrfs_initxattrs(struct inode *inode, 42248a3b636SEric Sandeen const struct xattr *xattr_array, void *fs_info) 4239d8f13baSMimi Zohar { 4249d8f13baSMimi Zohar const struct xattr *xattr; 4259d8f13baSMimi Zohar struct btrfs_trans_handle *trans = fs_info; 426827aa18eSFilipe Manana unsigned int nofs_flag; 4279d8f13baSMimi Zohar char *name; 4289d8f13baSMimi Zohar int err = 0; 4299d8f13baSMimi Zohar 430827aa18eSFilipe Manana /* 431827aa18eSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 432827aa18eSFilipe Manana * context to avoid deadlock if reclaim happens. 433827aa18eSFilipe Manana */ 434827aa18eSFilipe Manana nofs_flag = memalloc_nofs_save(); 4359d8f13baSMimi Zohar for (xattr = xattr_array; xattr->name != NULL; xattr++) { 4369d8f13baSMimi Zohar name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 43739a27ec1SDavid Sterba strlen(xattr->name) + 1, GFP_KERNEL); 4389d8f13baSMimi Zohar if (!name) { 4399d8f13baSMimi Zohar err = -ENOMEM; 4409d8f13baSMimi Zohar break; 4419d8f13baSMimi Zohar } 4429d8f13baSMimi Zohar strcpy(name, XATTR_SECURITY_PREFIX); 4439d8f13baSMimi Zohar strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); 4447852781dSDavid Sterba err = btrfs_setxattr(trans, inode, name, xattr->value, 4457852781dSDavid Sterba xattr->value_len, 0); 4469d8f13baSMimi Zohar kfree(name); 4479d8f13baSMimi Zohar if (err < 0) 4489d8f13baSMimi Zohar break; 4499d8f13baSMimi Zohar } 450827aa18eSFilipe Manana memalloc_nofs_restore(nofs_flag); 4519d8f13baSMimi Zohar return err; 4529d8f13baSMimi Zohar } 4539d8f13baSMimi Zohar 454f34f57a3SYan, Zheng int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, 4552a7dba39SEric Paris struct inode *inode, struct inode *dir, 4562a7dba39SEric Paris const struct qstr *qstr) 4570279b4cdSJim Owens { 4589d8f13baSMimi Zohar return security_inode_init_security(inode, dir, qstr, 4599d8f13baSMimi Zohar &btrfs_initxattrs, trans); 4600279b4cdSJim Owens } 461