free-space-cache.c 77 KB

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