backref.c 48 KB

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