File: geometry.c

package info (click to toggle)
vmelilo 1.5.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 432 kB
  • ctags: 919
  • sloc: ansic: 8,210; makefile: 157; asm: 122; perl: 18
file content (376 lines) | stat: -rw-r--r-- 7,394 bytes parent folder | download | duplicates (3)
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
/*
 *	VME Linux/m68k Loader
 *
 *	(c) Copyright 1997 by Nick Holgate
 *
 *	This file is subject to the terms and conditions of the GNU General Public
 *	License.  See the file COPYING for more details.
 */

/*--------------------------------------------------------------------------*/

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>

#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#define _LINUX_STAT_H	/* prevent inclusion of <linux/stat.h> from
						 * <linux/fs.h> redefintions cause lots of warnings
						 */

#include <linux/hdreg.h>
#include <linux/fs.h>
#include <linux/major.h>
 

#include "vmelilo.h"

extern int stat(const char *file, struct stat *buf);

/*--------------------------------------------------------------------------*/
/* SCSI disk major definitions taken from kernel 2.2.8 linux/major.h
 */

#ifndef SCSI_DISK0_MAJOR

#define SCSI_DISK0_MAJOR		8
#define SCSI_DISK1_MAJOR        65
#define SCSI_DISK2_MAJOR        66
#define SCSI_DISK3_MAJOR        67
#define SCSI_DISK4_MAJOR        68
#define SCSI_DISK5_MAJOR        69
#define SCSI_DISK6_MAJOR        70
#define SCSI_DISK7_MAJOR        71

#undef  SCSI_DISK_MAJOR			/* discard old 2.0 definition	*/

#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))

#endif

/*--------------------------------------------------------------------------*/

int
is_scsi_partition
(	unsigned	rdev
)
{
 	return SCSI_DISK_MAJOR(MAJOR(rdev)) && (MINOR(rdev) & 0x0f);
}

/*--------------------------------------------------------------------------*/
/* Get the Geometry for the Partition a File belongs to
 */

static
void
geometry
(	const char			*name,
	dev_t				*device,
	u_long				*start
)
{	struct hd_geometry	geo;
	char				devname[PATH_MAX+1];
	struct stat			info;
	DIR					*dir;
	const struct dirent	*dirent;
	int					fh;
	int					found = FALSE;

	if (stat(name, &info) == -1)
	{
		error_stat(name);
	}

	if (!S_ISREG(info.st_mode))
	{
		die("%s is not a regular file\n", name);
	}
	*device = info.st_dev;

	if (!(dir = opendir("/dev")))
	{
		error_opendir("/dev");
	}

	while ((dirent = readdir(dir)))
	{
		sprintf(devname, "/dev/%s", dirent->d_name);

		if ((stat(devname, &info) != -1)
		&&	S_ISBLK(info.st_mode)
		&&	(info.st_rdev == *device))
		{
			if (MAJOR(*device) == LOOP_MAJOR)
			{
				geo.start = 0;
			}
			else
			{
				if ((fh = open(devname, O_RDONLY)) == -1)
				{
					error_open(devname);
				}
	
				if (ioctl(fh, HDIO_GETGEO, &geo) == -1)
				{
					error_ioctl(devname, "HDIO_GETGEO");
				}
	
				close(fh);
			}
			found = TRUE;
			break;
		}
	}

	closedir(dir);

	if (!found)
	{
		die("Can't find special device entry for file `%s'\n", name);
	}

	*start = geo.start;
}

/*--------------------------------------------------------------------------*/
/* Create a Map for a File
 */

