backref.c 37 KB

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