free-space-cache.c 70 KB

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