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. #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. .xattr_name = NULL
  49. }
  50. };
  51. void __init btrfs_props_init(void)
  52. {
  53. struct prop_handler *p;
  54. hash_init(prop_handlers_ht);
  55. for (p = &prop_handlers[0]; p->xattr_name; p++) {
  56. u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
  57. hash_add(prop_handlers_ht, &p->node, h);
  58. }
  59. }
  60. static const struct hlist_head *find_prop_handlers_by_hash(const u64 hash)
  61. {
  62. struct hlist_head *h;
  63. h = &prop_handlers_ht[hash_min(hash, BTRFS_PROP_HANDLERS_HT_BITS)];
  64. if (hlist_empty(h))
  65. return NULL;
  66. return h;
  67. }
  68. static const struct prop_handler *
  69. find_prop_handler(const char *name,
  70. const struct hlist_head *handlers)
  71. {
  72. struct prop_handler *h;
  73. if (!handlers) {
  74. u64 hash = btrfs_name_hash(name, strlen(name));
  75. handlers = find_prop_handlers_by_hash(hash);
  76. if (!handlers)
  77. return NULL;
  78. }
  79. hlist_for_each_entry(h, handlers, node)
  80. if (!strcmp(h->xattr_name, name))
  81. return h;
  82. return NULL;
  83. }
  84. static int __btrfs_set_prop(struct btrfs_trans_handle *trans,
  85. struct inode *inode,
  86. const char *name,
  87. const char *value,
  88. size_t value_len,
  89. int flags)
  90. {
  91. const struct prop_handler *handler;
  92. int ret;
  93. if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
  94. return -EINVAL;
  95. handler = find_prop_handler(name, NULL);
  96. if (!handler)
  97. return -EINVAL;
  98. if (value_len == 0) {
  99. ret = __btrfs_setxattr(trans, inode, handler->xattr_name,
  100. NULL, 0, flags);
  101. if (ret)
  102. return ret;
  103. ret = handler->apply(inode, NULL, 0);
  104. ASSERT(ret == 0);
  105. return ret;
  106. }
  107. ret = handler->validate(value, value_len);
  108. if (ret)
  109. return ret;
  110. ret = __btrfs_setxattr(trans, inode, handler->xattr_name,
  111. value, value_len, flags);
  112. if (ret)
  113. return ret;
  114. ret = handler->apply(inode, value, value_len);
  115. if (ret) {
  116. __btrfs_setxattr(trans, inode, handler->xattr_name,
  117. NULL, 0, flags);
  118. return ret;
  119. }
  120. set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
  121. return 0;
  122. }
  123. int btrfs_set_prop(struct inode *inode,
  124. const char *name,
  125. const char *value,
  126. size_t value_len,
  127. int flags)
  128. {
  129. return __btrfs_set_prop(NULL, inode, name, value, value_len, flags);
  130. }
  131. static int iterate_object_props(struct btrfs_root *root,
  132. struct btrfs_path *path,
  133. u64 objectid,
  134. void (*iterator)(void *,
  135. const struct prop_handler *,
  136. const char *,
  137. size_t),
  138. void *ctx)
  139. {
  140. int ret;
  141. char *name_buf = NULL;
  142. char *value_buf = NULL;
  143. int name_buf_len = 0;
  144. int value_buf_len = 0;
  145. while (1) {
  146. struct btrfs_key key;
  147. struct btrfs_dir_item *di;
  148. struct extent_buffer *leaf;
  149. u32 total_len, cur, this_len;
  150. int slot;
  151. const struct hlist_head *handlers;
  152. slot = path->slots[0];
  153. leaf = path->nodes[0];
  154. if (slot >= btrfs_header_nritems(leaf)) {
  155. ret = btrfs_next_leaf(root, path);
  156. if (ret < 0)
  157. goto out;
  158. else if (ret > 0)
  159. break;
  160. continue;
  161. }
  162. btrfs_item_key_to_cpu(leaf, &key, slot);
  163. if (key.objectid != objectid)
  164. break;
  165. if (key.type != BTRFS_XATTR_ITEM_KEY)
  166. break;
  167. handlers = find_prop_handlers_by_hash(key.offset);
  168. if (!handlers)
  169. goto next_slot;
  170. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  171. cur = 0;
  172. total_len = btrfs_item_size_nr(leaf, slot);
  173. while (cur < total_len) {
  174. u32 name_len = btrfs_dir_name_len(leaf, di);
  175. u32 data_len = btrfs_dir_data_len(leaf, di);
  176. unsigned long name_ptr, data_ptr;
  177. const struct prop_handler *handler;
  178. this_len = sizeof(*di) + name_len + data_len;
  179. name_ptr = (unsigned long)(di + 1);
  180. data_ptr = name_ptr + name_len;
  181. if (name_len <= XATTR_BTRFS_PREFIX_LEN ||
  182. memcmp_extent_buffer(leaf, XATTR_BTRFS_PREFIX,
  183. name_ptr,
  184. XATTR_BTRFS_PREFIX_LEN))
  185. goto next_dir_item;
  186. if (name_len >= name_buf_len) {
  187. kfree(name_buf);
  188. name_buf_len = name_len + 1;
  189. name_buf = kmalloc(name_buf_len, GFP_NOFS);
  190. if (!name_buf) {
  191. ret = -ENOMEM;
  192. goto out;
  193. }
  194. }
  195. read_extent_buffer(leaf, name_buf, name_ptr, name_len);
  196. name_buf[name_len] = '\0';
  197. handler = find_prop_handler(name_buf, handlers);
  198. if (!handler)
  199. goto next_dir_item;
  200. if (data_len > value_buf_len) {
  201. kfree(value_buf);
  202. value_buf_len = data_len;
  203. value_buf = kmalloc(data_len, GFP_NOFS);
  204. if (!value_buf) {
  205. ret = -ENOMEM;
  206. goto out;
  207. }
  208. }
  209. read_extent_buffer(leaf, value_buf, data_ptr, data_len);
  210. iterator(ctx, handler, value_buf, data_len);
  211. next_dir_item:
  212. cur += this_len;
  213. di = (struct btrfs_dir_item *)((char *) di + this_len);
  214. }
  215. next_slot:
  216. path->slots[0]++;
  217. }
  218. ret = 0;
  219. out:
  220. btrfs_release_path(path);
  221. kfree(name_buf);
  222. kfree(value_buf);
  223. return ret;
  224. }
  225. static void inode_prop_iterator(void *ctx,
  226. const struct prop_handler *handler,
  227. const char *value,
  228. size_t len)
  229. {
  230. struct inode *inode = ctx;
  231. struct btrfs_root *root = BTRFS_I(inode)->root;
  232. int ret;
  233. ret = handler->apply(inode, value, len);
  234. if (unlikely(ret))
  235. btrfs_warn(root->fs_info,
  236. "error applying prop %s to ino %llu (root %llu): %d",
  237. handler->xattr_name, btrfs_ino(inode),
  238. root->root_key.objectid, ret);
  239. else
  240. set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
  241. }
  242. int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path)
  243. {
  244. struct btrfs_root *root = BTRFS_I(inode)->root;
  245. u64 ino = btrfs_ino(inode);
  246. int ret;
  247. ret = iterate_object_props(root, path, ino, inode_prop_iterator, inode);
  248. return ret;
  249. }
  250. static int inherit_props(struct btrfs_trans_handle *trans,
  251. struct inode *inode,
  252. struct inode *parent)
  253. {
  254. const struct prop_handler *h;
  255. struct btrfs_root *root = BTRFS_I(inode)->root;
  256. int ret;
  257. if (!test_bit(BTRFS_INODE_HAS_PROPS,
  258. &BTRFS_I(parent)->runtime_flags))
  259. return 0;
  260. for (h = &prop_handlers[0]; h->xattr_name; h++) {
  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 btrfs_key key;
  296. struct inode *parent_inode, *child_inode;
  297. int ret;
  298. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  299. key.type = BTRFS_INODE_ITEM_KEY;
  300. key.offset = 0;
  301. parent_inode = btrfs_iget(parent_root->fs_info->sb, &key,
  302. parent_root, NULL);
  303. if (IS_ERR(parent_inode))
  304. return PTR_ERR(parent_inode);
  305. child_inode = btrfs_iget(root->fs_info->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. }