suballoc.c 62 KB

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