super.c 95 KB

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