super.c 79 KB

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