backref.c 60 KB

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