~ubuntu-branches/ubuntu/edgy/e2fsprogs/edgy-updates

« back to all changes in this revision

Viewing changes to debugfs/setsuper.c

  • Committer: Bazaar Package Importer
  • Author(s): Tollef Fog Heen
  • Date: 2005-08-23 10:42:10 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050823104210-t15igvmgrkzea0dq
Tags: 1.38-2ubuntu1
* Merge with Debian.  (Ubuntu #13757)
* Remove tests/f_bad_disconnected_inode/image.gz to be able to build the
  package.  This will (hopefully) be in the next upstream version and is
  just used for testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * setsuper.c --- set a superblock value
3
 
 */
4
 
 
5
 
#include <stdio.h>
6
 
#include <unistd.h>
7
 
#include <stdlib.h>
8
 
#include <ctype.h>
9
 
#include <string.h>
10
 
#include <time.h>
11
 
#include <sys/types.h>
12
 
#include <sys/stat.h>
13
 
#ifdef HAVE_ERRNO_H
14
 
#include <errno.h>
15
 
#endif
16
 
#include <fcntl.h>
17
 
#include <utime.h>
18
 
 
19
 
#include "debugfs.h"
20
 
#include "uuid/uuid.h"
21
 
#include "e2p/e2p.h"
22
 
 
23
 
static struct ext2_super_block set_sb;
24
 
 
25
 
struct super_set_info {
26
 
        const char      *name;
27
 
        void    *ptr;
28
 
        unsigned int    size;
29
 
        errcode_t (*func)(struct super_set_info *info, char *arg);
30
 
};
31
 
 
32
 
static errcode_t parse_uint(struct super_set_info *info, char *arg);
33
 
static errcode_t parse_int(struct super_set_info *info, char *arg);
34
 
static errcode_t parse_string(struct super_set_info *info, char *arg);
35
 
static errcode_t parse_uuid(struct super_set_info *info, char *arg);
36
 
static errcode_t parse_hashalg(struct super_set_info *info, char *arg);
37
 
 
38
 
static struct super_set_info super_fields[] = {
39
 
        { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint },
40
 
        { "blocks_count", &set_sb.s_blocks_count, 4, parse_uint },
41
 
        { "r_blocks_count", &set_sb.s_r_blocks_count, 4, parse_uint },
42
 
        { "free_blocks_count", &set_sb.s_free_blocks_count, 4, parse_uint },
43
 
        { "free_inodes_count", &set_sb.s_free_inodes_count, 4, parse_uint },
44
 
        { "first_data_block", &set_sb.s_first_data_block, 4, parse_uint },
45
 
        { "log_block_size", &set_sb.s_log_block_size, 4, parse_uint },
46
 
        { "log_frag_size", &set_sb.s_log_frag_size, 4, parse_int },
47
 
        { "blocks_per_group", &set_sb.s_blocks_per_group, 4, parse_uint },
48
 
        { "frags_per_group", &set_sb.s_frags_per_group, 4, parse_uint },
49
 
        { "inodes_per_group", &set_sb.s_inodes_per_group, 4, parse_uint },
50
 
        /* s_mtime (time_t) */
51
 
        /* s_wtime (time_t) */
52
 
        { "mnt_count", &set_sb.s_mnt_count, 2, parse_uint },
53
 
        { "max_mnt_count", &set_sb.s_max_mnt_count, 2, parse_int },
54
 
        /* s_magic */
55
 
        { "state", &set_sb.s_state, 2, parse_uint },
56
 
        { "errors", &set_sb.s_errors, 2, parse_uint },
57
 
        { "minor_rev_level", &set_sb.s_minor_rev_level, 2, parse_uint },
58
 
        /* s_lastcheck (time_t) */
59
 
        { "checkinterval", &set_sb.s_checkinterval, 4, parse_uint },
60
 
        { "creator_os", &set_sb.s_creator_os, 4, parse_uint },
61
 
        { "rev_level", &set_sb.s_rev_level, 4, parse_uint },
62
 
        { "def_resuid", &set_sb.s_def_resuid, 2, parse_uint },
63
 
        { "def_resgid", &set_sb.s_def_resgid, 2, parse_uint },
64
 
        { "first_ino", &set_sb.s_first_ino, 4, parse_uint },
65
 
        { "inode_size", &set_sb.s_inode_size, 2, parse_uint },
66
 
        { "block_group_nr", &set_sb.s_block_group_nr, 2, parse_uint },
67
 
        { "feature_compat", &set_sb.s_feature_compat, 4, parse_uint },
68
 
        { "feature_incompat", &set_sb.s_feature_incompat, 4, parse_uint },
69
 
        { "feature_ro_compat", &set_sb.s_feature_ro_compat, 4, parse_uint }, 
70
 
        { "uuid", &set_sb.s_uuid, 16, parse_uuid },
71
 
        { "volume_name",  &set_sb.s_volume_name, 16, parse_string },
72
 
        { "last_mounted",  &set_sb.s_last_mounted, 64, parse_string },
73
 
        { "lastcheck",  &set_sb.s_lastcheck, 4, parse_uint },
74
 
        { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, 
75
 
                  4, parse_uint },
76
 
        { "prealloc_blocks", &set_sb.s_prealloc_blocks, 1, parse_uint },
77
 
        { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, 1,
78
 
                  parse_uint },
79
 
        /* s_padding1 */
80
 
        { "journal_uuid", &set_sb.s_journal_uuid, 16, parse_uuid },
81
 
        { "journal_inum", &set_sb.s_journal_inum, 4, parse_uint },
82
 
        { "journal_dev", &set_sb.s_journal_dev, 4, parse_uint },
83
 
        { "last_orphan", &set_sb.s_last_orphan, 4, parse_uint },
84
 
        { "hash_seed", &set_sb.s_hash_seed, 16, parse_uuid },
85
 
        { "def_hash_version", &set_sb.s_def_hash_version, 1, parse_hashalg },
86
 
        { 0, 0, 0, 0 }
87
 
};
88
 
 
89
 
static struct super_set_info *find_field(char *field)
90
 
{
91
 
        struct super_set_info *ss;
92
 
 
93
 
        if (strncmp(field, "s_", 2) == 0)
94
 
                field += 2;
95
 
        for (ss = super_fields ; ss->name ; ss++) {
96
 
                if (strcmp(ss->name, field) == 0)
97
 
                        return ss;
98
 
        }
99
 
        return NULL;
100
 
}
101
 
 
102
 
static errcode_t parse_uint(struct super_set_info *info, char *arg)
103
 
{
104
 
        unsigned long   num;
105
 
        char *tmp;
106
 
        __u32   *ptr32;
107
 
        __u16   *ptr16;
108
 
        __u8    *ptr8;
109
 
 
110
 
        num = strtoul(arg, &tmp, 0);
111
 
        if (*tmp) {
112
 
                fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
113
 
                        arg, info->name);
114
 
                return EINVAL;
115
 
        }
116
 
        switch (info->size) {
117
 
        case 4:
118
 
                ptr32 = (__u32 *) info->ptr;
119
 
                *ptr32 = num;
120
 
                break;
121
 
        case 2:
122
 
                ptr16 = (__u16 *) info->ptr;
123
 
                *ptr16 = num;
124
 
                break;
125
 
        case 1:
126
 
                ptr8 = (__u8 *) info->ptr;
127
 
                *ptr8 = num;
128
 
                break;
129
 
        }
130
 
        return 0;
131
 
}
132
 
 
133
 
