super.c 99 KB

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