backref.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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)
  789. break;
  790. next_inum = found_key.offset;
  791. /* regular exit ahead */
  792. if (parent == next_inum)
  793. break;
  794. slot = path->slots[0];
  795. eb = path->nodes[0];
  796. /* make sure we can use eb after releasing the path */
  797. if (eb != eb_in)
  798. atomic_inc(&eb->refs);
  799. btrfs_release_path(path);
  800. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  801. parent = next_inum;
  802. --bytes_left;
  803. if (bytes_left >= 0)
  804. dest[bytes_left] = '/';
  805. }
  806. btrfs_release_path(path);
  807. if (ret)
  808. return ERR_PTR(ret);
  809. return dest + bytes_left;
  810. }
  811. /*
  812. * this makes the path point to (logical EXTENT_ITEM *)
  813. * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
  814. * tree blocks and <0 on error.
  815. */
  816. int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
  817. struct btrfs_path *path, struct btrfs_key *found_key)
  818. {
  819. int ret;
  820. u64 flags;
  821. u32 item_size;
  822. struct extent_buffer *eb;
  823. struct btrfs_extent_item *ei;
  824. struct btrfs_key key;
  825. key.type = BTRFS_EXTENT_ITEM_KEY;
  826. key.objectid = logical;
  827. key.offset = (u64)-1;
  828. ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
  829. if (ret < 0)
  830. return ret;
  831. ret = btrfs_previous_item(fs_info->extent_root, path,
  832. 0, BTRFS_EXTENT_ITEM_KEY);
  833. if (ret < 0)
  834. return ret;
  835. btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
  836. if (found_key->type != BTRFS_EXTENT_ITEM_KEY ||
  837. found_key->objectid > logical ||
  838. found_key->objectid + found_key->offset <= logical) {
  839. pr_debug("logical %llu is not within any extent\n",
  840. (unsigned long long)logical);
  841. return -ENOENT;
  842. }
  843. eb = path->nodes[0];
  844. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  845. BUG_ON(item_size < sizeof(*ei));
  846. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  847. flags = btrfs_extent_flags(eb, ei);
  848. pr_debug("logical %llu is at position %llu within the extent (%llu "
  849. "EXTENT_ITEM %llu) flags %#llx size %u\n",
  850. (unsigned long long)logical,
  851. (unsigned long long)(logical - found_key->objectid),
  852. (unsigned long long)found_key->objectid,
  853. (unsigned long long)found_key->offset,
  854. (unsigned long long)flags, item_size);
  855. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  856. return BTRFS_EXTENT_FLAG_TREE_BLOCK;
  857. if (flags & BTRFS_EXTENT_FLAG_DATA)
  858. return BTRFS_EXTENT_FLAG_DATA;
  859. return -EIO;
  860. }
  861. /*
  862. * helper function to iterate extent inline refs. ptr must point to a 0 value
  863. * for the first call and may be modified. it is used to track state.
  864. * if more refs exist, 0 is returned and the next call to
  865. * __get_extent_inline_ref must pass the modified ptr parameter to get the
  866. * next ref. after the last ref was processed, 1 is returned.
  867. * returns <0 on error
  868. */
  869. static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
  870. struct btrfs_extent_item *ei, u32 item_size,
  871. struct btrfs_extent_inline_ref **out_eiref,
  872. int *out_type)
  873. {
  874. unsigned long end;
  875. u64 flags;
  876. struct btrfs_tree_block_info *info;
  877. if (!*ptr) {
  878. /* first call */
  879. flags = btrfs_extent_flags(eb, ei);
  880. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  881. info = (struct btrfs_tree_block_info *)(ei + 1);
  882. *out_eiref =
  883. (struct btrfs_extent_inline_ref *)(info + 1);
  884. } else {
  885. *out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
  886. }
  887. *ptr = (unsigned long)*out_eiref;
  888. if ((void *)*ptr >= (void *)ei + item_size)
  889. return -ENOENT;
  890. }
  891. end = (unsigned long)ei + item_size;
  892. *out_eiref = (struct btrfs_extent_inline_ref *)*ptr;
  893. *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
  894. *ptr += btrfs_extent_inline_ref_size(*out_type);
  895. WARN_ON(*ptr > end);
  896. if (*ptr == end)
  897. return 1; /* last */
  898. return 0;
  899. }
  900. /*
  901. * reads the tree block backref for an extent. tree level and root are returned
  902. * through out_level and out_root. ptr must point to a 0 value for the first
  903. * call and may be modified (see __get_extent_inline_ref comment).
  904. * returns 0 if data was provided, 1 if there was no more data to provide or
  905. * <0 on error.
  906. */
  907. int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
  908. struct btrfs_extent_item *ei, u32 item_size,
  909. u64 *out_root, u8 *out_level)
  910. {
  911. int ret;
  912. int type;
  913. struct btrfs_tree_block_info *info;
  914. struct btrfs_extent_inline_ref *eiref;
  915. if (*ptr == (unsigned long)-1)
  916. return 1;
  917. while (1) {
  918. ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
  919. &eiref, &type);
  920. if (ret < 0)
  921. return ret;
  922. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  923. type == BTRFS_SHARED_BLOCK_REF_KEY)
  924. break;
  925. if (ret == 1)
  926. return 1;
  927. }
  928. /* we can treat both ref types equally here */
  929. info = (struct btrfs_tree_block_info *)(ei + 1);
  930. *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
  931. *out_level = btrfs_tree_block_level(eb, info);
  932. if (ret == 1)
  933. *ptr = (unsigned long)-1;
  934. return 0;
  935. }
  936. static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
  937. struct btrfs_path *path, u64 logical,
  938. u64 orig_extent_item_objectid,
  939. u64 extent_item_pos, u64 root,
  940. iterate_extent_inodes_t *iterate, void *ctx)
  941. {
  942. u64 disk_byte;
  943. struct btrfs_key key;
  944. struct btrfs_file_extent_item *fi;
  945. struct extent_buffer *eb;
  946. int slot;
  947. int nritems;
  948. int ret = 0;
  949. int extent_type;
  950. u64 data_offset;
  951. u64 data_len;
  952. eb = read_tree_block(fs_info->tree_root, logical,
  953. fs_info->tree_root->leafsize, 0);
  954. if (!eb)
  955. return -EIO;
  956. /*
  957. * from the shared data ref, we only have the leaf but we need
  958. * the key. thus, we must look into all items and see that we
  959. * find one (some) with a reference to our extent item.
  960. */
  961. nritems = btrfs_header_nritems(eb);
  962. for (slot = 0; slot < nritems; ++slot) {
  963. btrfs_item_key_to_cpu(eb, &key, slot);
  964. if (key.type != BTRFS_EXTENT_DATA_KEY)
  965. continue;
  966. fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  967. extent_type = btrfs_file_extent_type(eb, fi);
  968. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  969. continue;
  970. /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
  971. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  972. if (disk_byte != orig_extent_item_objectid)
  973. continue;
  974. data_offset = btrfs_file_extent_offset(eb, fi);
  975. data_len = btrfs_file_extent_num_bytes(eb, fi);
  976. if (extent_item_pos < data_offset ||
  977. extent_item_pos >= data_offset + data_len)
  978. continue;
  979. pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
  980. "root %llu\n", orig_extent_item_objectid,
  981. key.objectid, key.offset, root);
  982. ret = iterate(key.objectid,
  983. key.offset + (extent_item_pos - data_offset),
  984. root, ctx);
  985. if (ret) {
  986. pr_debug("stopping iteration because ret=%d\n", ret);
  987. break;
  988. }
  989. }
  990. free_extent_buffer(eb);
  991. return ret;
  992. }
  993. /*
  994. * calls iterate() for every inode that references the extent identified by
  995. * the given parameters.
  996. * when the iterator function returns a non-zero value, iteration stops.
  997. * path is guaranteed to be in released state when iterate() is called.
  998. */
  999. int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
  1000. struct btrfs_path *path,
  1001. u64 extent_item_objectid, u64 extent_item_pos,
  1002. iterate_extent_inodes_t *iterate, void *ctx)
  1003. {
  1004. int ret;
  1005. struct list_head data_refs = LIST_HEAD_INIT(data_refs);
  1006. struct list_head shared_refs = LIST_HEAD_INIT(shared_refs);
  1007. struct btrfs_trans_handle *trans;
  1008. struct ulist *refs;
  1009. struct ulist *roots;
  1010. struct ulist_node *ref_node = NULL;
  1011. struct ulist_node *root_node = NULL;
  1012. struct seq_list seq_elem;
  1013. struct btrfs_delayed_ref_root *delayed_refs;
  1014. trans = btrfs_join_transaction(fs_info->extent_root);
  1015. if (IS_ERR(trans))
  1016. return PTR_ERR(trans);
  1017. pr_debug("resolving all inodes for extent %llu\n",
  1018. extent_item_objectid);
  1019. delayed_refs = &trans->transaction->delayed_refs;
  1020. spin_lock(&delayed_refs->lock);
  1021. btrfs_get_delayed_seq(delayed_refs, &seq_elem);
  1022. spin_unlock(&delayed_refs->lock);
  1023. ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
  1024. extent_item_pos, seq_elem.seq,
  1025. &refs);
  1026. if (ret)
  1027. goto out;
  1028. while (!ret && (ref_node = ulist_next(refs, ref_node))) {
  1029. ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, -1,
  1030. seq_elem.seq, &roots);
  1031. if (ret)
  1032. break;
  1033. while (!ret && (root_node = ulist_next(roots, root_node))) {
  1034. pr_debug("root %llu references leaf %llu\n",
  1035. root_node->val, ref_node->val);
  1036. ret = iterate_leaf_refs(fs_info, path, ref_node->val,
  1037. extent_item_objectid,
  1038. extent_item_pos, root_node->val,
  1039. iterate, ctx);
  1040. }
  1041. }
  1042. ulist_free(refs);
  1043. ulist_free(roots);
  1044. out:
  1045. btrfs_put_delayed_seq(delayed_refs, &seq_elem);
  1046. btrfs_end_transaction(trans, fs_info->extent_root);
  1047. return ret;
  1048. }
  1049. int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  1050. struct btrfs_path *path,
  1051. iterate_extent_inodes_t *iterate, void *ctx)
  1052. {
  1053. int ret;
  1054. u64 extent_item_pos;
  1055. struct btrfs_key found_key;
  1056. ret = extent_from_logical(fs_info, logical, path,
  1057. &found_key);
  1058. btrfs_release_path(path);
  1059. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1060. ret = -EINVAL;
  1061. if (ret < 0)
  1062. return ret;
  1063. extent_item_pos = logical - found_key.objectid;
  1064. ret = iterate_extent_inodes(fs_info, path, found_key.objectid,
  1065. extent_item_pos, iterate, ctx);
  1066. return ret;
  1067. }
  1068. static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
  1069. struct btrfs_path *path,
  1070. iterate_irefs_t *iterate, void *ctx)
  1071. {
  1072. int ret;
  1073. int slot;
  1074. u32 cur;
  1075. u32 len;
  1076. u32 name_len;
  1077. u64 parent = 0;
  1078. int found = 0;
  1079. struct extent_buffer *eb;
  1080. struct btrfs_item *item;
  1081. struct btrfs_inode_ref *iref;
  1082. struct btrfs_key found_key;
  1083. while (1) {
  1084. ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
  1085. &found_key);
  1086. if (ret < 0)
  1087. break;
  1088. if (ret) {
  1089. ret = found ? 0 : -ENOENT;
  1090. break;
  1091. }
  1092. ++found;
  1093. parent = found_key.offset;
  1094. slot = path->slots[0];
  1095. eb = path->nodes[0];
  1096. /* make sure we can use eb after releasing the path */
  1097. atomic_inc(&eb->refs);
  1098. btrfs_release_path(path);
  1099. item = btrfs_item_nr(eb, slot);
  1100. iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
  1101. for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
  1102. name_len = btrfs_inode_ref_name_len(eb, iref);
  1103. /* path must be released before calling iterate()! */
  1104. pr_debug("following ref at offset %u for inode %llu in "
  1105. "tree %llu\n", cur,
  1106. (unsigned long long)found_key.objectid,
  1107. (unsigned long long)fs_root->objectid);
  1108. ret = iterate(parent, iref, eb, ctx);
  1109. if (ret) {
  1110. free_extent_buffer(eb);
  1111. break;
  1112. }
  1113. len = sizeof(*iref) + name_len;
  1114. iref = (struct btrfs_inode_ref *)((char *)iref + len);
  1115. }
  1116. free_extent_buffer(eb);
  1117. }
  1118. btrfs_release_path(path);
  1119. return ret;
  1120. }
  1121. /*
  1122. * returns 0 if the path could be dumped (probably truncated)
  1123. * returns <0 in case of an error
  1124. */
  1125. static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref,
  1126. struct extent_buffer *eb, void *ctx)
  1127. {
  1128. struct inode_fs_paths *ipath = ctx;
  1129. char *fspath;
  1130. char *fspath_min;
  1131. int i = ipath->fspath->elem_cnt;
  1132. const int s_ptr = sizeof(char *);
  1133. u32 bytes_left;
  1134. bytes_left = ipath->fspath->bytes_left > s_ptr ?
  1135. ipath->fspath->bytes_left - s_ptr : 0;
  1136. fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
  1137. fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb,
  1138. inum, fspath_min, bytes_left);
  1139. if (IS_ERR(fspath))
  1140. return PTR_ERR(fspath);
  1141. if (fspath > fspath_min) {
  1142. pr_debug("path resolved: %s\n", fspath);
  1143. ipath->fspath->val[i] = (u64)(unsigned long)fspath;
  1144. ++ipath->fspath->elem_cnt;
  1145. ipath->fspath->bytes_left = fspath - fspath_min;
  1146. } else {
  1147. pr_debug("missed path, not enough space. missing bytes: %lu, "
  1148. "constructed so far: %s\n",
  1149. (unsigned long)(fspath_min - fspath), fspath_min);
  1150. ++ipath->fspath->elem_missed;
  1151. ipath->fspath->bytes_missing += fspath_min - fspath;
  1152. ipath->fspath->bytes_left = 0;
  1153. }
  1154. return 0;
  1155. }
  1156. /*
  1157. * this dumps all file system paths to the inode into the ipath struct, provided
  1158. * is has been created large enough. each path is zero-terminated and accessed
  1159. * from ipath->fspath->val[i].
  1160. * when it returns, there are ipath->fspath->elem_cnt number of paths available
  1161. * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
  1162. * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
  1163. * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
  1164. * have been needed to return all paths.
  1165. */
  1166. int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
  1167. {
  1168. return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
  1169. inode_to_path, ipath);
  1170. }
  1171. /*
  1172. * allocates space to return multiple file system paths for an inode.
  1173. * total_bytes to allocate are passed, note that space usable for actual path
  1174. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1175. * the returned pointer must be freed with free_ipath() in the end.
  1176. */
  1177. struct btrfs_data_container *init_data_container(u32 total_bytes)
  1178. {
  1179. struct btrfs_data_container *data;
  1180. size_t alloc_bytes;
  1181. alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
  1182. data = kmalloc(alloc_bytes, GFP_NOFS);
  1183. if (!data)
  1184. return ERR_PTR(-ENOMEM);
  1185. if (total_bytes >= sizeof(*data)) {
  1186. data->bytes_left = total_bytes - sizeof(*data);
  1187. data->bytes_missing = 0;
  1188. } else {
  1189. data->bytes_missing = sizeof(*data) - total_bytes;
  1190. data->bytes_left = 0;
  1191. }
  1192. data->elem_cnt = 0;
  1193. data->elem_missed = 0;
  1194. return data;
  1195. }
  1196. /*
  1197. * allocates space to return multiple file system paths for an inode.
  1198. * total_bytes to allocate are passed, note that space usable for actual path
  1199. * information will be total_bytes - sizeof(struct inode_fs_paths).
  1200. * the returned pointer must be freed with free_ipath() in the end.
  1201. */
  1202. struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
  1203. struct btrfs_path *path)
  1204. {
  1205. struct inode_fs_paths *ifp;
  1206. struct btrfs_data_container *fspath;
  1207. fspath = init_data_container(total_bytes);
  1208. if (IS_ERR(fspath))
  1209. return (void *)fspath;
  1210. ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
  1211. if (!ifp) {
  1212. kfree(fspath);
  1213. return ERR_PTR(-ENOMEM);
  1214. }
  1215. ifp->btrfs_path = path;
  1216. ifp->fspath = fspath;
  1217. ifp->fs_root = fs_root;
  1218. return ifp;
  1219. }
  1220. void free_ipath(struct inode_fs_paths *ipath)
  1221. {
  1222. kfree(ipath);
  1223. }