super.c 159 KB

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