super.c 107 KB

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