suballoc.c 73 KB

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