free-space-cache.c 74 KB

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