1 /* vi:set cindent tabstop=2 shiftwidth=2: */
2 /*
3  * libvxfs - library for reading Veritas Journaled FileSystem (VxFS)
4  * Copyright (c) 1999 Martin Hinner <mhi@penguin.cz>
5  *
6  * block.c: VxFS block routines and UnixWare vtoc support
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 
29 #include <libvxfs.h>
30 
31 int vxfd;
32 int vxbsize;
33 long vxpartoffset;
34 
35 int
vxopendev(char * device)36 vxopendev (char *device)
37 {
38 	vxbsize = 1024;
39 
40 	vxfd = open (device, O_RDONLY);
41 	if (vxfd < 0)
42 		{
43 			perror (device);
44 			return -1;
45 		}
46 	return 0;
47 }
48 
49 void
vxclosedev(void)50 vxclosedev (void)
51 {
52 	if (vxfd >= 0)
53 		{
54 			close (vxfd);
55 			vxfd = -1;
56 			vxbsize = 0;
57 		}
58 }
59 
60 int
vxsetpart(int part)61 vxsetpart (int part)
62 {
63 	struct uw_vtoc *vtoc;
64 
65 	if (part >= V_NUMPAR)
66 		{
67 			if (vxverbose)
68 				fprintf (stderr, "vxsetpart: Invalid partition %u\n", part);
69 			return -1;
70 		}
71 
72 	vtoc = vxreaduwvtoc ();
73 	if (vtoc == NULL)
74 		{
75 
76 			/* No partition specified, silently ignore */
77 			if (part == 0)
78 				return 0;
79 
80 			if (vxverbose)
81 				fprintf (stderr, "vxsetpart: vxreaduwvtoc returned NULL\n");
82 			return -1;
83 		}
84 
85 	if (vtoc->part[part].tag == V_UNUSED)
86 		{
87 			if (vxverbose)
88 				fprintf (stderr, "vxsetpart: Attemp to mount unused partition\n");
89 			return -1;
90 		}
91 
92 	vxpartoffset = vtoc->part[part].start;
93 
94 	free (vtoc);
95 	return 0;
96 }
97 
98 
99 struct uw_vtoc *
vxreaduwvtoc(void)100 vxreaduwvtoc (void)
101 {
102 	struct uw_vtoc *vtoc;
103 	struct uw_disklabel *disklabel;
104 	struct uw_dospt *dospt;
105 	int i,npart;
106 
107 	dospt = (struct uw_dospt *) malloc(VTOC_BSIZE);
108 	if (!dospt) {
109 		if (vxverbose)
110 			fprintf(stderr,"vxreaduwvtoc: Not enough memory!\n");
111 		return 0;
112 	}
113 
114 	if (lseek (vxfd, 0 , SEEK_SET) < 0)
115 		{
116 			if (vxverbose)
117 				perror ("vxreaduwvtoc: lseek");
118 			return 0;
119 		}
120 
121 	if (read (vxfd, dospt, VTOC_BSIZE) != VTOC_BSIZE)
122 		{
123 			if (vxverbose)
124 				perror ("vxreaduwvtoc: read");
125 			return 0;
126 		}
127 
128 	npart = -1;
129 	for (i=0;i<4;i++) {
130 		if (dospt->part[i].sys_ind == VTOC_DOSTYPE)
131 			npart = i;
132 	}
133 
134 	if (npart == -1) {
135 		if (vxverbose)
136 			fprintf(stderr,"vxreaduwvtoc: No UnixWare partition found!\n");
137 		free(dospt);
138 		return 0;
139 	}
140 
141 	disklabel = (struct uw_disklabel *) malloc (VTOC_BSIZE);
142 	if (!disklabel)
143 		{
144 			if (vxverbose)
145 				fprintf (stderr, "vxreaduwvtoc: Not enough memory!\n");
146 			return 0;
147 		}
148 
149 	if (lseek (vxfd, VTOC_BSIZE *
150 				(dospt->part[npart].start_sect + VTOC_SECTOR), SEEK_SET) < 0)
151 		{
152 			if (vxverbose)
153 				perror ("vxreaduwvtoc: lseek");
154 			return 0;
155 		}
156 
157 	if (read (vxfd, disklabel, VTOC_BSIZE) != VTOC_BSIZE)
158 		{
159 			if (vxverbose)
160 				perror ("vxreaduwvtoc: read");
161 			return 0;
162 		}
163 
164 	if (disklabel->vtoc.sanity != VTOC_SANE)
165 		{
166 			free (disklabel);
167 			return 0;
168 		}
169 
170 	vtoc = (struct uw_vtoc*)malloc(sizeof(struct uw_vtoc));
171 	if (vtoc == NULL) {
172 			if (vxverbose)
173 				fprintf (stderr, "vxreaduwvtoc: Not enough memory!\n");
174 			return 0;
175 	}
176 
177 	memcpy(vtoc, &disklabel->vtoc, sizeof(struct uw_vtoc));
178 
179 	free(disklabel);
180 
181 	return vtoc;
182 }
183 
184 void *
vxread(int blknum,int count)185 vxread (int blknum, int count)
186 {
187 	void *blk;
188 
189 	blk = malloc (vxbsize * count);
190 	if (!blk)
191 		{
192 			if (vxverbose)
193 				fprintf (stderr, "vxread(%u): Not enough memory!\n", blknum);
194 			return 0;
195 		}
196 
197 	if (lseek (vxfd, (VTOC_BSIZE * vxpartoffset) +
198 						 (blknum * vxbsize), SEEK_SET) < 0)
199 		{
200 			if (vxverbose)
201 				perror ("vxread: lseek");
202 			return 0;
203 		}
204 	if (read (vxfd, blk, vxbsize * count) != vxbsize * count)
205 		{
206 			if (vxverbose)
207 				perror ("vxread: read");
208 			return 0;
209 		}
210 	return blk;
211 }
212