const
FILEMAP *
create_file_map
(	const char	*name
)
{	int			fh;
	int			size;
	int			blk;
	int			extend;
	dev_t		device;
	u_long		numfrags;
	u_long		maxblks;
	u_long		offset;
	u_long		start;
	u_long		blksize;
	u_long		sectsper;
	FILEMAP		*map;

	/* open file */
	if ((name == NULL) || ((fh = open(name, O_RDONLY)) == -1))
	{
		return NULL;
	}

	/* get file size */
	if ((size = lseek(fh, 0, SEEK_END)) == -1)
	{
		error_seek(name);
	}

	/* get geometry of files partition */
	geometry(name, &device, &start);

	/* make sure file is on boot device */
	if (MAJOR(device) != MAJOR(boot_device))
	{
		die("File `%s' must reside on physical device `%s'\n",
				name, config.boot_device_name);
	}

	switch (MAJOR(boot_device))
	{
		case LOOP_MAJOR:
		{
			if (MINOR(device) != MINOR(boot_device))
			{
				die("File `%s' must reside on loopback filesystem\n"
					"associated with `%s'\n",
						name, config.boot_device_name);
			}
			break;
		}

		case RAMDISK_MAJOR:
		{
			if (MINOR(device) != MINOR(boot_device))
			{
				die("File `%s' must reside on ramdisk `%s'\n",
						name, config.boot_device_name);
			}
			break;
		}

		case SCSI_DISK0_MAJOR:
		case SCSI_DISK1_MAJOR:
		case SCSI_DISK2_MAJOR:
		case SCSI_DISK3_MAJOR:
		case SCSI_DISK4_MAJOR:
		case SCSI_DISK5_MAJOR:
		case SCSI_DISK6_MAJOR:
		case SCSI_DISK7_MAJOR:
		{
			if ((MINOR(device) & 0xf0) != (MINOR(boot_device) & 0xf0))
			{
				die("File `%s' must reside on physical SCSI device `%s'\n",
						name, config.boot_device_name);
			}
			break;
		}

		default:
		{
			die("Major device %d not supported (yet)\n", (int) MAJOR(device));
			break;
		}
	}

	/* get file system block size */
	if (ioctl(fh, FIGETBSZ, &blksize) == -1)
	{
		error_ioctl(name, "FIGETBSZ");
	}

	/* check that file system block size is divisible by sector size */
	if (blksize % SECTOR_SIZE)
	{
		die("File system block size (%lu) of file '%s'\n"
			"is not divisible by %d\n",
				blksize, name, SECTOR_SIZE);
	}

	/* get number of sectors per filesystem block */
	sectsper = blksize / SECTOR_SIZE;

	/* get number of file system blocks */
	maxblks = (size + blksize - 1) / blksize;

	/* allocate for worst case */
	if ((map = malloc((maxblks + 1) * sizeof(FILEMAP))) == NULL)
	{
		error_nomemory();
	}

	/* reset number of loader fragments */
	numfrags = 0;

	/* for each file system block */
	for (blk = 0; blk < maxblks; blk++)
	{
		/* convert block offset in file to block offset in partition */
		offset = blk;
		if (ioctl(fh, FIBMAP, &offset) == -1)
		{
			error_ioctl(name, "FIBMAP");
		}

		/* if not a hole */
		if (offset)
		{
			/* convert partition block offset to disk sector offset */
			offset = start + (offset * sectsper);
		}

		/* say create a new fragment */
		extend = FALSE;

		/* if some fragments already exist */
		if (numfrags != 0)
		{
			/* not a hole */
			if (offset)
			{
				/* if fragment starts at end of last fragment */
				if (offset == (map[numfrags].offset + map[numfrags].length))
				{
					/* say extend old fragment */
					extend = TRUE;
				}
			}

			/* a hole */
			else
			{
				/* if last fragment was a hole as well */
				if (map[numfrags].offset == 0)
				{
					/* say extend old fragment */
					extend = TRUE;
				}
			}
		}

		if (extend)
		{
			/* extend old fragment by one filesystem block */
			map[numfrags].length += sectsper;
		}
		else
		{
			/* new fragment */
			numfrags++;
			map[numfrags].offset = offset;
			map[numfrags].length = sectsper;
		}
	}

	/* record file size and number of fragments in first entry */
	MAP_FILESIZE(map) = size;
	MAP_NUMFRAGS(map) = numfrags;

	/* finish with file */
	close(fh);

	/* downsize filemap */
	if (numfrags != maxblks)
	{
		if ((map = realloc(map, (numfrags + 1) * sizeof(FILEMAP))) == NULL)
		{
			error_nomemory();
		}
	}

	return map;
}

/*--------------------------------------------------------------------------*/
/* Create the File Name for the Boot Block Backup
 */

const char *
create_backup_filename
(	void
)
{	int		size;
	char	*s;

	size = strlen(LILO_BACKUPFILEPREFIX) + 7;

	if (root_path)
	{
		size += strlen(root_path);
	}

	if ((s = malloc(size)) == NULL)
	{
		error_nomemory();
	}

	sprintf(s, "%s%s.%02x.%02x",
		root_path ? root_path : "", LILO_BACKUPFILEPREFIX,
			(int)MAJOR(boot_device), (int)MINOR(boot_device));

	return s;
}

/*-----------------------------< end of file >------------------------------*/