super.c 117 KB

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