super.c 122 KB

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