super.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215
  1. /*
  2. * linux/fs/ext4/super.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. */
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/fs.h>
  21. #include <linux/time.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/jbd2.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/parser.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/exportfs.h>
  31. #include <linux/vfs.h>
  32. #include <linux/random.h>
  33. #include <linux/mount.h>
  34. #include <linux/namei.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/ctype.h>
  39. #include <linux/log2.h>
  40. #include <linux/crc16.h>
  41. #include <asm/uaccess.h>
  42. #include "ext4.h"
  43. #include "ext4_jbd2.h"
  44. #include "xattr.h"
  45. #include "acl.h"
  46. #include "mballoc.h"
  47. #define CREATE_TRACE_POINTS
  48. #include <trace/events/ext4.h>
  49. struct proc_dir_entry *ext4_proc_root;
  50. static struct kset *ext4_kset;
  51. static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
  52. unsigned long journal_devnum);
  53. static int ext4_commit_super(struct super_block *sb, int sync);
  54. static void ext4_mark_recovery_complete(struct super_block *sb,
  55. struct ext4_super_block *es);
  56. static void ext4_clear_journal_err(struct super_block *sb,
  57. struct ext4_super_block *es);
  58. static int ext4_sync_fs(struct super_block *sb, int wait);
  59. static const char *ext4_decode_error(struct super_block *sb, int errno,
  60. char nbuf[16]);
  61. static int ext4_remount(struct super_block *sb, int *flags, char *data);
  62. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
  63. static int ext4_unfreeze(struct super_block *sb);
  64. static void ext4_write_super(struct super_block *sb);
  65. static int ext4_freeze(struct super_block *sb);
  66. static int ext4_get_sb(struct file_system_type *fs_type, int flags,
  67. const char *dev_name, void *data, struct vfsmount *mnt);
  68. #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  69. static struct file_system_type ext3_fs_type = {
  70. .owner = THIS_MODULE,
  71. .name = "ext3",
  72. .get_sb = ext4_get_sb,
  73. .kill_sb = kill_block_super,
  74. .fs_flags = FS_REQUIRES_DEV,
  75. };
  76. #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
  77. #else
  78. #define IS_EXT3_SB(sb) (0)
  79. #endif
  80. ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
  81. struct ext4_group_desc *bg)
  82. {
  83. return le32_to_cpu(bg->bg_block_bitmap_lo) |
  84. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  85. (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
  86. }
  87. ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
  88. struct ext4_group_desc *bg)
  89. {
  90. return le32_to_cpu(bg->bg_inode_bitmap_lo) |
  91. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  92. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
  93. }
  94. ext4_fsblk_t ext4_inode_table(struct super_block *sb,
  95. struct ext4_group_desc *bg)
  96. {
  97. return le32_to_cpu(bg->bg_inode_table_lo) |
  98. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  99. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
  100. }
  101. __u32 ext4_free_blks_count(struct super_block *sb,
  102. struct ext4_group_desc *bg)
  103. {
  104. return le16_to_cpu(bg->bg_free_blocks_count_lo) |
  105. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  106. (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
  107. }
  108. __u32 ext4_free_inodes_count(struct super_block *sb,
  109. struct ext4_group_desc *bg)
  110. {
  111. return le16_to_cpu(bg->bg_free_inodes_count_lo) |
  112. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  113. (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
  114. }
  115. __u32 ext4_used_dirs_count(struct super_block *sb,
  116. struct ext4_group_desc *bg)
  117. {
  118. return le16_to_cpu(bg->bg_used_dirs_count_lo) |
  119. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  120. (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
  121. }
  122. __u32 ext4_itable_unused_count(struct super_block *sb,
  123. struct ext4_group_desc *bg)
  124. {
  125. return le16_to_cpu(bg->bg_itable_unused_lo) |
  126. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  127. (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
  128. }
  129. void ext4_block_bitmap_set(struct super_block *sb,
  130. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  131. {
  132. bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
  133. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  134. bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
  135. }
  136. void ext4_inode_bitmap_set(struct super_block *sb,
  137. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  138. {
  139. bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
  140. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  141. bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
  142. }
  143. void ext4_inode_table_set(struct super_block *sb,
  144. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  145. {
  146. bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
  147. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  148. bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
  149. }
  150. void ext4_free_blks_set(struct super_block *sb,
  151. struct ext4_group_desc *bg, __u32 count)
  152. {
  153. bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
  154. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  155. bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
  156. }
  157. void ext4_free_inodes_set(struct super_block *sb,
  158. struct ext4_group_desc *bg, __u32 count)
  159. {
  160. bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
  161. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  162. bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
  163. }
  164. void ext4_used_dirs_set(struct super_block *sb,
  165. struct ext4_group_desc *bg, __u32 count)
  166. {
  167. bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
  168. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  169. bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
  170. }
  171. void ext4_itable_unused_set(struct super_block *sb,
  172. struct ext4_group_desc *bg, __u32 count)
  173. {
  174. bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
  175. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  176. bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
  177. }
  178. /* Just increment the non-pointer handle value */
  179. static handle_t *ext4_get_nojournal(void)
  180. {
  181. handle_t *handle = current->journal_info;
  182. unsigned long ref_cnt = (unsigned long)handle;
  183. BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
  184. ref_cnt++;
  185. handle = (handle_t *)ref_cnt;
  186. current->journal_info = handle;
  187. return handle;
  188. }
  189. /* Decrement the non-pointer handle value */
  190. static void ext4_put_nojournal(handle_t *handle)
  191. {
  192. unsigned long ref_cnt = (unsigned long)handle;
  193. BUG_ON(ref_cnt == 0);
  194. ref_cnt--;
  195. handle = (handle_t *)ref_cnt;
  196. current->journal_info = handle;
  197. }
  198. /*
  199. * Wrappers for jbd2_journal_start/end.
  200. *
  201. * The only special thing we need to do here is to make sure that all
  202. * journal_end calls result in the superblock being marked dirty, so
  203. * that sync() will call the filesystem's write_super callback if
  204. * appropriate.
  205. */
  206. handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
  207. {
  208. journal_t *journal;
  209. if (sb->s_flags & MS_RDONLY)
  210. return ERR_PTR(-EROFS);
  211. vfs_check_frozen(sb, SB_FREEZE_WRITE);
  212. /* Special case here: if the journal has aborted behind our
  213. * backs (eg. EIO in the commit thread), then we still need to
  214. * take the FS itself readonly cleanly. */
  215. journal = EXT4_SB(sb)->s_journal;
  216. if (journal) {
  217. if (is_journal_aborted(journal)) {
  218. ext4_abort(sb, __func__, "Detected aborted journal");
  219. return ERR_PTR(-EROFS);
  220. }
  221. return jbd2_journal_start(journal, nblocks);
  222. }
  223. return ext4_get_nojournal();
  224. }
  225. /*
  226. * The only special thing we need to do here is to make sure that all
  227. * jbd2_journal_stop calls result in the superblock being marked dirty, so
  228. * that sync() will call the filesystem's write_super callback if
  229. * appropriate.
  230. */
  231. int __ext4_journal_stop(const char *where, handle_t *handle)
  232. {
  233. struct super_block *sb;
  234. int err;
  235. int rc;
  236. if (!ext4_handle_valid(handle)) {
  237. ext4_put_nojournal(handle);
  238. return 0;
  239. }
  240. sb = handle->h_transaction->t_journal->j_private;
  241. err = handle->h_err;
  242. rc = jbd2_journal_stop(handle);
  243. if (!err)
  244. err = rc;
  245. if (err)
  246. __ext4_std_error(sb, where, err);
  247. return err;
  248. }
  249. void ext4_journal_abort_handle(const char *caller, const char *err_fn,
  250. struct buffer_head *bh, handle_t *handle, int err)
  251. {
  252. char nbuf[16];
  253. const char *errstr = ext4_decode_error(NULL, err, nbuf);
  254. BUG_ON(!ext4_handle_valid(handle));
  255. if (bh)
  256. BUFFER_TRACE(bh, "abort");
  257. if (!handle->h_err)
  258. handle->h_err = err;
  259. if (is_handle_aborted(handle))
  260. return;
  261. printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
  262. caller, errstr, err_fn);
  263. jbd2_journal_abort_handle(handle);
  264. }
  265. /* Deal with the reporting of failure conditions on a filesystem such as
  266. * inconsistencies detected or read IO failures.
  267. *
  268. * On ext2, we can store the error state of the filesystem in the
  269. * superblock. That is not possible on ext4, because we may have other
  270. * write ordering constraints on the superblock which prevent us from
  271. * writing it out straight away; and given that the journal is about to
  272. * be aborted, we can't rely on the current, or future, transactions to
  273. * write out the superblock safely.
  274. *
  275. * We'll just use the jbd2_journal_abort() error code to record an error in
  276. * the journal instead. On recovery, the journal will complain about
  277. * that error until we've noted it down and cleared it.
  278. */
  279. static void ext4_handle_error(struct super_block *sb)
  280. {
  281. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  282. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  283. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  284. if (sb->s_flags & MS_RDONLY)
  285. return;
  286. if (!test_opt(sb, ERRORS_CONT)) {
  287. journal_t *journal = EXT4_SB(sb)->s_journal;
  288. EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
  289. if (journal)
  290. jbd2_journal_abort(journal, -EIO);
  291. }
  292. if (test_opt(sb, ERRORS_RO)) {
  293. ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
  294. sb->s_flags |= MS_RDONLY;
  295. }
  296. ext4_commit_super(sb, 1);
  297. if (test_opt(sb, ERRORS_PANIC))
  298. panic("EXT4-fs (device %s): panic forced after error\n",
  299. sb->s_id);
  300. }
  301. void __ext4_error(struct super_block *sb, const char *function,
  302. const char *fmt, ...)
  303. {
  304. va_list args;
  305. va_start(args, fmt);
  306. printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
  307. vprintk(fmt, args);
  308. printk("\n");
  309. va_end(args);
  310. ext4_handle_error(sb);
  311. }
  312. void ext4_error_inode(const char *function, struct inode *inode,
  313. const char *fmt, ...)
  314. {
  315. va_list args;
  316. va_start(args, fmt);
  317. printk(KERN_CRIT "EXT4-fs error (device %s): %s: inode #%lu: (comm %s) ",
  318. inode->i_sb->s_id, function, inode->i_ino, current->comm);
  319. vprintk(fmt, args);
  320. printk("\n");
  321. va_end(args);
  322. ext4_handle_error(inode->i_sb);
  323. }
  324. void ext4_error_file(const char *function, struct file *file,
  325. const char *fmt, ...)
  326. {
  327. va_list args;
  328. struct inode *inode = file->f_dentry->d_inode;
  329. char pathname[80], *path;
  330. va_start(args, fmt);
  331. path = d_path(&(file->f_path), pathname, sizeof(pathname));
  332. if (!path)
  333. path = "(unknown)";
  334. printk(KERN_CRIT
  335. "EXT4-fs error (device %s): %s: inode #%lu (comm %s path %s): ",
  336. inode->i_sb->s_id, function, inode->i_ino, current->comm, path);
  337. vprintk(fmt, args);
  338. printk("\n");
  339. va_end(args);
  340. ext4_handle_error(inode->i_sb);
  341. }
  342. static const char *ext4_decode_error(struct super_block *sb, int errno,
  343. char nbuf[16])
  344. {
  345. char *errstr = NULL;
  346. switch (errno) {
  347. case -EIO:
  348. errstr = "IO failure";
  349. break;
  350. case -ENOMEM:
  351. errstr = "Out of memory";
  352. break;
  353. case -EROFS:
  354. if (!sb || (EXT4_SB(sb)->s_journal &&
  355. EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
  356. errstr = "Journal has aborted";
  357. else
  358. errstr = "Readonly filesystem";
  359. break;
  360. default:
  361. /* If the caller passed in an extra buffer for unknown
  362. * errors, textualise them now. Else we just return
  363. * NULL. */
  364. if (nbuf) {
  365. /* Check for truncated error codes... */
  366. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  367. errstr = nbuf;
  368. }
  369. break;
  370. }
  371. return errstr;
  372. }
  373. /* __ext4_std_error decodes expected errors from journaling functions
  374. * automatically and invokes the appropriate error response. */
  375. void __ext4_std_error(struct super_block *sb, const char *function, int errno)
  376. {
  377. char nbuf[16];
  378. const char *errstr;
  379. /* Special case: if the error is EROFS, and we're not already
  380. * inside a transaction, then there's really no point in logging
  381. * an error. */
  382. if (errno == -EROFS && journal_current_handle() == NULL &&
  383. (sb->s_flags & MS_RDONLY))
  384. return;
  385. errstr = ext4_decode_error(sb, errno, nbuf);
  386. printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
  387. sb->s_id, function, errstr);
  388. ext4_handle_error(sb);
  389. }
  390. /*
  391. * ext4_abort is a much stronger failure handler than ext4_error. The
  392. * abort function may be used to deal with unrecoverable failures such
  393. * as journal IO errors or ENOMEM at a critical moment in log management.
  394. *
  395. * We unconditionally force the filesystem into an ABORT|READONLY state,
  396. * unless the error response on the fs has been set to panic in which
  397. * case we take the easy way out and panic immediately.
  398. */
  399. void ext4_abort(struct super_block *sb, const char *function,
  400. const char *fmt, ...)
  401. {
  402. va_list args;
  403. va_start(args, fmt);
  404. printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
  405. vprintk(fmt, args);
  406. printk("\n");
  407. va_end(args);
  408. if (test_opt(sb, ERRORS_PANIC))
  409. panic("EXT4-fs panic from previous error\n");
  410. if (sb->s_flags & MS_RDONLY)
  411. return;
  412. ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
  413. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  414. sb->s_flags |= MS_RDONLY;
  415. EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
  416. if (EXT4_SB(sb)->s_journal)
  417. jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
  418. }
  419. void ext4_msg (struct super_block * sb, const char *prefix,
  420. const char *fmt, ...)
  421. {
  422. va_list args;
  423. va_start(args, fmt);
  424. printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
  425. vprintk(fmt, args);
  426. printk("\n");
  427. va_end(args);
  428. }
  429. void __ext4_warning(struct super_block *sb, const char *function,
  430. const char *fmt, ...)
  431. {
  432. va_list args;
  433. va_start(args, fmt);
  434. printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
  435. sb->s_id, function);
  436. vprintk(fmt, args);
  437. printk("\n");
  438. va_end(args);
  439. }
  440. void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
  441. const char *function, const char *fmt, ...)
  442. __releases(bitlock)
  443. __acquires(bitlock)
  444. {
  445. va_list args;
  446. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  447. va_start(args, fmt);
  448. printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
  449. vprintk(fmt, args);
  450. printk("\n");
  451. va_end(args);
  452. if (test_opt(sb, ERRORS_CONT)) {
  453. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  454. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  455. ext4_commit_super(sb, 0);
  456. return;
  457. }
  458. ext4_unlock_group(sb, grp);
  459. ext4_handle_error(sb);
  460. /*
  461. * We only get here in the ERRORS_RO case; relocking the group
  462. * may be dangerous, but nothing bad will happen since the
  463. * filesystem will have already been marked read/only and the
  464. * journal has been aborted. We return 1 as a hint to callers
  465. * who might what to use the return value from
  466. * ext4_grp_locked_error() to distinguish beween the
  467. * ERRORS_CONT and ERRORS_RO case, and perhaps return more
  468. * aggressively from the ext4 function in question, with a
  469. * more appropriate error code.
  470. */
  471. ext4_lock_group(sb, grp);
  472. return;
  473. }
  474. void ext4_update_dynamic_rev(struct super_block *sb)
  475. {
  476. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  477. if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
  478. return;
  479. ext4_warning(sb,
  480. "updating to rev %d because of new feature flag, "
  481. "running e2fsck is recommended",
  482. EXT4_DYNAMIC_REV);
  483. es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
  484. es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
  485. es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
  486. /* leave es->s_feature_*compat flags alone */
  487. /* es->s_uuid will be set by e2fsck if empty */
  488. /*
  489. * The rest of the superblock fields should be zero, and if not it
  490. * means they are likely already in use, so leave them alone. We
  491. * can leave it up to e2fsck to clean up any inconsistencies there.
  492. */
  493. }
  494. /*
  495. * Open the external journal device
  496. */
  497. static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
  498. {
  499. struct block_device *bdev;
  500. char b[BDEVNAME_SIZE];
  501. bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
  502. if (IS_ERR(bdev))
  503. goto fail;
  504. return bdev;
  505. fail:
  506. ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
  507. __bdevname(dev, b), PTR_ERR(bdev));
  508. return NULL;
  509. }
  510. /*
  511. * Release the journal device
  512. */
  513. static int ext4_blkdev_put(struct block_device *bdev)
  514. {
  515. bd_release(bdev);
  516. return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
  517. }
  518. static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
  519. {
  520. struct block_device *bdev;
  521. int ret = -ENODEV;
  522. bdev = sbi->journal_bdev;
  523. if (bdev) {
  524. ret = ext4_blkdev_put(bdev);
  525. sbi->journal_bdev = NULL;
  526. }
  527. return ret;
  528. }
  529. static inline struct inode *orphan_list_entry(struct list_head *l)
  530. {
  531. return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
  532. }
  533. static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
  534. {
  535. struct list_head *l;
  536. ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
  537. le32_to_cpu(sbi->s_es->s_last_orphan));
  538. printk(KERN_ERR "sb_info orphan list:\n");
  539. list_for_each(l, &sbi->s_orphan) {
  540. struct inode *inode = orphan_list_entry(l);
  541. printk(KERN_ERR " "
  542. "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
  543. inode->i_sb->s_id, inode->i_ino, inode,
  544. inode->i_mode, inode->i_nlink,
  545. NEXT_ORPHAN(inode));
  546. }
  547. }
  548. static void ext4_put_super(struct super_block *sb)
  549. {
  550. struct ext4_sb_info *sbi = EXT4_SB(sb);
  551. struct ext4_super_block *es = sbi->s_es;
  552. int i, err;
  553. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  554. flush_workqueue(sbi->dio_unwritten_wq);
  555. destroy_workqueue(sbi->dio_unwritten_wq);
  556. lock_super(sb);
  557. lock_kernel();
  558. if (sb->s_dirt)
  559. ext4_commit_super(sb, 1);
  560. if (sbi->s_journal) {
  561. err = jbd2_journal_destroy(sbi->s_journal);
  562. sbi->s_journal = NULL;
  563. if (err < 0)
  564. ext4_abort(sb, __func__,
  565. "Couldn't clean up the journal");
  566. }
  567. ext4_release_system_zone(sb);
  568. ext4_mb_release(sb);
  569. ext4_ext_release(sb);
  570. ext4_xattr_put_super(sb);
  571. if (!(sb->s_flags & MS_RDONLY)) {
  572. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  573. es->s_state = cpu_to_le16(sbi->s_mount_state);
  574. ext4_commit_super(sb, 1);
  575. }
  576. if (sbi->s_proc) {
  577. remove_proc_entry(sb->s_id, ext4_proc_root);
  578. }
  579. kobject_del(&sbi->s_kobj);
  580. for (i = 0; i < sbi->s_gdb_count; i++)
  581. brelse(sbi->s_group_desc[i]);
  582. kfree(sbi->s_group_desc);
  583. if (is_vmalloc_addr(sbi->s_flex_groups))
  584. vfree(sbi->s_flex_groups);
  585. else
  586. kfree(sbi->s_flex_groups);
  587. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  588. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  589. percpu_counter_destroy(&sbi->s_dirs_counter);
  590. percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
  591. brelse(sbi->s_sbh);
  592. #ifdef CONFIG_QUOTA
  593. for (i = 0; i < MAXQUOTAS; i++)
  594. kfree(sbi->s_qf_names[i]);
  595. #endif
  596. /* Debugging code just in case the in-memory inode orphan list
  597. * isn't empty. The on-disk one can be non-empty if we've
  598. * detected an error and taken the fs readonly, but the
  599. * in-memory list had better be clean by this point. */
  600. if (!list_empty(&sbi->s_orphan))
  601. dump_orphan_list(sb, sbi);
  602. J_ASSERT(list_empty(&sbi->s_orphan));
  603. invalidate_bdev(sb->s_bdev);
  604. if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
  605. /*
  606. * Invalidate the journal device's buffers. We don't want them
  607. * floating about in memory - the physical journal device may
  608. * hotswapped, and it breaks the `ro-after' testing code.
  609. */
  610. sync_blockdev(sbi->journal_bdev);
  611. invalidate_bdev(sbi->journal_bdev);
  612. ext4_blkdev_remove(sbi);
  613. }
  614. sb->s_fs_info = NULL;
  615. /*
  616. * Now that we are completely done shutting down the
  617. * superblock, we need to actually destroy the kobject.
  618. */
  619. unlock_kernel();
  620. unlock_super(sb);
  621. kobject_put(&sbi->s_kobj);
  622. wait_for_completion(&sbi->s_kobj_unregister);
  623. kfree(sbi->s_blockgroup_lock);
  624. kfree(sbi);
  625. }
  626. static struct kmem_cache *ext4_inode_cachep;
  627. /*
  628. * Called inside transaction, so use GFP_NOFS
  629. */
  630. static struct inode *ext4_alloc_inode(struct super_block *sb)
  631. {
  632. struct ext4_inode_info *ei;
  633. ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
  634. if (!ei)
  635. return NULL;
  636. ei->vfs_inode.i_version = 1;
  637. ei->vfs_inode.i_data.writeback_index = 0;
  638. memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
  639. INIT_LIST_HEAD(&ei->i_prealloc_list);
  640. spin_lock_init(&ei->i_prealloc_lock);
  641. /*
  642. * Note: We can be called before EXT4_SB(sb)->s_journal is set,
  643. * therefore it can be null here. Don't check it, just initialize
  644. * jinode.
  645. */
  646. jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
  647. ei->i_reserved_data_blocks = 0;
  648. ei->i_reserved_meta_blocks = 0;
  649. ei->i_allocated_meta_blocks = 0;
  650. ei->i_da_metadata_calc_len = 0;
  651. ei->i_delalloc_reserved_flag = 0;
  652. spin_lock_init(&(ei->i_block_reservation_lock));
  653. #ifdef CONFIG_QUOTA
  654. ei->i_reserved_quota = 0;
  655. #endif
  656. INIT_LIST_HEAD(&ei->i_completed_io_list);
  657. spin_lock_init(&ei->i_completed_io_lock);
  658. ei->cur_aio_dio = NULL;
  659. ei->i_sync_tid = 0;
  660. ei->i_datasync_tid = 0;
  661. return &ei->vfs_inode;
  662. }
  663. static void ext4_destroy_inode(struct inode *inode)
  664. {
  665. if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
  666. ext4_msg(inode->i_sb, KERN_ERR,
  667. "Inode %lu (%p): orphan list check failed!",
  668. inode->i_ino, EXT4_I(inode));
  669. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
  670. EXT4_I(inode), sizeof(struct ext4_inode_info),
  671. true);
  672. dump_stack();
  673. }
  674. kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
  675. }
  676. static void init_once(void *foo)
  677. {
  678. struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
  679. INIT_LIST_HEAD(&ei->i_orphan);
  680. #ifdef CONFIG_EXT4_FS_XATTR
  681. init_rwsem(&ei->xattr_sem);
  682. #endif
  683. init_rwsem(&ei->i_data_sem);
  684. inode_init_once(&ei->vfs_inode);
  685. }
  686. static int init_inodecache(void)
  687. {
  688. ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
  689. sizeof(struct ext4_inode_info),
  690. 0, (SLAB_RECLAIM_ACCOUNT|
  691. SLAB_MEM_SPREAD),
  692. init_once);
  693. if (ext4_inode_cachep == NULL)
  694. return -ENOMEM;
  695. return 0;
  696. }
  697. static void destroy_inodecache(void)
  698. {
  699. kmem_cache_destroy(ext4_inode_cachep);
  700. }
  701. static void ext4_clear_inode(struct inode *inode)
  702. {
  703. dquot_drop(inode);
  704. ext4_discard_preallocations(inode);
  705. if (EXT4_JOURNAL(inode))
  706. jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
  707. &EXT4_I(inode)->jinode);
  708. }
  709. static inline void ext4_show_quota_options(struct seq_file *seq,
  710. struct super_block *sb)
  711. {
  712. #if defined(CONFIG_QUOTA)
  713. struct ext4_sb_info *sbi = EXT4_SB(sb);
  714. if (sbi->s_jquota_fmt) {
  715. char *fmtname = "";
  716. switch (sbi->s_jquota_fmt) {
  717. case QFMT_VFS_OLD:
  718. fmtname = "vfsold";
  719. break;
  720. case QFMT_VFS_V0:
  721. fmtname = "vfsv0";
  722. break;
  723. case QFMT_VFS_V1:
  724. fmtname = "vfsv1";
  725. break;
  726. }
  727. seq_printf(seq, ",jqfmt=%s", fmtname);
  728. }
  729. if (sbi->s_qf_names[USRQUOTA])
  730. seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
  731. if (sbi->s_qf_names[GRPQUOTA])
  732. seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
  733. if (test_opt(sb, USRQUOTA))
  734. seq_puts(seq, ",usrquota");
  735. if (test_opt(sb, GRPQUOTA))
  736. seq_puts(seq, ",grpquota");
  737. #endif
  738. }
  739. /*
  740. * Show an option if
  741. * - it's set to a non-default value OR
  742. * - if the per-sb default is different from the global default
  743. */
  744. static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
  745. {
  746. int def_errors;
  747. unsigned long def_mount_opts;
  748. struct super_block *sb = vfs->mnt_sb;
  749. struct ext4_sb_info *sbi = EXT4_SB(sb);
  750. struct ext4_super_block *es = sbi->s_es;
  751. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  752. def_errors = le16_to_cpu(es->s_errors);
  753. if (sbi->s_sb_block != 1)
  754. seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
  755. if (test_opt(sb, MINIX_DF))
  756. seq_puts(seq, ",minixdf");
  757. if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
  758. seq_puts(seq, ",grpid");
  759. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
  760. seq_puts(seq, ",nogrpid");
  761. if (sbi->s_resuid != EXT4_DEF_RESUID ||
  762. le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
  763. seq_printf(seq, ",resuid=%u", sbi->s_resuid);
  764. }
  765. if (sbi->s_resgid != EXT4_DEF_RESGID ||
  766. le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
  767. seq_printf(seq, ",resgid=%u", sbi->s_resgid);
  768. }
  769. if (test_opt(sb, ERRORS_RO)) {
  770. if (def_errors == EXT4_ERRORS_PANIC ||
  771. def_errors == EXT4_ERRORS_CONTINUE) {
  772. seq_puts(seq, ",errors=remount-ro");
  773. }
  774. }
  775. if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
  776. seq_puts(seq, ",errors=continue");
  777. if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
  778. seq_puts(seq, ",errors=panic");
  779. if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
  780. seq_puts(seq, ",nouid32");
  781. if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
  782. seq_puts(seq, ",debug");
  783. if (test_opt(sb, OLDALLOC))
  784. seq_puts(seq, ",oldalloc");
  785. #ifdef CONFIG_EXT4_FS_XATTR
  786. if (test_opt(sb, XATTR_USER) &&
  787. !(def_mount_opts & EXT4_DEFM_XATTR_USER))
  788. seq_puts(seq, ",user_xattr");
  789. if (!test_opt(sb, XATTR_USER) &&
  790. (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
  791. seq_puts(seq, ",nouser_xattr");
  792. }
  793. #endif
  794. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  795. if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
  796. seq_puts(seq, ",acl");
  797. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
  798. seq_puts(seq, ",noacl");
  799. #endif
  800. if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
  801. seq_printf(seq, ",commit=%u",
  802. (unsigned) (sbi->s_commit_interval / HZ));
  803. }
  804. if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
  805. seq_printf(seq, ",min_batch_time=%u",
  806. (unsigned) sbi->s_min_batch_time);
  807. }
  808. if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
  809. seq_printf(seq, ",max_batch_time=%u",
  810. (unsigned) sbi->s_min_batch_time);
  811. }
  812. /*
  813. * We're changing the default of barrier mount option, so
  814. * let's always display its mount state so it's clear what its
  815. * status is.
  816. */
  817. seq_puts(seq, ",barrier=");
  818. seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
  819. if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
  820. seq_puts(seq, ",journal_async_commit");
  821. else if (test_opt(sb, JOURNAL_CHECKSUM))
  822. seq_puts(seq, ",journal_checksum");
  823. if (test_opt(sb, NOBH))
  824. seq_puts(seq, ",nobh");
  825. if (test_opt(sb, I_VERSION))
  826. seq_puts(seq, ",i_version");
  827. if (!test_opt(sb, DELALLOC))
  828. seq_puts(seq, ",nodelalloc");
  829. if (sbi->s_stripe)
  830. seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
  831. /*
  832. * journal mode get enabled in different ways
  833. * So just print the value even if we didn't specify it
  834. */
  835. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  836. seq_puts(seq, ",data=journal");
  837. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  838. seq_puts(seq, ",data=ordered");
  839. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
  840. seq_puts(seq, ",data=writeback");
  841. if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
  842. seq_printf(seq, ",inode_readahead_blks=%u",
  843. sbi->s_inode_readahead_blks);
  844. if (test_opt(sb, DATA_ERR_ABORT))
  845. seq_puts(seq, ",data_err=abort");
  846. if (test_opt(sb, NO_AUTO_DA_ALLOC))
  847. seq_puts(seq, ",noauto_da_alloc");
  848. if (test_opt(sb, DISCARD))
  849. seq_puts(seq, ",discard");
  850. if (test_opt(sb, NOLOAD))
  851. seq_puts(seq, ",norecovery");
  852. if (test_opt(sb, DIOREAD_NOLOCK))
  853. seq_puts(seq, ",dioread_nolock");
  854. ext4_show_quota_options(seq, sb);
  855. return 0;
  856. }
  857. static struct inode *ext4_nfs_get_inode(struct super_block *sb,
  858. u64 ino, u32 generation)
  859. {
  860. struct inode *inode;
  861. if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
  862. return ERR_PTR(-ESTALE);
  863. if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
  864. return ERR_PTR(-ESTALE);
  865. /* iget isn't really right if the inode is currently unallocated!!
  866. *
  867. * ext4_read_inode will return a bad_inode if the inode had been
  868. * deleted, so we should be safe.
  869. *
  870. * Currently we don't know the generation for parent directory, so
  871. * a generation of 0 means "accept any"
  872. */
  873. inode = ext4_iget(sb, ino);
  874. if (IS_ERR(inode))
  875. return ERR_CAST(inode);
  876. if (generation && inode->i_generation != generation) {
  877. iput(inode);
  878. return ERR_PTR(-ESTALE);
  879. }
  880. return inode;
  881. }
  882. static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
  883. int fh_len, int fh_type)
  884. {
  885. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  886. ext4_nfs_get_inode);
  887. }
  888. static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
  889. int fh_len, int fh_type)
  890. {
  891. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  892. ext4_nfs_get_inode);
  893. }
  894. /*
  895. * Try to release metadata pages (indirect blocks, directories) which are
  896. * mapped via the block device. Since these pages could have journal heads
  897. * which would prevent try_to_free_buffers() from freeing them, we must use
  898. * jbd2 layer's try_to_free_buffers() function to release them.
  899. */
  900. static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
  901. gfp_t wait)
  902. {
  903. journal_t *journal = EXT4_SB(sb)->s_journal;
  904. WARN_ON(PageChecked(page));
  905. if (!page_has_buffers(page))
  906. return 0;
  907. if (journal)
  908. return jbd2_journal_try_to_free_buffers(journal, page,
  909. wait & ~__GFP_WAIT);
  910. return try_to_free_buffers(page);
  911. }
  912. #ifdef CONFIG_QUOTA
  913. #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
  914. #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
  915. static int ext4_write_dquot(struct dquot *dquot);
  916. static int ext4_acquire_dquot(struct dquot *dquot);
  917. static int ext4_release_dquot(struct dquot *dquot);
  918. static int ext4_mark_dquot_dirty(struct dquot *dquot);
  919. static int ext4_write_info(struct super_block *sb, int type);
  920. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  921. char *path);
  922. static int ext4_quota_on_mount(struct super_block *sb, int type);
  923. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  924. size_t len, loff_t off);
  925. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  926. const char *data, size_t len, loff_t off);
  927. static const struct dquot_operations ext4_quota_operations = {
  928. #ifdef CONFIG_QUOTA
  929. .get_reserved_space = ext4_get_reserved_space,
  930. #endif
  931. .write_dquot = ext4_write_dquot,
  932. .acquire_dquot = ext4_acquire_dquot,
  933. .release_dquot = ext4_release_dquot,
  934. .mark_dirty = ext4_mark_dquot_dirty,
  935. .write_info = ext4_write_info,
  936. .alloc_dquot = dquot_alloc,
  937. .destroy_dquot = dquot_destroy,
  938. };
  939. static const struct quotactl_ops ext4_qctl_operations = {
  940. .quota_on = ext4_quota_on,
  941. .quota_off = dquot_quota_off,
  942. .quota_sync = dquot_quota_sync,
  943. .get_info = dquot_get_dqinfo,
  944. .set_info = dquot_set_dqinfo,
  945. .get_dqblk = dquot_get_dqblk,
  946. .set_dqblk = dquot_set_dqblk
  947. };
  948. #endif
  949. static const struct super_operations ext4_sops = {
  950. .alloc_inode = ext4_alloc_inode,
  951. .destroy_inode = ext4_destroy_inode,
  952. .write_inode = ext4_write_inode,
  953. .dirty_inode = ext4_dirty_inode,
  954. .delete_inode = ext4_delete_inode,
  955. .put_super = ext4_put_super,
  956. .sync_fs = ext4_sync_fs,
  957. .freeze_fs = ext4_freeze,
  958. .unfreeze_fs = ext4_unfreeze,
  959. .statfs = ext4_statfs,
  960. .remount_fs = ext4_remount,
  961. .clear_inode = ext4_clear_inode,
  962. .show_options = ext4_show_options,
  963. #ifdef CONFIG_QUOTA
  964. .quota_read = ext4_quota_read,
  965. .quota_write = ext4_quota_write,
  966. #endif
  967. .bdev_try_to_free_page = bdev_try_to_free_page,
  968. };
  969. static const struct super_operations ext4_nojournal_sops = {
  970. .alloc_inode = ext4_alloc_inode,
  971. .destroy_inode = ext4_destroy_inode,
  972. .write_inode = ext4_write_inode,
  973. .dirty_inode = ext4_dirty_inode,
  974. .delete_inode = ext4_delete_inode,
  975. .write_super = ext4_write_super,
  976. .put_super = ext4_put_super,
  977. .statfs = ext4_statfs,
  978. .remount_fs = ext4_remount,
  979. .clear_inode = ext4_clear_inode,
  980. .show_options = ext4_show_options,
  981. #ifdef CONFIG_QUOTA
  982. .quota_read = ext4_quota_read,
  983. .quota_write = ext4_quota_write,
  984. #endif
  985. .bdev_try_to_free_page = bdev_try_to_free_page,
  986. };
  987. static const struct export_operations ext4_export_ops = {
  988. .fh_to_dentry = ext4_fh_to_dentry,
  989. .fh_to_parent = ext4_fh_to_parent,
  990. .get_parent = ext4_get_parent,
  991. };
  992. enum {
  993. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  994. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
  995. Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
  996. Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
  997. Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
  998. Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
  999. Opt_journal_update, Opt_journal_dev,
  1000. Opt_journal_checksum, Opt_journal_async_commit,
  1001. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  1002. Opt_data_err_abort, Opt_data_err_ignore,
  1003. Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
  1004. Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
  1005. Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
  1006. Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
  1007. Opt_stripe, Opt_delalloc, Opt_nodelalloc,
  1008. Opt_block_validity, Opt_noblock_validity,
  1009. Opt_inode_readahead_blks, Opt_journal_ioprio,
  1010. Opt_dioread_nolock, Opt_dioread_lock,
  1011. Opt_discard, Opt_nodiscard,
  1012. };
  1013. static const match_table_t tokens = {
  1014. {Opt_bsd_df, "bsddf"},
  1015. {Opt_minix_df, "minixdf"},
  1016. {Opt_grpid, "grpid"},
  1017. {Opt_grpid, "bsdgroups"},
  1018. {Opt_nogrpid, "nogrpid"},
  1019. {Opt_nogrpid, "sysvgroups"},
  1020. {Opt_resgid, "resgid=%u"},
  1021. {Opt_resuid, "resuid=%u"},
  1022. {Opt_sb, "sb=%u"},
  1023. {Opt_err_cont, "errors=continue"},
  1024. {Opt_err_panic, "errors=panic"},
  1025. {Opt_err_ro, "errors=remount-ro"},
  1026. {Opt_nouid32, "nouid32"},
  1027. {Opt_debug, "debug"},
  1028. {Opt_oldalloc, "oldalloc"},
  1029. {Opt_orlov, "orlov"},
  1030. {Opt_user_xattr, "user_xattr"},
  1031. {Opt_nouser_xattr, "nouser_xattr"},
  1032. {Opt_acl, "acl"},
  1033. {Opt_noacl, "noacl"},
  1034. {Opt_noload, "noload"},
  1035. {Opt_noload, "norecovery"},
  1036. {Opt_nobh, "nobh"},
  1037. {Opt_bh, "bh"},
  1038. {Opt_commit, "commit=%u"},
  1039. {Opt_min_batch_time, "min_batch_time=%u"},
  1040. {Opt_max_batch_time, "max_batch_time=%u"},
  1041. {Opt_journal_update, "journal=update"},
  1042. {Opt_journal_dev, "journal_dev=%u"},
  1043. {Opt_journal_checksum, "journal_checksum"},
  1044. {Opt_journal_async_commit, "journal_async_commit"},
  1045. {Opt_abort, "abort"},
  1046. {Opt_data_journal, "data=journal"},
  1047. {Opt_data_ordered, "data=ordered"},
  1048. {Opt_data_writeback, "data=writeback"},
  1049. {Opt_data_err_abort, "data_err=abort"},
  1050. {Opt_data_err_ignore, "data_err=ignore"},
  1051. {Opt_offusrjquota, "usrjquota="},
  1052. {Opt_usrjquota, "usrjquota=%s"},
  1053. {Opt_offgrpjquota, "grpjquota="},
  1054. {Opt_grpjquota, "grpjquota=%s"},
  1055. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  1056. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  1057. {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
  1058. {Opt_grpquota, "grpquota"},
  1059. {Opt_noquota, "noquota"},
  1060. {Opt_quota, "quota"},
  1061. {Opt_usrquota, "usrquota"},
  1062. {Opt_barrier, "barrier=%u"},
  1063. {Opt_barrier, "barrier"},
  1064. {Opt_nobarrier, "nobarrier"},
  1065. {Opt_i_version, "i_version"},
  1066. {Opt_stripe, "stripe=%u"},
  1067. {Opt_resize, "resize"},
  1068. {Opt_delalloc, "delalloc"},
  1069. {Opt_nodelalloc, "nodelalloc"},
  1070. {Opt_block_validity, "block_validity"},
  1071. {Opt_noblock_validity, "noblock_validity"},
  1072. {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
  1073. {Opt_journal_ioprio, "journal_ioprio=%u"},
  1074. {Opt_auto_da_alloc, "auto_da_alloc=%u"},
  1075. {Opt_auto_da_alloc, "auto_da_alloc"},
  1076. {Opt_noauto_da_alloc, "noauto_da_alloc"},
  1077. {Opt_dioread_nolock, "dioread_nolock"},
  1078. {Opt_dioread_lock, "dioread_lock"},
  1079. {Opt_discard, "discard"},
  1080. {Opt_nodiscard, "nodiscard"},
  1081. {Opt_err, NULL},
  1082. };
  1083. static ext4_fsblk_t get_sb_block(void **data)
  1084. {
  1085. ext4_fsblk_t sb_block;
  1086. char *options = (char *) *data;
  1087. if (!options || strncmp(options, "sb=", 3) != 0)
  1088. return 1; /* Default location */
  1089. options += 3;
  1090. /* TODO: use simple_strtoll with >32bit ext4 */
  1091. sb_block = simple_strtoul(options, &options, 0);
  1092. if (*options && *options != ',') {
  1093. printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
  1094. (char *) *data);
  1095. return 1;
  1096. }
  1097. if (*options == ',')
  1098. options++;
  1099. *data = (void *) options;
  1100. return sb_block;
  1101. }
  1102. #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
  1103. static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
  1104. "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
  1105. #ifdef CONFIG_QUOTA
  1106. static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
  1107. {
  1108. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1109. char *qname;
  1110. if (sb_any_quota_loaded(sb) &&
  1111. !sbi->s_qf_names[qtype]) {
  1112. ext4_msg(sb, KERN_ERR,
  1113. "Cannot change journaled "
  1114. "quota options when quota turned on");
  1115. return 0;
  1116. }
  1117. qname = match_strdup(args);
  1118. if (!qname) {
  1119. ext4_msg(sb, KERN_ERR,
  1120. "Not enough memory for storing quotafile name");
  1121. return 0;
  1122. }
  1123. if (sbi->s_qf_names[qtype] &&
  1124. strcmp(sbi->s_qf_names[qtype], qname)) {
  1125. ext4_msg(sb, KERN_ERR,
  1126. "%s quota file already specified", QTYPE2NAME(qtype));
  1127. kfree(qname);
  1128. return 0;
  1129. }
  1130. sbi->s_qf_names[qtype] = qname;
  1131. if (strchr(sbi->s_qf_names[qtype], '/')) {
  1132. ext4_msg(sb, KERN_ERR,
  1133. "quotafile must be on filesystem root");
  1134. kfree(sbi->s_qf_names[qtype]);
  1135. sbi->s_qf_names[qtype] = NULL;
  1136. return 0;
  1137. }
  1138. set_opt(sbi->s_mount_opt, QUOTA);
  1139. return 1;
  1140. }
  1141. static int clear_qf_name(struct super_block *sb, int qtype)
  1142. {
  1143. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1144. if (sb_any_quota_loaded(sb) &&
  1145. sbi->s_qf_names[qtype]) {
  1146. ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
  1147. " when quota turned on");
  1148. return 0;
  1149. }
  1150. /*
  1151. * The space will be released later when all options are confirmed
  1152. * to be correct
  1153. */
  1154. sbi->s_qf_names[qtype] = NULL;
  1155. return 1;
  1156. }
  1157. #endif
  1158. static int parse_options(char *options, struct super_block *sb,
  1159. unsigned long *journal_devnum,
  1160. unsigned int *journal_ioprio,
  1161. ext4_fsblk_t *n_blocks_count, int is_remount)
  1162. {
  1163. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1164. char *p;
  1165. substring_t args[MAX_OPT_ARGS];
  1166. int data_opt = 0;
  1167. int option;
  1168. #ifdef CONFIG_QUOTA
  1169. int qfmt;
  1170. #endif
  1171. if (!options)
  1172. return 1;
  1173. while ((p = strsep(&options, ",")) != NULL) {
  1174. int token;
  1175. if (!*p)
  1176. continue;
  1177. /*
  1178. * Initialize args struct so we know whether arg was
  1179. * found; some options take optional arguments.
  1180. */
  1181. args[0].to = args[0].from = 0;
  1182. token = match_token(p, tokens, args);
  1183. switch (token) {
  1184. case Opt_bsd_df:
  1185. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1186. clear_opt(sbi->s_mount_opt, MINIX_DF);
  1187. break;
  1188. case Opt_minix_df:
  1189. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1190. set_opt(sbi->s_mount_opt, MINIX_DF);
  1191. break;
  1192. case Opt_grpid:
  1193. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1194. set_opt(sbi->s_mount_opt, GRPID);
  1195. break;
  1196. case Opt_nogrpid:
  1197. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1198. clear_opt(sbi->s_mount_opt, GRPID);
  1199. break;
  1200. case Opt_resuid:
  1201. if (match_int(&args[0], &option))
  1202. return 0;
  1203. sbi->s_resuid = option;
  1204. break;
  1205. case Opt_resgid:
  1206. if (match_int(&args[0], &option))
  1207. return 0;
  1208. sbi->s_resgid = option;
  1209. break;
  1210. case Opt_sb:
  1211. /* handled by get_sb_block() instead of here */
  1212. /* *sb_block = match_int(&args[0]); */
  1213. break;
  1214. case Opt_err_panic:
  1215. clear_opt(sbi->s_mount_opt, ERRORS_CONT);
  1216. clear_opt(sbi->s_mount_opt, ERRORS_RO);
  1217. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1218. break;
  1219. case Opt_err_ro:
  1220. clear_opt(sbi->s_mount_opt, ERRORS_CONT);
  1221. clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1222. set_opt(sbi->s_mount_opt, ERRORS_RO);
  1223. break;
  1224. case Opt_err_cont:
  1225. clear_opt(sbi->s_mount_opt, ERRORS_RO);
  1226. clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1227. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  1228. break;
  1229. case Opt_nouid32:
  1230. set_opt(sbi->s_mount_opt, NO_UID32);
  1231. break;
  1232. case Opt_debug:
  1233. set_opt(sbi->s_mount_opt, DEBUG);
  1234. break;
  1235. case Opt_oldalloc:
  1236. set_opt(sbi->s_mount_opt, OLDALLOC);
  1237. break;
  1238. case Opt_orlov:
  1239. clear_opt(sbi->s_mount_opt, OLDALLOC);
  1240. break;
  1241. #ifdef CONFIG_EXT4_FS_XATTR
  1242. case Opt_user_xattr:
  1243. set_opt(sbi->s_mount_opt, XATTR_USER);
  1244. break;
  1245. case Opt_nouser_xattr:
  1246. clear_opt(sbi->s_mount_opt, XATTR_USER);
  1247. break;
  1248. #else
  1249. case Opt_user_xattr:
  1250. case Opt_nouser_xattr:
  1251. ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
  1252. break;
  1253. #endif
  1254. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  1255. case Opt_acl:
  1256. set_opt(sbi->s_mount_opt, POSIX_ACL);
  1257. break;
  1258. case Opt_noacl:
  1259. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  1260. break;
  1261. #else
  1262. case Opt_acl:
  1263. case Opt_noacl:
  1264. ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
  1265. break;
  1266. #endif
  1267. case Opt_journal_update:
  1268. /* @@@ FIXME */
  1269. /* Eventually we will want to be able to create
  1270. a journal file here. For now, only allow the
  1271. user to specify an existing inode to be the
  1272. journal file. */
  1273. if (is_remount) {
  1274. ext4_msg(sb, KERN_ERR,
  1275. "Cannot specify journal on remount");
  1276. return 0;
  1277. }
  1278. set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
  1279. break;
  1280. case Opt_journal_dev:
  1281. if (is_remount) {
  1282. ext4_msg(sb, KERN_ERR,
  1283. "Cannot specify journal on remount");
  1284. return 0;
  1285. }
  1286. if (match_int(&args[0], &option))
  1287. return 0;
  1288. *journal_devnum = option;
  1289. break;
  1290. case Opt_journal_checksum:
  1291. set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
  1292. break;
  1293. case Opt_journal_async_commit:
  1294. set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
  1295. set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
  1296. break;
  1297. case Opt_noload:
  1298. set_opt(sbi->s_mount_opt, NOLOAD);
  1299. break;
  1300. case Opt_commit:
  1301. if (match_int(&args[0], &option))
  1302. return 0;
  1303. if (option < 0)
  1304. return 0;
  1305. if (option == 0)
  1306. option = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1307. sbi->s_commit_interval = HZ * option;
  1308. break;
  1309. case Opt_max_batch_time:
  1310. if (match_int(&args[0], &option))
  1311. return 0;
  1312. if (option < 0)
  1313. return 0;
  1314. if (option == 0)
  1315. option = EXT4_DEF_MAX_BATCH_TIME;
  1316. sbi->s_max_batch_time = option;
  1317. break;
  1318. case Opt_min_batch_time:
  1319. if (match_int(&args[0], &option))
  1320. return 0;
  1321. if (option < 0)
  1322. return 0;
  1323. sbi->s_min_batch_time = option;
  1324. break;
  1325. case Opt_data_journal:
  1326. data_opt = EXT4_MOUNT_JOURNAL_DATA;
  1327. goto datacheck;
  1328. case Opt_data_ordered:
  1329. data_opt = EXT4_MOUNT_ORDERED_DATA;
  1330. goto datacheck;
  1331. case Opt_data_writeback:
  1332. data_opt = EXT4_MOUNT_WRITEBACK_DATA;
  1333. datacheck:
  1334. if (is_remount) {
  1335. if (test_opt(sb, DATA_FLAGS) != data_opt) {
  1336. ext4_msg(sb, KERN_ERR,
  1337. "Cannot change data mode on remount");
  1338. return 0;
  1339. }
  1340. } else {
  1341. clear_opt(sbi->s_mount_opt, DATA_FLAGS);
  1342. sbi->s_mount_opt |= data_opt;
  1343. }
  1344. break;
  1345. case Opt_data_err_abort:
  1346. set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
  1347. break;
  1348. case Opt_data_err_ignore:
  1349. clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
  1350. break;
  1351. #ifdef CONFIG_QUOTA
  1352. case Opt_usrjquota:
  1353. if (!set_qf_name(sb, USRQUOTA, &args[0]))
  1354. return 0;
  1355. break;
  1356. case Opt_grpjquota:
  1357. if (!set_qf_name(sb, GRPQUOTA, &args[0]))
  1358. return 0;
  1359. break;
  1360. case Opt_offusrjquota:
  1361. if (!clear_qf_name(sb, USRQUOTA))
  1362. return 0;
  1363. break;
  1364. case Opt_offgrpjquota:
  1365. if (!clear_qf_name(sb, GRPQUOTA))
  1366. return 0;
  1367. break;
  1368. case Opt_jqfmt_vfsold:
  1369. qfmt = QFMT_VFS_OLD;
  1370. goto set_qf_format;
  1371. case Opt_jqfmt_vfsv0:
  1372. qfmt = QFMT_VFS_V0;
  1373. goto set_qf_format;
  1374. case Opt_jqfmt_vfsv1:
  1375. qfmt = QFMT_VFS_V1;
  1376. set_qf_format:
  1377. if (sb_any_quota_loaded(sb) &&
  1378. sbi->s_jquota_fmt != qfmt) {
  1379. ext4_msg(sb, KERN_ERR, "Cannot change "
  1380. "journaled quota options when "
  1381. "quota turned on");
  1382. return 0;
  1383. }
  1384. sbi->s_jquota_fmt = qfmt;
  1385. break;
  1386. case Opt_quota:
  1387. case Opt_usrquota:
  1388. set_opt(sbi->s_mount_opt, QUOTA);
  1389. set_opt(sbi->s_mount_opt, USRQUOTA);
  1390. break;
  1391. case Opt_grpquota:
  1392. set_opt(sbi->s_mount_opt, QUOTA);
  1393. set_opt(sbi->s_mount_opt, GRPQUOTA);
  1394. break;
  1395. case Opt_noquota:
  1396. if (sb_any_quota_loaded(sb)) {
  1397. ext4_msg(sb, KERN_ERR, "Cannot change quota "
  1398. "options when quota turned on");
  1399. return 0;
  1400. }
  1401. clear_opt(sbi->s_mount_opt, QUOTA);
  1402. clear_opt(sbi->s_mount_opt, USRQUOTA);
  1403. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  1404. break;
  1405. #else
  1406. case Opt_quota:
  1407. case Opt_usrquota:
  1408. case Opt_grpquota:
  1409. ext4_msg(sb, KERN_ERR,
  1410. "quota options not supported");
  1411. break;
  1412. case Opt_usrjquota:
  1413. case Opt_grpjquota:
  1414. case Opt_offusrjquota:
  1415. case Opt_offgrpjquota:
  1416. case Opt_jqfmt_vfsold:
  1417. case Opt_jqfmt_vfsv0:
  1418. case Opt_jqfmt_vfsv1:
  1419. ext4_msg(sb, KERN_ERR,
  1420. "journaled quota options not supported");
  1421. break;
  1422. case Opt_noquota:
  1423. break;
  1424. #endif
  1425. case Opt_abort:
  1426. sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
  1427. break;
  1428. case Opt_nobarrier:
  1429. clear_opt(sbi->s_mount_opt, BARRIER);
  1430. break;
  1431. case Opt_barrier:
  1432. if (args[0].from) {
  1433. if (match_int(&args[0], &option))
  1434. return 0;
  1435. } else
  1436. option = 1; /* No argument, default to 1 */
  1437. if (option)
  1438. set_opt(sbi->s_mount_opt, BARRIER);
  1439. else
  1440. clear_opt(sbi->s_mount_opt, BARRIER);
  1441. break;
  1442. case Opt_ignore:
  1443. break;
  1444. case Opt_resize:
  1445. if (!is_remount) {
  1446. ext4_msg(sb, KERN_ERR,
  1447. "resize option only available "
  1448. "for remount");
  1449. return 0;
  1450. }
  1451. if (match_int(&args[0], &option) != 0)
  1452. return 0;
  1453. *n_blocks_count = option;
  1454. break;
  1455. case Opt_nobh:
  1456. set_opt(sbi->s_mount_opt, NOBH);
  1457. break;
  1458. case Opt_bh:
  1459. clear_opt(sbi->s_mount_opt, NOBH);
  1460. break;
  1461. case Opt_i_version:
  1462. set_opt(sbi->s_mount_opt, I_VERSION);
  1463. sb->s_flags |= MS_I_VERSION;
  1464. break;
  1465. case Opt_nodelalloc:
  1466. clear_opt(sbi->s_mount_opt, DELALLOC);
  1467. break;
  1468. case Opt_stripe:
  1469. if (match_int(&args[0], &option))
  1470. return 0;
  1471. if (option < 0)
  1472. return 0;
  1473. sbi->s_stripe = option;
  1474. break;
  1475. case Opt_delalloc:
  1476. set_opt(sbi->s_mount_opt, DELALLOC);
  1477. break;
  1478. case Opt_block_validity:
  1479. set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
  1480. break;
  1481. case Opt_noblock_validity:
  1482. clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
  1483. break;
  1484. case Opt_inode_readahead_blks:
  1485. if (match_int(&args[0], &option))
  1486. return 0;
  1487. if (option < 0 || option > (1 << 30))
  1488. return 0;
  1489. if (!is_power_of_2(option)) {
  1490. ext4_msg(sb, KERN_ERR,
  1491. "EXT4-fs: inode_readahead_blks"
  1492. " must be a power of 2");
  1493. return 0;
  1494. }
  1495. sbi->s_inode_readahead_blks = option;
  1496. break;
  1497. case Opt_journal_ioprio:
  1498. if (match_int(&args[0], &option))
  1499. return 0;
  1500. if (option < 0 || option > 7)
  1501. break;
  1502. *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
  1503. option);
  1504. break;
  1505. case Opt_noauto_da_alloc:
  1506. set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
  1507. break;
  1508. case Opt_auto_da_alloc:
  1509. if (args[0].from) {
  1510. if (match_int(&args[0], &option))
  1511. return 0;
  1512. } else
  1513. option = 1; /* No argument, default to 1 */
  1514. if (option)
  1515. clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
  1516. else
  1517. set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
  1518. break;
  1519. case Opt_discard:
  1520. set_opt(sbi->s_mount_opt, DISCARD);
  1521. break;
  1522. case Opt_nodiscard:
  1523. clear_opt(sbi->s_mount_opt, DISCARD);
  1524. break;
  1525. case Opt_dioread_nolock:
  1526. set_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
  1527. break;
  1528. case Opt_dioread_lock:
  1529. clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
  1530. break;
  1531. default:
  1532. ext4_msg(sb, KERN_ERR,
  1533. "Unrecognized mount option \"%s\" "
  1534. "or missing value", p);
  1535. return 0;
  1536. }
  1537. }
  1538. #ifdef CONFIG_QUOTA
  1539. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  1540. if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
  1541. clear_opt(sbi->s_mount_opt, USRQUOTA);
  1542. if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
  1543. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  1544. if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
  1545. ext4_msg(sb, KERN_ERR, "old and new quota "
  1546. "format mixing");
  1547. return 0;
  1548. }
  1549. if (!sbi->s_jquota_fmt) {
  1550. ext4_msg(sb, KERN_ERR, "journaled quota format "
  1551. "not specified");
  1552. return 0;
  1553. }
  1554. } else {
  1555. if (sbi->s_jquota_fmt) {
  1556. ext4_msg(sb, KERN_ERR, "journaled quota format "
  1557. "specified with no journaling "
  1558. "enabled");
  1559. return 0;
  1560. }
  1561. }
  1562. #endif
  1563. return 1;
  1564. }
  1565. static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
  1566. int read_only)
  1567. {
  1568. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1569. int res = 0;
  1570. if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
  1571. ext4_msg(sb, KERN_ERR, "revision level too high, "
  1572. "forcing read-only mode");
  1573. res = MS_RDONLY;
  1574. }
  1575. if (read_only)
  1576. return res;
  1577. if (!(sbi->s_mount_state & EXT4_VALID_FS))
  1578. ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
  1579. "running e2fsck is recommended");
  1580. else if ((sbi->s_mount_state & EXT4_ERROR_FS))
  1581. ext4_msg(sb, KERN_WARNING,
  1582. "warning: mounting fs with errors, "
  1583. "running e2fsck is recommended");
  1584. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  1585. le16_to_cpu(es->s_mnt_count) >=
  1586. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  1587. ext4_msg(sb, KERN_WARNING,
  1588. "warning: maximal mount count reached, "
  1589. "running e2fsck is recommended");
  1590. else if (le32_to_cpu(es->s_checkinterval) &&
  1591. (le32_to_cpu(es->s_lastcheck) +
  1592. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  1593. ext4_msg(sb, KERN_WARNING,
  1594. "warning: checktime reached, "
  1595. "running e2fsck is recommended");
  1596. if (!sbi->s_journal)
  1597. es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  1598. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  1599. es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
  1600. le16_add_cpu(&es->s_mnt_count, 1);
  1601. es->s_mtime = cpu_to_le32(get_seconds());
  1602. ext4_update_dynamic_rev(sb);
  1603. if (sbi->s_journal)
  1604. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  1605. ext4_commit_super(sb, 1);
  1606. if (test_opt(sb, DEBUG))
  1607. printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
  1608. "bpg=%lu, ipg=%lu, mo=%04x]\n",
  1609. sb->s_blocksize,
  1610. sbi->s_groups_count,
  1611. EXT4_BLOCKS_PER_GROUP(sb),
  1612. EXT4_INODES_PER_GROUP(sb),
  1613. sbi->s_mount_opt);
  1614. return res;
  1615. }
  1616. static int ext4_fill_flex_info(struct super_block *sb)
  1617. {
  1618. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1619. struct ext4_group_desc *gdp = NULL;
  1620. ext4_group_t flex_group_count;
  1621. ext4_group_t flex_group;
  1622. int groups_per_flex = 0;
  1623. size_t size;
  1624. int i;
  1625. sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
  1626. groups_per_flex = 1 << sbi->s_log_groups_per_flex;
  1627. if (groups_per_flex < 2) {
  1628. sbi->s_log_groups_per_flex = 0;
  1629. return 1;
  1630. }
  1631. /* We allocate both existing and potentially added groups */
  1632. flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
  1633. ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
  1634. EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
  1635. size = flex_group_count * sizeof(struct flex_groups);
  1636. sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
  1637. if (sbi->s_flex_groups == NULL) {
  1638. sbi->s_flex_groups = vmalloc(size);
  1639. if (sbi->s_flex_groups)
  1640. memset(sbi->s_flex_groups, 0, size);
  1641. }
  1642. if (sbi->s_flex_groups == NULL) {
  1643. ext4_msg(sb, KERN_ERR, "not enough memory for "
  1644. "%u flex groups", flex_group_count);
  1645. goto failed;
  1646. }
  1647. for (i = 0; i < sbi->s_groups_count; i++) {
  1648. gdp = ext4_get_group_desc(sb, i, NULL);
  1649. flex_group = ext4_flex_group(sbi, i);
  1650. atomic_add(ext4_free_inodes_count(sb, gdp),
  1651. &sbi->s_flex_groups[flex_group].free_inodes);
  1652. atomic_add(ext4_free_blks_count(sb, gdp),
  1653. &sbi->s_flex_groups[flex_group].free_blocks);
  1654. atomic_add(ext4_used_dirs_count(sb, gdp),
  1655. &sbi->s_flex_groups[flex_group].used_dirs);
  1656. }
  1657. return 1;
  1658. failed:
  1659. return 0;
  1660. }
  1661. __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
  1662. struct ext4_group_desc *gdp)
  1663. {
  1664. __u16 crc = 0;
  1665. if (sbi->s_es->s_feature_ro_compat &
  1666. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  1667. int offset = offsetof(struct ext4_group_desc, bg_checksum);
  1668. __le32 le_group = cpu_to_le32(block_group);
  1669. crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
  1670. crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
  1671. crc = crc16(crc, (__u8 *)gdp, offset);
  1672. offset += sizeof(gdp->bg_checksum); /* skip checksum */
  1673. /* for checksum of struct ext4_group_desc do the rest...*/
  1674. if ((sbi->s_es->s_feature_incompat &
  1675. cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
  1676. offset < le16_to_cpu(sbi->s_es->s_desc_size))
  1677. crc = crc16(crc, (__u8 *)gdp + offset,
  1678. le16_to_cpu(sbi->s_es->s_desc_size) -
  1679. offset);
  1680. }
  1681. return cpu_to_le16(crc);
  1682. }
  1683. int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
  1684. struct ext4_group_desc *gdp)
  1685. {
  1686. if ((sbi->s_es->s_feature_ro_compat &
  1687. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
  1688. (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
  1689. return 0;
  1690. return 1;
  1691. }
  1692. /* Called at mount-time, super-block is locked */
  1693. static int ext4_check_descriptors(struct super_block *sb)
  1694. {
  1695. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1696. ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  1697. ext4_fsblk_t last_block;
  1698. ext4_fsblk_t block_bitmap;
  1699. ext4_fsblk_t inode_bitmap;
  1700. ext4_fsblk_t inode_table;
  1701. int flexbg_flag = 0;
  1702. ext4_group_t i;
  1703. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  1704. flexbg_flag = 1;
  1705. ext4_debug("Checking group descriptors");
  1706. for (i = 0; i < sbi->s_groups_count; i++) {
  1707. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1708. if (i == sbi->s_groups_count - 1 || flexbg_flag)
  1709. last_block = ext4_blocks_count(sbi->s_es) - 1;
  1710. else
  1711. last_block = first_block +
  1712. (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1713. block_bitmap = ext4_block_bitmap(sb, gdp);
  1714. if (block_bitmap < first_block || block_bitmap > last_block) {
  1715. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1716. "Block bitmap for group %u not in group "
  1717. "(block %llu)!", i, block_bitmap);
  1718. return 0;
  1719. }
  1720. inode_bitmap = ext4_inode_bitmap(sb, gdp);
  1721. if (inode_bitmap < first_block || inode_bitmap > last_block) {
  1722. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1723. "Inode bitmap for group %u not in group "
  1724. "(block %llu)!", i, inode_bitmap);
  1725. return 0;
  1726. }
  1727. inode_table = ext4_inode_table(sb, gdp);
  1728. if (inode_table < first_block ||
  1729. inode_table + sbi->s_itb_per_group - 1 > last_block) {
  1730. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1731. "Inode table for group %u not in group "
  1732. "(block %llu)!", i, inode_table);
  1733. return 0;
  1734. }
  1735. ext4_lock_group(sb, i);
  1736. if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
  1737. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1738. "Checksum for group %u failed (%u!=%u)",
  1739. i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
  1740. gdp)), le16_to_cpu(gdp->bg_checksum));
  1741. if (!(sb->s_flags & MS_RDONLY)) {
  1742. ext4_unlock_group(sb, i);
  1743. return 0;
  1744. }
  1745. }
  1746. ext4_unlock_group(sb, i);
  1747. if (!flexbg_flag)
  1748. first_block += EXT4_BLOCKS_PER_GROUP(sb);
  1749. }
  1750. ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
  1751. sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
  1752. return 1;
  1753. }
  1754. /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
  1755. * the superblock) which were deleted from all directories, but held open by
  1756. * a process at the time of a crash. We walk the list and try to delete these
  1757. * inodes at recovery time (only with a read-write filesystem).
  1758. *
  1759. * In order to keep the orphan inode chain consistent during traversal (in
  1760. * case of crash during recovery), we link each inode into the superblock
  1761. * orphan list_head and handle it the same way as an inode deletion during
  1762. * normal operation (which journals the operations for us).
  1763. *
  1764. * We only do an iget() and an iput() on each inode, which is very safe if we
  1765. * accidentally point at an in-use or already deleted inode. The worst that
  1766. * can happen in this case is that we get a "bit already cleared" message from
  1767. * ext4_free_inode(). The only reason we would point at a wrong inode is if
  1768. * e2fsck was run on this filesystem, and it must have already done the orphan
  1769. * inode cleanup for us, so we can safely abort without any further action.
  1770. */
  1771. static void ext4_orphan_cleanup(struct super_block *sb,
  1772. struct ext4_super_block *es)
  1773. {
  1774. unsigned int s_flags = sb->s_flags;
  1775. int nr_orphans = 0, nr_truncates = 0;
  1776. #ifdef CONFIG_QUOTA
  1777. int i;
  1778. #endif
  1779. if (!es->s_last_orphan) {
  1780. jbd_debug(4, "no orphan inodes to clean up\n");
  1781. return;
  1782. }
  1783. if (bdev_read_only(sb->s_bdev)) {
  1784. ext4_msg(sb, KERN_ERR, "write access "
  1785. "unavailable, skipping orphan cleanup");
  1786. return;
  1787. }
  1788. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  1789. if (es->s_last_orphan)
  1790. jbd_debug(1, "Errors on filesystem, "
  1791. "clearing orphan list.\n");
  1792. es->s_last_orphan = 0;
  1793. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1794. return;
  1795. }
  1796. if (s_flags & MS_RDONLY) {
  1797. ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
  1798. sb->s_flags &= ~MS_RDONLY;
  1799. }
  1800. #ifdef CONFIG_QUOTA
  1801. /* Needed for iput() to work correctly and not trash data */
  1802. sb->s_flags |= MS_ACTIVE;
  1803. /* Turn on quotas so that they are updated correctly */
  1804. for (i = 0; i < MAXQUOTAS; i++) {
  1805. if (EXT4_SB(sb)->s_qf_names[i]) {
  1806. int ret = ext4_quota_on_mount(sb, i);
  1807. if (ret < 0)
  1808. ext4_msg(sb, KERN_ERR,
  1809. "Cannot turn on journaled "
  1810. "quota: error %d", ret);
  1811. }
  1812. }
  1813. #endif
  1814. while (es->s_last_orphan) {
  1815. struct inode *inode;
  1816. inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
  1817. if (IS_ERR(inode)) {
  1818. es->s_last_orphan = 0;
  1819. break;
  1820. }
  1821. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1822. dquot_initialize(inode);
  1823. if (inode->i_nlink) {
  1824. ext4_msg(sb, KERN_DEBUG,
  1825. "%s: truncating inode %lu to %lld bytes",
  1826. __func__, inode->i_ino, inode->i_size);
  1827. jbd_debug(2, "truncating inode %lu to %lld bytes\n",
  1828. inode->i_ino, inode->i_size);
  1829. ext4_truncate(inode);
  1830. nr_truncates++;
  1831. } else {
  1832. ext4_msg(sb, KERN_DEBUG,
  1833. "%s: deleting unreferenced inode %lu",
  1834. __func__, inode->i_ino);
  1835. jbd_debug(2, "deleting unreferenced inode %lu\n",
  1836. inode->i_ino);
  1837. nr_orphans++;
  1838. }
  1839. iput(inode); /* The delete magic happens here! */
  1840. }
  1841. #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
  1842. if (nr_orphans)
  1843. ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
  1844. PLURAL(nr_orphans));
  1845. if (nr_truncates)
  1846. ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
  1847. PLURAL(nr_truncates));
  1848. #ifdef CONFIG_QUOTA
  1849. /* Turn quotas off */
  1850. for (i = 0; i < MAXQUOTAS; i++) {
  1851. if (sb_dqopt(sb)->files[i])
  1852. dquot_quota_off(sb, i);
  1853. }
  1854. #endif
  1855. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  1856. }
  1857. /*
  1858. * Maximal extent format file size.
  1859. * Resulting logical blkno at s_maxbytes must fit in our on-disk
  1860. * extent format containers, within a sector_t, and within i_blocks
  1861. * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
  1862. * so that won't be a limiting factor.
  1863. *
  1864. * Note, this does *not* consider any metadata overhead for vfs i_blocks.
  1865. */
  1866. static loff_t ext4_max_size(int blkbits, int has_huge_files)
  1867. {
  1868. loff_t res;
  1869. loff_t upper_limit = MAX_LFS_FILESIZE;
  1870. /* small i_blocks in vfs inode? */
  1871. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  1872. /*
  1873. * CONFIG_LBDAF is not enabled implies the inode
  1874. * i_block represent total blocks in 512 bytes
  1875. * 32 == size of vfs inode i_blocks * 8
  1876. */
  1877. upper_limit = (1LL << 32) - 1;
  1878. /* total blocks in file system block size */
  1879. upper_limit >>= (blkbits - 9);
  1880. upper_limit <<= blkbits;
  1881. }
  1882. /* 32-bit extent-start container, ee_block */
  1883. res = 1LL << 32;
  1884. res <<= blkbits;
  1885. res -= 1;
  1886. /* Sanity check against vm- & vfs- imposed limits */
  1887. if (res > upper_limit)
  1888. res = upper_limit;
  1889. return res;
  1890. }
  1891. /*
  1892. * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
  1893. * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
  1894. * We need to be 1 filesystem block less than the 2^48 sector limit.
  1895. */
  1896. static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
  1897. {
  1898. loff_t res = EXT4_NDIR_BLOCKS;
  1899. int meta_blocks;
  1900. loff_t upper_limit;
  1901. /* This is calculated to be the largest file size for a dense, block
  1902. * mapped file such that the file's total number of 512-byte sectors,
  1903. * including data and all indirect blocks, does not exceed (2^48 - 1).
  1904. *
  1905. * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
  1906. * number of 512-byte sectors of the file.
  1907. */
  1908. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  1909. /*
  1910. * !has_huge_files or CONFIG_LBDAF not enabled implies that
  1911. * the inode i_block field represents total file blocks in
  1912. * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
  1913. */
  1914. upper_limit = (1LL << 32) - 1;
  1915. /* total blocks in file system block size */
  1916. upper_limit >>= (bits - 9);
  1917. } else {
  1918. /*
  1919. * We use 48 bit ext4_inode i_blocks
  1920. * With EXT4_HUGE_FILE_FL set the i_blocks
  1921. * represent total number of blocks in
  1922. * file system block size
  1923. */
  1924. upper_limit = (1LL << 48) - 1;
  1925. }
  1926. /* indirect blocks */
  1927. meta_blocks = 1;
  1928. /* double indirect blocks */
  1929. meta_blocks += 1 + (1LL << (bits-2));
  1930. /* tripple indirect blocks */
  1931. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  1932. upper_limit -= meta_blocks;
  1933. upper_limit <<= bits;
  1934. res += 1LL << (bits-2);
  1935. res += 1LL << (2*(bits-2));
  1936. res += 1LL << (3*(bits-2));
  1937. res <<= bits;
  1938. if (res > upper_limit)
  1939. res = upper_limit;
  1940. if (res > MAX_LFS_FILESIZE)
  1941. res = MAX_LFS_FILESIZE;
  1942. return res;
  1943. }
  1944. static ext4_fsblk_t descriptor_loc(struct super_block *sb,
  1945. ext4_fsblk_t logical_sb_block, int nr)
  1946. {
  1947. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1948. ext4_group_t bg, first_meta_bg;
  1949. int has_super = 0;
  1950. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  1951. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  1952. nr < first_meta_bg)
  1953. return logical_sb_block + nr + 1;
  1954. bg = sbi->s_desc_per_block * nr;
  1955. if (ext4_bg_has_super(sb, bg))
  1956. has_super = 1;
  1957. return (has_super + ext4_group_first_block_no(sb, bg));
  1958. }
  1959. /**
  1960. * ext4_get_stripe_size: Get the stripe size.
  1961. * @sbi: In memory super block info
  1962. *
  1963. * If we have specified it via mount option, then
  1964. * use the mount option value. If the value specified at mount time is
  1965. * greater than the blocks per group use the super block value.
  1966. * If the super block value is greater than blocks per group return 0.
  1967. * Allocator needs it be less than blocks per group.
  1968. *
  1969. */
  1970. static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
  1971. {
  1972. unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
  1973. unsigned long stripe_width =
  1974. le32_to_cpu(sbi->s_es->s_raid_stripe_width);
  1975. if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
  1976. return sbi->s_stripe;
  1977. if (stripe_width <= sbi->s_blocks_per_group)
  1978. return stripe_width;
  1979. if (stride <= sbi->s_blocks_per_group)
  1980. return stride;
  1981. return 0;
  1982. }
  1983. /* sysfs supprt */
  1984. struct ext4_attr {
  1985. struct attribute attr;
  1986. ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
  1987. ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
  1988. const char *, size_t);
  1989. int offset;
  1990. };
  1991. static int parse_strtoul(const char *buf,
  1992. unsigned long max, unsigned long *value)
  1993. {
  1994. char *endp;
  1995. *value = simple_strtoul(skip_spaces(buf), &endp, 0);
  1996. endp = skip_spaces(endp);
  1997. if (*endp || *value > max)
  1998. return -EINVAL;
  1999. return 0;
  2000. }
  2001. static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
  2002. struct ext4_sb_info *sbi,
  2003. char *buf)
  2004. {
  2005. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2006. (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
  2007. }
  2008. static ssize_t session_write_kbytes_show(struct ext4_attr *a,
  2009. struct ext4_sb_info *sbi, char *buf)
  2010. {
  2011. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2012. return snprintf(buf, PAGE_SIZE, "%lu\n",
  2013. (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2014. sbi->s_sectors_written_start) >> 1);
  2015. }
  2016. static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
  2017. struct ext4_sb_info *sbi, char *buf)
  2018. {
  2019. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2020. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2021. (unsigned long long)(sbi->s_kbytes_written +
  2022. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2023. EXT4_SB(sb)->s_sectors_written_start) >> 1)));
  2024. }
  2025. static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
  2026. struct ext4_sb_info *sbi,
  2027. const char *buf, size_t count)
  2028. {
  2029. unsigned long t;
  2030. if (parse_strtoul(buf, 0x40000000, &t))
  2031. return -EINVAL;
  2032. if (!is_power_of_2(t))
  2033. return -EINVAL;
  2034. sbi->s_inode_readahead_blks = t;
  2035. return count;
  2036. }
  2037. static ssize_t sbi_ui_show(struct ext4_attr *a,
  2038. struct ext4_sb_info *sbi, char *buf)
  2039. {
  2040. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2041. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  2042. }
  2043. static ssize_t sbi_ui_store(struct ext4_attr *a,
  2044. struct ext4_sb_info *sbi,
  2045. const char *buf, size_t count)
  2046. {
  2047. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2048. unsigned long t;
  2049. if (parse_strtoul(buf, 0xffffffff, &t))
  2050. return -EINVAL;
  2051. *ui = t;
  2052. return count;
  2053. }
  2054. #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
  2055. static struct ext4_attr ext4_attr_##_name = { \
  2056. .attr = {.name = __stringify(_name), .mode = _mode }, \
  2057. .show = _show, \
  2058. .store = _store, \
  2059. .offset = offsetof(struct ext4_sb_info, _elname), \
  2060. }
  2061. #define EXT4_ATTR(name, mode, show, store) \
  2062. static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
  2063. #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
  2064. #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
  2065. #define EXT4_RW_ATTR_SBI_UI(name, elname) \
  2066. EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
  2067. #define ATTR_LIST(name) &ext4_attr_##name.attr
  2068. EXT4_RO_ATTR(delayed_allocation_blocks);
  2069. EXT4_RO_ATTR(session_write_kbytes);
  2070. EXT4_RO_ATTR(lifetime_write_kbytes);
  2071. EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
  2072. inode_readahead_blks_store, s_inode_readahead_blks);
  2073. EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
  2074. EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
  2075. EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
  2076. EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
  2077. EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
  2078. EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
  2079. EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
  2080. EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
  2081. static struct attribute *ext4_attrs[] = {
  2082. ATTR_LIST(delayed_allocation_blocks),
  2083. ATTR_LIST(session_write_kbytes),
  2084. ATTR_LIST(lifetime_write_kbytes),
  2085. ATTR_LIST(inode_readahead_blks),
  2086. ATTR_LIST(inode_goal),
  2087. ATTR_LIST(mb_stats),
  2088. ATTR_LIST(mb_max_to_scan),
  2089. ATTR_LIST(mb_min_to_scan),
  2090. ATTR_LIST(mb_order2_req),
  2091. ATTR_LIST(mb_stream_req),
  2092. ATTR_LIST(mb_group_prealloc),
  2093. ATTR_LIST(max_writeback_mb_bump),
  2094. NULL,
  2095. };
  2096. static ssize_t ext4_attr_show(struct kobject *kobj,
  2097. struct attribute *attr, char *buf)
  2098. {
  2099. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2100. s_kobj);
  2101. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2102. return a->show ? a->show(a, sbi, buf) : 0;
  2103. }
  2104. static ssize_t ext4_attr_store(struct kobject *kobj,
  2105. struct attribute *attr,
  2106. const char *buf, size_t len)
  2107. {
  2108. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2109. s_kobj);
  2110. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2111. return a->store ? a->store(a, sbi, buf, len) : 0;
  2112. }
  2113. static void ext4_sb_release(struct kobject *kobj)
  2114. {
  2115. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2116. s_kobj);
  2117. complete(&sbi->s_kobj_unregister);
  2118. }
  2119. static const struct sysfs_ops ext4_attr_ops = {
  2120. .show = ext4_attr_show,
  2121. .store = ext4_attr_store,
  2122. };
  2123. static struct kobj_type ext4_ktype = {
  2124. .default_attrs = ext4_attrs,
  2125. .sysfs_ops = &ext4_attr_ops,
  2126. .release = ext4_sb_release,
  2127. };
  2128. /*
  2129. * Check whether this filesystem can be mounted based on
  2130. * the features present and the RDONLY/RDWR mount requested.
  2131. * Returns 1 if this filesystem can be mounted as requested,
  2132. * 0 if it cannot be.
  2133. */
  2134. static int ext4_feature_set_ok(struct super_block *sb, int readonly)
  2135. {
  2136. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
  2137. ext4_msg(sb, KERN_ERR,
  2138. "Couldn't mount because of "
  2139. "unsupported optional features (%x)",
  2140. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  2141. ~EXT4_FEATURE_INCOMPAT_SUPP));
  2142. return 0;
  2143. }
  2144. if (readonly)
  2145. return 1;
  2146. /* Check that feature set is OK for a read-write mount */
  2147. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
  2148. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  2149. "unsupported optional features (%x)",
  2150. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  2151. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  2152. return 0;
  2153. }
  2154. /*
  2155. * Large file size enabled file system can only be mounted
  2156. * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
  2157. */
  2158. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  2159. if (sizeof(blkcnt_t) < sizeof(u64)) {
  2160. ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
  2161. "cannot be mounted RDWR without "
  2162. "CONFIG_LBDAF");
  2163. return 0;
  2164. }
  2165. }
  2166. return 1;
  2167. }
  2168. static int ext4_fill_super(struct super_block *sb, void *data, int silent)
  2169. __releases(kernel_lock)
  2170. __acquires(kernel_lock)
  2171. {
  2172. char *orig_data = kstrdup(data, GFP_KERNEL);
  2173. struct buffer_head *bh;
  2174. struct ext4_super_block *es = NULL;
  2175. struct ext4_sb_info *sbi;
  2176. ext4_fsblk_t block;
  2177. ext4_fsblk_t sb_block = get_sb_block(&data);
  2178. ext4_fsblk_t logical_sb_block;
  2179. unsigned long offset = 0;
  2180. unsigned long journal_devnum = 0;
  2181. unsigned long def_mount_opts;
  2182. struct inode *root;
  2183. char *cp;
  2184. const char *descr;
  2185. int ret = -EINVAL;
  2186. int blocksize;
  2187. unsigned int db_count;
  2188. unsigned int i;
  2189. int needs_recovery, has_huge_files;
  2190. __u64 blocks_count;
  2191. int err;
  2192. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  2193. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  2194. if (!sbi)
  2195. return -ENOMEM;
  2196. sbi->s_blockgroup_lock =
  2197. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  2198. if (!sbi->s_blockgroup_lock) {
  2199. kfree(sbi);
  2200. return -ENOMEM;
  2201. }
  2202. sb->s_fs_info = sbi;
  2203. sbi->s_mount_opt = 0;
  2204. sbi->s_resuid = EXT4_DEF_RESUID;
  2205. sbi->s_resgid = EXT4_DEF_RESGID;
  2206. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  2207. sbi->s_sb_block = sb_block;
  2208. sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
  2209. sectors[1]);
  2210. unlock_kernel();
  2211. /* Cleanup superblock name */
  2212. for (cp = sb->s_id; (cp = strchr(cp, '/'));)
  2213. *cp = '!';
  2214. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  2215. if (!blocksize) {
  2216. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  2217. goto out_fail;
  2218. }
  2219. /*
  2220. * The ext4 superblock will not be buffer aligned for other than 1kB
  2221. * block sizes. We need to calculate the offset from buffer start.
  2222. */
  2223. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  2224. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2225. offset = do_div(logical_sb_block, blocksize);
  2226. } else {
  2227. logical_sb_block = sb_block;
  2228. }
  2229. if (!(bh = sb_bread(sb, logical_sb_block))) {
  2230. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  2231. goto out_fail;
  2232. }
  2233. /*
  2234. * Note: s_es must be initialized as soon as possible because
  2235. * some ext4 macro-instructions depend on its value
  2236. */
  2237. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2238. sbi->s_es = es;
  2239. sb->s_magic = le16_to_cpu(es->s_magic);
  2240. if (sb->s_magic != EXT4_SUPER_MAGIC)
  2241. goto cantfind_ext4;
  2242. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  2243. /* Set defaults before we parse the mount options */
  2244. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  2245. if (def_mount_opts & EXT4_DEFM_DEBUG)
  2246. set_opt(sbi->s_mount_opt, DEBUG);
  2247. if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
  2248. ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
  2249. "2.6.38");
  2250. set_opt(sbi->s_mount_opt, GRPID);
  2251. }
  2252. if (def_mount_opts & EXT4_DEFM_UID16)
  2253. set_opt(sbi->s_mount_opt, NO_UID32);
  2254. #ifdef CONFIG_EXT4_FS_XATTR
  2255. if (def_mount_opts & EXT4_DEFM_XATTR_USER)
  2256. set_opt(sbi->s_mount_opt, XATTR_USER);
  2257. #endif
  2258. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  2259. if (def_mount_opts & EXT4_DEFM_ACL)
  2260. set_opt(sbi->s_mount_opt, POSIX_ACL);
  2261. #endif
  2262. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  2263. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  2264. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  2265. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  2266. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  2267. set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
  2268. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  2269. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  2270. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
  2271. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  2272. else
  2273. set_opt(sbi->s_mount_opt, ERRORS_RO);
  2274. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  2275. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  2276. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  2277. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  2278. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  2279. set_opt(sbi->s_mount_opt, BARRIER);
  2280. /*
  2281. * enable delayed allocation by default
  2282. * Use -o nodelalloc to turn it off
  2283. */
  2284. if (!IS_EXT3_SB(sb))
  2285. set_opt(sbi->s_mount_opt, DELALLOC);
  2286. if (!parse_options((char *) data, sb, &journal_devnum,
  2287. &journal_ioprio, NULL, 0))
  2288. goto failed_mount;
  2289. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2290. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2291. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  2292. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  2293. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  2294. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  2295. ext4_msg(sb, KERN_WARNING,
  2296. "feature flags set on rev 0 fs, "
  2297. "running e2fsck is recommended");
  2298. /*
  2299. * Check feature flags regardless of the revision level, since we
  2300. * previously didn't change the revision level when setting the flags,
  2301. * so there is a chance incompat flags are set on a rev 0 filesystem.
  2302. */
  2303. if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
  2304. goto failed_mount;
  2305. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  2306. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  2307. blocksize > EXT4_MAX_BLOCK_SIZE) {
  2308. ext4_msg(sb, KERN_ERR,
  2309. "Unsupported filesystem blocksize %d", blocksize);
  2310. goto failed_mount;
  2311. }
  2312. if (sb->s_blocksize != blocksize) {
  2313. /* Validate the filesystem blocksize */
  2314. if (!sb_set_blocksize(sb, blocksize)) {
  2315. ext4_msg(sb, KERN_ERR, "bad block size %d",
  2316. blocksize);
  2317. goto failed_mount;
  2318. }
  2319. brelse(bh);
  2320. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2321. offset = do_div(logical_sb_block, blocksize);
  2322. bh = sb_bread(sb, logical_sb_block);
  2323. if (!bh) {
  2324. ext4_msg(sb, KERN_ERR,
  2325. "Can't read superblock on 2nd try");
  2326. goto failed_mount;
  2327. }
  2328. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  2329. sbi->s_es = es;
  2330. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  2331. ext4_msg(sb, KERN_ERR,
  2332. "Magic mismatch, very weird!");
  2333. goto failed_mount;
  2334. }
  2335. }
  2336. has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2337. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  2338. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  2339. has_huge_files);
  2340. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  2341. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  2342. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  2343. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  2344. } else {
  2345. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  2346. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  2347. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  2348. (!is_power_of_2(sbi->s_inode_size)) ||
  2349. (sbi->s_inode_size > blocksize)) {
  2350. ext4_msg(sb, KERN_ERR,
  2351. "unsupported inode size: %d",
  2352. sbi->s_inode_size);
  2353. goto failed_mount;
  2354. }
  2355. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  2356. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  2357. }
  2358. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  2359. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  2360. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  2361. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  2362. !is_power_of_2(sbi->s_desc_size)) {
  2363. ext4_msg(sb, KERN_ERR,
  2364. "unsupported descriptor size %lu",
  2365. sbi->s_desc_size);
  2366. goto failed_mount;
  2367. }
  2368. } else
  2369. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  2370. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  2371. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  2372. if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
  2373. goto cantfind_ext4;
  2374. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  2375. if (sbi->s_inodes_per_block == 0)
  2376. goto cantfind_ext4;
  2377. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  2378. sbi->s_inodes_per_block;
  2379. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  2380. sbi->s_sbh = bh;
  2381. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2382. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  2383. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  2384. for (i = 0; i < 4; i++)
  2385. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  2386. sbi->s_def_hash_version = es->s_def_hash_version;
  2387. i = le32_to_cpu(es->s_flags);
  2388. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  2389. sbi->s_hash_unsigned = 3;
  2390. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  2391. #ifdef __CHAR_UNSIGNED__
  2392. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  2393. sbi->s_hash_unsigned = 3;
  2394. #else
  2395. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  2396. #endif
  2397. sb->s_dirt = 1;
  2398. }
  2399. if (sbi->s_blocks_per_group > blocksize * 8) {
  2400. ext4_msg(sb, KERN_ERR,
  2401. "#blocks per group too big: %lu",
  2402. sbi->s_blocks_per_group);
  2403. goto failed_mount;
  2404. }
  2405. if (sbi->s_inodes_per_group > blocksize * 8) {
  2406. ext4_msg(sb, KERN_ERR,
  2407. "#inodes per group too big: %lu",
  2408. sbi->s_inodes_per_group);
  2409. goto failed_mount;
  2410. }
  2411. /*
  2412. * Test whether we have more sectors than will fit in sector_t,
  2413. * and whether the max offset is addressable by the page cache.
  2414. */
  2415. if ((ext4_blocks_count(es) >
  2416. (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
  2417. (ext4_blocks_count(es) >
  2418. (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
  2419. ext4_msg(sb, KERN_ERR, "filesystem"
  2420. " too large to mount safely on this system");
  2421. if (sizeof(sector_t) < 8)
  2422. ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
  2423. ret = -EFBIG;
  2424. goto failed_mount;
  2425. }
  2426. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  2427. goto cantfind_ext4;
  2428. /* check blocks count against device size */
  2429. blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  2430. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  2431. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  2432. "exceeds size of device (%llu blocks)",
  2433. ext4_blocks_count(es), blocks_count);
  2434. goto failed_mount;
  2435. }
  2436. /*
  2437. * It makes no sense for the first data block to be beyond the end
  2438. * of the filesystem.
  2439. */
  2440. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  2441. ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
  2442. "block %u is beyond end of filesystem (%llu)",
  2443. le32_to_cpu(es->s_first_data_block),
  2444. ext4_blocks_count(es));
  2445. goto failed_mount;
  2446. }
  2447. blocks_count = (ext4_blocks_count(es) -
  2448. le32_to_cpu(es->s_first_data_block) +
  2449. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  2450. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  2451. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  2452. ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
  2453. "(block count %llu, first data block %u, "
  2454. "blocks per group %lu)", sbi->s_groups_count,
  2455. ext4_blocks_count(es),
  2456. le32_to_cpu(es->s_first_data_block),
  2457. EXT4_BLOCKS_PER_GROUP(sb));
  2458. goto failed_mount;
  2459. }
  2460. sbi->s_groups_count = blocks_count;
  2461. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  2462. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  2463. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  2464. EXT4_DESC_PER_BLOCK(sb);
  2465. sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
  2466. GFP_KERNEL);
  2467. if (sbi->s_group_desc == NULL) {
  2468. ext4_msg(sb, KERN_ERR, "not enough memory");
  2469. goto failed_mount;
  2470. }
  2471. #ifdef CONFIG_PROC_FS
  2472. if (ext4_proc_root)
  2473. sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
  2474. #endif
  2475. bgl_lock_init(sbi->s_blockgroup_lock);
  2476. for (i = 0; i < db_count; i++) {
  2477. block = descriptor_loc(sb, logical_sb_block, i);
  2478. sbi->s_group_desc[i] = sb_bread(sb, block);
  2479. if (!sbi->s_group_desc[i]) {
  2480. ext4_msg(sb, KERN_ERR,
  2481. "can't read group descriptor %d", i);
  2482. db_count = i;
  2483. goto failed_mount2;
  2484. }
  2485. }
  2486. if (!ext4_check_descriptors(sb)) {
  2487. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  2488. goto failed_mount2;
  2489. }
  2490. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  2491. if (!ext4_fill_flex_info(sb)) {
  2492. ext4_msg(sb, KERN_ERR,
  2493. "unable to initialize "
  2494. "flex_bg meta info!");
  2495. goto failed_mount2;
  2496. }
  2497. sbi->s_gdb_count = db_count;
  2498. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  2499. spin_lock_init(&sbi->s_next_gen_lock);
  2500. sbi->s_stripe = ext4_get_stripe_size(sbi);
  2501. sbi->s_max_writeback_mb_bump = 128;
  2502. /*
  2503. * set up enough so that it can read an inode
  2504. */
  2505. if (!test_opt(sb, NOLOAD) &&
  2506. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  2507. sb->s_op = &ext4_sops;
  2508. else
  2509. sb->s_op = &ext4_nojournal_sops;
  2510. sb->s_export_op = &ext4_export_ops;
  2511. sb->s_xattr = ext4_xattr_handlers;
  2512. #ifdef CONFIG_QUOTA
  2513. sb->s_qcop = &ext4_qctl_operations;
  2514. sb->dq_op = &ext4_quota_operations;
  2515. #endif
  2516. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  2517. mutex_init(&sbi->s_orphan_lock);
  2518. mutex_init(&sbi->s_resize_lock);
  2519. sb->s_root = NULL;
  2520. needs_recovery = (es->s_last_orphan != 0 ||
  2521. EXT4_HAS_INCOMPAT_FEATURE(sb,
  2522. EXT4_FEATURE_INCOMPAT_RECOVER));
  2523. /*
  2524. * The first inode we look at is the journal inode. Don't try
  2525. * root first: it may be modified in the journal!
  2526. */
  2527. if (!test_opt(sb, NOLOAD) &&
  2528. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  2529. if (ext4_load_journal(sb, es, journal_devnum))
  2530. goto failed_mount3;
  2531. } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
  2532. EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  2533. ext4_msg(sb, KERN_ERR, "required journal recovery "
  2534. "suppressed and not mounted read-only");
  2535. goto failed_mount_wq;
  2536. } else {
  2537. clear_opt(sbi->s_mount_opt, DATA_FLAGS);
  2538. set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
  2539. sbi->s_journal = NULL;
  2540. needs_recovery = 0;
  2541. goto no_journal;
  2542. }
  2543. if (ext4_blocks_count(es) > 0xffffffffULL &&
  2544. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  2545. JBD2_FEATURE_INCOMPAT_64BIT)) {
  2546. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  2547. goto failed_mount_wq;
  2548. }
  2549. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  2550. jbd2_journal_set_features(sbi->s_journal,
  2551. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2552. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2553. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  2554. jbd2_journal_set_features(sbi->s_journal,
  2555. JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
  2556. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  2557. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2558. } else {
  2559. jbd2_journal_clear_features(sbi->s_journal,
  2560. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2561. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2562. }
  2563. /* We have now updated the journal if required, so we can
  2564. * validate the data journaling mode. */
  2565. switch (test_opt(sb, DATA_FLAGS)) {
  2566. case 0:
  2567. /* No mode set, assume a default based on the journal
  2568. * capabilities: ORDERED_DATA if the journal can
  2569. * cope, else JOURNAL_DATA
  2570. */
  2571. if (jbd2_journal_check_available_features
  2572. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  2573. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  2574. else
  2575. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  2576. break;
  2577. case EXT4_MOUNT_ORDERED_DATA:
  2578. case EXT4_MOUNT_WRITEBACK_DATA:
  2579. if (!jbd2_journal_check_available_features
  2580. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  2581. ext4_msg(sb, KERN_ERR, "Journal does not support "
  2582. "requested data journaling mode");
  2583. goto failed_mount_wq;
  2584. }
  2585. default:
  2586. break;
  2587. }
  2588. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  2589. no_journal:
  2590. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  2591. ext4_count_free_blocks(sb));
  2592. if (!err)
  2593. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  2594. ext4_count_free_inodes(sb));
  2595. if (!err)
  2596. err = percpu_counter_init(&sbi->s_dirs_counter,
  2597. ext4_count_dirs(sb));
  2598. if (!err)
  2599. err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
  2600. if (err) {
  2601. ext4_msg(sb, KERN_ERR, "insufficient memory");
  2602. goto failed_mount_wq;
  2603. }
  2604. if (test_opt(sb, NOBH)) {
  2605. if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
  2606. ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
  2607. "its supported only with writeback mode");
  2608. clear_opt(sbi->s_mount_opt, NOBH);
  2609. }
  2610. if (test_opt(sb, DIOREAD_NOLOCK)) {
  2611. ext4_msg(sb, KERN_WARNING, "dioread_nolock option is "
  2612. "not supported with nobh mode");
  2613. goto failed_mount_wq;
  2614. }
  2615. }
  2616. EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
  2617. if (!EXT4_SB(sb)->dio_unwritten_wq) {
  2618. printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
  2619. goto failed_mount_wq;
  2620. }
  2621. /*
  2622. * The jbd2_journal_load will have done any necessary log recovery,
  2623. * so we can safely mount the rest of the filesystem now.
  2624. */
  2625. root = ext4_iget(sb, EXT4_ROOT_INO);
  2626. if (IS_ERR(root)) {
  2627. ext4_msg(sb, KERN_ERR, "get root inode failed");
  2628. ret = PTR_ERR(root);
  2629. goto failed_mount4;
  2630. }
  2631. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  2632. iput(root);
  2633. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  2634. goto failed_mount4;
  2635. }
  2636. sb->s_root = d_alloc_root(root);
  2637. if (!sb->s_root) {
  2638. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  2639. iput(root);
  2640. ret = -ENOMEM;
  2641. goto failed_mount4;
  2642. }
  2643. ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
  2644. /* determine the minimum size of new large inodes, if present */
  2645. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  2646. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  2647. EXT4_GOOD_OLD_INODE_SIZE;
  2648. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2649. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
  2650. if (sbi->s_want_extra_isize <
  2651. le16_to_cpu(es->s_want_extra_isize))
  2652. sbi->s_want_extra_isize =
  2653. le16_to_cpu(es->s_want_extra_isize);
  2654. if (sbi->s_want_extra_isize <
  2655. le16_to_cpu(es->s_min_extra_isize))
  2656. sbi->s_want_extra_isize =
  2657. le16_to_cpu(es->s_min_extra_isize);
  2658. }
  2659. }
  2660. /* Check if enough inode space is available */
  2661. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  2662. sbi->s_inode_size) {
  2663. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  2664. EXT4_GOOD_OLD_INODE_SIZE;
  2665. ext4_msg(sb, KERN_INFO, "required extra inode space not"
  2666. "available");
  2667. }
  2668. if (test_opt(sb, DELALLOC) &&
  2669. (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
  2670. ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
  2671. "requested data journaling mode");
  2672. clear_opt(sbi->s_mount_opt, DELALLOC);
  2673. }
  2674. if (test_opt(sb, DIOREAD_NOLOCK)) {
  2675. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  2676. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  2677. "option - requested data journaling mode");
  2678. clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
  2679. }
  2680. if (sb->s_blocksize < PAGE_SIZE) {
  2681. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  2682. "option - block size is too small");
  2683. clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
  2684. }
  2685. }
  2686. err = ext4_setup_system_zone(sb);
  2687. if (err) {
  2688. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  2689. "zone (%d)", err);
  2690. goto failed_mount4;
  2691. }
  2692. ext4_ext_init(sb);
  2693. err = ext4_mb_init(sb, needs_recovery);
  2694. if (err) {
  2695. ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
  2696. err);
  2697. goto failed_mount4;
  2698. }
  2699. sbi->s_kobj.kset = ext4_kset;
  2700. init_completion(&sbi->s_kobj_unregister);
  2701. err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
  2702. "%s", sb->s_id);
  2703. if (err) {
  2704. ext4_mb_release(sb);
  2705. ext4_ext_release(sb);
  2706. goto failed_mount4;
  2707. };
  2708. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  2709. ext4_orphan_cleanup(sb, es);
  2710. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  2711. if (needs_recovery) {
  2712. ext4_msg(sb, KERN_INFO, "recovery complete");
  2713. ext4_mark_recovery_complete(sb, es);
  2714. }
  2715. if (EXT4_SB(sb)->s_journal) {
  2716. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  2717. descr = " journalled data mode";
  2718. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  2719. descr = " ordered data mode";
  2720. else
  2721. descr = " writeback data mode";
  2722. } else
  2723. descr = "out journal";
  2724. ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
  2725. "Opts: %s", descr, orig_data);
  2726. lock_kernel();
  2727. kfree(orig_data);
  2728. return 0;
  2729. cantfind_ext4:
  2730. if (!silent)
  2731. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  2732. goto failed_mount;
  2733. failed_mount4:
  2734. ext4_msg(sb, KERN_ERR, "mount failed");
  2735. destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
  2736. failed_mount_wq:
  2737. ext4_release_system_zone(sb);
  2738. if (sbi->s_journal) {
  2739. jbd2_journal_destroy(sbi->s_journal);
  2740. sbi->s_journal = NULL;
  2741. }
  2742. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  2743. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  2744. percpu_counter_destroy(&sbi->s_dirs_counter);
  2745. percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
  2746. failed_mount3:
  2747. if (sbi->s_flex_groups) {
  2748. if (is_vmalloc_addr(sbi->s_flex_groups))
  2749. vfree(sbi->s_flex_groups);
  2750. else
  2751. kfree(sbi->s_flex_groups);
  2752. }
  2753. failed_mount2:
  2754. for (i = 0; i < db_count; i++)
  2755. brelse(sbi->s_group_desc[i]);
  2756. kfree(sbi->s_group_desc);
  2757. failed_mount:
  2758. if (sbi->s_proc) {
  2759. remove_proc_entry(sb->s_id, ext4_proc_root);
  2760. }
  2761. #ifdef CONFIG_QUOTA
  2762. for (i = 0; i < MAXQUOTAS; i++)
  2763. kfree(sbi->s_qf_names[i]);
  2764. #endif
  2765. ext4_blkdev_remove(sbi);
  2766. brelse(bh);
  2767. out_fail:
  2768. sb->s_fs_info = NULL;
  2769. kfree(sbi->s_blockgroup_lock);
  2770. kfree(sbi);
  2771. lock_kernel();
  2772. kfree(orig_data);
  2773. return ret;
  2774. }
  2775. /*
  2776. * Setup any per-fs journal parameters now. We'll do this both on
  2777. * initial mount, once the journal has been initialised but before we've
  2778. * done any recovery; and again on any subsequent remount.
  2779. */
  2780. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  2781. {
  2782. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2783. journal->j_commit_interval = sbi->s_commit_interval;
  2784. journal->j_min_batch_time = sbi->s_min_batch_time;
  2785. journal->j_max_batch_time = sbi->s_max_batch_time;
  2786. spin_lock(&journal->j_state_lock);
  2787. if (test_opt(sb, BARRIER))
  2788. journal->j_flags |= JBD2_BARRIER;
  2789. else
  2790. journal->j_flags &= ~JBD2_BARRIER;
  2791. if (test_opt(sb, DATA_ERR_ABORT))
  2792. journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
  2793. else
  2794. journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
  2795. spin_unlock(&journal->j_state_lock);
  2796. }
  2797. static journal_t *ext4_get_journal(struct super_block *sb,
  2798. unsigned int journal_inum)
  2799. {
  2800. struct inode *journal_inode;
  2801. journal_t *journal;
  2802. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  2803. /* First, test for the existence of a valid inode on disk. Bad
  2804. * things happen if we iget() an unused inode, as the subsequent
  2805. * iput() will try to delete it. */
  2806. journal_inode = ext4_iget(sb, journal_inum);
  2807. if (IS_ERR(journal_inode)) {
  2808. ext4_msg(sb, KERN_ERR, "no journal found");
  2809. return NULL;
  2810. }
  2811. if (!journal_inode->i_nlink) {
  2812. make_bad_inode(journal_inode);
  2813. iput(journal_inode);
  2814. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  2815. return NULL;
  2816. }
  2817. jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
  2818. journal_inode, journal_inode->i_size);
  2819. if (!S_ISREG(journal_inode->i_mode)) {
  2820. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  2821. iput(journal_inode);
  2822. return NULL;
  2823. }
  2824. journal = jbd2_journal_init_inode(journal_inode);
  2825. if (!journal) {
  2826. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  2827. iput(journal_inode);
  2828. return NULL;
  2829. }
  2830. journal->j_private = sb;
  2831. ext4_init_journal_params(sb, journal);
  2832. return journal;
  2833. }
  2834. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  2835. dev_t j_dev)
  2836. {
  2837. struct buffer_head *bh;
  2838. journal_t *journal;
  2839. ext4_fsblk_t start;
  2840. ext4_fsblk_t len;
  2841. int hblock, blocksize;
  2842. ext4_fsblk_t sb_block;
  2843. unsigned long offset;
  2844. struct ext4_super_block *es;
  2845. struct block_device *bdev;
  2846. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  2847. bdev = ext4_blkdev_get(j_dev, sb);
  2848. if (bdev == NULL)
  2849. return NULL;
  2850. if (bd_claim(bdev, sb)) {
  2851. ext4_msg(sb, KERN_ERR,
  2852. "failed to claim external journal device");
  2853. blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
  2854. return NULL;
  2855. }
  2856. blocksize = sb->s_blocksize;
  2857. hblock = bdev_logical_block_size(bdev);
  2858. if (blocksize < hblock) {
  2859. ext4_msg(sb, KERN_ERR,
  2860. "blocksize too small for journal device");
  2861. goto out_bdev;
  2862. }
  2863. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  2864. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  2865. set_blocksize(bdev, blocksize);
  2866. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  2867. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  2868. "external journal");
  2869. goto out_bdev;
  2870. }
  2871. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2872. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  2873. !(le32_to_cpu(es->s_feature_incompat) &
  2874. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  2875. ext4_msg(sb, KERN_ERR, "external journal has "
  2876. "bad superblock");
  2877. brelse(bh);
  2878. goto out_bdev;
  2879. }
  2880. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  2881. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  2882. brelse(bh);
  2883. goto out_bdev;
  2884. }
  2885. len = ext4_blocks_count(es);
  2886. start = sb_block + 1;
  2887. brelse(bh); /* we're done with the superblock */
  2888. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  2889. start, len, blocksize);
  2890. if (!journal) {
  2891. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  2892. goto out_bdev;
  2893. }
  2894. journal->j_private = sb;
  2895. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  2896. wait_on_buffer(journal->j_sb_buffer);
  2897. if (!buffer_uptodate(journal->j_sb_buffer)) {
  2898. ext4_msg(sb, KERN_ERR, "I/O error on journal device");
  2899. goto out_journal;
  2900. }
  2901. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  2902. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  2903. "user (unsupported) - %d",
  2904. be32_to_cpu(journal->j_superblock->s_nr_users));
  2905. goto out_journal;
  2906. }
  2907. EXT4_SB(sb)->journal_bdev = bdev;
  2908. ext4_init_journal_params(sb, journal);
  2909. return journal;
  2910. out_journal:
  2911. jbd2_journal_destroy(journal);
  2912. out_bdev:
  2913. ext4_blkdev_put(bdev);
  2914. return NULL;
  2915. }
  2916. static int ext4_load_journal(struct super_block *sb,
  2917. struct ext4_super_block *es,
  2918. unsigned long journal_devnum)
  2919. {
  2920. journal_t *journal;
  2921. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  2922. dev_t journal_dev;
  2923. int err = 0;
  2924. int really_read_only;
  2925. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  2926. if (journal_devnum &&
  2927. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  2928. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  2929. "numbers have changed");
  2930. journal_dev = new_decode_dev(journal_devnum);
  2931. } else
  2932. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  2933. really_read_only = bdev_read_only(sb->s_bdev);
  2934. /*
  2935. * Are we loading a blank journal or performing recovery after a
  2936. * crash? For recovery, we need to check in advance whether we
  2937. * can get read-write access to the device.
  2938. */
  2939. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  2940. if (sb->s_flags & MS_RDONLY) {
  2941. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  2942. "required on readonly filesystem");
  2943. if (really_read_only) {
  2944. ext4_msg(sb, KERN_ERR, "write access "
  2945. "unavailable, cannot proceed");
  2946. return -EROFS;
  2947. }
  2948. ext4_msg(sb, KERN_INFO, "write access will "
  2949. "be enabled during recovery");
  2950. }
  2951. }
  2952. if (journal_inum && journal_dev) {
  2953. ext4_msg(sb, KERN_ERR, "filesystem has both journal "
  2954. "and inode journals!");
  2955. return -EINVAL;
  2956. }
  2957. if (journal_inum) {
  2958. if (!(journal = ext4_get_journal(sb, journal_inum)))
  2959. return -EINVAL;
  2960. } else {
  2961. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  2962. return -EINVAL;
  2963. }
  2964. if (!(journal->j_flags & JBD2_BARRIER))
  2965. ext4_msg(sb, KERN_INFO, "barriers disabled");
  2966. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  2967. err = jbd2_journal_update_format(journal);
  2968. if (err) {
  2969. ext4_msg(sb, KERN_ERR, "error updating journal");
  2970. jbd2_journal_destroy(journal);
  2971. return err;
  2972. }
  2973. }
  2974. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  2975. err = jbd2_journal_wipe(journal, !really_read_only);
  2976. if (!err)
  2977. err = jbd2_journal_load(journal);
  2978. if (err) {
  2979. ext4_msg(sb, KERN_ERR, "error loading journal");
  2980. jbd2_journal_destroy(journal);
  2981. return err;
  2982. }
  2983. EXT4_SB(sb)->s_journal = journal;
  2984. ext4_clear_journal_err(sb, es);
  2985. if (journal_devnum &&
  2986. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  2987. es->s_journal_dev = cpu_to_le32(journal_devnum);
  2988. /* Make sure we flush the recovery flag to disk. */
  2989. ext4_commit_super(sb, 1);
  2990. }
  2991. return 0;
  2992. }
  2993. static int ext4_commit_super(struct super_block *sb, int sync)
  2994. {
  2995. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  2996. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  2997. int error = 0;
  2998. if (!sbh)
  2999. return error;
  3000. if (buffer_write_io_error(sbh)) {
  3001. /*
  3002. * Oh, dear. A previous attempt to write the
  3003. * superblock failed. This could happen because the
  3004. * USB device was yanked out. Or it could happen to
  3005. * be a transient write error and maybe the block will
  3006. * be remapped. Nothing we can do but to retry the
  3007. * write and hope for the best.
  3008. */
  3009. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  3010. "superblock detected");
  3011. clear_buffer_write_io_error(sbh);
  3012. set_buffer_uptodate(sbh);
  3013. }
  3014. /*
  3015. * If the file system is mounted read-only, don't update the
  3016. * superblock write time. This avoids updating the superblock
  3017. * write time when we are mounting the root file system
  3018. * read/only but we need to replay the journal; at that point,
  3019. * for people who are east of GMT and who make their clock
  3020. * tick in localtime for Windows bug-for-bug compatibility,
  3021. * the clock is set in the future, and this will cause e2fsck
  3022. * to complain and force a full file system check.
  3023. */
  3024. if (!(sb->s_flags & MS_RDONLY))
  3025. es->s_wtime = cpu_to_le32(get_seconds());
  3026. es->s_kbytes_written =
  3027. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
  3028. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  3029. EXT4_SB(sb)->s_sectors_written_start) >> 1));
  3030. ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
  3031. &EXT4_SB(sb)->s_freeblocks_counter));
  3032. es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
  3033. &EXT4_SB(sb)->s_freeinodes_counter));
  3034. sb->s_dirt = 0;
  3035. BUFFER_TRACE(sbh, "marking dirty");
  3036. mark_buffer_dirty(sbh);
  3037. if (sync) {
  3038. error = sync_dirty_buffer(sbh);
  3039. if (error)
  3040. return error;
  3041. error = buffer_write_io_error(sbh);
  3042. if (error) {
  3043. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  3044. "superblock");
  3045. clear_buffer_write_io_error(sbh);
  3046. set_buffer_uptodate(sbh);
  3047. }
  3048. }
  3049. return error;
  3050. }
  3051. /*
  3052. * Have we just finished recovery? If so, and if we are mounting (or
  3053. * remounting) the filesystem readonly, then we will end up with a
  3054. * consistent fs on disk. Record that fact.
  3055. */
  3056. static void ext4_mark_recovery_complete(struct super_block *sb,
  3057. struct ext4_super_block *es)
  3058. {
  3059. journal_t *journal = EXT4_SB(sb)->s_journal;
  3060. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3061. BUG_ON(journal != NULL);
  3062. return;
  3063. }
  3064. jbd2_journal_lock_updates(journal);
  3065. if (jbd2_journal_flush(journal) < 0)
  3066. goto out;
  3067. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  3068. sb->s_flags & MS_RDONLY) {
  3069. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3070. ext4_commit_super(sb, 1);
  3071. }
  3072. out:
  3073. jbd2_journal_unlock_updates(journal);
  3074. }
  3075. /*
  3076. * If we are mounting (or read-write remounting) a filesystem whose journal
  3077. * has recorded an error from a previous lifetime, move that error to the
  3078. * main filesystem now.
  3079. */
  3080. static void ext4_clear_journal_err(struct super_block *sb,
  3081. struct ext4_super_block *es)
  3082. {
  3083. journal_t *journal;
  3084. int j_errno;
  3085. const char *errstr;
  3086. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3087. journal = EXT4_SB(sb)->s_journal;
  3088. /*
  3089. * Now check for any error status which may have been recorded in the
  3090. * journal by a prior ext4_error() or ext4_abort()
  3091. */
  3092. j_errno = jbd2_journal_errno(journal);
  3093. if (j_errno) {
  3094. char nbuf[16];
  3095. errstr = ext4_decode_error(sb, j_errno, nbuf);
  3096. ext4_warning(sb, "Filesystem error recorded "
  3097. "from previous mount: %s", errstr);
  3098. ext4_warning(sb, "Marking fs in need of filesystem check.");
  3099. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  3100. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  3101. ext4_commit_super(sb, 1);
  3102. jbd2_journal_clear_err(journal);
  3103. }
  3104. }
  3105. /*
  3106. * Force the running and committing transactions to commit,
  3107. * and wait on the commit.
  3108. */
  3109. int ext4_force_commit(struct super_block *sb)
  3110. {
  3111. journal_t *journal;
  3112. int ret = 0;
  3113. if (sb->s_flags & MS_RDONLY)
  3114. return 0;
  3115. journal = EXT4_SB(sb)->s_journal;
  3116. if (journal) {
  3117. vfs_check_frozen(sb, SB_FREEZE_WRITE);
  3118. ret = ext4_journal_force_commit(journal);
  3119. }
  3120. return ret;
  3121. }
  3122. static void ext4_write_super(struct super_block *sb)
  3123. {
  3124. lock_super(sb);
  3125. ext4_commit_super(sb, 1);
  3126. unlock_super(sb);
  3127. }
  3128. static int ext4_sync_fs(struct super_block *sb, int wait)
  3129. {
  3130. int ret = 0;
  3131. tid_t target;
  3132. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3133. trace_ext4_sync_fs(sb, wait);
  3134. flush_workqueue(sbi->dio_unwritten_wq);
  3135. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  3136. if (wait)
  3137. jbd2_log_wait_commit(sbi->s_journal, target);
  3138. }
  3139. return ret;
  3140. }
  3141. /*
  3142. * LVM calls this function before a (read-only) snapshot is created. This
  3143. * gives us a chance to flush the journal completely and mark the fs clean.
  3144. */
  3145. static int ext4_freeze(struct super_block *sb)
  3146. {
  3147. int error = 0;
  3148. journal_t *journal;
  3149. if (sb->s_flags & MS_RDONLY)
  3150. return 0;
  3151. journal = EXT4_SB(sb)->s_journal;
  3152. /* Now we set up the journal barrier. */
  3153. jbd2_journal_lock_updates(journal);
  3154. /*
  3155. * Don't clear the needs_recovery flag if we failed to flush
  3156. * the journal.
  3157. */
  3158. error = jbd2_journal_flush(journal);
  3159. if (error < 0)
  3160. goto out;
  3161. /* Journal blocked and flushed, clear needs_recovery flag. */
  3162. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3163. error = ext4_commit_super(sb, 1);
  3164. out:
  3165. /* we rely on s_frozen to stop further updates */
  3166. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3167. return error;
  3168. }
  3169. /*
  3170. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  3171. * flag here, even though the filesystem is not technically dirty yet.
  3172. */
  3173. static int ext4_unfreeze(struct super_block *sb)
  3174. {
  3175. if (sb->s_flags & MS_RDONLY)
  3176. return 0;
  3177. lock_super(sb);
  3178. /* Reset the needs_recovery flag before the fs is unlocked. */
  3179. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3180. ext4_commit_super(sb, 1);
  3181. unlock_super(sb);
  3182. return 0;
  3183. }
  3184. static int ext4_remount(struct super_block *sb, int *flags, char *data)
  3185. {
  3186. struct ext4_super_block *es;
  3187. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3188. ext4_fsblk_t n_blocks_count = 0;
  3189. unsigned long old_sb_flags;
  3190. struct ext4_mount_options old_opts;
  3191. int enable_quota = 0;
  3192. ext4_group_t g;
  3193. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  3194. int err;
  3195. #ifdef CONFIG_QUOTA
  3196. int i;
  3197. #endif
  3198. char *orig_data = kstrdup(data, GFP_KERNEL);
  3199. lock_kernel();
  3200. /* Store the original options */
  3201. lock_super(sb);
  3202. old_sb_flags = sb->s_flags;
  3203. old_opts.s_mount_opt = sbi->s_mount_opt;
  3204. old_opts.s_resuid = sbi->s_resuid;
  3205. old_opts.s_resgid = sbi->s_resgid;
  3206. old_opts.s_commit_interval = sbi->s_commit_interval;
  3207. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  3208. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  3209. #ifdef CONFIG_QUOTA
  3210. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  3211. for (i = 0; i < MAXQUOTAS; i++)
  3212. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  3213. #endif
  3214. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  3215. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  3216. /*
  3217. * Allow the "check" option to be passed as a remount option.
  3218. */
  3219. if (!parse_options(data, sb, NULL, &journal_ioprio,
  3220. &n_blocks_count, 1)) {
  3221. err = -EINVAL;
  3222. goto restore_opts;
  3223. }
  3224. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
  3225. ext4_abort(sb, __func__, "Abort forced by user");
  3226. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  3227. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  3228. es = sbi->s_es;
  3229. if (sbi->s_journal) {
  3230. ext4_init_journal_params(sb, sbi->s_journal);
  3231. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3232. }
  3233. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  3234. n_blocks_count > ext4_blocks_count(es)) {
  3235. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
  3236. err = -EROFS;
  3237. goto restore_opts;
  3238. }
  3239. if (*flags & MS_RDONLY) {
  3240. err = dquot_suspend(sb, -1);
  3241. if (err < 0)
  3242. goto restore_opts;
  3243. /*
  3244. * First of all, the unconditional stuff we have to do
  3245. * to disable replay of the journal when we next remount
  3246. */
  3247. sb->s_flags |= MS_RDONLY;
  3248. /*
  3249. * OK, test if we are remounting a valid rw partition
  3250. * readonly, and if so set the rdonly flag and then
  3251. * mark the partition as valid again.
  3252. */
  3253. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  3254. (sbi->s_mount_state & EXT4_VALID_FS))
  3255. es->s_state = cpu_to_le16(sbi->s_mount_state);
  3256. if (sbi->s_journal)
  3257. ext4_mark_recovery_complete(sb, es);
  3258. } else {
  3259. /* Make sure we can mount this feature set readwrite */
  3260. if (!ext4_feature_set_ok(sb, 0)) {
  3261. err = -EROFS;
  3262. goto restore_opts;
  3263. }
  3264. /*
  3265. * Make sure the group descriptor checksums
  3266. * are sane. If they aren't, refuse to remount r/w.
  3267. */
  3268. for (g = 0; g < sbi->s_groups_count; g++) {
  3269. struct ext4_group_desc *gdp =
  3270. ext4_get_group_desc(sb, g, NULL);
  3271. if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
  3272. ext4_msg(sb, KERN_ERR,
  3273. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  3274. g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
  3275. le16_to_cpu(gdp->bg_checksum));
  3276. err = -EINVAL;
  3277. goto restore_opts;
  3278. }
  3279. }
  3280. /*
  3281. * If we have an unprocessed orphan list hanging
  3282. * around from a previously readonly bdev mount,
  3283. * require a full umount/remount for now.
  3284. */
  3285. if (es->s_last_orphan) {
  3286. ext4_msg(sb, KERN_WARNING, "Couldn't "
  3287. "remount RDWR because of unprocessed "
  3288. "orphan inode list. Please "
  3289. "umount/remount instead");
  3290. err = -EINVAL;
  3291. goto restore_opts;
  3292. }
  3293. /*
  3294. * Mounting a RDONLY partition read-write, so reread
  3295. * and store the current valid flag. (It may have
  3296. * been changed by e2fsck since we originally mounted
  3297. * the partition.)
  3298. */
  3299. if (sbi->s_journal)
  3300. ext4_clear_journal_err(sb, es);
  3301. sbi->s_mount_state = le16_to_cpu(es->s_state);
  3302. if ((err = ext4_group_extend(sb, es, n_blocks_count)))
  3303. goto restore_opts;
  3304. if (!ext4_setup_super(sb, es, 0))
  3305. sb->s_flags &= ~MS_RDONLY;
  3306. enable_quota = 1;
  3307. }
  3308. }
  3309. ext4_setup_system_zone(sb);
  3310. if (sbi->s_journal == NULL)
  3311. ext4_commit_super(sb, 1);
  3312. #ifdef CONFIG_QUOTA
  3313. /* Release old quota file names */
  3314. for (i = 0; i < MAXQUOTAS; i++)
  3315. if (old_opts.s_qf_names[i] &&
  3316. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3317. kfree(old_opts.s_qf_names[i]);
  3318. #endif
  3319. unlock_super(sb);
  3320. unlock_kernel();
  3321. if (enable_quota)
  3322. dquot_resume(sb, -1);
  3323. ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  3324. kfree(orig_data);
  3325. return 0;
  3326. restore_opts:
  3327. sb->s_flags = old_sb_flags;
  3328. sbi->s_mount_opt = old_opts.s_mount_opt;
  3329. sbi->s_resuid = old_opts.s_resuid;
  3330. sbi->s_resgid = old_opts.s_resgid;
  3331. sbi->s_commit_interval = old_opts.s_commit_interval;
  3332. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  3333. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  3334. #ifdef CONFIG_QUOTA
  3335. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  3336. for (i = 0; i < MAXQUOTAS; i++) {
  3337. if (sbi->s_qf_names[i] &&
  3338. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3339. kfree(sbi->s_qf_names[i]);
  3340. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  3341. }
  3342. #endif
  3343. unlock_super(sb);
  3344. unlock_kernel();
  3345. kfree(orig_data);
  3346. return err;
  3347. }
  3348. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  3349. {
  3350. struct super_block *sb = dentry->d_sb;
  3351. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3352. struct ext4_super_block *es = sbi->s_es;
  3353. u64 fsid;
  3354. if (test_opt(sb, MINIX_DF)) {
  3355. sbi->s_overhead_last = 0;
  3356. } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
  3357. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3358. ext4_fsblk_t overhead = 0;
  3359. /*
  3360. * Compute the overhead (FS structures). This is constant
  3361. * for a given filesystem unless the number of block groups
  3362. * changes so we cache the previous value until it does.
  3363. */
  3364. /*
  3365. * All of the blocks before first_data_block are
  3366. * overhead
  3367. */
  3368. overhead = le32_to_cpu(es->s_first_data_block);
  3369. /*
  3370. * Add the overhead attributed to the superblock and
  3371. * block group descriptors. If the sparse superblocks
  3372. * feature is turned on, then not all groups have this.
  3373. */
  3374. for (i = 0; i < ngroups; i++) {
  3375. overhead += ext4_bg_has_super(sb, i) +
  3376. ext4_bg_num_gdb(sb, i);
  3377. cond_resched();
  3378. }
  3379. /*
  3380. * Every block group has an inode bitmap, a block
  3381. * bitmap, and an inode table.
  3382. */
  3383. overhead += ngroups * (2 + sbi->s_itb_per_group);
  3384. sbi->s_overhead_last = overhead;
  3385. smp_wmb();
  3386. sbi->s_blocks_last = ext4_blocks_count(es);
  3387. }
  3388. buf->f_type = EXT4_SUPER_MAGIC;
  3389. buf->f_bsize = sb->s_blocksize;
  3390. buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
  3391. buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
  3392. percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
  3393. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  3394. if (buf->f_bfree < ext4_r_blocks_count(es))
  3395. buf->f_bavail = 0;
  3396. buf->f_files = le32_to_cpu(es->s_inodes_count);
  3397. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  3398. buf->f_namelen = EXT4_NAME_LEN;
  3399. fsid = le64_to_cpup((void *)es->s_uuid) ^
  3400. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  3401. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  3402. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  3403. return 0;
  3404. }
  3405. /* Helper function for writing quotas on sync - we need to start transaction
  3406. * before quota file is locked for write. Otherwise the are possible deadlocks:
  3407. * Process 1 Process 2
  3408. * ext4_create() quota_sync()
  3409. * jbd2_journal_start() write_dquot()
  3410. * dquot_initialize() down(dqio_mutex)
  3411. * down(dqio_mutex) jbd2_journal_start()
  3412. *
  3413. */
  3414. #ifdef CONFIG_QUOTA
  3415. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  3416. {
  3417. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  3418. }
  3419. static int ext4_write_dquot(struct dquot *dquot)
  3420. {
  3421. int ret, err;
  3422. handle_t *handle;
  3423. struct inode *inode;
  3424. inode = dquot_to_inode(dquot);
  3425. handle = ext4_journal_start(inode,
  3426. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  3427. if (IS_ERR(handle))
  3428. return PTR_ERR(handle);
  3429. ret = dquot_commit(dquot);
  3430. err = ext4_journal_stop(handle);
  3431. if (!ret)
  3432. ret = err;
  3433. return ret;
  3434. }
  3435. static int ext4_acquire_dquot(struct dquot *dquot)
  3436. {
  3437. int ret, err;
  3438. handle_t *handle;
  3439. handle = ext4_journal_start(dquot_to_inode(dquot),
  3440. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  3441. if (IS_ERR(handle))
  3442. return PTR_ERR(handle);
  3443. ret = dquot_acquire(dquot);
  3444. err = ext4_journal_stop(handle);
  3445. if (!ret)
  3446. ret = err;
  3447. return ret;
  3448. }
  3449. static int ext4_release_dquot(struct dquot *dquot)
  3450. {
  3451. int ret, err;
  3452. handle_t *handle;
  3453. handle = ext4_journal_start(dquot_to_inode(dquot),
  3454. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  3455. if (IS_ERR(handle)) {
  3456. /* Release dquot anyway to avoid endless cycle in dqput() */
  3457. dquot_release(dquot);
  3458. return PTR_ERR(handle);
  3459. }
  3460. ret = dquot_release(dquot);
  3461. err = ext4_journal_stop(handle);
  3462. if (!ret)
  3463. ret = err;
  3464. return ret;
  3465. }
  3466. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  3467. {
  3468. /* Are we journaling quotas? */
  3469. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  3470. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  3471. dquot_mark_dquot_dirty(dquot);
  3472. return ext4_write_dquot(dquot);
  3473. } else {
  3474. return dquot_mark_dquot_dirty(dquot);
  3475. }
  3476. }
  3477. static int ext4_write_info(struct super_block *sb, int type)
  3478. {
  3479. int ret, err;
  3480. handle_t *handle;
  3481. /* Data block + inode block */
  3482. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  3483. if (IS_ERR(handle))
  3484. return PTR_ERR(handle);
  3485. ret = dquot_commit_info(sb, type);
  3486. err = ext4_journal_stop(handle);
  3487. if (!ret)
  3488. ret = err;
  3489. return ret;
  3490. }
  3491. /*
  3492. * Turn on quotas during mount time - we need to find
  3493. * the quota file and such...
  3494. */
  3495. static int ext4_quota_on_mount(struct super_block *sb, int type)
  3496. {
  3497. return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  3498. EXT4_SB(sb)->s_jquota_fmt, type);
  3499. }
  3500. /*
  3501. * Standard function to be called on quota_on
  3502. */
  3503. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  3504. char *name)
  3505. {
  3506. int err;
  3507. struct path path;
  3508. if (!test_opt(sb, QUOTA))
  3509. return -EINVAL;
  3510. err = kern_path(name, LOOKUP_FOLLOW, &path);
  3511. if (err)
  3512. return err;
  3513. /* Quotafile not on the same filesystem? */
  3514. if (path.mnt->mnt_sb != sb) {
  3515. path_put(&path);
  3516. return -EXDEV;
  3517. }
  3518. /* Journaling quota? */
  3519. if (EXT4_SB(sb)->s_qf_names[type]) {
  3520. /* Quotafile not in fs root? */
  3521. if (path.dentry->d_parent != sb->s_root)
  3522. ext4_msg(sb, KERN_WARNING,
  3523. "Quota file not on filesystem root. "
  3524. "Journaled quota will not work");
  3525. }
  3526. /*
  3527. * When we journal data on quota file, we have to flush journal to see
  3528. * all updates to the file when we bypass pagecache...
  3529. */
  3530. if (EXT4_SB(sb)->s_journal &&
  3531. ext4_should_journal_data(path.dentry->d_inode)) {
  3532. /*
  3533. * We don't need to lock updates but journal_flush() could
  3534. * otherwise be livelocked...
  3535. */
  3536. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  3537. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  3538. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3539. if (err) {
  3540. path_put(&path);
  3541. return err;
  3542. }
  3543. }
  3544. err = dquot_quota_on_path(sb, type, format_id, &path);
  3545. path_put(&path);
  3546. return err;
  3547. }
  3548. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  3549. * acquiring the locks... As quota files are never truncated and quota code
  3550. * itself serializes the operations (and noone else should touch the files)
  3551. * we don't have to be afraid of races */
  3552. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  3553. size_t len, loff_t off)
  3554. {
  3555. struct inode *inode = sb_dqopt(sb)->files[type];
  3556. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  3557. int err = 0;
  3558. int offset = off & (sb->s_blocksize - 1);
  3559. int tocopy;
  3560. size_t toread;
  3561. struct buffer_head *bh;
  3562. loff_t i_size = i_size_read(inode);
  3563. if (off > i_size)
  3564. return 0;
  3565. if (off+len > i_size)
  3566. len = i_size-off;
  3567. toread = len;
  3568. while (toread > 0) {
  3569. tocopy = sb->s_blocksize - offset < toread ?
  3570. sb->s_blocksize - offset : toread;
  3571. bh = ext4_bread(NULL, inode, blk, 0, &err);
  3572. if (err)
  3573. return err;
  3574. if (!bh) /* A hole? */
  3575. memset(data, 0, tocopy);
  3576. else
  3577. memcpy(data, bh->b_data+offset, tocopy);
  3578. brelse(bh);
  3579. offset = 0;
  3580. toread -= tocopy;
  3581. data += tocopy;
  3582. blk++;
  3583. }
  3584. return len;
  3585. }
  3586. /* Write to quotafile (we know the transaction is already started and has
  3587. * enough credits) */
  3588. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  3589. const char *data, size_t len, loff_t off)
  3590. {
  3591. struct inode *inode = sb_dqopt(sb)->files[type];
  3592. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  3593. int err = 0;
  3594. int offset = off & (sb->s_blocksize - 1);
  3595. int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
  3596. struct buffer_head *bh;
  3597. handle_t *handle = journal_current_handle();
  3598. if (EXT4_SB(sb)->s_journal && !handle) {
  3599. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  3600. " cancelled because transaction is not started",
  3601. (unsigned long long)off, (unsigned long long)len);
  3602. return -EIO;
  3603. }
  3604. /*
  3605. * Since we account only one data block in transaction credits,
  3606. * then it is impossible to cross a block boundary.
  3607. */
  3608. if (sb->s_blocksize - offset < len) {
  3609. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  3610. " cancelled because not block aligned",
  3611. (unsigned long long)off, (unsigned long long)len);
  3612. return -EIO;
  3613. }
  3614. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  3615. bh = ext4_bread(handle, inode, blk, 1, &err);
  3616. if (!bh)
  3617. goto out;
  3618. if (journal_quota) {
  3619. err = ext4_journal_get_write_access(handle, bh);
  3620. if (err) {
  3621. brelse(bh);
  3622. goto out;
  3623. }
  3624. }
  3625. lock_buffer(bh);
  3626. memcpy(bh->b_data+offset, data, len);
  3627. flush_dcache_page(bh->b_page);
  3628. unlock_buffer(bh);
  3629. if (journal_quota)
  3630. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  3631. else {
  3632. /* Always do at least ordered writes for quotas */
  3633. err = ext4_jbd2_file_inode(handle, inode);
  3634. mark_buffer_dirty(bh);
  3635. }
  3636. brelse(bh);
  3637. out:
  3638. if (err) {
  3639. mutex_unlock(&inode->i_mutex);
  3640. return err;
  3641. }
  3642. if (inode->i_size < off + len) {
  3643. i_size_write(inode, off + len);
  3644. EXT4_I(inode)->i_disksize = inode->i_size;
  3645. }
  3646. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  3647. ext4_mark_inode_dirty(handle, inode);
  3648. mutex_unlock(&inode->i_mutex);
  3649. return len;
  3650. }
  3651. #endif
  3652. static int ext4_get_sb(struct file_system_type *fs_type, int flags,
  3653. const char *dev_name, void *data, struct vfsmount *mnt)
  3654. {
  3655. return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
  3656. }
  3657. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  3658. static struct file_system_type ext2_fs_type = {
  3659. .owner = THIS_MODULE,
  3660. .name = "ext2",
  3661. .get_sb = ext4_get_sb,
  3662. .kill_sb = kill_block_super,
  3663. .fs_flags = FS_REQUIRES_DEV,
  3664. };
  3665. static inline void register_as_ext2(void)
  3666. {
  3667. int err = register_filesystem(&ext2_fs_type);
  3668. if (err)
  3669. printk(KERN_WARNING
  3670. "EXT4-fs: Unable to register as ext2 (%d)\n", err);
  3671. }
  3672. static inline void unregister_as_ext2(void)
  3673. {
  3674. unregister_filesystem(&ext2_fs_type);
  3675. }
  3676. MODULE_ALIAS("ext2");
  3677. #else
  3678. static inline void register_as_ext2(void) { }
  3679. static inline void unregister_as_ext2(void) { }
  3680. #endif
  3681. #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  3682. static inline void register_as_ext3(void)
  3683. {
  3684. int err = register_filesystem(&ext3_fs_type);
  3685. if (err)
  3686. printk(KERN_WARNING
  3687. "EXT4-fs: Unable to register as ext3 (%d)\n", err);
  3688. }
  3689. static inline void unregister_as_ext3(void)
  3690. {
  3691. unregister_filesystem(&ext3_fs_type);
  3692. }
  3693. MODULE_ALIAS("ext3");
  3694. #else
  3695. static inline void register_as_ext3(void) { }
  3696. static inline void unregister_as_ext3(void) { }
  3697. #endif
  3698. static struct file_system_type ext4_fs_type = {
  3699. .owner = THIS_MODULE,
  3700. .name = "ext4",
  3701. .get_sb = ext4_get_sb,
  3702. .kill_sb = kill_block_super,
  3703. .fs_flags = FS_REQUIRES_DEV,
  3704. };
  3705. static int __init init_ext4_fs(void)
  3706. {
  3707. int err;
  3708. ext4_check_flag_values();
  3709. err = init_ext4_system_zone();
  3710. if (err)
  3711. return err;
  3712. ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
  3713. if (!ext4_kset)
  3714. goto out4;
  3715. ext4_proc_root = proc_mkdir("fs/ext4", NULL);
  3716. err = init_ext4_mballoc();
  3717. if (err)
  3718. goto out3;
  3719. err = init_ext4_xattr();
  3720. if (err)
  3721. goto out2;
  3722. err = init_inodecache();
  3723. if (err)
  3724. goto out1;
  3725. register_as_ext2();
  3726. register_as_ext3();
  3727. err = register_filesystem(&ext4_fs_type);
  3728. if (err)
  3729. goto out;
  3730. return 0;
  3731. out:
  3732. unregister_as_ext2();
  3733. unregister_as_ext3();
  3734. destroy_inodecache();
  3735. out1:
  3736. exit_ext4_xattr();
  3737. out2:
  3738. exit_ext4_mballoc();
  3739. out3:
  3740. remove_proc_entry("fs/ext4", NULL);
  3741. kset_unregister(ext4_kset);
  3742. out4:
  3743. exit_ext4_system_zone();
  3744. return err;
  3745. }
  3746. static void __exit exit_ext4_fs(void)
  3747. {
  3748. unregister_as_ext2();
  3749. unregister_as_ext3();
  3750. unregister_filesystem(&ext4_fs_type);
  3751. destroy_inodecache();
  3752. exit_ext4_xattr();
  3753. exit_ext4_mballoc();
  3754. remove_proc_entry("fs/ext4", NULL);
  3755. kset_unregister(ext4_kset);
  3756. exit_ext4_system_zone();
  3757. }
  3758. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  3759. MODULE_DESCRIPTION("Fourth Extended Filesystem");
  3760. MODULE_LICENSE("GPL");
  3761. module_init(init_ext4_fs)
  3762. module_exit(exit_ext4_fs)