backref.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. /*
  2. * Copyright (C) 2011 STRATO. All rights reserved.
  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 "ctree.h"
  19. #include "disk-io.h"
  20. #include "backref.h"
  21. #include "ulist.h"
  22. #include "transaction.h"
  23. #include "delayed-ref.h"
  24. #include "locking.h"
  25. struct extent_inode_elem {
  26. u64 inum;
  27. u64 offset;
  28. struct extent_inode_elem *next;
  29. };
  30. static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb,
  31. struct btrfs_file_extent_item *fi,
  32. u64 extent_item_pos,
  33. struct extent_inode_elem **eie)
  34. {
  35. u64 data_offset;
  36. u64 data_len;
  37. struct extent_inode_elem *e;
  38. data_offset = btrfs_file_extent_offset(eb, fi);
  39. data_len = btrfs_file_extent_num_bytes(eb, fi);
  40. if (extent_item_pos < data_offset ||
  41. extent_item_pos >= data_offset + data_len)
  42. return 1;
  43. e = kmalloc(sizeof(*e), GFP_NOFS);
  44. if (!e)
  45. return -ENOMEM;
  46. e->next = *eie;
  47. e->inum = key->objectid;
  48. e->offset = key->offset + (extent_item_pos - data_offset);
  49. *eie = e;
  50. return 0;
  51. }
  52. static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte,
  53. u64 extent_item_pos,
  54. struct extent_inode_elem **eie)
  55. {
  56. u64 disk_byte;
  57. struct btrfs_key key;
  58. struct btrfs_file_extent_item *fi;
  59. int slot;
  60. int nritems;
  61. int extent_type;
  62. int ret;
  63. /*
  64. * from the shared data ref, we only have the leaf but we need
  65. * the key. thus, we must look into all items and see that we
  66. * find one (some) with a reference to our extent item.
  67. */
  68. nritems = btrfs_header_nritems(eb);
  69. for (slot = 0; slot < nritems; ++slot) {
  70. btrfs_item_key_to_cpu(eb, &key, slot);
  71. if (key.type != BTRFS_EXTENT_DATA_KEY)
  72. continue;
  73. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  74. extent_type = btrfs_file_extent_type(eb, fi);
  75. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  76. continue;
  77. /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
  78. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  79. if (disk_byte != wanted_disk_byte)
  80. continue;
  81. ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
  82. if (ret < 0)
  83. return ret;
  84. }
  85. return 0;
  86. }
  87. /*
  88. * this structure records all encountered refs on the way up to the root
  89. */
  90. struct __prelim_ref {
  91. struct list_head list;
  92. u64 root_id;
  93. struct btrfs_key key_for_search;
  94. int level;
  95. int count;
  96. struct extent_inode_elem *inode_list;
  97. u64 parent;
  98. u64 wanted_disk_byte;
  99. };
  100. /*
  101. * the rules for all callers of this function are:
  102. * - obtaining the parent is the goal
  103. * - if you add a key, you must know that it is a correct key
  104. * - if you cannot add the parent or a correct key, then we will look into the
  105. * block later to set a correct key
  106. *
  107. * delayed refs
  108. * ============
  109. * backref type | shared | indirect | shared | indirect
  110. * information | tree | tree | data | data
  111. * --------------------+--------+----------+--------+----------
  112. * parent logical | y | - | - | -
  113. * key to resolve | - | y | y | y
  114. * tree block logical | - | - | - | -
  115. * root for resolving | y | y | y | y
  116. *
  117. * - column 1: we've the parent -> done
  118. * - column 2, 3, 4: we use the key to find the parent
  119. *
  120. * on disk refs (inline or keyed)
  121. * ==============================
  122. * backref type | shared | indirect | shared | indirect
  123. * information | tree | tree | data | data
  124. * --------------------+--------+----------+--------+----------
  125. * parent logical | y | - | y | -
  126. * key to resolve | - | - | - | y
  127. * tree block logical | y | y | y | y
  128. * root for resolving | - | y | y | y
  129. *
  130. * - column 1, 3: we've the parent -> done
  131. * - column 2: we take the first key from the block to find the parent
  132. * (see __add_missing_keys)
  133. * - column 4: we use the key to find the parent
  134. *
  135. * additional information that's available but not required to find the parent
  136. * block might help in merging entries to gain some speed.
  137. */
  138. static int __add_prelim_ref(struct list_head *head, u64 root_id,
  139. struct btrfs_key *key, int level,
  140. u64 parent, u64 wanted_disk_byte, int count)
  141. {
  142. struct __prelim_ref *ref;
  143. /* in case we're adding delayed refs, we're holding the refs spinlock */
  144. ref = kmalloc(sizeof(*ref), GFP_ATOMIC);
  145. if (!ref)
  146. return -ENOMEM;
  147. ref->root_id = root_id;
  148. if (key)
  149. ref->key_for_search = *key;
  150. else
  151. memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
  152. ref->inode_list = NULL;
  153. ref->level = level;
  154. ref->count = count;
  155. ref->parent = parent;
  156. ref->wanted_disk_byte = wanted_disk_byte;
  157. list_add_tail(&ref->list, head);
  158. return 0;
  159. }
  160. static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
  161. struct ulist *parents, int level,
  162. struct btrfs_key *key, u64 wanted_disk_byte,
  163. const u64 *extent_item_pos)
  164. {
  165. int ret;
  166. int slot = path->slots[level];
  167. struct extent_buffer *eb = path->nodes[level];
  168. struct btrfs_file_extent_item *fi;
  169. struct extent_inode_elem *eie = NULL;
  170. u64 disk_byte;
  171. u64 wanted_objectid = key->objectid;
  172. add_parent:
  173. if (level == 0 && extent_item_pos) {
  174. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  175. ret = check_extent_in_eb(key, eb, fi, *extent_item_pos, &eie);
  176. if (ret < 0)
  177. return ret;
  178. }
  179. ret = ulist_add(parents, eb->start, (unsigned long)eie, GFP_NOFS);
  180. if (ret < 0)
  181. return ret;
  182. if (level != 0)
  183. return 0;
  184. /*
  185. * if the current leaf is full with EXTENT_DATA items, we must
  186. * check the next one if that holds a reference as well.
  187. * ref->count cannot be used to skip this check.
  188. * repeat this until we don't find any additional EXTENT_DATA items.
  189. */
  190. while (1) {
  191. eie = NULL;
  192. ret = btrfs_next_leaf(root, path);
  193. if (ret < 0)
  194. return ret;
  195. if (ret)
  196. return 0;
  197. eb = path->nodes[0];
  198. for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) {
  199. btrfs_item_key_to_cpu(eb, key, slot);
  200. if (key->objectid != wanted_objectid ||
  201. key->type != BTRFS_EXTENT_DATA_KEY)
  202. return 0;
  203. fi = btrfs_item_ptr(eb, slot,
  204. struct btrfs_file_extent_item);
  205. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  206. if (disk_byte == wanted_disk_byte)
  207. goto add_parent;
  208. }
  209. }
  210. return 0;
  211. }
  212. /*
  213. * resolve an indirect backref in the form (root_id, key, level)
  214. * to a logical address
  215. */
  216. static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
  217. int search_commit_root,
  218. u64 time_seq,
  219. struct __prelim_ref *ref,
  220. struct ulist *parents,
  221. const u64 *extent_item_pos)
  222. {
  223. struct btrfs_path *path;
  224. struct btrfs_root *root;
  225. struct btrfs_key root_key;
  226. struct btrfs_key key = {0};
  227. struct extent_buffer *eb;
  228. int ret = 0;
  229. int root_level;
  230. int level = ref->level;
  231. path = btrfs_alloc_path();
  232. if (!path)
  233. return -ENOMEM;
  234. path->search_commit_root = !!search_commit_root;
  235. root_key.objectid = ref->root_id;
  236. root_key.type = BTRFS_ROOT_ITEM_KEY;
  237. root_key.offset = (u64)-1;
  238. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  239. if (IS_ERR(root)) {
  240. ret = PTR_ERR(root);
  241. goto out;
  242. }
  243. rcu_read_lock();
  244. root_level = btrfs_header_level(root->node);
  245. rcu_read_unlock();
  246. if (root_level + 1 == level)
  247. goto out;
  248. path->lowest_level = level;
  249. ret = btrfs_search_old_slot(root, &ref->key_for_search, path, time_seq);
  250. pr_debug("search slot in root %llu (level %d, ref count %d) returned "
  251. "%d for key (%llu %u %llu)\n",
  252. (unsigned long long)ref->root_id, level, ref->count, ret,
  253. (unsigned long long)ref->key_for_search.objectid,
  254. ref->key_for_search.type,
  255. (unsigned long long)ref->key_for_search.offset);
  256. if (ret < 0)
  257. goto out;
  258. eb = path->nodes[level];
  259. if (!eb) {
  260. WARN_ON(1);
  261. ret = 1;
  262. goto out;
  263. }
  264. if (level == 0)
  265. btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
  266. ret = add_all_parents(root, path, parents, level, &key,
  267. ref->wanted_disk_byte, extent_item_pos);
  268. out:
  269. btrfs_free_path(path);
  270. return ret;
  271. }
  272. /*
  273. * resolve all indirect backrefs from the list
  274. */
  275. static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
  276. int search_commit_root, u64 time_seq,
  277. struct list_head *head,
  278. const u64 *extent_item_pos)
  279. {
  280. int err;
  281. int ret = 0;
  282. struct __prelim_ref *ref;
  283. struct __prelim_ref *ref_safe;
  284. struct __prelim_ref *new_ref;
  285. struct ulist *parents;
  286. struct ulist_node *node;
  287. struct ulist_iterator uiter;
  288. parents = ulist_alloc(GFP_NOFS);
  289. if (!parents)
  290. return -ENOMEM;
  291. /*
  292. * _safe allows us to insert directly after the current item without
  293. * iterating over the newly inserted items.
  294. * we're also allowed to re-assign ref during iteration.
  295. */
  296. list_for_each_entry_safe(ref, ref_safe, head, list) {
  297. if (ref->parent) /* already direct */
  298. continue;
  299. if (ref->count == 0)
  300. continue;
  301. err = __resolve_indirect_ref(fs_info, search_commit_root,
  302. time_seq, ref, parents,
  303. extent_item_pos);
  304. if (err) {
  305. if (ret == 0)
  306. ret = err;
  307. continue;
  308. }
  309. /* we put the first parent into the ref at hand */
  310. ULIST_ITER_INIT(&uiter);
  311. node = ulist_next(parents, &uiter);
  312. ref->parent = node ? node->val : 0;
  313. ref->inode_list =
  314. node ? (struct extent_inode_elem *)node->aux : 0;
  315. /* additional parents require new refs being added here */
  316. while ((node = ulist_next(parents, &uiter))) {
  317. new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS);
  318. if (!new_ref) {
  319. ret = -ENOMEM;
  320. break;
  321. }
  322. memcpy(new_ref, ref, sizeof(*ref));
  323. new_ref->parent = node->val;
  324. new_ref->inode_list =
  325. (struct extent_inode_elem *)node->aux;
  326. list_add(&new_ref->list, &ref->list);
  327. }
  328. ulist_reinit(parents);
  329. }
  330. ulist_free(parents);
  331. return ret;
  332. }
  333. static inline int ref_for_same_block(struct __prelim_ref *ref1,
  334. struct __prelim_ref *ref2)
  335. {
  336. if (ref1->level != ref2->level)
  337. return 0;
  338. if (ref1->root_id != ref2->root_id)
  339. return 0;
  340. if (ref1->key_for_search.type != ref2->key_for_search.type)
  341. return 0;
  342. if (ref1->key_for_search.objectid != ref2->key_for_search.objectid)
  343. return 0;
  344. if (ref1->key_for_search.offset != ref2->key_for_search.offset)
  345. return 0;
  346. if (ref1->parent != ref2->parent)
  347. return 0;
  348. return 1;
  349. }
  350. /*
  351. * read tree blocks and add keys where required.
  352. */
  353. static int __add_missing_keys(struct btrfs_fs_info *fs_info,
  354. struct list_head *head)
  355. {
  356. struct list_head *pos;
  357. struct extent_buffer *eb;
  358. list_for_each(pos, head) {
  359. struct __prelim_ref *ref;
  360. ref = list_entry(pos, struct __prelim_ref, list);
  361. if (ref->parent)
  362. continue;
  363. if (ref->key_for_search.type)
  364. continue;
  365. BUG_ON(!ref->wanted_disk_byte);
  366. eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte,
  367. fs_info->tree_root->leafsize, 0);
  368. BUG_ON(!eb);
  369. btrfs_tree_read_lock(eb);
  370. if (btrfs_header_level(eb) == 0)
  371. btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
  372. else
  373. btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
  374. btrfs_tree_read_unlock(eb);
  375. free_extent_buffer(eb);
  376. }
  377. return 0;
  378. }
  379. /*
  380. * merge two lists of backrefs and adjust counts accordingly
  381. *
  382. * mode = 1: merge identical keys, if key is set
  383. * FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
  384. * additionally, we could even add a key range for the blocks we
  385. * looked into to merge even more (-> replace unresolved refs by those
  386. * having a parent).
  387. * mode = 2: merge identical parents
  388. */
  389. static int __merge_refs(struct list_head *head, int mode)
  390. {
  391. struct list_head *pos1;
  392. list_for_each(pos1, head) {
  393. struct list_head *n2;
  394. struct list_head *pos2;
  395. struct __prelim_ref *ref1;
  396. ref1 = list_entry(pos1, struct __prelim_ref, list);
  397. for (pos2 = pos1->next, n2 = pos2->next; pos2 != head;
  398. pos2 = n2, n2 = pos2->next) {
  399. struct __prelim_ref *ref2;
  400. struct __prelim_ref *xchg;
  401. ref2 = list_entry(pos2, struct __prelim_ref, list);
  402. if (mode == 1) {
  403. if (!ref_for_same_block(ref1, ref2))
  404. continue;
  405. if (!ref1->parent && ref2->parent) {
  406. xchg = ref1;
  407. ref1 = ref2;
  408. ref2 = xchg;
  409. }
  410. ref1->count += ref2->count;
  411. } else {
  412. if (ref1->parent != ref2->parent)
  413. continue;
  414. ref1->count += ref2->count;
  415. }
  416. list_del(&ref2->list);
  417. kfree(ref2);
  418. }
  419. }
  420. return 0;
  421. }
  422. /*
  423. * add all currently queued delayed refs from this head whose seq nr is
  424. * smaller or equal that seq to the list
  425. */
  426. static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
  427. struct list_head *prefs)
  428. {
  429. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  430. struct rb_node *n = &head->node.rb_node;
  431. struct btrfs_key key;
  432. struct btrfs_key op_key = {0};
  433. int sgn;
  434. int ret = 0;
  435. if (extent_op && extent_op->update_key)
  436. btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
  437. while ((n = rb_prev(n))) {
  438. struct btrfs_delayed_ref_node *node;
  439. node = rb_entry(n, struct btrfs_delayed_ref_node,
  440. rb_node);
  441. if (node->bytenr != head->node.bytenr)
  442. break;
  443. WARN_ON(node->is_head);
  444. if (node->seq > seq)
  445. continue;
  446. switch (node->action) {
  447. case BTRFS_ADD_DELAYED_EXTENT:
  448. case BTRFS_UPDATE_DELAYED_HEAD:
  449. WARN_ON(1);
  450. continue;
  451. case BTRFS_ADD_DELAYED_REF:
  452. sgn = 1;
  453. break;
  454. case BTRFS_DROP_DELAYED_REF:
  455. sgn = -1;
  456. break;
  457. default:
  458. BUG_ON(1);
  459. }
  460. switch (node->type) {
  461. case BTRFS_TREE_BLOCK_REF_KEY: {
  462. struct btrfs_delayed_tree_ref *ref;
  463. ref = btrfs_delayed_node_to_tree_ref(node);
  464. ret = __add_prelim_ref(prefs, ref->root, &op_key,
  465. ref->level + 1, 0, node->bytenr,
  466. node->ref_mod * sgn);
  467. break;
  468. }
  469. case BTRFS_SHARED_BLOCK_REF_KEY: {
  470. struct btrfs_delayed_tree_ref *ref;
  471. ref = btrfs_delayed_node_to_tree_ref(node);
  472. ret = __add_prelim_ref(prefs, ref->root, NULL,
  473. ref->level + 1, ref->parent,
  474. node->bytenr,
  475. node->ref_mod * sgn);
  476. break;
  477. }
  478. case BTRFS_EXTENT_DATA_REF_KEY: {
  479. struct btrfs_delayed_data_ref *ref;
  480. ref = btrfs_delayed_node_to_data_ref(node);
  481. key.objectid = ref->objectid;
  482. key.type = BTRFS_EXTENT_DATA_KEY;
  483. key.offset = ref->offset;
  484. ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
  485. node->bytenr,
  486. node->ref_mod * sgn);
  487. break;
  488. }
  489. case BTRFS_SHARED_DATA_REF_KEY: {
  490. struct btrfs_delayed_data_ref *ref;
  491. ref = btrfs_delayed_node_to_data_ref(node);
  492. key.objectid = ref->objectid;
  493. key.type = BTRFS_EXTENT_DATA_KEY;
  494. key.offset = ref->offset;
  495. ret = __add_prelim_ref(prefs, ref->root, &key, 0,
  496. ref->parent, node->bytenr,
  497. node->ref_mod * sgn);
  498. break;
  499. }
  500. default:
  501. WARN_ON(1);
  502. }
  503. BUG_ON(ret);
  504. }
  505. return 0;
  506. }
  507. /*
  508. * add all inline backrefs for bytenr to the list
  509. */
  510. static int __add_inline_refs(struct btrfs_fs_info *fs_info,
  511. struct btrfs_path *path, u64 bytenr,
  512. int *info_level, struct list_head *prefs)
  513. {
  514. int ret = 0;
  515. int slot;
  516. struct extent_buffer *leaf;
  517. struct btrfs_key key;
  518. unsigned long ptr;
  519. unsigned long end;
  520. struct btrfs_extent_item *ei;
  521. u64 flags;
  522. u64 item_size;
  523. /*
  524. * enumerate all inline refs
  525. */
  526. leaf = path->nodes[0];
  527. slot = path->slots[0];
  528. item_size = btrfs_item_size_nr(leaf, slot);
  529. BUG_ON(item_size < sizeof(*ei));
  530. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  531. flags = btrfs_extent_flags(leaf, ei);
  532. ptr = (unsigned long)(ei + 1);
  533. end = (unsigned long)ei + item_size;
  534. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  535. struct btrfs_tree_block_info *info;
  536. info = (struct btrfs_tree_block_info *)ptr;
  537. *info_level = btrfs_tree_block_level(leaf, info);
  538. ptr += sizeof(struct btrfs_tree_block_info);
  539. BUG_ON(ptr > end);
  540. } else {
  541. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  542. }
  543. while (ptr < end) {
  544. struct btrfs_extent_inline_ref *iref;
  545. u64 offset;
  546. int type;
  547. iref = (struct btrfs_extent_inline_ref *)ptr;
  548. type = btrfs_extent_inline_ref_type(leaf, iref);
  549. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  550. switch (type) {
  551. case BTRFS_SHARED_BLOCK_REF_KEY:
  552. ret = __add_prelim_ref(prefs, 0, NULL,
  553. *info_level + 1, offset,
  554. bytenr, 1);
  555. break;
  556. case BTRFS_SHARED_DATA_REF_KEY: {
  557. struct btrfs_shared_data_ref *sdref;
  558. int count;
  559. sdref = (struct btrfs_shared_data_ref *)(iref + 1);
  560. count = btrfs_shared_data_ref_count(leaf, sdref);
  561. ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
  562. bytenr, count);
  563. break;
  564. }
  565. case BTRFS_TREE_BLOCK_REF_KEY:
  566. ret = __add_prelim_ref(prefs, offset, NULL,
  567. *info_level + 1, 0,
  568. bytenr, 1);
  569. break;
  570. case BTRFS_EXTENT_DATA_REF_KEY: {
  571. struct btrfs_extent_data_ref *dref;
  572. int count;
  573. u64 root;
  574. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  575. count = btrfs_extent_data_ref_count(leaf, dref);
  576. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  577. dref);
  578. key.type = BTRFS_EXTENT_DATA_KEY;
  579. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  580. root = btrfs_extent_data_ref_root(leaf, dref);
  581. ret = __add_prelim_ref(prefs, root, &key, 0, 0,
  582. bytenr, count);
  583. break;
  584. }
  585. default:
  586. WARN_ON(1);
  587. }
  588. BUG_ON(ret);
  589. ptr += btrfs_extent_inline_ref_size(type);
  590. }
  591. return 0;
  592. }
  593. /*
  594. * add all non-inline backrefs for bytenr to the list
  595. */
  596. static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
  597. struct btrfs_path *path, u64 bytenr,
  598. int info_level, struct list_head *prefs)
  599. {
  600. struct btrfs_root *extent_root = fs_info->extent_root;
  601. int ret;
  602. int slot;
  603. struct extent_buffer *leaf;
  604. struct btrfs_key key;
  605. while (1) {
  606. ret = btrfs_next_item(extent_root, path);
  607. if (ret < 0)
  608. break;
  609. if (ret) {
  610. ret = 0;
  611. break;
  612. }
  613. slot = path->slots[0];
  614. leaf = path->nodes[0];
  615. btrfs_item_key_to_cpu(leaf, &key, slot);
  616. if (key.objectid != bytenr)
  617. break;
  618. if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
  619. continue;
  620. if (key.type > BTRFS_SHARED_DATA_REF_KEY)
  621. break;
  622. switch (key.type) {
  623. case BTRFS_SHARED_BLOCK_REF_KEY:
  624. ret = __add_prelim_ref(prefs, 0, NULL,
  625. info_level + 1, key.offset,
  626. bytenr, 1);
  627. break;
  628. case BTRFS_SHARED_DATA_REF_KEY: {
  629. struct btrfs_shared_data_ref *sdref;
  630. int count;
  631. sdref = btrfs_item_ptr(leaf, slot,
  632. struct btrfs_shared_data_ref);
  633. count = btrfs_shared_data_ref_count(leaf, sdref);
  634. ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
  635. bytenr, count);
  636. break;
  637. }
  638. case BTRFS_TREE_BLOCK_REF_KEY:
  639. ret = __add_prelim_ref(prefs, key.offset, NULL,
  640. info_level + 1, 0,
  641. bytenr, 1);
  642. break;
  643. case BTRFS_EXTENT_DATA_REF_KEY: {
  644. struct btrfs_extent_data_ref *dref;
  645. int count;
  646. u64 root;
  647. dref = btrfs_item_ptr(leaf, slot,
  648. struct btrfs_extent_data_ref);
  649. count = btrfs_extent_data_ref_count(leaf, dref);
  650. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  651. dref);
  652. key.type = BTRFS_EXTENT_DATA_KEY;
  653. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  654. root = btrfs_extent_data_ref_root(leaf, dref);
  655. ret = __add_prelim_ref(prefs, root, &key, 0, 0,
  656. bytenr, count);
  657. break;
  658. }
  659. default:
  660. WARN_ON(1);
  661. }
  662. BUG_ON(ret);
  663. }
  664. return ret;
  665. }
  666. /*
  667. * this adds all existing backrefs (inline backrefs, backrefs and delayed
  668. * refs) for the given bytenr to the refs list, merges duplicates and resolves
  669. * indirect refs to their parent bytenr.
  670. * When roots are found, they're added to the roots list
  671. *
  672. * FIXME some caching might speed things up
  673. */
  674. static int find_parent_nodes(struct btrfs_trans_handle *trans,
  675. struct btrfs_fs_info *fs_info, u64 bytenr,
  676. u64 delayed_ref_seq, u64 time_seq,
  677. struct ulist *refs, struct ulist *roots,
  678. const u64 *extent_item_pos)
  679. {
  680. struct btrfs_key key;
  681. struct btrfs_path *path;
  682. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  683. struct btrfs_delayed_ref_head *head;
  684. int info_level = 0;
  685. int ret;
  686. int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT);
  687. struct list_head prefs_delayed;
  688. struct list_head prefs;
  689. struct __prelim_ref *ref;
  690. INIT_LIST_HEAD(&prefs);
  691. INIT_LIST_HEAD(&prefs_delayed);
  692. key.objectid = bytenr;
  693. key.type = BTRFS_EXTENT_ITEM_KEY;
  694. key.offset = (u64)-1;
  695. path = btrfs_alloc_path();
  696. if (!path)
  697. return -ENOMEM;
  698. path->search_commit_root = !!search_commit_root;
  699. /*
  700. * grab both a lock on the path and a lock on the delayed ref head.
  701. * We need both to get a consistent picture of how the refs look
  702. * at a specified point in time
  703. */
  704. again:
  705. head = NULL;
  706. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  707. if (ret < 0)
  708. goto out;
  709. BUG_ON(ret == 0);
  710. if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) {
  711. /*
  712. * look if there are updates for this ref queued and lock the
  713. * head
  714. */
  715. delayed_refs = &trans->transaction->delayed_refs;
  716. spin_lock(&delayed_refs->lock);
  717. head = btrfs_find_delayed_ref_head(trans, bytenr);
  718. if (head) {
  719. if (!mutex_trylock(&head->mutex)) {
  720. atomic_inc(&head->node.refs);
  721. spin_unlock(&delayed_refs->lock);
  722. btrfs_release_path(path);
  723. /*
  724. * Mutex was contended, block until it's
  725. * released and try again
  726. */
  727. mutex_lock(&head->mutex);
  728. mutex_unlock(&head->mutex);
  729. btrfs_put_delayed_ref(&head->node);
  730. goto again;
  731. }
  732. ret = __add_delayed_refs(head, delayed_ref_seq,
  733. &prefs_delayed);
  734. if (ret) {
  735. spin_unlock(&delayed_refs->lock);
  736. goto out;
  737. }
  738. }
  739. spin_unlock(&delayed_refs->lock);
  740. }
  741. if (path->slots[0]) {
  742. struct extent_buffer *leaf;
  743. int slot;
  744. path->slots[0]--;
  745. leaf = path->nodes[0];
  746. slot = path->slots[0];
  747. btrfs_item_key_to_cpu(leaf, &key, slot);
  748. if (key.objectid == bytenr &&
  749. key.type == BTRFS_EXTENT_ITEM_KEY) {
  750. ret = __add_inline_refs(fs_info, path, bytenr,
  751. &info_level, &prefs);
  752. if (ret)
  753. goto out;
  754. ret = __add_keyed_refs(fs_info, path, bytenr,
  755. info_level, &prefs);
  756. if (ret)
  757. goto out;
  758. }
  759. }
  760. btrfs_release_path(path);
  761. list_splice_init(&prefs_delayed, &prefs);
  762. ret = __add_missing_keys(fs_info, &prefs);
  763. if (ret)
  764. goto out;
  765. ret = __merge_refs(&prefs, 1);
  766. if (ret)
  767. goto out;
  768. ret = __resolve_indirect_refs(fs_info, search_commit_root, time_seq,
  769. &prefs, extent_item_pos);
  770. if (ret)
  771. goto out;
  772. ret = __merge_refs(&prefs, 2);
  773. if (ret)
  774. goto out;
  775. while (!list_empty(&prefs)) {
  776. ref = list_first_entry(&prefs, struct __prelim_ref, list);
  777. list_del(&ref->list);
  778. if (ref->count < 0)
  779. WARN_ON(1);
  780. if (ref->count && ref->root_id && ref->parent == 0) {
  781. /* no parent == root of tree */
  782. ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
  783. BUG_ON(ret < 0);
  784. }
  785. if (ref->count && ref->parent) {
  786. struct extent_inode_elem *eie = NULL;
  787. if (extent_item_pos && !ref->inode_list) {
  788. u32 bsz;
  789. struct extent_buffer *eb;
  790. bsz = btrfs_level_size(fs_info->extent_root,
  791. info_level);
  792. eb = read_tree_block(fs_info->extent_root,
  793. ref->parent, bsz, 0);
  794. BUG_ON(!eb);
  795. ret = find_extent_in_eb(eb, bytenr,
  796. *extent_item_pos, &eie);
  797. ref->inode_list = eie;
  798. free_extent_buffer(eb);
  799. }
  800. ret = ulist_add_merge(refs, ref->parent,
  801. (unsigned long)ref->inode_list,
  802. (unsigned long *)&eie, GFP_NOFS);
  803. if (!ret && extent_item_pos) {
  804. /*
  805. * we've recorded that parent, so we must extend
  806. * its inode list here
  807. */
  808. BUG_ON(!eie);
  809. while (eie->next)
  810. eie = eie->next;
  811. eie->next = ref->inode_list;
  812. }
  813. BUG_ON(ret < 0);
  814. }
  815. kfree(ref);
  816. }
  817. out:
  818. if (head)
  819. mutex_unlock(&head->mutex);
  820. btrfs_free_path(path);
  821. while (!list_empty(&prefs)) {
  822. ref = list_first_entry(&prefs, struct __prelim_ref, list);
  823. list_del(&ref->list);
  824. kfree(ref);
  825. }
  826. while (!list_empty(&prefs_delayed)) {
  827. ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
  828. list);
  829. list_del(&ref->list);
  830. kfree(ref);
  831. }
  832. return ret;
  833. }
  834. static void free_leaf_list(struct ulist *blocks)
  835. {
  836. struct ulist_node *node = NULL;
  837. struct extent_inode_elem *eie;
  838. struct extent_inode_elem *eie_next;
  839. struct ulist_iterator uiter;
  840. ULIST_ITER_INIT(&uiter);
  841. while ((node = ulist_next(blocks, &uiter))) {
  842. if (!node->aux)
  843. continue;
  844. eie = (struct extent_inode_elem *)node->aux;
  845. for (; eie; eie = eie_next) {
  846. eie_next = eie->next;
  847. kfree(eie);
  848. }
  849. node->aux = 0;
  850. }
  851. ulist_free(blocks);
  852. }
  853. /*
  854. * Finds all leafs with a reference to the specified combination of bytenr and
  855. * offset. key_list_head will point to a list of corresponding keys (caller must
  856. * free each list element). The leafs will be stored in the leafs ulist, which
  857. * must be freed with ulist_free.
  858. *
  859. * returns 0 on success, <0 on error
  860. */
  861. static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
  862. struct btrfs_fs_info *fs_info, u64 bytenr,
  863. u64 delayed_ref_seq, u64 time_seq,
  864. struct ulist **leafs,
  865. const u64 *extent_item_pos)
  866. {
  867. struct ulist *tmp;
  868. int ret;
  869. tmp = ulist_alloc(GFP_NOFS);
  870. if (!tmp)
  871. return -ENOMEM;
  872. *leafs = ulist_alloc(GFP_NOFS);
  873. if (!*leafs) {
  874. ulist_free(tmp);
  875. return -ENOMEM;
  876. }
  877. ret = find_parent_nodes(trans, fs_info, bytenr, delayed_ref_seq,
  878. time_seq, *leafs, tmp, extent_item_pos);
  879. ulist_free(tmp);
  880. if (ret < 0 && ret != -ENOENT) {
  881. free_leaf_list(*leafs);
  882. return ret;
  883. }
  884. return 0;
  885. }
  886. /*
  887. * walk all backrefs for a given extent to find all roots that reference this
  888. * extent. Walking a backref means finding all extents that reference this
  889. * extent and in turn walk the backrefs of those, too. Naturally this is a
  890. * recursive process, but here it is implemented in an iterative fashion: We
  891. * find all referencing extents for the extent in question and put them on a
  892. * list. In turn, we find all referencing extents for those, further appending
  893. * to the list. The way we iterate the list allows adding more elements after
  894. * the current while iterating. The process stops when we reach the end of the
  895. * list. Found roots are added to the roots list.
  896. *
  897. * returns 0 on success, < 0 on error.
  898. */
  899. int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
  900. struct btrfs_fs_info *fs_info, u64 bytenr,
  901. u64 delayed_ref_seq, u64 time_seq,
  902. struct ulist **roots)
  903. {
  904. struct ulist *tmp;
  905. struct ulist_node *node = NULL;
  906. struct ulist_iterator uiter;
  907. int ret;
  908. tmp = ulist_alloc(GFP_NOFS);
  909. if (!tmp)
  910. return -ENOMEM;
  911. *roots = ulist_alloc(GFP_NOFS);
  912. if (!*roots) {
  913. ulist_free(tmp);
  914. return -ENOMEM;
  915. }
  916. ULIST_ITER_INIT(&uiter);
  917. while (1) {
  918. ret = find_parent_nodes(trans, fs_info, bytenr, delayed_ref_seq,
  919. time_seq, tmp, *roots, NULL);
  920. if (ret < 0 && ret != -ENOENT) {
  921. ulist_free(tmp);
  922. ulist_free(*roots);
  923. return ret;
  924. }
  925. node = ulist_next(tmp, &uiter);
  926. if (!node)
  927. break;
  928. bytenr = node->val;
  929. }
  930. ulist_free(tmp);
  931. return 0;
  932. }
  933. static int __inode_info(u64 inum, u64 ioff, u8 key_type,
  934. struct btrfs_root *fs_root, struct btrfs_path *path,
  935. struct btrfs_key *found_key)
  936. {
  937. int ret;
  938. struct btrfs_key key;
  939. struct extent_buffer *eb;
  940. key.type = key_type;
  941. key.objectid = inum;
  942. key.offset = ioff;
  943. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  944. if (ret < 0)
  945. return ret;
  946. eb = path->nodes[0];
  947. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  948. ret = btrfs_next_leaf(fs_root, path);
  949. if (ret)
  950. return ret;
  951. eb = path->nodes[0];
  952. }
  953. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  954. if (found_key->type != key.type || found_key->objectid != key.objectid)
  955. return 1;
  956. return 0;
  957. }
  958. /*
  959. * this makes the path point to (inum INODE_ITEM ioff)
  960. */
  961. int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
  962. struct btrfs_path *path)
  963. {
  964. struct btrfs_key key;
  965. return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path,
  966. &key);
  967. }
  968. static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
  969. struct btrfs_path *path,
  970. struct btrfs_key *found_key)
  971. {
  972. return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path,
  973. found_key);
  974. }
  975. /*
  976. * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements
  977. * of the path are separated by '/' and the path is guaranteed to be
  978. * 0-terminated. the path is only given within the current file system.
  979. * Therefore, it never starts with a '/'. the caller is responsible to provide
  980. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  981. * the start point of the resulting string is returned. this pointer is within
  982. * dest, normally.
  983. * in case the path buffer would overflow, the pointer is decremented further
  984. * as if output was written to the buffer, though no more output is actually
  985. * generated. that way, the caller can determine how much space would be
  986. * required for the path to fit into the buffer. in that case, the returned
  987. * value will be smaller than dest. callers must check this!
  988. */
  989. static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  990. struct btrfs_inode_ref *iref,
  991. struct extent_buffer *eb_in, u64 parent,
  992. char *dest, u32 size)
  993. {
  994. u32 len;
  995. int slot;
  996. u64 next_inum;
  997. int ret;
  998. s64 bytes_left = size - 1;
  999. struct extent_buffer *eb = eb_in;
  1000. struct btrfs_key found_key;
  1001. int leave_spinning = path->leave_spinning;
  1002. if (bytes_left >= 0)
  1003. dest[bytes_left] = '\0';
  1004. path->leave_spinning = 1;
  1005. while (1) {
  1006. len = btrfs_inode_ref_name_len(eb, iref);
  1007. bytes_left -= len;
  1008. if (bytes_left >= 0)
  1009. read_extent_buffer(eb, dest + bytes_left,
  1010. (unsigned long)(iref + 1), len);
  1011. if (eb != eb_in) {
  1012. btrfs_tree_read_unlock_blocking(eb);
  1013. free_extent_buffer(eb);
  1014. }
  1015. ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
  1016. if (ret > 0)
  1017. ret = -ENOENT;
  1018. if (ret)
  1019. break;
  1020. next_inum = found_key.offset;
  1021. /* regular exit ahead */
  1022. if (parent == next_inum)
  1023. break;
  1024. slot = path->slots[0];
  1025. eb = path->nodes[0];
  1026. /* make sure we can use eb after releasing the path */
  1027. if (eb != eb_in) {
  1028. atomic_inc(&eb->refs);
  1029. btrfs_tree_read_lock(eb);
  1030. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1031. }
  1032. btrfs_release_path(path);
  1033. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1034. parent = next_inum;
  1035. --bytes_left;
  1036. if (bytes_left >= 0)
  1037. dest[bytes_left] = '/';
  1038. }
  1039. btrfs_release_path(path);
  1040. path->leave_spinning = leave_spinning;
  1041. if (ret)
  1042. return ERR_PTR(ret);
  1043. return dest + bytes_left;
  1044. }
  1045. /*
  1046. * this makes the path point to (logical EXTENT_ITEM *)
  1047. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  1048. * tree blocks and <0 on error.
  1049. */
  1050. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  1051. struct btrfs_path *path, struct btrfs_key *found_key)
  1052. {
  1053. int ret;
  1054. u64 flags;
  1055. u32 item_size;
  1056. struct extent_buffer *eb;
  1057. struct btrfs_extent_item *ei;
  1058. struct btrfs_key key;
  1059. key.type = BTRFS_EXTENT_ITEM_KEY;
  1060. key.objectid = logical;
  1061. key.offset = (u64)-1;
  1062. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  1063. if (ret < 0)
  1064. return ret;
  1065. ret = btrfs_previous_item(fs_info->extent_root, path,
  1066. 0, BTRFS_EXTENT_ITEM_KEY);
  1067. if (ret < 0)
  1068. return ret;
  1069. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  1070. if (found_key->type != BTRFS_EXTENT_ITEM_KEY ||
  1071. found_key->objectid > logical ||
  1072. found_key->objectid + found_key->offset <= logical) {
  1073. pr_debug("logical %llu is not within any extent\n",
  1074. (unsigned long long)logical);
  1075. return -ENOENT;
  1076. }
  1077. eb = path->nodes[0];
  1078. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  1079. BUG_ON(item_size < sizeof(*ei));
  1080. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  1081. flags = btrfs_extent_flags(eb, ei);
  1082. pr_debug("logical %llu is at position %llu within the extent (%llu "
  1083. "EXTENT_ITEM %llu) flags %#llx size %u\n",
  1084. (unsigned long long)logical,
  1085. (unsigned long long)(logical - found_key->objectid),
  1086. (unsigned long long)found_key->objectid,
  1087. (unsigned long long)found_key->offset,
  1088. (unsigned long long)flags, item_size);
  1089. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1090. return BTRFS_EXTENT_FLAG_TREE_BLOCK;
  1091. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1092. return BTRFS_EXTENT_FLAG_DATA;
  1093. return -EIO;
  1094. }
  1095. /*
  1096. * helper function to iterate extent inline refs. ptr must point to a 0 value
  1097. * for the first call and may be modified. it is used to track state.
  1098. * if more refs exist, 0 is returned and the next call to
  1099. * __get_extent_inline_ref must pass the modified ptr parameter to get the
  1100. * next ref. after the last ref was processed, 1 is returned.
  1101. * returns <0 on error
  1102. */
  1103. static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
  1104. struct btrfs_extent_item *ei, u32 item_size,
  1105. struct btrfs_extent_inline_ref **out_eiref,
  1106. int *out_type)
  1107. {
  1108. unsigned long end;
  1109. u64 flags;
  1110. struct btrfs_tree_block_info *info;
  1111. if (!*ptr) {
  1112. /* first call */
  1113. flags = btrfs_extent_flags(eb, ei);
  1114. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1115. info = (struct btrfs_tree_block_info *)(ei + 1);
  1116. *out_eiref =
  1117. (struct btrfs_extent_inline_ref *)(info + 1);
  1118. } else {
  1119. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  1120. }
  1121. *ptr = (unsigned long)*out_eiref;
  1122. if ((void *)*ptr >= (void *)ei + item_size)
  1123. return -ENOENT;
  1124. }
  1125. end = (unsigned long)ei + item_size;
  1126. *out_eiref = (struct btrfs_extent_inline_ref *)*ptr;
  1127. *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
  1128. *ptr += btrfs_extent_inline_ref_size(*out_type);
  1129. WARN_ON(*ptr > end);
  1130. if (*ptr == end)
  1131. return 1; /* last */
  1132. return 0;
  1133. }
  1134. /*
  1135. * reads the tree block backref for an extent. tree level and root are returned
  1136. * through out_level and out_root. ptr must point to a 0 value for the first
  1137. * call and may be modified (see __get_extent_inline_ref comment).
  1138. * returns 0 if data was provided, 1 if there was no more data to provide or
  1139. * <0 on error.
  1140. */
  1141. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  1142. struct btrfs_extent_item *ei, u32 item_size,
  1143. u64 *out_root, u8 *out_level)
  1144. {
  1145. int ret;
  1146. int type;
  1147. struct btrfs_tree_block_info *info;
  1148. struct btrfs_extent_inline_ref *eiref;
  1149. if (*ptr == (unsigned long)-1)
  1150. return 1;
  1151. while (1) {
  1152. ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
  1153. &eiref, &type);
  1154. if (ret < 0)
  1155. return ret;
  1156. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1157. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1158. break;
  1159. if (ret == 1)
  1160. return 1;
  1161. }
  1162. /* we can treat both ref types equally here */
  1163. info = (struct btrfs_tree_block_info *)(ei + 1);
  1164. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  1165. *out_level = btrfs_tree_block_level(eb, info);
  1166. if (ret == 1)
  1167. *ptr = (unsigned long)-1;
  1168. return 0;
  1169. }
  1170. static int iterate_leaf_refs(struct extent_inode_elem *inode_list,
  1171. u64 root, u64 extent_item_objectid,
  1172. iterate_extent_inodes_t *iterate, void *ctx)
  1173. {
  1174. struct extent_inode_elem *eie;
  1175. int ret = 0;
  1176. for (eie = inode_list; eie; eie = eie->next) {
  1177. pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
  1178. "root %llu\n", extent_item_objectid,
  1179. eie->inum, eie->offset, root);
  1180. ret = iterate(eie->inum, eie->offset, root, ctx);
  1181. if (ret) {
  1182. pr_debug("stopping iteration for %llu due to ret=%d\n",
  1183. extent_item_objectid, ret);
  1184. break;
  1185. }
  1186. }
  1187. return ret;
  1188. }
  1189. /*
  1190. * calls iterate() for every inode that references the extent identified by
  1191. * the given parameters.
  1192. * when the iterator function returns a non-zero value, iteration stops.
  1193. */
  1194. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1195. u64 extent_item_objectid, u64 extent_item_pos,
  1196. int search_commit_root,
  1197. iterate_extent_inodes_t *iterate, void *ctx)
  1198. {
  1199. int ret;
  1200. struct list_head data_refs = LIST_HEAD_INIT(data_refs);
  1201. struct list_head shared_refs = LIST_HEAD_INIT(shared_refs);
  1202. struct btrfs_trans_handle *trans;
  1203. struct ulist *refs = NULL;
  1204. struct ulist *roots = NULL;
  1205. struct ulist_node *ref_node = NULL;
  1206. struct ulist_node *root_node = NULL;
  1207. struct seq_list seq_elem = {};
  1208. struct seq_list tree_mod_seq_elem = {};
  1209. struct ulist_iterator ref_uiter;
  1210. struct ulist_iterator root_uiter;
  1211. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  1212. pr_debug("resolving all inodes for extent %llu\n",
  1213. extent_item_objectid);
  1214. if (search_commit_root) {
  1215. trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT;
  1216. } else {
  1217. trans = btrfs_join_transaction(fs_info->extent_root);
  1218. if (IS_ERR(trans))
  1219. return PTR_ERR(trans);
  1220. delayed_refs = &trans->transaction->delayed_refs;
  1221. spin_lock(&delayed_refs->lock);
  1222. btrfs_get_delayed_seq(delayed_refs, &seq_elem);
  1223. spin_unlock(&delayed_refs->lock);
  1224. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1225. }
  1226. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1227. seq_elem.seq, tree_mod_seq_elem.seq, &refs,
  1228. &extent_item_pos);
  1229. if (ret)
  1230. goto out;
  1231. ULIST_ITER_INIT(&ref_uiter);
  1232. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  1233. ret = btrfs_find_all_roots(trans, fs_info, ref_node->val,
  1234. seq_elem.seq,
  1235. tree_mod_seq_elem.seq, &roots);
  1236. if (ret)
  1237. break;
  1238. ULIST_ITER_INIT(&root_uiter);
  1239. while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
  1240. pr_debug("root %llu references leaf %llu, data list "
  1241. "%#lx\n", root_node->val, ref_node->val,
  1242. ref_node->aux);
  1243. ret = iterate_leaf_refs(
  1244. (struct extent_inode_elem *)ref_node->aux,
  1245. root_node->val, extent_item_objectid,
  1246. iterate, ctx);
  1247. }
  1248. ulist_free(roots);
  1249. roots = NULL;
  1250. }
  1251. free_leaf_list(refs);
  1252. ulist_free(roots);
  1253. out:
  1254. if (!search_commit_root) {
  1255. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1256. btrfs_put_delayed_seq(delayed_refs, &seq_elem);
  1257. btrfs_end_transaction(trans, fs_info->extent_root);
  1258. }
  1259. return ret;
  1260. }
  1261. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1262. struct btrfs_path *path,
  1263. iterate_extent_inodes_t *iterate, void *ctx)
  1264. {
  1265. int ret;
  1266. u64 extent_item_pos;
  1267. struct btrfs_key found_key;
  1268. int search_commit_root = path->search_commit_root;
  1269. ret = extent_from_logical(fs_info, logical, path,
  1270. &found_key);
  1271. btrfs_release_path(path);
  1272. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1273. ret = -EINVAL;
  1274. if (ret < 0)
  1275. return ret;
  1276. extent_item_pos = logical - found_key.objectid;
  1277. ret = iterate_extent_inodes(fs_info, found_key.objectid,
  1278. extent_item_pos, search_commit_root,
  1279. iterate, ctx);
  1280. return ret;
  1281. }
  1282. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1283. struct btrfs_path *path,
  1284. iterate_irefs_t *iterate, void *ctx)
  1285. {
  1286. int ret = 0;
  1287. int slot;
  1288. u32 cur;
  1289. u32 len;
  1290. u32 name_len;
  1291. u64 parent = 0;
  1292. int found = 0;
  1293. struct extent_buffer *eb;
  1294. struct btrfs_item *item;
  1295. struct btrfs_inode_ref *iref;
  1296. struct btrfs_key found_key;
  1297. while (!ret) {
  1298. path->leave_spinning = 1;
  1299. ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
  1300. &found_key);
  1301. if (ret < 0)
  1302. break;
  1303. if (ret) {
  1304. ret = found ? 0 : -ENOENT;
  1305. break;
  1306. }
  1307. ++found;
  1308. parent = found_key.offset;
  1309. slot = path->slots[0];
  1310. eb = path->nodes[0];
  1311. /* make sure we can use eb after releasing the path */
  1312. atomic_inc(&eb->refs);
  1313. btrfs_tree_read_lock(eb);
  1314. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1315. btrfs_release_path(path);
  1316. item = btrfs_item_nr(eb, slot);
  1317. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1318. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1319. name_len = btrfs_inode_ref_name_len(eb, iref);
  1320. /* path must be released before calling iterate()! */
  1321. pr_debug("following ref at offset %u for inode %llu in "
  1322. "tree %llu\n", cur,
  1323. (unsigned long long)found_key.objectid,
  1324. (unsigned long long)fs_root->objectid);
  1325. ret = iterate(parent, iref, eb, ctx);
  1326. if (ret)
  1327. break;
  1328. len = sizeof(*iref) + name_len;
  1329. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1330. }
  1331. btrfs_tree_read_unlock_blocking(eb);
  1332. free_extent_buffer(eb);
  1333. }
  1334. btrfs_release_path(path);
  1335. return ret;
  1336. }
  1337. /*
  1338. * returns 0 if the path could be dumped (probably truncated)
  1339. * returns <0 in case of an error
  1340. */
  1341. static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref,
  1342. struct extent_buffer *eb, void *ctx)
  1343. {
  1344. struct inode_fs_paths *ipath = ctx;
  1345. char *fspath;
  1346. char *fspath_min;
  1347. int i = ipath->fspath->elem_cnt;
  1348. const int s_ptr = sizeof(char *);
  1349. u32 bytes_left;
  1350. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  1351. ipath->fspath->bytes_left - s_ptr : 0;
  1352. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  1353. fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb,
  1354. inum, fspath_min, bytes_left);
  1355. if (IS_ERR(fspath))
  1356. return PTR_ERR(fspath);
  1357. if (fspath > fspath_min) {
  1358. pr_debug("path resolved: %s\n", fspath);
  1359. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  1360. ++ipath->fspath->elem_cnt;
  1361. ipath->fspath->bytes_left = fspath - fspath_min;
  1362. } else {
  1363. pr_debug("missed path, not enough space. missing bytes: %lu, "
  1364. "constructed so far: %s\n",
  1365. (unsigned long)(fspath_min - fspath), fspath_min);
  1366. ++ipath->fspath->elem_missed;
  1367. ipath->fspath->bytes_missing += fspath_min - fspath;
  1368. ipath->fspath->bytes_left = 0;
  1369. }
  1370. return 0;
  1371. }
  1372. /*
  1373. * this dumps all file system paths to the inode into the ipath struct, provided
  1374. * is has been created large enough. each path is zero-terminated and accessed
  1375. * from ipath->fspath->val[i].
  1376. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  1377. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  1378. * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
  1379. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  1380. * have been needed to return all paths.
  1381. */
  1382. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  1383. {
  1384. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  1385. inode_to_path, ipath);
  1386. }
  1387. struct btrfs_data_container *init_data_container(u32 total_bytes)
  1388. {
  1389. struct btrfs_data_container *data;
  1390. size_t alloc_bytes;
  1391. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  1392. data = kmalloc(alloc_bytes, GFP_NOFS);
  1393. if (!data)
  1394. return ERR_PTR(-ENOMEM);
  1395. if (total_bytes >= sizeof(*data)) {
  1396. data->bytes_left = total_bytes - sizeof(*data);
  1397. data->bytes_missing = 0;
  1398. } else {
  1399. data->bytes_missing = sizeof(*data) - total_bytes;
  1400. data->bytes_left = 0;
  1401. }
  1402. data->elem_cnt = 0;
  1403. data->elem_missed = 0;
  1404. return data;
  1405. }
  1406. /*
  1407. * allocates space to return multiple file system paths for an inode.
  1408. * total_bytes to allocate are passed, note that space usable for actual path
  1409. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1410. * the returned pointer must be freed with free_ipath() in the end.
  1411. */
  1412. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  1413. struct btrfs_path *path)
  1414. {
  1415. struct inode_fs_paths *ifp;
  1416. struct btrfs_data_container *fspath;
  1417. fspath = init_data_container(total_bytes);
  1418. if (IS_ERR(fspath))
  1419. return (void *)fspath;
  1420. ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
  1421. if (!ifp) {
  1422. kfree(fspath);
  1423. return ERR_PTR(-ENOMEM);
  1424. }
  1425. ifp->btrfs_path = path;
  1426. ifp->fspath = fspath;
  1427. ifp->fs_root = fs_root;
  1428. return ifp;
  1429. }
  1430. void free_ipath(struct inode_fs_paths *ipath)
  1431. {
  1432. if (!ipath)
  1433. return;
  1434. kfree(ipath->fspath);
  1435. kfree(ipath);
  1436. }