super.c 112 KB

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