|
root / base / usr / src / cmd / fs.d / udfs / fsdb / fsdb.c
fsdb.c C 1424 lines 28.1 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <inttypes.h>
#include <libgen.h>
#include <locale.h>

#include <sys/param.h>
#include <sys/signal.h>
#include <sys/file.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/vnode.h>
#include <sys/mntent.h>
#include <sys/wait.h>
#include <sys/vtoc.h>
#include <sys/dkio.h>

#include <sys/fs/udf_volume.h>
#include "ud_lib.h"
#include "y.tab.h"

typedef unsigned short unicode_t;
#define	MAXNAMLEN	0x200

extern uint32_t i_number;

extern int32_t run_fsdb();

void usage();
void init_buffers();
char *getblk(u_offset_t);
int32_t parse_udfs(uint32_t);
int32_t parse_vds(uint32_t, uint32_t);
int32_t parse_part(struct part_desc *);
int32_t parse_lvd(struct log_vol_desc *);
int32_t parse_fsds();
int32_t get_vat_loc();
int32_t get_fid(uint32_t, uint8_t *, uint64_t);


char *progname;
char prompt[256] = "fsdb>";

#define	ARG_OVERRIDE	0
#define	ARG_NEW_PROMPT	1
#define	ARG_WR_ENABLED	2
#define	ARG_USAGE	3

char *subopt_v[] = {
	"o",
	"p",
	"w",
	"?",
	NULL
};
int32_t override = 0;
int32_t openflg = O_RDONLY;

#define	MAX_PARTS	10

/*
 * udp_flags
 */
#define	UDP_BITMAPS	0x00
#define	UDP_SPACETBLS	0x01

ud_handle_t udh;
int32_t fd, nparts, nmaps;
int32_t bmask, l2d, l2b;


uint16_t ricb_prn;
uint32_t ricb_loc, ricb_len;
extern int value;


int32_t
main(int argc, char *argv[])
{
	int opt, ret;
	uint32_t bsize;
	char *subopts, *optval;

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	progname = argv[0];

	while ((opt = getopt(argc, argv, "o:")) != EOF) {
		switch (opt) {
		case 'o' :
			subopts = optarg;
			while (*subopts != '\0') {
				switch (getsubopt(&subopts,
					subopt_v, &optval)) {
				case ARG_OVERRIDE :
					override = 1;
					(void) fprintf(stdout,
					gettext("error checking off\n"));
					break;
				case ARG_NEW_PROMPT :
					if (optval == NULL) {
						usage();
					}
					if (strlen(optval) > 255) {
						(void) fprintf(stdout,
						gettext("prompt should be less"
						"than 255 bytes\n"));
						exit(1);
					}
					(void) strcpy(prompt, optval);
					break;
				case ARG_WR_ENABLED :
					openflg = O_RDWR;
					break;
				case ARG_USAGE :
				default :
					usage();
				}
			}
			break;
		default :
			usage();
		}
	}

	if ((argc - optind) != 1) {	/* Should just have "special" left */
		usage();
	}

	if (ud_init(-1, &udh) != 0) {
		(void) fprintf(stderr,
		gettext("udfs labelit: cannot initialize ud_lib\n"));
		exit(1);
	}

	if ((fd = ud_open_dev(udh, argv[optind], openflg)) < 0) {
		perror("open");
		exit(1);
	}

	if ((ret = ud_fill_udfs_info(udh)) != 0) {
		return (ret);
	}

	if ((udh->udfs.flags & VALID_UDFS) == 0) {
		return (1);
	}

	bsize = udh->udfs.lbsize;
	bmask = bsize - 1;
	l2d = 0;
	while ((bsize >> l2d) > DEV_BSIZE) {
		l2d++;
	}
	l2b = l2d + 9;

	ricb_prn = udh->udfs.ricb_prn;
	ricb_loc = udh->udfs.ricb_loc;
	ricb_len = udh->udfs.ricb_len;

	value = i_number = ud_xlate_to_daddr(udh, ricb_prn, ricb_loc);

	init_buffers();

	run_fsdb();

	ud_fini(udh);
	(void) close(fd);

	return (0);
}

/*
 * usage - print usage and exit
 */
void
usage()
{
	(void) fprintf(stdout,
		gettext("usage:   %s [options] special\n"), progname);
	(void) fprintf(stdout,
		gettext("options:\n"));
	(void) fprintf(stdout,
		gettext("\t-o\tSpecify udfs filesystem sepcific options\n"));
	(void) fprintf(stdout,
		gettext("\t\tAvailable suboptions are:\n"));
	(void) fprintf(stdout,
		gettext("\t\t?\tdisplay usage\n"));
	(void) fprintf(stdout,
		gettext("\t\to\toverride some error conditions\n"));
	(void) fprintf(stdout,
		gettext("\t\tp\t\"string\" set prompt to string\n"));
	(void) fprintf(stdout,
		gettext("\t\tw\topen for write\n"));
	exit(1);
}

