suballoc.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * suballoc.c
  5. *
  6. * metadata alloc and free
  7. * Inspired by ext3 block groups.
  8. *
  9. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "blockcheck.h"
  35. #include "dlmglue.h"
  36. #include "inode.h"
  37. #include "journal.h"
  38. #include "localalloc.h"
  39. #include "suballoc.h"
  40. #include "super.h"
  41. #include "sysfile.h"
  42. #include "uptodate.h"
  43. #include "buffer_head_io.h"
  44. #define NOT_ALLOC_NEW_GROUP 0
  45. #define ALLOC_NEW_GROUP 0x1
  46. #define ALLOC_GROUPS_FROM_GLOBAL 0x2
  47. #define OCFS2_MAX_TO_STEAL 1024
  48. struct ocfs2_suballoc_result {
  49. u64 sr_bg_blkno; /* The bg we allocated from. Set
  50. to 0 when a block group is
  51. contiguous. */
  52. u64 sr_blkno; /* The first allocated block */
  53. unsigned int sr_bit_offset; /* The bit in the bg */
  54. unsigned int sr_bits; /* How many bits we claimed */
  55. };
  56. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
  57. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
  58. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
  59. static int ocfs2_block_group_fill(handle_t *handle,
  60. struct inode *alloc_inode,
  61. struct buffer_head *bg_bh,
  62. u64 group_blkno,
  63. unsigned int group_clusters,
  64. u16 my_chain,
  65. struct ocfs2_chain_list *cl);
  66. static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  67. struct inode *alloc_inode,
  68. struct buffer_head *bh,
  69. u64 max_block,
  70. u64 *last_alloc_group,
  71. int flags);
  72. static int ocfs2_cluster_group_search(struct inode *inode,
  73. struct buffer_head *group_bh,
  74. u32 bits_wanted, u32 min_bits,
  75. u64 max_block,
  76. struct ocfs2_suballoc_result *res);
  77. static int ocfs2_block_group_search(struct inode *inode,
  78. struct buffer_head *group_bh,
  79. u32 bits_wanted, u32 min_bits,
  80. u64 max_block,
  81. struct ocfs2_suballoc_result *res);
  82. static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
  83. handle_t *handle,
  84. u32 bits_wanted,
  85. u32 min_bits,
  86. struct ocfs2_suballoc_result *res);
  87. static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  88. int nr);
  89. static inline int ocfs2_block_group_set_bits(handle_t *handle,
  90. struct inode *alloc_inode,
  91. struct ocfs2_group_desc *bg,
  92. struct buffer_head *group_bh,
  93. unsigned int bit_off,
  94. unsigned int num_bits);
  95. static int ocfs2_relink_block_group(handle_t *handle,
  96. struct inode *alloc_inode,
  97. struct buffer_head *fe_bh,
  98. struct buffer_head *bg_bh,
  99. struct buffer_head *prev_bg_bh,
  100. u16 chain);
  101. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  102. u32 wanted);
  103. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  104. u64 bg_blkno,
  105. u16 bg_bit_off);
  106. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  107. u64 data_blkno,
  108. u64 *bg_blkno,
  109. u16 *bg_bit_off);
  110. static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
  111. u32 bits_wanted, u64 max_block,
  112. int flags,
  113. struct ocfs2_alloc_context **ac);
  114. void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
  115. {
  116. struct inode *inode = ac->ac_inode;
  117. if (inode) {
  118. if (ac->ac_which != OCFS2_AC_USE_LOCAL)
  119. ocfs2_inode_unlock(inode, 1);
  120. mutex_unlock(&inode->i_mutex);
  121. iput(inode);
  122. ac->ac_inode = NULL;
  123. }
  124. brelse(ac->ac_bh);
  125. ac->ac_bh = NULL;
  126. ac->ac_resv = NULL;
  127. }
  128. void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
  129. {
  130. ocfs2_free_ac_resource(ac);
  131. kfree(ac);
  132. }
  133. static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
  134. {
  135. return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
  136. }
  137. #define do_error(fmt, ...) \
  138. do{ \
  139. if (resize) \
  140. mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
  141. else \
  142. ocfs2_error(sb, fmt, ##__VA_ARGS__); \
  143. } while (0)
  144. static int ocfs2_validate_gd_self(struct super_block *sb,
  145. struct buffer_head *bh,
  146. int resize)
  147. {
  148. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  149. if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
  150. do_error("Group descriptor #%llu has bad signature %.*s",
  151. (unsigned long long)bh->b_blocknr, 7,
  152. gd->bg_signature);
  153. return -EINVAL;
  154. }
  155. if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
  156. do_error("Group descriptor #%llu has an invalid bg_blkno "
  157. "of %llu",
  158. (unsigned long long)bh->b_blocknr,
  159. (unsigned long long)le64_to_cpu(gd->bg_blkno));
  160. return -EINVAL;
  161. }
  162. if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
  163. do_error("Group descriptor #%llu has an invalid "
  164. "fs_generation of #%u",
  165. (unsigned long long)bh->b_blocknr,
  166. le32_to_cpu(gd->bg_generation));
  167. return -EINVAL;
  168. }
  169. if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
  170. do_error("Group descriptor #%llu has bit count %u but "
  171. "claims that %u are free",
  172. (unsigned long long)bh->b_blocknr,
  173. le16_to_cpu(gd->bg_bits),
  174. le16_to_cpu(gd->bg_free_bits_count));
  175. return -EINVAL;
  176. }
  177. if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
  178. do_error("Group descriptor #%llu has bit count %u but "
  179. "max bitmap bits of %u",
  180. (unsigned long long)bh->b_blocknr,
  181. le16_to_cpu(gd->bg_bits),
  182. 8 * le16_to_cpu(gd->bg_size));
  183. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. static int ocfs2_validate_gd_parent(struct super_block *sb,
  188. struct ocfs2_dinode *di,
  189. struct buffer_head *bh,
  190. int resize)
  191. {
  192. unsigned int max_bits;
  193. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  194. if (di->i_blkno != gd->bg_parent_dinode) {
  195. do_error("Group descriptor #%llu has bad parent "
  196. "pointer (%llu, expected %llu)",
  197. (unsigned long long)bh->b_blocknr,
  198. (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
  199. (unsigned long long)le64_to_cpu(di->i_blkno));
  200. return -EINVAL;
  201. }
  202. max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
  203. if (le16_to_cpu(gd->bg_bits) > max_bits) {
  204. do_error("Group descriptor #%llu has bit count of %u",
  205. (unsigned long long)bh->b_blocknr,
  206. le16_to_cpu(gd->bg_bits));
  207. return -EINVAL;
  208. }
  209. /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
  210. if ((le16_to_cpu(gd->bg_chain) >
  211. le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
  212. ((le16_to_cpu(gd->bg_chain) ==
  213. le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
  214. do_error("Group descriptor #%llu has bad chain %u",
  215. (unsigned long long)bh->b_blocknr,
  216. le16_to_cpu(gd->bg_chain));
  217. return -EINVAL;
  218. }
  219. return 0;
  220. }
  221. #undef do_error
  222. /*
  223. * This version only prints errors. It does not fail the filesystem, and
  224. * exists only for resize.
  225. */
  226. int ocfs2_check_group_descriptor(struct super_block *sb,
  227. struct ocfs2_dinode *di,
  228. struct buffer_head *bh)
  229. {
  230. int rc;
  231. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  232. BUG_ON(!buffer_uptodate(bh));
  233. /*
  234. * If the ecc fails, we return the error but otherwise
  235. * leave the filesystem running. We know any error is
  236. * local to this block.
  237. */
  238. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
  239. if (rc) {
  240. mlog(ML_ERROR,
  241. "Checksum failed for group descriptor %llu\n",
  242. (unsigned long long)bh->b_blocknr);
  243. } else
  244. rc = ocfs2_validate_gd_self(sb, bh, 1);
  245. if (!rc)
  246. rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
  247. return rc;
  248. }
  249. static int ocfs2_validate_group_descriptor(struct super_block *sb,
  250. struct buffer_head *bh)
  251. {
  252. int rc;
  253. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  254. mlog(0, "Validating group descriptor %llu\n",
  255. (unsigned long long)bh->b_blocknr);
  256. BUG_ON(!buffer_uptodate(bh));
  257. /*
  258. * If the ecc fails, we return the error but otherwise
  259. * leave the filesystem running. We know any error is
  260. * local to this block.
  261. */
  262. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
  263. if (rc)
  264. return rc;
  265. /*
  266. * Errors after here are fatal.
  267. */
  268. return ocfs2_validate_gd_self(sb, bh, 0);
  269. }
  270. int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
  271. u64 gd_blkno, struct buffer_head **bh)
  272. {
  273. int rc;
  274. struct buffer_head *tmp = *bh;
  275. rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
  276. ocfs2_validate_group_descriptor);
  277. if (rc)
  278. goto out;
  279. rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
  280. if (rc) {
  281. brelse(tmp);
  282. goto out;
  283. }
  284. /* If ocfs2_read_block() got us a new bh, pass it up. */
  285. if (!*bh)
  286. *bh = tmp;
  287. out:
  288. return rc;
  289. }
  290. static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
  291. struct ocfs2_group_desc *bg,
  292. struct ocfs2_chain_list *cl,
  293. u64 p_blkno, u32 clusters)
  294. {
  295. struct ocfs2_extent_list *el = &bg->bg_list;
  296. struct ocfs2_extent_rec *rec;
  297. BUG_ON(!ocfs2_supports_discontig_bh(osb));
  298. if (!el->l_next_free_rec)
  299. el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
  300. rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
  301. rec->e_blkno = p_blkno;
  302. rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
  303. le16_to_cpu(cl->cl_bpc));
  304. rec->e_leaf_clusters = cpu_to_le32(clusters);
  305. le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
  306. le16_add_cpu(&el->l_next_free_rec, 1);
  307. }
  308. static int ocfs2_block_group_fill(handle_t *handle,
  309. struct inode *alloc_inode,
  310. struct buffer_head *bg_bh,
  311. u64 group_blkno,
  312. unsigned int group_clusters,
  313. u16 my_chain,
  314. struct ocfs2_chain_list *cl)
  315. {
  316. int status = 0;
  317. struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
  318. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  319. struct super_block * sb = alloc_inode->i_sb;
  320. mlog_entry_void();
  321. if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
  322. ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
  323. "b_blocknr (%llu)",
  324. (unsigned long long)group_blkno,
  325. (unsigned long long) bg_bh->b_blocknr);
  326. status = -EIO;
  327. goto bail;
  328. }
  329. status = ocfs2_journal_access_gd(handle,
  330. INODE_CACHE(alloc_inode),
  331. bg_bh,
  332. OCFS2_JOURNAL_ACCESS_CREATE);
  333. if (status < 0) {
  334. mlog_errno(status);
  335. goto bail;
  336. }
  337. memset(bg, 0, sb->s_blocksize);
  338. strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
  339. bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  340. bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1));
  341. bg->bg_chain = cpu_to_le16(my_chain);
  342. bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
  343. bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
  344. bg->bg_blkno = cpu_to_le64(group_blkno);
  345. if (group_clusters == le16_to_cpu(cl->cl_cpg))
  346. bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
  347. else
  348. ocfs2_bg_discontig_add_extent(osb, bg, cl, bg->bg_blkno,
  349. group_clusters);
  350. /* set the 1st bit in the bitmap to account for the descriptor block */
  351. ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
  352. bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
  353. ocfs2_journal_dirty(handle, bg_bh);
  354. /* There is no need to zero out or otherwise initialize the
  355. * other blocks in a group - All valid FS metadata in a block
  356. * group stores the superblock fs_generation value at
  357. * allocation time. */
  358. bail:
  359. mlog_exit(status);
  360. return status;
  361. }
  362. static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
  363. {
  364. u16 curr, best;
  365. best = curr = 0;
  366. while (curr < le16_to_cpu(cl->cl_count)) {
  367. if (le32_to_cpu(cl->cl_recs[best].c_total) >
  368. le32_to_cpu(cl->cl_recs[curr].c_total))
  369. best = curr;
  370. curr++;
  371. }
  372. return best;
  373. }
  374. static struct buffer_head *
  375. ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
  376. struct inode *alloc_inode,
  377. struct ocfs2_alloc_context *ac,
  378. struct ocfs2_chain_list *cl)
  379. {
  380. int status;
  381. u32 bit_off, num_bits;
  382. u64 bg_blkno;
  383. struct buffer_head *bg_bh;
  384. unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
  385. status = ocfs2_claim_clusters(handle, ac,
  386. le16_to_cpu(cl->cl_cpg), &bit_off,
  387. &num_bits);
  388. if (status < 0) {
  389. if (status != -ENOSPC)
  390. mlog_errno(status);
  391. goto bail;
  392. }
  393. /* setup the group */
  394. bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  395. mlog(0, "new descriptor, record %u, at block %llu\n",
  396. alloc_rec, (unsigned long long)bg_blkno);
  397. bg_bh = sb_getblk(osb->sb, bg_blkno);
  398. if (!bg_bh) {
  399. status = -EIO;
  400. mlog_errno(status);
  401. goto bail;
  402. }
  403. ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
  404. status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
  405. bg_blkno, num_bits, alloc_rec, cl);
  406. if (status < 0) {
  407. brelse(bg_bh);
  408. mlog_errno(status);
  409. }
  410. bail:
  411. return status ? ERR_PTR(status) : bg_bh;
  412. }
  413. static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
  414. handle_t *handle,
  415. struct ocfs2_alloc_context *ac,
  416. unsigned int min_bits,
  417. u32 *bit_off, u32 *num_bits)
  418. {
  419. int status;
  420. while (min_bits) {
  421. status = ocfs2_claim_clusters(handle, ac, min_bits,
  422. bit_off, num_bits);
  423. if (status != -ENOSPC)
  424. break;
  425. min_bits >>= 1;
  426. }
  427. return status;
  428. }
  429. static int ocfs2_block_group_grow_discontig(handle_t *handle,
  430. struct inode *alloc_inode,
  431. struct buffer_head *bg_bh,
  432. struct ocfs2_alloc_context *ac,
  433. struct ocfs2_chain_list *cl,
  434. unsigned int min_bits)
  435. {
  436. int status;
  437. struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
  438. struct ocfs2_group_desc *bg =
  439. (struct ocfs2_group_desc *)bg_bh->b_data;
  440. unsigned int needed =
  441. ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
  442. u32 p_cpos, clusters;
  443. u64 p_blkno;
  444. struct ocfs2_extent_list *el = &bg->bg_list;
  445. status = ocfs2_journal_access_gd(handle,
  446. INODE_CACHE(alloc_inode),
  447. bg_bh,
  448. OCFS2_JOURNAL_ACCESS_CREATE);
  449. if (status < 0) {
  450. mlog_errno(status);
  451. goto bail;
  452. }
  453. while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
  454. le16_to_cpu(el->l_count))) {
  455. if (min_bits > needed)
  456. min_bits = needed;
  457. status = ocfs2_block_group_claim_bits(osb, handle, ac,
  458. min_bits, &p_cpos,
  459. &clusters);
  460. if (status < 0) {
  461. if (status != -ENOSPC)
  462. mlog_errno(status);
  463. goto bail;
  464. }
  465. p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
  466. ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
  467. clusters);
  468. min_bits = clusters;
  469. needed = ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
  470. }
  471. if (needed > 0) {
  472. }
  473. ocfs2_journal_dirty(handle, bg_bh);
  474. bail:
  475. return status;
  476. }
  477. static void ocfs2_bg_alloc_cleanup(handle_t *handle,
  478. struct ocfs2_alloc_context *cluster_ac,
  479. struct inode *alloc_inode,
  480. struct buffer_head *bg_bh)
  481. {
  482. int i, ret;
  483. struct ocfs2_group_desc *bg;
  484. struct ocfs2_extent_list *el;
  485. struct ocfs2_extent_rec *rec;
  486. if (!bg_bh)
  487. return;
  488. bg = (struct ocfs2_group_desc *)bg_bh->b_data;
  489. el = &bg->bg_list;
  490. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  491. rec = &el->l_recs[i];
  492. ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
  493. cluster_ac->ac_bh,
  494. le64_to_cpu(rec->e_blkno),
  495. le32_to_cpu(rec->e_leaf_clusters));
  496. if (ret)
  497. mlog_errno(ret);
  498. /* Try all the clusters to free */
  499. }
  500. ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
  501. brelse(bg_bh);
  502. }
  503. static struct buffer_head *
  504. ocfs2_block_group_alloc_discontig(handle_t *handle,
  505. struct inode *alloc_inode,
  506. struct ocfs2_alloc_context *ac,
  507. struct ocfs2_chain_list *cl)
  508. {
  509. int status;
  510. u32 bit_off, num_bits;
  511. u64 bg_blkno;
  512. unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
  513. struct buffer_head *bg_bh = NULL;
  514. unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
  515. struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
  516. if (!ocfs2_supports_discontig_bh(osb)) {
  517. status = -ENOSPC;
  518. goto bail;
  519. }
  520. status = ocfs2_extend_trans(handle,
  521. ocfs2_calc_bg_discontig_credits(osb->sb));
  522. if (status) {
  523. mlog_errno(status);
  524. goto bail;
  525. }
  526. /*
  527. * We're going to be grabbing from multiple cluster groups.
  528. * We don't have enough credits to relink them all, and the
  529. * cluster groups will be staying in cache for the duration of
  530. * this operation.
  531. */
  532. ac->ac_allow_chain_relink = 0;
  533. /* Claim the first region */
  534. status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
  535. &bit_off, &num_bits);
  536. if (status < 0) {
  537. if (status != -ENOSPC)
  538. mlog_errno(status);
  539. goto bail;
  540. }
  541. min_bits = num_bits;
  542. /* setup the group */
  543. bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  544. mlog(0, "new descriptor, record %u, at block %llu\n",
  545. alloc_rec, (unsigned long long)bg_blkno);
  546. bg_bh = sb_getblk(osb->sb, bg_blkno);
  547. if (!bg_bh) {
  548. status = -EIO;
  549. mlog_errno(status);
  550. goto bail;
  551. }
  552. ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
  553. status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
  554. bg_blkno, num_bits, alloc_rec, cl);
  555. if (status < 0) {
  556. mlog_errno(status);
  557. goto bail;
  558. }
  559. status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
  560. bg_bh, ac, cl, min_bits);
  561. if (status)
  562. mlog_errno(status);
  563. bail:
  564. if (status)
  565. ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
  566. return status ? ERR_PTR(status) : bg_bh;
  567. }
  568. /*
  569. * We expect the block group allocator to already be locked.
  570. */
  571. static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  572. struct inode *alloc_inode,
  573. struct buffer_head *bh,
  574. u64 max_block,
  575. u64 *last_alloc_group,
  576. int flags)
  577. {
  578. int status, credits;
  579. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  580. struct ocfs2_chain_list *cl;
  581. struct ocfs2_alloc_context *ac = NULL;
  582. handle_t *handle = NULL;
  583. u64 bg_blkno;
  584. struct buffer_head *bg_bh = NULL;
  585. struct ocfs2_group_desc *bg;
  586. BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
  587. mlog_entry_void();
  588. cl = &fe->id2.i_chain;
  589. status = ocfs2_reserve_clusters_with_limit(osb,
  590. le16_to_cpu(cl->cl_cpg),
  591. max_block, flags, &ac);
  592. if (status < 0) {
  593. if (status != -ENOSPC)
  594. mlog_errno(status);
  595. goto bail;
  596. }
  597. credits = ocfs2_calc_group_alloc_credits(osb->sb,
  598. le16_to_cpu(cl->cl_cpg));
  599. handle = ocfs2_start_trans(osb, credits);
  600. if (IS_ERR(handle)) {
  601. status = PTR_ERR(handle);
  602. handle = NULL;
  603. mlog_errno(status);
  604. goto bail;
  605. }
  606. if (last_alloc_group && *last_alloc_group != 0) {
  607. mlog(0, "use old allocation group %llu for block group alloc\n",
  608. (unsigned long long)*last_alloc_group);
  609. ac->ac_last_group = *last_alloc_group;
  610. }
  611. bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
  612. ac, cl);
  613. if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
  614. bg_bh = ocfs2_block_group_alloc_discontig(handle,
  615. alloc_inode,
  616. ac, cl);
  617. if (IS_ERR(bg_bh)) {
  618. status = PTR_ERR(bg_bh);
  619. bg_bh = NULL;
  620. if (status != -ENOSPC)
  621. mlog_errno(status);
  622. goto bail;
  623. }
  624. bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  625. status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  626. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  627. if (status < 0) {
  628. mlog_errno(status);
  629. goto bail;
  630. }
  631. le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_free,
  632. le16_to_cpu(bg->bg_free_bits_count));
  633. le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_total,
  634. le16_to_cpu(bg->bg_bits));
  635. cl->cl_recs[bg->bg_chain].c_blkno = cpu_to_le64(bg_blkno);
  636. if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
  637. le16_add_cpu(&cl->cl_next_free_rec, 1);
  638. le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
  639. le16_to_cpu(bg->bg_free_bits_count));
  640. le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
  641. le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
  642. ocfs2_journal_dirty(handle, bh);
  643. spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
  644. OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  645. fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
  646. le32_to_cpu(fe->i_clusters)));
  647. spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
  648. i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
  649. alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
  650. status = 0;
  651. /* save the new last alloc group so that the caller can cache it. */
  652. if (last_alloc_group)
  653. *last_alloc_group = ac->ac_last_group;
  654. bail:
  655. if (handle)
  656. ocfs2_commit_trans(osb, handle);
  657. if (ac)
  658. ocfs2_free_alloc_context(ac);
  659. brelse(bg_bh);
  660. mlog_exit(status);
  661. return status;
  662. }
  663. static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
  664. struct ocfs2_alloc_context *ac,
  665. int type,
  666. u32 slot,
  667. u64 *last_alloc_group,
  668. int flags)
  669. {
  670. int status;
  671. u32 bits_wanted = ac->ac_bits_wanted;
  672. struct inode *alloc_inode;
  673. struct buffer_head *bh = NULL;
  674. struct ocfs2_dinode *fe;
  675. u32 free_bits;
  676. mlog_entry_void();
  677. alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
  678. if (!alloc_inode) {
  679. mlog_errno(-EINVAL);
  680. return -EINVAL;
  681. }
  682. mutex_lock(&alloc_inode->i_mutex);
  683. status = ocfs2_inode_lock(alloc_inode, &bh, 1);
  684. if (status < 0) {
  685. mutex_unlock(&alloc_inode->i_mutex);
  686. iput(alloc_inode);
  687. mlog_errno(status);
  688. return status;
  689. }
  690. ac->ac_inode = alloc_inode;
  691. ac->ac_alloc_slot = slot;
  692. fe = (struct ocfs2_dinode *) bh->b_data;
  693. /* The bh was validated by the inode read inside
  694. * ocfs2_inode_lock(). Any corruption is a code bug. */
  695. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  696. if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
  697. ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
  698. (unsigned long long)le64_to_cpu(fe->i_blkno));
  699. status = -EIO;
  700. goto bail;
  701. }
  702. free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
  703. le32_to_cpu(fe->id1.bitmap1.i_used);
  704. if (bits_wanted > free_bits) {
  705. /* cluster bitmap never grows */
  706. if (ocfs2_is_cluster_bitmap(alloc_inode)) {
  707. mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
  708. bits_wanted, free_bits);
  709. status = -ENOSPC;
  710. goto bail;
  711. }
  712. if (!(flags & ALLOC_NEW_GROUP)) {
  713. mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
  714. "and we don't alloc a new group for it.\n",
  715. slot, bits_wanted, free_bits);
  716. status = -ENOSPC;
  717. goto bail;
  718. }
  719. status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
  720. ac->ac_max_block,
  721. last_alloc_group, flags);
  722. if (status < 0) {
  723. if (status != -ENOSPC)
  724. mlog_errno(status);
  725. goto bail;
  726. }
  727. atomic_inc(&osb->alloc_stats.bg_extends);
  728. /* You should never ask for this much metadata */
  729. BUG_ON(bits_wanted >
  730. (le32_to_cpu(fe->id1.bitmap1.i_total)
  731. - le32_to_cpu(fe->id1.bitmap1.i_used)));
  732. }
  733. get_bh(bh);
  734. ac->ac_bh = bh;
  735. bail:
  736. brelse(bh);
  737. mlog_exit(status);
  738. return status;
  739. }
  740. static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
  741. {
  742. spin_lock(&osb->osb_lock);
  743. osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
  744. spin_unlock(&osb->osb_lock);
  745. atomic_set(&osb->s_num_inodes_stolen, 0);
  746. }
  747. static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
  748. {
  749. spin_lock(&osb->osb_lock);
  750. osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
  751. spin_unlock(&osb->osb_lock);
  752. atomic_set(&osb->s_num_meta_stolen, 0);
  753. }
  754. void ocfs2_init_steal_slots(struct ocfs2_super *osb)
  755. {
  756. ocfs2_init_inode_steal_slot(osb);
  757. ocfs2_init_meta_steal_slot(osb);
  758. }
  759. static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
  760. {
  761. spin_lock(&osb->osb_lock);
  762. if (type == INODE_ALLOC_SYSTEM_INODE)
  763. osb->s_inode_steal_slot = slot;
  764. else if (type == EXTENT_ALLOC_SYSTEM_INODE)
  765. osb->s_meta_steal_slot = slot;
  766. spin_unlock(&osb->osb_lock);
  767. }
  768. static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
  769. {
  770. int slot = OCFS2_INVALID_SLOT;
  771. spin_lock(&osb->osb_lock);
  772. if (type == INODE_ALLOC_SYSTEM_INODE)
  773. slot = osb->s_inode_steal_slot;
  774. else if (type == EXTENT_ALLOC_SYSTEM_INODE)
  775. slot = osb->s_meta_steal_slot;
  776. spin_unlock(&osb->osb_lock);
  777. return slot;
  778. }
  779. static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
  780. {
  781. return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
  782. }
  783. static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
  784. {
  785. return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
  786. }
  787. static int ocfs2_steal_resource(struct ocfs2_super *osb,
  788. struct ocfs2_alloc_context *ac,
  789. int type)
  790. {
  791. int i, status = -ENOSPC;
  792. int slot = __ocfs2_get_steal_slot(osb, type);
  793. /* Start to steal resource from the first slot after ours. */
  794. if (slot == OCFS2_INVALID_SLOT)
  795. slot = osb->slot_num + 1;
  796. for (i = 0; i < osb->max_slots; i++, slot++) {
  797. if (slot == osb->max_slots)
  798. slot = 0;
  799. if (slot == osb->slot_num)
  800. continue;
  801. status = ocfs2_reserve_suballoc_bits(osb, ac,
  802. type,
  803. (u32)slot, NULL,
  804. NOT_ALLOC_NEW_GROUP);
  805. if (status >= 0) {
  806. __ocfs2_set_steal_slot(osb, slot, type);
  807. break;
  808. }
  809. ocfs2_free_ac_resource(ac);
  810. }
  811. return status;
  812. }
  813. static int ocfs2_steal_inode(struct ocfs2_super *osb,
  814. struct ocfs2_alloc_context *ac)
  815. {
  816. return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
  817. }
  818. static int ocfs2_steal_meta(struct ocfs2_super *osb,
  819. struct ocfs2_alloc_context *ac)
  820. {
  821. return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
  822. }
  823. int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
  824. int blocks,
  825. struct ocfs2_alloc_context **ac)
  826. {
  827. int status;
  828. int slot = ocfs2_get_meta_steal_slot(osb);
  829. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  830. if (!(*ac)) {
  831. status = -ENOMEM;
  832. mlog_errno(status);
  833. goto bail;
  834. }
  835. (*ac)->ac_bits_wanted = blocks;
  836. (*ac)->ac_which = OCFS2_AC_USE_META;
  837. (*ac)->ac_group_search = ocfs2_block_group_search;
  838. if (slot != OCFS2_INVALID_SLOT &&
  839. atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
  840. goto extent_steal;
  841. atomic_set(&osb->s_num_meta_stolen, 0);
  842. status = ocfs2_reserve_suballoc_bits(osb, (*ac),
  843. EXTENT_ALLOC_SYSTEM_INODE,
  844. (u32)osb->slot_num, NULL,
  845. ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
  846. if (status >= 0) {
  847. status = 0;
  848. if (slot != OCFS2_INVALID_SLOT)
  849. ocfs2_init_meta_steal_slot(osb);
  850. goto bail;
  851. } else if (status < 0 && status != -ENOSPC) {
  852. mlog_errno(status);
  853. goto bail;
  854. }
  855. ocfs2_free_ac_resource(*ac);
  856. extent_steal:
  857. status = ocfs2_steal_meta(osb, *ac);
  858. atomic_inc(&osb->s_num_meta_stolen);
  859. if (status < 0) {
  860. if (status != -ENOSPC)
  861. mlog_errno(status);
  862. goto bail;
  863. }
  864. status = 0;
  865. bail:
  866. if ((status < 0) && *ac) {
  867. ocfs2_free_alloc_context(*ac);
  868. *ac = NULL;
  869. }
  870. mlog_exit(status);
  871. return status;
  872. }
  873. int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
  874. struct ocfs2_extent_list *root_el,
  875. struct ocfs2_alloc_context **ac)
  876. {
  877. return ocfs2_reserve_new_metadata_blocks(osb,
  878. ocfs2_extend_meta_needed(root_el),
  879. ac);
  880. }
  881. int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
  882. struct ocfs2_alloc_context **ac)
  883. {
  884. int status;
  885. int slot = ocfs2_get_inode_steal_slot(osb);
  886. u64 alloc_group;
  887. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  888. if (!(*ac)) {
  889. status = -ENOMEM;
  890. mlog_errno(status);
  891. goto bail;
  892. }
  893. (*ac)->ac_bits_wanted = 1;
  894. (*ac)->ac_which = OCFS2_AC_USE_INODE;
  895. (*ac)->ac_group_search = ocfs2_block_group_search;
  896. /*
  897. * stat(2) can't handle i_ino > 32bits, so we tell the
  898. * lower levels not to allocate us a block group past that
  899. * limit. The 'inode64' mount option avoids this behavior.
  900. */
  901. if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
  902. (*ac)->ac_max_block = (u32)~0U;
  903. /*
  904. * slot is set when we successfully steal inode from other nodes.
  905. * It is reset in 3 places:
  906. * 1. when we flush the truncate log
  907. * 2. when we complete local alloc recovery.
  908. * 3. when we successfully allocate from our own slot.
  909. * After it is set, we will go on stealing inodes until we find the
  910. * need to check our slots to see whether there is some space for us.
  911. */
  912. if (slot != OCFS2_INVALID_SLOT &&
  913. atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
  914. goto inode_steal;
  915. atomic_set(&osb->s_num_inodes_stolen, 0);
  916. alloc_group = osb->osb_inode_alloc_group;
  917. status = ocfs2_reserve_suballoc_bits(osb, *ac,
  918. INODE_ALLOC_SYSTEM_INODE,
  919. (u32)osb->slot_num,
  920. &alloc_group,
  921. ALLOC_NEW_GROUP |
  922. ALLOC_GROUPS_FROM_GLOBAL);
  923. if (status >= 0) {
  924. status = 0;
  925. spin_lock(&osb->osb_lock);
  926. osb->osb_inode_alloc_group = alloc_group;
  927. spin_unlock(&osb->osb_lock);
  928. mlog(0, "after reservation, new allocation group is "
  929. "%llu\n", (unsigned long long)alloc_group);
  930. /*
  931. * Some inodes must be freed by us, so try to allocate
  932. * from our own next time.
  933. */
  934. if (slot != OCFS2_INVALID_SLOT)
  935. ocfs2_init_inode_steal_slot(osb);
  936. goto bail;
  937. } else if (status < 0 && status != -ENOSPC) {
  938. mlog_errno(status);
  939. goto bail;
  940. }
  941. ocfs2_free_ac_resource(*ac);
  942. inode_steal:
  943. status = ocfs2_steal_inode(osb, *ac);
  944. atomic_inc(&osb->s_num_inodes_stolen);
  945. if (status < 0) {
  946. if (status != -ENOSPC)
  947. mlog_errno(status);
  948. goto bail;
  949. }
  950. status = 0;
  951. bail:
  952. if ((status < 0) && *ac) {
  953. ocfs2_free_alloc_context(*ac);
  954. *ac = NULL;
  955. }
  956. mlog_exit(status);
  957. return status;
  958. }
  959. /* local alloc code has to do the same thing, so rather than do this
  960. * twice.. */
  961. int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
  962. struct ocfs2_alloc_context *ac)
  963. {
  964. int status;
  965. ac->ac_which = OCFS2_AC_USE_MAIN;
  966. ac->ac_group_search = ocfs2_cluster_group_search;
  967. status = ocfs2_reserve_suballoc_bits(osb, ac,
  968. GLOBAL_BITMAP_SYSTEM_INODE,
  969. OCFS2_INVALID_SLOT, NULL,
  970. ALLOC_NEW_GROUP);
  971. if (status < 0 && status != -ENOSPC) {
  972. mlog_errno(status);
  973. goto bail;
  974. }
  975. bail:
  976. return status;
  977. }
  978. /* Callers don't need to care which bitmap (local alloc or main) to
  979. * use so we figure it out for them, but unfortunately this clutters
  980. * things a bit. */
  981. static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
  982. u32 bits_wanted, u64 max_block,
  983. int flags,
  984. struct ocfs2_alloc_context **ac)
  985. {
  986. int status;
  987. mlog_entry_void();
  988. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  989. if (!(*ac)) {
  990. status = -ENOMEM;
  991. mlog_errno(status);
  992. goto bail;
  993. }
  994. (*ac)->ac_bits_wanted = bits_wanted;
  995. (*ac)->ac_max_block = max_block;
  996. status = -ENOSPC;
  997. if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
  998. ocfs2_alloc_should_use_local(osb, bits_wanted)) {
  999. status = ocfs2_reserve_local_alloc_bits(osb,
  1000. bits_wanted,
  1001. *ac);
  1002. if ((status < 0) && (status != -ENOSPC)) {
  1003. mlog_errno(status);
  1004. goto bail;
  1005. }
  1006. }
  1007. if (status == -ENOSPC) {
  1008. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  1009. if (status < 0) {
  1010. if (status != -ENOSPC)
  1011. mlog_errno(status);
  1012. goto bail;
  1013. }
  1014. }
  1015. status = 0;
  1016. bail:
  1017. if ((status < 0) && *ac) {
  1018. ocfs2_free_alloc_context(*ac);
  1019. *ac = NULL;
  1020. }
  1021. mlog_exit(status);
  1022. return status;
  1023. }
  1024. int ocfs2_reserve_clusters(struct ocfs2_super *osb,
  1025. u32 bits_wanted,
  1026. struct ocfs2_alloc_context **ac)
  1027. {
  1028. return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
  1029. ALLOC_NEW_GROUP, ac);
  1030. }
  1031. /*
  1032. * More or less lifted from ext3. I'll leave their description below:
  1033. *
  1034. * "For ext3 allocations, we must not reuse any blocks which are
  1035. * allocated in the bitmap buffer's "last committed data" copy. This
  1036. * prevents deletes from freeing up the page for reuse until we have
  1037. * committed the delete transaction.
  1038. *
  1039. * If we didn't do this, then deleting something and reallocating it as
  1040. * data would allow the old block to be overwritten before the
  1041. * transaction committed (because we force data to disk before commit).
  1042. * This would lead to corruption if we crashed between overwriting the
  1043. * data and committing the delete.
  1044. *
  1045. * @@@ We may want to make this allocation behaviour conditional on
  1046. * data-writes at some point, and disable it for metadata allocations or
  1047. * sync-data inodes."
  1048. *
  1049. * Note: OCFS2 already does this differently for metadata vs data
  1050. * allocations, as those bitmaps are separate and undo access is never
  1051. * called on a metadata group descriptor.
  1052. */
  1053. static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  1054. int nr)
  1055. {
  1056. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  1057. int ret;
  1058. if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
  1059. return 0;
  1060. if (!buffer_jbd(bg_bh))
  1061. return 1;
  1062. jbd_lock_bh_state(bg_bh);
  1063. bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
  1064. if (bg)
  1065. ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
  1066. else
  1067. ret = 1;
  1068. jbd_unlock_bh_state(bg_bh);
  1069. return ret;
  1070. }
  1071. static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
  1072. struct buffer_head *bg_bh,
  1073. unsigned int bits_wanted,
  1074. unsigned int total_bits,
  1075. struct ocfs2_suballoc_result *res)
  1076. {
  1077. void *bitmap;
  1078. u16 best_offset, best_size;
  1079. int offset, start, found, status = 0;
  1080. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  1081. /* Callers got this descriptor from
  1082. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  1083. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  1084. found = start = best_offset = best_size = 0;
  1085. bitmap = bg->bg_bitmap;
  1086. while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
  1087. if (offset == total_bits)
  1088. break;
  1089. if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
  1090. /* We found a zero, but we can't use it as it
  1091. * hasn't been put to disk yet! */
  1092. found = 0;
  1093. start = offset + 1;
  1094. } else if (offset == start) {
  1095. /* we found a zero */
  1096. found++;
  1097. /* move start to the next bit to test */
  1098. start++;
  1099. } else {
  1100. /* got a zero after some ones */
  1101. found = 1;
  1102. start = offset + 1;
  1103. }
  1104. if (found > best_size) {
  1105. best_size = found;
  1106. best_offset = start - found;
  1107. }
  1108. /* we got everything we needed */
  1109. if (found == bits_wanted) {
  1110. /* mlog(0, "Found it all!\n"); */
  1111. break;
  1112. }
  1113. }
  1114. if (best_size) {
  1115. res->sr_bit_offset = best_offset;
  1116. res->sr_bits = best_size;
  1117. } else {
  1118. status = -ENOSPC;
  1119. /* No error log here -- see the comment above
  1120. * ocfs2_test_bg_bit_allocatable */
  1121. }
  1122. return status;
  1123. }
  1124. static inline int ocfs2_block_group_set_bits(handle_t *handle,
  1125. struct inode *alloc_inode,
  1126. struct ocfs2_group_desc *bg,
  1127. struct buffer_head *group_bh,
  1128. unsigned int bit_off,
  1129. unsigned int num_bits)
  1130. {
  1131. int status;
  1132. void *bitmap = bg->bg_bitmap;
  1133. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  1134. mlog_entry_void();
  1135. /* All callers get the descriptor via
  1136. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  1137. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  1138. BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
  1139. mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
  1140. num_bits);
  1141. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1142. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  1143. status = ocfs2_journal_access_gd(handle,
  1144. INODE_CACHE(alloc_inode),
  1145. group_bh,
  1146. journal_type);
  1147. if (status < 0) {
  1148. mlog_errno(status);
  1149. goto bail;
  1150. }
  1151. le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
  1152. while(num_bits--)
  1153. ocfs2_set_bit(bit_off++, bitmap);
  1154. ocfs2_journal_dirty(handle, group_bh);
  1155. bail:
  1156. mlog_exit(status);
  1157. return status;
  1158. }
  1159. /* find the one with the most empty bits */
  1160. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
  1161. {
  1162. u16 curr, best;
  1163. BUG_ON(!cl->cl_next_free_rec);
  1164. best = curr = 0;
  1165. while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
  1166. if (le32_to_cpu(cl->cl_recs[curr].c_free) >
  1167. le32_to_cpu(cl->cl_recs[best].c_free))
  1168. best = curr;
  1169. curr++;
  1170. }
  1171. BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
  1172. return best;
  1173. }
  1174. static int ocfs2_relink_block_group(handle_t *handle,
  1175. struct inode *alloc_inode,
  1176. struct buffer_head *fe_bh,
  1177. struct buffer_head *bg_bh,
  1178. struct buffer_head *prev_bg_bh,
  1179. u16 chain)
  1180. {
  1181. int status;
  1182. /* there is a really tiny chance the journal calls could fail,
  1183. * but we wouldn't want inconsistent blocks in *any* case. */
  1184. u64 fe_ptr, bg_ptr, prev_bg_ptr;
  1185. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  1186. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  1187. struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
  1188. /* The caller got these descriptors from
  1189. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  1190. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  1191. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
  1192. mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
  1193. (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
  1194. (unsigned long long)le64_to_cpu(bg->bg_blkno),
  1195. (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
  1196. fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
  1197. bg_ptr = le64_to_cpu(bg->bg_next_group);
  1198. prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
  1199. status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  1200. prev_bg_bh,
  1201. OCFS2_JOURNAL_ACCESS_WRITE);
  1202. if (status < 0) {
  1203. mlog_errno(status);
  1204. goto out_rollback;
  1205. }
  1206. prev_bg->bg_next_group = bg->bg_next_group;
  1207. ocfs2_journal_dirty(handle, prev_bg_bh);
  1208. status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  1209. bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1210. if (status < 0) {
  1211. mlog_errno(status);
  1212. goto out_rollback;
  1213. }
  1214. bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
  1215. ocfs2_journal_dirty(handle, bg_bh);
  1216. status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  1217. fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1218. if (status < 0) {
  1219. mlog_errno(status);
  1220. goto out_rollback;
  1221. }
  1222. fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
  1223. ocfs2_journal_dirty(handle, fe_bh);
  1224. out_rollback:
  1225. if (status < 0) {
  1226. fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
  1227. bg->bg_next_group = cpu_to_le64(bg_ptr);
  1228. prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
  1229. }
  1230. mlog_exit(status);
  1231. return status;
  1232. }
  1233. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  1234. u32 wanted)
  1235. {
  1236. return le16_to_cpu(bg->bg_free_bits_count) > wanted;
  1237. }
  1238. /* return 0 on success, -ENOSPC to keep searching and any other < 0
  1239. * value on error. */
  1240. static int ocfs2_cluster_group_search(struct inode *inode,
  1241. struct buffer_head *group_bh,
  1242. u32 bits_wanted, u32 min_bits,
  1243. u64 max_block,
  1244. struct ocfs2_suballoc_result *res)
  1245. {
  1246. int search = -ENOSPC;
  1247. int ret;
  1248. u64 blkoff;
  1249. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
  1250. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1251. unsigned int max_bits, gd_cluster_off;
  1252. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1253. if (gd->bg_free_bits_count) {
  1254. max_bits = le16_to_cpu(gd->bg_bits);
  1255. /* Tail groups in cluster bitmaps which aren't cpg
  1256. * aligned are prone to partial extention by a failed
  1257. * fs resize. If the file system resize never got to
  1258. * update the dinode cluster count, then we don't want
  1259. * to trust any clusters past it, regardless of what
  1260. * the group descriptor says. */
  1261. gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
  1262. le64_to_cpu(gd->bg_blkno));
  1263. if ((gd_cluster_off + max_bits) >
  1264. OCFS2_I(inode)->ip_clusters) {
  1265. max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
  1266. mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
  1267. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  1268. le16_to_cpu(gd->bg_bits),
  1269. OCFS2_I(inode)->ip_clusters, max_bits);
  1270. }
  1271. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  1272. group_bh, bits_wanted,
  1273. max_bits, res);
  1274. if (ret)
  1275. return ret;
  1276. if (max_block) {
  1277. blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
  1278. gd_cluster_off +
  1279. res->sr_bit_offset +
  1280. res->sr_bits);
  1281. mlog(0, "Checking %llu against %llu\n",
  1282. (unsigned long long)blkoff,
  1283. (unsigned long long)max_block);
  1284. if (blkoff > max_block)
  1285. return -ENOSPC;
  1286. }
  1287. /* ocfs2_block_group_find_clear_bits() might
  1288. * return success, but we still want to return
  1289. * -ENOSPC unless it found the minimum number
  1290. * of bits. */
  1291. if (min_bits <= res->sr_bits)
  1292. search = 0; /* success */
  1293. else if (res->sr_bits) {
  1294. /*
  1295. * Don't show bits which we'll be returning
  1296. * for allocation to the local alloc bitmap.
  1297. */
  1298. ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
  1299. }
  1300. }
  1301. return search;
  1302. }
  1303. static int ocfs2_block_group_search(struct inode *inode,
  1304. struct buffer_head *group_bh,
  1305. u32 bits_wanted, u32 min_bits,
  1306. u64 max_block,
  1307. struct ocfs2_suballoc_result *res)
  1308. {
  1309. int ret = -ENOSPC;
  1310. u64 blkoff;
  1311. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1312. BUG_ON(min_bits != 1);
  1313. BUG_ON(ocfs2_is_cluster_bitmap(inode));
  1314. if (bg->bg_free_bits_count) {
  1315. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  1316. group_bh, bits_wanted,
  1317. le16_to_cpu(bg->bg_bits),
  1318. res);
  1319. if (!ret && max_block) {
  1320. blkoff = le64_to_cpu(bg->bg_blkno) +
  1321. res->sr_bit_offset + res->sr_bits;
  1322. mlog(0, "Checking %llu against %llu\n",
  1323. (unsigned long long)blkoff,
  1324. (unsigned long long)max_block);
  1325. if (blkoff > max_block)
  1326. ret = -ENOSPC;
  1327. }
  1328. }
  1329. return ret;
  1330. }
  1331. static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
  1332. handle_t *handle,
  1333. struct buffer_head *di_bh,
  1334. u32 num_bits,
  1335. u16 chain)
  1336. {
  1337. int ret;
  1338. u32 tmp_used;
  1339. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  1340. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
  1341. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1342. OCFS2_JOURNAL_ACCESS_WRITE);
  1343. if (ret < 0) {
  1344. mlog_errno(ret);
  1345. goto out;
  1346. }
  1347. tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
  1348. di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
  1349. le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
  1350. ocfs2_journal_dirty(handle, di_bh);
  1351. out:
  1352. return ret;
  1353. }
  1354. static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
  1355. struct ocfs2_extent_rec *rec,
  1356. struct ocfs2_chain_list *cl)
  1357. {
  1358. unsigned int bpc = le16_to_cpu(cl->cl_bpc);
  1359. unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
  1360. unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
  1361. if (res->sr_bit_offset < bitoff)
  1362. return 0;
  1363. if (res->sr_bit_offset >= (bitoff + bitcount))
  1364. return 0;
  1365. res->sr_blkno = le64_to_cpu(rec->e_blkno) +
  1366. (res->sr_bit_offset - bitoff);
  1367. if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
  1368. res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
  1369. return 1;
  1370. }
  1371. static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
  1372. struct ocfs2_group_desc *bg,
  1373. struct ocfs2_suballoc_result *res)
  1374. {
  1375. int i;
  1376. u64 bg_blkno = res->sr_bg_blkno; /* Save off */
  1377. struct ocfs2_extent_rec *rec;
  1378. struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
  1379. struct ocfs2_chain_list *cl = &di->id2.i_chain;
  1380. if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
  1381. res->sr_blkno = 0;
  1382. return;
  1383. }
  1384. res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
  1385. res->sr_bg_blkno = 0; /* Clear it for contig block groups */
  1386. if (!ocfs2_supports_discontig_bh(OCFS2_SB(ac->ac_inode->i_sb)) ||
  1387. !bg->bg_list.l_next_free_rec)
  1388. return;
  1389. for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
  1390. rec = &bg->bg_list.l_recs[i];
  1391. if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
  1392. res->sr_bg_blkno = bg_blkno; /* Restore */
  1393. break;
  1394. }
  1395. }
  1396. }
  1397. static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
  1398. handle_t *handle,
  1399. u32 bits_wanted,
  1400. u32 min_bits,
  1401. struct ocfs2_suballoc_result *res,
  1402. u16 *bits_left)
  1403. {
  1404. int ret;
  1405. struct buffer_head *group_bh = NULL;
  1406. struct ocfs2_group_desc *gd;
  1407. struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
  1408. struct inode *alloc_inode = ac->ac_inode;
  1409. ret = ocfs2_read_group_descriptor(alloc_inode, di,
  1410. res->sr_bg_blkno, &group_bh);
  1411. if (ret < 0) {
  1412. mlog_errno(ret);
  1413. return ret;
  1414. }
  1415. gd = (struct ocfs2_group_desc *) group_bh->b_data;
  1416. ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
  1417. ac->ac_max_block, res);
  1418. if (ret < 0) {
  1419. if (ret != -ENOSPC)
  1420. mlog_errno(ret);
  1421. goto out;
  1422. }
  1423. if (!ret)
  1424. ocfs2_bg_discontig_fix_result(ac, gd, res);
  1425. ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
  1426. res->sr_bits,
  1427. le16_to_cpu(gd->bg_chain));
  1428. if (ret < 0) {
  1429. mlog_errno(ret);
  1430. goto out;
  1431. }
  1432. ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
  1433. res->sr_bit_offset, res->sr_bits);
  1434. if (ret < 0)
  1435. mlog_errno(ret);
  1436. *bits_left = le16_to_cpu(gd->bg_free_bits_count);
  1437. out:
  1438. brelse(group_bh);
  1439. return ret;
  1440. }
  1441. static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
  1442. handle_t *handle,
  1443. u32 bits_wanted,
  1444. u32 min_bits,
  1445. struct ocfs2_suballoc_result *res,
  1446. u16 *bits_left)
  1447. {
  1448. int status;
  1449. u16 chain;
  1450. u32 tmp_used;
  1451. u64 next_group;
  1452. struct inode *alloc_inode = ac->ac_inode;
  1453. struct buffer_head *group_bh = NULL;
  1454. struct buffer_head *prev_group_bh = NULL;
  1455. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1456. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  1457. struct ocfs2_group_desc *bg;
  1458. chain = ac->ac_chain;
  1459. mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
  1460. bits_wanted, chain,
  1461. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
  1462. status = ocfs2_read_group_descriptor(alloc_inode, fe,
  1463. le64_to_cpu(cl->cl_recs[chain].c_blkno),
  1464. &group_bh);
  1465. if (status < 0) {
  1466. mlog_errno(status);
  1467. goto bail;
  1468. }
  1469. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1470. status = -ENOSPC;
  1471. /* for now, the chain search is a bit simplistic. We just use
  1472. * the 1st group with any empty bits. */
  1473. while ((status = ac->ac_group_search(alloc_inode, group_bh,
  1474. bits_wanted, min_bits,
  1475. ac->ac_max_block,
  1476. res)) == -ENOSPC) {
  1477. if (!bg->bg_next_group)
  1478. break;
  1479. brelse(prev_group_bh);
  1480. prev_group_bh = NULL;
  1481. next_group = le64_to_cpu(bg->bg_next_group);
  1482. prev_group_bh = group_bh;
  1483. group_bh = NULL;
  1484. status = ocfs2_read_group_descriptor(alloc_inode, fe,
  1485. next_group, &group_bh);
  1486. if (status < 0) {
  1487. mlog_errno(status);
  1488. goto bail;
  1489. }
  1490. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1491. }
  1492. if (status < 0) {
  1493. if (status != -ENOSPC)
  1494. mlog_errno(status);
  1495. goto bail;
  1496. }
  1497. mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
  1498. res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
  1499. res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
  1500. BUG_ON(res->sr_bits == 0);
  1501. if (!status)
  1502. ocfs2_bg_discontig_fix_result(ac, bg, res);
  1503. /*
  1504. * Keep track of previous block descriptor read. When
  1505. * we find a target, if we have read more than X
  1506. * number of descriptors, and the target is reasonably
  1507. * empty, relink him to top of his chain.
  1508. *
  1509. * We've read 0 extra blocks and only send one more to
  1510. * the transaction, yet the next guy to search has a
  1511. * much easier time.
  1512. *
  1513. * Do this *after* figuring out how many bits we're taking out
  1514. * of our target group.
  1515. */
  1516. if (ac->ac_allow_chain_relink &&
  1517. (prev_group_bh) &&
  1518. (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
  1519. status = ocfs2_relink_block_group(handle, alloc_inode,
  1520. ac->ac_bh, group_bh,
  1521. prev_group_bh, chain);
  1522. if (status < 0) {
  1523. mlog_errno(status);
  1524. goto bail;
  1525. }
  1526. }
  1527. /* Ok, claim our bits now: set the info on dinode, chainlist
  1528. * and then the group */
  1529. status = ocfs2_journal_access_di(handle,
  1530. INODE_CACHE(alloc_inode),
  1531. ac->ac_bh,
  1532. OCFS2_JOURNAL_ACCESS_WRITE);
  1533. if (status < 0) {
  1534. mlog_errno(status);
  1535. goto bail;
  1536. }
  1537. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  1538. fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
  1539. le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
  1540. ocfs2_journal_dirty(handle, ac->ac_bh);
  1541. status = ocfs2_block_group_set_bits(handle,
  1542. alloc_inode,
  1543. bg,
  1544. group_bh,
  1545. res->sr_bit_offset,
  1546. res->sr_bits);
  1547. if (status < 0) {
  1548. mlog_errno(status);
  1549. goto bail;
  1550. }
  1551. mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
  1552. (unsigned long long)le64_to_cpu(fe->i_blkno));
  1553. *bits_left = le16_to_cpu(bg->bg_free_bits_count);
  1554. bail:
  1555. brelse(group_bh);
  1556. brelse(prev_group_bh);
  1557. mlog_exit(status);
  1558. return status;
  1559. }
  1560. /* will give out up to bits_wanted contiguous bits. */
  1561. static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
  1562. handle_t *handle,
  1563. u32 bits_wanted,
  1564. u32 min_bits,
  1565. struct ocfs2_suballoc_result *res)
  1566. {
  1567. int status;
  1568. u16 victim, i;
  1569. u16 bits_left = 0;
  1570. struct ocfs2_chain_list *cl;
  1571. struct ocfs2_dinode *fe;
  1572. mlog_entry_void();
  1573. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1574. BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
  1575. BUG_ON(!ac->ac_bh);
  1576. fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1577. /* The bh was validated by the inode read during
  1578. * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
  1579. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  1580. if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
  1581. le32_to_cpu(fe->id1.bitmap1.i_total)) {
  1582. ocfs2_error(ac->ac_inode->i_sb,
  1583. "Chain allocator dinode %llu has %u used "
  1584. "bits but only %u total.",
  1585. (unsigned long long)le64_to_cpu(fe->i_blkno),
  1586. le32_to_cpu(fe->id1.bitmap1.i_used),
  1587. le32_to_cpu(fe->id1.bitmap1.i_total));
  1588. status = -EIO;
  1589. goto bail;
  1590. }
  1591. res->sr_bg_blkno = ac->ac_last_group;
  1592. if (res->sr_bg_blkno) {
  1593. /* Attempt to short-circuit the usual search mechanism
  1594. * by jumping straight to the most recently used
  1595. * allocation group. This helps us mantain some
  1596. * contiguousness across allocations. */
  1597. status = ocfs2_search_one_group(ac, handle, bits_wanted,
  1598. min_bits, res, &bits_left);
  1599. if (!status)
  1600. goto set_hint;
  1601. if (status < 0 && status != -ENOSPC) {
  1602. mlog_errno(status);
  1603. goto bail;
  1604. }
  1605. }
  1606. cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  1607. victim = ocfs2_find_victim_chain(cl);
  1608. ac->ac_chain = victim;
  1609. ac->ac_allow_chain_relink = 1;
  1610. status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
  1611. res, &bits_left);
  1612. if (!status)
  1613. goto set_hint;
  1614. if (status < 0 && status != -ENOSPC) {
  1615. mlog_errno(status);
  1616. goto bail;
  1617. }
  1618. mlog(0, "Search of victim chain %u came up with nothing, "
  1619. "trying all chains now.\n", victim);
  1620. /* If we didn't pick a good victim, then just default to
  1621. * searching each chain in order. Don't allow chain relinking
  1622. * because we only calculate enough journal credits for one
  1623. * relink per alloc. */
  1624. ac->ac_allow_chain_relink = 0;
  1625. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
  1626. if (i == victim)
  1627. continue;
  1628. if (!cl->cl_recs[i].c_free)
  1629. continue;
  1630. ac->ac_chain = i;
  1631. status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
  1632. res, &bits_left);
  1633. if (!status)
  1634. break;
  1635. if (status < 0 && status != -ENOSPC) {
  1636. mlog_errno(status);
  1637. goto bail;
  1638. }
  1639. }
  1640. set_hint:
  1641. if (status != -ENOSPC) {
  1642. /* If the next search of this group is not likely to
  1643. * yield a suitable extent, then we reset the last
  1644. * group hint so as to not waste a disk read */
  1645. if (bits_left < min_bits)
  1646. ac->ac_last_group = 0;
  1647. else
  1648. ac->ac_last_group = res->sr_bg_blkno;
  1649. }
  1650. bail:
  1651. mlog_exit(status);
  1652. return status;
  1653. }
  1654. int ocfs2_claim_metadata(handle_t *handle,
  1655. struct ocfs2_alloc_context *ac,
  1656. u32 bits_wanted,
  1657. u64 *suballoc_loc,
  1658. u16 *suballoc_bit_start,
  1659. unsigned int *num_bits,
  1660. u64 *blkno_start)
  1661. {
  1662. int status;
  1663. struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
  1664. BUG_ON(!ac);
  1665. BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
  1666. BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
  1667. status = ocfs2_claim_suballoc_bits(ac,
  1668. handle,
  1669. bits_wanted,
  1670. 1,
  1671. &res);
  1672. if (status < 0) {
  1673. mlog_errno(status);
  1674. goto bail;
  1675. }
  1676. atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
  1677. *suballoc_loc = res.sr_bg_blkno;
  1678. *suballoc_bit_start = res.sr_bit_offset;
  1679. *blkno_start = res.sr_blkno;
  1680. ac->ac_bits_given += res.sr_bits;
  1681. *num_bits = res.sr_bits;
  1682. status = 0;
  1683. bail:
  1684. mlog_exit(status);
  1685. return status;
  1686. }
  1687. static void ocfs2_init_inode_ac_group(struct inode *dir,
  1688. struct buffer_head *parent_fe_bh,
  1689. struct ocfs2_alloc_context *ac)
  1690. {
  1691. struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  1692. /*
  1693. * Try to allocate inodes from some specific group.
  1694. *
  1695. * If the parent dir has recorded the last group used in allocation,
  1696. * cool, use it. Otherwise if we try to allocate new inode from the
  1697. * same slot the parent dir belongs to, use the same chunk.
  1698. *
  1699. * We are very careful here to avoid the mistake of setting
  1700. * ac_last_group to a group descriptor from a different (unlocked) slot.
  1701. */
  1702. if (OCFS2_I(dir)->ip_last_used_group &&
  1703. OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
  1704. ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
  1705. else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
  1706. ac->ac_last_group = ocfs2_which_suballoc_group(
  1707. le64_to_cpu(fe->i_blkno),
  1708. le16_to_cpu(fe->i_suballoc_bit));
  1709. }
  1710. static inline void ocfs2_save_inode_ac_group(struct inode *dir,
  1711. struct ocfs2_alloc_context *ac)
  1712. {
  1713. OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
  1714. OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
  1715. }
  1716. int ocfs2_claim_new_inode(handle_t *handle,
  1717. struct inode *dir,
  1718. struct buffer_head *parent_fe_bh,
  1719. struct ocfs2_alloc_context *ac,
  1720. u64 *suballoc_loc,
  1721. u16 *suballoc_bit,
  1722. u64 *fe_blkno)
  1723. {
  1724. int status;
  1725. struct ocfs2_suballoc_result res;
  1726. mlog_entry_void();
  1727. BUG_ON(!ac);
  1728. BUG_ON(ac->ac_bits_given != 0);
  1729. BUG_ON(ac->ac_bits_wanted != 1);
  1730. BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
  1731. ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
  1732. status = ocfs2_claim_suballoc_bits(ac,
  1733. handle,
  1734. 1,
  1735. 1,
  1736. &res);
  1737. if (status < 0) {
  1738. mlog_errno(status);
  1739. goto bail;
  1740. }
  1741. atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
  1742. BUG_ON(res.sr_bits != 1);
  1743. *suballoc_loc = res.sr_bg_blkno;
  1744. *suballoc_bit = res.sr_bit_offset;
  1745. *fe_blkno = res.sr_blkno;
  1746. ac->ac_bits_given++;
  1747. ocfs2_save_inode_ac_group(dir, ac);
  1748. status = 0;
  1749. bail:
  1750. mlog_exit(status);
  1751. return status;
  1752. }
  1753. /* translate a group desc. blkno and it's bitmap offset into
  1754. * disk cluster offset. */
  1755. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  1756. u64 bg_blkno,
  1757. u16 bg_bit_off)
  1758. {
  1759. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1760. u32 cluster = 0;
  1761. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1762. if (bg_blkno != osb->first_cluster_group_blkno)
  1763. cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
  1764. cluster += (u32) bg_bit_off;
  1765. return cluster;
  1766. }
  1767. /* given a cluster offset, calculate which block group it belongs to
  1768. * and return that block offset. */
  1769. u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
  1770. {
  1771. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1772. u32 group_no;
  1773. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1774. group_no = cluster / osb->bitmap_cpg;
  1775. if (!group_no)
  1776. return osb->first_cluster_group_blkno;
  1777. return ocfs2_clusters_to_blocks(inode->i_sb,
  1778. group_no * osb->bitmap_cpg);
  1779. }
  1780. /* given the block number of a cluster start, calculate which cluster
  1781. * group and descriptor bitmap offset that corresponds to. */
  1782. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  1783. u64 data_blkno,
  1784. u64 *bg_blkno,
  1785. u16 *bg_bit_off)
  1786. {
  1787. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1788. u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
  1789. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1790. *bg_blkno = ocfs2_which_cluster_group(inode,
  1791. data_cluster);
  1792. if (*bg_blkno == osb->first_cluster_group_blkno)
  1793. *bg_bit_off = (u16) data_cluster;
  1794. else
  1795. *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
  1796. data_blkno - *bg_blkno);
  1797. }
  1798. /*
  1799. * min_bits - minimum contiguous chunk from this total allocation we
  1800. * can handle. set to what we asked for originally for a full
  1801. * contig. allocation, set to '1' to indicate we can deal with extents
  1802. * of any size.
  1803. */
  1804. int __ocfs2_claim_clusters(handle_t *handle,
  1805. struct ocfs2_alloc_context *ac,
  1806. u32 min_clusters,
  1807. u32 max_clusters,
  1808. u32 *cluster_start,
  1809. u32 *num_clusters)
  1810. {
  1811. int status;
  1812. unsigned int bits_wanted = max_clusters;
  1813. struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
  1814. struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
  1815. mlog_entry_void();
  1816. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1817. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
  1818. && ac->ac_which != OCFS2_AC_USE_MAIN);
  1819. if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
  1820. WARN_ON(min_clusters > 1);
  1821. status = ocfs2_claim_local_alloc_bits(osb,
  1822. handle,
  1823. ac,
  1824. bits_wanted,
  1825. cluster_start,
  1826. num_clusters);
  1827. if (!status)
  1828. atomic_inc(&osb->alloc_stats.local_data);
  1829. } else {
  1830. if (min_clusters > (osb->bitmap_cpg - 1)) {
  1831. /* The only paths asking for contiguousness
  1832. * should know about this already. */
  1833. mlog(ML_ERROR, "minimum allocation requested %u exceeds "
  1834. "group bitmap size %u!\n", min_clusters,
  1835. osb->bitmap_cpg);
  1836. status = -ENOSPC;
  1837. goto bail;
  1838. }
  1839. /* clamp the current request down to a realistic size. */
  1840. if (bits_wanted > (osb->bitmap_cpg - 1))
  1841. bits_wanted = osb->bitmap_cpg - 1;
  1842. status = ocfs2_claim_suballoc_bits(ac,
  1843. handle,
  1844. bits_wanted,
  1845. min_clusters,
  1846. &res);
  1847. if (!status) {
  1848. BUG_ON(res.sr_blkno); /* cluster alloc can't set */
  1849. *cluster_start =
  1850. ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
  1851. res.sr_bg_blkno,
  1852. res.sr_bit_offset);
  1853. atomic_inc(&osb->alloc_stats.bitmap_data);
  1854. }
  1855. }
  1856. if (status < 0) {
  1857. if (status != -ENOSPC)
  1858. mlog_errno(status);
  1859. goto bail;
  1860. }
  1861. ac->ac_bits_given += res.sr_bits;
  1862. *num_clusters = res.sr_bits;
  1863. bail:
  1864. mlog_exit(status);
  1865. return status;
  1866. }
  1867. int ocfs2_claim_clusters(handle_t *handle,
  1868. struct ocfs2_alloc_context *ac,
  1869. u32 min_clusters,
  1870. u32 *cluster_start,
  1871. u32 *num_clusters)
  1872. {
  1873. unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
  1874. return __ocfs2_claim_clusters(handle, ac, min_clusters,
  1875. bits_wanted, cluster_start, num_clusters);
  1876. }
  1877. static int ocfs2_block_group_clear_bits(handle_t *handle,
  1878. struct inode *alloc_inode,
  1879. struct ocfs2_group_desc *bg,
  1880. struct buffer_head *group_bh,
  1881. unsigned int bit_off,
  1882. unsigned int num_bits,
  1883. void (*undo_fn)(unsigned int bit,
  1884. unsigned long *bmap))
  1885. {
  1886. int status;
  1887. unsigned int tmp;
  1888. struct ocfs2_group_desc *undo_bg = NULL;
  1889. mlog_entry_void();
  1890. /* The caller got this descriptor from
  1891. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  1892. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  1893. mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
  1894. BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
  1895. status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  1896. group_bh,
  1897. undo_fn ?
  1898. OCFS2_JOURNAL_ACCESS_UNDO :
  1899. OCFS2_JOURNAL_ACCESS_WRITE);
  1900. if (status < 0) {
  1901. mlog_errno(status);
  1902. goto bail;
  1903. }
  1904. if (undo_fn) {
  1905. jbd_lock_bh_state(group_bh);
  1906. undo_bg = (struct ocfs2_group_desc *)
  1907. bh2jh(group_bh)->b_committed_data;
  1908. BUG_ON(!undo_bg);
  1909. }
  1910. tmp = num_bits;
  1911. while(tmp--) {
  1912. ocfs2_clear_bit((bit_off + tmp),
  1913. (unsigned long *) bg->bg_bitmap);
  1914. if (undo_fn)
  1915. undo_fn(bit_off + tmp,
  1916. (unsigned long *) undo_bg->bg_bitmap);
  1917. }
  1918. le16_add_cpu(&bg->bg_free_bits_count, num_bits);
  1919. if (undo_fn)
  1920. jbd_unlock_bh_state(group_bh);
  1921. ocfs2_journal_dirty(handle, group_bh);
  1922. bail:
  1923. return status;
  1924. }
  1925. /*
  1926. * expects the suballoc inode to already be locked.
  1927. */
  1928. static int _ocfs2_free_suballoc_bits(handle_t *handle,
  1929. struct inode *alloc_inode,
  1930. struct buffer_head *alloc_bh,
  1931. unsigned int start_bit,
  1932. u64 bg_blkno,
  1933. unsigned int count,
  1934. void (*undo_fn)(unsigned int bit,
  1935. unsigned long *bitmap))
  1936. {
  1937. int status = 0;
  1938. u32 tmp_used;
  1939. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
  1940. struct ocfs2_chain_list *cl = &fe->id2.i_chain;
  1941. struct buffer_head *group_bh = NULL;
  1942. struct ocfs2_group_desc *group;
  1943. mlog_entry_void();
  1944. /* The alloc_bh comes from ocfs2_free_dinode() or
  1945. * ocfs2_free_clusters(). The callers have all locked the
  1946. * allocator and gotten alloc_bh from the lock call. This
  1947. * validates the dinode buffer. Any corruption that has happended
  1948. * is a code bug. */
  1949. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  1950. BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
  1951. mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
  1952. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
  1953. (unsigned long long)bg_blkno, start_bit);
  1954. status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
  1955. &group_bh);
  1956. if (status < 0) {
  1957. mlog_errno(status);
  1958. goto bail;
  1959. }
  1960. group = (struct ocfs2_group_desc *) group_bh->b_data;
  1961. BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
  1962. status = ocfs2_block_group_clear_bits(handle, alloc_inode,
  1963. group, group_bh,
  1964. start_bit, count, undo_fn);
  1965. if (status < 0) {
  1966. mlog_errno(status);
  1967. goto bail;
  1968. }
  1969. status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  1970. alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1971. if (status < 0) {
  1972. mlog_errno(status);
  1973. goto bail;
  1974. }
  1975. le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
  1976. count);
  1977. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  1978. fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
  1979. ocfs2_journal_dirty(handle, alloc_bh);
  1980. bail:
  1981. brelse(group_bh);
  1982. mlog_exit(status);
  1983. return status;
  1984. }
  1985. int ocfs2_free_suballoc_bits(handle_t *handle,
  1986. struct inode *alloc_inode,
  1987. struct buffer_head *alloc_bh,
  1988. unsigned int start_bit,
  1989. u64 bg_blkno,
  1990. unsigned int count)
  1991. {
  1992. return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
  1993. start_bit, bg_blkno, count, NULL);
  1994. }
  1995. int ocfs2_free_dinode(handle_t *handle,
  1996. struct inode *inode_alloc_inode,
  1997. struct buffer_head *inode_alloc_bh,
  1998. struct ocfs2_dinode *di)
  1999. {
  2000. u64 blk = le64_to_cpu(di->i_blkno);
  2001. u16 bit = le16_to_cpu(di->i_suballoc_bit);
  2002. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  2003. return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
  2004. inode_alloc_bh, bit, bg_blkno, 1);
  2005. }
  2006. static int _ocfs2_free_clusters(handle_t *handle,
  2007. struct inode *bitmap_inode,
  2008. struct buffer_head *bitmap_bh,
  2009. u64 start_blk,
  2010. unsigned int num_clusters,
  2011. void (*undo_fn)(unsigned int bit,
  2012. unsigned long *bitmap))
  2013. {
  2014. int status;
  2015. u16 bg_start_bit;
  2016. u64 bg_blkno;
  2017. struct ocfs2_dinode *fe;
  2018. /* You can't ever have a contiguous set of clusters
  2019. * bigger than a block group bitmap so we never have to worry
  2020. * about looping on them. */
  2021. mlog_entry_void();
  2022. /* This is expensive. We can safely remove once this stuff has
  2023. * gotten tested really well. */
  2024. BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
  2025. fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
  2026. ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
  2027. &bg_start_bit);
  2028. mlog(0, "want to free %u clusters starting at block %llu\n",
  2029. num_clusters, (unsigned long long)start_blk);
  2030. mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
  2031. (unsigned long long)bg_blkno, bg_start_bit);
  2032. status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
  2033. bg_start_bit, bg_blkno,
  2034. num_clusters, undo_fn);
  2035. if (status < 0) {
  2036. mlog_errno(status);
  2037. goto out;
  2038. }
  2039. ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
  2040. num_clusters);
  2041. out:
  2042. mlog_exit(status);
  2043. return status;
  2044. }
  2045. int ocfs2_free_clusters(handle_t *handle,
  2046. struct inode *bitmap_inode,
  2047. struct buffer_head *bitmap_bh,
  2048. u64 start_blk,
  2049. unsigned int num_clusters)
  2050. {
  2051. return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
  2052. start_blk, num_clusters,
  2053. _ocfs2_set_bit);
  2054. }
  2055. /*
  2056. * Give never-used clusters back to the global bitmap. We don't need
  2057. * to protect these bits in the undo buffer.
  2058. */
  2059. int ocfs2_release_clusters(handle_t *handle,
  2060. struct inode *bitmap_inode,
  2061. struct buffer_head *bitmap_bh,
  2062. u64 start_blk,
  2063. unsigned int num_clusters)
  2064. {
  2065. return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
  2066. start_blk, num_clusters,
  2067. _ocfs2_clear_bit);
  2068. }
  2069. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
  2070. {
  2071. printk("Block Group:\n");
  2072. printk("bg_signature: %s\n", bg->bg_signature);
  2073. printk("bg_size: %u\n", bg->bg_size);
  2074. printk("bg_bits: %u\n", bg->bg_bits);
  2075. printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
  2076. printk("bg_chain: %u\n", bg->bg_chain);
  2077. printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
  2078. printk("bg_next_group: %llu\n",
  2079. (unsigned long long)bg->bg_next_group);
  2080. printk("bg_parent_dinode: %llu\n",
  2081. (unsigned long long)bg->bg_parent_dinode);
  2082. printk("bg_blkno: %llu\n",
  2083. (unsigned long long)bg->bg_blkno);
  2084. }
  2085. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
  2086. {
  2087. int i;
  2088. printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
  2089. printk("i_signature: %s\n", fe->i_signature);
  2090. printk("i_size: %llu\n",
  2091. (unsigned long long)fe->i_size);
  2092. printk("i_clusters: %u\n", fe->i_clusters);
  2093. printk("i_generation: %u\n",
  2094. le32_to_cpu(fe->i_generation));
  2095. printk("id1.bitmap1.i_used: %u\n",
  2096. le32_to_cpu(fe->id1.bitmap1.i_used));
  2097. printk("id1.bitmap1.i_total: %u\n",
  2098. le32_to_cpu(fe->id1.bitmap1.i_total));
  2099. printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
  2100. printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
  2101. printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
  2102. printk("id2.i_chain.cl_next_free_rec: %u\n",
  2103. fe->id2.i_chain.cl_next_free_rec);
  2104. for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
  2105. printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
  2106. fe->id2.i_chain.cl_recs[i].c_free);
  2107. printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
  2108. fe->id2.i_chain.cl_recs[i].c_total);
  2109. printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
  2110. (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
  2111. }
  2112. }
  2113. /*
  2114. * For a given allocation, determine which allocators will need to be
  2115. * accessed, and lock them, reserving the appropriate number of bits.
  2116. *
  2117. * Sparse file systems call this from ocfs2_write_begin_nolock()
  2118. * and ocfs2_allocate_unwritten_extents().
  2119. *
  2120. * File systems which don't support holes call this from
  2121. * ocfs2_extend_allocation().
  2122. */
  2123. int ocfs2_lock_allocators(struct inode *inode,
  2124. struct ocfs2_extent_tree *et,
  2125. u32 clusters_to_add, u32 extents_to_split,
  2126. struct ocfs2_alloc_context **data_ac,
  2127. struct ocfs2_alloc_context **meta_ac)
  2128. {
  2129. int ret = 0, num_free_extents;
  2130. unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
  2131. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2132. *meta_ac = NULL;
  2133. if (data_ac)
  2134. *data_ac = NULL;
  2135. BUG_ON(clusters_to_add != 0 && data_ac == NULL);
  2136. num_free_extents = ocfs2_num_free_extents(osb, et);
  2137. if (num_free_extents < 0) {
  2138. ret = num_free_extents;
  2139. mlog_errno(ret);
  2140. goto out;
  2141. }
  2142. /*
  2143. * Sparse allocation file systems need to be more conservative
  2144. * with reserving room for expansion - the actual allocation
  2145. * happens while we've got a journal handle open so re-taking
  2146. * a cluster lock (because we ran out of room for another
  2147. * extent) will violate ordering rules.
  2148. *
  2149. * Most of the time we'll only be seeing this 1 cluster at a time
  2150. * anyway.
  2151. *
  2152. * Always lock for any unwritten extents - we might want to
  2153. * add blocks during a split.
  2154. */
  2155. if (!num_free_extents ||
  2156. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
  2157. ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
  2158. if (ret < 0) {
  2159. if (ret != -ENOSPC)
  2160. mlog_errno(ret);
  2161. goto out;
  2162. }
  2163. }
  2164. if (clusters_to_add == 0)
  2165. goto out;
  2166. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  2167. if (ret < 0) {
  2168. if (ret != -ENOSPC)
  2169. mlog_errno(ret);
  2170. goto out;
  2171. }
  2172. out:
  2173. if (ret) {
  2174. if (*meta_ac) {
  2175. ocfs2_free_alloc_context(*meta_ac);
  2176. *meta_ac = NULL;
  2177. }
  2178. /*
  2179. * We cannot have an error and a non null *data_ac.
  2180. */
  2181. }
  2182. return ret;
  2183. }
  2184. /*
  2185. * Read the inode specified by blkno to get suballoc_slot and
  2186. * suballoc_bit.
  2187. */
  2188. static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
  2189. u16 *suballoc_slot, u16 *suballoc_bit)
  2190. {
  2191. int status;
  2192. struct buffer_head *inode_bh = NULL;
  2193. struct ocfs2_dinode *inode_fe;
  2194. mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
  2195. /* dirty read disk */
  2196. status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
  2197. if (status < 0) {
  2198. mlog(ML_ERROR, "read block %llu failed %d\n",
  2199. (unsigned long long)blkno, status);
  2200. goto bail;
  2201. }
  2202. inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
  2203. if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
  2204. mlog(ML_ERROR, "invalid inode %llu requested\n",
  2205. (unsigned long long)blkno);
  2206. status = -EINVAL;
  2207. goto bail;
  2208. }
  2209. if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
  2210. (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
  2211. mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
  2212. (unsigned long long)blkno,
  2213. (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
  2214. status = -EINVAL;
  2215. goto bail;
  2216. }
  2217. if (suballoc_slot)
  2218. *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
  2219. if (suballoc_bit)
  2220. *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
  2221. bail:
  2222. brelse(inode_bh);
  2223. mlog_exit(status);
  2224. return status;
  2225. }
  2226. /*
  2227. * test whether bit is SET in allocator bitmap or not. on success, 0
  2228. * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
  2229. * is returned and *res is meaningless. Call this after you have
  2230. * cluster locked against suballoc, or you may get a result based on
  2231. * non-up2date contents
  2232. */
  2233. static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
  2234. struct inode *suballoc,
  2235. struct buffer_head *alloc_bh, u64 blkno,
  2236. u16 bit, int *res)
  2237. {
  2238. struct ocfs2_dinode *alloc_fe;
  2239. struct ocfs2_group_desc *group;
  2240. struct buffer_head *group_bh = NULL;
  2241. u64 bg_blkno;
  2242. int status;
  2243. mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
  2244. (unsigned int)bit);
  2245. alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
  2246. if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
  2247. mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
  2248. (unsigned int)bit,
  2249. ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
  2250. status = -EINVAL;
  2251. goto bail;
  2252. }
  2253. bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
  2254. status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
  2255. &group_bh);
  2256. if (status < 0) {
  2257. mlog(ML_ERROR, "read group %llu failed %d\n",
  2258. (unsigned long long)bg_blkno, status);
  2259. goto bail;
  2260. }
  2261. group = (struct ocfs2_group_desc *) group_bh->b_data;
  2262. *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
  2263. bail:
  2264. brelse(group_bh);
  2265. mlog_exit(status);
  2266. return status;
  2267. }
  2268. /*
  2269. * Test if the bit representing this inode (blkno) is set in the
  2270. * suballocator.
  2271. *
  2272. * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
  2273. *
  2274. * In the event of failure, a negative value is returned and *res is
  2275. * meaningless.
  2276. *
  2277. * Callers must make sure to hold nfs_sync_lock to prevent
  2278. * ocfs2_delete_inode() on another node from accessing the same
  2279. * suballocator concurrently.
  2280. */
  2281. int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
  2282. {
  2283. int status;
  2284. u16 suballoc_bit = 0, suballoc_slot = 0;
  2285. struct inode *inode_alloc_inode;
  2286. struct buffer_head *alloc_bh = NULL;
  2287. mlog_entry("blkno: %llu", (unsigned long long)blkno);
  2288. status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
  2289. &suballoc_bit);
  2290. if (status < 0) {
  2291. mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
  2292. goto bail;
  2293. }
  2294. inode_alloc_inode =
  2295. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  2296. suballoc_slot);
  2297. if (!inode_alloc_inode) {
  2298. /* the error code could be inaccurate, but we are not able to
  2299. * get the correct one. */
  2300. status = -EINVAL;
  2301. mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
  2302. (u32)suballoc_slot);
  2303. goto bail;
  2304. }
  2305. mutex_lock(&inode_alloc_inode->i_mutex);
  2306. status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
  2307. if (status < 0) {
  2308. mutex_unlock(&inode_alloc_inode->i_mutex);
  2309. mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
  2310. (u32)suballoc_slot, status);
  2311. goto bail;
  2312. }
  2313. status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
  2314. blkno, suballoc_bit, res);
  2315. if (status < 0)
  2316. mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
  2317. ocfs2_inode_unlock(inode_alloc_inode, 0);
  2318. mutex_unlock(&inode_alloc_inode->i_mutex);
  2319. iput(inode_alloc_inode);
  2320. brelse(alloc_bh);
  2321. bail:
  2322. mlog_exit(status);
  2323. return status;
  2324. }