backref.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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. fs_info->tree_root->nodesize, 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. * FIXME some caching might speed things up
  780. */
  781. static int find_parent_nodes(struct btrfs_trans_handle *trans,
  782. struct btrfs_fs_info *fs_info, u64 bytenr,
  783. u64 time_seq, struct ulist *refs,
  784. struct ulist *roots, const u64 *extent_item_pos,
  785. u64 root_objectid, u64 inum)
  786. {
  787. struct btrfs_key key;
  788. struct btrfs_path *path;
  789. struct btrfs_delayed_ref_root *delayed_refs = NULL;
  790. struct btrfs_delayed_ref_head *head;
  791. int info_level = 0;
  792. int ret;
  793. struct list_head prefs_delayed;
  794. struct list_head prefs;
  795. struct __prelim_ref *ref;
  796. struct extent_inode_elem *eie = NULL;
  797. u64 total_refs = 0;
  798. INIT_LIST_HEAD(&prefs);
  799. INIT_LIST_HEAD(&prefs_delayed);
  800. key.objectid = bytenr;
  801. key.offset = (u64)-1;
  802. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  803. key.type = BTRFS_METADATA_ITEM_KEY;
  804. else
  805. key.type = BTRFS_EXTENT_ITEM_KEY;
  806. path = btrfs_alloc_path();
  807. if (!path)
  808. return -ENOMEM;
  809. if (!trans) {
  810. path->search_commit_root = 1;
  811. path->skip_locking = 1;
  812. }
  813. /*
  814. * grab both a lock on the path and a lock on the delayed ref head.
  815. * We need both to get a consistent picture of how the refs look
  816. * at a specified point in time
  817. */
  818. again:
  819. head = NULL;
  820. ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
  821. if (ret < 0)
  822. goto out;
  823. BUG_ON(ret == 0);
  824. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  825. if (trans && likely(trans->type != __TRANS_DUMMY)) {
  826. #else
  827. if (trans) {
  828. #endif
  829. /*
  830. * look if there are updates for this ref queued and lock the
  831. * head
  832. */
  833. delayed_refs = &trans->transaction->delayed_refs;
  834. spin_lock(&delayed_refs->lock);
  835. head = btrfs_find_delayed_ref_head(trans, bytenr);
  836. if (head) {
  837. if (!mutex_trylock(&head->mutex)) {
  838. atomic_inc(&head->node.refs);
  839. spin_unlock(&delayed_refs->lock);
  840. btrfs_release_path(path);
  841. /*
  842. * Mutex was contended, block until it's
  843. * released and try again
  844. */
  845. mutex_lock(&head->mutex);
  846. mutex_unlock(&head->mutex);
  847. btrfs_put_delayed_ref(&head->node);
  848. goto again;
  849. }
  850. spin_unlock(&delayed_refs->lock);
  851. ret = __add_delayed_refs(head, time_seq,
  852. &prefs_delayed, &total_refs,
  853. inum);
  854. mutex_unlock(&head->mutex);
  855. if (ret)
  856. goto out;
  857. } else {
  858. spin_unlock(&delayed_refs->lock);
  859. }
  860. }
  861. if (path->slots[0]) {
  862. struct extent_buffer *leaf;
  863. int slot;
  864. path->slots[0]--;
  865. leaf = path->nodes[0];
  866. slot = path->slots[0];
  867. btrfs_item_key_to_cpu(leaf, &key, slot);
  868. if (key.objectid == bytenr &&
  869. (key.type == BTRFS_EXTENT_ITEM_KEY ||
  870. key.type == BTRFS_METADATA_ITEM_KEY)) {
  871. ret = __add_inline_refs(fs_info, path, bytenr,
  872. &info_level, &prefs,
  873. &total_refs, inum);
  874. if (ret)
  875. goto out;
  876. ret = __add_keyed_refs(fs_info, path, bytenr,
  877. info_level, &prefs, inum);
  878. if (ret)
  879. goto out;
  880. }
  881. }
  882. btrfs_release_path(path);
  883. list_splice_init(&prefs_delayed, &prefs);
  884. ret = __add_missing_keys(fs_info, &prefs);
  885. if (ret)
  886. goto out;
  887. __merge_refs(&prefs, 1);
  888. ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs,
  889. extent_item_pos, total_refs,
  890. root_objectid);
  891. if (ret)
  892. goto out;
  893. __merge_refs(&prefs, 2);
  894. while (!list_empty(&prefs)) {
  895. ref = list_first_entry(&prefs, struct __prelim_ref, list);
  896. WARN_ON(ref->count < 0);
  897. if (roots && ref->count && ref->root_id && ref->parent == 0) {
  898. if (root_objectid && ref->root_id != root_objectid) {
  899. ret = BACKREF_FOUND_SHARED;
  900. goto out;
  901. }
  902. /* no parent == root of tree */
  903. ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
  904. if (ret < 0)
  905. goto out;
  906. }
  907. if (ref->count && ref->parent) {
  908. if (extent_item_pos && !ref->inode_list &&
  909. ref->level == 0) {
  910. u32 bsz;
  911. struct extent_buffer *eb;
  912. bsz = fs_info->extent_root->nodesize;
  913. eb = read_tree_block(fs_info->extent_root,
  914. ref->parent, bsz, 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. int btrfs_check_shared(struct btrfs_trans_handle *trans,
  1067. struct btrfs_fs_info *fs_info, u64 root_objectid,
  1068. u64 inum, u64 bytenr)
  1069. {
  1070. struct ulist *tmp = NULL;
  1071. struct ulist *roots = NULL;
  1072. struct ulist_iterator uiter;
  1073. struct ulist_node *node;
  1074. struct seq_list elem = {};
  1075. int ret = 0;
  1076. tmp = ulist_alloc(GFP_NOFS);
  1077. roots = ulist_alloc(GFP_NOFS);
  1078. if (!tmp || !roots) {
  1079. ulist_free(tmp);
  1080. ulist_free(roots);
  1081. return -ENOMEM;
  1082. }
  1083. if (trans)
  1084. btrfs_get_tree_mod_seq(fs_info, &elem);
  1085. else
  1086. down_read(&fs_info->commit_root_sem);
  1087. ULIST_ITER_INIT(&uiter);
  1088. while (1) {
  1089. ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
  1090. roots, NULL, root_objectid, inum);
  1091. if (ret == BACKREF_FOUND_SHARED) {
  1092. ret = 1;
  1093. break;
  1094. }
  1095. if (ret < 0 && ret != -ENOENT)
  1096. break;
  1097. node = ulist_next(tmp, &uiter);
  1098. if (!node)
  1099. break;
  1100. bytenr = node->val;
  1101. cond_resched();
  1102. }
  1103. if (trans)
  1104. btrfs_put_tree_mod_seq(fs_info, &elem);
  1105. else
  1106. up_read(&fs_info->commit_root_sem);
  1107. ulist_free(tmp);
  1108. ulist_free(roots);
  1109. return ret;
  1110. }
  1111. /*
  1112. * this makes the path point to (inum INODE_ITEM ioff)
  1113. */
  1114. int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
  1115. struct btrfs_path *path)
  1116. {
  1117. struct btrfs_key key;
  1118. return btrfs_find_item(fs_root, path, inum, ioff,
  1119. BTRFS_INODE_ITEM_KEY, &key);
  1120. }
  1121. static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
  1122. struct btrfs_path *path,
  1123. struct btrfs_key *found_key)
  1124. {
  1125. return btrfs_find_item(fs_root, path, inum, ioff,
  1126. BTRFS_INODE_REF_KEY, found_key);
  1127. }
  1128. int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
  1129. u64 start_off, struct btrfs_path *path,
  1130. struct btrfs_inode_extref **ret_extref,
  1131. u64 *found_off)
  1132. {
  1133. int ret, slot;
  1134. struct btrfs_key key;
  1135. struct btrfs_key found_key;
  1136. struct btrfs_inode_extref *extref;
  1137. struct extent_buffer *leaf;
  1138. unsigned long ptr;
  1139. key.objectid = inode_objectid;
  1140. key.type = BTRFS_INODE_EXTREF_KEY;
  1141. key.offset = start_off;
  1142. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1143. if (ret < 0)
  1144. return ret;
  1145. while (1) {
  1146. leaf = path->nodes[0];
  1147. slot = path->slots[0];
  1148. if (slot >= btrfs_header_nritems(leaf)) {
  1149. /*
  1150. * If the item at offset is not found,
  1151. * btrfs_search_slot will point us to the slot
  1152. * where it should be inserted. In our case
  1153. * that will be the slot directly before the
  1154. * next INODE_REF_KEY_V2 item. In the case
  1155. * that we're pointing to the last slot in a
  1156. * leaf, we must move one leaf over.
  1157. */
  1158. ret = btrfs_next_leaf(root, path);
  1159. if (ret) {
  1160. if (ret >= 1)
  1161. ret = -ENOENT;
  1162. break;
  1163. }
  1164. continue;
  1165. }
  1166. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1167. /*
  1168. * Check that we're still looking at an extended ref key for
  1169. * this particular objectid. If we have different
  1170. * objectid or type then there are no more to be found
  1171. * in the tree and we can exit.
  1172. */
  1173. ret = -ENOENT;
  1174. if (found_key.objectid != inode_objectid)
  1175. break;
  1176. if (found_key.type != BTRFS_INODE_EXTREF_KEY)
  1177. break;
  1178. ret = 0;
  1179. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1180. extref = (struct btrfs_inode_extref *)ptr;
  1181. *ret_extref = extref;
  1182. if (found_off)
  1183. *found_off = found_key.offset;
  1184. break;
  1185. }
  1186. return ret;
  1187. }
  1188. /*
  1189. * this iterates to turn a name (from iref/extref) into a full filesystem path.
  1190. * Elements of the path are separated by '/' and the path is guaranteed to be
  1191. * 0-terminated. the path is only given within the current file system.
  1192. * Therefore, it never starts with a '/'. the caller is responsible to provide
  1193. * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
  1194. * the start point of the resulting string is returned. this pointer is within
  1195. * dest, normally.
  1196. * in case the path buffer would overflow, the pointer is decremented further
  1197. * as if output was written to the buffer, though no more output is actually
  1198. * generated. that way, the caller can determine how much space would be
  1199. * required for the path to fit into the buffer. in that case, the returned
  1200. * value will be smaller than dest. callers must check this!
  1201. */
  1202. char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
  1203. u32 name_len, unsigned long name_off,
  1204. struct extent_buffer *eb_in, u64 parent,
  1205. char *dest, u32 size)
  1206. {
  1207. int slot;
  1208. u64 next_inum;
  1209. int ret;
  1210. s64 bytes_left = ((s64)size) - 1;
  1211. struct extent_buffer *eb = eb_in;
  1212. struct btrfs_key found_key;
  1213. int leave_spinning = path->leave_spinning;
  1214. struct btrfs_inode_ref *iref;
  1215. if (bytes_left >= 0)
  1216. dest[bytes_left] = '\0';
  1217. path->leave_spinning = 1;
  1218. while (1) {
  1219. bytes_left -= name_len;
  1220. if (bytes_left >= 0)
  1221. read_extent_buffer(eb, dest + bytes_left,
  1222. name_off, name_len);
  1223. if (eb != eb_in) {
  1224. btrfs_tree_read_unlock_blocking(eb);
  1225. free_extent_buffer(eb);
  1226. }
  1227. ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
  1228. if (ret > 0)
  1229. ret = -ENOENT;
  1230. if (ret)
  1231. break;
  1232. next_inum = found_key.offset;
  1233. /* regular exit ahead */
  1234. if (parent == next_inum)
  1235. break;
  1236. slot = path->slots[0];
  1237. eb = path->nodes[0];
  1238. /* make sure we can use eb after releasing the path */
  1239. if (eb != eb_in) {
  1240. atomic_inc(&eb->refs);
  1241. btrfs_tree_read_lock(eb);
  1242. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1243. }
  1244. btrfs_release_path(path);
  1245. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1246. name_len = btrfs_inode_ref_name_len(eb, iref);
  1247. name_off = (unsigned long)(iref + 1);
  1248. parent = next_inum;
  1249. --bytes_left;
  1250. if (bytes_left >= 0)
  1251. dest[bytes_left] = '/';
  1252. }
  1253. btrfs_release_path(path);
  1254. path->leave_spinning = leave_spinning;
  1255. if (ret)
  1256. return ERR_PTR(ret);
  1257. return dest + bytes_left;
  1258. }
  1259. /*
  1260. * this makes the path point to (logical EXTENT_ITEM *)
  1261. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  1262. * tree blocks and <0 on error.
  1263. */
  1264. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  1265. struct btrfs_path *path, struct btrfs_key *found_key,
  1266. u64 *flags_ret)
  1267. {
  1268. int ret;
  1269. u64 flags;
  1270. u64 size = 0;
  1271. u32 item_size;
  1272. struct extent_buffer *eb;
  1273. struct btrfs_extent_item *ei;
  1274. struct btrfs_key key;
  1275. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1276. key.type = BTRFS_METADATA_ITEM_KEY;
  1277. else
  1278. key.type = BTRFS_EXTENT_ITEM_KEY;
  1279. key.objectid = logical;
  1280. key.offset = (u64)-1;
  1281. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  1282. if (ret < 0)
  1283. return ret;
  1284. ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
  1285. if (ret) {
  1286. if (ret > 0)
  1287. ret = -ENOENT;
  1288. return ret;
  1289. }
  1290. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  1291. if (found_key->type == BTRFS_METADATA_ITEM_KEY)
  1292. size = fs_info->extent_root->nodesize;
  1293. else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
  1294. size = found_key->offset;
  1295. if (found_key->objectid > logical ||
  1296. found_key->objectid + size <= logical) {
  1297. pr_debug("logical %llu is not within any extent\n", logical);
  1298. return -ENOENT;
  1299. }
  1300. eb = path->nodes[0];
  1301. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  1302. BUG_ON(item_size < sizeof(*ei));
  1303. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  1304. flags = btrfs_extent_flags(eb, ei);
  1305. pr_debug("logical %llu is at position %llu within the extent (%llu "
  1306. "EXTENT_ITEM %llu) flags %#llx size %u\n",
  1307. logical, logical - found_key->objectid, found_key->objectid,
  1308. found_key->offset, flags, item_size);
  1309. WARN_ON(!flags_ret);
  1310. if (flags_ret) {
  1311. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1312. *flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
  1313. else if (flags & BTRFS_EXTENT_FLAG_DATA)
  1314. *flags_ret = BTRFS_EXTENT_FLAG_DATA;
  1315. else
  1316. BUG_ON(1);
  1317. return 0;
  1318. }
  1319. return -EIO;
  1320. }
  1321. /*
  1322. * helper function to iterate extent inline refs. ptr must point to a 0 value
  1323. * for the first call and may be modified. it is used to track state.
  1324. * if more refs exist, 0 is returned and the next call to
  1325. * __get_extent_inline_ref must pass the modified ptr parameter to get the
  1326. * next ref. after the last ref was processed, 1 is returned.
  1327. * returns <0 on error
  1328. */
  1329. static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
  1330. struct btrfs_key *key,
  1331. struct btrfs_extent_item *ei, u32 item_size,
  1332. struct btrfs_extent_inline_ref **out_eiref,
  1333. int *out_type)
  1334. {
  1335. unsigned long end;
  1336. u64 flags;
  1337. struct btrfs_tree_block_info *info;
  1338. if (!*ptr) {
  1339. /* first call */
  1340. flags = btrfs_extent_flags(eb, ei);
  1341. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1342. if (key->type == BTRFS_METADATA_ITEM_KEY) {
  1343. /* a skinny metadata extent */
  1344. *out_eiref =
  1345. (struct btrfs_extent_inline_ref *)(ei + 1);
  1346. } else {
  1347. WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
  1348. info = (struct btrfs_tree_block_info *)(ei + 1);
  1349. *out_eiref =
  1350. (struct btrfs_extent_inline_ref *)(info + 1);
  1351. }
  1352. } else {
  1353. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  1354. }
  1355. *ptr = (unsigned long)*out_eiref;
  1356. if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
  1357. return -ENOENT;
  1358. }
  1359. end = (unsigned long)ei + item_size;
  1360. *out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
  1361. *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
  1362. *ptr += btrfs_extent_inline_ref_size(*out_type);
  1363. WARN_ON(*ptr > end);
  1364. if (*ptr == end)
  1365. return 1; /* last */
  1366. return 0;
  1367. }
  1368. /*
  1369. * reads the tree block backref for an extent. tree level and root are returned
  1370. * through out_level and out_root. ptr must point to a 0 value for the first
  1371. * call and may be modified (see __get_extent_inline_ref comment).
  1372. * returns 0 if data was provided, 1 if there was no more data to provide or
  1373. * <0 on error.
  1374. */
  1375. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  1376. struct btrfs_key *key, struct btrfs_extent_item *ei,
  1377. u32 item_size, u64 *out_root, u8 *out_level)
  1378. {
  1379. int ret;
  1380. int type;
  1381. struct btrfs_tree_block_info *info;
  1382. struct btrfs_extent_inline_ref *eiref;
  1383. if (*ptr == (unsigned long)-1)
  1384. return 1;
  1385. while (1) {
  1386. ret = __get_extent_inline_ref(ptr, eb, key, ei, item_size,
  1387. &eiref, &type);
  1388. if (ret < 0)
  1389. return ret;
  1390. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1391. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1392. break;
  1393. if (ret == 1)
  1394. return 1;
  1395. }
  1396. /* we can treat both ref types equally here */
  1397. info = (struct btrfs_tree_block_info *)(ei + 1);
  1398. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  1399. *out_level = btrfs_tree_block_level(eb, info);
  1400. if (ret == 1)
  1401. *ptr = (unsigned long)-1;
  1402. return 0;
  1403. }
  1404. static int iterate_leaf_refs(struct extent_inode_elem *inode_list,
  1405. u64 root, u64 extent_item_objectid,
  1406. iterate_extent_inodes_t *iterate, void *ctx)
  1407. {
  1408. struct extent_inode_elem *eie;
  1409. int ret = 0;
  1410. for (eie = inode_list; eie; eie = eie->next) {
  1411. pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
  1412. "root %llu\n", extent_item_objectid,
  1413. eie->inum, eie->offset, root);
  1414. ret = iterate(eie->inum, eie->offset, root, ctx);
  1415. if (ret) {
  1416. pr_debug("stopping iteration for %llu due to ret=%d\n",
  1417. extent_item_objectid, ret);
  1418. break;
  1419. }
  1420. }
  1421. return ret;
  1422. }
  1423. /*
  1424. * calls iterate() for every inode that references the extent identified by
  1425. * the given parameters.
  1426. * when the iterator function returns a non-zero value, iteration stops.
  1427. */
  1428. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1429. u64 extent_item_objectid, u64 extent_item_pos,
  1430. int search_commit_root,
  1431. iterate_extent_inodes_t *iterate, void *ctx)
  1432. {
  1433. int ret;
  1434. struct btrfs_trans_handle *trans = NULL;
  1435. struct ulist *refs = NULL;
  1436. struct ulist *roots = NULL;
  1437. struct ulist_node *ref_node = NULL;
  1438. struct ulist_node *root_node = NULL;
  1439. struct seq_list tree_mod_seq_elem = {};
  1440. struct ulist_iterator ref_uiter;
  1441. struct ulist_iterator root_uiter;
  1442. pr_debug("resolving all inodes for extent %llu\n",
  1443. extent_item_objectid);
  1444. if (!search_commit_root) {
  1445. trans = btrfs_join_transaction(fs_info->extent_root);
  1446. if (IS_ERR(trans))
  1447. return PTR_ERR(trans);
  1448. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1449. } else {
  1450. down_read(&fs_info->commit_root_sem);
  1451. }
  1452. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1453. tree_mod_seq_elem.seq, &refs,
  1454. &extent_item_pos);
  1455. if (ret)
  1456. goto out;
  1457. ULIST_ITER_INIT(&ref_uiter);
  1458. while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
  1459. ret = __btrfs_find_all_roots(trans, fs_info, ref_node->val,
  1460. tree_mod_seq_elem.seq, &roots);
  1461. if (ret)
  1462. break;
  1463. ULIST_ITER_INIT(&root_uiter);
  1464. while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
  1465. pr_debug("root %llu references leaf %llu, data list "
  1466. "%#llx\n", root_node->val, ref_node->val,
  1467. ref_node->aux);
  1468. ret = iterate_leaf_refs((struct extent_inode_elem *)
  1469. (uintptr_t)ref_node->aux,
  1470. root_node->val,
  1471. extent_item_objectid,
  1472. iterate, ctx);
  1473. }
  1474. ulist_free(roots);
  1475. }
  1476. free_leaf_list(refs);
  1477. out:
  1478. if (!search_commit_root) {
  1479. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1480. btrfs_end_transaction(trans, fs_info->extent_root);
  1481. } else {
  1482. up_read(&fs_info->commit_root_sem);
  1483. }
  1484. return ret;
  1485. }
  1486. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1487. struct btrfs_path *path,
  1488. iterate_extent_inodes_t *iterate, void *ctx)
  1489. {
  1490. int ret;
  1491. u64 extent_item_pos;
  1492. u64 flags = 0;
  1493. struct btrfs_key found_key;
  1494. int search_commit_root = path->search_commit_root;
  1495. ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
  1496. btrfs_release_path(path);
  1497. if (ret < 0)
  1498. return ret;
  1499. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1500. return -EINVAL;
  1501. extent_item_pos = logical - found_key.objectid;
  1502. ret = iterate_extent_inodes(fs_info, found_key.objectid,
  1503. extent_item_pos, search_commit_root,
  1504. iterate, ctx);
  1505. return ret;
  1506. }
  1507. typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
  1508. struct extent_buffer *eb, void *ctx);
  1509. static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
  1510. struct btrfs_path *path,
  1511. iterate_irefs_t *iterate, void *ctx)
  1512. {
  1513. int ret = 0;
  1514. int slot;
  1515. u32 cur;
  1516. u32 len;
  1517. u32 name_len;
  1518. u64 parent = 0;
  1519. int found = 0;
  1520. struct extent_buffer *eb;
  1521. struct btrfs_item *item;
  1522. struct btrfs_inode_ref *iref;
  1523. struct btrfs_key found_key;
  1524. while (!ret) {
  1525. ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
  1526. &found_key);
  1527. if (ret < 0)
  1528. break;
  1529. if (ret) {
  1530. ret = found ? 0 : -ENOENT;
  1531. break;
  1532. }
  1533. ++found;
  1534. parent = found_key.offset;
  1535. slot = path->slots[0];
  1536. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1537. if (!eb) {
  1538. ret = -ENOMEM;
  1539. break;
  1540. }
  1541. extent_buffer_get(eb);
  1542. btrfs_tree_read_lock(eb);
  1543. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1544. btrfs_release_path(path);
  1545. item = btrfs_item_nr(slot);
  1546. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1547. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1548. name_len = btrfs_inode_ref_name_len(eb, iref);
  1549. /* path must be released before calling iterate()! */
  1550. pr_debug("following ref at offset %u for inode %llu in "
  1551. "tree %llu\n", cur, found_key.objectid,
  1552. fs_root->objectid);
  1553. ret = iterate(parent, name_len,
  1554. (unsigned long)(iref + 1), eb, ctx);
  1555. if (ret)
  1556. break;
  1557. len = sizeof(*iref) + name_len;
  1558. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1559. }
  1560. btrfs_tree_read_unlock_blocking(eb);
  1561. free_extent_buffer(eb);
  1562. }
  1563. btrfs_release_path(path);
  1564. return ret;
  1565. }
  1566. static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
  1567. struct btrfs_path *path,
  1568. iterate_irefs_t *iterate, void *ctx)
  1569. {
  1570. int ret;
  1571. int slot;
  1572. u64 offset = 0;
  1573. u64 parent;
  1574. int found = 0;
  1575. struct extent_buffer *eb;
  1576. struct btrfs_inode_extref *extref;
  1577. struct extent_buffer *leaf;
  1578. u32 item_size;
  1579. u32 cur_offset;
  1580. unsigned long ptr;
  1581. while (1) {
  1582. ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
  1583. &offset);
  1584. if (ret < 0)
  1585. break;
  1586. if (ret) {
  1587. ret = found ? 0 : -ENOENT;
  1588. break;
  1589. }
  1590. ++found;
  1591. slot = path->slots[0];
  1592. eb = btrfs_clone_extent_buffer(path->nodes[0]);
  1593. if (!eb) {
  1594. ret = -ENOMEM;
  1595. break;
  1596. }
  1597. extent_buffer_get(eb);
  1598. btrfs_tree_read_lock(eb);
  1599. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1600. btrfs_release_path(path);
  1601. leaf = path->nodes[0];
  1602. item_size = btrfs_item_size_nr(leaf, slot);
  1603. ptr = btrfs_item_ptr_offset(leaf, slot);
  1604. cur_offset = 0;
  1605. while (cur_offset < item_size) {
  1606. u32 name_len;
  1607. extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
  1608. parent = btrfs_inode_extref_parent(eb, extref);
  1609. name_len = btrfs_inode_extref_name_len(eb, extref);
  1610. ret = iterate(parent, name_len,
  1611. (unsigned long)&extref->name, eb, ctx);
  1612. if (ret)
  1613. break;
  1614. cur_offset += btrfs_inode_extref_name_len(leaf, extref);
  1615. cur_offset += sizeof(*extref);
  1616. }
  1617. btrfs_tree_read_unlock_blocking(eb);
  1618. free_extent_buffer(eb);
  1619. offset++;
  1620. }
  1621. btrfs_release_path(path);
  1622. return ret;
  1623. }
  1624. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1625. struct btrfs_path *path, iterate_irefs_t *iterate,
  1626. void *ctx)
  1627. {
  1628. int ret;
  1629. int found_refs = 0;
  1630. ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
  1631. if (!ret)
  1632. ++found_refs;
  1633. else if (ret != -ENOENT)
  1634. return ret;
  1635. ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
  1636. if (ret == -ENOENT && found_refs)
  1637. return 0;
  1638. return ret;
  1639. }
  1640. /*
  1641. * returns 0 if the path could be dumped (probably truncated)
  1642. * returns <0 in case of an error
  1643. */
  1644. static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
  1645. struct extent_buffer *eb, void *ctx)
  1646. {
  1647. struct inode_fs_paths *ipath = ctx;
  1648. char *fspath;
  1649. char *fspath_min;
  1650. int i = ipath->fspath->elem_cnt;
  1651. const int s_ptr = sizeof(char *);
  1652. u32 bytes_left;
  1653. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  1654. ipath->fspath->bytes_left - s_ptr : 0;
  1655. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  1656. fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
  1657. name_off, eb, inum, fspath_min, bytes_left);
  1658. if (IS_ERR(fspath))
  1659. return PTR_ERR(fspath);
  1660. if (fspath > fspath_min) {
  1661. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  1662. ++ipath->fspath->elem_cnt;
  1663. ipath->fspath->bytes_left = fspath - fspath_min;
  1664. } else {
  1665. ++ipath->fspath->elem_missed;
  1666. ipath->fspath->bytes_missing += fspath_min - fspath;
  1667. ipath->fspath->bytes_left = 0;
  1668. }
  1669. return 0;
  1670. }
  1671. /*
  1672. * this dumps all file system paths to the inode into the ipath struct, provided
  1673. * is has been created large enough. each path is zero-terminated and accessed
  1674. * from ipath->fspath->val[i].
  1675. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  1676. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  1677. * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
  1678. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  1679. * have been needed to return all paths.
  1680. */
  1681. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  1682. {
  1683. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  1684. inode_to_path, ipath);
  1685. }
  1686. struct btrfs_data_container *init_data_container(u32 total_bytes)
  1687. {
  1688. struct btrfs_data_container *data;
  1689. size_t alloc_bytes;
  1690. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  1691. data = vmalloc(alloc_bytes);
  1692. if (!data)
  1693. return ERR_PTR(-ENOMEM);
  1694. if (total_bytes >= sizeof(*data)) {
  1695. data->bytes_left = total_bytes - sizeof(*data);
  1696. data->bytes_missing = 0;
  1697. } else {
  1698. data->bytes_missing = sizeof(*data) - total_bytes;
  1699. data->bytes_left = 0;
  1700. }
  1701. data->elem_cnt = 0;
  1702. data->elem_missed = 0;
  1703. return data;
  1704. }
  1705. /*
  1706. * allocates space to return multiple file system paths for an inode.
  1707. * total_bytes to allocate are passed, note that space usable for actual path
  1708. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1709. * the returned pointer must be freed with free_ipath() in the end.
  1710. */
  1711. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  1712. struct btrfs_path *path)
  1713. {
  1714. struct inode_fs_paths *ifp;
  1715. struct btrfs_data_container *fspath;
  1716. fspath = init_data_container(total_bytes);
  1717. if (IS_ERR(fspath))
  1718. return (void *)fspath;
  1719. ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
  1720. if (!ifp) {
  1721. kfree(fspath);
  1722. return ERR_PTR(-ENOMEM);
  1723. }
  1724. ifp->btrfs_path = path;
  1725. ifp->fspath = fspath;
  1726. ifp->fs_root = fs_root;
  1727. return ifp;
  1728. }
  1729. void free_ipath(struct inode_fs_paths *ipath)
  1730. {
  1731. if (!ipath)
  1732. return;
  1733. vfree(ipath->fspath);
  1734. kfree(ipath);
  1735. }