#define	NBUF	10
static struct lbuf {
	struct lbuf	*fwd;
	struct lbuf	*back;
	int32_t		valid;
	char		*blkaddr;
	u_offset_t	blkno;
} lbuf[NBUF], bhdr;

#define	INSERT(bp)	\
	{ \
		bp->back = &bhdr; \
		bp->fwd = bhdr.fwd; \
		bhdr.fwd->back = bp; \
		bhdr.fwd = bp; \
	}

void
init_buffers()
{
	int32_t i;
	char *addr;
	struct lbuf *bp;

	addr = malloc(NBUF * udh->udfs.lbsize);
	bhdr.fwd = bhdr.back = &bhdr;
	for (i = 0; i < NBUF; i++) {
		bp = &lbuf[i];
		bp->blkaddr = addr + i * udh->udfs.lbsize;
		bp->valid = 0;
		INSERT(bp);
	}
}

char *
getblk(u_offset_t address)
{
	u_offset_t off, block;
	struct lbuf *bp;

	off = address & ~bmask;
	block = address >> l2b;
	for (bp = bhdr.fwd; bp != &bhdr; bp = bp->fwd) {
		if (bp->valid && bp->blkno == block) {
			goto found;
		}
	}
	bp = bhdr.back;
	bp->blkno = block;
	bp->valid = 0;
	errno = 0;
	if (llseek(fd, off, SEEK_SET) != off) {
		(void) fprintf(stdout,
			gettext("Seek failed fd %x off %llx errno %x\n"),
			fd, off, errno);
		return (NULL);
	}
	errno = 0;
	if (read(fd, bp->blkaddr, udh->udfs.lbsize) != udh->udfs.lbsize) {
		(void) fprintf(stdout,
			gettext("Read failed fd %x off %llx errno %x\n"),
			fd, off, errno);
		return (NULL);
	}
	bp->valid = 1;
found:
	bp->back->fwd = bp->fwd;
	bp->fwd->back = bp->back;
	INSERT(bp);
	return (bp->blkaddr);
}


int32_t
putblk(caddr_t address)
{
	u_offset_t off;
	struct lbuf *bp;

	if (openflg == O_RDONLY) {
		(void) fprintf(stdout,
			gettext("Not run with -w flag\n"));
		return (1);
	}

	for (bp = bhdr.fwd; bp != &bhdr; bp = bp->fwd) {
		if (bp->valid && bp->blkaddr == address) {
			goto found;
		}
	}
	(void) fprintf(stdout,
		gettext("Could not find the buffer\n"));
	return (1);

found:
	off = bp->blkno << l2b;
	if (llseek(fd, off, SEEK_SET) == off) {
		if (write(fd, bp->blkaddr, udh->udfs.lbsize) ==
			udh->udfs.lbsize) {
			return (0);
		}
		(void) fprintf(stdout,
			gettext("Write failed fd %x off %llx errno %x\n"),
			fd, off, errno);
	} else {
		(void) fprintf(stdout,
			gettext("Seek failed fd %x off %llx errno %x\n"),
			fd, off, errno);
	}
	return (1);
}

void
inval_bufs()
{
	struct lbuf *bp;

	for (bp = bhdr.fwd; bp != &bhdr; bp = bp->fwd) {
		bp->valid = 0;
	}
}

/*
 * If addr == NULL then use id to print the desc
 * other wise use addr to self identify the type of desc
 */
