super.c 84 KB

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