backref.c 60 KB

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