void
print_desc(uint32_t addr, int32_t id)
{
	struct tag *tag;
	caddr_t baddr;

	/*
	 * Read the block at addr
	 * find out the type of tag
	 * and print the descriptor
	 */
	if (addr != 0) {
		if ((baddr = getblk(addr & (~bmask))) == NULL) {
			(void) fprintf(stdout,
				gettext("Could not read block %x\n"),
				addr >> l2b);
		}
		/* LINTED */
		tag = (struct tag *)(baddr + (addr & bmask));
	} else {
		switch (id) {
			case AVD :
				/* LINTED */
				if ((tag = (struct tag *)getblk(
					udh->udfs.avdp_loc << l2b)) == NULL) {
					(void) fprintf(stdout,
					gettext("Could not read AVDP\n"));
				}
				break;
			case MVDS :
			case RVDS :
			case INTS :
				{
					uint32_t i, end;

					if (id == MVDS) {
						i = udh->udfs.mvds_loc;
						end = i +
						(udh->udfs.mvds_len >> l2b);
					} else if (id == RVDS) {
						i = udh->udfs.rvds_loc;
						end = i +
						(udh->udfs.rvds_len >> l2b);
					} else {
						i = udh->udfs.lvid_loc;
						end = i +
						(udh->udfs.lvid_len >> l2b);
					}

					for (; i < end; i++) {
						print_desc(i << l2b, 0);
					}
				}
				return;
			case FSDS :
			case ROOT :
				{
					uint16_t prn;
					uint32_t i, end, block;

					if (id == FSDS) {
						prn = udh->udfs.fsds_prn;
						i = udh->udfs.fsds_loc;
						end = i +
						(udh->udfs.fsds_len >> l2b);
					} else {
						prn = ricb_prn;
						i = ricb_loc;
						end = i + (ricb_len >> l2b);
					}

					for (; i < end; i++) {
						if ((block = ud_xlate_to_daddr(
							udh, prn, i)) == 0) {
							(void) fprintf(stdout,
							gettext("Cannot xlate "
							"prn %x loc %x\n"),
							prn, i);
							continue;
						}
						print_desc(block << l2b, 0);
					}
				}
				/* FALLTHROUGH */
			default :
				return;
		}
	}

	switch (SWAP_16(tag->tag_id)) {
		case UD_PRI_VOL_DESC :
			print_pvd(stdout, (struct pri_vol_desc *)tag);
			break;
		case UD_ANCH_VOL_DESC :
			print_avd(stdout, (struct anch_vol_desc_ptr *)tag);
			break;
		case UD_VOL_DESC_PTR :
			print_vdp(stdout, (struct vol_desc_ptr *)tag);
			break;
		case UD_IMPL_USE_DESC :
			print_iuvd(stdout, (struct iuvd_desc *)tag);
			break;
		case UD_PART_DESC :
			print_part(stdout, (struct part_desc *)tag);
			break;
		case UD_LOG_VOL_DESC :
			print_lvd(stdout, (struct log_vol_desc *)tag);
			break;
		case UD_UNALL_SPA_DESC :
			print_usd(stdout, (struct unall_spc_desc *)tag);
			break;
		case UD_TERM_DESC :
			(void) fprintf(stdout, "TERM DESC\n");
			print_tag(stdout, tag);
			break;
		case UD_LOG_VOL_INT :
			print_lvid(stdout, (struct log_vol_int_desc *)tag);
			break;
		case UD_FILE_SET_DESC :
			print_fsd(stdout, udh, (struct file_set_desc *)tag);
			break;
		case UD_FILE_ID_DESC :
			print_fid(stdout, (struct file_id *)tag);
			break;
		case UD_ALLOC_EXT_DESC :
			print_aed(stdout, (struct alloc_ext_desc *)tag);
			break;
		case UD_INDIRECT_ENT :
			print_ie(stdout, (struct indirect_entry *)tag);
			break;
		case UD_TERMINAL_ENT :
			print_td(stdout, (struct term_desc *)tag);
			break;
		case UD_FILE_ENTRY :
			print_fe(stdout, (struct file_entry *)tag);
			break;
		case UD_EXT_ATTR_HDR :
		case UD_UNALL_SPA_ENT :
		case UD_SPA_BMAP_DESC :
		case UD_PART_INT_DESC :
		case UD_EXT_FILE_ENT :
			break;
		default :
			(void) fprintf(stdout,
				gettext("unknown descriptor\n"));
			print_tag(stdout, tag);
			break;
	}
}

