super.c 113 KB

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