super.c 82 KB

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