void
set_file(int32_t id, uint32_t iloc, uint64_t value)
{
	uint8_t i8;
	uint16_t i16;
	uint32_t i32, block, ea_len, ea_off;
	uint64_t i64;
	struct file_entry *fe;
	struct dev_spec_ear *ds;
	struct attr_hdr *ah;
	struct ext_attr_hdr *eah;

	/* LINTED */
	if ((fe = (struct file_entry *)getblk(iloc)) == NULL) {
		return;
	}
	if (ud_verify_tag(udh, &fe->fe_tag, UD_FILE_ENTRY,
			SWAP_32(fe->fe_tag.tag_loc), 1, 1) != 0) {
		return;
	}
	i8 = (uint8_t)value;
	i16 = SWAP_16(((uint16_t)value));
	i32 = SWAP_32(((uint32_t)value));
	i64 = SWAP_64(value);
	switch (id) {
		case ATTZ :
			fe->fe_acc_time.ts_tzone = i16;
			break;
		case ATYE  :
			fe->fe_acc_time.ts_year = i16;
			break;
		case ATMO  :
			fe->fe_acc_time.ts_month = i8;
			break;
		case ATDA  :
			fe->fe_acc_time.ts_day = i8;
			break;
		case ATHO  :
			fe->fe_acc_time.ts_hour = i8;
			break;
		case ATMI  :
			fe->fe_acc_time.ts_min = i8;
			break;
		case ATSE  :
			fe->fe_acc_time.ts_sec = i8;
			break;
		case ATCE  :
			fe->fe_acc_time.ts_csec = i8;
			break;
		case ATHU  :
			fe->fe_acc_time.ts_husec = i8;
			break;
		case ATMIC :
			fe->fe_acc_time.ts_usec = i8;
			break;
		case CTTZ  :
			fe->fe_attr_time.ts_tzone = i16;
			break;
		case CTYE  :
			fe->fe_attr_time.ts_year = i16;
			break;
		case CTMO  :
			fe->fe_attr_time.ts_month = i8;
			break;
		case CTDA  :
			fe->fe_attr_time.ts_day = i8;
			break;
		case CTHO  :
			fe->fe_attr_time.ts_hour = i8;
			break;
		case CTMI  :
			fe->fe_attr_time.ts_min = i8;
			break;
		case CTSE  :
			fe->fe_attr_time.ts_sec = i8;
			break;
		case CTCE  :
			fe->fe_attr_time.ts_csec = i8;
			break;
		case CTHU  :
			fe->fe_attr_time.ts_husec = i8;
			break;
		case CTMIC :
			fe->fe_attr_time.ts_usec = i8;
			break;
		case MTTZ  :
			fe->fe_mod_time.ts_tzone = i16;
			break;
		case MTYE  :
			fe->fe_mod_time.ts_year = i16;
			break;
		case MTMO  :
			fe->fe_mod_time.ts_month = i8;
			break;
		case MTDA  :
			fe->fe_mod_time.ts_day = i8;
			break;
		case MTHO  :
			fe->fe_mod_time.ts_hour = i8;
			break;
		case MTMI  :
			fe->fe_mod_time.ts_min = i8;
			break;
		case MTSE  :
			fe->fe_mod_time.ts_sec = i8;
			break;
		case MTCE  :
			fe->fe_mod_time.ts_csec = i8;
			break;
		case MTHU  :
			fe->fe_mod_time.ts_husec = i8;
			break;
		case MTMIC :
			fe->fe_mod_time.ts_usec = i8;
			break;
		case GID  :
			fe->fe_gid = i32;
			break;
		case LN  :
			fe->fe_lcount = i16;
			break;
		case MD  :
			fe->fe_perms = i32;
			break;
		case MAJ  :
		case MIO  :
			if ((fe->fe_icb_tag.itag_ftype != VBLK) &&
				(fe->fe_icb_tag.itag_ftype != VCHR)) {
				(void) fprintf(stdout,
					gettext("Not a device\n"));
				break;
			}
			/* LINTED */
			eah = (struct ext_attr_hdr *)fe->fe_spec;
			ea_off = SWAP_32(eah->eah_ial);
			ea_len = SWAP_32(fe->fe_len_ear);
			block = SWAP_32(eah->eah_tag.tag_loc);
			if (ea_len && (ud_verify_tag(udh, &eah->eah_tag,
				UD_EXT_ATTR_HDR, block, 1, 1) == 0)) {
				while (ea_off < ea_len) {
					/* LINTED */
					ah = (struct attr_hdr *)
						&fe->fe_spec[ea_off];
					if ((ah->ahdr_atype == SWAP_32(12)) &&
						(ah->ahdr_astype == 1)) {

						ds = (struct dev_spec_ear *)ah;
						if (id == MAJ) {
							ds->ds_major_id = i32;
						} else {
							ds->ds_minor_id = i32;
						}
						ud_make_tag(udh, &eah->eah_tag,
							UD_EXT_ATTR_HDR, block,
						eah->eah_tag.tag_crc_len);
						break;
					}
				}
			}
			(void) fprintf(stdout,
			gettext("does not have a Device Specification EA\n"));
			break;
		case NM  :
			break;
		case SZ  :
			fe->fe_info_len = i64;
			break;
		case UID  :
			fe->fe_uid = i32;
			break;
		case UNIQ :
			fe->fe_uniq_id = i32;
			break;
		default :
			(void) fprintf(stdout,
				gettext("Unknown set\n"));
	}
	ud_make_tag(udh, &fe->fe_tag, UD_FILE_ENTRY,
		SWAP_32(fe->fe_tag.tag_loc), fe->fe_tag.tag_crc_len);
	(void) putblk((caddr_t)fe);
}

caddr_t
verify_inode(uint32_t addr, uint32_t type)
{
	struct file_entry *fe;
	struct tag *tag;

	/* LINTED */
	if ((tag = (struct tag *)getblk(addr & (~bmask))) == NULL) {
		(void) fprintf(stdout,
			gettext("Could not read block %x\n"),
			addr >> l2b);
	} else {
		if (ud_verify_tag(udh, tag, UD_FILE_ENTRY,
			addr >> l2b, 0, 1) != 0) {
			(void) fprintf(stdout,
				gettext("Not a file entry(inode) at %x\n"),
				addr >> l2b);
		} else {
			if (ud_verify_tag(udh, tag, UD_FILE_ENTRY,
				SWAP_32(tag->tag_loc), 1, 1) != 0) {
				(void) fprintf(stdout,
					gettext("CRC failed\n"));
			} else {
				fe = (struct file_entry *)tag;
				if ((type == 0) ||
					(type == fe->fe_icb_tag.itag_ftype)) {
					return ((caddr_t)tag);
				}
			}
		}
	}
	return (0);
}

