suballoc.c 75 KB

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