inode-tests.c 33 KB

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