backref.c 47 KB

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