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 Library 8 * General Public License, version 2. 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" 21 #include <ext2fs/ext2_ext_attr.h> 22 23 #ifdef WORDS_BIGENDIAN 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_desc_size = ext2fs_swab16(sb->s_desc_size); 64 sb->s_default_mount_opts = ext2fs_swab32(sb->s_default_mount_opts); 65 sb->s_first_meta_bg = ext2fs_swab32(sb->s_first_meta_bg); 66 sb->s_mkfs_time = ext2fs_swab32(sb->s_mkfs_time); 67 sb->s_blocks_count_hi = ext2fs_swab32(sb->s_blocks_count_hi); 68 sb->s_r_blocks_count_hi = ext2fs_swab32(sb->s_r_blocks_count_hi); 69 sb->s_free_blocks_hi = ext2fs_swab32(sb->s_free_blocks_hi); 70 sb->s_min_extra_isize = ext2fs_swab16(sb->s_min_extra_isize); 71 sb->s_want_extra_isize = ext2fs_swab16(sb->s_want_extra_isize); 72 sb->s_flags = ext2fs_swab32(sb->s_flags); 73 sb->s_kbytes_written = ext2fs_swab64(sb->s_kbytes_written); 74 sb->s_snapshot_inum = ext2fs_swab32(sb->s_snapshot_inum); 75 sb->s_snapshot_id = ext2fs_swab32(sb->s_snapshot_id); 76 sb->s_snapshot_r_blocks_count = 77 ext2fs_swab64(sb->s_snapshot_r_blocks_count); 78 sb->s_snapshot_list = ext2fs_swab32(sb->s_snapshot_list); 79 80 for (i=0; i < 4; i++) 81 sb->s_hash_seed[i] = ext2fs_swab32(sb->s_hash_seed[i]); 82 83 /* if journal backup is for a valid extent-based journal... */ 84 if (!ext2fs_extent_header_verify(sb->s_jnl_blocks, 85 sizeof(sb->s_jnl_blocks))) { 86 /* ... swap only the journal i_size */ 87 sb->s_jnl_blocks[16] = ext2fs_swab32(sb->s_jnl_blocks[16]); 88 /* and the extent data is not swapped on read */ 89 return; 90 } 91 92 /* direct/indirect journal: swap it all */ 93 for (i=0; i < 17; i++) 94 sb->s_jnl_blocks[i] = ext2fs_swab32(sb->s_jnl_blocks[i]); 95 } 96 97 void ext2fs_swap_group_desc(struct ext2_group_desc *gdp) 98 { 99 gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap); 100 gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap); 101 gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table); 102 gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count); 103 gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count); 104 gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count); 105 gdp->bg_flags = ext2fs_swab16(gdp->bg_flags); 106 gdp->bg_itable_unused = ext2fs_swab16(gdp->bg_itable_unused); 107 gdp->bg_checksum = ext2fs_swab16(gdp->bg_checksum); 108 } 109 110 void ext2fs_swap_ext_attr_header(struct ext2_ext_attr_header *to_header, 111 struct ext2_ext_attr_header *from_header) 112 { 113 int n; 114 115 to_header->h_magic = ext2fs_swab32(from_header->h_magic); 116 to_header->h_blocks = ext2fs_swab32(from_header->h_blocks); 117 to_header->h_refcount = ext2fs_swab32(from_header->h_refcount); 118 to_header->h_hash = ext2fs_swab32(from_header->h_hash); 119 for (n = 0; n < 4; n++) 120 to_header->h_reserved[n] = 121 ext2fs_swab32(from_header->h_reserved[n]); 122 } 123 124 void ext2fs_swap_ext_attr_entry(struct ext2_ext_attr_entry *to_entry, 125 struct ext2_ext_attr_entry *from_entry) 126 { 127 to_entry->e_value_offs = ext2fs_swab16(from_entry->e_value_offs); 128 to_entry->e_value_block = ext2fs_swab32(from_entry->e_value_block); 129 to_entry->e_value_size = ext2fs_swab32(from_entry->e_value_size); 130 to_entry->e_hash = ext2fs_swab32(from_entry->e_hash); 131 } 132 133 void ext2fs_swap_ext_attr(char *to, char *from, int bufsize, int has_header) 134 { 135 struct ext2_ext_attr_header *from_header = 136 (struct ext2_ext_attr_header *)from; 137 struct ext2_ext_attr_header *to_header = 138 (struct ext2_ext_attr_header *)to; 139 struct ext2_ext_attr_entry *from_entry, *to_entry; 140 char *from_end = (char *)from_header + bufsize; 141 142 if (to_header != from_header) 143 memcpy(to_header, from_header, bufsize); 144 145 if (has_header) { 146 ext2fs_swap_ext_attr_header(to_header, from_header); 147 148 from_entry = (struct ext2_ext_attr_entry *)(from_header+1); 149 to_entry = (struct ext2_ext_attr_entry *)(to_header+1); 150 } else { 151 from_entry = (struct ext2_ext_attr_entry *)from_header; 152 to_entry = (struct ext2_ext_attr_entry *)to_header; 153 } 154 155 while ((char *)from_entry < from_end && *(__u32 *)from_entry) { 156 ext2fs_swap_ext_attr_entry(to_entry, from_entry); 157 from_entry = EXT2_EXT_ATTR_NEXT(from_entry); 158 to_entry = EXT2_EXT_ATTR_NEXT(to_entry); 159 } 160 } 161 162 void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t, 163 struct ext2_inode_large *f, int hostorder, 164 int bufsize) 165 { 166 unsigned i, has_data_blocks, extra_isize, attr_magic; 167 int has_extents = 0; 168 int islnk = 0; 169 __u32 *eaf, *eat; 170 171 if (hostorder && LINUX_S_ISLNK(f->i_mode)) 172 islnk = 1; 173 t->i_mode = ext2fs_swab16(f->i_mode); 174 if (!hostorder && LINUX_S_ISLNK(t->i_mode)) 175 islnk = 1; 176 t->i_uid = ext2fs_swab16(f->i_uid); 177 t->i_size = ext2fs_swab32(f->i_size); 178 t->i_atime = ext2fs_swab32(f->i_atime); 179 t->i_ctime = ext2fs_swab32(f->i_ctime); 180 t->i_mtime = ext2fs_swab32(f->i_mtime); 181 t->i_dtime = ext2fs_swab32(f->i_dtime); 182 t->i_gid = ext2fs_swab16(f->i_gid); 183 t->i_links_count = ext2fs_swab16(f->i_links_count); 184 t->i_file_acl = ext2fs_swab32(f->i_file_acl); 185 if (hostorder) 186 has_data_blocks = ext2fs_inode_data_blocks(fs, 187 (struct ext2_inode *) f); 188 t->i_blocks = ext2fs_swab32(f->i_blocks); 189 if (!hostorder) 190 has_data_blocks = ext2fs_inode_data_blocks(fs, 191 (struct ext2_inode *) t); 192 if (hostorder && (f->i_flags & EXT4_EXTENTS_FL)) 193 has_extents = 1; 194 t->i_flags = ext2fs_swab32(f->i_flags); 195 if (!hostorder && (t->i_flags & EXT4_EXTENTS_FL)) 196 has_extents = 1; 197 t->i_dir_acl = ext2fs_swab32(f->i_dir_acl); 198 /* extent data are swapped on access, not here */ 199 if (!has_extents && (!islnk || has_data_blocks)) { 200 for (i = 0; i < EXT2_N_BLOCKS; i++) 201 t->i_block[i] = ext2fs_swab32(f->i_block[i]); 202 } else if (t != f) { 203 for (i = 0; i < EXT2_N_BLOCKS; i++) 204 t->i_block[i] = f->i_block[i]; 205 } 206 t->i_generation = ext2fs_swab32(f->i_generation); 207 t->i_faddr = ext2fs_swab32(f->i_faddr); 208 209 switch (fs->super->s_creator_os) { 210 case EXT2_OS_LINUX: 211 t->osd1.linux1.l_i_version = 212 ext2fs_swab32(f->osd1.linux1.l_i_version); 213 t->osd2.linux2.l_i_blocks_hi = 214 ext2fs_swab16(f->osd2.linux2.l_i_blocks_hi); 215 t->osd2.linux2.l_i_file_acl_high = 216 ext2fs_swab16(f->osd2.linux2.l_i_file_acl_high); 217 t->osd2.linux2.l_i_uid_high = 218 ext2fs_swab16 (f->osd2.linux2.l_i_uid_high); 219 t->osd2.linux2.l_i_gid_high = 220 ext2fs_swab16 (f->osd2.linux2.l_i_gid_high); 221 t->osd2.linux2.l_i_reserved2 = 222 ext2fs_swab32(f->osd2.linux2.l_i_reserved2); 223 break; 224 case EXT2_OS_HURD: 225 t->osd1.hurd1.h_i_translator = 226 ext2fs_swab32 (f->osd1.hurd1.h_i_translator); 227 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag; 228 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize; 229 t->osd2.hurd2.h_i_mode_high = 230 ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high); 231 t->osd2.hurd2.h_i_uid_high = 232 ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high); 233 t->osd2.hurd2.h_i_gid_high = 234 ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high); 235 t->osd2.hurd2.h_i_author = 236 ext2fs_swab32 (f->osd2.hurd2.h_i_author); 237 break; 238 default: 239 break; 240 } 241 242 if (bufsize < (int) (sizeof(struct ext2_inode) + sizeof(__u16))) 243 return; /* no i_extra_isize field */ 244 245 if (hostorder) 246 extra_isize = f->i_extra_isize; 247 t->i_extra_isize = ext2fs_swab16(f->i_extra_isize); 248 if (!hostorder) 249 extra_isize = t->i_extra_isize; 250 if (extra_isize > EXT2_INODE_SIZE(fs->super) - 251 sizeof(struct ext2_inode)) { 252 /* this is error case: i_extra_size is too large */ 253 return; 254 } 255 256 i = sizeof(struct ext2_inode) + extra_isize + sizeof(__u32); 257 if (bufsize < (int) i) 258 return; /* no space for EA magic */ 259 260 eaf = (__u32 *) (((char *) f) + sizeof(struct ext2_inode) + 261 extra_isize); 262 263 attr_magic = *eaf; 264 if (!hostorder) 265 attr_magic = ext2fs_swab32(attr_magic); 266 267 if (attr_magic != EXT2_EXT_ATTR_MAGIC) 268 return; /* it seems no magic here */ 269 270 eat = (__u32 *) (((char *) t) + sizeof(struct ext2_inode) + 271 extra_isize); 272 *eat = ext2fs_swab32(*eaf); 273 274 /* convert EA(s) */ 275 ext2fs_swap_ext_attr((char *) (eat + 1), (char *) (eaf + 1), 276 bufsize - sizeof(struct ext2_inode) - 277 extra_isize - sizeof(__u32), 0); 278 279 } 280 281 void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t, 282 struct ext2_inode *f, int hostorder) 283 { 284 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) t, 285 (struct ext2_inode_large *) f, hostorder, 286 sizeof(struct ext2_inode)); 287 } 288 289 #endif 290