backref.c 60 KB

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