free-space-cache.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641
  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_NOFS & ~__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. * and -1 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 -1;
  1053. WARN_ON(io_ctl->pages);
  1054. ret = io_ctl_init(io_ctl, inode, root, 1);
  1055. if (ret)
  1056. return -1;
  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. io_ctl_prepare_pages(io_ctl, inode, 0);
  1073. lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  1074. 0, &cached_state);
  1075. io_ctl_set_generation(io_ctl, trans->transid);
  1076. mutex_lock(&ctl->cache_writeout_mutex);
  1077. /* Write out the extent entries in the free space cache */
  1078. spin_lock(&ctl->tree_lock);
  1079. ret = write_cache_extent_entries(io_ctl, ctl,
  1080. block_group, &entries, &bitmaps,
  1081. &bitmap_list);
  1082. if (ret)
  1083. goto out_nospc_locked;
  1084. /*
  1085. * Some spaces that are freed in the current transaction are pinned,
  1086. * they will be added into free space cache after the transaction is
  1087. * committed, we shouldn't lose them.
  1088. *
  1089. * If this changes while we are working we'll get added back to
  1090. * the dirty list and redo it. No locking needed
  1091. */
  1092. ret = write_pinned_extent_entries(root, block_group, io_ctl, &entries);
  1093. if (ret)
  1094. goto out_nospc_locked;
  1095. /*
  1096. * At last, we write out all the bitmaps and keep cache_writeout_mutex
  1097. * locked while doing it because a concurrent trim can be manipulating
  1098. * or freeing the bitmap.
  1099. */
  1100. ret = write_bitmap_entries(io_ctl, &bitmap_list);
  1101. spin_unlock(&ctl->tree_lock);
  1102. mutex_unlock(&ctl->cache_writeout_mutex);
  1103. if (ret)
  1104. goto out_nospc;
  1105. /* Zero out the rest of the pages just to make sure */
  1106. io_ctl_zero_remaining_pages(io_ctl);
  1107. /* Everything is written out, now we dirty the pages in the file. */
  1108. ret = btrfs_dirty_pages(root, inode, io_ctl->pages, io_ctl->num_pages,
  1109. 0, i_size_read(inode), &cached_state);
  1110. if (ret)
  1111. goto out_nospc;
  1112. if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
  1113. up_write(&block_group->data_rwsem);
  1114. /*
  1115. * Release the pages and unlock the extent, we will flush
  1116. * them out later
  1117. */
  1118. io_ctl_drop_pages(io_ctl);
  1119. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  1120. i_size_read(inode) - 1, &cached_state, GFP_NOFS);
  1121. /*
  1122. * at this point the pages are under IO and we're happy,
  1123. * The caller is responsible for waiting on them and updating the
  1124. * the cache and the inode
  1125. */
  1126. io_ctl->entries = entries;
  1127. io_ctl->bitmaps = bitmaps;
  1128. ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
  1129. if (ret)
  1130. goto out;
  1131. return 0;
  1132. out:
  1133. io_ctl->inode = NULL;
  1134. io_ctl_free(io_ctl);
  1135. if (ret) {
  1136. invalidate_inode_pages2(inode->i_mapping);
  1137. BTRFS_I(inode)->generation = 0;
  1138. }
  1139. btrfs_update_inode(trans, root, inode);
  1140. if (must_iput)
  1141. iput(inode);
  1142. return ret;
  1143. out_nospc_locked:
  1144. cleanup_bitmap_list(&bitmap_list);
  1145. spin_unlock(&ctl->tree_lock);
  1146. mutex_unlock(&ctl->cache_writeout_mutex);
  1147. out_nospc:
  1148. cleanup_write_cache_enospc(inode, io_ctl, &cached_state, &bitmap_list);
  1149. if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
  1150. up_write(&block_group->data_rwsem);
  1151. goto out;
  1152. }
  1153. int btrfs_write_out_cache(struct btrfs_root *root,
  1154. struct btrfs_trans_handle *trans,
  1155. struct btrfs_block_group_cache *block_group,
  1156. struct btrfs_path *path)
  1157. {
  1158. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1159. struct inode *inode;
  1160. int ret = 0;
  1161. root = root->fs_info->tree_root;
  1162. spin_lock(&block_group->lock);
  1163. if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
  1164. spin_unlock(&block_group->lock);
  1165. return 0;
  1166. }
  1167. spin_unlock(&block_group->lock);
  1168. inode = lookup_free_space_inode(root, block_group, path);
  1169. if (IS_ERR(inode))
  1170. return 0;
  1171. ret = __btrfs_write_out_cache(root, inode, ctl, block_group,
  1172. &block_group->io_ctl, trans,
  1173. path, block_group->key.objectid);
  1174. if (ret) {
  1175. #ifdef DEBUG
  1176. btrfs_err(root->fs_info,
  1177. "failed to write free space cache for block group %llu",
  1178. block_group->key.objectid);
  1179. #endif
  1180. spin_lock(&block_group->lock);
  1181. block_group->disk_cache_state = BTRFS_DC_ERROR;
  1182. spin_unlock(&block_group->lock);
  1183. block_group->io_ctl.inode = NULL;
  1184. iput(inode);
  1185. }
  1186. /*
  1187. * if ret == 0 the caller is expected to call btrfs_wait_cache_io
  1188. * to wait for IO and put the inode
  1189. */
  1190. return ret;
  1191. }
  1192. static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
  1193. u64 offset)
  1194. {
  1195. ASSERT(offset >= bitmap_start);
  1196. offset -= bitmap_start;
  1197. return (unsigned long)(div_u64(offset, unit));
  1198. }
  1199. static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
  1200. {
  1201. return (unsigned long)(div_u64(bytes, unit));
  1202. }
  1203. static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
  1204. u64 offset)
  1205. {
  1206. u64 bitmap_start;
  1207. u32 bytes_per_bitmap;
  1208. bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
  1209. bitmap_start = offset - ctl->start;
  1210. bitmap_start = div_u64(bitmap_start, bytes_per_bitmap);
  1211. bitmap_start *= bytes_per_bitmap;
  1212. bitmap_start += ctl->start;
  1213. return bitmap_start;
  1214. }
  1215. static int tree_insert_offset(struct rb_root *root, u64 offset,
  1216. struct rb_node *node, int bitmap)
  1217. {
  1218. struct rb_node **p = &root->rb_node;
  1219. struct rb_node *parent = NULL;
  1220. struct btrfs_free_space *info;
  1221. while (*p) {
  1222. parent = *p;
  1223. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  1224. if (offset < info->offset) {
  1225. p = &(*p)->rb_left;
  1226. } else if (offset > info->offset) {
  1227. p = &(*p)->rb_right;
  1228. } else {
  1229. /*
  1230. * we could have a bitmap entry and an extent entry
  1231. * share the same offset. If this is the case, we want
  1232. * the extent entry to always be found first if we do a
  1233. * linear search through the tree, since we want to have
  1234. * the quickest allocation time, and allocating from an
  1235. * extent is faster than allocating from a bitmap. So
  1236. * if we're inserting a bitmap and we find an entry at
  1237. * this offset, we want to go right, or after this entry
  1238. * logically. If we are inserting an extent and we've
  1239. * found a bitmap, we want to go left, or before
  1240. * logically.
  1241. */
  1242. if (bitmap) {
  1243. if (info->bitmap) {
  1244. WARN_ON_ONCE(1);
  1245. return -EEXIST;
  1246. }
  1247. p = &(*p)->rb_right;
  1248. } else {
  1249. if (!info->bitmap) {
  1250. WARN_ON_ONCE(1);
  1251. return -EEXIST;
  1252. }
  1253. p = &(*p)->rb_left;
  1254. }
  1255. }
  1256. }
  1257. rb_link_node(node, parent, p);
  1258. rb_insert_color(node, root);
  1259. return 0;
  1260. }
  1261. /*
  1262. * searches the tree for the given offset.
  1263. *
  1264. * fuzzy - If this is set, then we are trying to make an allocation, and we just
  1265. * want a section that has at least bytes size and comes at or after the given
  1266. * offset.
  1267. */
  1268. static struct btrfs_free_space *
  1269. tree_search_offset(struct btrfs_free_space_ctl *ctl,
  1270. u64 offset, int bitmap_only, int fuzzy)
  1271. {
  1272. struct rb_node *n = ctl->free_space_offset.rb_node;
  1273. struct btrfs_free_space *entry, *prev = NULL;
  1274. /* find entry that is closest to the 'offset' */
  1275. while (1) {
  1276. if (!n) {
  1277. entry = NULL;
  1278. break;
  1279. }
  1280. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1281. prev = entry;
  1282. if (offset < entry->offset)
  1283. n = n->rb_left;
  1284. else if (offset > entry->offset)
  1285. n = n->rb_right;
  1286. else
  1287. break;
  1288. }
  1289. if (bitmap_only) {
  1290. if (!entry)
  1291. return NULL;
  1292. if (entry->bitmap)
  1293. return entry;
  1294. /*
  1295. * bitmap entry and extent entry may share same offset,
  1296. * in that case, bitmap entry comes after extent entry.
  1297. */
  1298. n = rb_next(n);
  1299. if (!n)
  1300. return NULL;
  1301. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1302. if (entry->offset != offset)
  1303. return NULL;
  1304. WARN_ON(!entry->bitmap);
  1305. return entry;
  1306. } else if (entry) {
  1307. if (entry->bitmap) {
  1308. /*
  1309. * if previous extent entry covers the offset,
  1310. * we should return it instead of the bitmap entry
  1311. */
  1312. n = rb_prev(&entry->offset_index);
  1313. if (n) {
  1314. prev = rb_entry(n, struct btrfs_free_space,
  1315. offset_index);
  1316. if (!prev->bitmap &&
  1317. prev->offset + prev->bytes > offset)
  1318. entry = prev;
  1319. }
  1320. }
  1321. return entry;
  1322. }
  1323. if (!prev)
  1324. return NULL;
  1325. /* find last entry before the 'offset' */
  1326. entry = prev;
  1327. if (entry->offset > offset) {
  1328. n = rb_prev(&entry->offset_index);
  1329. if (n) {
  1330. entry = rb_entry(n, struct btrfs_free_space,
  1331. offset_index);
  1332. ASSERT(entry->offset <= offset);
  1333. } else {
  1334. if (fuzzy)
  1335. return entry;
  1336. else
  1337. return NULL;
  1338. }
  1339. }
  1340. if (entry->bitmap) {
  1341. n = rb_prev(&entry->offset_index);
  1342. if (n) {
  1343. prev = rb_entry(n, struct btrfs_free_space,
  1344. offset_index);
  1345. if (!prev->bitmap &&
  1346. prev->offset + prev->bytes > offset)
  1347. return prev;
  1348. }
  1349. if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
  1350. return entry;
  1351. } else if (entry->offset + entry->bytes > offset)
  1352. return entry;
  1353. if (!fuzzy)
  1354. return NULL;
  1355. while (1) {
  1356. if (entry->bitmap) {
  1357. if (entry->offset + BITS_PER_BITMAP *
  1358. ctl->unit > offset)
  1359. break;
  1360. } else {
  1361. if (entry->offset + entry->bytes > offset)
  1362. break;
  1363. }
  1364. n = rb_next(&entry->offset_index);
  1365. if (!n)
  1366. return NULL;
  1367. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  1368. }
  1369. return entry;
  1370. }
  1371. static inline void
  1372. __unlink_free_space(struct btrfs_free_space_ctl *ctl,
  1373. struct btrfs_free_space *info)
  1374. {
  1375. rb_erase(&info->offset_index, &ctl->free_space_offset);
  1376. ctl->free_extents--;
  1377. }
  1378. static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
  1379. struct btrfs_free_space *info)
  1380. {
  1381. __unlink_free_space(ctl, info);
  1382. ctl->free_space -= info->bytes;
  1383. }
  1384. static int link_free_space(struct btrfs_free_space_ctl *ctl,
  1385. struct btrfs_free_space *info)
  1386. {
  1387. int ret = 0;
  1388. ASSERT(info->bytes || info->bitmap);
  1389. ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
  1390. &info->offset_index, (info->bitmap != NULL));
  1391. if (ret)
  1392. return ret;
  1393. ctl->free_space += info->bytes;
  1394. ctl->free_extents++;
  1395. return ret;
  1396. }
  1397. static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
  1398. {
  1399. struct btrfs_block_group_cache *block_group = ctl->private;
  1400. u64 max_bytes;
  1401. u64 bitmap_bytes;
  1402. u64 extent_bytes;
  1403. u64 size = block_group->key.offset;
  1404. u32 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
  1405. u32 max_bitmaps = div_u64(size + bytes_per_bg - 1, bytes_per_bg);
  1406. max_bitmaps = max_t(u32, max_bitmaps, 1);
  1407. ASSERT(ctl->total_bitmaps <= max_bitmaps);
  1408. /*
  1409. * The goal is to keep the total amount of memory used per 1gb of space
  1410. * at or below 32k, so we need to adjust how much memory we allow to be
  1411. * used by extent based free space tracking
  1412. */
  1413. if (size < 1024 * 1024 * 1024)
  1414. max_bytes = MAX_CACHE_BYTES_PER_GIG;
  1415. else
  1416. max_bytes = MAX_CACHE_BYTES_PER_GIG *
  1417. div_u64(size, 1024 * 1024 * 1024);
  1418. /*
  1419. * we want to account for 1 more bitmap than what we have so we can make
  1420. * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
  1421. * we add more bitmaps.
  1422. */
  1423. bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
  1424. if (bitmap_bytes >= max_bytes) {
  1425. ctl->extents_thresh = 0;
  1426. return;
  1427. }
  1428. /*
  1429. * we want the extent entry threshold to always be at most 1/2 the max
  1430. * bytes we can have, or whatever is less than that.
  1431. */
  1432. extent_bytes = max_bytes - bitmap_bytes;
  1433. extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
  1434. ctl->extents_thresh =
  1435. div_u64(extent_bytes, sizeof(struct btrfs_free_space));
  1436. }
  1437. static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  1438. struct btrfs_free_space *info,
  1439. u64 offset, u64 bytes)
  1440. {
  1441. unsigned long start, count;
  1442. start = offset_to_bit(info->offset, ctl->unit, offset);
  1443. count = bytes_to_bits(bytes, ctl->unit);
  1444. ASSERT(start + count <= BITS_PER_BITMAP);
  1445. bitmap_clear(info->bitmap, start, count);
  1446. info->bytes -= bytes;
  1447. }
  1448. static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  1449. struct btrfs_free_space *info, u64 offset,
  1450. u64 bytes)
  1451. {
  1452. __bitmap_clear_bits(ctl, info, offset, bytes);
  1453. ctl->free_space -= bytes;
  1454. }
  1455. static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
  1456. struct btrfs_free_space *info, u64 offset,
  1457. u64 bytes)
  1458. {
  1459. unsigned long start, count;
  1460. start = offset_to_bit(info->offset, ctl->unit, offset);
  1461. count = bytes_to_bits(bytes, ctl->unit);
  1462. ASSERT(start + count <= BITS_PER_BITMAP);
  1463. bitmap_set(info->bitmap, start, count);
  1464. info->bytes += bytes;
  1465. ctl->free_space += bytes;
  1466. }
  1467. /*
  1468. * If we can not find suitable extent, we will use bytes to record
  1469. * the size of the max extent.
  1470. */
  1471. static int search_bitmap(struct btrfs_free_space_ctl *ctl,
  1472. struct btrfs_free_space *bitmap_info, u64 *offset,
  1473. u64 *bytes)
  1474. {
  1475. unsigned long found_bits = 0;
  1476. unsigned long max_bits = 0;
  1477. unsigned long bits, i;
  1478. unsigned long next_zero;
  1479. unsigned long extent_bits;
  1480. i = offset_to_bit(bitmap_info->offset, ctl->unit,
  1481. max_t(u64, *offset, bitmap_info->offset));
  1482. bits = bytes_to_bits(*bytes, ctl->unit);
  1483. for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
  1484. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  1485. BITS_PER_BITMAP, i);
  1486. extent_bits = next_zero - i;
  1487. if (extent_bits >= bits) {
  1488. found_bits = extent_bits;
  1489. break;
  1490. } else if (extent_bits > max_bits) {
  1491. max_bits = extent_bits;
  1492. }
  1493. i = next_zero;
  1494. }
  1495. if (found_bits) {
  1496. *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
  1497. *bytes = (u64)(found_bits) * ctl->unit;
  1498. return 0;
  1499. }
  1500. *bytes = (u64)(max_bits) * ctl->unit;
  1501. return -1;
  1502. }
  1503. /* Cache the size of the max extent in bytes */
  1504. static struct btrfs_free_space *
  1505. find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
  1506. unsigned long align, u64 *max_extent_size)
  1507. {
  1508. struct btrfs_free_space *entry;
  1509. struct rb_node *node;
  1510. u64 tmp;
  1511. u64 align_off;
  1512. int ret;
  1513. if (!ctl->free_space_offset.rb_node)
  1514. goto out;
  1515. entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
  1516. if (!entry)
  1517. goto out;
  1518. for (node = &entry->offset_index; node; node = rb_next(node)) {
  1519. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1520. if (entry->bytes < *bytes) {
  1521. if (entry->bytes > *max_extent_size)
  1522. *max_extent_size = entry->bytes;
  1523. continue;
  1524. }
  1525. /* make sure the space returned is big enough
  1526. * to match our requested alignment
  1527. */
  1528. if (*bytes >= align) {
  1529. tmp = entry->offset - ctl->start + align - 1;
  1530. tmp = div64_u64(tmp, align);
  1531. tmp = tmp * align + ctl->start;
  1532. align_off = tmp - entry->offset;
  1533. } else {
  1534. align_off = 0;
  1535. tmp = entry->offset;
  1536. }
  1537. if (entry->bytes < *bytes + align_off) {
  1538. if (entry->bytes > *max_extent_size)
  1539. *max_extent_size = entry->bytes;
  1540. continue;
  1541. }
  1542. if (entry->bitmap) {
  1543. u64 size = *bytes;
  1544. ret = search_bitmap(ctl, entry, &tmp, &size);
  1545. if (!ret) {
  1546. *offset = tmp;
  1547. *bytes = size;
  1548. return entry;
  1549. } else if (size > *max_extent_size) {
  1550. *max_extent_size = size;
  1551. }
  1552. continue;
  1553. }
  1554. *offset = tmp;
  1555. *bytes = entry->bytes - align_off;
  1556. return entry;
  1557. }
  1558. out:
  1559. return NULL;
  1560. }
  1561. static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
  1562. struct btrfs_free_space *info, u64 offset)
  1563. {
  1564. info->offset = offset_to_bitmap(ctl, offset);
  1565. info->bytes = 0;
  1566. INIT_LIST_HEAD(&info->list);
  1567. link_free_space(ctl, info);
  1568. ctl->total_bitmaps++;
  1569. ctl->op->recalc_thresholds(ctl);
  1570. }
  1571. static void free_bitmap(struct btrfs_free_space_ctl *ctl,
  1572. struct btrfs_free_space *bitmap_info)
  1573. {
  1574. unlink_free_space(ctl, bitmap_info);
  1575. kfree(bitmap_info->bitmap);
  1576. kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
  1577. ctl->total_bitmaps--;
  1578. ctl->op->recalc_thresholds(ctl);
  1579. }
  1580. static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
  1581. struct btrfs_free_space *bitmap_info,
  1582. u64 *offset, u64 *bytes)
  1583. {
  1584. u64 end;
  1585. u64 search_start, search_bytes;
  1586. int ret;
  1587. again:
  1588. end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
  1589. /*
  1590. * We need to search for bits in this bitmap. We could only cover some
  1591. * of the extent in this bitmap thanks to how we add space, so we need
  1592. * to search for as much as it as we can and clear that amount, and then
  1593. * go searching for the next bit.
  1594. */
  1595. search_start = *offset;
  1596. search_bytes = ctl->unit;
  1597. search_bytes = min(search_bytes, end - search_start + 1);
  1598. ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
  1599. if (ret < 0 || search_start != *offset)
  1600. return -EINVAL;
  1601. /* We may have found more bits than what we need */
  1602. search_bytes = min(search_bytes, *bytes);
  1603. /* Cannot clear past the end of the bitmap */
  1604. search_bytes = min(search_bytes, end - search_start + 1);
  1605. bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
  1606. *offset += search_bytes;
  1607. *bytes -= search_bytes;
  1608. if (*bytes) {
  1609. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1610. if (!bitmap_info->bytes)
  1611. free_bitmap(ctl, bitmap_info);
  1612. /*
  1613. * no entry after this bitmap, but we still have bytes to
  1614. * remove, so something has gone wrong.
  1615. */
  1616. if (!next)
  1617. return -EINVAL;
  1618. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1619. offset_index);
  1620. /*
  1621. * if the next entry isn't a bitmap we need to return to let the
  1622. * extent stuff do its work.
  1623. */
  1624. if (!bitmap_info->bitmap)
  1625. return -EAGAIN;
  1626. /*
  1627. * Ok the next item is a bitmap, but it may not actually hold
  1628. * the information for the rest of this free space stuff, so
  1629. * look for it, and if we don't find it return so we can try
  1630. * everything over again.
  1631. */
  1632. search_start = *offset;
  1633. search_bytes = ctl->unit;
  1634. ret = search_bitmap(ctl, bitmap_info, &search_start,
  1635. &search_bytes);
  1636. if (ret < 0 || search_start != *offset)
  1637. return -EAGAIN;
  1638. goto again;
  1639. } else if (!bitmap_info->bytes)
  1640. free_bitmap(ctl, bitmap_info);
  1641. return 0;
  1642. }
  1643. static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
  1644. struct btrfs_free_space *info, u64 offset,
  1645. u64 bytes)
  1646. {
  1647. u64 bytes_to_set = 0;
  1648. u64 end;
  1649. end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
  1650. bytes_to_set = min(end - offset, bytes);
  1651. bitmap_set_bits(ctl, info, offset, bytes_to_set);
  1652. return bytes_to_set;
  1653. }
  1654. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  1655. struct btrfs_free_space *info)
  1656. {
  1657. struct btrfs_block_group_cache *block_group = ctl->private;
  1658. /*
  1659. * If we are below the extents threshold then we can add this as an
  1660. * extent, and don't have to deal with the bitmap
  1661. */
  1662. if (ctl->free_extents < ctl->extents_thresh) {
  1663. /*
  1664. * If this block group has some small extents we don't want to
  1665. * use up all of our free slots in the cache with them, we want
  1666. * to reserve them to larger extents, however if we have plent
  1667. * of cache left then go ahead an dadd them, no sense in adding
  1668. * the overhead of a bitmap if we don't have to.
  1669. */
  1670. if (info->bytes <= block_group->sectorsize * 4) {
  1671. if (ctl->free_extents * 2 <= ctl->extents_thresh)
  1672. return false;
  1673. } else {
  1674. return false;
  1675. }
  1676. }
  1677. /*
  1678. * The original block groups from mkfs can be really small, like 8
  1679. * megabytes, so don't bother with a bitmap for those entries. However
  1680. * some block groups can be smaller than what a bitmap would cover but
  1681. * are still large enough that they could overflow the 32k memory limit,
  1682. * so allow those block groups to still be allowed to have a bitmap
  1683. * entry.
  1684. */
  1685. if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
  1686. return false;
  1687. return true;
  1688. }
  1689. static struct btrfs_free_space_op free_space_op = {
  1690. .recalc_thresholds = recalculate_thresholds,
  1691. .use_bitmap = use_bitmap,
  1692. };
  1693. static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
  1694. struct btrfs_free_space *info)
  1695. {
  1696. struct btrfs_free_space *bitmap_info;
  1697. struct btrfs_block_group_cache *block_group = NULL;
  1698. int added = 0;
  1699. u64 bytes, offset, bytes_added;
  1700. int ret;
  1701. bytes = info->bytes;
  1702. offset = info->offset;
  1703. if (!ctl->op->use_bitmap(ctl, info))
  1704. return 0;
  1705. if (ctl->op == &free_space_op)
  1706. block_group = ctl->private;
  1707. again:
  1708. /*
  1709. * Since we link bitmaps right into the cluster we need to see if we
  1710. * have a cluster here, and if so and it has our bitmap we need to add
  1711. * the free space to that bitmap.
  1712. */
  1713. if (block_group && !list_empty(&block_group->cluster_list)) {
  1714. struct btrfs_free_cluster *cluster;
  1715. struct rb_node *node;
  1716. struct btrfs_free_space *entry;
  1717. cluster = list_entry(block_group->cluster_list.next,
  1718. struct btrfs_free_cluster,
  1719. block_group_list);
  1720. spin_lock(&cluster->lock);
  1721. node = rb_first(&cluster->root);
  1722. if (!node) {
  1723. spin_unlock(&cluster->lock);
  1724. goto no_cluster_bitmap;
  1725. }
  1726. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1727. if (!entry->bitmap) {
  1728. spin_unlock(&cluster->lock);
  1729. goto no_cluster_bitmap;
  1730. }
  1731. if (entry->offset == offset_to_bitmap(ctl, offset)) {
  1732. bytes_added = add_bytes_to_bitmap(ctl, entry,
  1733. offset, bytes);
  1734. bytes -= bytes_added;
  1735. offset += bytes_added;
  1736. }
  1737. spin_unlock(&cluster->lock);
  1738. if (!bytes) {
  1739. ret = 1;
  1740. goto out;
  1741. }
  1742. }
  1743. no_cluster_bitmap:
  1744. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  1745. 1, 0);
  1746. if (!bitmap_info) {
  1747. ASSERT(added == 0);
  1748. goto new_bitmap;
  1749. }
  1750. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
  1751. bytes -= bytes_added;
  1752. offset += bytes_added;
  1753. added = 0;
  1754. if (!bytes) {
  1755. ret = 1;
  1756. goto out;
  1757. } else
  1758. goto again;
  1759. new_bitmap:
  1760. if (info && info->bitmap) {
  1761. add_new_bitmap(ctl, info, offset);
  1762. added = 1;
  1763. info = NULL;
  1764. goto again;
  1765. } else {
  1766. spin_unlock(&ctl->tree_lock);
  1767. /* no pre-allocated info, allocate a new one */
  1768. if (!info) {
  1769. info = kmem_cache_zalloc(btrfs_free_space_cachep,
  1770. GFP_NOFS);
  1771. if (!info) {
  1772. spin_lock(&ctl->tree_lock);
  1773. ret = -ENOMEM;
  1774. goto out;
  1775. }
  1776. }
  1777. /* allocate the bitmap */
  1778. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1779. spin_lock(&ctl->tree_lock);
  1780. if (!info->bitmap) {
  1781. ret = -ENOMEM;
  1782. goto out;
  1783. }
  1784. goto again;
  1785. }
  1786. out:
  1787. if (info) {
  1788. if (info->bitmap)
  1789. kfree(info->bitmap);
  1790. kmem_cache_free(btrfs_free_space_cachep, info);
  1791. }
  1792. return ret;
  1793. }
  1794. static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
  1795. struct btrfs_free_space *info, bool update_stat)
  1796. {
  1797. struct btrfs_free_space *left_info;
  1798. struct btrfs_free_space *right_info;
  1799. bool merged = false;
  1800. u64 offset = info->offset;
  1801. u64 bytes = info->bytes;
  1802. /*
  1803. * first we want to see if there is free space adjacent to the range we
  1804. * are adding, if there is remove that struct and add a new one to
  1805. * cover the entire range
  1806. */
  1807. right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
  1808. if (right_info && rb_prev(&right_info->offset_index))
  1809. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1810. struct btrfs_free_space, offset_index);
  1811. else
  1812. left_info = tree_search_offset(ctl, offset - 1, 0, 0);
  1813. if (right_info && !right_info->bitmap) {
  1814. if (update_stat)
  1815. unlink_free_space(ctl, right_info);
  1816. else
  1817. __unlink_free_space(ctl, right_info);
  1818. info->bytes += right_info->bytes;
  1819. kmem_cache_free(btrfs_free_space_cachep, right_info);
  1820. merged = true;
  1821. }
  1822. if (left_info && !left_info->bitmap &&
  1823. left_info->offset + left_info->bytes == offset) {
  1824. if (update_stat)
  1825. unlink_free_space(ctl, left_info);
  1826. else
  1827. __unlink_free_space(ctl, left_info);
  1828. info->offset = left_info->offset;
  1829. info->bytes += left_info->bytes;
  1830. kmem_cache_free(btrfs_free_space_cachep, left_info);
  1831. merged = true;
  1832. }
  1833. return merged;
  1834. }
  1835. static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
  1836. struct btrfs_free_space *info,
  1837. bool update_stat)
  1838. {
  1839. struct btrfs_free_space *bitmap;
  1840. unsigned long i;
  1841. unsigned long j;
  1842. const u64 end = info->offset + info->bytes;
  1843. const u64 bitmap_offset = offset_to_bitmap(ctl, end);
  1844. u64 bytes;
  1845. bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
  1846. if (!bitmap)
  1847. return false;
  1848. i = offset_to_bit(bitmap->offset, ctl->unit, end);
  1849. j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
  1850. if (j == i)
  1851. return false;
  1852. bytes = (j - i) * ctl->unit;
  1853. info->bytes += bytes;
  1854. if (update_stat)
  1855. bitmap_clear_bits(ctl, bitmap, end, bytes);
  1856. else
  1857. __bitmap_clear_bits(ctl, bitmap, end, bytes);
  1858. if (!bitmap->bytes)
  1859. free_bitmap(ctl, bitmap);
  1860. return true;
  1861. }
  1862. static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
  1863. struct btrfs_free_space *info,
  1864. bool update_stat)
  1865. {
  1866. struct btrfs_free_space *bitmap;
  1867. u64 bitmap_offset;
  1868. unsigned long i;
  1869. unsigned long j;
  1870. unsigned long prev_j;
  1871. u64 bytes;
  1872. bitmap_offset = offset_to_bitmap(ctl, info->offset);
  1873. /* If we're on a boundary, try the previous logical bitmap. */
  1874. if (bitmap_offset == info->offset) {
  1875. if (info->offset == 0)
  1876. return false;
  1877. bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
  1878. }
  1879. bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
  1880. if (!bitmap)
  1881. return false;
  1882. i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
  1883. j = 0;
  1884. prev_j = (unsigned long)-1;
  1885. for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
  1886. if (j > i)
  1887. break;
  1888. prev_j = j;
  1889. }
  1890. if (prev_j == i)
  1891. return false;
  1892. if (prev_j == (unsigned long)-1)
  1893. bytes = (i + 1) * ctl->unit;
  1894. else
  1895. bytes = (i - prev_j) * ctl->unit;
  1896. info->offset -= bytes;
  1897. info->bytes += bytes;
  1898. if (update_stat)
  1899. bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
  1900. else
  1901. __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
  1902. if (!bitmap->bytes)
  1903. free_bitmap(ctl, bitmap);
  1904. return true;
  1905. }
  1906. /*
  1907. * We prefer always to allocate from extent entries, both for clustered and
  1908. * non-clustered allocation requests. So when attempting to add a new extent
  1909. * entry, try to see if there's adjacent free space in bitmap entries, and if
  1910. * there is, migrate that space from the bitmaps to the extent.
  1911. * Like this we get better chances of satisfying space allocation requests
  1912. * because we attempt to satisfy them based on a single cache entry, and never
  1913. * on 2 or more entries - even if the entries represent a contiguous free space
  1914. * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
  1915. * ends).
  1916. */
  1917. static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
  1918. struct btrfs_free_space *info,
  1919. bool update_stat)
  1920. {
  1921. /*
  1922. * Only work with disconnected entries, as we can change their offset,
  1923. * and must be extent entries.
  1924. */
  1925. ASSERT(!info->bitmap);
  1926. ASSERT(RB_EMPTY_NODE(&info->offset_index));
  1927. if (ctl->total_bitmaps > 0) {
  1928. bool stole_end;
  1929. bool stole_front = false;
  1930. stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
  1931. if (ctl->total_bitmaps > 0)
  1932. stole_front = steal_from_bitmap_to_front(ctl, info,
  1933. update_stat);
  1934. if (stole_end || stole_front)
  1935. try_merge_free_space(ctl, info, update_stat);
  1936. }
  1937. }
  1938. int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
  1939. u64 offset, u64 bytes)
  1940. {
  1941. struct btrfs_free_space *info;
  1942. int ret = 0;
  1943. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  1944. if (!info)
  1945. return -ENOMEM;
  1946. info->offset = offset;
  1947. info->bytes = bytes;
  1948. RB_CLEAR_NODE(&info->offset_index);
  1949. spin_lock(&ctl->tree_lock);
  1950. if (try_merge_free_space(ctl, info, true))
  1951. goto link;
  1952. /*
  1953. * There was no extent directly to the left or right of this new
  1954. * extent then we know we're going to have to allocate a new extent, so
  1955. * before we do that see if we need to drop this into a bitmap
  1956. */
  1957. ret = insert_into_bitmap(ctl, info);
  1958. if (ret < 0) {
  1959. goto out;
  1960. } else if (ret) {
  1961. ret = 0;
  1962. goto out;
  1963. }
  1964. link:
  1965. /*
  1966. * Only steal free space from adjacent bitmaps if we're sure we're not
  1967. * going to add the new free space to existing bitmap entries - because
  1968. * that would mean unnecessary work that would be reverted. Therefore
  1969. * attempt to steal space from bitmaps if we're adding an extent entry.
  1970. */
  1971. steal_from_bitmap(ctl, info, true);
  1972. ret = link_free_space(ctl, info);
  1973. if (ret)
  1974. kmem_cache_free(btrfs_free_space_cachep, info);
  1975. out:
  1976. spin_unlock(&ctl->tree_lock);
  1977. if (ret) {
  1978. printk(KERN_CRIT "BTRFS: unable to add free space :%d\n", ret);
  1979. ASSERT(ret != -EEXIST);
  1980. }
  1981. return ret;
  1982. }
  1983. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1984. u64 offset, u64 bytes)
  1985. {
  1986. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1987. struct btrfs_free_space *info;
  1988. int ret;
  1989. bool re_search = false;
  1990. spin_lock(&ctl->tree_lock);
  1991. again:
  1992. ret = 0;
  1993. if (!bytes)
  1994. goto out_lock;
  1995. info = tree_search_offset(ctl, offset, 0, 0);
  1996. if (!info) {
  1997. /*
  1998. * oops didn't find an extent that matched the space we wanted
  1999. * to remove, look for a bitmap instead
  2000. */
  2001. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  2002. 1, 0);
  2003. if (!info) {
  2004. /*
  2005. * If we found a partial bit of our free space in a
  2006. * bitmap but then couldn't find the other part this may
  2007. * be a problem, so WARN about it.
  2008. */
  2009. WARN_ON(re_search);
  2010. goto out_lock;
  2011. }
  2012. }
  2013. re_search = false;
  2014. if (!info->bitmap) {
  2015. unlink_free_space(ctl, info);
  2016. if (offset == info->offset) {
  2017. u64 to_free = min(bytes, info->bytes);
  2018. info->bytes -= to_free;
  2019. info->offset += to_free;
  2020. if (info->bytes) {
  2021. ret = link_free_space(ctl, info);
  2022. WARN_ON(ret);
  2023. } else {
  2024. kmem_cache_free(btrfs_free_space_cachep, info);
  2025. }
  2026. offset += to_free;
  2027. bytes -= to_free;
  2028. goto again;
  2029. } else {
  2030. u64 old_end = info->bytes + info->offset;
  2031. info->bytes = offset - info->offset;
  2032. ret = link_free_space(ctl, info);
  2033. WARN_ON(ret);
  2034. if (ret)
  2035. goto out_lock;
  2036. /* Not enough bytes in this entry to satisfy us */
  2037. if (old_end < offset + bytes) {
  2038. bytes -= old_end - offset;
  2039. offset = old_end;
  2040. goto again;
  2041. } else if (old_end == offset + bytes) {
  2042. /* all done */
  2043. goto out_lock;
  2044. }
  2045. spin_unlock(&ctl->tree_lock);
  2046. ret = btrfs_add_free_space(block_group, offset + bytes,
  2047. old_end - (offset + bytes));
  2048. WARN_ON(ret);
  2049. goto out;
  2050. }
  2051. }
  2052. ret = remove_from_bitmap(ctl, info, &offset, &bytes);
  2053. if (ret == -EAGAIN) {
  2054. re_search = true;
  2055. goto again;
  2056. }
  2057. out_lock:
  2058. spin_unlock(&ctl->tree_lock);
  2059. out:
  2060. return ret;
  2061. }
  2062. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  2063. u64 bytes)
  2064. {
  2065. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2066. struct btrfs_free_space *info;
  2067. struct rb_node *n;
  2068. int count = 0;
  2069. for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
  2070. info = rb_entry(n, struct btrfs_free_space, offset_index);
  2071. if (info->bytes >= bytes && !block_group->ro)
  2072. count++;
  2073. btrfs_crit(block_group->fs_info,
  2074. "entry offset %llu, bytes %llu, bitmap %s",
  2075. info->offset, info->bytes,
  2076. (info->bitmap) ? "yes" : "no");
  2077. }
  2078. btrfs_info(block_group->fs_info, "block group has cluster?: %s",
  2079. list_empty(&block_group->cluster_list) ? "no" : "yes");
  2080. btrfs_info(block_group->fs_info,
  2081. "%d blocks of free space at or bigger than bytes is", count);
  2082. }
  2083. void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
  2084. {
  2085. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2086. spin_lock_init(&ctl->tree_lock);
  2087. ctl->unit = block_group->sectorsize;
  2088. ctl->start = block_group->key.objectid;
  2089. ctl->private = block_group;
  2090. ctl->op = &free_space_op;
  2091. INIT_LIST_HEAD(&ctl->trimming_ranges);
  2092. mutex_init(&ctl->cache_writeout_mutex);
  2093. /*
  2094. * we only want to have 32k of ram per block group for keeping
  2095. * track of free space, and if we pass 1/2 of that we want to
  2096. * start converting things over to using bitmaps
  2097. */
  2098. ctl->extents_thresh = ((1024 * 32) / 2) /
  2099. sizeof(struct btrfs_free_space);
  2100. }
  2101. /*
  2102. * for a given cluster, put all of its extents back into the free
  2103. * space cache. If the block group passed doesn't match the block group
  2104. * pointed to by the cluster, someone else raced in and freed the
  2105. * cluster already. In that case, we just return without changing anything
  2106. */
  2107. static int
  2108. __btrfs_return_cluster_to_free_space(
  2109. struct btrfs_block_group_cache *block_group,
  2110. struct btrfs_free_cluster *cluster)
  2111. {
  2112. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2113. struct btrfs_free_space *entry;
  2114. struct rb_node *node;
  2115. spin_lock(&cluster->lock);
  2116. if (cluster->block_group != block_group)
  2117. goto out;
  2118. cluster->block_group = NULL;
  2119. cluster->window_start = 0;
  2120. list_del_init(&cluster->block_group_list);
  2121. node = rb_first(&cluster->root);
  2122. while (node) {
  2123. bool bitmap;
  2124. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2125. node = rb_next(&entry->offset_index);
  2126. rb_erase(&entry->offset_index, &cluster->root);
  2127. RB_CLEAR_NODE(&entry->offset_index);
  2128. bitmap = (entry->bitmap != NULL);
  2129. if (!bitmap) {
  2130. try_merge_free_space(ctl, entry, false);
  2131. steal_from_bitmap(ctl, entry, false);
  2132. }
  2133. tree_insert_offset(&ctl->free_space_offset,
  2134. entry->offset, &entry->offset_index, bitmap);
  2135. }
  2136. cluster->root = RB_ROOT;
  2137. out:
  2138. spin_unlock(&cluster->lock);
  2139. btrfs_put_block_group(block_group);
  2140. return 0;
  2141. }
  2142. static void __btrfs_remove_free_space_cache_locked(
  2143. struct btrfs_free_space_ctl *ctl)
  2144. {
  2145. struct btrfs_free_space *info;
  2146. struct rb_node *node;
  2147. while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
  2148. info = rb_entry(node, struct btrfs_free_space, offset_index);
  2149. if (!info->bitmap) {
  2150. unlink_free_space(ctl, info);
  2151. kmem_cache_free(btrfs_free_space_cachep, info);
  2152. } else {
  2153. free_bitmap(ctl, info);
  2154. }
  2155. cond_resched_lock(&ctl->tree_lock);
  2156. }
  2157. }
  2158. void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
  2159. {
  2160. spin_lock(&ctl->tree_lock);
  2161. __btrfs_remove_free_space_cache_locked(ctl);
  2162. spin_unlock(&ctl->tree_lock);
  2163. }
  2164. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  2165. {
  2166. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2167. struct btrfs_free_cluster *cluster;
  2168. struct list_head *head;
  2169. spin_lock(&ctl->tree_lock);
  2170. while ((head = block_group->cluster_list.next) !=
  2171. &block_group->cluster_list) {
  2172. cluster = list_entry(head, struct btrfs_free_cluster,
  2173. block_group_list);
  2174. WARN_ON(cluster->block_group != block_group);
  2175. __btrfs_return_cluster_to_free_space(block_group, cluster);
  2176. cond_resched_lock(&ctl->tree_lock);
  2177. }
  2178. __btrfs_remove_free_space_cache_locked(ctl);
  2179. spin_unlock(&ctl->tree_lock);
  2180. }
  2181. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  2182. u64 offset, u64 bytes, u64 empty_size,
  2183. u64 *max_extent_size)
  2184. {
  2185. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2186. struct btrfs_free_space *entry = NULL;
  2187. u64 bytes_search = bytes + empty_size;
  2188. u64 ret = 0;
  2189. u64 align_gap = 0;
  2190. u64 align_gap_len = 0;
  2191. spin_lock(&ctl->tree_lock);
  2192. entry = find_free_space(ctl, &offset, &bytes_search,
  2193. block_group->full_stripe_len, max_extent_size);
  2194. if (!entry)
  2195. goto out;
  2196. ret = offset;
  2197. if (entry->bitmap) {
  2198. bitmap_clear_bits(ctl, entry, offset, bytes);
  2199. if (!entry->bytes)
  2200. free_bitmap(ctl, entry);
  2201. } else {
  2202. unlink_free_space(ctl, entry);
  2203. align_gap_len = offset - entry->offset;
  2204. align_gap = entry->offset;
  2205. entry->offset = offset + bytes;
  2206. WARN_ON(entry->bytes < bytes + align_gap_len);
  2207. entry->bytes -= bytes + align_gap_len;
  2208. if (!entry->bytes)
  2209. kmem_cache_free(btrfs_free_space_cachep, entry);
  2210. else
  2211. link_free_space(ctl, entry);
  2212. }
  2213. out:
  2214. spin_unlock(&ctl->tree_lock);
  2215. if (align_gap_len)
  2216. __btrfs_add_free_space(ctl, align_gap, align_gap_len);
  2217. return ret;
  2218. }
  2219. /*
  2220. * given a cluster, put all of its extents back into the free space
  2221. * cache. If a block group is passed, this function will only free
  2222. * a cluster that belongs to the passed block group.
  2223. *
  2224. * Otherwise, it'll get a reference on the block group pointed to by the
  2225. * cluster and remove the cluster from it.
  2226. */
  2227. int btrfs_return_cluster_to_free_space(
  2228. struct btrfs_block_group_cache *block_group,
  2229. struct btrfs_free_cluster *cluster)
  2230. {
  2231. struct btrfs_free_space_ctl *ctl;
  2232. int ret;
  2233. /* first, get a safe pointer to the block group */
  2234. spin_lock(&cluster->lock);
  2235. if (!block_group) {
  2236. block_group = cluster->block_group;
  2237. if (!block_group) {
  2238. spin_unlock(&cluster->lock);
  2239. return 0;
  2240. }
  2241. } else if (cluster->block_group != block_group) {
  2242. /* someone else has already freed it don't redo their work */
  2243. spin_unlock(&cluster->lock);
  2244. return 0;
  2245. }
  2246. atomic_inc(&block_group->count);
  2247. spin_unlock(&cluster->lock);
  2248. ctl = block_group->free_space_ctl;
  2249. /* now return any extents the cluster had on it */
  2250. spin_lock(&ctl->tree_lock);
  2251. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  2252. spin_unlock(&ctl->tree_lock);
  2253. /* finally drop our ref */
  2254. btrfs_put_block_group(block_group);
  2255. return ret;
  2256. }
  2257. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  2258. struct btrfs_free_cluster *cluster,
  2259. struct btrfs_free_space *entry,
  2260. u64 bytes, u64 min_start,
  2261. u64 *max_extent_size)
  2262. {
  2263. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2264. int err;
  2265. u64 search_start = cluster->window_start;
  2266. u64 search_bytes = bytes;
  2267. u64 ret = 0;
  2268. search_start = min_start;
  2269. search_bytes = bytes;
  2270. err = search_bitmap(ctl, entry, &search_start, &search_bytes);
  2271. if (err) {
  2272. if (search_bytes > *max_extent_size)
  2273. *max_extent_size = search_bytes;
  2274. return 0;
  2275. }
  2276. ret = search_start;
  2277. __bitmap_clear_bits(ctl, entry, ret, bytes);
  2278. return ret;
  2279. }
  2280. /*
  2281. * given a cluster, try to allocate 'bytes' from it, returns 0
  2282. * if it couldn't find anything suitably large, or a logical disk offset
  2283. * if things worked out
  2284. */
  2285. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  2286. struct btrfs_free_cluster *cluster, u64 bytes,
  2287. u64 min_start, u64 *max_extent_size)
  2288. {
  2289. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2290. struct btrfs_free_space *entry = NULL;
  2291. struct rb_node *node;
  2292. u64 ret = 0;
  2293. spin_lock(&cluster->lock);
  2294. if (bytes > cluster->max_size)
  2295. goto out;
  2296. if (cluster->block_group != block_group)
  2297. goto out;
  2298. node = rb_first(&cluster->root);
  2299. if (!node)
  2300. goto out;
  2301. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2302. while (1) {
  2303. if (entry->bytes < bytes && entry->bytes > *max_extent_size)
  2304. *max_extent_size = entry->bytes;
  2305. if (entry->bytes < bytes ||
  2306. (!entry->bitmap && entry->offset < min_start)) {
  2307. node = rb_next(&entry->offset_index);
  2308. if (!node)
  2309. break;
  2310. entry = rb_entry(node, struct btrfs_free_space,
  2311. offset_index);
  2312. continue;
  2313. }
  2314. if (entry->bitmap) {
  2315. ret = btrfs_alloc_from_bitmap(block_group,
  2316. cluster, entry, bytes,
  2317. cluster->window_start,
  2318. max_extent_size);
  2319. if (ret == 0) {
  2320. node = rb_next(&entry->offset_index);
  2321. if (!node)
  2322. break;
  2323. entry = rb_entry(node, struct btrfs_free_space,
  2324. offset_index);
  2325. continue;
  2326. }
  2327. cluster->window_start += bytes;
  2328. } else {
  2329. ret = entry->offset;
  2330. entry->offset += bytes;
  2331. entry->bytes -= bytes;
  2332. }
  2333. if (entry->bytes == 0)
  2334. rb_erase(&entry->offset_index, &cluster->root);
  2335. break;
  2336. }
  2337. out:
  2338. spin_unlock(&cluster->lock);
  2339. if (!ret)
  2340. return 0;
  2341. spin_lock(&ctl->tree_lock);
  2342. ctl->free_space -= bytes;
  2343. if (entry->bytes == 0) {
  2344. ctl->free_extents--;
  2345. if (entry->bitmap) {
  2346. kfree(entry->bitmap);
  2347. ctl->total_bitmaps--;
  2348. ctl->op->recalc_thresholds(ctl);
  2349. }
  2350. kmem_cache_free(btrfs_free_space_cachep, entry);
  2351. }
  2352. spin_unlock(&ctl->tree_lock);
  2353. return ret;
  2354. }
  2355. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  2356. struct btrfs_free_space *entry,
  2357. struct btrfs_free_cluster *cluster,
  2358. u64 offset, u64 bytes,
  2359. u64 cont1_bytes, u64 min_bytes)
  2360. {
  2361. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2362. unsigned long next_zero;
  2363. unsigned long i;
  2364. unsigned long want_bits;
  2365. unsigned long min_bits;
  2366. unsigned long found_bits;
  2367. unsigned long start = 0;
  2368. unsigned long total_found = 0;
  2369. int ret;
  2370. i = offset_to_bit(entry->offset, ctl->unit,
  2371. max_t(u64, offset, entry->offset));
  2372. want_bits = bytes_to_bits(bytes, ctl->unit);
  2373. min_bits = bytes_to_bits(min_bytes, ctl->unit);
  2374. again:
  2375. found_bits = 0;
  2376. for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
  2377. next_zero = find_next_zero_bit(entry->bitmap,
  2378. BITS_PER_BITMAP, i);
  2379. if (next_zero - i >= min_bits) {
  2380. found_bits = next_zero - i;
  2381. break;
  2382. }
  2383. i = next_zero;
  2384. }
  2385. if (!found_bits)
  2386. return -ENOSPC;
  2387. if (!total_found) {
  2388. start = i;
  2389. cluster->max_size = 0;
  2390. }
  2391. total_found += found_bits;
  2392. if (cluster->max_size < found_bits * ctl->unit)
  2393. cluster->max_size = found_bits * ctl->unit;
  2394. if (total_found < want_bits || cluster->max_size < cont1_bytes) {
  2395. i = next_zero + 1;
  2396. goto again;
  2397. }
  2398. cluster->window_start = start * ctl->unit + entry->offset;
  2399. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  2400. ret = tree_insert_offset(&cluster->root, entry->offset,
  2401. &entry->offset_index, 1);
  2402. ASSERT(!ret); /* -EEXIST; Logic error */
  2403. trace_btrfs_setup_cluster(block_group, cluster,
  2404. total_found * ctl->unit, 1);
  2405. return 0;
  2406. }
  2407. /*
  2408. * This searches the block group for just extents to fill the cluster with.
  2409. * Try to find a cluster with at least bytes total bytes, at least one
  2410. * extent of cont1_bytes, and other clusters of at least min_bytes.
  2411. */
  2412. static noinline int
  2413. setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
  2414. struct btrfs_free_cluster *cluster,
  2415. struct list_head *bitmaps, u64 offset, u64 bytes,
  2416. u64 cont1_bytes, u64 min_bytes)
  2417. {
  2418. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2419. struct btrfs_free_space *first = NULL;
  2420. struct btrfs_free_space *entry = NULL;
  2421. struct btrfs_free_space *last;
  2422. struct rb_node *node;
  2423. u64 window_free;
  2424. u64 max_extent;
  2425. u64 total_size = 0;
  2426. entry = tree_search_offset(ctl, offset, 0, 1);
  2427. if (!entry)
  2428. return -ENOSPC;
  2429. /*
  2430. * We don't want bitmaps, so just move along until we find a normal
  2431. * extent entry.
  2432. */
  2433. while (entry->bitmap || entry->bytes < min_bytes) {
  2434. if (entry->bitmap && list_empty(&entry->list))
  2435. list_add_tail(&entry->list, bitmaps);
  2436. node = rb_next(&entry->offset_index);
  2437. if (!node)
  2438. return -ENOSPC;
  2439. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2440. }
  2441. window_free = entry->bytes;
  2442. max_extent = entry->bytes;
  2443. first = entry;
  2444. last = entry;
  2445. for (node = rb_next(&entry->offset_index); node;
  2446. node = rb_next(&entry->offset_index)) {
  2447. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2448. if (entry->bitmap) {
  2449. if (list_empty(&entry->list))
  2450. list_add_tail(&entry->list, bitmaps);
  2451. continue;
  2452. }
  2453. if (entry->bytes < min_bytes)
  2454. continue;
  2455. last = entry;
  2456. window_free += entry->bytes;
  2457. if (entry->bytes > max_extent)
  2458. max_extent = entry->bytes;
  2459. }
  2460. if (window_free < bytes || max_extent < cont1_bytes)
  2461. return -ENOSPC;
  2462. cluster->window_start = first->offset;
  2463. node = &first->offset_index;
  2464. /*
  2465. * now we've found our entries, pull them out of the free space
  2466. * cache and put them into the cluster rbtree
  2467. */
  2468. do {
  2469. int ret;
  2470. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2471. node = rb_next(&entry->offset_index);
  2472. if (entry->bitmap || entry->bytes < min_bytes)
  2473. continue;
  2474. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  2475. ret = tree_insert_offset(&cluster->root, entry->offset,
  2476. &entry->offset_index, 0);
  2477. total_size += entry->bytes;
  2478. ASSERT(!ret); /* -EEXIST; Logic error */
  2479. } while (node && entry != last);
  2480. cluster->max_size = max_extent;
  2481. trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
  2482. return 0;
  2483. }
  2484. /*
  2485. * This specifically looks for bitmaps that may work in the cluster, we assume
  2486. * that we have already failed to find extents that will work.
  2487. */
  2488. static noinline int
  2489. setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
  2490. struct btrfs_free_cluster *cluster,
  2491. struct list_head *bitmaps, u64 offset, u64 bytes,
  2492. u64 cont1_bytes, u64 min_bytes)
  2493. {
  2494. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2495. struct btrfs_free_space *entry;
  2496. int ret = -ENOSPC;
  2497. u64 bitmap_offset = offset_to_bitmap(ctl, offset);
  2498. if (ctl->total_bitmaps == 0)
  2499. return -ENOSPC;
  2500. /*
  2501. * The bitmap that covers offset won't be in the list unless offset
  2502. * is just its start offset.
  2503. */
  2504. entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
  2505. if (entry->offset != bitmap_offset) {
  2506. entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
  2507. if (entry && list_empty(&entry->list))
  2508. list_add(&entry->list, bitmaps);
  2509. }
  2510. list_for_each_entry(entry, bitmaps, list) {
  2511. if (entry->bytes < bytes)
  2512. continue;
  2513. ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
  2514. bytes, cont1_bytes, min_bytes);
  2515. if (!ret)
  2516. return 0;
  2517. }
  2518. /*
  2519. * The bitmaps list has all the bitmaps that record free space
  2520. * starting after offset, so no more search is required.
  2521. */
  2522. return -ENOSPC;
  2523. }
  2524. /*
  2525. * here we try to find a cluster of blocks in a block group. The goal
  2526. * is to find at least bytes+empty_size.
  2527. * We might not find them all in one contiguous area.
  2528. *
  2529. * returns zero and sets up cluster if things worked out, otherwise
  2530. * it returns -enospc
  2531. */
  2532. int btrfs_find_space_cluster(struct btrfs_root *root,
  2533. struct btrfs_block_group_cache *block_group,
  2534. struct btrfs_free_cluster *cluster,
  2535. u64 offset, u64 bytes, u64 empty_size)
  2536. {
  2537. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2538. struct btrfs_free_space *entry, *tmp;
  2539. LIST_HEAD(bitmaps);
  2540. u64 min_bytes;
  2541. u64 cont1_bytes;
  2542. int ret;
  2543. /*
  2544. * Choose the minimum extent size we'll require for this
  2545. * cluster. For SSD_SPREAD, don't allow any fragmentation.
  2546. * For metadata, allow allocates with smaller extents. For
  2547. * data, keep it dense.
  2548. */
  2549. if (btrfs_test_opt(root, SSD_SPREAD)) {
  2550. cont1_bytes = min_bytes = bytes + empty_size;
  2551. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  2552. cont1_bytes = bytes;
  2553. min_bytes = block_group->sectorsize;
  2554. } else {
  2555. cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
  2556. min_bytes = block_group->sectorsize;
  2557. }
  2558. spin_lock(&ctl->tree_lock);
  2559. /*
  2560. * If we know we don't have enough space to make a cluster don't even
  2561. * bother doing all the work to try and find one.
  2562. */
  2563. if (ctl->free_space < bytes) {
  2564. spin_unlock(&ctl->tree_lock);
  2565. return -ENOSPC;
  2566. }
  2567. spin_lock(&cluster->lock);
  2568. /* someone already found a cluster, hooray */
  2569. if (cluster->block_group) {
  2570. ret = 0;
  2571. goto out;
  2572. }
  2573. trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
  2574. min_bytes);
  2575. ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
  2576. bytes + empty_size,
  2577. cont1_bytes, min_bytes);
  2578. if (ret)
  2579. ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
  2580. offset, bytes + empty_size,
  2581. cont1_bytes, min_bytes);
  2582. /* Clear our temporary list */
  2583. list_for_each_entry_safe(entry, tmp, &bitmaps, list)
  2584. list_del_init(&entry->list);
  2585. if (!ret) {
  2586. atomic_inc(&block_group->count);
  2587. list_add_tail(&cluster->block_group_list,
  2588. &block_group->cluster_list);
  2589. cluster->block_group = block_group;
  2590. } else {
  2591. trace_btrfs_failed_cluster_setup(block_group);
  2592. }
  2593. out:
  2594. spin_unlock(&cluster->lock);
  2595. spin_unlock(&ctl->tree_lock);
  2596. return ret;
  2597. }
  2598. /*
  2599. * simple code to zero out a cluster
  2600. */
  2601. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  2602. {
  2603. spin_lock_init(&cluster->lock);
  2604. spin_lock_init(&cluster->refill_lock);
  2605. cluster->root = RB_ROOT;
  2606. cluster->max_size = 0;
  2607. INIT_LIST_HEAD(&cluster->block_group_list);
  2608. cluster->block_group = NULL;
  2609. }
  2610. static int do_trimming(struct btrfs_block_group_cache *block_group,
  2611. u64 *total_trimmed, u64 start, u64 bytes,
  2612. u64 reserved_start, u64 reserved_bytes,
  2613. struct btrfs_trim_range *trim_entry)
  2614. {
  2615. struct btrfs_space_info *space_info = block_group->space_info;
  2616. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2617. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2618. int ret;
  2619. int update = 0;
  2620. u64 trimmed = 0;
  2621. spin_lock(&space_info->lock);
  2622. spin_lock(&block_group->lock);
  2623. if (!block_group->ro) {
  2624. block_group->reserved += reserved_bytes;
  2625. space_info->bytes_reserved += reserved_bytes;
  2626. update = 1;
  2627. }
  2628. spin_unlock(&block_group->lock);
  2629. spin_unlock(&space_info->lock);
  2630. ret = btrfs_discard_extent(fs_info->extent_root,
  2631. start, bytes, &trimmed);
  2632. if (!ret)
  2633. *total_trimmed += trimmed;
  2634. mutex_lock(&ctl->cache_writeout_mutex);
  2635. btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
  2636. list_del(&trim_entry->list);
  2637. mutex_unlock(&ctl->cache_writeout_mutex);
  2638. if (update) {
  2639. spin_lock(&space_info->lock);
  2640. spin_lock(&block_group->lock);
  2641. if (block_group->ro)
  2642. space_info->bytes_readonly += reserved_bytes;
  2643. block_group->reserved -= reserved_bytes;
  2644. space_info->bytes_reserved -= reserved_bytes;
  2645. spin_unlock(&space_info->lock);
  2646. spin_unlock(&block_group->lock);
  2647. }
  2648. return ret;
  2649. }
  2650. static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
  2651. u64 *total_trimmed, u64 start, u64 end, u64 minlen)
  2652. {
  2653. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2654. struct btrfs_free_space *entry;
  2655. struct rb_node *node;
  2656. int ret = 0;
  2657. u64 extent_start;
  2658. u64 extent_bytes;
  2659. u64 bytes;
  2660. while (start < end) {
  2661. struct btrfs_trim_range trim_entry;
  2662. mutex_lock(&ctl->cache_writeout_mutex);
  2663. spin_lock(&ctl->tree_lock);
  2664. if (ctl->free_space < minlen) {
  2665. spin_unlock(&ctl->tree_lock);
  2666. mutex_unlock(&ctl->cache_writeout_mutex);
  2667. break;
  2668. }
  2669. entry = tree_search_offset(ctl, start, 0, 1);
  2670. if (!entry) {
  2671. spin_unlock(&ctl->tree_lock);
  2672. mutex_unlock(&ctl->cache_writeout_mutex);
  2673. break;
  2674. }
  2675. /* skip bitmaps */
  2676. while (entry->bitmap) {
  2677. node = rb_next(&entry->offset_index);
  2678. if (!node) {
  2679. spin_unlock(&ctl->tree_lock);
  2680. mutex_unlock(&ctl->cache_writeout_mutex);
  2681. goto out;
  2682. }
  2683. entry = rb_entry(node, struct btrfs_free_space,
  2684. offset_index);
  2685. }
  2686. if (entry->offset >= end) {
  2687. spin_unlock(&ctl->tree_lock);
  2688. mutex_unlock(&ctl->cache_writeout_mutex);
  2689. break;
  2690. }
  2691. extent_start = entry->offset;
  2692. extent_bytes = entry->bytes;
  2693. start = max(start, extent_start);
  2694. bytes = min(extent_start + extent_bytes, end) - start;
  2695. if (bytes < minlen) {
  2696. spin_unlock(&ctl->tree_lock);
  2697. mutex_unlock(&ctl->cache_writeout_mutex);
  2698. goto next;
  2699. }
  2700. unlink_free_space(ctl, entry);
  2701. kmem_cache_free(btrfs_free_space_cachep, entry);
  2702. spin_unlock(&ctl->tree_lock);
  2703. trim_entry.start = extent_start;
  2704. trim_entry.bytes = extent_bytes;
  2705. list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
  2706. mutex_unlock(&ctl->cache_writeout_mutex);
  2707. ret = do_trimming(block_group, total_trimmed, start, bytes,
  2708. extent_start, extent_bytes, &trim_entry);
  2709. if (ret)
  2710. break;
  2711. next:
  2712. start += bytes;
  2713. if (fatal_signal_pending(current)) {
  2714. ret = -ERESTARTSYS;
  2715. break;
  2716. }
  2717. cond_resched();
  2718. }
  2719. out:
  2720. return ret;
  2721. }
  2722. static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
  2723. u64 *total_trimmed, u64 start, u64 end, u64 minlen)
  2724. {
  2725. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2726. struct btrfs_free_space *entry;
  2727. int ret = 0;
  2728. int ret2;
  2729. u64 bytes;
  2730. u64 offset = offset_to_bitmap(ctl, start);
  2731. while (offset < end) {
  2732. bool next_bitmap = false;
  2733. struct btrfs_trim_range trim_entry;
  2734. mutex_lock(&ctl->cache_writeout_mutex);
  2735. spin_lock(&ctl->tree_lock);
  2736. if (ctl->free_space < minlen) {
  2737. spin_unlock(&ctl->tree_lock);
  2738. mutex_unlock(&ctl->cache_writeout_mutex);
  2739. break;
  2740. }
  2741. entry = tree_search_offset(ctl, offset, 1, 0);
  2742. if (!entry) {
  2743. spin_unlock(&ctl->tree_lock);
  2744. mutex_unlock(&ctl->cache_writeout_mutex);
  2745. next_bitmap = true;
  2746. goto next;
  2747. }
  2748. bytes = minlen;
  2749. ret2 = search_bitmap(ctl, entry, &start, &bytes);
  2750. if (ret2 || start >= end) {
  2751. spin_unlock(&ctl->tree_lock);
  2752. mutex_unlock(&ctl->cache_writeout_mutex);
  2753. next_bitmap = true;
  2754. goto next;
  2755. }
  2756. bytes = min(bytes, end - start);
  2757. if (bytes < minlen) {
  2758. spin_unlock(&ctl->tree_lock);
  2759. mutex_unlock(&ctl->cache_writeout_mutex);
  2760. goto next;
  2761. }
  2762. bitmap_clear_bits(ctl, entry, start, bytes);
  2763. if (entry->bytes == 0)
  2764. free_bitmap(ctl, entry);
  2765. spin_unlock(&ctl->tree_lock);
  2766. trim_entry.start = start;
  2767. trim_entry.bytes = bytes;
  2768. list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
  2769. mutex_unlock(&ctl->cache_writeout_mutex);
  2770. ret = do_trimming(block_group, total_trimmed, start, bytes,
  2771. start, bytes, &trim_entry);
  2772. if (ret)
  2773. break;
  2774. next:
  2775. if (next_bitmap) {
  2776. offset += BITS_PER_BITMAP * ctl->unit;
  2777. } else {
  2778. start += bytes;
  2779. if (start >= offset + BITS_PER_BITMAP * ctl->unit)
  2780. offset += BITS_PER_BITMAP * ctl->unit;
  2781. }
  2782. if (fatal_signal_pending(current)) {
  2783. ret = -ERESTARTSYS;
  2784. break;
  2785. }
  2786. cond_resched();
  2787. }
  2788. return ret;
  2789. }
  2790. int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
  2791. u64 *trimmed, u64 start, u64 end, u64 minlen)
  2792. {
  2793. int ret;
  2794. *trimmed = 0;
  2795. spin_lock(&block_group->lock);
  2796. if (block_group->removed) {
  2797. spin_unlock(&block_group->lock);
  2798. return 0;
  2799. }
  2800. atomic_inc(&block_group->trimming);
  2801. spin_unlock(&block_group->lock);
  2802. ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
  2803. if (ret)
  2804. goto out;
  2805. ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
  2806. out:
  2807. spin_lock(&block_group->lock);
  2808. if (atomic_dec_and_test(&block_group->trimming) &&
  2809. block_group->removed) {
  2810. struct extent_map_tree *em_tree;
  2811. struct extent_map *em;
  2812. spin_unlock(&block_group->lock);
  2813. lock_chunks(block_group->fs_info->chunk_root);
  2814. em_tree = &block_group->fs_info->mapping_tree.map_tree;
  2815. write_lock(&em_tree->lock);
  2816. em = lookup_extent_mapping(em_tree, block_group->key.objectid,
  2817. 1);
  2818. BUG_ON(!em); /* logic error, can't happen */
  2819. /*
  2820. * remove_extent_mapping() will delete us from the pinned_chunks
  2821. * list, which is protected by the chunk mutex.
  2822. */
  2823. remove_extent_mapping(em_tree, em);
  2824. write_unlock(&em_tree->lock);
  2825. unlock_chunks(block_group->fs_info->chunk_root);
  2826. /* once for us and once for the tree */
  2827. free_extent_map(em);
  2828. free_extent_map(em);
  2829. /*
  2830. * We've left one free space entry and other tasks trimming
  2831. * this block group have left 1 entry each one. Free them.
  2832. */
  2833. __btrfs_remove_free_space_cache(block_group->free_space_ctl);
  2834. } else {
  2835. spin_unlock(&block_group->lock);
  2836. }
  2837. return ret;
  2838. }
  2839. /*
  2840. * Find the left-most item in the cache tree, and then return the
  2841. * smallest inode number in the item.
  2842. *
  2843. * Note: the returned inode number may not be the smallest one in
  2844. * the tree, if the left-most item is a bitmap.
  2845. */
  2846. u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
  2847. {
  2848. struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
  2849. struct btrfs_free_space *entry = NULL;
  2850. u64 ino = 0;
  2851. spin_lock(&ctl->tree_lock);
  2852. if (RB_EMPTY_ROOT(&ctl->free_space_offset))
  2853. goto out;
  2854. entry = rb_entry(rb_first(&ctl->free_space_offset),
  2855. struct btrfs_free_space, offset_index);
  2856. if (!entry->bitmap) {
  2857. ino = entry->offset;
  2858. unlink_free_space(ctl, entry);
  2859. entry->offset++;
  2860. entry->bytes--;
  2861. if (!entry->bytes)
  2862. kmem_cache_free(btrfs_free_space_cachep, entry);
  2863. else
  2864. link_free_space(ctl, entry);
  2865. } else {
  2866. u64 offset = 0;
  2867. u64 count = 1;
  2868. int ret;
  2869. ret = search_bitmap(ctl, entry, &offset, &count);
  2870. /* Logic error; Should be empty if it can't find anything */
  2871. ASSERT(!ret);
  2872. ino = offset;
  2873. bitmap_clear_bits(ctl, entry, offset, 1);
  2874. if (entry->bytes == 0)
  2875. free_bitmap(ctl, entry);
  2876. }
  2877. out:
  2878. spin_unlock(&ctl->tree_lock);
  2879. return ino;
  2880. }
  2881. struct inode *lookup_free_ino_inode(struct btrfs_root *root,
  2882. struct btrfs_path *path)
  2883. {
  2884. struct inode *inode = NULL;
  2885. spin_lock(&root->ino_cache_lock);
  2886. if (root->ino_cache_inode)
  2887. inode = igrab(root->ino_cache_inode);
  2888. spin_unlock(&root->ino_cache_lock);
  2889. if (inode)
  2890. return inode;
  2891. inode = __lookup_free_space_inode(root, path, 0);
  2892. if (IS_ERR(inode))
  2893. return inode;
  2894. spin_lock(&root->ino_cache_lock);
  2895. if (!btrfs_fs_closing(root->fs_info))
  2896. root->ino_cache_inode = igrab(inode);
  2897. spin_unlock(&root->ino_cache_lock);
  2898. return inode;
  2899. }
  2900. int create_free_ino_inode(struct btrfs_root *root,
  2901. struct btrfs_trans_handle *trans,
  2902. struct btrfs_path *path)
  2903. {
  2904. return __create_free_space_inode(root, trans, path,
  2905. BTRFS_FREE_INO_OBJECTID, 0);
  2906. }
  2907. int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  2908. {
  2909. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2910. struct btrfs_path *path;
  2911. struct inode *inode;
  2912. int ret = 0;
  2913. u64 root_gen = btrfs_root_generation(&root->root_item);
  2914. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2915. return 0;
  2916. /*
  2917. * If we're unmounting then just return, since this does a search on the
  2918. * normal root and not the commit root and we could deadlock.
  2919. */
  2920. if (btrfs_fs_closing(fs_info))
  2921. return 0;
  2922. path = btrfs_alloc_path();
  2923. if (!path)
  2924. return 0;
  2925. inode = lookup_free_ino_inode(root, path);
  2926. if (IS_ERR(inode))
  2927. goto out;
  2928. if (root_gen != BTRFS_I(inode)->generation)
  2929. goto out_put;
  2930. ret = __load_free_space_cache(root, inode, ctl, path, 0);
  2931. if (ret < 0)
  2932. btrfs_err(fs_info,
  2933. "failed to load free ino cache for root %llu",
  2934. root->root_key.objectid);
  2935. out_put:
  2936. iput(inode);
  2937. out:
  2938. btrfs_free_path(path);
  2939. return ret;
  2940. }
  2941. int btrfs_write_out_ino_cache(struct btrfs_root *root,
  2942. struct btrfs_trans_handle *trans,
  2943. struct btrfs_path *path,
  2944. struct inode *inode)
  2945. {
  2946. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2947. int ret;
  2948. struct btrfs_io_ctl io_ctl;
  2949. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2950. return 0;
  2951. memset(&io_ctl, 0, sizeof(io_ctl));
  2952. ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl,
  2953. trans, path, 0);
  2954. if (!ret)
  2955. ret = btrfs_wait_cache_io(root, trans, NULL, &io_ctl, path, 0);
  2956. if (ret) {
  2957. btrfs_delalloc_release_metadata(inode, inode->i_size);
  2958. #ifdef DEBUG
  2959. btrfs_err(root->fs_info,
  2960. "failed to write free ino cache for root %llu",
  2961. root->root_key.objectid);
  2962. #endif
  2963. }
  2964. return ret;
  2965. }
  2966. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  2967. /*
  2968. * Use this if you need to make a bitmap or extent entry specifically, it
  2969. * doesn't do any of the merging that add_free_space does, this acts a lot like
  2970. * how the free space cache loading stuff works, so you can get really weird
  2971. * configurations.
  2972. */
  2973. int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
  2974. u64 offset, u64 bytes, bool bitmap)
  2975. {
  2976. struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
  2977. struct btrfs_free_space *info = NULL, *bitmap_info;
  2978. void *map = NULL;
  2979. u64 bytes_added;
  2980. int ret;
  2981. again:
  2982. if (!info) {
  2983. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  2984. if (!info)
  2985. return -ENOMEM;
  2986. }
  2987. if (!bitmap) {
  2988. spin_lock(&ctl->tree_lock);
  2989. info->offset = offset;
  2990. info->bytes = bytes;
  2991. ret = link_free_space(ctl, info);
  2992. spin_unlock(&ctl->tree_lock);
  2993. if (ret)
  2994. kmem_cache_free(btrfs_free_space_cachep, info);
  2995. return ret;
  2996. }
  2997. if (!map) {
  2998. map = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  2999. if (!map) {
  3000. kmem_cache_free(btrfs_free_space_cachep, info);
  3001. return -ENOMEM;
  3002. }
  3003. }
  3004. spin_lock(&ctl->tree_lock);
  3005. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  3006. 1, 0);
  3007. if (!bitmap_info) {
  3008. info->bitmap = map;
  3009. map = NULL;
  3010. add_new_bitmap(ctl, info, offset);
  3011. bitmap_info = info;
  3012. info = NULL;
  3013. }
  3014. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
  3015. bytes -= bytes_added;
  3016. offset += bytes_added;
  3017. spin_unlock(&ctl->tree_lock);
  3018. if (bytes)
  3019. goto again;
  3020. if (info)
  3021. kmem_cache_free(btrfs_free_space_cachep, info);
  3022. if (map)
  3023. kfree(map);
  3024. return 0;
  3025. }
  3026. /*
  3027. * Checks to see if the given range is in the free space cache. This is really
  3028. * just used to check the absence of space, so if there is free space in the
  3029. * range at all we will return 1.
  3030. */
  3031. int test_check_exists(struct btrfs_block_group_cache *cache,
  3032. u64 offset, u64 bytes)
  3033. {
  3034. struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
  3035. struct btrfs_free_space *info;
  3036. int ret = 0;
  3037. spin_lock(&ctl->tree_lock);
  3038. info = tree_search_offset(ctl, offset, 0, 0);
  3039. if (!info) {
  3040. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  3041. 1, 0);
  3042. if (!info)
  3043. goto out;
  3044. }
  3045. have_info:
  3046. if (info->bitmap) {
  3047. u64 bit_off, bit_bytes;
  3048. struct rb_node *n;
  3049. struct btrfs_free_space *tmp;
  3050. bit_off = offset;
  3051. bit_bytes = ctl->unit;
  3052. ret = search_bitmap(ctl, info, &bit_off, &bit_bytes);
  3053. if (!ret) {
  3054. if (bit_off == offset) {
  3055. ret = 1;
  3056. goto out;
  3057. } else if (bit_off > offset &&
  3058. offset + bytes > bit_off) {
  3059. ret = 1;
  3060. goto out;
  3061. }
  3062. }
  3063. n = rb_prev(&info->offset_index);
  3064. while (n) {
  3065. tmp = rb_entry(n, struct btrfs_free_space,
  3066. offset_index);
  3067. if (tmp->offset + tmp->bytes < offset)
  3068. break;
  3069. if (offset + bytes < tmp->offset) {
  3070. n = rb_prev(&info->offset_index);
  3071. continue;
  3072. }
  3073. info = tmp;
  3074. goto have_info;
  3075. }
  3076. n = rb_next(&info->offset_index);
  3077. while (n) {
  3078. tmp = rb_entry(n, struct btrfs_free_space,
  3079. offset_index);
  3080. if (offset + bytes < tmp->offset)
  3081. break;
  3082. if (tmp->offset + tmp->bytes < offset) {
  3083. n = rb_next(&info->offset_index);
  3084. continue;
  3085. }
  3086. info = tmp;
  3087. goto have_info;
  3088. }
  3089. ret = 0;
  3090. goto out;
  3091. }
  3092. if (info->offset == offset) {
  3093. ret = 1;
  3094. goto out;
  3095. }
  3096. if (offset > info->offset && offset < info->offset + info->bytes)
  3097. ret = 1;
  3098. out:
  3099. spin_unlock(&ctl->tree_lock);
  3100. return ret;
  3101. }
  3102. #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */