free-space-cache.c 87 KB

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