suballoc.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908
  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. #include <cluster/masklog.h>
  31. #include "ocfs2.h"
  32. #include "alloc.h"
  33. #include "blockcheck.h"
  34. #include "dlmglue.h"
  35. #include "inode.h"
  36. #include "journal.h"
  37. #include "localalloc.h"
  38. #include "suballoc.h"
  39. #include "super.h"
  40. #include "sysfile.h"
  41. #include "uptodate.h"
  42. #include "ocfs2_trace.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. trace_ocfs2_validate_group_descriptor(
  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, unsigned int 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_le16(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. if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
  341. ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
  342. "b_blocknr (%llu)",
  343. (unsigned long long)group_blkno,
  344. (unsigned long long) bg_bh->b_blocknr);
  345. status = -EIO;
  346. goto bail;
  347. }
  348. status = ocfs2_journal_access_gd(handle,
  349. INODE_CACHE(alloc_inode),
  350. bg_bh,
  351. OCFS2_JOURNAL_ACCESS_CREATE);
  352. if (status < 0) {
  353. mlog_errno(status);
  354. goto bail;
  355. }
  356. memset(bg, 0, sb->s_blocksize);
  357. strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
  358. bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  359. bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
  360. osb->s_feature_incompat));
  361. bg->bg_chain = cpu_to_le16(my_chain);
  362. bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
  363. bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
  364. bg->bg_blkno = cpu_to_le64(group_blkno);
  365. if (group_clusters == le16_to_cpu(cl->cl_cpg))
  366. bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
  367. else
  368. ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
  369. group_clusters);
  370. /* set the 1st bit in the bitmap to account for the descriptor block */
  371. ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
  372. bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
  373. ocfs2_journal_dirty(handle, bg_bh);
  374. /* There is no need to zero out or otherwise initialize the
  375. * other blocks in a group - All valid FS metadata in a block
  376. * group stores the superblock fs_generation value at
  377. * allocation time. */
  378. bail:
  379. if (status)
  380. mlog_errno(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. trace_ocfs2_block_group_alloc_contig(
  417. (unsigned long long)bg_blkno, alloc_rec);
  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. le16_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_disable_chain_relink = 1;
  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. trace_ocfs2_block_group_alloc_discontig(
  573. (unsigned long long)bg_blkno, alloc_rec);
  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. cl = &fe->id2.i_chain;
  616. status = ocfs2_reserve_clusters_with_limit(osb,
  617. le16_to_cpu(cl->cl_cpg),
  618. max_block, flags, &ac);
  619. if (status < 0) {
  620. if (status != -ENOSPC)
  621. mlog_errno(status);
  622. goto bail;
  623. }
  624. credits = ocfs2_calc_group_alloc_credits(osb->sb,
  625. le16_to_cpu(cl->cl_cpg));
  626. handle = ocfs2_start_trans(osb, credits);
  627. if (IS_ERR(handle)) {
  628. status = PTR_ERR(handle);
  629. handle = NULL;
  630. mlog_errno(status);
  631. goto bail;
  632. }
  633. if (last_alloc_group && *last_alloc_group != 0) {
  634. trace_ocfs2_block_group_alloc(
  635. (unsigned long long)*last_alloc_group);
  636. ac->ac_last_group = *last_alloc_group;
  637. }
  638. bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
  639. ac, cl);
  640. if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
  641. bg_bh = ocfs2_block_group_alloc_discontig(handle,
  642. alloc_inode,
  643. ac, cl);
  644. if (IS_ERR(bg_bh)) {
  645. status = PTR_ERR(bg_bh);
  646. bg_bh = NULL;
  647. if (status != -ENOSPC)
  648. mlog_errno(status);
  649. goto bail;
  650. }
  651. bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  652. status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  653. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  654. if (status < 0) {
  655. mlog_errno(status);
  656. goto bail;
  657. }
  658. alloc_rec = le16_to_cpu(bg->bg_chain);
  659. le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
  660. le16_to_cpu(bg->bg_free_bits_count));
  661. le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
  662. le16_to_cpu(bg->bg_bits));
  663. cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
  664. if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
  665. le16_add_cpu(&cl->cl_next_free_rec, 1);
  666. le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
  667. le16_to_cpu(bg->bg_free_bits_count));
  668. le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
  669. le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
  670. ocfs2_journal_dirty(handle, bh);
  671. spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
  672. OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  673. fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
  674. le32_to_cpu(fe->i_clusters)));
  675. spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
  676. i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
  677. alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
  678. status = 0;
  679. /* save the new last alloc group so that the caller can cache it. */
  680. if (last_alloc_group)
  681. *last_alloc_group = ac->ac_last_group;
  682. bail:
  683. if (handle)
  684. ocfs2_commit_trans(osb, handle);
  685. if (ac)
  686. ocfs2_free_alloc_context(ac);
  687. brelse(bg_bh);
  688. if (status)
  689. mlog_errno(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. alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
  706. if (!alloc_inode) {
  707. mlog_errno(-EINVAL);
  708. return -EINVAL;
  709. }
  710. mutex_lock(&alloc_inode->i_mutex);
  711. status = ocfs2_inode_lock(alloc_inode, &bh, 1);
  712. if (status < 0) {
  713. mutex_unlock(&alloc_inode->i_mutex);
  714. iput(alloc_inode);
  715. mlog_errno(status);
  716. return status;
  717. }
  718. ac->ac_inode = alloc_inode;
  719. ac->ac_alloc_slot = slot;
  720. fe = (struct ocfs2_dinode *) bh->b_data;
  721. /* The bh was validated by the inode read inside
  722. * ocfs2_inode_lock(). Any corruption is a code bug. */
  723. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  724. if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
  725. ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
  726. (unsigned long long)le64_to_cpu(fe->i_blkno));
  727. status = -EIO;
  728. goto bail;
  729. }
  730. free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
  731. le32_to_cpu(fe->id1.bitmap1.i_used);
  732. if (bits_wanted > free_bits) {
  733. /* cluster bitmap never grows */
  734. if (ocfs2_is_cluster_bitmap(alloc_inode)) {
  735. trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
  736. free_bits);
  737. status = -ENOSPC;
  738. goto bail;
  739. }
  740. if (!(flags & ALLOC_NEW_GROUP)) {
  741. trace_ocfs2_reserve_suballoc_bits_no_new_group(
  742. slot, bits_wanted, free_bits);
  743. status = -ENOSPC;
  744. goto bail;
  745. }
  746. status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
  747. ac->ac_max_block,
  748. last_alloc_group, flags);
  749. if (status < 0) {
  750. if (status != -ENOSPC)
  751. mlog_errno(status);
  752. goto bail;
  753. }
  754. atomic_inc(&osb->alloc_stats.bg_extends);
  755. /* You should never ask for this much metadata */
  756. BUG_ON(bits_wanted >
  757. (le32_to_cpu(fe->id1.bitmap1.i_total)
  758. - le32_to_cpu(fe->id1.bitmap1.i_used)));
  759. }
  760. get_bh(bh);
  761. ac->ac_bh = bh;
  762. bail:
  763. brelse(bh);
  764. if (status)
  765. mlog_errno(status);
  766. return status;
  767. }
  768. static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
  769. {
  770. spin_lock(&osb->osb_lock);
  771. osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
  772. spin_unlock(&osb->osb_lock);
  773. atomic_set(&osb->s_num_inodes_stolen, 0);
  774. }
  775. static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
  776. {
  777. spin_lock(&osb->osb_lock);
  778. osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
  779. spin_unlock(&osb->osb_lock);
  780. atomic_set(&osb->s_num_meta_stolen, 0);
  781. }
  782. void ocfs2_init_steal_slots(struct ocfs2_super *osb)
  783. {
  784. ocfs2_init_inode_steal_slot(osb);
  785. ocfs2_init_meta_steal_slot(osb);
  786. }
  787. static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
  788. {
  789. spin_lock(&osb->osb_lock);
  790. if (type == INODE_ALLOC_SYSTEM_INODE)
  791. osb->s_inode_steal_slot = slot;
  792. else if (type == EXTENT_ALLOC_SYSTEM_INODE)
  793. osb->s_meta_steal_slot = slot;
  794. spin_unlock(&osb->osb_lock);
  795. }
  796. static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
  797. {
  798. int slot = OCFS2_INVALID_SLOT;
  799. spin_lock(&osb->osb_lock);
  800. if (type == INODE_ALLOC_SYSTEM_INODE)
  801. slot = osb->s_inode_steal_slot;
  802. else if (type == EXTENT_ALLOC_SYSTEM_INODE)
  803. slot = osb->s_meta_steal_slot;
  804. spin_unlock(&osb->osb_lock);
  805. return slot;
  806. }
  807. static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
  808. {
  809. return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
  810. }
  811. static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
  812. {
  813. return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
  814. }
  815. static int ocfs2_steal_resource(struct ocfs2_super *osb,
  816. struct ocfs2_alloc_context *ac,
  817. int type)
  818. {
  819. int i, status = -ENOSPC;
  820. int slot = __ocfs2_get_steal_slot(osb, type);
  821. /* Start to steal resource from the first slot after ours. */
  822. if (slot == OCFS2_INVALID_SLOT)
  823. slot = osb->slot_num + 1;
  824. for (i = 0; i < osb->max_slots; i++, slot++) {
  825. if (slot == osb->max_slots)
  826. slot = 0;
  827. if (slot == osb->slot_num)
  828. continue;
  829. status = ocfs2_reserve_suballoc_bits(osb, ac,
  830. type,
  831. (u32)slot, NULL,
  832. NOT_ALLOC_NEW_GROUP);
  833. if (status >= 0) {
  834. __ocfs2_set_steal_slot(osb, slot, type);
  835. break;
  836. }
  837. ocfs2_free_ac_resource(ac);
  838. }
  839. return status;
  840. }
  841. static int ocfs2_steal_inode(struct ocfs2_super *osb,
  842. struct ocfs2_alloc_context *ac)
  843. {
  844. return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
  845. }
  846. static int ocfs2_steal_meta(struct ocfs2_super *osb,
  847. struct ocfs2_alloc_context *ac)
  848. {
  849. return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
  850. }
  851. int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
  852. int blocks,
  853. struct ocfs2_alloc_context **ac)
  854. {
  855. int status;
  856. int slot = ocfs2_get_meta_steal_slot(osb);
  857. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  858. if (!(*ac)) {
  859. status = -ENOMEM;
  860. mlog_errno(status);
  861. goto bail;
  862. }
  863. (*ac)->ac_bits_wanted = blocks;
  864. (*ac)->ac_which = OCFS2_AC_USE_META;
  865. (*ac)->ac_group_search = ocfs2_block_group_search;
  866. if (slot != OCFS2_INVALID_SLOT &&
  867. atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
  868. goto extent_steal;
  869. atomic_set(&osb->s_num_meta_stolen, 0);
  870. status = ocfs2_reserve_suballoc_bits(osb, (*ac),
  871. EXTENT_ALLOC_SYSTEM_INODE,
  872. (u32)osb->slot_num, NULL,
  873. ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
  874. if (status >= 0) {
  875. status = 0;
  876. if (slot != OCFS2_INVALID_SLOT)
  877. ocfs2_init_meta_steal_slot(osb);
  878. goto bail;
  879. } else if (status < 0 && status != -ENOSPC) {
  880. mlog_errno(status);
  881. goto bail;
  882. }
  883. ocfs2_free_ac_resource(*ac);
  884. extent_steal:
  885. status = ocfs2_steal_meta(osb, *ac);
  886. atomic_inc(&osb->s_num_meta_stolen);
  887. if (status < 0) {
  888. if (status != -ENOSPC)
  889. mlog_errno(status);
  890. goto bail;
  891. }
  892. status = 0;
  893. bail:
  894. if ((status < 0) && *ac) {
  895. ocfs2_free_alloc_context(*ac);
  896. *ac = NULL;
  897. }
  898. if (status)
  899. mlog_errno(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. trace_ocfs2_reserve_new_inode_new_group(
  958. (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. if (status)
  986. mlog_errno(status);
  987. return status;
  988. }
  989. /* local alloc code has to do the same thing, so rather than do this
  990. * twice.. */
  991. int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
  992. struct ocfs2_alloc_context *ac)
  993. {
  994. int status;
  995. ac->ac_which = OCFS2_AC_USE_MAIN;
  996. ac->ac_group_search = ocfs2_cluster_group_search;
  997. status = ocfs2_reserve_suballoc_bits(osb, ac,
  998. GLOBAL_BITMAP_SYSTEM_INODE,
  999. OCFS2_INVALID_SLOT, NULL,
  1000. ALLOC_NEW_GROUP);
  1001. if (status < 0 && status != -ENOSPC) {
  1002. mlog_errno(status);
  1003. goto bail;
  1004. }
  1005. bail:
  1006. return status;
  1007. }
  1008. /* Callers don't need to care which bitmap (local alloc or main) to
  1009. * use so we figure it out for them, but unfortunately this clutters
  1010. * things a bit. */
  1011. static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
  1012. u32 bits_wanted, u64 max_block,
  1013. int flags,
  1014. struct ocfs2_alloc_context **ac)
  1015. {
  1016. int status;
  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. if (status)
  1051. mlog_errno(status);
  1052. return status;
  1053. }
  1054. int ocfs2_reserve_clusters(struct ocfs2_super *osb,
  1055. u32 bits_wanted,
  1056. struct ocfs2_alloc_context **ac)
  1057. {
  1058. return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
  1059. ALLOC_NEW_GROUP, ac);
  1060. }
  1061. /*
  1062. * More or less lifted from ext3. I'll leave their description below:
  1063. *
  1064. * "For ext3 allocations, we must not reuse any blocks which are
  1065. * allocated in the bitmap buffer's "last committed data" copy. This
  1066. * prevents deletes from freeing up the page for reuse until we have
  1067. * committed the delete transaction.
  1068. *
  1069. * If we didn't do this, then deleting something and reallocating it as
  1070. * data would allow the old block to be overwritten before the
  1071. * transaction committed (because we force data to disk before commit).
  1072. * This would lead to corruption if we crashed between overwriting the
  1073. * data and committing the delete.
  1074. *
  1075. * @@@ We may want to make this allocation behaviour conditional on
  1076. * data-writes at some point, and disable it for metadata allocations or
  1077. * sync-data inodes."
  1078. *
  1079. * Note: OCFS2 already does this differently for metadata vs data
  1080. * allocations, as those bitmaps are separate and undo access is never
  1081. * called on a metadata group descriptor.
  1082. */
  1083. static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  1084. int nr)
  1085. {
  1086. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  1087. int ret;
  1088. if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
  1089. return 0;
  1090. if (!buffer_jbd(bg_bh))
  1091. return 1;
  1092. jbd_lock_bh_state(bg_bh);
  1093. bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
  1094. if (bg)
  1095. ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
  1096. else
  1097. ret = 1;
  1098. jbd_unlock_bh_state(bg_bh);
  1099. return ret;
  1100. }
  1101. static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
  1102. struct buffer_head *bg_bh,
  1103. unsigned int bits_wanted,
  1104. unsigned int total_bits,
  1105. struct ocfs2_suballoc_result *res)
  1106. {
  1107. void *bitmap;
  1108. u16 best_offset, best_size;
  1109. int offset, start, found, status = 0;
  1110. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  1111. /* Callers got this descriptor from
  1112. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  1113. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  1114. found = start = best_offset = best_size = 0;
  1115. bitmap = bg->bg_bitmap;
  1116. while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
  1117. if (offset == total_bits)
  1118. break;
  1119. if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
  1120. /* We found a zero, but we can't use it as it
  1121. * hasn't been put to disk yet! */
  1122. found = 0;
  1123. start = offset + 1;
  1124. } else if (offset == start) {
  1125. /* we found a zero */
  1126. found++;
  1127. /* move start to the next bit to test */
  1128. start++;
  1129. } else {
  1130. /* got a zero after some ones */
  1131. found = 1;
  1132. start = offset + 1;
  1133. }
  1134. if (found > best_size) {
  1135. best_size = found;
  1136. best_offset = start - found;
  1137. }
  1138. /* we got everything we needed */
  1139. if (found == bits_wanted) {
  1140. /* mlog(0, "Found it all!\n"); */
  1141. break;
  1142. }
  1143. }
  1144. if (best_size) {
  1145. res->sr_bit_offset = best_offset;
  1146. res->sr_bits = best_size;
  1147. } else {
  1148. status = -ENOSPC;
  1149. /* No error log here -- see the comment above
  1150. * ocfs2_test_bg_bit_allocatable */
  1151. }
  1152. return status;
  1153. }
  1154. static inline int ocfs2_block_group_set_bits(handle_t *handle,
  1155. struct inode *alloc_inode,
  1156. struct ocfs2_group_desc *bg,
  1157. struct buffer_head *group_bh,
  1158. unsigned int bit_off,
  1159. unsigned int num_bits)
  1160. {
  1161. int status;
  1162. void *bitmap = bg->bg_bitmap;
  1163. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  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. trace_ocfs2_block_group_set_bits(bit_off, num_bits);
  1169. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1170. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  1171. status = ocfs2_journal_access_gd(handle,
  1172. INODE_CACHE(alloc_inode),
  1173. group_bh,
  1174. journal_type);
  1175. if (status < 0) {
  1176. mlog_errno(status);
  1177. goto bail;
  1178. }
  1179. le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
  1180. if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
  1181. ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit"
  1182. " count %u but claims %u are freed. num_bits %d",
  1183. (unsigned long long)le64_to_cpu(bg->bg_blkno),
  1184. le16_to_cpu(bg->bg_bits),
  1185. le16_to_cpu(bg->bg_free_bits_count), num_bits);
  1186. return -EROFS;
  1187. }
  1188. while(num_bits--)
  1189. ocfs2_set_bit(bit_off++, bitmap);
  1190. ocfs2_journal_dirty(handle, group_bh);
  1191. bail:
  1192. if (status)
  1193. mlog_errno(status);
  1194. return status;
  1195. }
  1196. /* find the one with the most empty bits */
  1197. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
  1198. {
  1199. u16 curr, best;
  1200. BUG_ON(!cl->cl_next_free_rec);
  1201. best = curr = 0;
  1202. while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
  1203. if (le32_to_cpu(cl->cl_recs[curr].c_free) >
  1204. le32_to_cpu(cl->cl_recs[best].c_free))
  1205. best = curr;
  1206. curr++;
  1207. }
  1208. BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
  1209. return best;
  1210. }
  1211. static int ocfs2_relink_block_group(handle_t *handle,
  1212. struct inode *alloc_inode,
  1213. struct buffer_head *fe_bh,
  1214. struct buffer_head *bg_bh,
  1215. struct buffer_head *prev_bg_bh,
  1216. u16 chain)
  1217. {
  1218. int status;
  1219. /* there is a really tiny chance the journal calls could fail,
  1220. * but we wouldn't want inconsistent blocks in *any* case. */
  1221. u64 fe_ptr, bg_ptr, prev_bg_ptr;
  1222. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  1223. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  1224. struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
  1225. /* The caller got these descriptors from
  1226. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  1227. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  1228. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
  1229. trace_ocfs2_relink_block_group(
  1230. (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
  1231. (unsigned long long)le64_to_cpu(bg->bg_blkno),
  1232. (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
  1233. fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
  1234. bg_ptr = le64_to_cpu(bg->bg_next_group);
  1235. prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
  1236. status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  1237. prev_bg_bh,
  1238. OCFS2_JOURNAL_ACCESS_WRITE);
  1239. if (status < 0) {
  1240. mlog_errno(status);
  1241. goto out_rollback;
  1242. }
  1243. prev_bg->bg_next_group = bg->bg_next_group;
  1244. ocfs2_journal_dirty(handle, prev_bg_bh);
  1245. status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  1246. bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1247. if (status < 0) {
  1248. mlog_errno(status);
  1249. goto out_rollback;
  1250. }
  1251. bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
  1252. ocfs2_journal_dirty(handle, bg_bh);
  1253. status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  1254. fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1255. if (status < 0) {
  1256. mlog_errno(status);
  1257. goto out_rollback;
  1258. }
  1259. fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
  1260. ocfs2_journal_dirty(handle, fe_bh);
  1261. out_rollback:
  1262. if (status < 0) {
  1263. fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
  1264. bg->bg_next_group = cpu_to_le64(bg_ptr);
  1265. prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
  1266. }
  1267. if (status)
  1268. mlog_errno(status);
  1269. return status;
  1270. }
  1271. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  1272. u32 wanted)
  1273. {
  1274. return le16_to_cpu(bg->bg_free_bits_count) > wanted;
  1275. }
  1276. /* return 0 on success, -ENOSPC to keep searching and any other < 0
  1277. * value on error. */
  1278. static int ocfs2_cluster_group_search(struct inode *inode,
  1279. struct buffer_head *group_bh,
  1280. u32 bits_wanted, u32 min_bits,
  1281. u64 max_block,
  1282. struct ocfs2_suballoc_result *res)
  1283. {
  1284. int search = -ENOSPC;
  1285. int ret;
  1286. u64 blkoff;
  1287. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
  1288. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1289. unsigned int max_bits, gd_cluster_off;
  1290. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1291. if (gd->bg_free_bits_count) {
  1292. max_bits = le16_to_cpu(gd->bg_bits);
  1293. /* Tail groups in cluster bitmaps which aren't cpg
  1294. * aligned are prone to partial extension by a failed
  1295. * fs resize. If the file system resize never got to
  1296. * update the dinode cluster count, then we don't want
  1297. * to trust any clusters past it, regardless of what
  1298. * the group descriptor says. */
  1299. gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
  1300. le64_to_cpu(gd->bg_blkno));
  1301. if ((gd_cluster_off + max_bits) >
  1302. OCFS2_I(inode)->ip_clusters) {
  1303. max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
  1304. trace_ocfs2_cluster_group_search_wrong_max_bits(
  1305. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  1306. le16_to_cpu(gd->bg_bits),
  1307. OCFS2_I(inode)->ip_clusters, max_bits);
  1308. }
  1309. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  1310. group_bh, bits_wanted,
  1311. max_bits, res);
  1312. if (ret)
  1313. return ret;
  1314. if (max_block) {
  1315. blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
  1316. gd_cluster_off +
  1317. res->sr_bit_offset +
  1318. res->sr_bits);
  1319. trace_ocfs2_cluster_group_search_max_block(
  1320. (unsigned long long)blkoff,
  1321. (unsigned long long)max_block);
  1322. if (blkoff > max_block)
  1323. return -ENOSPC;
  1324. }
  1325. /* ocfs2_block_group_find_clear_bits() might
  1326. * return success, but we still want to return
  1327. * -ENOSPC unless it found the minimum number
  1328. * of bits. */
  1329. if (min_bits <= res->sr_bits)
  1330. search = 0; /* success */
  1331. else if (res->sr_bits) {
  1332. /*
  1333. * Don't show bits which we'll be returning
  1334. * for allocation to the local alloc bitmap.
  1335. */
  1336. ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
  1337. }
  1338. }
  1339. return search;
  1340. }
  1341. static int ocfs2_block_group_search(struct inode *inode,
  1342. struct buffer_head *group_bh,
  1343. u32 bits_wanted, u32 min_bits,
  1344. u64 max_block,
  1345. struct ocfs2_suballoc_result *res)
  1346. {
  1347. int ret = -ENOSPC;
  1348. u64 blkoff;
  1349. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1350. BUG_ON(min_bits != 1);
  1351. BUG_ON(ocfs2_is_cluster_bitmap(inode));
  1352. if (bg->bg_free_bits_count) {
  1353. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  1354. group_bh, bits_wanted,
  1355. le16_to_cpu(bg->bg_bits),
  1356. res);
  1357. if (!ret && max_block) {
  1358. blkoff = le64_to_cpu(bg->bg_blkno) +
  1359. res->sr_bit_offset + res->sr_bits;
  1360. trace_ocfs2_block_group_search_max_block(
  1361. (unsigned long long)blkoff,
  1362. (unsigned long long)max_block);
  1363. if (blkoff > max_block)
  1364. ret = -ENOSPC;
  1365. }
  1366. }
  1367. return ret;
  1368. }
  1369. static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
  1370. handle_t *handle,
  1371. struct buffer_head *di_bh,
  1372. u32 num_bits,
  1373. u16 chain)
  1374. {
  1375. int ret;
  1376. u32 tmp_used;
  1377. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  1378. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
  1379. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1380. OCFS2_JOURNAL_ACCESS_WRITE);
  1381. if (ret < 0) {
  1382. mlog_errno(ret);
  1383. goto out;
  1384. }
  1385. tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
  1386. di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
  1387. le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
  1388. ocfs2_journal_dirty(handle, di_bh);
  1389. out:
  1390. return ret;
  1391. }
  1392. static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
  1393. struct ocfs2_extent_rec *rec,
  1394. struct ocfs2_chain_list *cl)
  1395. {
  1396. unsigned int bpc = le16_to_cpu(cl->cl_bpc);
  1397. unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
  1398. unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc;
  1399. if (res->sr_bit_offset < bitoff)
  1400. return 0;
  1401. if (res->sr_bit_offset >= (bitoff + bitcount))
  1402. return 0;
  1403. res->sr_blkno = le64_to_cpu(rec->e_blkno) +
  1404. (res->sr_bit_offset - bitoff);
  1405. if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
  1406. res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
  1407. return 1;
  1408. }
  1409. static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
  1410. struct ocfs2_group_desc *bg,
  1411. struct ocfs2_suballoc_result *res)
  1412. {
  1413. int i;
  1414. u64 bg_blkno = res->sr_bg_blkno; /* Save off */
  1415. struct ocfs2_extent_rec *rec;
  1416. struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
  1417. struct ocfs2_chain_list *cl = &di->id2.i_chain;
  1418. if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
  1419. res->sr_blkno = 0;
  1420. return;
  1421. }
  1422. res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
  1423. res->sr_bg_blkno = 0; /* Clear it for contig block groups */
  1424. if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
  1425. !bg->bg_list.l_next_free_rec)
  1426. return;
  1427. for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
  1428. rec = &bg->bg_list.l_recs[i];
  1429. if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
  1430. res->sr_bg_blkno = bg_blkno; /* Restore */
  1431. break;
  1432. }
  1433. }
  1434. }
  1435. static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
  1436. handle_t *handle,
  1437. u32 bits_wanted,
  1438. u32 min_bits,
  1439. struct ocfs2_suballoc_result *res,
  1440. u16 *bits_left)
  1441. {
  1442. int ret;
  1443. struct buffer_head *group_bh = NULL;
  1444. struct ocfs2_group_desc *gd;
  1445. struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
  1446. struct inode *alloc_inode = ac->ac_inode;
  1447. ret = ocfs2_read_group_descriptor(alloc_inode, di,
  1448. res->sr_bg_blkno, &group_bh);
  1449. if (ret < 0) {
  1450. mlog_errno(ret);
  1451. return ret;
  1452. }
  1453. gd = (struct ocfs2_group_desc *) group_bh->b_data;
  1454. ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
  1455. ac->ac_max_block, res);
  1456. if (ret < 0) {
  1457. if (ret != -ENOSPC)
  1458. mlog_errno(ret);
  1459. goto out;
  1460. }
  1461. if (!ret)
  1462. ocfs2_bg_discontig_fix_result(ac, gd, res);
  1463. /*
  1464. * sr_bg_blkno might have been changed by
  1465. * ocfs2_bg_discontig_fix_result
  1466. */
  1467. res->sr_bg_stable_blkno = group_bh->b_blocknr;
  1468. if (ac->ac_find_loc_only)
  1469. goto out_loc_only;
  1470. ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
  1471. res->sr_bits,
  1472. le16_to_cpu(gd->bg_chain));
  1473. if (ret < 0) {
  1474. mlog_errno(ret);
  1475. goto out;
  1476. }
  1477. ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
  1478. res->sr_bit_offset, res->sr_bits);
  1479. if (ret < 0)
  1480. mlog_errno(ret);
  1481. out_loc_only:
  1482. *bits_left = le16_to_cpu(gd->bg_free_bits_count);
  1483. out:
  1484. brelse(group_bh);
  1485. return ret;
  1486. }
  1487. static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
  1488. handle_t *handle,
  1489. u32 bits_wanted,
  1490. u32 min_bits,
  1491. struct ocfs2_suballoc_result *res,
  1492. u16 *bits_left)
  1493. {
  1494. int status;
  1495. u16 chain;
  1496. u64 next_group;
  1497. struct inode *alloc_inode = ac->ac_inode;
  1498. struct buffer_head *group_bh = NULL;
  1499. struct buffer_head *prev_group_bh = NULL;
  1500. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1501. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  1502. struct ocfs2_group_desc *bg;
  1503. chain = ac->ac_chain;
  1504. trace_ocfs2_search_chain_begin(
  1505. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
  1506. bits_wanted, chain);
  1507. status = ocfs2_read_group_descriptor(alloc_inode, fe,
  1508. le64_to_cpu(cl->cl_recs[chain].c_blkno),
  1509. &group_bh);
  1510. if (status < 0) {
  1511. mlog_errno(status);
  1512. goto bail;
  1513. }
  1514. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1515. status = -ENOSPC;
  1516. /* for now, the chain search is a bit simplistic. We just use
  1517. * the 1st group with any empty bits. */
  1518. while ((status = ac->ac_group_search(alloc_inode, group_bh,
  1519. bits_wanted, min_bits,
  1520. ac->ac_max_block,
  1521. res)) == -ENOSPC) {
  1522. if (!bg->bg_next_group)
  1523. break;
  1524. brelse(prev_group_bh);
  1525. prev_group_bh = NULL;
  1526. next_group = le64_to_cpu(bg->bg_next_group);
  1527. prev_group_bh = group_bh;
  1528. group_bh = NULL;
  1529. status = ocfs2_read_group_descriptor(alloc_inode, fe,
  1530. next_group, &group_bh);
  1531. if (status < 0) {
  1532. mlog_errno(status);
  1533. goto bail;
  1534. }
  1535. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1536. }
  1537. if (status < 0) {
  1538. if (status != -ENOSPC)
  1539. mlog_errno(status);
  1540. goto bail;
  1541. }
  1542. trace_ocfs2_search_chain_succ(
  1543. (unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
  1544. res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
  1545. BUG_ON(res->sr_bits == 0);
  1546. if (!status)
  1547. ocfs2_bg_discontig_fix_result(ac, bg, res);
  1548. /*
  1549. * sr_bg_blkno might have been changed by
  1550. * ocfs2_bg_discontig_fix_result
  1551. */
  1552. res->sr_bg_stable_blkno = group_bh->b_blocknr;
  1553. /*
  1554. * Keep track of previous block descriptor read. When
  1555. * we find a target, if we have read more than X
  1556. * number of descriptors, and the target is reasonably
  1557. * empty, relink him to top of his chain.
  1558. *
  1559. * We've read 0 extra blocks and only send one more to
  1560. * the transaction, yet the next guy to search has a
  1561. * much easier time.
  1562. *
  1563. * Do this *after* figuring out how many bits we're taking out
  1564. * of our target group.
  1565. */
  1566. if (!ac->ac_disable_chain_relink &&
  1567. (prev_group_bh) &&
  1568. (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
  1569. status = ocfs2_relink_block_group(handle, alloc_inode,
  1570. ac->ac_bh, group_bh,
  1571. prev_group_bh, chain);
  1572. if (status < 0) {
  1573. mlog_errno(status);
  1574. goto bail;
  1575. }
  1576. }
  1577. if (ac->ac_find_loc_only)
  1578. goto out_loc_only;
  1579. status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
  1580. ac->ac_bh, res->sr_bits,
  1581. chain);
  1582. if (status) {
  1583. mlog_errno(status);
  1584. goto bail;
  1585. }
  1586. status = ocfs2_block_group_set_bits(handle,
  1587. alloc_inode,
  1588. bg,
  1589. group_bh,
  1590. res->sr_bit_offset,
  1591. res->sr_bits);
  1592. if (status < 0) {
  1593. mlog_errno(status);
  1594. goto bail;
  1595. }
  1596. trace_ocfs2_search_chain_end(
  1597. (unsigned long long)le64_to_cpu(fe->i_blkno),
  1598. res->sr_bits);
  1599. out_loc_only:
  1600. *bits_left = le16_to_cpu(bg->bg_free_bits_count);
  1601. bail:
  1602. brelse(group_bh);
  1603. brelse(prev_group_bh);
  1604. if (status)
  1605. mlog_errno(status);
  1606. return status;
  1607. }
  1608. /* will give out up to bits_wanted contiguous bits. */
  1609. static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
  1610. handle_t *handle,
  1611. u32 bits_wanted,
  1612. u32 min_bits,
  1613. struct ocfs2_suballoc_result *res)
  1614. {
  1615. int status;
  1616. u16 victim, i;
  1617. u16 bits_left = 0;
  1618. u64 hint = ac->ac_last_group;
  1619. struct ocfs2_chain_list *cl;
  1620. struct ocfs2_dinode *fe;
  1621. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1622. BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
  1623. BUG_ON(!ac->ac_bh);
  1624. fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1625. /* The bh was validated by the inode read during
  1626. * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
  1627. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  1628. if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
  1629. le32_to_cpu(fe->id1.bitmap1.i_total)) {
  1630. ocfs2_error(ac->ac_inode->i_sb,
  1631. "Chain allocator dinode %llu has %u used "
  1632. "bits but only %u total.",
  1633. (unsigned long long)le64_to_cpu(fe->i_blkno),
  1634. le32_to_cpu(fe->id1.bitmap1.i_used),
  1635. le32_to_cpu(fe->id1.bitmap1.i_total));
  1636. status = -EIO;
  1637. goto bail;
  1638. }
  1639. res->sr_bg_blkno = hint;
  1640. if (res->sr_bg_blkno) {
  1641. /* Attempt to short-circuit the usual search mechanism
  1642. * by jumping straight to the most recently used
  1643. * allocation group. This helps us maintain some
  1644. * contiguousness across allocations. */
  1645. status = ocfs2_search_one_group(ac, handle, bits_wanted,
  1646. min_bits, res, &bits_left);
  1647. if (!status)
  1648. goto set_hint;
  1649. if (status < 0 && status != -ENOSPC) {
  1650. mlog_errno(status);
  1651. goto bail;
  1652. }
  1653. }
  1654. cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  1655. victim = ocfs2_find_victim_chain(cl);
  1656. ac->ac_chain = victim;
  1657. status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
  1658. res, &bits_left);
  1659. if (!status) {
  1660. hint = ocfs2_group_from_res(res);
  1661. goto set_hint;
  1662. }
  1663. if (status < 0 && status != -ENOSPC) {
  1664. mlog_errno(status);
  1665. goto bail;
  1666. }
  1667. trace_ocfs2_claim_suballoc_bits(victim);
  1668. /* If we didn't pick a good victim, then just default to
  1669. * searching each chain in order. Don't allow chain relinking
  1670. * because we only calculate enough journal credits for one
  1671. * relink per alloc. */
  1672. ac->ac_disable_chain_relink = 1;
  1673. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
  1674. if (i == victim)
  1675. continue;
  1676. if (!cl->cl_recs[i].c_free)
  1677. continue;
  1678. ac->ac_chain = i;
  1679. status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
  1680. res, &bits_left);
  1681. if (!status) {
  1682. hint = ocfs2_group_from_res(res);
  1683. break;
  1684. }
  1685. if (status < 0 && status != -ENOSPC) {
  1686. mlog_errno(status);
  1687. goto bail;
  1688. }
  1689. }
  1690. set_hint:
  1691. if (status != -ENOSPC) {
  1692. /* If the next search of this group is not likely to
  1693. * yield a suitable extent, then we reset the last
  1694. * group hint so as to not waste a disk read */
  1695. if (bits_left < min_bits)
  1696. ac->ac_last_group = 0;
  1697. else
  1698. ac->ac_last_group = hint;
  1699. }
  1700. bail:
  1701. if (status)
  1702. mlog_errno(status);
  1703. return status;
  1704. }
  1705. int ocfs2_claim_metadata(handle_t *handle,
  1706. struct ocfs2_alloc_context *ac,
  1707. u32 bits_wanted,
  1708. u64 *suballoc_loc,
  1709. u16 *suballoc_bit_start,
  1710. unsigned int *num_bits,
  1711. u64 *blkno_start)
  1712. {
  1713. int status;
  1714. struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
  1715. BUG_ON(!ac);
  1716. BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
  1717. BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
  1718. status = ocfs2_claim_suballoc_bits(ac,
  1719. handle,
  1720. bits_wanted,
  1721. 1,
  1722. &res);
  1723. if (status < 0) {
  1724. mlog_errno(status);
  1725. goto bail;
  1726. }
  1727. atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
  1728. *suballoc_loc = res.sr_bg_blkno;
  1729. *suballoc_bit_start = res.sr_bit_offset;
  1730. *blkno_start = res.sr_blkno;
  1731. ac->ac_bits_given += res.sr_bits;
  1732. *num_bits = res.sr_bits;
  1733. status = 0;
  1734. bail:
  1735. if (status)
  1736. mlog_errno(status);
  1737. return status;
  1738. }
  1739. static void ocfs2_init_inode_ac_group(struct inode *dir,
  1740. struct buffer_head *parent_di_bh,
  1741. struct ocfs2_alloc_context *ac)
  1742. {
  1743. struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
  1744. /*
  1745. * Try to allocate inodes from some specific group.
  1746. *
  1747. * If the parent dir has recorded the last group used in allocation,
  1748. * cool, use it. Otherwise if we try to allocate new inode from the
  1749. * same slot the parent dir belongs to, use the same chunk.
  1750. *
  1751. * We are very careful here to avoid the mistake of setting
  1752. * ac_last_group to a group descriptor from a different (unlocked) slot.
  1753. */
  1754. if (OCFS2_I(dir)->ip_last_used_group &&
  1755. OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
  1756. ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
  1757. else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
  1758. if (di->i_suballoc_loc)
  1759. ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
  1760. else
  1761. ac->ac_last_group = ocfs2_which_suballoc_group(
  1762. le64_to_cpu(di->i_blkno),
  1763. le16_to_cpu(di->i_suballoc_bit));
  1764. }
  1765. }
  1766. static inline void ocfs2_save_inode_ac_group(struct inode *dir,
  1767. struct ocfs2_alloc_context *ac)
  1768. {
  1769. OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
  1770. OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
  1771. }
  1772. int ocfs2_find_new_inode_loc(struct inode *dir,
  1773. struct buffer_head *parent_fe_bh,
  1774. struct ocfs2_alloc_context *ac,
  1775. u64 *fe_blkno)
  1776. {
  1777. int ret;
  1778. handle_t *handle = NULL;
  1779. struct ocfs2_suballoc_result *res;
  1780. BUG_ON(!ac);
  1781. BUG_ON(ac->ac_bits_given != 0);
  1782. BUG_ON(ac->ac_bits_wanted != 1);
  1783. BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
  1784. res = kzalloc(sizeof(*res), GFP_NOFS);
  1785. if (res == NULL) {
  1786. ret = -ENOMEM;
  1787. mlog_errno(ret);
  1788. goto out;
  1789. }
  1790. ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
  1791. /*
  1792. * The handle started here is for chain relink. Alternatively,
  1793. * we could just disable relink for these calls.
  1794. */
  1795. handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
  1796. if (IS_ERR(handle)) {
  1797. ret = PTR_ERR(handle);
  1798. handle = NULL;
  1799. mlog_errno(ret);
  1800. goto out;
  1801. }
  1802. /*
  1803. * This will instruct ocfs2_claim_suballoc_bits and
  1804. * ocfs2_search_one_group to search but save actual allocation
  1805. * for later.
  1806. */
  1807. ac->ac_find_loc_only = 1;
  1808. ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
  1809. if (ret < 0) {
  1810. mlog_errno(ret);
  1811. goto out;
  1812. }
  1813. ac->ac_find_loc_priv = res;
  1814. *fe_blkno = res->sr_blkno;
  1815. out:
  1816. if (handle)
  1817. ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);
  1818. if (ret)
  1819. kfree(res);
  1820. return ret;
  1821. }
  1822. int ocfs2_claim_new_inode_at_loc(handle_t *handle,
  1823. struct inode *dir,
  1824. struct ocfs2_alloc_context *ac,
  1825. u64 *suballoc_loc,
  1826. u16 *suballoc_bit,
  1827. u64 di_blkno)
  1828. {
  1829. int ret;
  1830. u16 chain;
  1831. struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
  1832. struct buffer_head *bg_bh = NULL;
  1833. struct ocfs2_group_desc *bg;
  1834. struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1835. /*
  1836. * Since di_blkno is being passed back in, we check for any
  1837. * inconsistencies which may have happened between
  1838. * calls. These are code bugs as di_blkno is not expected to
  1839. * change once returned from ocfs2_find_new_inode_loc()
  1840. */
  1841. BUG_ON(res->sr_blkno != di_blkno);
  1842. ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
  1843. res->sr_bg_stable_blkno, &bg_bh);
  1844. if (ret) {
  1845. mlog_errno(ret);
  1846. goto out;
  1847. }
  1848. bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  1849. chain = le16_to_cpu(bg->bg_chain);
  1850. ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
  1851. ac->ac_bh, res->sr_bits,
  1852. chain);
  1853. if (ret) {
  1854. mlog_errno(ret);
  1855. goto out;
  1856. }
  1857. ret = ocfs2_block_group_set_bits(handle,
  1858. ac->ac_inode,
  1859. bg,
  1860. bg_bh,
  1861. res->sr_bit_offset,
  1862. res->sr_bits);
  1863. if (ret < 0) {
  1864. mlog_errno(ret);
  1865. goto out;
  1866. }
  1867. trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
  1868. res->sr_bits);
  1869. atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
  1870. BUG_ON(res->sr_bits != 1);
  1871. *suballoc_loc = res->sr_bg_blkno;
  1872. *suballoc_bit = res->sr_bit_offset;
  1873. ac->ac_bits_given++;
  1874. ocfs2_save_inode_ac_group(dir, ac);
  1875. out:
  1876. brelse(bg_bh);
  1877. return ret;
  1878. }
  1879. int ocfs2_claim_new_inode(handle_t *handle,
  1880. struct inode *dir,
  1881. struct buffer_head *parent_fe_bh,
  1882. struct ocfs2_alloc_context *ac,
  1883. u64 *suballoc_loc,
  1884. u16 *suballoc_bit,
  1885. u64 *fe_blkno)
  1886. {
  1887. int status;
  1888. struct ocfs2_suballoc_result res;
  1889. BUG_ON(!ac);
  1890. BUG_ON(ac->ac_bits_given != 0);
  1891. BUG_ON(ac->ac_bits_wanted != 1);
  1892. BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
  1893. ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
  1894. status = ocfs2_claim_suballoc_bits(ac,
  1895. handle,
  1896. 1,
  1897. 1,
  1898. &res);
  1899. if (status < 0) {
  1900. mlog_errno(status);
  1901. goto bail;
  1902. }
  1903. atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
  1904. BUG_ON(res.sr_bits != 1);
  1905. *suballoc_loc = res.sr_bg_blkno;
  1906. *suballoc_bit = res.sr_bit_offset;
  1907. *fe_blkno = res.sr_blkno;
  1908. ac->ac_bits_given++;
  1909. ocfs2_save_inode_ac_group(dir, ac);
  1910. status = 0;
  1911. bail:
  1912. if (status)
  1913. mlog_errno(status);
  1914. return status;
  1915. }
  1916. /* translate a group desc. blkno and it's bitmap offset into
  1917. * disk cluster offset. */
  1918. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  1919. u64 bg_blkno,
  1920. u16 bg_bit_off)
  1921. {
  1922. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1923. u32 cluster = 0;
  1924. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1925. if (bg_blkno != osb->first_cluster_group_blkno)
  1926. cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
  1927. cluster += (u32) bg_bit_off;
  1928. return cluster;
  1929. }
  1930. /* given a cluster offset, calculate which block group it belongs to
  1931. * and return that block offset. */
  1932. u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
  1933. {
  1934. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1935. u32 group_no;
  1936. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1937. group_no = cluster / osb->bitmap_cpg;
  1938. if (!group_no)
  1939. return osb->first_cluster_group_blkno;
  1940. return ocfs2_clusters_to_blocks(inode->i_sb,
  1941. group_no * osb->bitmap_cpg);
  1942. }
  1943. /* given the block number of a cluster start, calculate which cluster
  1944. * group and descriptor bitmap offset that corresponds to. */
  1945. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  1946. u64 data_blkno,
  1947. u64 *bg_blkno,
  1948. u16 *bg_bit_off)
  1949. {
  1950. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1951. u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
  1952. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1953. *bg_blkno = ocfs2_which_cluster_group(inode,
  1954. data_cluster);
  1955. if (*bg_blkno == osb->first_cluster_group_blkno)
  1956. *bg_bit_off = (u16) data_cluster;
  1957. else
  1958. *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
  1959. data_blkno - *bg_blkno);
  1960. }
  1961. /*
  1962. * min_bits - minimum contiguous chunk from this total allocation we
  1963. * can handle. set to what we asked for originally for a full
  1964. * contig. allocation, set to '1' to indicate we can deal with extents
  1965. * of any size.
  1966. */
  1967. int __ocfs2_claim_clusters(handle_t *handle,
  1968. struct ocfs2_alloc_context *ac,
  1969. u32 min_clusters,
  1970. u32 max_clusters,
  1971. u32 *cluster_start,
  1972. u32 *num_clusters)
  1973. {
  1974. int status;
  1975. unsigned int bits_wanted = max_clusters;
  1976. struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
  1977. struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
  1978. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1979. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
  1980. && ac->ac_which != OCFS2_AC_USE_MAIN);
  1981. if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
  1982. WARN_ON(min_clusters > 1);
  1983. status = ocfs2_claim_local_alloc_bits(osb,
  1984. handle,
  1985. ac,
  1986. bits_wanted,
  1987. cluster_start,
  1988. num_clusters);
  1989. if (!status)
  1990. atomic_inc(&osb->alloc_stats.local_data);
  1991. } else {
  1992. if (min_clusters > (osb->bitmap_cpg - 1)) {
  1993. /* The only paths asking for contiguousness
  1994. * should know about this already. */
  1995. mlog(ML_ERROR, "minimum allocation requested %u exceeds "
  1996. "group bitmap size %u!\n", min_clusters,
  1997. osb->bitmap_cpg);
  1998. status = -ENOSPC;
  1999. goto bail;
  2000. }
  2001. /* clamp the current request down to a realistic size. */
  2002. if (bits_wanted > (osb->bitmap_cpg - 1))
  2003. bits_wanted = osb->bitmap_cpg - 1;
  2004. status = ocfs2_claim_suballoc_bits(ac,
  2005. handle,
  2006. bits_wanted,
  2007. min_clusters,
  2008. &res);
  2009. if (!status) {
  2010. BUG_ON(res.sr_blkno); /* cluster alloc can't set */
  2011. *cluster_start =
  2012. ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
  2013. res.sr_bg_blkno,
  2014. res.sr_bit_offset);
  2015. atomic_inc(&osb->alloc_stats.bitmap_data);
  2016. *num_clusters = res.sr_bits;
  2017. }
  2018. }
  2019. if (status < 0) {
  2020. if (status != -ENOSPC)
  2021. mlog_errno(status);
  2022. goto bail;
  2023. }
  2024. ac->ac_bits_given += *num_clusters;
  2025. bail:
  2026. if (status)
  2027. mlog_errno(status);
  2028. return status;
  2029. }
  2030. int ocfs2_claim_clusters(handle_t *handle,
  2031. struct ocfs2_alloc_context *ac,
  2032. u32 min_clusters,
  2033. u32 *cluster_start,
  2034. u32 *num_clusters)
  2035. {
  2036. unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
  2037. return __ocfs2_claim_clusters(handle, ac, min_clusters,
  2038. bits_wanted, cluster_start, num_clusters);
  2039. }
  2040. static int ocfs2_block_group_clear_bits(handle_t *handle,
  2041. struct inode *alloc_inode,
  2042. struct ocfs2_group_desc *bg,
  2043. struct buffer_head *group_bh,
  2044. unsigned int bit_off,
  2045. unsigned int num_bits,
  2046. void (*undo_fn)(unsigned int bit,
  2047. unsigned long *bmap))
  2048. {
  2049. int status;
  2050. unsigned int tmp;
  2051. struct ocfs2_group_desc *undo_bg = NULL;
  2052. /* The caller got this descriptor from
  2053. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  2054. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  2055. trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
  2056. BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
  2057. status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  2058. group_bh,
  2059. undo_fn ?
  2060. OCFS2_JOURNAL_ACCESS_UNDO :
  2061. OCFS2_JOURNAL_ACCESS_WRITE);
  2062. if (status < 0) {
  2063. mlog_errno(status);
  2064. goto bail;
  2065. }
  2066. if (undo_fn) {
  2067. jbd_lock_bh_state(group_bh);
  2068. undo_bg = (struct ocfs2_group_desc *)
  2069. bh2jh(group_bh)->b_committed_data;
  2070. BUG_ON(!undo_bg);
  2071. }
  2072. tmp = num_bits;
  2073. while(tmp--) {
  2074. ocfs2_clear_bit((bit_off + tmp),
  2075. (unsigned long *) bg->bg_bitmap);
  2076. if (undo_fn)
  2077. undo_fn(bit_off + tmp,
  2078. (unsigned long *) undo_bg->bg_bitmap);
  2079. }
  2080. le16_add_cpu(&bg->bg_free_bits_count, num_bits);
  2081. if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
  2082. ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit"
  2083. " count %u but claims %u are freed. num_bits %d",
  2084. (unsigned long long)le64_to_cpu(bg->bg_blkno),
  2085. le16_to_cpu(bg->bg_bits),
  2086. le16_to_cpu(bg->bg_free_bits_count), num_bits);
  2087. return -EROFS;
  2088. }
  2089. if (undo_fn)
  2090. jbd_unlock_bh_state(group_bh);
  2091. ocfs2_journal_dirty(handle, group_bh);
  2092. bail:
  2093. return status;
  2094. }
  2095. /*
  2096. * expects the suballoc inode to already be locked.
  2097. */
  2098. static int _ocfs2_free_suballoc_bits(handle_t *handle,
  2099. struct inode *alloc_inode,
  2100. struct buffer_head *alloc_bh,
  2101. unsigned int start_bit,
  2102. u64 bg_blkno,
  2103. unsigned int count,
  2104. void (*undo_fn)(unsigned int bit,
  2105. unsigned long *bitmap))
  2106. {
  2107. int status = 0;
  2108. u32 tmp_used;
  2109. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
  2110. struct ocfs2_chain_list *cl = &fe->id2.i_chain;
  2111. struct buffer_head *group_bh = NULL;
  2112. struct ocfs2_group_desc *group;
  2113. /* The alloc_bh comes from ocfs2_free_dinode() or
  2114. * ocfs2_free_clusters(). The callers have all locked the
  2115. * allocator and gotten alloc_bh from the lock call. This
  2116. * validates the dinode buffer. Any corruption that has happened
  2117. * is a code bug. */
  2118. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  2119. BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
  2120. trace_ocfs2_free_suballoc_bits(
  2121. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
  2122. (unsigned long long)bg_blkno,
  2123. start_bit, count);
  2124. status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
  2125. &group_bh);
  2126. if (status < 0) {
  2127. mlog_errno(status);
  2128. goto bail;
  2129. }
  2130. group = (struct ocfs2_group_desc *) group_bh->b_data;
  2131. BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
  2132. status = ocfs2_block_group_clear_bits(handle, alloc_inode,
  2133. group, group_bh,
  2134. start_bit, count, undo_fn);
  2135. if (status < 0) {
  2136. mlog_errno(status);
  2137. goto bail;
  2138. }
  2139. status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  2140. alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  2141. if (status < 0) {
  2142. mlog_errno(status);
  2143. goto bail;
  2144. }
  2145. le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
  2146. count);
  2147. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  2148. fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
  2149. ocfs2_journal_dirty(handle, alloc_bh);
  2150. bail:
  2151. brelse(group_bh);
  2152. if (status)
  2153. mlog_errno(status);
  2154. return status;
  2155. }
  2156. int ocfs2_free_suballoc_bits(handle_t *handle,
  2157. struct inode *alloc_inode,
  2158. struct buffer_head *alloc_bh,
  2159. unsigned int start_bit,
  2160. u64 bg_blkno,
  2161. unsigned int count)
  2162. {
  2163. return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
  2164. start_bit, bg_blkno, count, NULL);
  2165. }
  2166. int ocfs2_free_dinode(handle_t *handle,
  2167. struct inode *inode_alloc_inode,
  2168. struct buffer_head *inode_alloc_bh,
  2169. struct ocfs2_dinode *di)
  2170. {
  2171. u64 blk = le64_to_cpu(di->i_blkno);
  2172. u16 bit = le16_to_cpu(di->i_suballoc_bit);
  2173. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  2174. if (di->i_suballoc_loc)
  2175. bg_blkno = le64_to_cpu(di->i_suballoc_loc);
  2176. return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
  2177. inode_alloc_bh, bit, bg_blkno, 1);
  2178. }
  2179. static int _ocfs2_free_clusters(handle_t *handle,
  2180. struct inode *bitmap_inode,
  2181. struct buffer_head *bitmap_bh,
  2182. u64 start_blk,
  2183. unsigned int num_clusters,
  2184. void (*undo_fn)(unsigned int bit,
  2185. unsigned long *bitmap))
  2186. {
  2187. int status;
  2188. u16 bg_start_bit;
  2189. u64 bg_blkno;
  2190. struct ocfs2_dinode *fe;
  2191. /* You can't ever have a contiguous set of clusters
  2192. * bigger than a block group bitmap so we never have to worry
  2193. * about looping on them.
  2194. * This is expensive. We can safely remove once this stuff has
  2195. * gotten tested really well. */
  2196. BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
  2197. fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
  2198. ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
  2199. &bg_start_bit);
  2200. trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
  2201. (unsigned long long)start_blk,
  2202. bg_start_bit, num_clusters);
  2203. status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
  2204. bg_start_bit, bg_blkno,
  2205. num_clusters, undo_fn);
  2206. if (status < 0) {
  2207. mlog_errno(status);
  2208. goto out;
  2209. }
  2210. ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
  2211. num_clusters);
  2212. out:
  2213. if (status)
  2214. mlog_errno(status);
  2215. return status;
  2216. }
  2217. int ocfs2_free_clusters(handle_t *handle,
  2218. struct inode *bitmap_inode,
  2219. struct buffer_head *bitmap_bh,
  2220. u64 start_blk,
  2221. unsigned int num_clusters)
  2222. {
  2223. return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
  2224. start_blk, num_clusters,
  2225. _ocfs2_set_bit);
  2226. }
  2227. /*
  2228. * Give never-used clusters back to the global bitmap. We don't need
  2229. * to protect these bits in the undo buffer.
  2230. */
  2231. int ocfs2_release_clusters(handle_t *handle,
  2232. struct inode *bitmap_inode,
  2233. struct buffer_head *bitmap_bh,
  2234. u64 start_blk,
  2235. unsigned int num_clusters)
  2236. {
  2237. return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
  2238. start_blk, num_clusters,
  2239. _ocfs2_clear_bit);
  2240. }
  2241. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
  2242. {
  2243. printk("Block Group:\n");
  2244. printk("bg_signature: %s\n", bg->bg_signature);
  2245. printk("bg_size: %u\n", bg->bg_size);
  2246. printk("bg_bits: %u\n", bg->bg_bits);
  2247. printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
  2248. printk("bg_chain: %u\n", bg->bg_chain);
  2249. printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
  2250. printk("bg_next_group: %llu\n",
  2251. (unsigned long long)bg->bg_next_group);
  2252. printk("bg_parent_dinode: %llu\n",
  2253. (unsigned long long)bg->bg_parent_dinode);
  2254. printk("bg_blkno: %llu\n",
  2255. (unsigned long long)bg->bg_blkno);
  2256. }
  2257. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
  2258. {
  2259. int i;
  2260. printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
  2261. printk("i_signature: %s\n", fe->i_signature);
  2262. printk("i_size: %llu\n",
  2263. (unsigned long long)fe->i_size);
  2264. printk("i_clusters: %u\n", fe->i_clusters);
  2265. printk("i_generation: %u\n",
  2266. le32_to_cpu(fe->i_generation));
  2267. printk("id1.bitmap1.i_used: %u\n",
  2268. le32_to_cpu(fe->id1.bitmap1.i_used));
  2269. printk("id1.bitmap1.i_total: %u\n",
  2270. le32_to_cpu(fe->id1.bitmap1.i_total));
  2271. printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
  2272. printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
  2273. printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
  2274. printk("id2.i_chain.cl_next_free_rec: %u\n",
  2275. fe->id2.i_chain.cl_next_free_rec);
  2276. for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
  2277. printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
  2278. fe->id2.i_chain.cl_recs[i].c_free);
  2279. printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
  2280. fe->id2.i_chain.cl_recs[i].c_total);
  2281. printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
  2282. (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
  2283. }
  2284. }
  2285. /*
  2286. * For a given allocation, determine which allocators will need to be
  2287. * accessed, and lock them, reserving the appropriate number of bits.
  2288. *
  2289. * Sparse file systems call this from ocfs2_write_begin_nolock()
  2290. * and ocfs2_allocate_unwritten_extents().
  2291. *
  2292. * File systems which don't support holes call this from
  2293. * ocfs2_extend_allocation().
  2294. */
  2295. int ocfs2_lock_allocators(struct inode *inode,
  2296. struct ocfs2_extent_tree *et,
  2297. u32 clusters_to_add, u32 extents_to_split,
  2298. struct ocfs2_alloc_context **data_ac,
  2299. struct ocfs2_alloc_context **meta_ac)
  2300. {
  2301. int ret = 0, num_free_extents;
  2302. unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
  2303. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2304. *meta_ac = NULL;
  2305. if (data_ac)
  2306. *data_ac = NULL;
  2307. BUG_ON(clusters_to_add != 0 && data_ac == NULL);
  2308. num_free_extents = ocfs2_num_free_extents(osb, et);
  2309. if (num_free_extents < 0) {
  2310. ret = num_free_extents;
  2311. mlog_errno(ret);
  2312. goto out;
  2313. }
  2314. /*
  2315. * Sparse allocation file systems need to be more conservative
  2316. * with reserving room for expansion - the actual allocation
  2317. * happens while we've got a journal handle open so re-taking
  2318. * a cluster lock (because we ran out of room for another
  2319. * extent) will violate ordering rules.
  2320. *
  2321. * Most of the time we'll only be seeing this 1 cluster at a time
  2322. * anyway.
  2323. *
  2324. * Always lock for any unwritten extents - we might want to
  2325. * add blocks during a split.
  2326. */
  2327. if (!num_free_extents ||
  2328. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
  2329. ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
  2330. if (ret < 0) {
  2331. if (ret != -ENOSPC)
  2332. mlog_errno(ret);
  2333. goto out;
  2334. }
  2335. }
  2336. if (clusters_to_add == 0)
  2337. goto out;
  2338. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  2339. if (ret < 0) {
  2340. if (ret != -ENOSPC)
  2341. mlog_errno(ret);
  2342. goto out;
  2343. }
  2344. out:
  2345. if (ret) {
  2346. if (*meta_ac) {
  2347. ocfs2_free_alloc_context(*meta_ac);
  2348. *meta_ac = NULL;
  2349. }
  2350. /*
  2351. * We cannot have an error and a non null *data_ac.
  2352. */
  2353. }
  2354. return ret;
  2355. }
  2356. /*
  2357. * Read the inode specified by blkno to get suballoc_slot and
  2358. * suballoc_bit.
  2359. */
  2360. static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
  2361. u16 *suballoc_slot, u64 *group_blkno,
  2362. u16 *suballoc_bit)
  2363. {
  2364. int status;
  2365. struct buffer_head *inode_bh = NULL;
  2366. struct ocfs2_dinode *inode_fe;
  2367. trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
  2368. /* dirty read disk */
  2369. status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
  2370. if (status < 0) {
  2371. mlog(ML_ERROR, "read block %llu failed %d\n",
  2372. (unsigned long long)blkno, status);
  2373. goto bail;
  2374. }
  2375. inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
  2376. if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
  2377. mlog(ML_ERROR, "invalid inode %llu requested\n",
  2378. (unsigned long long)blkno);
  2379. status = -EINVAL;
  2380. goto bail;
  2381. }
  2382. if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
  2383. (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
  2384. mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
  2385. (unsigned long long)blkno,
  2386. (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
  2387. status = -EINVAL;
  2388. goto bail;
  2389. }
  2390. if (suballoc_slot)
  2391. *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
  2392. if (suballoc_bit)
  2393. *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
  2394. if (group_blkno)
  2395. *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
  2396. bail:
  2397. brelse(inode_bh);
  2398. if (status)
  2399. mlog_errno(status);
  2400. return status;
  2401. }
  2402. /*
  2403. * test whether bit is SET in allocator bitmap or not. on success, 0
  2404. * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
  2405. * is returned and *res is meaningless. Call this after you have
  2406. * cluster locked against suballoc, or you may get a result based on
  2407. * non-up2date contents
  2408. */
  2409. static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
  2410. struct inode *suballoc,
  2411. struct buffer_head *alloc_bh,
  2412. u64 group_blkno, u64 blkno,
  2413. u16 bit, int *res)
  2414. {
  2415. struct ocfs2_dinode *alloc_di;
  2416. struct ocfs2_group_desc *group;
  2417. struct buffer_head *group_bh = NULL;
  2418. u64 bg_blkno;
  2419. int status;
  2420. trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
  2421. (unsigned int)bit);
  2422. alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
  2423. if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
  2424. mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
  2425. (unsigned int)bit,
  2426. ocfs2_bits_per_group(&alloc_di->id2.i_chain));
  2427. status = -EINVAL;
  2428. goto bail;
  2429. }
  2430. bg_blkno = group_blkno ? group_blkno :
  2431. ocfs2_which_suballoc_group(blkno, bit);
  2432. status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
  2433. &group_bh);
  2434. if (status < 0) {
  2435. mlog(ML_ERROR, "read group %llu failed %d\n",
  2436. (unsigned long long)bg_blkno, status);
  2437. goto bail;
  2438. }
  2439. group = (struct ocfs2_group_desc *) group_bh->b_data;
  2440. *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
  2441. bail:
  2442. brelse(group_bh);
  2443. if (status)
  2444. mlog_errno(status);
  2445. return status;
  2446. }
  2447. /*
  2448. * Test if the bit representing this inode (blkno) is set in the
  2449. * suballocator.
  2450. *
  2451. * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
  2452. *
  2453. * In the event of failure, a negative value is returned and *res is
  2454. * meaningless.
  2455. *
  2456. * Callers must make sure to hold nfs_sync_lock to prevent
  2457. * ocfs2_delete_inode() on another node from accessing the same
  2458. * suballocator concurrently.
  2459. */
  2460. int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
  2461. {
  2462. int status;
  2463. u64 group_blkno = 0;
  2464. u16 suballoc_bit = 0, suballoc_slot = 0;
  2465. struct inode *inode_alloc_inode;
  2466. struct buffer_head *alloc_bh = NULL;
  2467. trace_ocfs2_test_inode_bit((unsigned long long)blkno);
  2468. status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
  2469. &group_blkno, &suballoc_bit);
  2470. if (status < 0) {
  2471. mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
  2472. goto bail;
  2473. }
  2474. inode_alloc_inode =
  2475. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  2476. suballoc_slot);
  2477. if (!inode_alloc_inode) {
  2478. /* the error code could be inaccurate, but we are not able to
  2479. * get the correct one. */
  2480. status = -EINVAL;
  2481. mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
  2482. (u32)suballoc_slot);
  2483. goto bail;
  2484. }
  2485. mutex_lock(&inode_alloc_inode->i_mutex);
  2486. status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
  2487. if (status < 0) {
  2488. mutex_unlock(&inode_alloc_inode->i_mutex);
  2489. mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
  2490. (u32)suballoc_slot, status);
  2491. goto bail;
  2492. }
  2493. status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
  2494. group_blkno, blkno, suballoc_bit, res);
  2495. if (status < 0)
  2496. mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
  2497. ocfs2_inode_unlock(inode_alloc_inode, 0);
  2498. mutex_unlock(&inode_alloc_inode->i_mutex);
  2499. iput(inode_alloc_inode);
  2500. brelse(alloc_bh);
  2501. bail:
  2502. if (status)
  2503. mlog_errno(status);
  2504. return status;
  2505. }