props.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. struct btrfs_fs_info *fs_info = root->fs_info;
  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 (verify_dir_item(fs_info, leaf,
  182. path->slots[0], di)) {
  183. ret = -EIO;
  184. goto out;
  185. }
  186. if (name_len <= XATTR_BTRFS_PREFIX_LEN ||
  187. memcmp_extent_buffer(leaf, XATTR_BTRFS_PREFIX,
  188. name_ptr,
  189. XATTR_BTRFS_PREFIX_LEN))
  190. goto next_dir_item;
  191. if (name_len >= name_buf_len) {
  192. kfree(name_buf);
  193. name_buf_len = name_len + 1;
  194. name_buf = kmalloc(name_buf_len, GFP_NOFS);
  195. if (!name_buf) {
  196. ret = -ENOMEM;
  197. goto out;
  198. }
  199. }
  200. read_extent_buffer(leaf, name_buf, name_ptr, name_len);
  201. name_buf[name_len] = '\0';
  202. handler = find_prop_handler(name_buf, handlers);
  203. if (!handler)
  204. goto next_dir_item;
  205. if (data_len > value_buf_len) {
  206. kfree(value_buf);
  207. value_buf_len = data_len;
  208. value_buf = kmalloc(data_len, GFP_NOFS);
  209. if (!value_buf) {
  210. ret = -ENOMEM;
  211. goto out;
  212. }
  213. }
  214. read_extent_buffer(leaf, value_buf, data_ptr, data_len);
  215. iterator(ctx, handler, value_buf, data_len);
  216. next_dir_item:
  217. cur += this_len;
  218. di = (struct btrfs_dir_item *)((char *) di + this_len);
  219. }
  220. next_slot:
  221. path->slots[0]++;
  222. }
  223. ret = 0;
  224. out:
  225. btrfs_release_path(path);
  226. kfree(name_buf);
  227. kfree(value_buf);
  228. return ret;
  229. }
  230. static void inode_prop_iterator(void *ctx,
  231. const struct prop_handler *handler,
  232. const char *value,
  233. size_t len)
  234. {
  235. struct inode *inode = ctx;
  236. struct btrfs_root *root = BTRFS_I(inode)->root;
  237. int ret;
  238. ret = handler->apply(inode, value, len);
  239. if (unlikely(ret))
  240. btrfs_warn(root->fs_info,
  241. "error applying prop %s to ino %llu (root %llu): %d",
  242. handler->xattr_name, btrfs_ino(BTRFS_I(inode)),
  243. root->root_key.objectid, ret);
  244. else
  245. set_bit(BTRFS_INODE_HAS_PROPS, &BTRFS_I(inode)->runtime_flags);
  246. }
  247. int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path)
  248. {
  249. struct btrfs_root *root = BTRFS_I(inode)->root;
  250. u64 ino = btrfs_ino(BTRFS_I(inode));
  251. int ret;
  252. ret = iterate_object_props(root, path, ino, inode_prop_iterator, inode);
  253. return ret;
  254. }
  255. static int inherit_props(struct btrfs_trans_handle *trans,
  256. struct inode *inode,
  257. struct inode *parent)
  258. {
  259. struct btrfs_root *root = BTRFS_I(inode)->root;
  260. struct btrfs_fs_info *fs_info = root->fs_info;
  261. int ret;
  262. int i;
  263. if (!test_bit(BTRFS_INODE_HAS_PROPS,
  264. &BTRFS_I(parent)->runtime_flags))
  265. return 0;
  266. for (i = 0; i < ARRAY_SIZE(prop_handlers); i++) {
  267. const struct prop_handler *h = &prop_handlers[i];
  268. const char *value;
  269. u64 num_bytes;
  270. if (!h->inheritable)
  271. continue;
  272. value = h->extract(parent);
  273. if (!value)
  274. continue;
  275. num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
  276. ret = btrfs_block_rsv_add(root, trans->block_rsv,
  277. num_bytes, BTRFS_RESERVE_NO_FLUSH);
  278. if (ret)
  279. goto out;
  280. ret = __btrfs_set_prop(trans, inode, h->xattr_name,
  281. value, strlen(value), 0);
  282. btrfs_block_rsv_release(fs_info, trans->block_rsv, num_bytes);
  283. if (ret)
  284. goto out;
  285. }
  286. ret = 0;
  287. out:
  288. return ret;
  289. }
  290. int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
  291. struct inode *inode,
  292. struct inode *dir)
  293. {
  294. if (!dir)
  295. return 0;
  296. return inherit_props(trans, inode, dir);
  297. }
  298. int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
  299. struct btrfs_root *root,
  300. struct btrfs_root *parent_root)
  301. {
  302. struct super_block *sb = root->fs_info->sb;
  303. struct btrfs_key key;
  304. struct inode *parent_inode, *child_inode;
  305. int ret;
  306. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  307. key.type = BTRFS_INODE_ITEM_KEY;
  308. key.offset = 0;
  309. parent_inode = btrfs_iget(sb, &key, parent_root, NULL);
  310. if (IS_ERR(parent_inode))
  311. return PTR_ERR(parent_inode);
  312. child_inode = btrfs_iget(sb, &key, root, NULL);
  313. if (IS_ERR(child_inode)) {
  314. iput(parent_inode);
  315. return PTR_ERR(child_inode);
  316. }
  317. ret = inherit_props(trans, child_inode, parent_inode);
  318. iput(child_inode);
  319. iput(parent_inode);
  320. return ret;
  321. }
  322. static int prop_compression_validate(const char *value, size_t len)
  323. {
  324. if (!strncmp("lzo", value, len))
  325. return 0;
  326. else if (!strncmp("zlib", value, len))
  327. return 0;
  328. return -EINVAL;
  329. }
  330. static int prop_compression_apply(struct inode *inode,
  331. const char *value,
  332. size_t len)
  333. {
  334. int type;
  335. if (len == 0) {
  336. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  337. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  338. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  339. return 0;
  340. }
  341. if (!strncmp("lzo", value, len))
  342. type = BTRFS_COMPRESS_LZO;
  343. else if (!strncmp("zlib", value, len))
  344. type = BTRFS_COMPRESS_ZLIB;
  345. else
  346. return -EINVAL;
  347. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  348. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  349. BTRFS_I(inode)->force_compress = type;
  350. return 0;
  351. }
  352. static const char *prop_compression_extract(struct inode *inode)
  353. {
  354. switch (BTRFS_I(inode)->force_compress) {
  355. case BTRFS_COMPRESS_ZLIB:
  356. return "zlib";
  357. case BTRFS_COMPRESS_LZO:
  358. return "lzo";
  359. }
  360. return NULL;
  361. }