backref.c 42 KB

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