inode-tests.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * Copyright (C) 2013 Fusion IO. 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 "btrfs-tests.h"
  19. #include "../ctree.h"
  20. #include "../btrfs_inode.h"
  21. #include "../disk-io.h"
  22. #include "../extent_io.h"
  23. #include "../volumes.h"
  24. static struct btrfs_fs_info *alloc_dummy_fs_info(void)
  25. {
  26. struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
  27. GFP_NOFS);
  28. if (!fs_info)
  29. return fs_info;
  30. fs_info->fs_devices = kzalloc(sizeof(struct btrfs_fs_devices),
  31. GFP_NOFS);
  32. if (!fs_info->fs_devices) {
  33. kfree(fs_info);
  34. return NULL;
  35. }
  36. return fs_info;
  37. }
  38. static void free_dummy_root(struct btrfs_root *root)
  39. {
  40. if (!root)
  41. return;
  42. if (root->fs_info) {
  43. kfree(root->fs_info->fs_devices);
  44. kfree(root->fs_info);
  45. }
  46. if (root->node)
  47. free_extent_buffer(root->node);
  48. kfree(root);
  49. }
  50. static void insert_extent(struct btrfs_root *root, u64 start, u64 len,
  51. u64 ram_bytes, u64 offset, u64 disk_bytenr,
  52. u64 disk_len, u32 type, u8 compression, int slot)
  53. {
  54. struct btrfs_path path;
  55. struct btrfs_file_extent_item *fi;
  56. struct extent_buffer *leaf = root->node;
  57. struct btrfs_key key;
  58. u32 value_len = sizeof(struct btrfs_file_extent_item);
  59. if (type == BTRFS_FILE_EXTENT_INLINE)
  60. value_len += len;
  61. memset(&path, 0, sizeof(path));
  62. path.nodes[0] = leaf;
  63. path.slots[0] = slot;
  64. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  65. key.type = BTRFS_EXTENT_DATA_KEY;
  66. key.offset = start;
  67. setup_items_for_insert(root, &path, &key, &value_len, value_len,
  68. value_len + sizeof(struct btrfs_item), 1);
  69. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  70. btrfs_set_file_extent_generation(leaf, fi, 1);
  71. btrfs_set_file_extent_type(leaf, fi, type);
  72. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  73. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_len);
  74. btrfs_set_file_extent_offset(leaf, fi, offset);
  75. btrfs_set_file_extent_num_bytes(leaf, fi, len);
  76. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  77. btrfs_set_file_extent_compression(leaf, fi, compression);
  78. btrfs_set_file_extent_encryption(leaf, fi, 0);
  79. btrfs_set_file_extent_other_encoding(leaf, fi, 0);
  80. }
  81. /*
  82. * Build the most complicated map of extents the earth has ever seen. We want
  83. * this so we can test all of the corner cases of btrfs_get_extent. Here is a
  84. * diagram of how the extents will look though this may not be possible we still
  85. * want to make sure everything acts normally (the last number is not inclusive)
  86. *
  87. * [0 - 5][5 - 6][6 - 10][10 - 4096][ 4096 - 8192 ][8192 - 12288]
  88. * [hole ][inline][ hole ][ regular ][regular1 split][ hole ]
  89. *
  90. * [ 12288 - 20480][20480 - 24576][ 24576 - 28672 ][28672 - 36864][36864 - 45056]
  91. * [regular1 split][ prealloc1 ][prealloc1 written][ prealloc1 ][ compressed ]
  92. *
  93. * [45056 - 49152][49152-53248][53248-61440][61440-65536][ 65536+81920 ]
  94. * [ compressed1 ][ regular ][compressed1][ regular ][ hole but no extent]
  95. *
  96. * [81920-86016]
  97. * [ regular ]
  98. */
  99. static void setup_file_extents(struct btrfs_root *root)
  100. {
  101. int slot = 0;
  102. u64 disk_bytenr = 1 * 1024 * 1024;
  103. u64 offset = 0;
  104. /* First we want a hole */
  105. insert_extent(root, offset, 5, 5, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
  106. slot);
  107. slot++;
  108. offset += 5;
  109. /*
  110. * Now we want an inline extent, I don't think this is possible but hey
  111. * why not? Also keep in mind if we have an inline extent it counts as
  112. * the whole first page. If we were to expand it we would have to cow
  113. * and we wouldn't have an inline extent anymore.
  114. */
  115. insert_extent(root, offset, 1, 1, 0, 0, 0, BTRFS_FILE_EXTENT_INLINE, 0,
  116. slot);
  117. slot++;
  118. offset = 4096;
  119. /* Now another hole */
  120. insert_extent(root, offset, 4, 4, 0, 0, 0, BTRFS_FILE_EXTENT_REG, 0,
  121. slot);
  122. slot++;
  123. offset += 4;
  124. /* Now for a regular extent */
  125. insert_extent(root, offset, 4095, 4095, 0, disk_bytenr, 4096,
  126. BTRFS_FILE_EXTENT_REG, 0, slot);
  127. slot++;
  128. disk_bytenr += 4096;
  129. offset += 4095;
  130. /*
  131. * Now for 3 extents that were split from a hole punch so we test
  132. * offsets properly.
  133. */
  134. insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
  135. BTRFS_FILE_EXTENT_REG, 0, slot);
  136. slot++;
  137. offset += 4096;
  138. insert_extent(root, offset, 4096, 4096, 0, 0, 0, BTRFS_FILE_EXTENT_REG,
  139. 0, slot);
  140. slot++;
  141. offset += 4096;
  142. insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
  143. BTRFS_FILE_EXTENT_REG, 0, slot);
  144. slot++;
  145. offset += 8192;
  146. disk_bytenr += 16384;
  147. /* Now for a unwritten prealloc extent */
  148. insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
  149. BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
  150. slot++;
  151. offset += 4096;
  152. /*
  153. * We want to jack up disk_bytenr a little more so the em stuff doesn't
  154. * merge our records.
  155. */
  156. disk_bytenr += 8192;
  157. /*
  158. * Now for a partially written prealloc extent, basically the same as
  159. * the hole punch example above. Ram_bytes never changes when you mark
  160. * extents written btw.
  161. */
  162. insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 16384,
  163. BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
  164. slot++;
  165. offset += 4096;
  166. insert_extent(root, offset, 4096, 16384, 4096, disk_bytenr, 16384,
  167. BTRFS_FILE_EXTENT_REG, 0, slot);
  168. slot++;
  169. offset += 4096;
  170. insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 16384,
  171. BTRFS_FILE_EXTENT_PREALLOC, 0, slot);
  172. slot++;
  173. offset += 8192;
  174. disk_bytenr += 16384;
  175. /* Now a normal compressed extent */
  176. insert_extent(root, offset, 8192, 8192, 0, disk_bytenr, 4096,
  177. BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
  178. slot++;
  179. offset += 8192;
  180. /* No merges */
  181. disk_bytenr += 8192;
  182. /* Now a split compressed extent */
  183. insert_extent(root, offset, 4096, 16384, 0, disk_bytenr, 4096,
  184. BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
  185. slot++;
  186. offset += 4096;
  187. insert_extent(root, offset, 4096, 4096, 0, disk_bytenr + 4096, 4096,
  188. BTRFS_FILE_EXTENT_REG, 0, slot);
  189. slot++;
  190. offset += 4096;
  191. insert_extent(root, offset, 8192, 16384, 8192, disk_bytenr, 4096,
  192. BTRFS_FILE_EXTENT_REG, BTRFS_COMPRESS_ZLIB, slot);
  193. slot++;
  194. offset += 8192;
  195. disk_bytenr += 8192;
  196. /* Now extents that have a hole but no hole extent */
  197. insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
  198. BTRFS_FILE_EXTENT_REG, 0, slot);
  199. slot++;
  200. offset += 16384;
  201. disk_bytenr += 4096;
  202. insert_extent(root, offset, 4096, 4096, 0, disk_bytenr, 4096,
  203. BTRFS_FILE_EXTENT_REG, 0, slot);
  204. }
  205. static unsigned long prealloc_only = 0;
  206. static unsigned long compressed_only = 0;
  207. static unsigned long vacancy_only = 0;
  208. static noinline int test_btrfs_get_extent(void)
  209. {
  210. struct inode *inode = NULL;
  211. struct btrfs_root *root = NULL;
  212. struct extent_map *em = NULL;
  213. u64 orig_start;
  214. u64 disk_bytenr;
  215. u64 offset;
  216. int ret = -ENOMEM;
  217. set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
  218. set_bit(EXTENT_FLAG_VACANCY, &vacancy_only);
  219. set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
  220. inode = btrfs_new_test_inode();
  221. if (!inode) {
  222. test_msg("Couldn't allocate inode\n");
  223. return ret;
  224. }
  225. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  226. BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
  227. BTRFS_I(inode)->location.offset = 0;
  228. root = btrfs_alloc_dummy_root();
  229. if (IS_ERR(root)) {
  230. test_msg("Couldn't allocate root\n");
  231. goto out;
  232. }
  233. /*
  234. * We do this since btrfs_get_extent wants to assign em->bdev to
  235. * root->fs_info->fs_devices->latest_bdev.
  236. */
  237. root->fs_info = alloc_dummy_fs_info();
  238. if (!root->fs_info) {
  239. test_msg("Couldn't allocate dummy fs info\n");
  240. goto out;
  241. }
  242. root->node = alloc_dummy_extent_buffer(0, 4096);
  243. if (!root->node) {
  244. test_msg("Couldn't allocate dummy buffer\n");
  245. goto out;
  246. }
  247. /*
  248. * We will just free a dummy node if it's ref count is 2 so we need an
  249. * extra ref so our searches don't accidently release our page.
  250. */
  251. extent_buffer_get(root->node);
  252. btrfs_set_header_nritems(root->node, 0);
  253. btrfs_set_header_level(root->node, 0);
  254. ret = -EINVAL;
  255. /* First with no extents */
  256. BTRFS_I(inode)->root = root;
  257. em = btrfs_get_extent(inode, NULL, 0, 0, 4096, 0);
  258. if (IS_ERR(em)) {
  259. em = NULL;
  260. test_msg("Got an error when we shouldn't have\n");
  261. goto out;
  262. }
  263. if (em->block_start != EXTENT_MAP_HOLE) {
  264. test_msg("Expected a hole, got %llu\n", em->block_start);
  265. goto out;
  266. }
  267. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  268. test_msg("Vacancy flag wasn't set properly\n");
  269. goto out;
  270. }
  271. free_extent_map(em);
  272. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  273. /*
  274. * All of the magic numbers are based on the mapping setup in
  275. * setup_file_extents, so if you change anything there you need to
  276. * update the comment and update the expected values below.
  277. */
  278. setup_file_extents(root);
  279. em = btrfs_get_extent(inode, NULL, 0, 0, (u64)-1, 0);
  280. if (IS_ERR(em)) {
  281. test_msg("Got an error when we shouldn't have\n");
  282. goto out;
  283. }
  284. if (em->block_start != EXTENT_MAP_HOLE) {
  285. test_msg("Expected a hole, got %llu\n", em->block_start);
  286. goto out;
  287. }
  288. if (em->start != 0 || em->len != 5) {
  289. test_msg("Unexpected extent wanted start 0 len 5, got start "
  290. "%llu len %llu\n", em->start, em->len);
  291. goto out;
  292. }
  293. if (em->flags != 0) {
  294. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  295. goto out;
  296. }
  297. offset = em->start + em->len;
  298. free_extent_map(em);
  299. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  300. if (IS_ERR(em)) {
  301. test_msg("Got an error when we shouldn't have\n");
  302. goto out;
  303. }
  304. if (em->block_start != EXTENT_MAP_INLINE) {
  305. test_msg("Expected an inline, got %llu\n", em->block_start);
  306. goto out;
  307. }
  308. if (em->start != offset || em->len != 4091) {
  309. test_msg("Unexpected extent wanted start %llu len 1, got start "
  310. "%llu len %llu\n", offset, em->start, em->len);
  311. goto out;
  312. }
  313. if (em->flags != 0) {
  314. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  315. goto out;
  316. }
  317. /*
  318. * We don't test anything else for inline since it doesn't get set
  319. * unless we have a page for it to write into. Maybe we should change
  320. * this?
  321. */
  322. offset = em->start + em->len;
  323. free_extent_map(em);
  324. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  325. if (IS_ERR(em)) {
  326. test_msg("Got an error when we shouldn't have\n");
  327. goto out;
  328. }
  329. if (em->block_start != EXTENT_MAP_HOLE) {
  330. test_msg("Expected a hole, got %llu\n", em->block_start);
  331. goto out;
  332. }
  333. if (em->start != offset || em->len != 4) {
  334. test_msg("Unexpected extent wanted start %llu len 4, got start "
  335. "%llu len %llu\n", offset, em->start, em->len);
  336. goto out;
  337. }
  338. if (em->flags != 0) {
  339. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  340. goto out;
  341. }
  342. offset = em->start + em->len;
  343. free_extent_map(em);
  344. /* Regular extent */
  345. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  346. if (IS_ERR(em)) {
  347. test_msg("Got an error when we shouldn't have\n");
  348. goto out;
  349. }
  350. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  351. test_msg("Expected a real extent, got %llu\n", em->block_start);
  352. goto out;
  353. }
  354. if (em->start != offset || em->len != 4095) {
  355. test_msg("Unexpected extent wanted start %llu len 4095, got "
  356. "start %llu len %llu\n", offset, em->start, em->len);
  357. goto out;
  358. }
  359. if (em->flags != 0) {
  360. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  361. goto out;
  362. }
  363. if (em->orig_start != em->start) {
  364. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  365. em->orig_start);
  366. goto out;
  367. }
  368. offset = em->start + em->len;
  369. free_extent_map(em);
  370. /* The next 3 are split extents */
  371. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  372. if (IS_ERR(em)) {
  373. test_msg("Got an error when we shouldn't have\n");
  374. goto out;
  375. }
  376. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  377. test_msg("Expected a real extent, got %llu\n", em->block_start);
  378. goto out;
  379. }
  380. if (em->start != offset || em->len != 4096) {
  381. test_msg("Unexpected extent wanted start %llu len 4096, got "
  382. "start %llu len %llu\n", offset, em->start, em->len);
  383. goto out;
  384. }
  385. if (em->flags != 0) {
  386. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  387. goto out;
  388. }
  389. if (em->orig_start != em->start) {
  390. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  391. em->orig_start);
  392. goto out;
  393. }
  394. disk_bytenr = em->block_start;
  395. orig_start = em->start;
  396. offset = em->start + em->len;
  397. free_extent_map(em);
  398. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  399. if (IS_ERR(em)) {
  400. test_msg("Got an error when we shouldn't have\n");
  401. goto out;
  402. }
  403. if (em->block_start != EXTENT_MAP_HOLE) {
  404. test_msg("Expected a hole, got %llu\n", em->block_start);
  405. goto out;
  406. }
  407. if (em->start != offset || em->len != 4096) {
  408. test_msg("Unexpected extent wanted start %llu len 4096, got "
  409. "start %llu len %llu\n", offset, em->start, em->len);
  410. goto out;
  411. }
  412. if (em->flags != 0) {
  413. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  414. goto out;
  415. }
  416. offset = em->start + em->len;
  417. free_extent_map(em);
  418. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  419. if (IS_ERR(em)) {
  420. test_msg("Got an error when we shouldn't have\n");
  421. goto out;
  422. }
  423. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  424. test_msg("Expected a real extent, got %llu\n", em->block_start);
  425. goto out;
  426. }
  427. if (em->start != offset || em->len != 8192) {
  428. test_msg("Unexpected extent wanted start %llu len 8192, got "
  429. "start %llu len %llu\n", offset, em->start, em->len);
  430. goto out;
  431. }
  432. if (em->flags != 0) {
  433. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  434. goto out;
  435. }
  436. if (em->orig_start != orig_start) {
  437. test_msg("Wrong orig offset, want %llu, have %llu\n",
  438. orig_start, em->orig_start);
  439. goto out;
  440. }
  441. disk_bytenr += (em->start - orig_start);
  442. if (em->block_start != disk_bytenr) {
  443. test_msg("Wrong block start, want %llu, have %llu\n",
  444. disk_bytenr, em->block_start);
  445. goto out;
  446. }
  447. offset = em->start + em->len;
  448. free_extent_map(em);
  449. /* Prealloc extent */
  450. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  451. if (IS_ERR(em)) {
  452. test_msg("Got an error when we shouldn't have\n");
  453. goto out;
  454. }
  455. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  456. test_msg("Expected a real extent, got %llu\n", em->block_start);
  457. goto out;
  458. }
  459. if (em->start != offset || em->len != 4096) {
  460. test_msg("Unexpected extent wanted start %llu len 4096, got "
  461. "start %llu len %llu\n", offset, em->start, em->len);
  462. goto out;
  463. }
  464. if (em->flags != prealloc_only) {
  465. test_msg("Unexpected flags set, want %lu have %lu\n",
  466. prealloc_only, em->flags);
  467. goto out;
  468. }
  469. if (em->orig_start != em->start) {
  470. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  471. em->orig_start);
  472. goto out;
  473. }
  474. offset = em->start + em->len;
  475. free_extent_map(em);
  476. /* The next 3 are a half written prealloc extent */
  477. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  478. if (IS_ERR(em)) {
  479. test_msg("Got an error when we shouldn't have\n");
  480. goto out;
  481. }
  482. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  483. test_msg("Expected a real extent, got %llu\n", em->block_start);
  484. goto out;
  485. }
  486. if (em->start != offset || em->len != 4096) {
  487. test_msg("Unexpected extent wanted start %llu len 4096, got "
  488. "start %llu len %llu\n", offset, em->start, em->len);
  489. goto out;
  490. }
  491. if (em->flags != prealloc_only) {
  492. test_msg("Unexpected flags set, want %lu have %lu\n",
  493. prealloc_only, em->flags);
  494. goto out;
  495. }
  496. if (em->orig_start != em->start) {
  497. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  498. em->orig_start);
  499. goto out;
  500. }
  501. disk_bytenr = em->block_start;
  502. orig_start = em->start;
  503. offset = em->start + em->len;
  504. free_extent_map(em);
  505. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  506. if (IS_ERR(em)) {
  507. test_msg("Got an error when we shouldn't have\n");
  508. goto out;
  509. }
  510. if (em->block_start >= EXTENT_MAP_HOLE) {
  511. test_msg("Expected a real extent, got %llu\n", em->block_start);
  512. goto out;
  513. }
  514. if (em->start != offset || em->len != 4096) {
  515. test_msg("Unexpected extent wanted start %llu len 4096, got "
  516. "start %llu len %llu\n", offset, em->start, em->len);
  517. goto out;
  518. }
  519. if (em->flags != 0) {
  520. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  521. goto out;
  522. }
  523. if (em->orig_start != orig_start) {
  524. test_msg("Unexpected orig offset, wanted %llu, have %llu\n",
  525. orig_start, em->orig_start);
  526. goto out;
  527. }
  528. if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
  529. test_msg("Unexpected block start, wanted %llu, have %llu\n",
  530. disk_bytenr + (em->start - em->orig_start),
  531. em->block_start);
  532. goto out;
  533. }
  534. offset = em->start + em->len;
  535. free_extent_map(em);
  536. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  537. if (IS_ERR(em)) {
  538. test_msg("Got an error when we shouldn't have\n");
  539. goto out;
  540. }
  541. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  542. test_msg("Expected a real extent, got %llu\n", em->block_start);
  543. goto out;
  544. }
  545. if (em->start != offset || em->len != 8192) {
  546. test_msg("Unexpected extent wanted start %llu len 8192, got "
  547. "start %llu len %llu\n", offset, em->start, em->len);
  548. goto out;
  549. }
  550. if (em->flags != prealloc_only) {
  551. test_msg("Unexpected flags set, want %lu have %lu\n",
  552. prealloc_only, em->flags);
  553. goto out;
  554. }
  555. if (em->orig_start != orig_start) {
  556. test_msg("Wrong orig offset, want %llu, have %llu\n", orig_start,
  557. em->orig_start);
  558. goto out;
  559. }
  560. if (em->block_start != (disk_bytenr + (em->start - em->orig_start))) {
  561. test_msg("Unexpected block start, wanted %llu, have %llu\n",
  562. disk_bytenr + (em->start - em->orig_start),
  563. em->block_start);
  564. goto out;
  565. }
  566. offset = em->start + em->len;
  567. free_extent_map(em);
  568. /* Now for the compressed extent */
  569. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  570. if (IS_ERR(em)) {
  571. test_msg("Got an error when we shouldn't have\n");
  572. goto out;
  573. }
  574. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  575. test_msg("Expected a real extent, got %llu\n", em->block_start);
  576. goto out;
  577. }
  578. if (em->start != offset || em->len != 8192) {
  579. test_msg("Unexpected extent wanted start %llu len 8192, got "
  580. "start %llu len %llu\n", offset, em->start, em->len);
  581. goto out;
  582. }
  583. if (em->flags != compressed_only) {
  584. test_msg("Unexpected flags set, want %lu have %lu\n",
  585. compressed_only, em->flags);
  586. goto out;
  587. }
  588. if (em->orig_start != em->start) {
  589. test_msg("Wrong orig offset, want %llu, have %llu\n",
  590. em->start, em->orig_start);
  591. goto out;
  592. }
  593. if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
  594. test_msg("Unexpected compress type, wanted %d, got %d\n",
  595. BTRFS_COMPRESS_ZLIB, em->compress_type);
  596. goto out;
  597. }
  598. offset = em->start + em->len;
  599. free_extent_map(em);
  600. /* Split compressed extent */
  601. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  602. if (IS_ERR(em)) {
  603. test_msg("Got an error when we shouldn't have\n");
  604. goto out;
  605. }
  606. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  607. test_msg("Expected a real extent, got %llu\n", em->block_start);
  608. goto out;
  609. }
  610. if (em->start != offset || em->len != 4096) {
  611. test_msg("Unexpected extent wanted start %llu len 4096, got "
  612. "start %llu len %llu\n", offset, em->start, em->len);
  613. goto out;
  614. }
  615. if (em->flags != compressed_only) {
  616. test_msg("Unexpected flags set, want %lu have %lu\n",
  617. compressed_only, em->flags);
  618. goto out;
  619. }
  620. if (em->orig_start != em->start) {
  621. test_msg("Wrong orig offset, want %llu, have %llu\n",
  622. em->start, em->orig_start);
  623. goto out;
  624. }
  625. if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
  626. test_msg("Unexpected compress type, wanted %d, got %d\n",
  627. BTRFS_COMPRESS_ZLIB, em->compress_type);
  628. goto out;
  629. }
  630. disk_bytenr = em->block_start;
  631. orig_start = em->start;
  632. offset = em->start + em->len;
  633. free_extent_map(em);
  634. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  635. if (IS_ERR(em)) {
  636. test_msg("Got an error when we shouldn't have\n");
  637. goto out;
  638. }
  639. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  640. test_msg("Expected a real extent, got %llu\n", em->block_start);
  641. goto out;
  642. }
  643. if (em->start != offset || em->len != 4096) {
  644. test_msg("Unexpected extent wanted start %llu len 4096, got "
  645. "start %llu len %llu\n", offset, em->start, em->len);
  646. goto out;
  647. }
  648. if (em->flags != 0) {
  649. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  650. goto out;
  651. }
  652. if (em->orig_start != em->start) {
  653. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  654. em->orig_start);
  655. goto out;
  656. }
  657. offset = em->start + em->len;
  658. free_extent_map(em);
  659. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  660. if (IS_ERR(em)) {
  661. test_msg("Got an error when we shouldn't have\n");
  662. goto out;
  663. }
  664. if (em->block_start != disk_bytenr) {
  665. test_msg("Block start does not match, want %llu got %llu\n",
  666. disk_bytenr, em->block_start);
  667. goto out;
  668. }
  669. if (em->start != offset || em->len != 8192) {
  670. test_msg("Unexpected extent wanted start %llu len 8192, got "
  671. "start %llu len %llu\n", offset, em->start, em->len);
  672. goto out;
  673. }
  674. if (em->flags != compressed_only) {
  675. test_msg("Unexpected flags set, want %lu have %lu\n",
  676. compressed_only, em->flags);
  677. goto out;
  678. }
  679. if (em->orig_start != orig_start) {
  680. test_msg("Wrong orig offset, want %llu, have %llu\n",
  681. em->start, orig_start);
  682. goto out;
  683. }
  684. if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
  685. test_msg("Unexpected compress type, wanted %d, got %d\n",
  686. BTRFS_COMPRESS_ZLIB, em->compress_type);
  687. goto out;
  688. }
  689. offset = em->start + em->len;
  690. free_extent_map(em);
  691. /* A hole between regular extents but no hole extent */
  692. em = btrfs_get_extent(inode, NULL, 0, offset + 6, 4096, 0);
  693. if (IS_ERR(em)) {
  694. test_msg("Got an error when we shouldn't have\n");
  695. goto out;
  696. }
  697. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  698. test_msg("Expected a real extent, got %llu\n", em->block_start);
  699. goto out;
  700. }
  701. if (em->start != offset || em->len != 4096) {
  702. test_msg("Unexpected extent wanted start %llu len 4096, got "
  703. "start %llu len %llu\n", offset, em->start, em->len);
  704. goto out;
  705. }
  706. if (em->flags != 0) {
  707. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  708. goto out;
  709. }
  710. if (em->orig_start != em->start) {
  711. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  712. em->orig_start);
  713. goto out;
  714. }
  715. offset = em->start + em->len;
  716. free_extent_map(em);
  717. em = btrfs_get_extent(inode, NULL, 0, offset, 4096 * 1024, 0);
  718. if (IS_ERR(em)) {
  719. test_msg("Got an error when we shouldn't have\n");
  720. goto out;
  721. }
  722. if (em->block_start != EXTENT_MAP_HOLE) {
  723. test_msg("Expected a hole extent, got %llu\n", em->block_start);
  724. goto out;
  725. }
  726. /*
  727. * Currently we just return a length that we requested rather than the
  728. * length of the actual hole, if this changes we'll have to change this
  729. * test.
  730. */
  731. if (em->start != offset || em->len != 12288) {
  732. test_msg("Unexpected extent wanted start %llu len 12288, got "
  733. "start %llu len %llu\n", offset, em->start, em->len);
  734. goto out;
  735. }
  736. if (em->flags != vacancy_only) {
  737. test_msg("Unexpected flags set, want %lu have %lu\n",
  738. vacancy_only, em->flags);
  739. goto out;
  740. }
  741. if (em->orig_start != em->start) {
  742. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  743. em->orig_start);
  744. goto out;
  745. }
  746. offset = em->start + em->len;
  747. free_extent_map(em);
  748. em = btrfs_get_extent(inode, NULL, 0, offset, 4096, 0);
  749. if (IS_ERR(em)) {
  750. test_msg("Got an error when we shouldn't have\n");
  751. goto out;
  752. }
  753. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  754. test_msg("Expected a real extent, got %llu\n", em->block_start);
  755. goto out;
  756. }
  757. if (em->start != offset || em->len != 4096) {
  758. test_msg("Unexpected extent wanted start %llu len 4096, got "
  759. "start %llu len %llu\n", offset, em->start, em->len);
  760. goto out;
  761. }
  762. if (em->flags != 0) {
  763. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  764. goto out;
  765. }
  766. if (em->orig_start != em->start) {
  767. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  768. em->orig_start);
  769. goto out;
  770. }
  771. ret = 0;
  772. out:
  773. if (!IS_ERR(em))
  774. free_extent_map(em);
  775. iput(inode);
  776. free_dummy_root(root);
  777. return ret;
  778. }
  779. int btrfs_test_inodes(void)
  780. {
  781. test_msg("Running btrfs_get_extent tests\n");
  782. return test_btrfs_get_extent();
  783. }