super.c 93 KB

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