blob: 08bfd8da29839e4f3dbe83543fc46befe5212cd5 [file] [log] [blame]
Theodore Ts'o7dec0502004-12-21 20:37:36 -05001/*
2 * set_fields.c --- set a superblock value
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o7dec0502004-12-21 20:37:36 -05004 * Copyright (C) 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o7dec0502004-12-21 20:37:36 -05006 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
Theodore Ts'o007df862008-02-18 22:16:52 -050012#define _XOPEN_SOURCE 600 /* for inclusion of strptime() and strtoull */
13
14#ifdef HAVE_STRTOULL
15#define STRTOULL strtoull
16#else
17#define STRTOULL strtoul
18#endif
Theodore Ts'o619a8ea2005-01-19 13:26:38 -050019
Theodore Ts'od1154eb2011-09-18 17:34:37 -040020#include "config.h"
Theodore Ts'o7dec0502004-12-21 20:37:36 -050021#include <stdio.h>
22#include <unistd.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include <string.h>
Theodore Ts'o8192c632008-01-01 10:27:38 -050026#include <strings.h>
Theodore Ts'o7dec0502004-12-21 20:37:36 -050027#include <time.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
Theodore Ts'oa98b51e2007-08-03 20:10:46 -040033#if HAVE_STRINGS_H
34#include <strings.h>
35#endif
Theodore Ts'o7dec0502004-12-21 20:37:36 -050036#include <fcntl.h>
37#include <utime.h>
38
39#include "debugfs.h"
40#include "uuid/uuid.h"
41#include "e2p/e2p.h"
42
43static struct ext2_super_block set_sb;
Theodore Ts'o16c581d2011-09-15 13:38:41 -040044static struct ext2_inode_large set_inode;
Theodore Ts'oc7c12092007-04-13 15:56:00 -040045static struct ext2_group_desc set_gd;
Theodore Ts'o16c581d2011-09-15 13:38:41 -040046static struct ext4_group_desc set_gd4;
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -050047static struct mmp_struct set_mmp;
Andreas Dilgercf96ecf2008-03-30 20:05:12 -040048static dgrp_t set_bg;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -050049static ext2_ino_t set_ino;
50static int array_idx;
51
52#define FLAG_ARRAY 0x0001
Theodore Ts'o7dec0502004-12-21 20:37:36 -050053
54struct field_set_info {
55 const char *name;
56 void *ptr;
Theodore Ts'o16c581d2011-09-15 13:38:41 -040057 void *ptr2;
Theodore Ts'o7dec0502004-12-21 20:37:36 -050058 unsigned int size;
Theodore Ts'o16c581d2011-09-15 13:38:41 -040059 errcode_t (*func)(struct field_set_info *info, char *field, char *arg);
Theodore Ts'o08fd3f32004-12-23 14:11:34 -050060 int flags;
61 int max_idx;
Theodore Ts'o7dec0502004-12-21 20:37:36 -050062};
63
Theodore Ts'o16c581d2011-09-15 13:38:41 -040064static errcode_t parse_uint(struct field_set_info *info, char *field, char *arg);
65static errcode_t parse_int(struct field_set_info *info, char *field, char *arg);
66static errcode_t parse_string(struct field_set_info *info, char *field, char *arg);
67static errcode_t parse_uuid(struct field_set_info *info, char *field, char *arg);
68static errcode_t parse_hashalg(struct field_set_info *info, char *field, char *arg);
69static errcode_t parse_time(struct field_set_info *info, char *field, char *arg);
70static errcode_t parse_bmap(struct field_set_info *info, char *field, char *arg);
71static errcode_t parse_gd_csum(struct field_set_info *info, char *field, char *arg);
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -050072static errcode_t parse_mmp_clear(struct field_set_info *info, char *field,
73 char *arg);
Theodore Ts'o7dec0502004-12-21 20:37:36 -050074
75static struct field_set_info super_fields[] = {
Theodore Ts'o16c581d2011-09-15 13:38:41 -040076 { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
77 { "blocks_count", &set_sb.s_blocks_count, &set_sb.s_blocks_count_hi,
78 4, parse_uint },
79 { "r_blocks_count", &set_sb.s_r_blocks_count,
80 &set_sb.s_r_blocks_count_hi, 4, parse_uint },
81 { "free_blocks_count", &set_sb.s_free_blocks_count,
82 &set_sb.s_free_blocks_hi, 4, parse_uint },
83 { "free_inodes_count", &set_sb.s_free_inodes_count, NULL, 4, parse_uint },
84 { "first_data_block", &set_sb.s_first_data_block, NULL, 4, parse_uint },
85 { "log_block_size", &set_sb.s_log_block_size, NULL, 4, parse_uint },
86 { "log_cluster_size", &set_sb.s_log_cluster_size, NULL, 4, parse_int },
87 { "blocks_per_group", &set_sb.s_blocks_per_group, NULL, 4, parse_uint },
88 { "clusters_per_group", &set_sb.s_clusters_per_group, NULL, 4, parse_uint },
89 { "inodes_per_group", &set_sb.s_inodes_per_group, NULL, 4, parse_uint },
90 { "mtime", &set_sb.s_mtime, NULL, 4, parse_time },
91 { "wtime", &set_sb.s_wtime, NULL, 4, parse_time },
92 { "mnt_count", &set_sb.s_mnt_count, NULL, 2, parse_uint },
93 { "max_mnt_count", &set_sb.s_max_mnt_count, NULL, 2, parse_int },
Theodore Ts'o7dec0502004-12-21 20:37:36 -050094 /* s_magic */
Theodore Ts'o16c581d2011-09-15 13:38:41 -040095 { "state", &set_sb.s_state, NULL, 2, parse_uint },
96 { "errors", &set_sb.s_errors, NULL, 2, parse_uint },
97 { "minor_rev_level", &set_sb.s_minor_rev_level, NULL, 2, parse_uint },
98 { "lastcheck", &set_sb.s_lastcheck, NULL, 4, parse_time },
99 { "checkinterval", &set_sb.s_checkinterval, NULL, 4, parse_uint },
100 { "creator_os", &set_sb.s_creator_os, NULL, 4, parse_uint },
101 { "rev_level", &set_sb.s_rev_level, NULL, 4, parse_uint },
102 { "def_resuid", &set_sb.s_def_resuid, NULL, 2, parse_uint },
103 { "def_resgid", &set_sb.s_def_resgid, NULL, 2, parse_uint },
104 { "first_ino", &set_sb.s_first_ino, NULL, 4, parse_uint },
105 { "inode_size", &set_sb.s_inode_size, NULL, 2, parse_uint },
106 { "block_group_nr", &set_sb.s_block_group_nr, NULL, 2, parse_uint },
107 { "feature_compat", &set_sb.s_feature_compat, NULL, 4, parse_uint },
108 { "feature_incompat", &set_sb.s_feature_incompat, NULL, 4, parse_uint },
109 { "feature_ro_compat", &set_sb.s_feature_ro_compat, NULL, 4, parse_uint },
110 { "uuid", &set_sb.s_uuid, NULL, 16, parse_uuid },
111 { "volume_name", &set_sb.s_volume_name, NULL, 16, parse_string },
112 { "last_mounted", &set_sb.s_last_mounted, NULL, 64, parse_string },
113 { "lastcheck", &set_sb.s_lastcheck, NULL, 4, parse_uint },
114 { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, NULL,
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500115 4, parse_uint },
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400116 { "prealloc_blocks", &set_sb.s_prealloc_blocks, NULL, 1, parse_uint },
117 { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, NULL, 1,
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500118 parse_uint },
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400119 { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, NULL, 2,
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500120 parse_uint },
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400121 { "journal_uuid", &set_sb.s_journal_uuid, NULL, 16, parse_uuid },
122 { "journal_inum", &set_sb.s_journal_inum, NULL, 4, parse_uint },
123 { "journal_dev", &set_sb.s_journal_dev, NULL, 4, parse_uint },
124 { "last_orphan", &set_sb.s_last_orphan, NULL, 4, parse_uint },
125 { "hash_seed", &set_sb.s_hash_seed, NULL, 16, parse_uuid },
126 { "def_hash_version", &set_sb.s_def_hash_version, NULL, 1, parse_hashalg },
127 { "jnl_backup_type", &set_sb.s_jnl_backup_type, NULL, 1, parse_uint },
128 { "desc_size", &set_sb.s_desc_size, NULL, 2, parse_uint },
129 { "default_mount_opts", &set_sb.s_default_mount_opts, NULL, 4, parse_uint },
130 { "first_meta_bg", &set_sb.s_first_meta_bg, NULL, 4, parse_uint },
131 { "mkfs_time", &set_sb.s_mkfs_time, NULL, 4, parse_time },
132 { "jnl_blocks", &set_sb.s_jnl_blocks[0], NULL, 4, parse_uint, FLAG_ARRAY,
Theodore Ts'o89c80152005-01-20 21:45:27 -0500133 17 },
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400134 { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 2, parse_uint },
135 { "want_extra_isize", &set_sb.s_want_extra_isize, NULL, 2, parse_uint },
136 { "flags", &set_sb.s_flags, NULL, 4, parse_uint },
137 { "raid_stride", &set_sb.s_raid_stride, NULL, 2, parse_uint },
138 { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 4, parse_uint },
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400139 { "mmp_interval", &set_sb.s_mmp_update_interval, NULL, 2, parse_uint },
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400140 { "mmp_block", &set_sb.s_mmp_block, NULL, 8, parse_uint },
141 { "raid_stripe_width", &set_sb.s_raid_stripe_width, NULL, 4, parse_uint },
142 { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, NULL, 1, parse_uint },
143 { "kbytes_written", &set_sb.s_kbytes_written, NULL, 8, parse_uint },
144 { "snapshot_inum", &set_sb.s_snapshot_inum, NULL, 4, parse_uint },
145 { "snapshot_id", &set_sb.s_snapshot_id, NULL, 4, parse_uint },
146 { "snapshot_r_blocks_count", &set_sb.s_snapshot_r_blocks_count,
147 NULL, 8, parse_uint },
148 { "snapshot_list", &set_sb.s_snapshot_list, NULL, 4, parse_uint },
149 { "mount_opts", &set_sb.s_mount_opts, NULL, 64, parse_string },
150 { "usr_quota_inum", &set_sb.s_usr_quota_inum, NULL, 4, parse_uint },
151 { "grp_quota_inum", &set_sb.s_grp_quota_inum, NULL, 4, parse_uint },
152 { "overhead_blocks", &set_sb.s_overhead_blocks, NULL, 4, parse_uint },
153 { "checksum", &set_sb.s_checksum, NULL, 4, parse_uint },
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500154 { 0, 0, 0, 0 }
155};
156
157static struct field_set_info inode_fields[] = {
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400158 { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
159 { "mode", &set_inode.i_mode, NULL, 2, parse_uint },
160 { "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high,
161 2, parse_uint },
162 { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint },
163 { "atime", &set_inode.i_atime, NULL, 4, parse_time },
164 { "ctime", &set_inode.i_ctime, NULL, 4, parse_time },
165 { "mtime", &set_inode.i_mtime, NULL, 4, parse_time },
166 { "dtime", &set_inode.i_dtime, NULL, 4, parse_time },
167 { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high,
168 2, parse_uint },
169 { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint },
170 /* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */
171 { "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi,
172 6, parse_uint },
173 { "flags", &set_inode.i_flags, NULL, 4, parse_uint },
174 { "version", &set_inode.osd1.linux1.l_i_version,
175 &set_inode.i_version_hi, 4, parse_uint },
176 { "translator", &set_inode.osd1.hurd1.h_i_translator, NULL, 4, parse_uint },
177 { "block", &set_inode.i_block[0], NULL, 4, parse_uint, FLAG_ARRAY,
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500178 EXT2_NDIR_BLOCKS },
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400179 { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], NULL, 4, parse_uint },
180 { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], NULL, 4, parse_uint },
181 { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], NULL, 4, parse_uint },
182 { "generation", &set_inode.i_generation, NULL, 4, parse_uint },
183 /* Special case: i_file_acl_high is 2 bytes */
184 { "file_acl", &set_inode.i_file_acl,
185 &set_inode.osd2.linux2.l_i_file_acl_high, 6, parse_uint },
186 { "dir_acl", &set_inode.i_dir_acl, NULL, 4, parse_uint },
187 { "faddr", &set_inode.i_faddr, NULL, 4, parse_uint },
188 { "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint },
189 { "fsize", &set_inode.osd2.hurd2.h_i_fsize, NULL, 1, parse_uint },
190 { "checksum", &set_inode.osd2.linux2.l_i_checksum_lo,
191 &set_inode.i_checksum_hi, 2, parse_uint },
192 { "author", &set_inode.osd2.hurd2.h_i_author, NULL,
193 4, parse_uint },
194 { "extra_isize", &set_inode.i_extra_isize, NULL,
195 2, parse_uint },
196 { "ctime_extra", &set_inode.i_ctime_extra, NULL,
197 4, parse_uint },
198 { "mtime_extra", &set_inode.i_mtime_extra, NULL,
199 4, parse_uint },
200 { "atime_extra", &set_inode.i_atime_extra, NULL,
201 4, parse_uint },
202 { "crtime", &set_inode.i_crtime, NULL, 4, parse_uint },
203 { "crtime_extra", &set_inode.i_crtime_extra, NULL,
204 4, parse_uint },
205 { "bmap", NULL, NULL, 4, parse_bmap, FLAG_ARRAY },
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500206 { 0, 0, 0, 0 }
207};
208
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400209static struct field_set_info ext2_bg_fields[] = {
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400210 { "block_bitmap", &set_gd.bg_block_bitmap, NULL, 4, parse_uint },
211 { "inode_bitmap", &set_gd.bg_inode_bitmap, NULL, 4, parse_uint },
212 { "inode_table", &set_gd.bg_inode_table, NULL, 4, parse_uint },
213 { "free_blocks_count", &set_gd.bg_free_blocks_count, NULL, 2, parse_uint },
214 { "free_inodes_count", &set_gd.bg_free_inodes_count, NULL, 2, parse_uint },
215 { "used_dirs_count", &set_gd.bg_used_dirs_count, NULL, 2, parse_uint },
216 { "flags", &set_gd.bg_flags, NULL, 2, parse_uint },
217 { "itable_unused", &set_gd.bg_itable_unused, NULL, 2, parse_uint },
218 { "checksum", &set_gd.bg_checksum, NULL, 2, parse_gd_csum },
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400219 { 0, 0, 0, 0 }
220};
221
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400222static struct field_set_info ext4_bg_fields[] = {
223 { "block_bitmap", &set_gd4.bg_block_bitmap,
224 &set_gd4.bg_block_bitmap_hi, 4, parse_uint },
225 { "inode_bitmap", &set_gd4.bg_inode_bitmap,
226 &set_gd4.bg_inode_bitmap_hi, 4, parse_uint },
227 { "inode_table", &set_gd4.bg_inode_table,
228 &set_gd4.bg_inode_table_hi, 4, parse_uint },
229 { "free_blocks_count", &set_gd4.bg_free_blocks_count,
230 &set_gd4.bg_free_blocks_count_hi, 2, parse_uint },
231 { "free_inodes_count", &set_gd4.bg_free_inodes_count,
232 &set_gd4.bg_free_inodes_count_hi, 2, parse_uint },
233 { "used_dirs_count", &set_gd4.bg_used_dirs_count,
234 &set_gd4.bg_used_dirs_count_hi, 2, parse_uint },
235 { "flags", &set_gd4.bg_flags, NULL, 2, parse_uint },
236 { "exclude_bitmap", &set_gd4.bg_exclude_bitmap_lo,
237 &set_gd4.bg_exclude_bitmap_hi, 4, parse_uint },
238 { "block_bitmap_csum", &set_gd4.bg_block_bitmap_csum_lo,
239 &set_gd4.bg_block_bitmap_csum_hi, 2, parse_uint },
240 { "inode_bitmap_csum", &set_gd4.bg_inode_bitmap_csum_lo,
241 &set_gd4.bg_inode_bitmap_csum_hi, 2, parse_uint },
242 { "itable_unused", &set_gd4.bg_itable_unused,
243 &set_gd4.bg_itable_unused_hi, 2, parse_uint },
244 { "checksum", &set_gd4.bg_checksum, NULL, 2, parse_gd_csum },
245 { 0, 0, 0, 0 }
246};
247
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500248static struct field_set_info mmp_fields[] = {
249 { "clear", &set_mmp.mmp_magic, NULL, sizeof(set_mmp), parse_mmp_clear },
250 { "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint },
251 { "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint },
252 { "time", &set_mmp.mmp_time, NULL, 8, parse_uint },
253 { "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename),
254 parse_string },
255 { "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname),
256 parse_string },
257 { "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint },
258};
Eric Sandeen7105c182011-11-11 21:06:04 -0500259
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400260static int check_suffix(const char *field)
261{
262 int len = strlen(field);
263
264 if (len <= 3)
265 return 0;
266 field += len-3;
267 if (!strcmp(field, "_lo"))
268 return 1;
269 if (!strcmp(field, "_hi"))
270 return 2;
271 return 0;
272}
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400273
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500274static struct field_set_info *find_field(struct field_set_info *fields,
275 char *field)
276{
277 struct field_set_info *ss;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500278 const char *prefix;
279 char *arg, *delim, *idx, *tmp;
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400280 int suffix, prefix_len;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500281
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500282 if (fields == super_fields)
283 prefix = "s_";
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400284 else if (fields == inode_fields)
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500285 prefix = "i_";
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400286 else
287 prefix = "bg_";
288 prefix_len = strlen(prefix);
289 if (strncmp(field, prefix, prefix_len) == 0)
290 field += prefix_len;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500291
292 arg = malloc(strlen(field)+1);
293 if (!arg)
294 return NULL;
295 strcpy(arg, field);
296
297 idx = strchr(arg, '[');
298 if (idx) {
299 *idx++ = 0;
300 delim = idx + strlen(idx) - 1;
301 if (!*idx || *delim != ']')
302 idx = 0;
303 else
304 *delim = 0;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500305 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400306 /*
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500307 * Can we parse the number?
308 */
309 if (idx) {
310 array_idx = strtol(idx, &tmp, 0);
311 if (*tmp)
312 idx = 0;
313 }
314
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400315 /*
316 * If there is a valid _hi or a _lo suffix, strip it off
317 */
318 suffix = check_suffix(arg);
319 if (suffix > 0)
320 arg[strlen(arg)-3] = 0;
321
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500322 for (ss = fields ; ss->name ; ss++) {
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400323 if (suffix && ss->ptr2 == 0)
324 continue;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500325 if (ss->flags & FLAG_ARRAY) {
326 if (!idx || (strcmp(ss->name, arg) != 0))
327 continue;
328 if (ss->max_idx > 0 && array_idx >= ss->max_idx)
329 continue;
330 } else {
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400331 if (strcmp(ss->name, arg) != 0)
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500332 continue;
333 }
Theodore Ts'o6e9761c2009-06-15 03:52:30 -0400334 free(arg);
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500335 return ss;
336 }
Theodore Ts'o6e9761c2009-06-15 03:52:30 -0400337 free(arg);
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500338 return NULL;
339}
340
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400341/*
342 * Note: info->size == 6 is special; this means a base size 4 bytes,
343 * and secondiory (high) size of 2 bytes. This is needed for the
344 * special case of i_blocks_high and i_file_acl_high.
345 */
346static errcode_t parse_uint(struct field_set_info *info, char *field,
347 char *arg)
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500348{
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400349 unsigned long long n, num, mask, limit;
350 int suffix = check_suffix(field);
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500351 char *tmp;
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400352 void *field1 = info->ptr, *field2 = info->ptr2;
353 int size = (info->size == 6) ? 4 : info->size;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500354 union {
Theodore Ts'o007df862008-02-18 22:16:52 -0500355 __u64 *ptr64;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500356 __u32 *ptr32;
357 __u16 *ptr16;
358 __u8 *ptr8;
359 } u;
360
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400361 if (suffix == 1)
362 field2 = 0;
363 if (suffix == 2) {
364 field1 = field2;
365 field2 = 0;
366 }
367
368 u.ptr8 = (__u8 *) field1;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500369 if (info->flags & FLAG_ARRAY)
370 u.ptr8 += array_idx * info->size;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500371
Theodore Ts'o007df862008-02-18 22:16:52 -0500372 errno = 0;
373 num = STRTOULL(arg, &tmp, 0);
374 if (*tmp || errno) {
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500375 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
376 arg, info->name);
377 return EINVAL;
378 }
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400379 mask = ~0ULL >> ((8 - size) * 8);
Theodore Ts'o007df862008-02-18 22:16:52 -0500380 limit = ~0ULL >> ((8 - info->size) * 8);
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400381 if (field2 && info->size != 6)
382 limit = ~0ULL >> ((8 - info->size*2) * 8);
383
Theodore Ts'o007df862008-02-18 22:16:52 -0500384 if (num > limit) {
385 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
386 arg, info->name, limit);
387 return EINVAL;
388 }
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400389 n = num & mask;
390 switch (size) {
Theodore Ts'o007df862008-02-18 22:16:52 -0500391 case 8:
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400392 *u.ptr64 = n;
Theodore Ts'o007df862008-02-18 22:16:52 -0500393 break;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500394 case 4:
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400395 *u.ptr32 = n;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500396 break;
397 case 2:
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400398 *u.ptr16 = n;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500399 break;
400 case 1:
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400401 *u.ptr8 = n;
402 break;
403 }
404 if (!field2)
405 return 0;
406 n = num >> (size*8);
407 u.ptr8 = (__u8 *) field2;
408 if (info->size == 6)
409 size = 2;
410 switch (size) {
411 case 8:
412 *u.ptr64 = n;
413 break;
414 case 4:
415 *u.ptr32 = n;
416 break;
417 case 2:
418 *u.ptr16 = n;
419 break;
420 case 1:
421 *u.ptr8 = n;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500422 break;
423 }
424 return 0;
425}
426
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500427static errcode_t parse_int(struct field_set_info *info,
428 char *field EXT2FS_ATTR((unused)), char *arg)
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500429{
430 long num;
431 char *tmp;
432 __s32 *ptr32;
433 __s16 *ptr16;
434 __s8 *ptr8;
435
436 num = strtol(arg, &tmp, 0);
437 if (*tmp) {
438 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
439 arg, info->name);
440 return EINVAL;
441 }
442 switch (info->size) {
443 case 4:
444 ptr32 = (__s32 *) info->ptr;
445 *ptr32 = num;
446 break;
447 case 2:
448 ptr16 = (__s16 *) info->ptr;
449 *ptr16 = num;
450 break;
451 case 1:
452 ptr8 = (__s8 *) info->ptr;
453 *ptr8 = num;
454 break;
455 }
456 return 0;
457}
458
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500459static errcode_t parse_string(struct field_set_info *info,
460 char *field EXT2FS_ATTR((unused)), char *arg)
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500461{
462 char *cp = (char *) info->ptr;
463
464 if (strlen(arg) >= info->size) {
465 fprintf(stderr, "Error maximum size for %s is %d.\n",
466 info->name, info->size);
467 return EINVAL;
468 }
469 strcpy(cp, arg);
470 return 0;
471}
472
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500473static errcode_t parse_time(struct field_set_info *info,
474 char *field EXT2FS_ATTR((unused)), char *arg)
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500475{
Theodore Ts'oe3e92532006-05-21 19:26:06 -0400476 time_t t;
477 __u32 *ptr32;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500478
479 ptr32 = (__u32 *) info->ptr;
480
Theodore Ts'o4efae602005-09-24 21:56:38 -0400481 t = string_to_time(arg);
482
483 if (t == ((time_t) -1)) {
484 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
485 arg, info->name);
486 return EINVAL;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500487 }
Theodore Ts'o4efae602005-09-24 21:56:38 -0400488 *ptr32 = t;
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500489 return 0;
490}
491
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500492static errcode_t parse_uuid(struct field_set_info *info,
493 char *field EXT2FS_ATTR((unused)), char *arg)
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500494{
495 unsigned char * p = (unsigned char *) info->ptr;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400496
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500497 if ((strcasecmp(arg, "null") == 0) ||
498 (strcasecmp(arg, "clear") == 0)) {
499 uuid_clear(p);
500 } else if (strcasecmp(arg, "time") == 0) {
501 uuid_generate_time(p);
502 } else if (strcasecmp(arg, "random") == 0) {
503 uuid_generate(p);
504 } else if (uuid_parse(arg, p)) {
505 fprintf(stderr, "Invalid UUID format: %s\n", arg);
506 return EINVAL;
507 }
508 return 0;
509}
510
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500511static errcode_t parse_hashalg(struct field_set_info *info,
512 char *field EXT2FS_ATTR((unused)), char *arg)
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500513{
514 int hashv;
515 unsigned char *p = (unsigned char *) info->ptr;
516
517 hashv = e2p_string2hash(arg);
518 if (hashv < 0) {
519 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
520 return EINVAL;
521 }
522 *p = hashv;
523 return 0;
524}
525
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500526static errcode_t parse_bmap(struct field_set_info *info,
527 char *field EXT2FS_ATTR((unused)), char *arg)
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500528{
529 unsigned long num;
530 blk_t blk;
531 errcode_t retval;
532 char *tmp;
533
534 num = strtoul(arg, &tmp, 0);
535 if (*tmp) {
536 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
537 arg, info->name);
538 return EINVAL;
539 }
540 blk = num;
541
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400542 retval = ext2fs_bmap(current_fs, set_ino,
543 (struct ext2_inode *) &set_inode,
544 0, BMAP_SET, array_idx, &blk);
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500545 if (retval) {
546 com_err("set_inode", retval, "while setting block map");
547 }
Theodore Ts'o619a8ea2005-01-19 13:26:38 -0500548 return retval;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500549}
550
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400551static errcode_t parse_gd_csum(struct field_set_info *info, char *field,
552 char *arg)
Andreas Dilgercf96ecf2008-03-30 20:05:12 -0400553{
554
555 if (strcmp(arg, "calc") == 0) {
556 ext2fs_group_desc_csum_set(current_fs, set_bg);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400557 memcpy(&set_gd, ext2fs_group_desc(current_fs,
558 current_fs->group_desc,
559 set_bg),
560 sizeof(set_gd));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400561 printf("Checksum set to 0x%04x\n",
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400562 ext2fs_bg_checksum(current_fs, set_bg));
Andreas Dilgercf96ecf2008-03-30 20:05:12 -0400563 return 0;
564 }
565
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400566 return parse_uint(info, field, arg);
Andreas Dilgercf96ecf2008-03-30 20:05:12 -0400567}
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500568
569static void print_possible_fields(struct field_set_info *fields)
570{
571 struct field_set_info *ss;
572 const char *type, *cmd;
573 FILE *f;
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500574 char name[40], idx[40];
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500575
576 if (fields == super_fields) {
577 type = "Superblock";
578 cmd = "set_super_value";
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400579 } else if (fields == inode_fields) {
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500580 type = "Inode";
581 cmd = "set_inode";
Eric Sandeen7105c182011-11-11 21:06:04 -0500582 } else if (fields == mmp_fields) {
583 type = "MMP";
584 cmd = "set_mmp_value";
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400585 } else {
586 type = "Block group descriptor";
587 cmd = "set_block_group";
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500588 }
589 f = open_pager();
590
591 fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
592
593 for (ss = fields ; ss->name ; ss++) {
594 type = "unknown";
595 if (ss->func == parse_string)
596 type = "string";
597 else if (ss->func == parse_int)
598 type = "integer";
599 else if (ss->func == parse_uint)
600 type = "unsigned integer";
601 else if (ss->func == parse_uuid)
602 type = "UUID";
603 else if (ss->func == parse_hashalg)
604 type = "hash algorithm";
605 else if (ss->func == parse_time)
606 type = "date/time";
Theodore Ts'o1d3a9512005-01-07 21:14:12 -0500607 else if (ss->func == parse_bmap)
608 type = "set physical->logical block map";
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400609 else if (ss->func == parse_gd_csum)
610 type = "unsigned integer OR \"calc\"";
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500611 strcpy(name, ss->name);
612 if (ss->flags & FLAG_ARRAY) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400613 if (ss->max_idx > 0)
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500614 sprintf(idx, "[%d]", ss->max_idx);
615 else
616 strcpy(idx, "[]");
617 strcat(name, idx);
618 }
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400619 if (ss->ptr2)
620 strcat(name, "[_hi|_lo]");
621 fprintf(f, "\t%-25s\t%s\n", name, type);
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500622 }
623 close_pager(f);
624}
625
626
627void do_set_super(int argc, char *argv[])
628{
629 const char *usage = "<field> <value>\n"
630 "\t\"set_super_value -l\" will list the names of "
631 "superblock fields\n\twhich can be set.";
632 static struct field_set_info *ss;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400633
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500634 if ((argc == 2) && !strcmp(argv[1], "-l")) {
635 print_possible_fields(super_fields);
636 return;
637 }
638
639 if (common_args_process(argc, argv, 3, 3, "set_super_value",
640 usage, CHECK_FS_RW))
641 return;
642
643 if ((ss = find_field(super_fields, argv[1])) == 0) {
644 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
645 return;
646 }
647 set_sb = *current_fs->super;
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400648 if (ss->func(ss, argv[1], argv[2]) == 0) {
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500649 *current_fs->super = set_sb;
650 ext2fs_mark_super_dirty(current_fs);
651 }
652}
653
654void do_set_inode(int argc, char *argv[])
655{
656 const char *usage = "<inode> <field> <value>\n"
Theodore Ts'o1d3a9512005-01-07 21:14:12 -0500657 "\t\"set_inode_field -l\" will list the names of "
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500658 "the fields in an ext2 inode\n\twhich can be set.";
659 static struct field_set_info *ss;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400660
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500661 if ((argc == 2) && !strcmp(argv[1], "-l")) {
662 print_possible_fields(inode_fields);
663 return;
664 }
665
666 if (common_args_process(argc, argv, 4, 4, "set_inode",
667 usage, CHECK_FS_RW))
668 return;
669
670 if ((ss = find_field(inode_fields, argv[2])) == 0) {
671 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
672 return;
673 }
674
Theodore Ts'o08fd3f32004-12-23 14:11:34 -0500675 set_ino = string_to_inode(argv[1]);
676 if (!set_ino)
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500677 return;
678
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400679 if (debugfs_read_inode_full(set_ino,
680 (struct ext2_inode *) &set_inode, argv[1],
681 sizeof(set_inode)))
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500682 return;
683
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400684 if (ss->func(ss, argv[2], argv[3]) == 0) {
685 if (debugfs_write_inode_full(set_ino,
686 (struct ext2_inode *) &set_inode,
687 argv[1], sizeof(set_inode)))
Theodore Ts'o7dec0502004-12-21 20:37:36 -0500688 return;
689 }
690}
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400691
692void do_set_block_group_descriptor(int argc, char *argv[])
693{
694 const char *usage = "<bg number> <field> <value>\n"
695 "\t\"set_block_group_descriptor -l\" will list the names of "
696 "the fields in a block group descriptor\n\twhich can be set.";
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400697 struct field_set_info *table;
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400698 struct field_set_info *ss;
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400699 char *end;
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400700 void *edit, *target;
701 int size;
702
703 /*
704 *Determine whether we are editing an ext2 or ext4 block
705 * group descriptor
706 */
707 if (current_fs && current_fs->super->s_feature_incompat &
708 EXT4_FEATURE_INCOMPAT_64BIT) {
709 table = ext4_bg_fields;
710 edit = &set_gd4;
711 size = sizeof(set_gd4);
712 } else {
713 table = ext2_bg_fields;
714 edit = &set_gd;
715 size = sizeof(set_gd);
716 }
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400717
718 if ((argc == 2) && !strcmp(argv[1], "-l")) {
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400719 print_possible_fields(table);
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400720 return;
721 }
722
723 if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
724 usage, CHECK_FS_RW))
725 return;
726
727 set_bg = strtoul(argv[1], &end, 0);
728 if (*end) {
729 com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
730 return;
731 }
732
733 if (set_bg >= current_fs->group_desc_count) {
734 com_err(argv[0], 0, "block group number too big: %d", set_bg);
735 return;
736 }
737
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400738 if ((ss = find_field(table, argv[2])) == 0) {
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400739 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
740 return;
741 }
742
Theodore Ts'o16c581d2011-09-15 13:38:41 -0400743 target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg);
744 memcpy(edit, target, size);
745 if (ss->func(ss, argv[2], argv[3]) == 0) {
746 memcpy(target, edit, size);
Theodore Ts'oc7c12092007-04-13 15:56:00 -0400747 ext2fs_mark_super_dirty(current_fs);
748 }
749}
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400750
Theodore Ts'o3cebf9c2011-11-17 15:35:00 -0500751static errcode_t parse_mmp_clear(struct field_set_info *info,
752 char *field EXT2FS_ATTR((unused)),
753 char *arg EXT2FS_ATTR((unused)))
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400754{
755 errcode_t retval;
756
757 retval = ext2fs_mmp_clear(current_fs);
758 if (retval != 0)
759 com_err("set_mmp_value", retval, "while clearing MMP block\n");
760 else
761 memcpy(info->ptr, current_fs->mmp_buf, info->size);
762
763 return 1; /* we don't need the MMP block written again */
764}
765
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400766void do_set_mmp_value(int argc, char *argv[])
767{
768 const char *usage = "<field> <value>\n"
769 "\t\"set_mmp_value -l\" will list the names of "
770 "MMP fields\n\twhich can be set.";
771 static struct field_set_info *smmp;
772 struct mmp_struct *mmp_s;
773 errcode_t retval;
774
775 if (argc == 2 && strcmp(argv[1], "-l") == 0) {
776 print_possible_fields(mmp_fields);
777 return;
778 }
779
Eric Sandeen7105c182011-11-11 21:06:04 -0500780 if (check_fs_open(argv[0]))
781 return;
782
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400783 if (current_fs->super->s_mmp_block == 0) {
784 com_err(argv[0], 0, "no MMP block allocated\n");
785 return;
786 }
787
788 if (common_args_process(argc, argv, 2, 3, "set_mmp_value",
789 usage, CHECK_FS_RW))
790 return;
791
792 mmp_s = current_fs->mmp_buf;
793 if (mmp_s == NULL) {
794 retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s);
795 if (retval) {
796 com_err(argv[0], retval, "allocating MMP buffer\n");
797 return;
798 }
799 retval = ext2fs_mmp_read(current_fs,
800 current_fs->super->s_mmp_block, mmp_s);
801 if (retval) {
802 com_err(argv[0], retval, "reading MMP block %llu.\n",
803 (long long)current_fs->super->s_mmp_block);
804 ext2fs_free_mem(mmp_s);
805 return;
806 }
807 current_fs->mmp_buf = mmp_s;
808 }
809
810 smmp = find_field(mmp_fields, argv[1]);
811 if (smmp == 0) {
812 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
813 return;
814 }
815
816 set_mmp = *mmp_s;
817 if (smmp->func(smmp, argv[1], argv[2]) == 0) {
818 ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block,
819 &set_mmp);
820 *mmp_s = set_mmp;
821 }
822}
823