backref.c 42 KB

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