free-space-cache.c 77 KB

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