suballoc.c 77 KB

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