static errcode_t parse_int(struct super_set_info *info, char *arg)
134
 
{
135
 
        long    num;
136
 
        char *tmp;
137
 
        __s32   *ptr32;
138
 
        __s16   *ptr16;
139
 
        __s8    *ptr8;
140
 
 
141
 
        num = strtol(arg, &tmp, 0);
142
 
        if (*tmp) {
143
 
                fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
144
 
                        arg, info->name);
145
 
                return EINVAL;
146
 
        }
147
 
        switch (info->size) {
148
 
        case 4:
149
 
                ptr32 = (__s32 *) info->ptr;
150
 
                *ptr32 = num;
151
 
                break;
152
 
        case 2:
153
 
                ptr16 = (__s16 *) info->ptr;
154
 
                *ptr16 = num;
155
 
                break;
156
 
        case 1:
157
 
                ptr8 = (__s8 *) info->ptr;
158
 
                *ptr8 = num;
159
 
                break;
160
 
        }
161
 
        return 0;
162
 
}
163
 
 
164
 
static errcode_t parse_string(struct super_set_info *info, char *arg)
165
 
{
166
 
        char    *cp = (char *) info->ptr;
167
 
 
168
 
        if (strlen(arg) >= info->size) {
169
 
                fprintf(stderr, "Error maximum size for %s is %d.\n",
170
 
                        info->name, info->size);
171
 
                return EINVAL;
172
 
        }
173
 
        strcpy(cp, arg);
174
 
        return 0;
175
 
}
176
 
 
177
 
