super.c 107 KB

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