1.\" $OpenBSD: disklabel.9,v 1.13 2008/06/26 05:42:08 ray Exp $ 2.\" $NetBSD: disklabel.9,v 1.7 1999/03/06 22:09:29 mycroft Exp $ 3.\" 4.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Paul Kranenburg. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd $Mdocdate: June 26 2008 $ 32.Dt DISKLABEL 9 33.Os 34.Sh NAME 35.Nm disklabel , 36.Nm readdisklabel , 37.Nm writedisklabel , 38.Nm setdisklabel , 39.Nm bounds_check_with_label 40.Nd disk label management routines 41.Sh SYNOPSIS 42.Fd #include <sys/param.h> 43.Fd #include <sys/disklabel.h> 44.Ft char * 45.Fn readdisklabel "dev_t dev" "void (*strat)(struct buf *)" "struct disklabel *lp" "int spoofonly" 46.Ft int 47.Fn writedisklabel "dev_t dev" "void (*strat)(struct buf *)" "struct disklabel *lp" 48.Ft int 49.Fn setdisklabel "struct disklabel *olp" "struct disklabel *nlp" "u_int openmask" 50.Ft int 51.Fn bounds_check_with_label "struct buf *bp" "struct disklabel *lp" "int wlabel" 52.Sh DESCRIPTION 53This collection of routines provides a disklabel management interface to 54kernel device drivers. 55These routines are classified as machine- or architecture-dependent because 56of restrictions imposed by the machine architecture and boot-strapping code 57on the location of the label, or because cooperation with other operating 58systems requires specialized conversion code. 59.Pp 60.Fn readdisklabel 61attempts to read a disklabel from the device identified by 62.Fa dev , 63using the device strategy routine passed in 64.Fa strat . 65Note that a buffer structure is required to pass to the strategy routine; 66it needs to be acquired and parametrized for the intended I/O operation, 67and disposed of when the operation has completed. 68Some fields in the disklabel passed in 69.Fa lp 70may be pre-initialized by the caller in order to meet device driver 71requirements for the I/O operation initiated to get to the disklabel data 72on the medium. 73In particular, the field 74.Dq d_secsize , 75if non-zero, is used by 76.Fn readdisklabel 77to get an appropriately sized buffer to pass to the device strategy routine. 78Unspecified fields in 79.Fa lp 80should be set to zero. 81If the medium does not contain a native disklabel that can be read in directly 82or 83.Fa spoofonly 84argument is a true value, 85If a disk label can not be found or constructed, a string containing 86an approximated description of the failure mode is returned. 87Otherwise the 88.Dv NULL 89string is returned. 90.Pp 91.Fn writedisklabel 92stores disk label information contained in the disk label structure given by 93.Fa lp 94on the device identified by 95.Fa dev . 96Like 97.Fn readdisklabel , 98it acquires and sets up an I/O buffer to pass to the strategy routine 99.Fa strat . 100.Fn writedisklabel 101returns 0 on success and 102.Dv EINVAL 103if the disk label specifies invalid or unconvertible values. 104Otherwise, any error condition reported by the device strategy routine 105in the buffer's 106.Dq Va b_error 107field is returned. 108.Pp 109.Fn setdisklabel 110checks a proposed new disk label passed in 111.Fa nlp 112for some amount of basic sanity. 113This includes a check on attempts to 114change the location, or reduce the size, of an existing disk partition 115that is currently in use by the system. 116The current disposition of the disk partitions is made available through 117.Fa olp 118and 119.Fa openmask , 120which provide, respectively, the existing disk label and a bit mask 121identifying the partitions that are currently in use. 122Failure to pass on 123.Dq basic sanity , 124results in a 125.Dv EINVAL 126return value, while a vetoed update of the partition layout is signalled by a 127.Dv EBUSY 128return value. 129Otherwise, 0 is returned. 130.Pp 131.Fn bounds_check_with_label 132is used to check whether a device transfer described by 133.Fa bp 134to the device identified by 135.Fa dev , 136is properly contained within a disk partition of the disk with label 137.Fa lp . 138If this check fails, 139.Fn bounds_check_with_label 140sets the buffer's 141.Dq Va b_error 142field to 143.Dv EINVAL 144and sets the 145.Dv B_ERROR 146flag in 147.Dq Va b_flags . 148If the argument 149.Fa wlabel 150is zero, and the transfer is a write operation, a check is done if the transfer 151would overwrite 152.Pq a portion of 153the disklabel area on the medium. 154If that is the case, 155.Dv EROFS 156is set in 157.Dq Va b_error 158and the 159.Dv B_ERROR 160flag is set in 161.Dq Va b_flags . 162Note that 163.Fa wlabel 164should be set to a non-zero value if the intended operation is expected to 165install or update the disk label. 166Programs that intend to do so using the raw device interface should notify 167the driver by using a 168.Dv DIOCWLABEL 169ioctl function. 170A zero value is returned if any of the bound checks failed or 171transfer was attempted exactly at the end of disk partition. 172Otherwise the value of 1 is returned. 173.Sh SEE ALSO 174.Xr disklabel 5 , 175.Xr disklabel 8 , 176.Xr fdisk 8 177