free-space-cache.c 77 KB

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