backref.c 37 KB

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