props.c 10.0 KB

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