backref.c 60 KB

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