backref.c 52 KB

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