xref: /titanic_41/usr/src/lib/lvm/libmeta/common/meta_devstamp.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1993-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * get timestamp from device
31  */
32 
33 #include <meta.h>
34 
35 /*
36  * get timestamp
37  */
38 int
39 getdevstamp(
40 	mddrivename_t	*dnp,
41 	time_t		*stamp,		/* return timestamp here */
42 	md_error_t	*ep
43 )
44 {
45 	int		fd;
46 	int		partno;
47 	struct vtoc	vtocbuf;
48 	mdname_t	*np;
49 
50 	if ((np = metaslicename(dnp, MD_SLICE0, ep)) == NULL)
51 		return (-1);
52 
53 	/* open given device */
54 	if ((fd = open(np->rname, O_RDONLY | O_NDELAY, 0)) < 0)
55 		return (mdsyserror(ep, errno, np->cname));
56 
57 	/* re-read vtoc */
58 	if (meta_getvtoc(fd, np->cname, &vtocbuf, &partno, ep) == -1) {
59 		(void) close(fd);
60 		return (-1);
61 	}
62 
63 	/* close device */
64 	(void) close(fd);	/* sd/ssd bug */
65 
66 	/* return timestamp, success */
67 	*stamp = vtocbuf.timestamp[partno];
68 	return (0);
69 }
70 
71 /*
72  * returns
73  *	0 on success,
74  * 	ENOTSUP if it's not a device with a vtoc
75  *	-1 on failure
76  */
77 int
78 setdevstamp(
79 	mddrivename_t	*dnp,
80 	time_t		*stamp,		/* returned timestamp */
81 	md_error_t	*ep
82 )
83 {
84 	int		fd;
85 	int		partno;
86 	struct vtoc	vtocbuf;
87 	time_t		now = time(NULL);
88 	mdname_t	*np;
89 
90 	if ((np = metaslicename(dnp, MD_SLICE0, ep)) == NULL)
91 		return (-1);
92 
93 	/* open for vtoc */
94 	if ((fd = open(np->rname, O_RDWR | O_NDELAY, 0)) < 0)
95 		return (mdsyserror(ep, errno, np->cname));
96 
97 	if (meta_getvtoc(fd, np->cname, &vtocbuf, &partno, ep) == -1) {
98 		(void) close(fd);
99 		if (partno == VT_ENOTSUP)
100 			return (ENOTSUP);
101 		else
102 			return (-1);
103 	}
104 
105 	*stamp = vtocbuf.timestamp[partno] = now;
106 
107 	if (meta_setvtoc(fd, np->cname, &vtocbuf, ep) == -1) {
108 		(void) close(fd);
109 		return (-1);
110 	}
111 
112 	/* Clear the timestamp */
113 	vtocbuf.timestamp[partno] = 0;
114 
115 	if (meta_getvtoc(fd, np->cname, &vtocbuf, &partno, ep) == -1) {
116 		(void) close(fd);
117 		return (-1);
118 	}
119 
120 	(void) close(fd);	/* sd/ssd bug */
121 
122 	if (*stamp != vtocbuf.timestamp[partno])
123 		return (mddeverror(ep, MDE_CANTVERIFY_VTOC, NODEV64,
124 				    np->cname));
125 
126 	return (0);
127 }
128