free-space-cache.c 92 KB

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