props.c 9.7 KB

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