backref.c 57 KB

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