backref.c 42 KB

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