summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdiskmgt/common/media.c
blob: 853ac8c69aec9562a0400f6c80be168875af9b6e (plain)
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
/*
 * 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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <fcntl.h>
#include <libdevinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stropts.h>
#include <sys/dkio.h>
#include <sys/sunddi.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/vtoc.h>
#include <sys/efi_partition.h>

#include "libdiskmgt.h"
#include "disks_private.h"
#include "partition.h"

#define	IOCTLRETRIES		2
#define	IOCTLRETRYINTERVAL	1

static descriptor_t	**apply_filter(descriptor_t **media, int filter[],
			    int *errp);
static int		get_attrs(disk_t *dp, int fd, nvlist_t *attrs);
static int		get_rmm_name(disk_t *dp, char *mname, int size);
static int		get_media_type(uint_t media_type);
static int		desc_ok(descriptor_t *dp);

/*
 * This function gets the descriptors we are associated with.
 */
descriptor_t **
media_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
    int *errp)
{
	if (!desc_ok(desc)) {
		*errp = ENODEV;
		return (NULL);
	}

	switch (type) {
	case DM_DRIVE:
		return (drive_get_assocs(desc, errp));
	case DM_PARTITION:
		return (partition_get_assocs(desc, errp));
	case DM_SLICE:
		return (slice_get_assocs(desc, errp));
	}

	*errp = EINVAL;
	return (NULL);
}

/*
 * Get the media descriptors for the given drive/partition/slice.
 */
descriptor_t **
media_get_assocs(descriptor_t *dp, int *errp)
{
	descriptor_t	**media;
	char		mname[MAXPATHLEN];

	if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
		/*
		 * For drives, this means no media but slice/part.
		 * require media.
		 */
		if (dp->type == DM_DRIVE) {
			return (libdiskmgt_empty_desc_array(errp));
		} else {
			*errp = ENODEV;
			return (NULL);
		}
	}

	/* make the snapshot */
	media = (descriptor_t **)calloc(2, sizeof (descriptor_t *));
	if (media == NULL) {
		*errp = ENOMEM;
		return (NULL);
	}

	media[0] = cache_get_desc(DM_MEDIA, dp->p.disk, mname, NULL, errp);
	if (*errp != 0) {
		free(media);
		return (NULL);
	}
	media[1] = NULL;

	*errp = 0;
	return (media);
}

nvlist_t *
media_get_attributes(descriptor_t *dp, int *errp)
{
	nvlist_t	*attrs = NULL;
	int		fd;

	if (!desc_ok(dp)) {
		*errp = ENODEV;
		return (NULL);
	}

	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
		*errp = ENOMEM;
		return (NULL);
	}

	fd = drive_open_disk(dp->p.disk, NULL, 0);

	if ((*errp = get_attrs(dp->p.disk, fd, attrs)) != 0) {
		nvlist_free(attrs);
		attrs = NULL;
	}

	if (fd >= 0) {
		(void) close(fd);
	}

	return (attrs);
}

descriptor_t *
media_get_descriptor_by_name(char *name, int *errp)
{
	descriptor_t	**media;
	int		i;
	descriptor_t	*medium = NULL;

	media = cache_get_descriptors(DM_MEDIA, errp);
	if (*errp != 0) {
		return (NULL);
	}

	for (i = 0; media[i]; i++) {
		if (libdiskmgt_str_eq(name, media[i]->name)) {
			medium = media[i];
		} else {
			/* clean up the unused descriptors */
			cache_free_descriptor(media[i]);
		}
	}
	free(media);

	if (medium == NULL) {
		*errp = ENODEV;
	}

	return (medium);
}

descriptor_t **
media_get_descriptors(int filter[], int *errp)
{
	descriptor_t	**media;

	media = cache_get_descriptors(DM_MEDIA, errp);
	if (*errp != 0) {
		return (NULL);
	}

	if (filter != NULL && filter[0] != DM_FILTER_END) {
		descriptor_t	**found;

		found = apply_filter(media, filter, errp);
		if (*errp != 0) {
			media = NULL;
		} else {
			media = found;
		}
	}

	return (media);
}

char *
media_get_name(descriptor_t *desc)
{
	return (desc->name);
}

/* ARGSUSED */
nvlist_t *
media_get_stats(descriptor_t *dp, int stat_type, int *errp)
{
	/* There are no stat types defined for media */
	*errp = EINVAL;
	return (NULL);
}

