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