backref.c 46 KB

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