free-space-cache.c 72 KB

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