int
media_make_descriptors()
{
	int		error;
	disk_t		*dp;
	char		mname[MAXPATHLEN];

	dp = cache_get_disklist();
	while (dp != NULL) {
		if (media_read_name(dp, mname, sizeof (mname))) {
			cache_load_desc(DM_MEDIA, dp, mname, NULL, &error);
			if (error != 0) {
				return (error);
			}
		}

		dp = dp->next;
	}

	return (0);
}

/*
 * Read the media information.
 */
int
media_read_info(int fd, struct dk_minfo *minfo)
{
	int	status;
	int	tries = 0;

	minfo->dki_media_type = 0;

	/*
	 * This ioctl can fail if the media is not loaded or spun up.
	 * Retrying can sometimes succeed since the first ioctl will have
	 * started the media before the ioctl timed out so the media may be
	 * spun up on the subsequent attempt.
	 */
	while ((status = ioctl(fd, DKIOCGMEDIAINFO, minfo)) < 0) {
		tries++;
		if (tries >= IOCTLRETRIES) {
			break;
		}
		(void) sleep(IOCTLRETRYINTERVAL);
	}

	if (status < 0) {
		return (0);
	}

	return (1);
}

/* return 1 if there is media, 0 if not. */
int
media_read_name(disk_t *dp, char *mname, int size)
{
	mname[0] = 0;

	if (!dp->removable) {
		/* not removable, so media name is devid */
		if (dp->device_id != NULL) {
			(void) strlcpy(mname, dp->device_id, size);
		}
		return (1);
	}

	/* This is a removable media drive. */
	return (get_rmm_name(dp, mname, size));
}

static descriptor_t **
apply_filter(descriptor_t **media, int filter[], int *errp)
{
	descriptor_t	**found;
	int		i;
	int		cnt = 0;
	int		pos;

	/* count the number of media in the snapshot */
	for (i = 0; media[i]; i++) {
		cnt++;
	}

	found = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
	if (found == NULL) {
		*errp = ENOMEM;
		cache_free_descriptors(media);
		return (NULL);
	}

	pos = 0;
	for (i = 0; media[i]; i++) {
		int	fd;
		struct	dk_minfo minfo;

		if ((fd = drive_open_disk(media[i]->p.disk, NULL, 0)) < 0) {
			continue;
		}

		if (media_read_info(fd, &minfo)) {
			int	mtype;
			int	j;
			int	match;

			mtype = get_media_type(minfo.dki_media_type);

			match = 0;
			for (j = 0; filter[j] != DM_FILTER_END; j++) {
				if (mtype == filter[j]) {
					found[pos++] = media[i];
					match = 1;
					break;
				}
			}

			if (!match) {
				cache_free_descriptor(media[i]);
			}
		}
		(void) close(fd);
	}
	found[pos] = NULL;
	free(media);

	*errp = 0;
	return (found);
}

/* return 1 if the media descriptor is still valid, 0 if not. */
static int
desc_ok(descriptor_t *dp)
{
	/* First verify the media name for removable media */
	if (dp->p.disk->removable) {
		char	mname[MAXPATHLEN];

		if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
			return (0);
		}

		if (mname[0] == 0) {
			return (libdiskmgt_str_eq(dp->name, NULL));
		} else {
			return (libdiskmgt_str_eq(dp->name, mname));
		}
	}

	return (1);
}

