super.c 101 KB

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