free-space-cache.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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. #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
  27. #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
  28. static void recalculate_thresholds(struct btrfs_block_group_cache
  29. *block_group);
  30. static int link_free_space(struct btrfs_block_group_cache *block_group,
  31. struct btrfs_free_space *info);
  32. struct inode *lookup_free_space_inode(struct btrfs_root *root,
  33. struct btrfs_block_group_cache
  34. *block_group, struct btrfs_path *path)
  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. spin_lock(&block_group->lock);
  44. if (block_group->inode)
  45. inode = igrab(block_group->inode);
  46. spin_unlock(&block_group->lock);
  47. if (inode)
  48. return inode;
  49. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  50. key.offset = block_group->key.objectid;
  51. key.type = 0;
  52. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  53. if (ret < 0)
  54. return ERR_PTR(ret);
  55. if (ret > 0) {
  56. btrfs_release_path(root, path);
  57. return ERR_PTR(-ENOENT);
  58. }
  59. leaf = path->nodes[0];
  60. header = btrfs_item_ptr(leaf, path->slots[0],
  61. struct btrfs_free_space_header);
  62. btrfs_free_space_key(leaf, header, &disk_key);
  63. btrfs_disk_key_to_cpu(&location, &disk_key);
  64. btrfs_release_path(root, path);
  65. inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
  66. if (!inode)
  67. return ERR_PTR(-ENOENT);
  68. if (IS_ERR(inode))
  69. return inode;
  70. if (is_bad_inode(inode)) {
  71. iput(inode);
  72. return ERR_PTR(-ENOENT);
  73. }
  74. spin_lock(&block_group->lock);
  75. if (!root->fs_info->closing) {
  76. block_group->inode = igrab(inode);
  77. block_group->iref = 1;
  78. }
  79. spin_unlock(&block_group->lock);
  80. return inode;
  81. }
  82. int create_free_space_inode(struct btrfs_root *root,
  83. struct btrfs_trans_handle *trans,
  84. struct btrfs_block_group_cache *block_group,
  85. struct btrfs_path *path)
  86. {
  87. struct btrfs_key key;
  88. struct btrfs_disk_key disk_key;
  89. struct btrfs_free_space_header *header;
  90. struct btrfs_inode_item *inode_item;
  91. struct extent_buffer *leaf;
  92. u64 objectid;
  93. int ret;
  94. ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
  95. if (ret < 0)
  96. return ret;
  97. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  98. if (ret)
  99. return ret;
  100. leaf = path->nodes[0];
  101. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  102. struct btrfs_inode_item);
  103. btrfs_item_key(leaf, &disk_key, path->slots[0]);
  104. memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
  105. sizeof(*inode_item));
  106. btrfs_set_inode_generation(leaf, inode_item, trans->transid);
  107. btrfs_set_inode_size(leaf, inode_item, 0);
  108. btrfs_set_inode_nbytes(leaf, inode_item, 0);
  109. btrfs_set_inode_uid(leaf, inode_item, 0);
  110. btrfs_set_inode_gid(leaf, inode_item, 0);
  111. btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
  112. btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
  113. BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
  114. btrfs_set_inode_nlink(leaf, inode_item, 1);
  115. btrfs_set_inode_transid(leaf, inode_item, trans->transid);
  116. btrfs_set_inode_block_group(leaf, inode_item,
  117. block_group->key.objectid);
  118. btrfs_mark_buffer_dirty(leaf);
  119. btrfs_release_path(root, path);
  120. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  121. key.offset = block_group->key.objectid;
  122. key.type = 0;
  123. ret = btrfs_insert_empty_item(trans, root, path, &key,
  124. sizeof(struct btrfs_free_space_header));
  125. if (ret < 0) {
  126. btrfs_release_path(root, path);
  127. return ret;
  128. }
  129. leaf = path->nodes[0];
  130. header = btrfs_item_ptr(leaf, path->slots[0],
  131. struct btrfs_free_space_header);
  132. memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
  133. btrfs_set_free_space_key(leaf, header, &disk_key);
  134. btrfs_mark_buffer_dirty(leaf);
  135. btrfs_release_path(root, path);
  136. return 0;
  137. }
  138. int btrfs_truncate_free_space_cache(struct btrfs_root *root,
  139. struct btrfs_trans_handle *trans,
  140. struct btrfs_path *path,
  141. struct inode *inode)
  142. {
  143. loff_t oldsize;
  144. int ret = 0;
  145. trans->block_rsv = root->orphan_block_rsv;
  146. ret = btrfs_block_rsv_check(trans, root,
  147. root->orphan_block_rsv,
  148. 0, 5);
  149. if (ret)
  150. return ret;
  151. oldsize = i_size_read(inode);
  152. btrfs_i_size_write(inode, 0);
  153. truncate_pagecache(inode, oldsize, 0);
  154. /*
  155. * We don't need an orphan item because truncating the free space cache
  156. * will never be split across transactions.
  157. */
  158. ret = btrfs_truncate_inode_items(trans, root, inode,
  159. 0, BTRFS_EXTENT_DATA_KEY);
  160. if (ret) {
  161. WARN_ON(1);
  162. return ret;
  163. }
  164. return btrfs_update_inode(trans, root, inode);
  165. }
  166. static int readahead_cache(struct inode *inode)
  167. {
  168. struct file_ra_state *ra;
  169. unsigned long last_index;
  170. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  171. if (!ra)
  172. return -ENOMEM;
  173. file_ra_state_init(ra, inode->i_mapping);
  174. last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  175. page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
  176. kfree(ra);
  177. return 0;
  178. }
  179. int load_free_space_cache(struct btrfs_fs_info *fs_info,
  180. struct btrfs_block_group_cache *block_group)
  181. {
  182. struct btrfs_root *root = fs_info->tree_root;
  183. struct inode *inode;
  184. struct btrfs_free_space_header *header;
  185. struct extent_buffer *leaf;
  186. struct page *page;
  187. struct btrfs_path *path;
  188. u32 *checksums = NULL, *crc;
  189. char *disk_crcs = NULL;
  190. struct btrfs_key key;
  191. struct list_head bitmaps;
  192. u64 num_entries;
  193. u64 num_bitmaps;
  194. u64 generation;
  195. u32 cur_crc = ~(u32)0;
  196. pgoff_t index = 0;
  197. unsigned long first_page_offset;
  198. int num_checksums;
  199. int ret = 0;
  200. /*
  201. * If we're unmounting then just return, since this does a search on the
  202. * normal root and not the commit root and we could deadlock.
  203. */
  204. smp_mb();
  205. if (fs_info->closing)
  206. return 0;
  207. /*
  208. * If this block group has been marked to be cleared for one reason or
  209. * another then we can't trust the on disk cache, so just return.
  210. */
  211. spin_lock(&block_group->lock);
  212. if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
  213. spin_unlock(&block_group->lock);
  214. return 0;
  215. }
  216. spin_unlock(&block_group->lock);
  217. INIT_LIST_HEAD(&bitmaps);
  218. path = btrfs_alloc_path();
  219. if (!path)
  220. return 0;
  221. inode = lookup_free_space_inode(root, block_group, path);
  222. if (IS_ERR(inode)) {
  223. btrfs_free_path(path);
  224. return 0;
  225. }
  226. /* Nothing in the space cache, goodbye */
  227. if (!i_size_read(inode)) {
  228. btrfs_free_path(path);
  229. goto out;
  230. }
  231. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  232. key.offset = block_group->key.objectid;
  233. key.type = 0;
  234. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  235. if (ret) {
  236. btrfs_free_path(path);
  237. goto out;
  238. }
  239. leaf = path->nodes[0];
  240. header = btrfs_item_ptr(leaf, path->slots[0],
  241. struct btrfs_free_space_header);
  242. num_entries = btrfs_free_space_entries(leaf, header);
  243. num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
  244. generation = btrfs_free_space_generation(leaf, header);
  245. btrfs_free_path(path);
  246. if (BTRFS_I(inode)->generation != generation) {
  247. printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
  248. " not match free space cache generation (%llu) for "
  249. "block group %llu\n",
  250. (unsigned long long)BTRFS_I(inode)->generation,
  251. (unsigned long long)generation,
  252. (unsigned long long)block_group->key.objectid);
  253. goto free_cache;
  254. }
  255. if (!num_entries)
  256. goto out;
  257. /* Setup everything for doing checksumming */
  258. num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
  259. checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
  260. if (!checksums)
  261. goto out;
  262. first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
  263. disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
  264. if (!disk_crcs)
  265. goto out;
  266. ret = readahead_cache(inode);
  267. if (ret) {
  268. ret = 0;
  269. goto out;
  270. }
  271. while (1) {
  272. struct btrfs_free_space_entry *entry;
  273. struct btrfs_free_space *e;
  274. void *addr;
  275. unsigned long offset = 0;
  276. unsigned long start_offset = 0;
  277. int need_loop = 0;
  278. if (!num_entries && !num_bitmaps)
  279. break;
  280. if (index == 0) {
  281. start_offset = first_page_offset;
  282. offset = start_offset;
  283. }
  284. page = grab_cache_page(inode->i_mapping, index);
  285. if (!page) {
  286. ret = 0;
  287. goto free_cache;
  288. }
  289. if (!PageUptodate(page)) {
  290. btrfs_readpage(NULL, page);
  291. lock_page(page);
  292. if (!PageUptodate(page)) {
  293. unlock_page(page);
  294. page_cache_release(page);
  295. printk(KERN_ERR "btrfs: error reading free "
  296. "space cache: %llu\n",
  297. (unsigned long long)
  298. block_group->key.objectid);
  299. goto free_cache;
  300. }
  301. }
  302. addr = kmap(page);
  303. if (index == 0) {
  304. u64 *gen;
  305. memcpy(disk_crcs, addr, first_page_offset);
  306. gen = addr + (sizeof(u32) * num_checksums);
  307. if (*gen != BTRFS_I(inode)->generation) {
  308. printk(KERN_ERR "btrfs: space cache generation"
  309. " (%llu) does not match inode (%llu) "
  310. "for block group %llu\n",
  311. (unsigned long long)*gen,
  312. (unsigned long long)
  313. BTRFS_I(inode)->generation,
  314. (unsigned long long)
  315. block_group->key.objectid);
  316. kunmap(page);
  317. unlock_page(page);
  318. page_cache_release(page);
  319. goto free_cache;
  320. }
  321. crc = (u32 *)disk_crcs;
  322. }
  323. entry = addr + start_offset;
  324. /* First lets check our crc before we do anything fun */
  325. cur_crc = ~(u32)0;
  326. cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
  327. PAGE_CACHE_SIZE - start_offset);
  328. btrfs_csum_final(cur_crc, (char *)&cur_crc);
  329. if (cur_crc != *crc) {
  330. printk(KERN_ERR "btrfs: crc mismatch for page %lu in "
  331. "block group %llu\n", index,
  332. (unsigned long long)block_group->key.objectid);
  333. kunmap(page);
  334. unlock_page(page);
  335. page_cache_release(page);
  336. goto free_cache;
  337. }
  338. crc++;
  339. while (1) {
  340. if (!num_entries)
  341. break;
  342. need_loop = 1;
  343. e = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  344. if (!e) {
  345. kunmap(page);
  346. unlock_page(page);
  347. page_cache_release(page);
  348. goto free_cache;
  349. }
  350. e->offset = le64_to_cpu(entry->offset);
  351. e->bytes = le64_to_cpu(entry->bytes);
  352. if (!e->bytes) {
  353. kunmap(page);
  354. kfree(e);
  355. unlock_page(page);
  356. page_cache_release(page);
  357. goto free_cache;
  358. }
  359. if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
  360. spin_lock(&block_group->tree_lock);
  361. ret = link_free_space(block_group, e);
  362. spin_unlock(&block_group->tree_lock);
  363. BUG_ON(ret);
  364. } else {
  365. e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  366. if (!e->bitmap) {
  367. kunmap(page);
  368. kfree(e);
  369. unlock_page(page);
  370. page_cache_release(page);
  371. goto free_cache;
  372. }
  373. spin_lock(&block_group->tree_lock);
  374. ret = link_free_space(block_group, e);
  375. block_group->total_bitmaps++;
  376. recalculate_thresholds(block_group);
  377. spin_unlock(&block_group->tree_lock);
  378. list_add_tail(&e->list, &bitmaps);
  379. }
  380. num_entries--;
  381. offset += sizeof(struct btrfs_free_space_entry);
  382. if (offset + sizeof(struct btrfs_free_space_entry) >=
  383. PAGE_CACHE_SIZE)
  384. break;
  385. entry++;
  386. }
  387. /*
  388. * We read an entry out of this page, we need to move on to the
  389. * next page.
  390. */
  391. if (need_loop) {
  392. kunmap(page);
  393. goto next;
  394. }
  395. /*
  396. * We add the bitmaps at the end of the entries in order that
  397. * the bitmap entries are added to the cache.
  398. */
  399. e = list_entry(bitmaps.next, struct btrfs_free_space, list);
  400. list_del_init(&e->list);
  401. memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
  402. kunmap(page);
  403. num_bitmaps--;
  404. next:
  405. unlock_page(page);
  406. page_cache_release(page);
  407. index++;
  408. }
  409. ret = 1;
  410. out:
  411. kfree(checksums);
  412. kfree(disk_crcs);
  413. iput(inode);
  414. return ret;
  415. free_cache:
  416. /* This cache is bogus, make sure it gets cleared */
  417. spin_lock(&block_group->lock);
  418. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  419. spin_unlock(&block_group->lock);
  420. btrfs_remove_free_space_cache(block_group);
  421. goto out;
  422. }
  423. int btrfs_write_out_cache(struct btrfs_root *root,
  424. struct btrfs_trans_handle *trans,
  425. struct btrfs_block_group_cache *block_group,
  426. struct btrfs_path *path)
  427. {
  428. struct btrfs_free_space_header *header;
  429. struct extent_buffer *leaf;
  430. struct inode *inode;
  431. struct rb_node *node;
  432. struct list_head *pos, *n;
  433. struct page *page;
  434. struct extent_state *cached_state = NULL;
  435. struct list_head bitmap_list;
  436. struct btrfs_key key;
  437. u64 bytes = 0;
  438. u32 *crc, *checksums;
  439. pgoff_t index = 0, last_index = 0;
  440. unsigned long first_page_offset;
  441. int num_checksums;
  442. int entries = 0;
  443. int bitmaps = 0;
  444. int ret = 0;
  445. root = root->fs_info->tree_root;
  446. INIT_LIST_HEAD(&bitmap_list);
  447. spin_lock(&block_group->lock);
  448. if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
  449. spin_unlock(&block_group->lock);
  450. return 0;
  451. }
  452. spin_unlock(&block_group->lock);
  453. inode = lookup_free_space_inode(root, block_group, path);
  454. if (IS_ERR(inode))
  455. return 0;
  456. if (!i_size_read(inode)) {
  457. iput(inode);
  458. return 0;
  459. }
  460. node = rb_first(&block_group->free_space_offset);
  461. if (!node) {
  462. iput(inode);
  463. return 0;
  464. }
  465. last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  466. filemap_write_and_wait(inode->i_mapping);
  467. btrfs_wait_ordered_range(inode, inode->i_size &
  468. ~(root->sectorsize - 1), (u64)-1);
  469. /* We need a checksum per page. */
  470. num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
  471. crc = checksums = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
  472. if (!crc) {
  473. iput(inode);
  474. return 0;
  475. }
  476. /* Since the first page has all of our checksums and our generation we
  477. * need to calculate the offset into the page that we can start writing
  478. * our entries.
  479. */
  480. first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
  481. /*
  482. * Lock all pages first so we can lock the extent safely.
  483. *
  484. * NOTE: Because we hold the ref the entire time we're going to write to
  485. * the page find_get_page should never fail, so we don't do a check
  486. * after find_get_page at this point. Just putting this here so people
  487. * know and don't freak out.
  488. */
  489. while (index <= last_index) {
  490. page = grab_cache_page(inode->i_mapping, index);
  491. if (!page) {
  492. pgoff_t i = 0;
  493. while (i < index) {
  494. page = find_get_page(inode->i_mapping, i);
  495. unlock_page(page);
  496. page_cache_release(page);
  497. page_cache_release(page);
  498. i++;
  499. }
  500. goto out_free;
  501. }
  502. index++;
  503. }
  504. index = 0;
  505. lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  506. 0, &cached_state, GFP_NOFS);
  507. /* Write out the extent entries */
  508. do {
  509. struct btrfs_free_space_entry *entry;
  510. void *addr;
  511. unsigned long offset = 0;
  512. unsigned long start_offset = 0;
  513. if (index == 0) {
  514. start_offset = first_page_offset;
  515. offset = start_offset;
  516. }
  517. page = find_get_page(inode->i_mapping, index);
  518. addr = kmap(page);
  519. entry = addr + start_offset;
  520. memset(addr, 0, PAGE_CACHE_SIZE);
  521. while (1) {
  522. struct btrfs_free_space *e;
  523. e = rb_entry(node, struct btrfs_free_space, offset_index);
  524. entries++;
  525. entry->offset = cpu_to_le64(e->offset);
  526. entry->bytes = cpu_to_le64(e->bytes);
  527. if (e->bitmap) {
  528. entry->type = BTRFS_FREE_SPACE_BITMAP;
  529. list_add_tail(&e->list, &bitmap_list);
  530. bitmaps++;
  531. } else {
  532. entry->type = BTRFS_FREE_SPACE_EXTENT;
  533. }
  534. node = rb_next(node);
  535. if (!node)
  536. break;
  537. offset += sizeof(struct btrfs_free_space_entry);
  538. if (offset + sizeof(struct btrfs_free_space_entry) >=
  539. PAGE_CACHE_SIZE)
  540. break;
  541. entry++;
  542. }
  543. *crc = ~(u32)0;
  544. *crc = btrfs_csum_data(root, addr + start_offset, *crc,
  545. PAGE_CACHE_SIZE - start_offset);
  546. kunmap(page);
  547. btrfs_csum_final(*crc, (char *)crc);
  548. crc++;
  549. bytes += PAGE_CACHE_SIZE;
  550. ClearPageChecked(page);
  551. set_page_extent_mapped(page);
  552. SetPageUptodate(page);
  553. set_page_dirty(page);
  554. /*
  555. * We need to release our reference we got for grab_cache_page,
  556. * except for the first page which will hold our checksums, we
  557. * do that below.
  558. */
  559. if (index != 0) {
  560. unlock_page(page);
  561. page_cache_release(page);
  562. }
  563. page_cache_release(page);
  564. index++;
  565. } while (node);
  566. /* Write out the bitmaps */
  567. list_for_each_safe(pos, n, &bitmap_list) {
  568. void *addr;
  569. struct btrfs_free_space *entry =
  570. list_entry(pos, struct btrfs_free_space, list);
  571. page = find_get_page(inode->i_mapping, index);
  572. addr = kmap(page);
  573. memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
  574. *crc = ~(u32)0;
  575. *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
  576. kunmap(page);
  577. btrfs_csum_final(*crc, (char *)crc);
  578. crc++;
  579. bytes += PAGE_CACHE_SIZE;
  580. ClearPageChecked(page);
  581. set_page_extent_mapped(page);
  582. SetPageUptodate(page);
  583. set_page_dirty(page);
  584. unlock_page(page);
  585. page_cache_release(page);
  586. page_cache_release(page);
  587. list_del_init(&entry->list);
  588. index++;
  589. }
  590. /* Zero out the rest of the pages just to make sure */
  591. while (index <= last_index) {
  592. void *addr;
  593. page = find_get_page(inode->i_mapping, index);
  594. addr = kmap(page);
  595. memset(addr, 0, PAGE_CACHE_SIZE);
  596. kunmap(page);
  597. ClearPageChecked(page);
  598. set_page_extent_mapped(page);
  599. SetPageUptodate(page);
  600. set_page_dirty(page);
  601. unlock_page(page);
  602. page_cache_release(page);
  603. page_cache_release(page);
  604. bytes += PAGE_CACHE_SIZE;
  605. index++;
  606. }
  607. btrfs_set_extent_delalloc(inode, 0, bytes - 1, &cached_state);
  608. /* Write the checksums and trans id to the first page */
  609. {
  610. void *addr;
  611. u64 *gen;
  612. page = find_get_page(inode->i_mapping, 0);
  613. addr = kmap(page);
  614. memcpy(addr, checksums, sizeof(u32) * num_checksums);
  615. gen = addr + (sizeof(u32) * num_checksums);
  616. *gen = trans->transid;
  617. kunmap(page);
  618. ClearPageChecked(page);
  619. set_page_extent_mapped(page);
  620. SetPageUptodate(page);
  621. set_page_dirty(page);
  622. unlock_page(page);
  623. page_cache_release(page);
  624. page_cache_release(page);
  625. }
  626. BTRFS_I(inode)->generation = trans->transid;
  627. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  628. i_size_read(inode) - 1, &cached_state, GFP_NOFS);
  629. filemap_write_and_wait(inode->i_mapping);
  630. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  631. key.offset = block_group->key.objectid;
  632. key.type = 0;
  633. ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
  634. if (ret < 0) {
  635. ret = 0;
  636. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  637. EXTENT_DIRTY | EXTENT_DELALLOC |
  638. EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
  639. goto out_free;
  640. }
  641. leaf = path->nodes[0];
  642. if (ret > 0) {
  643. struct btrfs_key found_key;
  644. BUG_ON(!path->slots[0]);
  645. path->slots[0]--;
  646. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  647. if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
  648. found_key.offset != block_group->key.objectid) {
  649. ret = 0;
  650. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  651. EXTENT_DIRTY | EXTENT_DELALLOC |
  652. EXTENT_DO_ACCOUNTING, 0, 0, NULL,
  653. GFP_NOFS);
  654. btrfs_release_path(root, path);
  655. goto out_free;
  656. }
  657. }
  658. header = btrfs_item_ptr(leaf, path->slots[0],
  659. struct btrfs_free_space_header);
  660. btrfs_set_free_space_entries(leaf, header, entries);
  661. btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
  662. btrfs_set_free_space_generation(leaf, header, trans->transid);
  663. btrfs_mark_buffer_dirty(leaf);
  664. btrfs_release_path(root, path);
  665. ret = 1;
  666. out_free:
  667. if (ret == 0) {
  668. invalidate_inode_pages2_range(inode->i_mapping, 0, index);
  669. spin_lock(&block_group->lock);
  670. block_group->disk_cache_state = BTRFS_DC_ERROR;
  671. spin_unlock(&block_group->lock);
  672. BTRFS_I(inode)->generation = 0;
  673. }
  674. kfree(checksums);
  675. btrfs_update_inode(trans, root, inode);
  676. iput(inode);
  677. return ret;
  678. }
  679. static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
  680. u64 offset)
  681. {
  682. BUG_ON(offset < bitmap_start);
  683. offset -= bitmap_start;
  684. return (unsigned long)(div64_u64(offset, sectorsize));
  685. }
  686. static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
  687. {
  688. return (unsigned long)(div64_u64(bytes, sectorsize));
  689. }
  690. static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
  691. u64 offset)
  692. {
  693. u64 bitmap_start;
  694. u64 bytes_per_bitmap;
  695. bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
  696. bitmap_start = offset - block_group->key.objectid;
  697. bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
  698. bitmap_start *= bytes_per_bitmap;
  699. bitmap_start += block_group->key.objectid;
  700. return bitmap_start;
  701. }
  702. static int tree_insert_offset(struct rb_root *root, u64 offset,
  703. struct rb_node *node, int bitmap)
  704. {
  705. struct rb_node **p = &root->rb_node;
  706. struct rb_node *parent = NULL;
  707. struct btrfs_free_space *info;
  708. while (*p) {
  709. parent = *p;
  710. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  711. if (offset < info->offset) {
  712. p = &(*p)->rb_left;
  713. } else if (offset > info->offset) {
  714. p = &(*p)->rb_right;
  715. } else {
  716. /*
  717. * we could have a bitmap entry and an extent entry
  718. * share the same offset. If this is the case, we want
  719. * the extent entry to always be found first if we do a
  720. * linear search through the tree, since we want to have
  721. * the quickest allocation time, and allocating from an
  722. * extent is faster than allocating from a bitmap. So
  723. * if we're inserting a bitmap and we find an entry at
  724. * this offset, we want to go right, or after this entry
  725. * logically. If we are inserting an extent and we've
  726. * found a bitmap, we want to go left, or before
  727. * logically.
  728. */
  729. if (bitmap) {
  730. WARN_ON(info->bitmap);
  731. p = &(*p)->rb_right;
  732. } else {
  733. WARN_ON(!info->bitmap);
  734. p = &(*p)->rb_left;
  735. }
  736. }
  737. }
  738. rb_link_node(node, parent, p);
  739. rb_insert_color(node, root);
  740. return 0;
  741. }
  742. /*
  743. * searches the tree for the given offset.
  744. *
  745. * fuzzy - If this is set, then we are trying to make an allocation, and we just
  746. * want a section that has at least bytes size and comes at or after the given
  747. * offset.
  748. */
  749. static struct btrfs_free_space *
  750. tree_search_offset(struct btrfs_block_group_cache *block_group,
  751. u64 offset, int bitmap_only, int fuzzy)
  752. {
  753. struct rb_node *n = block_group->free_space_offset.rb_node;
  754. struct btrfs_free_space *entry, *prev = NULL;
  755. /* find entry that is closest to the 'offset' */
  756. while (1) {
  757. if (!n) {
  758. entry = NULL;
  759. break;
  760. }
  761. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  762. prev = entry;
  763. if (offset < entry->offset)
  764. n = n->rb_left;
  765. else if (offset > entry->offset)
  766. n = n->rb_right;
  767. else
  768. break;
  769. }
  770. if (bitmap_only) {
  771. if (!entry)
  772. return NULL;
  773. if (entry->bitmap)
  774. return entry;
  775. /*
  776. * bitmap entry and extent entry may share same offset,
  777. * in that case, bitmap entry comes after extent entry.
  778. */
  779. n = rb_next(n);
  780. if (!n)
  781. return NULL;
  782. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  783. if (entry->offset != offset)
  784. return NULL;
  785. WARN_ON(!entry->bitmap);
  786. return entry;
  787. } else if (entry) {
  788. if (entry->bitmap) {
  789. /*
  790. * if previous extent entry covers the offset,
  791. * we should return it instead of the bitmap entry
  792. */
  793. n = &entry->offset_index;
  794. while (1) {
  795. n = rb_prev(n);
  796. if (!n)
  797. break;
  798. prev = rb_entry(n, struct btrfs_free_space,
  799. offset_index);
  800. if (!prev->bitmap) {
  801. if (prev->offset + prev->bytes > offset)
  802. entry = prev;
  803. break;
  804. }
  805. }
  806. }
  807. return entry;
  808. }
  809. if (!prev)
  810. return NULL;
  811. /* find last entry before the 'offset' */
  812. entry = prev;
  813. if (entry->offset > offset) {
  814. n = rb_prev(&entry->offset_index);
  815. if (n) {
  816. entry = rb_entry(n, struct btrfs_free_space,
  817. offset_index);
  818. BUG_ON(entry->offset > offset);
  819. } else {
  820. if (fuzzy)
  821. return entry;
  822. else
  823. return NULL;
  824. }
  825. }
  826. if (entry->bitmap) {
  827. n = &entry->offset_index;
  828. while (1) {
  829. n = rb_prev(n);
  830. if (!n)
  831. break;
  832. prev = rb_entry(n, struct btrfs_free_space,
  833. offset_index);
  834. if (!prev->bitmap) {
  835. if (prev->offset + prev->bytes > offset)
  836. return prev;
  837. break;
  838. }
  839. }
  840. if (entry->offset + BITS_PER_BITMAP *
  841. block_group->sectorsize > offset)
  842. return entry;
  843. } else if (entry->offset + entry->bytes > offset)
  844. return entry;
  845. if (!fuzzy)
  846. return NULL;
  847. while (1) {
  848. if (entry->bitmap) {
  849. if (entry->offset + BITS_PER_BITMAP *
  850. block_group->sectorsize > offset)
  851. break;
  852. } else {
  853. if (entry->offset + entry->bytes > offset)
  854. break;
  855. }
  856. n = rb_next(&entry->offset_index);
  857. if (!n)
  858. return NULL;
  859. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  860. }
  861. return entry;
  862. }
  863. static void unlink_free_space(struct btrfs_block_group_cache *block_group,
  864. struct btrfs_free_space *info)
  865. {
  866. rb_erase(&info->offset_index, &block_group->free_space_offset);
  867. block_group->free_extents--;
  868. block_group->free_space -= info->bytes;
  869. }
  870. static int link_free_space(struct btrfs_block_group_cache *block_group,
  871. struct btrfs_free_space *info)
  872. {
  873. int ret = 0;
  874. BUG_ON(!info->bitmap && !info->bytes);
  875. ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
  876. &info->offset_index, (info->bitmap != NULL));
  877. if (ret)
  878. return ret;
  879. block_group->free_space += info->bytes;
  880. block_group->free_extents++;
  881. return ret;
  882. }
  883. static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
  884. {
  885. u64 max_bytes;
  886. u64 bitmap_bytes;
  887. u64 extent_bytes;
  888. /*
  889. * The goal is to keep the total amount of memory used per 1gb of space
  890. * at or below 32k, so we need to adjust how much memory we allow to be
  891. * used by extent based free space tracking
  892. */
  893. max_bytes = MAX_CACHE_BYTES_PER_GIG *
  894. (div64_u64(block_group->key.offset, 1024 * 1024 * 1024));
  895. /*
  896. * we want to account for 1 more bitmap than what we have so we can make
  897. * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
  898. * we add more bitmaps.
  899. */
  900. bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
  901. if (bitmap_bytes >= max_bytes) {
  902. block_group->extents_thresh = 0;
  903. return;
  904. }
  905. /*
  906. * we want the extent entry threshold to always be at most 1/2 the maxw
  907. * bytes we can have, or whatever is less than that.
  908. */
  909. extent_bytes = max_bytes - bitmap_bytes;
  910. extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
  911. block_group->extents_thresh =
  912. div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
  913. }
  914. static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
  915. struct btrfs_free_space *info, u64 offset,
  916. u64 bytes)
  917. {
  918. unsigned long start, end;
  919. unsigned long i;
  920. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  921. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  922. BUG_ON(end > BITS_PER_BITMAP);
  923. for (i = start; i < end; i++)
  924. clear_bit(i, info->bitmap);
  925. info->bytes -= bytes;
  926. block_group->free_space -= bytes;
  927. }
  928. static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
  929. struct btrfs_free_space *info, u64 offset,
  930. u64 bytes)
  931. {
  932. unsigned long start, end;
  933. unsigned long i;
  934. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  935. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  936. BUG_ON(end > BITS_PER_BITMAP);
  937. for (i = start; i < end; i++)
  938. set_bit(i, info->bitmap);
  939. info->bytes += bytes;
  940. block_group->free_space += bytes;
  941. }
  942. static int search_bitmap(struct btrfs_block_group_cache *block_group,
  943. struct btrfs_free_space *bitmap_info, u64 *offset,
  944. u64 *bytes)
  945. {
  946. unsigned long found_bits = 0;
  947. unsigned long bits, i;
  948. unsigned long next_zero;
  949. i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
  950. max_t(u64, *offset, bitmap_info->offset));
  951. bits = bytes_to_bits(*bytes, block_group->sectorsize);
  952. for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
  953. i < BITS_PER_BITMAP;
  954. i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
  955. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  956. BITS_PER_BITMAP, i);
  957. if ((next_zero - i) >= bits) {
  958. found_bits = next_zero - i;
  959. break;
  960. }
  961. i = next_zero;
  962. }
  963. if (found_bits) {
  964. *offset = (u64)(i * block_group->sectorsize) +
  965. bitmap_info->offset;
  966. *bytes = (u64)(found_bits) * block_group->sectorsize;
  967. return 0;
  968. }
  969. return -1;
  970. }
  971. static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
  972. *block_group, u64 *offset,
  973. u64 *bytes, int debug)
  974. {
  975. struct btrfs_free_space *entry;
  976. struct rb_node *node;
  977. int ret;
  978. if (!block_group->free_space_offset.rb_node)
  979. return NULL;
  980. entry = tree_search_offset(block_group,
  981. offset_to_bitmap(block_group, *offset),
  982. 0, 1);
  983. if (!entry)
  984. return NULL;
  985. for (node = &entry->offset_index; node; node = rb_next(node)) {
  986. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  987. if (entry->bytes < *bytes)
  988. continue;
  989. if (entry->bitmap) {
  990. ret = search_bitmap(block_group, entry, offset, bytes);
  991. if (!ret)
  992. return entry;
  993. continue;
  994. }
  995. *offset = entry->offset;
  996. *bytes = entry->bytes;
  997. return entry;
  998. }
  999. return NULL;
  1000. }
  1001. static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
  1002. struct btrfs_free_space *info, u64 offset)
  1003. {
  1004. u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
  1005. int max_bitmaps = (int)div64_u64(block_group->key.offset +
  1006. bytes_per_bg - 1, bytes_per_bg);
  1007. BUG_ON(block_group->total_bitmaps >= max_bitmaps);
  1008. info->offset = offset_to_bitmap(block_group, offset);
  1009. info->bytes = 0;
  1010. link_free_space(block_group, info);
  1011. block_group->total_bitmaps++;
  1012. recalculate_thresholds(block_group);
  1013. }
  1014. static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
  1015. struct btrfs_free_space *bitmap_info,
  1016. u64 *offset, u64 *bytes)
  1017. {
  1018. u64 end;
  1019. u64 search_start, search_bytes;
  1020. int ret;
  1021. again:
  1022. end = bitmap_info->offset +
  1023. (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
  1024. /*
  1025. * XXX - this can go away after a few releases.
  1026. *
  1027. * since the only user of btrfs_remove_free_space is the tree logging
  1028. * stuff, and the only way to test that is under crash conditions, we
  1029. * want to have this debug stuff here just in case somethings not
  1030. * working. Search the bitmap for the space we are trying to use to
  1031. * make sure its actually there. If its not there then we need to stop
  1032. * because something has gone wrong.
  1033. */
  1034. search_start = *offset;
  1035. search_bytes = *bytes;
  1036. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1037. &search_bytes);
  1038. BUG_ON(ret < 0 || search_start != *offset);
  1039. if (*offset > bitmap_info->offset && *offset + *bytes > end) {
  1040. bitmap_clear_bits(block_group, bitmap_info, *offset,
  1041. end - *offset + 1);
  1042. *bytes -= end - *offset + 1;
  1043. *offset = end + 1;
  1044. } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
  1045. bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
  1046. *bytes = 0;
  1047. }
  1048. if (*bytes) {
  1049. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1050. if (!bitmap_info->bytes) {
  1051. unlink_free_space(block_group, bitmap_info);
  1052. kfree(bitmap_info->bitmap);
  1053. kfree(bitmap_info);
  1054. block_group->total_bitmaps--;
  1055. recalculate_thresholds(block_group);
  1056. }
  1057. /*
  1058. * no entry after this bitmap, but we still have bytes to
  1059. * remove, so something has gone wrong.
  1060. */
  1061. if (!next)
  1062. return -EINVAL;
  1063. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1064. offset_index);
  1065. /*
  1066. * if the next entry isn't a bitmap we need to return to let the
  1067. * extent stuff do its work.
  1068. */
  1069. if (!bitmap_info->bitmap)
  1070. return -EAGAIN;
  1071. /*
  1072. * Ok the next item is a bitmap, but it may not actually hold
  1073. * the information for the rest of this free space stuff, so
  1074. * look for it, and if we don't find it return so we can try
  1075. * everything over again.
  1076. */
  1077. search_start = *offset;
  1078. search_bytes = *bytes;
  1079. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1080. &search_bytes);
  1081. if (ret < 0 || search_start != *offset)
  1082. return -EAGAIN;
  1083. goto again;
  1084. } else if (!bitmap_info->bytes) {
  1085. unlink_free_space(block_group, bitmap_info);
  1086. kfree(bitmap_info->bitmap);
  1087. kfree(bitmap_info);
  1088. block_group->total_bitmaps--;
  1089. recalculate_thresholds(block_group);
  1090. }
  1091. return 0;
  1092. }
  1093. static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
  1094. struct btrfs_free_space *info)
  1095. {
  1096. struct btrfs_free_space *bitmap_info;
  1097. int added = 0;
  1098. u64 bytes, offset, end;
  1099. int ret;
  1100. /*
  1101. * If we are below the extents threshold then we can add this as an
  1102. * extent, and don't have to deal with the bitmap
  1103. */
  1104. if (block_group->free_extents < block_group->extents_thresh &&
  1105. info->bytes > block_group->sectorsize * 4)
  1106. return 0;
  1107. /*
  1108. * some block groups are so tiny they can't be enveloped by a bitmap, so
  1109. * don't even bother to create a bitmap for this
  1110. */
  1111. if (BITS_PER_BITMAP * block_group->sectorsize >
  1112. block_group->key.offset)
  1113. return 0;
  1114. bytes = info->bytes;
  1115. offset = info->offset;
  1116. again:
  1117. bitmap_info = tree_search_offset(block_group,
  1118. offset_to_bitmap(block_group, offset),
  1119. 1, 0);
  1120. if (!bitmap_info) {
  1121. BUG_ON(added);
  1122. goto new_bitmap;
  1123. }
  1124. end = bitmap_info->offset +
  1125. (u64)(BITS_PER_BITMAP * block_group->sectorsize);
  1126. if (offset >= bitmap_info->offset && offset + bytes > end) {
  1127. bitmap_set_bits(block_group, bitmap_info, offset,
  1128. end - offset);
  1129. bytes -= end - offset;
  1130. offset = end;
  1131. added = 0;
  1132. } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
  1133. bitmap_set_bits(block_group, bitmap_info, offset, bytes);
  1134. bytes = 0;
  1135. } else {
  1136. BUG();
  1137. }
  1138. if (!bytes) {
  1139. ret = 1;
  1140. goto out;
  1141. } else
  1142. goto again;
  1143. new_bitmap:
  1144. if (info && info->bitmap) {
  1145. add_new_bitmap(block_group, info, offset);
  1146. added = 1;
  1147. info = NULL;
  1148. goto again;
  1149. } else {
  1150. spin_unlock(&block_group->tree_lock);
  1151. /* no pre-allocated info, allocate a new one */
  1152. if (!info) {
  1153. info = kzalloc(sizeof(struct btrfs_free_space),
  1154. GFP_NOFS);
  1155. if (!info) {
  1156. spin_lock(&block_group->tree_lock);
  1157. ret = -ENOMEM;
  1158. goto out;
  1159. }
  1160. }
  1161. /* allocate the bitmap */
  1162. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1163. spin_lock(&block_group->tree_lock);
  1164. if (!info->bitmap) {
  1165. ret = -ENOMEM;
  1166. goto out;
  1167. }
  1168. goto again;
  1169. }
  1170. out:
  1171. if (info) {
  1172. if (info->bitmap)
  1173. kfree(info->bitmap);
  1174. kfree(info);
  1175. }
  1176. return ret;
  1177. }
  1178. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  1179. u64 offset, u64 bytes)
  1180. {
  1181. struct btrfs_free_space *right_info = NULL;
  1182. struct btrfs_free_space *left_info = NULL;
  1183. struct btrfs_free_space *info = NULL;
  1184. int ret = 0;
  1185. info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  1186. if (!info)
  1187. return -ENOMEM;
  1188. info->offset = offset;
  1189. info->bytes = bytes;
  1190. spin_lock(&block_group->tree_lock);
  1191. /*
  1192. * first we want to see if there is free space adjacent to the range we
  1193. * are adding, if there is remove that struct and add a new one to
  1194. * cover the entire range
  1195. */
  1196. right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
  1197. if (right_info && rb_prev(&right_info->offset_index))
  1198. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1199. struct btrfs_free_space, offset_index);
  1200. else
  1201. left_info = tree_search_offset(block_group, offset - 1, 0, 0);
  1202. /*
  1203. * If there was no extent directly to the left or right of this new
  1204. * extent then we know we're going to have to allocate a new extent, so
  1205. * before we do that see if we need to drop this into a bitmap
  1206. */
  1207. if ((!left_info || left_info->bitmap) &&
  1208. (!right_info || right_info->bitmap)) {
  1209. ret = insert_into_bitmap(block_group, info);
  1210. if (ret < 0) {
  1211. goto out;
  1212. } else if (ret) {
  1213. ret = 0;
  1214. goto out;
  1215. }
  1216. }
  1217. if (right_info && !right_info->bitmap) {
  1218. unlink_free_space(block_group, right_info);
  1219. info->bytes += right_info->bytes;
  1220. kfree(right_info);
  1221. }
  1222. if (left_info && !left_info->bitmap &&
  1223. left_info->offset + left_info->bytes == offset) {
  1224. unlink_free_space(block_group, left_info);
  1225. info->offset = left_info->offset;
  1226. info->bytes += left_info->bytes;
  1227. kfree(left_info);
  1228. }
  1229. ret = link_free_space(block_group, info);
  1230. if (ret)
  1231. kfree(info);
  1232. out:
  1233. spin_unlock(&block_group->tree_lock);
  1234. if (ret) {
  1235. printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
  1236. BUG_ON(ret == -EEXIST);
  1237. }
  1238. return ret;
  1239. }
  1240. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1241. u64 offset, u64 bytes)
  1242. {
  1243. struct btrfs_free_space *info;
  1244. struct btrfs_free_space *next_info = NULL;
  1245. int ret = 0;
  1246. spin_lock(&block_group->tree_lock);
  1247. again:
  1248. info = tree_search_offset(block_group, offset, 0, 0);
  1249. if (!info) {
  1250. /*
  1251. * oops didn't find an extent that matched the space we wanted
  1252. * to remove, look for a bitmap instead
  1253. */
  1254. info = tree_search_offset(block_group,
  1255. offset_to_bitmap(block_group, offset),
  1256. 1, 0);
  1257. if (!info) {
  1258. WARN_ON(1);
  1259. goto out_lock;
  1260. }
  1261. }
  1262. if (info->bytes < bytes && rb_next(&info->offset_index)) {
  1263. u64 end;
  1264. next_info = rb_entry(rb_next(&info->offset_index),
  1265. struct btrfs_free_space,
  1266. offset_index);
  1267. if (next_info->bitmap)
  1268. end = next_info->offset + BITS_PER_BITMAP *
  1269. block_group->sectorsize - 1;
  1270. else
  1271. end = next_info->offset + next_info->bytes;
  1272. if (next_info->bytes < bytes ||
  1273. next_info->offset > offset || offset > end) {
  1274. printk(KERN_CRIT "Found free space at %llu, size %llu,"
  1275. " trying to use %llu\n",
  1276. (unsigned long long)info->offset,
  1277. (unsigned long long)info->bytes,
  1278. (unsigned long long)bytes);
  1279. WARN_ON(1);
  1280. ret = -EINVAL;
  1281. goto out_lock;
  1282. }
  1283. info = next_info;
  1284. }
  1285. if (info->bytes == bytes) {
  1286. unlink_free_space(block_group, info);
  1287. if (info->bitmap) {
  1288. kfree(info->bitmap);
  1289. block_group->total_bitmaps--;
  1290. }
  1291. kfree(info);
  1292. goto out_lock;
  1293. }
  1294. if (!info->bitmap && info->offset == offset) {
  1295. unlink_free_space(block_group, info);
  1296. info->offset += bytes;
  1297. info->bytes -= bytes;
  1298. link_free_space(block_group, info);
  1299. goto out_lock;
  1300. }
  1301. if (!info->bitmap && info->offset <= offset &&
  1302. info->offset + info->bytes >= offset + bytes) {
  1303. u64 old_start = info->offset;
  1304. /*
  1305. * we're freeing space in the middle of the info,
  1306. * this can happen during tree log replay
  1307. *
  1308. * first unlink the old info and then
  1309. * insert it again after the hole we're creating
  1310. */
  1311. unlink_free_space(block_group, info);
  1312. if (offset + bytes < info->offset + info->bytes) {
  1313. u64 old_end = info->offset + info->bytes;
  1314. info->offset = offset + bytes;
  1315. info->bytes = old_end - info->offset;
  1316. ret = link_free_space(block_group, info);
  1317. WARN_ON(ret);
  1318. if (ret)
  1319. goto out_lock;
  1320. } else {
  1321. /* the hole we're creating ends at the end
  1322. * of the info struct, just free the info
  1323. */
  1324. kfree(info);
  1325. }
  1326. spin_unlock(&block_group->tree_lock);
  1327. /* step two, insert a new info struct to cover
  1328. * anything before the hole
  1329. */
  1330. ret = btrfs_add_free_space(block_group, old_start,
  1331. offset - old_start);
  1332. WARN_ON(ret);
  1333. goto out;
  1334. }
  1335. ret = remove_from_bitmap(block_group, info, &offset, &bytes);
  1336. if (ret == -EAGAIN)
  1337. goto again;
  1338. BUG_ON(ret);
  1339. out_lock:
  1340. spin_unlock(&block_group->tree_lock);
  1341. out:
  1342. return ret;
  1343. }
  1344. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  1345. u64 bytes)
  1346. {
  1347. struct btrfs_free_space *info;
  1348. struct rb_node *n;
  1349. int count = 0;
  1350. for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
  1351. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1352. if (info->bytes >= bytes)
  1353. count++;
  1354. printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
  1355. (unsigned long long)info->offset,
  1356. (unsigned long long)info->bytes,
  1357. (info->bitmap) ? "yes" : "no");
  1358. }
  1359. printk(KERN_INFO "block group has cluster?: %s\n",
  1360. list_empty(&block_group->cluster_list) ? "no" : "yes");
  1361. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  1362. "\n", count);
  1363. }
  1364. u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
  1365. {
  1366. struct btrfs_free_space *info;
  1367. struct rb_node *n;
  1368. u64 ret = 0;
  1369. for (n = rb_first(&block_group->free_space_offset); n;
  1370. n = rb_next(n)) {
  1371. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1372. ret += info->bytes;
  1373. }
  1374. return ret;
  1375. }
  1376. /*
  1377. * for a given cluster, put all of its extents back into the free
  1378. * space cache. If the block group passed doesn't match the block group
  1379. * pointed to by the cluster, someone else raced in and freed the
  1380. * cluster already. In that case, we just return without changing anything
  1381. */
  1382. static int
  1383. __btrfs_return_cluster_to_free_space(
  1384. struct btrfs_block_group_cache *block_group,
  1385. struct btrfs_free_cluster *cluster)
  1386. {
  1387. struct btrfs_free_space *entry;
  1388. struct rb_node *node;
  1389. bool bitmap;
  1390. spin_lock(&cluster->lock);
  1391. if (cluster->block_group != block_group)
  1392. goto out;
  1393. bitmap = cluster->points_to_bitmap;
  1394. cluster->block_group = NULL;
  1395. cluster->window_start = 0;
  1396. list_del_init(&cluster->block_group_list);
  1397. cluster->points_to_bitmap = false;
  1398. if (bitmap)
  1399. goto out;
  1400. node = rb_first(&cluster->root);
  1401. while (node) {
  1402. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1403. node = rb_next(&entry->offset_index);
  1404. rb_erase(&entry->offset_index, &cluster->root);
  1405. BUG_ON(entry->bitmap);
  1406. tree_insert_offset(&block_group->free_space_offset,
  1407. entry->offset, &entry->offset_index, 0);
  1408. }
  1409. cluster->root = RB_ROOT;
  1410. out:
  1411. spin_unlock(&cluster->lock);
  1412. btrfs_put_block_group(block_group);
  1413. return 0;
  1414. }
  1415. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  1416. {
  1417. struct btrfs_free_space *info;
  1418. struct rb_node *node;
  1419. struct btrfs_free_cluster *cluster;
  1420. struct list_head *head;
  1421. spin_lock(&block_group->tree_lock);
  1422. while ((head = block_group->cluster_list.next) !=
  1423. &block_group->cluster_list) {
  1424. cluster = list_entry(head, struct btrfs_free_cluster,
  1425. block_group_list);
  1426. WARN_ON(cluster->block_group != block_group);
  1427. __btrfs_return_cluster_to_free_space(block_group, cluster);
  1428. if (need_resched()) {
  1429. spin_unlock(&block_group->tree_lock);
  1430. cond_resched();
  1431. spin_lock(&block_group->tree_lock);
  1432. }
  1433. }
  1434. while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
  1435. info = rb_entry(node, struct btrfs_free_space, offset_index);
  1436. unlink_free_space(block_group, info);
  1437. if (info->bitmap)
  1438. kfree(info->bitmap);
  1439. kfree(info);
  1440. if (need_resched()) {
  1441. spin_unlock(&block_group->tree_lock);
  1442. cond_resched();
  1443. spin_lock(&block_group->tree_lock);
  1444. }
  1445. }
  1446. spin_unlock(&block_group->tree_lock);
  1447. }
  1448. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  1449. u64 offset, u64 bytes, u64 empty_size)
  1450. {
  1451. struct btrfs_free_space *entry = NULL;
  1452. u64 bytes_search = bytes + empty_size;
  1453. u64 ret = 0;
  1454. spin_lock(&block_group->tree_lock);
  1455. entry = find_free_space(block_group, &offset, &bytes_search, 0);
  1456. if (!entry)
  1457. goto out;
  1458. ret = offset;
  1459. if (entry->bitmap) {
  1460. bitmap_clear_bits(block_group, entry, offset, bytes);
  1461. if (!entry->bytes) {
  1462. unlink_free_space(block_group, entry);
  1463. kfree(entry->bitmap);
  1464. kfree(entry);
  1465. block_group->total_bitmaps--;
  1466. recalculate_thresholds(block_group);
  1467. }
  1468. } else {
  1469. unlink_free_space(block_group, entry);
  1470. entry->offset += bytes;
  1471. entry->bytes -= bytes;
  1472. if (!entry->bytes)
  1473. kfree(entry);
  1474. else
  1475. link_free_space(block_group, entry);
  1476. }
  1477. out:
  1478. spin_unlock(&block_group->tree_lock);
  1479. return ret;
  1480. }
  1481. /*
  1482. * given a cluster, put all of its extents back into the free space
  1483. * cache. If a block group is passed, this function will only free
  1484. * a cluster that belongs to the passed block group.
  1485. *
  1486. * Otherwise, it'll get a reference on the block group pointed to by the
  1487. * cluster and remove the cluster from it.
  1488. */
  1489. int btrfs_return_cluster_to_free_space(
  1490. struct btrfs_block_group_cache *block_group,
  1491. struct btrfs_free_cluster *cluster)
  1492. {
  1493. int ret;
  1494. /* first, get a safe pointer to the block group */
  1495. spin_lock(&cluster->lock);
  1496. if (!block_group) {
  1497. block_group = cluster->block_group;
  1498. if (!block_group) {
  1499. spin_unlock(&cluster->lock);
  1500. return 0;
  1501. }
  1502. } else if (cluster->block_group != block_group) {
  1503. /* someone else has already freed it don't redo their work */
  1504. spin_unlock(&cluster->lock);
  1505. return 0;
  1506. }
  1507. atomic_inc(&block_group->count);
  1508. spin_unlock(&cluster->lock);
  1509. /* now return any extents the cluster had on it */
  1510. spin_lock(&block_group->tree_lock);
  1511. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  1512. spin_unlock(&block_group->tree_lock);
  1513. /* finally drop our ref */
  1514. btrfs_put_block_group(block_group);
  1515. return ret;
  1516. }
  1517. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  1518. struct btrfs_free_cluster *cluster,
  1519. u64 bytes, u64 min_start)
  1520. {
  1521. struct btrfs_free_space *entry;
  1522. int err;
  1523. u64 search_start = cluster->window_start;
  1524. u64 search_bytes = bytes;
  1525. u64 ret = 0;
  1526. spin_lock(&block_group->tree_lock);
  1527. spin_lock(&cluster->lock);
  1528. if (!cluster->points_to_bitmap)
  1529. goto out;
  1530. if (cluster->block_group != block_group)
  1531. goto out;
  1532. /*
  1533. * search_start is the beginning of the bitmap, but at some point it may
  1534. * be a good idea to point to the actual start of the free area in the
  1535. * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
  1536. * to 1 to make sure we get the bitmap entry
  1537. */
  1538. entry = tree_search_offset(block_group,
  1539. offset_to_bitmap(block_group, search_start),
  1540. 1, 0);
  1541. if (!entry || !entry->bitmap)
  1542. goto out;
  1543. search_start = min_start;
  1544. search_bytes = bytes;
  1545. err = search_bitmap(block_group, entry, &search_start,
  1546. &search_bytes);
  1547. if (err)
  1548. goto out;
  1549. ret = search_start;
  1550. bitmap_clear_bits(block_group, entry, ret, bytes);
  1551. out:
  1552. spin_unlock(&cluster->lock);
  1553. spin_unlock(&block_group->tree_lock);
  1554. return ret;
  1555. }
  1556. /*
  1557. * given a cluster, try to allocate 'bytes' from it, returns 0
  1558. * if it couldn't find anything suitably large, or a logical disk offset
  1559. * if things worked out
  1560. */
  1561. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  1562. struct btrfs_free_cluster *cluster, u64 bytes,
  1563. u64 min_start)
  1564. {
  1565. struct btrfs_free_space *entry = NULL;
  1566. struct rb_node *node;
  1567. u64 ret = 0;
  1568. if (cluster->points_to_bitmap)
  1569. return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
  1570. min_start);
  1571. spin_lock(&cluster->lock);
  1572. if (bytes > cluster->max_size)
  1573. goto out;
  1574. if (cluster->block_group != block_group)
  1575. goto out;
  1576. node = rb_first(&cluster->root);
  1577. if (!node)
  1578. goto out;
  1579. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1580. while(1) {
  1581. if (entry->bytes < bytes || entry->offset < min_start) {
  1582. struct rb_node *node;
  1583. node = rb_next(&entry->offset_index);
  1584. if (!node)
  1585. break;
  1586. entry = rb_entry(node, struct btrfs_free_space,
  1587. offset_index);
  1588. continue;
  1589. }
  1590. ret = entry->offset;
  1591. entry->offset += bytes;
  1592. entry->bytes -= bytes;
  1593. if (entry->bytes == 0) {
  1594. rb_erase(&entry->offset_index, &cluster->root);
  1595. kfree(entry);
  1596. }
  1597. break;
  1598. }
  1599. out:
  1600. spin_unlock(&cluster->lock);
  1601. return ret;
  1602. }
  1603. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  1604. struct btrfs_free_space *entry,
  1605. struct btrfs_free_cluster *cluster,
  1606. u64 offset, u64 bytes, u64 min_bytes)
  1607. {
  1608. unsigned long next_zero;
  1609. unsigned long i;
  1610. unsigned long search_bits;
  1611. unsigned long total_bits;
  1612. unsigned long found_bits;
  1613. unsigned long start = 0;
  1614. unsigned long total_found = 0;
  1615. bool found = false;
  1616. i = offset_to_bit(entry->offset, block_group->sectorsize,
  1617. max_t(u64, offset, entry->offset));
  1618. search_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
  1619. total_bits = bytes_to_bits(bytes, block_group->sectorsize);
  1620. again:
  1621. found_bits = 0;
  1622. for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
  1623. i < BITS_PER_BITMAP;
  1624. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
  1625. next_zero = find_next_zero_bit(entry->bitmap,
  1626. BITS_PER_BITMAP, i);
  1627. if (next_zero - i >= search_bits) {
  1628. found_bits = next_zero - i;
  1629. break;
  1630. }
  1631. i = next_zero;
  1632. }
  1633. if (!found_bits)
  1634. return -1;
  1635. if (!found) {
  1636. start = i;
  1637. found = true;
  1638. }
  1639. total_found += found_bits;
  1640. if (cluster->max_size < found_bits * block_group->sectorsize)
  1641. cluster->max_size = found_bits * block_group->sectorsize;
  1642. if (total_found < total_bits) {
  1643. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
  1644. if (i - start > total_bits * 2) {
  1645. total_found = 0;
  1646. cluster->max_size = 0;
  1647. found = false;
  1648. }
  1649. goto again;
  1650. }
  1651. cluster->window_start = start * block_group->sectorsize +
  1652. entry->offset;
  1653. cluster->points_to_bitmap = true;
  1654. return 0;
  1655. }
  1656. /*
  1657. * here we try to find a cluster of blocks in a block group. The goal
  1658. * is to find at least bytes free and up to empty_size + bytes free.
  1659. * We might not find them all in one contiguous area.
  1660. *
  1661. * returns zero and sets up cluster if things worked out, otherwise
  1662. * it returns -enospc
  1663. */
  1664. int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
  1665. struct btrfs_root *root,
  1666. struct btrfs_block_group_cache *block_group,
  1667. struct btrfs_free_cluster *cluster,
  1668. u64 offset, u64 bytes, u64 empty_size)
  1669. {
  1670. struct btrfs_free_space *entry = NULL;
  1671. struct rb_node *node;
  1672. struct btrfs_free_space *next;
  1673. struct btrfs_free_space *last = NULL;
  1674. u64 min_bytes;
  1675. u64 window_start;
  1676. u64 window_free;
  1677. u64 max_extent = 0;
  1678. bool found_bitmap = false;
  1679. int ret;
  1680. /* for metadata, allow allocates with more holes */
  1681. if (btrfs_test_opt(root, SSD_SPREAD)) {
  1682. min_bytes = bytes + empty_size;
  1683. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1684. /*
  1685. * we want to do larger allocations when we are
  1686. * flushing out the delayed refs, it helps prevent
  1687. * making more work as we go along.
  1688. */
  1689. if (trans->transaction->delayed_refs.flushing)
  1690. min_bytes = max(bytes, (bytes + empty_size) >> 1);
  1691. else
  1692. min_bytes = max(bytes, (bytes + empty_size) >> 4);
  1693. } else
  1694. min_bytes = max(bytes, (bytes + empty_size) >> 2);
  1695. spin_lock(&block_group->tree_lock);
  1696. spin_lock(&cluster->lock);
  1697. /* someone already found a cluster, hooray */
  1698. if (cluster->block_group) {
  1699. ret = 0;
  1700. goto out;
  1701. }
  1702. again:
  1703. entry = tree_search_offset(block_group, offset, found_bitmap, 1);
  1704. if (!entry) {
  1705. ret = -ENOSPC;
  1706. goto out;
  1707. }
  1708. /*
  1709. * If found_bitmap is true, we exhausted our search for extent entries,
  1710. * and we just want to search all of the bitmaps that we can find, and
  1711. * ignore any extent entries we find.
  1712. */
  1713. while (entry->bitmap || found_bitmap ||
  1714. (!entry->bitmap && entry->bytes < min_bytes)) {
  1715. struct rb_node *node = rb_next(&entry->offset_index);
  1716. if (entry->bitmap && entry->bytes > bytes + empty_size) {
  1717. ret = btrfs_bitmap_cluster(block_group, entry, cluster,
  1718. offset, bytes + empty_size,
  1719. min_bytes);
  1720. if (!ret)
  1721. goto got_it;
  1722. }
  1723. if (!node) {
  1724. ret = -ENOSPC;
  1725. goto out;
  1726. }
  1727. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1728. }
  1729. /*
  1730. * We already searched all the extent entries from the passed in offset
  1731. * to the end and didn't find enough space for the cluster, and we also
  1732. * didn't find any bitmaps that met our criteria, just go ahead and exit
  1733. */
  1734. if (found_bitmap) {
  1735. ret = -ENOSPC;
  1736. goto out;
  1737. }
  1738. cluster->points_to_bitmap = false;
  1739. window_start = entry->offset;
  1740. window_free = entry->bytes;
  1741. last = entry;
  1742. max_extent = entry->bytes;
  1743. while (1) {
  1744. /* out window is just right, lets fill it */
  1745. if (window_free >= bytes + empty_size)
  1746. break;
  1747. node = rb_next(&last->offset_index);
  1748. if (!node) {
  1749. if (found_bitmap)
  1750. goto again;
  1751. ret = -ENOSPC;
  1752. goto out;
  1753. }
  1754. next = rb_entry(node, struct btrfs_free_space, offset_index);
  1755. /*
  1756. * we found a bitmap, so if this search doesn't result in a
  1757. * cluster, we know to go and search again for the bitmaps and
  1758. * start looking for space there
  1759. */
  1760. if (next->bitmap) {
  1761. if (!found_bitmap)
  1762. offset = next->offset;
  1763. found_bitmap = true;
  1764. last = next;
  1765. continue;
  1766. }
  1767. /*
  1768. * we haven't filled the empty size and the window is
  1769. * very large. reset and try again
  1770. */
  1771. if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
  1772. next->offset - window_start > (bytes + empty_size) * 2) {
  1773. entry = next;
  1774. window_start = entry->offset;
  1775. window_free = entry->bytes;
  1776. last = entry;
  1777. max_extent = entry->bytes;
  1778. } else {
  1779. last = next;
  1780. window_free += next->bytes;
  1781. if (entry->bytes > max_extent)
  1782. max_extent = entry->bytes;
  1783. }
  1784. }
  1785. cluster->window_start = entry->offset;
  1786. /*
  1787. * now we've found our entries, pull them out of the free space
  1788. * cache and put them into the cluster rbtree
  1789. *
  1790. * The cluster includes an rbtree, but only uses the offset index
  1791. * of each free space cache entry.
  1792. */
  1793. while (1) {
  1794. node = rb_next(&entry->offset_index);
  1795. if (entry->bitmap && node) {
  1796. entry = rb_entry(node, struct btrfs_free_space,
  1797. offset_index);
  1798. continue;
  1799. } else if (entry->bitmap && !node) {
  1800. break;
  1801. }
  1802. rb_erase(&entry->offset_index, &block_group->free_space_offset);
  1803. ret = tree_insert_offset(&cluster->root, entry->offset,
  1804. &entry->offset_index, 0);
  1805. BUG_ON(ret);
  1806. if (!node || entry == last)
  1807. break;
  1808. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1809. }
  1810. cluster->max_size = max_extent;
  1811. got_it:
  1812. ret = 0;
  1813. atomic_inc(&block_group->count);
  1814. list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
  1815. cluster->block_group = block_group;
  1816. out:
  1817. spin_unlock(&cluster->lock);
  1818. spin_unlock(&block_group->tree_lock);
  1819. return ret;
  1820. }
  1821. /*
  1822. * simple code to zero out a cluster
  1823. */
  1824. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  1825. {
  1826. spin_lock_init(&cluster->lock);
  1827. spin_lock_init(&cluster->refill_lock);
  1828. cluster->root = RB_ROOT;
  1829. cluster->max_size = 0;
  1830. cluster->points_to_bitmap = false;
  1831. INIT_LIST_HEAD(&cluster->block_group_list);
  1832. cluster->block_group = NULL;
  1833. }