backref.c 46 KB

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