free-space-cache.c 64 KB

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