free-space-cache.c 72 KB

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