free-space-cache.c 72 KB

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