backref.c 52 KB

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