free-space-cache.c 87 KB

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