free-space-cache.c 94 KB

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