super.c 142 KB

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