super.c 112 KB

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