free-space-cache.c 72 KB

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