super.c 113 KB

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