super.c 106 KB

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