backref.c 52 KB

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