backref.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  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. cond_resched();
  587. }
  588. out:
  589. ulist_free(parents);
  590. return ret;
  591. }
  592. /*
  593. * read tree blocks and add keys where required.
  594. */
  595. static int add_missing_keys(struct btrfs_fs_info *fs_info,
  596. struct preftrees *preftrees)
  597. {
  598. struct prelim_ref *ref;
  599. struct extent_buffer *eb;
  600. struct preftree *tree = &preftrees->indirect_missing_keys;
  601. struct rb_node *node;
  602. while ((node = rb_first(&tree->root))) {
  603. ref = rb_entry(node, struct prelim_ref, rbnode);
  604. rb_erase(node, &tree->root);
  605. BUG_ON(ref->parent); /* should not be a direct ref */
  606. BUG_ON(ref->key_for_search.type);
  607. BUG_ON(!ref->wanted_disk_byte);
  608. eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
  609. if (IS_ERR(eb)) {
  610. free_pref(ref);
  611. return PTR_ERR(eb);
  612. } else if (!extent_buffer_uptodate(eb)) {
  613. free_pref(ref);
  614. free_extent_buffer(eb);
  615. return -EIO;
  616. }
  617. btrfs_tree_read_lock(eb);
  618. if (btrfs_header_level(eb) == 0)
  619. btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
  620. else
  621. btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
  622. btrfs_tree_read_unlock(eb);
  623. free_extent_buffer(eb);
  624. prelim_ref_insert(fs_info, &preftrees->indirect, ref);
  625. cond_resched();
  626. }
  627. return 0;
  628. }
  629. /*
  630. * add all currently queued delayed refs from this head whose seq nr is
  631. * smaller or equal that seq to the list
  632. */
  633. static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
  634. struct btrfs_delayed_ref_head *head, u64 seq,
  635. struct preftrees *preftrees, u64 *total_refs,
  636. u64 inum)
  637. {
  638. struct btrfs_delayed_ref_node *node;
  639. struct btrfs_delayed_extent_op *extent_op = head->extent_op;
  640. struct btrfs_key key;
  641. struct btrfs_key tmp_op_key;
  642. struct btrfs_key *op_key = NULL;
  643. int sgn;
  644. int ret = 0;
  645. if (extent_op && extent_op->update_key) {
  646. btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key);
  647. op_key = &tmp_op_key;
  648. }
  649. spin_lock(&head->lock);
  650. list_for_each_entry(node, &head->ref_list, list) {
  651. if (node->seq > seq)
  652. continue;
  653. switch (node->action) {
  654. case BTRFS_ADD_DELAYED_EXTENT:
  655. case BTRFS_UPDATE_DELAYED_HEAD:
  656. WARN_ON(1);
  657. continue;
  658. case BTRFS_ADD_DELAYED_REF:
  659. sgn = 1;
  660. break;
  661. case BTRFS_DROP_DELAYED_REF:
  662. sgn = -1;
  663. break;
  664. default:
  665. BUG_ON(1);
  666. }
  667. *total_refs += (node->ref_mod * sgn);
  668. switch (node->type) {
  669. case BTRFS_TREE_BLOCK_REF_KEY: {
  670. /* NORMAL INDIRECT METADATA backref */
  671. struct btrfs_delayed_tree_ref *ref;
  672. ref = btrfs_delayed_node_to_tree_ref(node);
  673. ret = add_indirect_ref(fs_info, preftrees, ref->root,
  674. &tmp_op_key, ref->level + 1,
  675. node->bytenr,
  676. node->ref_mod * sgn,
  677. GFP_ATOMIC);
  678. break;
  679. }
  680. case BTRFS_SHARED_BLOCK_REF_KEY: {
  681. /* SHARED DIRECT METADATA backref */
  682. struct btrfs_delayed_tree_ref *ref;
  683. ref = btrfs_delayed_node_to_tree_ref(node);
  684. ret = add_direct_ref(fs_info, preftrees,
  685. ref->level + 1, ref->parent,
  686. node->bytenr, node->ref_mod * sgn,
  687. GFP_ATOMIC);
  688. break;
  689. }
  690. case BTRFS_EXTENT_DATA_REF_KEY: {
  691. /* NORMAL INDIRECT DATA backref */
  692. struct btrfs_delayed_data_ref *ref;
  693. ref = btrfs_delayed_node_to_data_ref(node);
  694. key.objectid = ref->objectid;
  695. key.type = BTRFS_EXTENT_DATA_KEY;
  696. key.offset = ref->offset;
  697. /*
  698. * Found a inum that doesn't match our known inum, we
  699. * know it's shared.
  700. */
  701. if (inum && ref->objectid != inum) {
  702. ret = BACKREF_FOUND_SHARED;
  703. break;
  704. }
  705. ret = add_indirect_ref(fs_info, preftrees, ref->root,
  706. &key, 0, node->bytenr,
  707. node->ref_mod * sgn,
  708. GFP_ATOMIC);
  709. break;
  710. }
  711. case BTRFS_SHARED_DATA_REF_KEY: {
  712. /* SHARED DIRECT FULL backref */
  713. struct btrfs_delayed_data_ref *ref;
  714. ref = btrfs_delayed_node_to_data_ref(node);
  715. ret = add_direct_ref(fs_info, preftrees, 0,
  716. ref->parent, node->bytenr,
  717. node->ref_mod * sgn,
  718. GFP_ATOMIC);
  719. break;
  720. }
  721. default:
  722. WARN_ON(1);
  723. }
  724. if (ret)
  725. break;
  726. }
  727. spin_unlock(&head->lock);
  728. return ret;
  729. }
  730. /*
  731. * add all inline backrefs for bytenr to the list
  732. */
  733. static int add_inline_refs(const struct btrfs_fs_info *fs_info,
  734. struct btrfs_path *path, u64 bytenr,
  735. int *info_level, struct preftrees *preftrees,
  736. u64 *total_refs, u64 inum)
  737. {
  738. int ret = 0;
  739. int slot;
  740. struct extent_buffer *leaf;
  741. struct btrfs_key key;
  742. struct btrfs_key found_key;
  743. unsigned long ptr;
  744. unsigned long end;
  745. struct btrfs_extent_item *ei;
  746. u64 flags;
  747. u64 item_size;
  748. /*
  749. * enumerate all inline refs
  750. */
  751. leaf = path->nodes[0];
  752. slot = path->slots[0];
  753. item_size = btrfs_item_size_nr(leaf, slot);
  754. BUG_ON(item_size < sizeof(*ei));
  755. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  756. flags = btrfs_extent_flags(leaf, ei);
  757. *total_refs += btrfs_extent_refs(leaf, ei);
  758. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  759. ptr = (unsigned long)(ei + 1);
  760. end = (unsigned long)ei + item_size;
  761. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  762. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  763. struct btrfs_tree_block_info *info;
  764. info = (struct btrfs_tree_block_info *)ptr;
  765. *info_level = btrfs_tree_block_level(leaf, info);
  766. ptr += sizeof(struct btrfs_tree_block_info);
  767. BUG_ON(ptr > end);
  768. } else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
  769. *info_level = found_key.offset;
  770. } else {
  771. BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
  772. }
  773. while (ptr < end) {
  774. struct btrfs_extent_inline_ref *iref;
  775. u64 offset;
  776. int type;
  777. iref = (struct btrfs_extent_inline_ref *)ptr;
  778. type = btrfs_extent_inline_ref_type(leaf, iref);
  779. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  780. switch (type) {
  781. case BTRFS_SHARED_BLOCK_REF_KEY:
  782. ret = add_direct_ref(fs_info, preftrees,
  783. *info_level + 1, offset,
  784. bytenr, 1, GFP_NOFS);
  785. break;
  786. case BTRFS_SHARED_DATA_REF_KEY: {
  787. struct btrfs_shared_data_ref *sdref;
  788. int count;
  789. sdref = (struct btrfs_shared_data_ref *)(iref + 1);
  790. count = btrfs_shared_data_ref_count(leaf, sdref);
  791. ret = add_direct_ref(fs_info, preftrees, 0, offset,
  792. bytenr, count, GFP_NOFS);
  793. break;
  794. }
  795. case BTRFS_TREE_BLOCK_REF_KEY:
  796. ret = add_indirect_ref(fs_info, preftrees, offset,
  797. NULL, *info_level + 1,
  798. bytenr, 1, GFP_NOFS);
  799. break;
  800. case BTRFS_EXTENT_DATA_REF_KEY: {
  801. struct btrfs_extent_data_ref *dref;
  802. int count;
  803. u64 root;
  804. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  805. count = btrfs_extent_data_ref_count(leaf, dref);
  806. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  807. dref);
  808. key.type = BTRFS_EXTENT_DATA_KEY;
  809. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  810. if (inum && key.objectid != inum) {
  811. ret = BACKREF_FOUND_SHARED;
  812. break;
  813. }
  814. root = btrfs_extent_data_ref_root(leaf, dref);
  815. ret = add_indirect_ref(fs_info, preftrees, root,
  816. &key, 0, bytenr, count,
  817. GFP_NOFS);
  818. break;
  819. }
  820. default:
  821. WARN_ON(1);
  822. }
  823. if (ret)
  824. return ret;
  825. ptr += btrfs_extent_inline_ref_size(type);
  826. }
  827. return 0;
  828. }
  829. /*
  830. * add all non-inline backrefs for bytenr to the list
  831. */
  832. static int add_keyed_refs(struct btrfs_fs_info *fs_info,
  833. struct btrfs_path *path, u64 bytenr,
  834. int info_level, struct preftrees *preftrees,
  835. u64 inum)
  836. {
  837. struct btrfs_root *extent_root = fs_info->extent_root;
  838. int ret;
  839. int slot;
  840. struct extent_buffer *leaf;
  841. struct btrfs_key key;
  842. while (1) {
  843. ret = btrfs_next_item(extent_root, path);
  844. if (ret < 0)
  845. break;
  846. if (ret) {
  847. ret = 0;
  848. break;
  849. }
  850. slot = path->slots[0];
  851. leaf = path->nodes[0];
  852. btrfs_item_key_to_cpu(leaf, &key, slot);
  853. if (key.objectid != bytenr)
  854. break;
  855. if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
  856. continue;
  857. if (key.type > BTRFS_SHARED_DATA_REF_KEY)
  858. break;
  859. switch (key.type) {
  860. case BTRFS_SHARED_BLOCK_REF_KEY:
  861. /* SHARED DIRECT METADATA backref */
  862. ret = add_direct_ref(fs_info, preftrees,
  863. info_level + 1, key.offset,
  864. bytenr, 1, GFP_NOFS);
  865. break;
  866. case BTRFS_SHARED_DATA_REF_KEY: {
  867. /* SHARED DIRECT FULL backref */
  868. struct btrfs_shared_data_ref *sdref;
  869. int count;
  870. sdref = btrfs_item_ptr(leaf, slot,
  871. struct btrfs_shared_data_ref);
  872. count = btrfs_shared_data_ref_count(leaf, sdref);
  873. ret = add_direct_ref(fs_info, preftrees, 0,
  874. key.offset, bytenr, count,
  875. GFP_NOFS);
  876. break;
  877. }
  878. case BTRFS_TREE_BLOCK_REF_KEY:
  879. /* NORMAL INDIRECT METADATA backref */
  880. ret = add_indirect_ref(fs_info, preftrees, key.offset,
  881. NULL, info_level + 1, bytenr,
  882. 1, GFP_NOFS);
  883. break;
  884. case BTRFS_EXTENT_DATA_REF_KEY: {
  885. /* NORMAL INDIRECT DATA backref */
  886. struct btrfs_extent_data_ref *dref;
  887. int count;
  888. u64 root;
  889. dref = btrfs_item_ptr(leaf, slot,
  890. struct btrfs_extent_data_ref);
  891. count = btrfs_extent_data_ref_count(leaf, dref);
  892. key.objectid = btrfs_extent_data_ref_objectid(leaf,
  893. dref);
  894. key.type = BTRFS_EXTENT_DATA_KEY;
  895. key.offset = btrfs_extent_data_ref_offset(leaf, dref);
  896. if (inum && key.objectid != inum) {
  897. ret = BACKREF_FOUND_SHARED;
  898. break;
  899. }
  900. root = btrfs_extent_data_ref_root(leaf, dref);
  901. ret = add_indirect_ref(fs_info, preftrees, root,
  902. &key, 0, bytenr, count,
  903. GFP_NOFS);
  904. break;
  905. }
  906. default:
  907. WARN_ON(1);
  908. }
  909. if (ret)
  910. return ret;
  911. }
  912. return ret;
  913. }
  914. /*
  915. * this adds all existing backrefs (inline backrefs, backrefs and delayed
  916. * refs) for the given bytenr to the refs list, merges duplicates and resolves
  917. * indirect refs to their parent bytenr.
  918. * When roots are found, they're added to the roots list
  919. *
  920. * NOTE: This can return values > 0
  921. *
  922. * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
  923. * much like trans == NULL case, the difference only lies in it will not
  924. * commit root.
  925. * The special case is for qgroup to search roots in commit_transaction().
  926. *
  927. * FIXME some caching might speed things up
  928. */
  929. static int find_parent_nodes(struct btrfs_trans_handle *trans,
  930. struct btrfs_fs_info *fs_info, u64 bytenr,
  931. u64 time_seq, struct ulist *refs,
  932. struct ulist *roots, const u64 *extent_item_pos,
  933. u64 root_objectid, u64 inum)
  934. {
  935. struct btrfs_key key;
  936. struct btrfs_path *path;
  937. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  938. struct btrfs_delayed_ref_head *head;
  939. int info_level = 0;
  940. int ret;
  941. struct prelim_ref *ref;
  942. struct rb_node *node;
  943. struct extent_inode_elem *eie = NULL;
  944. /* total of both direct AND indirect refs! */
  945. u64 total_refs = 0;
  946. struct preftrees preftrees = {
  947. .direct = PREFTREE_INIT,
  948. .indirect = PREFTREE_INIT,
  949. .indirect_missing_keys = PREFTREE_INIT
  950. };
  951. key.objectid = bytenr;
  952. key.offset = (u64)-1;
  953. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  954. key.type = BTRFS_METADATA_ITEM_KEY;
  955. else
  956. key.type = BTRFS_EXTENT_ITEM_KEY;
  957. path = btrfs_alloc_path();
  958. if (!path)
  959. return -ENOMEM;
  960. if (!trans) {
  961. path->search_commit_root = 1;
  962. path->skip_locking = 1;
  963. }
  964. if (time_seq == SEQ_LAST)
  965. path->skip_locking = 1;
  966. /*
  967. * grab both a lock on the path and a lock on the delayed ref head.
  968. * We need both to get a consistent picture of how the refs look
  969. * at a specified point in time
  970. */
  971. again:
  972. head = NULL;
  973. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  974. if (ret < 0)
  975. goto out;
  976. BUG_ON(ret == 0);
  977. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  978. if (trans && likely(trans->type != __TRANS_DUMMY) &&
  979. time_seq != SEQ_LAST) {
  980. #else
  981. if (trans && time_seq != SEQ_LAST) {
  982. #endif
  983. /*
  984. * look if there are updates for this ref queued and lock the
  985. * head
  986. */
  987. delayed_refs = &trans->transaction->delayed_refs;
  988. spin_lock(&delayed_refs->lock);
  989. head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
  990. if (head) {
  991. if (!mutex_trylock(&head->mutex)) {
  992. refcount_inc(&head->node.refs);
  993. spin_unlock(&delayed_refs->lock);
  994. btrfs_release_path(path);
  995. /*
  996. * Mutex was contended, block until it's
  997. * released and try again
  998. */
  999. mutex_lock(&head->mutex);
  1000. mutex_unlock(&head->mutex);
  1001. btrfs_put_delayed_ref(&head->node);
  1002. goto again;
  1003. }
  1004. spin_unlock(&delayed_refs->lock);
  1005. ret = add_delayed_refs(fs_info, head, time_seq,
  1006. &preftrees, &total_refs, inum);
  1007. mutex_unlock(&head->mutex);
  1008. if (ret)
  1009. goto out;
  1010. } else {
  1011. spin_unlock(&delayed_refs->lock);
  1012. }
  1013. }
  1014. if (path->slots[0]) {
  1015. struct extent_buffer *leaf;
  1016. int slot;
  1017. path->slots[0]--;
  1018. leaf = path->nodes[0];
  1019. slot = path->slots[0];
  1020. btrfs_item_key_to_cpu(leaf, &key, slot);
  1021. if (key.objectid == bytenr &&
  1022. (key.type == BTRFS_EXTENT_ITEM_KEY ||
  1023. key.type == BTRFS_METADATA_ITEM_KEY)) {
  1024. ret = add_inline_refs(fs_info, path, bytenr,
  1025. &info_level, &preftrees,
  1026. &total_refs, inum);
  1027. if (ret)
  1028. goto out;
  1029. ret = add_keyed_refs(fs_info, path, bytenr, info_level,
  1030. &preftrees, inum);
  1031. if (ret)
  1032. goto out;
  1033. }
  1034. }
  1035. btrfs_release_path(path);
  1036. ret = add_missing_keys(fs_info, &preftrees);
  1037. if (ret)
  1038. goto out;
  1039. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root));
  1040. ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
  1041. extent_item_pos, total_refs,
  1042. root_objectid);
  1043. if (ret)
  1044. goto out;
  1045. WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root));
  1046. /*
  1047. * This walks the tree of merged and resolved refs. Tree blocks are
  1048. * read in as needed. Unique entries are added to the ulist, and
  1049. * the list of found roots is updated.
  1050. *
  1051. * We release the entire tree in one go before returning.
  1052. */
  1053. node = rb_first(&preftrees.direct.root);
  1054. while (node) {
  1055. ref = rb_entry(node, struct prelim_ref, rbnode);
  1056. node = rb_next(&ref->rbnode);
  1057. WARN_ON(ref->count < 0);
  1058. if (roots && ref->count && ref->root_id && ref->parent == 0) {
  1059. if (root_objectid && ref->root_id != root_objectid) {
  1060. ret = BACKREF_FOUND_SHARED;
  1061. goto out;
  1062. }
  1063. /* no parent == root of tree */
  1064. ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
  1065. if (ret < 0)
  1066. goto out;
  1067. }
  1068. if (ref->count && ref->parent) {
  1069. if (extent_item_pos && !ref->inode_list &&
  1070. ref->level == 0) {
  1071. struct extent_buffer *eb;
  1072. eb = read_tree_block(fs_info, ref->parent, 0);
  1073. if (IS_ERR(eb)) {
  1074. ret = PTR_ERR(eb);
  1075. goto out;
  1076. } else if (!extent_buffer_uptodate(eb)) {
  1077. free_extent_buffer(eb);
  1078. ret = -EIO;
  1079. goto out;
  1080. }
  1081. btrfs_tree_read_lock(eb);
  1082. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1083. ret = find_extent_in_eb(eb, bytenr,
  1084. *extent_item_pos, &eie);
  1085. btrfs_tree_read_unlock_blocking(eb);
  1086. free_extent_buffer(eb);
  1087. if (ret < 0)
  1088. goto out;
  1089. ref->inode_list = eie;
  1090. }
  1091. ret = ulist_add_merge_ptr(refs, ref->parent,
  1092. ref->inode_list,
  1093. (void **)&eie, GFP_NOFS);
  1094. if (ret < 0)
  1095. goto out;
  1096. if (!ret && extent_item_pos) {
  1097. /*
  1098. * we've recorded that parent, so we must extend
  1099. * its inode list here
  1100. */
  1101. BUG_ON(!eie);
  1102. while (eie->next)
  1103. eie = eie->next;
  1104. eie->next = ref->inode_list;
  1105. }
  1106. eie = NULL;
  1107. }
  1108. cond_resched();
  1109. }
  1110. out:
  1111. btrfs_free_path(path);
  1112. prelim_release(&preftrees.direct);
  1113. prelim_release(&preftrees.indirect);
  1114. prelim_release(&preftrees.indirect_missing_keys);
  1115. if (ret < 0)
  1116. free_inode_elem_list(eie);
  1117. return ret;
  1118. }
  1119. static void free_leaf_list(struct ulist *blocks)
  1120. {
  1121. struct ulist_node *node = NULL;
  1122. struct extent_inode_elem *eie;
  1123. struct ulist_iterator uiter;
  1124. ULIST_ITER_INIT(&uiter);
  1125. while ((node = ulist_next(blocks, &uiter))) {
  1126. if (!node->aux)
  1127. continue;
  1128. eie = unode_aux_to_inode_list(node);
  1129. free_inode_elem_list(eie);
  1130. node->aux = 0;
  1131. }
  1132. ulist_free(blocks);
  1133. }
  1134. /*
  1135. * Finds all leafs with a reference to the specified combination of bytenr and
  1136. * offset. key_list_head will point to a list of corresponding keys (caller must
  1137. * free each list element). The leafs will be stored in the leafs ulist, which
  1138. * must be freed with ulist_free.
  1139. *
  1140. * returns 0 on success, <0 on error
  1141. */
  1142. static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
  1143. struct btrfs_fs_info *fs_info, u64 bytenr,
  1144. u64 time_seq, struct ulist **leafs,
  1145. const u64 *extent_item_pos)
  1146. {
  1147. int ret;
  1148. *leafs = ulist_alloc(GFP_NOFS);
  1149. if (!*leafs)
  1150. return -ENOMEM;
  1151. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1152. *leafs, NULL, extent_item_pos, 0, 0);
  1153. if (ret < 0 && ret != -ENOENT) {
  1154. free_leaf_list(*leafs);
  1155. return ret;
  1156. }
  1157. return 0;
  1158. }
  1159. /*
  1160. * walk all backrefs for a given extent to find all roots that reference this
  1161. * extent. Walking a backref means finding all extents that reference this
  1162. * extent and in turn walk the backrefs of those, too. Naturally this is a
  1163. * recursive process, but here it is implemented in an iterative fashion: We
  1164. * find all referencing extents for the extent in question and put them on a
  1165. * list. In turn, we find all referencing extents for those, further appending
  1166. * to the list. The way we iterate the list allows adding more elements after
  1167. * the current while iterating. The process stops when we reach the end of the
  1168. * list. Found roots are added to the roots list.
  1169. *
  1170. * returns 0 on success, < 0 on error.
  1171. */
  1172. static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
  1173. struct btrfs_fs_info *fs_info, u64 bytenr,
  1174. u64 time_seq, struct ulist **roots)
  1175. {
  1176. struct ulist *tmp;
  1177. struct ulist_node *node = NULL;
  1178. struct ulist_iterator uiter;
  1179. int ret;
  1180. tmp = ulist_alloc(GFP_NOFS);
  1181. if (!tmp)
  1182. return -ENOMEM;
  1183. *roots = ulist_alloc(GFP_NOFS);
  1184. if (!*roots) {
  1185. ulist_free(tmp);
  1186. return -ENOMEM;
  1187. }
  1188. ULIST_ITER_INIT(&uiter);
  1189. while (1) {
  1190. ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
  1191. tmp, *roots, NULL, 0, 0);
  1192. if (ret < 0 && ret != -ENOENT) {
  1193. ulist_free(tmp);
  1194. ulist_free(*roots);
  1195. return ret;
  1196. }
  1197. node = ulist_next(tmp, &uiter);
  1198. if (!node)
  1199. break;
  1200. bytenr = node->val;
  1201. cond_resched();
  1202. }
  1203. ulist_free(tmp);
  1204. return 0;
  1205. }
  1206. int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
  1207. struct btrfs_fs_info *fs_info, u64 bytenr,
  1208. u64 time_seq, struct ulist **roots)
  1209. {
  1210. int ret;
  1211. if (!trans)
  1212. down_read(&fs_info->commit_root_sem);
  1213. ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
  1214. time_seq, roots);
  1215. if (!trans)
  1216. up_read(&fs_info->commit_root_sem);
  1217. return ret;
  1218. }
  1219. /**
  1220. * btrfs_check_shared - tell us whether an extent is shared
  1221. *
  1222. * btrfs_check_shared uses the backref walking code but will short
  1223. * circuit as soon as it finds a root or inode that doesn't match the
  1224. * one passed in. This provides a significant performance benefit for
  1225. * callers (such as fiemap) which want to know whether the extent is
  1226. * shared but do not need a ref count.
  1227. *
  1228. * This attempts to allocate a transaction in order to account for
  1229. * delayed refs, but continues on even when the alloc fails.
  1230. *
  1231. * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
  1232. */
  1233. int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
  1234. {
  1235. struct btrfs_fs_info *fs_info = root->fs_info;
  1236. struct btrfs_trans_handle *trans;
  1237. struct ulist *tmp = NULL;
  1238. struct ulist *roots = NULL;
  1239. struct ulist_iterator uiter;
  1240. struct ulist_node *node;
  1241. struct seq_list elem = SEQ_LIST_INIT(elem);
  1242. int ret = 0;
  1243. tmp = ulist_alloc(GFP_NOFS);
  1244. roots = ulist_alloc(GFP_NOFS);
  1245. if (!tmp || !roots) {
  1246. ulist_free(tmp);
  1247. ulist_free(roots);
  1248. return -ENOMEM;
  1249. }
  1250. trans = btrfs_join_transaction(root);
  1251. if (IS_ERR(trans)) {
  1252. trans = NULL;
  1253. down_read(&fs_info->commit_root_sem);
  1254. } else {
  1255. btrfs_get_tree_mod_seq(fs_info, &elem);
  1256. }
  1257. ULIST_ITER_INIT(&uiter);
  1258. while (1) {
  1259. ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
  1260. roots, NULL, root->objectid, inum);
  1261. if (ret == BACKREF_FOUND_SHARED) {
  1262. /* this is the only condition under which we return 1 */
  1263. ret = 1;
  1264. break;
  1265. }
  1266. if (ret < 0 && ret != -ENOENT)
  1267. break;
  1268. ret = 0;
  1269. node = ulist_next(tmp, &uiter);
  1270. if (!node)
  1271. break;
  1272. bytenr = node->val;
  1273. cond_resched();
  1274. }
  1275. if (trans) {
  1276. btrfs_put_tree_mod_seq(fs_info, &elem);
  1277. btrfs_end_transaction(trans);
  1278. } else {
  1279. up_read(&fs_info->commit_root_sem);
  1280. }
  1281. ulist_free(tmp);
  1282. ulist_free(roots);
  1283. return ret;
  1284. }
  1285. int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
  1286. u64 start_off, struct btrfs_path *path,
  1287. struct btrfs_inode_extref **ret_extref,
  1288. u64 *found_off)
  1289. {
  1290. int ret, slot;
  1291. struct btrfs_key key;
  1292. struct btrfs_key found_key;
  1293. struct btrfs_inode_extref *extref;
  1294. const struct extent_buffer *leaf;
  1295. unsigned long ptr;
  1296. key.objectid = inode_objectid;
  1297. key.type = BTRFS_INODE_EXTREF_KEY;
  1298. key.offset = start_off;
  1299. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1300. if (ret < 0)
  1301. return ret;
  1302. while (1) {
  1303. leaf = path->nodes[0];
  1304. slot = path->slots[0];
  1305. if (slot >= btrfs_header_nritems(leaf)) {
  1306. /*
  1307. * If the item at offset is not found,
  1308. * btrfs_search_slot will point us to the slot
  1309. * where it should be inserted. In our case
  1310. * that will be the slot directly before the
  1311. * next INODE_REF_KEY_V2 item. In the case
  1312. * that we're pointing to the last slot in a
  1313. * leaf, we must move one leaf over.
  1314. */
  1315. ret = btrfs_next_leaf(root, path);
  1316. if (ret) {
  1317. if (ret >= 1)
  1318. ret = -ENOENT;
  1319. break;
  1320. }
  1321. continue;
  1322. }
  1323. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1324. /*
  1325. * Check that we're still looking at an extended ref key for
  1326. * this particular objectid. If we have different
  1327. * objectid or type then there are no more to be found
  1328. * in the tree and we can exit.
  1329. */
  1330. ret = -ENOENT;
  1331. if (found_key.objectid != inode_objectid)
  1332. break;
  1333. if (found_key.type != BTRFS_INODE_EXTREF_KEY)
  1334. break;
  1335. ret = 0;
  1336. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1337. extref = (struct btrfs_inode_extref *)ptr;
  1338. *ret_extref = extref;
  1339. if (found_off)
  1340. *found_off = found_key.offset;
  1341. break;
  1342. }
  1343. return ret;
  1344. }
  1345. /*
  1346. * this iterates to turn a name (from iref/extref) into a full filesystem path.
  1347. * Elements of the path are separated by '/' and the path is guaranteed to be
  1348. * 0-terminated. the path is only given within the current file system.
  1349. * Therefore, it never starts with a '/'. the caller is responsible to provide
  1350. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  1351. * the start point of the resulting string is returned. this pointer is within
  1352. * dest, normally.
  1353. * in case the path buffer would overflow, the pointer is decremented further
  1354. * as if output was written to the buffer, though no more output is actually
  1355. * generated. that way, the caller can determine how much space would be
  1356. * required for the path to fit into the buffer. in that case, the returned
  1357. * value will be smaller than dest. callers must check this!
  1358. */
  1359. char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  1360. u32 name_len, unsigned long name_off,
  1361. struct extent_buffer *eb_in, u64 parent,
  1362. char *dest, u32 size)
  1363. {
  1364. int slot;
  1365. u64 next_inum;
  1366. int ret;
  1367. s64 bytes_left = ((s64)size) - 1;
  1368. struct extent_buffer *eb = eb_in;
  1369. struct btrfs_key found_key;
  1370. int leave_spinning = path->leave_spinning;
  1371. struct btrfs_inode_ref *iref;
  1372. if (bytes_left >= 0)
  1373. dest[bytes_left] = '\0';
  1374. path->leave_spinning = 1;
  1375. while (1) {
  1376. bytes_left -= name_len;
  1377. if (bytes_left >= 0)
  1378. read_extent_buffer(eb, dest + bytes_left,
  1379. name_off, name_len);
  1380. if (eb != eb_in) {
  1381. if (!path->skip_locking)
  1382. btrfs_tree_read_unlock_blocking(eb);
  1383. free_extent_buffer(eb);
  1384. }
  1385. ret = btrfs_find_item(fs_root, path, parent, 0,
  1386. BTRFS_INODE_REF_KEY, &found_key);
  1387. if (ret > 0)
  1388. ret = -ENOENT;
  1389. if (ret)
  1390. break;
  1391. next_inum = found_key.offset;
  1392. /* regular exit ahead */
  1393. if (parent == next_inum)
  1394. break;
  1395. slot = path->slots[0];
  1396. eb = path->nodes[0];
  1397. /* make sure we can use eb after releasing the path */
  1398. if (eb != eb_in) {
  1399. if (!path->skip_locking)
  1400. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1401. path->nodes[0] = NULL;
  1402. path->locks[0] = 0;
  1403. }
  1404. btrfs_release_path(path);
  1405. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1406. name_len = btrfs_inode_ref_name_len(eb, iref);
  1407. name_off = (unsigned long)(iref + 1);
  1408. parent = next_inum;
  1409. --bytes_left;
  1410. if (bytes_left >= 0)
  1411. dest[bytes_left] = '/';
  1412. }
  1413. btrfs_release_path(path);
  1414. path->leave_spinning = leave_spinning;
  1415. if (ret)
  1416. return ERR_PTR(ret);
  1417. return dest + bytes_left;
  1418. }
  1419. /*
  1420. * this makes the path point to (logical EXTENT_ITEM *)
  1421. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  1422. * tree blocks and <0 on error.
  1423. */
  1424. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  1425. struct btrfs_path *path, struct btrfs_key *found_key,
  1426. u64 *flags_ret)
  1427. {
  1428. int ret;
  1429. u64 flags;
  1430. u64 size = 0;
  1431. u32 item_size;
  1432. const struct extent_buffer *eb;
  1433. struct btrfs_extent_item *ei;
  1434. struct btrfs_key key;
  1435. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1436. key.type = BTRFS_METADATA_ITEM_KEY;
  1437. else
  1438. key.type = BTRFS_EXTENT_ITEM_KEY;
  1439. key.objectid = logical;
  1440. key.offset = (u64)-1;
  1441. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  1442. if (ret < 0)
  1443. return ret;
  1444. ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
  1445. if (ret) {
  1446. if (ret > 0)
  1447. ret = -ENOENT;
  1448. return ret;
  1449. }
  1450. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  1451. if (found_key->type == BTRFS_METADATA_ITEM_KEY)
  1452. size = fs_info->nodesize;
  1453. else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
  1454. size = found_key->offset;
  1455. if (found_key->objectid > logical ||
  1456. found_key->objectid + size <= logical) {
  1457. btrfs_debug(fs_info,
  1458. "logical %llu is not within any extent", logical);
  1459. return -ENOENT;
  1460. }
  1461. eb = path->nodes[0];
  1462. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  1463. BUG_ON(item_size < sizeof(*ei));
  1464. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  1465. flags = btrfs_extent_flags(eb, ei);
  1466. btrfs_debug(fs_info,
  1467. "logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
  1468. logical, logical - found_key->objectid, found_key->objectid,
  1469. found_key->offset, flags, item_size);
  1470. WARN_ON(!flags_ret);
  1471. if (flags_ret) {
  1472. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1473. *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
  1474. else if (flags & BTRFS_EXTENT_FLAG_DATA)
  1475. *flags_ret = BTRFS_EXTENT_FLAG_DATA;
  1476. else
  1477. BUG_ON(1);
  1478. return 0;
  1479. }
  1480. return -EIO;
  1481. }
  1482. /*
  1483. * helper function to iterate extent inline refs. ptr must point to a 0 value
  1484. * for the first call and may be modified. it is used to track state.
  1485. * if more refs exist, 0 is returned and the next call to
  1486. * get_extent_inline_ref must pass the modified ptr parameter to get the
  1487. * next ref. after the last ref was processed, 1 is returned.
  1488. * returns <0 on error
  1489. */
  1490. static int get_extent_inline_ref(unsigned long *ptr,
  1491. const struct extent_buffer *eb,
  1492. const struct btrfs_key *key,
  1493. const struct btrfs_extent_item *ei,
  1494. u32 item_size,
  1495. struct btrfs_extent_inline_ref **out_eiref,
  1496. int *out_type)
  1497. {
  1498. unsigned long end;
  1499. u64 flags;
  1500. struct btrfs_tree_block_info *info;
  1501. if (!*ptr) {
  1502. /* first call */
  1503. flags = btrfs_extent_flags(eb, ei);
  1504. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1505. if (key->type == BTRFS_METADATA_ITEM_KEY) {
  1506. /* a skinny metadata extent */
  1507. *out_eiref =
  1508. (struct btrfs_extent_inline_ref *)(ei + 1);
  1509. } else {
  1510. WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
  1511. info = (struct btrfs_tree_block_info *)(ei + 1);
  1512. *out_eiref =
  1513. (struct btrfs_extent_inline_ref *)(info + 1);
  1514. }
  1515. } else {
  1516. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  1517. }
  1518. *ptr = (unsigned long)*out_eiref;
  1519. if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
  1520. return -ENOENT;
  1521. }
  1522. end = (unsigned long)ei + item_size;
  1523. *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
  1524. *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
  1525. *ptr += btrfs_extent_inline_ref_size(*out_type);
  1526. WARN_ON(*ptr > end);
  1527. if (*ptr == end)
  1528. return 1; /* last */
  1529. return 0;
  1530. }
  1531. /*
  1532. * reads the tree block backref for an extent. tree level and root are returned
  1533. * through out_level and out_root. ptr must point to a 0 value for the first
  1534. * call and may be modified (see get_extent_inline_ref comment).
  1535. * returns 0 if data was provided, 1 if there was no more data to provide or
  1536. * <0 on error.
  1537. */
  1538. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  1539. struct btrfs_key *key, struct btrfs_extent_item *ei,
  1540. u32 item_size, u64 *out_root, u8 *out_level)
  1541. {
  1542. int ret;
  1543. int type;
  1544. struct btrfs_extent_inline_ref *eiref;
  1545. if (*ptr == (unsigned long)-1)
  1546. return 1;
  1547. while (1) {
  1548. ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
  1549. &eiref, &type);
  1550. if (ret < 0)
  1551. return ret;
  1552. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1553. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1554. break;
  1555. if (ret == 1)
  1556. return 1;
  1557. }
  1558. /* we can treat both ref types equally here */
  1559. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  1560. if (key->type == BTRFS_EXTENT_ITEM_KEY) {
  1561. struct btrfs_tree_block_info *info;
  1562. info = (struct btrfs_tree_block_info *)(ei + 1);
  1563. *out_level = btrfs_tree_block_level(eb, info);
  1564. } else {
  1565. ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
  1566. *out_level = (u8)key->offset;
  1567. }
  1568. if (ret == 1)
  1569. *ptr = (unsigned long)-1;
  1570. return 0;
  1571. }
  1572. static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
  1573. struct extent_inode_elem *inode_list,
  1574. u64 root, u64 extent_item_objectid,
  1575. iterate_extent_inodes_t *iterate, void *ctx)
  1576. {
  1577. struct extent_inode_elem *eie;
  1578. int ret = 0;
  1579. for (eie = inode_list; eie; eie = eie->next) {
  1580. btrfs_debug(fs_info,
  1581. "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
  1582. extent_item_objectid, eie->inum,
  1583. eie->offset, root);
  1584. ret = iterate(eie->inum, eie->offset, root, ctx);
  1585. if (ret) {
  1586. btrfs_debug(fs_info,
  1587. "stopping iteration for %llu due to ret=%d",
  1588. extent_item_objectid, ret);
  1589. break;
  1590. }
  1591. }
  1592. return ret;
  1593. }
  1594. /*
  1595. * calls iterate() for every inode that references the extent identified by
  1596. * the given parameters.
  1597. * when the iterator function returns a non-zero value, iteration stops.
  1598. */
  1599. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1600. u64 extent_item_objectid, u64 extent_item_pos,
  1601. int search_commit_root,
  1602. iterate_extent_inodes_t *iterate, void *ctx)
  1603. {
  1604. int ret;
  1605. struct btrfs_trans_handle *trans = NULL;
  1606. struct ulist *refs = NULL;
  1607. struct ulist *roots = NULL;
  1608. struct ulist_node *ref_node = NULL;
  1609. struct ulist_node *root_node = NULL;
  1610. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  1611. struct ulist_iterator ref_uiter;
  1612. struct ulist_iterator root_uiter;
  1613. btrfs_debug(fs_info, "resolving all inodes for extent %llu",
  1614. extent_item_objectid);
  1615. if (!search_commit_root) {
  1616. trans = btrfs_join_transaction(fs_info->extent_root);
  1617. if (IS_ERR(trans))
  1618. return PTR_ERR(trans);
  1619. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1620. } else {
  1621. down_read(&fs_info->commit_root_sem);
  1622. }
  1623. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1624. tree_mod_seq_elem.seq, &refs,
  1625. &extent_item_pos);
  1626. if (ret)
  1627. goto out;
  1628. ULIST_ITER_INIT(&ref_uiter);
  1629. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  1630. ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
  1631. tree_mod_seq_elem.seq, &roots);
  1632. if (ret)
  1633. break;
  1634. ULIST_ITER_INIT(&root_uiter);
  1635. while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
  1636. btrfs_debug(fs_info,
  1637. "root %llu references leaf %llu, data list %#llx",
  1638. root_node->val, ref_node->val,
  1639. ref_node->aux);
  1640. ret = iterate_leaf_refs(fs_info,
  1641. (struct extent_inode_elem *)
  1642. (uintptr_t)ref_node->aux,
  1643. root_node->val,
  1644. extent_item_objectid,
  1645. iterate, ctx);
  1646. }
  1647. ulist_free(roots);
  1648. }
  1649. free_leaf_list(refs);
  1650. out:
  1651. if (!search_commit_root) {
  1652. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1653. btrfs_end_transaction(trans);
  1654. } else {
  1655. up_read(&fs_info->commit_root_sem);
  1656. }
  1657. return ret;
  1658. }
  1659. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1660. struct btrfs_path *path,
  1661. iterate_extent_inodes_t *iterate, void *ctx)
  1662. {
  1663. int ret;
  1664. u64 extent_item_pos;
  1665. u64 flags = 0;
  1666. struct btrfs_key found_key;
  1667. int search_commit_root = path->search_commit_root;
  1668. ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
  1669. btrfs_release_path(path);
  1670. if (ret < 0)
  1671. return ret;
  1672. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1673. return -EINVAL;
  1674. extent_item_pos = logical - found_key.objectid;
  1675. ret = iterate_extent_inodes(fs_info, found_key.objectid,
  1676. extent_item_pos, search_commit_root,
  1677. iterate, ctx);
  1678. return ret;
  1679. }
  1680. typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
  1681. struct extent_buffer *eb, void *ctx);
  1682. static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
  1683. struct btrfs_path *path,
  1684. iterate_irefs_t *iterate, void *ctx)
  1685. {
  1686. int ret = 0;
  1687. int slot;
  1688. u32 cur;
  1689. u32 len;
  1690. u32 name_len;
  1691. u64 parent = 0;
  1692. int found = 0;
  1693. struct extent_buffer *eb;
  1694. struct btrfs_item *item;
  1695. struct btrfs_inode_ref *iref;
  1696. struct btrfs_key found_key;
  1697. while (!ret) {
  1698. ret = btrfs_find_item(fs_root, path, inum,
  1699. parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
  1700. &found_key);
  1701. if (ret < 0)
  1702. break;
  1703. if (ret) {
  1704. ret = found ? 0 : -ENOENT;
  1705. break;
  1706. }
  1707. ++found;
  1708. parent = found_key.offset;
  1709. slot = path->slots[0];
  1710. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1711. if (!eb) {
  1712. ret = -ENOMEM;
  1713. break;
  1714. }
  1715. extent_buffer_get(eb);
  1716. btrfs_tree_read_lock(eb);
  1717. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1718. btrfs_release_path(path);
  1719. item = btrfs_item_nr(slot);
  1720. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1721. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1722. name_len = btrfs_inode_ref_name_len(eb, iref);
  1723. /* path must be released before calling iterate()! */
  1724. btrfs_debug(fs_root->fs_info,
  1725. "following ref at offset %u for inode %llu in tree %llu",
  1726. cur, found_key.objectid, fs_root->objectid);
  1727. ret = iterate(parent, name_len,
  1728. (unsigned long)(iref + 1), eb, ctx);
  1729. if (ret)
  1730. break;
  1731. len = sizeof(*iref) + name_len;
  1732. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1733. }
  1734. btrfs_tree_read_unlock_blocking(eb);
  1735. free_extent_buffer(eb);
  1736. }
  1737. btrfs_release_path(path);
  1738. return ret;
  1739. }
  1740. static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
  1741. struct btrfs_path *path,
  1742. iterate_irefs_t *iterate, void *ctx)
  1743. {
  1744. int ret;
  1745. int slot;
  1746. u64 offset = 0;
  1747. u64 parent;
  1748. int found = 0;
  1749. struct extent_buffer *eb;
  1750. struct btrfs_inode_extref *extref;
  1751. u32 item_size;
  1752. u32 cur_offset;
  1753. unsigned long ptr;
  1754. while (1) {
  1755. ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
  1756. &offset);
  1757. if (ret < 0)
  1758. break;
  1759. if (ret) {
  1760. ret = found ? 0 : -ENOENT;
  1761. break;
  1762. }
  1763. ++found;
  1764. slot = path->slots[0];
  1765. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1766. if (!eb) {
  1767. ret = -ENOMEM;
  1768. break;
  1769. }
  1770. extent_buffer_get(eb);
  1771. btrfs_tree_read_lock(eb);
  1772. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1773. btrfs_release_path(path);
  1774. item_size = btrfs_item_size_nr(eb, slot);
  1775. ptr = btrfs_item_ptr_offset(eb, slot);
  1776. cur_offset = 0;
  1777. while (cur_offset < item_size) {
  1778. u32 name_len;
  1779. extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
  1780. parent = btrfs_inode_extref_parent(eb, extref);
  1781. name_len = btrfs_inode_extref_name_len(eb, extref);
  1782. ret = iterate(parent, name_len,
  1783. (unsigned long)&extref->name, eb, ctx);
  1784. if (ret)
  1785. break;
  1786. cur_offset += btrfs_inode_extref_name_len(eb, extref);
  1787. cur_offset += sizeof(*extref);
  1788. }
  1789. btrfs_tree_read_unlock_blocking(eb);
  1790. free_extent_buffer(eb);
  1791. offset++;
  1792. }
  1793. btrfs_release_path(path);
  1794. return ret;
  1795. }
  1796. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1797. struct btrfs_path *path, iterate_irefs_t *iterate,
  1798. void *ctx)
  1799. {
  1800. int ret;
  1801. int found_refs = 0;
  1802. ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
  1803. if (!ret)
  1804. ++found_refs;
  1805. else if (ret != -ENOENT)
  1806. return ret;
  1807. ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
  1808. if (ret == -ENOENT && found_refs)
  1809. return 0;
  1810. return ret;
  1811. }
  1812. /*
  1813. * returns 0 if the path could be dumped (probably truncated)
  1814. * returns <0 in case of an error
  1815. */
  1816. static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
  1817. struct extent_buffer *eb, void *ctx)
  1818. {
  1819. struct inode_fs_paths *ipath = ctx;
  1820. char *fspath;
  1821. char *fspath_min;
  1822. int i = ipath->fspath->elem_cnt;
  1823. const int s_ptr = sizeof(char *);
  1824. u32 bytes_left;
  1825. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  1826. ipath->fspath->bytes_left - s_ptr : 0;
  1827. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  1828. fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
  1829. name_off, eb, inum, fspath_min, bytes_left);
  1830. if (IS_ERR(fspath))
  1831. return PTR_ERR(fspath);
  1832. if (fspath > fspath_min) {
  1833. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  1834. ++ipath->fspath->elem_cnt;
  1835. ipath->fspath->bytes_left = fspath - fspath_min;
  1836. } else {
  1837. ++ipath->fspath->elem_missed;
  1838. ipath->fspath->bytes_missing += fspath_min - fspath;
  1839. ipath->fspath->bytes_left = 0;
  1840. }
  1841. return 0;
  1842. }
  1843. /*
  1844. * this dumps all file system paths to the inode into the ipath struct, provided
  1845. * is has been created large enough. each path is zero-terminated and accessed
  1846. * from ipath->fspath->val[i].
  1847. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  1848. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  1849. * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
  1850. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  1851. * have been needed to return all paths.
  1852. */
  1853. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  1854. {
  1855. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  1856. inode_to_path, ipath);
  1857. }
  1858. struct btrfs_data_container *init_data_container(u32 total_bytes)
  1859. {
  1860. struct btrfs_data_container *data;
  1861. size_t alloc_bytes;
  1862. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  1863. data = kvmalloc(alloc_bytes, GFP_KERNEL);
  1864. if (!data)
  1865. return ERR_PTR(-ENOMEM);
  1866. if (total_bytes >= sizeof(*data)) {
  1867. data->bytes_left = total_bytes - sizeof(*data);
  1868. data->bytes_missing = 0;
  1869. } else {
  1870. data->bytes_missing = sizeof(*data) - total_bytes;
  1871. data->bytes_left = 0;
  1872. }
  1873. data->elem_cnt = 0;
  1874. data->elem_missed = 0;
  1875. return data;
  1876. }
  1877. /*
  1878. * allocates space to return multiple file system paths for an inode.
  1879. * total_bytes to allocate are passed, note that space usable for actual path
  1880. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1881. * the returned pointer must be freed with free_ipath() in the end.
  1882. */
  1883. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  1884. struct btrfs_path *path)
  1885. {
  1886. struct inode_fs_paths *ifp;
  1887. struct btrfs_data_container *fspath;
  1888. fspath = init_data_container(total_bytes);
  1889. if (IS_ERR(fspath))
  1890. return (void *)fspath;
  1891. ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
  1892. if (!ifp) {
  1893. kvfree(fspath);
  1894. return ERR_PTR(-ENOMEM);
  1895. }
  1896. ifp->btrfs_path = path;
  1897. ifp->fspath = fspath;
  1898. ifp->fs_root = fs_root;
  1899. return ifp;
  1900. }
  1901. void free_ipath(struct inode_fs_paths *ipath)
  1902. {
  1903. if (!ipath)
  1904. return;
  1905. kvfree(ipath->fspath);
  1906. kfree(ipath);
  1907. }