free-space-cache.c 69 KB

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