free-space-cache.c 72 KB

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