free-space-cache.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. /*
  2. * Copyright (C) 2008 Red Hat. 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/pagemap.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/math64.h>
  22. #include "ctree.h"
  23. #include "free-space-cache.h"
  24. #include "transaction.h"
  25. #include "disk-io.h"
  26. #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
  27. #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
  28. static void recalculate_thresholds(struct btrfs_block_group_cache
  29. *block_group);
  30. static int link_free_space(struct btrfs_block_group_cache *block_group,
  31. struct btrfs_free_space *info);
  32. struct inode *lookup_free_space_inode(struct btrfs_root *root,
  33. struct btrfs_block_group_cache
  34. *block_group, struct btrfs_path *path)
  35. {
  36. struct btrfs_key key;
  37. struct btrfs_key location;
  38. struct btrfs_disk_key disk_key;
  39. struct btrfs_free_space_header *header;
  40. struct extent_buffer *leaf;
  41. struct inode *inode = NULL;
  42. int ret;
  43. spin_lock(&block_group->lock);
  44. if (block_group->inode)
  45. inode = igrab(block_group->inode);
  46. spin_unlock(&block_group->lock);
  47. if (inode)
  48. return inode;
  49. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  50. key.offset = block_group->key.objectid;
  51. key.type = 0;
  52. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  53. if (ret < 0)
  54. return ERR_PTR(ret);
  55. if (ret > 0) {
  56. btrfs_release_path(root, path);
  57. return ERR_PTR(-ENOENT);
  58. }
  59. leaf = path->nodes[0];
  60. header = btrfs_item_ptr(leaf, path->slots[0],
  61. struct btrfs_free_space_header);
  62. btrfs_free_space_key(leaf, header, &disk_key);
  63. btrfs_disk_key_to_cpu(&location, &disk_key);
  64. btrfs_release_path(root, path);
  65. inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
  66. if (!inode)
  67. return ERR_PTR(-ENOENT);
  68. if (IS_ERR(inode))
  69. return inode;
  70. if (is_bad_inode(inode)) {
  71. iput(inode);
  72. return ERR_PTR(-ENOENT);
  73. }
  74. spin_lock(&block_group->lock);
  75. if (!root->fs_info->closing) {
  76. block_group->inode = igrab(inode);
  77. block_group->iref = 1;
  78. }
  79. spin_unlock(&block_group->lock);
  80. return inode;
  81. }
  82. int create_free_space_inode(struct btrfs_root *root,
  83. struct btrfs_trans_handle *trans,
  84. struct btrfs_block_group_cache *block_group,
  85. struct btrfs_path *path)
  86. {
  87. struct btrfs_key key;
  88. struct btrfs_disk_key disk_key;
  89. struct btrfs_free_space_header *header;
  90. struct btrfs_inode_item *inode_item;
  91. struct extent_buffer *leaf;
  92. u64 objectid;
  93. int ret;
  94. ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
  95. if (ret < 0)
  96. return ret;
  97. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  98. if (ret)
  99. return ret;
  100. leaf = path->nodes[0];
  101. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  102. struct btrfs_inode_item);
  103. btrfs_item_key(leaf, &disk_key, path->slots[0]);
  104. memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
  105. sizeof(*inode_item));
  106. btrfs_set_inode_generation(leaf, inode_item, trans->transid);
  107. btrfs_set_inode_size(leaf, inode_item, 0);
  108. btrfs_set_inode_nbytes(leaf, inode_item, 0);
  109. btrfs_set_inode_uid(leaf, inode_item, 0);
  110. btrfs_set_inode_gid(leaf, inode_item, 0);
  111. btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
  112. btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
  113. BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
  114. btrfs_set_inode_nlink(leaf, inode_item, 1);
  115. btrfs_set_inode_transid(leaf, inode_item, trans->transid);
  116. btrfs_set_inode_block_group(leaf, inode_item,
  117. block_group->key.objectid);
  118. btrfs_mark_buffer_dirty(leaf);
  119. btrfs_release_path(root, path);
  120. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  121. key.offset = block_group->key.objectid;
  122. key.type = 0;
  123. ret = btrfs_insert_empty_item(trans, root, path, &key,
  124. sizeof(struct btrfs_free_space_header));
  125. if (ret < 0) {
  126. btrfs_release_path(root, path);
  127. return ret;
  128. }
  129. leaf = path->nodes[0];
  130. header = btrfs_item_ptr(leaf, path->slots[0],
  131. struct btrfs_free_space_header);
  132. memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
  133. btrfs_set_free_space_key(leaf, header, &disk_key);
  134. btrfs_mark_buffer_dirty(leaf);
  135. btrfs_release_path(root, path);
  136. return 0;
  137. }
  138. int btrfs_truncate_free_space_cache(struct btrfs_root *root,
  139. struct btrfs_trans_handle *trans,
  140. struct btrfs_path *path,
  141. struct inode *inode)
  142. {
  143. loff_t oldsize;
  144. int ret = 0;
  145. trans->block_rsv = root->orphan_block_rsv;
  146. ret = btrfs_block_rsv_check(trans, root,
  147. root->orphan_block_rsv,
  148. 0, 5);
  149. if (ret)
  150. return ret;
  151. oldsize = i_size_read(inode);
  152. btrfs_i_size_write(inode, 0);
  153. truncate_pagecache(inode, oldsize, 0);
  154. /*
  155. * We don't need an orphan item because truncating the free space cache
  156. * will never be split across transactions.
  157. */
  158. ret = btrfs_truncate_inode_items(trans, root, inode,
  159. 0, BTRFS_EXTENT_DATA_KEY);
  160. if (ret) {
  161. WARN_ON(1);
  162. return ret;
  163. }
  164. return btrfs_update_inode(trans, root, inode);
  165. }
  166. static int readahead_cache(struct inode *inode)
  167. {
  168. struct file_ra_state *ra;
  169. unsigned long last_index;
  170. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  171. if (!ra)
  172. return -ENOMEM;
  173. file_ra_state_init(ra, inode->i_mapping);
  174. last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  175. page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
  176. kfree(ra);
  177. return 0;
  178. }
  179. int load_free_space_cache(struct btrfs_fs_info *fs_info,
  180. struct btrfs_block_group_cache *block_group)
  181. {
  182. struct btrfs_root *root = fs_info->tree_root;
  183. struct inode *inode;
  184. struct btrfs_free_space_header *header;
  185. struct extent_buffer *leaf;
  186. struct page *page;
  187. struct btrfs_path *path;
  188. u32 *checksums = NULL, *crc;
  189. char *disk_crcs = NULL;
  190. struct btrfs_key key;
  191. struct list_head bitmaps;
  192. u64 num_entries;
  193. u64 num_bitmaps;
  194. u64 generation;
  195. u32 cur_crc = ~(u32)0;
  196. pgoff_t index = 0;
  197. unsigned long first_page_offset;
  198. int num_checksums;
  199. int ret = 0;
  200. /*
  201. * If we're unmounting then just return, since this does a search on the
  202. * normal root and not the commit root and we could deadlock.
  203. */
  204. smp_mb();
  205. if (fs_info->closing)
  206. return 0;
  207. /*
  208. * If this block group has been marked to be cleared for one reason or
  209. * another then we can't trust the on disk cache, so just return.
  210. */
  211. spin_lock(&block_group->lock);
  212. if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
  213. spin_unlock(&block_group->lock);
  214. return 0;
  215. }
  216. spin_unlock(&block_group->lock);
  217. INIT_LIST_HEAD(&bitmaps);
  218. path = btrfs_alloc_path();
  219. if (!path)
  220. return 0;
  221. inode = lookup_free_space_inode(root, block_group, path);
  222. if (IS_ERR(inode)) {
  223. btrfs_free_path(path);
  224. return 0;
  225. }
  226. /* Nothing in the space cache, goodbye */
  227. if (!i_size_read(inode)) {
  228. btrfs_free_path(path);
  229. goto out;
  230. }
  231. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  232. key.offset = block_group->key.objectid;
  233. key.type = 0;
  234. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  235. if (ret) {
  236. btrfs_free_path(path);
  237. goto out;
  238. }
  239. leaf = path->nodes[0];
  240. header = btrfs_item_ptr(leaf, path->slots[0],
  241. struct btrfs_free_space_header);
  242. num_entries = btrfs_free_space_entries(leaf, header);
  243. num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
  244. generation = btrfs_free_space_generation(leaf, header);
  245. btrfs_free_path(path);
  246. if (BTRFS_I(inode)->generation != generation) {
  247. printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
  248. " not match free space cache generation (%llu) for "
  249. "block group %llu\n",
  250. (unsigned long long)BTRFS_I(inode)->generation,
  251. (unsigned long long)generation,
  252. (unsigned long long)block_group->key.objectid);
  253. goto free_cache;
  254. }
  255. if (!num_entries)
  256. goto out;
  257. /* Setup everything for doing checksumming */
  258. num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
  259. checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
  260. if (!checksums)
  261. goto out;
  262. first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
  263. disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
  264. if (!disk_crcs)
  265. goto out;
  266. ret = readahead_cache(inode);
  267. if (ret) {
  268. ret = 0;
  269. goto out;
  270. }
  271. while (1) {
  272. struct btrfs_free_space_entry *entry;
  273. struct btrfs_free_space *e;
  274. void *addr;
  275. unsigned long offset = 0;
  276. unsigned long start_offset = 0;
  277. int need_loop = 0;
  278. if (!num_entries && !num_bitmaps)
  279. break;
  280. if (index == 0) {
  281. start_offset = first_page_offset;
  282. offset = start_offset;
  283. }
  284. page = grab_cache_page(inode->i_mapping, index);
  285. if (!page) {
  286. ret = 0;
  287. goto free_cache;
  288. }
  289. if (!PageUptodate(page)) {
  290. btrfs_readpage(NULL, page);
  291. lock_page(page);
  292. if (!PageUptodate(page)) {
  293. unlock_page(page);
  294. page_cache_release(page);
  295. printk(KERN_ERR "btrfs: error reading free "
  296. "space cache: %llu\n",
  297. (unsigned long long)
  298. block_group->key.objectid);
  299. goto free_cache;
  300. }
  301. }
  302. addr = kmap(page);
  303. if (index == 0) {
  304. u64 *gen;
  305. memcpy(disk_crcs, addr, first_page_offset);
  306. gen = addr + (sizeof(u32) * num_checksums);
  307. if (*gen != BTRFS_I(inode)->generation) {
  308. printk(KERN_ERR "btrfs: space cache generation"
  309. " (%llu) does not match inode (%llu) "
  310. "for block group %llu\n",
  311. (unsigned long long)*gen,
  312. (unsigned long long)
  313. BTRFS_I(inode)->generation,
  314. (unsigned long long)
  315. block_group->key.objectid);
  316. kunmap(page);
  317. unlock_page(page);
  318. page_cache_release(page);
  319. goto free_cache;
  320. }
  321. crc = (u32 *)disk_crcs;
  322. }
  323. entry = addr + start_offset;
  324. /* First lets check our crc before we do anything fun */
  325. cur_crc = ~(u32)0;
  326. cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
  327. PAGE_CACHE_SIZE - start_offset);
  328. btrfs_csum_final(cur_crc, (char *)&cur_crc);
  329. if (cur_crc != *crc) {
  330. printk(KERN_ERR "btrfs: crc mismatch for page %lu in "
  331. "block group %llu\n", index,
  332. (unsigned long long)block_group->key.objectid);
  333. kunmap(page);
  334. unlock_page(page);
  335. page_cache_release(page);
  336. goto free_cache;
  337. }
  338. crc++;
  339. while (1) {
  340. if (!num_entries)
  341. break;
  342. need_loop = 1;
  343. e = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  344. if (!e) {
  345. kunmap(page);
  346. unlock_page(page);
  347. page_cache_release(page);
  348. goto free_cache;
  349. }
  350. e->offset = le64_to_cpu(entry->offset);
  351. e->bytes = le64_to_cpu(entry->bytes);
  352. if (!e->bytes) {
  353. kunmap(page);
  354. kfree(e);
  355. unlock_page(page);
  356. page_cache_release(page);
  357. goto free_cache;
  358. }
  359. if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
  360. spin_lock(&block_group->tree_lock);
  361. ret = link_free_space(block_group, e);
  362. spin_unlock(&block_group->tree_lock);
  363. BUG_ON(ret);
  364. } else {
  365. e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  366. if (!e->bitmap) {
  367. kunmap(page);
  368. kfree(e);
  369. unlock_page(page);
  370. page_cache_release(page);
  371. goto free_cache;
  372. }
  373. spin_lock(&block_group->tree_lock);
  374. ret = link_free_space(block_group, e);
  375. block_group->total_bitmaps++;
  376. recalculate_thresholds(block_group);
  377. spin_unlock(&block_group->tree_lock);
  378. list_add_tail(&e->list, &bitmaps);
  379. }
  380. num_entries--;
  381. offset += sizeof(struct btrfs_free_space_entry);
  382. if (offset + sizeof(struct btrfs_free_space_entry) >=
  383. PAGE_CACHE_SIZE)
  384. break;
  385. entry++;
  386. }
  387. /*
  388. * We read an entry out of this page, we need to move on to the
  389. * next page.
  390. */
  391. if (need_loop) {
  392. kunmap(page);
  393. goto next;
  394. }
  395. /*
  396. * We add the bitmaps at the end of the entries in order that
  397. * the bitmap entries are added to the cache.
  398. */
  399. e = list_entry(bitmaps.next, struct btrfs_free_space, list);
  400. list_del_init(&e->list);
  401. memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
  402. kunmap(page);
  403. num_bitmaps--;
  404. next:
  405. unlock_page(page);
  406. page_cache_release(page);
  407. index++;
  408. }
  409. ret = 1;
  410. out:
  411. kfree(checksums);
  412. kfree(disk_crcs);
  413. iput(inode);
  414. return ret;
  415. free_cache:
  416. /* This cache is bogus, make sure it gets cleared */
  417. spin_lock(&block_group->lock);
  418. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  419. spin_unlock(&block_group->lock);
  420. btrfs_remove_free_space_cache(block_group);
  421. goto out;
  422. }
  423. int btrfs_write_out_cache(struct btrfs_root *root,
  424. struct btrfs_trans_handle *trans,
  425. struct btrfs_block_group_cache *block_group,
  426. struct btrfs_path *path)
  427. {
  428. struct btrfs_free_space_header *header;
  429. struct extent_buffer *leaf;
  430. struct inode *inode;
  431. struct rb_node *node;
  432. struct list_head *pos, *n;
  433. struct page *page;
  434. struct extent_state *cached_state = NULL;
  435. struct list_head bitmap_list;
  436. struct btrfs_key key;
  437. u64 bytes = 0;
  438. u32 *crc, *checksums;
  439. pgoff_t index = 0, last_index = 0;
  440. unsigned long first_page_offset;
  441. int num_checksums;
  442. int entries = 0;
  443. int bitmaps = 0;
  444. int ret = 0;
  445. root = root->fs_info->tree_root;
  446. INIT_LIST_HEAD(&bitmap_list);
  447. spin_lock(&block_group->lock);
  448. if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
  449. spin_unlock(&block_group->lock);
  450. return 0;
  451. }
  452. spin_unlock(&block_group->lock);
  453. inode = lookup_free_space_inode(root, block_group, path);
  454. if (IS_ERR(inode))
  455. return 0;
  456. if (!i_size_read(inode)) {
  457. iput(inode);
  458. return 0;
  459. }
  460. node = rb_first(&block_group->free_space_offset);
  461. if (!node) {
  462. iput(inode);
  463. return 0;
  464. }
  465. last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  466. filemap_write_and_wait(inode->i_mapping);
  467. btrfs_wait_ordered_range(inode, inode->i_size &
  468. ~(root->sectorsize - 1), (u64)-1);
  469. /* We need a checksum per page. */
  470. num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
  471. crc = checksums = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
  472. if (!crc) {
  473. iput(inode);
  474. return 0;
  475. }
  476. /* Since the first page has all of our checksums and our generation we
  477. * need to calculate the offset into the page that we can start writing
  478. * our entries.
  479. */
  480. first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
  481. /*
  482. * Lock all pages first so we can lock the extent safely.
  483. *
  484. * NOTE: Because we hold the ref the entire time we're going to write to
  485. * the page find_get_page should never fail, so we don't do a check
  486. * after find_get_page at this point. Just putting this here so people
  487. * know and don't freak out.
  488. */
  489. while (index <= last_index) {
  490. page = grab_cache_page(inode->i_mapping, index);
  491. if (!page) {
  492. pgoff_t i = 0;
  493. while (i < index) {
  494. page = find_get_page(inode->i_mapping, i);
  495. unlock_page(page);
  496. page_cache_release(page);
  497. page_cache_release(page);
  498. i++;
  499. }
  500. goto out_free;
  501. }
  502. index++;
  503. }
  504. index = 0;
  505. lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  506. 0, &cached_state, GFP_NOFS);
  507. /* Write out the extent entries */
  508. do {
  509. struct btrfs_free_space_entry *entry;
  510. void *addr;
  511. unsigned long offset = 0;
  512. unsigned long start_offset = 0;
  513. if (index == 0) {
  514. start_offset = first_page_offset;
  515. offset = start_offset;
  516. }
  517. page = find_get_page(inode->i_mapping, index);
  518. addr = kmap(page);
  519. entry = addr + start_offset;
  520. memset(addr, 0, PAGE_CACHE_SIZE);
  521. while (1) {
  522. struct btrfs_free_space *e;
  523. e = rb_entry(node, struct btrfs_free_space, offset_index);
  524. entries++;
  525. entry->offset = cpu_to_le64(e->offset);
  526. entry->bytes = cpu_to_le64(e->bytes);
  527. if (e->bitmap) {
  528. entry->type = BTRFS_FREE_SPACE_BITMAP;
  529. list_add_tail(&e->list, &bitmap_list);
  530. bitmaps++;
  531. } else {
  532. entry->type = BTRFS_FREE_SPACE_EXTENT;
  533. }
  534. node = rb_next(node);
  535. if (!node)
  536. break;
  537. offset += sizeof(struct btrfs_free_space_entry);
  538. if (offset + sizeof(struct btrfs_free_space_entry) >=
  539. PAGE_CACHE_SIZE)
  540. break;
  541. entry++;
  542. }
  543. *crc = ~(u32)0;
  544. *crc = btrfs_csum_data(root, addr + start_offset, *crc,
  545. PAGE_CACHE_SIZE - start_offset);
  546. kunmap(page);
  547. btrfs_csum_final(*crc, (char *)crc);
  548. crc++;
  549. bytes += PAGE_CACHE_SIZE;
  550. ClearPageChecked(page);
  551. set_page_extent_mapped(page);
  552. SetPageUptodate(page);
  553. set_page_dirty(page);
  554. /*
  555. * We need to release our reference we got for grab_cache_page,
  556. * except for the first page which will hold our checksums, we
  557. * do that below.
  558. */
  559. if (index != 0) {
  560. unlock_page(page);
  561. page_cache_release(page);
  562. }
  563. page_cache_release(page);
  564. index++;
  565. } while (node);
  566. /* Write out the bitmaps */
  567. list_for_each_safe(pos, n, &bitmap_list) {
  568. void *addr;
  569. struct btrfs_free_space *entry =
  570. list_entry(pos, struct btrfs_free_space, list);
  571. page = find_get_page(inode->i_mapping, index);
  572. addr = kmap(page);
  573. memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
  574. *crc = ~(u32)0;
  575. *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
  576. kunmap(page);
  577. btrfs_csum_final(*crc, (char *)crc);
  578. crc++;
  579. bytes += PAGE_CACHE_SIZE;
  580. ClearPageChecked(page);
  581. set_page_extent_mapped(page);
  582. SetPageUptodate(page);
  583. set_page_dirty(page);
  584. unlock_page(page);
  585. page_cache_release(page);
  586. page_cache_release(page);
  587. list_del_init(&entry->list);
  588. index++;
  589. }
  590. /* Zero out the rest of the pages just to make sure */
  591. while (index <= last_index) {
  592. void *addr;
  593. page = find_get_page(inode->i_mapping, index);
  594. addr = kmap(page);
  595. memset(addr, 0, PAGE_CACHE_SIZE);
  596. kunmap(page);
  597. ClearPageChecked(page);
  598. set_page_extent_mapped(page);
  599. SetPageUptodate(page);
  600. set_page_dirty(page);
  601. unlock_page(page);
  602. page_cache_release(page);
  603. page_cache_release(page);
  604. bytes += PAGE_CACHE_SIZE;
  605. index++;
  606. }
  607. btrfs_set_extent_delalloc(inode, 0, bytes - 1, &cached_state);
  608. /* Write the checksums and trans id to the first page */
  609. {
  610. void *addr;
  611. u64 *gen;
  612. page = find_get_page(inode->i_mapping, 0);
  613. addr = kmap(page);
  614. memcpy(addr, checksums, sizeof(u32) * num_checksums);
  615. gen = addr + (sizeof(u32) * num_checksums);
  616. *gen = trans->transid;
  617. kunmap(page);
  618. ClearPageChecked(page);
  619. set_page_extent_mapped(page);
  620. SetPageUptodate(page);
  621. set_page_dirty(page);
  622. unlock_page(page);
  623. page_cache_release(page);
  624. page_cache_release(page);
  625. }
  626. BTRFS_I(inode)->generation = trans->transid;
  627. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  628. i_size_read(inode) - 1, &cached_state, GFP_NOFS);
  629. filemap_write_and_wait(inode->i_mapping);
  630. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  631. key.offset = block_group->key.objectid;
  632. key.type = 0;
  633. ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
  634. if (ret < 0) {
  635. ret = 0;
  636. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  637. EXTENT_DIRTY | EXTENT_DELALLOC |
  638. EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
  639. goto out_free;
  640. }
  641. leaf = path->nodes[0];
  642. if (ret > 0) {
  643. struct btrfs_key found_key;
  644. BUG_ON(!path->slots[0]);
  645. path->slots[0]--;
  646. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  647. if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
  648. found_key.offset != block_group->key.objectid) {
  649. ret = 0;
  650. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  651. EXTENT_DIRTY | EXTENT_DELALLOC |
  652. EXTENT_DO_ACCOUNTING, 0, 0, NULL,
  653. GFP_NOFS);
  654. btrfs_release_path(root, path);
  655. goto out_free;
  656. }
  657. }
  658. header = btrfs_item_ptr(leaf, path->slots[0],
  659. struct btrfs_free_space_header);
  660. btrfs_set_free_space_entries(leaf, header, entries);
  661. btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
  662. btrfs_set_free_space_generation(leaf, header, trans->transid);
  663. btrfs_mark_buffer_dirty(leaf);
  664. btrfs_release_path(root, path);
  665. ret = 1;
  666. out_free:
  667. if (ret == 0) {
  668. invalidate_inode_pages2_range(inode->i_mapping, 0, index);
  669. spin_lock(&block_group->lock);
  670. block_group->disk_cache_state = BTRFS_DC_ERROR;
  671. spin_unlock(&block_group->lock);
  672. BTRFS_I(inode)->generation = 0;
  673. }
  674. kfree(checksums);
  675. btrfs_update_inode(trans, root, inode);
  676. iput(inode);
  677. return ret;
  678. }
  679. static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
  680. u64 offset)
  681. {
  682. BUG_ON(offset < bitmap_start);
  683. offset -= bitmap_start;
  684. return (unsigned long)(div64_u64(offset, sectorsize));
  685. }
  686. static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
  687. {
  688. return (unsigned long)(div64_u64(bytes, sectorsize));
  689. }
  690. static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
  691. u64 offset)
  692. {
  693. u64 bitmap_start;
  694. u64 bytes_per_bitmap;
  695. bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
  696. bitmap_start = offset - block_group->key.objectid;
  697. bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
  698. bitmap_start *= bytes_per_bitmap;
  699. bitmap_start += block_group->key.objectid;
  700. return bitmap_start;
  701. }
  702. static int tree_insert_offset(struct rb_root *root, u64 offset,
  703. struct rb_node *node, int bitmap)
  704. {
  705. struct rb_node **p = &root->rb_node;
  706. struct rb_node *parent = NULL;
  707. struct btrfs_free_space *info;
  708. while (*p) {
  709. parent = *p;
  710. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  711. if (offset < info->offset) {
  712. p = &(*p)->rb_left;
  713. } else if (offset > info->offset) {
  714. p = &(*p)->rb_right;
  715. } else {
  716. /*
  717. * we could have a bitmap entry and an extent entry
  718. * share the same offset. If this is the case, we want
  719. * the extent entry to always be found first if we do a
  720. * linear search through the tree, since we want to have
  721. * the quickest allocation time, and allocating from an
  722. * extent is faster than allocating from a bitmap. So
  723. * if we're inserting a bitmap and we find an entry at
  724. * this offset, we want to go right, or after this entry
  725. * logically. If we are inserting an extent and we've
  726. * found a bitmap, we want to go left, or before
  727. * logically.
  728. */
  729. if (bitmap) {
  730. WARN_ON(info->bitmap);
  731. p = &(*p)->rb_right;
  732. } else {
  733. WARN_ON(!info->bitmap);
  734. p = &(*p)->rb_left;
  735. }
  736. }
  737. }
  738. rb_link_node(node, parent, p);
  739. rb_insert_color(node, root);
  740. return 0;
  741. }
  742. /*
  743. * searches the tree for the given offset.
  744. *
  745. * fuzzy - If this is set, then we are trying to make an allocation, and we just
  746. * want a section that has at least bytes size and comes at or after the given
  747. * offset.
  748. */
  749. static struct btrfs_free_space *
  750. tree_search_offset(struct btrfs_block_group_cache *block_group,
  751. u64 offset, int bitmap_only, int fuzzy)
  752. {
  753. struct rb_node *n = block_group->free_space_offset.rb_node;
  754. struct btrfs_free_space *entry, *prev = NULL;
  755. /* find entry that is closest to the 'offset' */
  756. while (1) {
  757. if (!n) {
  758. entry = NULL;
  759. break;
  760. }
  761. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  762. prev = entry;
  763. if (offset < entry->offset)
  764. n = n->rb_left;
  765. else if (offset > entry->offset)
  766. n = n->rb_right;
  767. else
  768. break;
  769. }
  770. if (bitmap_only) {
  771. if (!entry)
  772. return NULL;
  773. if (entry->bitmap)
  774. return entry;
  775. /*
  776. * bitmap entry and extent entry may share same offset,
  777. * in that case, bitmap entry comes after extent entry.
  778. */
  779. n = rb_next(n);
  780. if (!n)
  781. return NULL;
  782. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  783. if (entry->offset != offset)
  784. return NULL;
  785. WARN_ON(!entry->bitmap);
  786. return entry;
  787. } else if (entry) {
  788. if (entry->bitmap) {
  789. /*
  790. * if previous extent entry covers the offset,
  791. * we should return it instead of the bitmap entry
  792. */
  793. n = &entry->offset_index;
  794. while (1) {
  795. n = rb_prev(n);
  796. if (!n)
  797. break;
  798. prev = rb_entry(n, struct btrfs_free_space,
  799. offset_index);
  800. if (!prev->bitmap) {
  801. if (prev->offset + prev->bytes > offset)
  802. entry = prev;
  803. break;
  804. }
  805. }
  806. }
  807. return entry;
  808. }
  809. if (!prev)
  810. return NULL;
  811. /* find last entry before the 'offset' */
  812. entry = prev;
  813. if (entry->offset > offset) {
  814. n = rb_prev(&entry->offset_index);
  815. if (n) {
  816. entry = rb_entry(n, struct btrfs_free_space,
  817. offset_index);
  818. BUG_ON(entry->offset > offset);
  819. } else {
  820. if (fuzzy)
  821. return entry;
  822. else
  823. return NULL;
  824. }
  825. }
  826. if (entry->bitmap) {
  827. n = &entry->offset_index;
  828. while (1) {
  829. n = rb_prev(n);
  830. if (!n)
  831. break;
  832. prev = rb_entry(n, struct btrfs_free_space,
  833. offset_index);
  834. if (!prev->bitmap) {
  835. if (prev->offset + prev->bytes > offset)
  836. return prev;
  837. break;
  838. }
  839. }
  840. if (entry->offset + BITS_PER_BITMAP *
  841. block_group->sectorsize > offset)
  842. return entry;
  843. } else if (entry->offset + entry->bytes > offset)
  844. return entry;
  845. if (!fuzzy)
  846. return NULL;
  847. while (1) {
  848. if (entry->bitmap) {
  849. if (entry->offset + BITS_PER_BITMAP *
  850. block_group->sectorsize > offset)
  851. break;
  852. } else {
  853. if (entry->offset + entry->bytes > offset)
  854. break;
  855. }
  856. n = rb_next(&entry->offset_index);
  857. if (!n)
  858. return NULL;
  859. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  860. }
  861. return entry;
  862. }
  863. static void unlink_free_space(struct btrfs_block_group_cache *block_group,
  864. struct btrfs_free_space *info)
  865. {
  866. rb_erase(&info->offset_index, &block_group->free_space_offset);
  867. block_group->free_extents--;
  868. block_group->free_space -= info->bytes;
  869. }
  870. static int link_free_space(struct btrfs_block_group_cache *block_group,
  871. struct btrfs_free_space *info)
  872. {
  873. int ret = 0;
  874. BUG_ON(!info->bitmap && !info->bytes);
  875. ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
  876. &info->offset_index, (info->bitmap != NULL));
  877. if (ret)
  878. return ret;
  879. block_group->free_space += info->bytes;
  880. block_group->free_extents++;
  881. return ret;
  882. }
  883. static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
  884. {
  885. u64 max_bytes;
  886. u64 bitmap_bytes;
  887. u64 extent_bytes;
  888. u64 size = block_group->key.offset;
  889. /*
  890. * The goal is to keep the total amount of memory used per 1gb of space
  891. * at or below 32k, so we need to adjust how much memory we allow to be
  892. * used by extent based free space tracking
  893. */
  894. if (size < 1024 * 1024 * 1024)
  895. max_bytes = MAX_CACHE_BYTES_PER_GIG;
  896. else
  897. max_bytes = MAX_CACHE_BYTES_PER_GIG *
  898. div64_u64(size, 1024 * 1024 * 1024);
  899. /*
  900. * we want to account for 1 more bitmap than what we have so we can make
  901. * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
  902. * we add more bitmaps.
  903. */
  904. bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
  905. if (bitmap_bytes >= max_bytes) {
  906. block_group->extents_thresh = 0;
  907. return;
  908. }
  909. /*
  910. * we want the extent entry threshold to always be at most 1/2 the maxw
  911. * bytes we can have, or whatever is less than that.
  912. */
  913. extent_bytes = max_bytes - bitmap_bytes;
  914. extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
  915. block_group->extents_thresh =
  916. div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
  917. }
  918. static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
  919. struct btrfs_free_space *info, u64 offset,
  920. u64 bytes)
  921. {
  922. unsigned long start, end;
  923. unsigned long i;
  924. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  925. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  926. BUG_ON(end > BITS_PER_BITMAP);
  927. for (i = start; i < end; i++)
  928. clear_bit(i, info->bitmap);
  929. info->bytes -= bytes;
  930. block_group->free_space -= bytes;
  931. }
  932. static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
  933. struct btrfs_free_space *info, u64 offset,
  934. u64 bytes)
  935. {
  936. unsigned long start, end;
  937. unsigned long i;
  938. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  939. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  940. BUG_ON(end > BITS_PER_BITMAP);
  941. for (i = start; i < end; i++)
  942. set_bit(i, info->bitmap);
  943. info->bytes += bytes;
  944. block_group->free_space += bytes;
  945. }
  946. static int search_bitmap(struct btrfs_block_group_cache *block_group,
  947. struct btrfs_free_space *bitmap_info, u64 *offset,
  948. u64 *bytes)
  949. {
  950. unsigned long found_bits = 0;
  951. unsigned long bits, i;
  952. unsigned long next_zero;
  953. i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
  954. max_t(u64, *offset, bitmap_info->offset));
  955. bits = bytes_to_bits(*bytes, block_group->sectorsize);
  956. for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
  957. i < BITS_PER_BITMAP;
  958. i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
  959. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  960. BITS_PER_BITMAP, i);
  961. if ((next_zero - i) >= bits) {
  962. found_bits = next_zero - i;
  963. break;
  964. }
  965. i = next_zero;
  966. }
  967. if (found_bits) {
  968. *offset = (u64)(i * block_group->sectorsize) +
  969. bitmap_info->offset;
  970. *bytes = (u64)(found_bits) * block_group->sectorsize;
  971. return 0;
  972. }
  973. return -1;
  974. }
  975. static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
  976. *block_group, u64 *offset,
  977. u64 *bytes, int debug)
  978. {
  979. struct btrfs_free_space *entry;
  980. struct rb_node *node;
  981. int ret;
  982. if (!block_group->free_space_offset.rb_node)
  983. return NULL;
  984. entry = tree_search_offset(block_group,
  985. offset_to_bitmap(block_group, *offset),
  986. 0, 1);
  987. if (!entry)
  988. return NULL;
  989. for (node = &entry->offset_index; node; node = rb_next(node)) {
  990. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  991. if (entry->bytes < *bytes)
  992. continue;
  993. if (entry->bitmap) {
  994. ret = search_bitmap(block_group, entry, offset, bytes);
  995. if (!ret)
  996. return entry;
  997. continue;
  998. }
  999. *offset = entry->offset;
  1000. *bytes = entry->bytes;
  1001. return entry;
  1002. }
  1003. return NULL;
  1004. }
  1005. static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
  1006. struct btrfs_free_space *info, u64 offset)
  1007. {
  1008. u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
  1009. int max_bitmaps = (int)div64_u64(block_group->key.offset +
  1010. bytes_per_bg - 1, bytes_per_bg);
  1011. BUG_ON(block_group->total_bitmaps >= max_bitmaps);
  1012. info->offset = offset_to_bitmap(block_group, offset);
  1013. info->bytes = 0;
  1014. link_free_space(block_group, info);
  1015. block_group->total_bitmaps++;
  1016. recalculate_thresholds(block_group);
  1017. }
  1018. static void free_bitmap(struct btrfs_block_group_cache *block_group,
  1019. struct btrfs_free_space *bitmap_info)
  1020. {
  1021. unlink_free_space(block_group, bitmap_info);
  1022. kfree(bitmap_info->bitmap);
  1023. kfree(bitmap_info);
  1024. block_group->total_bitmaps--;
  1025. recalculate_thresholds(block_group);
  1026. }
  1027. static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
  1028. struct btrfs_free_space *bitmap_info,
  1029. u64 *offset, u64 *bytes)
  1030. {
  1031. u64 end;
  1032. u64 search_start, search_bytes;
  1033. int ret;
  1034. again:
  1035. end = bitmap_info->offset +
  1036. (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
  1037. /*
  1038. * XXX - this can go away after a few releases.
  1039. *
  1040. * since the only user of btrfs_remove_free_space is the tree logging
  1041. * stuff, and the only way to test that is under crash conditions, we
  1042. * want to have this debug stuff here just in case somethings not
  1043. * working. Search the bitmap for the space we are trying to use to
  1044. * make sure its actually there. If its not there then we need to stop
  1045. * because something has gone wrong.
  1046. */
  1047. search_start = *offset;
  1048. search_bytes = *bytes;
  1049. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1050. &search_bytes);
  1051. BUG_ON(ret < 0 || search_start != *offset);
  1052. if (*offset > bitmap_info->offset && *offset + *bytes > end) {
  1053. bitmap_clear_bits(block_group, bitmap_info, *offset,
  1054. end - *offset + 1);
  1055. *bytes -= end - *offset + 1;
  1056. *offset = end + 1;
  1057. } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
  1058. bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
  1059. *bytes = 0;
  1060. }
  1061. if (*bytes) {
  1062. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1063. if (!bitmap_info->bytes)
  1064. free_bitmap(block_group, bitmap_info);
  1065. /*
  1066. * no entry after this bitmap, but we still have bytes to
  1067. * remove, so something has gone wrong.
  1068. */
  1069. if (!next)
  1070. return -EINVAL;
  1071. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1072. offset_index);
  1073. /*
  1074. * if the next entry isn't a bitmap we need to return to let the
  1075. * extent stuff do its work.
  1076. */
  1077. if (!bitmap_info->bitmap)
  1078. return -EAGAIN;
  1079. /*
  1080. * Ok the next item is a bitmap, but it may not actually hold
  1081. * the information for the rest of this free space stuff, so
  1082. * look for it, and if we don't find it return so we can try
  1083. * everything over again.
  1084. */
  1085. search_start = *offset;
  1086. search_bytes = *bytes;
  1087. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1088. &search_bytes);
  1089. if (ret < 0 || search_start != *offset)
  1090. return -EAGAIN;
  1091. goto again;
  1092. } else if (!bitmap_info->bytes)
  1093. free_bitmap(block_group, bitmap_info);
  1094. return 0;
  1095. }
  1096. static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
  1097. struct btrfs_free_space *info)
  1098. {
  1099. struct btrfs_free_space *bitmap_info;
  1100. int added = 0;
  1101. u64 bytes, offset, end;
  1102. int ret;
  1103. /*
  1104. * If we are below the extents threshold then we can add this as an
  1105. * extent, and don't have to deal with the bitmap
  1106. */
  1107. if (block_group->free_extents < block_group->extents_thresh &&
  1108. info->bytes > block_group->sectorsize * 4)
  1109. return 0;
  1110. /*
  1111. * some block groups are so tiny they can't be enveloped by a bitmap, so
  1112. * don't even bother to create a bitmap for this
  1113. */
  1114. if (BITS_PER_BITMAP * block_group->sectorsize >
  1115. block_group->key.offset)
  1116. return 0;
  1117. bytes = info->bytes;
  1118. offset = info->offset;
  1119. again:
  1120. bitmap_info = tree_search_offset(block_group,
  1121. offset_to_bitmap(block_group, offset),
  1122. 1, 0);
  1123. if (!bitmap_info) {
  1124. BUG_ON(added);
  1125. goto new_bitmap;
  1126. }
  1127. end = bitmap_info->offset +
  1128. (u64)(BITS_PER_BITMAP * block_group->sectorsize);
  1129. if (offset >= bitmap_info->offset && offset + bytes > end) {
  1130. bitmap_set_bits(block_group, bitmap_info, offset,
  1131. end - offset);
  1132. bytes -= end - offset;
  1133. offset = end;
  1134. added = 0;
  1135. } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
  1136. bitmap_set_bits(block_group, bitmap_info, offset, bytes);
  1137. bytes = 0;
  1138. } else {
  1139. BUG();
  1140. }
  1141. if (!bytes) {
  1142. ret = 1;
  1143. goto out;
  1144. } else
  1145. goto again;
  1146. new_bitmap:
  1147. if (info && info->bitmap) {
  1148. add_new_bitmap(block_group, info, offset);
  1149. added = 1;
  1150. info = NULL;
  1151. goto again;
  1152. } else {
  1153. spin_unlock(&block_group->tree_lock);
  1154. /* no pre-allocated info, allocate a new one */
  1155. if (!info) {
  1156. info = kzalloc(sizeof(struct btrfs_free_space),
  1157. GFP_NOFS);
  1158. if (!info) {
  1159. spin_lock(&block_group->tree_lock);
  1160. ret = -ENOMEM;
  1161. goto out;
  1162. }
  1163. }
  1164. /* allocate the bitmap */
  1165. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1166. spin_lock(&block_group->tree_lock);
  1167. if (!info->bitmap) {
  1168. ret = -ENOMEM;
  1169. goto out;
  1170. }
  1171. goto again;
  1172. }
  1173. out:
  1174. if (info) {
  1175. if (info->bitmap)
  1176. kfree(info->bitmap);
  1177. kfree(info);
  1178. }
  1179. return ret;
  1180. }
  1181. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  1182. u64 offset, u64 bytes)
  1183. {
  1184. struct btrfs_free_space *right_info = NULL;
  1185. struct btrfs_free_space *left_info = NULL;
  1186. struct btrfs_free_space *info = NULL;
  1187. int ret = 0;
  1188. info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  1189. if (!info)
  1190. return -ENOMEM;
  1191. info->offset = offset;
  1192. info->bytes = bytes;
  1193. spin_lock(&block_group->tree_lock);
  1194. /*
  1195. * first we want to see if there is free space adjacent to the range we
  1196. * are adding, if there is remove that struct and add a new one to
  1197. * cover the entire range
  1198. */
  1199. right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
  1200. if (right_info && rb_prev(&right_info->offset_index))
  1201. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1202. struct btrfs_free_space, offset_index);
  1203. else
  1204. left_info = tree_search_offset(block_group, offset - 1, 0, 0);
  1205. /*
  1206. * If there was no extent directly to the left or right of this new
  1207. * extent then we know we're going to have to allocate a new extent, so
  1208. * before we do that see if we need to drop this into a bitmap
  1209. */
  1210. if ((!left_info || left_info->bitmap) &&
  1211. (!right_info || right_info->bitmap)) {
  1212. ret = insert_into_bitmap(block_group, info);
  1213. if (ret < 0) {
  1214. goto out;
  1215. } else if (ret) {
  1216. ret = 0;
  1217. goto out;
  1218. }
  1219. }
  1220. if (right_info && !right_info->bitmap) {
  1221. unlink_free_space(block_group, right_info);
  1222. info->bytes += right_info->bytes;
  1223. kfree(right_info);
  1224. }
  1225. if (left_info && !left_info->bitmap &&
  1226. left_info->offset + left_info->bytes == offset) {
  1227. unlink_free_space(block_group, left_info);
  1228. info->offset = left_info->offset;
  1229. info->bytes += left_info->bytes;
  1230. kfree(left_info);
  1231. }
  1232. ret = link_free_space(block_group, info);
  1233. if (ret)
  1234. kfree(info);
  1235. out:
  1236. spin_unlock(&block_group->tree_lock);
  1237. if (ret) {
  1238. printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
  1239. BUG_ON(ret == -EEXIST);
  1240. }
  1241. return ret;
  1242. }
  1243. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1244. u64 offset, u64 bytes)
  1245. {
  1246. struct btrfs_free_space *info;
  1247. struct btrfs_free_space *next_info = NULL;
  1248. int ret = 0;
  1249. spin_lock(&block_group->tree_lock);
  1250. again:
  1251. info = tree_search_offset(block_group, offset, 0, 0);
  1252. if (!info) {
  1253. /*
  1254. * oops didn't find an extent that matched the space we wanted
  1255. * to remove, look for a bitmap instead
  1256. */
  1257. info = tree_search_offset(block_group,
  1258. offset_to_bitmap(block_group, offset),
  1259. 1, 0);
  1260. if (!info) {
  1261. WARN_ON(1);
  1262. goto out_lock;
  1263. }
  1264. }
  1265. if (info->bytes < bytes && rb_next(&info->offset_index)) {
  1266. u64 end;
  1267. next_info = rb_entry(rb_next(&info->offset_index),
  1268. struct btrfs_free_space,
  1269. offset_index);
  1270. if (next_info->bitmap)
  1271. end = next_info->offset + BITS_PER_BITMAP *
  1272. block_group->sectorsize - 1;
  1273. else
  1274. end = next_info->offset + next_info->bytes;
  1275. if (next_info->bytes < bytes ||
  1276. next_info->offset > offset || offset > end) {
  1277. printk(KERN_CRIT "Found free space at %llu, size %llu,"
  1278. " trying to use %llu\n",
  1279. (unsigned long long)info->offset,
  1280. (unsigned long long)info->bytes,
  1281. (unsigned long long)bytes);
  1282. WARN_ON(1);
  1283. ret = -EINVAL;
  1284. goto out_lock;
  1285. }
  1286. info = next_info;
  1287. }
  1288. if (info->bytes == bytes) {
  1289. unlink_free_space(block_group, info);
  1290. if (info->bitmap) {
  1291. kfree(info->bitmap);
  1292. block_group->total_bitmaps--;
  1293. }
  1294. kfree(info);
  1295. goto out_lock;
  1296. }
  1297. if (!info->bitmap && info->offset == offset) {
  1298. unlink_free_space(block_group, info);
  1299. info->offset += bytes;
  1300. info->bytes -= bytes;
  1301. link_free_space(block_group, info);
  1302. goto out_lock;
  1303. }
  1304. if (!info->bitmap && info->offset <= offset &&
  1305. info->offset + info->bytes >= offset + bytes) {
  1306. u64 old_start = info->offset;
  1307. /*
  1308. * we're freeing space in the middle of the info,
  1309. * this can happen during tree log replay
  1310. *
  1311. * first unlink the old info and then
  1312. * insert it again after the hole we're creating
  1313. */
  1314. unlink_free_space(block_group, info);
  1315. if (offset + bytes < info->offset + info->bytes) {
  1316. u64 old_end = info->offset + info->bytes;
  1317. info->offset = offset + bytes;
  1318. info->bytes = old_end - info->offset;
  1319. ret = link_free_space(block_group, info);
  1320. WARN_ON(ret);
  1321. if (ret)
  1322. goto out_lock;
  1323. } else {
  1324. /* the hole we're creating ends at the end
  1325. * of the info struct, just free the info
  1326. */
  1327. kfree(info);
  1328. }
  1329. spin_unlock(&block_group->tree_lock);
  1330. /* step two, insert a new info struct to cover
  1331. * anything before the hole
  1332. */
  1333. ret = btrfs_add_free_space(block_group, old_start,
  1334. offset - old_start);
  1335. WARN_ON(ret);
  1336. goto out;
  1337. }
  1338. ret = remove_from_bitmap(block_group, info, &offset, &bytes);
  1339. if (ret == -EAGAIN)
  1340. goto again;
  1341. BUG_ON(ret);
  1342. out_lock:
  1343. spin_unlock(&block_group->tree_lock);
  1344. out:
  1345. return ret;
  1346. }
  1347. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  1348. u64 bytes)
  1349. {
  1350. struct btrfs_free_space *info;
  1351. struct rb_node *n;
  1352. int count = 0;
  1353. for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
  1354. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1355. if (info->bytes >= bytes)
  1356. count++;
  1357. printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
  1358. (unsigned long long)info->offset,
  1359. (unsigned long long)info->bytes,
  1360. (info->bitmap) ? "yes" : "no");
  1361. }
  1362. printk(KERN_INFO "block group has cluster?: %s\n",
  1363. list_empty(&block_group->cluster_list) ? "no" : "yes");
  1364. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  1365. "\n", count);
  1366. }
  1367. u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
  1368. {
  1369. struct btrfs_free_space *info;
  1370. struct rb_node *n;
  1371. u64 ret = 0;
  1372. for (n = rb_first(&block_group->free_space_offset); n;
  1373. n = rb_next(n)) {
  1374. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1375. ret += info->bytes;
  1376. }
  1377. return ret;
  1378. }
  1379. /*
  1380. * for a given cluster, put all of its extents back into the free
  1381. * space cache. If the block group passed doesn't match the block group
  1382. * pointed to by the cluster, someone else raced in and freed the
  1383. * cluster already. In that case, we just return without changing anything
  1384. */
  1385. static int
  1386. __btrfs_return_cluster_to_free_space(
  1387. struct btrfs_block_group_cache *block_group,
  1388. struct btrfs_free_cluster *cluster)
  1389. {
  1390. struct btrfs_free_space *entry;
  1391. struct rb_node *node;
  1392. bool bitmap;
  1393. spin_lock(&cluster->lock);
  1394. if (cluster->block_group != block_group)
  1395. goto out;
  1396. bitmap = cluster->points_to_bitmap;
  1397. cluster->block_group = NULL;
  1398. cluster->window_start = 0;
  1399. list_del_init(&cluster->block_group_list);
  1400. cluster->points_to_bitmap = false;
  1401. if (bitmap)
  1402. goto out;
  1403. node = rb_first(&cluster->root);
  1404. while (node) {
  1405. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1406. node = rb_next(&entry->offset_index);
  1407. rb_erase(&entry->offset_index, &cluster->root);
  1408. BUG_ON(entry->bitmap);
  1409. tree_insert_offset(&block_group->free_space_offset,
  1410. entry->offset, &entry->offset_index, 0);
  1411. }
  1412. cluster->root = RB_ROOT;
  1413. out:
  1414. spin_unlock(&cluster->lock);
  1415. btrfs_put_block_group(block_group);
  1416. return 0;
  1417. }
  1418. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  1419. {
  1420. struct btrfs_free_space *info;
  1421. struct rb_node *node;
  1422. struct btrfs_free_cluster *cluster;
  1423. struct list_head *head;
  1424. spin_lock(&block_group->tree_lock);
  1425. while ((head = block_group->cluster_list.next) !=
  1426. &block_group->cluster_list) {
  1427. cluster = list_entry(head, struct btrfs_free_cluster,
  1428. block_group_list);
  1429. WARN_ON(cluster->block_group != block_group);
  1430. __btrfs_return_cluster_to_free_space(block_group, cluster);
  1431. if (need_resched()) {
  1432. spin_unlock(&block_group->tree_lock);
  1433. cond_resched();
  1434. spin_lock(&block_group->tree_lock);
  1435. }
  1436. }
  1437. while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
  1438. info = rb_entry(node, struct btrfs_free_space, offset_index);
  1439. unlink_free_space(block_group, info);
  1440. if (info->bitmap)
  1441. kfree(info->bitmap);
  1442. kfree(info);
  1443. if (need_resched()) {
  1444. spin_unlock(&block_group->tree_lock);
  1445. cond_resched();
  1446. spin_lock(&block_group->tree_lock);
  1447. }
  1448. }
  1449. spin_unlock(&block_group->tree_lock);
  1450. }
  1451. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  1452. u64 offset, u64 bytes, u64 empty_size)
  1453. {
  1454. struct btrfs_free_space *entry = NULL;
  1455. u64 bytes_search = bytes + empty_size;
  1456. u64 ret = 0;
  1457. spin_lock(&block_group->tree_lock);
  1458. entry = find_free_space(block_group, &offset, &bytes_search, 0);
  1459. if (!entry)
  1460. goto out;
  1461. ret = offset;
  1462. if (entry->bitmap) {
  1463. bitmap_clear_bits(block_group, entry, offset, bytes);
  1464. if (!entry->bytes)
  1465. free_bitmap(block_group, entry);
  1466. } else {
  1467. unlink_free_space(block_group, entry);
  1468. entry->offset += bytes;
  1469. entry->bytes -= bytes;
  1470. if (!entry->bytes)
  1471. kfree(entry);
  1472. else
  1473. link_free_space(block_group, entry);
  1474. }
  1475. out:
  1476. spin_unlock(&block_group->tree_lock);
  1477. return ret;
  1478. }
  1479. /*
  1480. * given a cluster, put all of its extents back into the free space
  1481. * cache. If a block group is passed, this function will only free
  1482. * a cluster that belongs to the passed block group.
  1483. *
  1484. * Otherwise, it'll get a reference on the block group pointed to by the
  1485. * cluster and remove the cluster from it.
  1486. */
  1487. int btrfs_return_cluster_to_free_space(
  1488. struct btrfs_block_group_cache *block_group,
  1489. struct btrfs_free_cluster *cluster)
  1490. {
  1491. int ret;
  1492. /* first, get a safe pointer to the block group */
  1493. spin_lock(&cluster->lock);
  1494. if (!block_group) {
  1495. block_group = cluster->block_group;
  1496. if (!block_group) {
  1497. spin_unlock(&cluster->lock);
  1498. return 0;
  1499. }
  1500. } else if (cluster->block_group != block_group) {
  1501. /* someone else has already freed it don't redo their work */
  1502. spin_unlock(&cluster->lock);
  1503. return 0;
  1504. }
  1505. atomic_inc(&block_group->count);
  1506. spin_unlock(&cluster->lock);
  1507. /* now return any extents the cluster had on it */
  1508. spin_lock(&block_group->tree_lock);
  1509. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  1510. spin_unlock(&block_group->tree_lock);
  1511. /* finally drop our ref */
  1512. btrfs_put_block_group(block_group);
  1513. return ret;
  1514. }
  1515. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  1516. struct btrfs_free_cluster *cluster,
  1517. u64 bytes, u64 min_start)
  1518. {
  1519. struct btrfs_free_space *entry;
  1520. int err;
  1521. u64 search_start = cluster->window_start;
  1522. u64 search_bytes = bytes;
  1523. u64 ret = 0;
  1524. spin_lock(&block_group->tree_lock);
  1525. spin_lock(&cluster->lock);
  1526. if (!cluster->points_to_bitmap)
  1527. goto out;
  1528. if (cluster->block_group != block_group)
  1529. goto out;
  1530. /*
  1531. * search_start is the beginning of the bitmap, but at some point it may
  1532. * be a good idea to point to the actual start of the free area in the
  1533. * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
  1534. * to 1 to make sure we get the bitmap entry
  1535. */
  1536. entry = tree_search_offset(block_group,
  1537. offset_to_bitmap(block_group, search_start),
  1538. 1, 0);
  1539. if (!entry || !entry->bitmap)
  1540. goto out;
  1541. search_start = min_start;
  1542. search_bytes = bytes;
  1543. err = search_bitmap(block_group, entry, &search_start,
  1544. &search_bytes);
  1545. if (err)
  1546. goto out;
  1547. ret = search_start;
  1548. bitmap_clear_bits(block_group, entry, ret, bytes);
  1549. out:
  1550. spin_unlock(&cluster->lock);
  1551. spin_unlock(&block_group->tree_lock);
  1552. return ret;
  1553. }
  1554. /*
  1555. * given a cluster, try to allocate 'bytes' from it, returns 0
  1556. * if it couldn't find anything suitably large, or a logical disk offset
  1557. * if things worked out
  1558. */
  1559. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  1560. struct btrfs_free_cluster *cluster, u64 bytes,
  1561. u64 min_start)
  1562. {
  1563. struct btrfs_free_space *entry = NULL;
  1564. struct rb_node *node;
  1565. u64 ret = 0;
  1566. if (cluster->points_to_bitmap)
  1567. return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
  1568. min_start);
  1569. spin_lock(&cluster->lock);
  1570. if (bytes > cluster->max_size)
  1571. goto out;
  1572. if (cluster->block_group != block_group)
  1573. goto out;
  1574. node = rb_first(&cluster->root);
  1575. if (!node)
  1576. goto out;
  1577. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1578. while(1) {
  1579. if (entry->bytes < bytes || entry->offset < min_start) {
  1580. struct rb_node *node;
  1581. node = rb_next(&entry->offset_index);
  1582. if (!node)
  1583. break;
  1584. entry = rb_entry(node, struct btrfs_free_space,
  1585. offset_index);
  1586. continue;
  1587. }
  1588. ret = entry->offset;
  1589. entry->offset += bytes;
  1590. entry->bytes -= bytes;
  1591. if (entry->bytes == 0) {
  1592. rb_erase(&entry->offset_index, &cluster->root);
  1593. kfree(entry);
  1594. }
  1595. break;
  1596. }
  1597. out:
  1598. spin_unlock(&cluster->lock);
  1599. return ret;
  1600. }
  1601. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  1602. struct btrfs_free_space *entry,
  1603. struct btrfs_free_cluster *cluster,
  1604. u64 offset, u64 bytes, u64 min_bytes)
  1605. {
  1606. unsigned long next_zero;
  1607. unsigned long i;
  1608. unsigned long search_bits;
  1609. unsigned long total_bits;
  1610. unsigned long found_bits;
  1611. unsigned long start = 0;
  1612. unsigned long total_found = 0;
  1613. bool found = false;
  1614. i = offset_to_bit(entry->offset, block_group->sectorsize,
  1615. max_t(u64, offset, entry->offset));
  1616. search_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
  1617. total_bits = bytes_to_bits(bytes, block_group->sectorsize);
  1618. again:
  1619. found_bits = 0;
  1620. for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
  1621. i < BITS_PER_BITMAP;
  1622. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
  1623. next_zero = find_next_zero_bit(entry->bitmap,
  1624. BITS_PER_BITMAP, i);
  1625. if (next_zero - i >= search_bits) {
  1626. found_bits = next_zero - i;
  1627. break;
  1628. }
  1629. i = next_zero;
  1630. }
  1631. if (!found_bits)
  1632. return -1;
  1633. if (!found) {
  1634. start = i;
  1635. found = true;
  1636. }
  1637. total_found += found_bits;
  1638. if (cluster->max_size < found_bits * block_group->sectorsize)
  1639. cluster->max_size = found_bits * block_group->sectorsize;
  1640. if (total_found < total_bits) {
  1641. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
  1642. if (i - start > total_bits * 2) {
  1643. total_found = 0;
  1644. cluster->max_size = 0;
  1645. found = false;
  1646. }
  1647. goto again;
  1648. }
  1649. cluster->window_start = start * block_group->sectorsize +
  1650. entry->offset;
  1651. cluster->points_to_bitmap = true;
  1652. return 0;
  1653. }
  1654. /*
  1655. * here we try to find a cluster of blocks in a block group. The goal
  1656. * is to find at least bytes free and up to empty_size + bytes free.
  1657. * We might not find them all in one contiguous area.
  1658. *
  1659. * returns zero and sets up cluster if things worked out, otherwise
  1660. * it returns -enospc
  1661. */
  1662. int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
  1663. struct btrfs_root *root,
  1664. struct btrfs_block_group_cache *block_group,
  1665. struct btrfs_free_cluster *cluster,
  1666. u64 offset, u64 bytes, u64 empty_size)
  1667. {
  1668. struct btrfs_free_space *entry = NULL;
  1669. struct rb_node *node;
  1670. struct btrfs_free_space *next;
  1671. struct btrfs_free_space *last = NULL;
  1672. u64 min_bytes;
  1673. u64 window_start;
  1674. u64 window_free;
  1675. u64 max_extent = 0;
  1676. bool found_bitmap = false;
  1677. int ret;
  1678. /* for metadata, allow allocates with more holes */
  1679. if (btrfs_test_opt(root, SSD_SPREAD)) {
  1680. min_bytes = bytes + empty_size;
  1681. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1682. /*
  1683. * we want to do larger allocations when we are
  1684. * flushing out the delayed refs, it helps prevent
  1685. * making more work as we go along.
  1686. */
  1687. if (trans->transaction->delayed_refs.flushing)
  1688. min_bytes = max(bytes, (bytes + empty_size) >> 1);
  1689. else
  1690. min_bytes = max(bytes, (bytes + empty_size) >> 4);
  1691. } else
  1692. min_bytes = max(bytes, (bytes + empty_size) >> 2);
  1693. spin_lock(&block_group->tree_lock);
  1694. spin_lock(&cluster->lock);
  1695. /* someone already found a cluster, hooray */
  1696. if (cluster->block_group) {
  1697. ret = 0;
  1698. goto out;
  1699. }
  1700. again:
  1701. entry = tree_search_offset(block_group, offset, found_bitmap, 1);
  1702. if (!entry) {
  1703. ret = -ENOSPC;
  1704. goto out;
  1705. }
  1706. /*
  1707. * If found_bitmap is true, we exhausted our search for extent entries,
  1708. * and we just want to search all of the bitmaps that we can find, and
  1709. * ignore any extent entries we find.
  1710. */
  1711. while (entry->bitmap || found_bitmap ||
  1712. (!entry->bitmap && entry->bytes < min_bytes)) {
  1713. struct rb_node *node = rb_next(&entry->offset_index);
  1714. if (entry->bitmap && entry->bytes > bytes + empty_size) {
  1715. ret = btrfs_bitmap_cluster(block_group, entry, cluster,
  1716. offset, bytes + empty_size,
  1717. min_bytes);
  1718. if (!ret)
  1719. goto got_it;
  1720. }
  1721. if (!node) {
  1722. ret = -ENOSPC;
  1723. goto out;
  1724. }
  1725. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1726. }
  1727. /*
  1728. * We already searched all the extent entries from the passed in offset
  1729. * to the end and didn't find enough space for the cluster, and we also
  1730. * didn't find any bitmaps that met our criteria, just go ahead and exit
  1731. */
  1732. if (found_bitmap) {
  1733. ret = -ENOSPC;
  1734. goto out;
  1735. }
  1736. cluster->points_to_bitmap = false;
  1737. window_start = entry->offset;
  1738. window_free = entry->bytes;
  1739. last = entry;
  1740. max_extent = entry->bytes;
  1741. while (1) {
  1742. /* out window is just right, lets fill it */
  1743. if (window_free >= bytes + empty_size)
  1744. break;
  1745. node = rb_next(&last->offset_index);
  1746. if (!node) {
  1747. if (found_bitmap)
  1748. goto again;
  1749. ret = -ENOSPC;
  1750. goto out;
  1751. }
  1752. next = rb_entry(node, struct btrfs_free_space, offset_index);
  1753. /*
  1754. * we found a bitmap, so if this search doesn't result in a
  1755. * cluster, we know to go and search again for the bitmaps and
  1756. * start looking for space there
  1757. */
  1758. if (next->bitmap) {
  1759. if (!found_bitmap)
  1760. offset = next->offset;
  1761. found_bitmap = true;
  1762. last = next;
  1763. continue;
  1764. }
  1765. /*
  1766. * we haven't filled the empty size and the window is
  1767. * very large. reset and try again
  1768. */
  1769. if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
  1770. next->offset - window_start > (bytes + empty_size) * 2) {
  1771. entry = next;
  1772. window_start = entry->offset;
  1773. window_free = entry->bytes;
  1774. last = entry;
  1775. max_extent = entry->bytes;
  1776. } else {
  1777. last = next;
  1778. window_free += next->bytes;
  1779. if (entry->bytes > max_extent)
  1780. max_extent = entry->bytes;
  1781. }
  1782. }
  1783. cluster->window_start = entry->offset;
  1784. /*
  1785. * now we've found our entries, pull them out of the free space
  1786. * cache and put them into the cluster rbtree
  1787. *
  1788. * The cluster includes an rbtree, but only uses the offset index
  1789. * of each free space cache entry.
  1790. */
  1791. while (1) {
  1792. node = rb_next(&entry->offset_index);
  1793. if (entry->bitmap && node) {
  1794. entry = rb_entry(node, struct btrfs_free_space,
  1795. offset_index);
  1796. continue;
  1797. } else if (entry->bitmap && !node) {
  1798. break;
  1799. }
  1800. rb_erase(&entry->offset_index, &block_group->free_space_offset);
  1801. ret = tree_insert_offset(&cluster->root, entry->offset,
  1802. &entry->offset_index, 0);
  1803. BUG_ON(ret);
  1804. if (!node || entry == last)
  1805. break;
  1806. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1807. }
  1808. cluster->max_size = max_extent;
  1809. got_it:
  1810. ret = 0;
  1811. atomic_inc(&block_group->count);
  1812. list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
  1813. cluster->block_group = block_group;
  1814. out:
  1815. spin_unlock(&cluster->lock);
  1816. spin_unlock(&block_group->tree_lock);
  1817. return ret;
  1818. }
  1819. /*
  1820. * simple code to zero out a cluster
  1821. */
  1822. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  1823. {
  1824. spin_lock_init(&cluster->lock);
  1825. spin_lock_init(&cluster->refill_lock);
  1826. cluster->root = RB_ROOT;
  1827. cluster->max_size = 0;
  1828. cluster->points_to_bitmap = false;
  1829. INIT_LIST_HEAD(&cluster->block_group_list);
  1830. cluster->block_group = NULL;
  1831. }