super.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. /*
  2. * linux/fs/ext2/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/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/parser.h>
  25. #include <linux/random.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/exportfs.h>
  28. #include <linux/vfs.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/mount.h>
  31. #include <linux/log2.h>
  32. #include <linux/quotaops.h>
  33. #include <asm/uaccess.h>
  34. #include "ext2.h"
  35. #include "xattr.h"
  36. #include "acl.h"
  37. static void ext2_sync_super(struct super_block *sb,
  38. struct ext2_super_block *es, int wait);
  39. static int ext2_remount (struct super_block * sb, int * flags, char * data);
  40. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
  41. static int ext2_sync_fs(struct super_block *sb, int wait);
  42. static int ext2_freeze(struct super_block *sb);
  43. static int ext2_unfreeze(struct super_block *sb);
  44. void ext2_error(struct super_block *sb, const char *function,
  45. const char *fmt, ...)
  46. {
  47. struct va_format vaf;
  48. va_list args;
  49. struct ext2_sb_info *sbi = EXT2_SB(sb);
  50. struct ext2_super_block *es = sbi->s_es;
  51. if (!(sb->s_flags & MS_RDONLY)) {
  52. spin_lock(&sbi->s_lock);
  53. sbi->s_mount_state |= EXT2_ERROR_FS;
  54. es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
  55. spin_unlock(&sbi->s_lock);
  56. ext2_sync_super(sb, es, 1);
  57. }
  58. va_start(args, fmt);
  59. vaf.fmt = fmt;
  60. vaf.va = &args;
  61. printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
  62. sb->s_id, function, &vaf);
  63. va_end(args);
  64. if (test_opt(sb, ERRORS_PANIC))
  65. panic("EXT2-fs: panic from previous error\n");
  66. if (test_opt(sb, ERRORS_RO)) {
  67. ext2_msg(sb, KERN_CRIT,
  68. "error: remounting filesystem read-only");
  69. sb->s_flags |= MS_RDONLY;
  70. }
  71. }
  72. void ext2_msg(struct super_block *sb, const char *prefix,
  73. const char *fmt, ...)
  74. {
  75. struct va_format vaf;
  76. va_list args;
  77. va_start(args, fmt);
  78. vaf.fmt = fmt;
  79. vaf.va = &args;
  80. printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
  81. va_end(args);
  82. }
  83. /*
  84. * This must be called with sbi->s_lock held.
  85. */
  86. void ext2_update_dynamic_rev(struct super_block *sb)
  87. {
  88. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  89. if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
  90. return;
  91. ext2_msg(sb, KERN_WARNING,
  92. "warning: updating to rev %d because of "
  93. "new feature flag, running e2fsck is recommended",
  94. EXT2_DYNAMIC_REV);
  95. es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
  96. es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
  97. es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
  98. /* leave es->s_feature_*compat flags alone */
  99. /* es->s_uuid will be set by e2fsck if empty */
  100. /*
  101. * The rest of the superblock fields should be zero, and if not it
  102. * means they are likely already in use, so leave them alone. We
  103. * can leave it up to e2fsck to clean up any inconsistencies there.
  104. */
  105. }
  106. static void ext2_put_super (struct super_block * sb)
  107. {
  108. int db_count;
  109. int i;
  110. struct ext2_sb_info *sbi = EXT2_SB(sb);
  111. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  112. ext2_xattr_put_super(sb);
  113. if (!(sb->s_flags & MS_RDONLY)) {
  114. struct ext2_super_block *es = sbi->s_es;
  115. spin_lock(&sbi->s_lock);
  116. es->s_state = cpu_to_le16(sbi->s_mount_state);
  117. spin_unlock(&sbi->s_lock);
  118. ext2_sync_super(sb, es, 1);
  119. }
  120. db_count = sbi->s_gdb_count;
  121. for (i = 0; i < db_count; i++)
  122. if (sbi->s_group_desc[i])
  123. brelse (sbi->s_group_desc[i]);
  124. kfree(sbi->s_group_desc);
  125. kfree(sbi->s_debts);
  126. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  127. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  128. percpu_counter_destroy(&sbi->s_dirs_counter);
  129. brelse (sbi->s_sbh);
  130. sb->s_fs_info = NULL;
  131. kfree(sbi->s_blockgroup_lock);
  132. kfree(sbi);
  133. }
  134. static struct kmem_cache * ext2_inode_cachep;
  135. static struct inode *ext2_alloc_inode(struct super_block *sb)
  136. {
  137. struct ext2_inode_info *ei;
  138. ei = kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
  139. if (!ei)
  140. return NULL;
  141. ei->i_block_alloc_info = NULL;
  142. ei->vfs_inode.i_version = 1;
  143. #ifdef CONFIG_QUOTA
  144. memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
  145. #endif
  146. return &ei->vfs_inode;
  147. }
  148. static void ext2_i_callback(struct rcu_head *head)
  149. {
  150. struct inode *inode = container_of(head, struct inode, i_rcu);
  151. kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
  152. }
  153. static void ext2_destroy_inode(struct inode *inode)
  154. {
  155. call_rcu(&inode->i_rcu, ext2_i_callback);
  156. }
  157. static void init_once(void *foo)
  158. {
  159. struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
  160. rwlock_init(&ei->i_meta_lock);
  161. #ifdef CONFIG_EXT2_FS_XATTR
  162. init_rwsem(&ei->xattr_sem);
  163. #endif
  164. mutex_init(&ei->truncate_mutex);
  165. #ifdef CONFIG_FS_DAX
  166. init_rwsem(&ei->dax_sem);
  167. #endif
  168. inode_init_once(&ei->vfs_inode);
  169. }
  170. static int __init init_inodecache(void)
  171. {
  172. ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
  173. sizeof(struct ext2_inode_info),
  174. 0, (SLAB_RECLAIM_ACCOUNT|
  175. SLAB_MEM_SPREAD),
  176. init_once);
  177. if (ext2_inode_cachep == NULL)
  178. return -ENOMEM;
  179. return 0;
  180. }
  181. static void destroy_inodecache(void)
  182. {
  183. /*
  184. * Make sure all delayed rcu free inodes are flushed before we
  185. * destroy cache.
  186. */
  187. rcu_barrier();
  188. kmem_cache_destroy(ext2_inode_cachep);
  189. }
  190. static int ext2_show_options(struct seq_file *seq, struct dentry *root)
  191. {
  192. struct super_block *sb = root->d_sb;
  193. struct ext2_sb_info *sbi = EXT2_SB(sb);
  194. struct ext2_super_block *es = sbi->s_es;
  195. unsigned long def_mount_opts;
  196. spin_lock(&sbi->s_lock);
  197. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  198. if (sbi->s_sb_block != 1)
  199. seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
  200. if (test_opt(sb, MINIX_DF))
  201. seq_puts(seq, ",minixdf");
  202. if (test_opt(sb, GRPID))
  203. seq_puts(seq, ",grpid");
  204. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
  205. seq_puts(seq, ",nogrpid");
  206. if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT2_DEF_RESUID)) ||
  207. le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
  208. seq_printf(seq, ",resuid=%u",
  209. from_kuid_munged(&init_user_ns, sbi->s_resuid));
  210. }
  211. if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT2_DEF_RESGID)) ||
  212. le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
  213. seq_printf(seq, ",resgid=%u",
  214. from_kgid_munged(&init_user_ns, sbi->s_resgid));
  215. }
  216. if (test_opt(sb, ERRORS_RO)) {
  217. int def_errors = le16_to_cpu(es->s_errors);
  218. if (def_errors == EXT2_ERRORS_PANIC ||
  219. def_errors == EXT2_ERRORS_CONTINUE) {
  220. seq_puts(seq, ",errors=remount-ro");
  221. }
  222. }
  223. if (test_opt(sb, ERRORS_CONT))
  224. seq_puts(seq, ",errors=continue");
  225. if (test_opt(sb, ERRORS_PANIC))
  226. seq_puts(seq, ",errors=panic");
  227. if (test_opt(sb, NO_UID32))
  228. seq_puts(seq, ",nouid32");
  229. if (test_opt(sb, DEBUG))
  230. seq_puts(seq, ",debug");
  231. if (test_opt(sb, OLDALLOC))
  232. seq_puts(seq, ",oldalloc");
  233. #ifdef CONFIG_EXT2_FS_XATTR
  234. if (test_opt(sb, XATTR_USER))
  235. seq_puts(seq, ",user_xattr");
  236. if (!test_opt(sb, XATTR_USER) &&
  237. (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
  238. seq_puts(seq, ",nouser_xattr");
  239. }
  240. #endif
  241. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  242. if (test_opt(sb, POSIX_ACL))
  243. seq_puts(seq, ",acl");
  244. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
  245. seq_puts(seq, ",noacl");
  246. #endif
  247. if (test_opt(sb, NOBH))
  248. seq_puts(seq, ",nobh");
  249. #if defined(CONFIG_QUOTA)
  250. if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
  251. seq_puts(seq, ",usrquota");
  252. if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
  253. seq_puts(seq, ",grpquota");
  254. #endif
  255. #ifdef CONFIG_FS_DAX
  256. if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
  257. seq_puts(seq, ",xip");
  258. if (sbi->s_mount_opt & EXT2_MOUNT_DAX)
  259. seq_puts(seq, ",dax");
  260. #endif
  261. if (!test_opt(sb, RESERVATION))
  262. seq_puts(seq, ",noreservation");
  263. spin_unlock(&sbi->s_lock);
  264. return 0;
  265. }
  266. #ifdef CONFIG_QUOTA
  267. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
  268. static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
  269. static struct dquot **ext2_get_dquots(struct inode *inode)
  270. {
  271. return EXT2_I(inode)->i_dquot;
  272. }
  273. #endif
  274. static const struct super_operations ext2_sops = {
  275. .alloc_inode = ext2_alloc_inode,
  276. .destroy_inode = ext2_destroy_inode,
  277. .write_inode = ext2_write_inode,
  278. .evict_inode = ext2_evict_inode,
  279. .put_super = ext2_put_super,
  280. .sync_fs = ext2_sync_fs,
  281. .freeze_fs = ext2_freeze,
  282. .unfreeze_fs = ext2_unfreeze,
  283. .statfs = ext2_statfs,
  284. .remount_fs = ext2_remount,
  285. .show_options = ext2_show_options,
  286. #ifdef CONFIG_QUOTA
  287. .quota_read = ext2_quota_read,
  288. .quota_write = ext2_quota_write,
  289. .get_dquots = ext2_get_dquots,
  290. #endif
  291. };
  292. static struct inode *ext2_nfs_get_inode(struct super_block *sb,
  293. u64 ino, u32 generation)
  294. {
  295. struct inode *inode;
  296. if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
  297. return ERR_PTR(-ESTALE);
  298. if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  299. return ERR_PTR(-ESTALE);
  300. /*
  301. * ext2_iget isn't quite right if the inode is currently unallocated!
  302. * However ext2_iget currently does appropriate checks to handle stale
  303. * inodes so everything is OK.
  304. */
  305. inode = ext2_iget(sb, ino);
  306. if (IS_ERR(inode))
  307. return ERR_CAST(inode);
  308. if (generation && inode->i_generation != generation) {
  309. /* we didn't find the right inode.. */
  310. iput(inode);
  311. return ERR_PTR(-ESTALE);
  312. }
  313. return inode;
  314. }
  315. static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  316. int fh_len, int fh_type)
  317. {
  318. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  319. ext2_nfs_get_inode);
  320. }
  321. static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
  322. int fh_len, int fh_type)
  323. {
  324. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  325. ext2_nfs_get_inode);
  326. }
  327. static const struct export_operations ext2_export_ops = {
  328. .fh_to_dentry = ext2_fh_to_dentry,
  329. .fh_to_parent = ext2_fh_to_parent,
  330. .get_parent = ext2_get_parent,
  331. };
  332. static unsigned long get_sb_block(void **data)
  333. {
  334. unsigned long sb_block;
  335. char *options = (char *) *data;
  336. if (!options || strncmp(options, "sb=", 3) != 0)
  337. return 1; /* Default location */
  338. options += 3;
  339. sb_block = simple_strtoul(options, &options, 0);
  340. if (*options && *options != ',') {
  341. printk("EXT2-fs: Invalid sb specification: %s\n",
  342. (char *) *data);
  343. return 1;
  344. }
  345. if (*options == ',')
  346. options++;
  347. *data = (void *) options;
  348. return sb_block;
  349. }
  350. enum {
  351. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  352. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
  353. Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
  354. Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
  355. Opt_acl, Opt_noacl, Opt_xip, Opt_dax, Opt_ignore, Opt_err, Opt_quota,
  356. Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
  357. };
  358. static const match_table_t tokens = {
  359. {Opt_bsd_df, "bsddf"},
  360. {Opt_minix_df, "minixdf"},
  361. {Opt_grpid, "grpid"},
  362. {Opt_grpid, "bsdgroups"},
  363. {Opt_nogrpid, "nogrpid"},
  364. {Opt_nogrpid, "sysvgroups"},
  365. {Opt_resgid, "resgid=%u"},
  366. {Opt_resuid, "resuid=%u"},
  367. {Opt_sb, "sb=%u"},
  368. {Opt_err_cont, "errors=continue"},
  369. {Opt_err_panic, "errors=panic"},
  370. {Opt_err_ro, "errors=remount-ro"},
  371. {Opt_nouid32, "nouid32"},
  372. {Opt_nocheck, "check=none"},
  373. {Opt_nocheck, "nocheck"},
  374. {Opt_debug, "debug"},
  375. {Opt_oldalloc, "oldalloc"},
  376. {Opt_orlov, "orlov"},
  377. {Opt_nobh, "nobh"},
  378. {Opt_user_xattr, "user_xattr"},
  379. {Opt_nouser_xattr, "nouser_xattr"},
  380. {Opt_acl, "acl"},
  381. {Opt_noacl, "noacl"},
  382. {Opt_xip, "xip"},
  383. {Opt_dax, "dax"},
  384. {Opt_grpquota, "grpquota"},
  385. {Opt_ignore, "noquota"},
  386. {Opt_quota, "quota"},
  387. {Opt_usrquota, "usrquota"},
  388. {Opt_reservation, "reservation"},
  389. {Opt_noreservation, "noreservation"},
  390. {Opt_err, NULL}
  391. };
  392. static int parse_options(char *options, struct super_block *sb)
  393. {
  394. char *p;
  395. struct ext2_sb_info *sbi = EXT2_SB(sb);
  396. substring_t args[MAX_OPT_ARGS];
  397. int option;
  398. kuid_t uid;
  399. kgid_t gid;
  400. if (!options)
  401. return 1;
  402. while ((p = strsep (&options, ",")) != NULL) {
  403. int token;
  404. if (!*p)
  405. continue;
  406. token = match_token(p, tokens, args);
  407. switch (token) {
  408. case Opt_bsd_df:
  409. clear_opt (sbi->s_mount_opt, MINIX_DF);
  410. break;
  411. case Opt_minix_df:
  412. set_opt (sbi->s_mount_opt, MINIX_DF);
  413. break;
  414. case Opt_grpid:
  415. set_opt (sbi->s_mount_opt, GRPID);
  416. break;
  417. case Opt_nogrpid:
  418. clear_opt (sbi->s_mount_opt, GRPID);
  419. break;
  420. case Opt_resuid:
  421. if (match_int(&args[0], &option))
  422. return 0;
  423. uid = make_kuid(current_user_ns(), option);
  424. if (!uid_valid(uid)) {
  425. ext2_msg(sb, KERN_ERR, "Invalid uid value %d", option);
  426. return 0;
  427. }
  428. sbi->s_resuid = uid;
  429. break;
  430. case Opt_resgid:
  431. if (match_int(&args[0], &option))
  432. return 0;
  433. gid = make_kgid(current_user_ns(), option);
  434. if (!gid_valid(gid)) {
  435. ext2_msg(sb, KERN_ERR, "Invalid gid value %d", option);
  436. return 0;
  437. }
  438. sbi->s_resgid = gid;
  439. break;
  440. case Opt_sb:
  441. /* handled by get_sb_block() instead of here */
  442. /* *sb_block = match_int(&args[0]); */
  443. break;
  444. case Opt_err_panic:
  445. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  446. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  447. set_opt (sbi->s_mount_opt, ERRORS_PANIC);
  448. break;
  449. case Opt_err_ro:
  450. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  451. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  452. set_opt (sbi->s_mount_opt, ERRORS_RO);
  453. break;
  454. case Opt_err_cont:
  455. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  456. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  457. set_opt (sbi->s_mount_opt, ERRORS_CONT);
  458. break;
  459. case Opt_nouid32:
  460. set_opt (sbi->s_mount_opt, NO_UID32);
  461. break;
  462. case Opt_nocheck:
  463. clear_opt (sbi->s_mount_opt, CHECK);
  464. break;
  465. case Opt_debug:
  466. set_opt (sbi->s_mount_opt, DEBUG);
  467. break;
  468. case Opt_oldalloc:
  469. set_opt (sbi->s_mount_opt, OLDALLOC);
  470. break;
  471. case Opt_orlov:
  472. clear_opt (sbi->s_mount_opt, OLDALLOC);
  473. break;
  474. case Opt_nobh:
  475. set_opt (sbi->s_mount_opt, NOBH);
  476. break;
  477. #ifdef CONFIG_EXT2_FS_XATTR
  478. case Opt_user_xattr:
  479. set_opt (sbi->s_mount_opt, XATTR_USER);
  480. break;
  481. case Opt_nouser_xattr:
  482. clear_opt (sbi->s_mount_opt, XATTR_USER);
  483. break;
  484. #else
  485. case Opt_user_xattr:
  486. case Opt_nouser_xattr:
  487. ext2_msg(sb, KERN_INFO, "(no)user_xattr options"
  488. "not supported");
  489. break;
  490. #endif
  491. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  492. case Opt_acl:
  493. set_opt(sbi->s_mount_opt, POSIX_ACL);
  494. break;
  495. case Opt_noacl:
  496. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  497. break;
  498. #else
  499. case Opt_acl:
  500. case Opt_noacl:
  501. ext2_msg(sb, KERN_INFO,
  502. "(no)acl options not supported");
  503. break;
  504. #endif
  505. case Opt_xip:
  506. ext2_msg(sb, KERN_INFO, "use dax instead of xip");
  507. set_opt(sbi->s_mount_opt, XIP);
  508. /* Fall through */
  509. case Opt_dax:
  510. #ifdef CONFIG_FS_DAX
  511. ext2_msg(sb, KERN_WARNING,
  512. "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
  513. set_opt(sbi->s_mount_opt, DAX);
  514. #else
  515. ext2_msg(sb, KERN_INFO, "dax option not supported");
  516. #endif
  517. break;
  518. #if defined(CONFIG_QUOTA)
  519. case Opt_quota:
  520. case Opt_usrquota:
  521. set_opt(sbi->s_mount_opt, USRQUOTA);
  522. break;
  523. case Opt_grpquota:
  524. set_opt(sbi->s_mount_opt, GRPQUOTA);
  525. break;
  526. #else
  527. case Opt_quota:
  528. case Opt_usrquota:
  529. case Opt_grpquota:
  530. ext2_msg(sb, KERN_INFO,
  531. "quota operations not supported");
  532. break;
  533. #endif
  534. case Opt_reservation:
  535. set_opt(sbi->s_mount_opt, RESERVATION);
  536. ext2_msg(sb, KERN_INFO, "reservations ON");
  537. break;
  538. case Opt_noreservation:
  539. clear_opt(sbi->s_mount_opt, RESERVATION);
  540. ext2_msg(sb, KERN_INFO, "reservations OFF");
  541. break;
  542. case Opt_ignore:
  543. break;
  544. default:
  545. return 0;
  546. }
  547. }
  548. return 1;
  549. }
  550. static int ext2_setup_super (struct super_block * sb,
  551. struct ext2_super_block * es,
  552. int read_only)
  553. {
  554. int res = 0;
  555. struct ext2_sb_info *sbi = EXT2_SB(sb);
  556. if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
  557. ext2_msg(sb, KERN_ERR,
  558. "error: revision level too high, "
  559. "forcing read-only mode");
  560. res = MS_RDONLY;
  561. }
  562. if (read_only)
  563. return res;
  564. if (!(sbi->s_mount_state & EXT2_VALID_FS))
  565. ext2_msg(sb, KERN_WARNING,
  566. "warning: mounting unchecked fs, "
  567. "running e2fsck is recommended");
  568. else if ((sbi->s_mount_state & EXT2_ERROR_FS))
  569. ext2_msg(sb, KERN_WARNING,
  570. "warning: mounting fs with errors, "
  571. "running e2fsck is recommended");
  572. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  573. le16_to_cpu(es->s_mnt_count) >=
  574. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  575. ext2_msg(sb, KERN_WARNING,
  576. "warning: maximal mount count reached, "
  577. "running e2fsck is recommended");
  578. else if (le32_to_cpu(es->s_checkinterval) &&
  579. (le32_to_cpu(es->s_lastcheck) +
  580. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  581. ext2_msg(sb, KERN_WARNING,
  582. "warning: checktime reached, "
  583. "running e2fsck is recommended");
  584. if (!le16_to_cpu(es->s_max_mnt_count))
  585. es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
  586. le16_add_cpu(&es->s_mnt_count, 1);
  587. if (test_opt (sb, DEBUG))
  588. ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
  589. "bpg=%lu, ipg=%lu, mo=%04lx]",
  590. EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
  591. sbi->s_frag_size,
  592. sbi->s_groups_count,
  593. EXT2_BLOCKS_PER_GROUP(sb),
  594. EXT2_INODES_PER_GROUP(sb),
  595. sbi->s_mount_opt);
  596. return res;
  597. }
  598. static int ext2_check_descriptors(struct super_block *sb)
  599. {
  600. int i;
  601. struct ext2_sb_info *sbi = EXT2_SB(sb);
  602. ext2_debug ("Checking group descriptors");
  603. for (i = 0; i < sbi->s_groups_count; i++) {
  604. struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
  605. ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
  606. ext2_fsblk_t last_block;
  607. if (i == sbi->s_groups_count - 1)
  608. last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
  609. else
  610. last_block = first_block +
  611. (EXT2_BLOCKS_PER_GROUP(sb) - 1);
  612. if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
  613. le32_to_cpu(gdp->bg_block_bitmap) > last_block)
  614. {
  615. ext2_error (sb, "ext2_check_descriptors",
  616. "Block bitmap for group %d"
  617. " not in group (block %lu)!",
  618. i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
  619. return 0;
  620. }
  621. if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
  622. le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
  623. {
  624. ext2_error (sb, "ext2_check_descriptors",
  625. "Inode bitmap for group %d"
  626. " not in group (block %lu)!",
  627. i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
  628. return 0;
  629. }
  630. if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
  631. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
  632. last_block)
  633. {
  634. ext2_error (sb, "ext2_check_descriptors",
  635. "Inode table for group %d"
  636. " not in group (block %lu)!",
  637. i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
  638. return 0;
  639. }
  640. }
  641. return 1;
  642. }
  643. /*
  644. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  645. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  646. * We need to be 1 filesystem block less than the 2^32 sector limit.
  647. */
  648. static loff_t ext2_max_size(int bits)
  649. {
  650. loff_t res = EXT2_NDIR_BLOCKS;
  651. int meta_blocks;
  652. unsigned int upper_limit;
  653. unsigned int ppb = 1 << (bits-2);
  654. /* This is calculated to be the largest file size for a
  655. * dense, file such that the total number of
  656. * sectors in the file, including data and all indirect blocks,
  657. * does not exceed 2^32 -1
  658. * __u32 i_blocks representing the total number of
  659. * 512 bytes blocks of the file
  660. */
  661. upper_limit = (1LL << 32) - 1;
  662. /* total blocks in file system block size */
  663. upper_limit >>= (bits - 9);
  664. /* Compute how many blocks we can address by block tree */
  665. res += 1LL << (bits-2);
  666. res += 1LL << (2*(bits-2));
  667. res += 1LL << (3*(bits-2));
  668. /* Does block tree limit file size? */
  669. if (res < upper_limit)
  670. goto check_lfs;
  671. res = upper_limit;
  672. /* How many metadata blocks are needed for addressing upper_limit? */
  673. upper_limit -= EXT2_NDIR_BLOCKS;
  674. /* indirect blocks */
  675. meta_blocks = 1;
  676. upper_limit -= ppb;
  677. /* double indirect blocks */
  678. if (upper_limit < ppb * ppb) {
  679. meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb);
  680. res -= meta_blocks;
  681. goto check_lfs;
  682. }
  683. meta_blocks += 1 + ppb;
  684. upper_limit -= ppb * ppb;
  685. /* tripple indirect blocks for the rest */
  686. meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb) +
  687. DIV_ROUND_UP(upper_limit, ppb*ppb);
  688. res -= meta_blocks;
  689. check_lfs:
  690. res <<= bits;
  691. if (res > MAX_LFS_FILESIZE)
  692. res = MAX_LFS_FILESIZE;
  693. return res;
  694. }
  695. static unsigned long descriptor_loc(struct super_block *sb,
  696. unsigned long logic_sb_block,
  697. int nr)
  698. {
  699. struct ext2_sb_info *sbi = EXT2_SB(sb);
  700. unsigned long bg, first_meta_bg;
  701. int has_super = 0;
  702. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  703. if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
  704. nr < first_meta_bg)
  705. return (logic_sb_block + nr + 1);
  706. bg = sbi->s_desc_per_block * nr;
  707. if (ext2_bg_has_super(sb, bg))
  708. has_super = 1;
  709. return ext2_group_first_block_no(sb, bg) + has_super;
  710. }
  711. static int ext2_fill_super(struct super_block *sb, void *data, int silent)
  712. {
  713. struct buffer_head * bh;
  714. struct ext2_sb_info * sbi;
  715. struct ext2_super_block * es;
  716. struct inode *root;
  717. unsigned long block;
  718. unsigned long sb_block = get_sb_block(&data);
  719. unsigned long logic_sb_block;
  720. unsigned long offset = 0;
  721. unsigned long def_mount_opts;
  722. long ret = -EINVAL;
  723. int blocksize = BLOCK_SIZE;
  724. int db_count;
  725. int i, j;
  726. __le32 features;
  727. int err;
  728. err = -ENOMEM;
  729. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  730. if (!sbi)
  731. goto failed;
  732. sbi->s_blockgroup_lock =
  733. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  734. if (!sbi->s_blockgroup_lock) {
  735. kfree(sbi);
  736. goto failed;
  737. }
  738. sb->s_fs_info = sbi;
  739. sbi->s_sb_block = sb_block;
  740. spin_lock_init(&sbi->s_lock);
  741. /*
  742. * See what the current blocksize for the device is, and
  743. * use that as the blocksize. Otherwise (or if the blocksize
  744. * is smaller than the default) use the default.
  745. * This is important for devices that have a hardware
  746. * sectorsize that is larger than the default.
  747. */
  748. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  749. if (!blocksize) {
  750. ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
  751. goto failed_sbi;
  752. }
  753. /*
  754. * If the superblock doesn't start on a hardware sector boundary,
  755. * calculate the offset.
  756. */
  757. if (blocksize != BLOCK_SIZE) {
  758. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  759. offset = (sb_block*BLOCK_SIZE) % blocksize;
  760. } else {
  761. logic_sb_block = sb_block;
  762. }
  763. if (!(bh = sb_bread(sb, logic_sb_block))) {
  764. ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
  765. goto failed_sbi;
  766. }
  767. /*
  768. * Note: s_es must be initialized as soon as possible because
  769. * some ext2 macro-instructions depend on its value
  770. */
  771. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  772. sbi->s_es = es;
  773. sb->s_magic = le16_to_cpu(es->s_magic);
  774. if (sb->s_magic != EXT2_SUPER_MAGIC)
  775. goto cantfind_ext2;
  776. /* Set defaults before we parse the mount options */
  777. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  778. if (def_mount_opts & EXT2_DEFM_DEBUG)
  779. set_opt(sbi->s_mount_opt, DEBUG);
  780. if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
  781. set_opt(sbi->s_mount_opt, GRPID);
  782. if (def_mount_opts & EXT2_DEFM_UID16)
  783. set_opt(sbi->s_mount_opt, NO_UID32);
  784. #ifdef CONFIG_EXT2_FS_XATTR
  785. if (def_mount_opts & EXT2_DEFM_XATTR_USER)
  786. set_opt(sbi->s_mount_opt, XATTR_USER);
  787. #endif
  788. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  789. if (def_mount_opts & EXT2_DEFM_ACL)
  790. set_opt(sbi->s_mount_opt, POSIX_ACL);
  791. #endif
  792. if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
  793. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  794. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
  795. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  796. else
  797. set_opt(sbi->s_mount_opt, ERRORS_RO);
  798. sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
  799. sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
  800. set_opt(sbi->s_mount_opt, RESERVATION);
  801. if (!parse_options((char *) data, sb))
  802. goto failed_mount;
  803. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  804. ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
  805. MS_POSIXACL : 0);
  806. sb->s_iflags |= SB_I_CGROUPWB;
  807. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
  808. (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
  809. EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  810. EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  811. ext2_msg(sb, KERN_WARNING,
  812. "warning: feature flags set on rev 0 fs, "
  813. "running e2fsck is recommended");
  814. /*
  815. * Check feature flags regardless of the revision level, since we
  816. * previously didn't change the revision level when setting the flags,
  817. * so there is a chance incompat flags are set on a rev 0 filesystem.
  818. */
  819. features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
  820. if (features) {
  821. ext2_msg(sb, KERN_ERR, "error: couldn't mount because of "
  822. "unsupported optional features (%x)",
  823. le32_to_cpu(features));
  824. goto failed_mount;
  825. }
  826. if (!(sb->s_flags & MS_RDONLY) &&
  827. (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
  828. ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
  829. "unsupported optional features (%x)",
  830. le32_to_cpu(features));
  831. goto failed_mount;
  832. }
  833. blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
  834. if (sbi->s_mount_opt & EXT2_MOUNT_DAX) {
  835. if (blocksize != PAGE_SIZE) {
  836. ext2_msg(sb, KERN_ERR,
  837. "error: unsupported blocksize for dax");
  838. goto failed_mount;
  839. }
  840. if (!sb->s_bdev->bd_disk->fops->direct_access) {
  841. ext2_msg(sb, KERN_ERR,
  842. "error: device does not support dax");
  843. goto failed_mount;
  844. }
  845. }
  846. /* If the blocksize doesn't match, re-read the thing.. */
  847. if (sb->s_blocksize != blocksize) {
  848. brelse(bh);
  849. if (!sb_set_blocksize(sb, blocksize)) {
  850. ext2_msg(sb, KERN_ERR,
  851. "error: bad blocksize %d", blocksize);
  852. goto failed_sbi;
  853. }
  854. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  855. offset = (sb_block*BLOCK_SIZE) % blocksize;
  856. bh = sb_bread(sb, logic_sb_block);
  857. if(!bh) {
  858. ext2_msg(sb, KERN_ERR, "error: couldn't read"
  859. "superblock on 2nd try");
  860. goto failed_sbi;
  861. }
  862. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  863. sbi->s_es = es;
  864. if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
  865. ext2_msg(sb, KERN_ERR, "error: magic mismatch");
  866. goto failed_mount;
  867. }
  868. }
  869. sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
  870. sb->s_max_links = EXT2_LINK_MAX;
  871. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
  872. sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
  873. sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
  874. } else {
  875. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  876. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  877. if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
  878. !is_power_of_2(sbi->s_inode_size) ||
  879. (sbi->s_inode_size > blocksize)) {
  880. ext2_msg(sb, KERN_ERR,
  881. "error: unsupported inode size: %d",
  882. sbi->s_inode_size);
  883. goto failed_mount;
  884. }
  885. }
  886. sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
  887. le32_to_cpu(es->s_log_frag_size);
  888. if (sbi->s_frag_size == 0)
  889. goto cantfind_ext2;
  890. sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
  891. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  892. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  893. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  894. if (EXT2_INODE_SIZE(sb) == 0)
  895. goto cantfind_ext2;
  896. sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
  897. if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
  898. goto cantfind_ext2;
  899. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  900. sbi->s_inodes_per_block;
  901. sbi->s_desc_per_block = sb->s_blocksize /
  902. sizeof (struct ext2_group_desc);
  903. sbi->s_sbh = bh;
  904. sbi->s_mount_state = le16_to_cpu(es->s_state);
  905. sbi->s_addr_per_block_bits =
  906. ilog2 (EXT2_ADDR_PER_BLOCK(sb));
  907. sbi->s_desc_per_block_bits =
  908. ilog2 (EXT2_DESC_PER_BLOCK(sb));
  909. if (sb->s_magic != EXT2_SUPER_MAGIC)
  910. goto cantfind_ext2;
  911. if (sb->s_blocksize != bh->b_size) {
  912. if (!silent)
  913. ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
  914. goto failed_mount;
  915. }
  916. if (sb->s_blocksize != sbi->s_frag_size) {
  917. ext2_msg(sb, KERN_ERR,
  918. "error: fragsize %lu != blocksize %lu"
  919. "(not supported yet)",
  920. sbi->s_frag_size, sb->s_blocksize);
  921. goto failed_mount;
  922. }
  923. if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
  924. ext2_msg(sb, KERN_ERR,
  925. "error: #blocks per group too big: %lu",
  926. sbi->s_blocks_per_group);
  927. goto failed_mount;
  928. }
  929. if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
  930. ext2_msg(sb, KERN_ERR,
  931. "error: #fragments per group too big: %lu",
  932. sbi->s_frags_per_group);
  933. goto failed_mount;
  934. }
  935. if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
  936. ext2_msg(sb, KERN_ERR,
  937. "error: #inodes per group too big: %lu",
  938. sbi->s_inodes_per_group);
  939. goto failed_mount;
  940. }
  941. if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
  942. goto cantfind_ext2;
  943. sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
  944. le32_to_cpu(es->s_first_data_block) - 1)
  945. / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
  946. db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
  947. EXT2_DESC_PER_BLOCK(sb);
  948. sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
  949. if (sbi->s_group_desc == NULL) {
  950. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  951. goto failed_mount;
  952. }
  953. bgl_lock_init(sbi->s_blockgroup_lock);
  954. sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
  955. if (!sbi->s_debts) {
  956. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  957. goto failed_mount_group_desc;
  958. }
  959. for (i = 0; i < db_count; i++) {
  960. block = descriptor_loc(sb, logic_sb_block, i);
  961. sbi->s_group_desc[i] = sb_bread(sb, block);
  962. if (!sbi->s_group_desc[i]) {
  963. for (j = 0; j < i; j++)
  964. brelse (sbi->s_group_desc[j]);
  965. ext2_msg(sb, KERN_ERR,
  966. "error: unable to read group descriptors");
  967. goto failed_mount_group_desc;
  968. }
  969. }
  970. if (!ext2_check_descriptors (sb)) {
  971. ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
  972. goto failed_mount2;
  973. }
  974. sbi->s_gdb_count = db_count;
  975. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  976. spin_lock_init(&sbi->s_next_gen_lock);
  977. /* per fileystem reservation list head & lock */
  978. spin_lock_init(&sbi->s_rsv_window_lock);
  979. sbi->s_rsv_window_root = RB_ROOT;
  980. /*
  981. * Add a single, static dummy reservation to the start of the
  982. * reservation window list --- it gives us a placeholder for
  983. * append-at-start-of-list which makes the allocation logic
  984. * _much_ simpler.
  985. */
  986. sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  987. sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  988. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  989. sbi->s_rsv_window_head.rsv_goal_size = 0;
  990. ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
  991. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  992. ext2_count_free_blocks(sb), GFP_KERNEL);
  993. if (!err) {
  994. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  995. ext2_count_free_inodes(sb), GFP_KERNEL);
  996. }
  997. if (!err) {
  998. err = percpu_counter_init(&sbi->s_dirs_counter,
  999. ext2_count_dirs(sb), GFP_KERNEL);
  1000. }
  1001. if (err) {
  1002. ext2_msg(sb, KERN_ERR, "error: insufficient memory");
  1003. goto failed_mount3;
  1004. }
  1005. /*
  1006. * set up enough so that it can read an inode
  1007. */
  1008. sb->s_op = &ext2_sops;
  1009. sb->s_export_op = &ext2_export_ops;
  1010. sb->s_xattr = ext2_xattr_handlers;
  1011. #ifdef CONFIG_QUOTA
  1012. sb->dq_op = &dquot_operations;
  1013. sb->s_qcop = &dquot_quotactl_ops;
  1014. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
  1015. #endif
  1016. root = ext2_iget(sb, EXT2_ROOT_INO);
  1017. if (IS_ERR(root)) {
  1018. ret = PTR_ERR(root);
  1019. goto failed_mount3;
  1020. }
  1021. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1022. iput(root);
  1023. ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
  1024. goto failed_mount3;
  1025. }
  1026. sb->s_root = d_make_root(root);
  1027. if (!sb->s_root) {
  1028. ext2_msg(sb, KERN_ERR, "error: get root inode failed");
  1029. ret = -ENOMEM;
  1030. goto failed_mount3;
  1031. }
  1032. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
  1033. ext2_msg(sb, KERN_WARNING,
  1034. "warning: mounting ext3 filesystem as ext2");
  1035. if (ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY))
  1036. sb->s_flags |= MS_RDONLY;
  1037. ext2_write_super(sb);
  1038. return 0;
  1039. cantfind_ext2:
  1040. if (!silent)
  1041. ext2_msg(sb, KERN_ERR,
  1042. "error: can't find an ext2 filesystem on dev %s.",
  1043. sb->s_id);
  1044. goto failed_mount;
  1045. failed_mount3:
  1046. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  1047. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1048. percpu_counter_destroy(&sbi->s_dirs_counter);
  1049. failed_mount2:
  1050. for (i = 0; i < db_count; i++)
  1051. brelse(sbi->s_group_desc[i]);
  1052. failed_mount_group_desc:
  1053. kfree(sbi->s_group_desc);
  1054. kfree(sbi->s_debts);
  1055. failed_mount:
  1056. brelse(bh);
  1057. failed_sbi:
  1058. sb->s_fs_info = NULL;
  1059. kfree(sbi->s_blockgroup_lock);
  1060. kfree(sbi);
  1061. failed:
  1062. return ret;
  1063. }
  1064. static void ext2_clear_super_error(struct super_block *sb)
  1065. {
  1066. struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
  1067. if (buffer_write_io_error(sbh)) {
  1068. /*
  1069. * Oh, dear. A previous attempt to write the
  1070. * superblock failed. This could happen because the
  1071. * USB device was yanked out. Or it could happen to
  1072. * be a transient write error and maybe the block will
  1073. * be remapped. Nothing we can do but to retry the
  1074. * write and hope for the best.
  1075. */
  1076. ext2_msg(sb, KERN_ERR,
  1077. "previous I/O error to superblock detected\n");
  1078. clear_buffer_write_io_error(sbh);
  1079. set_buffer_uptodate(sbh);
  1080. }
  1081. }
  1082. static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
  1083. int wait)
  1084. {
  1085. ext2_clear_super_error(sb);
  1086. spin_lock(&EXT2_SB(sb)->s_lock);
  1087. es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
  1088. es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
  1089. es->s_wtime = cpu_to_le32(get_seconds());
  1090. /* unlock before we do IO */
  1091. spin_unlock(&EXT2_SB(sb)->s_lock);
  1092. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  1093. if (wait)
  1094. sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
  1095. }
  1096. /*
  1097. * In the second extended file system, it is not necessary to
  1098. * write the super block since we use a mapping of the
  1099. * disk super block in a buffer.
  1100. *
  1101. * However, this function is still used to set the fs valid
  1102. * flags to 0. We need to set this flag to 0 since the fs
  1103. * may have been checked while mounted and e2fsck may have
  1104. * set s_state to EXT2_VALID_FS after some corrections.
  1105. */
  1106. static int ext2_sync_fs(struct super_block *sb, int wait)
  1107. {
  1108. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1109. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  1110. /*
  1111. * Write quota structures to quota file, sync_blockdev() will write
  1112. * them to disk later
  1113. */
  1114. dquot_writeback_dquots(sb, -1);
  1115. spin_lock(&sbi->s_lock);
  1116. if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
  1117. ext2_debug("setting valid to 0\n");
  1118. es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
  1119. }
  1120. spin_unlock(&sbi->s_lock);
  1121. ext2_sync_super(sb, es, wait);
  1122. return 0;
  1123. }
  1124. static int ext2_freeze(struct super_block *sb)
  1125. {
  1126. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1127. /*
  1128. * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared
  1129. * because we have unattached inodes and thus filesystem is not fully
  1130. * consistent.
  1131. */
  1132. if (atomic_long_read(&sb->s_remove_count)) {
  1133. ext2_sync_fs(sb, 1);
  1134. return 0;
  1135. }
  1136. /* Set EXT2_FS_VALID flag */
  1137. spin_lock(&sbi->s_lock);
  1138. sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state);
  1139. spin_unlock(&sbi->s_lock);
  1140. ext2_sync_super(sb, sbi->s_es, 1);
  1141. return 0;
  1142. }
  1143. static int ext2_unfreeze(struct super_block *sb)
  1144. {
  1145. /* Just write sb to clear EXT2_VALID_FS flag */
  1146. ext2_write_super(sb);
  1147. return 0;
  1148. }
  1149. void ext2_write_super(struct super_block *sb)
  1150. {
  1151. if (!(sb->s_flags & MS_RDONLY))
  1152. ext2_sync_fs(sb, 1);
  1153. }
  1154. static int ext2_remount (struct super_block * sb, int * flags, char * data)
  1155. {
  1156. struct ext2_sb_info * sbi = EXT2_SB(sb);
  1157. struct ext2_super_block * es;
  1158. struct ext2_mount_options old_opts;
  1159. unsigned long old_sb_flags;
  1160. int err;
  1161. sync_filesystem(sb);
  1162. spin_lock(&sbi->s_lock);
  1163. /* Store the old options */
  1164. old_sb_flags = sb->s_flags;
  1165. old_opts.s_mount_opt = sbi->s_mount_opt;
  1166. old_opts.s_resuid = sbi->s_resuid;
  1167. old_opts.s_resgid = sbi->s_resgid;
  1168. /*
  1169. * Allow the "check" option to be passed as a remount option.
  1170. */
  1171. if (!parse_options(data, sb)) {
  1172. err = -EINVAL;
  1173. goto restore_opts;
  1174. }
  1175. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1176. ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1177. es = sbi->s_es;
  1178. if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT2_MOUNT_DAX) {
  1179. ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
  1180. "dax flag with busy inodes while remounting");
  1181. sbi->s_mount_opt ^= EXT2_MOUNT_DAX;
  1182. }
  1183. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
  1184. spin_unlock(&sbi->s_lock);
  1185. return 0;
  1186. }
  1187. if (*flags & MS_RDONLY) {
  1188. if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
  1189. !(sbi->s_mount_state & EXT2_VALID_FS)) {
  1190. spin_unlock(&sbi->s_lock);
  1191. return 0;
  1192. }
  1193. /*
  1194. * OK, we are remounting a valid rw partition rdonly, so set
  1195. * the rdonly flag and then mark the partition as valid again.
  1196. */
  1197. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1198. es->s_mtime = cpu_to_le32(get_seconds());
  1199. spin_unlock(&sbi->s_lock);
  1200. err = dquot_suspend(sb, -1);
  1201. if (err < 0) {
  1202. spin_lock(&sbi->s_lock);
  1203. goto restore_opts;
  1204. }
  1205. ext2_sync_super(sb, es, 1);
  1206. } else {
  1207. __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1208. ~EXT2_FEATURE_RO_COMPAT_SUPP);
  1209. if (ret) {
  1210. ext2_msg(sb, KERN_WARNING,
  1211. "warning: couldn't remount RDWR because of "
  1212. "unsupported optional features (%x).",
  1213. le32_to_cpu(ret));
  1214. err = -EROFS;
  1215. goto restore_opts;
  1216. }
  1217. /*
  1218. * Mounting a RDONLY partition read-write, so reread and
  1219. * store the current valid flag. (It may have been changed
  1220. * by e2fsck since we originally mounted the partition.)
  1221. */
  1222. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1223. if (!ext2_setup_super (sb, es, 0))
  1224. sb->s_flags &= ~MS_RDONLY;
  1225. spin_unlock(&sbi->s_lock);
  1226. ext2_write_super(sb);
  1227. dquot_resume(sb, -1);
  1228. }
  1229. return 0;
  1230. restore_opts:
  1231. sbi->s_mount_opt = old_opts.s_mount_opt;
  1232. sbi->s_resuid = old_opts.s_resuid;
  1233. sbi->s_resgid = old_opts.s_resgid;
  1234. sb->s_flags = old_sb_flags;
  1235. spin_unlock(&sbi->s_lock);
  1236. return err;
  1237. }
  1238. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
  1239. {
  1240. struct super_block *sb = dentry->d_sb;
  1241. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1242. struct ext2_super_block *es = sbi->s_es;
  1243. u64 fsid;
  1244. spin_lock(&sbi->s_lock);
  1245. if (test_opt (sb, MINIX_DF))
  1246. sbi->s_overhead_last = 0;
  1247. else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  1248. unsigned long i, overhead = 0;
  1249. smp_rmb();
  1250. /*
  1251. * Compute the overhead (FS structures). This is constant
  1252. * for a given filesystem unless the number of block groups
  1253. * changes so we cache the previous value until it does.
  1254. */
  1255. /*
  1256. * All of the blocks before first_data_block are
  1257. * overhead
  1258. */
  1259. overhead = le32_to_cpu(es->s_first_data_block);
  1260. /*
  1261. * Add the overhead attributed to the superblock and
  1262. * block group descriptors. If the sparse superblocks
  1263. * feature is turned on, then not all groups have this.
  1264. */
  1265. for (i = 0; i < sbi->s_groups_count; i++)
  1266. overhead += ext2_bg_has_super(sb, i) +
  1267. ext2_bg_num_gdb(sb, i);
  1268. /*
  1269. * Every block group has an inode bitmap, a block
  1270. * bitmap, and an inode table.
  1271. */
  1272. overhead += (sbi->s_groups_count *
  1273. (2 + sbi->s_itb_per_group));
  1274. sbi->s_overhead_last = overhead;
  1275. smp_wmb();
  1276. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  1277. }
  1278. buf->f_type = EXT2_SUPER_MAGIC;
  1279. buf->f_bsize = sb->s_blocksize;
  1280. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
  1281. buf->f_bfree = ext2_count_free_blocks(sb);
  1282. es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
  1283. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  1284. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  1285. buf->f_bavail = 0;
  1286. buf->f_files = le32_to_cpu(es->s_inodes_count);
  1287. buf->f_ffree = ext2_count_free_inodes(sb);
  1288. es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
  1289. buf->f_namelen = EXT2_NAME_LEN;
  1290. fsid = le64_to_cpup((void *)es->s_uuid) ^
  1291. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  1292. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  1293. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  1294. spin_unlock(&sbi->s_lock);
  1295. return 0;
  1296. }
  1297. static struct dentry *ext2_mount(struct file_system_type *fs_type,
  1298. int flags, const char *dev_name, void *data)
  1299. {
  1300. return mount_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
  1301. }
  1302. #ifdef CONFIG_QUOTA
  1303. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  1304. * acquiring the locks... As quota files are never truncated and quota code
  1305. * itself serializes the operations (and no one else should touch the files)
  1306. * we don't have to be afraid of races */
  1307. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
  1308. size_t len, loff_t off)
  1309. {
  1310. struct inode *inode = sb_dqopt(sb)->files[type];
  1311. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1312. int err = 0;
  1313. int offset = off & (sb->s_blocksize - 1);
  1314. int tocopy;
  1315. size_t toread;
  1316. struct buffer_head tmp_bh;
  1317. struct buffer_head *bh;
  1318. loff_t i_size = i_size_read(inode);
  1319. if (off > i_size)
  1320. return 0;
  1321. if (off+len > i_size)
  1322. len = i_size-off;
  1323. toread = len;
  1324. while (toread > 0) {
  1325. tocopy = sb->s_blocksize - offset < toread ?
  1326. sb->s_blocksize - offset : toread;
  1327. tmp_bh.b_state = 0;
  1328. tmp_bh.b_size = sb->s_blocksize;
  1329. err = ext2_get_block(inode, blk, &tmp_bh, 0);
  1330. if (err < 0)
  1331. return err;
  1332. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  1333. memset(data, 0, tocopy);
  1334. else {
  1335. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1336. if (!bh)
  1337. return -EIO;
  1338. memcpy(data, bh->b_data+offset, tocopy);
  1339. brelse(bh);
  1340. }
  1341. offset = 0;
  1342. toread -= tocopy;
  1343. data += tocopy;
  1344. blk++;
  1345. }
  1346. return len;
  1347. }
  1348. /* Write to quotafile */
  1349. static ssize_t ext2_quota_write(struct super_block *sb, int type,
  1350. const char *data, size_t len, loff_t off)
  1351. {
  1352. struct inode *inode = sb_dqopt(sb)->files[type];
  1353. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1354. int err = 0;
  1355. int offset = off & (sb->s_blocksize - 1);
  1356. int tocopy;
  1357. size_t towrite = len;
  1358. struct buffer_head tmp_bh;
  1359. struct buffer_head *bh;
  1360. while (towrite > 0) {
  1361. tocopy = sb->s_blocksize - offset < towrite ?
  1362. sb->s_blocksize - offset : towrite;
  1363. tmp_bh.b_state = 0;
  1364. tmp_bh.b_size = sb->s_blocksize;
  1365. err = ext2_get_block(inode, blk, &tmp_bh, 1);
  1366. if (err < 0)
  1367. goto out;
  1368. if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
  1369. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1370. else
  1371. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  1372. if (unlikely(!bh)) {
  1373. err = -EIO;
  1374. goto out;
  1375. }
  1376. lock_buffer(bh);
  1377. memcpy(bh->b_data+offset, data, tocopy);
  1378. flush_dcache_page(bh->b_page);
  1379. set_buffer_uptodate(bh);
  1380. mark_buffer_dirty(bh);
  1381. unlock_buffer(bh);
  1382. brelse(bh);
  1383. offset = 0;
  1384. towrite -= tocopy;
  1385. data += tocopy;
  1386. blk++;
  1387. }
  1388. out:
  1389. if (len == towrite)
  1390. return err;
  1391. if (inode->i_size < off+len-towrite)
  1392. i_size_write(inode, off+len-towrite);
  1393. inode->i_version++;
  1394. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1395. mark_inode_dirty(inode);
  1396. return len - towrite;
  1397. }
  1398. #endif
  1399. static struct file_system_type ext2_fs_type = {
  1400. .owner = THIS_MODULE,
  1401. .name = "ext2",
  1402. .mount = ext2_mount,
  1403. .kill_sb = kill_block_super,
  1404. .fs_flags = FS_REQUIRES_DEV,
  1405. };
  1406. MODULE_ALIAS_FS("ext2");
  1407. static int __init init_ext2_fs(void)
  1408. {
  1409. int err = init_ext2_xattr();
  1410. if (err)
  1411. return err;
  1412. err = init_inodecache();
  1413. if (err)
  1414. goto out1;
  1415. err = register_filesystem(&ext2_fs_type);
  1416. if (err)
  1417. goto out;
  1418. return 0;
  1419. out:
  1420. destroy_inodecache();
  1421. out1:
  1422. exit_ext2_xattr();
  1423. return err;
  1424. }
  1425. static void __exit exit_ext2_fs(void)
  1426. {
  1427. unregister_filesystem(&ext2_fs_type);
  1428. destroy_inodecache();
  1429. exit_ext2_xattr();
  1430. }
  1431. MODULE_AUTHOR("Remy Card and others");
  1432. MODULE_DESCRIPTION("Second Extended Filesystem");
  1433. MODULE_LICENSE("GPL");
  1434. module_init(init_ext2_fs)
  1435. module_exit(exit_ext2_fs)