backref.c 52 KB

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