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 793e125a74SAnand Jain int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, 803e125a74SAnand Jain const char *name, 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 8904e6863bSAnand Jain ASSERT(trans); 9004e6863bSAnand Jain 91da17066cSJeff Mahoney if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info)) 92f34f57a3SYan, Zheng return -ENOSPC; 935103e947SJosef Bacik 945103e947SJosef Bacik path = btrfs_alloc_path(); 9595819c05SChristoph Hellwig if (!path) 965103e947SJosef Bacik return -ENOMEM; 975f5bc6b1SFilipe Manana path->skip_release_on_error = 1; 985103e947SJosef Bacik 995f5bc6b1SFilipe Manana if (!value) { 100f85b7379SDavid Sterba di = btrfs_lookup_xattr(trans, root, path, 101f85b7379SDavid Sterba btrfs_ino(BTRFS_I(inode)), name, name_len, -1); 1025f5bc6b1SFilipe Manana if (!di && (flags & XATTR_REPLACE)) 1035f5bc6b1SFilipe Manana ret = -ENODATA; 1045cdf83edSFilipe Manana else if (IS_ERR(di)) 1055cdf83edSFilipe Manana ret = PTR_ERR(di); 1065f5bc6b1SFilipe Manana else if (di) 1075f5bc6b1SFilipe Manana ret = btrfs_delete_one_dir_name(trans, root, path, di); 1085103e947SJosef Bacik goto out; 1095f5bc6b1SFilipe Manana } 1105f5bc6b1SFilipe Manana 1115f5bc6b1SFilipe Manana /* 1125f5bc6b1SFilipe Manana * For a replace we can't just do the insert blindly. 1135f5bc6b1SFilipe Manana * Do a lookup first (read-only btrfs_search_slot), and return if xattr 1145f5bc6b1SFilipe Manana * doesn't exist. If it exists, fall down below to the insert/replace 1155f5bc6b1SFilipe Manana * path - we can't race with a concurrent xattr delete, because the VFS 1165f5bc6b1SFilipe Manana * locks the inode's i_mutex before calling setxattr or removexattr. 1175f5bc6b1SFilipe Manana */ 1185f5bc6b1SFilipe Manana if (flags & XATTR_REPLACE) { 1195955102cSAl Viro ASSERT(inode_is_locked(inode)); 120f85b7379SDavid Sterba di = btrfs_lookup_xattr(NULL, root, path, 121f85b7379SDavid Sterba btrfs_ino(BTRFS_I(inode)), name, name_len, 0); 1225cdf83edSFilipe Manana if (!di) 1235103e947SJosef Bacik ret = -ENODATA; 1245cdf83edSFilipe Manana else if (IS_ERR(di)) 1255cdf83edSFilipe Manana ret = PTR_ERR(di); 1265cdf83edSFilipe Manana if (ret) 1275103e947SJosef Bacik goto out; 128fa09200bSJosef Bacik btrfs_release_path(path); 1295f5bc6b1SFilipe Manana di = NULL; 13033268eafSJosef Bacik } 1315103e947SJosef Bacik 1324a0cc7caSNikolay Borisov ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)), 133f34f57a3SYan, Zheng name, name_len, value, size); 1345f5bc6b1SFilipe Manana if (ret == -EOVERFLOW) { 135ed3ee9f4SJosef Bacik /* 1365f5bc6b1SFilipe Manana * We have an existing item in a leaf, split_leaf couldn't 1375f5bc6b1SFilipe Manana * expand it. That item might have or not a dir_item that 1385f5bc6b1SFilipe Manana * matches our target xattr, so lets check. 139ed3ee9f4SJosef Bacik */ 1405f5bc6b1SFilipe Manana ret = 0; 1415f5bc6b1SFilipe Manana btrfs_assert_tree_locked(path->nodes[0]); 1422ff7e61eSJeff Mahoney di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 1435f5bc6b1SFilipe Manana if (!di && !(flags & XATTR_REPLACE)) { 1445f5bc6b1SFilipe Manana ret = -ENOSPC; 145fa09200bSJosef Bacik goto out; 1465f5bc6b1SFilipe Manana } 1475f5bc6b1SFilipe Manana } else if (ret == -EEXIST) { 1485f5bc6b1SFilipe Manana ret = 0; 1492ff7e61eSJeff Mahoney di = btrfs_match_dir_item_name(fs_info, path, name, name_len); 1505f5bc6b1SFilipe Manana ASSERT(di); /* logic error */ 1515f5bc6b1SFilipe Manana } else if (ret) { 152fa09200bSJosef Bacik goto out; 153fa09200bSJosef Bacik } 154fa09200bSJosef Bacik 1555f5bc6b1SFilipe Manana if (di && (flags & XATTR_CREATE)) { 1565f5bc6b1SFilipe Manana ret = -EEXIST; 1575f5bc6b1SFilipe Manana goto out; 1585f5bc6b1SFilipe Manana } 1595f5bc6b1SFilipe Manana 1605f5bc6b1SFilipe Manana if (di) { 1615f5bc6b1SFilipe Manana /* 1625f5bc6b1SFilipe Manana * We're doing a replace, and it must be atomic, that is, at 1635f5bc6b1SFilipe Manana * any point in time we have either the old or the new xattr 1645f5bc6b1SFilipe Manana * value in the tree. We don't want readers (getxattr and 1655f5bc6b1SFilipe Manana * listxattrs) to miss a value, this is specially important 1665f5bc6b1SFilipe Manana * for ACLs. 1675f5bc6b1SFilipe Manana */ 1685f5bc6b1SFilipe Manana const int slot = path->slots[0]; 1695f5bc6b1SFilipe Manana struct extent_buffer *leaf = path->nodes[0]; 1705f5bc6b1SFilipe Manana const u16 old_data_len = btrfs_dir_data_len(leaf, di); 1715f5bc6b1SFilipe Manana const u32 item_size = btrfs_item_size_nr(leaf, slot); 1725f5bc6b1SFilipe Manana const u32 data_size = sizeof(*di) + name_len + size; 1735f5bc6b1SFilipe Manana struct btrfs_item *item; 1745f5bc6b1SFilipe Manana unsigned long data_ptr; 1755f5bc6b1SFilipe Manana char *ptr; 1765f5bc6b1SFilipe Manana 1775f5bc6b1SFilipe Manana if (size > old_data_len) { 178e902baacSDavid Sterba if (btrfs_leaf_free_space(leaf) < 1795f5bc6b1SFilipe Manana (size - old_data_len)) { 1805f5bc6b1SFilipe Manana ret = -ENOSPC; 1815f5bc6b1SFilipe Manana goto out; 1825f5bc6b1SFilipe Manana } 1835f5bc6b1SFilipe Manana } 1845f5bc6b1SFilipe Manana 1855f5bc6b1SFilipe Manana if (old_data_len + name_len + sizeof(*di) == item_size) { 1865f5bc6b1SFilipe Manana /* No other xattrs packed in the same leaf item. */ 1875f5bc6b1SFilipe Manana if (size > old_data_len) 188c71dd880SDavid Sterba btrfs_extend_item(path, size - old_data_len); 1895f5bc6b1SFilipe Manana else if (size < old_data_len) 19078ac4f9eSDavid Sterba btrfs_truncate_item(path, 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; 196c71dd880SDavid Sterba btrfs_extend_item(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 */ 222e3de9b15SAnand Jain int btrfs_setxattr_trans(struct inode *inode, const char *name, 223f34f57a3SYan, Zheng const void *value, size_t size, int flags) 224f34f57a3SYan, Zheng { 225f34f57a3SYan, Zheng struct btrfs_root *root = BTRFS_I(inode)->root; 226e3de9b15SAnand Jain struct btrfs_trans_handle *trans; 227f34f57a3SYan, Zheng int ret; 228f34f57a3SYan, Zheng 229a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 2); 230a22285a6SYan, Zheng if (IS_ERR(trans)) 231a22285a6SYan, Zheng return PTR_ERR(trans); 232f34f57a3SYan, Zheng 2332d74fa3eSAnand Jain ret = btrfs_setxattr(trans, inode, name, value, size, flags); 234f34f57a3SYan, Zheng if (ret) 235f34f57a3SYan, Zheng goto out; 236f34f57a3SYan, Zheng 2370c4d2d95SJosef Bacik inode_inc_iversion(inode); 238c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 239e9976151SJosef Bacik set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); 240f34f57a3SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 241f34f57a3SYan, Zheng BUG_ON(ret); 242f34f57a3SYan, Zheng out: 2433a45bb20SJeff Mahoney btrfs_end_transaction(trans); 2445103e947SJosef Bacik return ret; 2455103e947SJosef Bacik } 2465103e947SJosef Bacik 2475103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 2485103e947SJosef Bacik { 249daac7ba6SFilipe Manana struct btrfs_key key; 2502b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 2515103e947SJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 2525103e947SJosef Bacik struct btrfs_path *path; 253daac7ba6SFilipe Manana int ret = 0; 254eaa47d86SChristoph Hellwig size_t total_size = 0, size_left = size; 2555103e947SJosef Bacik 2565103e947SJosef Bacik /* 2575103e947SJosef Bacik * ok we want all objects associated with this id. 2585103e947SJosef Bacik * NOTE: we set key.offset = 0; because we want to start with the 2595103e947SJosef Bacik * first xattr that we find and walk forward 2605103e947SJosef Bacik */ 2614a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(inode)); 262962a298fSDavid Sterba key.type = BTRFS_XATTR_ITEM_KEY; 2635103e947SJosef Bacik key.offset = 0; 2645103e947SJosef Bacik 2655103e947SJosef Bacik path = btrfs_alloc_path(); 2665103e947SJosef Bacik if (!path) 2675103e947SJosef Bacik return -ENOMEM; 268e4058b54SDavid Sterba path->reada = READA_FORWARD; 2695103e947SJosef Bacik 2705103e947SJosef Bacik /* search for our xattrs */ 2715103e947SJosef Bacik ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2725103e947SJosef Bacik if (ret < 0) 2735103e947SJosef Bacik goto err; 2742e6a0035SLi Zefan 2755103e947SJosef Bacik while (1) { 276daac7ba6SFilipe Manana struct extent_buffer *leaf; 277daac7ba6SFilipe Manana int slot; 278daac7ba6SFilipe Manana struct btrfs_dir_item *di; 279daac7ba6SFilipe Manana struct btrfs_key found_key; 280daac7ba6SFilipe Manana u32 item_size; 281daac7ba6SFilipe Manana u32 cur; 282daac7ba6SFilipe Manana 2835103e947SJosef Bacik leaf = path->nodes[0]; 2845103e947SJosef Bacik slot = path->slots[0]; 2855103e947SJosef Bacik 2865103e947SJosef Bacik /* this is where we start walking through the path */ 2872e6a0035SLi Zefan if (slot >= btrfs_header_nritems(leaf)) { 2885103e947SJosef Bacik /* 2895103e947SJosef Bacik * if we've reached the last slot in this leaf we need 2905103e947SJosef Bacik * to go to the next leaf and reset everything 2915103e947SJosef Bacik */ 2925103e947SJosef Bacik ret = btrfs_next_leaf(root, path); 2932e6a0035SLi Zefan if (ret < 0) 2942e6a0035SLi Zefan goto err; 2952e6a0035SLi Zefan else if (ret > 0) 2965103e947SJosef Bacik break; 2972e6a0035SLi Zefan continue; 2985103e947SJosef Bacik } 2995103e947SJosef Bacik 3005103e947SJosef Bacik btrfs_item_key_to_cpu(leaf, &found_key, slot); 3015103e947SJosef Bacik 3025103e947SJosef Bacik /* check to make sure this item is what we want */ 3035103e947SJosef Bacik if (found_key.objectid != key.objectid) 3045103e947SJosef Bacik break; 305f1cd1f0bSFilipe Manana if (found_key.type > BTRFS_XATTR_ITEM_KEY) 3065103e947SJosef Bacik break; 307f1cd1f0bSFilipe Manana if (found_key.type < BTRFS_XATTR_ITEM_KEY) 308daac7ba6SFilipe Manana goto next_item; 3095103e947SJosef Bacik 3105103e947SJosef Bacik di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 311daac7ba6SFilipe Manana item_size = btrfs_item_size_nr(leaf, slot); 312daac7ba6SFilipe Manana cur = 0; 313daac7ba6SFilipe Manana while (cur < item_size) { 314daac7ba6SFilipe Manana u16 name_len = btrfs_dir_name_len(leaf, di); 315daac7ba6SFilipe Manana u16 data_len = btrfs_dir_data_len(leaf, di); 316daac7ba6SFilipe Manana u32 this_len = sizeof(*di) + name_len + data_len; 317daac7ba6SFilipe Manana unsigned long name_ptr = (unsigned long)(di + 1); 3185103e947SJosef Bacik 319eaa47d86SChristoph Hellwig total_size += name_len + 1; 320daac7ba6SFilipe Manana /* 321daac7ba6SFilipe Manana * We are just looking for how big our buffer needs to 322daac7ba6SFilipe Manana * be. 323daac7ba6SFilipe Manana */ 3245103e947SJosef Bacik if (!size) 3252e6a0035SLi Zefan goto next; 3265103e947SJosef Bacik 327eaa47d86SChristoph Hellwig if (!buffer || (name_len + 1) > size_left) { 3285103e947SJosef Bacik ret = -ERANGE; 329b16281c3SYehuda Sadeh Weinraub goto err; 3305103e947SJosef Bacik } 3315103e947SJosef Bacik 332eaa47d86SChristoph Hellwig read_extent_buffer(leaf, buffer, name_ptr, name_len); 333eaa47d86SChristoph Hellwig buffer[name_len] = '\0'; 334eaa47d86SChristoph Hellwig 335eaa47d86SChristoph Hellwig size_left -= name_len + 1; 336eaa47d86SChristoph Hellwig buffer += name_len + 1; 3372e6a0035SLi Zefan next: 338daac7ba6SFilipe Manana cur += this_len; 339daac7ba6SFilipe Manana di = (struct btrfs_dir_item *)((char *)di + this_len); 340daac7ba6SFilipe Manana } 341daac7ba6SFilipe Manana next_item: 3422e6a0035SLi Zefan path->slots[0]++; 3435103e947SJosef Bacik } 3445103e947SJosef Bacik ret = total_size; 3455103e947SJosef Bacik 3465103e947SJosef Bacik err: 3475103e947SJosef Bacik btrfs_free_path(path); 3485103e947SJosef Bacik 3495103e947SJosef Bacik return ret; 3505103e947SJosef Bacik } 3515103e947SJosef Bacik 3529172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_get(const struct xattr_handler *handler, 353b296821aSAl Viro struct dentry *unused, struct inode *inode, 354b296821aSAl Viro const char *name, void *buffer, size_t size) 3559172abbcSAndreas Gruenbacher { 3569172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 3577852781dSDavid Sterba return btrfs_getxattr(inode, name, buffer, size); 3589172abbcSAndreas Gruenbacher } 3599172abbcSAndreas Gruenbacher 3609172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_set(const struct xattr_handler *handler, 36159301226SAl Viro struct dentry *unused, struct inode *inode, 36259301226SAl Viro const char *name, const void *buffer, 36359301226SAl Viro size_t size, int flags) 3649172abbcSAndreas Gruenbacher { 3659172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 366e3de9b15SAnand Jain return btrfs_setxattr_trans(inode, name, buffer, size, flags); 3679172abbcSAndreas Gruenbacher } 3689172abbcSAndreas Gruenbacher 3699172abbcSAndreas Gruenbacher static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, 37059301226SAl Viro struct dentry *unused, struct inode *inode, 3719172abbcSAndreas Gruenbacher const char *name, const void *value, 3729172abbcSAndreas Gruenbacher size_t size, int flags) 3739172abbcSAndreas Gruenbacher { 374f22125e5SAnand Jain int ret; 375*b3f6a4beSAnand Jain struct btrfs_trans_handle *trans; 376*b3f6a4beSAnand Jain struct btrfs_root *root = BTRFS_I(inode)->root; 377f22125e5SAnand Jain 3789172abbcSAndreas Gruenbacher name = xattr_full_name(handler, name); 379f22125e5SAnand Jain ret = btrfs_validate_prop(name, value, size); 380f22125e5SAnand Jain if (ret) 381f22125e5SAnand Jain return ret; 382*b3f6a4beSAnand Jain 383*b3f6a4beSAnand Jain trans = btrfs_start_transaction(root, 2); 384*b3f6a4beSAnand Jain if (IS_ERR(trans)) 385*b3f6a4beSAnand Jain return PTR_ERR(trans); 386*b3f6a4beSAnand Jain 387*b3f6a4beSAnand Jain ret = btrfs_set_prop(trans, inode, name, value, size, flags); 388*b3f6a4beSAnand Jain if (!ret) { 389*b3f6a4beSAnand Jain inode_inc_iversion(inode); 390*b3f6a4beSAnand Jain inode->i_ctime = current_time(inode); 391*b3f6a4beSAnand Jain set_bit(BTRFS_INODE_COPY_EVERYTHING, 392*b3f6a4beSAnand Jain &BTRFS_I(inode)->runtime_flags); 393*b3f6a4beSAnand Jain ret = btrfs_update_inode(trans, root, inode); 394*b3f6a4beSAnand Jain BUG_ON(ret); 395*b3f6a4beSAnand Jain } 396*b3f6a4beSAnand Jain 397*b3f6a4beSAnand Jain btrfs_end_transaction(trans); 398*b3f6a4beSAnand Jain 399*b3f6a4beSAnand Jain return ret; 4009172abbcSAndreas Gruenbacher } 4019172abbcSAndreas Gruenbacher 4029172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_security_xattr_handler = { 4039172abbcSAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 4049172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 4059172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 4069172abbcSAndreas Gruenbacher }; 4079172abbcSAndreas Gruenbacher 4089172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_trusted_xattr_handler = { 4099172abbcSAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 4109172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 4119172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 4129172abbcSAndreas Gruenbacher }; 4139172abbcSAndreas Gruenbacher 4149172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_user_xattr_handler = { 4159172abbcSAndreas Gruenbacher .prefix = XATTR_USER_PREFIX, 4169172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 4179172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set, 4189172abbcSAndreas Gruenbacher }; 4199172abbcSAndreas Gruenbacher 4209172abbcSAndreas Gruenbacher static const struct xattr_handler btrfs_btrfs_xattr_handler = { 4219172abbcSAndreas Gruenbacher .prefix = XATTR_BTRFS_PREFIX, 4229172abbcSAndreas Gruenbacher .get = btrfs_xattr_handler_get, 4239172abbcSAndreas Gruenbacher .set = btrfs_xattr_handler_set_prop, 4249172abbcSAndreas Gruenbacher }; 4259172abbcSAndreas Gruenbacher 426f01cbd3fSStephen Hemminger const struct xattr_handler *btrfs_xattr_handlers[] = { 4279172abbcSAndreas Gruenbacher &btrfs_security_xattr_handler, 4280eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 429996a710dSChristoph Hellwig &posix_acl_access_xattr_handler, 430996a710dSChristoph Hellwig &posix_acl_default_xattr_handler, 43195819c05SChristoph Hellwig #endif 4329172abbcSAndreas Gruenbacher &btrfs_trusted_xattr_handler, 4339172abbcSAndreas Gruenbacher &btrfs_user_xattr_handler, 4349172abbcSAndreas Gruenbacher &btrfs_btrfs_xattr_handler, 43595819c05SChristoph Hellwig NULL, 43695819c05SChristoph Hellwig }; 43795819c05SChristoph Hellwig 43848a3b636SEric Sandeen static int btrfs_initxattrs(struct inode *inode, 439419a6f30SAnand Jain const struct xattr *xattr_array, void *fs_private) 4409d8f13baSMimi Zohar { 441419a6f30SAnand Jain struct btrfs_trans_handle *trans = fs_private; 4429d8f13baSMimi Zohar const struct xattr *xattr; 443827aa18eSFilipe Manana unsigned int nofs_flag; 4449d8f13baSMimi Zohar char *name; 4459d8f13baSMimi Zohar int err = 0; 4469d8f13baSMimi Zohar 447827aa18eSFilipe Manana /* 448827aa18eSFilipe Manana * We're holding a transaction handle, so use a NOFS memory allocation 449827aa18eSFilipe Manana * context to avoid deadlock if reclaim happens. 450827aa18eSFilipe Manana */ 451827aa18eSFilipe Manana nofs_flag = memalloc_nofs_save(); 4529d8f13baSMimi Zohar for (xattr = xattr_array; xattr->name != NULL; xattr++) { 4539d8f13baSMimi Zohar name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 45439a27ec1SDavid Sterba strlen(xattr->name) + 1, GFP_KERNEL); 4559d8f13baSMimi Zohar if (!name) { 4569d8f13baSMimi Zohar err = -ENOMEM; 4579d8f13baSMimi Zohar break; 4589d8f13baSMimi Zohar } 4599d8f13baSMimi Zohar strcpy(name, XATTR_SECURITY_PREFIX); 4609d8f13baSMimi Zohar strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); 46104e6863bSAnand Jain err = btrfs_setxattr(trans, inode, name, xattr->value, 4627852781dSDavid Sterba xattr->value_len, 0); 4639d8f13baSMimi Zohar kfree(name); 4649d8f13baSMimi Zohar if (err < 0) 4659d8f13baSMimi Zohar break; 4669d8f13baSMimi Zohar } 467827aa18eSFilipe Manana memalloc_nofs_restore(nofs_flag); 4689d8f13baSMimi Zohar return err; 4699d8f13baSMimi Zohar } 4709d8f13baSMimi Zohar 471f34f57a3SYan, Zheng int btrfs_xattr_security_init(struct btrfs_trans_handle *trans, 4722a7dba39SEric Paris struct inode *inode, struct inode *dir, 4732a7dba39SEric Paris const struct qstr *qstr) 4740279b4cdSJim Owens { 4759d8f13baSMimi Zohar return security_inode_init_security(inode, dir, qstr, 4769d8f13baSMimi Zohar &btrfs_initxattrs, trans); 4770279b4cdSJim Owens } 478