free-space-cache.c 94 KB

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