super.c 41 KB

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