free-space-cache.c 93 KB

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