backref.c 50 KB

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