free-space-cache.c 65 KB

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