free-space-cache.c 55 KB

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