free-space-cache.c 74 KB

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