backref.c 41 KB

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