xref: /illumos-gate/usr/src/lib/libdiskmgt/common/libdiskmgt.h (revision 5f10ef697f250374b7b917e10961c4e02d4e3112)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26  * Copyright 2016 Nexenta Systems, Inc.
27  */
28 
29 #ifndef _LIBDISKMGT_H
30 #define	_LIBDISKMGT_H
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <libnvpair.h>
37 #include <sys/swap.h>
38 
39 
40 /*
41  * Disk Management Library
42  *
43  * This library provides a common way to gather information about a system's
44  * disks, controllers, and related components.
45  *
46  *
47  * THREADS
48  * -------
49  *
50  * In general all of the functions are thread safe, however there are some
51  * specific considerations for getting events.  The dm_get_event function may
52  * block the calling thread if no event is currently available.  If another
53  * thread calls dm_get_event while a thread is already blocked in this function,
54  * the second thread will also block.  When an event arrives and multiple
55  * threads are waiting for events, it is undefined which thread will be
56  * unblocked and receive the event.  If a callback is used for handling events,
57  * this is equivalent to the dm_get_event function, so mixing callbacks and
58  * dm_get_event is also nondeterministic.
59  *
60  *
61  * ERRORS
62  * ------
63  *
64  * In general all of the functions take an errno pointer.  This is an integer
65  * that will contain 0 if the function succeeded or contains an errno (see
66  * errno.h) if there was an error.  If the function returns some data, that
67  * return data will generally be null if an error occured (see the API comment
68  * for the specific function for details).  Many of the functions take a
69  * descriptor and provide more information for that descriptor.  These functions
70  * may return an error if the object was removed between the call which obtained
71  * the descriptor and the call to get more information about the object (errno
72  * will be ENODEV).  Only a few of the possible errno values will be returned;
73  * typically:
74  *     EPERM       not super-user
75  *     ENOMEM      not enough memory
76  *     ENODEV      no such device
77  *     EINVAL      invalid argument
78  *     ENOENT      no event queue has been created
79  *
80  * Many of the functions require the application to be running as root in order
81  * to get complete information.  EPERM will be returned if the application is
82  * not running as root.  However, not all of the functions have this requirement
83  * (i.e. event handling).
84  *
85  * It is possible for the system to run out of memory while receiving events.
86  * Since event receipt is asyncronous from the dm_get_event call there may not
87  * be a thread waiting when the event occurs and ENOMEM is detected.  In this
88  * case the event will be lost.  The first call to dm_get_event following this
89  * condition will immediately return ENOMEM, even if events are queued.
90  * Subsequent calls can return events.  The dm_get_event call will clear the
91  * pending ENOMEM condition.  There is no way to know how many events were lost
92  * when this situation occurs.  If a thread is waiting when the event arrives
93  * and the ENOMEM condition occurs, the call will also return with ENOMEM.
94  * There is no way to determine if the system ran out of memory before the
95  * dm_get_event call or while the thread was blocked in the dm_get_event call
96  * since both conditions cause dm_get_event to return ENOMEM.
97  *
98  *
99  * MEMORY MANAGEMENT
100  * -----------------
101  *
102  * Most of the functions that return data are returning memory that has been
103  * allocated and must be freed by the application when no longer needed.  The
104  * application should call the proper free function to free the memory.  Most of
105  * the functions return either a nvlist or an array of descriptors.  The normal
106  * nvlist function (nvlist_free; see libnvpair(3LIB)) can be used to free the
107  * simple nvlists.  Other functions are provided to free the more complex data
108  * structures.
109  *
110  * The following list shows the functions that return allocated memory and the
111  * corresponding function to free the memory:
112  *     dm_get_descriptors            dm_free_descriptors
113  *     dm_get_associated_descriptors dm_free_descriptors
114  *     dm_get_descriptor_by_name     dm_free_descriptor
115  *     dm_get_name                   dm_free_name
116  *     dm_get_attributes             nvlist_free
117  *     dm_get_stats	          nvlist_free
118  *     dm_get_event                  nvlist_free
119  *
120  *
121  * EVENTS
122  * ------
123  *
124  * Event information is returned as a nvlist.  It may be possible to return more
125  * information about events over time, especially information about what has
126  * changed.  However, that may not always be the case, so by using an nvlist we
127  * have a very generic event indication.  At a minimum the event will return the
128  * name of the device, the type of device (see dm_desc_type_t) and the type of
129  * event.  The event type is a string which can currently be; add, remove,
130  * change.
131  *
132  * If a drive goes up or down this could be returned as event type "change".
133  * The application could get the drive information to see that the "status"
134  * attribute has changed value (ideally the event would include an attribute
135  * with the name of the changed attribute as the value).  Although the API can
136  * return events for all drive related changes, events will not necessarily be
137  * delivered for all changes unless the system generates those events.
138  *
139  *
140  * Controller/HBAs
141  * ---------------
142  *
143  * In general the API means "the parent node of the drive in the device tree"
144  * where the word "controller" is used.  This can actually be either the HBA or
145  * the drive controller depending on the type of the drive.
146  *
147  * Drives can be connected to their controller(s) in three different ways:
148  *     single controller
149  *     multiple controllers
150  *     multiple controllers with mpxio
151  * These cases will lead to different information being available for the
152  * configuration.  The two interesting cases are multi-path with and without
153  * mpxio.  With mpxio the drive will have a unique name and a single controller
154  * (scsi_vhci).  The physical controllers, the paths to the drive, can be
155  * obtained by calling dm_get_associated_descriptors with a drive descriptor and
156  * a type of DM_PATH.  This will only return these physical paths when MPXIO, or
157  * possibly some future similar feature, is controlling the drive.
158  *
159  * Without mpxio the drive does not have a unique public name (in all cases the
160  * alias(es) of the drive can be determined by calling
161  * dm_get_associated_descriptors to get the DM_ALIAS descriptors.  There will be
162  * more than one controller returned from dm_get_associated_descriptors when
163  * called with a type of DM_CONTROLLER.  The controllers for each of the aliases
164  * will be returned in the same order as the aliases descriptors.  For example,
165  * a drive with two paths has the aliases c5t3d2 and c7t1d0.  There will be two
166  * controllers returned; the first corresponds to c5 and the second corresponds
167  * to c7.
168  *
169  * In the multi-path, non-mpxio case the drive has more than one alias.
170  * Although most of the drive attributes are represented on the drive (see
171  * dm_get_attributes) there can be some different attributes for the different
172  * aliases for the drive.  Use dm_get_associated_descriptors to get the DM_ALIAS
173  * descriptors which can then be used to obtain these attributes.  Use of this
174  * algorithm is not restricted to the multi-path, non-mpxio case.  For example,
175  * it can be used to get the target/lun for a SCSI drive with a single path.
176  */
177 
178 /*
179  * Holds all the data regarding the device.
180  * Private to libdiskmgt. Must use dm_xxx functions to set/get data.
181  */
182 typedef uint64_t  dm_descriptor_t;
183 
184 typedef enum {
185 	DM_WHO_MKFS = 0,
186 	DM_WHO_ZPOOL,
187 	DM_WHO_ZPOOL_FORCE,
188 	DM_WHO_FORMAT,
189 	DM_WHO_SWAP,
190 	DM_WHO_DUMP,
191 	DM_WHO_ZPOOL_SPARE
192 } dm_who_type_t;
193 
194 /*
195  * The API uses a "descriptor" to identify the managed objects such as drives,
196  * controllers, media, slices, partitions, paths and buses.  The descriptors are
197  * opaque and are only returned or used as parameters to the other functions in
198  * the API.  The descriptor definition is a typedef to dm_descriptor_t.
199  *
200  * Applications call either the dm_get_descriptors or
201  * dm_get_associated_descriptors function to obtain a list of descriptors of a
202  * specific type.  The application specifies the desired type from the following
203  * enumeration:
204  */
205 typedef enum {
206     DM_DRIVE = 0,
207     DM_CONTROLLER,
208     DM_MEDIA,
209     DM_SLICE,
210     DM_PARTITION,
211     DM_PATH,
212     DM_ALIAS,
213     DM_BUS
214 } dm_desc_type_t;
215 
216 /*
217  * These descriptors are associated with each other in the following way:
218  *
219  *                      alias                 partition
220  *     _                    \                /   |
221  *    / \                    \              /    |
222  *    \ /                     \            /     |
223  *    bus --- controller --- drive --- media     |
224  *                     |      /            \     |
225  *                     |     /              \    |
226  *                     |    /                \   |
227  *                      path                  slice
228  *
229  * The dm_get_associated_descriptors function can be used get the descriptors
230  * associated with a given descriptor.  The dm_get_associated_types function can
231  * be used to find the types that can be associated with a given type.
232  *
233  * The attributes and values for these objects are described using a list of
234  * name/value pairs (see libnvpair(3LIB) and the specific comments for each
235  * function in the API section of this document).
236  *
237  * Drives and media have a type which are defined as the following enumerations.
238  * There could be additional types added to these enumerations as new drive and
239  * media types are supported by the system.
240  */
241 
242 typedef enum {
243     DM_DT_UNKNOWN = 0,
244     DM_DT_FIXED,
245     DM_DT_ZIP,
246     DM_DT_JAZ,
247     DM_DT_FLOPPY,
248     DM_DT_MO_ERASABLE,
249     DM_DT_MO_WRITEONCE,
250     DM_DT_AS_MO,
251     DM_DT_CDROM,
252     DM_DT_CDR,
253     DM_DT_CDRW,
254     DM_DT_DVDROM,
255     DM_DT_DVDR,
256     DM_DT_DVDRAM,
257     DM_DT_DVDRW,
258     DM_DT_DDCDROM,
259     DM_DT_DDCDR,
260     DM_DT_DDCDRW
261 } dm_drive_type_t;
262 
263 typedef enum {
264     DM_MT_UNKNOWN = 0,
265     DM_MT_FIXED,
266     DM_MT_FLOPPY,
267     DM_MT_CDROM,
268     DM_MT_ZIP,
269     DM_MT_JAZ,
270     DM_MT_CDR,
271     DM_MT_CDRW,
272     DM_MT_DVDROM,
273     DM_MT_DVDR,
274     DM_MT_DVDRAM,
275     DM_MT_MO_ERASABLE,
276     DM_MT_MO_WRITEONCE,
277     DM_MT_AS_MO
278 } dm_media_type_t;
279 
280 #define	DM_FILTER_END	-1
281 
282 /*
283  * The dm_get_stats function takes a stat_type argument for the specific sample
284  * to get for the descriptor.  The following enums specify the drive and slice
285  * stat types.
286  */
287 /* drive stat name */
288 typedef enum {
289     DM_DRV_STAT_PERFORMANCE = 0,
290     DM_DRV_STAT_DIAGNOSTIC,
291     DM_DRV_STAT_TEMPERATURE
292 } dm_drive_stat_t;
293 
294 /* slice stat name */
295 typedef enum {
296     DM_SLICE_STAT_USE = 0
297 } dm_slice_stat_t;
298 
299 /* partition type */
300 typedef enum {
301 	DM_PRIMARY = 0,
302 	DM_EXTENDED,
303 	DM_LOGICAL
304 } dm_partition_type_t;
305 
306 /* attribute definitions */
307 
308 /* drive */
309 #define	DM_DISK_UP		1
310 #define	DM_DISK_DOWN		0
311 
312 #define	DM_CLUSTERED		"clustered"
313 #define	DM_DRVTYPE		"drvtype"
314 #define	DM_FAILING		"failing"
315 #define	DM_LOADED		"loaded"	/* also in media */
316 #define	DM_NDNRERRS		"ndevice_not_ready_errors"
317 #define	DM_NBYTESREAD		"nbytes_read"
318 #define	DM_NBYTESWRITTEN	"nbytes_written"
319 #define	DM_NHARDERRS		"nhard_errors"
320 #define	DM_NILLREQERRS		"nillegal_req_errors"
321 #define	DM_NMEDIAERRS		"nmedia_errors"
322 #define	DM_NNODEVERRS		"nno_dev_errors"
323 #define	DM_NREADOPS		"nread_ops"
324 #define	DM_NRECOVERRS		"nrecoverable_errors"
325 #define	DM_NSOFTERRS		"nsoft_errors"
326 #define	DM_NTRANSERRS		"ntransport_errors"
327 #define	DM_NWRITEOPS		"nwrite_ops"
328 #define	DM_OPATH		"opath"
329 #define	DM_PRODUCT_ID		"product_id"
330 #define	DM_REMOVABLE		"removable"	/* also in media */
331 #define	DM_RPM			"rpm"
332 #define	DM_SOLIDSTATE		"solid_state"
333 #define	DM_STATUS		"status"
334 #define	DM_SYNC_SPEED		"sync_speed"
335 #define	DM_TEMPERATURE		"temperature"
336 #define	DM_VENDOR_ID		"vendor_id"
337 #define	DM_WIDE			"wide"		/* also on controller */
338 #define	DM_WWN			"wwn"
339 
340 /* bus */
341 #define	DM_BTYPE		"btype"
342 #define	DM_CLOCK		"clock"		/* also on controller */
343 #define	DM_PNAME		"pname"
344 
345 /* controller */
346 #define	DM_FAST			"fast"
347 #define	DM_FAST20		"fast20"
348 #define	DM_FAST40		"fast40"
349 #define	DM_FAST80		"fast80"
350 #define	DM_MULTIPLEX		"multiplex"
351 #define	DM_PATH_STATE		"path_state"
352 
353 #define	DM_CTYPE_ATA		"ata"
354 #define	DM_CTYPE_SCSI		"scsi"
355 #define	DM_CTYPE_FIBRE		"fibre channel"
356 #define	DM_CTYPE_USB		"usb"
357 #define	DM_CTYPE_UNKNOWN	"unknown"
358 
359 /* media */
360 #define	DM_BLOCKSIZE		"blocksize"
361 #define	DM_FDISK		"fdisk"
362 #define	DM_MTYPE		"mtype"
363 #define	DM_NACTUALCYLINDERS	"nactual_cylinders"
364 #define	DM_NALTCYLINDERS	"nalt_cylinders"
365 #define	DM_NCYLINDERS		"ncylinders"
366 #define	DM_NHEADS		"nheads"
367 #define	DM_NPHYSCYLINDERS	"nphys_cylinders"
368 #define	DM_NSECTORS		"nsectors"	/* also in partition */
369 #define	DM_SIZE			"size"		/* also in slice */
370 #define	DM_NACCESSIBLE		"naccessible"
371 #define	DM_LABEL		"label"
372 
373 /* partition */
374 #define	DM_BCYL			"bcyl"
375 #define	DM_BHEAD		"bhead"
376 #define	DM_BOOTID		"bootid"
377 #define	DM_BSECT		"bsect"
378 #define	DM_ECYL			"ecyl"
379 #define	DM_EHEAD		"ehead"
380 #define	DM_ESECT		"esect"
381 #define	DM_PTYPE		"ptype" /* this references the partition id */
382 #define	DM_PARTITION_TYPE	"part_type" /* primary, extended, logical */
383 #define	DM_RELSECT		"relsect"
384 
385 /* slice */
386 #define	DM_DEVICEID		"deviceid"
387 #define	DM_DEVT			"devt"
388 #define	DM_INDEX		"index"
389 #define	DM_EFI_NAME		"name"
390 #define	DM_MOUNTPOINT		"mountpoint"
391 #define	DM_LOCALNAME		"localname"
392 #define	DM_START		"start"
393 #define	DM_TAG			"tag"
394 #define	DM_FLAG			"flag"
395 #define	DM_EFI			"efi"	/* also on media */
396 #define	DM_USED_BY		"used_by"
397 #define	DM_USED_NAME		"used_name"
398 #define	DM_USE_MOUNT		"mount"
399 #define	DM_USE_LU		"lu"
400 #define	DM_USE_DUMP		"dump"
401 #define	DM_USE_VXVM		"vxvm"
402 #define	DM_USE_FS		"fs"
403 #define	DM_USE_VFSTAB		"vfstab"
404 #define	DM_USE_EXPORTED_ZPOOL	"exported_zpool"
405 #define	DM_USE_ACTIVE_ZPOOL	"active_zpool"
406 #define	DM_USE_SPARE_ZPOOL	"spare_zpool"
407 #define	DM_USE_L2CACHE_ZPOOL	"l2cache_zpool"
408 
409 /* event */
410 #define	DM_EV_NAME		"name"
411 #define	DM_EV_DTYPE		"edtype"
412 #define	DM_EV_TYPE		"evtype"
413 #define	DM_EV_TADD		"add"
414 #define	DM_EV_TREMOVE		"remove"
415 #define	DM_EV_TCHANGE		"change"
416 
417 /* findisks */
418 #define	DM_CTYPE		"ctype"
419 #define	DM_LUN			"lun"
420 #define	DM_TARGET		"target"
421 
422 #define	NOINUSE_SET	getenv("NOINUSE_CHECK") != NULL
423 
424 void			dm_free_descriptors(dm_descriptor_t *desc_list);
425 void			dm_free_descriptor(dm_descriptor_t desc);
426 void			dm_free_name(char *name);
427 void			dm_free_swapentries(swaptbl_t *);
428 
429 dm_descriptor_t		*dm_get_descriptors(dm_desc_type_t type, int filter[],
430 			    int *errp);
431 dm_descriptor_t		*dm_get_associated_descriptors(dm_descriptor_t desc,
432 			    dm_desc_type_t type, int *errp);
433 dm_desc_type_t		*dm_get_associated_types(dm_desc_type_t type);
434 dm_descriptor_t		dm_get_descriptor_by_name(dm_desc_type_t desc_type,
435 			    char *name, int *errp);
436 char			*dm_get_name(dm_descriptor_t desc, int *errp);
437 dm_desc_type_t		dm_get_type(dm_descriptor_t desc);
438 nvlist_t		*dm_get_attributes(dm_descriptor_t desc, int *errp);
439 nvlist_t		*dm_get_stats(dm_descriptor_t desc, int stat_type,
440 			    int *errp);
441 void			dm_init_event_queue(void(*callback)(nvlist_t *, int),
442 			    int *errp);
443 nvlist_t		*dm_get_event(int *errp);
444 void			dm_get_slices(char *drive, dm_descriptor_t **slices,
445 			    int *errp);
446 void			dm_get_slice_stats(char *slice, nvlist_t **dev_stats,
447 			    int *errp);
448 int			dm_get_swapentries(swaptbl_t **, int *);
449 void			dm_get_usage_string(char *who, char *data, char **msg);
450 int			dm_inuse(char *dev_name, char **msg, dm_who_type_t who,
451 			    int *errp);
452 int			dm_inuse_swap(const char *dev_name, int *errp);
453 int			dm_isoverlapping(char *dev_name, char **msg, int *errp);
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
459 #endif /* _LIBDISKMGT_H */
460