inode-tests.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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(nodesize, sectorsize);
  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);
  230. if (IS_ERR(root)) {
  231. test_msg("Couldn't allocate root\n");
  232. goto out;
  233. }
  234. root->node = alloc_dummy_extent_buffer(fs_info, 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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(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(BTRFS_I(inode), NULL, 0, offset + 6,
  696. sectorsize, 0);
  697. if (IS_ERR(em)) {
  698. test_msg("Got an error when we shouldn't have\n");
  699. goto out;
  700. }
  701. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  702. test_msg("Expected a real extent, got %llu\n", em->block_start);
  703. goto out;
  704. }
  705. if (em->start != offset || em->len != sectorsize) {
  706. test_msg("Unexpected extent wanted start %llu len %u, "
  707. "got start %llu len %llu\n",
  708. offset, sectorsize, em->start, em->len);
  709. goto out;
  710. }
  711. if (em->flags != 0) {
  712. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  713. goto out;
  714. }
  715. if (em->orig_start != em->start) {
  716. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  717. em->orig_start);
  718. goto out;
  719. }
  720. offset = em->start + em->len;
  721. free_extent_map(em);
  722. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, 4096 * 1024, 0);
  723. if (IS_ERR(em)) {
  724. test_msg("Got an error when we shouldn't have\n");
  725. goto out;
  726. }
  727. if (em->block_start != EXTENT_MAP_HOLE) {
  728. test_msg("Expected a hole extent, got %llu\n", em->block_start);
  729. goto out;
  730. }
  731. /*
  732. * Currently we just return a length that we requested rather than the
  733. * length of the actual hole, if this changes we'll have to change this
  734. * test.
  735. */
  736. if (em->start != offset || em->len != 3 * sectorsize) {
  737. test_msg("Unexpected extent wanted start %llu len %u, "
  738. "got start %llu len %llu\n",
  739. offset, 3 * sectorsize, em->start, em->len);
  740. goto out;
  741. }
  742. if (em->flags != vacancy_only) {
  743. test_msg("Unexpected flags set, want %lu have %lu\n",
  744. vacancy_only, em->flags);
  745. goto out;
  746. }
  747. if (em->orig_start != em->start) {
  748. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  749. em->orig_start);
  750. goto out;
  751. }
  752. offset = em->start + em->len;
  753. free_extent_map(em);
  754. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, sectorsize, 0);
  755. if (IS_ERR(em)) {
  756. test_msg("Got an error when we shouldn't have\n");
  757. goto out;
  758. }
  759. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  760. test_msg("Expected a real extent, got %llu\n", em->block_start);
  761. goto out;
  762. }
  763. if (em->start != offset || em->len != sectorsize) {
  764. test_msg("Unexpected extent wanted start %llu len %u,"
  765. "got start %llu len %llu\n",
  766. offset, sectorsize, em->start, em->len);
  767. goto out;
  768. }
  769. if (em->flags != 0) {
  770. test_msg("Unexpected flags set, want 0 have %lu\n", em->flags);
  771. goto out;
  772. }
  773. if (em->orig_start != em->start) {
  774. test_msg("Wrong orig offset, want %llu, have %llu\n", em->start,
  775. em->orig_start);
  776. goto out;
  777. }
  778. ret = 0;
  779. out:
  780. if (!IS_ERR(em))
  781. free_extent_map(em);
  782. iput(inode);
  783. btrfs_free_dummy_root(root);
  784. btrfs_free_dummy_fs_info(fs_info);
  785. return ret;
  786. }
  787. static int test_hole_first(u32 sectorsize, u32 nodesize)
  788. {
  789. struct btrfs_fs_info *fs_info = NULL;
  790. struct inode *inode = NULL;
  791. struct btrfs_root *root = NULL;
  792. struct extent_map *em = NULL;
  793. int ret = -ENOMEM;
  794. inode = btrfs_new_test_inode();
  795. if (!inode) {
  796. test_msg("Couldn't allocate inode\n");
  797. return ret;
  798. }
  799. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  800. BTRFS_I(inode)->location.objectid = BTRFS_FIRST_FREE_OBJECTID;
  801. BTRFS_I(inode)->location.offset = 0;
  802. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  803. if (!fs_info) {
  804. test_msg("Couldn't allocate dummy fs info\n");
  805. goto out;
  806. }
  807. root = btrfs_alloc_dummy_root(fs_info);
  808. if (IS_ERR(root)) {
  809. test_msg("Couldn't allocate root\n");
  810. goto out;
  811. }
  812. root->node = alloc_dummy_extent_buffer(fs_info, nodesize);
  813. if (!root->node) {
  814. test_msg("Couldn't allocate dummy buffer\n");
  815. goto out;
  816. }
  817. extent_buffer_get(root->node);
  818. btrfs_set_header_nritems(root->node, 0);
  819. btrfs_set_header_level(root->node, 0);
  820. BTRFS_I(inode)->root = root;
  821. ret = -EINVAL;
  822. /*
  823. * Need a blank inode item here just so we don't confuse
  824. * btrfs_get_extent.
  825. */
  826. insert_inode_item_key(root);
  827. insert_extent(root, sectorsize, sectorsize, sectorsize, 0, sectorsize,
  828. sectorsize, BTRFS_FILE_EXTENT_REG, 0, 1);
  829. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, 0, 2 * sectorsize, 0);
  830. if (IS_ERR(em)) {
  831. test_msg("Got an error when we shouldn't have\n");
  832. goto out;
  833. }
  834. if (em->block_start != EXTENT_MAP_HOLE) {
  835. test_msg("Expected a hole, got %llu\n", em->block_start);
  836. goto out;
  837. }
  838. if (em->start != 0 || em->len != sectorsize) {
  839. test_msg("Unexpected extent wanted start 0 len %u, "
  840. "got start %llu len %llu\n",
  841. sectorsize, em->start, em->len);
  842. goto out;
  843. }
  844. if (em->flags != vacancy_only) {
  845. test_msg("Wrong flags, wanted %lu, have %lu\n", vacancy_only,
  846. em->flags);
  847. goto out;
  848. }
  849. free_extent_map(em);
  850. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, sectorsize,
  851. 2 * sectorsize, 0);
  852. if (IS_ERR(em)) {
  853. test_msg("Got an error when we shouldn't have\n");
  854. goto out;
  855. }
  856. if (em->block_start != sectorsize) {
  857. test_msg("Expected a real extent, got %llu\n", em->block_start);
  858. goto out;
  859. }
  860. if (em->start != sectorsize || em->len != sectorsize) {
  861. test_msg("Unexpected extent wanted start %u len %u, "
  862. "got start %llu len %llu\n",
  863. sectorsize, sectorsize, em->start, em->len);
  864. goto out;
  865. }
  866. if (em->flags != 0) {
  867. test_msg("Unexpected flags set, wanted 0 got %lu\n",
  868. em->flags);
  869. goto out;
  870. }
  871. ret = 0;
  872. out:
  873. if (!IS_ERR(em))
  874. free_extent_map(em);
  875. iput(inode);
  876. btrfs_free_dummy_root(root);
  877. btrfs_free_dummy_fs_info(fs_info);
  878. return ret;
  879. }
  880. static int test_extent_accounting(u32 sectorsize, u32 nodesize)
  881. {
  882. struct btrfs_fs_info *fs_info = NULL;
  883. struct inode *inode = NULL;
  884. struct btrfs_root *root = NULL;
  885. int ret = -ENOMEM;
  886. inode = btrfs_new_test_inode();
  887. if (!inode) {
  888. test_msg("Couldn't allocate inode\n");
  889. return ret;
  890. }
  891. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  892. if (!fs_info) {
  893. test_msg("Couldn't allocate dummy fs info\n");
  894. goto out;
  895. }
  896. root = btrfs_alloc_dummy_root(fs_info);
  897. if (IS_ERR(root)) {
  898. test_msg("Couldn't allocate root\n");
  899. goto out;
  900. }
  901. BTRFS_I(inode)->root = root;
  902. btrfs_test_inode_set_ops(inode);
  903. /* [BTRFS_MAX_EXTENT_SIZE] */
  904. BTRFS_I(inode)->outstanding_extents++;
  905. ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1,
  906. NULL, 0);
  907. if (ret) {
  908. test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
  909. goto out;
  910. }
  911. if (BTRFS_I(inode)->outstanding_extents != 1) {
  912. ret = -EINVAL;
  913. test_msg("Miscount, wanted 1, got %u\n",
  914. BTRFS_I(inode)->outstanding_extents);
  915. goto out;
  916. }
  917. /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
  918. BTRFS_I(inode)->outstanding_extents++;
  919. ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
  920. BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
  921. NULL, 0);
  922. if (ret) {
  923. test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
  924. goto out;
  925. }
  926. if (BTRFS_I(inode)->outstanding_extents != 2) {
  927. ret = -EINVAL;
  928. test_msg("Miscount, wanted 2, got %u\n",
  929. BTRFS_I(inode)->outstanding_extents);
  930. goto out;
  931. }
  932. /* [BTRFS_MAX_EXTENT_SIZE/2][sectorsize HOLE][the rest] */
  933. ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
  934. BTRFS_MAX_EXTENT_SIZE >> 1,
  935. (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
  936. EXTENT_DELALLOC | EXTENT_DIRTY |
  937. EXTENT_UPTODATE | EXTENT_DO_ACCOUNTING, 0, 0,
  938. NULL, GFP_KERNEL);
  939. if (ret) {
  940. test_msg("clear_extent_bit returned %d\n", ret);
  941. goto out;
  942. }
  943. if (BTRFS_I(inode)->outstanding_extents != 2) {
  944. ret = -EINVAL;
  945. test_msg("Miscount, wanted 2, got %u\n",
  946. BTRFS_I(inode)->outstanding_extents);
  947. goto out;
  948. }
  949. /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
  950. BTRFS_I(inode)->outstanding_extents++;
  951. ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
  952. (BTRFS_MAX_EXTENT_SIZE >> 1)
  953. + sectorsize - 1,
  954. NULL, 0);
  955. if (ret) {
  956. test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
  957. goto out;
  958. }
  959. if (BTRFS_I(inode)->outstanding_extents != 2) {
  960. ret = -EINVAL;
  961. test_msg("Miscount, wanted 2, got %u\n",
  962. BTRFS_I(inode)->outstanding_extents);
  963. goto out;
  964. }
  965. /*
  966. * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
  967. *
  968. * I'm artificially adding 2 to outstanding_extents because in the
  969. * buffered IO case we'd add things up as we go, but I don't feel like
  970. * doing that here, this isn't the interesting case we want to test.
  971. */
  972. BTRFS_I(inode)->outstanding_extents += 2;
  973. ret = btrfs_set_extent_delalloc(inode,
  974. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
  975. (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
  976. NULL, 0);
  977. if (ret) {
  978. test_msg("btrfs_set_extent_delalloc 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. * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
  989. */
  990. BTRFS_I(inode)->outstanding_extents++;
  991. ret = btrfs_set_extent_delalloc(inode,
  992. BTRFS_MAX_EXTENT_SIZE + sectorsize,
  993. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0);
  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. /* [BTRFS_MAX_EXTENT_SIZE+4k][4K HOLE][BTRFS_MAX_EXTENT_SIZE+4k] */
  1005. ret = clear_extent_bit(&BTRFS_I(inode)->io_tree,
  1006. BTRFS_MAX_EXTENT_SIZE + sectorsize,
  1007. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
  1008. EXTENT_DIRTY | EXTENT_DELALLOC |
  1009. EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
  1010. NULL, GFP_KERNEL);
  1011. if (ret) {
  1012. test_msg("clear_extent_bit returned %d\n", ret);
  1013. goto out;
  1014. }
  1015. if (BTRFS_I(inode)->outstanding_extents != 4) {
  1016. ret = -EINVAL;
  1017. test_msg("Miscount, wanted 4, got %u\n",
  1018. BTRFS_I(inode)->outstanding_extents);
  1019. goto out;
  1020. }
  1021. /*
  1022. * Refill the hole again just for good measure, because I thought it
  1023. * might fail and I'd rather satisfy my paranoia at this point.
  1024. */
  1025. BTRFS_I(inode)->outstanding_extents++;
  1026. ret = btrfs_set_extent_delalloc(inode,
  1027. BTRFS_MAX_EXTENT_SIZE + sectorsize,
  1028. BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0);
  1029. if (ret) {
  1030. test_msg("btrfs_set_extent_delalloc returned %d\n", ret);
  1031. goto out;
  1032. }
  1033. if (BTRFS_I(inode)->outstanding_extents != 3) {
  1034. ret = -EINVAL;
  1035. test_msg("Miscount, wanted 3, got %u\n",
  1036. BTRFS_I(inode)->outstanding_extents);
  1037. goto out;
  1038. }
  1039. /* Empty */
  1040. ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  1041. EXTENT_DIRTY | EXTENT_DELALLOC |
  1042. EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
  1043. NULL, GFP_KERNEL);
  1044. if (ret) {
  1045. test_msg("clear_extent_bit returned %d\n", ret);
  1046. goto out;
  1047. }
  1048. if (BTRFS_I(inode)->outstanding_extents) {
  1049. ret = -EINVAL;
  1050. test_msg("Miscount, wanted 0, got %u\n",
  1051. BTRFS_I(inode)->outstanding_extents);
  1052. goto out;
  1053. }
  1054. ret = 0;
  1055. out:
  1056. if (ret)
  1057. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  1058. EXTENT_DIRTY | EXTENT_DELALLOC |
  1059. EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0,
  1060. NULL, GFP_KERNEL);
  1061. iput(inode);
  1062. btrfs_free_dummy_root(root);
  1063. btrfs_free_dummy_fs_info(fs_info);
  1064. return ret;
  1065. }
  1066. int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
  1067. {
  1068. int ret;
  1069. set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
  1070. set_bit(EXTENT_FLAG_VACANCY, &vacancy_only);
  1071. set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
  1072. test_msg("Running btrfs_get_extent tests\n");
  1073. ret = test_btrfs_get_extent(sectorsize, nodesize);
  1074. if (ret)
  1075. return ret;
  1076. test_msg("Running hole first btrfs_get_extent test\n");
  1077. ret = test_hole_first(sectorsize, nodesize);
  1078. if (ret)
  1079. return ret;
  1080. test_msg("Running outstanding_extents tests\n");
  1081. return test_extent_accounting(sectorsize, nodesize);
  1082. }