free-space-cache.c 63 KB

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