free-space-cache.c 84 KB

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