suballoc.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  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. if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
  792. return 0;
  793. if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
  794. return 1;
  795. bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
  796. return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
  797. }
  798. static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
  799. struct buffer_head *bg_bh,
  800. unsigned int bits_wanted,
  801. unsigned int total_bits,
  802. u16 *bit_off,
  803. u16 *bits_found)
  804. {
  805. void *bitmap;
  806. u16 best_offset, best_size;
  807. int offset, start, found, status = 0;
  808. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  809. /* Callers got this descriptor from
  810. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  811. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  812. found = start = best_offset = best_size = 0;
  813. bitmap = bg->bg_bitmap;
  814. while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
  815. if (offset == total_bits)
  816. break;
  817. if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
  818. /* We found a zero, but we can't use it as it
  819. * hasn't been put to disk yet! */
  820. found = 0;
  821. start = offset + 1;
  822. } else if (offset == start) {
  823. /* we found a zero */
  824. found++;
  825. /* move start to the next bit to test */
  826. start++;
  827. } else {
  828. /* got a zero after some ones */
  829. found = 1;
  830. start = offset + 1;
  831. }
  832. if (found > best_size) {
  833. best_size = found;
  834. best_offset = start - found;
  835. }
  836. /* we got everything we needed */
  837. if (found == bits_wanted) {
  838. /* mlog(0, "Found it all!\n"); */
  839. break;
  840. }
  841. }
  842. /* XXX: I think the first clause is equivalent to the second
  843. * - jlbec */
  844. if (found == bits_wanted) {
  845. *bit_off = start - found;
  846. *bits_found = found;
  847. } else if (best_size) {
  848. *bit_off = best_offset;
  849. *bits_found = best_size;
  850. } else {
  851. status = -ENOSPC;
  852. /* No error log here -- see the comment above
  853. * ocfs2_test_bg_bit_allocatable */
  854. }
  855. return status;
  856. }
  857. static inline int ocfs2_block_group_set_bits(handle_t *handle,
  858. struct inode *alloc_inode,
  859. struct ocfs2_group_desc *bg,
  860. struct buffer_head *group_bh,
  861. unsigned int bit_off,
  862. unsigned int num_bits)
  863. {
  864. int status;
  865. void *bitmap = bg->bg_bitmap;
  866. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  867. mlog_entry_void();
  868. /* All callers get the descriptor via
  869. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  870. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  871. BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
  872. mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
  873. num_bits);
  874. if (ocfs2_is_cluster_bitmap(alloc_inode))
  875. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  876. status = ocfs2_journal_access_gd(handle,
  877. alloc_inode,
  878. group_bh,
  879. journal_type);
  880. if (status < 0) {
  881. mlog_errno(status);
  882. goto bail;
  883. }
  884. le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
  885. while(num_bits--)
  886. ocfs2_set_bit(bit_off++, bitmap);
  887. status = ocfs2_journal_dirty(handle,
  888. group_bh);
  889. if (status < 0) {
  890. mlog_errno(status);
  891. goto bail;
  892. }
  893. bail:
  894. mlog_exit(status);
  895. return status;
  896. }
  897. /* find the one with the most empty bits */
  898. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
  899. {
  900. u16 curr, best;
  901. BUG_ON(!cl->cl_next_free_rec);
  902. best = curr = 0;
  903. while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
  904. if (le32_to_cpu(cl->cl_recs[curr].c_free) >
  905. le32_to_cpu(cl->cl_recs[best].c_free))
  906. best = curr;
  907. curr++;
  908. }
  909. BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
  910. return best;
  911. }
  912. static int ocfs2_relink_block_group(handle_t *handle,
  913. struct inode *alloc_inode,
  914. struct buffer_head *fe_bh,
  915. struct buffer_head *bg_bh,
  916. struct buffer_head *prev_bg_bh,
  917. u16 chain)
  918. {
  919. int status;
  920. /* there is a really tiny chance the journal calls could fail,
  921. * but we wouldn't want inconsistent blocks in *any* case. */
  922. u64 fe_ptr, bg_ptr, prev_bg_ptr;
  923. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  924. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  925. struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
  926. /* The caller got these descriptors from
  927. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  928. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  929. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
  930. mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
  931. (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
  932. (unsigned long long)le64_to_cpu(bg->bg_blkno),
  933. (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
  934. fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
  935. bg_ptr = le64_to_cpu(bg->bg_next_group);
  936. prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
  937. status = ocfs2_journal_access_gd(handle, alloc_inode, prev_bg_bh,
  938. OCFS2_JOURNAL_ACCESS_WRITE);
  939. if (status < 0) {
  940. mlog_errno(status);
  941. goto out_rollback;
  942. }
  943. prev_bg->bg_next_group = bg->bg_next_group;
  944. status = ocfs2_journal_dirty(handle, prev_bg_bh);
  945. if (status < 0) {
  946. mlog_errno(status);
  947. goto out_rollback;
  948. }
  949. status = ocfs2_journal_access_gd(handle, alloc_inode, bg_bh,
  950. OCFS2_JOURNAL_ACCESS_WRITE);
  951. if (status < 0) {
  952. mlog_errno(status);
  953. goto out_rollback;
  954. }
  955. bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
  956. status = ocfs2_journal_dirty(handle, bg_bh);
  957. if (status < 0) {
  958. mlog_errno(status);
  959. goto out_rollback;
  960. }
  961. status = ocfs2_journal_access_di(handle, alloc_inode, fe_bh,
  962. OCFS2_JOURNAL_ACCESS_WRITE);
  963. if (status < 0) {
  964. mlog_errno(status);
  965. goto out_rollback;
  966. }
  967. fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
  968. status = ocfs2_journal_dirty(handle, fe_bh);
  969. if (status < 0) {
  970. mlog_errno(status);
  971. goto out_rollback;
  972. }
  973. status = 0;
  974. out_rollback:
  975. if (status < 0) {
  976. fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
  977. bg->bg_next_group = cpu_to_le64(bg_ptr);
  978. prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
  979. }
  980. mlog_exit(status);
  981. return status;
  982. }
  983. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  984. u32 wanted)
  985. {
  986. return le16_to_cpu(bg->bg_free_bits_count) > wanted;
  987. }
  988. /* return 0 on success, -ENOSPC to keep searching and any other < 0
  989. * value on error. */
  990. static int ocfs2_cluster_group_search(struct inode *inode,
  991. struct buffer_head *group_bh,
  992. u32 bits_wanted, u32 min_bits,
  993. u64 max_block,
  994. u16 *bit_off, u16 *bits_found)
  995. {
  996. int search = -ENOSPC;
  997. int ret;
  998. u64 blkoff;
  999. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
  1000. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1001. u16 tmp_off, tmp_found;
  1002. unsigned int max_bits, gd_cluster_off;
  1003. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1004. if (gd->bg_free_bits_count) {
  1005. max_bits = le16_to_cpu(gd->bg_bits);
  1006. /* Tail groups in cluster bitmaps which aren't cpg
  1007. * aligned are prone to partial extention by a failed
  1008. * fs resize. If the file system resize never got to
  1009. * update the dinode cluster count, then we don't want
  1010. * to trust any clusters past it, regardless of what
  1011. * the group descriptor says. */
  1012. gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
  1013. le64_to_cpu(gd->bg_blkno));
  1014. if ((gd_cluster_off + max_bits) >
  1015. OCFS2_I(inode)->ip_clusters) {
  1016. max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
  1017. mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
  1018. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  1019. le16_to_cpu(gd->bg_bits),
  1020. OCFS2_I(inode)->ip_clusters, max_bits);
  1021. }
  1022. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  1023. group_bh, bits_wanted,
  1024. max_bits,
  1025. &tmp_off, &tmp_found);
  1026. if (ret)
  1027. return ret;
  1028. if (max_block) {
  1029. blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
  1030. gd_cluster_off +
  1031. tmp_off + tmp_found);
  1032. mlog(0, "Checking %llu against %llu\n",
  1033. (unsigned long long)blkoff,
  1034. (unsigned long long)max_block);
  1035. if (blkoff > max_block)
  1036. return -ENOSPC;
  1037. }
  1038. /* ocfs2_block_group_find_clear_bits() might
  1039. * return success, but we still want to return
  1040. * -ENOSPC unless it found the minimum number
  1041. * of bits. */
  1042. if (min_bits <= tmp_found) {
  1043. *bit_off = tmp_off;
  1044. *bits_found = tmp_found;
  1045. search = 0; /* success */
  1046. } else if (tmp_found) {
  1047. /*
  1048. * Don't show bits which we'll be returning
  1049. * for allocation to the local alloc bitmap.
  1050. */
  1051. ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
  1052. }
  1053. }
  1054. return search;
  1055. }
  1056. static int ocfs2_block_group_search(struct inode *inode,
  1057. struct buffer_head *group_bh,
  1058. u32 bits_wanted, u32 min_bits,
  1059. u64 max_block,
  1060. u16 *bit_off, u16 *bits_found)
  1061. {
  1062. int ret = -ENOSPC;
  1063. u64 blkoff;
  1064. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1065. BUG_ON(min_bits != 1);
  1066. BUG_ON(ocfs2_is_cluster_bitmap(inode));
  1067. if (bg->bg_free_bits_count) {
  1068. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  1069. group_bh, bits_wanted,
  1070. le16_to_cpu(bg->bg_bits),
  1071. bit_off, bits_found);
  1072. if (!ret && max_block) {
  1073. blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
  1074. *bits_found;
  1075. mlog(0, "Checking %llu against %llu\n",
  1076. (unsigned long long)blkoff,
  1077. (unsigned long long)max_block);
  1078. if (blkoff > max_block)
  1079. ret = -ENOSPC;
  1080. }
  1081. }
  1082. return ret;
  1083. }
  1084. static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
  1085. handle_t *handle,
  1086. struct buffer_head *di_bh,
  1087. u32 num_bits,
  1088. u16 chain)
  1089. {
  1090. int ret;
  1091. u32 tmp_used;
  1092. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  1093. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
  1094. ret = ocfs2_journal_access_di(handle, inode, di_bh,
  1095. OCFS2_JOURNAL_ACCESS_WRITE);
  1096. if (ret < 0) {
  1097. mlog_errno(ret);
  1098. goto out;
  1099. }
  1100. tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
  1101. di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
  1102. le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
  1103. ret = ocfs2_journal_dirty(handle, di_bh);
  1104. if (ret < 0)
  1105. mlog_errno(ret);
  1106. out:
  1107. return ret;
  1108. }
  1109. static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
  1110. handle_t *handle,
  1111. u32 bits_wanted,
  1112. u32 min_bits,
  1113. u16 *bit_off,
  1114. unsigned int *num_bits,
  1115. u64 gd_blkno,
  1116. u16 *bits_left)
  1117. {
  1118. int ret;
  1119. u16 found;
  1120. struct buffer_head *group_bh = NULL;
  1121. struct ocfs2_group_desc *gd;
  1122. struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
  1123. struct inode *alloc_inode = ac->ac_inode;
  1124. ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
  1125. &group_bh);
  1126. if (ret < 0) {
  1127. mlog_errno(ret);
  1128. return ret;
  1129. }
  1130. gd = (struct ocfs2_group_desc *) group_bh->b_data;
  1131. ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
  1132. ac->ac_max_block, bit_off, &found);
  1133. if (ret < 0) {
  1134. if (ret != -ENOSPC)
  1135. mlog_errno(ret);
  1136. goto out;
  1137. }
  1138. *num_bits = found;
  1139. ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
  1140. *num_bits,
  1141. le16_to_cpu(gd->bg_chain));
  1142. if (ret < 0) {
  1143. mlog_errno(ret);
  1144. goto out;
  1145. }
  1146. ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
  1147. *bit_off, *num_bits);
  1148. if (ret < 0)
  1149. mlog_errno(ret);
  1150. *bits_left = le16_to_cpu(gd->bg_free_bits_count);
  1151. out:
  1152. brelse(group_bh);
  1153. return ret;
  1154. }
  1155. static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
  1156. handle_t *handle,
  1157. u32 bits_wanted,
  1158. u32 min_bits,
  1159. u16 *bit_off,
  1160. unsigned int *num_bits,
  1161. u64 *bg_blkno,
  1162. u16 *bits_left)
  1163. {
  1164. int status;
  1165. u16 chain, tmp_bits;
  1166. u32 tmp_used;
  1167. u64 next_group;
  1168. struct inode *alloc_inode = ac->ac_inode;
  1169. struct buffer_head *group_bh = NULL;
  1170. struct buffer_head *prev_group_bh = NULL;
  1171. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1172. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  1173. struct ocfs2_group_desc *bg;
  1174. chain = ac->ac_chain;
  1175. mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
  1176. bits_wanted, chain,
  1177. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
  1178. status = ocfs2_read_group_descriptor(alloc_inode, fe,
  1179. le64_to_cpu(cl->cl_recs[chain].c_blkno),
  1180. &group_bh);
  1181. if (status < 0) {
  1182. mlog_errno(status);
  1183. goto bail;
  1184. }
  1185. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1186. status = -ENOSPC;
  1187. /* for now, the chain search is a bit simplistic. We just use
  1188. * the 1st group with any empty bits. */
  1189. while ((status = ac->ac_group_search(alloc_inode, group_bh,
  1190. bits_wanted, min_bits,
  1191. ac->ac_max_block, bit_off,
  1192. &tmp_bits)) == -ENOSPC) {
  1193. if (!bg->bg_next_group)
  1194. break;
  1195. brelse(prev_group_bh);
  1196. prev_group_bh = NULL;
  1197. next_group = le64_to_cpu(bg->bg_next_group);
  1198. prev_group_bh = group_bh;
  1199. group_bh = NULL;
  1200. status = ocfs2_read_group_descriptor(alloc_inode, fe,
  1201. next_group, &group_bh);
  1202. if (status < 0) {
  1203. mlog_errno(status);
  1204. goto bail;
  1205. }
  1206. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1207. }
  1208. if (status < 0) {
  1209. if (status != -ENOSPC)
  1210. mlog_errno(status);
  1211. goto bail;
  1212. }
  1213. mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
  1214. tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
  1215. *num_bits = tmp_bits;
  1216. BUG_ON(*num_bits == 0);
  1217. /*
  1218. * Keep track of previous block descriptor read. When
  1219. * we find a target, if we have read more than X
  1220. * number of descriptors, and the target is reasonably
  1221. * empty, relink him to top of his chain.
  1222. *
  1223. * We've read 0 extra blocks and only send one more to
  1224. * the transaction, yet the next guy to search has a
  1225. * much easier time.
  1226. *
  1227. * Do this *after* figuring out how many bits we're taking out
  1228. * of our target group.
  1229. */
  1230. if (ac->ac_allow_chain_relink &&
  1231. (prev_group_bh) &&
  1232. (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
  1233. status = ocfs2_relink_block_group(handle, alloc_inode,
  1234. ac->ac_bh, group_bh,
  1235. prev_group_bh, chain);
  1236. if (status < 0) {
  1237. mlog_errno(status);
  1238. goto bail;
  1239. }
  1240. }
  1241. /* Ok, claim our bits now: set the info on dinode, chainlist
  1242. * and then the group */
  1243. status = ocfs2_journal_access_di(handle,
  1244. alloc_inode,
  1245. ac->ac_bh,
  1246. OCFS2_JOURNAL_ACCESS_WRITE);
  1247. if (status < 0) {
  1248. mlog_errno(status);
  1249. goto bail;
  1250. }
  1251. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  1252. fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
  1253. le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
  1254. status = ocfs2_journal_dirty(handle,
  1255. ac->ac_bh);
  1256. if (status < 0) {
  1257. mlog_errno(status);
  1258. goto bail;
  1259. }
  1260. status = ocfs2_block_group_set_bits(handle,
  1261. alloc_inode,
  1262. bg,
  1263. group_bh,
  1264. *bit_off,
  1265. *num_bits);
  1266. if (status < 0) {
  1267. mlog_errno(status);
  1268. goto bail;
  1269. }
  1270. mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
  1271. (unsigned long long)le64_to_cpu(fe->i_blkno));
  1272. *bg_blkno = le64_to_cpu(bg->bg_blkno);
  1273. *bits_left = le16_to_cpu(bg->bg_free_bits_count);
  1274. bail:
  1275. brelse(group_bh);
  1276. brelse(prev_group_bh);
  1277. mlog_exit(status);
  1278. return status;
  1279. }
  1280. /* will give out up to bits_wanted contiguous bits. */
  1281. static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
  1282. struct ocfs2_alloc_context *ac,
  1283. handle_t *handle,
  1284. u32 bits_wanted,
  1285. u32 min_bits,
  1286. u16 *bit_off,
  1287. unsigned int *num_bits,
  1288. u64 *bg_blkno)
  1289. {
  1290. int status;
  1291. u16 victim, i;
  1292. u16 bits_left = 0;
  1293. u64 hint_blkno = ac->ac_last_group;
  1294. struct ocfs2_chain_list *cl;
  1295. struct ocfs2_dinode *fe;
  1296. mlog_entry_void();
  1297. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1298. BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
  1299. BUG_ON(!ac->ac_bh);
  1300. fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1301. /* The bh was validated by the inode read during
  1302. * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
  1303. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  1304. if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
  1305. le32_to_cpu(fe->id1.bitmap1.i_total)) {
  1306. ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
  1307. "bits but only %u total.",
  1308. (unsigned long long)le64_to_cpu(fe->i_blkno),
  1309. le32_to_cpu(fe->id1.bitmap1.i_used),
  1310. le32_to_cpu(fe->id1.bitmap1.i_total));
  1311. status = -EIO;
  1312. goto bail;
  1313. }
  1314. if (hint_blkno) {
  1315. /* Attempt to short-circuit the usual search mechanism
  1316. * by jumping straight to the most recently used
  1317. * allocation group. This helps us mantain some
  1318. * contiguousness across allocations. */
  1319. status = ocfs2_search_one_group(ac, handle, bits_wanted,
  1320. min_bits, bit_off, num_bits,
  1321. hint_blkno, &bits_left);
  1322. if (!status) {
  1323. /* Be careful to update *bg_blkno here as the
  1324. * caller is expecting it to be filled in, and
  1325. * ocfs2_search_one_group() won't do that for
  1326. * us. */
  1327. *bg_blkno = hint_blkno;
  1328. goto set_hint;
  1329. }
  1330. if (status < 0 && status != -ENOSPC) {
  1331. mlog_errno(status);
  1332. goto bail;
  1333. }
  1334. }
  1335. cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  1336. victim = ocfs2_find_victim_chain(cl);
  1337. ac->ac_chain = victim;
  1338. ac->ac_allow_chain_relink = 1;
  1339. status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
  1340. num_bits, bg_blkno, &bits_left);
  1341. if (!status)
  1342. goto set_hint;
  1343. if (status < 0 && status != -ENOSPC) {
  1344. mlog_errno(status);
  1345. goto bail;
  1346. }
  1347. mlog(0, "Search of victim chain %u came up with nothing, "
  1348. "trying all chains now.\n", victim);
  1349. /* If we didn't pick a good victim, then just default to
  1350. * searching each chain in order. Don't allow chain relinking
  1351. * because we only calculate enough journal credits for one
  1352. * relink per alloc. */
  1353. ac->ac_allow_chain_relink = 0;
  1354. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
  1355. if (i == victim)
  1356. continue;
  1357. if (!cl->cl_recs[i].c_free)
  1358. continue;
  1359. ac->ac_chain = i;
  1360. status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
  1361. bit_off, num_bits, bg_blkno,
  1362. &bits_left);
  1363. if (!status)
  1364. break;
  1365. if (status < 0 && status != -ENOSPC) {
  1366. mlog_errno(status);
  1367. goto bail;
  1368. }
  1369. }
  1370. set_hint:
  1371. if (status != -ENOSPC) {
  1372. /* If the next search of this group is not likely to
  1373. * yield a suitable extent, then we reset the last
  1374. * group hint so as to not waste a disk read */
  1375. if (bits_left < min_bits)
  1376. ac->ac_last_group = 0;
  1377. else
  1378. ac->ac_last_group = *bg_blkno;
  1379. }
  1380. bail:
  1381. mlog_exit(status);
  1382. return status;
  1383. }
  1384. int ocfs2_claim_metadata(struct ocfs2_super *osb,
  1385. handle_t *handle,
  1386. struct ocfs2_alloc_context *ac,
  1387. u32 bits_wanted,
  1388. u16 *suballoc_bit_start,
  1389. unsigned int *num_bits,
  1390. u64 *blkno_start)
  1391. {
  1392. int status;
  1393. u64 bg_blkno;
  1394. BUG_ON(!ac);
  1395. BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
  1396. BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
  1397. status = ocfs2_claim_suballoc_bits(osb,
  1398. ac,
  1399. handle,
  1400. bits_wanted,
  1401. 1,
  1402. suballoc_bit_start,
  1403. num_bits,
  1404. &bg_blkno);
  1405. if (status < 0) {
  1406. mlog_errno(status);
  1407. goto bail;
  1408. }
  1409. atomic_inc(&osb->alloc_stats.bg_allocs);
  1410. *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
  1411. ac->ac_bits_given += (*num_bits);
  1412. status = 0;
  1413. bail:
  1414. mlog_exit(status);
  1415. return status;
  1416. }
  1417. static void ocfs2_init_inode_ac_group(struct inode *dir,
  1418. struct buffer_head *parent_fe_bh,
  1419. struct ocfs2_alloc_context *ac)
  1420. {
  1421. struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  1422. /*
  1423. * Try to allocate inodes from some specific group.
  1424. *
  1425. * If the parent dir has recorded the last group used in allocation,
  1426. * cool, use it. Otherwise if we try to allocate new inode from the
  1427. * same slot the parent dir belongs to, use the same chunk.
  1428. *
  1429. * We are very careful here to avoid the mistake of setting
  1430. * ac_last_group to a group descriptor from a different (unlocked) slot.
  1431. */
  1432. if (OCFS2_I(dir)->ip_last_used_group &&
  1433. OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
  1434. ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
  1435. else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
  1436. ac->ac_last_group = ocfs2_which_suballoc_group(
  1437. le64_to_cpu(fe->i_blkno),
  1438. le16_to_cpu(fe->i_suballoc_bit));
  1439. }
  1440. static inline void ocfs2_save_inode_ac_group(struct inode *dir,
  1441. struct ocfs2_alloc_context *ac)
  1442. {
  1443. OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
  1444. OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
  1445. }
  1446. int ocfs2_claim_new_inode(struct ocfs2_super *osb,
  1447. handle_t *handle,
  1448. struct inode *dir,
  1449. struct buffer_head *parent_fe_bh,
  1450. struct ocfs2_alloc_context *ac,
  1451. u16 *suballoc_bit,
  1452. u64 *fe_blkno)
  1453. {
  1454. int status;
  1455. unsigned int num_bits;
  1456. u64 bg_blkno;
  1457. mlog_entry_void();
  1458. BUG_ON(!ac);
  1459. BUG_ON(ac->ac_bits_given != 0);
  1460. BUG_ON(ac->ac_bits_wanted != 1);
  1461. BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
  1462. ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
  1463. status = ocfs2_claim_suballoc_bits(osb,
  1464. ac,
  1465. handle,
  1466. 1,
  1467. 1,
  1468. suballoc_bit,
  1469. &num_bits,
  1470. &bg_blkno);
  1471. if (status < 0) {
  1472. mlog_errno(status);
  1473. goto bail;
  1474. }
  1475. atomic_inc(&osb->alloc_stats.bg_allocs);
  1476. BUG_ON(num_bits != 1);
  1477. *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
  1478. ac->ac_bits_given++;
  1479. ocfs2_save_inode_ac_group(dir, ac);
  1480. status = 0;
  1481. bail:
  1482. mlog_exit(status);
  1483. return status;
  1484. }
  1485. /* translate a group desc. blkno and it's bitmap offset into
  1486. * disk cluster offset. */
  1487. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  1488. u64 bg_blkno,
  1489. u16 bg_bit_off)
  1490. {
  1491. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1492. u32 cluster = 0;
  1493. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1494. if (bg_blkno != osb->first_cluster_group_blkno)
  1495. cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
  1496. cluster += (u32) bg_bit_off;
  1497. return cluster;
  1498. }
  1499. /* given a cluster offset, calculate which block group it belongs to
  1500. * and return that block offset. */
  1501. u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
  1502. {
  1503. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1504. u32 group_no;
  1505. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1506. group_no = cluster / osb->bitmap_cpg;
  1507. if (!group_no)
  1508. return osb->first_cluster_group_blkno;
  1509. return ocfs2_clusters_to_blocks(inode->i_sb,
  1510. group_no * osb->bitmap_cpg);
  1511. }
  1512. /* given the block number of a cluster start, calculate which cluster
  1513. * group and descriptor bitmap offset that corresponds to. */
  1514. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  1515. u64 data_blkno,
  1516. u64 *bg_blkno,
  1517. u16 *bg_bit_off)
  1518. {
  1519. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1520. u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
  1521. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1522. *bg_blkno = ocfs2_which_cluster_group(inode,
  1523. data_cluster);
  1524. if (*bg_blkno == osb->first_cluster_group_blkno)
  1525. *bg_bit_off = (u16) data_cluster;
  1526. else
  1527. *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
  1528. data_blkno - *bg_blkno);
  1529. }
  1530. /*
  1531. * min_bits - minimum contiguous chunk from this total allocation we
  1532. * can handle. set to what we asked for originally for a full
  1533. * contig. allocation, set to '1' to indicate we can deal with extents
  1534. * of any size.
  1535. */
  1536. int __ocfs2_claim_clusters(struct ocfs2_super *osb,
  1537. handle_t *handle,
  1538. struct ocfs2_alloc_context *ac,
  1539. u32 min_clusters,
  1540. u32 max_clusters,
  1541. u32 *cluster_start,
  1542. u32 *num_clusters)
  1543. {
  1544. int status;
  1545. unsigned int bits_wanted = max_clusters;
  1546. u64 bg_blkno = 0;
  1547. u16 bg_bit_off;
  1548. mlog_entry_void();
  1549. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1550. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
  1551. && ac->ac_which != OCFS2_AC_USE_MAIN);
  1552. if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
  1553. status = ocfs2_claim_local_alloc_bits(osb,
  1554. handle,
  1555. ac,
  1556. bits_wanted,
  1557. cluster_start,
  1558. num_clusters);
  1559. if (!status)
  1560. atomic_inc(&osb->alloc_stats.local_data);
  1561. } else {
  1562. if (min_clusters > (osb->bitmap_cpg - 1)) {
  1563. /* The only paths asking for contiguousness
  1564. * should know about this already. */
  1565. mlog(ML_ERROR, "minimum allocation requested %u exceeds "
  1566. "group bitmap size %u!\n", min_clusters,
  1567. osb->bitmap_cpg);
  1568. status = -ENOSPC;
  1569. goto bail;
  1570. }
  1571. /* clamp the current request down to a realistic size. */
  1572. if (bits_wanted > (osb->bitmap_cpg - 1))
  1573. bits_wanted = osb->bitmap_cpg - 1;
  1574. status = ocfs2_claim_suballoc_bits(osb,
  1575. ac,
  1576. handle,
  1577. bits_wanted,
  1578. min_clusters,
  1579. &bg_bit_off,
  1580. num_clusters,
  1581. &bg_blkno);
  1582. if (!status) {
  1583. *cluster_start =
  1584. ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
  1585. bg_blkno,
  1586. bg_bit_off);
  1587. atomic_inc(&osb->alloc_stats.bitmap_data);
  1588. }
  1589. }
  1590. if (status < 0) {
  1591. if (status != -ENOSPC)
  1592. mlog_errno(status);
  1593. goto bail;
  1594. }
  1595. ac->ac_bits_given += *num_clusters;
  1596. bail:
  1597. mlog_exit(status);
  1598. return status;
  1599. }
  1600. int ocfs2_claim_clusters(struct ocfs2_super *osb,
  1601. handle_t *handle,
  1602. struct ocfs2_alloc_context *ac,
  1603. u32 min_clusters,
  1604. u32 *cluster_start,
  1605. u32 *num_clusters)
  1606. {
  1607. unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
  1608. return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
  1609. bits_wanted, cluster_start, num_clusters);
  1610. }
  1611. static inline int ocfs2_block_group_clear_bits(handle_t *handle,
  1612. struct inode *alloc_inode,
  1613. struct ocfs2_group_desc *bg,
  1614. struct buffer_head *group_bh,
  1615. unsigned int bit_off,
  1616. unsigned int num_bits)
  1617. {
  1618. int status;
  1619. unsigned int tmp;
  1620. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  1621. struct ocfs2_group_desc *undo_bg = NULL;
  1622. mlog_entry_void();
  1623. /* The caller got this descriptor from
  1624. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  1625. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  1626. mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
  1627. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1628. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  1629. status = ocfs2_journal_access_gd(handle, alloc_inode, group_bh,
  1630. journal_type);
  1631. if (status < 0) {
  1632. mlog_errno(status);
  1633. goto bail;
  1634. }
  1635. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1636. undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
  1637. tmp = num_bits;
  1638. while(tmp--) {
  1639. ocfs2_clear_bit((bit_off + tmp),
  1640. (unsigned long *) bg->bg_bitmap);
  1641. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1642. ocfs2_set_bit(bit_off + tmp,
  1643. (unsigned long *) undo_bg->bg_bitmap);
  1644. }
  1645. le16_add_cpu(&bg->bg_free_bits_count, num_bits);
  1646. status = ocfs2_journal_dirty(handle, group_bh);
  1647. if (status < 0)
  1648. mlog_errno(status);
  1649. bail:
  1650. return status;
  1651. }
  1652. /*
  1653. * expects the suballoc inode to already be locked.
  1654. */
  1655. int ocfs2_free_suballoc_bits(handle_t *handle,
  1656. struct inode *alloc_inode,
  1657. struct buffer_head *alloc_bh,
  1658. unsigned int start_bit,
  1659. u64 bg_blkno,
  1660. unsigned int count)
  1661. {
  1662. int status = 0;
  1663. u32 tmp_used;
  1664. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
  1665. struct ocfs2_chain_list *cl = &fe->id2.i_chain;
  1666. struct buffer_head *group_bh = NULL;
  1667. struct ocfs2_group_desc *group;
  1668. mlog_entry_void();
  1669. /* The alloc_bh comes from ocfs2_free_dinode() or
  1670. * ocfs2_free_clusters(). The callers have all locked the
  1671. * allocator and gotten alloc_bh from the lock call. This
  1672. * validates the dinode buffer. Any corruption that has happended
  1673. * is a code bug. */
  1674. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  1675. BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
  1676. mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
  1677. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
  1678. (unsigned long long)bg_blkno, start_bit);
  1679. status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
  1680. &group_bh);
  1681. if (status < 0) {
  1682. mlog_errno(status);
  1683. goto bail;
  1684. }
  1685. group = (struct ocfs2_group_desc *) group_bh->b_data;
  1686. BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
  1687. status = ocfs2_block_group_clear_bits(handle, alloc_inode,
  1688. group, group_bh,
  1689. start_bit, count);
  1690. if (status < 0) {
  1691. mlog_errno(status);
  1692. goto bail;
  1693. }
  1694. status = ocfs2_journal_access_di(handle, alloc_inode, alloc_bh,
  1695. OCFS2_JOURNAL_ACCESS_WRITE);
  1696. if (status < 0) {
  1697. mlog_errno(status);
  1698. goto bail;
  1699. }
  1700. le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
  1701. count);
  1702. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  1703. fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
  1704. status = ocfs2_journal_dirty(handle, alloc_bh);
  1705. if (status < 0) {
  1706. mlog_errno(status);
  1707. goto bail;
  1708. }
  1709. bail:
  1710. brelse(group_bh);
  1711. mlog_exit(status);
  1712. return status;
  1713. }
  1714. int ocfs2_free_dinode(handle_t *handle,
  1715. struct inode *inode_alloc_inode,
  1716. struct buffer_head *inode_alloc_bh,
  1717. struct ocfs2_dinode *di)
  1718. {
  1719. u64 blk = le64_to_cpu(di->i_blkno);
  1720. u16 bit = le16_to_cpu(di->i_suballoc_bit);
  1721. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1722. return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
  1723. inode_alloc_bh, bit, bg_blkno, 1);
  1724. }
  1725. int ocfs2_free_clusters(handle_t *handle,
  1726. struct inode *bitmap_inode,
  1727. struct buffer_head *bitmap_bh,
  1728. u64 start_blk,
  1729. unsigned int num_clusters)
  1730. {
  1731. int status;
  1732. u16 bg_start_bit;
  1733. u64 bg_blkno;
  1734. struct ocfs2_dinode *fe;
  1735. /* You can't ever have a contiguous set of clusters
  1736. * bigger than a block group bitmap so we never have to worry
  1737. * about looping on them. */
  1738. mlog_entry_void();
  1739. /* This is expensive. We can safely remove once this stuff has
  1740. * gotten tested really well. */
  1741. BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
  1742. fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
  1743. ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
  1744. &bg_start_bit);
  1745. mlog(0, "want to free %u clusters starting at block %llu\n",
  1746. num_clusters, (unsigned long long)start_blk);
  1747. mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
  1748. (unsigned long long)bg_blkno, bg_start_bit);
  1749. status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
  1750. bg_start_bit, bg_blkno,
  1751. num_clusters);
  1752. if (status < 0) {
  1753. mlog_errno(status);
  1754. goto out;
  1755. }
  1756. ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
  1757. num_clusters);
  1758. out:
  1759. mlog_exit(status);
  1760. return status;
  1761. }
  1762. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
  1763. {
  1764. printk("Block Group:\n");
  1765. printk("bg_signature: %s\n", bg->bg_signature);
  1766. printk("bg_size: %u\n", bg->bg_size);
  1767. printk("bg_bits: %u\n", bg->bg_bits);
  1768. printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
  1769. printk("bg_chain: %u\n", bg->bg_chain);
  1770. printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
  1771. printk("bg_next_group: %llu\n",
  1772. (unsigned long long)bg->bg_next_group);
  1773. printk("bg_parent_dinode: %llu\n",
  1774. (unsigned long long)bg->bg_parent_dinode);
  1775. printk("bg_blkno: %llu\n",
  1776. (unsigned long long)bg->bg_blkno);
  1777. }
  1778. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
  1779. {
  1780. int i;
  1781. printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
  1782. printk("i_signature: %s\n", fe->i_signature);
  1783. printk("i_size: %llu\n",
  1784. (unsigned long long)fe->i_size);
  1785. printk("i_clusters: %u\n", fe->i_clusters);
  1786. printk("i_generation: %u\n",
  1787. le32_to_cpu(fe->i_generation));
  1788. printk("id1.bitmap1.i_used: %u\n",
  1789. le32_to_cpu(fe->id1.bitmap1.i_used));
  1790. printk("id1.bitmap1.i_total: %u\n",
  1791. le32_to_cpu(fe->id1.bitmap1.i_total));
  1792. printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
  1793. printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
  1794. printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
  1795. printk("id2.i_chain.cl_next_free_rec: %u\n",
  1796. fe->id2.i_chain.cl_next_free_rec);
  1797. for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
  1798. printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
  1799. fe->id2.i_chain.cl_recs[i].c_free);
  1800. printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
  1801. fe->id2.i_chain.cl_recs[i].c_total);
  1802. printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
  1803. (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
  1804. }
  1805. }
  1806. /*
  1807. * For a given allocation, determine which allocators will need to be
  1808. * accessed, and lock them, reserving the appropriate number of bits.
  1809. *
  1810. * Sparse file systems call this from ocfs2_write_begin_nolock()
  1811. * and ocfs2_allocate_unwritten_extents().
  1812. *
  1813. * File systems which don't support holes call this from
  1814. * ocfs2_extend_allocation().
  1815. */
  1816. int ocfs2_lock_allocators(struct inode *inode,
  1817. struct ocfs2_extent_tree *et,
  1818. u32 clusters_to_add, u32 extents_to_split,
  1819. struct ocfs2_alloc_context **data_ac,
  1820. struct ocfs2_alloc_context **meta_ac)
  1821. {
  1822. int ret = 0, num_free_extents;
  1823. unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
  1824. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1825. *meta_ac = NULL;
  1826. if (data_ac)
  1827. *data_ac = NULL;
  1828. BUG_ON(clusters_to_add != 0 && data_ac == NULL);
  1829. num_free_extents = ocfs2_num_free_extents(osb, inode, et);
  1830. if (num_free_extents < 0) {
  1831. ret = num_free_extents;
  1832. mlog_errno(ret);
  1833. goto out;
  1834. }
  1835. /*
  1836. * Sparse allocation file systems need to be more conservative
  1837. * with reserving room for expansion - the actual allocation
  1838. * happens while we've got a journal handle open so re-taking
  1839. * a cluster lock (because we ran out of room for another
  1840. * extent) will violate ordering rules.
  1841. *
  1842. * Most of the time we'll only be seeing this 1 cluster at a time
  1843. * anyway.
  1844. *
  1845. * Always lock for any unwritten extents - we might want to
  1846. * add blocks during a split.
  1847. */
  1848. if (!num_free_extents ||
  1849. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
  1850. ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
  1851. if (ret < 0) {
  1852. if (ret != -ENOSPC)
  1853. mlog_errno(ret);
  1854. goto out;
  1855. }
  1856. }
  1857. if (clusters_to_add == 0)
  1858. goto out;
  1859. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  1860. if (ret < 0) {
  1861. if (ret != -ENOSPC)
  1862. mlog_errno(ret);
  1863. goto out;
  1864. }
  1865. out:
  1866. if (ret) {
  1867. if (*meta_ac) {
  1868. ocfs2_free_alloc_context(*meta_ac);
  1869. *meta_ac = NULL;
  1870. }
  1871. /*
  1872. * We cannot have an error and a non null *data_ac.
  1873. */
  1874. }
  1875. return ret;
  1876. }
  1877. /*
  1878. * Read the inode specified by blkno to get suballoc_slot and
  1879. * suballoc_bit.
  1880. */
  1881. static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
  1882. u16 *suballoc_slot, u16 *suballoc_bit)
  1883. {
  1884. int status;
  1885. struct buffer_head *inode_bh = NULL;
  1886. struct ocfs2_dinode *inode_fe;
  1887. mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
  1888. /* dirty read disk */
  1889. status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
  1890. if (status < 0) {
  1891. mlog(ML_ERROR, "read block %llu failed %d\n",
  1892. (unsigned long long)blkno, status);
  1893. goto bail;
  1894. }
  1895. inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
  1896. if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
  1897. mlog(ML_ERROR, "invalid inode %llu requested\n",
  1898. (unsigned long long)blkno);
  1899. status = -EINVAL;
  1900. goto bail;
  1901. }
  1902. if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
  1903. (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
  1904. mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
  1905. (unsigned long long)blkno,
  1906. (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
  1907. status = -EINVAL;
  1908. goto bail;
  1909. }
  1910. if (suballoc_slot)
  1911. *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
  1912. if (suballoc_bit)
  1913. *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
  1914. bail:
  1915. brelse(inode_bh);
  1916. mlog_exit(status);
  1917. return status;
  1918. }
  1919. /*
  1920. * test whether bit is SET in allocator bitmap or not. on success, 0
  1921. * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
  1922. * is returned and *res is meaningless. Call this after you have
  1923. * cluster locked against suballoc, or you may get a result based on
  1924. * non-up2date contents
  1925. */
  1926. static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
  1927. struct inode *suballoc,
  1928. struct buffer_head *alloc_bh, u64 blkno,
  1929. u16 bit, int *res)
  1930. {
  1931. struct ocfs2_dinode *alloc_fe;
  1932. struct ocfs2_group_desc *group;
  1933. struct buffer_head *group_bh = NULL;
  1934. u64 bg_blkno;
  1935. int status;
  1936. mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
  1937. (unsigned int)bit);
  1938. alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
  1939. if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
  1940. mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
  1941. (unsigned int)bit,
  1942. ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
  1943. status = -EINVAL;
  1944. goto bail;
  1945. }
  1946. bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
  1947. status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
  1948. &group_bh);
  1949. if (status < 0) {
  1950. mlog(ML_ERROR, "read group %llu failed %d\n",
  1951. (unsigned long long)bg_blkno, status);
  1952. goto bail;
  1953. }
  1954. group = (struct ocfs2_group_desc *) group_bh->b_data;
  1955. *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
  1956. bail:
  1957. brelse(group_bh);
  1958. mlog_exit(status);
  1959. return status;
  1960. }
  1961. /*
  1962. * Test if the bit representing this inode (blkno) is set in the
  1963. * suballocator.
  1964. *
  1965. * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
  1966. *
  1967. * In the event of failure, a negative value is returned and *res is
  1968. * meaningless.
  1969. *
  1970. * Callers must make sure to hold nfs_sync_lock to prevent
  1971. * ocfs2_delete_inode() on another node from accessing the same
  1972. * suballocator concurrently.
  1973. */
  1974. int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
  1975. {
  1976. int status;
  1977. u16 suballoc_bit = 0, suballoc_slot = 0;
  1978. struct inode *inode_alloc_inode;
  1979. struct buffer_head *alloc_bh = NULL;
  1980. mlog_entry("blkno: %llu", (unsigned long long)blkno);
  1981. status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
  1982. &suballoc_bit);
  1983. if (status < 0) {
  1984. mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
  1985. goto bail;
  1986. }
  1987. inode_alloc_inode =
  1988. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  1989. suballoc_slot);
  1990. if (!inode_alloc_inode) {
  1991. /* the error code could be inaccurate, but we are not able to
  1992. * get the correct one. */
  1993. status = -EINVAL;
  1994. mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
  1995. (u32)suballoc_slot);
  1996. goto bail;
  1997. }
  1998. mutex_lock(&inode_alloc_inode->i_mutex);
  1999. status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
  2000. if (status < 0) {
  2001. mutex_unlock(&inode_alloc_inode->i_mutex);
  2002. mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
  2003. (u32)suballoc_slot, status);
  2004. goto bail;
  2005. }
  2006. status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
  2007. blkno, suballoc_bit, res);
  2008. if (status < 0)
  2009. mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
  2010. ocfs2_inode_unlock(inode_alloc_inode, 0);
  2011. mutex_unlock(&inode_alloc_inode->i_mutex);
  2012. iput(inode_alloc_inode);
  2013. brelse(alloc_bh);
  2014. bail:
  2015. mlog_exit(status);
  2016. return status;
  2017. }