free-space-cache.c 76 KB

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