summaryrefslogtreecommitdiff
path: root/ext2fs/hyper.c
blob: 79c1b08ae15ca4cb8ab33cb10848ad3d334f757a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Fetching and storing the hypermetadata (superblock and bg summary info).

   Copyright (C) 1994, 1995 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <string.h>
#include <stdio.h>
#include "ext2fs.h"

error_t
get_hypermetadata (void)
{
  error_t err = diskfs_catch_exception ();

  if (err)
    return err;

  sblock = (struct ext2_super_block *)boffs_ptr (SBLOCK_OFFS);
  
  if (sblock->s_magic != EXT2_SUPER_MAGIC
#ifdef EXT2FS_PRE_02B_COMPAT
      && sblock->s_magic != EXT2_PRE_02B_MAGIC
#endif
      )
    ext2_panic("get_hypermetadata",
	       "Bad magic number %#x (should be %#x)",
	       sblock->s_magic, EXT2_SUPER_MAGIC);

  block_size = EXT2_MIN_BLOCK_SIZE << sblock->s_log_block_size;

  if (block_size > 8192)
    ext2_panic("get_hypermetadata",
	       "Block size %ld is too big (max is 8192 bytes)", block_size);

  log2_dev_blocks_per_fs_block = 0;
  while ((device_block_size << log2_dev_blocks_per_fs_block) < block_size)
    log2_dev_blocks_per_fs_block++;
  if ((device_block_size << log2_dev_blocks_per_fs_block) != block_size)
    ext2_panic("get_hypermetadata",
	       "Block size %ld isn't a power-of-two multiple of the device"
	       " block size (%d)!",
	       block_size, device_block_size);

  log2_stat_blocks_per_fs_block = 0;
  while ((512 << log2_stat_blocks_per_fs_block) < block_size)
    log2_stat_blocks_per_fs_block++;
  if ((512 << log2_stat_blocks_per_fs_block) != block_size)
    ext2_panic("get_hypermetadata",
	       "Block size %ld isn't a power-of-two multiple of 512!",
	       block_size);

  log2_block_size = 0;
  while ((1 << log2_block_size) < block_size)
    log2_block_size++;
  if ((1 << log2_block_size) != block_size)
    ext2_panic("get_hypermetadata",
	       "Block size %ld isn't a power of two!", block_size);

  if (!diskfs_readonly && block_size < vm_page_size)
    /* If the block size is too small, we have to take extra care when
       writing out pages from the global pager, to make sure we don't stomp
       on any file pager blocks.  In this case use a bitmap to record which
       global blocks are actually modified so the pager can write only them.
       Since small block sizes are probably used on smaller media (to save
       space), this shouldn't be too much of a problem.  */
    {
      /* One bit per filesystem block.  */
      vm_allocate (mach_task_self (), (vm_address_t *)&modified_global_blocks,
		   (sblock->s_blocks_count >> log2_block_size) >> 3, 1);
    }
  else
    modified_global_blocks = 0;

  /* Set these handy variables.  */
  inodes_per_block = block_size / sizeof (struct ext2_inode);

  frag_size = EXT2_MIN_FRAG_SIZE << sblock->s_log_frag_size;
  if (frag_size)
    frags_per_block = block_size / frag_size;
  else
    ext2_panic("get_hypermetadata", "Frag size is zero!");

  groups_count =
    ((sblock->s_blocks_count - sblock->s_first_data_block +
      sblock->s_blocks_per_group - 1)
     / sblock->s_blocks_per_group);

  itb_per_group = sblock->s_inodes_per_group / inodes_per_block;
  desc_per_block = block_size / sizeof (struct ext2_group_desc);
  addr_per_block = block_size / sizeof (u32);
  db_per_group = (groups_count + desc_per_block - 1) / desc_per_block;

  diskfs_end_catch_exception ();

  return 0;
}

void
diskfs_set_hypermetadata (int wait, int clean)
{
  if (clean && !(sblock->s_state & EXT2_VALID_FS))
    /* The filesystem is clean, so we need to set the clean flag.  We
       just write the superblock directly, without using the paged copy.  */
    {
      vm_address_t page_buf = get_page_buf ();
      sblock_dirty = 0;		/* doesn't matter if this gets stomped */
      bcopy (sblock, (void *)page_buf, SBLOCK_SIZE);
      ((struct ext2_super_block *)page_buf)->s_state |= EXT2_VALID_FS;
      dev_write_sync (SBLOCK_OFFS / device_block_size, page_buf, block_size);
      free_page_buf (page_buf);
    }
  else if (sblock_dirty)
    sync_super_block ();
}