free-space-cache.c 70 KB

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