free-space-cache.c 59 KB

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