void
print_inode(uint32_t addr)
{
	if (verify_inode(addr, 0) != NULL) {
		print_desc(addr, 0);
	}
}

int32_t
verify_dent(uint32_t i_addr, uint32_t nent)
{
	uint32_t ent = 0;
	uint64_t off = 0;
	uint8_t buf[1024];
	struct file_id *fid;

	/* LINTED */
	fid = (struct file_id *)buf;

	if (verify_inode(i_addr, 4) == 0) {
		(void) fprintf(stdout,
			gettext("Inode is not a directory\n"));
		return (1);
	}

	while (get_fid(i_addr >> l2b, buf, off) == 0) {
		off += FID_LEN(fid);
		if (ent == nent) {
			return (0);
		}
		ent++;
	}
	(void) fprintf(stdout,
		gettext("Reached EOF\n"));
	return (1);
}

void
print_dent(uint32_t i_addr, uint32_t nent)
{
	uint32_t ent = 0;
	uint64_t off = 0;
	uint8_t buf[1024];
	struct file_id *fid;

	/* LINTED */
	fid = (struct file_id *)buf;

	if (verify_dent(i_addr, nent) == 0) {
		while (get_fid(i_addr >> l2b, buf, off) == 0) {
			off += FID_LEN(fid);
			if (ent == nent) {
				print_fid(stdout, fid);
				return;
			}
			ent++;
		}
	}
}

uint32_t in;
uint32_t de_count, ie_count;
struct ext {
	uint16_t prn;
	uint16_t flags;
	uint32_t blkno;
	uint32_t len;
} *de, *ie;

int32_t
get_blkno(uint32_t inode, uint32_t *blkno, uint64_t off)
{
	struct file_entry *fe;
	int32_t i, d, nent;
	uint16_t prn, flags, elen;
	uint32_t desc_type, bno, len;
	struct short_ad *sad;
	struct long_ad *lad;
	uint64_t b_off, e_off;

	if (inode != in) {
		/* LINTED */
		if ((fe = (struct file_entry *)
				getblk(inode << l2b)) == NULL) {
			(void) fprintf(stdout,
				gettext("Could not read block %x\n"),
				off & (~bmask));
			return (1);
		}
		desc_type = SWAP_16(fe->fe_icb_tag.itag_flags) & 0x7;
		sad = NULL;
		lad = NULL;
		if (desc_type == ICB_FLAG_SHORT_AD) {
			elen = sizeof (struct short_ad);
			/* LINTED */
			sad = (struct short_ad *)
				(fe->fe_spec + SWAP_32(fe->fe_len_ear));
		} else if (desc_type == ICB_FLAG_LONG_AD) {
			elen = sizeof (struct long_ad);
			/* LINTED */
			lad = (struct long_ad *)
				(fe->fe_spec + SWAP_32(fe->fe_len_ear));
		} else if (desc_type == ICB_FLAG_ONE_AD) {
			*blkno = inode;
			return (0);
		} else {
			/* This cannot happen return */
			return (EINVAL);
		}

		nent = SWAP_32(fe->fe_len_adesc) / elen;
		de = malloc(nent * sizeof (struct ext));
		if (de == NULL) {
			(void) fprintf(stdout,
				gettext("could not allocate memeory\n"));
			return (1);
		}
		in = inode;
		de_count = nent;
		for (d = 0, i = 0; i < nent; i++) {
			switch (desc_type) {
			case ICB_FLAG_SHORT_AD:
				prn = 0;
				bno = SWAP_32(sad->sad_ext_loc);
				len = SWAP_32(sad->sad_ext_len);
				break;
			case ICB_FLAG_LONG_AD:
				prn = SWAP_16(lad->lad_ext_prn);
				bno = SWAP_32(lad->lad_ext_loc);
				len = SWAP_32(lad->lad_ext_len);
				break;
			default:
				prn = 0;
				bno = 0;
				len = 0;
			}
			flags = len >> 30;
			if (flags == 0x3) {
				(void) fprintf(stdout,
					gettext("Handle IE\n"));
			} else {
				de[d].prn = prn;
				de[d].flags = flags;
				de[d].blkno = bno;
				de[d].len = len & 0x3FFFFFFF;
				d++;
			}
		}
	}

	b_off = 0;
	for (i = 0; i < de_count; i++) {
		e_off = b_off + de[i].len;
		if (off < e_off) {
			bno = de[i].blkno + ((off - b_off) >> l2b);
			if ((*blkno = ud_xlate_to_daddr(
					udh, de[i].prn, bno)) == 0) {
				return (1);
			}
			return (0);
		}
		b_off = e_off;
	}
	return (1);
}

