free-space-cache.c 92 KB

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