super.c 141 KB

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