backref.c 42 KB

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