backref.c 43 KB

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