super.c 42 KB

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