super.c 39 KB

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