super.c 85 KB

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