free-space-cache.c 70 KB

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