free-space-cache.c 72 KB

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