backref.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  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/mm.h>
  19. #include <linux/rbtree.h>
  20. #include "ctree.h"
  21. #include "disk-io.h"
  22. #include "backref.h"
  23. #include "ulist.h"
  24. #include "transaction.h"
  25. #include "delayed-ref.h"
  26. #include "locking.h"
  27. /* Just an arbitrary number so we can be sure this happened */
  28. #define BACKREF_FOUND_SHARED 6
  29. struct extent_inode_elem {
  30. u64 inum;
  31. u64 offset;
  32. struct extent_inode_elem *next;
  33. };
  34. static int check_extent_in_eb(const struct btrfs_key *key,
  35. const struct extent_buffer *eb,
  36. const struct btrfs_file_extent_item *fi,
  37. u64 extent_item_pos,
  38. struct extent_inode_elem **eie)
  39. {
  40. u64 offset = 0;
  41. struct extent_inode_elem *e;
  42. if (!btrfs_file_extent_compression(eb, fi) &&
  43. !btrfs_file_extent_encryption(eb, fi) &&
  44. !btrfs_file_extent_other_encoding(eb, fi)) {
  45. u64 data_offset;
  46. u64 data_len;
  47. data_offset = btrfs_file_extent_offset(eb, fi);
  48. data_len = btrfs_file_extent_num_bytes(eb, fi);
  49. if (extent_item_pos < data_offset ||
  50. extent_item_pos >= data_offset + data_len)
  51. return 1;
  52. offset = extent_item_pos - data_offset;
  53. }
  54. e = kmalloc(sizeof(*e), GFP_NOFS);
  55. if (!e)
  56. return -ENOMEM;
  57. e->next = *eie;
  58. e->inum = key->objectid;
  59. e->offset = key->offset + offset;
  60. *eie = e;
  61. return 0;
  62. }
  63. static void free_inode_elem_list(struct extent_inode_elem *eie)
  64. {
  65. struct extent_inode_elem *eie_next;
  66. for (; eie; eie = eie_next) {
  67. eie_next = eie->next;
  68. kfree(eie);
  69. }
  70. }
  71. static int find_extent_in_eb(const struct extent_buffer *eb,
  72. u64 wanted_disk_byte, u64 extent_item_pos,
  73. struct extent_inode_elem **eie)
  74. {
  75. u64 disk_byte;
  76. struct btrfs_key key;
  77. struct btrfs_file_extent_item *fi;
  78. int slot;
  79. int nritems;
  80. int extent_type;
  81. int ret;
  82. /*
  83. * from the shared data ref, we only have the leaf but we need
  84. * the key. thus, we must look into all items and see that we
  85. * find one (some) with a reference to our extent item.
  86. */
  87. nritems = btrfs_header_nritems(eb);
  88. for (slot = 0; slot < nritems; ++slot) {
  89. btrfs_item_key_to_cpu(eb, &key, slot);
  90. if (key.type != BTRFS_EXTENT_DATA_KEY)
  91. continue;
  92. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  93. extent_type = btrfs_file_extent_type(eb, fi);
  94. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  95. continue;
  96. /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
  97. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  98. if (disk_byte != wanted_disk_byte)
  99. continue;
  100. ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
  101. if (ret < 0)
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * this structure records all encountered refs on the way up to the root
  108. */
  109. struct prelim_ref {
  110. struct rb_node rbnode;
  111. u64 root_id;
  112. struct btrfs_key key_for_search;
  113. int level;
  114. int count;
  115. struct extent_inode_elem *inode_list;
  116. u64 parent;
  117. u64 wanted_disk_byte;
  118. };
  119. struct preftree {
  120. struct rb_root root;
  121. unsigned int count;
  122. };
  123. #define PREFTREE_INIT { .root = RB_ROOT, .count = 0 }
  124. struct preftrees {
  125. struct preftree direct; /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */
  126. struct preftree indirect; /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */
  127. struct preftree indirect_missing_keys;
  128. };
  129. static struct kmem_cache *btrfs_prelim_ref_cache;
  130. int __init btrfs_prelim_ref_init(void)
  131. {
  132. btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
  133. sizeof(struct prelim_ref),
  134. 0,
  135. SLAB_MEM_SPREAD,
  136. NULL);
  137. if (!btrfs_prelim_ref_cache)
  138. return -ENOMEM;
  139. return 0;
  140. }
  141. void btrfs_prelim_ref_exit(void)
  142. {
  143. kmem_cache_destroy(btrfs_prelim_ref_cache);
  144. }
  145. static void free_pref(struct prelim_ref *ref)
  146. {
  147. kmem_cache_free(btrfs_prelim_ref_cache, ref);
  148. }
  149. /*
  150. * Return 0 when both refs are for the same block (and can be merged).
  151. * A -1 return indicates ref1 is a 'lower' block than ref2, while 1
  152. * indicates a 'higher' block.
  153. */
  154. static int prelim_ref_compare(struct prelim_ref *ref1,
  155. struct prelim_ref *ref2)
  156. {
  157. if (ref1->level < ref2->level)
  158. return -1;
  159. if (ref1->level > ref2->level)
  160. return 1;
  161. if (ref1->root_id < ref2->root_id)
  162. return -1;
  163. if (ref1->root_id > ref2->root_id)
  164. return 1;
  165. if (ref1->key_for_search.type < ref2->key_for_search.type)
  166. return -1;
  167. if (ref1->key_for_search.type > ref2->key_for_search.type)
  168. return 1;
  169. if (ref1->key_for_search.objectid < ref2->key_for_search.objectid)
  170. return -1;
  171. if (ref1->key_for_search.objectid > ref2->key_for_search.objectid)
  172. return 1;
  173. if (ref1->key_for_search.offset < ref2->key_for_search.offset)
  174. return -1;
  175. if (ref1->key_for_search.offset > ref2->key_for_search.offset)
  176. return 1;
  177. if (ref1->parent < ref2->parent)
  178. return -1;
  179. if (ref1->parent > ref2->parent)
  180. return 1;
  181. return 0;
  182. }
  183. /*
  184. * Add @newref to the @root rbtree, merging identical refs.
  185. *
  186. * Callers should assumed that newref has been freed after calling.
  187. */
  188. static void prelim_ref_insert(struct preftree *preftree,
  189. struct prelim_ref *newref)
  190. {
  191. struct rb_root *root;
  192. struct rb_node **p;
  193. struct rb_node *parent = NULL;
  194. struct prelim_ref *ref;
  195. int result;
  196. root = &preftree->root;
  197. p = &root->rb_node;
  198. while (*p) {
  199. parent = *p;
  200. ref = rb_entry(parent, struct prelim_ref, rbnode);
  201. result = prelim_ref_compare(ref, newref);
  202. if (result < 0) {
  203. p = &(*p)->rb_left;
  204. } else if (result > 0) {
  205. p = &(*p)->rb_right;
  206. } else {
  207. /* Identical refs, merge them and free @newref */
  208. struct extent_inode_elem *eie = ref->inode_list;
  209. while (eie && eie->next)
  210. eie = eie->next;
  211. if (!eie)
  212. ref->inode_list = newref->inode_list;
  213. else
  214. eie->next = newref->inode_list;
  215. ref->count += newref->count;
  216. free_pref(newref);
  217. return;
  218. }
  219. }
  220. preftree->count++;
  221. rb_link_node(&newref->rbnode, parent, p);
  222. rb_insert_color(&newref->rbnode, root);
  223. }
  224. /*
  225. * Release the entire tree. We don't care about internal consistency so
  226. * just free everything and then reset the tree root.
  227. */
  228. static void prelim_release(struct preftree *preftree)
  229. {
  230. struct prelim_ref *ref, *next_ref;
  231. rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root,
  232. rbnode)
  233. free_pref(ref);
  234. preftree->root = RB_ROOT;
  235. preftree->count = 0;
  236. }
  237. /*
  238. * the rules for all callers of this function are:
  239. * - obtaining the parent is the goal
  240. * - if you add a key, you must know that it is a correct key
  241. * - if you cannot add the parent or a correct key, then we will look into the
  242. * block later to set a correct key
  243. *
  244. * delayed refs
  245. * ============
  246. * backref type | shared | indirect | shared | indirect
  247. * information | tree | tree | data | data
  248. * --------------------+--------+----------+--------+----------
  249. * parent logical | y | - | - | -
  250. * key to resolve | - | y | y | y
  251. * tree block logical | - | - | - | -
  252. * root for resolving | y | y | y | y
  253. *
  254. * - column 1: we've the parent -> done
  255. * - column 2, 3, 4: we use the key to find the parent
  256. *
  257. * on disk refs (inline or keyed)
  258. * ==============================
  259. * backref type | shared | indirect | shared | indirect
  260. * information | tree | tree | data | data
  261. * --------------------+--------+----------+--------+----------
  262. * parent logical | y | - | y | -
  263. * key to resolve | - | - | - | y
  264. * tree block logical | y | y | y | y
  265. * root for resolving | - | y | y | y
  266. *
  267. * - column 1, 3: we've the parent -> done
  268. * - column 2: we take the first key from the block to find the parent
  269. * (see add_missing_keys)
  270. * - column 4: we use the key to find the parent
  271. *
  272. * additional information that's available but not required to find the parent
  273. * block might help in merging entries to gain some speed.
  274. */
  275. static int add_prelim_ref(struct preftree *preftree, u64 root_id,
  276. const struct btrfs_key *key, int level, u64 parent,
  277. u64 wanted_disk_byte, int count, gfp_t gfp_mask)
  278. {
  279. struct prelim_ref *ref;
  280. if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
  281. return 0;
  282. ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
  283. if (!ref)
  284. return -ENOMEM;
  285. ref->root_id = root_id;
  286. if (key) {
  287. ref->key_for_search = *key;
  288. /*
  289. * We can often find data backrefs with an offset that is too
  290. * large (>= LLONG_MAX, maximum allowed file offset) due to
  291. * underflows when subtracting a file's offset with the data
  292. * offset of its corresponding extent data item. This can
  293. * happen for example in the clone ioctl.
  294. * So if we detect such case we set the search key's offset to
  295. * zero to make sure we will find the matching file extent item
  296. * at add_all_parents(), otherwise we will miss it because the
  297. * offset taken form the backref is much larger then the offset
  298. * of the file extent item. This can make us scan a very large
  299. * number of file extent items, but at least it will not make
  300. * us miss any.
  301. * This is an ugly workaround for a behaviour that should have
  302. * never existed, but it does and a fix for the clone ioctl
  303. * would touch a lot of places, cause backwards incompatibility
  304. * and would not fix the problem for extents cloned with older
  305. * kernels.
  306. */
  307. if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
  308. ref->key_for_search.offset >= LLONG_MAX)
  309. ref->key_for_search.offset = 0;
  310. } else {
  311. memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
  312. }
  313. ref->inode_list = NULL;
  314. ref->level = level;
  315. ref->count = count;
  316. ref->parent = parent;
  317. ref->wanted_disk_byte = wanted_disk_byte;
  318. prelim_ref_insert(preftree, ref);
  319. return 0;
  320. }
  321. /* direct refs use root == 0, key == NULL */
  322. static int add_direct_ref(struct preftrees *preftrees, int level, u64 parent,
  323. u64 wanted_disk_byte, int count, gfp_t gfp_mask)
  324. {
  325. return add_prelim_ref(&preftrees->direct, 0, NULL, level, parent,
  326. wanted_disk_byte, count, gfp_mask);
  327. }
  328. /* indirect refs use parent == 0 */
  329. static int add_indirect_ref(struct preftrees *preftrees, u64 root_id,
  330. const struct btrfs_key *key, int level,
  331. u64 wanted_disk_byte, int count, gfp_t gfp_mask)
  332. {
  333. struct preftree *tree = &preftrees->indirect;
  334. if (!key)
  335. tree = &preftrees->indirect_missing_keys;
  336. return add_prelim_ref(tree, root_id, key, level, 0,
  337. wanted_disk_byte, count, gfp_mask);
  338. }
  339. static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
  340. struct ulist *parents, struct prelim_ref *ref,
  341. int level, u64 time_seq, const u64 *extent_item_pos,
  342. u64 total_refs)
  343. {
  344. int ret = 0;
  345. int slot;
  346. struct extent_buffer *eb;
  347. struct btrfs_key key;
  348. struct btrfs_key *key_for_search = &ref->key_for_search;
  349. struct btrfs_file_extent_item *fi;
  350. struct extent_inode_elem *eie = NULL, *old = NULL;
  351. u64 disk_byte;
  352. u64 wanted_disk_byte = ref->wanted_disk_byte;
  353. u64 count = 0;
  354. if (level != 0) {
  355. eb = path->nodes[level];
  356. ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
  357. if (ret < 0)
  358. return ret;
  359. return 0;
  360. }
  361. /*
  362. * We normally enter this function with the path already pointing to
  363. * the first item to check. But sometimes, we may enter it with
  364. * slot==nritems. In that case, go to the next leaf before we continue.
  365. */
  366. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  367. if (time_seq == SEQ_LAST)
  368. ret = btrfs_next_leaf(root, path);
  369. else
  370. ret = btrfs_next_old_leaf(root, path, time_seq);
  371. }
  372. while (!ret && count < total_refs) {
  373. eb = path->nodes[0];
  374. slot = path->slots[0];
  375. btrfs_item_key_to_cpu(eb, &key, slot);
  376. if (key.objectid != key_for_search->objectid ||
  377. key.type != BTRFS_EXTENT_DATA_KEY)
  378. break;
  379. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  380. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  381. if (disk_byte == wanted_disk_byte) {
  382. eie = NULL;
  383. old = NULL;
  384. count++;
  385. if (extent_item_pos) {
  386. ret = check_extent_in_eb(&key, eb, fi,
  387. *extent_item_pos,
  388. &eie);
  389. if (ret < 0)
  390. break;
  391. }
  392. if (ret > 0)
  393. goto next;
  394. ret = ulist_add_merge_ptr(parents, eb->start,
  395. eie, (void **)&old, GFP_NOFS);
  396. if (ret < 0)
  397. break;
  398. if (!ret && extent_item_pos) {
  399. while (old->next)
  400. old = old->next;
  401. old->next = eie;
  402. }
  403. eie = NULL;
  404. }
  405. next:
  406. if (time_seq == SEQ_LAST)
  407. ret = btrfs_next_item(root, path);
  408. else
  409. ret = btrfs_next_old_item(root, path, time_seq);
  410. }
  411. if (ret > 0)
  412. ret = 0;
  413. else if (ret < 0)
  414. free_inode_elem_list(eie);
  415. return ret;
  416. }
  417. /*
  418. * resolve an indirect backref in the form (root_id, key, level)
  419. * to a logical address
  420. */
  421. static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
  422. struct btrfs_path *path, u64 time_seq,
  423. struct prelim_ref *ref, struct ulist *parents,
  424. const u64 *extent_item_pos, u64 total_refs)
  425. {
  426. struct btrfs_root *root;
  427. struct btrfs_key root_key;
  428. struct extent_buffer *eb;
  429. int ret = 0;
  430. int root_level;
  431. int level = ref->level;
  432. int index;
  433. root_key.objectid = ref->root_id;
  434. root_key.type = BTRFS_ROOT_ITEM_KEY;
  435. root_key.offset = (u64)-1;
  436. index = srcu_read_lock(&fs_info->subvol_srcu);
  437. root = btrfs_get_fs_root(fs_info, &root_key, false);
  438. if (IS_ERR(root)) {
  439. srcu_read_unlock(&fs_info->subvol_srcu, index);
  440. ret = PTR_ERR(root);
  441. goto out;
  442. }
  443. if (btrfs_is_testing(fs_info)) {
  444. srcu_read_unlock(&fs_info->subvol_srcu, index);
  445. ret = -ENOENT;
  446. goto out;
  447. }
  448. if (path->search_commit_root)
  449. root_level = btrfs_header_level(root->commit_root);
  450. else if (time_seq == SEQ_LAST)
  451. root_level = btrfs_header_level(root->node);
  452. else
  453. root_level = btrfs_old_root_level(root, time_seq);
  454. if (root_level + 1 == level) {
  455. srcu_read_unlock(&fs_info->subvol_srcu, index);
  456. goto out;
  457. }
  458. path->lowest_level = level;
  459. if (time_seq == SEQ_LAST)
  460. ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
  461. 0, 0);
  462. else
  463. ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
  464. time_seq);
  465. /* root node has been locked, we can release @subvol_srcu safely here */
  466. srcu_read_unlock(&fs_info->subvol_srcu, index);
  467. btrfs_debug(fs_info,
  468. "search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
  469. ref->root_id, level, ref->count, ret,
  470. ref->key_for_search.objectid, ref->key_for_search.type,
  471. ref->key_for_search.offset);
  472. if (ret < 0)
  473. goto out;
  474. eb = path->nodes[level];
  475. while (!eb) {
  476. if (WARN_ON(!level)) {
  477. ret = 1;
  478. goto out;
  479. }
  480. level--;
  481. eb = path->nodes[level];
  482. }
  483. ret = add_all_parents(root, path, parents, ref, level, time_seq,
  484. extent_item_pos, total_refs);
  485. out:
  486. path->lowest_level = 0;
  487. btrfs_release_path(path);
  488. return ret;
  489. }
  490. static struct extent_inode_elem *
  491. unode_aux_to_inode_list(struct ulist_node *node)
  492. {
  493. if (!node)
  494. return NULL;
  495. return (struct extent_inode_elem *)(uintptr_t)node->aux;
  496. }
  497. /*
  498. * We maintain three seperate rbtrees: one for direct refs, one for
  499. * indirect refs which have a key, and one for indirect refs which do not
  500. * have a key. Each tree does merge on insertion.
  501. *
  502. * Once all of the references are located, we iterate over the tree of
  503. * indirect refs with missing keys. An appropriate key is located and
  504. * the ref is moved onto the tree for indirect refs. After all missing
  505. * keys are thus located, we iterate over the indirect ref tree, resolve
  506. * each reference, and then insert the resolved reference onto the
  507. * direct tree (merging there too).
  508. *
  509. * New backrefs (i.e., for parent nodes) are added to the appropriate
  510. * rbtree as they are encountered. The new backrefs are subsequently
  511. * resolved as above.
  512. */
  513. static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
  514. struct btrfs_path *path, u64 time_seq,
  515. struct preftrees *preftrees,
  516. const u64 *extent_item_pos, u64 total_refs,
  517. u64 root_objectid)
  518. {
  519. int err;
  520. int ret = 0;
  521. struct ulist *parents;
  522. struct ulist_node *node;
  523. struct ulist_iterator uiter;
  524. struct rb_node *rnode;
  525. parents = ulist_alloc(GFP_NOFS);
  526. if (!parents)
  527. return -ENOMEM;
  528. /*
  529. * We could trade memory usage for performance here by iterating
  530. * the tree, allocating new refs for each insertion, and then
  531. * freeing the entire indirect tree when we're done. In some test
  532. * cases, the tree can grow quite large (~200k objects).
  533. */
  534. while ((rnode = rb_first(&preftrees->indirect.root))) {
  535. struct prelim_ref *ref;
  536. ref = rb_entry(rnode, struct prelim_ref, rbnode);
  537. if (WARN(ref->parent,
  538. "BUG: direct ref found in indirect tree")) {
  539. ret = -EINVAL;
  540. goto out;
  541. }
  542. rb_erase(&ref->rbnode, &preftrees->indirect.root);
  543. preftrees->indirect.count--;
  544. if (ref->count == 0) {
  545. free_pref(ref);
  546. continue;
  547. }
  548. if (root_objectid && ref->root_id != root_objectid) {
  549. free_pref(ref);
  550. ret = BACKREF_FOUND_SHARED;
  551. goto out;
  552. }
  553. err = resolve_indirect_ref(fs_info, path, time_seq, ref,
  554. parents, extent_item_pos,
  555. total_refs);
  556. /*
  557. * we can only tolerate ENOENT,otherwise,we should catch error
  558. * and return directly.
  559. */
  560. if (err == -ENOENT) {
  561. prelim_ref_insert(&preftrees->direct, ref);
  562. continue;
  563. } else if (err) {
  564. free_pref(ref);
  565. ret = err;
  566. goto out;
  567. }
  568. /* we put the first parent into the ref at hand */
  569. ULIST_ITER_INIT(&uiter);
  570. node = ulist_next(parents, &uiter);
  571. ref->parent = node ? node->val : 0;
  572. ref->inode_list = unode_aux_to_inode_list(node);
  573. /* Add a prelim_ref(s) for any other parent(s). */
  574. while ((node = ulist_next(parents, &uiter))) {
  575. struct prelim_ref *new_ref;
  576. new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
  577. GFP_NOFS);
  578. if (!new_ref) {
  579. free_pref(ref);
  580. ret = -ENOMEM;
  581. goto out;
  582. }
  583. memcpy(new_ref, ref, sizeof(*ref));
  584. new_ref->parent = node->val;
  585. new_ref->inode_list = unode_aux_to_inode_list(node);
  586. prelim_ref_insert(&preftrees->direct, new_ref);
  587. }
  588. /* Now it's a direct ref, put it in the the direct tree */
  589. prelim_ref_insert(&preftrees->direct, ref);
  590. ulist_reinit(parents);
  591. }
  592. out:
  593. ulist_free(parents);
  594. return ret;
  595. }
  596. /*
  597. * read tree blocks and add keys where required.
  598. */
  599. static int add_missing_keys(struct btrfs_fs_info *fs_info,
  600. struct preftrees *preftrees)
  601. {
  602. struct prelim_ref *ref;
  603. struct extent_buffer *eb;
  604. struct preftree *tree = &preftrees->indirect_missing_keys;
  605. struct rb_node *node;
  606. while ((node = rb_first(&tree->root))) {
  607. ref = rb_entry(node, struct prelim_ref, rbnode);
  608. rb_erase(node, &tree->root);
  609. BUG_ON(ref->parent); /* should not be a direct ref */
  610. BUG_ON(ref->key_for_search.type);
  611. BUG_ON(!ref->wanted_disk_byte);
  612. eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
  613. if (IS_ERR(eb)) {
  614. free_pref(ref);
  615. return PTR_ERR(eb);
  616. } else if (!extent_buffer_uptodate(eb)) {
  617. free_pref(ref);
  618. free_extent_buffer(eb);
  619. return -EIO;
  620. }
  621. btrfs_tree_read_lock(eb);
  622. if (btrfs_header_level(eb) == 0)
  623. btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
  624. else
  625. btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
  626. btrfs_tree_read_unlock(eb);
  627. free_extent_buffer(eb);
  628. prelim_ref_insert(&preftrees->indirect, ref);
  629. }
  630. return 0;
  631. }
  632. /*
  633. * add all currently queued delayed refs from this head whose seq nr is
  634. * smaller or equal that seq to the list
  635. */
  636. static int add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
  637. struct preftrees *preftrees, u64 *total_refs,
  638. u64 inum)
  639. {
  640. struct btrfs_delayed_ref_node *node;
  641. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  642. struct btrfs_key key;
  643. struct btrfs_key tmp_op_key;
  644. struct btrfs_key *op_key = NULL;
  645. int sgn;
  646. int ret = 0;
  647. if (extent_op && extent_op->update_key) {
  648. btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key);
  649. op_key = &tmp_op_key;
  650. }
  651. spin_lock(&head->lock);
  652. list_for_each_entry(node, &head->ref_list, list) {
  653. if (node->seq > seq)
  654. continue;
  655. switch (node->action) {
  656. case BTRFS_ADD_DELAYED_EXTENT:
  657. case BTRFS_UPDATE_DELAYED_HEAD:
  658. WARN_ON(1);
  659. continue;
  660. case BTRFS_ADD_DELAYED_REF:
  661. sgn = 1;
  662. break;
  663. case BTRFS_DROP_DELAYED_REF:
  664. sgn = -1;
  665. break;
  666. default:
  667. BUG_ON(1);
  668. }
  669. *total_refs += (node->ref_mod * sgn);
  670. switch (node->type) {
  671. case BTRFS_TREE_BLOCK_REF_KEY: {
  672. /* NORMAL INDIRECT METADATA backref */
  673. struct btrfs_delayed_tree_ref *ref;
  674. ref = btrfs_delayed_node_to_tree_ref(node);
  675. ret = add_indirect_ref(preftrees, ref->root, &tmp_op_key,
  676. ref->level + 1, node->bytenr,
  677. node->ref_mod * sgn,
  678. GFP_ATOMIC);
  679. break;
  680. }
  681. case BTRFS_SHARED_BLOCK_REF_KEY: {
  682. /* SHARED DIRECT METADATA backref */
  683. struct btrfs_delayed_tree_ref *ref;
  684. ref = btrfs_delayed_node_to_tree_ref(node);
  685. ret = add_direct_ref(preftrees, ref->level + 1,
  686. ref->parent, node->bytenr,
  687. node->ref_mod * sgn,
  688. GFP_ATOMIC);
  689. break;
  690. }
  691. case BTRFS_EXTENT_DATA_REF_KEY: {
  692. /* NORMAL INDIRECT DATA backref */
  693. struct btrfs_delayed_data_ref *ref;
  694. ref = btrfs_delayed_node_to_data_ref(node);
  695. key.objectid = ref->objectid;
  696. key.type = BTRFS_EXTENT_DATA_KEY;
  697. key.offset = ref->offset;
  698. /*
  699. * Found a inum that doesn't match our known inum, we
  700. * know it's shared.
  701. */
  702. if (inum && ref->objectid != inum) {
  703. ret = BACKREF_FOUND_SHARED;
  704. break;
  705. }
  706. ret = add_indirect_ref(preftrees, ref->root, &key, 0,
  707. node->bytenr,
  708. node->ref_mod * sgn,
  709. GFP_ATOMIC);
  710. break;
  711. }
  712. case BTRFS_SHARED_DATA_REF_KEY: {
  713. /* SHARED DIRECT FULL backref */
  714. struct btrfs_delayed_data_ref *ref;
  715. ref = btrfs_delayed_node_to_data_ref(node);
  716. ret = add_direct_ref(preftrees, 0, ref->parent,
  717. node->bytenr,
  718. node->ref_mod * sgn,
  719. GFP_ATOMIC);
  720. break;
  721. }
  722. default:
  723. WARN_ON(1);
  724. }
  725. if (ret)
  726. break;
  727. }
  728. spin_unlock(&head->lock);
  729. return ret;
  730. }
  731. /*
  732. * add all inline backrefs for bytenr to the list
  733. */
  734. static int add_inline_refs(struct btrfs_path *path, u64 bytenr,
  735. int *info_level, struct preftrees *preftrees,
  736. u64 *total_refs, u64 inum)
  737. {
  738. int ret = 0;
  739. int slot;
  740. struct extent_buffer *leaf;
  741. struct btrfs_key key;
  742. struct btrfs_key found_key;
  743. unsigned long ptr;
  744. unsigned long end;
  745. struct btrfs_extent_item *ei;
  746. u64 flags;
  747. u64 item_size;
  748. /*
  749. * enumerate all inline refs
  750. */
  751. leaf = path->nodes[0];
  752. slot = path->slots[0];
  753. item_size = btrfs_item_size_nr(leaf, slot);
  754. BUG_ON(item_size < sizeof(*ei));
  755. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  756. flags = btrfs_extent_flags(leaf, ei);
  757. *total_refs += btrfs_extent_refs(leaf, ei);
  758. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  759. ptr = (unsigned long)(ei + 1);
  760. end = (unsigned long)ei + item_size;
  761. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  762. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  763. struct btrfs_tree_block_info *info;
  764. info = (struct btrfs_tree_block_info *)ptr;
  765. *info_level = btrfs_tree_block_level(leaf, info);
  766. ptr += sizeof(struct btrfs_tree_block_info);
  767. BUG_ON(ptr > end);
  768. } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
  769. *info_level = found_key.offset;
  770. } else {
  771. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  772. }
  773. while (ptr < end) {
  774. struct btrfs_extent_inline_ref *iref;
  775. u64 offset;
  776. int type;
  777. iref = (struct btrfs_extent_inline_ref *)ptr;
  778. type = btrfs_extent_inline_ref_type(leaf, iref);
  779. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  780. switch (type) {
  781. case BTRFS_SHARED_BLOCK_REF_KEY:
  782. ret = add_direct_ref(preftrees, *info_level + 1, offset,
  783. bytenr, 1, GFP_NOFS);
  784. break;
  785. case BTRFS_SHARED_DATA_REF_KEY: {
  786. struct btrfs_shared_data_ref *sdref;
  787. int count;
  788. sdref = (struct btrfs_shared_data_ref *)(iref + 1);
  789. count = btrfs_shared_data_ref_count(leaf, sdref);
  790. ret = add_direct_ref(preftrees, 0, offset,
  791. bytenr, count, GFP_NOFS);
  792. break;
  793. }
  794. case BTRFS_TREE_BLOCK_REF_KEY:
  795. ret = add_indirect_ref(preftrees, offset, NULL,
  796. *info_level + 1, bytenr, 1,
  797. GFP_NOFS);
  798. break;
  799. case BTRFS_EXTENT_DATA_REF_KEY: {
  800. struct btrfs_extent_data_ref *dref;
  801. int count;
  802. u64 root;
  803. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  804. count = btrfs_extent_data_ref_count(leaf, dref);
  805. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  806. dref);
  807. key.type = BTRFS_EXTENT_DATA_KEY;
  808. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  809. if (inum && key.objectid != inum) {
  810. ret = BACKREF_FOUND_SHARED;
  811. break;
  812. }
  813. root = btrfs_extent_data_ref_root(leaf, dref);
  814. ret = add_indirect_ref(preftrees, root, &key, 0, bytenr,
  815. count, GFP_NOFS);
  816. break;
  817. }
  818. default:
  819. WARN_ON(1);
  820. }
  821. if (ret)
  822. return ret;
  823. ptr += btrfs_extent_inline_ref_size(type);
  824. }
  825. return 0;
  826. }
  827. /*
  828. * add all non-inline backrefs for bytenr to the list
  829. */
  830. static int add_keyed_refs(struct btrfs_fs_info *fs_info,
  831. struct btrfs_path *path, u64 bytenr,
  832. int info_level, struct preftrees *preftrees,
  833. u64 inum)
  834. {
  835. struct btrfs_root *extent_root = fs_info->extent_root;
  836. int ret;
  837. int slot;
  838. struct extent_buffer *leaf;
  839. struct btrfs_key key;
  840. while (1) {
  841. ret = btrfs_next_item(extent_root, path);
  842. if (ret < 0)
  843. break;
  844. if (ret) {
  845. ret = 0;
  846. break;
  847. }
  848. slot = path->slots[0];
  849. leaf = path->nodes[0];
  850. btrfs_item_key_to_cpu(leaf, &key, slot);
  851. if (key.objectid != bytenr)
  852. break;
  853. if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
  854. continue;
  855. if (key.type > BTRFS_SHARED_DATA_REF_KEY)
  856. break;
  857. switch (key.type) {
  858. case BTRFS_SHARED_BLOCK_REF_KEY:
  859. /* SHARED DIRECT METADATA backref */
  860. ret = add_direct_ref(preftrees, info_level + 1,
  861. key.offset, bytenr, 1,
  862. GFP_NOFS);
  863. break;
  864. case BTRFS_SHARED_DATA_REF_KEY: {
  865. /* SHARED DIRECT FULL backref */
  866. struct btrfs_shared_data_ref *sdref;
  867. int count;
  868. sdref = btrfs_item_ptr(leaf, slot,
  869. struct btrfs_shared_data_ref);
  870. count = btrfs_shared_data_ref_count(leaf, sdref);
  871. ret = add_direct_ref(preftrees, 0, key.offset, bytenr,
  872. count, GFP_NOFS);
  873. break;
  874. }
  875. case BTRFS_TREE_BLOCK_REF_KEY:
  876. /* NORMAL INDIRECT METADATA backref */
  877. ret = add_indirect_ref(preftrees, key.offset, NULL,
  878. info_level + 1, bytenr, 1,
  879. GFP_NOFS);
  880. break;
  881. case BTRFS_EXTENT_DATA_REF_KEY: {
  882. /* NORMAL INDIRECT DATA backref */
  883. struct btrfs_extent_data_ref *dref;
  884. int count;
  885. u64 root;
  886. dref = btrfs_item_ptr(leaf, slot,
  887. struct btrfs_extent_data_ref);
  888. count = btrfs_extent_data_ref_count(leaf, dref);
  889. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  890. dref);
  891. key.type = BTRFS_EXTENT_DATA_KEY;
  892. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  893. if (inum && key.objectid != inum) {
  894. ret = BACKREF_FOUND_SHARED;
  895. break;
  896. }
  897. root = btrfs_extent_data_ref_root(leaf, dref);
  898. ret = add_indirect_ref(preftrees, root, &key, 0, bytenr,
  899. count, GFP_NOFS);
  900. break;
  901. }
  902. default:
  903. WARN_ON(1);
  904. }
  905. if (ret)
  906. return ret;
  907. }
  908. return ret;
  909. }
  910. /*
  911. * this adds all existing backrefs (inline backrefs, backrefs and delayed
  912. * refs) for the given bytenr to the refs list, merges duplicates and resolves
  913. * indirect refs to their parent bytenr.
  914. * When roots are found, they're added to the roots list
  915. *
  916. * NOTE: This can return values > 0
  917. *
  918. * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
  919. * much like trans == NULL case, the difference only lies in it will not
  920. * commit root.
  921. * The special case is for qgroup to search roots in commit_transaction().
  922. *
  923. * FIXME some caching might speed things up
  924. */
  925. static int find_parent_nodes(struct btrfs_trans_handle *trans,
  926. struct btrfs_fs_info *fs_info, u64 bytenr,
  927. u64 time_seq, struct ulist *refs,
  928. struct ulist *roots, const u64 *extent_item_pos,
  929. u64 root_objectid, u64 inum)
  930. {
  931. struct btrfs_key key;
  932. struct btrfs_path *path;
  933. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  934. struct btrfs_delayed_ref_head *head;
  935. int info_level = 0;
  936. int ret;
  937. struct prelim_ref *ref;
  938. struct rb_node *node;
  939. struct extent_inode_elem *eie = NULL;
  940. /* total of both direct AND indirect refs! */
  941. u64 total_refs = 0;
  942. struct preftrees preftrees = {
  943. .direct = PREFTREE_INIT,
  944. .indirect = PREFTREE_INIT,
  945. .indirect_missing_keys = PREFTREE_INIT
  946. };
  947. key.objectid = bytenr;
  948. key.offset = (u64)-1;
  949. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  950. key.type = BTRFS_METADATA_ITEM_KEY;
  951. else
  952. key.type = BTRFS_EXTENT_ITEM_KEY;
  953. path = btrfs_alloc_path();
  954. if (!path)
  955. return -ENOMEM;
  956. if (!trans) {
  957. path->search_commit_root = 1;
  958. path->skip_locking = 1;
  959. }
  960. if (time_seq == SEQ_LAST)
  961. path->skip_locking = 1;
  962. /*
  963. * grab both a lock on the path and a lock on the delayed ref head.
  964. * We need both to get a consistent picture of how the refs look
  965. * at a specified point in time
  966. */
  967. again:
  968. head = NULL;
  969. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  970. if (ret < 0)
  971. goto out;
  972. BUG_ON(ret == 0);
  973. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  974. if (trans && likely(trans->type != __TRANS_DUMMY) &&
  975. time_seq != SEQ_LAST) {
  976. #else
  977. if (trans && time_seq != SEQ_LAST) {
  978. #endif
  979. /*
  980. * look if there are updates for this ref queued and lock the
  981. * head
  982. */
  983. delayed_refs = &trans->transaction->delayed_refs;
  984. spin_lock(&delayed_refs->lock);
  985. head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
  986. if (head) {
  987. if (!mutex_trylock(&head->mutex)) {
  988. refcount_inc(&head->node.refs);
  989. spin_unlock(&delayed_refs->lock);
  990. btrfs_release_path(path);
  991. /*
  992. * Mutex was contended, block until it's
  993. * released and try again
  994. */
  995. mutex_lock(&head->mutex);
  996. mutex_unlock(&head->mutex);
  997. btrfs_put_delayed_ref(&head->node);
  998. goto again;
  999. }
  1000. spin_unlock(&delayed_refs->lock);
  1001. ret = add_delayed_refs(head, time_seq, &preftrees,
  1002. &total_refs, inum);
  1003. mutex_unlock(&head->mutex);
  1004. if (ret)
  1005. goto out;
  1006. } else {
  1007. spin_unlock(&delayed_refs->lock);
  1008. }
  1009. }
  1010. if (path->slots[0]) {
  1011. struct extent_buffer *leaf;
  1012. int slot;
  1013. path->slots[0]--;
  1014. leaf = path->nodes[0];
  1015. slot = path->slots[0];
  1016. btrfs_item_key_to_cpu(leaf, &key, slot);
  1017. if (key.objectid == bytenr &&
  1018. (key.type == BTRFS_EXTENT_ITEM_KEY ||
  1019. key.type == BTRFS_METADATA_ITEM_KEY)) {
  1020. ret = add_inline_refs(path, bytenr, &info_level,
  1021. &preftrees, &total_refs, inum);
  1022. if (ret)
  1023. goto out;
  1024. ret = add_keyed_refs(fs_info, path, bytenr, info_level,
  1025. &preftrees, inum);
  1026. if (ret)
  1027. goto out;
  1028. }
  1029. }
  1030. btrfs_release_path(path);
  1031. ret = add_missing_keys(fs_info, &preftrees);
  1032. if (ret)
  1033. goto out;
  1034. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root));
  1035. ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
  1036. extent_item_pos, total_refs,
  1037. root_objectid);
  1038. if (ret)
  1039. goto out;
  1040. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root));
  1041. /*
  1042. * This walks the tree of merged and resolved refs. Tree blocks are
  1043. * read in as needed. Unique entries are added to the ulist, and
  1044. * the list of found roots is updated.
  1045. *
  1046. * We release the entire tree in one go before returning.
  1047. */
  1048. node = rb_first(&preftrees.direct.root);
  1049. while (node) {
  1050. ref = rb_entry(node, struct prelim_ref, rbnode);
  1051. node = rb_next(&ref->rbnode);
  1052. WARN_ON(ref->count < 0);
  1053. if (roots && ref->count && ref->root_id && ref->parent == 0) {
  1054. if (root_objectid && ref->root_id != root_objectid) {
  1055. ret = BACKREF_FOUND_SHARED;
  1056. goto out;
  1057. }
  1058. /* no parent == root of tree */
  1059. ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
  1060. if (ret < 0)
  1061. goto out;
  1062. }
  1063. if (ref->count && ref->parent) {
  1064. if (extent_item_pos && !ref->inode_list &&
  1065. ref->level == 0) {
  1066. struct extent_buffer *eb;
  1067. eb = read_tree_block(fs_info, ref->parent, 0);
  1068. if (IS_ERR(eb)) {
  1069. ret = PTR_ERR(eb);
  1070. goto out;
  1071. } else if (!extent_buffer_uptodate(eb)) {
  1072. free_extent_buffer(eb);
  1073. ret = -EIO;
  1074. goto out;
  1075. }
  1076. btrfs_tree_read_lock(eb);
  1077. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1078. ret = find_extent_in_eb(eb, bytenr,
  1079. *extent_item_pos, &eie);
  1080. btrfs_tree_read_unlock_blocking(eb);
  1081. free_extent_buffer(eb);
  1082. if (ret < 0)
  1083. goto out;
  1084. ref->inode_list = eie;
  1085. }
  1086. ret = ulist_add_merge_ptr(refs, ref->parent,
  1087. ref->inode_list,
  1088. (void **)&eie, GFP_NOFS);
  1089. if (ret < 0)
  1090. goto out;
  1091. if (!ret && extent_item_pos) {
  1092. /*
  1093. * we've recorded that parent, so we must extend
  1094. * its inode list here
  1095. */
  1096. BUG_ON(!eie);
  1097. while (eie->next)
  1098. eie = eie->next;
  1099. eie->next = ref->inode_list;
  1100. }
  1101. eie = NULL;
  1102. }
  1103. }
  1104. out:
  1105. btrfs_free_path(path);
  1106. prelim_release(&preftrees.direct);
  1107. prelim_release(&preftrees.indirect);
  1108. prelim_release(&preftrees.indirect_missing_keys);
  1109. if (ret < 0)
  1110. free_inode_elem_list(eie);
  1111. return ret;
  1112. }
  1113. static void free_leaf_list(struct ulist *blocks)
  1114. {
  1115. struct ulist_node *node = NULL;
  1116. struct extent_inode_elem *eie;
  1117. struct ulist_iterator uiter;
  1118. ULIST_ITER_INIT(&uiter);
  1119. while ((node = ulist_next(blocks, &uiter))) {
  1120. if (!node->aux)
  1121. continue;
  1122. eie = unode_aux_to_inode_list(node);
  1123. free_inode_elem_list(eie);
  1124. node->aux = 0;
  1125. }
  1126. ulist_free(blocks);
  1127. }
  1128. /*
  1129. * Finds all leafs with a reference to the specified combination of bytenr and
  1130. * offset. key_list_head will point to a list of corresponding keys (caller must
  1131. * free each list element). The leafs will be stored in the leafs ulist, which
  1132. * must be freed with ulist_free.
  1133. *
  1134. * returns 0 on success, <0 on error
  1135. */
  1136. static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
  1137. struct btrfs_fs_info *fs_info, u64 bytenr,
  1138. u64 time_seq, struct ulist **leafs,
  1139. const u64 *extent_item_pos)
  1140. {
  1141. int ret;
  1142. *leafs = ulist_alloc(GFP_NOFS);
  1143. if (!*leafs)
  1144. return -ENOMEM;
  1145. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1146. *leafs, NULL, extent_item_pos, 0, 0);
  1147. if (ret < 0 && ret != -ENOENT) {
  1148. free_leaf_list(*leafs);
  1149. return ret;
  1150. }
  1151. return 0;
  1152. }
  1153. /*
  1154. * walk all backrefs for a given extent to find all roots that reference this
  1155. * extent. Walking a backref means finding all extents that reference this
  1156. * extent and in turn walk the backrefs of those, too. Naturally this is a
  1157. * recursive process, but here it is implemented in an iterative fashion: We
  1158. * find all referencing extents for the extent in question and put them on a
  1159. * list. In turn, we find all referencing extents for those, further appending
  1160. * to the list. The way we iterate the list allows adding more elements after
  1161. * the current while iterating. The process stops when we reach the end of the
  1162. * list. Found roots are added to the roots list.
  1163. *
  1164. * returns 0 on success, < 0 on error.
  1165. */
  1166. static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
  1167. struct btrfs_fs_info *fs_info, u64 bytenr,
  1168. u64 time_seq, struct ulist **roots)
  1169. {
  1170. struct ulist *tmp;
  1171. struct ulist_node *node = NULL;
  1172. struct ulist_iterator uiter;
  1173. int ret;
  1174. tmp = ulist_alloc(GFP_NOFS);
  1175. if (!tmp)
  1176. return -ENOMEM;
  1177. *roots = ulist_alloc(GFP_NOFS);
  1178. if (!*roots) {
  1179. ulist_free(tmp);
  1180. return -ENOMEM;
  1181. }
  1182. ULIST_ITER_INIT(&uiter);
  1183. while (1) {
  1184. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1185. tmp, *roots, NULL, 0, 0);
  1186. if (ret < 0 && ret != -ENOENT) {
  1187. ulist_free(tmp);
  1188. ulist_free(*roots);
  1189. return ret;
  1190. }
  1191. node = ulist_next(tmp, &uiter);
  1192. if (!node)
  1193. break;
  1194. bytenr = node->val;
  1195. cond_resched();
  1196. }
  1197. ulist_free(tmp);
  1198. return 0;
  1199. }
  1200. int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
  1201. struct btrfs_fs_info *fs_info, u64 bytenr,
  1202. u64 time_seq, struct ulist **roots)
  1203. {
  1204. int ret;
  1205. if (!trans)
  1206. down_read(&fs_info->commit_root_sem);
  1207. ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
  1208. time_seq, roots);
  1209. if (!trans)
  1210. up_read(&fs_info->commit_root_sem);
  1211. return ret;
  1212. }
  1213. /**
  1214. * btrfs_check_shared - tell us whether an extent is shared
  1215. *
  1216. * btrfs_check_shared uses the backref walking code but will short
  1217. * circuit as soon as it finds a root or inode that doesn't match the
  1218. * one passed in. This provides a significant performance benefit for
  1219. * callers (such as fiemap) which want to know whether the extent is
  1220. * shared but do not need a ref count.
  1221. *
  1222. * This attempts to allocate a transaction in order to account for
  1223. * delayed refs, but continues on even when the alloc fails.
  1224. *
  1225. * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
  1226. */
  1227. int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
  1228. {
  1229. struct btrfs_fs_info *fs_info = root->fs_info;
  1230. struct btrfs_trans_handle *trans;
  1231. struct ulist *tmp = NULL;
  1232. struct ulist *roots = NULL;
  1233. struct ulist_iterator uiter;
  1234. struct ulist_node *node;
  1235. struct seq_list elem = SEQ_LIST_INIT(elem);
  1236. int ret = 0;
  1237. tmp = ulist_alloc(GFP_NOFS);
  1238. roots = ulist_alloc(GFP_NOFS);
  1239. if (!tmp || !roots) {
  1240. ulist_free(tmp);
  1241. ulist_free(roots);
  1242. return -ENOMEM;
  1243. }
  1244. trans = btrfs_join_transaction(root);
  1245. if (IS_ERR(trans)) {
  1246. trans = NULL;
  1247. down_read(&fs_info->commit_root_sem);
  1248. } else {
  1249. btrfs_get_tree_mod_seq(fs_info, &elem);
  1250. }
  1251. ULIST_ITER_INIT(&uiter);
  1252. while (1) {
  1253. ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
  1254. roots, NULL, root->objectid, inum);
  1255. if (ret == BACKREF_FOUND_SHARED) {
  1256. /* this is the only condition under which we return 1 */
  1257. ret = 1;
  1258. break;
  1259. }
  1260. if (ret < 0 && ret != -ENOENT)
  1261. break;
  1262. ret = 0;
  1263. node = ulist_next(tmp, &uiter);
  1264. if (!node)
  1265. break;
  1266. bytenr = node->val;
  1267. cond_resched();
  1268. }
  1269. if (trans) {
  1270. btrfs_put_tree_mod_seq(fs_info, &elem);
  1271. btrfs_end_transaction(trans);
  1272. } else {
  1273. up_read(&fs_info->commit_root_sem);
  1274. }
  1275. ulist_free(tmp);
  1276. ulist_free(roots);
  1277. return ret;
  1278. }
  1279. int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
  1280. u64 start_off, struct btrfs_path *path,
  1281. struct btrfs_inode_extref **ret_extref,
  1282. u64 *found_off)
  1283. {
  1284. int ret, slot;
  1285. struct btrfs_key key;
  1286. struct btrfs_key found_key;
  1287. struct btrfs_inode_extref *extref;
  1288. const struct extent_buffer *leaf;
  1289. unsigned long ptr;
  1290. key.objectid = inode_objectid;
  1291. key.type = BTRFS_INODE_EXTREF_KEY;
  1292. key.offset = start_off;
  1293. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1294. if (ret < 0)
  1295. return ret;
  1296. while (1) {
  1297. leaf = path->nodes[0];
  1298. slot = path->slots[0];
  1299. if (slot >= btrfs_header_nritems(leaf)) {
  1300. /*
  1301. * If the item at offset is not found,
  1302. * btrfs_search_slot will point us to the slot
  1303. * where it should be inserted. In our case
  1304. * that will be the slot directly before the
  1305. * next INODE_REF_KEY_V2 item. In the case
  1306. * that we're pointing to the last slot in a
  1307. * leaf, we must move one leaf over.
  1308. */
  1309. ret = btrfs_next_leaf(root, path);
  1310. if (ret) {
  1311. if (ret >= 1)
  1312. ret = -ENOENT;
  1313. break;
  1314. }
  1315. continue;
  1316. }
  1317. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1318. /*
  1319. * Check that we're still looking at an extended ref key for
  1320. * this particular objectid. If we have different
  1321. * objectid or type then there are no more to be found
  1322. * in the tree and we can exit.
  1323. */
  1324. ret = -ENOENT;
  1325. if (found_key.objectid != inode_objectid)
  1326. break;
  1327. if (found_key.type != BTRFS_INODE_EXTREF_KEY)
  1328. break;
  1329. ret = 0;
  1330. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1331. extref = (struct btrfs_inode_extref *)ptr;
  1332. *ret_extref = extref;
  1333. if (found_off)
  1334. *found_off = found_key.offset;
  1335. break;
  1336. }
  1337. return ret;
  1338. }
  1339. /*
  1340. * this iterates to turn a name (from iref/extref) into a full filesystem path.
  1341. * Elements of the path are separated by '/' and the path is guaranteed to be
  1342. * 0-terminated. the path is only given within the current file system.
  1343. * Therefore, it never starts with a '/'. the caller is responsible to provide
  1344. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  1345. * the start point of the resulting string is returned. this pointer is within
  1346. * dest, normally.
  1347. * in case the path buffer would overflow, the pointer is decremented further
  1348. * as if output was written to the buffer, though no more output is actually
  1349. * generated. that way, the caller can determine how much space would be
  1350. * required for the path to fit into the buffer. in that case, the returned
  1351. * value will be smaller than dest. callers must check this!
  1352. */
  1353. char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  1354. u32 name_len, unsigned long name_off,
  1355. struct extent_buffer *eb_in, u64 parent,
  1356. char *dest, u32 size)
  1357. {
  1358. int slot;
  1359. u64 next_inum;
  1360. int ret;
  1361. s64 bytes_left = ((s64)size) - 1;
  1362. struct extent_buffer *eb = eb_in;
  1363. struct btrfs_key found_key;
  1364. int leave_spinning = path->leave_spinning;
  1365. struct btrfs_inode_ref *iref;
  1366. if (bytes_left >= 0)
  1367. dest[bytes_left] = '\0';
  1368. path->leave_spinning = 1;
  1369. while (1) {
  1370. bytes_left -= name_len;
  1371. if (bytes_left >= 0)
  1372. read_extent_buffer(eb, dest + bytes_left,
  1373. name_off, name_len);
  1374. if (eb != eb_in) {
  1375. if (!path->skip_locking)
  1376. btrfs_tree_read_unlock_blocking(eb);
  1377. free_extent_buffer(eb);
  1378. }
  1379. ret = btrfs_find_item(fs_root, path, parent, 0,
  1380. BTRFS_INODE_REF_KEY, &found_key);
  1381. if (ret > 0)
  1382. ret = -ENOENT;
  1383. if (ret)
  1384. break;
  1385. next_inum = found_key.offset;
  1386. /* regular exit ahead */
  1387. if (parent == next_inum)
  1388. break;
  1389. slot = path->slots[0];
  1390. eb = path->nodes[0];
  1391. /* make sure we can use eb after releasing the path */
  1392. if (eb != eb_in) {
  1393. if (!path->skip_locking)
  1394. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1395. path->nodes[0] = NULL;
  1396. path->locks[0] = 0;
  1397. }
  1398. btrfs_release_path(path);
  1399. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1400. name_len = btrfs_inode_ref_name_len(eb, iref);
  1401. name_off = (unsigned long)(iref + 1);
  1402. parent = next_inum;
  1403. --bytes_left;
  1404. if (bytes_left >= 0)
  1405. dest[bytes_left] = '/';
  1406. }
  1407. btrfs_release_path(path);
  1408. path->leave_spinning = leave_spinning;
  1409. if (ret)
  1410. return ERR_PTR(ret);
  1411. return dest + bytes_left;
  1412. }
  1413. /*
  1414. * this makes the path point to (logical EXTENT_ITEM *)
  1415. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  1416. * tree blocks and <0 on error.
  1417. */
  1418. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  1419. struct btrfs_path *path, struct btrfs_key *found_key,
  1420. u64 *flags_ret)
  1421. {
  1422. int ret;
  1423. u64 flags;
  1424. u64 size = 0;
  1425. u32 item_size;
  1426. const struct extent_buffer *eb;
  1427. struct btrfs_extent_item *ei;
  1428. struct btrfs_key key;
  1429. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1430. key.type = BTRFS_METADATA_ITEM_KEY;
  1431. else
  1432. key.type = BTRFS_EXTENT_ITEM_KEY;
  1433. key.objectid = logical;
  1434. key.offset = (u64)-1;
  1435. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  1436. if (ret < 0)
  1437. return ret;
  1438. ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
  1439. if (ret) {
  1440. if (ret > 0)
  1441. ret = -ENOENT;
  1442. return ret;
  1443. }
  1444. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  1445. if (found_key->type == BTRFS_METADATA_ITEM_KEY)
  1446. size = fs_info->nodesize;
  1447. else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
  1448. size = found_key->offset;
  1449. if (found_key->objectid > logical ||
  1450. found_key->objectid + size <= logical) {
  1451. btrfs_debug(fs_info,
  1452. "logical %llu is not within any extent", logical);
  1453. return -ENOENT;
  1454. }
  1455. eb = path->nodes[0];
  1456. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  1457. BUG_ON(item_size < sizeof(*ei));
  1458. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  1459. flags = btrfs_extent_flags(eb, ei);
  1460. btrfs_debug(fs_info,
  1461. "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
  1462. logical, logical - found_key->objectid, found_key->objectid,
  1463. found_key->offset, flags, item_size);
  1464. WARN_ON(!flags_ret);
  1465. if (flags_ret) {
  1466. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1467. *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
  1468. else if (flags & BTRFS_EXTENT_FLAG_DATA)
  1469. *flags_ret = BTRFS_EXTENT_FLAG_DATA;
  1470. else
  1471. BUG_ON(1);
  1472. return 0;
  1473. }
  1474. return -EIO;
  1475. }
  1476. /*
  1477. * helper function to iterate extent inline refs. ptr must point to a 0 value
  1478. * for the first call and may be modified. it is used to track state.
  1479. * if more refs exist, 0 is returned and the next call to
  1480. * get_extent_inline_ref must pass the modified ptr parameter to get the
  1481. * next ref. after the last ref was processed, 1 is returned.
  1482. * returns <0 on error
  1483. */
  1484. static int get_extent_inline_ref(unsigned long *ptr,
  1485. const struct extent_buffer *eb,
  1486. const struct btrfs_key *key,
  1487. const struct btrfs_extent_item *ei,
  1488. u32 item_size,
  1489. struct btrfs_extent_inline_ref **out_eiref,
  1490. int *out_type)
  1491. {
  1492. unsigned long end;
  1493. u64 flags;
  1494. struct btrfs_tree_block_info *info;
  1495. if (!*ptr) {
  1496. /* first call */
  1497. flags = btrfs_extent_flags(eb, ei);
  1498. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1499. if (key->type == BTRFS_METADATA_ITEM_KEY) {
  1500. /* a skinny metadata extent */
  1501. *out_eiref =
  1502. (struct btrfs_extent_inline_ref *)(ei + 1);
  1503. } else {
  1504. WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
  1505. info = (struct btrfs_tree_block_info *)(ei + 1);
  1506. *out_eiref =
  1507. (struct btrfs_extent_inline_ref *)(info + 1);
  1508. }
  1509. } else {
  1510. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  1511. }
  1512. *ptr = (unsigned long)*out_eiref;
  1513. if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
  1514. return -ENOENT;
  1515. }
  1516. end = (unsigned long)ei + item_size;
  1517. *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
  1518. *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
  1519. *ptr += btrfs_extent_inline_ref_size(*out_type);
  1520. WARN_ON(*ptr > end);
  1521. if (*ptr == end)
  1522. return 1; /* last */
  1523. return 0;
  1524. }
  1525. /*
  1526. * reads the tree block backref for an extent. tree level and root are returned
  1527. * through out_level and out_root. ptr must point to a 0 value for the first
  1528. * call and may be modified (see get_extent_inline_ref comment).
  1529. * returns 0 if data was provided, 1 if there was no more data to provide or
  1530. * <0 on error.
  1531. */
  1532. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  1533. struct btrfs_key *key, struct btrfs_extent_item *ei,
  1534. u32 item_size, u64 *out_root, u8 *out_level)
  1535. {
  1536. int ret;
  1537. int type;
  1538. struct btrfs_extent_inline_ref *eiref;
  1539. if (*ptr == (unsigned long)-1)
  1540. return 1;
  1541. while (1) {
  1542. ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
  1543. &eiref, &type);
  1544. if (ret < 0)
  1545. return ret;
  1546. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1547. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1548. break;
  1549. if (ret == 1)
  1550. return 1;
  1551. }
  1552. /* we can treat both ref types equally here */
  1553. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  1554. if (key->type == BTRFS_EXTENT_ITEM_KEY) {
  1555. struct btrfs_tree_block_info *info;
  1556. info = (struct btrfs_tree_block_info *)(ei + 1);
  1557. *out_level = btrfs_tree_block_level(eb, info);
  1558. } else {
  1559. ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
  1560. *out_level = (u8)key->offset;
  1561. }
  1562. if (ret == 1)
  1563. *ptr = (unsigned long)-1;
  1564. return 0;
  1565. }
  1566. static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
  1567. struct extent_inode_elem *inode_list,
  1568. u64 root, u64 extent_item_objectid,
  1569. iterate_extent_inodes_t *iterate, void *ctx)
  1570. {
  1571. struct extent_inode_elem *eie;
  1572. int ret = 0;
  1573. for (eie = inode_list; eie; eie = eie->next) {
  1574. btrfs_debug(fs_info,
  1575. "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
  1576. extent_item_objectid, eie->inum,
  1577. eie->offset, root);
  1578. ret = iterate(eie->inum, eie->offset, root, ctx);
  1579. if (ret) {
  1580. btrfs_debug(fs_info,
  1581. "stopping iteration for %llu due to ret=%d",
  1582. extent_item_objectid, ret);
  1583. break;
  1584. }
  1585. }
  1586. return ret;
  1587. }
  1588. /*
  1589. * calls iterate() for every inode that references the extent identified by
  1590. * the given parameters.
  1591. * when the iterator function returns a non-zero value, iteration stops.
  1592. */
  1593. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1594. u64 extent_item_objectid, u64 extent_item_pos,
  1595. int search_commit_root,
  1596. iterate_extent_inodes_t *iterate, void *ctx)
  1597. {
  1598. int ret;
  1599. struct btrfs_trans_handle *trans = NULL;
  1600. struct ulist *refs = NULL;
  1601. struct ulist *roots = NULL;
  1602. struct ulist_node *ref_node = NULL;
  1603. struct ulist_node *root_node = NULL;
  1604. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  1605. struct ulist_iterator ref_uiter;
  1606. struct ulist_iterator root_uiter;
  1607. btrfs_debug(fs_info, "resolving all inodes for extent %llu",
  1608. extent_item_objectid);
  1609. if (!search_commit_root) {
  1610. trans = btrfs_join_transaction(fs_info->extent_root);
  1611. if (IS_ERR(trans))
  1612. return PTR_ERR(trans);
  1613. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1614. } else {
  1615. down_read(&fs_info->commit_root_sem);
  1616. }
  1617. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1618. tree_mod_seq_elem.seq, &refs,
  1619. &extent_item_pos);
  1620. if (ret)
  1621. goto out;
  1622. ULIST_ITER_INIT(&ref_uiter);
  1623. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  1624. ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
  1625. tree_mod_seq_elem.seq, &roots);
  1626. if (ret)
  1627. break;
  1628. ULIST_ITER_INIT(&root_uiter);
  1629. while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
  1630. btrfs_debug(fs_info,
  1631. "root %llu references leaf %llu, data list %#llx",
  1632. root_node->val, ref_node->val,
  1633. ref_node->aux);
  1634. ret = iterate_leaf_refs(fs_info,
  1635. (struct extent_inode_elem *)
  1636. (uintptr_t)ref_node->aux,
  1637. root_node->val,
  1638. extent_item_objectid,
  1639. iterate, ctx);
  1640. }
  1641. ulist_free(roots);
  1642. }
  1643. free_leaf_list(refs);
  1644. out:
  1645. if (!search_commit_root) {
  1646. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1647. btrfs_end_transaction(trans);
  1648. } else {
  1649. up_read(&fs_info->commit_root_sem);
  1650. }
  1651. return ret;
  1652. }
  1653. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1654. struct btrfs_path *path,
  1655. iterate_extent_inodes_t *iterate, void *ctx)
  1656. {
  1657. int ret;
  1658. u64 extent_item_pos;
  1659. u64 flags = 0;
  1660. struct btrfs_key found_key;
  1661. int search_commit_root = path->search_commit_root;
  1662. ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
  1663. btrfs_release_path(path);
  1664. if (ret < 0)
  1665. return ret;
  1666. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1667. return -EINVAL;
  1668. extent_item_pos = logical - found_key.objectid;
  1669. ret = iterate_extent_inodes(fs_info, found_key.objectid,
  1670. extent_item_pos, search_commit_root,
  1671. iterate, ctx);
  1672. return ret;
  1673. }
  1674. typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
  1675. struct extent_buffer *eb, void *ctx);
  1676. static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
  1677. struct btrfs_path *path,
  1678. iterate_irefs_t *iterate, void *ctx)
  1679. {
  1680. int ret = 0;
  1681. int slot;
  1682. u32 cur;
  1683. u32 len;
  1684. u32 name_len;
  1685. u64 parent = 0;
  1686. int found = 0;
  1687. struct extent_buffer *eb;
  1688. struct btrfs_item *item;
  1689. struct btrfs_inode_ref *iref;
  1690. struct btrfs_key found_key;
  1691. while (!ret) {
  1692. ret = btrfs_find_item(fs_root, path, inum,
  1693. parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
  1694. &found_key);
  1695. if (ret < 0)
  1696. break;
  1697. if (ret) {
  1698. ret = found ? 0 : -ENOENT;
  1699. break;
  1700. }
  1701. ++found;
  1702. parent = found_key.offset;
  1703. slot = path->slots[0];
  1704. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1705. if (!eb) {
  1706. ret = -ENOMEM;
  1707. break;
  1708. }
  1709. extent_buffer_get(eb);
  1710. btrfs_tree_read_lock(eb);
  1711. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1712. btrfs_release_path(path);
  1713. item = btrfs_item_nr(slot);
  1714. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1715. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1716. name_len = btrfs_inode_ref_name_len(eb, iref);
  1717. /* path must be released before calling iterate()! */
  1718. btrfs_debug(fs_root->fs_info,
  1719. "following ref at offset %u for inode %llu in tree %llu",
  1720. cur, found_key.objectid, fs_root->objectid);
  1721. ret = iterate(parent, name_len,
  1722. (unsigned long)(iref + 1), eb, ctx);
  1723. if (ret)
  1724. break;
  1725. len = sizeof(*iref) + name_len;
  1726. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1727. }
  1728. btrfs_tree_read_unlock_blocking(eb);
  1729. free_extent_buffer(eb);
  1730. }
  1731. btrfs_release_path(path);
  1732. return ret;
  1733. }
  1734. static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
  1735. struct btrfs_path *path,
  1736. iterate_irefs_t *iterate, void *ctx)
  1737. {
  1738. int ret;
  1739. int slot;
  1740. u64 offset = 0;
  1741. u64 parent;
  1742. int found = 0;
  1743. struct extent_buffer *eb;
  1744. struct btrfs_inode_extref *extref;
  1745. u32 item_size;
  1746. u32 cur_offset;
  1747. unsigned long ptr;
  1748. while (1) {
  1749. ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
  1750. &offset);
  1751. if (ret < 0)
  1752. break;
  1753. if (ret) {
  1754. ret = found ? 0 : -ENOENT;
  1755. break;
  1756. }
  1757. ++found;
  1758. slot = path->slots[0];
  1759. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1760. if (!eb) {
  1761. ret = -ENOMEM;
  1762. break;
  1763. }
  1764. extent_buffer_get(eb);
  1765. btrfs_tree_read_lock(eb);
  1766. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1767. btrfs_release_path(path);
  1768. item_size = btrfs_item_size_nr(eb, slot);
  1769. ptr = btrfs_item_ptr_offset(eb, slot);
  1770. cur_offset = 0;
  1771. while (cur_offset < item_size) {
  1772. u32 name_len;
  1773. extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
  1774. parent = btrfs_inode_extref_parent(eb, extref);
  1775. name_len = btrfs_inode_extref_name_len(eb, extref);
  1776. ret = iterate(parent, name_len,
  1777. (unsigned long)&extref->name, eb, ctx);
  1778. if (ret)
  1779. break;
  1780. cur_offset += btrfs_inode_extref_name_len(eb, extref);
  1781. cur_offset += sizeof(*extref);
  1782. }
  1783. btrfs_tree_read_unlock_blocking(eb);
  1784. free_extent_buffer(eb);
  1785. offset++;
  1786. }
  1787. btrfs_release_path(path);
  1788. return ret;
  1789. }
  1790. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1791. struct btrfs_path *path, iterate_irefs_t *iterate,
  1792. void *ctx)
  1793. {
  1794. int ret;
  1795. int found_refs = 0;
  1796. ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
  1797. if (!ret)
  1798. ++found_refs;
  1799. else if (ret != -ENOENT)
  1800. return ret;
  1801. ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
  1802. if (ret == -ENOENT && found_refs)
  1803. return 0;
  1804. return ret;
  1805. }
  1806. /*
  1807. * returns 0 if the path could be dumped (probably truncated)
  1808. * returns <0 in case of an error
  1809. */
  1810. static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
  1811. struct extent_buffer *eb, void *ctx)
  1812. {
  1813. struct inode_fs_paths *ipath = ctx;
  1814. char *fspath;
  1815. char *fspath_min;
  1816. int i = ipath->fspath->elem_cnt;
  1817. const int s_ptr = sizeof(char *);
  1818. u32 bytes_left;
  1819. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  1820. ipath->fspath->bytes_left - s_ptr : 0;
  1821. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  1822. fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
  1823. name_off, eb, inum, fspath_min, bytes_left);
  1824. if (IS_ERR(fspath))
  1825. return PTR_ERR(fspath);
  1826. if (fspath > fspath_min) {
  1827. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  1828. ++ipath->fspath->elem_cnt;
  1829. ipath->fspath->bytes_left = fspath - fspath_min;
  1830. } else {
  1831. ++ipath->fspath->elem_missed;
  1832. ipath->fspath->bytes_missing += fspath_min - fspath;
  1833. ipath->fspath->bytes_left = 0;
  1834. }
  1835. return 0;
  1836. }
  1837. /*
  1838. * this dumps all file system paths to the inode into the ipath struct, provided
  1839. * is has been created large enough. each path is zero-terminated and accessed
  1840. * from ipath->fspath->val[i].
  1841. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  1842. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  1843. * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
  1844. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  1845. * have been needed to return all paths.
  1846. */
  1847. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  1848. {
  1849. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  1850. inode_to_path, ipath);
  1851. }
  1852. struct btrfs_data_container *init_data_container(u32 total_bytes)
  1853. {
  1854. struct btrfs_data_container *data;
  1855. size_t alloc_bytes;
  1856. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  1857. data = kvmalloc(alloc_bytes, GFP_KERNEL);
  1858. if (!data)
  1859. return ERR_PTR(-ENOMEM);
  1860. if (total_bytes >= sizeof(*data)) {
  1861. data->bytes_left = total_bytes - sizeof(*data);
  1862. data->bytes_missing = 0;
  1863. } else {
  1864. data->bytes_missing = sizeof(*data) - total_bytes;
  1865. data->bytes_left = 0;
  1866. }
  1867. data->elem_cnt = 0;
  1868. data->elem_missed = 0;
  1869. return data;
  1870. }
  1871. /*
  1872. * allocates space to return multiple file system paths for an inode.
  1873. * total_bytes to allocate are passed, note that space usable for actual path
  1874. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1875. * the returned pointer must be freed with free_ipath() in the end.
  1876. */
  1877. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  1878. struct btrfs_path *path)
  1879. {
  1880. struct inode_fs_paths *ifp;
  1881. struct btrfs_data_container *fspath;
  1882. fspath = init_data_container(total_bytes);
  1883. if (IS_ERR(fspath))
  1884. return (void *)fspath;
  1885. ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
  1886. if (!ifp) {
  1887. kvfree(fspath);
  1888. return ERR_PTR(-ENOMEM);
  1889. }
  1890. ifp->btrfs_path = path;
  1891. ifp->fspath = fspath;
  1892. ifp->fs_root = fs_root;
  1893. return ifp;
  1894. }
  1895. void free_ipath(struct inode_fs_paths *ipath)
  1896. {
  1897. if (!ipath)
  1898. return;
  1899. kvfree(ipath->fspath);
  1900. kfree(ipath);
  1901. }