backref.c 36 KB

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