free-space-cache.c 90 KB

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