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