free-space-cache.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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 out;
  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. last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  461. filemap_write_and_wait(inode->i_mapping);
  462. btrfs_wait_ordered_range(inode, inode->i_size &
  463. ~(root->sectorsize - 1), (u64)-1);
  464. /* We need a checksum per page. */
  465. num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
  466. crc = checksums = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
  467. if (!crc) {
  468. iput(inode);
  469. return 0;
  470. }
  471. /* Since the first page has all of our checksums and our generation we
  472. * need to calculate the offset into the page that we can start writing
  473. * our entries.
  474. */
  475. first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
  476. node = rb_first(&block_group->free_space_offset);
  477. if (!node)
  478. goto out_free;
  479. /*
  480. * Lock all pages first so we can lock the extent safely.
  481. *
  482. * NOTE: Because we hold the ref the entire time we're going to write to
  483. * the page find_get_page should never fail, so we don't do a check
  484. * after find_get_page at this point. Just putting this here so people
  485. * know and don't freak out.
  486. */
  487. while (index <= last_index) {
  488. page = grab_cache_page(inode->i_mapping, index);
  489. if (!page) {
  490. pgoff_t i = 0;
  491. while (i < index) {
  492. page = find_get_page(inode->i_mapping, i);
  493. unlock_page(page);
  494. page_cache_release(page);
  495. page_cache_release(page);
  496. i++;
  497. }
  498. goto out_free;
  499. }
  500. index++;
  501. }
  502. index = 0;
  503. lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  504. 0, &cached_state, GFP_NOFS);
  505. /* Write out the extent entries */
  506. do {
  507. struct btrfs_free_space_entry *entry;
  508. void *addr;
  509. unsigned long offset = 0;
  510. unsigned long start_offset = 0;
  511. if (index == 0) {
  512. start_offset = first_page_offset;
  513. offset = start_offset;
  514. }
  515. page = find_get_page(inode->i_mapping, index);
  516. addr = kmap(page);
  517. entry = addr + start_offset;
  518. memset(addr, 0, PAGE_CACHE_SIZE);
  519. while (1) {
  520. struct btrfs_free_space *e;
  521. e = rb_entry(node, struct btrfs_free_space, offset_index);
  522. entries++;
  523. entry->offset = cpu_to_le64(e->offset);
  524. entry->bytes = cpu_to_le64(e->bytes);
  525. if (e->bitmap) {
  526. entry->type = BTRFS_FREE_SPACE_BITMAP;
  527. list_add_tail(&e->list, &bitmap_list);
  528. bitmaps++;
  529. } else {
  530. entry->type = BTRFS_FREE_SPACE_EXTENT;
  531. }
  532. node = rb_next(node);
  533. if (!node)
  534. break;
  535. offset += sizeof(struct btrfs_free_space_entry);
  536. if (offset + sizeof(struct btrfs_free_space_entry) >=
  537. PAGE_CACHE_SIZE)
  538. break;
  539. entry++;
  540. }
  541. *crc = ~(u32)0;
  542. *crc = btrfs_csum_data(root, addr + start_offset, *crc,
  543. PAGE_CACHE_SIZE - start_offset);
  544. kunmap(page);
  545. btrfs_csum_final(*crc, (char *)crc);
  546. crc++;
  547. bytes += PAGE_CACHE_SIZE;
  548. ClearPageChecked(page);
  549. set_page_extent_mapped(page);
  550. SetPageUptodate(page);
  551. set_page_dirty(page);
  552. /*
  553. * We need to release our reference we got for grab_cache_page,
  554. * except for the first page which will hold our checksums, we
  555. * do that below.
  556. */
  557. if (index != 0) {
  558. unlock_page(page);
  559. page_cache_release(page);
  560. }
  561. page_cache_release(page);
  562. index++;
  563. } while (node);
  564. /* Write out the bitmaps */
  565. list_for_each_safe(pos, n, &bitmap_list) {
  566. void *addr;
  567. struct btrfs_free_space *entry =
  568. list_entry(pos, struct btrfs_free_space, list);
  569. page = find_get_page(inode->i_mapping, index);
  570. addr = kmap(page);
  571. memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
  572. *crc = ~(u32)0;
  573. *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
  574. kunmap(page);
  575. btrfs_csum_final(*crc, (char *)crc);
  576. crc++;
  577. bytes += PAGE_CACHE_SIZE;
  578. ClearPageChecked(page);
  579. set_page_extent_mapped(page);
  580. SetPageUptodate(page);
  581. set_page_dirty(page);
  582. unlock_page(page);
  583. page_cache_release(page);
  584. page_cache_release(page);
  585. list_del_init(&entry->list);
  586. index++;
  587. }
  588. /* Zero out the rest of the pages just to make sure */
  589. while (index <= last_index) {
  590. void *addr;
  591. page = find_get_page(inode->i_mapping, index);
  592. addr = kmap(page);
  593. memset(addr, 0, PAGE_CACHE_SIZE);
  594. kunmap(page);
  595. ClearPageChecked(page);
  596. set_page_extent_mapped(page);
  597. SetPageUptodate(page);
  598. set_page_dirty(page);
  599. unlock_page(page);
  600. page_cache_release(page);
  601. page_cache_release(page);
  602. bytes += PAGE_CACHE_SIZE;
  603. index++;
  604. }
  605. btrfs_set_extent_delalloc(inode, 0, bytes - 1, &cached_state);
  606. /* Write the checksums and trans id to the first page */
  607. {
  608. void *addr;
  609. u64 *gen;
  610. page = find_get_page(inode->i_mapping, 0);
  611. addr = kmap(page);
  612. memcpy(addr, checksums, sizeof(u32) * num_checksums);
  613. gen = addr + (sizeof(u32) * num_checksums);
  614. *gen = trans->transid;
  615. kunmap(page);
  616. ClearPageChecked(page);
  617. set_page_extent_mapped(page);
  618. SetPageUptodate(page);
  619. set_page_dirty(page);
  620. unlock_page(page);
  621. page_cache_release(page);
  622. page_cache_release(page);
  623. }
  624. BTRFS_I(inode)->generation = trans->transid;
  625. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  626. i_size_read(inode) - 1, &cached_state, GFP_NOFS);
  627. filemap_write_and_wait(inode->i_mapping);
  628. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  629. key.offset = block_group->key.objectid;
  630. key.type = 0;
  631. ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
  632. if (ret < 0) {
  633. ret = 0;
  634. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  635. EXTENT_DIRTY | EXTENT_DELALLOC |
  636. EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
  637. goto out_free;
  638. }
  639. leaf = path->nodes[0];
  640. if (ret > 0) {
  641. struct btrfs_key found_key;
  642. BUG_ON(!path->slots[0]);
  643. path->slots[0]--;
  644. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  645. if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
  646. found_key.offset != block_group->key.objectid) {
  647. ret = 0;
  648. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  649. EXTENT_DIRTY | EXTENT_DELALLOC |
  650. EXTENT_DO_ACCOUNTING, 0, 0, NULL,
  651. GFP_NOFS);
  652. btrfs_release_path(root, path);
  653. goto out_free;
  654. }
  655. }
  656. header = btrfs_item_ptr(leaf, path->slots[0],
  657. struct btrfs_free_space_header);
  658. btrfs_set_free_space_entries(leaf, header, entries);
  659. btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
  660. btrfs_set_free_space_generation(leaf, header, trans->transid);
  661. btrfs_mark_buffer_dirty(leaf);
  662. btrfs_release_path(root, path);
  663. ret = 1;
  664. out_free:
  665. if (ret == 0) {
  666. invalidate_inode_pages2_range(inode->i_mapping, 0, index);
  667. spin_lock(&block_group->lock);
  668. block_group->disk_cache_state = BTRFS_DC_ERROR;
  669. spin_unlock(&block_group->lock);
  670. BTRFS_I(inode)->generation = 0;
  671. }
  672. kfree(checksums);
  673. btrfs_update_inode(trans, root, inode);
  674. iput(inode);
  675. return ret;
  676. }
  677. static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
  678. u64 offset)
  679. {
  680. BUG_ON(offset < bitmap_start);
  681. offset -= bitmap_start;
  682. return (unsigned long)(div64_u64(offset, sectorsize));
  683. }
  684. static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
  685. {
  686. return (unsigned long)(div64_u64(bytes, sectorsize));
  687. }
  688. static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
  689. u64 offset)
  690. {
  691. u64 bitmap_start;
  692. u64 bytes_per_bitmap;
  693. bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
  694. bitmap_start = offset - block_group->key.objectid;
  695. bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
  696. bitmap_start *= bytes_per_bitmap;
  697. bitmap_start += block_group->key.objectid;
  698. return bitmap_start;
  699. }
  700. static int tree_insert_offset(struct rb_root *root, u64 offset,
  701. struct rb_node *node, int bitmap)
  702. {
  703. struct rb_node **p = &root->rb_node;
  704. struct rb_node *parent = NULL;
  705. struct btrfs_free_space *info;
  706. while (*p) {
  707. parent = *p;
  708. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  709. if (offset < info->offset) {
  710. p = &(*p)->rb_left;
  711. } else if (offset > info->offset) {
  712. p = &(*p)->rb_right;
  713. } else {
  714. /*
  715. * we could have a bitmap entry and an extent entry
  716. * share the same offset. If this is the case, we want
  717. * the extent entry to always be found first if we do a
  718. * linear search through the tree, since we want to have
  719. * the quickest allocation time, and allocating from an
  720. * extent is faster than allocating from a bitmap. So
  721. * if we're inserting a bitmap and we find an entry at
  722. * this offset, we want to go right, or after this entry
  723. * logically. If we are inserting an extent and we've
  724. * found a bitmap, we want to go left, or before
  725. * logically.
  726. */
  727. if (bitmap) {
  728. WARN_ON(info->bitmap);
  729. p = &(*p)->rb_right;
  730. } else {
  731. WARN_ON(!info->bitmap);
  732. p = &(*p)->rb_left;
  733. }
  734. }
  735. }
  736. rb_link_node(node, parent, p);
  737. rb_insert_color(node, root);
  738. return 0;
  739. }
  740. /*
  741. * searches the tree for the given offset.
  742. *
  743. * fuzzy - If this is set, then we are trying to make an allocation, and we just
  744. * want a section that has at least bytes size and comes at or after the given
  745. * offset.
  746. */
  747. static struct btrfs_free_space *
  748. tree_search_offset(struct btrfs_block_group_cache *block_group,
  749. u64 offset, int bitmap_only, int fuzzy)
  750. {
  751. struct rb_node *n = block_group->free_space_offset.rb_node;
  752. struct btrfs_free_space *entry, *prev = NULL;
  753. /* find entry that is closest to the 'offset' */
  754. while (1) {
  755. if (!n) {
  756. entry = NULL;
  757. break;
  758. }
  759. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  760. prev = entry;
  761. if (offset < entry->offset)
  762. n = n->rb_left;
  763. else if (offset > entry->offset)
  764. n = n->rb_right;
  765. else
  766. break;
  767. }
  768. if (bitmap_only) {
  769. if (!entry)
  770. return NULL;
  771. if (entry->bitmap)
  772. return entry;
  773. /*
  774. * bitmap entry and extent entry may share same offset,
  775. * in that case, bitmap entry comes after extent entry.
  776. */
  777. n = rb_next(n);
  778. if (!n)
  779. return NULL;
  780. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  781. if (entry->offset != offset)
  782. return NULL;
  783. WARN_ON(!entry->bitmap);
  784. return entry;
  785. } else if (entry) {
  786. if (entry->bitmap) {
  787. /*
  788. * if previous extent entry covers the offset,
  789. * we should return it instead of the bitmap entry
  790. */
  791. n = &entry->offset_index;
  792. while (1) {
  793. n = rb_prev(n);
  794. if (!n)
  795. break;
  796. prev = rb_entry(n, struct btrfs_free_space,
  797. offset_index);
  798. if (!prev->bitmap) {
  799. if (prev->offset + prev->bytes > offset)
  800. entry = prev;
  801. break;
  802. }
  803. }
  804. }
  805. return entry;
  806. }
  807. if (!prev)
  808. return NULL;
  809. /* find last entry before the 'offset' */
  810. entry = prev;
  811. if (entry->offset > offset) {
  812. n = rb_prev(&entry->offset_index);
  813. if (n) {
  814. entry = rb_entry(n, struct btrfs_free_space,
  815. offset_index);
  816. BUG_ON(entry->offset > offset);
  817. } else {
  818. if (fuzzy)
  819. return entry;
  820. else
  821. return NULL;
  822. }
  823. }
  824. if (entry->bitmap) {
  825. n = &entry->offset_index;
  826. while (1) {
  827. n = rb_prev(n);
  828. if (!n)
  829. break;
  830. prev = rb_entry(n, struct btrfs_free_space,
  831. offset_index);
  832. if (!prev->bitmap) {
  833. if (prev->offset + prev->bytes > offset)
  834. return prev;
  835. break;
  836. }
  837. }
  838. if (entry->offset + BITS_PER_BITMAP *
  839. block_group->sectorsize > offset)
  840. return entry;
  841. } else if (entry->offset + entry->bytes > offset)
  842. return entry;
  843. if (!fuzzy)
  844. return NULL;
  845. while (1) {
  846. if (entry->bitmap) {
  847. if (entry->offset + BITS_PER_BITMAP *
  848. block_group->sectorsize > offset)
  849. break;
  850. } else {
  851. if (entry->offset + entry->bytes > offset)
  852. break;
  853. }
  854. n = rb_next(&entry->offset_index);
  855. if (!n)
  856. return NULL;
  857. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  858. }
  859. return entry;
  860. }
  861. static void unlink_free_space(struct btrfs_block_group_cache *block_group,
  862. struct btrfs_free_space *info)
  863. {
  864. rb_erase(&info->offset_index, &block_group->free_space_offset);
  865. block_group->free_extents--;
  866. block_group->free_space -= info->bytes;
  867. }
  868. static int link_free_space(struct btrfs_block_group_cache *block_group,
  869. struct btrfs_free_space *info)
  870. {
  871. int ret = 0;
  872. BUG_ON(!info->bitmap && !info->bytes);
  873. ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
  874. &info->offset_index, (info->bitmap != NULL));
  875. if (ret)
  876. return ret;
  877. block_group->free_space += info->bytes;
  878. block_group->free_extents++;
  879. return ret;
  880. }
  881. static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
  882. {
  883. u64 max_bytes;
  884. u64 bitmap_bytes;
  885. u64 extent_bytes;
  886. /*
  887. * The goal is to keep the total amount of memory used per 1gb of space
  888. * at or below 32k, so we need to adjust how much memory we allow to be
  889. * used by extent based free space tracking
  890. */
  891. max_bytes = MAX_CACHE_BYTES_PER_GIG *
  892. (div64_u64(block_group->key.offset, 1024 * 1024 * 1024));
  893. /*
  894. * we want to account for 1 more bitmap than what we have so we can make
  895. * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
  896. * we add more bitmaps.
  897. */
  898. bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
  899. if (bitmap_bytes >= max_bytes) {
  900. block_group->extents_thresh = 0;
  901. return;
  902. }
  903. /*
  904. * we want the extent entry threshold to always be at most 1/2 the maxw
  905. * bytes we can have, or whatever is less than that.
  906. */
  907. extent_bytes = max_bytes - bitmap_bytes;
  908. extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
  909. block_group->extents_thresh =
  910. div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
  911. }
  912. static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
  913. struct btrfs_free_space *info, u64 offset,
  914. u64 bytes)
  915. {
  916. unsigned long start, end;
  917. unsigned long i;
  918. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  919. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  920. BUG_ON(end > BITS_PER_BITMAP);
  921. for (i = start; i < end; i++)
  922. clear_bit(i, info->bitmap);
  923. info->bytes -= bytes;
  924. block_group->free_space -= bytes;
  925. }
  926. static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
  927. struct btrfs_free_space *info, u64 offset,
  928. u64 bytes)
  929. {
  930. unsigned long start, end;
  931. unsigned long i;
  932. start = offset_to_bit(info->offset, block_group->sectorsize, offset);
  933. end = start + bytes_to_bits(bytes, block_group->sectorsize);
  934. BUG_ON(end > BITS_PER_BITMAP);
  935. for (i = start; i < end; i++)
  936. set_bit(i, info->bitmap);
  937. info->bytes += bytes;
  938. block_group->free_space += bytes;
  939. }
  940. static int search_bitmap(struct btrfs_block_group_cache *block_group,
  941. struct btrfs_free_space *bitmap_info, u64 *offset,
  942. u64 *bytes)
  943. {
  944. unsigned long found_bits = 0;
  945. unsigned long bits, i;
  946. unsigned long next_zero;
  947. i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
  948. max_t(u64, *offset, bitmap_info->offset));
  949. bits = bytes_to_bits(*bytes, block_group->sectorsize);
  950. for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
  951. i < BITS_PER_BITMAP;
  952. i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
  953. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  954. BITS_PER_BITMAP, i);
  955. if ((next_zero - i) >= bits) {
  956. found_bits = next_zero - i;
  957. break;
  958. }
  959. i = next_zero;
  960. }
  961. if (found_bits) {
  962. *offset = (u64)(i * block_group->sectorsize) +
  963. bitmap_info->offset;
  964. *bytes = (u64)(found_bits) * block_group->sectorsize;
  965. return 0;
  966. }
  967. return -1;
  968. }
  969. static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
  970. *block_group, u64 *offset,
  971. u64 *bytes, int debug)
  972. {
  973. struct btrfs_free_space *entry;
  974. struct rb_node *node;
  975. int ret;
  976. if (!block_group->free_space_offset.rb_node)
  977. return NULL;
  978. entry = tree_search_offset(block_group,
  979. offset_to_bitmap(block_group, *offset),
  980. 0, 1);
  981. if (!entry)
  982. return NULL;
  983. for (node = &entry->offset_index; node; node = rb_next(node)) {
  984. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  985. if (entry->bytes < *bytes)
  986. continue;
  987. if (entry->bitmap) {
  988. ret = search_bitmap(block_group, entry, offset, bytes);
  989. if (!ret)
  990. return entry;
  991. continue;
  992. }
  993. *offset = entry->offset;
  994. *bytes = entry->bytes;
  995. return entry;
  996. }
  997. return NULL;
  998. }
  999. static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
  1000. struct btrfs_free_space *info, u64 offset)
  1001. {
  1002. u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
  1003. int max_bitmaps = (int)div64_u64(block_group->key.offset +
  1004. bytes_per_bg - 1, bytes_per_bg);
  1005. BUG_ON(block_group->total_bitmaps >= max_bitmaps);
  1006. info->offset = offset_to_bitmap(block_group, offset);
  1007. info->bytes = 0;
  1008. link_free_space(block_group, info);
  1009. block_group->total_bitmaps++;
  1010. recalculate_thresholds(block_group);
  1011. }
  1012. static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
  1013. struct btrfs_free_space *bitmap_info,
  1014. u64 *offset, u64 *bytes)
  1015. {
  1016. u64 end;
  1017. u64 search_start, search_bytes;
  1018. int ret;
  1019. again:
  1020. end = bitmap_info->offset +
  1021. (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
  1022. /*
  1023. * XXX - this can go away after a few releases.
  1024. *
  1025. * since the only user of btrfs_remove_free_space is the tree logging
  1026. * stuff, and the only way to test that is under crash conditions, we
  1027. * want to have this debug stuff here just in case somethings not
  1028. * working. Search the bitmap for the space we are trying to use to
  1029. * make sure its actually there. If its not there then we need to stop
  1030. * because something has gone wrong.
  1031. */
  1032. search_start = *offset;
  1033. search_bytes = *bytes;
  1034. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1035. &search_bytes);
  1036. BUG_ON(ret < 0 || search_start != *offset);
  1037. if (*offset > bitmap_info->offset && *offset + *bytes > end) {
  1038. bitmap_clear_bits(block_group, bitmap_info, *offset,
  1039. end - *offset + 1);
  1040. *bytes -= end - *offset + 1;
  1041. *offset = end + 1;
  1042. } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
  1043. bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
  1044. *bytes = 0;
  1045. }
  1046. if (*bytes) {
  1047. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1048. if (!bitmap_info->bytes) {
  1049. unlink_free_space(block_group, bitmap_info);
  1050. kfree(bitmap_info->bitmap);
  1051. kfree(bitmap_info);
  1052. block_group->total_bitmaps--;
  1053. recalculate_thresholds(block_group);
  1054. }
  1055. /*
  1056. * no entry after this bitmap, but we still have bytes to
  1057. * remove, so something has gone wrong.
  1058. */
  1059. if (!next)
  1060. return -EINVAL;
  1061. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1062. offset_index);
  1063. /*
  1064. * if the next entry isn't a bitmap we need to return to let the
  1065. * extent stuff do its work.
  1066. */
  1067. if (!bitmap_info->bitmap)
  1068. return -EAGAIN;
  1069. /*
  1070. * Ok the next item is a bitmap, but it may not actually hold
  1071. * the information for the rest of this free space stuff, so
  1072. * look for it, and if we don't find it return so we can try
  1073. * everything over again.
  1074. */
  1075. search_start = *offset;
  1076. search_bytes = *bytes;
  1077. ret = search_bitmap(block_group, bitmap_info, &search_start,
  1078. &search_bytes);
  1079. if (ret < 0 || search_start != *offset)
  1080. return -EAGAIN;
  1081. goto again;
  1082. } else if (!bitmap_info->bytes) {
  1083. unlink_free_space(block_group, bitmap_info);
  1084. kfree(bitmap_info->bitmap);
  1085. kfree(bitmap_info);
  1086. block_group->total_bitmaps--;
  1087. recalculate_thresholds(block_group);
  1088. }
  1089. return 0;
  1090. }
  1091. static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
  1092. struct btrfs_free_space *info)
  1093. {
  1094. struct btrfs_free_space *bitmap_info;
  1095. int added = 0;
  1096. u64 bytes, offset, end;
  1097. int ret;
  1098. /*
  1099. * If we are below the extents threshold then we can add this as an
  1100. * extent, and don't have to deal with the bitmap
  1101. */
  1102. if (block_group->free_extents < block_group->extents_thresh &&
  1103. info->bytes > block_group->sectorsize * 4)
  1104. return 0;
  1105. /*
  1106. * some block groups are so tiny they can't be enveloped by a bitmap, so
  1107. * don't even bother to create a bitmap for this
  1108. */
  1109. if (BITS_PER_BITMAP * block_group->sectorsize >
  1110. block_group->key.offset)
  1111. return 0;
  1112. bytes = info->bytes;
  1113. offset = info->offset;
  1114. again:
  1115. bitmap_info = tree_search_offset(block_group,
  1116. offset_to_bitmap(block_group, offset),
  1117. 1, 0);
  1118. if (!bitmap_info) {
  1119. BUG_ON(added);
  1120. goto new_bitmap;
  1121. }
  1122. end = bitmap_info->offset +
  1123. (u64)(BITS_PER_BITMAP * block_group->sectorsize);
  1124. if (offset >= bitmap_info->offset && offset + bytes > end) {
  1125. bitmap_set_bits(block_group, bitmap_info, offset,
  1126. end - offset);
  1127. bytes -= end - offset;
  1128. offset = end;
  1129. added = 0;
  1130. } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
  1131. bitmap_set_bits(block_group, bitmap_info, offset, bytes);
  1132. bytes = 0;
  1133. } else {
  1134. BUG();
  1135. }
  1136. if (!bytes) {
  1137. ret = 1;
  1138. goto out;
  1139. } else
  1140. goto again;
  1141. new_bitmap:
  1142. if (info && info->bitmap) {
  1143. add_new_bitmap(block_group, info, offset);
  1144. added = 1;
  1145. info = NULL;
  1146. goto again;
  1147. } else {
  1148. spin_unlock(&block_group->tree_lock);
  1149. /* no pre-allocated info, allocate a new one */
  1150. if (!info) {
  1151. info = kzalloc(sizeof(struct btrfs_free_space),
  1152. GFP_NOFS);
  1153. if (!info) {
  1154. spin_lock(&block_group->tree_lock);
  1155. ret = -ENOMEM;
  1156. goto out;
  1157. }
  1158. }
  1159. /* allocate the bitmap */
  1160. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1161. spin_lock(&block_group->tree_lock);
  1162. if (!info->bitmap) {
  1163. ret = -ENOMEM;
  1164. goto out;
  1165. }
  1166. goto again;
  1167. }
  1168. out:
  1169. if (info) {
  1170. if (info->bitmap)
  1171. kfree(info->bitmap);
  1172. kfree(info);
  1173. }
  1174. return ret;
  1175. }
  1176. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  1177. u64 offset, u64 bytes)
  1178. {
  1179. struct btrfs_free_space *right_info = NULL;
  1180. struct btrfs_free_space *left_info = NULL;
  1181. struct btrfs_free_space *info = NULL;
  1182. int ret = 0;
  1183. info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  1184. if (!info)
  1185. return -ENOMEM;
  1186. info->offset = offset;
  1187. info->bytes = bytes;
  1188. spin_lock(&block_group->tree_lock);
  1189. /*
  1190. * first we want to see if there is free space adjacent to the range we
  1191. * are adding, if there is remove that struct and add a new one to
  1192. * cover the entire range
  1193. */
  1194. right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
  1195. if (right_info && rb_prev(&right_info->offset_index))
  1196. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1197. struct btrfs_free_space, offset_index);
  1198. else
  1199. left_info = tree_search_offset(block_group, offset - 1, 0, 0);
  1200. /*
  1201. * If there was no extent directly to the left or right of this new
  1202. * extent then we know we're going to have to allocate a new extent, so
  1203. * before we do that see if we need to drop this into a bitmap
  1204. */
  1205. if ((!left_info || left_info->bitmap) &&
  1206. (!right_info || right_info->bitmap)) {
  1207. ret = insert_into_bitmap(block_group, info);
  1208. if (ret < 0) {
  1209. goto out;
  1210. } else if (ret) {
  1211. ret = 0;
  1212. goto out;
  1213. }
  1214. }
  1215. if (right_info && !right_info->bitmap) {
  1216. unlink_free_space(block_group, right_info);
  1217. info->bytes += right_info->bytes;
  1218. kfree(right_info);
  1219. }
  1220. if (left_info && !left_info->bitmap &&
  1221. left_info->offset + left_info->bytes == offset) {
  1222. unlink_free_space(block_group, left_info);
  1223. info->offset = left_info->offset;
  1224. info->bytes += left_info->bytes;
  1225. kfree(left_info);
  1226. }
  1227. ret = link_free_space(block_group, info);
  1228. if (ret)
  1229. kfree(info);
  1230. out:
  1231. spin_unlock(&block_group->tree_lock);
  1232. if (ret) {
  1233. printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
  1234. BUG_ON(ret == -EEXIST);
  1235. }
  1236. return ret;
  1237. }
  1238. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1239. u64 offset, u64 bytes)
  1240. {
  1241. struct btrfs_free_space *info;
  1242. struct btrfs_free_space *next_info = NULL;
  1243. int ret = 0;
  1244. spin_lock(&block_group->tree_lock);
  1245. again:
  1246. info = tree_search_offset(block_group, offset, 0, 0);
  1247. if (!info) {
  1248. /*
  1249. * oops didn't find an extent that matched the space we wanted
  1250. * to remove, look for a bitmap instead
  1251. */
  1252. info = tree_search_offset(block_group,
  1253. offset_to_bitmap(block_group, offset),
  1254. 1, 0);
  1255. if (!info) {
  1256. WARN_ON(1);
  1257. goto out_lock;
  1258. }
  1259. }
  1260. if (info->bytes < bytes && rb_next(&info->offset_index)) {
  1261. u64 end;
  1262. next_info = rb_entry(rb_next(&info->offset_index),
  1263. struct btrfs_free_space,
  1264. offset_index);
  1265. if (next_info->bitmap)
  1266. end = next_info->offset + BITS_PER_BITMAP *
  1267. block_group->sectorsize - 1;
  1268. else
  1269. end = next_info->offset + next_info->bytes;
  1270. if (next_info->bytes < bytes ||
  1271. next_info->offset > offset || offset > end) {
  1272. printk(KERN_CRIT "Found free space at %llu, size %llu,"
  1273. " trying to use %llu\n",
  1274. (unsigned long long)info->offset,
  1275. (unsigned long long)info->bytes,
  1276. (unsigned long long)bytes);
  1277. WARN_ON(1);
  1278. ret = -EINVAL;
  1279. goto out_lock;
  1280. }
  1281. info = next_info;
  1282. }
  1283. if (info->bytes == bytes) {
  1284. unlink_free_space(block_group, info);
  1285. if (info->bitmap) {
  1286. kfree(info->bitmap);
  1287. block_group->total_bitmaps--;
  1288. }
  1289. kfree(info);
  1290. goto out_lock;
  1291. }
  1292. if (!info->bitmap && info->offset == offset) {
  1293. unlink_free_space(block_group, info);
  1294. info->offset += bytes;
  1295. info->bytes -= bytes;
  1296. link_free_space(block_group, info);
  1297. goto out_lock;
  1298. }
  1299. if (!info->bitmap && info->offset <= offset &&
  1300. info->offset + info->bytes >= offset + bytes) {
  1301. u64 old_start = info->offset;
  1302. /*
  1303. * we're freeing space in the middle of the info,
  1304. * this can happen during tree log replay
  1305. *
  1306. * first unlink the old info and then
  1307. * insert it again after the hole we're creating
  1308. */
  1309. unlink_free_space(block_group, info);
  1310. if (offset + bytes < info->offset + info->bytes) {
  1311. u64 old_end = info->offset + info->bytes;
  1312. info->offset = offset + bytes;
  1313. info->bytes = old_end - info->offset;
  1314. ret = link_free_space(block_group, info);
  1315. WARN_ON(ret);
  1316. if (ret)
  1317. goto out_lock;
  1318. } else {
  1319. /* the hole we're creating ends at the end
  1320. * of the info struct, just free the info
  1321. */
  1322. kfree(info);
  1323. }
  1324. spin_unlock(&block_group->tree_lock);
  1325. /* step two, insert a new info struct to cover
  1326. * anything before the hole
  1327. */
  1328. ret = btrfs_add_free_space(block_group, old_start,
  1329. offset - old_start);
  1330. WARN_ON(ret);
  1331. goto out;
  1332. }
  1333. ret = remove_from_bitmap(block_group, info, &offset, &bytes);
  1334. if (ret == -EAGAIN)
  1335. goto again;
  1336. BUG_ON(ret);
  1337. out_lock:
  1338. spin_unlock(&block_group->tree_lock);
  1339. out:
  1340. return ret;
  1341. }
  1342. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  1343. u64 bytes)
  1344. {
  1345. struct btrfs_free_space *info;
  1346. struct rb_node *n;
  1347. int count = 0;
  1348. for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
  1349. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1350. if (info->bytes >= bytes)
  1351. count++;
  1352. printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
  1353. (unsigned long long)info->offset,
  1354. (unsigned long long)info->bytes,
  1355. (info->bitmap) ? "yes" : "no");
  1356. }
  1357. printk(KERN_INFO "block group has cluster?: %s\n",
  1358. list_empty(&block_group->cluster_list) ? "no" : "yes");
  1359. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  1360. "\n", count);
  1361. }
  1362. u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
  1363. {
  1364. struct btrfs_free_space *info;
  1365. struct rb_node *n;
  1366. u64 ret = 0;
  1367. for (n = rb_first(&block_group->free_space_offset); n;
  1368. n = rb_next(n)) {
  1369. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1370. ret += info->bytes;
  1371. }
  1372. return ret;
  1373. }
  1374. /*
  1375. * for a given cluster, put all of its extents back into the free
  1376. * space cache. If the block group passed doesn't match the block group
  1377. * pointed to by the cluster, someone else raced in and freed the
  1378. * cluster already. In that case, we just return without changing anything
  1379. */
  1380. static int
  1381. __btrfs_return_cluster_to_free_space(
  1382. struct btrfs_block_group_cache *block_group,
  1383. struct btrfs_free_cluster *cluster)
  1384. {
  1385. struct btrfs_free_space *entry;
  1386. struct rb_node *node;
  1387. bool bitmap;
  1388. spin_lock(&cluster->lock);
  1389. if (cluster->block_group != block_group)
  1390. goto out;
  1391. bitmap = cluster->points_to_bitmap;
  1392. cluster->block_group = NULL;
  1393. cluster->window_start = 0;
  1394. list_del_init(&cluster->block_group_list);
  1395. cluster->points_to_bitmap = false;
  1396. if (bitmap)
  1397. goto out;
  1398. node = rb_first(&cluster->root);
  1399. while (node) {
  1400. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1401. node = rb_next(&entry->offset_index);
  1402. rb_erase(&entry->offset_index, &cluster->root);
  1403. BUG_ON(entry->bitmap);
  1404. tree_insert_offset(&block_group->free_space_offset,
  1405. entry->offset, &entry->offset_index, 0);
  1406. }
  1407. cluster->root = RB_ROOT;
  1408. out:
  1409. spin_unlock(&cluster->lock);
  1410. btrfs_put_block_group(block_group);
  1411. return 0;
  1412. }
  1413. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  1414. {
  1415. struct btrfs_free_space *info;
  1416. struct rb_node *node;
  1417. struct btrfs_free_cluster *cluster;
  1418. struct list_head *head;
  1419. spin_lock(&block_group->tree_lock);
  1420. while ((head = block_group->cluster_list.next) !=
  1421. &block_group->cluster_list) {
  1422. cluster = list_entry(head, struct btrfs_free_cluster,
  1423. block_group_list);
  1424. WARN_ON(cluster->block_group != block_group);
  1425. __btrfs_return_cluster_to_free_space(block_group, cluster);
  1426. if (need_resched()) {
  1427. spin_unlock(&block_group->tree_lock);
  1428. cond_resched();
  1429. spin_lock(&block_group->tree_lock);
  1430. }
  1431. }
  1432. while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
  1433. info = rb_entry(node, struct btrfs_free_space, offset_index);
  1434. unlink_free_space(block_group, info);
  1435. if (info->bitmap)
  1436. kfree(info->bitmap);
  1437. kfree(info);
  1438. if (need_resched()) {
  1439. spin_unlock(&block_group->tree_lock);
  1440. cond_resched();
  1441. spin_lock(&block_group->tree_lock);
  1442. }
  1443. }
  1444. spin_unlock(&block_group->tree_lock);
  1445. }
  1446. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  1447. u64 offset, u64 bytes, u64 empty_size)
  1448. {
  1449. struct btrfs_free_space *entry = NULL;
  1450. u64 bytes_search = bytes + empty_size;
  1451. u64 ret = 0;
  1452. spin_lock(&block_group->tree_lock);
  1453. entry = find_free_space(block_group, &offset, &bytes_search, 0);
  1454. if (!entry)
  1455. goto out;
  1456. ret = offset;
  1457. if (entry->bitmap) {
  1458. bitmap_clear_bits(block_group, entry, offset, bytes);
  1459. if (!entry->bytes) {
  1460. unlink_free_space(block_group, entry);
  1461. kfree(entry->bitmap);
  1462. kfree(entry);
  1463. block_group->total_bitmaps--;
  1464. recalculate_thresholds(block_group);
  1465. }
  1466. } else {
  1467. unlink_free_space(block_group, entry);
  1468. entry->offset += bytes;
  1469. entry->bytes -= bytes;
  1470. if (!entry->bytes)
  1471. kfree(entry);
  1472. else
  1473. link_free_space(block_group, entry);
  1474. }
  1475. out:
  1476. spin_unlock(&block_group->tree_lock);
  1477. return ret;
  1478. }
  1479. /*
  1480. * given a cluster, put all of its extents back into the free space
  1481. * cache. If a block group is passed, this function will only free
  1482. * a cluster that belongs to the passed block group.
  1483. *
  1484. * Otherwise, it'll get a reference on the block group pointed to by the
  1485. * cluster and remove the cluster from it.
  1486. */
  1487. int btrfs_return_cluster_to_free_space(
  1488. struct btrfs_block_group_cache *block_group,
  1489. struct btrfs_free_cluster *cluster)
  1490. {
  1491. int ret;
  1492. /* first, get a safe pointer to the block group */
  1493. spin_lock(&cluster->lock);
  1494. if (!block_group) {
  1495. block_group = cluster->block_group;
  1496. if (!block_group) {
  1497. spin_unlock(&cluster->lock);
  1498. return 0;
  1499. }
  1500. } else if (cluster->block_group != block_group) {
  1501. /* someone else has already freed it don't redo their work */
  1502. spin_unlock(&cluster->lock);
  1503. return 0;
  1504. }
  1505. atomic_inc(&block_group->count);
  1506. spin_unlock(&cluster->lock);
  1507. /* now return any extents the cluster had on it */
  1508. spin_lock(&block_group->tree_lock);
  1509. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  1510. spin_unlock(&block_group->tree_lock);
  1511. /* finally drop our ref */
  1512. btrfs_put_block_group(block_group);
  1513. return ret;
  1514. }
  1515. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  1516. struct btrfs_free_cluster *cluster,
  1517. u64 bytes, u64 min_start)
  1518. {
  1519. struct btrfs_free_space *entry;
  1520. int err;
  1521. u64 search_start = cluster->window_start;
  1522. u64 search_bytes = bytes;
  1523. u64 ret = 0;
  1524. spin_lock(&block_group->tree_lock);
  1525. spin_lock(&cluster->lock);
  1526. if (!cluster->points_to_bitmap)
  1527. goto out;
  1528. if (cluster->block_group != block_group)
  1529. goto out;
  1530. /*
  1531. * search_start is the beginning of the bitmap, but at some point it may
  1532. * be a good idea to point to the actual start of the free area in the
  1533. * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
  1534. * to 1 to make sure we get the bitmap entry
  1535. */
  1536. entry = tree_search_offset(block_group,
  1537. offset_to_bitmap(block_group, search_start),
  1538. 1, 0);
  1539. if (!entry || !entry->bitmap)
  1540. goto out;
  1541. search_start = min_start;
  1542. search_bytes = bytes;
  1543. err = search_bitmap(block_group, entry, &search_start,
  1544. &search_bytes);
  1545. if (err)
  1546. goto out;
  1547. ret = search_start;
  1548. bitmap_clear_bits(block_group, entry, ret, bytes);
  1549. out:
  1550. spin_unlock(&cluster->lock);
  1551. spin_unlock(&block_group->tree_lock);
  1552. return ret;
  1553. }
  1554. /*
  1555. * given a cluster, try to allocate 'bytes' from it, returns 0
  1556. * if it couldn't find anything suitably large, or a logical disk offset
  1557. * if things worked out
  1558. */
  1559. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  1560. struct btrfs_free_cluster *cluster, u64 bytes,
  1561. u64 min_start)
  1562. {
  1563. struct btrfs_free_space *entry = NULL;
  1564. struct rb_node *node;
  1565. u64 ret = 0;
  1566. if (cluster->points_to_bitmap)
  1567. return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
  1568. min_start);
  1569. spin_lock(&cluster->lock);
  1570. if (bytes > cluster->max_size)
  1571. goto out;
  1572. if (cluster->block_group != block_group)
  1573. goto out;
  1574. node = rb_first(&cluster->root);
  1575. if (!node)
  1576. goto out;
  1577. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1578. while(1) {
  1579. if (entry->bytes < bytes || entry->offset < min_start) {
  1580. struct rb_node *node;
  1581. node = rb_next(&entry->offset_index);
  1582. if (!node)
  1583. break;
  1584. entry = rb_entry(node, struct btrfs_free_space,
  1585. offset_index);
  1586. continue;
  1587. }
  1588. ret = entry->offset;
  1589. entry->offset += bytes;
  1590. entry->bytes -= bytes;
  1591. if (entry->bytes == 0) {
  1592. rb_erase(&entry->offset_index, &cluster->root);
  1593. kfree(entry);
  1594. }
  1595. break;
  1596. }
  1597. out:
  1598. spin_unlock(&cluster->lock);
  1599. return ret;
  1600. }
  1601. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  1602. struct btrfs_free_space *entry,
  1603. struct btrfs_free_cluster *cluster,
  1604. u64 offset, u64 bytes, u64 min_bytes)
  1605. {
  1606. unsigned long next_zero;
  1607. unsigned long i;
  1608. unsigned long search_bits;
  1609. unsigned long total_bits;
  1610. unsigned long found_bits;
  1611. unsigned long start = 0;
  1612. unsigned long total_found = 0;
  1613. bool found = false;
  1614. i = offset_to_bit(entry->offset, block_group->sectorsize,
  1615. max_t(u64, offset, entry->offset));
  1616. search_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
  1617. total_bits = bytes_to_bits(bytes, block_group->sectorsize);
  1618. again:
  1619. found_bits = 0;
  1620. for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
  1621. i < BITS_PER_BITMAP;
  1622. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
  1623. next_zero = find_next_zero_bit(entry->bitmap,
  1624. BITS_PER_BITMAP, i);
  1625. if (next_zero - i >= search_bits) {
  1626. found_bits = next_zero - i;
  1627. break;
  1628. }
  1629. i = next_zero;
  1630. }
  1631. if (!found_bits)
  1632. return -1;
  1633. if (!found) {
  1634. start = i;
  1635. found = true;
  1636. }
  1637. total_found += found_bits;
  1638. if (cluster->max_size < found_bits * block_group->sectorsize)
  1639. cluster->max_size = found_bits * block_group->sectorsize;
  1640. if (total_found < total_bits) {
  1641. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
  1642. if (i - start > total_bits * 2) {
  1643. total_found = 0;
  1644. cluster->max_size = 0;
  1645. found = false;
  1646. }
  1647. goto again;
  1648. }
  1649. cluster->window_start = start * block_group->sectorsize +
  1650. entry->offset;
  1651. cluster->points_to_bitmap = true;
  1652. return 0;
  1653. }
  1654. /*
  1655. * here we try to find a cluster of blocks in a block group. The goal
  1656. * is to find at least bytes free and up to empty_size + bytes free.
  1657. * We might not find them all in one contiguous area.
  1658. *
  1659. * returns zero and sets up cluster if things worked out, otherwise
  1660. * it returns -enospc
  1661. */
  1662. int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
  1663. struct btrfs_root *root,
  1664. struct btrfs_block_group_cache *block_group,
  1665. struct btrfs_free_cluster *cluster,
  1666. u64 offset, u64 bytes, u64 empty_size)
  1667. {
  1668. struct btrfs_free_space *entry = NULL;
  1669. struct rb_node *node;
  1670. struct btrfs_free_space *next;
  1671. struct btrfs_free_space *last = NULL;
  1672. u64 min_bytes;
  1673. u64 window_start;
  1674. u64 window_free;
  1675. u64 max_extent = 0;
  1676. bool found_bitmap = false;
  1677. int ret;
  1678. /* for metadata, allow allocates with more holes */
  1679. if (btrfs_test_opt(root, SSD_SPREAD)) {
  1680. min_bytes = bytes + empty_size;
  1681. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1682. /*
  1683. * we want to do larger allocations when we are
  1684. * flushing out the delayed refs, it helps prevent
  1685. * making more work as we go along.
  1686. */
  1687. if (trans->transaction->delayed_refs.flushing)
  1688. min_bytes = max(bytes, (bytes + empty_size) >> 1);
  1689. else
  1690. min_bytes = max(bytes, (bytes + empty_size) >> 4);
  1691. } else
  1692. min_bytes = max(bytes, (bytes + empty_size) >> 2);
  1693. spin_lock(&block_group->tree_lock);
  1694. spin_lock(&cluster->lock);
  1695. /* someone already found a cluster, hooray */
  1696. if (cluster->block_group) {
  1697. ret = 0;
  1698. goto out;
  1699. }
  1700. again:
  1701. entry = tree_search_offset(block_group, offset, found_bitmap, 1);
  1702. if (!entry) {
  1703. ret = -ENOSPC;
  1704. goto out;
  1705. }
  1706. /*
  1707. * If found_bitmap is true, we exhausted our search for extent entries,
  1708. * and we just want to search all of the bitmaps that we can find, and
  1709. * ignore any extent entries we find.
  1710. */
  1711. while (entry->bitmap || found_bitmap ||
  1712. (!entry->bitmap && entry->bytes < min_bytes)) {
  1713. struct rb_node *node = rb_next(&entry->offset_index);
  1714. if (entry->bitmap && entry->bytes > bytes + empty_size) {
  1715. ret = btrfs_bitmap_cluster(block_group, entry, cluster,
  1716. offset, bytes + empty_size,
  1717. min_bytes);
  1718. if (!ret)
  1719. goto got_it;
  1720. }
  1721. if (!node) {
  1722. ret = -ENOSPC;
  1723. goto out;
  1724. }
  1725. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1726. }
  1727. /*
  1728. * We already searched all the extent entries from the passed in offset
  1729. * to the end and didn't find enough space for the cluster, and we also
  1730. * didn't find any bitmaps that met our criteria, just go ahead and exit
  1731. */
  1732. if (found_bitmap) {
  1733. ret = -ENOSPC;
  1734. goto out;
  1735. }
  1736. cluster->points_to_bitmap = false;
  1737. window_start = entry->offset;
  1738. window_free = entry->bytes;
  1739. last = entry;
  1740. max_extent = entry->bytes;
  1741. while (1) {
  1742. /* out window is just right, lets fill it */
  1743. if (window_free >= bytes + empty_size)
  1744. break;
  1745. node = rb_next(&last->offset_index);
  1746. if (!node) {
  1747. if (found_bitmap)
  1748. goto again;
  1749. ret = -ENOSPC;
  1750. goto out;
  1751. }
  1752. next = rb_entry(node, struct btrfs_free_space, offset_index);
  1753. /*
  1754. * we found a bitmap, so if this search doesn't result in a
  1755. * cluster, we know to go and search again for the bitmaps and
  1756. * start looking for space there
  1757. */
  1758. if (next->bitmap) {
  1759. if (!found_bitmap)
  1760. offset = next->offset;
  1761. found_bitmap = true;
  1762. last = next;
  1763. continue;
  1764. }
  1765. /*
  1766. * we haven't filled the empty size and the window is
  1767. * very large. reset and try again
  1768. */
  1769. if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
  1770. next->offset - window_start > (bytes + empty_size) * 2) {
  1771. entry = next;
  1772. window_start = entry->offset;
  1773. window_free = entry->bytes;
  1774. last = entry;
  1775. max_extent = entry->bytes;
  1776. } else {
  1777. last = next;
  1778. window_free += next->bytes;
  1779. if (entry->bytes > max_extent)
  1780. max_extent = entry->bytes;
  1781. }
  1782. }
  1783. cluster->window_start = entry->offset;
  1784. /*
  1785. * now we've found our entries, pull them out of the free space
  1786. * cache and put them into the cluster rbtree
  1787. *
  1788. * The cluster includes an rbtree, but only uses the offset index
  1789. * of each free space cache entry.
  1790. */
  1791. while (1) {
  1792. node = rb_next(&entry->offset_index);
  1793. if (entry->bitmap && node) {
  1794. entry = rb_entry(node, struct btrfs_free_space,
  1795. offset_index);
  1796. continue;
  1797. } else if (entry->bitmap && !node) {
  1798. break;
  1799. }
  1800. rb_erase(&entry->offset_index, &block_group->free_space_offset);
  1801. ret = tree_insert_offset(&cluster->root, entry->offset,
  1802. &entry->offset_index, 0);
  1803. BUG_ON(ret);
  1804. if (!node || entry == last)
  1805. break;
  1806. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1807. }
  1808. cluster->max_size = max_extent;
  1809. got_it:
  1810. ret = 0;
  1811. atomic_inc(&block_group->count);
  1812. list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
  1813. cluster->block_group = block_group;
  1814. out:
  1815. spin_unlock(&cluster->lock);
  1816. spin_unlock(&block_group->tree_lock);
  1817. return ret;
  1818. }
  1819. /*
  1820. * simple code to zero out a cluster
  1821. */
  1822. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  1823. {
  1824. spin_lock_init(&cluster->lock);
  1825. spin_lock_init(&cluster->refill_lock);
  1826. cluster->root = RB_ROOT;
  1827. cluster->max_size = 0;
  1828. cluster->points_to_bitmap = false;
  1829. INIT_LIST_HEAD(&cluster->block_group_list);
  1830. cluster->block_group = NULL;
  1831. }