free-space-cache.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  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. u64 size = block_group->key.offset;
  889. /*
  890. * The goal is to keep the total amount of memory used per 1gb of space
  891. * at or below 32k, so we need to adjust how much memory we allow to be
  892. * used by extent based free space tracking
  893. */
  894. if (size < 1024 * 1024 * 1024)
  895. max_bytes = MAX_CACHE_BYTES_PER_GIG;
  896. else
  897. max_bytes = MAX_CACHE_BYTES_PER_GIG *
  898. div64_u64(size, 1024 * 1024 * 1024);
  899. /*
  900. * we want to account for 1 more bitmap than what we have so we can make
  901. * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
  902. * we add more bitmaps.
  903. */
  904. bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
  905. if (bitmap_bytes >= max_bytes) {
  906. block_group->extents_thresh = 0;
  907. return;
  908. }
  909. /*
  910. * we want the extent entry threshold to always be at most 1/2 the maxw
  911. * bytes we can have, or whatever is less than that.
  912. */
  913. extent_bytes = max_bytes - bitmap_bytes;
  914. extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
  915. block_group->extents_thresh =
  916. div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
  917. }
  918. static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
  919. struct btrfs_free_space *info, u64 offset,
  920. u64 bytes)
  921. {
  922. unsigned long start, end;
  923. unsigned long i;
  924. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  925. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  926. BUG_ON(end > BITS_PER_BITMAP);
  927. for (i = start; i < end; i++)
  928. clear_bit(i, info->bitmap);
  929. info->bytes -= bytes;
  930. block_group->free_space -= bytes;
  931. }
  932. static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
  933. struct btrfs_free_space *info, u64 offset,
  934. u64 bytes)
  935. {
  936. unsigned long start, end;
  937. unsigned long i;
  938. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  939. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  940. BUG_ON(end > BITS_PER_BITMAP);
  941. for (i = start; i < end; i++)
  942. set_bit(i, info->bitmap);
  943. info->bytes += bytes;
  944. block_group->free_space += bytes;
  945. }
  946. static int search_bitmap(struct btrfs_block_group_cache *block_group,
  947. struct btrfs_free_space *bitmap_info, u64 *offset,
  948. u64 *bytes)
  949. {
  950. unsigned long found_bits = 0;
  951. unsigned long bits, i;
  952. unsigned long next_zero;
  953. i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
  954. max_t(u64, *offset, bitmap_info->offset));
  955. bits = bytes_to_bits(*bytes, block_group->sectorsize);
  956. for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
  957. i < BITS_PER_BITMAP;
  958. i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
  959. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  960. BITS_PER_BITMAP, i);
  961. if ((next_zero - i) >= bits) {
  962. found_bits = next_zero - i;
  963. break;
  964. }
  965. i = next_zero;
  966. }
  967. if (found_bits) {
  968. *offset = (u64)(i * block_group->sectorsize) +
  969. bitmap_info->offset;
  970. *bytes = (u64)(found_bits) * block_group->sectorsize;
  971. return 0;
  972. }
  973. return -1;
  974. }
  975. static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
  976. *block_group, u64 *offset,
  977. u64 *bytes, int debug)
  978. {
  979. struct btrfs_free_space *entry;
  980. struct rb_node *node;
  981. int ret;
  982. if (!block_group->free_space_offset.rb_node)
  983. return NULL;
  984. entry = tree_search_offset(block_group,
  985. offset_to_bitmap(block_group, *offset),
  986. 0, 1);
  987. if (!entry)
  988. return NULL;
  989. for (node = &entry->offset_index; node; node = rb_next(node)) {
  990. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  991. if (entry->bytes < *bytes)
  992. continue;
  993. if (entry->bitmap) {
  994. ret = search_bitmap(block_group, entry, offset, bytes);
  995. if (!ret)
  996. return entry;
  997. continue;
  998. }
  999. *offset = entry->offset;
  1000. *bytes = entry->bytes;
  1001. return entry;
  1002. }
  1003. return NULL;
  1004. }
  1005. static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
  1006. struct btrfs_free_space *info, u64 offset)
  1007. {
  1008. u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
  1009. int max_bitmaps = (int)div64_u64(block_group->key.offset +
  1010. bytes_per_bg - 1, bytes_per_bg);
  1011. BUG_ON(block_group->total_bitmaps >= max_bitmaps);
  1012. info->offset = offset_to_bitmap(block_group, offset);
  1013. info->bytes = 0;
  1014. link_free_space(block_group, info);
  1015. block_group->total_bitmaps++;
  1016. recalculate_thresholds(block_group);
  1017. }
  1018. static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
  1019. struct btrfs_free_space *bitmap_info,
  1020. u64 *offset, u64 *bytes)
  1021. {
  1022. u64 end;
  1023. u64 search_start, search_bytes;
  1024. int ret;
  1025. again:
  1026. end = bitmap_info->offset +
  1027. (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
  1028. /*
  1029. * XXX - this can go away after a few releases.
  1030. *
  1031. * since the only user of btrfs_remove_free_space is the tree logging
  1032. * stuff, and the only way to test that is under crash conditions, we
  1033. * want to have this debug stuff here just in case somethings not
  1034. * working. Search the bitmap for the space we are trying to use to
  1035. * make sure its actually there. If its not there then we need to stop
  1036. * because something has gone wrong.
  1037. */
  1038. search_start = *offset;
  1039. search_bytes = *bytes;
  1040. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1041. &search_bytes);
  1042. BUG_ON(ret < 0 || search_start != *offset);
  1043. if (*offset > bitmap_info->offset && *offset + *bytes > end) {
  1044. bitmap_clear_bits(block_group, bitmap_info, *offset,
  1045. end - *offset + 1);
  1046. *bytes -= end - *offset + 1;
  1047. *offset = end + 1;
  1048. } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
  1049. bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
  1050. *bytes = 0;
  1051. }
  1052. if (*bytes) {
  1053. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1054. if (!bitmap_info->bytes) {
  1055. unlink_free_space(block_group, bitmap_info);
  1056. kfree(bitmap_info->bitmap);
  1057. kfree(bitmap_info);
  1058. block_group->total_bitmaps--;
  1059. recalculate_thresholds(block_group);
  1060. }
  1061. /*
  1062. * no entry after this bitmap, but we still have bytes to
  1063. * remove, so something has gone wrong.
  1064. */
  1065. if (!next)
  1066. return -EINVAL;
  1067. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1068. offset_index);
  1069. /*
  1070. * if the next entry isn't a bitmap we need to return to let the
  1071. * extent stuff do its work.
  1072. */
  1073. if (!bitmap_info->bitmap)
  1074. return -EAGAIN;
  1075. /*
  1076. * Ok the next item is a bitmap, but it may not actually hold
  1077. * the information for the rest of this free space stuff, so
  1078. * look for it, and if we don't find it return so we can try
  1079. * everything over again.
  1080. */
  1081. search_start = *offset;
  1082. search_bytes = *bytes;
  1083. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1084. &search_bytes);
  1085. if (ret < 0 || search_start != *offset)
  1086. return -EAGAIN;
  1087. goto again;
  1088. } else if (!bitmap_info->bytes) {
  1089. unlink_free_space(block_group, bitmap_info);
  1090. kfree(bitmap_info->bitmap);
  1091. kfree(bitmap_info);
  1092. block_group->total_bitmaps--;
  1093. recalculate_thresholds(block_group);
  1094. }
  1095. return 0;
  1096. }
  1097. static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
  1098. struct btrfs_free_space *info)
  1099. {
  1100. struct btrfs_free_space *bitmap_info;
  1101. int added = 0;
  1102. u64 bytes, offset, end;
  1103. int ret;
  1104. /*
  1105. * If we are below the extents threshold then we can add this as an
  1106. * extent, and don't have to deal with the bitmap
  1107. */
  1108. if (block_group->free_extents < block_group->extents_thresh &&
  1109. info->bytes > block_group->sectorsize * 4)
  1110. return 0;
  1111. /*
  1112. * some block groups are so tiny they can't be enveloped by a bitmap, so
  1113. * don't even bother to create a bitmap for this
  1114. */
  1115. if (BITS_PER_BITMAP * block_group->sectorsize >
  1116. block_group->key.offset)
  1117. return 0;
  1118. bytes = info->bytes;
  1119. offset = info->offset;
  1120. again:
  1121. bitmap_info = tree_search_offset(block_group,
  1122. offset_to_bitmap(block_group, offset),
  1123. 1, 0);
  1124. if (!bitmap_info) {
  1125. BUG_ON(added);
  1126. goto new_bitmap;
  1127. }
  1128. end = bitmap_info->offset +
  1129. (u64)(BITS_PER_BITMAP * block_group->sectorsize);
  1130. if (offset >= bitmap_info->offset && offset + bytes > end) {
  1131. bitmap_set_bits(block_group, bitmap_info, offset,
  1132. end - offset);
  1133. bytes -= end - offset;
  1134. offset = end;
  1135. added = 0;
  1136. } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
  1137. bitmap_set_bits(block_group, bitmap_info, offset, bytes);
  1138. bytes = 0;
  1139. } else {
  1140. BUG();
  1141. }
  1142. if (!bytes) {
  1143. ret = 1;
  1144. goto out;
  1145. } else
  1146. goto again;
  1147. new_bitmap:
  1148. if (info && info->bitmap) {
  1149. add_new_bitmap(block_group, info, offset);
  1150. added = 1;
  1151. info = NULL;
  1152. goto again;
  1153. } else {
  1154. spin_unlock(&block_group->tree_lock);
  1155. /* no pre-allocated info, allocate a new one */
  1156. if (!info) {
  1157. info = kzalloc(sizeof(struct btrfs_free_space),
  1158. GFP_NOFS);
  1159. if (!info) {
  1160. spin_lock(&block_group->tree_lock);
  1161. ret = -ENOMEM;
  1162. goto out;
  1163. }
  1164. }
  1165. /* allocate the bitmap */
  1166. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1167. spin_lock(&block_group->tree_lock);
  1168. if (!info->bitmap) {
  1169. ret = -ENOMEM;
  1170. goto out;
  1171. }
  1172. goto again;
  1173. }
  1174. out:
  1175. if (info) {
  1176. if (info->bitmap)
  1177. kfree(info->bitmap);
  1178. kfree(info);
  1179. }
  1180. return ret;
  1181. }
  1182. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  1183. u64 offset, u64 bytes)
  1184. {
  1185. struct btrfs_free_space *right_info = NULL;
  1186. struct btrfs_free_space *left_info = NULL;
  1187. struct btrfs_free_space *info = NULL;
  1188. int ret = 0;
  1189. info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  1190. if (!info)
  1191. return -ENOMEM;
  1192. info->offset = offset;
  1193. info->bytes = bytes;
  1194. spin_lock(&block_group->tree_lock);
  1195. /*
  1196. * first we want to see if there is free space adjacent to the range we
  1197. * are adding, if there is remove that struct and add a new one to
  1198. * cover the entire range
  1199. */
  1200. right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
  1201. if (right_info && rb_prev(&right_info->offset_index))
  1202. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1203. struct btrfs_free_space, offset_index);
  1204. else
  1205. left_info = tree_search_offset(block_group, offset - 1, 0, 0);
  1206. /*
  1207. * If there was no extent directly to the left or right of this new
  1208. * extent then we know we're going to have to allocate a new extent, so
  1209. * before we do that see if we need to drop this into a bitmap
  1210. */
  1211. if ((!left_info || left_info->bitmap) &&
  1212. (!right_info || right_info->bitmap)) {
  1213. ret = insert_into_bitmap(block_group, info);
  1214. if (ret < 0) {
  1215. goto out;
  1216. } else if (ret) {
  1217. ret = 0;
  1218. goto out;
  1219. }
  1220. }
  1221. if (right_info && !right_info->bitmap) {
  1222. unlink_free_space(block_group, right_info);
  1223. info->bytes += right_info->bytes;
  1224. kfree(right_info);
  1225. }
  1226. if (left_info && !left_info->bitmap &&
  1227. left_info->offset + left_info->bytes == offset) {
  1228. unlink_free_space(block_group, left_info);
  1229. info->offset = left_info->offset;
  1230. info->bytes += left_info->bytes;
  1231. kfree(left_info);
  1232. }
  1233. ret = link_free_space(block_group, info);
  1234. if (ret)
  1235. kfree(info);
  1236. out:
  1237. spin_unlock(&block_group->tree_lock);
  1238. if (ret) {
  1239. printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
  1240. BUG_ON(ret == -EEXIST);
  1241. }
  1242. return ret;
  1243. }
  1244. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1245. u64 offset, u64 bytes)
  1246. {
  1247. struct btrfs_free_space *info;
  1248. struct btrfs_free_space *next_info = NULL;
  1249. int ret = 0;
  1250. spin_lock(&block_group->tree_lock);
  1251. again:
  1252. info = tree_search_offset(block_group, offset, 0, 0);
  1253. if (!info) {
  1254. /*
  1255. * oops didn't find an extent that matched the space we wanted
  1256. * to remove, look for a bitmap instead
  1257. */
  1258. info = tree_search_offset(block_group,
  1259. offset_to_bitmap(block_group, offset),
  1260. 1, 0);
  1261. if (!info) {
  1262. WARN_ON(1);
  1263. goto out_lock;
  1264. }
  1265. }
  1266. if (info->bytes < bytes && rb_next(&info->offset_index)) {
  1267. u64 end;
  1268. next_info = rb_entry(rb_next(&info->offset_index),
  1269. struct btrfs_free_space,
  1270. offset_index);
  1271. if (next_info->bitmap)
  1272. end = next_info->offset + BITS_PER_BITMAP *
  1273. block_group->sectorsize - 1;
  1274. else
  1275. end = next_info->offset + next_info->bytes;
  1276. if (next_info->bytes < bytes ||
  1277. next_info->offset > offset || offset > end) {
  1278. printk(KERN_CRIT "Found free space at %llu, size %llu,"
  1279. " trying to use %llu\n",
  1280. (unsigned long long)info->offset,
  1281. (unsigned long long)info->bytes,
  1282. (unsigned long long)bytes);
  1283. WARN_ON(1);
  1284. ret = -EINVAL;
  1285. goto out_lock;
  1286. }
  1287. info = next_info;
  1288. }
  1289. if (info->bytes == bytes) {
  1290. unlink_free_space(block_group, info);
  1291. if (info->bitmap) {
  1292. kfree(info->bitmap);
  1293. block_group->total_bitmaps--;
  1294. }
  1295. kfree(info);
  1296. goto out_lock;
  1297. }
  1298. if (!info->bitmap && info->offset == offset) {
  1299. unlink_free_space(block_group, info);
  1300. info->offset += bytes;
  1301. info->bytes -= bytes;
  1302. link_free_space(block_group, info);
  1303. goto out_lock;
  1304. }
  1305. if (!info->bitmap && info->offset <= offset &&
  1306. info->offset + info->bytes >= offset + bytes) {
  1307. u64 old_start = info->offset;
  1308. /*
  1309. * we're freeing space in the middle of the info,
  1310. * this can happen during tree log replay
  1311. *
  1312. * first unlink the old info and then
  1313. * insert it again after the hole we're creating
  1314. */
  1315. unlink_free_space(block_group, info);
  1316. if (offset + bytes < info->offset + info->bytes) {
  1317. u64 old_end = info->offset + info->bytes;
  1318. info->offset = offset + bytes;
  1319. info->bytes = old_end - info->offset;
  1320. ret = link_free_space(block_group, info);
  1321. WARN_ON(ret);
  1322. if (ret)
  1323. goto out_lock;
  1324. } else {
  1325. /* the hole we're creating ends at the end
  1326. * of the info struct, just free the info
  1327. */
  1328. kfree(info);
  1329. }
  1330. spin_unlock(&block_group->tree_lock);
  1331. /* step two, insert a new info struct to cover
  1332. * anything before the hole
  1333. */
  1334. ret = btrfs_add_free_space(block_group, old_start,
  1335. offset - old_start);
  1336. WARN_ON(ret);
  1337. goto out;
  1338. }
  1339. ret = remove_from_bitmap(block_group, info, &offset, &bytes);
  1340. if (ret == -EAGAIN)
  1341. goto again;
  1342. BUG_ON(ret);
  1343. out_lock:
  1344. spin_unlock(&block_group->tree_lock);
  1345. out:
  1346. return ret;
  1347. }
  1348. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  1349. u64 bytes)
  1350. {
  1351. struct btrfs_free_space *info;
  1352. struct rb_node *n;
  1353. int count = 0;
  1354. for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
  1355. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1356. if (info->bytes >= bytes)
  1357. count++;
  1358. printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
  1359. (unsigned long long)info->offset,
  1360. (unsigned long long)info->bytes,
  1361. (info->bitmap) ? "yes" : "no");
  1362. }
  1363. printk(KERN_INFO "block group has cluster?: %s\n",
  1364. list_empty(&block_group->cluster_list) ? "no" : "yes");
  1365. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  1366. "\n", count);
  1367. }
  1368. u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
  1369. {
  1370. struct btrfs_free_space *info;
  1371. struct rb_node *n;
  1372. u64 ret = 0;
  1373. for (n = rb_first(&block_group->free_space_offset); n;
  1374. n = rb_next(n)) {
  1375. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1376. ret += info->bytes;
  1377. }
  1378. return ret;
  1379. }
  1380. /*
  1381. * for a given cluster, put all of its extents back into the free
  1382. * space cache. If the block group passed doesn't match the block group
  1383. * pointed to by the cluster, someone else raced in and freed the
  1384. * cluster already. In that case, we just return without changing anything
  1385. */
  1386. static int
  1387. __btrfs_return_cluster_to_free_space(
  1388. struct btrfs_block_group_cache *block_group,
  1389. struct btrfs_free_cluster *cluster)
  1390. {
  1391. struct btrfs_free_space *entry;
  1392. struct rb_node *node;
  1393. bool bitmap;
  1394. spin_lock(&cluster->lock);
  1395. if (cluster->block_group != block_group)
  1396. goto out;
  1397. bitmap = cluster->points_to_bitmap;
  1398. cluster->block_group = NULL;
  1399. cluster->window_start = 0;
  1400. list_del_init(&cluster->block_group_list);
  1401. cluster->points_to_bitmap = false;
  1402. if (bitmap)
  1403. goto out;
  1404. node = rb_first(&cluster->root);
  1405. while (node) {
  1406. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1407. node = rb_next(&entry->offset_index);
  1408. rb_erase(&entry->offset_index, &cluster->root);
  1409. BUG_ON(entry->bitmap);
  1410. tree_insert_offset(&block_group->free_space_offset,
  1411. entry->offset, &entry->offset_index, 0);
  1412. }
  1413. cluster->root = RB_ROOT;
  1414. out:
  1415. spin_unlock(&cluster->lock);
  1416. btrfs_put_block_group(block_group);
  1417. return 0;
  1418. }
  1419. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  1420. {
  1421. struct btrfs_free_space *info;
  1422. struct rb_node *node;
  1423. struct btrfs_free_cluster *cluster;
  1424. struct list_head *head;
  1425. spin_lock(&block_group->tree_lock);
  1426. while ((head = block_group->cluster_list.next) !=
  1427. &block_group->cluster_list) {
  1428. cluster = list_entry(head, struct btrfs_free_cluster,
  1429. block_group_list);
  1430. WARN_ON(cluster->block_group != block_group);
  1431. __btrfs_return_cluster_to_free_space(block_group, cluster);
  1432. if (need_resched()) {
  1433. spin_unlock(&block_group->tree_lock);
  1434. cond_resched();
  1435. spin_lock(&block_group->tree_lock);
  1436. }
  1437. }
  1438. while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
  1439. info = rb_entry(node, struct btrfs_free_space, offset_index);
  1440. unlink_free_space(block_group, info);
  1441. if (info->bitmap)
  1442. kfree(info->bitmap);
  1443. kfree(info);
  1444. if (need_resched()) {
  1445. spin_unlock(&block_group->tree_lock);
  1446. cond_resched();
  1447. spin_lock(&block_group->tree_lock);
  1448. }
  1449. }
  1450. spin_unlock(&block_group->tree_lock);
  1451. }
  1452. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  1453. u64 offset, u64 bytes, u64 empty_size)
  1454. {
  1455. struct btrfs_free_space *entry = NULL;
  1456. u64 bytes_search = bytes + empty_size;
  1457. u64 ret = 0;
  1458. spin_lock(&block_group->tree_lock);
  1459. entry = find_free_space(block_group, &offset, &bytes_search, 0);
  1460. if (!entry)
  1461. goto out;
  1462. ret = offset;
  1463. if (entry->bitmap) {
  1464. bitmap_clear_bits(block_group, entry, offset, bytes);
  1465. if (!entry->bytes) {
  1466. unlink_free_space(block_group, entry);
  1467. kfree(entry->bitmap);
  1468. kfree(entry);
  1469. block_group->total_bitmaps--;
  1470. recalculate_thresholds(block_group);
  1471. }
  1472. } else {
  1473. unlink_free_space(block_group, entry);
  1474. entry->offset += bytes;
  1475. entry->bytes -= bytes;
  1476. if (!entry->bytes)
  1477. kfree(entry);
  1478. else
  1479. link_free_space(block_group, entry);
  1480. }
  1481. out:
  1482. spin_unlock(&block_group->tree_lock);
  1483. return ret;
  1484. }
  1485. /*
  1486. * given a cluster, put all of its extents back into the free space
  1487. * cache. If a block group is passed, this function will only free
  1488. * a cluster that belongs to the passed block group.
  1489. *
  1490. * Otherwise, it'll get a reference on the block group pointed to by the
  1491. * cluster and remove the cluster from it.
  1492. */
  1493. int btrfs_return_cluster_to_free_space(
  1494. struct btrfs_block_group_cache *block_group,
  1495. struct btrfs_free_cluster *cluster)
  1496. {
  1497. int ret;
  1498. /* first, get a safe pointer to the block group */
  1499. spin_lock(&cluster->lock);
  1500. if (!block_group) {
  1501. block_group = cluster->block_group;
  1502. if (!block_group) {
  1503. spin_unlock(&cluster->lock);
  1504. return 0;
  1505. }
  1506. } else if (cluster->block_group != block_group) {
  1507. /* someone else has already freed it don't redo their work */
  1508. spin_unlock(&cluster->lock);
  1509. return 0;
  1510. }
  1511. atomic_inc(&block_group->count);
  1512. spin_unlock(&cluster->lock);
  1513. /* now return any extents the cluster had on it */
  1514. spin_lock(&block_group->tree_lock);
  1515. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  1516. spin_unlock(&block_group->tree_lock);
  1517. /* finally drop our ref */
  1518. btrfs_put_block_group(block_group);
  1519. return ret;
  1520. }
  1521. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  1522. struct btrfs_free_cluster *cluster,
  1523. u64 bytes, u64 min_start)
  1524. {
  1525. struct btrfs_free_space *entry;
  1526. int err;
  1527. u64 search_start = cluster->window_start;
  1528. u64 search_bytes = bytes;
  1529. u64 ret = 0;
  1530. spin_lock(&block_group->tree_lock);
  1531. spin_lock(&cluster->lock);
  1532. if (!cluster->points_to_bitmap)
  1533. goto out;
  1534. if (cluster->block_group != block_group)
  1535. goto out;
  1536. /*
  1537. * search_start is the beginning of the bitmap, but at some point it may
  1538. * be a good idea to point to the actual start of the free area in the
  1539. * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
  1540. * to 1 to make sure we get the bitmap entry
  1541. */
  1542. entry = tree_search_offset(block_group,
  1543. offset_to_bitmap(block_group, search_start),
  1544. 1, 0);
  1545. if (!entry || !entry->bitmap)
  1546. goto out;
  1547. search_start = min_start;
  1548. search_bytes = bytes;
  1549. err = search_bitmap(block_group, entry, &search_start,
  1550. &search_bytes);
  1551. if (err)
  1552. goto out;
  1553. ret = search_start;
  1554. bitmap_clear_bits(block_group, entry, ret, bytes);
  1555. out:
  1556. spin_unlock(&cluster->lock);
  1557. spin_unlock(&block_group->tree_lock);
  1558. return ret;
  1559. }
  1560. /*
  1561. * given a cluster, try to allocate 'bytes' from it, returns 0
  1562. * if it couldn't find anything suitably large, or a logical disk offset
  1563. * if things worked out
  1564. */
  1565. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  1566. struct btrfs_free_cluster *cluster, u64 bytes,
  1567. u64 min_start)
  1568. {
  1569. struct btrfs_free_space *entry = NULL;
  1570. struct rb_node *node;
  1571. u64 ret = 0;
  1572. if (cluster->points_to_bitmap)
  1573. return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
  1574. min_start);
  1575. spin_lock(&cluster->lock);
  1576. if (bytes > cluster->max_size)
  1577. goto out;
  1578. if (cluster->block_group != block_group)
  1579. goto out;
  1580. node = rb_first(&cluster->root);
  1581. if (!node)
  1582. goto out;
  1583. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1584. while(1) {
  1585. if (entry->bytes < bytes || entry->offset < min_start) {
  1586. struct rb_node *node;
  1587. node = rb_next(&entry->offset_index);
  1588. if (!node)
  1589. break;
  1590. entry = rb_entry(node, struct btrfs_free_space,
  1591. offset_index);
  1592. continue;
  1593. }
  1594. ret = entry->offset;
  1595. entry->offset += bytes;
  1596. entry->bytes -= bytes;
  1597. if (entry->bytes == 0) {
  1598. rb_erase(&entry->offset_index, &cluster->root);
  1599. kfree(entry);
  1600. }
  1601. break;
  1602. }
  1603. out:
  1604. spin_unlock(&cluster->lock);
  1605. return ret;
  1606. }
  1607. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  1608. struct btrfs_free_space *entry,
  1609. struct btrfs_free_cluster *cluster,
  1610. u64 offset, u64 bytes, u64 min_bytes)
  1611. {
  1612. unsigned long next_zero;
  1613. unsigned long i;
  1614. unsigned long search_bits;
  1615. unsigned long total_bits;
  1616. unsigned long found_bits;
  1617. unsigned long start = 0;
  1618. unsigned long total_found = 0;
  1619. bool found = false;
  1620. i = offset_to_bit(entry->offset, block_group->sectorsize,
  1621. max_t(u64, offset, entry->offset));
  1622. search_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
  1623. total_bits = bytes_to_bits(bytes, block_group->sectorsize);
  1624. again:
  1625. found_bits = 0;
  1626. for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
  1627. i < BITS_PER_BITMAP;
  1628. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
  1629. next_zero = find_next_zero_bit(entry->bitmap,
  1630. BITS_PER_BITMAP, i);
  1631. if (next_zero - i >= search_bits) {
  1632. found_bits = next_zero - i;
  1633. break;
  1634. }
  1635. i = next_zero;
  1636. }
  1637. if (!found_bits)
  1638. return -1;
  1639. if (!found) {
  1640. start = i;
  1641. found = true;
  1642. }
  1643. total_found += found_bits;
  1644. if (cluster->max_size < found_bits * block_group->sectorsize)
  1645. cluster->max_size = found_bits * block_group->sectorsize;
  1646. if (total_found < total_bits) {
  1647. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
  1648. if (i - start > total_bits * 2) {
  1649. total_found = 0;
  1650. cluster->max_size = 0;
  1651. found = false;
  1652. }
  1653. goto again;
  1654. }
  1655. cluster->window_start = start * block_group->sectorsize +
  1656. entry->offset;
  1657. cluster->points_to_bitmap = true;
  1658. return 0;
  1659. }
  1660. /*
  1661. * here we try to find a cluster of blocks in a block group. The goal
  1662. * is to find at least bytes free and up to empty_size + bytes free.
  1663. * We might not find them all in one contiguous area.
  1664. *
  1665. * returns zero and sets up cluster if things worked out, otherwise
  1666. * it returns -enospc
  1667. */
  1668. int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
  1669. struct btrfs_root *root,
  1670. struct btrfs_block_group_cache *block_group,
  1671. struct btrfs_free_cluster *cluster,
  1672. u64 offset, u64 bytes, u64 empty_size)
  1673. {
  1674. struct btrfs_free_space *entry = NULL;
  1675. struct rb_node *node;
  1676. struct btrfs_free_space *next;
  1677. struct btrfs_free_space *last = NULL;
  1678. u64 min_bytes;
  1679. u64 window_start;
  1680. u64 window_free;
  1681. u64 max_extent = 0;
  1682. bool found_bitmap = false;
  1683. int ret;
  1684. /* for metadata, allow allocates with more holes */
  1685. if (btrfs_test_opt(root, SSD_SPREAD)) {
  1686. min_bytes = bytes + empty_size;
  1687. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1688. /*
  1689. * we want to do larger allocations when we are
  1690. * flushing out the delayed refs, it helps prevent
  1691. * making more work as we go along.
  1692. */
  1693. if (trans->transaction->delayed_refs.flushing)
  1694. min_bytes = max(bytes, (bytes + empty_size) >> 1);
  1695. else
  1696. min_bytes = max(bytes, (bytes + empty_size) >> 4);
  1697. } else
  1698. min_bytes = max(bytes, (bytes + empty_size) >> 2);
  1699. spin_lock(&block_group->tree_lock);
  1700. spin_lock(&cluster->lock);
  1701. /* someone already found a cluster, hooray */
  1702. if (cluster->block_group) {
  1703. ret = 0;
  1704. goto out;
  1705. }
  1706. again:
  1707. entry = tree_search_offset(block_group, offset, found_bitmap, 1);
  1708. if (!entry) {
  1709. ret = -ENOSPC;
  1710. goto out;
  1711. }
  1712. /*
  1713. * If found_bitmap is true, we exhausted our search for extent entries,
  1714. * and we just want to search all of the bitmaps that we can find, and
  1715. * ignore any extent entries we find.
  1716. */
  1717. while (entry->bitmap || found_bitmap ||
  1718. (!entry->bitmap && entry->bytes < min_bytes)) {
  1719. struct rb_node *node = rb_next(&entry->offset_index);
  1720. if (entry->bitmap && entry->bytes > bytes + empty_size) {
  1721. ret = btrfs_bitmap_cluster(block_group, entry, cluster,
  1722. offset, bytes + empty_size,
  1723. min_bytes);
  1724. if (!ret)
  1725. goto got_it;
  1726. }
  1727. if (!node) {
  1728. ret = -ENOSPC;
  1729. goto out;
  1730. }
  1731. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1732. }
  1733. /*
  1734. * We already searched all the extent entries from the passed in offset
  1735. * to the end and didn't find enough space for the cluster, and we also
  1736. * didn't find any bitmaps that met our criteria, just go ahead and exit
  1737. */
  1738. if (found_bitmap) {
  1739. ret = -ENOSPC;
  1740. goto out;
  1741. }
  1742. cluster->points_to_bitmap = false;
  1743. window_start = entry->offset;
  1744. window_free = entry->bytes;
  1745. last = entry;
  1746. max_extent = entry->bytes;
  1747. while (1) {
  1748. /* out window is just right, lets fill it */
  1749. if (window_free >= bytes + empty_size)
  1750. break;
  1751. node = rb_next(&last->offset_index);
  1752. if (!node) {
  1753. if (found_bitmap)
  1754. goto again;
  1755. ret = -ENOSPC;
  1756. goto out;
  1757. }
  1758. next = rb_entry(node, struct btrfs_free_space, offset_index);
  1759. /*
  1760. * we found a bitmap, so if this search doesn't result in a
  1761. * cluster, we know to go and search again for the bitmaps and
  1762. * start looking for space there
  1763. */
  1764. if (next->bitmap) {
  1765. if (!found_bitmap)
  1766. offset = next->offset;
  1767. found_bitmap = true;
  1768. last = next;
  1769. continue;
  1770. }
  1771. /*
  1772. * we haven't filled the empty size and the window is
  1773. * very large. reset and try again
  1774. */
  1775. if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
  1776. next->offset - window_start > (bytes + empty_size) * 2) {
  1777. entry = next;
  1778. window_start = entry->offset;
  1779. window_free = entry->bytes;
  1780. last = entry;
  1781. max_extent = entry->bytes;
  1782. } else {
  1783. last = next;
  1784. window_free += next->bytes;
  1785. if (entry->bytes > max_extent)
  1786. max_extent = entry->bytes;
  1787. }
  1788. }
  1789. cluster->window_start = entry->offset;
  1790. /*
  1791. * now we've found our entries, pull them out of the free space
  1792. * cache and put them into the cluster rbtree
  1793. *
  1794. * The cluster includes an rbtree, but only uses the offset index
  1795. * of each free space cache entry.
  1796. */
  1797. while (1) {
  1798. node = rb_next(&entry->offset_index);
  1799. if (entry->bitmap && node) {
  1800. entry = rb_entry(node, struct btrfs_free_space,
  1801. offset_index);
  1802. continue;
  1803. } else if (entry->bitmap && !node) {
  1804. break;
  1805. }
  1806. rb_erase(&entry->offset_index, &block_group->free_space_offset);
  1807. ret = tree_insert_offset(&cluster->root, entry->offset,
  1808. &entry->offset_index, 0);
  1809. BUG_ON(ret);
  1810. if (!node || entry == last)
  1811. break;
  1812. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1813. }
  1814. cluster->max_size = max_extent;
  1815. got_it:
  1816. ret = 0;
  1817. atomic_inc(&block_group->count);
  1818. list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
  1819. cluster->block_group = block_group;
  1820. out:
  1821. spin_unlock(&cluster->lock);
  1822. spin_unlock(&block_group->tree_lock);
  1823. return ret;
  1824. }
  1825. /*
  1826. * simple code to zero out a cluster
  1827. */
  1828. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  1829. {
  1830. spin_lock_init(&cluster->lock);
  1831. spin_lock_init(&cluster->refill_lock);
  1832. cluster->root = RB_ROOT;
  1833. cluster->max_size = 0;
  1834. cluster->points_to_bitmap = false;
  1835. INIT_LIST_HEAD(&cluster->block_group_list);
  1836. cluster->block_group = NULL;
  1837. }