backref.c 59 KB

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