props.c 9.7 KB

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