inode-tests.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Fusion IO. All rights reserved.
  4. */
  5. #include <linux/types.h>
  6. #include "btrfs-tests.h"
  7. #include "../ctree.h"
  8. #include "../btrfs_inode.h"
  9. #include "../disk-io.h"
  10. #include "../extent_io.h"
  11. #include "../volumes.h"
  12. #include "../compression.h"
  13. static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
  14. u64 ram_bytes, u64 offset, u64 disk_bytenr,
  15. u64 disk_len, u32 type, u8 compression, int slot)
  16. {
  17. struct btrfs_path path;
  18. struct btrfs_file_extent_item *fi;
  19. struct extent_buffer *leaf = root->node;
  20. struct btrfs_key key;
  21. u32 value_len = sizeof(struct btrfs_file_extent_item);
  22. if (type == BTRFS_FILE_EXTENT_INLINE)
  23. value_len += len;
  24. memset(&path, 0, sizeof(path));
  25. path.nodes[0] = leaf;
  26. path.slots[0] = slot;
  27. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  28. key.type = BTRFS_EXTENT_DATA_KEY;
  29. key.offset = start;
  30. setup_items_for_insert(root, &path, &key, &value_len, value_len,
  31. value_len + sizeof(struct btrfs_item), 1);
  32. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  33. btrfs_set_file_extent_generation(leaf, fi, 1);
  34. btrfs_set_file_extent_type(leaf, fi, type);
  35. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  36. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
  37. btrfs_set_file_extent_offset(leaf, fi, offset);
  38. btrfs_set_file_extent_num_bytes(leaf, fi, len);
  39. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  40. btrfs_set_file_extent_compression(leaf, fi, compression);
  41. btrfs_set_file_extent_encryption(leaf, fi, 0);
  42. btrfs_set_file_extent_other_encoding(leaf, fi, 0);
  43. }
  44. static void insert_inode_item_key(struct btrfs_root *root)
  45. {
  46. struct btrfs_path path;
  47. struct extent_buffer *leaf = root->node;
  48. struct btrfs_key key;
  49. u32 value_len = 0;
  50. memset(&path, 0, sizeof(path));
  51. path.nodes[0] = leaf;
  52. path.slots[0] = 0;
  53. key.objectid = BTRFS_INODE_ITEM_KEY;
  54. key.type = BTRFS_INODE_ITEM_KEY;
  55. key.offset = 0;
  56. setup_items_for_insert(root, &path, &key, &value_len, value_len,
  57. value_len + sizeof(struct btrfs_item), 1);
  58. }
  59. /*
  60. * Build the most complicated map of extents the earth has ever seen. We want
  61. * this so we can test all of the corner cases of btrfs_get_extent. Here is a
  62. * diagram of how the extents will look though this may not be possible we still
  63. * want to make sure everything acts normally (the last number is not inclusive)
  64. *
  65. * [0 - 5][5 - 6][ 6 - 4096 ][ 4096 - 4100][4100 - 8195][8195 - 12291]
  66. * [hole ][inline][hole but no extent][ hole ][ regular ][regular1 split]
  67. *
  68. * [12291 - 16387][16387 - 24579][24579 - 28675][ 28675 - 32771][32771 - 36867 ]
  69. * [ hole ][regular1 split][ prealloc ][ prealloc1 ][prealloc1 written]
  70. *
  71. * [36867 - 45059][45059 - 53251][53251 - 57347][57347 - 61443][61443- 69635]
  72. * [ prealloc1 ][ compressed ][ compressed1 ][ regular ][ compressed1]
  73. *
  74. * [69635-73731][ 73731 - 86019 ][86019-90115]
  75. * [ regular ][ hole but no extent][ regular ]
  76. */
  77. static void setup_file_extents(struct btrfs_root *root, u32 sectorsize)
  78. {
  79. int slot = 0;
  80. u64 disk_bytenr = SZ_1M;
  81. u64 offset = 0;
  82. /* First we want a hole */
  83. insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
  84. slot);
  85. slot++;
  86. offset += 5;
  87. /*
  88. * Now we want an inline extent, I don't think this is possible but hey
  89. * why not? Also keep in mind if we have an inline extent it counts as
  90. * the whole first page. If we were to expand it we would have to cow
  91. * and we wouldn't have an inline extent anymore.
  92. */
  93. insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
  94. slot);
  95. slot++;
  96. offset = sectorsize;
  97. /* Now another hole */
  98. insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
  99. slot);
  100. slot++;
  101. offset += 4;
  102. /* Now for a regular extent */
  103. insert_extent(root, offset, sectorsize - 1, sectorsize - 1, 0,
  104. disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
  105. slot++;
  106. disk_bytenr += sectorsize;
  107. offset += sectorsize - 1;
  108. /*
  109. * Now for 3 extents that were split from a hole punch so we test
  110. * offsets properly.
  111. */
  112. insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
  113. 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
  114. slot++;
  115. offset += sectorsize;
  116. insert_extent(root, offset, sectorsize, sectorsize, 0, 0, 0,
  117. BTRFS_FILE_EXTENT_REG, 0, slot);
  118. slot++;
  119. offset += sectorsize;
  120. insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
  121. 2 * sectorsize, disk_bytenr, 4 * sectorsize,
  122. BTRFS_FILE_EXTENT_REG, 0, slot);
  123. slot++;
  124. offset += 2 * sectorsize;
  125. disk_bytenr += 4 * sectorsize;
  126. /* Now for a unwritten prealloc extent */
  127. insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
  128. sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
  129. slot++;
  130. offset += sectorsize;
  131. /*
  132. * We want to jack up disk_bytenr a little more so the em stuff doesn't
  133. * merge our records.
  134. */
  135. disk_bytenr += 2 * sectorsize;
  136. /*
  137. * Now for a partially written prealloc extent, basically the same as
  138. * the hole punch example above. Ram_bytes never changes when you mark
  139. * extents written btw.
  140. */
  141. insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
  142. 4 * sectorsize, BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
  143. slot++;
  144. offset += sectorsize;
  145. insert_extent(root, offset, sectorsize, 4 * sectorsize, sectorsize,
  146. disk_bytenr, 4 * sectorsize, BTRFS_FILE_EXTENT_REG, 0,
  147. slot);
  148. slot++;
  149. offset += sectorsize;
  150. insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
  151. 2 * sectorsize, disk_bytenr, 4 * sectorsize,
  152. BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
  153. slot++;
  154. offset += 2 * sectorsize;
  155. disk_bytenr += 4 * sectorsize;
  156. /* Now a normal compressed extent */
  157. insert_extent(root, offset, 2 * sectorsize, 2 * sectorsize, 0,
  158. disk_bytenr, sectorsize, BTRFS_FILE_EXTENT_REG,
  159. BTRFS_COMPRESS_ZLIB, slot);
  160. slot++;
  161. offset += 2 * sectorsize;
  162. /* No merges */
  163. disk_bytenr += 2 * sectorsize;
  164. /* Now a split compressed extent */
  165. insert_extent(root, offset, sectorsize, 4 * sectorsize, 0, disk_bytenr,
  166. sectorsize, BTRFS_FILE_EXTENT_REG,
  167. BTRFS_COMPRESS_ZLIB, slot);
  168. slot++;
  169. offset += sectorsize;
  170. insert_extent(root, offset, sectorsize, sectorsize, 0,
  171. disk_bytenr + sectorsize, sectorsize,
  172. BTRFS_FILE_EXTENT_REG, 0, slot);
  173. slot++;
  174. offset += sectorsize;
  175. insert_extent(root, offset, 2 * sectorsize, 4 * sectorsize,
  176. 2 * sectorsize, disk_bytenr, sectorsize,
  177. BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
  178. slot++;
  179. offset += 2 * sectorsize;
  180. disk_bytenr += 2 * sectorsize;
  181. /* Now extents that have a hole but no hole extent */
  182. insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
  183. sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
  184. slot++;
  185. offset += 4 * sectorsize;
  186. disk_bytenr += sectorsize;
  187. insert_extent(root, offset, sectorsize, sectorsize, 0, disk_bytenr,
  188. sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
  189. }
  190. static unsigned long prealloc_only = 0;
  191. static unsigned long compressed_only = 0;
  192. static unsigned long vacancy_only = 0;
  193. static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
  194. {
  195. struct btrfs_fs_info *fs_info = NULL;
  196. struct inode *inode = NULL;
  197. struct btrfs_root *root = NULL;
  198. struct extent_map *em = NULL;
  199. u64 orig_start;
  200. u64 disk_bytenr;
  201. u64 offset;
  202. int ret = -ENOMEM;
  203. inode = btrfs_new_test_inode();
  204. if (!inode) {
  205. test_err("couldn't allocate inode");
  206. return ret;
  207. }
  208. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  209. BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
  210. BTRFS_I(inode)->location.offset = 0;
  211. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  212. if (!fs_info) {
  213. test_err("couldn't allocate dummy fs info");
  214. goto out;
  215. }
  216. root = btrfs_alloc_dummy_root(fs_info);
  217. if (IS_ERR(root)) {
  218. test_err("couldn't allocate root");
  219. goto out;
  220. }
  221. root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
  222. if (!root->node) {
  223. test_err("couldn't allocate dummy buffer");
  224. goto out;
  225. }
  226. /*
  227. * We will just free a dummy node if it's ref count is 2 so we need an
  228. * extra ref so our searches don't accidentally release our page.
  229. */
  230. extent_buffer_get(root->node);
  231. btrfs_set_header_nritems(root->node, 0);
  232. btrfs_set_header_level(root->node, 0);
  233. ret = -EINVAL;
  234. /* First with no extents */
  235. BTRFS_I(inode)->root = root;
  236. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, sectorsize, 0);
  237. if (IS_ERR(em)) {
  238. em = NULL;
  239. test_err("got an error when we shouldn't have");
  240. goto out;
  241. }
  242. if (em->block_start != EXTENT_MAP_HOLE) {
  243. test_err("expected a hole, got %llu", em->block_start);
  244. goto out;
  245. }
  246. free_extent_map(em);
  247. btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
  248. /*
  249. * All of the magic numbers are based on the mapping setup in
  250. * setup_file_extents, so if you change anything there you need to
  251. * update the comment and update the expected values below.
  252. */
  253. setup_file_extents(root, sectorsize);
  254. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, (u64)-1, 0);
  255. if (IS_ERR(em)) {
  256. test_err("got an error when we shouldn't have");
  257. goto out;
  258. }
  259. if (em->block_start != EXTENT_MAP_HOLE) {
  260. test_err("expected a hole, got %llu", em->block_start);
  261. goto out;
  262. }
  263. if (em->start != 0 || em->len != 5) {
  264. test_err(
  265. "unexpected extent wanted start 0 len 5, got start %llu len %llu",
  266. em->start, em->len);
  267. goto out;
  268. }
  269. if (em->flags != 0) {
  270. test_err("unexpected flags set, want 0 have %lu", em->flags);
  271. goto out;
  272. }
  273. offset = em->start + em->len;
  274. free_extent_map(em);
  275. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  276. if (IS_ERR(em)) {
  277. test_err("got an error when we shouldn't have");
  278. goto out;
  279. }
  280. if (em->block_start != EXTENT_MAP_INLINE) {
  281. test_err("expected an inline, got %llu", em->block_start);
  282. goto out;
  283. }
  284. if (em->start != offset || em->len != (sectorsize - 5)) {
  285. test_err(
  286. "unexpected extent wanted start %llu len 1, got start %llu len %llu",
  287. offset, em->start, em->len);
  288. goto out;
  289. }
  290. if (em->flags != 0) {
  291. test_err("unexpected flags set, want 0 have %lu", em->flags);
  292. goto out;
  293. }
  294. /*
  295. * We don't test anything else for inline since it doesn't get set
  296. * unless we have a page for it to write into. Maybe we should change
  297. * this?
  298. */
  299. offset = em->start + em->len;
  300. free_extent_map(em);
  301. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  302. if (IS_ERR(em)) {
  303. test_err("got an error when we shouldn't have");
  304. goto out;
  305. }
  306. if (em->block_start != EXTENT_MAP_HOLE) {
  307. test_err("expected a hole, got %llu", em->block_start);
  308. goto out;
  309. }
  310. if (em->start != offset || em->len != 4) {
  311. test_err(
  312. "unexpected extent wanted start %llu len 4, got start %llu len %llu",
  313. offset, em->start, em->len);
  314. goto out;
  315. }
  316. if (em->flags != 0) {
  317. test_err("unexpected flags set, want 0 have %lu", em->flags);
  318. goto out;
  319. }
  320. offset = em->start + em->len;
  321. free_extent_map(em);
  322. /* Regular extent */
  323. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  324. if (IS_ERR(em)) {
  325. test_err("got an error when we shouldn't have");
  326. goto out;
  327. }
  328. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  329. test_err("expected a real extent, got %llu", em->block_start);
  330. goto out;
  331. }
  332. if (em->start != offset || em->len != sectorsize - 1) {
  333. test_err(
  334. "unexpected extent wanted start %llu len 4095, got start %llu len %llu",
  335. offset, em->start, em->len);
  336. goto out;
  337. }
  338. if (em->flags != 0) {
  339. test_err("unexpected flags set, want 0 have %lu", em->flags);
  340. goto out;
  341. }
  342. if (em->orig_start != em->start) {
  343. test_err("wrong orig offset, want %llu, have %llu", em->start,
  344. em->orig_start);
  345. goto out;
  346. }
  347. offset = em->start + em->len;
  348. free_extent_map(em);
  349. /* The next 3 are split extents */
  350. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  351. if (IS_ERR(em)) {
  352. test_err("got an error when we shouldn't have");
  353. goto out;
  354. }
  355. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  356. test_err("expected a real extent, got %llu", em->block_start);
  357. goto out;
  358. }
  359. if (em->start != offset || em->len != sectorsize) {
  360. test_err(
  361. "unexpected extent start %llu len %u, got start %llu len %llu",
  362. offset, sectorsize, em->start, em->len);
  363. goto out;
  364. }
  365. if (em->flags != 0) {
  366. test_err("unexpected flags set, want 0 have %lu", em->flags);
  367. goto out;
  368. }
  369. if (em->orig_start != em->start) {
  370. test_err("wrong orig offset, want %llu, have %llu", em->start,
  371. em->orig_start);
  372. goto out;
  373. }
  374. disk_bytenr = em->block_start;
  375. orig_start = em->start;
  376. offset = em->start + em->len;
  377. free_extent_map(em);
  378. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  379. if (IS_ERR(em)) {
  380. test_err("got an error when we shouldn't have");
  381. goto out;
  382. }
  383. if (em->block_start != EXTENT_MAP_HOLE) {
  384. test_err("expected a hole, got %llu", em->block_start);
  385. goto out;
  386. }
  387. if (em->start != offset || em->len != sectorsize) {
  388. test_err(
  389. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  390. offset, sectorsize, em->start, em->len);
  391. goto out;
  392. }
  393. if (em->flags != 0) {
  394. test_err("unexpected flags set, want 0 have %lu", em->flags);
  395. goto out;
  396. }
  397. offset = em->start + em->len;
  398. free_extent_map(em);
  399. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  400. if (IS_ERR(em)) {
  401. test_err("got an error when we shouldn't have");
  402. goto out;
  403. }
  404. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  405. test_err("expected a real extent, got %llu", em->block_start);
  406. goto out;
  407. }
  408. if (em->start != offset || em->len != 2 * sectorsize) {
  409. test_err(
  410. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  411. offset, 2 * sectorsize, em->start, em->len);
  412. goto out;
  413. }
  414. if (em->flags != 0) {
  415. test_err("unexpected flags set, want 0 have %lu", em->flags);
  416. goto out;
  417. }
  418. if (em->orig_start != orig_start) {
  419. test_err("wrong orig offset, want %llu, have %llu",
  420. orig_start, em->orig_start);
  421. goto out;
  422. }
  423. disk_bytenr += (em->start - orig_start);
  424. if (em->block_start != disk_bytenr) {
  425. test_err("wrong block start, want %llu, have %llu",
  426. disk_bytenr, em->block_start);
  427. goto out;
  428. }
  429. offset = em->start + em->len;
  430. free_extent_map(em);
  431. /* Prealloc extent */
  432. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  433. if (IS_ERR(em)) {
  434. test_err("got an error when we shouldn't have");
  435. goto out;
  436. }
  437. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  438. test_err("expected a real extent, got %llu", em->block_start);
  439. goto out;
  440. }
  441. if (em->start != offset || em->len != sectorsize) {
  442. test_err(
  443. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  444. offset, sectorsize, em->start, em->len);
  445. goto out;
  446. }
  447. if (em->flags != prealloc_only) {
  448. test_err("unexpected flags set, want %lu have %lu",
  449. prealloc_only, em->flags);
  450. goto out;
  451. }
  452. if (em->orig_start != em->start) {
  453. test_err("wrong orig offset, want %llu, have %llu", em->start,
  454. em->orig_start);
  455. goto out;
  456. }
  457. offset = em->start + em->len;
  458. free_extent_map(em);
  459. /* The next 3 are a half written prealloc extent */
  460. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  461. if (IS_ERR(em)) {
  462. test_err("got an error when we shouldn't have");
  463. goto out;
  464. }
  465. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  466. test_err("expected a real extent, got %llu", em->block_start);
  467. goto out;
  468. }
  469. if (em->start != offset || em->len != sectorsize) {
  470. test_err(
  471. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  472. offset, sectorsize, em->start, em->len);
  473. goto out;
  474. }
  475. if (em->flags != prealloc_only) {
  476. test_err("unexpected flags set, want %lu have %lu",
  477. prealloc_only, em->flags);
  478. goto out;
  479. }
  480. if (em->orig_start != em->start) {
  481. test_err("wrong orig offset, want %llu, have %llu", em->start,
  482. em->orig_start);
  483. goto out;
  484. }
  485. disk_bytenr = em->block_start;
  486. orig_start = em->start;
  487. offset = em->start + em->len;
  488. free_extent_map(em);
  489. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  490. if (IS_ERR(em)) {
  491. test_err("got an error when we shouldn't have");
  492. goto out;
  493. }
  494. if (em->block_start >= EXTENT_MAP_HOLE) {
  495. test_err("expected a real extent, got %llu", em->block_start);
  496. goto out;
  497. }
  498. if (em->start != offset || em->len != sectorsize) {
  499. test_err(
  500. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  501. offset, sectorsize, em->start, em->len);
  502. goto out;
  503. }
  504. if (em->flags != 0) {
  505. test_err("unexpected flags set, want 0 have %lu", em->flags);
  506. goto out;
  507. }
  508. if (em->orig_start != orig_start) {
  509. test_err("unexpected orig offset, wanted %llu, have %llu",
  510. orig_start, em->orig_start);
  511. goto out;
  512. }
  513. if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
  514. test_err("unexpected block start, wanted %llu, have %llu",
  515. disk_bytenr + (em->start - em->orig_start),
  516. em->block_start);
  517. goto out;
  518. }
  519. offset = em->start + em->len;
  520. free_extent_map(em);
  521. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  522. if (IS_ERR(em)) {
  523. test_err("got an error when we shouldn't have");
  524. goto out;
  525. }
  526. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  527. test_err("expected a real extent, got %llu", em->block_start);
  528. goto out;
  529. }
  530. if (em->start != offset || em->len != 2 * sectorsize) {
  531. test_err(
  532. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  533. offset, 2 * sectorsize, em->start, em->len);
  534. goto out;
  535. }
  536. if (em->flags != prealloc_only) {
  537. test_err("unexpected flags set, want %lu have %lu",
  538. prealloc_only, em->flags);
  539. goto out;
  540. }
  541. if (em->orig_start != orig_start) {
  542. test_err("wrong orig offset, want %llu, have %llu", orig_start,
  543. em->orig_start);
  544. goto out;
  545. }
  546. if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
  547. test_err("unexpected block start, wanted %llu, have %llu",
  548. disk_bytenr + (em->start - em->orig_start),
  549. em->block_start);
  550. goto out;
  551. }
  552. offset = em->start + em->len;
  553. free_extent_map(em);
  554. /* Now for the compressed extent */
  555. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  556. if (IS_ERR(em)) {
  557. test_err("got an error when we shouldn't have");
  558. goto out;
  559. }
  560. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  561. test_err("expected a real extent, got %llu", em->block_start);
  562. goto out;
  563. }
  564. if (em->start != offset || em->len != 2 * sectorsize) {
  565. test_err(
  566. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  567. offset, 2 * sectorsize, em->start, em->len);
  568. goto out;
  569. }
  570. if (em->flags != compressed_only) {
  571. test_err("unexpected flags set, want %lu have %lu",
  572. compressed_only, em->flags);
  573. goto out;
  574. }
  575. if (em->orig_start != em->start) {
  576. test_err("wrong orig offset, want %llu, have %llu",
  577. em->start, em->orig_start);
  578. goto out;
  579. }
  580. if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
  581. test_err("unexpected compress type, wanted %d, got %d",
  582. BTRFS_COMPRESS_ZLIB, em->compress_type);
  583. goto out;
  584. }
  585. offset = em->start + em->len;
  586. free_extent_map(em);
  587. /* Split compressed extent */
  588. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  589. if (IS_ERR(em)) {
  590. test_err("got an error when we shouldn't have");
  591. goto out;
  592. }
  593. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  594. test_err("expected a real extent, got %llu", em->block_start);
  595. goto out;
  596. }
  597. if (em->start != offset || em->len != sectorsize) {
  598. test_err(
  599. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  600. offset, sectorsize, em->start, em->len);
  601. goto out;
  602. }
  603. if (em->flags != compressed_only) {
  604. test_err("unexpected flags set, want %lu have %lu",
  605. compressed_only, em->flags);
  606. goto out;
  607. }
  608. if (em->orig_start != em->start) {
  609. test_err("wrong orig offset, want %llu, have %llu",
  610. em->start, em->orig_start);
  611. goto out;
  612. }
  613. if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
  614. test_err("unexpected compress type, wanted %d, got %d",
  615. BTRFS_COMPRESS_ZLIB, em->compress_type);
  616. goto out;
  617. }
  618. disk_bytenr = em->block_start;
  619. orig_start = em->start;
  620. offset = em->start + em->len;
  621. free_extent_map(em);
  622. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  623. if (IS_ERR(em)) {
  624. test_err("got an error when we shouldn't have");
  625. goto out;
  626. }
  627. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  628. test_err("expected a real extent, got %llu", em->block_start);
  629. goto out;
  630. }
  631. if (em->start != offset || em->len != sectorsize) {
  632. test_err(
  633. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  634. offset, sectorsize, em->start, em->len);
  635. goto out;
  636. }
  637. if (em->flags != 0) {
  638. test_err("unexpected flags set, want 0 have %lu", em->flags);
  639. goto out;
  640. }
  641. if (em->orig_start != em->start) {
  642. test_err("wrong orig offset, want %llu, have %llu", em->start,
  643. em->orig_start);
  644. goto out;
  645. }
  646. offset = em->start + em->len;
  647. free_extent_map(em);
  648. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  649. if (IS_ERR(em)) {
  650. test_err("got an error when we shouldn't have");
  651. goto out;
  652. }
  653. if (em->block_start != disk_bytenr) {
  654. test_err("block start does not match, want %llu got %llu",
  655. disk_bytenr, em->block_start);
  656. goto out;
  657. }
  658. if (em->start != offset || em->len != 2 * sectorsize) {
  659. test_err(
  660. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  661. offset, 2 * sectorsize, em->start, em->len);
  662. goto out;
  663. }
  664. if (em->flags != compressed_only) {
  665. test_err("unexpected flags set, want %lu have %lu",
  666. compressed_only, em->flags);
  667. goto out;
  668. }
  669. if (em->orig_start != orig_start) {
  670. test_err("wrong orig offset, want %llu, have %llu",
  671. em->start, orig_start);
  672. goto out;
  673. }
  674. if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
  675. test_err("unexpected compress type, wanted %d, got %d",
  676. BTRFS_COMPRESS_ZLIB, em->compress_type);
  677. goto out;
  678. }
  679. offset = em->start + em->len;
  680. free_extent_map(em);
  681. /* A hole between regular extents but no hole extent */
  682. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset + 6,
  683. sectorsize, 0);
  684. if (IS_ERR(em)) {
  685. test_err("got an error when we shouldn't have");
  686. goto out;
  687. }
  688. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  689. test_err("expected a real extent, got %llu", em->block_start);
  690. goto out;
  691. }
  692. if (em->start != offset || em->len != sectorsize) {
  693. test_err(
  694. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  695. offset, sectorsize, em->start, em->len);
  696. goto out;
  697. }
  698. if (em->flags != 0) {
  699. test_err("unexpected flags set, want 0 have %lu", em->flags);
  700. goto out;
  701. }
  702. if (em->orig_start != em->start) {
  703. test_err("wrong orig offset, want %llu, have %llu", em->start,
  704. em->orig_start);
  705. goto out;
  706. }
  707. offset = em->start + em->len;
  708. free_extent_map(em);
  709. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, SZ_4M, 0);
  710. if (IS_ERR(em)) {
  711. test_err("got an error when we shouldn't have");
  712. goto out;
  713. }
  714. if (em->block_start != EXTENT_MAP_HOLE) {
  715. test_err("expected a hole extent, got %llu", em->block_start);
  716. goto out;
  717. }
  718. /*
  719. * Currently we just return a length that we requested rather than the
  720. * length of the actual hole, if this changes we'll have to change this
  721. * test.
  722. */
  723. if (em->start != offset || em->len != 3 * sectorsize) {
  724. test_err(
  725. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  726. offset, 3 * sectorsize, em->start, em->len);
  727. goto out;
  728. }
  729. if (em->flags != vacancy_only) {
  730. test_err("unexpected flags set, want %lu have %lu",
  731. vacancy_only, em->flags);
  732. goto out;
  733. }
  734. if (em->orig_start != em->start) {
  735. test_err("wrong orig offset, want %llu, have %llu", em->start,
  736. em->orig_start);
  737. goto out;
  738. }
  739. offset = em->start + em->len;
  740. free_extent_map(em);
  741. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  742. if (IS_ERR(em)) {
  743. test_err("got an error when we shouldn't have");
  744. goto out;
  745. }
  746. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  747. test_err("expected a real extent, got %llu", em->block_start);
  748. goto out;
  749. }
  750. if (em->start != offset || em->len != sectorsize) {
  751. test_err(
  752. "unexpected extent wanted start %llu len %u, got start %llu len %llu",
  753. offset, sectorsize, em->start, em->len);
  754. goto out;
  755. }
  756. if (em->flags != 0) {
  757. test_err("unexpected flags set, want 0 have %lu", em->flags);
  758. goto out;
  759. }
  760. if (em->orig_start != em->start) {
  761. test_err("wrong orig offset, want %llu, have %llu", em->start,
  762. em->orig_start);
  763. goto out;
  764. }
  765. ret = 0;
  766. out:
  767. if (!IS_ERR(em))
  768. free_extent_map(em);
  769. iput(inode);
  770. btrfs_free_dummy_root(root);
  771. btrfs_free_dummy_fs_info(fs_info);
  772. return ret;
  773. }
  774. static int test_hole_first(u32 sectorsize, u32 nodesize)
  775. {
  776. struct btrfs_fs_info *fs_info = NULL;
  777. struct inode *inode = NULL;
  778. struct btrfs_root *root = NULL;
  779. struct extent_map *em = NULL;
  780. int ret = -ENOMEM;
  781. inode = btrfs_new_test_inode();
  782. if (!inode) {
  783. test_err("couldn't allocate inode");
  784. return ret;
  785. }
  786. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  787. BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
  788. BTRFS_I(inode)->location.offset = 0;
  789. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  790. if (!fs_info) {
  791. test_err("couldn't allocate dummy fs info");
  792. goto out;
  793. }
  794. root = btrfs_alloc_dummy_root(fs_info);
  795. if (IS_ERR(root)) {
  796. test_err("couldn't allocate root");
  797. goto out;
  798. }
  799. root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
  800. if (!root->node) {
  801. test_err("couldn't allocate dummy buffer");
  802. goto out;
  803. }
  804. extent_buffer_get(root->node);
  805. btrfs_set_header_nritems(root->node, 0);
  806. btrfs_set_header_level(root->node, 0);
  807. BTRFS_I(inode)->root = root;
  808. ret = -EINVAL;
  809. /*
  810. * Need a blank inode item here just so we don't confuse
  811. * btrfs_get_extent.
  812. */
  813. insert_inode_item_key(root);
  814. insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
  815. sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
  816. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0);
  817. if (IS_ERR(em)) {
  818. test_err("got an error when we shouldn't have");
  819. goto out;
  820. }
  821. if (em->block_start != EXTENT_MAP_HOLE) {
  822. test_err("expected a hole, got %llu", em->block_start);
  823. goto out;
  824. }
  825. if (em->start != 0 || em->len != sectorsize) {
  826. test_err(
  827. "unexpected extent wanted start 0 len %u, got start %llu len %llu",
  828. sectorsize, em->start, em->len);
  829. goto out;
  830. }
  831. if (em->flags != vacancy_only) {
  832. test_err("wrong flags, wanted %lu, have %lu", vacancy_only,
  833. em->flags);
  834. goto out;
  835. }
  836. free_extent_map(em);
  837. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize,
  838. 2 * sectorsize, 0);
  839. if (IS_ERR(em)) {
  840. test_err("got an error when we shouldn't have");
  841. goto out;
  842. }
  843. if (em->block_start != sectorsize) {
  844. test_err("expected a real extent, got %llu", em->block_start);
  845. goto out;
  846. }
  847. if (em->start != sectorsize || em->len != sectorsize) {
  848. test_err(
  849. "unexpected extent wanted start %u len %u, got start %llu len %llu",
  850. sectorsize, sectorsize, em->start, em->len);
  851. goto out;
  852. }
  853. if (em->flags != 0) {
  854. test_err("unexpected flags set, wanted 0 got %lu",
  855. em->flags);
  856. goto out;
  857. }
  858. ret = 0;
  859. out:
  860. if (!IS_ERR(em))
  861. free_extent_map(em);
  862. iput(inode);
  863. btrfs_free_dummy_root(root);
  864. btrfs_free_dummy_fs_info(fs_info);
  865. return ret;
  866. }
  867. static int test_extent_accounting(u32 sectorsize, u32 nodesize)
  868. {
  869. struct btrfs_fs_info *fs_info = NULL;
  870. struct inode *inode = NULL;
  871. struct btrfs_root *root = NULL;
  872. int ret = -ENOMEM;
  873. inode = btrfs_new_test_inode();
  874. if (!inode) {
  875. test_err("couldn't allocate inode");
  876. return ret;
  877. }
  878. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  879. if (!fs_info) {
  880. test_err("couldn't allocate dummy fs info");
  881. goto out;
  882. }
  883. root = btrfs_alloc_dummy_root(fs_info);
  884. if (IS_ERR(root)) {
  885. test_err("couldn't allocate root");
  886. goto out;
  887. }
  888. BTRFS_I(inode)->root = root;
  889. btrfs_test_inode_set_ops(inode);
  890. /* [BTRFS_MAX_EXTENT_SIZE] */
  891. ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, 0,
  892. NULL, 0);
  893. if (ret) {
  894. test_err("btrfs_set_extent_delalloc returned %d", ret);
  895. goto out;
  896. }
  897. if (BTRFS_I(inode)->outstanding_extents != 1) {
  898. ret = -EINVAL;
  899. test_err("miscount, wanted 1, got %u",
  900. BTRFS_I(inode)->outstanding_extents);
  901. goto out;
  902. }
  903. /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
  904. ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
  905. BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
  906. 0, NULL, 0);
  907. if (ret) {
  908. test_err("btrfs_set_extent_delalloc returned %d", ret);
  909. goto out;
  910. }
  911. if (BTRFS_I(inode)->outstanding_extents != 2) {
  912. ret = -EINVAL;
  913. test_err("miscount, wanted 2, got %u",
  914. BTRFS_I(inode)->outstanding_extents);
  915. goto out;
  916. }
  917. /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
  918. ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
  919. BTRFS_MAX_EXTENT_SIZE >> 1,
  920. (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
  921. EXTENT_DELALLOC | EXTENT_DIRTY |
  922. EXTENT_UPTODATE, 0, 0, NULL);
  923. if (ret) {
  924. test_err("clear_extent_bit returned %d", ret);
  925. goto out;
  926. }
  927. if (BTRFS_I(inode)->outstanding_extents != 2) {
  928. ret = -EINVAL;
  929. test_err("miscount, wanted 2, got %u",
  930. BTRFS_I(inode)->outstanding_extents);
  931. goto out;
  932. }
  933. /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
  934. ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
  935. (BTRFS_MAX_EXTENT_SIZE >> 1)
  936. + sectorsize - 1,
  937. 0, NULL, 0);
  938. if (ret) {
  939. test_err("btrfs_set_extent_delalloc returned %d", ret);
  940. goto out;
  941. }
  942. if (BTRFS_I(inode)->outstanding_extents != 2) {
  943. ret = -EINVAL;
  944. test_err("miscount, wanted 2, got %u",
  945. BTRFS_I(inode)->outstanding_extents);
  946. goto out;
  947. }
  948. /*
  949. * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
  950. */
  951. ret = btrfs_set_extent_delalloc(inode,
  952. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
  953. (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
  954. 0, NULL, 0);
  955. if (ret) {
  956. test_err("btrfs_set_extent_delalloc returned %d", ret);
  957. goto out;
  958. }
  959. if (BTRFS_I(inode)->outstanding_extents != 4) {
  960. ret = -EINVAL;
  961. test_err("miscount, wanted 4, got %u",
  962. BTRFS_I(inode)->outstanding_extents);
  963. goto out;
  964. }
  965. /*
  966. * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
  967. */
  968. ret = btrfs_set_extent_delalloc(inode,
  969. BTRFS_MAX_EXTENT_SIZE + sectorsize,
  970. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
  971. if (ret) {
  972. test_err("btrfs_set_extent_delalloc returned %d", ret);
  973. goto out;
  974. }
  975. if (BTRFS_I(inode)->outstanding_extents != 3) {
  976. ret = -EINVAL;
  977. test_err("miscount, wanted 3, got %u",
  978. BTRFS_I(inode)->outstanding_extents);
  979. goto out;
  980. }
  981. /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
  982. ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
  983. BTRFS_MAX_EXTENT_SIZE + sectorsize,
  984. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
  985. EXTENT_DIRTY | EXTENT_DELALLOC |
  986. EXTENT_UPTODATE, 0, 0, NULL);
  987. if (ret) {
  988. test_err("clear_extent_bit returned %d", ret);
  989. goto out;
  990. }
  991. if (BTRFS_I(inode)->outstanding_extents != 4) {
  992. ret = -EINVAL;
  993. test_err("miscount, wanted 4, got %u",
  994. BTRFS_I(inode)->outstanding_extents);
  995. goto out;
  996. }
  997. /*
  998. * Refill the hole again just for good measure, because I thought it
  999. * might fail and I'd rather satisfy my paranoia at this point.
  1000. */
  1001. ret = btrfs_set_extent_delalloc(inode,
  1002. BTRFS_MAX_EXTENT_SIZE + sectorsize,
  1003. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, 0, NULL, 0);
  1004. if (ret) {
  1005. test_err("btrfs_set_extent_delalloc returned %d", ret);
  1006. goto out;
  1007. }
  1008. if (BTRFS_I(inode)->outstanding_extents != 3) {
  1009. ret = -EINVAL;
  1010. test_err("miscount, wanted 3, got %u",
  1011. BTRFS_I(inode)->outstanding_extents);
  1012. goto out;
  1013. }
  1014. /* Empty */
  1015. ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  1016. EXTENT_DIRTY | EXTENT_DELALLOC |
  1017. EXTENT_UPTODATE, 0, 0, NULL);
  1018. if (ret) {
  1019. test_err("clear_extent_bit returned %d", ret);
  1020. goto out;
  1021. }
  1022. if (BTRFS_I(inode)->outstanding_extents) {
  1023. ret = -EINVAL;
  1024. test_err("miscount, wanted 0, got %u",
  1025. BTRFS_I(inode)->outstanding_extents);
  1026. goto out;
  1027. }
  1028. ret = 0;
  1029. out:
  1030. if (ret)
  1031. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  1032. EXTENT_DIRTY | EXTENT_DELALLOC |
  1033. EXTENT_UPTODATE, 0, 0, NULL);
  1034. iput(inode);
  1035. btrfs_free_dummy_root(root);
  1036. btrfs_free_dummy_fs_info(fs_info);
  1037. return ret;
  1038. }
  1039. int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
  1040. {
  1041. int ret;
  1042. set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
  1043. set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
  1044. test_msg("running btrfs_get_extent tests");
  1045. ret = test_btrfs_get_extent(sectorsize, nodesize);
  1046. if (ret)
  1047. return ret;
  1048. test_msg("running hole first btrfs_get_extent test");
  1049. ret = test_hole_first(sectorsize, nodesize);
  1050. if (ret)
  1051. return ret;
  1052. test_msg("running outstanding_extents tests");
  1053. return test_extent_accounting(sectorsize, nodesize);
  1054. }