/*
 * assume the buffer is big enough
 * for the entire request
 */
int32_t
read_file(uint32_t inode, uint8_t *buf, uint32_t count, uint64_t off)
{
	caddr_t addr;
	uint32_t bno, tcount;


	while (count) {
		if (get_blkno(inode, &bno, off) != 0) {
			return (1);
		}
		if ((addr = getblk(bno << l2b)) == NULL) {
			return (1);
		}
		if (bno == inode) {
			struct file_entry *fe;
			/*
			 * embedded file
			 */
			/* LINTED */
			fe = (struct file_entry *)addr;
			addr += 0xB0 + SWAP_32(fe->fe_len_ear);
			if (off >= SWAP_64(fe->fe_info_len)) {
				return (1);
			}
		}
		tcount = udh->udfs.lbsize - (off & bmask);
		if (tcount > count) {
			tcount = count;
		}
		addr += off & bmask;
		(void) memcpy(buf, addr, tcount);
		count -= tcount;
		buf += tcount;
		off += tcount;
	}
	return (0);
}

int32_t
get_fid(uint32_t inode, uint8_t *buf, uint64_t off)
{
	struct file_id *fid;

	/* LINTED */
	fid = (struct file_id *)buf;
	if ((read_file(inode, buf, sizeof (struct file_id), off)) != 0) {
		return (1);
	}

	if (ud_verify_tag(udh, &fid->fid_tag, UD_FILE_ID_DESC, 0, 0, 1) != 0) {
		(void) fprintf(stdout,
			gettext("file_id tag does not verify off %llx\n"),
			off);
		return (1);
	}

	if ((read_file(inode, buf, FID_LEN(fid), off)) != 0) {
		return (1);
	}

	return (0);
}

/*
 * Path is absolute path
 */
int32_t
inode_from_path(char *path, uint32_t *in, uint8_t *fl)
{
	char dname[1024];
	char fname[256];
	int32_t err;
	uint32_t dinode;
	struct tag *tag;
	uint8_t flags;

	uint8_t buf[1024];
	uint64_t off;
	struct file_id *fid;
	uint8_t *addr;

	if (strcmp(path, "/") == 0) {
		*fl = FID_DIR;
		if ((*in = ud_xlate_to_daddr(udh, ricb_prn, ricb_loc)) == 0) {
			return (1);
		}
		return (0);
	}

	(void) strcpy(dname, path);
	(void) strcpy(fname, basename(dname));
	(void) dirname(dname);

	if ((err = inode_from_path(dname, &dinode, &flags))  != 0) {
		return (1);
	}


	/*
	 * Check if dname is a directory
	 */
	if ((flags & FID_DIR) == 0) {
		(void) fprintf(stdout,
			gettext("Path %s is not a directory\n"), path);
	}

	/*
	 * Search for the fname in the directory now
	 */


	off = 0;
	/* LINTED */
	fid = (struct file_id *)buf;
	while (get_fid(dinode, buf, off) == 0) {
		off += FID_LEN(fid);
		if (fid->fid_flags & FID_DELETED) {
			continue;
		}
		addr = &fid->fid_spec[SWAP_16((fid)->fid_iulen) + 1];
		if (fid->fid_flags & FID_PARENT) {
			addr[0] = '.';
			addr[1] = '.';
			addr[2] = '\0';
		} else {
			addr[fid->fid_idlen] = '\0';
		}
		if (strcmp((caddr_t)addr, fname) == 0) {
			*fl = fid->fid_flags;
			if ((*in = ud_xlate_to_daddr(udh,
				SWAP_16(fid->fid_icb.lad_ext_prn),
				SWAP_32(fid->fid_icb.lad_ext_loc))) == 0) {
				return (1);
			}
			/* LINTED */
			if ((tag = (struct tag *)getblk(*in << l2b)) == NULL) {
				(void) fprintf(stdout,
					gettext("Could not read block %x\n"),
					*in);
				return (1);
			}
			if (ud_verify_tag(udh, tag, UD_FILE_ENTRY,
					0, 0, 1) != 0) {
				(void) fprintf(stdout,
					gettext("Not a file entry(inode)"
					" at %x\n"), *in);
				return (1);
			}
			if (ud_verify_tag(udh, tag, UD_FILE_ENTRY,
					SWAP_32(tag->tag_loc), 1, 1) != 0) {
				(void) fprintf(stdout,
					gettext("CRC failed\n"));
				return (1);
			}

			return (0);
		}
	}
	return (err);
}

struct recu_dir {
	struct recu_dir *next;
	uint32_t inode;
	char *nm;
};

