free-space-cache.c 68 KB

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