suballoc.c 65 KB

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