super.c 122 KB

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