blob: 968f41c23a4555bb52f547cafe4b986dc66445ae [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
2 * swapfs.c --- swap ext2 filesystem data structures
3 *
4 * Copyright (C) 1995 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8#include <stdio.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <time.h>
12
13#include <linux/ext2_fs.h>
14
15#include "ext2fs.h"
16
17void ext2fs_swap_super(struct ext2_super_block * super)
18{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000019 struct ext2fs_sb *s = (struct ext2fs_sb *) super;
20
Theodore Ts'o50e1e101997-04-26 13:58:21 +000021 super->s_inodes_count = ext2fs_swab32(super->s_inodes_count);
22 super->s_blocks_count = ext2fs_swab32(super->s_blocks_count);
23 super->s_r_blocks_count = ext2fs_swab32(super->s_r_blocks_count);
24 super->s_free_blocks_count = ext2fs_swab32(super->s_free_blocks_count);
25 super->s_free_inodes_count = ext2fs_swab32(super->s_free_inodes_count);
26 super->s_first_data_block = ext2fs_swab32(super->s_first_data_block);
27 super->s_log_block_size = ext2fs_swab32(super->s_log_block_size);
28 super->s_log_frag_size = ext2fs_swab32(super->s_log_frag_size);
29 super->s_blocks_per_group = ext2fs_swab32(super->s_blocks_per_group);
30 super->s_frags_per_group = ext2fs_swab32(super->s_frags_per_group);
31 super->s_inodes_per_group = ext2fs_swab32(super->s_inodes_per_group);
32 super->s_mtime = ext2fs_swab32(super->s_mtime);
33 super->s_wtime = ext2fs_swab32(super->s_wtime);
34 super->s_mnt_count = ext2fs_swab16(super->s_mnt_count);
35 super->s_max_mnt_count = ext2fs_swab16(super->s_max_mnt_count);
36 super->s_magic = ext2fs_swab16(super->s_magic);
37 super->s_state = ext2fs_swab16(super->s_state);
38 super->s_errors = ext2fs_swab16(super->s_errors);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000039 s->s_minor_rev_level = ext2fs_swab16(s->s_minor_rev_level);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000040 super->s_lastcheck = ext2fs_swab32(super->s_lastcheck);
41 super->s_checkinterval = ext2fs_swab32(super->s_checkinterval);
42 super->s_creator_os = ext2fs_swab32(super->s_creator_os);
43 super->s_rev_level = ext2fs_swab32(super->s_rev_level);
44#ifdef EXT2_DEF_RESUID
45 super->s_def_resuid = ext2fs_swab16(super->s_def_resuid);
46 super->s_def_resgid = ext2fs_swab16(super->s_def_resgid);
47#endif
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000048 s->s_first_ino = ext2fs_swab32(s->s_first_ino);
49 s->s_inode_size = ext2fs_swab16(s->s_inode_size);
50 s->s_block_group_nr = ext2fs_swab16(s->s_block_group_nr);
51 s->s_feature_compat = ext2fs_swab32(s->s_feature_compat);
52 s->s_feature_incompat = ext2fs_swab32(s->s_feature_incompat);
53 s->s_feature_ro_compat = ext2fs_swab32(s->s_feature_ro_compat);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000054}
55
56void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
57{
58 gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
59 gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
60 gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
61 gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
62 gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
63 gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
64}
65
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000066void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
67 struct ext2_inode *f, int hostorder)
68{
69 unsigned i;
70 int islnk = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000071
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000072 if (hostorder && LINUX_S_ISLNK(f->i_mode))
73 islnk = 1;
74 t->i_mode = ext2fs_swab16(f->i_mode);
75 if (!hostorder && LINUX_S_ISLNK(t->i_mode))
76 islnk = 1;
77 t->i_uid = ext2fs_swab16(f->i_uid);
78 t->i_size = ext2fs_swab32(f->i_size);
79 t->i_atime = ext2fs_swab32(f->i_atime);
80 t->i_ctime = ext2fs_swab32(f->i_ctime);
81 t->i_mtime = ext2fs_swab32(f->i_mtime);
82 t->i_dtime = ext2fs_swab32(f->i_dtime);
83 t->i_gid = ext2fs_swab16(f->i_gid);
84 t->i_links_count = ext2fs_swab16(f->i_links_count);
85 t->i_blocks = ext2fs_swab32(f->i_blocks);
86 t->i_flags = ext2fs_swab32(f->i_flags);
87 if (!islnk || f->i_blocks) {
88 for (i = 0; i < EXT2_N_BLOCKS; i++)
89 t->i_block[i] = ext2fs_swab32(f->i_block[i]);
90 } else if (t != f) {
91 for (i = 0; i < EXT2_N_BLOCKS; i++)
92 t->i_block[i] = f->i_block[i];
93 }
94 t->i_version = ext2fs_swab32(f->i_version);
95 t->i_file_acl = ext2fs_swab32(f->i_file_acl);
96 t->i_dir_acl = ext2fs_swab32(f->i_dir_acl);
97 t->i_faddr = ext2fs_swab32(f->i_faddr);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000098
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000099 switch (fs->super->s_creator_os) {
100 case EXT2_OS_LINUX:
101 t->osd2.linux2.l_i_frag = f->osd2.linux2.l_i_frag;
102 t->osd2.linux2.l_i_fsize = f->osd2.linux2.l_i_fsize;
103 t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1);
104 break;
105 case EXT2_OS_HURD:
106 t->osd1.hurd1.h_i_translator =
107 ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
108 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
109 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
110 t->osd2.hurd2.h_i_mode_high =
111 ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
112 t->osd2.hurd2.h_i_uid_high =
113 ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
114 t->osd2.hurd2.h_i_gid_high =
115 ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
116 t->osd2.hurd2.h_i_author =
117 ext2fs_swab32 (f->osd2.hurd2.h_i_author);
118 break;
119 case EXT2_OS_MASIX:
120 t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag;
121 t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize;
122 t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1);
123 break;
124 }
125}
126