free-space-cache.c 94 KB

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