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"
21 #include <ext2fs/ext2_ext_attr.h>
22
23 #ifdef EXT2FS_ENABLE_SWAPFS
ext2fs_swap_super(struct ext2_super_block * sb)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 for (i=0; i < 4; i++)
74 sb->s_hash_seed[i] = ext2fs_swab32(sb->s_hash_seed[i]);
75 for (i=0; i < 17; i++)
76 sb->s_jnl_blocks[i] = ext2fs_swab32(sb->s_jnl_blocks[i]);
77
78 }
79
ext2fs_swap_group_desc(struct ext2_group_desc * gdp)80 void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
81 {
82 gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
83 gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
84 gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
85 gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
86 gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
87 gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
88 gdp->bg_flags = ext2fs_swab16(gdp->bg_flags);
89 gdp->bg_itable_unused = ext2fs_swab16(gdp->bg_itable_unused);
90 gdp->bg_checksum = ext2fs_swab16(gdp->bg_checksum);
91 }
92
ext2fs_swap_ext_attr(char * to,char * from,int bufsize,int has_header)93 void ext2fs_swap_ext_attr(char *to, char *from, int bufsize, int has_header)
94 {
95 struct ext2_ext_attr_header *from_header =
96 (struct ext2_ext_attr_header *)from;
97 struct ext2_ext_attr_header *to_header =
98 (struct ext2_ext_attr_header *)to;
99 struct ext2_ext_attr_entry *from_entry, *to_entry;
100 char *from_end = (char *)from_header + bufsize;
101 int n;
102
103 if (to_header != from_header)
104 memcpy(to_header, from_header, bufsize);
105
106 from_entry = (struct ext2_ext_attr_entry *)from_header;
107 to_entry = (struct ext2_ext_attr_entry *)to_header;
108
109 if (has_header) {
110 to_header->h_magic = ext2fs_swab32(from_header->h_magic);
111 to_header->h_blocks = ext2fs_swab32(from_header->h_blocks);
112 to_header->h_refcount = ext2fs_swab32(from_header->h_refcount);
113 for (n=0; n<4; n++)
114 to_header->h_reserved[n] =
115 ext2fs_swab32(from_header->h_reserved[n]);
116 from_entry = (struct ext2_ext_attr_entry *)(from_header+1);
117 to_entry = (struct ext2_ext_attr_entry *)(to_header+1);
118 }
119
120 while ((char *)from_entry < from_end && *(__u32 *)from_entry) {
121 to_entry->e_value_offs =
122 ext2fs_swab16(from_entry->e_value_offs);
123 to_entry->e_value_block =
124 ext2fs_swab32(from_entry->e_value_block);
125 to_entry->e_value_size =
126 ext2fs_swab32(from_entry->e_value_size);
127 from_entry = EXT2_EXT_ATTR_NEXT(from_entry);
128 to_entry = EXT2_EXT_ATTR_NEXT(to_entry);
129 }
130 }
131
ext2fs_swap_inode_full(ext2_filsys fs,struct ext2_inode_large * t,struct ext2_inode_large * f,int hostorder,int bufsize)132 void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t,
133 struct ext2_inode_large *f, int hostorder,
134 int bufsize)
135 {
136 unsigned i, has_data_blocks, extra_isize;
137 int islnk = 0;
138 __u32 *eaf, *eat;
139
140 if (hostorder && LINUX_S_ISLNK(f->i_mode))
141 islnk = 1;
142 t->i_mode = ext2fs_swab16(f->i_mode);
143 if (!hostorder && LINUX_S_ISLNK(t->i_mode))
144 islnk = 1;
145 t->i_uid = ext2fs_swab16(f->i_uid);
146 t->i_size = ext2fs_swab32(f->i_size);
147 t->i_atime = ext2fs_swab32(f->i_atime);
148 t->i_ctime = ext2fs_swab32(f->i_ctime);
149 t->i_mtime = ext2fs_swab32(f->i_mtime);
150 t->i_dtime = ext2fs_swab32(f->i_dtime);
151 t->i_gid = ext2fs_swab16(f->i_gid);
152 t->i_links_count = ext2fs_swab16(f->i_links_count);
153 t->i_file_acl = ext2fs_swab32(f->i_file_acl);
154 if (hostorder)
155 has_data_blocks = ext2fs_inode_data_blocks(fs,
156 (struct ext2_inode *) f);
157 t->i_blocks = ext2fs_swab32(f->i_blocks);
158 if (!hostorder)
159 has_data_blocks = ext2fs_inode_data_blocks(fs,
160 (struct ext2_inode *) t);
161 t->i_flags = ext2fs_swab32(f->i_flags);
162 t->i_dir_acl = ext2fs_swab32(f->i_dir_acl);
163 if (!islnk || has_data_blocks ) {
164 for (i = 0; i < EXT2_N_BLOCKS; i++)
165 t->i_block[i] = ext2fs_swab32(f->i_block[i]);
166 } else if (t != f) {
167 for (i = 0; i < EXT2_N_BLOCKS; i++)
168 t->i_block[i] = f->i_block[i];
169 }
170 t->i_generation = ext2fs_swab32(f->i_generation);
171 t->i_faddr = ext2fs_swab32(f->i_faddr);
172
173 switch (fs->super->s_creator_os) {
174 case EXT2_OS_LINUX:
175 t->osd1.linux1.l_i_version =
176 ext2fs_swab32(f->osd1.linux1.l_i_version);
177 t->osd2.linux2.l_i_blocks_hi =
178 ext2fs_swab16(f->osd2.linux2.l_i_blocks_hi);
179 t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1);
180 t->osd2.linux2.l_i_uid_high =
181 ext2fs_swab16 (f->osd2.linux2.l_i_uid_high);
182 t->osd2.linux2.l_i_gid_high =
183 ext2fs_swab16 (f->osd2.linux2.l_i_gid_high);
184 t->osd2.linux2.l_i_reserved2 =
185 ext2fs_swab32(f->osd2.linux2.l_i_reserved2);
186 break;
187 case EXT2_OS_HURD:
188 t->osd1.hurd1.h_i_translator =
189 ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
190 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
191 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
192 t->osd2.hurd2.h_i_mode_high =
193 ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
194 t->osd2.hurd2.h_i_uid_high =
195 ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
196 t->osd2.hurd2.h_i_gid_high =
197 ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
198 t->osd2.hurd2.h_i_author =
199 ext2fs_swab32 (f->osd2.hurd2.h_i_author);
200 break;
201 case EXT2_OS_MASIX:
202 t->osd1.masix1.m_i_reserved1 =
203 ext2fs_swab32(f->osd1.masix1.m_i_reserved1);
204 t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag;
205 t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize;
206 t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1);
207 t->osd2.masix2.m_i_reserved2[0] =
208 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[0]);
209 t->osd2.masix2.m_i_reserved2[1] =
210 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[1]);
211 break;
212 }
213
214 if (bufsize < (int) (sizeof(struct ext2_inode) + sizeof(__u16)))
215 return; /* no i_extra_isize field */
216
217 if (hostorder)
218 extra_isize = f->i_extra_isize;
219 t->i_extra_isize = ext2fs_swab16(f->i_extra_isize);
220 if (!hostorder)
221 extra_isize = t->i_extra_isize;
222 if (extra_isize > EXT2_INODE_SIZE(fs->super) -
223 sizeof(struct ext2_inode)) {
224 /* this is error case: i_extra_size is too large */
225 return;
226 }
227
228 i = sizeof(struct ext2_inode) + extra_isize + sizeof(__u32);
229 if (bufsize < (int) i)
230 return; /* no space for EA magic */
231
232 eaf = (__u32 *) (((char *) f) + sizeof(struct ext2_inode) +
233 extra_isize);
234
235 if (ext2fs_swab32(*eaf) != EXT2_EXT_ATTR_MAGIC)
236 return; /* it seems no magic here */
237
238 eat = (__u32 *) (((char *) t) + sizeof(struct ext2_inode) +
239 extra_isize);
240 *eat = ext2fs_swab32(*eaf);
241
242 /* convert EA(s) */
243 ext2fs_swap_ext_attr((char *) (eat + 1), (char *) (eaf + 1),
244 bufsize - sizeof(struct ext2_inode) -
245 extra_isize - sizeof(__u32), 0);
246
247 }
248
ext2fs_swap_inode(ext2_filsys fs,struct ext2_inode * t,struct ext2_inode * f,int hostorder)249 void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
250 struct ext2_inode *f, int hostorder)
251 {
252 ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) t,
253 (struct ext2_inode_large *) f, hostorder,
254 sizeof(struct ext2_inode));
255 }
256
257 #endif
258