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