free-space-cache.c 92 KB

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