super.c 121 KB

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