backref.c 52 KB

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