free-space-cache.c 92 KB

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