super.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163
  1. /*
  2. * linux/fs/ext3/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/blkdev.h>
  20. #include <linux/parser.h>
  21. #include <linux/exportfs.h>
  22. #include <linux/statfs.h>
  23. #include <linux/random.h>
  24. #include <linux/mount.h>
  25. #include <linux/quotaops.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/log2.h>
  28. #include <linux/cleancache.h>
  29. #include <linux/namei.h>
  30. #include <asm/uaccess.h>
  31. #define CREATE_TRACE_POINTS
  32. #include "ext3.h"
  33. #include "xattr.h"
  34. #include "acl.h"
  35. #include "namei.h"
  36. #ifdef CONFIG_EXT3_DEFAULTS_TO_ORDERED
  37. #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_ORDERED_DATA
  38. #else
  39. #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_WRITEBACK_DATA
  40. #endif
  41. static int ext3_load_journal(struct super_block *, struct ext3_super_block *,
  42. unsigned long journal_devnum);
  43. static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
  44. unsigned int);
  45. static int ext3_commit_super(struct super_block *sb,
  46. struct ext3_super_block *es,
  47. int sync);
  48. static void ext3_mark_recovery_complete(struct super_block * sb,
  49. struct ext3_super_block * es);
  50. static void ext3_clear_journal_err(struct super_block * sb,
  51. struct ext3_super_block * es);
  52. static int ext3_sync_fs(struct super_block *sb, int wait);
  53. static const char *ext3_decode_error(struct super_block * sb, int errno,
  54. char nbuf[16]);
  55. static int ext3_remount (struct super_block * sb, int * flags, char * data);
  56. static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf);
  57. static int ext3_unfreeze(struct super_block *sb);
  58. static int ext3_freeze(struct super_block *sb);
  59. /*
  60. * Wrappers for journal_start/end.
  61. */
  62. handle_t *ext3_journal_start_sb(struct super_block *sb, int nblocks)
  63. {
  64. journal_t *journal;
  65. if (sb->s_flags & MS_RDONLY)
  66. return ERR_PTR(-EROFS);
  67. /* Special case here: if the journal has aborted behind our
  68. * backs (eg. EIO in the commit thread), then we still need to
  69. * take the FS itself readonly cleanly. */
  70. journal = EXT3_SB(sb)->s_journal;
  71. if (is_journal_aborted(journal)) {
  72. ext3_abort(sb, __func__,
  73. "Detected aborted journal");
  74. return ERR_PTR(-EROFS);
  75. }
  76. return journal_start(journal, nblocks);
  77. }
  78. int __ext3_journal_stop(const char *where, handle_t *handle)
  79. {
  80. struct super_block *sb;
  81. int err;
  82. int rc;
  83. sb = handle->h_transaction->t_journal->j_private;
  84. err = handle->h_err;
  85. rc = journal_stop(handle);
  86. if (!err)
  87. err = rc;
  88. if (err)
  89. __ext3_std_error(sb, where, err);
  90. return err;
  91. }
  92. void ext3_journal_abort_handle(const char *caller, const char *err_fn,
  93. struct buffer_head *bh, handle_t *handle, int err)
  94. {
  95. char nbuf[16];
  96. const char *errstr = ext3_decode_error(NULL, err, nbuf);
  97. if (bh)
  98. BUFFER_TRACE(bh, "abort");
  99. if (!handle->h_err)
  100. handle->h_err = err;
  101. if (is_handle_aborted(handle))
  102. return;
  103. printk(KERN_ERR "EXT3-fs: %s: aborting transaction: %s in %s\n",
  104. caller, errstr, err_fn);
  105. journal_abort_handle(handle);
  106. }
  107. void ext3_msg(struct super_block *sb, const char *prefix,
  108. const char *fmt, ...)
  109. {
  110. struct va_format vaf;
  111. va_list args;
  112. va_start(args, fmt);
  113. vaf.fmt = fmt;
  114. vaf.va = &args;
  115. printk("%sEXT3-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
  116. va_end(args);
  117. }
  118. /* Deal with the reporting of failure conditions on a filesystem such as
  119. * inconsistencies detected or read IO failures.
  120. *
  121. * On ext2, we can store the error state of the filesystem in the
  122. * superblock. That is not possible on ext3, because we may have other
  123. * write ordering constraints on the superblock which prevent us from
  124. * writing it out straight away; and given that the journal is about to
  125. * be aborted, we can't rely on the current, or future, transactions to
  126. * write out the superblock safely.
  127. *
  128. * We'll just use the journal_abort() error code to record an error in
  129. * the journal instead. On recovery, the journal will complain about
  130. * that error until we've noted it down and cleared it.
  131. */
  132. static void ext3_handle_error(struct super_block *sb)
  133. {
  134. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  135. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  136. es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
  137. if (sb->s_flags & MS_RDONLY)
  138. return;
  139. if (!test_opt (sb, ERRORS_CONT)) {
  140. journal_t *journal = EXT3_SB(sb)->s_journal;
  141. set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
  142. if (journal)
  143. journal_abort(journal, -EIO);
  144. }
  145. if (test_opt (sb, ERRORS_RO)) {
  146. ext3_msg(sb, KERN_CRIT,
  147. "error: remounting filesystem read-only");
  148. /*
  149. * Make sure updated value of ->s_mount_state will be visible
  150. * before ->s_flags update.
  151. */
  152. smp_wmb();
  153. sb->s_flags |= MS_RDONLY;
  154. }
  155. ext3_commit_super(sb, es, 1);
  156. if (test_opt(sb, ERRORS_PANIC))
  157. panic("EXT3-fs (%s): panic forced after error\n",
  158. sb->s_id);
  159. }
  160. void ext3_error(struct super_block *sb, const char *function,
  161. const char *fmt, ...)
  162. {
  163. struct va_format vaf;
  164. va_list args;
  165. va_start(args, fmt);
  166. vaf.fmt = fmt;
  167. vaf.va = &args;
  168. printk(KERN_CRIT "EXT3-fs error (device %s): %s: %pV\n",
  169. sb->s_id, function, &vaf);
  170. va_end(args);
  171. ext3_handle_error(sb);
  172. }
  173. static const char *ext3_decode_error(struct super_block * sb, int errno,
  174. char nbuf[16])
  175. {
  176. char *errstr = NULL;
  177. switch (errno) {
  178. case -EIO:
  179. errstr = "IO failure";
  180. break;
  181. case -ENOMEM:
  182. errstr = "Out of memory";
  183. break;
  184. case -EROFS:
  185. if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
  186. errstr = "Journal has aborted";
  187. else
  188. errstr = "Readonly filesystem";
  189. break;
  190. default:
  191. /* If the caller passed in an extra buffer for unknown
  192. * errors, textualise them now. Else we just return
  193. * NULL. */
  194. if (nbuf) {
  195. /* Check for truncated error codes... */
  196. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  197. errstr = nbuf;
  198. }
  199. break;
  200. }
  201. return errstr;
  202. }
  203. /* __ext3_std_error decodes expected errors from journaling functions
  204. * automatically and invokes the appropriate error response. */
  205. void __ext3_std_error (struct super_block * sb, const char * function,
  206. int errno)
  207. {
  208. char nbuf[16];
  209. const char *errstr;
  210. /* Special case: if the error is EROFS, and we're not already
  211. * inside a transaction, then there's really no point in logging
  212. * an error. */
  213. if (errno == -EROFS && journal_current_handle() == NULL &&
  214. (sb->s_flags & MS_RDONLY))
  215. return;
  216. errstr = ext3_decode_error(sb, errno, nbuf);
  217. ext3_msg(sb, KERN_CRIT, "error in %s: %s", function, errstr);
  218. ext3_handle_error(sb);
  219. }
  220. /*
  221. * ext3_abort is a much stronger failure handler than ext3_error. The
  222. * abort function may be used to deal with unrecoverable failures such
  223. * as journal IO errors or ENOMEM at a critical moment in log management.
  224. *
  225. * We unconditionally force the filesystem into an ABORT|READONLY state,
  226. * unless the error response on the fs has been set to panic in which
  227. * case we take the easy way out and panic immediately.
  228. */
  229. void ext3_abort(struct super_block *sb, const char *function,
  230. const char *fmt, ...)
  231. {
  232. struct va_format vaf;
  233. va_list args;
  234. va_start(args, fmt);
  235. vaf.fmt = fmt;
  236. vaf.va = &args;
  237. printk(KERN_CRIT "EXT3-fs (%s): error: %s: %pV\n",
  238. sb->s_id, function, &vaf);
  239. va_end(args);
  240. if (test_opt(sb, ERRORS_PANIC))
  241. panic("EXT3-fs: panic from previous error\n");
  242. if (sb->s_flags & MS_RDONLY)
  243. return;
  244. ext3_msg(sb, KERN_CRIT,
  245. "error: remounting filesystem read-only");
  246. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  247. set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
  248. /*
  249. * Make sure updated value of ->s_mount_state will be visible
  250. * before ->s_flags update.
  251. */
  252. smp_wmb();
  253. sb->s_flags |= MS_RDONLY;
  254. if (EXT3_SB(sb)->s_journal)
  255. journal_abort(EXT3_SB(sb)->s_journal, -EIO);
  256. }
  257. void ext3_warning(struct super_block *sb, const char *function,
  258. const char *fmt, ...)
  259. {
  260. struct va_format vaf;
  261. va_list args;
  262. va_start(args, fmt);
  263. vaf.fmt = fmt;
  264. vaf.va = &args;
  265. printk(KERN_WARNING "EXT3-fs (%s): warning: %s: %pV\n",
  266. sb->s_id, function, &vaf);
  267. va_end(args);
  268. }
  269. void ext3_update_dynamic_rev(struct super_block *sb)
  270. {
  271. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  272. if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
  273. return;
  274. ext3_msg(sb, KERN_WARNING,
  275. "warning: updating to rev %d because of "
  276. "new feature flag, running e2fsck is recommended",
  277. EXT3_DYNAMIC_REV);
  278. es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
  279. es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
  280. es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
  281. /* leave es->s_feature_*compat flags alone */
  282. /* es->s_uuid will be set by e2fsck if empty */
  283. /*
  284. * The rest of the superblock fields should be zero, and if not it
  285. * means they are likely already in use, so leave them alone. We
  286. * can leave it up to e2fsck to clean up any inconsistencies there.
  287. */
  288. }
  289. /*
  290. * Open the external journal device
  291. */
  292. static struct block_device *ext3_blkdev_get(dev_t dev, struct super_block *sb)
  293. {
  294. struct block_device *bdev;
  295. char b[BDEVNAME_SIZE];
  296. bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
  297. if (IS_ERR(bdev))
  298. goto fail;
  299. return bdev;
  300. fail:
  301. ext3_msg(sb, KERN_ERR, "error: failed to open journal device %s: %ld",
  302. __bdevname(dev, b), PTR_ERR(bdev));
  303. return NULL;
  304. }
  305. /*
  306. * Release the journal device
  307. */
  308. static void ext3_blkdev_put(struct block_device *bdev)
  309. {
  310. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  311. }
  312. static void ext3_blkdev_remove(struct ext3_sb_info *sbi)
  313. {
  314. struct block_device *bdev;
  315. bdev = sbi->journal_bdev;
  316. if (bdev) {
  317. ext3_blkdev_put(bdev);
  318. sbi->journal_bdev = NULL;
  319. }
  320. }
  321. static inline struct inode *orphan_list_entry(struct list_head *l)
  322. {
  323. return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
  324. }
  325. static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
  326. {
  327. struct list_head *l;
  328. ext3_msg(sb, KERN_ERR, "error: sb orphan head is %d",
  329. le32_to_cpu(sbi->s_es->s_last_orphan));
  330. ext3_msg(sb, KERN_ERR, "sb_info orphan list:");
  331. list_for_each(l, &sbi->s_orphan) {
  332. struct inode *inode = orphan_list_entry(l);
  333. ext3_msg(sb, KERN_ERR, " "
  334. "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
  335. inode->i_sb->s_id, inode->i_ino, inode,
  336. inode->i_mode, inode->i_nlink,
  337. NEXT_ORPHAN(inode));
  338. }
  339. }
  340. static void ext3_put_super (struct super_block * sb)
  341. {
  342. struct ext3_sb_info *sbi = EXT3_SB(sb);
  343. struct ext3_super_block *es = sbi->s_es;
  344. int i, err;
  345. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  346. ext3_xattr_put_super(sb);
  347. err = journal_destroy(sbi->s_journal);
  348. sbi->s_journal = NULL;
  349. if (err < 0)
  350. ext3_abort(sb, __func__, "Couldn't clean up the journal");
  351. if (!(sb->s_flags & MS_RDONLY)) {
  352. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  353. es->s_state = cpu_to_le16(sbi->s_mount_state);
  354. BUFFER_TRACE(sbi->s_sbh, "marking dirty");
  355. mark_buffer_dirty(sbi->s_sbh);
  356. ext3_commit_super(sb, es, 1);
  357. }
  358. for (i = 0; i < sbi->s_gdb_count; i++)
  359. brelse(sbi->s_group_desc[i]);
  360. kfree(sbi->s_group_desc);
  361. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  362. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  363. percpu_counter_destroy(&sbi->s_dirs_counter);
  364. brelse(sbi->s_sbh);
  365. #ifdef CONFIG_QUOTA
  366. for (i = 0; i < EXT3_MAXQUOTAS; i++)
  367. kfree(sbi->s_qf_names[i]);
  368. #endif
  369. /* Debugging code just in case the in-memory inode orphan list
  370. * isn't empty. The on-disk one can be non-empty if we've
  371. * detected an error and taken the fs readonly, but the
  372. * in-memory list had better be clean by this point. */
  373. if (!list_empty(&sbi->s_orphan))
  374. dump_orphan_list(sb, sbi);
  375. J_ASSERT(list_empty(&sbi->s_orphan));
  376. invalidate_bdev(sb->s_bdev);
  377. if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
  378. /*
  379. * Invalidate the journal device's buffers. We don't want them
  380. * floating about in memory - the physical journal device may
  381. * hotswapped, and it breaks the `ro-after' testing code.
  382. */
  383. sync_blockdev(sbi->journal_bdev);
  384. invalidate_bdev(sbi->journal_bdev);
  385. ext3_blkdev_remove(sbi);
  386. }
  387. sb->s_fs_info = NULL;
  388. kfree(sbi->s_blockgroup_lock);
  389. kfree(sbi);
  390. }
  391. static struct kmem_cache *ext3_inode_cachep;
  392. /*
  393. * Called inside transaction, so use GFP_NOFS
  394. */
  395. static struct inode *ext3_alloc_inode(struct super_block *sb)
  396. {
  397. struct ext3_inode_info *ei;
  398. ei = kmem_cache_alloc(ext3_inode_cachep, GFP_NOFS);
  399. if (!ei)
  400. return NULL;
  401. ei->i_block_alloc_info = NULL;
  402. ei->vfs_inode.i_version = 1;
  403. atomic_set(&ei->i_datasync_tid, 0);
  404. atomic_set(&ei->i_sync_tid, 0);
  405. #ifdef CONFIG_QUOTA
  406. memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
  407. #endif
  408. return &ei->vfs_inode;
  409. }
  410. static int ext3_drop_inode(struct inode *inode)
  411. {
  412. int drop = generic_drop_inode(inode);
  413. trace_ext3_drop_inode(inode, drop);
  414. return drop;
  415. }
  416. static void ext3_i_callback(struct rcu_head *head)
  417. {
  418. struct inode *inode = container_of(head, struct inode, i_rcu);
  419. kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
  420. }
  421. static void ext3_destroy_inode(struct inode *inode)
  422. {
  423. if (!list_empty(&(EXT3_I(inode)->i_orphan))) {
  424. printk("EXT3 Inode %p: orphan list check failed!\n",
  425. EXT3_I(inode));
  426. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
  427. EXT3_I(inode), sizeof(struct ext3_inode_info),
  428. false);
  429. dump_stack();
  430. }
  431. call_rcu(&inode->i_rcu, ext3_i_callback);
  432. }
  433. static void init_once(void *foo)
  434. {
  435. struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
  436. INIT_LIST_HEAD(&ei->i_orphan);
  437. #ifdef CONFIG_EXT3_FS_XATTR
  438. init_rwsem(&ei->xattr_sem);
  439. #endif
  440. mutex_init(&ei->truncate_mutex);
  441. inode_init_once(&ei->vfs_inode);
  442. }
  443. static int __init init_inodecache(void)
  444. {
  445. ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
  446. sizeof(struct ext3_inode_info),
  447. 0, (SLAB_RECLAIM_ACCOUNT|
  448. SLAB_MEM_SPREAD),
  449. init_once);
  450. if (ext3_inode_cachep == NULL)
  451. return -ENOMEM;
  452. return 0;
  453. }
  454. static void destroy_inodecache(void)
  455. {
  456. /*
  457. * Make sure all delayed rcu free inodes are flushed before we
  458. * destroy cache.
  459. */
  460. rcu_barrier();
  461. kmem_cache_destroy(ext3_inode_cachep);
  462. }
  463. static inline void ext3_show_quota_options(struct seq_file *seq, struct super_block *sb)
  464. {
  465. #if defined(CONFIG_QUOTA)
  466. struct ext3_sb_info *sbi = EXT3_SB(sb);
  467. if (sbi->s_jquota_fmt) {
  468. char *fmtname = "";
  469. switch (sbi->s_jquota_fmt) {
  470. case QFMT_VFS_OLD:
  471. fmtname = "vfsold";
  472. break;
  473. case QFMT_VFS_V0:
  474. fmtname = "vfsv0";
  475. break;
  476. case QFMT_VFS_V1:
  477. fmtname = "vfsv1";
  478. break;
  479. }
  480. seq_printf(seq, ",jqfmt=%s", fmtname);
  481. }
  482. if (sbi->s_qf_names[USRQUOTA])
  483. seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
  484. if (sbi->s_qf_names[GRPQUOTA])
  485. seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
  486. if (test_opt(sb, USRQUOTA))
  487. seq_puts(seq, ",usrquota");
  488. if (test_opt(sb, GRPQUOTA))
  489. seq_puts(seq, ",grpquota");
  490. #endif
  491. }
  492. static char *data_mode_string(unsigned long mode)
  493. {
  494. switch (mode) {
  495. case EXT3_MOUNT_JOURNAL_DATA:
  496. return "journal";
  497. case EXT3_MOUNT_ORDERED_DATA:
  498. return "ordered";
  499. case EXT3_MOUNT_WRITEBACK_DATA:
  500. return "writeback";
  501. }
  502. return "unknown";
  503. }
  504. /*
  505. * Show an option if
  506. * - it's set to a non-default value OR
  507. * - if the per-sb default is different from the global default
  508. */
  509. static int ext3_show_options(struct seq_file *seq, struct dentry *root)
  510. {
  511. struct super_block *sb = root->d_sb;
  512. struct ext3_sb_info *sbi = EXT3_SB(sb);
  513. struct ext3_super_block *es = sbi->s_es;
  514. unsigned long def_mount_opts;
  515. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  516. if (sbi->s_sb_block != 1)
  517. seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
  518. if (test_opt(sb, MINIX_DF))
  519. seq_puts(seq, ",minixdf");
  520. if (test_opt(sb, GRPID))
  521. seq_puts(seq, ",grpid");
  522. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT3_DEFM_BSDGROUPS))
  523. seq_puts(seq, ",nogrpid");
  524. if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT3_DEF_RESUID)) ||
  525. le16_to_cpu(es->s_def_resuid) != EXT3_DEF_RESUID) {
  526. seq_printf(seq, ",resuid=%u",
  527. from_kuid_munged(&init_user_ns, sbi->s_resuid));
  528. }
  529. if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT3_DEF_RESGID)) ||
  530. le16_to_cpu(es->s_def_resgid) != EXT3_DEF_RESGID) {
  531. seq_printf(seq, ",resgid=%u",
  532. from_kgid_munged(&init_user_ns, sbi->s_resgid));
  533. }
  534. if (test_opt(sb, ERRORS_RO)) {
  535. int def_errors = le16_to_cpu(es->s_errors);
  536. if (def_errors == EXT3_ERRORS_PANIC ||
  537. def_errors == EXT3_ERRORS_CONTINUE) {
  538. seq_puts(seq, ",errors=remount-ro");
  539. }
  540. }
  541. if (test_opt(sb, ERRORS_CONT))
  542. seq_puts(seq, ",errors=continue");
  543. if (test_opt(sb, ERRORS_PANIC))
  544. seq_puts(seq, ",errors=panic");
  545. if (test_opt(sb, NO_UID32))
  546. seq_puts(seq, ",nouid32");
  547. if (test_opt(sb, DEBUG))
  548. seq_puts(seq, ",debug");
  549. #ifdef CONFIG_EXT3_FS_XATTR
  550. if (test_opt(sb, XATTR_USER))
  551. seq_puts(seq, ",user_xattr");
  552. if (!test_opt(sb, XATTR_USER) &&
  553. (def_mount_opts & EXT3_DEFM_XATTR_USER)) {
  554. seq_puts(seq, ",nouser_xattr");
  555. }
  556. #endif
  557. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  558. if (test_opt(sb, POSIX_ACL))
  559. seq_puts(seq, ",acl");
  560. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT3_DEFM_ACL))
  561. seq_puts(seq, ",noacl");
  562. #endif
  563. if (!test_opt(sb, RESERVATION))
  564. seq_puts(seq, ",noreservation");
  565. if (sbi->s_commit_interval) {
  566. seq_printf(seq, ",commit=%u",
  567. (unsigned) (sbi->s_commit_interval / HZ));
  568. }
  569. /*
  570. * Always display barrier state so it's clear what the status is.
  571. */
  572. seq_puts(seq, ",barrier=");
  573. seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
  574. seq_printf(seq, ",data=%s", data_mode_string(test_opt(sb, DATA_FLAGS)));
  575. if (test_opt(sb, DATA_ERR_ABORT))
  576. seq_puts(seq, ",data_err=abort");
  577. if (test_opt(sb, NOLOAD))
  578. seq_puts(seq, ",norecovery");
  579. ext3_show_quota_options(seq, sb);
  580. return 0;
  581. }
  582. static struct inode *ext3_nfs_get_inode(struct super_block *sb,
  583. u64 ino, u32 generation)
  584. {
  585. struct inode *inode;
  586. if (ino < EXT3_FIRST_INO(sb) && ino != EXT3_ROOT_INO)
  587. return ERR_PTR(-ESTALE);
  588. if (ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count))
  589. return ERR_PTR(-ESTALE);
  590. /* iget isn't really right if the inode is currently unallocated!!
  591. *
  592. * ext3_read_inode will return a bad_inode if the inode had been
  593. * deleted, so we should be safe.
  594. *
  595. * Currently we don't know the generation for parent directory, so
  596. * a generation of 0 means "accept any"
  597. */
  598. inode = ext3_iget(sb, ino);
  599. if (IS_ERR(inode))
  600. return ERR_CAST(inode);
  601. if (generation && inode->i_generation != generation) {
  602. iput(inode);
  603. return ERR_PTR(-ESTALE);
  604. }
  605. return inode;
  606. }
  607. static struct dentry *ext3_fh_to_dentry(struct super_block *sb, struct fid *fid,
  608. int fh_len, int fh_type)
  609. {
  610. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  611. ext3_nfs_get_inode);
  612. }
  613. static struct dentry *ext3_fh_to_parent(struct super_block *sb, struct fid *fid,
  614. int fh_len, int fh_type)
  615. {
  616. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  617. ext3_nfs_get_inode);
  618. }
  619. /*
  620. * Try to release metadata pages (indirect blocks, directories) which are
  621. * mapped via the block device. Since these pages could have journal heads
  622. * which would prevent try_to_free_buffers() from freeing them, we must use
  623. * jbd layer's try_to_free_buffers() function to release them.
  624. */
  625. static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
  626. gfp_t wait)
  627. {
  628. journal_t *journal = EXT3_SB(sb)->s_journal;
  629. WARN_ON(PageChecked(page));
  630. if (!page_has_buffers(page))
  631. return 0;
  632. if (journal)
  633. return journal_try_to_free_buffers(journal, page,
  634. wait & ~__GFP_WAIT);
  635. return try_to_free_buffers(page);
  636. }
  637. #ifdef CONFIG_QUOTA
  638. #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
  639. #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
  640. static int ext3_write_dquot(struct dquot *dquot);
  641. static int ext3_acquire_dquot(struct dquot *dquot);
  642. static int ext3_release_dquot(struct dquot *dquot);
  643. static int ext3_mark_dquot_dirty(struct dquot *dquot);
  644. static int ext3_write_info(struct super_block *sb, int type);
  645. static int ext3_quota_on(struct super_block *sb, int type, int format_id,
  646. struct path *path);
  647. static int ext3_quota_on_mount(struct super_block *sb, int type);
  648. static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
  649. size_t len, loff_t off);
  650. static ssize_t ext3_quota_write(struct super_block *sb, int type,
  651. const char *data, size_t len, loff_t off);
  652. static struct dquot **ext3_get_dquots(struct inode *inode)
  653. {
  654. return EXT3_I(inode)->i_dquot;
  655. }
  656. static const struct dquot_operations ext3_quota_operations = {
  657. .write_dquot = ext3_write_dquot,
  658. .acquire_dquot = ext3_acquire_dquot,
  659. .release_dquot = ext3_release_dquot,
  660. .mark_dirty = ext3_mark_dquot_dirty,
  661. .write_info = ext3_write_info,
  662. .alloc_dquot = dquot_alloc,
  663. .destroy_dquot = dquot_destroy,
  664. };
  665. static const struct quotactl_ops ext3_qctl_operations = {
  666. .quota_on = ext3_quota_on,
  667. .quota_off = dquot_quota_off,
  668. .quota_sync = dquot_quota_sync,
  669. .get_info = dquot_get_dqinfo,
  670. .set_info = dquot_set_dqinfo,
  671. .get_dqblk = dquot_get_dqblk,
  672. .set_dqblk = dquot_set_dqblk
  673. };
  674. #endif
  675. static const struct super_operations ext3_sops = {
  676. .alloc_inode = ext3_alloc_inode,
  677. .destroy_inode = ext3_destroy_inode,
  678. .write_inode = ext3_write_inode,
  679. .dirty_inode = ext3_dirty_inode,
  680. .drop_inode = ext3_drop_inode,
  681. .evict_inode = ext3_evict_inode,
  682. .put_super = ext3_put_super,
  683. .sync_fs = ext3_sync_fs,
  684. .freeze_fs = ext3_freeze,
  685. .unfreeze_fs = ext3_unfreeze,
  686. .statfs = ext3_statfs,
  687. .remount_fs = ext3_remount,
  688. .show_options = ext3_show_options,
  689. #ifdef CONFIG_QUOTA
  690. .quota_read = ext3_quota_read,
  691. .quota_write = ext3_quota_write,
  692. .get_dquots = ext3_get_dquots,
  693. #endif
  694. .bdev_try_to_free_page = bdev_try_to_free_page,
  695. };
  696. static const struct export_operations ext3_export_ops = {
  697. .fh_to_dentry = ext3_fh_to_dentry,
  698. .fh_to_parent = ext3_fh_to_parent,
  699. .get_parent = ext3_get_parent,
  700. };
  701. enum {
  702. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  703. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
  704. Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
  705. Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
  706. Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
  707. Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
  708. Opt_journal_path,
  709. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  710. Opt_data_err_abort, Opt_data_err_ignore,
  711. Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
  712. Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
  713. Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
  714. Opt_resize, Opt_usrquota, Opt_grpquota
  715. };
  716. static const match_table_t tokens = {
  717. {Opt_bsd_df, "bsddf"},
  718. {Opt_minix_df, "minixdf"},
  719. {Opt_grpid, "grpid"},
  720. {Opt_grpid, "bsdgroups"},
  721. {Opt_nogrpid, "nogrpid"},
  722. {Opt_nogrpid, "sysvgroups"},
  723. {Opt_resgid, "resgid=%u"},
  724. {Opt_resuid, "resuid=%u"},
  725. {Opt_sb, "sb=%u"},
  726. {Opt_err_cont, "errors=continue"},
  727. {Opt_err_panic, "errors=panic"},
  728. {Opt_err_ro, "errors=remount-ro"},
  729. {Opt_nouid32, "nouid32"},
  730. {Opt_nocheck, "nocheck"},
  731. {Opt_nocheck, "check=none"},
  732. {Opt_debug, "debug"},
  733. {Opt_oldalloc, "oldalloc"},
  734. {Opt_orlov, "orlov"},
  735. {Opt_user_xattr, "user_xattr"},
  736. {Opt_nouser_xattr, "nouser_xattr"},
  737. {Opt_acl, "acl"},
  738. {Opt_noacl, "noacl"},
  739. {Opt_reservation, "reservation"},
  740. {Opt_noreservation, "noreservation"},
  741. {Opt_noload, "noload"},
  742. {Opt_noload, "norecovery"},
  743. {Opt_nobh, "nobh"},
  744. {Opt_bh, "bh"},
  745. {Opt_commit, "commit=%u"},
  746. {Opt_journal_update, "journal=update"},
  747. {Opt_journal_inum, "journal=%u"},
  748. {Opt_journal_dev, "journal_dev=%u"},
  749. {Opt_journal_path, "journal_path=%s"},
  750. {Opt_abort, "abort"},
  751. {Opt_data_journal, "data=journal"},
  752. {Opt_data_ordered, "data=ordered"},
  753. {Opt_data_writeback, "data=writeback"},
  754. {Opt_data_err_abort, "data_err=abort"},
  755. {Opt_data_err_ignore, "data_err=ignore"},
  756. {Opt_offusrjquota, "usrjquota="},
  757. {Opt_usrjquota, "usrjquota=%s"},
  758. {Opt_offgrpjquota, "grpjquota="},
  759. {Opt_grpjquota, "grpjquota=%s"},
  760. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  761. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  762. {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
  763. {Opt_grpquota, "grpquota"},
  764. {Opt_noquota, "noquota"},
  765. {Opt_quota, "quota"},
  766. {Opt_usrquota, "usrquota"},
  767. {Opt_barrier, "barrier=%u"},
  768. {Opt_barrier, "barrier"},
  769. {Opt_nobarrier, "nobarrier"},
  770. {Opt_resize, "resize"},
  771. {Opt_err, NULL},
  772. };
  773. static ext3_fsblk_t get_sb_block(void **data, struct super_block *sb)
  774. {
  775. ext3_fsblk_t sb_block;
  776. char *options = (char *) *data;
  777. if (!options || strncmp(options, "sb=", 3) != 0)
  778. return 1; /* Default location */
  779. options += 3;
  780. /*todo: use simple_strtoll with >32bit ext3 */
  781. sb_block = simple_strtoul(options, &options, 0);
  782. if (*options && *options != ',') {
  783. ext3_msg(sb, KERN_ERR, "error: invalid sb specification: %s",
  784. (char *) *data);
  785. return 1;
  786. }
  787. if (*options == ',')
  788. options++;
  789. *data = (void *) options;
  790. return sb_block;
  791. }
  792. #ifdef CONFIG_QUOTA
  793. static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
  794. {
  795. struct ext3_sb_info *sbi = EXT3_SB(sb);
  796. char *qname;
  797. if (sb_any_quota_loaded(sb) &&
  798. !sbi->s_qf_names[qtype]) {
  799. ext3_msg(sb, KERN_ERR,
  800. "Cannot change journaled "
  801. "quota options when quota turned on");
  802. return 0;
  803. }
  804. qname = match_strdup(args);
  805. if (!qname) {
  806. ext3_msg(sb, KERN_ERR,
  807. "Not enough memory for storing quotafile name");
  808. return 0;
  809. }
  810. if (sbi->s_qf_names[qtype]) {
  811. int same = !strcmp(sbi->s_qf_names[qtype], qname);
  812. kfree(qname);
  813. if (!same) {
  814. ext3_msg(sb, KERN_ERR,
  815. "%s quota file already specified",
  816. QTYPE2NAME(qtype));
  817. }
  818. return same;
  819. }
  820. if (strchr(qname, '/')) {
  821. ext3_msg(sb, KERN_ERR,
  822. "quotafile must be on filesystem root");
  823. kfree(qname);
  824. return 0;
  825. }
  826. sbi->s_qf_names[qtype] = qname;
  827. set_opt(sbi->s_mount_opt, QUOTA);
  828. return 1;
  829. }
  830. static int clear_qf_name(struct super_block *sb, int qtype) {
  831. struct ext3_sb_info *sbi = EXT3_SB(sb);
  832. if (sb_any_quota_loaded(sb) &&
  833. sbi->s_qf_names[qtype]) {
  834. ext3_msg(sb, KERN_ERR, "Cannot change journaled quota options"
  835. " when quota turned on");
  836. return 0;
  837. }
  838. if (sbi->s_qf_names[qtype]) {
  839. kfree(sbi->s_qf_names[qtype]);
  840. sbi->s_qf_names[qtype] = NULL;
  841. }
  842. return 1;
  843. }
  844. #endif
  845. static int parse_options (char *options, struct super_block *sb,
  846. unsigned int *inum, unsigned long *journal_devnum,
  847. ext3_fsblk_t *n_blocks_count, int is_remount)
  848. {
  849. struct ext3_sb_info *sbi = EXT3_SB(sb);
  850. char * p;
  851. substring_t args[MAX_OPT_ARGS];
  852. int data_opt = 0;
  853. int option;
  854. kuid_t uid;
  855. kgid_t gid;
  856. char *journal_path;
  857. struct inode *journal_inode;
  858. struct path path;
  859. int error;
  860. #ifdef CONFIG_QUOTA
  861. int qfmt;
  862. #endif
  863. if (!options)
  864. return 1;
  865. while ((p = strsep (&options, ",")) != NULL) {
  866. int token;
  867. if (!*p)
  868. continue;
  869. /*
  870. * Initialize args struct so we know whether arg was
  871. * found; some options take optional arguments.
  872. */
  873. args[0].to = args[0].from = NULL;
  874. token = match_token(p, tokens, args);
  875. switch (token) {
  876. case Opt_bsd_df:
  877. clear_opt (sbi->s_mount_opt, MINIX_DF);
  878. break;
  879. case Opt_minix_df:
  880. set_opt (sbi->s_mount_opt, MINIX_DF);
  881. break;
  882. case Opt_grpid:
  883. set_opt (sbi->s_mount_opt, GRPID);
  884. break;
  885. case Opt_nogrpid:
  886. clear_opt (sbi->s_mount_opt, GRPID);
  887. break;
  888. case Opt_resuid:
  889. if (match_int(&args[0], &option))
  890. return 0;
  891. uid = make_kuid(current_user_ns(), option);
  892. if (!uid_valid(uid)) {
  893. ext3_msg(sb, KERN_ERR, "Invalid uid value %d", option);
  894. return 0;
  895. }
  896. sbi->s_resuid = uid;
  897. break;
  898. case Opt_resgid:
  899. if (match_int(&args[0], &option))
  900. return 0;
  901. gid = make_kgid(current_user_ns(), option);
  902. if (!gid_valid(gid)) {
  903. ext3_msg(sb, KERN_ERR, "Invalid gid value %d", option);
  904. return 0;
  905. }
  906. sbi->s_resgid = gid;
  907. break;
  908. case Opt_sb:
  909. /* handled by get_sb_block() instead of here */
  910. /* *sb_block = match_int(&args[0]); */
  911. break;
  912. case Opt_err_panic:
  913. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  914. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  915. set_opt (sbi->s_mount_opt, ERRORS_PANIC);
  916. break;
  917. case Opt_err_ro:
  918. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  919. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  920. set_opt (sbi->s_mount_opt, ERRORS_RO);
  921. break;
  922. case Opt_err_cont:
  923. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  924. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  925. set_opt (sbi->s_mount_opt, ERRORS_CONT);
  926. break;
  927. case Opt_nouid32:
  928. set_opt (sbi->s_mount_opt, NO_UID32);
  929. break;
  930. case Opt_nocheck:
  931. clear_opt (sbi->s_mount_opt, CHECK);
  932. break;
  933. case Opt_debug:
  934. set_opt (sbi->s_mount_opt, DEBUG);
  935. break;
  936. case Opt_oldalloc:
  937. ext3_msg(sb, KERN_WARNING,
  938. "Ignoring deprecated oldalloc option");
  939. break;
  940. case Opt_orlov:
  941. ext3_msg(sb, KERN_WARNING,
  942. "Ignoring deprecated orlov option");
  943. break;
  944. #ifdef CONFIG_EXT3_FS_XATTR
  945. case Opt_user_xattr:
  946. set_opt (sbi->s_mount_opt, XATTR_USER);
  947. break;
  948. case Opt_nouser_xattr:
  949. clear_opt (sbi->s_mount_opt, XATTR_USER);
  950. break;
  951. #else
  952. case Opt_user_xattr:
  953. case Opt_nouser_xattr:
  954. ext3_msg(sb, KERN_INFO,
  955. "(no)user_xattr options not supported");
  956. break;
  957. #endif
  958. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  959. case Opt_acl:
  960. set_opt(sbi->s_mount_opt, POSIX_ACL);
  961. break;
  962. case Opt_noacl:
  963. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  964. break;
  965. #else
  966. case Opt_acl:
  967. case Opt_noacl:
  968. ext3_msg(sb, KERN_INFO,
  969. "(no)acl options not supported");
  970. break;
  971. #endif
  972. case Opt_reservation:
  973. set_opt(sbi->s_mount_opt, RESERVATION);
  974. break;
  975. case Opt_noreservation:
  976. clear_opt(sbi->s_mount_opt, RESERVATION);
  977. break;
  978. case Opt_journal_update:
  979. /* @@@ FIXME */
  980. /* Eventually we will want to be able to create
  981. a journal file here. For now, only allow the
  982. user to specify an existing inode to be the
  983. journal file. */
  984. if (is_remount) {
  985. ext3_msg(sb, KERN_ERR, "error: cannot specify "
  986. "journal on remount");
  987. return 0;
  988. }
  989. set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
  990. break;
  991. case Opt_journal_inum:
  992. if (is_remount) {
  993. ext3_msg(sb, KERN_ERR, "error: cannot specify "
  994. "journal on remount");
  995. return 0;
  996. }
  997. if (match_int(&args[0], &option))
  998. return 0;
  999. *inum = option;
  1000. break;
  1001. case Opt_journal_dev:
  1002. if (is_remount) {
  1003. ext3_msg(sb, KERN_ERR, "error: cannot specify "
  1004. "journal on remount");
  1005. return 0;
  1006. }
  1007. if (match_int(&args[0], &option))
  1008. return 0;
  1009. *journal_devnum = option;
  1010. break;
  1011. case Opt_journal_path:
  1012. if (is_remount) {
  1013. ext3_msg(sb, KERN_ERR, "error: cannot specify "
  1014. "journal on remount");
  1015. return 0;
  1016. }
  1017. journal_path = match_strdup(&args[0]);
  1018. if (!journal_path) {
  1019. ext3_msg(sb, KERN_ERR, "error: could not dup "
  1020. "journal device string");
  1021. return 0;
  1022. }
  1023. error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
  1024. if (error) {
  1025. ext3_msg(sb, KERN_ERR, "error: could not find "
  1026. "journal device path: error %d", error);
  1027. kfree(journal_path);
  1028. return 0;
  1029. }
  1030. journal_inode = path.dentry->d_inode;
  1031. if (!S_ISBLK(journal_inode->i_mode)) {
  1032. ext3_msg(sb, KERN_ERR, "error: journal path %s "
  1033. "is not a block device", journal_path);
  1034. path_put(&path);
  1035. kfree(journal_path);
  1036. return 0;
  1037. }
  1038. *journal_devnum = new_encode_dev(journal_inode->i_rdev);
  1039. path_put(&path);
  1040. kfree(journal_path);
  1041. break;
  1042. case Opt_noload:
  1043. set_opt (sbi->s_mount_opt, NOLOAD);
  1044. break;
  1045. case Opt_commit:
  1046. if (match_int(&args[0], &option))
  1047. return 0;
  1048. if (option < 0)
  1049. return 0;
  1050. if (option == 0)
  1051. option = JBD_DEFAULT_MAX_COMMIT_AGE;
  1052. sbi->s_commit_interval = HZ * option;
  1053. break;
  1054. case Opt_data_journal:
  1055. data_opt = EXT3_MOUNT_JOURNAL_DATA;
  1056. goto datacheck;
  1057. case Opt_data_ordered:
  1058. data_opt = EXT3_MOUNT_ORDERED_DATA;
  1059. goto datacheck;
  1060. case Opt_data_writeback:
  1061. data_opt = EXT3_MOUNT_WRITEBACK_DATA;
  1062. datacheck:
  1063. if (is_remount) {
  1064. if (test_opt(sb, DATA_FLAGS) == data_opt)
  1065. break;
  1066. ext3_msg(sb, KERN_ERR,
  1067. "error: cannot change "
  1068. "data mode on remount. The filesystem "
  1069. "is mounted in data=%s mode and you "
  1070. "try to remount it in data=%s mode.",
  1071. data_mode_string(test_opt(sb,
  1072. DATA_FLAGS)),
  1073. data_mode_string(data_opt));
  1074. return 0;
  1075. } else {
  1076. clear_opt(sbi->s_mount_opt, DATA_FLAGS);
  1077. sbi->s_mount_opt |= data_opt;
  1078. }
  1079. break;
  1080. case Opt_data_err_abort:
  1081. set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
  1082. break;
  1083. case Opt_data_err_ignore:
  1084. clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
  1085. break;
  1086. #ifdef CONFIG_QUOTA
  1087. case Opt_usrjquota:
  1088. if (!set_qf_name(sb, USRQUOTA, &args[0]))
  1089. return 0;
  1090. break;
  1091. case Opt_grpjquota:
  1092. if (!set_qf_name(sb, GRPQUOTA, &args[0]))
  1093. return 0;
  1094. break;
  1095. case Opt_offusrjquota:
  1096. if (!clear_qf_name(sb, USRQUOTA))
  1097. return 0;
  1098. break;
  1099. case Opt_offgrpjquota:
  1100. if (!clear_qf_name(sb, GRPQUOTA))
  1101. return 0;
  1102. break;
  1103. case Opt_jqfmt_vfsold:
  1104. qfmt = QFMT_VFS_OLD;
  1105. goto set_qf_format;
  1106. case Opt_jqfmt_vfsv0:
  1107. qfmt = QFMT_VFS_V0;
  1108. goto set_qf_format;
  1109. case Opt_jqfmt_vfsv1:
  1110. qfmt = QFMT_VFS_V1;
  1111. set_qf_format:
  1112. if (sb_any_quota_loaded(sb) &&
  1113. sbi->s_jquota_fmt != qfmt) {
  1114. ext3_msg(sb, KERN_ERR, "error: cannot change "
  1115. "journaled quota options when "
  1116. "quota turned on.");
  1117. return 0;
  1118. }
  1119. sbi->s_jquota_fmt = qfmt;
  1120. break;
  1121. case Opt_quota:
  1122. case Opt_usrquota:
  1123. set_opt(sbi->s_mount_opt, QUOTA);
  1124. set_opt(sbi->s_mount_opt, USRQUOTA);
  1125. break;
  1126. case Opt_grpquota:
  1127. set_opt(sbi->s_mount_opt, QUOTA);
  1128. set_opt(sbi->s_mount_opt, GRPQUOTA);
  1129. break;
  1130. case Opt_noquota:
  1131. if (sb_any_quota_loaded(sb)) {
  1132. ext3_msg(sb, KERN_ERR, "error: cannot change "
  1133. "quota options when quota turned on.");
  1134. return 0;
  1135. }
  1136. clear_opt(sbi->s_mount_opt, QUOTA);
  1137. clear_opt(sbi->s_mount_opt, USRQUOTA);
  1138. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  1139. break;
  1140. #else
  1141. case Opt_quota:
  1142. case Opt_usrquota:
  1143. case Opt_grpquota:
  1144. ext3_msg(sb, KERN_ERR,
  1145. "error: quota options not supported.");
  1146. break;
  1147. case Opt_usrjquota:
  1148. case Opt_grpjquota:
  1149. case Opt_offusrjquota:
  1150. case Opt_offgrpjquota:
  1151. case Opt_jqfmt_vfsold:
  1152. case Opt_jqfmt_vfsv0:
  1153. case Opt_jqfmt_vfsv1:
  1154. ext3_msg(sb, KERN_ERR,
  1155. "error: journaled quota options not "
  1156. "supported.");
  1157. break;
  1158. case Opt_noquota:
  1159. break;
  1160. #endif
  1161. case Opt_abort:
  1162. set_opt(sbi->s_mount_opt, ABORT);
  1163. break;
  1164. case Opt_nobarrier:
  1165. clear_opt(sbi->s_mount_opt, BARRIER);
  1166. break;
  1167. case Opt_barrier:
  1168. if (args[0].from) {
  1169. if (match_int(&args[0], &option))
  1170. return 0;
  1171. } else
  1172. option = 1; /* No argument, default to 1 */
  1173. if (option)
  1174. set_opt(sbi->s_mount_opt, BARRIER);
  1175. else
  1176. clear_opt(sbi->s_mount_opt, BARRIER);
  1177. break;
  1178. case Opt_ignore:
  1179. break;
  1180. case Opt_resize:
  1181. if (!is_remount) {
  1182. ext3_msg(sb, KERN_ERR,
  1183. "error: resize option only available "
  1184. "for remount");
  1185. return 0;
  1186. }
  1187. if (match_int(&args[0], &option) != 0)
  1188. return 0;
  1189. *n_blocks_count = option;
  1190. break;
  1191. case Opt_nobh:
  1192. ext3_msg(sb, KERN_WARNING,
  1193. "warning: ignoring deprecated nobh option");
  1194. break;
  1195. case Opt_bh:
  1196. ext3_msg(sb, KERN_WARNING,
  1197. "warning: ignoring deprecated bh option");
  1198. break;
  1199. default:
  1200. ext3_msg(sb, KERN_ERR,
  1201. "error: unrecognized mount option \"%s\" "
  1202. "or missing value", p);
  1203. return 0;
  1204. }
  1205. }
  1206. #ifdef CONFIG_QUOTA
  1207. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  1208. if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
  1209. clear_opt(sbi->s_mount_opt, USRQUOTA);
  1210. if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
  1211. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  1212. if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
  1213. ext3_msg(sb, KERN_ERR, "error: old and new quota "
  1214. "format mixing.");
  1215. return 0;
  1216. }
  1217. if (!sbi->s_jquota_fmt) {
  1218. ext3_msg(sb, KERN_ERR, "error: journaled quota format "
  1219. "not specified.");
  1220. return 0;
  1221. }
  1222. }
  1223. #endif
  1224. return 1;
  1225. }
  1226. static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
  1227. int read_only)
  1228. {
  1229. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1230. int res = 0;
  1231. if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
  1232. ext3_msg(sb, KERN_ERR,
  1233. "error: revision level too high, "
  1234. "forcing read-only mode");
  1235. res = MS_RDONLY;
  1236. }
  1237. if (read_only)
  1238. return res;
  1239. if (!(sbi->s_mount_state & EXT3_VALID_FS))
  1240. ext3_msg(sb, KERN_WARNING,
  1241. "warning: mounting unchecked fs, "
  1242. "running e2fsck is recommended");
  1243. else if ((sbi->s_mount_state & EXT3_ERROR_FS))
  1244. ext3_msg(sb, KERN_WARNING,
  1245. "warning: mounting fs with errors, "
  1246. "running e2fsck is recommended");
  1247. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
  1248. le16_to_cpu(es->s_mnt_count) >=
  1249. le16_to_cpu(es->s_max_mnt_count))
  1250. ext3_msg(sb, KERN_WARNING,
  1251. "warning: maximal mount count reached, "
  1252. "running e2fsck is recommended");
  1253. else if (le32_to_cpu(es->s_checkinterval) &&
  1254. (le32_to_cpu(es->s_lastcheck) +
  1255. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  1256. ext3_msg(sb, KERN_WARNING,
  1257. "warning: checktime reached, "
  1258. "running e2fsck is recommended");
  1259. #if 0
  1260. /* @@@ We _will_ want to clear the valid bit if we find
  1261. inconsistencies, to force a fsck at reboot. But for
  1262. a plain journaled filesystem we can keep it set as
  1263. valid forever! :) */
  1264. es->s_state &= cpu_to_le16(~EXT3_VALID_FS);
  1265. #endif
  1266. if (!le16_to_cpu(es->s_max_mnt_count))
  1267. es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
  1268. le16_add_cpu(&es->s_mnt_count, 1);
  1269. es->s_mtime = cpu_to_le32(get_seconds());
  1270. ext3_update_dynamic_rev(sb);
  1271. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1272. ext3_commit_super(sb, es, 1);
  1273. if (test_opt(sb, DEBUG))
  1274. ext3_msg(sb, KERN_INFO, "[bs=%lu, gc=%lu, "
  1275. "bpg=%lu, ipg=%lu, mo=%04lx]",
  1276. sb->s_blocksize,
  1277. sbi->s_groups_count,
  1278. EXT3_BLOCKS_PER_GROUP(sb),
  1279. EXT3_INODES_PER_GROUP(sb),
  1280. sbi->s_mount_opt);
  1281. if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
  1282. char b[BDEVNAME_SIZE];
  1283. ext3_msg(sb, KERN_INFO, "using external journal on %s",
  1284. bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
  1285. } else {
  1286. ext3_msg(sb, KERN_INFO, "using internal journal");
  1287. }
  1288. cleancache_init_fs(sb);
  1289. return res;
  1290. }
  1291. /* Called at mount-time, super-block is locked */
  1292. static int ext3_check_descriptors(struct super_block *sb)
  1293. {
  1294. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1295. int i;
  1296. ext3_debug ("Checking group descriptors");
  1297. for (i = 0; i < sbi->s_groups_count; i++) {
  1298. struct ext3_group_desc *gdp = ext3_get_group_desc(sb, i, NULL);
  1299. ext3_fsblk_t first_block = ext3_group_first_block_no(sb, i);
  1300. ext3_fsblk_t last_block;
  1301. if (i == sbi->s_groups_count - 1)
  1302. last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
  1303. else
  1304. last_block = first_block +
  1305. (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  1306. if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
  1307. le32_to_cpu(gdp->bg_block_bitmap) > last_block)
  1308. {
  1309. ext3_error (sb, "ext3_check_descriptors",
  1310. "Block bitmap for group %d"
  1311. " not in group (block %lu)!",
  1312. i, (unsigned long)
  1313. le32_to_cpu(gdp->bg_block_bitmap));
  1314. return 0;
  1315. }
  1316. if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
  1317. le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
  1318. {
  1319. ext3_error (sb, "ext3_check_descriptors",
  1320. "Inode bitmap for group %d"
  1321. " not in group (block %lu)!",
  1322. i, (unsigned long)
  1323. le32_to_cpu(gdp->bg_inode_bitmap));
  1324. return 0;
  1325. }
  1326. if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
  1327. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
  1328. last_block)
  1329. {
  1330. ext3_error (sb, "ext3_check_descriptors",
  1331. "Inode table for group %d"
  1332. " not in group (block %lu)!",
  1333. i, (unsigned long)
  1334. le32_to_cpu(gdp->bg_inode_table));
  1335. return 0;
  1336. }
  1337. }
  1338. sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
  1339. sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
  1340. return 1;
  1341. }
  1342. /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
  1343. * the superblock) which were deleted from all directories, but held open by
  1344. * a process at the time of a crash. We walk the list and try to delete these
  1345. * inodes at recovery time (only with a read-write filesystem).
  1346. *
  1347. * In order to keep the orphan inode chain consistent during traversal (in
  1348. * case of crash during recovery), we link each inode into the superblock
  1349. * orphan list_head and handle it the same way as an inode deletion during
  1350. * normal operation (which journals the operations for us).
  1351. *
  1352. * We only do an iget() and an iput() on each inode, which is very safe if we
  1353. * accidentally point at an in-use or already deleted inode. The worst that
  1354. * can happen in this case is that we get a "bit already cleared" message from
  1355. * ext3_free_inode(). The only reason we would point at a wrong inode is if
  1356. * e2fsck was run on this filesystem, and it must have already done the orphan
  1357. * inode cleanup for us, so we can safely abort without any further action.
  1358. */
  1359. static void ext3_orphan_cleanup (struct super_block * sb,
  1360. struct ext3_super_block * es)
  1361. {
  1362. unsigned int s_flags = sb->s_flags;
  1363. int nr_orphans = 0, nr_truncates = 0;
  1364. #ifdef CONFIG_QUOTA
  1365. int i;
  1366. #endif
  1367. if (!es->s_last_orphan) {
  1368. jbd_debug(4, "no orphan inodes to clean up\n");
  1369. return;
  1370. }
  1371. if (bdev_read_only(sb->s_bdev)) {
  1372. ext3_msg(sb, KERN_ERR, "error: write access "
  1373. "unavailable, skipping orphan cleanup.");
  1374. return;
  1375. }
  1376. /* Check if feature set allows readwrite operations */
  1377. if (EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP)) {
  1378. ext3_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
  1379. "unknown ROCOMPAT features");
  1380. return;
  1381. }
  1382. if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
  1383. /* don't clear list on RO mount w/ errors */
  1384. if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
  1385. jbd_debug(1, "Errors on filesystem, "
  1386. "clearing orphan list.\n");
  1387. es->s_last_orphan = 0;
  1388. }
  1389. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1390. return;
  1391. }
  1392. if (s_flags & MS_RDONLY) {
  1393. ext3_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
  1394. sb->s_flags &= ~MS_RDONLY;
  1395. }
  1396. #ifdef CONFIG_QUOTA
  1397. /* Needed for iput() to work correctly and not trash data */
  1398. sb->s_flags |= MS_ACTIVE;
  1399. /* Turn on quotas so that they are updated correctly */
  1400. for (i = 0; i < EXT3_MAXQUOTAS; i++) {
  1401. if (EXT3_SB(sb)->s_qf_names[i]) {
  1402. int ret = ext3_quota_on_mount(sb, i);
  1403. if (ret < 0)
  1404. ext3_msg(sb, KERN_ERR,
  1405. "error: cannot turn on journaled "
  1406. "quota: %d", ret);
  1407. }
  1408. }
  1409. #endif
  1410. while (es->s_last_orphan) {
  1411. struct inode *inode;
  1412. inode = ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
  1413. if (IS_ERR(inode)) {
  1414. es->s_last_orphan = 0;
  1415. break;
  1416. }
  1417. list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
  1418. dquot_initialize(inode);
  1419. if (inode->i_nlink) {
  1420. printk(KERN_DEBUG
  1421. "%s: truncating inode %lu to %Ld bytes\n",
  1422. __func__, inode->i_ino, inode->i_size);
  1423. jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
  1424. inode->i_ino, inode->i_size);
  1425. ext3_truncate(inode);
  1426. nr_truncates++;
  1427. } else {
  1428. printk(KERN_DEBUG
  1429. "%s: deleting unreferenced inode %lu\n",
  1430. __func__, inode->i_ino);
  1431. jbd_debug(2, "deleting unreferenced inode %lu\n",
  1432. inode->i_ino);
  1433. nr_orphans++;
  1434. }
  1435. iput(inode); /* The delete magic happens here! */
  1436. }
  1437. #define PLURAL(x) (x), ((x)==1) ? "" : "s"
  1438. if (nr_orphans)
  1439. ext3_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
  1440. PLURAL(nr_orphans));
  1441. if (nr_truncates)
  1442. ext3_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
  1443. PLURAL(nr_truncates));
  1444. #ifdef CONFIG_QUOTA
  1445. /* Turn quotas off */
  1446. for (i = 0; i < EXT3_MAXQUOTAS; i++) {
  1447. if (sb_dqopt(sb)->files[i])
  1448. dquot_quota_off(sb, i);
  1449. }
  1450. #endif
  1451. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  1452. }
  1453. /*
  1454. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  1455. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  1456. * We need to be 1 filesystem block less than the 2^32 sector limit.
  1457. */
  1458. static loff_t ext3_max_size(int bits)
  1459. {
  1460. loff_t res = EXT3_NDIR_BLOCKS;
  1461. int meta_blocks;
  1462. loff_t upper_limit;
  1463. /* This is calculated to be the largest file size for a
  1464. * dense, file such that the total number of
  1465. * sectors in the file, including data and all indirect blocks,
  1466. * does not exceed 2^32 -1
  1467. * __u32 i_blocks representing the total number of
  1468. * 512 bytes blocks of the file
  1469. */
  1470. upper_limit = (1LL << 32) - 1;
  1471. /* total blocks in file system block size */
  1472. upper_limit >>= (bits - 9);
  1473. /* indirect blocks */
  1474. meta_blocks = 1;
  1475. /* double indirect blocks */
  1476. meta_blocks += 1 + (1LL << (bits-2));
  1477. /* tripple indirect blocks */
  1478. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  1479. upper_limit -= meta_blocks;
  1480. upper_limit <<= bits;
  1481. res += 1LL << (bits-2);
  1482. res += 1LL << (2*(bits-2));
  1483. res += 1LL << (3*(bits-2));
  1484. res <<= bits;
  1485. if (res > upper_limit)
  1486. res = upper_limit;
  1487. if (res > MAX_LFS_FILESIZE)
  1488. res = MAX_LFS_FILESIZE;
  1489. return res;
  1490. }
  1491. static ext3_fsblk_t descriptor_loc(struct super_block *sb,
  1492. ext3_fsblk_t logic_sb_block,
  1493. int nr)
  1494. {
  1495. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1496. unsigned long bg, first_meta_bg;
  1497. int has_super = 0;
  1498. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  1499. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
  1500. nr < first_meta_bg)
  1501. return (logic_sb_block + nr + 1);
  1502. bg = sbi->s_desc_per_block * nr;
  1503. if (ext3_bg_has_super(sb, bg))
  1504. has_super = 1;
  1505. return (has_super + ext3_group_first_block_no(sb, bg));
  1506. }
  1507. static int ext3_fill_super (struct super_block *sb, void *data, int silent)
  1508. {
  1509. struct buffer_head * bh;
  1510. struct ext3_super_block *es = NULL;
  1511. struct ext3_sb_info *sbi;
  1512. ext3_fsblk_t block;
  1513. ext3_fsblk_t sb_block = get_sb_block(&data, sb);
  1514. ext3_fsblk_t logic_sb_block;
  1515. unsigned long offset = 0;
  1516. unsigned int journal_inum = 0;
  1517. unsigned long journal_devnum = 0;
  1518. unsigned long def_mount_opts;
  1519. struct inode *root;
  1520. int blocksize;
  1521. int hblock;
  1522. int db_count;
  1523. int i;
  1524. int needs_recovery;
  1525. int ret = -EINVAL;
  1526. __le32 features;
  1527. int err;
  1528. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  1529. if (!sbi)
  1530. return -ENOMEM;
  1531. sbi->s_blockgroup_lock =
  1532. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  1533. if (!sbi->s_blockgroup_lock) {
  1534. kfree(sbi);
  1535. return -ENOMEM;
  1536. }
  1537. sb->s_fs_info = sbi;
  1538. sbi->s_sb_block = sb_block;
  1539. blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
  1540. if (!blocksize) {
  1541. ext3_msg(sb, KERN_ERR, "error: unable to set blocksize");
  1542. goto out_fail;
  1543. }
  1544. /*
  1545. * The ext3 superblock will not be buffer aligned for other than 1kB
  1546. * block sizes. We need to calculate the offset from buffer start.
  1547. */
  1548. if (blocksize != EXT3_MIN_BLOCK_SIZE) {
  1549. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1550. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1551. } else {
  1552. logic_sb_block = sb_block;
  1553. }
  1554. if (!(bh = sb_bread(sb, logic_sb_block))) {
  1555. ext3_msg(sb, KERN_ERR, "error: unable to read superblock");
  1556. goto out_fail;
  1557. }
  1558. /*
  1559. * Note: s_es must be initialized as soon as possible because
  1560. * some ext3 macro-instructions depend on its value
  1561. */
  1562. es = (struct ext3_super_block *) (bh->b_data + offset);
  1563. sbi->s_es = es;
  1564. sb->s_magic = le16_to_cpu(es->s_magic);
  1565. if (sb->s_magic != EXT3_SUPER_MAGIC)
  1566. goto cantfind_ext3;
  1567. /* Set defaults before we parse the mount options */
  1568. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  1569. if (def_mount_opts & EXT3_DEFM_DEBUG)
  1570. set_opt(sbi->s_mount_opt, DEBUG);
  1571. if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
  1572. set_opt(sbi->s_mount_opt, GRPID);
  1573. if (def_mount_opts & EXT3_DEFM_UID16)
  1574. set_opt(sbi->s_mount_opt, NO_UID32);
  1575. #ifdef CONFIG_EXT3_FS_XATTR
  1576. if (def_mount_opts & EXT3_DEFM_XATTR_USER)
  1577. set_opt(sbi->s_mount_opt, XATTR_USER);
  1578. #endif
  1579. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  1580. if (def_mount_opts & EXT3_DEFM_ACL)
  1581. set_opt(sbi->s_mount_opt, POSIX_ACL);
  1582. #endif
  1583. if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
  1584. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1585. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
  1586. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  1587. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
  1588. set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
  1589. if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
  1590. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1591. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_CONTINUE)
  1592. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  1593. else
  1594. set_opt(sbi->s_mount_opt, ERRORS_RO);
  1595. sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
  1596. sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
  1597. /* enable barriers by default */
  1598. set_opt(sbi->s_mount_opt, BARRIER);
  1599. set_opt(sbi->s_mount_opt, RESERVATION);
  1600. if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
  1601. NULL, 0))
  1602. goto failed_mount;
  1603. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1604. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  1605. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
  1606. (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
  1607. EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  1608. EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  1609. ext3_msg(sb, KERN_WARNING,
  1610. "warning: feature flags set on rev 0 fs, "
  1611. "running e2fsck is recommended");
  1612. /*
  1613. * Check feature flags regardless of the revision level, since we
  1614. * previously didn't change the revision level when setting the flags,
  1615. * so there is a chance incompat flags are set on a rev 0 filesystem.
  1616. */
  1617. features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
  1618. if (features) {
  1619. ext3_msg(sb, KERN_ERR,
  1620. "error: couldn't mount because of unsupported "
  1621. "optional features (%x)", le32_to_cpu(features));
  1622. goto failed_mount;
  1623. }
  1624. features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
  1625. if (!(sb->s_flags & MS_RDONLY) && features) {
  1626. ext3_msg(sb, KERN_ERR,
  1627. "error: couldn't mount RDWR because of unsupported "
  1628. "optional features (%x)", le32_to_cpu(features));
  1629. goto failed_mount;
  1630. }
  1631. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  1632. if (blocksize < EXT3_MIN_BLOCK_SIZE ||
  1633. blocksize > EXT3_MAX_BLOCK_SIZE) {
  1634. ext3_msg(sb, KERN_ERR,
  1635. "error: couldn't mount because of unsupported "
  1636. "filesystem blocksize %d", blocksize);
  1637. goto failed_mount;
  1638. }
  1639. hblock = bdev_logical_block_size(sb->s_bdev);
  1640. if (sb->s_blocksize != blocksize) {
  1641. /*
  1642. * Make sure the blocksize for the filesystem is larger
  1643. * than the hardware sectorsize for the machine.
  1644. */
  1645. if (blocksize < hblock) {
  1646. ext3_msg(sb, KERN_ERR,
  1647. "error: fsblocksize %d too small for "
  1648. "hardware sectorsize %d", blocksize, hblock);
  1649. goto failed_mount;
  1650. }
  1651. brelse (bh);
  1652. if (!sb_set_blocksize(sb, blocksize)) {
  1653. ext3_msg(sb, KERN_ERR,
  1654. "error: bad blocksize %d", blocksize);
  1655. goto out_fail;
  1656. }
  1657. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1658. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1659. bh = sb_bread(sb, logic_sb_block);
  1660. if (!bh) {
  1661. ext3_msg(sb, KERN_ERR,
  1662. "error: can't read superblock on 2nd try");
  1663. goto failed_mount;
  1664. }
  1665. es = (struct ext3_super_block *)(bh->b_data + offset);
  1666. sbi->s_es = es;
  1667. if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
  1668. ext3_msg(sb, KERN_ERR,
  1669. "error: magic mismatch");
  1670. goto failed_mount;
  1671. }
  1672. }
  1673. sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
  1674. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
  1675. sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
  1676. sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
  1677. } else {
  1678. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  1679. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  1680. if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
  1681. (!is_power_of_2(sbi->s_inode_size)) ||
  1682. (sbi->s_inode_size > blocksize)) {
  1683. ext3_msg(sb, KERN_ERR,
  1684. "error: unsupported inode size: %d",
  1685. sbi->s_inode_size);
  1686. goto failed_mount;
  1687. }
  1688. }
  1689. sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
  1690. le32_to_cpu(es->s_log_frag_size);
  1691. if (blocksize != sbi->s_frag_size) {
  1692. ext3_msg(sb, KERN_ERR,
  1693. "error: fragsize %lu != blocksize %u (unsupported)",
  1694. sbi->s_frag_size, blocksize);
  1695. goto failed_mount;
  1696. }
  1697. sbi->s_frags_per_block = 1;
  1698. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  1699. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  1700. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  1701. if (EXT3_INODE_SIZE(sb) == 0 || EXT3_INODES_PER_GROUP(sb) == 0)
  1702. goto cantfind_ext3;
  1703. sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
  1704. if (sbi->s_inodes_per_block == 0)
  1705. goto cantfind_ext3;
  1706. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  1707. sbi->s_inodes_per_block;
  1708. sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
  1709. sbi->s_sbh = bh;
  1710. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1711. sbi->s_addr_per_block_bits = ilog2(EXT3_ADDR_PER_BLOCK(sb));
  1712. sbi->s_desc_per_block_bits = ilog2(EXT3_DESC_PER_BLOCK(sb));
  1713. for (i=0; i < 4; i++)
  1714. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  1715. sbi->s_def_hash_version = es->s_def_hash_version;
  1716. i = le32_to_cpu(es->s_flags);
  1717. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  1718. sbi->s_hash_unsigned = 3;
  1719. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  1720. #ifdef __CHAR_UNSIGNED__
  1721. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  1722. sbi->s_hash_unsigned = 3;
  1723. #else
  1724. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  1725. #endif
  1726. }
  1727. if (sbi->s_blocks_per_group > blocksize * 8) {
  1728. ext3_msg(sb, KERN_ERR,
  1729. "#blocks per group too big: %lu",
  1730. sbi->s_blocks_per_group);
  1731. goto failed_mount;
  1732. }
  1733. if (sbi->s_frags_per_group > blocksize * 8) {
  1734. ext3_msg(sb, KERN_ERR,
  1735. "error: #fragments per group too big: %lu",
  1736. sbi->s_frags_per_group);
  1737. goto failed_mount;
  1738. }
  1739. if (sbi->s_inodes_per_group > blocksize * 8) {
  1740. ext3_msg(sb, KERN_ERR,
  1741. "error: #inodes per group too big: %lu",
  1742. sbi->s_inodes_per_group);
  1743. goto failed_mount;
  1744. }
  1745. err = generic_check_addressable(sb->s_blocksize_bits,
  1746. le32_to_cpu(es->s_blocks_count));
  1747. if (err) {
  1748. ext3_msg(sb, KERN_ERR,
  1749. "error: filesystem is too large to mount safely");
  1750. if (sizeof(sector_t) < 8)
  1751. ext3_msg(sb, KERN_ERR,
  1752. "error: CONFIG_LBDAF not enabled");
  1753. ret = err;
  1754. goto failed_mount;
  1755. }
  1756. if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
  1757. goto cantfind_ext3;
  1758. sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
  1759. le32_to_cpu(es->s_first_data_block) - 1)
  1760. / EXT3_BLOCKS_PER_GROUP(sb)) + 1;
  1761. db_count = DIV_ROUND_UP(sbi->s_groups_count, EXT3_DESC_PER_BLOCK(sb));
  1762. sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
  1763. GFP_KERNEL);
  1764. if (sbi->s_group_desc == NULL) {
  1765. ext3_msg(sb, KERN_ERR,
  1766. "error: not enough memory");
  1767. ret = -ENOMEM;
  1768. goto failed_mount;
  1769. }
  1770. bgl_lock_init(sbi->s_blockgroup_lock);
  1771. for (i = 0; i < db_count; i++) {
  1772. block = descriptor_loc(sb, logic_sb_block, i);
  1773. sbi->s_group_desc[i] = sb_bread(sb, block);
  1774. if (!sbi->s_group_desc[i]) {
  1775. ext3_msg(sb, KERN_ERR,
  1776. "error: can't read group descriptor %d", i);
  1777. db_count = i;
  1778. goto failed_mount2;
  1779. }
  1780. }
  1781. if (!ext3_check_descriptors (sb)) {
  1782. ext3_msg(sb, KERN_ERR,
  1783. "error: group descriptors corrupted");
  1784. goto failed_mount2;
  1785. }
  1786. sbi->s_gdb_count = db_count;
  1787. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1788. spin_lock_init(&sbi->s_next_gen_lock);
  1789. /* per fileystem reservation list head & lock */
  1790. spin_lock_init(&sbi->s_rsv_window_lock);
  1791. sbi->s_rsv_window_root = RB_ROOT;
  1792. /* Add a single, static dummy reservation to the start of the
  1793. * reservation window list --- it gives us a placeholder for
  1794. * append-at-start-of-list which makes the allocation logic
  1795. * _much_ simpler. */
  1796. sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1797. sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1798. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  1799. sbi->s_rsv_window_head.rsv_goal_size = 0;
  1800. ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
  1801. /*
  1802. * set up enough so that it can read an inode
  1803. */
  1804. sb->s_op = &ext3_sops;
  1805. sb->s_export_op = &ext3_export_ops;
  1806. sb->s_xattr = ext3_xattr_handlers;
  1807. #ifdef CONFIG_QUOTA
  1808. sb->s_qcop = &ext3_qctl_operations;
  1809. sb->dq_op = &ext3_quota_operations;
  1810. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
  1811. #endif
  1812. memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
  1813. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  1814. mutex_init(&sbi->s_orphan_lock);
  1815. mutex_init(&sbi->s_resize_lock);
  1816. sb->s_root = NULL;
  1817. needs_recovery = (es->s_last_orphan != 0 ||
  1818. EXT3_HAS_INCOMPAT_FEATURE(sb,
  1819. EXT3_FEATURE_INCOMPAT_RECOVER));
  1820. /*
  1821. * The first inode we look at is the journal inode. Don't try
  1822. * root first: it may be modified in the journal!
  1823. */
  1824. if (!test_opt(sb, NOLOAD) &&
  1825. EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
  1826. if (ext3_load_journal(sb, es, journal_devnum))
  1827. goto failed_mount2;
  1828. } else if (journal_inum) {
  1829. if (ext3_create_journal(sb, es, journal_inum))
  1830. goto failed_mount2;
  1831. } else {
  1832. if (!silent)
  1833. ext3_msg(sb, KERN_ERR,
  1834. "error: no journal found. "
  1835. "mounting ext3 over ext2?");
  1836. goto failed_mount2;
  1837. }
  1838. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  1839. ext3_count_free_blocks(sb), GFP_KERNEL);
  1840. if (!err) {
  1841. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  1842. ext3_count_free_inodes(sb), GFP_KERNEL);
  1843. }
  1844. if (!err) {
  1845. err = percpu_counter_init(&sbi->s_dirs_counter,
  1846. ext3_count_dirs(sb), GFP_KERNEL);
  1847. }
  1848. if (err) {
  1849. ext3_msg(sb, KERN_ERR, "error: insufficient memory");
  1850. ret = err;
  1851. goto failed_mount3;
  1852. }
  1853. /* We have now updated the journal if required, so we can
  1854. * validate the data journaling mode. */
  1855. switch (test_opt(sb, DATA_FLAGS)) {
  1856. case 0:
  1857. /* No mode set, assume a default based on the journal
  1858. capabilities: ORDERED_DATA if the journal can
  1859. cope, else JOURNAL_DATA */
  1860. if (journal_check_available_features
  1861. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
  1862. set_opt(sbi->s_mount_opt, DEFAULT_DATA_MODE);
  1863. else
  1864. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1865. break;
  1866. case EXT3_MOUNT_ORDERED_DATA:
  1867. case EXT3_MOUNT_WRITEBACK_DATA:
  1868. if (!journal_check_available_features
  1869. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
  1870. ext3_msg(sb, KERN_ERR,
  1871. "error: journal does not support "
  1872. "requested data journaling mode");
  1873. goto failed_mount3;
  1874. }
  1875. default:
  1876. break;
  1877. }
  1878. /*
  1879. * The journal_load will have done any necessary log recovery,
  1880. * so we can safely mount the rest of the filesystem now.
  1881. */
  1882. root = ext3_iget(sb, EXT3_ROOT_INO);
  1883. if (IS_ERR(root)) {
  1884. ext3_msg(sb, KERN_ERR, "error: get root inode failed");
  1885. ret = PTR_ERR(root);
  1886. goto failed_mount3;
  1887. }
  1888. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1889. iput(root);
  1890. ext3_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
  1891. goto failed_mount3;
  1892. }
  1893. sb->s_root = d_make_root(root);
  1894. if (!sb->s_root) {
  1895. ext3_msg(sb, KERN_ERR, "error: get root dentry failed");
  1896. ret = -ENOMEM;
  1897. goto failed_mount3;
  1898. }
  1899. if (ext3_setup_super(sb, es, sb->s_flags & MS_RDONLY))
  1900. sb->s_flags |= MS_RDONLY;
  1901. EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
  1902. ext3_orphan_cleanup(sb, es);
  1903. EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
  1904. if (needs_recovery) {
  1905. ext3_mark_recovery_complete(sb, es);
  1906. ext3_msg(sb, KERN_INFO, "recovery complete");
  1907. }
  1908. ext3_msg(sb, KERN_INFO, "mounted filesystem with %s data mode",
  1909. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
  1910. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
  1911. "writeback");
  1912. return 0;
  1913. cantfind_ext3:
  1914. if (!silent)
  1915. ext3_msg(sb, KERN_INFO,
  1916. "error: can't find ext3 filesystem on dev %s.",
  1917. sb->s_id);
  1918. goto failed_mount;
  1919. failed_mount3:
  1920. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  1921. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1922. percpu_counter_destroy(&sbi->s_dirs_counter);
  1923. journal_destroy(sbi->s_journal);
  1924. failed_mount2:
  1925. for (i = 0; i < db_count; i++)
  1926. brelse(sbi->s_group_desc[i]);
  1927. kfree(sbi->s_group_desc);
  1928. failed_mount:
  1929. #ifdef CONFIG_QUOTA
  1930. for (i = 0; i < EXT3_MAXQUOTAS; i++)
  1931. kfree(sbi->s_qf_names[i]);
  1932. #endif
  1933. ext3_blkdev_remove(sbi);
  1934. brelse(bh);
  1935. out_fail:
  1936. sb->s_fs_info = NULL;
  1937. kfree(sbi->s_blockgroup_lock);
  1938. kfree(sbi);
  1939. return ret;
  1940. }
  1941. /*
  1942. * Setup any per-fs journal parameters now. We'll do this both on
  1943. * initial mount, once the journal has been initialised but before we've
  1944. * done any recovery; and again on any subsequent remount.
  1945. */
  1946. static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
  1947. {
  1948. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1949. if (sbi->s_commit_interval)
  1950. journal->j_commit_interval = sbi->s_commit_interval;
  1951. /* We could also set up an ext3-specific default for the commit
  1952. * interval here, but for now we'll just fall back to the jbd
  1953. * default. */
  1954. spin_lock(&journal->j_state_lock);
  1955. if (test_opt(sb, BARRIER))
  1956. journal->j_flags |= JFS_BARRIER;
  1957. else
  1958. journal->j_flags &= ~JFS_BARRIER;
  1959. if (test_opt(sb, DATA_ERR_ABORT))
  1960. journal->j_flags |= JFS_ABORT_ON_SYNCDATA_ERR;
  1961. else
  1962. journal->j_flags &= ~JFS_ABORT_ON_SYNCDATA_ERR;
  1963. spin_unlock(&journal->j_state_lock);
  1964. }
  1965. static journal_t *ext3_get_journal(struct super_block *sb,
  1966. unsigned int journal_inum)
  1967. {
  1968. struct inode *journal_inode;
  1969. journal_t *journal;
  1970. /* First, test for the existence of a valid inode on disk. Bad
  1971. * things happen if we iget() an unused inode, as the subsequent
  1972. * iput() will try to delete it. */
  1973. journal_inode = ext3_iget(sb, journal_inum);
  1974. if (IS_ERR(journal_inode)) {
  1975. ext3_msg(sb, KERN_ERR, "error: no journal found");
  1976. return NULL;
  1977. }
  1978. if (!journal_inode->i_nlink) {
  1979. make_bad_inode(journal_inode);
  1980. iput(journal_inode);
  1981. ext3_msg(sb, KERN_ERR, "error: journal inode is deleted");
  1982. return NULL;
  1983. }
  1984. jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
  1985. journal_inode, journal_inode->i_size);
  1986. if (!S_ISREG(journal_inode->i_mode)) {
  1987. ext3_msg(sb, KERN_ERR, "error: invalid journal inode");
  1988. iput(journal_inode);
  1989. return NULL;
  1990. }
  1991. journal = journal_init_inode(journal_inode);
  1992. if (!journal) {
  1993. ext3_msg(sb, KERN_ERR, "error: could not load journal inode");
  1994. iput(journal_inode);
  1995. return NULL;
  1996. }
  1997. journal->j_private = sb;
  1998. ext3_init_journal_params(sb, journal);
  1999. return journal;
  2000. }
  2001. static journal_t *ext3_get_dev_journal(struct super_block *sb,
  2002. dev_t j_dev)
  2003. {
  2004. struct buffer_head * bh;
  2005. journal_t *journal;
  2006. ext3_fsblk_t start;
  2007. ext3_fsblk_t len;
  2008. int hblock, blocksize;
  2009. ext3_fsblk_t sb_block;
  2010. unsigned long offset;
  2011. struct ext3_super_block * es;
  2012. struct block_device *bdev;
  2013. bdev = ext3_blkdev_get(j_dev, sb);
  2014. if (bdev == NULL)
  2015. return NULL;
  2016. blocksize = sb->s_blocksize;
  2017. hblock = bdev_logical_block_size(bdev);
  2018. if (blocksize < hblock) {
  2019. ext3_msg(sb, KERN_ERR,
  2020. "error: blocksize too small for journal device");
  2021. goto out_bdev;
  2022. }
  2023. sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
  2024. offset = EXT3_MIN_BLOCK_SIZE % blocksize;
  2025. set_blocksize(bdev, blocksize);
  2026. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  2027. ext3_msg(sb, KERN_ERR, "error: couldn't read superblock of "
  2028. "external journal");
  2029. goto out_bdev;
  2030. }
  2031. es = (struct ext3_super_block *) (bh->b_data + offset);
  2032. if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
  2033. !(le32_to_cpu(es->s_feature_incompat) &
  2034. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  2035. ext3_msg(sb, KERN_ERR, "error: external journal has "
  2036. "bad superblock");
  2037. brelse(bh);
  2038. goto out_bdev;
  2039. }
  2040. if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  2041. ext3_msg(sb, KERN_ERR, "error: journal UUID does not match");
  2042. brelse(bh);
  2043. goto out_bdev;
  2044. }
  2045. len = le32_to_cpu(es->s_blocks_count);
  2046. start = sb_block + 1;
  2047. brelse(bh); /* we're done with the superblock */
  2048. journal = journal_init_dev(bdev, sb->s_bdev,
  2049. start, len, blocksize);
  2050. if (!journal) {
  2051. ext3_msg(sb, KERN_ERR,
  2052. "error: failed to create device journal");
  2053. goto out_bdev;
  2054. }
  2055. journal->j_private = sb;
  2056. if (!bh_uptodate_or_lock(journal->j_sb_buffer)) {
  2057. if (bh_submit_read(journal->j_sb_buffer)) {
  2058. ext3_msg(sb, KERN_ERR, "I/O error on journal device");
  2059. goto out_journal;
  2060. }
  2061. }
  2062. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  2063. ext3_msg(sb, KERN_ERR,
  2064. "error: external journal has more than one "
  2065. "user (unsupported) - %d",
  2066. be32_to_cpu(journal->j_superblock->s_nr_users));
  2067. goto out_journal;
  2068. }
  2069. EXT3_SB(sb)->journal_bdev = bdev;
  2070. ext3_init_journal_params(sb, journal);
  2071. return journal;
  2072. out_journal:
  2073. journal_destroy(journal);
  2074. out_bdev:
  2075. ext3_blkdev_put(bdev);
  2076. return NULL;
  2077. }
  2078. static int ext3_load_journal(struct super_block *sb,
  2079. struct ext3_super_block *es,
  2080. unsigned long journal_devnum)
  2081. {
  2082. journal_t *journal;
  2083. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  2084. dev_t journal_dev;
  2085. int err = 0;
  2086. int really_read_only;
  2087. if (journal_devnum &&
  2088. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  2089. ext3_msg(sb, KERN_INFO, "external journal device major/minor "
  2090. "numbers have changed");
  2091. journal_dev = new_decode_dev(journal_devnum);
  2092. } else
  2093. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  2094. really_read_only = bdev_read_only(sb->s_bdev);
  2095. /*
  2096. * Are we loading a blank journal or performing recovery after a
  2097. * crash? For recovery, we need to check in advance whether we
  2098. * can get read-write access to the device.
  2099. */
  2100. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
  2101. if (sb->s_flags & MS_RDONLY) {
  2102. ext3_msg(sb, KERN_INFO,
  2103. "recovery required on readonly filesystem");
  2104. if (really_read_only) {
  2105. ext3_msg(sb, KERN_ERR, "error: write access "
  2106. "unavailable, cannot proceed");
  2107. return -EROFS;
  2108. }
  2109. ext3_msg(sb, KERN_INFO,
  2110. "write access will be enabled during recovery");
  2111. }
  2112. }
  2113. if (journal_inum && journal_dev) {
  2114. ext3_msg(sb, KERN_ERR, "error: filesystem has both journal "
  2115. "and inode journals");
  2116. return -EINVAL;
  2117. }
  2118. if (journal_inum) {
  2119. if (!(journal = ext3_get_journal(sb, journal_inum)))
  2120. return -EINVAL;
  2121. } else {
  2122. if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
  2123. return -EINVAL;
  2124. }
  2125. if (!(journal->j_flags & JFS_BARRIER))
  2126. printk(KERN_INFO "EXT3-fs: barriers not enabled\n");
  2127. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  2128. err = journal_update_format(journal);
  2129. if (err) {
  2130. ext3_msg(sb, KERN_ERR, "error updating journal");
  2131. journal_destroy(journal);
  2132. return err;
  2133. }
  2134. }
  2135. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
  2136. err = journal_wipe(journal, !really_read_only);
  2137. if (!err)
  2138. err = journal_load(journal);
  2139. if (err) {
  2140. ext3_msg(sb, KERN_ERR, "error loading journal");
  2141. journal_destroy(journal);
  2142. return err;
  2143. }
  2144. EXT3_SB(sb)->s_journal = journal;
  2145. ext3_clear_journal_err(sb, es);
  2146. if (!really_read_only && journal_devnum &&
  2147. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  2148. es->s_journal_dev = cpu_to_le32(journal_devnum);
  2149. /* Make sure we flush the recovery flag to disk. */
  2150. ext3_commit_super(sb, es, 1);
  2151. }
  2152. return 0;
  2153. }
  2154. static int ext3_create_journal(struct super_block *sb,
  2155. struct ext3_super_block *es,
  2156. unsigned int journal_inum)
  2157. {
  2158. journal_t *journal;
  2159. int err;
  2160. if (sb->s_flags & MS_RDONLY) {
  2161. ext3_msg(sb, KERN_ERR,
  2162. "error: readonly filesystem when trying to "
  2163. "create journal");
  2164. return -EROFS;
  2165. }
  2166. journal = ext3_get_journal(sb, journal_inum);
  2167. if (!journal)
  2168. return -EINVAL;
  2169. ext3_msg(sb, KERN_INFO, "creating new journal on inode %u",
  2170. journal_inum);
  2171. err = journal_create(journal);
  2172. if (err) {
  2173. ext3_msg(sb, KERN_ERR, "error creating journal");
  2174. journal_destroy(journal);
  2175. return -EIO;
  2176. }
  2177. EXT3_SB(sb)->s_journal = journal;
  2178. ext3_update_dynamic_rev(sb);
  2179. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2180. EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
  2181. es->s_journal_inum = cpu_to_le32(journal_inum);
  2182. /* Make sure we flush the recovery flag to disk. */
  2183. ext3_commit_super(sb, es, 1);
  2184. return 0;
  2185. }
  2186. static int ext3_commit_super(struct super_block *sb,
  2187. struct ext3_super_block *es,
  2188. int sync)
  2189. {
  2190. struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
  2191. int error = 0;
  2192. if (!sbh)
  2193. return error;
  2194. if (buffer_write_io_error(sbh)) {
  2195. /*
  2196. * Oh, dear. A previous attempt to write the
  2197. * superblock failed. This could happen because the
  2198. * USB device was yanked out. Or it could happen to
  2199. * be a transient write error and maybe the block will
  2200. * be remapped. Nothing we can do but to retry the
  2201. * write and hope for the best.
  2202. */
  2203. ext3_msg(sb, KERN_ERR, "previous I/O error to "
  2204. "superblock detected");
  2205. clear_buffer_write_io_error(sbh);
  2206. set_buffer_uptodate(sbh);
  2207. }
  2208. /*
  2209. * If the file system is mounted read-only, don't update the
  2210. * superblock write time. This avoids updating the superblock
  2211. * write time when we are mounting the root file system
  2212. * read/only but we need to replay the journal; at that point,
  2213. * for people who are east of GMT and who make their clock
  2214. * tick in localtime for Windows bug-for-bug compatibility,
  2215. * the clock is set in the future, and this will cause e2fsck
  2216. * to complain and force a full file system check.
  2217. */
  2218. if (!(sb->s_flags & MS_RDONLY))
  2219. es->s_wtime = cpu_to_le32(get_seconds());
  2220. es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
  2221. es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
  2222. BUFFER_TRACE(sbh, "marking dirty");
  2223. mark_buffer_dirty(sbh);
  2224. if (sync) {
  2225. error = sync_dirty_buffer(sbh);
  2226. if (buffer_write_io_error(sbh)) {
  2227. ext3_msg(sb, KERN_ERR, "I/O error while writing "
  2228. "superblock");
  2229. clear_buffer_write_io_error(sbh);
  2230. set_buffer_uptodate(sbh);
  2231. }
  2232. }
  2233. return error;
  2234. }
  2235. /*
  2236. * Have we just finished recovery? If so, and if we are mounting (or
  2237. * remounting) the filesystem readonly, then we will end up with a
  2238. * consistent fs on disk. Record that fact.
  2239. */
  2240. static void ext3_mark_recovery_complete(struct super_block * sb,
  2241. struct ext3_super_block * es)
  2242. {
  2243. journal_t *journal = EXT3_SB(sb)->s_journal;
  2244. journal_lock_updates(journal);
  2245. if (journal_flush(journal) < 0)
  2246. goto out;
  2247. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
  2248. sb->s_flags & MS_RDONLY) {
  2249. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2250. ext3_commit_super(sb, es, 1);
  2251. }
  2252. out:
  2253. journal_unlock_updates(journal);
  2254. }
  2255. /*
  2256. * If we are mounting (or read-write remounting) a filesystem whose journal
  2257. * has recorded an error from a previous lifetime, move that error to the
  2258. * main filesystem now.
  2259. */
  2260. static void ext3_clear_journal_err(struct super_block *sb,
  2261. struct ext3_super_block *es)
  2262. {
  2263. journal_t *journal;
  2264. int j_errno;
  2265. const char *errstr;
  2266. journal = EXT3_SB(sb)->s_journal;
  2267. /*
  2268. * Now check for any error status which may have been recorded in the
  2269. * journal by a prior ext3_error() or ext3_abort()
  2270. */
  2271. j_errno = journal_errno(journal);
  2272. if (j_errno) {
  2273. char nbuf[16];
  2274. errstr = ext3_decode_error(sb, j_errno, nbuf);
  2275. ext3_warning(sb, __func__, "Filesystem error recorded "
  2276. "from previous mount: %s", errstr);
  2277. ext3_warning(sb, __func__, "Marking fs in need of "
  2278. "filesystem check.");
  2279. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  2280. es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
  2281. ext3_commit_super (sb, es, 1);
  2282. journal_clear_err(journal);
  2283. }
  2284. }
  2285. /*
  2286. * Force the running and committing transactions to commit,
  2287. * and wait on the commit.
  2288. */
  2289. int ext3_force_commit(struct super_block *sb)
  2290. {
  2291. journal_t *journal;
  2292. int ret;
  2293. if (sb->s_flags & MS_RDONLY)
  2294. return 0;
  2295. journal = EXT3_SB(sb)->s_journal;
  2296. ret = ext3_journal_force_commit(journal);
  2297. return ret;
  2298. }
  2299. static int ext3_sync_fs(struct super_block *sb, int wait)
  2300. {
  2301. tid_t target;
  2302. trace_ext3_sync_fs(sb, wait);
  2303. /*
  2304. * Writeback quota in non-journalled quota case - journalled quota has
  2305. * no dirty dquots
  2306. */
  2307. dquot_writeback_dquots(sb, -1);
  2308. if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
  2309. if (wait)
  2310. log_wait_commit(EXT3_SB(sb)->s_journal, target);
  2311. }
  2312. return 0;
  2313. }
  2314. /*
  2315. * LVM calls this function before a (read-only) snapshot is created. This
  2316. * gives us a chance to flush the journal completely and mark the fs clean.
  2317. */
  2318. static int ext3_freeze(struct super_block *sb)
  2319. {
  2320. int error = 0;
  2321. journal_t *journal;
  2322. if (!(sb->s_flags & MS_RDONLY)) {
  2323. journal = EXT3_SB(sb)->s_journal;
  2324. /* Now we set up the journal barrier. */
  2325. journal_lock_updates(journal);
  2326. /*
  2327. * We don't want to clear needs_recovery flag when we failed
  2328. * to flush the journal.
  2329. */
  2330. error = journal_flush(journal);
  2331. if (error < 0)
  2332. goto out;
  2333. /* Journal blocked and flushed, clear needs_recovery flag. */
  2334. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2335. error = ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  2336. if (error)
  2337. goto out;
  2338. }
  2339. return 0;
  2340. out:
  2341. journal_unlock_updates(journal);
  2342. return error;
  2343. }
  2344. /*
  2345. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  2346. * flag here, even though the filesystem is not technically dirty yet.
  2347. */
  2348. static int ext3_unfreeze(struct super_block *sb)
  2349. {
  2350. if (!(sb->s_flags & MS_RDONLY)) {
  2351. /* Reser the needs_recovery flag before the fs is unlocked. */
  2352. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2353. ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  2354. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  2355. }
  2356. return 0;
  2357. }
  2358. static int ext3_remount (struct super_block * sb, int * flags, char * data)
  2359. {
  2360. struct ext3_super_block * es;
  2361. struct ext3_sb_info *sbi = EXT3_SB(sb);
  2362. ext3_fsblk_t n_blocks_count = 0;
  2363. unsigned long old_sb_flags;
  2364. struct ext3_mount_options old_opts;
  2365. int enable_quota = 0;
  2366. int err;
  2367. #ifdef CONFIG_QUOTA
  2368. int i;
  2369. #endif
  2370. sync_filesystem(sb);
  2371. /* Store the original options */
  2372. old_sb_flags = sb->s_flags;
  2373. old_opts.s_mount_opt = sbi->s_mount_opt;
  2374. old_opts.s_resuid = sbi->s_resuid;
  2375. old_opts.s_resgid = sbi->s_resgid;
  2376. old_opts.s_commit_interval = sbi->s_commit_interval;
  2377. #ifdef CONFIG_QUOTA
  2378. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  2379. for (i = 0; i < EXT3_MAXQUOTAS; i++)
  2380. if (sbi->s_qf_names[i]) {
  2381. old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
  2382. GFP_KERNEL);
  2383. if (!old_opts.s_qf_names[i]) {
  2384. int j;
  2385. for (j = 0; j < i; j++)
  2386. kfree(old_opts.s_qf_names[j]);
  2387. return -ENOMEM;
  2388. }
  2389. } else
  2390. old_opts.s_qf_names[i] = NULL;
  2391. #endif
  2392. /*
  2393. * Allow the "check" option to be passed as a remount option.
  2394. */
  2395. if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
  2396. err = -EINVAL;
  2397. goto restore_opts;
  2398. }
  2399. if (test_opt(sb, ABORT))
  2400. ext3_abort(sb, __func__, "Abort forced by user");
  2401. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2402. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2403. es = sbi->s_es;
  2404. ext3_init_journal_params(sb, sbi->s_journal);
  2405. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  2406. n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
  2407. if (test_opt(sb, ABORT)) {
  2408. err = -EROFS;
  2409. goto restore_opts;
  2410. }
  2411. if (*flags & MS_RDONLY) {
  2412. err = dquot_suspend(sb, -1);
  2413. if (err < 0)
  2414. goto restore_opts;
  2415. /*
  2416. * First of all, the unconditional stuff we have to do
  2417. * to disable replay of the journal when we next remount
  2418. */
  2419. sb->s_flags |= MS_RDONLY;
  2420. /*
  2421. * OK, test if we are remounting a valid rw partition
  2422. * readonly, and if so set the rdonly flag and then
  2423. * mark the partition as valid again.
  2424. */
  2425. if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
  2426. (sbi->s_mount_state & EXT3_VALID_FS))
  2427. es->s_state = cpu_to_le16(sbi->s_mount_state);
  2428. ext3_mark_recovery_complete(sb, es);
  2429. } else {
  2430. __le32 ret;
  2431. if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
  2432. ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
  2433. ext3_msg(sb, KERN_WARNING,
  2434. "warning: couldn't remount RDWR "
  2435. "because of unsupported optional "
  2436. "features (%x)", le32_to_cpu(ret));
  2437. err = -EROFS;
  2438. goto restore_opts;
  2439. }
  2440. /*
  2441. * If we have an unprocessed orphan list hanging
  2442. * around from a previously readonly bdev mount,
  2443. * require a full umount & mount for now.
  2444. */
  2445. if (es->s_last_orphan) {
  2446. ext3_msg(sb, KERN_WARNING, "warning: couldn't "
  2447. "remount RDWR because of unprocessed "
  2448. "orphan inode list. Please "
  2449. "umount & mount instead.");
  2450. err = -EINVAL;
  2451. goto restore_opts;
  2452. }
  2453. /*
  2454. * Mounting a RDONLY partition read-write, so reread
  2455. * and store the current valid flag. (It may have
  2456. * been changed by e2fsck since we originally mounted
  2457. * the partition.)
  2458. */
  2459. ext3_clear_journal_err(sb, es);
  2460. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2461. if ((err = ext3_group_extend(sb, es, n_blocks_count)))
  2462. goto restore_opts;
  2463. if (!ext3_setup_super (sb, es, 0))
  2464. sb->s_flags &= ~MS_RDONLY;
  2465. enable_quota = 1;
  2466. }
  2467. }
  2468. #ifdef CONFIG_QUOTA
  2469. /* Release old quota file names */
  2470. for (i = 0; i < EXT3_MAXQUOTAS; i++)
  2471. kfree(old_opts.s_qf_names[i]);
  2472. #endif
  2473. if (enable_quota)
  2474. dquot_resume(sb, -1);
  2475. return 0;
  2476. restore_opts:
  2477. sb->s_flags = old_sb_flags;
  2478. sbi->s_mount_opt = old_opts.s_mount_opt;
  2479. sbi->s_resuid = old_opts.s_resuid;
  2480. sbi->s_resgid = old_opts.s_resgid;
  2481. sbi->s_commit_interval = old_opts.s_commit_interval;
  2482. #ifdef CONFIG_QUOTA
  2483. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  2484. for (i = 0; i < EXT3_MAXQUOTAS; i++) {
  2485. kfree(sbi->s_qf_names[i]);
  2486. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  2487. }
  2488. #endif
  2489. return err;
  2490. }
  2491. static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf)
  2492. {
  2493. struct super_block *sb = dentry->d_sb;
  2494. struct ext3_sb_info *sbi = EXT3_SB(sb);
  2495. struct ext3_super_block *es = sbi->s_es;
  2496. u64 fsid;
  2497. if (test_opt(sb, MINIX_DF)) {
  2498. sbi->s_overhead_last = 0;
  2499. } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  2500. unsigned long ngroups = sbi->s_groups_count, i;
  2501. ext3_fsblk_t overhead = 0;
  2502. smp_rmb();
  2503. /*
  2504. * Compute the overhead (FS structures). This is constant
  2505. * for a given filesystem unless the number of block groups
  2506. * changes so we cache the previous value until it does.
  2507. */
  2508. /*
  2509. * All of the blocks before first_data_block are
  2510. * overhead
  2511. */
  2512. overhead = le32_to_cpu(es->s_first_data_block);
  2513. /*
  2514. * Add the overhead attributed to the superblock and
  2515. * block group descriptors. If the sparse superblocks
  2516. * feature is turned on, then not all groups have this.
  2517. */
  2518. for (i = 0; i < ngroups; i++) {
  2519. overhead += ext3_bg_has_super(sb, i) +
  2520. ext3_bg_num_gdb(sb, i);
  2521. cond_resched();
  2522. }
  2523. /*
  2524. * Every block group has an inode bitmap, a block
  2525. * bitmap, and an inode table.
  2526. */
  2527. overhead += ngroups * (2 + sbi->s_itb_per_group);
  2528. /* Add the internal journal blocks as well */
  2529. if (sbi->s_journal && !sbi->journal_bdev)
  2530. overhead += sbi->s_journal->j_maxlen;
  2531. sbi->s_overhead_last = overhead;
  2532. smp_wmb();
  2533. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  2534. }
  2535. buf->f_type = EXT3_SUPER_MAGIC;
  2536. buf->f_bsize = sb->s_blocksize;
  2537. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
  2538. buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
  2539. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  2540. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  2541. buf->f_bavail = 0;
  2542. buf->f_files = le32_to_cpu(es->s_inodes_count);
  2543. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  2544. buf->f_namelen = EXT3_NAME_LEN;
  2545. fsid = le64_to_cpup((void *)es->s_uuid) ^
  2546. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  2547. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  2548. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  2549. return 0;
  2550. }
  2551. /* Helper function for writing quotas on sync - we need to start transaction before quota file
  2552. * is locked for write. Otherwise the are possible deadlocks:
  2553. * Process 1 Process 2
  2554. * ext3_create() quota_sync()
  2555. * journal_start() write_dquot()
  2556. * dquot_initialize() down(dqio_mutex)
  2557. * down(dqio_mutex) journal_start()
  2558. *
  2559. */
  2560. #ifdef CONFIG_QUOTA
  2561. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  2562. {
  2563. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
  2564. }
  2565. static int ext3_write_dquot(struct dquot *dquot)
  2566. {
  2567. int ret, err;
  2568. handle_t *handle;
  2569. struct inode *inode;
  2570. inode = dquot_to_inode(dquot);
  2571. handle = ext3_journal_start(inode,
  2572. EXT3_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  2573. if (IS_ERR(handle))
  2574. return PTR_ERR(handle);
  2575. ret = dquot_commit(dquot);
  2576. err = ext3_journal_stop(handle);
  2577. if (!ret)
  2578. ret = err;
  2579. return ret;
  2580. }
  2581. static int ext3_acquire_dquot(struct dquot *dquot)
  2582. {
  2583. int ret, err;
  2584. handle_t *handle;
  2585. handle = ext3_journal_start(dquot_to_inode(dquot),
  2586. EXT3_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  2587. if (IS_ERR(handle))
  2588. return PTR_ERR(handle);
  2589. ret = dquot_acquire(dquot);
  2590. err = ext3_journal_stop(handle);
  2591. if (!ret)
  2592. ret = err;
  2593. return ret;
  2594. }
  2595. static int ext3_release_dquot(struct dquot *dquot)
  2596. {
  2597. int ret, err;
  2598. handle_t *handle;
  2599. handle = ext3_journal_start(dquot_to_inode(dquot),
  2600. EXT3_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  2601. if (IS_ERR(handle)) {
  2602. /* Release dquot anyway to avoid endless cycle in dqput() */
  2603. dquot_release(dquot);
  2604. return PTR_ERR(handle);
  2605. }
  2606. ret = dquot_release(dquot);
  2607. err = ext3_journal_stop(handle);
  2608. if (!ret)
  2609. ret = err;
  2610. return ret;
  2611. }
  2612. static int ext3_mark_dquot_dirty(struct dquot *dquot)
  2613. {
  2614. /* Are we journaling quotas? */
  2615. if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  2616. EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  2617. dquot_mark_dquot_dirty(dquot);
  2618. return ext3_write_dquot(dquot);
  2619. } else {
  2620. return dquot_mark_dquot_dirty(dquot);
  2621. }
  2622. }
  2623. static int ext3_write_info(struct super_block *sb, int type)
  2624. {
  2625. int ret, err;
  2626. handle_t *handle;
  2627. /* Data block + inode block */
  2628. handle = ext3_journal_start(sb->s_root->d_inode, 2);
  2629. if (IS_ERR(handle))
  2630. return PTR_ERR(handle);
  2631. ret = dquot_commit_info(sb, type);
  2632. err = ext3_journal_stop(handle);
  2633. if (!ret)
  2634. ret = err;
  2635. return ret;
  2636. }
  2637. /*
  2638. * Turn on quotas during mount time - we need to find
  2639. * the quota file and such...
  2640. */
  2641. static int ext3_quota_on_mount(struct super_block *sb, int type)
  2642. {
  2643. return dquot_quota_on_mount(sb, EXT3_SB(sb)->s_qf_names[type],
  2644. EXT3_SB(sb)->s_jquota_fmt, type);
  2645. }
  2646. /*
  2647. * Standard function to be called on quota_on
  2648. */
  2649. static int ext3_quota_on(struct super_block *sb, int type, int format_id,
  2650. struct path *path)
  2651. {
  2652. int err;
  2653. if (!test_opt(sb, QUOTA))
  2654. return -EINVAL;
  2655. /* Quotafile not on the same filesystem? */
  2656. if (path->dentry->d_sb != sb)
  2657. return -EXDEV;
  2658. /* Journaling quota? */
  2659. if (EXT3_SB(sb)->s_qf_names[type]) {
  2660. /* Quotafile not of fs root? */
  2661. if (path->dentry->d_parent != sb->s_root)
  2662. ext3_msg(sb, KERN_WARNING,
  2663. "warning: Quota file not on filesystem root. "
  2664. "Journaled quota will not work.");
  2665. }
  2666. /*
  2667. * When we journal data on quota file, we have to flush journal to see
  2668. * all updates to the file when we bypass pagecache...
  2669. */
  2670. if (ext3_should_journal_data(path->dentry->d_inode)) {
  2671. /*
  2672. * We don't need to lock updates but journal_flush() could
  2673. * otherwise be livelocked...
  2674. */
  2675. journal_lock_updates(EXT3_SB(sb)->s_journal);
  2676. err = journal_flush(EXT3_SB(sb)->s_journal);
  2677. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  2678. if (err)
  2679. return err;
  2680. }
  2681. return dquot_quota_on(sb, type, format_id, path);
  2682. }
  2683. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  2684. * acquiring the locks... As quota files are never truncated and quota code
  2685. * itself serializes the operations (and no one else should touch the files)
  2686. * we don't have to be afraid of races */
  2687. static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
  2688. size_t len, loff_t off)
  2689. {
  2690. struct inode *inode = sb_dqopt(sb)->files[type];
  2691. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2692. int err = 0;
  2693. int offset = off & (sb->s_blocksize - 1);
  2694. int tocopy;
  2695. size_t toread;
  2696. struct buffer_head *bh;
  2697. loff_t i_size = i_size_read(inode);
  2698. if (off > i_size)
  2699. return 0;
  2700. if (off+len > i_size)
  2701. len = i_size-off;
  2702. toread = len;
  2703. while (toread > 0) {
  2704. tocopy = sb->s_blocksize - offset < toread ?
  2705. sb->s_blocksize - offset : toread;
  2706. bh = ext3_bread(NULL, inode, blk, 0, &err);
  2707. if (err)
  2708. return err;
  2709. if (!bh) /* A hole? */
  2710. memset(data, 0, tocopy);
  2711. else
  2712. memcpy(data, bh->b_data+offset, tocopy);
  2713. brelse(bh);
  2714. offset = 0;
  2715. toread -= tocopy;
  2716. data += tocopy;
  2717. blk++;
  2718. }
  2719. return len;
  2720. }
  2721. /* Write to quotafile (we know the transaction is already started and has
  2722. * enough credits) */
  2723. static ssize_t ext3_quota_write(struct super_block *sb, int type,
  2724. const char *data, size_t len, loff_t off)
  2725. {
  2726. struct inode *inode = sb_dqopt(sb)->files[type];
  2727. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2728. int err = 0;
  2729. int offset = off & (sb->s_blocksize - 1);
  2730. int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
  2731. struct buffer_head *bh;
  2732. handle_t *handle = journal_current_handle();
  2733. if (!handle) {
  2734. ext3_msg(sb, KERN_WARNING,
  2735. "warning: quota write (off=%llu, len=%llu)"
  2736. " cancelled because transaction is not started.",
  2737. (unsigned long long)off, (unsigned long long)len);
  2738. return -EIO;
  2739. }
  2740. /*
  2741. * Since we account only one data block in transaction credits,
  2742. * then it is impossible to cross a block boundary.
  2743. */
  2744. if (sb->s_blocksize - offset < len) {
  2745. ext3_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  2746. " cancelled because not block aligned",
  2747. (unsigned long long)off, (unsigned long long)len);
  2748. return -EIO;
  2749. }
  2750. bh = ext3_bread(handle, inode, blk, 1, &err);
  2751. if (!bh)
  2752. goto out;
  2753. if (journal_quota) {
  2754. err = ext3_journal_get_write_access(handle, bh);
  2755. if (err) {
  2756. brelse(bh);
  2757. goto out;
  2758. }
  2759. }
  2760. lock_buffer(bh);
  2761. memcpy(bh->b_data+offset, data, len);
  2762. flush_dcache_page(bh->b_page);
  2763. unlock_buffer(bh);
  2764. if (journal_quota)
  2765. err = ext3_journal_dirty_metadata(handle, bh);
  2766. else {
  2767. /* Always do at least ordered writes for quotas */
  2768. err = ext3_journal_dirty_data(handle, bh);
  2769. mark_buffer_dirty(bh);
  2770. }
  2771. brelse(bh);
  2772. out:
  2773. if (err)
  2774. return err;
  2775. if (inode->i_size < off + len) {
  2776. i_size_write(inode, off + len);
  2777. EXT3_I(inode)->i_disksize = inode->i_size;
  2778. }
  2779. inode->i_version++;
  2780. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2781. ext3_mark_inode_dirty(handle, inode);
  2782. return len;
  2783. }
  2784. #endif
  2785. static struct dentry *ext3_mount(struct file_system_type *fs_type,
  2786. int flags, const char *dev_name, void *data)
  2787. {
  2788. return mount_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
  2789. }
  2790. static struct file_system_type ext3_fs_type = {
  2791. .owner = THIS_MODULE,
  2792. .name = "ext3",
  2793. .mount = ext3_mount,
  2794. .kill_sb = kill_block_super,
  2795. .fs_flags = FS_REQUIRES_DEV,
  2796. };
  2797. MODULE_ALIAS_FS("ext3");
  2798. static int __init init_ext3_fs(void)
  2799. {
  2800. int err = init_ext3_xattr();
  2801. if (err)
  2802. return err;
  2803. err = init_inodecache();
  2804. if (err)
  2805. goto out1;
  2806. err = register_filesystem(&ext3_fs_type);
  2807. if (err)
  2808. goto out;
  2809. return 0;
  2810. out:
  2811. destroy_inodecache();
  2812. out1:
  2813. exit_ext3_xattr();
  2814. return err;
  2815. }
  2816. static void __exit exit_ext3_fs(void)
  2817. {
  2818. unregister_filesystem(&ext3_fs_type);
  2819. destroy_inodecache();
  2820. exit_ext3_xattr();
  2821. }
  2822. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  2823. MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
  2824. MODULE_LICENSE("GPL");
  2825. module_init(init_ext3_fs)
  2826. module_exit(exit_ext3_fs)