Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / swapfs.c
1 /*
2  * swapfs.c --- swap ext2 filesystem data structures
3  * 
4  * 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%
10  */
11
12 #include <stdio.h>
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <time.h>
17
18 #include <linux/ext2_fs.h>
19
20 #include "ext2fs.h"
21
22 void ext2fs_swap_super(struct ext2_super_block * super)
23 {
24         struct ext2fs_sb *s = (struct ext2fs_sb *) super;
25         
26         super->s_inodes_count = ext2fs_swab32(super->s_inodes_count);
27         super->s_blocks_count = ext2fs_swab32(super->s_blocks_count);
28         super->s_r_blocks_count = ext2fs_swab32(super->s_r_blocks_count);
29         super->s_free_blocks_count = ext2fs_swab32(super->s_free_blocks_count);
30         super->s_free_inodes_count = ext2fs_swab32(super->s_free_inodes_count);
31         super->s_first_data_block = ext2fs_swab32(super->s_first_data_block);
32         super->s_log_block_size = ext2fs_swab32(super->s_log_block_size);
33         super->s_log_frag_size = ext2fs_swab32(super->s_log_frag_size);
34         super->s_blocks_per_group = ext2fs_swab32(super->s_blocks_per_group);
35         super->s_frags_per_group = ext2fs_swab32(super->s_frags_per_group);
36         super->s_inodes_per_group = ext2fs_swab32(super->s_inodes_per_group);
37         super->s_mtime = ext2fs_swab32(super->s_mtime);
38         super->s_wtime = ext2fs_swab32(super->s_wtime);
39         super->s_mnt_count = ext2fs_swab16(super->s_mnt_count);
40         super->s_max_mnt_count = ext2fs_swab16(super->s_max_mnt_count);
41         super->s_magic = ext2fs_swab16(super->s_magic);
42         super->s_state = ext2fs_swab16(super->s_state);
43         super->s_errors = ext2fs_swab16(super->s_errors);
44         s->s_minor_rev_level = ext2fs_swab16(s->s_minor_rev_level);
45         super->s_lastcheck = ext2fs_swab32(super->s_lastcheck);
46         super->s_checkinterval = ext2fs_swab32(super->s_checkinterval);
47         super->s_creator_os = ext2fs_swab32(super->s_creator_os);
48         super->s_rev_level = ext2fs_swab32(super->s_rev_level);
49 #ifdef  EXT2_DEF_RESUID
50         super->s_def_resuid = ext2fs_swab16(super->s_def_resuid);
51         super->s_def_resgid = ext2fs_swab16(super->s_def_resgid);
52 #endif
53         s->s_first_ino = ext2fs_swab32(s->s_first_ino);
54         s->s_inode_size = ext2fs_swab16(s->s_inode_size);
55         s->s_block_group_nr = ext2fs_swab16(s->s_block_group_nr);
56         s->s_feature_compat = ext2fs_swab32(s->s_feature_compat);
57         s->s_feature_incompat = ext2fs_swab32(s->s_feature_incompat);
58         s->s_feature_ro_compat = ext2fs_swab32(s->s_feature_ro_compat);
59 }
60
61 void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
62 {
63         gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
64         gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
65         gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
66         gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
67         gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
68         gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
69 }
70
71 void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
72                        struct ext2_inode *f, int hostorder)
73 {
74         unsigned i;
75         int islnk = 0;
76         
77         if (hostorder && LINUX_S_ISLNK(f->i_mode))
78                 islnk = 1;
79         t->i_mode = ext2fs_swab16(f->i_mode);
80         if (!hostorder && LINUX_S_ISLNK(t->i_mode))
81                 islnk = 1;
82         t->i_uid = ext2fs_swab16(f->i_uid);
83         t->i_size = ext2fs_swab32(f->i_size);
84         t->i_atime = ext2fs_swab32(f->i_atime);
85         t->i_ctime = ext2fs_swab32(f->i_ctime);
86         t->i_mtime = ext2fs_swab32(f->i_mtime);
87         t->i_dtime = ext2fs_swab32(f->i_dtime);
88         t->i_gid = ext2fs_swab16(f->i_gid);
89         t->i_links_count = ext2fs_swab16(f->i_links_count);
90         t->i_blocks = ext2fs_swab32(f->i_blocks);
91         t->i_flags = ext2fs_swab32(f->i_flags);
92         if (!islnk || f->i_blocks) {
93                 for (i = 0; i < EXT2_N_BLOCKS; i++)
94                         t->i_block[i] = ext2fs_swab32(f->i_block[i]);
95         } else if (t != f) {
96                 for (i = 0; i < EXT2_N_BLOCKS; i++)
97                         t->i_block[i] = f->i_block[i];
98         }
99         t->i_version = ext2fs_swab32(f->i_version);
100         t->i_file_acl = ext2fs_swab32(f->i_file_acl);
101         t->i_dir_acl = ext2fs_swab32(f->i_dir_acl);
102         t->i_faddr = ext2fs_swab32(f->i_faddr);
103
104         switch (fs->super->s_creator_os) {
105         case EXT2_OS_LINUX:
106                 t->osd1.linux1.l_i_reserved1 =
107                         ext2fs_swab32(f->osd1.linux1.l_i_reserved1);
108                 t->osd2.linux2.l_i_frag = f->osd2.linux2.l_i_frag;
109                 t->osd2.linux2.l_i_fsize = f->osd2.linux2.l_i_fsize;
110                 t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1);
111                 t->osd2.linux2.l_i_reserved2[0] =
112                         ext2fs_swab32(f->osd2.linux2.l_i_reserved2[0]);
113                 t->osd2.linux2.l_i_reserved2[1] =
114                         ext2fs_swab32(f->osd2.linux2.l_i_reserved2[1]);
115                 break;
116         case EXT2_OS_HURD:
117                 t->osd1.hurd1.h_i_translator =
118                   ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
119                 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
120                 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
121                 t->osd2.hurd2.h_i_mode_high =
122                   ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
123                 t->osd2.hurd2.h_i_uid_high =
124                   ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
125                 t->osd2.hurd2.h_i_gid_high =
126                   ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
127                 t->osd2.hurd2.h_i_author =
128                   ext2fs_swab32 (f->osd2.hurd2.h_i_author);
129                 break;
130         case EXT2_OS_MASIX:
131                 t->osd1.masix1.m_i_reserved1 =
132                         ext2fs_swab32(f->osd1.masix1.m_i_reserved1);
133                 t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag;
134                 t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize;
135                 t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1);
136                 t->osd2.masix2.m_i_reserved2[0] =
137                         ext2fs_swab32(f->osd2.masix2.m_i_reserved2[0]);
138                 t->osd2.masix2.m_i_reserved2[1] =
139                         ext2fs_swab32(f->osd2.masix2.m_i_reserved2[1]);
140                 break;
141         }
142 }
143