super.c 153 KB

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