inode-tests.c 32 KB

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