super.c 113 KB

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