inode-tests.c 32 KB

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