backref.c 52 KB

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