void
list(char *nm, uint32_t in, uint32_t fl)
{
	uint8_t buf[1024];
	uint64_t off;
	struct file_id *fid;
	struct recu_dir *rd, *erd, *temp;
	uint32_t iloc;

	rd = erd = temp = NULL;
	if (verify_inode(in << l2b, 4) == 0) {
		(void) fprintf(stdout,
			gettext("Inode is not a directory\n"));
		return;
	}

	if (fl & 2) {
		(void) printf("\n");
		if (fl & 1) {
			(void) fprintf(stdout,
				gettext("i#: %x\t"), in);
		}
		(void) printf("%s\n", nm);
	}

	off = 0;
	/* LINTED */
	fid = (struct file_id *)buf;
	while (get_fid(in, buf, off) == 0) {
		off += FID_LEN(fid);
		if (fid->fid_flags & FID_DELETED) {
			continue;
		}
		iloc = ud_xlate_to_daddr(udh, SWAP_16(fid->fid_icb.lad_ext_prn),
				SWAP_32(fid->fid_icb.lad_ext_loc));
		if (fl & 1) {
			(void) fprintf(stdout,
				gettext("i#: %x\t"), iloc);
		}
		if (fid->fid_flags & FID_PARENT) {
			(void) fprintf(stdout,
				gettext("..\n"));
		} else {
			int32_t i;
			uint8_t *addr;

			addr = &fid->fid_spec[SWAP_16((fid)->fid_iulen) + 1];
			for (i = 0; i < fid->fid_idlen - 1; i++)
				(void) fprintf(stdout, "%c", addr[i]);
			(void) fprintf(stdout, "\n");
			if ((fid->fid_flags & FID_DIR) &&
				(fl & 2)) {
				temp = (struct recu_dir *)
					malloc(sizeof (struct recu_dir));
				if (temp == NULL) {
					(void) fprintf(stdout,
					gettext("Could not allocate memory\n"));
				} else {
					temp->next = NULL;
					temp->inode = iloc;
					temp->nm = malloc(strlen(nm) + 1 +
						fid->fid_idlen + 1);
					if (temp->nm != NULL) {
						(void) strcpy(temp->nm, nm);
						(void) strcat(temp->nm, "/");
						(void) strncat(temp->nm,
							(char *)addr,
							fid->fid_idlen);
					}
					if (rd == NULL) {
						erd = rd = temp;
					} else {
						erd->next = temp;
						erd = temp;
					}
				}
			}
		}
	}

	while (rd != NULL) {
		if (rd->nm != NULL) {
			list(rd->nm, rd->inode, fl);
		} else {
			list(".", rd->inode, fl);
		}
		temp = rd;
		rd = rd->next;
		if (temp->nm) {
			free(temp->nm);
		}
		free(temp);
	}
}

void
fill_pattern(uint32_t addr, uint32_t count, char *pattern)
{
	uint32_t beg, end, soff, lcount;
	int32_t len = strlen(pattern);
	caddr_t buf, p;

	if (openflg == O_RDONLY) {
		(void) fprintf(stdout,
			gettext("Not run with -w flag\n"));
		return;
	}

	if (count == 0) {
		count = 1;
	}
	beg = addr;
	end = addr + count * len;
	soff = beg & (~bmask);
	lcount = ((end + bmask) & (~bmask)) - soff;

	inval_bufs();

	buf = malloc(lcount);

	if (llseek(fd, soff, SEEK_SET) != soff) {
		(void) fprintf(stdout,
			gettext("Seek failed fd %x off %llx errno %x\n"),
			fd, soff, errno);
		goto end;
	}

	if (read(fd, buf, lcount) != lcount) {
		(void) fprintf(stdout,
			gettext("Read failed fd %x off %llx errno %x\n"),
			fd, soff, errno);
		goto end;
	}

	p = buf + (addr & bmask);
	while (count--) {
		(void) strncpy(p, pattern, len);
		p += len;
	}

	if (write(fd, buf, lcount) != lcount) {
		(void) fprintf(stdout,
			gettext("Write failed fd %x off %llx errno %x\n"),
			fd, soff, errno);
		goto end;
	}
end:
	free(buf);
}

