free-space-cache.c 72 KB

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