super.c 114 KB

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