static int
get_attrs(disk_t *dp, int fd, nvlist_t *attrs)
{
	struct	dk_minfo minfo;
	struct	dk_geom	geometry;

	if (fd < 0) {
		return (ENODEV);
	}

	bzero(&minfo, sizeof (struct dk_minfo));

	/* The first thing to do is read the media */
	if (!media_read_info(fd, &minfo)) {
		return (ENODEV);
	}

	if (partition_has_fdisk(dp, fd)) {
		if (nvlist_add_boolean(attrs, DM_FDISK) != 0) {
			return (ENOMEM);
		}
	}

	if (dp->removable) {
		if (nvlist_add_boolean(attrs, DM_REMOVABLE) != 0) {
			return (ENOMEM);
		}

		if (nvlist_add_boolean(attrs, DM_LOADED) != 0) {
			return (ENOMEM);
		}
	}

	if (nvlist_add_uint64(attrs, DM_SIZE, minfo.dki_capacity) != 0) {
		return (ENOMEM);
	}

	if (nvlist_add_uint32(attrs, DM_BLOCKSIZE, minfo.dki_lbsize) != 0) {
		return (ENOMEM);
	}

	if (nvlist_add_uint32(attrs, DM_MTYPE,
	    get_media_type(minfo.dki_media_type)) != 0) {
		return (ENOMEM);
	}

	/* only for disks < 1TB  and x86 */
#if defined(i386) || defined(__amd64)
	if (ioctl(fd, DKIOCG_PHYGEOM, &geometry) >= 0) {
#else
	/* sparc call */
	if (ioctl(fd, DKIOCGGEOM, &geometry) >= 0) {
#endif
		struct extvtoc	vtoc;

		if (nvlist_add_uint64(attrs, DM_START, 0) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
		    geometry.dkg_ncyl * geometry.dkg_nhead * geometry.dkg_nsect)
		    != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NCYLINDERS, geometry.dkg_ncyl)
		    != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NPHYSCYLINDERS,
		    geometry.dkg_pcyl) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NALTCYLINDERS,
		    geometry.dkg_acyl) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NHEADS,
		    geometry.dkg_nhead) != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NSECTORS, geometry.dkg_nsect)
		    != 0) {
			return (ENOMEM);
		}
		if (nvlist_add_uint32(attrs, DM_NACTUALCYLINDERS,
		    geometry.dkg_ncyl) != 0) {
			return (ENOMEM);
		}

		if (read_extvtoc(fd, &vtoc) >= 0 && vtoc.v_volume[0] != 0) {
			char	label[LEN_DKL_VVOL + 1];

			(void) snprintf(label, sizeof (label), "%.*s",
			    LEN_DKL_VVOL, vtoc.v_volume);
			if (nvlist_add_string(attrs, DM_LABEL, label) != 0) {
				return (ENOMEM);
			}
		}

	} else {
		/* check for disks > 1TB for accessible size */
		struct dk_gpt	*efip;

		if (efi_alloc_and_read(fd, &efip) >= 0) {
			diskaddr_t	p8size = 0;

			if (nvlist_add_boolean(attrs, DM_EFI) != 0) {
				return (ENOMEM);
			}
			if (nvlist_add_uint64(attrs, DM_START,
			    efip->efi_first_u_lba) != 0) {
				return (ENOMEM);
			}
			/* partition 8 is reserved on EFI labels */
			if (efip->efi_nparts >= 9) {
				p8size = efip->efi_parts[8].p_size;
			}
			if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
			    (efip->efi_last_u_lba - p8size) -
			    efip->efi_first_u_lba) != 0) {
				efi_free(efip);
				return (ENOMEM);
			}
			efi_free(efip);
		}
	}
	return (0);
}

static int
get_media_type(uint_t media_type)
{
	switch (media_type) {
	case DK_UNKNOWN:
		return (DM_MT_UNKNOWN);
	case DK_MO_ERASABLE:
		return (DM_MT_MO_ERASABLE);
	case DK_MO_WRITEONCE:
		return (DM_MT_MO_WRITEONCE);
	case DK_AS_MO:
		return (DM_MT_AS_MO);
	case DK_CDROM:
		return (DM_MT_CDROM);
	case DK_CDR:
		return (DM_MT_CDR);
	case DK_CDRW:
		return (DM_MT_CDRW);
	case DK_DVDROM:
		return (DM_MT_DVDROM);
	case DK_DVDR:
		return (DM_MT_DVDR);
	case DK_DVDRAM:
		return (DM_MT_DVDRAM);
	case DK_FIXED_DISK:
		return (DM_MT_FIXED);
	case DK_FLOPPY:
		return (DM_MT_FLOPPY);
	case DK_ZIP:
		return (DM_MT_ZIP);
	case DK_JAZ:
		return (DM_MT_JAZ);
	default:
		return (DM_MT_UNKNOWN);
	}
}

/*
 * This function handles removable media.
 */
static int
get_rmm_name(disk_t *dp, char *mname, int size)
{
	int		loaded;
	int		fd;

	loaded = 0;

	if ((fd = drive_open_disk(dp, NULL, 0)) >= 0) {
		struct dk_minfo minfo;

		if ((loaded = media_read_info(fd, &minfo))) {
			struct extvtoc vtoc;

			if (read_extvtoc(fd, &vtoc) >= 0) {
				if (vtoc.v_volume[0] != NULL) {
					if (LEN_DKL_VVOL < size) {
						(void) strlcpy(mname,
						    vtoc.v_volume,
						    LEN_DKL_VVOL);
					} else {
						(void) strlcpy(mname,
						    vtoc.v_volume, size);
					}
				}
			}
		}

		(void) close(fd);
	}

	return (loaded);
}