backref.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  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->tree_root, ref->wanted_disk_byte,
  693. 0);
  694. if (IS_ERR(eb)) {
  695. return PTR_ERR(eb);
  696. } else if (!extent_buffer_uptodate(eb)) {
  697. free_extent_buffer(eb);
  698. return -EIO;
  699. }
  700. btrfs_tree_read_lock(eb);
  701. if (btrfs_header_level(eb) == 0)
  702. btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
  703. else
  704. btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
  705. btrfs_tree_read_unlock(eb);
  706. free_extent_buffer(eb);
  707. }
  708. return 0;
  709. }
  710. /*
  711. * merge backrefs and adjust counts accordingly
  712. *
  713. * mode = 1: merge identical keys, if key is set
  714. * FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
  715. * additionally, we could even add a key range for the blocks we
  716. * looked into to merge even more (-> replace unresolved refs by those
  717. * having a parent).
  718. * mode = 2: merge identical parents
  719. */
  720. static void __merge_refs(struct list_head *head, int mode)
  721. {
  722. struct __prelim_ref *pos1;
  723. list_for_each_entry(pos1, head, list) {
  724. struct __prelim_ref *pos2 = pos1, *tmp;
  725. list_for_each_entry_safe_continue(pos2, tmp, head, list) {
  726. struct __prelim_ref *ref1 = pos1, *ref2 = pos2;
  727. struct extent_inode_elem *eie;
  728. if (!ref_for_same_block(ref1, ref2))
  729. continue;
  730. if (mode == 1) {
  731. if (!ref1->parent && ref2->parent)
  732. swap(ref1, ref2);
  733. } else {
  734. if (ref1->parent != ref2->parent)
  735. continue;
  736. }
  737. eie = ref1->inode_list;
  738. while (eie && eie->next)
  739. eie = eie->next;
  740. if (eie)
  741. eie->next = ref2->inode_list;
  742. else
  743. ref1->inode_list = ref2->inode_list;
  744. ref1->count += ref2->count;
  745. list_del(&ref2->list);
  746. kmem_cache_free(btrfs_prelim_ref_cache, ref2);
  747. cond_resched();
  748. }
  749. }
  750. }
  751. /*
  752. * add all currently queued delayed refs from this head whose seq nr is
  753. * smaller or equal that seq to the list
  754. */
  755. static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
  756. struct list_head *prefs, u64 *total_refs,
  757. u64 inum)
  758. {
  759. struct btrfs_delayed_ref_node *node;
  760. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  761. struct btrfs_key key;
  762. struct btrfs_key op_key = {0};
  763. int sgn;
  764. int ret = 0;
  765. if (extent_op && extent_op->update_key)
  766. btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
  767. spin_lock(&head->lock);
  768. list_for_each_entry(node, &head->ref_list, list) {
  769. if (node->seq > seq)
  770. continue;
  771. switch (node->action) {
  772. case BTRFS_ADD_DELAYED_EXTENT:
  773. case BTRFS_UPDATE_DELAYED_HEAD:
  774. WARN_ON(1);
  775. continue;
  776. case BTRFS_ADD_DELAYED_REF:
  777. sgn = 1;
  778. break;
  779. case BTRFS_DROP_DELAYED_REF:
  780. sgn = -1;
  781. break;
  782. default:
  783. BUG_ON(1);
  784. }
  785. *total_refs += (node->ref_mod * sgn);
  786. switch (node->type) {
  787. case BTRFS_TREE_BLOCK_REF_KEY: {
  788. struct btrfs_delayed_tree_ref *ref;
  789. ref = btrfs_delayed_node_to_tree_ref(node);
  790. ret = __add_prelim_ref(prefs, ref->root, &op_key,
  791. ref->level + 1, 0, node->bytenr,
  792. node->ref_mod * sgn, GFP_ATOMIC);
  793. break;
  794. }
  795. case BTRFS_SHARED_BLOCK_REF_KEY: {
  796. struct btrfs_delayed_tree_ref *ref;
  797. ref = btrfs_delayed_node_to_tree_ref(node);
  798. ret = __add_prelim_ref(prefs, 0, NULL,
  799. ref->level + 1, ref->parent,
  800. node->bytenr,
  801. node->ref_mod * sgn, GFP_ATOMIC);
  802. break;
  803. }
  804. case BTRFS_EXTENT_DATA_REF_KEY: {
  805. struct btrfs_delayed_data_ref *ref;
  806. ref = btrfs_delayed_node_to_data_ref(node);
  807. key.objectid = ref->objectid;
  808. key.type = BTRFS_EXTENT_DATA_KEY;
  809. key.offset = ref->offset;
  810. /*
  811. * Found a inum that doesn't match our known inum, we
  812. * know it's shared.
  813. */
  814. if (inum && ref->objectid != inum) {
  815. ret = BACKREF_FOUND_SHARED;
  816. break;
  817. }
  818. ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
  819. node->bytenr,
  820. node->ref_mod * sgn, GFP_ATOMIC);
  821. break;
  822. }
  823. case BTRFS_SHARED_DATA_REF_KEY: {
  824. struct btrfs_delayed_data_ref *ref;
  825. ref = btrfs_delayed_node_to_data_ref(node);
  826. ret = __add_prelim_ref(prefs, 0, NULL, 0,
  827. ref->parent, node->bytenr,
  828. node->ref_mod * sgn, GFP_ATOMIC);
  829. break;
  830. }
  831. default:
  832. WARN_ON(1);
  833. }
  834. if (ret)
  835. break;
  836. }
  837. spin_unlock(&head->lock);
  838. return ret;
  839. }
  840. /*
  841. * add all inline backrefs for bytenr to the list
  842. */
  843. static int __add_inline_refs(struct btrfs_fs_info *fs_info,
  844. struct btrfs_path *path, u64 bytenr,
  845. int *info_level, struct list_head *prefs,
  846. struct ref_root *ref_tree,
  847. u64 *total_refs, u64 inum)
  848. {
  849. int ret = 0;
  850. int slot;
  851. struct extent_buffer *leaf;
  852. struct btrfs_key key;
  853. struct btrfs_key found_key;
  854. unsigned long ptr;
  855. unsigned long end;
  856. struct btrfs_extent_item *ei;
  857. u64 flags;
  858. u64 item_size;
  859. /*
  860. * enumerate all inline refs
  861. */
  862. leaf = path->nodes[0];
  863. slot = path->slots[0];
  864. item_size = btrfs_item_size_nr(leaf, slot);
  865. BUG_ON(item_size < sizeof(*ei));
  866. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  867. flags = btrfs_extent_flags(leaf, ei);
  868. *total_refs += btrfs_extent_refs(leaf, ei);
  869. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  870. ptr = (unsigned long)(ei + 1);
  871. end = (unsigned long)ei + item_size;
  872. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  873. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  874. struct btrfs_tree_block_info *info;
  875. info = (struct btrfs_tree_block_info *)ptr;
  876. *info_level = btrfs_tree_block_level(leaf, info);
  877. ptr += sizeof(struct btrfs_tree_block_info);
  878. BUG_ON(ptr > end);
  879. } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
  880. *info_level = found_key.offset;
  881. } else {
  882. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  883. }
  884. while (ptr < end) {
  885. struct btrfs_extent_inline_ref *iref;
  886. u64 offset;
  887. int type;
  888. iref = (struct btrfs_extent_inline_ref *)ptr;
  889. type = btrfs_extent_inline_ref_type(leaf, iref);
  890. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  891. switch (type) {
  892. case BTRFS_SHARED_BLOCK_REF_KEY:
  893. ret = __add_prelim_ref(prefs, 0, NULL,
  894. *info_level + 1, offset,
  895. bytenr, 1, GFP_NOFS);
  896. break;
  897. case BTRFS_SHARED_DATA_REF_KEY: {
  898. struct btrfs_shared_data_ref *sdref;
  899. int count;
  900. sdref = (struct btrfs_shared_data_ref *)(iref + 1);
  901. count = btrfs_shared_data_ref_count(leaf, sdref);
  902. ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
  903. bytenr, count, GFP_NOFS);
  904. if (ref_tree) {
  905. if (!ret)
  906. ret = ref_tree_add(ref_tree, 0, 0, 0,
  907. bytenr, count);
  908. if (!ret && ref_tree->unique_refs > 1)
  909. ret = BACKREF_FOUND_SHARED;
  910. }
  911. break;
  912. }
  913. case BTRFS_TREE_BLOCK_REF_KEY:
  914. ret = __add_prelim_ref(prefs, offset, NULL,
  915. *info_level + 1, 0,
  916. bytenr, 1, GFP_NOFS);
  917. break;
  918. case BTRFS_EXTENT_DATA_REF_KEY: {
  919. struct btrfs_extent_data_ref *dref;
  920. int count;
  921. u64 root;
  922. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  923. count = btrfs_extent_data_ref_count(leaf, dref);
  924. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  925. dref);
  926. key.type = BTRFS_EXTENT_DATA_KEY;
  927. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  928. if (inum && key.objectid != inum) {
  929. ret = BACKREF_FOUND_SHARED;
  930. break;
  931. }
  932. root = btrfs_extent_data_ref_root(leaf, dref);
  933. ret = __add_prelim_ref(prefs, root, &key, 0, 0,
  934. bytenr, count, GFP_NOFS);
  935. if (ref_tree) {
  936. if (!ret)
  937. ret = ref_tree_add(ref_tree, root,
  938. key.objectid,
  939. key.offset, 0,
  940. count);
  941. if (!ret && ref_tree->unique_refs > 1)
  942. ret = BACKREF_FOUND_SHARED;
  943. }
  944. break;
  945. }
  946. default:
  947. WARN_ON(1);
  948. }
  949. if (ret)
  950. return ret;
  951. ptr += btrfs_extent_inline_ref_size(type);
  952. }
  953. return 0;
  954. }
  955. /*
  956. * add all non-inline backrefs for bytenr to the list
  957. */
  958. static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
  959. struct btrfs_path *path, u64 bytenr,
  960. int info_level, struct list_head *prefs,
  961. struct ref_root *ref_tree, u64 inum)
  962. {
  963. struct btrfs_root *extent_root = fs_info->extent_root;
  964. int ret;
  965. int slot;
  966. struct extent_buffer *leaf;
  967. struct btrfs_key key;
  968. while (1) {
  969. ret = btrfs_next_item(extent_root, path);
  970. if (ret < 0)
  971. break;
  972. if (ret) {
  973. ret = 0;
  974. break;
  975. }
  976. slot = path->slots[0];
  977. leaf = path->nodes[0];
  978. btrfs_item_key_to_cpu(leaf, &key, slot);
  979. if (key.objectid != bytenr)
  980. break;
  981. if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
  982. continue;
  983. if (key.type > BTRFS_SHARED_DATA_REF_KEY)
  984. break;
  985. switch (key.type) {
  986. case BTRFS_SHARED_BLOCK_REF_KEY:
  987. ret = __add_prelim_ref(prefs, 0, NULL,
  988. info_level + 1, key.offset,
  989. bytenr, 1, GFP_NOFS);
  990. break;
  991. case BTRFS_SHARED_DATA_REF_KEY: {
  992. struct btrfs_shared_data_ref *sdref;
  993. int count;
  994. sdref = btrfs_item_ptr(leaf, slot,
  995. struct btrfs_shared_data_ref);
  996. count = btrfs_shared_data_ref_count(leaf, sdref);
  997. ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
  998. bytenr, count, GFP_NOFS);
  999. if (ref_tree) {
  1000. if (!ret)
  1001. ret = ref_tree_add(ref_tree, 0, 0, 0,
  1002. bytenr, count);
  1003. if (!ret && ref_tree->unique_refs > 1)
  1004. ret = BACKREF_FOUND_SHARED;
  1005. }
  1006. break;
  1007. }
  1008. case BTRFS_TREE_BLOCK_REF_KEY:
  1009. ret = __add_prelim_ref(prefs, key.offset, NULL,
  1010. info_level + 1, 0,
  1011. bytenr, 1, GFP_NOFS);
  1012. break;
  1013. case BTRFS_EXTENT_DATA_REF_KEY: {
  1014. struct btrfs_extent_data_ref *dref;
  1015. int count;
  1016. u64 root;
  1017. dref = btrfs_item_ptr(leaf, slot,
  1018. struct btrfs_extent_data_ref);
  1019. count = btrfs_extent_data_ref_count(leaf, dref);
  1020. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  1021. dref);
  1022. key.type = BTRFS_EXTENT_DATA_KEY;
  1023. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  1024. if (inum && key.objectid != inum) {
  1025. ret = BACKREF_FOUND_SHARED;
  1026. break;
  1027. }
  1028. root = btrfs_extent_data_ref_root(leaf, dref);
  1029. ret = __add_prelim_ref(prefs, root, &key, 0, 0,
  1030. bytenr, count, GFP_NOFS);
  1031. if (ref_tree) {
  1032. if (!ret)
  1033. ret = ref_tree_add(ref_tree, root,
  1034. key.objectid,
  1035. key.offset, 0,
  1036. count);
  1037. if (!ret && ref_tree->unique_refs > 1)
  1038. ret = BACKREF_FOUND_SHARED;
  1039. }
  1040. break;
  1041. }
  1042. default:
  1043. WARN_ON(1);
  1044. }
  1045. if (ret)
  1046. return ret;
  1047. }
  1048. return ret;
  1049. }
  1050. /*
  1051. * this adds all existing backrefs (inline backrefs, backrefs and delayed
  1052. * refs) for the given bytenr to the refs list, merges duplicates and resolves
  1053. * indirect refs to their parent bytenr.
  1054. * When roots are found, they're added to the roots list
  1055. *
  1056. * NOTE: This can return values > 0
  1057. *
  1058. * If time_seq is set to (u64)-1, it will not search delayed_refs, and behave
  1059. * much like trans == NULL case, the difference only lies in it will not
  1060. * commit root.
  1061. * The special case is for qgroup to search roots in commit_transaction().
  1062. *
  1063. * If check_shared is set to 1, any extent has more than one ref item, will
  1064. * be returned BACKREF_FOUND_SHARED immediately.
  1065. *
  1066. * FIXME some caching might speed things up
  1067. */
  1068. static int find_parent_nodes(struct btrfs_trans_handle *trans,
  1069. struct btrfs_fs_info *fs_info, u64 bytenr,
  1070. u64 time_seq, struct ulist *refs,
  1071. struct ulist *roots, const u64 *extent_item_pos,
  1072. u64 root_objectid, u64 inum, int check_shared)
  1073. {
  1074. struct btrfs_key key;
  1075. struct btrfs_path *path;
  1076. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  1077. struct btrfs_delayed_ref_head *head;
  1078. int info_level = 0;
  1079. int ret;
  1080. struct list_head prefs_delayed;
  1081. struct list_head prefs;
  1082. struct __prelim_ref *ref;
  1083. struct extent_inode_elem *eie = NULL;
  1084. struct ref_root *ref_tree = NULL;
  1085. u64 total_refs = 0;
  1086. INIT_LIST_HEAD(&prefs);
  1087. INIT_LIST_HEAD(&prefs_delayed);
  1088. key.objectid = bytenr;
  1089. key.offset = (u64)-1;
  1090. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1091. key.type = BTRFS_METADATA_ITEM_KEY;
  1092. else
  1093. key.type = BTRFS_EXTENT_ITEM_KEY;
  1094. path = btrfs_alloc_path();
  1095. if (!path)
  1096. return -ENOMEM;
  1097. if (!trans) {
  1098. path->search_commit_root = 1;
  1099. path->skip_locking = 1;
  1100. }
  1101. if (time_seq == (u64)-1)
  1102. path->skip_locking = 1;
  1103. /*
  1104. * grab both a lock on the path and a lock on the delayed ref head.
  1105. * We need both to get a consistent picture of how the refs look
  1106. * at a specified point in time
  1107. */
  1108. again:
  1109. head = NULL;
  1110. if (check_shared) {
  1111. if (!ref_tree) {
  1112. ref_tree = ref_root_alloc();
  1113. if (!ref_tree) {
  1114. ret = -ENOMEM;
  1115. goto out;
  1116. }
  1117. } else {
  1118. ref_root_fini(ref_tree);
  1119. }
  1120. }
  1121. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  1122. if (ret < 0)
  1123. goto out;
  1124. BUG_ON(ret == 0);
  1125. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  1126. if (trans && likely(trans->type != __TRANS_DUMMY) &&
  1127. time_seq != (u64)-1) {
  1128. #else
  1129. if (trans && time_seq != (u64)-1) {
  1130. #endif
  1131. /*
  1132. * look if there are updates for this ref queued and lock the
  1133. * head
  1134. */
  1135. delayed_refs = &trans->transaction->delayed_refs;
  1136. spin_lock(&delayed_refs->lock);
  1137. head = btrfs_find_delayed_ref_head(trans, bytenr);
  1138. if (head) {
  1139. if (!mutex_trylock(&head->mutex)) {
  1140. atomic_inc(&head->node.refs);
  1141. spin_unlock(&delayed_refs->lock);
  1142. btrfs_release_path(path);
  1143. /*
  1144. * Mutex was contended, block until it's
  1145. * released and try again
  1146. */
  1147. mutex_lock(&head->mutex);
  1148. mutex_unlock(&head->mutex);
  1149. btrfs_put_delayed_ref(&head->node);
  1150. goto again;
  1151. }
  1152. spin_unlock(&delayed_refs->lock);
  1153. ret = __add_delayed_refs(head, time_seq,
  1154. &prefs_delayed, &total_refs,
  1155. inum);
  1156. mutex_unlock(&head->mutex);
  1157. if (ret)
  1158. goto out;
  1159. } else {
  1160. spin_unlock(&delayed_refs->lock);
  1161. }
  1162. if (check_shared && !list_empty(&prefs_delayed)) {
  1163. /*
  1164. * Add all delay_ref to the ref_tree and check if there
  1165. * are multiple ref items added.
  1166. */
  1167. list_for_each_entry(ref, &prefs_delayed, list) {
  1168. if (ref->key_for_search.type) {
  1169. ret = ref_tree_add(ref_tree,
  1170. ref->root_id,
  1171. ref->key_for_search.objectid,
  1172. ref->key_for_search.offset,
  1173. 0, ref->count);
  1174. if (ret)
  1175. goto out;
  1176. } else {
  1177. ret = ref_tree_add(ref_tree, 0, 0, 0,
  1178. ref->parent, ref->count);
  1179. if (ret)
  1180. goto out;
  1181. }
  1182. }
  1183. if (ref_tree->unique_refs > 1) {
  1184. ret = BACKREF_FOUND_SHARED;
  1185. goto out;
  1186. }
  1187. }
  1188. }
  1189. if (path->slots[0]) {
  1190. struct extent_buffer *leaf;
  1191. int slot;
  1192. path->slots[0]--;
  1193. leaf = path->nodes[0];
  1194. slot = path->slots[0];
  1195. btrfs_item_key_to_cpu(leaf, &key, slot);
  1196. if (key.objectid == bytenr &&
  1197. (key.type == BTRFS_EXTENT_ITEM_KEY ||
  1198. key.type == BTRFS_METADATA_ITEM_KEY)) {
  1199. ret = __add_inline_refs(fs_info, path, bytenr,
  1200. &info_level, &prefs,
  1201. ref_tree, &total_refs,
  1202. inum);
  1203. if (ret)
  1204. goto out;
  1205. ret = __add_keyed_refs(fs_info, path, bytenr,
  1206. info_level, &prefs,
  1207. ref_tree, inum);
  1208. if (ret)
  1209. goto out;
  1210. }
  1211. }
  1212. btrfs_release_path(path);
  1213. list_splice_init(&prefs_delayed, &prefs);
  1214. ret = __add_missing_keys(fs_info, &prefs);
  1215. if (ret)
  1216. goto out;
  1217. __merge_refs(&prefs, 1);
  1218. ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs,
  1219. extent_item_pos, total_refs,
  1220. root_objectid);
  1221. if (ret)
  1222. goto out;
  1223. __merge_refs(&prefs, 2);
  1224. while (!list_empty(&prefs)) {
  1225. ref = list_first_entry(&prefs, struct __prelim_ref, list);
  1226. WARN_ON(ref->count < 0);
  1227. if (roots && ref->count && ref->root_id && ref->parent == 0) {
  1228. if (root_objectid && ref->root_id != root_objectid) {
  1229. ret = BACKREF_FOUND_SHARED;
  1230. goto out;
  1231. }
  1232. /* no parent == root of tree */
  1233. ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
  1234. if (ret < 0)
  1235. goto out;
  1236. }
  1237. if (ref->count && ref->parent) {
  1238. if (extent_item_pos && !ref->inode_list &&
  1239. ref->level == 0) {
  1240. struct extent_buffer *eb;
  1241. eb = read_tree_block(fs_info->extent_root,
  1242. ref->parent, 0);
  1243. if (IS_ERR(eb)) {
  1244. ret = PTR_ERR(eb);
  1245. goto out;
  1246. } else if (!extent_buffer_uptodate(eb)) {
  1247. free_extent_buffer(eb);
  1248. ret = -EIO;
  1249. goto out;
  1250. }
  1251. btrfs_tree_read_lock(eb);
  1252. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1253. ret = find_extent_in_eb(eb, bytenr,
  1254. *extent_item_pos, &eie);
  1255. btrfs_tree_read_unlock_blocking(eb);
  1256. free_extent_buffer(eb);
  1257. if (ret < 0)
  1258. goto out;
  1259. ref->inode_list = eie;
  1260. }
  1261. ret = ulist_add_merge_ptr(refs, ref->parent,
  1262. ref->inode_list,
  1263. (void **)&eie, GFP_NOFS);
  1264. if (ret < 0)
  1265. goto out;
  1266. if (!ret && extent_item_pos) {
  1267. /*
  1268. * we've recorded that parent, so we must extend
  1269. * its inode list here
  1270. */
  1271. BUG_ON(!eie);
  1272. while (eie->next)
  1273. eie = eie->next;
  1274. eie->next = ref->inode_list;
  1275. }
  1276. eie = NULL;
  1277. }
  1278. list_del(&ref->list);
  1279. kmem_cache_free(btrfs_prelim_ref_cache, ref);
  1280. }
  1281. out:
  1282. btrfs_free_path(path);
  1283. ref_root_free(ref_tree);
  1284. while (!list_empty(&prefs)) {
  1285. ref = list_first_entry(&prefs, struct __prelim_ref, list);
  1286. list_del(&ref->list);
  1287. kmem_cache_free(btrfs_prelim_ref_cache, ref);
  1288. }
  1289. while (!list_empty(&prefs_delayed)) {
  1290. ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
  1291. list);
  1292. list_del(&ref->list);
  1293. kmem_cache_free(btrfs_prelim_ref_cache, ref);
  1294. }
  1295. if (ret < 0)
  1296. free_inode_elem_list(eie);
  1297. return ret;
  1298. }
  1299. static void free_leaf_list(struct ulist *blocks)
  1300. {
  1301. struct ulist_node *node = NULL;
  1302. struct extent_inode_elem *eie;
  1303. struct ulist_iterator uiter;
  1304. ULIST_ITER_INIT(&uiter);
  1305. while ((node = ulist_next(blocks, &uiter))) {
  1306. if (!node->aux)
  1307. continue;
  1308. eie = (struct extent_inode_elem *)(uintptr_t)node->aux;
  1309. free_inode_elem_list(eie);
  1310. node->aux = 0;
  1311. }
  1312. ulist_free(blocks);
  1313. }
  1314. /*
  1315. * Finds all leafs with a reference to the specified combination of bytenr and
  1316. * offset. key_list_head will point to a list of corresponding keys (caller must
  1317. * free each list element). The leafs will be stored in the leafs ulist, which
  1318. * must be freed with ulist_free.
  1319. *
  1320. * returns 0 on success, <0 on error
  1321. */
  1322. static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
  1323. struct btrfs_fs_info *fs_info, u64 bytenr,
  1324. u64 time_seq, struct ulist **leafs,
  1325. const u64 *extent_item_pos)
  1326. {
  1327. int ret;
  1328. *leafs = ulist_alloc(GFP_NOFS);
  1329. if (!*leafs)
  1330. return -ENOMEM;
  1331. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1332. *leafs, NULL, extent_item_pos, 0, 0, 0);
  1333. if (ret < 0 && ret != -ENOENT) {
  1334. free_leaf_list(*leafs);
  1335. return ret;
  1336. }
  1337. return 0;
  1338. }
  1339. /*
  1340. * walk all backrefs for a given extent to find all roots that reference this
  1341. * extent. Walking a backref means finding all extents that reference this
  1342. * extent and in turn walk the backrefs of those, too. Naturally this is a
  1343. * recursive process, but here it is implemented in an iterative fashion: We
  1344. * find all referencing extents for the extent in question and put them on a
  1345. * list. In turn, we find all referencing extents for those, further appending
  1346. * to the list. The way we iterate the list allows adding more elements after
  1347. * the current while iterating. The process stops when we reach the end of the
  1348. * list. Found roots are added to the roots list.
  1349. *
  1350. * returns 0 on success, < 0 on error.
  1351. */
  1352. static int __btrfs_find_all_roots(struct btrfs_trans_handle *trans,
  1353. struct btrfs_fs_info *fs_info, u64 bytenr,
  1354. u64 time_seq, struct ulist **roots)
  1355. {
  1356. struct ulist *tmp;
  1357. struct ulist_node *node = NULL;
  1358. struct ulist_iterator uiter;
  1359. int ret;
  1360. tmp = ulist_alloc(GFP_NOFS);
  1361. if (!tmp)
  1362. return -ENOMEM;
  1363. *roots = ulist_alloc(GFP_NOFS);
  1364. if (!*roots) {
  1365. ulist_free(tmp);
  1366. return -ENOMEM;
  1367. }
  1368. ULIST_ITER_INIT(&uiter);
  1369. while (1) {
  1370. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1371. tmp, *roots, NULL, 0, 0, 0);
  1372. if (ret < 0 && ret != -ENOENT) {
  1373. ulist_free(tmp);
  1374. ulist_free(*roots);
  1375. return ret;
  1376. }
  1377. node = ulist_next(tmp, &uiter);
  1378. if (!node)
  1379. break;
  1380. bytenr = node->val;
  1381. cond_resched();
  1382. }
  1383. ulist_free(tmp);
  1384. return 0;
  1385. }
  1386. int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
  1387. struct btrfs_fs_info *fs_info, u64 bytenr,
  1388. u64 time_seq, struct ulist **roots)
  1389. {
  1390. int ret;
  1391. if (!trans)
  1392. down_read(&fs_info->commit_root_sem);
  1393. ret = __btrfs_find_all_roots(trans, fs_info, bytenr, time_seq, roots);
  1394. if (!trans)
  1395. up_read(&fs_info->commit_root_sem);
  1396. return ret;
  1397. }
  1398. /**
  1399. * btrfs_check_shared - tell us whether an extent is shared
  1400. *
  1401. * @trans: optional trans handle
  1402. *
  1403. * btrfs_check_shared uses the backref walking code but will short
  1404. * circuit as soon as it finds a root or inode that doesn't match the
  1405. * one passed in. This provides a significant performance benefit for
  1406. * callers (such as fiemap) which want to know whether the extent is
  1407. * shared but do not need a ref count.
  1408. *
  1409. * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
  1410. */
  1411. int btrfs_check_shared(struct btrfs_trans_handle *trans,
  1412. struct btrfs_fs_info *fs_info, u64 root_objectid,
  1413. u64 inum, u64 bytenr)
  1414. {
  1415. struct ulist *tmp = NULL;
  1416. struct ulist *roots = NULL;
  1417. struct ulist_iterator uiter;
  1418. struct ulist_node *node;
  1419. struct seq_list elem = SEQ_LIST_INIT(elem);
  1420. int ret = 0;
  1421. tmp = ulist_alloc(GFP_NOFS);
  1422. roots = ulist_alloc(GFP_NOFS);
  1423. if (!tmp || !roots) {
  1424. ulist_free(tmp);
  1425. ulist_free(roots);
  1426. return -ENOMEM;
  1427. }
  1428. if (trans)
  1429. btrfs_get_tree_mod_seq(fs_info, &elem);
  1430. else
  1431. down_read(&fs_info->commit_root_sem);
  1432. ULIST_ITER_INIT(&uiter);
  1433. while (1) {
  1434. ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
  1435. roots, NULL, root_objectid, inum, 1);
  1436. if (ret == BACKREF_FOUND_SHARED) {
  1437. /* this is the only condition under which we return 1 */
  1438. ret = 1;
  1439. break;
  1440. }
  1441. if (ret < 0 && ret != -ENOENT)
  1442. break;
  1443. ret = 0;
  1444. node = ulist_next(tmp, &uiter);
  1445. if (!node)
  1446. break;
  1447. bytenr = node->val;
  1448. cond_resched();
  1449. }
  1450. if (trans)
  1451. btrfs_put_tree_mod_seq(fs_info, &elem);
  1452. else
  1453. up_read(&fs_info->commit_root_sem);
  1454. ulist_free(tmp);
  1455. ulist_free(roots);
  1456. return ret;
  1457. }
  1458. int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
  1459. u64 start_off, struct btrfs_path *path,
  1460. struct btrfs_inode_extref **ret_extref,
  1461. u64 *found_off)
  1462. {
  1463. int ret, slot;
  1464. struct btrfs_key key;
  1465. struct btrfs_key found_key;
  1466. struct btrfs_inode_extref *extref;
  1467. struct extent_buffer *leaf;
  1468. unsigned long ptr;
  1469. key.objectid = inode_objectid;
  1470. key.type = BTRFS_INODE_EXTREF_KEY;
  1471. key.offset = start_off;
  1472. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1473. if (ret < 0)
  1474. return ret;
  1475. while (1) {
  1476. leaf = path->nodes[0];
  1477. slot = path->slots[0];
  1478. if (slot >= btrfs_header_nritems(leaf)) {
  1479. /*
  1480. * If the item at offset is not found,
  1481. * btrfs_search_slot will point us to the slot
  1482. * where it should be inserted. In our case
  1483. * that will be the slot directly before the
  1484. * next INODE_REF_KEY_V2 item. In the case
  1485. * that we're pointing to the last slot in a
  1486. * leaf, we must move one leaf over.
  1487. */
  1488. ret = btrfs_next_leaf(root, path);
  1489. if (ret) {
  1490. if (ret >= 1)
  1491. ret = -ENOENT;
  1492. break;
  1493. }
  1494. continue;
  1495. }
  1496. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1497. /*
  1498. * Check that we're still looking at an extended ref key for
  1499. * this particular objectid. If we have different
  1500. * objectid or type then there are no more to be found
  1501. * in the tree and we can exit.
  1502. */
  1503. ret = -ENOENT;
  1504. if (found_key.objectid != inode_objectid)
  1505. break;
  1506. if (found_key.type != BTRFS_INODE_EXTREF_KEY)
  1507. break;
  1508. ret = 0;
  1509. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1510. extref = (struct btrfs_inode_extref *)ptr;
  1511. *ret_extref = extref;
  1512. if (found_off)
  1513. *found_off = found_key.offset;
  1514. break;
  1515. }
  1516. return ret;
  1517. }
  1518. /*
  1519. * this iterates to turn a name (from iref/extref) into a full filesystem path.
  1520. * Elements of the path are separated by '/' and the path is guaranteed to be
  1521. * 0-terminated. the path is only given within the current file system.
  1522. * Therefore, it never starts with a '/'. the caller is responsible to provide
  1523. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  1524. * the start point of the resulting string is returned. this pointer is within
  1525. * dest, normally.
  1526. * in case the path buffer would overflow, the pointer is decremented further
  1527. * as if output was written to the buffer, though no more output is actually
  1528. * generated. that way, the caller can determine how much space would be
  1529. * required for the path to fit into the buffer. in that case, the returned
  1530. * value will be smaller than dest. callers must check this!
  1531. */
  1532. char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  1533. u32 name_len, unsigned long name_off,
  1534. struct extent_buffer *eb_in, u64 parent,
  1535. char *dest, u32 size)
  1536. {
  1537. int slot;
  1538. u64 next_inum;
  1539. int ret;
  1540. s64 bytes_left = ((s64)size) - 1;
  1541. struct extent_buffer *eb = eb_in;
  1542. struct btrfs_key found_key;
  1543. int leave_spinning = path->leave_spinning;
  1544. struct btrfs_inode_ref *iref;
  1545. if (bytes_left >= 0)
  1546. dest[bytes_left] = '\0';
  1547. path->leave_spinning = 1;
  1548. while (1) {
  1549. bytes_left -= name_len;
  1550. if (bytes_left >= 0)
  1551. read_extent_buffer(eb, dest + bytes_left,
  1552. name_off, name_len);
  1553. if (eb != eb_in) {
  1554. if (!path->skip_locking)
  1555. btrfs_tree_read_unlock_blocking(eb);
  1556. free_extent_buffer(eb);
  1557. }
  1558. ret = btrfs_find_item(fs_root, path, parent, 0,
  1559. BTRFS_INODE_REF_KEY, &found_key);
  1560. if (ret > 0)
  1561. ret = -ENOENT;
  1562. if (ret)
  1563. break;
  1564. next_inum = found_key.offset;
  1565. /* regular exit ahead */
  1566. if (parent == next_inum)
  1567. break;
  1568. slot = path->slots[0];
  1569. eb = path->nodes[0];
  1570. /* make sure we can use eb after releasing the path */
  1571. if (eb != eb_in) {
  1572. if (!path->skip_locking)
  1573. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1574. path->nodes[0] = NULL;
  1575. path->locks[0] = 0;
  1576. }
  1577. btrfs_release_path(path);
  1578. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1579. name_len = btrfs_inode_ref_name_len(eb, iref);
  1580. name_off = (unsigned long)(iref + 1);
  1581. parent = next_inum;
  1582. --bytes_left;
  1583. if (bytes_left >= 0)
  1584. dest[bytes_left] = '/';
  1585. }
  1586. btrfs_release_path(path);
  1587. path->leave_spinning = leave_spinning;
  1588. if (ret)
  1589. return ERR_PTR(ret);
  1590. return dest + bytes_left;
  1591. }
  1592. /*
  1593. * this makes the path point to (logical EXTENT_ITEM *)
  1594. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  1595. * tree blocks and <0 on error.
  1596. */
  1597. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  1598. struct btrfs_path *path, struct btrfs_key *found_key,
  1599. u64 *flags_ret)
  1600. {
  1601. int ret;
  1602. u64 flags;
  1603. u64 size = 0;
  1604. u32 item_size;
  1605. struct extent_buffer *eb;
  1606. struct btrfs_extent_item *ei;
  1607. struct btrfs_key key;
  1608. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1609. key.type = BTRFS_METADATA_ITEM_KEY;
  1610. else
  1611. key.type = BTRFS_EXTENT_ITEM_KEY;
  1612. key.objectid = logical;
  1613. key.offset = (u64)-1;
  1614. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  1615. if (ret < 0)
  1616. return ret;
  1617. ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
  1618. if (ret) {
  1619. if (ret > 0)
  1620. ret = -ENOENT;
  1621. return ret;
  1622. }
  1623. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  1624. if (found_key->type == BTRFS_METADATA_ITEM_KEY)
  1625. size = fs_info->extent_root->nodesize;
  1626. else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
  1627. size = found_key->offset;
  1628. if (found_key->objectid > logical ||
  1629. found_key->objectid + size <= logical) {
  1630. btrfs_debug(fs_info,
  1631. "logical %llu is not within any extent", logical);
  1632. return -ENOENT;
  1633. }
  1634. eb = path->nodes[0];
  1635. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  1636. BUG_ON(item_size < sizeof(*ei));
  1637. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  1638. flags = btrfs_extent_flags(eb, ei);
  1639. btrfs_debug(fs_info,
  1640. "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
  1641. logical, logical - found_key->objectid, found_key->objectid,
  1642. found_key->offset, flags, item_size);
  1643. WARN_ON(!flags_ret);
  1644. if (flags_ret) {
  1645. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1646. *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
  1647. else if (flags & BTRFS_EXTENT_FLAG_DATA)
  1648. *flags_ret = BTRFS_EXTENT_FLAG_DATA;
  1649. else
  1650. BUG_ON(1);
  1651. return 0;
  1652. }
  1653. return -EIO;
  1654. }
  1655. /*
  1656. * helper function to iterate extent inline refs. ptr must point to a 0 value
  1657. * for the first call and may be modified. it is used to track state.
  1658. * if more refs exist, 0 is returned and the next call to
  1659. * __get_extent_inline_ref must pass the modified ptr parameter to get the
  1660. * next ref. after the last ref was processed, 1 is returned.
  1661. * returns <0 on error
  1662. */
  1663. static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
  1664. struct btrfs_key *key,
  1665. struct btrfs_extent_item *ei, u32 item_size,
  1666. struct btrfs_extent_inline_ref **out_eiref,
  1667. int *out_type)
  1668. {
  1669. unsigned long end;
  1670. u64 flags;
  1671. struct btrfs_tree_block_info *info;
  1672. if (!*ptr) {
  1673. /* first call */
  1674. flags = btrfs_extent_flags(eb, ei);
  1675. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1676. if (key->type == BTRFS_METADATA_ITEM_KEY) {
  1677. /* a skinny metadata extent */
  1678. *out_eiref =
  1679. (struct btrfs_extent_inline_ref *)(ei + 1);
  1680. } else {
  1681. WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
  1682. info = (struct btrfs_tree_block_info *)(ei + 1);
  1683. *out_eiref =
  1684. (struct btrfs_extent_inline_ref *)(info + 1);
  1685. }
  1686. } else {
  1687. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  1688. }
  1689. *ptr = (unsigned long)*out_eiref;
  1690. if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
  1691. return -ENOENT;
  1692. }
  1693. end = (unsigned long)ei + item_size;
  1694. *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
  1695. *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
  1696. *ptr += btrfs_extent_inline_ref_size(*out_type);
  1697. WARN_ON(*ptr > end);
  1698. if (*ptr == end)
  1699. return 1; /* last */
  1700. return 0;
  1701. }
  1702. /*
  1703. * reads the tree block backref for an extent. tree level and root are returned
  1704. * through out_level and out_root. ptr must point to a 0 value for the first
  1705. * call and may be modified (see __get_extent_inline_ref comment).
  1706. * returns 0 if data was provided, 1 if there was no more data to provide or
  1707. * <0 on error.
  1708. */
  1709. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  1710. struct btrfs_key *key, struct btrfs_extent_item *ei,
  1711. u32 item_size, u64 *out_root, u8 *out_level)
  1712. {
  1713. int ret;
  1714. int type;
  1715. struct btrfs_extent_inline_ref *eiref;
  1716. if (*ptr == (unsigned long)-1)
  1717. return 1;
  1718. while (1) {
  1719. ret = __get_extent_inline_ref(ptr, eb, key, ei, item_size,
  1720. &eiref, &type);
  1721. if (ret < 0)
  1722. return ret;
  1723. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1724. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1725. break;
  1726. if (ret == 1)
  1727. return 1;
  1728. }
  1729. /* we can treat both ref types equally here */
  1730. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  1731. if (key->type == BTRFS_EXTENT_ITEM_KEY) {
  1732. struct btrfs_tree_block_info *info;
  1733. info = (struct btrfs_tree_block_info *)(ei + 1);
  1734. *out_level = btrfs_tree_block_level(eb, info);
  1735. } else {
  1736. ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
  1737. *out_level = (u8)key->offset;
  1738. }
  1739. if (ret == 1)
  1740. *ptr = (unsigned long)-1;
  1741. return 0;
  1742. }
  1743. static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
  1744. struct extent_inode_elem *inode_list,
  1745. u64 root, u64 extent_item_objectid,
  1746. iterate_extent_inodes_t *iterate, void *ctx)
  1747. {
  1748. struct extent_inode_elem *eie;
  1749. int ret = 0;
  1750. for (eie = inode_list; eie; eie = eie->next) {
  1751. btrfs_debug(fs_info,
  1752. "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
  1753. extent_item_objectid, eie->inum,
  1754. eie->offset, root);
  1755. ret = iterate(eie->inum, eie->offset, root, ctx);
  1756. if (ret) {
  1757. btrfs_debug(fs_info,
  1758. "stopping iteration for %llu due to ret=%d",
  1759. extent_item_objectid, ret);
  1760. break;
  1761. }
  1762. }
  1763. return ret;
  1764. }
  1765. /*
  1766. * calls iterate() for every inode that references the extent identified by
  1767. * the given parameters.
  1768. * when the iterator function returns a non-zero value, iteration stops.
  1769. */
  1770. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1771. u64 extent_item_objectid, u64 extent_item_pos,
  1772. int search_commit_root,
  1773. iterate_extent_inodes_t *iterate, void *ctx)
  1774. {
  1775. int ret;
  1776. struct btrfs_trans_handle *trans = NULL;
  1777. struct ulist *refs = NULL;
  1778. struct ulist *roots = NULL;
  1779. struct ulist_node *ref_node = NULL;
  1780. struct ulist_node *root_node = NULL;
  1781. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  1782. struct ulist_iterator ref_uiter;
  1783. struct ulist_iterator root_uiter;
  1784. btrfs_debug(fs_info, "resolving all inodes for extent %llu",
  1785. extent_item_objectid);
  1786. if (!search_commit_root) {
  1787. trans = btrfs_attach_transaction(fs_info->extent_root);
  1788. if (IS_ERR(trans)) {
  1789. if (PTR_ERR(trans) != -ENOENT &&
  1790. PTR_ERR(trans) != -EROFS)
  1791. return PTR_ERR(trans);
  1792. trans = NULL;
  1793. }
  1794. }
  1795. if (trans)
  1796. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1797. else
  1798. down_read(&fs_info->commit_root_sem);
  1799. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1800. tree_mod_seq_elem.seq, &refs,
  1801. &extent_item_pos);
  1802. if (ret)
  1803. goto out;
  1804. ULIST_ITER_INIT(&ref_uiter);
  1805. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  1806. ret = __btrfs_find_all_roots(trans, fs_info, ref_node->val,
  1807. tree_mod_seq_elem.seq, &roots);
  1808. if (ret)
  1809. break;
  1810. ULIST_ITER_INIT(&root_uiter);
  1811. while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
  1812. btrfs_debug(fs_info,
  1813. "root %llu references leaf %llu, data list %#llx",
  1814. root_node->val, ref_node->val,
  1815. ref_node->aux);
  1816. ret = iterate_leaf_refs(fs_info,
  1817. (struct extent_inode_elem *)
  1818. (uintptr_t)ref_node->aux,
  1819. root_node->val,
  1820. extent_item_objectid,
  1821. iterate, ctx);
  1822. }
  1823. ulist_free(roots);
  1824. }
  1825. free_leaf_list(refs);
  1826. out:
  1827. if (trans) {
  1828. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1829. btrfs_end_transaction(trans, fs_info->extent_root);
  1830. } else {
  1831. up_read(&fs_info->commit_root_sem);
  1832. }
  1833. return ret;
  1834. }
  1835. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1836. struct btrfs_path *path,
  1837. iterate_extent_inodes_t *iterate, void *ctx)
  1838. {
  1839. int ret;
  1840. u64 extent_item_pos;
  1841. u64 flags = 0;
  1842. struct btrfs_key found_key;
  1843. int search_commit_root = path->search_commit_root;
  1844. ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
  1845. btrfs_release_path(path);
  1846. if (ret < 0)
  1847. return ret;
  1848. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1849. return -EINVAL;
  1850. extent_item_pos = logical - found_key.objectid;
  1851. ret = iterate_extent_inodes(fs_info, found_key.objectid,
  1852. extent_item_pos, search_commit_root,
  1853. iterate, ctx);
  1854. return ret;
  1855. }
  1856. typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
  1857. struct extent_buffer *eb, void *ctx);
  1858. static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
  1859. struct btrfs_path *path,
  1860. iterate_irefs_t *iterate, void *ctx)
  1861. {
  1862. int ret = 0;
  1863. int slot;
  1864. u32 cur;
  1865. u32 len;
  1866. u32 name_len;
  1867. u64 parent = 0;
  1868. int found = 0;
  1869. struct extent_buffer *eb;
  1870. struct btrfs_item *item;
  1871. struct btrfs_inode_ref *iref;
  1872. struct btrfs_key found_key;
  1873. while (!ret) {
  1874. ret = btrfs_find_item(fs_root, path, inum,
  1875. parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
  1876. &found_key);
  1877. if (ret < 0)
  1878. break;
  1879. if (ret) {
  1880. ret = found ? 0 : -ENOENT;
  1881. break;
  1882. }
  1883. ++found;
  1884. parent = found_key.offset;
  1885. slot = path->slots[0];
  1886. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1887. if (!eb) {
  1888. ret = -ENOMEM;
  1889. break;
  1890. }
  1891. extent_buffer_get(eb);
  1892. btrfs_tree_read_lock(eb);
  1893. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1894. btrfs_release_path(path);
  1895. item = btrfs_item_nr(slot);
  1896. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1897. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1898. name_len = btrfs_inode_ref_name_len(eb, iref);
  1899. /* path must be released before calling iterate()! */
  1900. btrfs_debug(fs_root->fs_info,
  1901. "following ref at offset %u for inode %llu in tree %llu",
  1902. cur, found_key.objectid, fs_root->objectid);
  1903. ret = iterate(parent, name_len,
  1904. (unsigned long)(iref + 1), eb, ctx);
  1905. if (ret)
  1906. break;
  1907. len = sizeof(*iref) + name_len;
  1908. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1909. }
  1910. btrfs_tree_read_unlock_blocking(eb);
  1911. free_extent_buffer(eb);
  1912. }
  1913. btrfs_release_path(path);
  1914. return ret;
  1915. }
  1916. static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
  1917. struct btrfs_path *path,
  1918. iterate_irefs_t *iterate, void *ctx)
  1919. {
  1920. int ret;
  1921. int slot;
  1922. u64 offset = 0;
  1923. u64 parent;
  1924. int found = 0;
  1925. struct extent_buffer *eb;
  1926. struct btrfs_inode_extref *extref;
  1927. u32 item_size;
  1928. u32 cur_offset;
  1929. unsigned long ptr;
  1930. while (1) {
  1931. ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
  1932. &offset);
  1933. if (ret < 0)
  1934. break;
  1935. if (ret) {
  1936. ret = found ? 0 : -ENOENT;
  1937. break;
  1938. }
  1939. ++found;
  1940. slot = path->slots[0];
  1941. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1942. if (!eb) {
  1943. ret = -ENOMEM;
  1944. break;
  1945. }
  1946. extent_buffer_get(eb);
  1947. btrfs_tree_read_lock(eb);
  1948. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1949. btrfs_release_path(path);
  1950. item_size = btrfs_item_size_nr(eb, slot);
  1951. ptr = btrfs_item_ptr_offset(eb, slot);
  1952. cur_offset = 0;
  1953. while (cur_offset < item_size) {
  1954. u32 name_len;
  1955. extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
  1956. parent = btrfs_inode_extref_parent(eb, extref);
  1957. name_len = btrfs_inode_extref_name_len(eb, extref);
  1958. ret = iterate(parent, name_len,
  1959. (unsigned long)&extref->name, eb, ctx);
  1960. if (ret)
  1961. break;
  1962. cur_offset += btrfs_inode_extref_name_len(eb, extref);
  1963. cur_offset += sizeof(*extref);
  1964. }
  1965. btrfs_tree_read_unlock_blocking(eb);
  1966. free_extent_buffer(eb);
  1967. offset++;
  1968. }
  1969. btrfs_release_path(path);
  1970. return ret;
  1971. }
  1972. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1973. struct btrfs_path *path, iterate_irefs_t *iterate,
  1974. void *ctx)
  1975. {
  1976. int ret;
  1977. int found_refs = 0;
  1978. ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
  1979. if (!ret)
  1980. ++found_refs;
  1981. else if (ret != -ENOENT)
  1982. return ret;
  1983. ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
  1984. if (ret == -ENOENT && found_refs)
  1985. return 0;
  1986. return ret;
  1987. }
  1988. /*
  1989. * returns 0 if the path could be dumped (probably truncated)
  1990. * returns <0 in case of an error
  1991. */
  1992. static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
  1993. struct extent_buffer *eb, void *ctx)
  1994. {
  1995. struct inode_fs_paths *ipath = ctx;
  1996. char *fspath;
  1997. char *fspath_min;
  1998. int i = ipath->fspath->elem_cnt;
  1999. const int s_ptr = sizeof(char *);
  2000. u32 bytes_left;
  2001. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  2002. ipath->fspath->bytes_left - s_ptr : 0;
  2003. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  2004. fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
  2005. name_off, eb, inum, fspath_min, bytes_left);
  2006. if (IS_ERR(fspath))
  2007. return PTR_ERR(fspath);
  2008. if (fspath > fspath_min) {
  2009. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  2010. ++ipath->fspath->elem_cnt;
  2011. ipath->fspath->bytes_left = fspath - fspath_min;
  2012. } else {
  2013. ++ipath->fspath->elem_missed;
  2014. ipath->fspath->bytes_missing += fspath_min - fspath;
  2015. ipath->fspath->bytes_left = 0;
  2016. }
  2017. return 0;
  2018. }
  2019. /*
  2020. * this dumps all file system paths to the inode into the ipath struct, provided
  2021. * is has been created large enough. each path is zero-terminated and accessed
  2022. * from ipath->fspath->val[i].
  2023. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  2024. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  2025. * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
  2026. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  2027. * have been needed to return all paths.
  2028. */
  2029. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  2030. {
  2031. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  2032. inode_to_path, ipath);
  2033. }
  2034. struct btrfs_data_container *init_data_container(u32 total_bytes)
  2035. {
  2036. struct btrfs_data_container *data;
  2037. size_t alloc_bytes;
  2038. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  2039. data = vmalloc(alloc_bytes);
  2040. if (!data)
  2041. return ERR_PTR(-ENOMEM);
  2042. if (total_bytes >= sizeof(*data)) {
  2043. data->bytes_left = total_bytes - sizeof(*data);
  2044. data->bytes_missing = 0;
  2045. } else {
  2046. data->bytes_missing = sizeof(*data) - total_bytes;
  2047. data->bytes_left = 0;
  2048. }
  2049. data->elem_cnt = 0;
  2050. data->elem_missed = 0;
  2051. return data;
  2052. }
  2053. /*
  2054. * allocates space to return multiple file system paths for an inode.
  2055. * total_bytes to allocate are passed, note that space usable for actual path
  2056. * information will be total_bytes - sizeof(struct inode_fs_paths).
  2057. * the returned pointer must be freed with free_ipath() in the end.
  2058. */
  2059. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  2060. struct btrfs_path *path)
  2061. {
  2062. struct inode_fs_paths *ifp;
  2063. struct btrfs_data_container *fspath;
  2064. fspath = init_data_container(total_bytes);
  2065. if (IS_ERR(fspath))
  2066. return (void *)fspath;
  2067. ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
  2068. if (!ifp) {
  2069. vfree(fspath);
  2070. return ERR_PTR(-ENOMEM);
  2071. }
  2072. ifp->btrfs_path = path;
  2073. ifp->fspath = fspath;
  2074. ifp->fs_root = fs_root;
  2075. return ifp;
  2076. }
  2077. void free_ipath(struct inode_fs_paths *ipath)
  2078. {
  2079. if (!ipath)
  2080. return;
  2081. vfree(ipath->fspath);
  2082. kfree(ipath);
  2083. }