free-space-cache.c 70 KB

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