free-space-cache.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  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 void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  1011. struct btrfs_free_space *info, u64 offset,
  1012. 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. ctl->free_space -= bytes;
  1021. }
  1022. static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
  1023. struct btrfs_free_space *info, u64 offset,
  1024. u64 bytes)
  1025. {
  1026. unsigned long start, count;
  1027. start = offset_to_bit(info->offset, ctl->unit, offset);
  1028. count = bytes_to_bits(bytes, ctl->unit);
  1029. BUG_ON(start + count > BITS_PER_BITMAP);
  1030. bitmap_set(info->bitmap, start, count);
  1031. info->bytes += bytes;
  1032. ctl->free_space += bytes;
  1033. }
  1034. static int search_bitmap(struct btrfs_free_space_ctl *ctl,
  1035. struct btrfs_free_space *bitmap_info, u64 *offset,
  1036. u64 *bytes)
  1037. {
  1038. unsigned long found_bits = 0;
  1039. unsigned long bits, i;
  1040. unsigned long next_zero;
  1041. i = offset_to_bit(bitmap_info->offset, ctl->unit,
  1042. max_t(u64, *offset, bitmap_info->offset));
  1043. bits = bytes_to_bits(*bytes, ctl->unit);
  1044. for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
  1045. i < BITS_PER_BITMAP;
  1046. i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
  1047. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  1048. BITS_PER_BITMAP, i);
  1049. if ((next_zero - i) >= bits) {
  1050. found_bits = next_zero - i;
  1051. break;
  1052. }
  1053. i = next_zero;
  1054. }
  1055. if (found_bits) {
  1056. *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
  1057. *bytes = (u64)(found_bits) * ctl->unit;
  1058. return 0;
  1059. }
  1060. return -1;
  1061. }
  1062. static struct btrfs_free_space *
  1063. find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
  1064. {
  1065. struct btrfs_free_space *entry;
  1066. struct rb_node *node;
  1067. int ret;
  1068. if (!ctl->free_space_offset.rb_node)
  1069. return NULL;
  1070. entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
  1071. if (!entry)
  1072. return NULL;
  1073. for (node = &entry->offset_index; node; node = rb_next(node)) {
  1074. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1075. if (entry->bytes < *bytes)
  1076. continue;
  1077. if (entry->bitmap) {
  1078. ret = search_bitmap(ctl, entry, offset, bytes);
  1079. if (!ret)
  1080. return entry;
  1081. continue;
  1082. }
  1083. *offset = entry->offset;
  1084. *bytes = entry->bytes;
  1085. return entry;
  1086. }
  1087. return NULL;
  1088. }
  1089. static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
  1090. struct btrfs_free_space *info, u64 offset)
  1091. {
  1092. info->offset = offset_to_bitmap(ctl, offset);
  1093. info->bytes = 0;
  1094. link_free_space(ctl, info);
  1095. ctl->total_bitmaps++;
  1096. ctl->op->recalc_thresholds(ctl);
  1097. }
  1098. static void free_bitmap(struct btrfs_free_space_ctl *ctl,
  1099. struct btrfs_free_space *bitmap_info)
  1100. {
  1101. unlink_free_space(ctl, bitmap_info);
  1102. kfree(bitmap_info->bitmap);
  1103. kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
  1104. ctl->total_bitmaps--;
  1105. ctl->op->recalc_thresholds(ctl);
  1106. }
  1107. static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
  1108. struct btrfs_free_space *bitmap_info,
  1109. u64 *offset, u64 *bytes)
  1110. {
  1111. u64 end;
  1112. u64 search_start, search_bytes;
  1113. int ret;
  1114. again:
  1115. end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
  1116. /*
  1117. * XXX - this can go away after a few releases.
  1118. *
  1119. * since the only user of btrfs_remove_free_space is the tree logging
  1120. * stuff, and the only way to test that is under crash conditions, we
  1121. * want to have this debug stuff here just in case somethings not
  1122. * working. Search the bitmap for the space we are trying to use to
  1123. * make sure its actually there. If its not there then we need to stop
  1124. * because something has gone wrong.
  1125. */
  1126. search_start = *offset;
  1127. search_bytes = *bytes;
  1128. search_bytes = min(search_bytes, end - search_start + 1);
  1129. ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
  1130. BUG_ON(ret < 0 || search_start != *offset);
  1131. if (*offset > bitmap_info->offset && *offset + *bytes > end) {
  1132. bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
  1133. *bytes -= end - *offset + 1;
  1134. *offset = end + 1;
  1135. } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
  1136. bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
  1137. *bytes = 0;
  1138. }
  1139. if (*bytes) {
  1140. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1141. if (!bitmap_info->bytes)
  1142. free_bitmap(ctl, bitmap_info);
  1143. /*
  1144. * no entry after this bitmap, but we still have bytes to
  1145. * remove, so something has gone wrong.
  1146. */
  1147. if (!next)
  1148. return -EINVAL;
  1149. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1150. offset_index);
  1151. /*
  1152. * if the next entry isn't a bitmap we need to return to let the
  1153. * extent stuff do its work.
  1154. */
  1155. if (!bitmap_info->bitmap)
  1156. return -EAGAIN;
  1157. /*
  1158. * Ok the next item is a bitmap, but it may not actually hold
  1159. * the information for the rest of this free space stuff, so
  1160. * look for it, and if we don't find it return so we can try
  1161. * everything over again.
  1162. */
  1163. search_start = *offset;
  1164. search_bytes = *bytes;
  1165. ret = search_bitmap(ctl, bitmap_info, &search_start,
  1166. &search_bytes);
  1167. if (ret < 0 || search_start != *offset)
  1168. return -EAGAIN;
  1169. goto again;
  1170. } else if (!bitmap_info->bytes)
  1171. free_bitmap(ctl, bitmap_info);
  1172. return 0;
  1173. }
  1174. static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
  1175. struct btrfs_free_space *info, u64 offset,
  1176. u64 bytes)
  1177. {
  1178. u64 bytes_to_set = 0;
  1179. u64 end;
  1180. end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
  1181. bytes_to_set = min(end - offset, bytes);
  1182. bitmap_set_bits(ctl, info, offset, bytes_to_set);
  1183. return bytes_to_set;
  1184. }
  1185. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  1186. struct btrfs_free_space *info)
  1187. {
  1188. struct btrfs_block_group_cache *block_group = ctl->private;
  1189. /*
  1190. * If we are below the extents threshold then we can add this as an
  1191. * extent, and don't have to deal with the bitmap
  1192. */
  1193. if (ctl->free_extents < ctl->extents_thresh) {
  1194. /*
  1195. * If this block group has some small extents we don't want to
  1196. * use up all of our free slots in the cache with them, we want
  1197. * to reserve them to larger extents, however if we have plent
  1198. * of cache left then go ahead an dadd them, no sense in adding
  1199. * the overhead of a bitmap if we don't have to.
  1200. */
  1201. if (info->bytes <= block_group->sectorsize * 4) {
  1202. if (ctl->free_extents * 2 <= ctl->extents_thresh)
  1203. return false;
  1204. } else {
  1205. return false;
  1206. }
  1207. }
  1208. /*
  1209. * some block groups are so tiny they can't be enveloped by a bitmap, so
  1210. * don't even bother to create a bitmap for this
  1211. */
  1212. if (BITS_PER_BITMAP * block_group->sectorsize >
  1213. block_group->key.offset)
  1214. return false;
  1215. return true;
  1216. }
  1217. static struct btrfs_free_space_op free_space_op = {
  1218. .recalc_thresholds = recalculate_thresholds,
  1219. .use_bitmap = use_bitmap,
  1220. };
  1221. static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
  1222. struct btrfs_free_space *info)
  1223. {
  1224. struct btrfs_free_space *bitmap_info;
  1225. struct btrfs_block_group_cache *block_group = NULL;
  1226. int added = 0;
  1227. u64 bytes, offset, bytes_added;
  1228. int ret;
  1229. bytes = info->bytes;
  1230. offset = info->offset;
  1231. if (!ctl->op->use_bitmap(ctl, info))
  1232. return 0;
  1233. if (ctl->op == &free_space_op)
  1234. block_group = ctl->private;
  1235. again:
  1236. /*
  1237. * Since we link bitmaps right into the cluster we need to see if we
  1238. * have a cluster here, and if so and it has our bitmap we need to add
  1239. * the free space to that bitmap.
  1240. */
  1241. if (block_group && !list_empty(&block_group->cluster_list)) {
  1242. struct btrfs_free_cluster *cluster;
  1243. struct rb_node *node;
  1244. struct btrfs_free_space *entry;
  1245. cluster = list_entry(block_group->cluster_list.next,
  1246. struct btrfs_free_cluster,
  1247. block_group_list);
  1248. spin_lock(&cluster->lock);
  1249. node = rb_first(&cluster->root);
  1250. if (!node) {
  1251. spin_unlock(&cluster->lock);
  1252. goto no_cluster_bitmap;
  1253. }
  1254. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1255. if (!entry->bitmap) {
  1256. spin_unlock(&cluster->lock);
  1257. goto no_cluster_bitmap;
  1258. }
  1259. if (entry->offset == offset_to_bitmap(ctl, offset)) {
  1260. bytes_added = add_bytes_to_bitmap(ctl, entry,
  1261. offset, bytes);
  1262. bytes -= bytes_added;
  1263. offset += bytes_added;
  1264. }
  1265. spin_unlock(&cluster->lock);
  1266. if (!bytes) {
  1267. ret = 1;
  1268. goto out;
  1269. }
  1270. }
  1271. no_cluster_bitmap:
  1272. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  1273. 1, 0);
  1274. if (!bitmap_info) {
  1275. BUG_ON(added);
  1276. goto new_bitmap;
  1277. }
  1278. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
  1279. bytes -= bytes_added;
  1280. offset += bytes_added;
  1281. added = 0;
  1282. if (!bytes) {
  1283. ret = 1;
  1284. goto out;
  1285. } else
  1286. goto again;
  1287. new_bitmap:
  1288. if (info && info->bitmap) {
  1289. add_new_bitmap(ctl, info, offset);
  1290. added = 1;
  1291. info = NULL;
  1292. goto again;
  1293. } else {
  1294. spin_unlock(&ctl->tree_lock);
  1295. /* no pre-allocated info, allocate a new one */
  1296. if (!info) {
  1297. info = kmem_cache_zalloc(btrfs_free_space_cachep,
  1298. GFP_NOFS);
  1299. if (!info) {
  1300. spin_lock(&ctl->tree_lock);
  1301. ret = -ENOMEM;
  1302. goto out;
  1303. }
  1304. }
  1305. /* allocate the bitmap */
  1306. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1307. spin_lock(&ctl->tree_lock);
  1308. if (!info->bitmap) {
  1309. ret = -ENOMEM;
  1310. goto out;
  1311. }
  1312. goto again;
  1313. }
  1314. out:
  1315. if (info) {
  1316. if (info->bitmap)
  1317. kfree(info->bitmap);
  1318. kmem_cache_free(btrfs_free_space_cachep, info);
  1319. }
  1320. return ret;
  1321. }
  1322. static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
  1323. struct btrfs_free_space *info, bool update_stat)
  1324. {
  1325. struct btrfs_free_space *left_info;
  1326. struct btrfs_free_space *right_info;
  1327. bool merged = false;
  1328. u64 offset = info->offset;
  1329. u64 bytes = info->bytes;
  1330. /*
  1331. * first we want to see if there is free space adjacent to the range we
  1332. * are adding, if there is remove that struct and add a new one to
  1333. * cover the entire range
  1334. */
  1335. right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
  1336. if (right_info && rb_prev(&right_info->offset_index))
  1337. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1338. struct btrfs_free_space, offset_index);
  1339. else
  1340. left_info = tree_search_offset(ctl, offset - 1, 0, 0);
  1341. if (right_info && !right_info->bitmap) {
  1342. if (update_stat)
  1343. unlink_free_space(ctl, right_info);
  1344. else
  1345. __unlink_free_space(ctl, right_info);
  1346. info->bytes += right_info->bytes;
  1347. kmem_cache_free(btrfs_free_space_cachep, right_info);
  1348. merged = true;
  1349. }
  1350. if (left_info && !left_info->bitmap &&
  1351. left_info->offset + left_info->bytes == offset) {
  1352. if (update_stat)
  1353. unlink_free_space(ctl, left_info);
  1354. else
  1355. __unlink_free_space(ctl, left_info);
  1356. info->offset = left_info->offset;
  1357. info->bytes += left_info->bytes;
  1358. kmem_cache_free(btrfs_free_space_cachep, left_info);
  1359. merged = true;
  1360. }
  1361. return merged;
  1362. }
  1363. int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
  1364. u64 offset, u64 bytes)
  1365. {
  1366. struct btrfs_free_space *info;
  1367. int ret = 0;
  1368. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  1369. if (!info)
  1370. return -ENOMEM;
  1371. info->offset = offset;
  1372. info->bytes = bytes;
  1373. spin_lock(&ctl->tree_lock);
  1374. if (try_merge_free_space(ctl, info, true))
  1375. goto link;
  1376. /*
  1377. * There was no extent directly to the left or right of this new
  1378. * extent then we know we're going to have to allocate a new extent, so
  1379. * before we do that see if we need to drop this into a bitmap
  1380. */
  1381. ret = insert_into_bitmap(ctl, info);
  1382. if (ret < 0) {
  1383. goto out;
  1384. } else if (ret) {
  1385. ret = 0;
  1386. goto out;
  1387. }
  1388. link:
  1389. ret = link_free_space(ctl, info);
  1390. if (ret)
  1391. kmem_cache_free(btrfs_free_space_cachep, info);
  1392. out:
  1393. spin_unlock(&ctl->tree_lock);
  1394. if (ret) {
  1395. printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
  1396. BUG_ON(ret == -EEXIST);
  1397. }
  1398. return ret;
  1399. }
  1400. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1401. u64 offset, u64 bytes)
  1402. {
  1403. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1404. struct btrfs_free_space *info;
  1405. struct btrfs_free_space *next_info = NULL;
  1406. int ret = 0;
  1407. spin_lock(&ctl->tree_lock);
  1408. again:
  1409. info = tree_search_offset(ctl, offset, 0, 0);
  1410. if (!info) {
  1411. /*
  1412. * oops didn't find an extent that matched the space we wanted
  1413. * to remove, look for a bitmap instead
  1414. */
  1415. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  1416. 1, 0);
  1417. if (!info) {
  1418. WARN_ON(1);
  1419. goto out_lock;
  1420. }
  1421. }
  1422. if (info->bytes < bytes && rb_next(&info->offset_index)) {
  1423. u64 end;
  1424. next_info = rb_entry(rb_next(&info->offset_index),
  1425. struct btrfs_free_space,
  1426. offset_index);
  1427. if (next_info->bitmap)
  1428. end = next_info->offset +
  1429. BITS_PER_BITMAP * ctl->unit - 1;
  1430. else
  1431. end = next_info->offset + next_info->bytes;
  1432. if (next_info->bytes < bytes ||
  1433. next_info->offset > offset || offset > end) {
  1434. printk(KERN_CRIT "Found free space at %llu, size %llu,"
  1435. " trying to use %llu\n",
  1436. (unsigned long long)info->offset,
  1437. (unsigned long long)info->bytes,
  1438. (unsigned long long)bytes);
  1439. WARN_ON(1);
  1440. ret = -EINVAL;
  1441. goto out_lock;
  1442. }
  1443. info = next_info;
  1444. }
  1445. if (info->bytes == bytes) {
  1446. unlink_free_space(ctl, info);
  1447. if (info->bitmap) {
  1448. kfree(info->bitmap);
  1449. ctl->total_bitmaps--;
  1450. }
  1451. kmem_cache_free(btrfs_free_space_cachep, info);
  1452. goto out_lock;
  1453. }
  1454. if (!info->bitmap && info->offset == offset) {
  1455. unlink_free_space(ctl, info);
  1456. info->offset += bytes;
  1457. info->bytes -= bytes;
  1458. link_free_space(ctl, info);
  1459. goto out_lock;
  1460. }
  1461. if (!info->bitmap && info->offset <= offset &&
  1462. info->offset + info->bytes >= offset + bytes) {
  1463. u64 old_start = info->offset;
  1464. /*
  1465. * we're freeing space in the middle of the info,
  1466. * this can happen during tree log replay
  1467. *
  1468. * first unlink the old info and then
  1469. * insert it again after the hole we're creating
  1470. */
  1471. unlink_free_space(ctl, info);
  1472. if (offset + bytes < info->offset + info->bytes) {
  1473. u64 old_end = info->offset + info->bytes;
  1474. info->offset = offset + bytes;
  1475. info->bytes = old_end - info->offset;
  1476. ret = link_free_space(ctl, info);
  1477. WARN_ON(ret);
  1478. if (ret)
  1479. goto out_lock;
  1480. } else {
  1481. /* the hole we're creating ends at the end
  1482. * of the info struct, just free the info
  1483. */
  1484. kmem_cache_free(btrfs_free_space_cachep, info);
  1485. }
  1486. spin_unlock(&ctl->tree_lock);
  1487. /* step two, insert a new info struct to cover
  1488. * anything before the hole
  1489. */
  1490. ret = btrfs_add_free_space(block_group, old_start,
  1491. offset - old_start);
  1492. WARN_ON(ret);
  1493. goto out;
  1494. }
  1495. ret = remove_from_bitmap(ctl, info, &offset, &bytes);
  1496. if (ret == -EAGAIN)
  1497. goto again;
  1498. BUG_ON(ret);
  1499. out_lock:
  1500. spin_unlock(&ctl->tree_lock);
  1501. out:
  1502. return ret;
  1503. }
  1504. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  1505. u64 bytes)
  1506. {
  1507. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1508. struct btrfs_free_space *info;
  1509. struct rb_node *n;
  1510. int count = 0;
  1511. for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
  1512. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1513. if (info->bytes >= bytes)
  1514. count++;
  1515. printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
  1516. (unsigned long long)info->offset,
  1517. (unsigned long long)info->bytes,
  1518. (info->bitmap) ? "yes" : "no");
  1519. }
  1520. printk(KERN_INFO "block group has cluster?: %s\n",
  1521. list_empty(&block_group->cluster_list) ? "no" : "yes");
  1522. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  1523. "\n", count);
  1524. }
  1525. void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
  1526. {
  1527. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1528. spin_lock_init(&ctl->tree_lock);
  1529. ctl->unit = block_group->sectorsize;
  1530. ctl->start = block_group->key.objectid;
  1531. ctl->private = block_group;
  1532. ctl->op = &free_space_op;
  1533. /*
  1534. * we only want to have 32k of ram per block group for keeping
  1535. * track of free space, and if we pass 1/2 of that we want to
  1536. * start converting things over to using bitmaps
  1537. */
  1538. ctl->extents_thresh = ((1024 * 32) / 2) /
  1539. sizeof(struct btrfs_free_space);
  1540. }
  1541. /*
  1542. * for a given cluster, put all of its extents back into the free
  1543. * space cache. If the block group passed doesn't match the block group
  1544. * pointed to by the cluster, someone else raced in and freed the
  1545. * cluster already. In that case, we just return without changing anything
  1546. */
  1547. static int
  1548. __btrfs_return_cluster_to_free_space(
  1549. struct btrfs_block_group_cache *block_group,
  1550. struct btrfs_free_cluster *cluster)
  1551. {
  1552. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1553. struct btrfs_free_space *entry;
  1554. struct rb_node *node;
  1555. spin_lock(&cluster->lock);
  1556. if (cluster->block_group != block_group)
  1557. goto out;
  1558. cluster->block_group = NULL;
  1559. cluster->window_start = 0;
  1560. list_del_init(&cluster->block_group_list);
  1561. node = rb_first(&cluster->root);
  1562. while (node) {
  1563. bool bitmap;
  1564. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1565. node = rb_next(&entry->offset_index);
  1566. rb_erase(&entry->offset_index, &cluster->root);
  1567. bitmap = (entry->bitmap != NULL);
  1568. if (!bitmap)
  1569. try_merge_free_space(ctl, entry, false);
  1570. tree_insert_offset(&ctl->free_space_offset,
  1571. entry->offset, &entry->offset_index, bitmap);
  1572. }
  1573. cluster->root = RB_ROOT;
  1574. out:
  1575. spin_unlock(&cluster->lock);
  1576. btrfs_put_block_group(block_group);
  1577. return 0;
  1578. }
  1579. void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
  1580. {
  1581. struct btrfs_free_space *info;
  1582. struct rb_node *node;
  1583. while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
  1584. info = rb_entry(node, struct btrfs_free_space, offset_index);
  1585. if (!info->bitmap) {
  1586. unlink_free_space(ctl, info);
  1587. kmem_cache_free(btrfs_free_space_cachep, info);
  1588. } else {
  1589. free_bitmap(ctl, info);
  1590. }
  1591. if (need_resched()) {
  1592. spin_unlock(&ctl->tree_lock);
  1593. cond_resched();
  1594. spin_lock(&ctl->tree_lock);
  1595. }
  1596. }
  1597. }
  1598. void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
  1599. {
  1600. spin_lock(&ctl->tree_lock);
  1601. __btrfs_remove_free_space_cache_locked(ctl);
  1602. spin_unlock(&ctl->tree_lock);
  1603. }
  1604. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  1605. {
  1606. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1607. struct btrfs_free_cluster *cluster;
  1608. struct list_head *head;
  1609. spin_lock(&ctl->tree_lock);
  1610. while ((head = block_group->cluster_list.next) !=
  1611. &block_group->cluster_list) {
  1612. cluster = list_entry(head, struct btrfs_free_cluster,
  1613. block_group_list);
  1614. WARN_ON(cluster->block_group != block_group);
  1615. __btrfs_return_cluster_to_free_space(block_group, cluster);
  1616. if (need_resched()) {
  1617. spin_unlock(&ctl->tree_lock);
  1618. cond_resched();
  1619. spin_lock(&ctl->tree_lock);
  1620. }
  1621. }
  1622. __btrfs_remove_free_space_cache_locked(ctl);
  1623. spin_unlock(&ctl->tree_lock);
  1624. }
  1625. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  1626. u64 offset, u64 bytes, u64 empty_size)
  1627. {
  1628. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1629. struct btrfs_free_space *entry = NULL;
  1630. u64 bytes_search = bytes + empty_size;
  1631. u64 ret = 0;
  1632. spin_lock(&ctl->tree_lock);
  1633. entry = find_free_space(ctl, &offset, &bytes_search);
  1634. if (!entry)
  1635. goto out;
  1636. ret = offset;
  1637. if (entry->bitmap) {
  1638. bitmap_clear_bits(ctl, entry, offset, bytes);
  1639. if (!entry->bytes)
  1640. free_bitmap(ctl, entry);
  1641. } else {
  1642. unlink_free_space(ctl, entry);
  1643. entry->offset += bytes;
  1644. entry->bytes -= bytes;
  1645. if (!entry->bytes)
  1646. kmem_cache_free(btrfs_free_space_cachep, entry);
  1647. else
  1648. link_free_space(ctl, entry);
  1649. }
  1650. out:
  1651. spin_unlock(&ctl->tree_lock);
  1652. return ret;
  1653. }
  1654. /*
  1655. * given a cluster, put all of its extents back into the free space
  1656. * cache. If a block group is passed, this function will only free
  1657. * a cluster that belongs to the passed block group.
  1658. *
  1659. * Otherwise, it'll get a reference on the block group pointed to by the
  1660. * cluster and remove the cluster from it.
  1661. */
  1662. int btrfs_return_cluster_to_free_space(
  1663. struct btrfs_block_group_cache *block_group,
  1664. struct btrfs_free_cluster *cluster)
  1665. {
  1666. struct btrfs_free_space_ctl *ctl;
  1667. int ret;
  1668. /* first, get a safe pointer to the block group */
  1669. spin_lock(&cluster->lock);
  1670. if (!block_group) {
  1671. block_group = cluster->block_group;
  1672. if (!block_group) {
  1673. spin_unlock(&cluster->lock);
  1674. return 0;
  1675. }
  1676. } else if (cluster->block_group != block_group) {
  1677. /* someone else has already freed it don't redo their work */
  1678. spin_unlock(&cluster->lock);
  1679. return 0;
  1680. }
  1681. atomic_inc(&block_group->count);
  1682. spin_unlock(&cluster->lock);
  1683. ctl = block_group->free_space_ctl;
  1684. /* now return any extents the cluster had on it */
  1685. spin_lock(&ctl->tree_lock);
  1686. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  1687. spin_unlock(&ctl->tree_lock);
  1688. /* finally drop our ref */
  1689. btrfs_put_block_group(block_group);
  1690. return ret;
  1691. }
  1692. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  1693. struct btrfs_free_cluster *cluster,
  1694. struct btrfs_free_space *entry,
  1695. u64 bytes, u64 min_start)
  1696. {
  1697. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1698. int err;
  1699. u64 search_start = cluster->window_start;
  1700. u64 search_bytes = bytes;
  1701. u64 ret = 0;
  1702. search_start = min_start;
  1703. search_bytes = bytes;
  1704. err = search_bitmap(ctl, entry, &search_start, &search_bytes);
  1705. if (err)
  1706. return 0;
  1707. ret = search_start;
  1708. bitmap_clear_bits(ctl, entry, ret, bytes);
  1709. return ret;
  1710. }
  1711. /*
  1712. * given a cluster, try to allocate 'bytes' from it, returns 0
  1713. * if it couldn't find anything suitably large, or a logical disk offset
  1714. * if things worked out
  1715. */
  1716. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  1717. struct btrfs_free_cluster *cluster, u64 bytes,
  1718. u64 min_start)
  1719. {
  1720. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1721. struct btrfs_free_space *entry = NULL;
  1722. struct rb_node *node;
  1723. u64 ret = 0;
  1724. spin_lock(&cluster->lock);
  1725. if (bytes > cluster->max_size)
  1726. goto out;
  1727. if (cluster->block_group != block_group)
  1728. goto out;
  1729. node = rb_first(&cluster->root);
  1730. if (!node)
  1731. goto out;
  1732. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1733. while(1) {
  1734. if (entry->bytes < bytes ||
  1735. (!entry->bitmap && entry->offset < min_start)) {
  1736. node = rb_next(&entry->offset_index);
  1737. if (!node)
  1738. break;
  1739. entry = rb_entry(node, struct btrfs_free_space,
  1740. offset_index);
  1741. continue;
  1742. }
  1743. if (entry->bitmap) {
  1744. ret = btrfs_alloc_from_bitmap(block_group,
  1745. cluster, entry, bytes,
  1746. min_start);
  1747. if (ret == 0) {
  1748. node = rb_next(&entry->offset_index);
  1749. if (!node)
  1750. break;
  1751. entry = rb_entry(node, struct btrfs_free_space,
  1752. offset_index);
  1753. continue;
  1754. }
  1755. } else {
  1756. ret = entry->offset;
  1757. entry->offset += bytes;
  1758. entry->bytes -= bytes;
  1759. }
  1760. if (entry->bytes == 0)
  1761. rb_erase(&entry->offset_index, &cluster->root);
  1762. break;
  1763. }
  1764. out:
  1765. spin_unlock(&cluster->lock);
  1766. if (!ret)
  1767. return 0;
  1768. spin_lock(&ctl->tree_lock);
  1769. ctl->free_space -= bytes;
  1770. if (entry->bytes == 0) {
  1771. ctl->free_extents--;
  1772. if (entry->bitmap) {
  1773. kfree(entry->bitmap);
  1774. ctl->total_bitmaps--;
  1775. ctl->op->recalc_thresholds(ctl);
  1776. }
  1777. kmem_cache_free(btrfs_free_space_cachep, entry);
  1778. }
  1779. spin_unlock(&ctl->tree_lock);
  1780. return ret;
  1781. }
  1782. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  1783. struct btrfs_free_space *entry,
  1784. struct btrfs_free_cluster *cluster,
  1785. u64 offset, u64 bytes, u64 min_bytes)
  1786. {
  1787. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1788. unsigned long next_zero;
  1789. unsigned long i;
  1790. unsigned long search_bits;
  1791. unsigned long total_bits;
  1792. unsigned long found_bits;
  1793. unsigned long start = 0;
  1794. unsigned long total_found = 0;
  1795. int ret;
  1796. bool found = false;
  1797. i = offset_to_bit(entry->offset, block_group->sectorsize,
  1798. max_t(u64, offset, entry->offset));
  1799. search_bits = bytes_to_bits(bytes, block_group->sectorsize);
  1800. total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
  1801. again:
  1802. found_bits = 0;
  1803. for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
  1804. i < BITS_PER_BITMAP;
  1805. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
  1806. next_zero = find_next_zero_bit(entry->bitmap,
  1807. BITS_PER_BITMAP, i);
  1808. if (next_zero - i >= search_bits) {
  1809. found_bits = next_zero - i;
  1810. break;
  1811. }
  1812. i = next_zero;
  1813. }
  1814. if (!found_bits)
  1815. return -ENOSPC;
  1816. if (!found) {
  1817. start = i;
  1818. found = true;
  1819. }
  1820. total_found += found_bits;
  1821. if (cluster->max_size < found_bits * block_group->sectorsize)
  1822. cluster->max_size = found_bits * block_group->sectorsize;
  1823. if (total_found < total_bits) {
  1824. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
  1825. if (i - start > total_bits * 2) {
  1826. total_found = 0;
  1827. cluster->max_size = 0;
  1828. found = false;
  1829. }
  1830. goto again;
  1831. }
  1832. cluster->window_start = start * block_group->sectorsize +
  1833. entry->offset;
  1834. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  1835. ret = tree_insert_offset(&cluster->root, entry->offset,
  1836. &entry->offset_index, 1);
  1837. BUG_ON(ret);
  1838. return 0;
  1839. }
  1840. /*
  1841. * This searches the block group for just extents to fill the cluster with.
  1842. */
  1843. static noinline int
  1844. setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
  1845. struct btrfs_free_cluster *cluster,
  1846. struct list_head *bitmaps, u64 offset, u64 bytes,
  1847. u64 min_bytes)
  1848. {
  1849. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1850. struct btrfs_free_space *first = NULL;
  1851. struct btrfs_free_space *entry = NULL;
  1852. struct btrfs_free_space *prev = NULL;
  1853. struct btrfs_free_space *last;
  1854. struct rb_node *node;
  1855. u64 window_start;
  1856. u64 window_free;
  1857. u64 max_extent;
  1858. u64 max_gap = 128 * 1024;
  1859. entry = tree_search_offset(ctl, offset, 0, 1);
  1860. if (!entry)
  1861. return -ENOSPC;
  1862. /*
  1863. * We don't want bitmaps, so just move along until we find a normal
  1864. * extent entry.
  1865. */
  1866. while (entry->bitmap) {
  1867. if (list_empty(&entry->list))
  1868. list_add_tail(&entry->list, bitmaps);
  1869. node = rb_next(&entry->offset_index);
  1870. if (!node)
  1871. return -ENOSPC;
  1872. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1873. }
  1874. window_start = entry->offset;
  1875. window_free = entry->bytes;
  1876. max_extent = entry->bytes;
  1877. first = entry;
  1878. last = entry;
  1879. prev = entry;
  1880. while (window_free <= min_bytes) {
  1881. node = rb_next(&entry->offset_index);
  1882. if (!node)
  1883. return -ENOSPC;
  1884. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1885. if (entry->bitmap) {
  1886. if (list_empty(&entry->list))
  1887. list_add_tail(&entry->list, bitmaps);
  1888. continue;
  1889. }
  1890. /*
  1891. * we haven't filled the empty size and the window is
  1892. * very large. reset and try again
  1893. */
  1894. if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
  1895. entry->offset - window_start > (min_bytes * 2)) {
  1896. first = entry;
  1897. window_start = entry->offset;
  1898. window_free = entry->bytes;
  1899. last = entry;
  1900. max_extent = entry->bytes;
  1901. } else {
  1902. last = entry;
  1903. window_free += entry->bytes;
  1904. if (entry->bytes > max_extent)
  1905. max_extent = entry->bytes;
  1906. }
  1907. prev = entry;
  1908. }
  1909. cluster->window_start = first->offset;
  1910. node = &first->offset_index;
  1911. /*
  1912. * now we've found our entries, pull them out of the free space
  1913. * cache and put them into the cluster rbtree
  1914. */
  1915. do {
  1916. int ret;
  1917. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1918. node = rb_next(&entry->offset_index);
  1919. if (entry->bitmap)
  1920. continue;
  1921. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  1922. ret = tree_insert_offset(&cluster->root, entry->offset,
  1923. &entry->offset_index, 0);
  1924. BUG_ON(ret);
  1925. } while (node && entry != last);
  1926. cluster->max_size = max_extent;
  1927. return 0;
  1928. }
  1929. /*
  1930. * This specifically looks for bitmaps that may work in the cluster, we assume
  1931. * that we have already failed to find extents that will work.
  1932. */
  1933. static noinline int
  1934. setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
  1935. struct btrfs_free_cluster *cluster,
  1936. struct list_head *bitmaps, u64 offset, u64 bytes,
  1937. u64 min_bytes)
  1938. {
  1939. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1940. struct btrfs_free_space *entry;
  1941. struct rb_node *node;
  1942. int ret = -ENOSPC;
  1943. if (ctl->total_bitmaps == 0)
  1944. return -ENOSPC;
  1945. /*
  1946. * First check our cached list of bitmaps and see if there is an entry
  1947. * here that will work.
  1948. */
  1949. list_for_each_entry(entry, bitmaps, list) {
  1950. if (entry->bytes < min_bytes)
  1951. continue;
  1952. ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
  1953. bytes, min_bytes);
  1954. if (!ret)
  1955. return 0;
  1956. }
  1957. /*
  1958. * If we do have entries on our list and we are here then we didn't find
  1959. * anything, so go ahead and get the next entry after the last entry in
  1960. * this list and start the search from there.
  1961. */
  1962. if (!list_empty(bitmaps)) {
  1963. entry = list_entry(bitmaps->prev, struct btrfs_free_space,
  1964. list);
  1965. node = rb_next(&entry->offset_index);
  1966. if (!node)
  1967. return -ENOSPC;
  1968. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1969. goto search;
  1970. }
  1971. entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
  1972. if (!entry)
  1973. return -ENOSPC;
  1974. search:
  1975. node = &entry->offset_index;
  1976. do {
  1977. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1978. node = rb_next(&entry->offset_index);
  1979. if (!entry->bitmap)
  1980. continue;
  1981. if (entry->bytes < min_bytes)
  1982. continue;
  1983. ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
  1984. bytes, min_bytes);
  1985. } while (ret && node);
  1986. return ret;
  1987. }
  1988. /*
  1989. * here we try to find a cluster of blocks in a block group. The goal
  1990. * is to find at least bytes free and up to empty_size + bytes free.
  1991. * We might not find them all in one contiguous area.
  1992. *
  1993. * returns zero and sets up cluster if things worked out, otherwise
  1994. * it returns -enospc
  1995. */
  1996. int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
  1997. struct btrfs_root *root,
  1998. struct btrfs_block_group_cache *block_group,
  1999. struct btrfs_free_cluster *cluster,
  2000. u64 offset, u64 bytes, u64 empty_size)
  2001. {
  2002. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2003. struct list_head bitmaps;
  2004. struct btrfs_free_space *entry, *tmp;
  2005. u64 min_bytes;
  2006. int ret;
  2007. /* for metadata, allow allocates with more holes */
  2008. if (btrfs_test_opt(root, SSD_SPREAD)) {
  2009. min_bytes = bytes + empty_size;
  2010. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  2011. /*
  2012. * we want to do larger allocations when we are
  2013. * flushing out the delayed refs, it helps prevent
  2014. * making more work as we go along.
  2015. */
  2016. if (trans->transaction->delayed_refs.flushing)
  2017. min_bytes = max(bytes, (bytes + empty_size) >> 1);
  2018. else
  2019. min_bytes = max(bytes, (bytes + empty_size) >> 4);
  2020. } else
  2021. min_bytes = max(bytes, (bytes + empty_size) >> 2);
  2022. spin_lock(&ctl->tree_lock);
  2023. /*
  2024. * If we know we don't have enough space to make a cluster don't even
  2025. * bother doing all the work to try and find one.
  2026. */
  2027. if (ctl->free_space < min_bytes) {
  2028. spin_unlock(&ctl->tree_lock);
  2029. return -ENOSPC;
  2030. }
  2031. spin_lock(&cluster->lock);
  2032. /* someone already found a cluster, hooray */
  2033. if (cluster->block_group) {
  2034. ret = 0;
  2035. goto out;
  2036. }
  2037. INIT_LIST_HEAD(&bitmaps);
  2038. ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
  2039. bytes, min_bytes);
  2040. if (ret)
  2041. ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
  2042. offset, bytes, min_bytes);
  2043. /* Clear our temporary list */
  2044. list_for_each_entry_safe(entry, tmp, &bitmaps, list)
  2045. list_del_init(&entry->list);
  2046. if (!ret) {
  2047. atomic_inc(&block_group->count);
  2048. list_add_tail(&cluster->block_group_list,
  2049. &block_group->cluster_list);
  2050. cluster->block_group = block_group;
  2051. }
  2052. out:
  2053. spin_unlock(&cluster->lock);
  2054. spin_unlock(&ctl->tree_lock);
  2055. return ret;
  2056. }
  2057. /*
  2058. * simple code to zero out a cluster
  2059. */
  2060. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  2061. {
  2062. spin_lock_init(&cluster->lock);
  2063. spin_lock_init(&cluster->refill_lock);
  2064. cluster->root = RB_ROOT;
  2065. cluster->max_size = 0;
  2066. INIT_LIST_HEAD(&cluster->block_group_list);
  2067. cluster->block_group = NULL;
  2068. }
  2069. int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
  2070. u64 *trimmed, u64 start, u64 end, u64 minlen)
  2071. {
  2072. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2073. struct btrfs_free_space *entry = NULL;
  2074. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2075. u64 bytes = 0;
  2076. u64 actually_trimmed;
  2077. int ret = 0;
  2078. *trimmed = 0;
  2079. while (start < end) {
  2080. spin_lock(&ctl->tree_lock);
  2081. if (ctl->free_space < minlen) {
  2082. spin_unlock(&ctl->tree_lock);
  2083. break;
  2084. }
  2085. entry = tree_search_offset(ctl, start, 0, 1);
  2086. if (!entry)
  2087. entry = tree_search_offset(ctl,
  2088. offset_to_bitmap(ctl, start),
  2089. 1, 1);
  2090. if (!entry || entry->offset >= end) {
  2091. spin_unlock(&ctl->tree_lock);
  2092. break;
  2093. }
  2094. if (entry->bitmap) {
  2095. ret = search_bitmap(ctl, entry, &start, &bytes);
  2096. if (!ret) {
  2097. if (start >= end) {
  2098. spin_unlock(&ctl->tree_lock);
  2099. break;
  2100. }
  2101. bytes = min(bytes, end - start);
  2102. bitmap_clear_bits(ctl, entry, start, bytes);
  2103. if (entry->bytes == 0)
  2104. free_bitmap(ctl, entry);
  2105. } else {
  2106. start = entry->offset + BITS_PER_BITMAP *
  2107. block_group->sectorsize;
  2108. spin_unlock(&ctl->tree_lock);
  2109. ret = 0;
  2110. continue;
  2111. }
  2112. } else {
  2113. start = entry->offset;
  2114. bytes = min(entry->bytes, end - start);
  2115. unlink_free_space(ctl, entry);
  2116. kmem_cache_free(btrfs_free_space_cachep, entry);
  2117. }
  2118. spin_unlock(&ctl->tree_lock);
  2119. if (bytes >= minlen) {
  2120. int update_ret;
  2121. update_ret = btrfs_update_reserved_bytes(block_group,
  2122. bytes, 1, 1);
  2123. ret = btrfs_error_discard_extent(fs_info->extent_root,
  2124. start,
  2125. bytes,
  2126. &actually_trimmed);
  2127. btrfs_add_free_space(block_group, start, bytes);
  2128. if (!update_ret)
  2129. btrfs_update_reserved_bytes(block_group,
  2130. bytes, 0, 1);
  2131. if (ret)
  2132. break;
  2133. *trimmed += actually_trimmed;
  2134. }
  2135. start += bytes;
  2136. bytes = 0;
  2137. if (fatal_signal_pending(current)) {
  2138. ret = -ERESTARTSYS;
  2139. break;
  2140. }
  2141. cond_resched();
  2142. }
  2143. return ret;
  2144. }
  2145. /*
  2146. * Find the left-most item in the cache tree, and then return the
  2147. * smallest inode number in the item.
  2148. *
  2149. * Note: the returned inode number may not be the smallest one in
  2150. * the tree, if the left-most item is a bitmap.
  2151. */
  2152. u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
  2153. {
  2154. struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
  2155. struct btrfs_free_space *entry = NULL;
  2156. u64 ino = 0;
  2157. spin_lock(&ctl->tree_lock);
  2158. if (RB_EMPTY_ROOT(&ctl->free_space_offset))
  2159. goto out;
  2160. entry = rb_entry(rb_first(&ctl->free_space_offset),
  2161. struct btrfs_free_space, offset_index);
  2162. if (!entry->bitmap) {
  2163. ino = entry->offset;
  2164. unlink_free_space(ctl, entry);
  2165. entry->offset++;
  2166. entry->bytes--;
  2167. if (!entry->bytes)
  2168. kmem_cache_free(btrfs_free_space_cachep, entry);
  2169. else
  2170. link_free_space(ctl, entry);
  2171. } else {
  2172. u64 offset = 0;
  2173. u64 count = 1;
  2174. int ret;
  2175. ret = search_bitmap(ctl, entry, &offset, &count);
  2176. BUG_ON(ret);
  2177. ino = offset;
  2178. bitmap_clear_bits(ctl, entry, offset, 1);
  2179. if (entry->bytes == 0)
  2180. free_bitmap(ctl, entry);
  2181. }
  2182. out:
  2183. spin_unlock(&ctl->tree_lock);
  2184. return ino;
  2185. }
  2186. struct inode *lookup_free_ino_inode(struct btrfs_root *root,
  2187. struct btrfs_path *path)
  2188. {
  2189. struct inode *inode = NULL;
  2190. spin_lock(&root->cache_lock);
  2191. if (root->cache_inode)
  2192. inode = igrab(root->cache_inode);
  2193. spin_unlock(&root->cache_lock);
  2194. if (inode)
  2195. return inode;
  2196. inode = __lookup_free_space_inode(root, path, 0);
  2197. if (IS_ERR(inode))
  2198. return inode;
  2199. spin_lock(&root->cache_lock);
  2200. if (!btrfs_fs_closing(root->fs_info))
  2201. root->cache_inode = igrab(inode);
  2202. spin_unlock(&root->cache_lock);
  2203. return inode;
  2204. }
  2205. int create_free_ino_inode(struct btrfs_root *root,
  2206. struct btrfs_trans_handle *trans,
  2207. struct btrfs_path *path)
  2208. {
  2209. return __create_free_space_inode(root, trans, path,
  2210. BTRFS_FREE_INO_OBJECTID, 0);
  2211. }
  2212. int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  2213. {
  2214. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2215. struct btrfs_path *path;
  2216. struct inode *inode;
  2217. int ret = 0;
  2218. u64 root_gen = btrfs_root_generation(&root->root_item);
  2219. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2220. return 0;
  2221. /*
  2222. * If we're unmounting then just return, since this does a search on the
  2223. * normal root and not the commit root and we could deadlock.
  2224. */
  2225. if (btrfs_fs_closing(fs_info))
  2226. return 0;
  2227. path = btrfs_alloc_path();
  2228. if (!path)
  2229. return 0;
  2230. inode = lookup_free_ino_inode(root, path);
  2231. if (IS_ERR(inode))
  2232. goto out;
  2233. if (root_gen != BTRFS_I(inode)->generation)
  2234. goto out_put;
  2235. ret = __load_free_space_cache(root, inode, ctl, path, 0);
  2236. if (ret < 0)
  2237. printk(KERN_ERR "btrfs: failed to load free ino cache for "
  2238. "root %llu\n", root->root_key.objectid);
  2239. out_put:
  2240. iput(inode);
  2241. out:
  2242. btrfs_free_path(path);
  2243. return ret;
  2244. }
  2245. int btrfs_write_out_ino_cache(struct btrfs_root *root,
  2246. struct btrfs_trans_handle *trans,
  2247. struct btrfs_path *path)
  2248. {
  2249. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2250. struct inode *inode;
  2251. int ret;
  2252. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2253. return 0;
  2254. inode = lookup_free_ino_inode(root, path);
  2255. if (IS_ERR(inode))
  2256. return 0;
  2257. ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
  2258. if (ret < 0)
  2259. printk(KERN_ERR "btrfs: failed to write free ino cache "
  2260. "for root %llu\n", root->root_key.objectid);
  2261. iput(inode);
  2262. return ret;
  2263. }