free-space-cache.c 93 KB

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