suballoc.c 77 KB

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