super.c 80 KB

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