backref.c 50 KB

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