backref.c 58 KB

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