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.
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.
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]
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
39 #pragma ident "%Z%%M% %I% %E% SMI"
42 * Label a file system volume.
49 #include <sys/param.h>
50 #include <sys/types.h>
51 #include <sys/mntent.h>
54 #define bcopy(f, t, n) (void) memcpy(t, f, n)
55 #define bzero(s, n) memset(s, 0, n)
56 #define bcmp(s, d, n) memcmp(s, d, n)
58 #define index(s, r) strchr(s, r)
59 #define rindex(s, r) strrchr(s, r)
61 #include <sys/vnode.h>
63 #include <sys/fs/ufs_inode.h>
64 #include <sys/fs/ufs_fs.h>
67 static void label(char *, char *, char *);
74 #define sblock sb_un.sblk
75 #define altsblock altsb_un.sblk
81 main(int argc
, char *argv
[])
88 while ((opt
= getopt(argc
, argv
, "o:")) != EOF
) {
91 case 'o': /* specific options (none defined yet) */
98 if (optind
> (argc
- 1)) {
102 argv
= &argv
[optind
];
106 if (strlen(fsname
) > 6) {
107 (void) fprintf(stderr
, gettext("labelit: "));
108 (void) fprintf(stderr
,
109 gettext("fsname can not be longer than 6 characters\n"));
115 if (strlen(volume
) > 6) {
116 (void) fprintf(stderr
, gettext("labelit: "));
117 (void) fprintf(stderr
,
118 gettext("volume can not be longer than 6 characters\n"));
122 label(special
, fsname
, volume
);
130 (void) fprintf(stderr
, gettext(
131 "ufs usage: labelit [-F ufs] [gen opts] special [fsname volume]\n"));
136 label(char *special
, char *fsname
, char *volume
)
143 struct fs
*fsp
, *altfsp
;
145 if (fsname
== NULL
) {
146 f
= open64(special
, O_RDONLY
);
148 f
= open64(special
, O_RDWR
);
151 (void) fprintf(stderr
, gettext("labelit: "));
155 if (llseek(f
, (offset_t
)SBLOCK
* DEV_BSIZE
, 0) < 0) {
156 (void) fprintf(stderr
, gettext("labelit: "));
160 if (read(f
, &sblock
, SBSIZE
) != SBSIZE
) {
161 (void) fprintf(stderr
, gettext("labelit: "));
165 if ((sblock
.fs_magic
!= FS_MAGIC
) &&
166 (sblock
.fs_magic
!= MTB_UFS_MAGIC
)) {
167 (void) fprintf(stderr
, gettext("labelit: "));
168 (void) fprintf(stderr
,
169 gettext("bad super block magic number\n"));
172 if ((sblock
.fs_magic
== FS_MAGIC
) &&
173 ((sblock
.fs_version
!= UFS_EFISTYLE4NONEFI_VERSION_2
) &&
174 (sblock
.fs_version
!= UFS_VERSION_MIN
))) {
175 (void) fprintf(stderr
, gettext("labelit: "));
176 (void) fprintf(stderr
,
177 gettext("unrecognized UFS format version: %d\n"),
181 if ((sblock
.fs_magic
== MTB_UFS_MAGIC
) &&
182 ((sblock
.fs_version
> MTB_UFS_VERSION_1
) ||
183 (sblock
.fs_version
< MTB_UFS_VERSION_MIN
))) {
184 (void) fprintf(stderr
, gettext("labelit: "));
185 (void) fprintf(stderr
,
186 gettext("unrecognized UFS format version: %d\n"),
193 * Is block layout available?
196 if (sblock
.fs_cpc
<= 0 && (fsname
|| volume
)) {
197 (void) fprintf(stderr
, gettext("labelit: "));
198 (void) fprintf(stderr
,
199 gettext("insufficient superblock space for file system label\n"));
204 * calculate the available blocks for each rotational position
206 blk
= sblock
.fs_spc
* sblock
.fs_cpc
/ NSPF(&sblock
);
207 for (i
= 0; i
< blk
; i
+= sblock
.fs_frag
)
210 blk
= i
/ sblock
.fs_frag
;
211 p
= (char *)&(fs_rotbl(fsp
)[blk
]);
213 if (fsname
!= NULL
) {
214 for (i
= 0; i
< 14; i
++)
216 for (i
= 0; (i
< 6) && (fsname
[i
]); i
++, p
++)
220 if (volume
!= NULL
) {
221 for (i
= 0; (i
< 6) && (volume
[i
]); i
++, p
++)
224 if (fsname
!= NULL
) {
225 if (llseek(f
, (offset_t
)SBLOCK
* DEV_BSIZE
, 0) < 0) {
226 (void) fprintf(stderr
, gettext("labelit: "));
230 if (write(f
, &sblock
, SBSIZE
) != SBSIZE
) {
231 (void) fprintf(stderr
, gettext("labelit: "));
235 for (i
= 0; i
< sblock
.fs_ncg
; i
++) {
237 * In the case of multi-terabyte ufs file
238 * systems, only the first ten and last ten
239 * cylinder groups have copies of the superblock.
241 if (sblock
.fs_magic
== MTB_UFS_MAGIC
&&
242 sblock
.fs_ncg
> 20 &&
243 (i
>= 10 && i
< sblock
.fs_ncg
- 10))
246 (offset_t
)cgsblock(&sblock
, i
) * sblock
.fs_fsize
;
247 if (llseek(f
, offset
, 0) < 0) {
248 (void) fprintf(stderr
, gettext("labelit: "));
253 if (read(f
, &altsblock
, SBSIZE
) != SBSIZE
) {
254 (void) fprintf(stderr
, gettext("labelit: "));
258 if ((altsblock
.fs_magic
!= FS_MAGIC
) &&
259 (altsblock
.fs_magic
!= MTB_UFS_MAGIC
)) {
260 (void) fprintf(stderr
, gettext("labelit: "));
261 (void) fprintf(stderr
,
262 gettext("bad alternate super block(%i) magic number\n"), i
);
265 if ((altsblock
.fs_magic
== FS_MAGIC
) &&
266 ((altsblock
.fs_version
!=
267 UFS_EFISTYLE4NONEFI_VERSION_2
) &&
268 (altsblock
.fs_version
!= UFS_VERSION_MIN
))) {
269 (void) fprintf(stderr
, gettext("labelit: "));
270 (void) fprintf(stderr
,
271 gettext("bad alternate super block UFS format version: %d\n"),
272 altsblock
.fs_version
);
275 if ((altsblock
.fs_magic
== MTB_UFS_MAGIC
) &&
276 ((altsblock
.fs_version
> MTB_UFS_VERSION_1
) ||
277 (altsblock
.fs_version
< MTB_UFS_VERSION_MIN
))) {
278 (void) fprintf(stderr
, gettext("labelit: "));
279 (void) fprintf(stderr
,
280 gettext("bad alternate super block UFS format version: %d\n"),
281 altsblock
.fs_version
);
284 bcopy((char *)&(fs_rotbl(fsp
)[blk
]),
285 (char *)&(fs_rotbl(altfsp
)[blk
]), 14);
287 if (llseek(f
, offset
, 0) < 0) {
288 (void) fprintf(stderr
, gettext("labelit: "));
292 if (write(f
, &altsblock
, SBSIZE
) != SBSIZE
) {
293 (void) fprintf(stderr
, gettext("labelit: "));
299 p
= (char *)&(fs_rotbl(fsp
)[blk
]);
300 (void) fprintf(stderr
, gettext("fsname: "));
301 for (i
= 0; (i
< 6) && (*p
); i
++, p
++) {
302 (void) fprintf(stderr
, "%c", *p
);
304 (void) fprintf(stderr
, "\n");
305 (void) fprintf(stderr
, gettext("volume: "));
307 for (i
= 0; (i
< 6); i
++, p
++) {
308 (void) fprintf(stderr
, "%c", *p
);
310 (void) fprintf(stderr
, "\n");