free-space-cache.c 65 KB

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