free-space-cache.c 92 KB

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