super.c 116 KB

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