free-space-cache.c 72 KB

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