free-space-cache.c 65 KB

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