Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * swapfs.c --- swap ext2 filesystem data structures |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996, 2002 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #if HAVE_UNISTD_H |
| 14 | #include <unistd.h> |
| 15 | #endif |
| 16 | #include <string.h> |
| 17 | #include <time.h> |
| 18 | |
| 19 | #include "ext2_fs.h" |
| 20 | #include "ext2fs.h" |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 21 | #include "ext2_ext_attr.h" |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef EXT2FS_ENABLE_SWAPFS |
| 24 | void ext2fs_swap_super(struct ext2_super_block * sb) |
| 25 | { |
| 26 | int i; |
| 27 | sb->s_inodes_count = ext2fs_swab32(sb->s_inodes_count); |
| 28 | sb->s_blocks_count = ext2fs_swab32(sb->s_blocks_count); |
| 29 | sb->s_r_blocks_count = ext2fs_swab32(sb->s_r_blocks_count); |
| 30 | sb->s_free_blocks_count = ext2fs_swab32(sb->s_free_blocks_count); |
| 31 | sb->s_free_inodes_count = ext2fs_swab32(sb->s_free_inodes_count); |
| 32 | sb->s_first_data_block = ext2fs_swab32(sb->s_first_data_block); |
| 33 | sb->s_log_block_size = ext2fs_swab32(sb->s_log_block_size); |
| 34 | sb->s_log_frag_size = ext2fs_swab32(sb->s_log_frag_size); |
| 35 | sb->s_blocks_per_group = ext2fs_swab32(sb->s_blocks_per_group); |
| 36 | sb->s_frags_per_group = ext2fs_swab32(sb->s_frags_per_group); |
| 37 | sb->s_inodes_per_group = ext2fs_swab32(sb->s_inodes_per_group); |
| 38 | sb->s_mtime = ext2fs_swab32(sb->s_mtime); |
| 39 | sb->s_wtime = ext2fs_swab32(sb->s_wtime); |
| 40 | sb->s_mnt_count = ext2fs_swab16(sb->s_mnt_count); |
| 41 | sb->s_max_mnt_count = ext2fs_swab16(sb->s_max_mnt_count); |
| 42 | sb->s_magic = ext2fs_swab16(sb->s_magic); |
| 43 | sb->s_state = ext2fs_swab16(sb->s_state); |
| 44 | sb->s_errors = ext2fs_swab16(sb->s_errors); |
| 45 | sb->s_minor_rev_level = ext2fs_swab16(sb->s_minor_rev_level); |
| 46 | sb->s_lastcheck = ext2fs_swab32(sb->s_lastcheck); |
| 47 | sb->s_checkinterval = ext2fs_swab32(sb->s_checkinterval); |
| 48 | sb->s_creator_os = ext2fs_swab32(sb->s_creator_os); |
| 49 | sb->s_rev_level = ext2fs_swab32(sb->s_rev_level); |
| 50 | sb->s_def_resuid = ext2fs_swab16(sb->s_def_resuid); |
| 51 | sb->s_def_resgid = ext2fs_swab16(sb->s_def_resgid); |
| 52 | sb->s_first_ino = ext2fs_swab32(sb->s_first_ino); |
| 53 | sb->s_inode_size = ext2fs_swab16(sb->s_inode_size); |
| 54 | sb->s_block_group_nr = ext2fs_swab16(sb->s_block_group_nr); |
| 55 | sb->s_feature_compat = ext2fs_swab32(sb->s_feature_compat); |
| 56 | sb->s_feature_incompat = ext2fs_swab32(sb->s_feature_incompat); |
| 57 | sb->s_feature_ro_compat = ext2fs_swab32(sb->s_feature_ro_compat); |
| 58 | sb->s_algorithm_usage_bitmap = ext2fs_swab32(sb->s_algorithm_usage_bitmap); |
| 59 | sb->s_reserved_gdt_blocks = ext2fs_swab16(sb->s_reserved_gdt_blocks); |
| 60 | sb->s_journal_inum = ext2fs_swab32(sb->s_journal_inum); |
| 61 | sb->s_journal_dev = ext2fs_swab32(sb->s_journal_dev); |
| 62 | sb->s_last_orphan = ext2fs_swab32(sb->s_last_orphan); |
| 63 | sb->s_default_mount_opts = ext2fs_swab32(sb->s_default_mount_opts); |
| 64 | sb->s_first_meta_bg = ext2fs_swab32(sb->s_first_meta_bg); |
| 65 | sb->s_mkfs_time = ext2fs_swab32(sb->s_mkfs_time); |
| 66 | for (i=0; i < 4; i++) |
| 67 | sb->s_hash_seed[i] = ext2fs_swab32(sb->s_hash_seed[i]); |
| 68 | for (i=0; i < 17; i++) |
| 69 | sb->s_jnl_blocks[i] = ext2fs_swab32(sb->s_jnl_blocks[i]); |
| 70 | |
| 71 | } |
| 72 | |
| 73 | void ext2fs_swap_group_desc(struct ext2_group_desc *gdp) |
| 74 | { |
| 75 | gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap); |
| 76 | gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap); |
| 77 | gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table); |
| 78 | gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count); |
| 79 | gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count); |
| 80 | gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count); |
| 81 | } |
| 82 | |
| 83 | void ext2fs_swap_ext_attr(char *to, char *from, int bufsize, int has_header) |
| 84 | { |
| 85 | struct ext2_ext_attr_header *from_header = |
| 86 | (struct ext2_ext_attr_header *)from; |
| 87 | struct ext2_ext_attr_header *to_header = |
| 88 | (struct ext2_ext_attr_header *)to; |
| 89 | struct ext2_ext_attr_entry *from_entry, *to_entry; |
| 90 | char *from_end = (char *)from_header + bufsize; |
| 91 | int n; |
| 92 | |
| 93 | if (to_header != from_header) |
| 94 | memcpy(to_header, from_header, bufsize); |
| 95 | |
| 96 | from_entry = (struct ext2_ext_attr_entry *)from_header; |
| 97 | to_entry = (struct ext2_ext_attr_entry *)to_header; |
| 98 | |
| 99 | if (has_header) { |
| 100 | to_header->h_magic = ext2fs_swab32(from_header->h_magic); |
| 101 | to_header->h_blocks = ext2fs_swab32(from_header->h_blocks); |
| 102 | to_header->h_refcount = ext2fs_swab32(from_header->h_refcount); |
| 103 | for (n=0; n<4; n++) |
| 104 | to_header->h_reserved[n] = |
| 105 | ext2fs_swab32(from_header->h_reserved[n]); |
| 106 | from_entry = (struct ext2_ext_attr_entry *)(from_header+1); |
| 107 | to_entry = (struct ext2_ext_attr_entry *)(to_header+1); |
| 108 | } |
| 109 | |
| 110 | while ((char *)from_entry < from_end && *(__u32 *)from_entry) { |
| 111 | to_entry->e_value_offs = |
| 112 | ext2fs_swab16(from_entry->e_value_offs); |
| 113 | to_entry->e_value_block = |
| 114 | ext2fs_swab32(from_entry->e_value_block); |
| 115 | to_entry->e_value_size = |
| 116 | ext2fs_swab32(from_entry->e_value_size); |
| 117 | from_entry = EXT2_EXT_ATTR_NEXT(from_entry); |
| 118 | to_entry = EXT2_EXT_ATTR_NEXT(to_entry); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t, |
| 123 | struct ext2_inode_large *f, int hostorder, |
| 124 | int bufsize) |
| 125 | { |
| 126 | unsigned i; |
| 127 | int islnk = 0; |
| 128 | __u32 *eaf, *eat; |
| 129 | |
| 130 | if (hostorder && LINUX_S_ISLNK(f->i_mode)) |
| 131 | islnk = 1; |
| 132 | t->i_mode = ext2fs_swab16(f->i_mode); |
| 133 | if (!hostorder && LINUX_S_ISLNK(t->i_mode)) |
| 134 | islnk = 1; |
| 135 | t->i_uid = ext2fs_swab16(f->i_uid); |
| 136 | t->i_size = ext2fs_swab32(f->i_size); |
| 137 | t->i_atime = ext2fs_swab32(f->i_atime); |
| 138 | t->i_ctime = ext2fs_swab32(f->i_ctime); |
| 139 | t->i_mtime = ext2fs_swab32(f->i_mtime); |
| 140 | t->i_dtime = ext2fs_swab32(f->i_dtime); |
| 141 | t->i_gid = ext2fs_swab16(f->i_gid); |
| 142 | t->i_links_count = ext2fs_swab16(f->i_links_count); |
| 143 | t->i_blocks = ext2fs_swab32(f->i_blocks); |
| 144 | t->i_flags = ext2fs_swab32(f->i_flags); |
| 145 | t->i_file_acl = ext2fs_swab32(f->i_file_acl); |
| 146 | t->i_dir_acl = ext2fs_swab32(f->i_dir_acl); |
| 147 | if (!islnk || ext2fs_inode_data_blocks(fs, (struct ext2_inode *)t)) { |
| 148 | for (i = 0; i < EXT2_N_BLOCKS; i++) |
| 149 | t->i_block[i] = ext2fs_swab32(f->i_block[i]); |
| 150 | } else if (t != f) { |
| 151 | for (i = 0; i < EXT2_N_BLOCKS; i++) |
| 152 | t->i_block[i] = f->i_block[i]; |
| 153 | } |
| 154 | t->i_generation = ext2fs_swab32(f->i_generation); |
| 155 | t->i_faddr = ext2fs_swab32(f->i_faddr); |
| 156 | |
| 157 | switch (fs->super->s_creator_os) { |
| 158 | case EXT2_OS_LINUX: |
| 159 | t->osd1.linux1.l_i_reserved1 = |
| 160 | ext2fs_swab32(f->osd1.linux1.l_i_reserved1); |
| 161 | t->osd2.linux2.l_i_frag = f->osd2.linux2.l_i_frag; |
| 162 | t->osd2.linux2.l_i_fsize = f->osd2.linux2.l_i_fsize; |
| 163 | t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1); |
| 164 | t->osd2.linux2.l_i_uid_high = |
| 165 | ext2fs_swab16 (f->osd2.linux2.l_i_uid_high); |
| 166 | t->osd2.linux2.l_i_gid_high = |
| 167 | ext2fs_swab16 (f->osd2.linux2.l_i_gid_high); |
| 168 | t->osd2.linux2.l_i_reserved2 = |
| 169 | ext2fs_swab32(f->osd2.linux2.l_i_reserved2); |
| 170 | break; |
| 171 | case EXT2_OS_HURD: |
| 172 | t->osd1.hurd1.h_i_translator = |
| 173 | ext2fs_swab32 (f->osd1.hurd1.h_i_translator); |
| 174 | t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag; |
| 175 | t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize; |
| 176 | t->osd2.hurd2.h_i_mode_high = |
| 177 | ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high); |
| 178 | t->osd2.hurd2.h_i_uid_high = |
| 179 | ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high); |
| 180 | t->osd2.hurd2.h_i_gid_high = |
| 181 | ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high); |
| 182 | t->osd2.hurd2.h_i_author = |
| 183 | ext2fs_swab32 (f->osd2.hurd2.h_i_author); |
| 184 | break; |
| 185 | case EXT2_OS_MASIX: |
| 186 | t->osd1.masix1.m_i_reserved1 = |
| 187 | ext2fs_swab32(f->osd1.masix1.m_i_reserved1); |
| 188 | t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag; |
| 189 | t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize; |
| 190 | t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1); |
| 191 | t->osd2.masix2.m_i_reserved2[0] = |
| 192 | ext2fs_swab32(f->osd2.masix2.m_i_reserved2[0]); |
| 193 | t->osd2.masix2.m_i_reserved2[1] = |
| 194 | ext2fs_swab32(f->osd2.masix2.m_i_reserved2[1]); |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | if (bufsize < (int) (sizeof(struct ext2_inode) + sizeof(__u16))) |
| 199 | return; /* no i_extra_isize field */ |
| 200 | |
| 201 | t->i_extra_isize = ext2fs_swab16(f->i_extra_isize); |
| 202 | if (t->i_extra_isize > EXT2_INODE_SIZE(fs->super) - |
| 203 | sizeof(struct ext2_inode)) { |
| 204 | /* this is error case: i_extra_size is too large */ |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | i = sizeof(struct ext2_inode) + t->i_extra_isize + sizeof(__u32); |
| 209 | if (bufsize < (int) i) |
| 210 | return; /* no space for EA magic */ |
| 211 | |
| 212 | eaf = (__u32 *) (((char *) f) + sizeof(struct ext2_inode) + |
| 213 | f->i_extra_isize); |
| 214 | |
| 215 | if (ext2fs_swab32(*eaf) != EXT2_EXT_ATTR_MAGIC) |
| 216 | return; /* it seems no magic here */ |
| 217 | |
| 218 | eat = (__u32 *) (((char *) t) + sizeof(struct ext2_inode) + |
| 219 | f->i_extra_isize); |
| 220 | *eat = ext2fs_swab32(*eaf); |
| 221 | |
| 222 | /* convert EA(s) */ |
| 223 | ext2fs_swap_ext_attr((char *) (eat + 1), (char *) (eaf + 1), |
| 224 | bufsize - sizeof(struct ext2_inode) - |
| 225 | t->i_extra_isize - sizeof(__u32), 0); |
| 226 | |
| 227 | } |
| 228 | |
| 229 | void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t, |
| 230 | struct ext2_inode *f, int hostorder) |
| 231 | { |
| 232 | ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) t, |
| 233 | (struct ext2_inode_large *) f, hostorder, |
| 234 | sizeof(struct ext2_inode)); |
| 235 | } |
| 236 | |
| 237 | #endif |