^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2014 Filipe David Borba Manana <fdmanana@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/hashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "props.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define BTRFS_PROP_HANDLERS_HT_BITS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct prop_handler {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct hlist_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const char *xattr_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int (*validate)(const char *value, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int (*apply)(struct inode *inode, const char *value, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) const char *(*extract)(struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int inheritable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static const struct hlist_head *find_prop_handlers_by_hash(const u64 hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct hlist_head *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) h = &prop_handlers_ht[hash_min(hash, BTRFS_PROP_HANDLERS_HT_BITS)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (hlist_empty(h))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static const struct prop_handler *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) find_prop_handler(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const struct hlist_head *handlers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct prop_handler *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (!handlers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u64 hash = btrfs_name_hash(name, strlen(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) handlers = find_prop_handlers_by_hash(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!handlers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) hlist_for_each_entry(h, handlers, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!strcmp(h->xattr_name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int btrfs_validate_prop(const char *name, const char *value, size_t value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) const struct prop_handler *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) handler = find_prop_handler(name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (value_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return handler->validate(value, value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) const char *name, const char *value, size_t value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) const struct prop_handler *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) handler = find_prop_handler(name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (value_len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ret = btrfs_setxattr(trans, inode, handler->xattr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) NULL, 0, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ret = handler->apply(inode, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ASSERT(ret == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ret = btrfs_setxattr(trans, inode, handler->xattr_name, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) value_len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ret = handler->apply(inode, value, value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) btrfs_setxattr(trans, inode, handler->xattr_name, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 0, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int iterate_object_props(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) u64 objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) void (*iterator)(void *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) const struct prop_handler *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) const char *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) size_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) char *name_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) char *value_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int name_buf_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int value_buf_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u32 total_len, cur, this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) const struct hlist_head *handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (key.objectid != objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (key.type != BTRFS_XATTR_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) handlers = find_prop_handlers_by_hash(key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!handlers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) cur = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) total_len = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) while (cur < total_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u32 name_len = btrfs_dir_name_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) u32 data_len = btrfs_dir_data_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) unsigned long name_ptr, data_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) const struct prop_handler *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) this_len = sizeof(*di) + name_len + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) name_ptr = (unsigned long)(di + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) data_ptr = name_ptr + name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (name_len <= XATTR_BTRFS_PREFIX_LEN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) memcmp_extent_buffer(leaf, XATTR_BTRFS_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) name_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) XATTR_BTRFS_PREFIX_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) goto next_dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (name_len >= name_buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) kfree(name_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) name_buf_len = name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) name_buf = kmalloc(name_buf_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!name_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) read_extent_buffer(leaf, name_buf, name_ptr, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) name_buf[name_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) handler = find_prop_handler(name_buf, handlers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (!handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) goto next_dir_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (data_len > value_buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) kfree(value_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) value_buf_len = data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) value_buf = kmalloc(data_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!value_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) read_extent_buffer(leaf, value_buf, data_ptr, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) iterator(ctx, handler, value_buf, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) next_dir_item:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) cur += this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) di = (struct btrfs_dir_item *)((char *) di + this_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) next_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) kfree(name_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) kfree(value_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static void inode_prop_iterator(void *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) const struct prop_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) const char *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct inode *inode = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = handler->apply(inode, value, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) btrfs_warn(root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) "error applying prop %s to ino %llu (root %llu): %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) handler->xattr_name, btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) root->root_key.objectid, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) u64 ino = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ret = iterate_object_props(root, path, ino, inode_prop_iterator, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int prop_compression_validate(const char *value, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (btrfs_compress_is_valid_type(value, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static int prop_compression_apply(struct inode *inode, const char *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) BTRFS_I(inode)->prop_compress = BTRFS_COMPRESS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!strncmp("lzo", value, 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) type = BTRFS_COMPRESS_LZO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) } else if (!strncmp("zlib", value, 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) type = BTRFS_COMPRESS_ZLIB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) } else if (!strncmp("zstd", value, 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) type = BTRFS_COMPRESS_ZSTD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) BTRFS_I(inode)->prop_compress = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static const char *prop_compression_extract(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) switch (BTRFS_I(inode)->prop_compress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) case BTRFS_COMPRESS_ZLIB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) case BTRFS_COMPRESS_LZO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) case BTRFS_COMPRESS_ZSTD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return btrfs_compress_type2str(BTRFS_I(inode)->prop_compress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static struct prop_handler prop_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) .xattr_name = XATTR_BTRFS_PREFIX "compression",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .validate = prop_compression_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .apply = prop_compression_apply,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) .extract = prop_compression_extract,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) .inheritable = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static int inherit_props(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) bool need_reserve = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!test_bit(BTRFS_INODE_HAS_PROPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) &BTRFS_I(parent)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) const struct prop_handler *h = &prop_handlers[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) const char *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) u64 num_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (!h->inheritable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) value = h->extract(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * This is not strictly necessary as the property should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * valid, but in case it isn't, don't propagate it futher.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) ret = h->validate(value, strlen(value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * Currently callers should be reserving 1 item for properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * since we only have 1 property that we currently support. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * we add more in the future we need to try and reserve more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * space for them. But we should also revisit how we do space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * reservations if we do add more properties in the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (need_reserve) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ret = btrfs_block_rsv_add(root, trans->block_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) num_bytes, BTRFS_RESERVE_NO_FLUSH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ret = btrfs_setxattr(trans, inode, h->xattr_name, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) strlen(value), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ret = h->apply(inode, value, strlen(value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) btrfs_setxattr(trans, inode, h->xattr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) set_bit(BTRFS_INODE_HAS_PROPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (need_reserve) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) btrfs_block_rsv_release(fs_info, trans->block_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) num_bytes, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) need_reserve = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return inherit_props(trans, inode, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct btrfs_root *parent_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct super_block *sb = root->fs_info->sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct inode *parent_inode, *child_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) parent_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, parent_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (IS_ERR(parent_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return PTR_ERR(parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) child_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (IS_ERR(child_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) iput(parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return PTR_ERR(child_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) ret = inherit_props(trans, child_inode, parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) iput(child_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) iput(parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) void __init btrfs_props_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct prop_handler *p = &prop_handlers[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) hash_add(prop_handlers_ht, &p->node, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)