free-space-cache.c 73 KB

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