free-space-cache.c 77 KB

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