super.c 113 KB

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