void
dump_disk(uint32_t addr, uint32_t count, char *format)
{
	uint32_t beg, end, soff, lcount;
	int32_t len, prperline, n;
	uint8_t *buf, *p;
	uint16_t *p_16;
	uint32_t *p_32;

	if (strlen(format) != 1) {
		(void) fprintf(stdout,
			gettext("Invalid command\n"));
		return;
	}
	if (count == 0) {
		count = 1;
	}
	switch (*format) {
		case 'b' :
			/* FALLTHROUGH */
		case 'c' :
			/* FALLTHROUGH */
		case 'd' :
			/* FALLTHROUGH */
		case 'o' :
			len = 1;
			prperline = 16;
			break;
		case 'x' :
			len = 2;
			prperline = 8;
			break;
		case 'D' :
			/* FALLTHROUGH */
		case 'O' :
			/* FALLTHROUGH */
		case 'X' :
			len = 4;
			prperline = 4;
			break;
		default :
			(void) fprintf(stdout,
				gettext("Invalid format\n"));
			return;
	}

	beg = addr;
	end = addr + count * len;
	soff = beg & (~bmask);
	lcount = ((end + bmask) & (~bmask)) - soff;

	inval_bufs();

	buf = malloc(lcount);
	if (llseek(fd, soff, SEEK_SET) != soff) {
		(void) fprintf(stdout,
			gettext("Seek failed fd %x off %llx errno %x\n"),
			fd, soff, errno);
		goto end;
	}

	if (read(fd, buf, lcount) != lcount) {
		(void) fprintf(stdout,
			gettext("Read failed fd %x off %llx errno %x\n"),
			fd, soff, errno);
		goto end;
	}
	p = buf + (addr & bmask);
	/* LINTED */
	p_16 = (uint16_t *)p;
	/* LINTED */
	p_32 = (uint32_t *)p;
	n = 0;
	while (n < count) {
		switch (*format) {
			case 'b' :
				(void) fprintf(stdout,
					"%4x ", *((uint8_t *)p));
				break;
			case 'c' :
				(void) fprintf(stdout,
					"%4c ", *((uint8_t *)p));
				break;
			case 'd' :
				(void) fprintf(stdout,
					"%4d ", *((uint8_t *)p));
				break;
			case 'o' :
				(void) fprintf(stdout,
					"%4o ", *((uint8_t *)p));
				break;
			case 'x' :
				(void) fprintf(stdout,
					"%8x ", *p_16);
				break;
			case 'D' :
				(void) fprintf(stdout,
					"%16d ", *p_32);
				break;
			case 'O' :
				(void) fprintf(stdout,
					"%16o ", *p_32);
				break;
			case 'X' :
				(void) fprintf(stdout,
					"%16x ", *p_32);
				break;
		}
		p += len;
		n++;
		if ((n % prperline) == 0) {
			(void) fprintf(stdout, "\n");
		}
	}
	if (n % prperline) {
		(void) fprintf(stdout, "\n");
	}
end:
	free(buf);
}

void
find_it(char *dir, char *name, uint32_t in, uint32_t fl)
{
	uint8_t buf[1024], *addr;
	uint64_t off;
	struct file_id *fid;
	uint32_t iloc, d_in;
	uint8_t d_fl;
	struct recu_dir *rd, *erd, *temp;

	rd = erd = temp = NULL;

	if (inode_from_path(dir, &d_in, &d_fl) != 0) {
		(void) fprintf(stdout,
			gettext("Could not find directory %s"), dir);
		return;
	}

	if ((d_fl & FID_DIR) == 0) {
		(void) fprintf(stdout,
			gettext("Path %s is not a directory\n"), dir);
		return;
	}

	if (verify_inode(d_in << l2b, 4) == 0) {
		(void) fprintf(stdout,
			gettext("Inode is not a directory\n"));
		return;
	}

	off = 0;
	/* LINTED */
	fid = (struct file_id *)buf;
	while (get_fid(d_in, buf, off) == 0) {
		off += FID_LEN(fid);
		if ((fid->fid_flags & FID_DELETED) ||
			(fid->fid_flags & FID_PARENT)) {
			continue;
		}

		iloc = ud_xlate_to_daddr(udh, SWAP_16(fid->fid_icb.lad_ext_prn),
				SWAP_32(fid->fid_icb.lad_ext_loc));
		addr = &fid->fid_spec[SWAP_16((fid)->fid_iulen) + 1];
		if (((fl & 4) && (in == iloc)) ||
		((fl & 2) && (strcmp(name, (char *)addr) == 0))) {
			(void) printf("%s %x %s\n", dir, iloc, addr);
		}

		if (fid->fid_flags & FID_DIR) {
			temp = (struct recu_dir *)
				malloc(sizeof (struct recu_dir));
			if (temp == NULL) {
				(void) fprintf(stdout,
				gettext("Could not allocate memory\n"));
			} else {
				temp->next = NULL;
				temp->inode = iloc;
				temp->nm = malloc(strlen(dir) + 1 +
					fid->fid_idlen + 1);
				if (temp->nm != NULL) {
					(void) strcpy(temp->nm, dir);
					(void) strcat(temp->nm, "/");
					(void) strncat(temp->nm, (char *)addr,
						fid->fid_idlen);
				} else {
					(void) fprintf(stdout, gettext(
					"Could not allocate memory\n"));
				}
				if (rd == NULL) {
					erd = rd = temp;
				} else {
					erd->next = temp;
					erd = temp;
				}
			}
		}
	}

	while (rd != NULL) {
		if (rd->nm != NULL) {
			find_it(rd->nm, name, in, fl);
		}
		temp = rd;
		rd = rd->next;
		if (temp->nm) {
			free(temp->nm);
		}
		free(temp);
	}
}