backref.c 36 KB

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