backref.c 59 KB

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