super.c 117 KB

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