props.c 11 KB

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