static errcode_t parse_uuid(struct super_set_info *info, char *arg)
178
 
{
179
 
        unsigned char * p = (unsigned char *) info->ptr;
180
 
        
181
 
        if ((strcasecmp(arg, "null") == 0) ||
182
 
            (strcasecmp(arg, "clear") == 0)) {
183
 
                uuid_clear(p);
184
 
        } else if (strcasecmp(arg, "time") == 0) {
185
 
                uuid_generate_time(p);
186
 
        } else if (strcasecmp(arg, "random") == 0) {
187
 
                uuid_generate(p);
188
 
        } else if (uuid_parse(arg, p)) {
189
 
                fprintf(stderr, "Invalid UUID format: %s\n", arg);
190
 
                return EINVAL;
191
 
        }
192
 
        return 0;
193
 
}
194
 
 
195
 
static errcode_t parse_hashalg(struct super_set_info *info, char *arg)
196
 
{
197
 
        int     hashv;
198
 
        unsigned char   *p = (unsigned char *) info->ptr;
199
 
 
200
 
        hashv = e2p_string2hash(arg);
201
 
        if (hashv < 0) {
202
 
                fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
203
 
                return EINVAL;
204
 
        }
205
 
        *p = hashv;
206
 
        return 0;
207
 
}
208
 
 
209
 
 
210
 
static void print_possible_fields(void)
211
 
{
212
 
        struct super_set_info *ss;
213
 
        const char      *type;
214
 
 
215
 
        printf("Superblock fields supported by the set_super_value command:\n");
216
 
        for (ss = super_fields ; ss->name ; ss++) {
217
 
                type = "unknown";
218
 
                if (ss->func == parse_string)
219
 
                        type = "string";
220
 
                else if (ss->func == parse_int)
221
 
                        type = "integer";
222
 
                else if (ss->func == parse_uint)
223
 
                        type = "unsigned integer";
224
 
                else if (ss->func == parse_uuid)
225
 
                        type = "UUID";
226
 
                else if (ss->func == parse_hashalg)
227
 
                        type = "hash algorithm";
228
 
                printf("\t%-20s\t%s\n", ss->name, type);
229
 
        }
230
 
}
231
 
 
232
 
 
233
 
void do_set_super(int argc, char *argv[])
234
 
{
235
 
        const char *usage = "<field> <value>\n"
236
 
                "\t\"set_super_value -l\" will list the names of "
237
 
                "superblock fields\n\twhich can be set.";
238
 
        static struct super_set_info *ss;
239
 
        
240
 
        if ((argc == 2) && !strcmp(argv[1], "-l")) {
241
 
                print_possible_fields();
242
 
                return;
243
 
        }
244
 
 
245
 
        if (common_args_process(argc, argv, 3, 3, "set_super_value",
246
 
                                usage, CHECK_FS_RW))
247
 
                return;
248
 
 
249
 
        if ((ss = find_field(argv[1])) == 0) {
250
 
                com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
251
 
                return;
252
 
        }
253
 
        set_sb = *current_fs->super;
254
 
        if (ss->func(ss, argv[2]) == 0) {
255
 
                *current_fs->super = set_sb;
256
 
                ext2fs_mark_super_dirty(current_fs);
257
 
        }
258
 
}