props.c 9.4 KB

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