free-space-cache.c 92 KB

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