super.c 101 KB

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