blob: de347ac306ebf77ee64d00738eafc941ab451c07 [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
2 * swapfs.c --- swap ext2 filesystem data structures
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1995, 1996 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%
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010 */
11
12#include <stdio.h>
13#include <unistd.h>
14#include <stdlib.h>
15#include <time.h>
16
17#include <linux/ext2_fs.h>
18
19#include "ext2fs.h"
20
21void ext2fs_swap_super(struct ext2_super_block * super)
22{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000023 struct ext2fs_sb *s = (struct ext2fs_sb *) super;
24
Theodore Ts'o50e1e101997-04-26 13:58:21 +000025 super->s_inodes_count = ext2fs_swab32(super->s_inodes_count);
26 super->s_blocks_count = ext2fs_swab32(super->s_blocks_count);
27 super->s_r_blocks_count = ext2fs_swab32(super->s_r_blocks_count);
28 super->s_free_blocks_count = ext2fs_swab32(super->s_free_blocks_count);
29 super->s_free_inodes_count = ext2fs_swab32(super->s_free_inodes_count);
30 super->s_first_data_block = ext2fs_swab32(super->s_first_data_block);
31 super->s_log_block_size = ext2fs_swab32(super->s_log_block_size);
32 super->s_log_frag_size = ext2fs_swab32(super->s_log_frag_size);
33 super->s_blocks_per_group = ext2fs_swab32(super->s_blocks_per_group);
34 super->s_frags_per_group = ext2fs_swab32(super->s_frags_per_group);
35 super->s_inodes_per_group = ext2fs_swab32(super->s_inodes_per_group);
36 super->s_mtime = ext2fs_swab32(super->s_mtime);
37 super->s_wtime = ext2fs_swab32(super->s_wtime);
38 super->s_mnt_count = ext2fs_swab16(super->s_mnt_count);
39 super->s_max_mnt_count = ext2fs_swab16(super->s_max_mnt_count);
40 super->s_magic = ext2fs_swab16(super->s_magic);
41 super->s_state = ext2fs_swab16(super->s_state);
42 super->s_errors = ext2fs_swab16(super->s_errors);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000043 s->s_minor_rev_level = ext2fs_swab16(s->s_minor_rev_level);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000044 super->s_lastcheck = ext2fs_swab32(super->s_lastcheck);
45 super->s_checkinterval = ext2fs_swab32(super->s_checkinterval);
46 super->s_creator_os = ext2fs_swab32(super->s_creator_os);
47 super->s_rev_level = ext2fs_swab32(super->s_rev_level);
48#ifdef EXT2_DEF_RESUID
49 super->s_def_resuid = ext2fs_swab16(super->s_def_resuid);
50 super->s_def_resgid = ext2fs_swab16(super->s_def_resgid);
51#endif
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000052 s->s_first_ino = ext2fs_swab32(s->s_first_ino);
53 s->s_inode_size = ext2fs_swab16(s->s_inode_size);
54 s->s_block_group_nr = ext2fs_swab16(s->s_block_group_nr);
55 s->s_feature_compat = ext2fs_swab32(s->s_feature_compat);
56 s->s_feature_incompat = ext2fs_swab32(s->s_feature_incompat);
57 s->s_feature_ro_compat = ext2fs_swab32(s->s_feature_ro_compat);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000058}
59
60void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
61{
62 gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
63 gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
64 gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
65 gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
66 gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
67 gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
68}
69
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000070void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
71 struct ext2_inode *f, int hostorder)
72{
73 unsigned i;
74 int islnk = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000075
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000076 if (hostorder && LINUX_S_ISLNK(f->i_mode))
77 islnk = 1;
78 t->i_mode = ext2fs_swab16(f->i_mode);
79 if (!hostorder && LINUX_S_ISLNK(t->i_mode))
80 islnk = 1;
81 t->i_uid = ext2fs_swab16(f->i_uid);
82 t->i_size = ext2fs_swab32(f->i_size);
83 t->i_atime = ext2fs_swab32(f->i_atime);
84 t->i_ctime = ext2fs_swab32(f->i_ctime);
85 t->i_mtime = ext2fs_swab32(f->i_mtime);
86 t->i_dtime = ext2fs_swab32(f->i_dtime);
87 t->i_gid = ext2fs_swab16(f->i_gid);
88 t->i_links_count = ext2fs_swab16(f->i_links_count);
89 t->i_blocks = ext2fs_swab32(f->i_blocks);
90 t->i_flags = ext2fs_swab32(f->i_flags);
91 if (!islnk || f->i_blocks) {
92 for (i = 0; i < EXT2_N_BLOCKS; i++)
93 t->i_block[i] = ext2fs_swab32(f->i_block[i]);
94 } else if (t != f) {
95 for (i = 0; i < EXT2_N_BLOCKS; i++)
96 t->i_block[i] = f->i_block[i];
97 }
98 t->i_version = ext2fs_swab32(f->i_version);
99 t->i_file_acl = ext2fs_swab32(f->i_file_acl);
100 t->i_dir_acl = ext2fs_swab32(f->i_dir_acl);
101 t->i_faddr = ext2fs_swab32(f->i_faddr);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000102
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000103 switch (fs->super->s_creator_os) {
104 case EXT2_OS_LINUX:
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000105 t->osd1.linux1.l_i_reserved1 =
106 ext2fs_swab32(f->osd1.linux1.l_i_reserved1);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000107 t->osd2.linux2.l_i_frag = f->osd2.linux2.l_i_frag;
108 t->osd2.linux2.l_i_fsize = f->osd2.linux2.l_i_fsize;
109 t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000110 t->osd2.linux2.l_i_reserved2[0] =
111 ext2fs_swab32(f->osd2.linux2.l_i_reserved2[0]);
112 t->osd2.linux2.l_i_reserved2[1] =
113 ext2fs_swab32(f->osd2.linux2.l_i_reserved2[1]);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000114 break;
115 case EXT2_OS_HURD:
116 t->osd1.hurd1.h_i_translator =
117 ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
118 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
119 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
120 t->osd2.hurd2.h_i_mode_high =
121 ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
122 t->osd2.hurd2.h_i_uid_high =
123 ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
124 t->osd2.hurd2.h_i_gid_high =
125 ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
126 t->osd2.hurd2.h_i_author =
127 ext2fs_swab32 (f->osd2.hurd2.h_i_author);
128 break;
129 case EXT2_OS_MASIX:
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000130 t->osd1.masix1.m_i_reserved1 =
131 ext2fs_swab32(f->osd1.masix1.m_i_reserved1);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000132 t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag;
133 t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize;
134 t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000135 t->osd2.masix2.m_i_reserved2[0] =
136 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[0]);
137 t->osd2.masix2.m_i_reserved2[1] =
138 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[1]);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000139 break;
140 }
141}
142