super.c 114 KB

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