backref.c 52 KB

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