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