backref.c 51 KB

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