free-space-cache.c 65 KB

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