suballoc.c 76 KB

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