super.c 121 KB

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