super.c 35 KB

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