backref.c 60 KB

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