backref.c 60 KB

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