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