free-space-cache.c 80 KB

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