backref.c 59 KB

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