super.c 103 KB

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