free-space-cache.c 72 KB

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