free-space-cache.c 63 KB

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