xref: /titanic_44/usr/src/cmd/fs.d/cachefs/fsck/dlog_ck.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1996-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <stddef.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
34*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
35*7c478bd9Sstevel@tonic-gate #include <unistd.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_dlog.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /* forward references */
42*7c478bd9Sstevel@tonic-gate static int create_mapfile(char *fname, int size);
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate int
dlog_ck(char * dir_path,ino64_t * maxlocalfilenop)45*7c478bd9Sstevel@tonic-gate dlog_ck(char *dir_path, ino64_t *maxlocalfilenop)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	int err;
48*7c478bd9Sstevel@tonic-gate 	int n;
49*7c478bd9Sstevel@tonic-gate 	char dlog_path[MAXPATHLEN];
50*7c478bd9Sstevel@tonic-gate 	char dmap_path[MAXPATHLEN];
51*7c478bd9Sstevel@tonic-gate 	struct stat64 statinfo;
52*7c478bd9Sstevel@tonic-gate 	int fd;
53*7c478bd9Sstevel@tonic-gate 	int dlog_version;
54*7c478bd9Sstevel@tonic-gate 	off_t offset;
55*7c478bd9Sstevel@tonic-gate 	struct cfs_dlog_entry buf;
56*7c478bd9Sstevel@tonic-gate 	int max_seq_num;
57*7c478bd9Sstevel@tonic-gate 	int ent_count = 0;
58*7c478bd9Sstevel@tonic-gate 	ino64_t fileno, maxlocalfileno;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if (maxlocalfilenop)
61*7c478bd9Sstevel@tonic-gate 		*maxlocalfilenop = 0LL;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	n = strlen(dir_path) + strlen(CACHEFS_DLOG_FILE) + 2;
64*7c478bd9Sstevel@tonic-gate 	if (n > MAXPATHLEN) {
65*7c478bd9Sstevel@tonic-gate 		pr_err(gettext("%s/%s: path too long"),
66*7c478bd9Sstevel@tonic-gate 		    dir_path, CACHEFS_DLOG_FILE);
67*7c478bd9Sstevel@tonic-gate 		return (-1);
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	sprintf(dlog_path, "%s/%s", dir_path, CACHEFS_DLOG_FILE);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	n = strlen(dir_path) + strlen(CACHEFS_DMAP_FILE) + 2;
72*7c478bd9Sstevel@tonic-gate 	if (n > MAXPATHLEN) {
73*7c478bd9Sstevel@tonic-gate 		pr_err(gettext("%s/%s: path too long"),
74*7c478bd9Sstevel@tonic-gate 		    dir_path, CACHEFS_DMAP_FILE);
75*7c478bd9Sstevel@tonic-gate 		return (-1);
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 	sprintf(dmap_path, "%s/%s", dir_path, CACHEFS_DMAP_FILE);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	err = lstat64(dlog_path, &statinfo);
80*7c478bd9Sstevel@tonic-gate 	if (err < 0) {
81*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
82*7c478bd9Sstevel@tonic-gate 			(void) unlink(dmap_path);
83*7c478bd9Sstevel@tonic-gate 		/*
84*7c478bd9Sstevel@tonic-gate 		 * No disconnect log(dlog) file exists to check
85*7c478bd9Sstevel@tonic-gate 		 */
86*7c478bd9Sstevel@tonic-gate 		return (0);
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	/* this file will be <2GB */
90*7c478bd9Sstevel@tonic-gate 	fd = open(dlog_path, O_RDWR);
91*7c478bd9Sstevel@tonic-gate 	if (fd < 0) {
92*7c478bd9Sstevel@tonic-gate 		pr_err(gettext("can't open %s"), dlog_path);
93*7c478bd9Sstevel@tonic-gate 		return (-2);
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 	err = read(fd, &dlog_version, sizeof (dlog_version));
96*7c478bd9Sstevel@tonic-gate 	if (err != sizeof (dlog_version)) {
97*7c478bd9Sstevel@tonic-gate 		pr_err(gettext("can't read %s"), dlog_path);
98*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
99*7c478bd9Sstevel@tonic-gate 		return (-3);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	if (dlog_version != CFS_DLOG_VERSION) {
102*7c478bd9Sstevel@tonic-gate 		pr_err(gettext(
103*7c478bd9Sstevel@tonic-gate 		    "unknown version number in %s"), dlog_path);
104*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
105*7c478bd9Sstevel@tonic-gate 		return (-4);
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	offset = sizeof (dlog_version);
109*7c478bd9Sstevel@tonic-gate 	max_seq_num = 0;
110*7c478bd9Sstevel@tonic-gate 	maxlocalfileno = 0LL;
111*7c478bd9Sstevel@tonic-gate 	while (offset < (off_t)statinfo.st_size) {
112*7c478bd9Sstevel@tonic-gate 		err = (int) lseek(fd, offset, SEEK_SET);
113*7c478bd9Sstevel@tonic-gate 		if (err == -1) {
114*7c478bd9Sstevel@tonic-gate 			pr_err(gettext("can't lseek %s"), dlog_path);
115*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
116*7c478bd9Sstevel@tonic-gate 			return (-5);
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		err = read(fd, &buf, sizeof (buf));
120*7c478bd9Sstevel@tonic-gate 		if (err < 0) {
121*7c478bd9Sstevel@tonic-gate 			pr_err(gettext("can't read %s"), dlog_path);
122*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
123*7c478bd9Sstevel@tonic-gate 			return (-6);
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 		++ent_count;
126*7c478bd9Sstevel@tonic-gate 		if (buf.dl_op ==  CFS_DLOG_TRAILER) {
127*7c478bd9Sstevel@tonic-gate 			goto out;
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 		if ((buf.dl_len & 3) == 0) {
130*7c478bd9Sstevel@tonic-gate 			/*
131*7c478bd9Sstevel@tonic-gate 			 * Record length must be on a word boundary and
132*7c478bd9Sstevel@tonic-gate 			 * fit into the correct size range.
133*7c478bd9Sstevel@tonic-gate 			 */
134*7c478bd9Sstevel@tonic-gate 			if ((buf.dl_len < sizeof (int)) ||
135*7c478bd9Sstevel@tonic-gate 			    (buf.dl_len > CFS_DLOG_ENTRY_MAXSIZE)) {
136*7c478bd9Sstevel@tonic-gate 				goto out;
137*7c478bd9Sstevel@tonic-gate 			}
138*7c478bd9Sstevel@tonic-gate 			/*
139*7c478bd9Sstevel@tonic-gate 			 * Make sure length does not point beyond end of
140*7c478bd9Sstevel@tonic-gate 			 * file
141*7c478bd9Sstevel@tonic-gate 			 */
142*7c478bd9Sstevel@tonic-gate 			if ((offset + (off_t)buf.dl_len) >
143*7c478bd9Sstevel@tonic-gate 			    (off_t)statinfo.st_size) {
144*7c478bd9Sstevel@tonic-gate 				goto out;
145*7c478bd9Sstevel@tonic-gate 			}
146*7c478bd9Sstevel@tonic-gate 		} else {
147*7c478bd9Sstevel@tonic-gate 			goto out;
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		/* make sure the valid field is reasonable */
151*7c478bd9Sstevel@tonic-gate 		switch (buf.dl_valid) {
152*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_VAL_CRASH:
153*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_VAL_COMMITTED:
154*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_VAL_ERROR:
155*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_VAL_PROCESSED:
156*7c478bd9Sstevel@tonic-gate 			break;
157*7c478bd9Sstevel@tonic-gate 		default:
158*7c478bd9Sstevel@tonic-gate 			goto out;
159*7c478bd9Sstevel@tonic-gate 		}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		/* make sure the operation field is reasonable */
162*7c478bd9Sstevel@tonic-gate 		fileno = 0LL;
163*7c478bd9Sstevel@tonic-gate 		switch (buf.dl_op) {
164*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_CREATE:
165*7c478bd9Sstevel@tonic-gate 			fileno = buf.dl_u.dl_create.dl_new_cid.cid_fileno;
166*7c478bd9Sstevel@tonic-gate 			break;
167*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_REMOVE:
168*7c478bd9Sstevel@tonic-gate 			break;
169*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_LINK:
170*7c478bd9Sstevel@tonic-gate 			break;
171*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_RENAME:
172*7c478bd9Sstevel@tonic-gate 			break;
173*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_MKDIR:
174*7c478bd9Sstevel@tonic-gate 			fileno = buf.dl_u.dl_mkdir.dl_child_cid.cid_fileno;
175*7c478bd9Sstevel@tonic-gate 			break;
176*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_RMDIR:
177*7c478bd9Sstevel@tonic-gate 			break;
178*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_SYMLINK:
179*7c478bd9Sstevel@tonic-gate 			fileno = buf.dl_u.dl_symlink.dl_child_cid.cid_fileno;
180*7c478bd9Sstevel@tonic-gate 			break;
181*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_SETATTR:
182*7c478bd9Sstevel@tonic-gate 			break;
183*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_SETSECATTR:
184*7c478bd9Sstevel@tonic-gate 			break;
185*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_MODIFIED:
186*7c478bd9Sstevel@tonic-gate 			break;
187*7c478bd9Sstevel@tonic-gate 		case CFS_DLOG_MAPFID:
188*7c478bd9Sstevel@tonic-gate 			break;
189*7c478bd9Sstevel@tonic-gate 		default:
190*7c478bd9Sstevel@tonic-gate 			goto out;
191*7c478bd9Sstevel@tonic-gate 		}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 		/* track the largest local fileno used */
194*7c478bd9Sstevel@tonic-gate 		if (maxlocalfileno < fileno)
195*7c478bd9Sstevel@tonic-gate 			maxlocalfileno = fileno;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 		/* track the largest sequence number used */
198*7c478bd9Sstevel@tonic-gate 		if (max_seq_num < buf.dl_seq) {
199*7c478bd9Sstevel@tonic-gate 			max_seq_num = buf.dl_seq;
200*7c478bd9Sstevel@tonic-gate 		}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 		offset += buf.dl_len;
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate out:
206*7c478bd9Sstevel@tonic-gate 	if ((buf.dl_op != CFS_DLOG_TRAILER) ||
207*7c478bd9Sstevel@tonic-gate 	    (buf.dl_len != sizeof (struct cfs_dlog_trailer)) ||
208*7c478bd9Sstevel@tonic-gate 	    (buf.dl_valid != CFS_DLOG_VAL_COMMITTED) ||
209*7c478bd9Sstevel@tonic-gate 	    ((offset + (off_t)buf.dl_len) != (off_t)statinfo.st_size)) {
210*7c478bd9Sstevel@tonic-gate 		ftruncate(fd, offset);
211*7c478bd9Sstevel@tonic-gate 		buf.dl_len = sizeof (struct cfs_dlog_trailer);
212*7c478bd9Sstevel@tonic-gate 		buf.dl_op = CFS_DLOG_TRAILER;
213*7c478bd9Sstevel@tonic-gate 		buf.dl_valid = CFS_DLOG_VAL_COMMITTED;
214*7c478bd9Sstevel@tonic-gate 		buf.dl_seq = max_seq_num + 1;
215*7c478bd9Sstevel@tonic-gate 		if (wrdlog(fd, &buf,  buf.dl_len, offset) != 0) {
216*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
217*7c478bd9Sstevel@tonic-gate 			return (-7);
218*7c478bd9Sstevel@tonic-gate 		}
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	if (fsync(fd) == -1) {
222*7c478bd9Sstevel@tonic-gate 		pr_err(gettext("Cannot sync %s"), dlog_path);
223*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
224*7c478bd9Sstevel@tonic-gate 		return (-8);
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 	(void) close(fd); /* ignore return since fsync() successful */
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	/* check to see that mapfile exists; if not, create it. */
229*7c478bd9Sstevel@tonic-gate 	if (access(dmap_path, F_OK) != 0) {
230*7c478bd9Sstevel@tonic-gate 		/* XXX ent_count is a very high upper bound */
231*7c478bd9Sstevel@tonic-gate 		if (create_mapfile(dmap_path,
232*7c478bd9Sstevel@tonic-gate 		    ent_count * sizeof (struct cfs_dlog_mapping_space)) != 0) {
233*7c478bd9Sstevel@tonic-gate 			return (-9);
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	if (maxlocalfilenop)
238*7c478bd9Sstevel@tonic-gate 		*maxlocalfilenop = maxlocalfileno;
239*7c478bd9Sstevel@tonic-gate 	return (0);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate int
wrdlog(int fd,char * buf,int len,off_t offset)243*7c478bd9Sstevel@tonic-gate wrdlog(int fd, char * buf, int len, off_t offset)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	int err;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	err = lseek(fd, offset, SEEK_SET);
248*7c478bd9Sstevel@tonic-gate 	if (err < 0) {
249*7c478bd9Sstevel@tonic-gate 		return (-1);
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	err = write(fd, buf, len);
253*7c478bd9Sstevel@tonic-gate 	if (err != len) {
254*7c478bd9Sstevel@tonic-gate 		return (-2);
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	return (0);
258*7c478bd9Sstevel@tonic-gate }
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate static int
create_mapfile(char * fname,int size)261*7c478bd9Sstevel@tonic-gate create_mapfile(char *fname, int size)
262*7c478bd9Sstevel@tonic-gate {
263*7c478bd9Sstevel@tonic-gate 	char buffy[BUFSIZ];
264*7c478bd9Sstevel@tonic-gate 	int fd, rc, wsize;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	/* this file will be <2GB */
267*7c478bd9Sstevel@tonic-gate 	fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
268*7c478bd9Sstevel@tonic-gate 	if (fd < 0)
269*7c478bd9Sstevel@tonic-gate 		return (errno);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	memset(buffy, '\0', sizeof (buffy));
272*7c478bd9Sstevel@tonic-gate 	while (size > 0) {
273*7c478bd9Sstevel@tonic-gate 		wsize = (size > sizeof (buffy)) ? sizeof (buffy) : size;
274*7c478bd9Sstevel@tonic-gate 		if (write(fd, buffy, wsize) != wsize) {
275*7c478bd9Sstevel@tonic-gate 			rc = errno;
276*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
277*7c478bd9Sstevel@tonic-gate 			(void) unlink(fname);
278*7c478bd9Sstevel@tonic-gate 			return (rc);
279*7c478bd9Sstevel@tonic-gate 		}
280*7c478bd9Sstevel@tonic-gate 		size -= wsize;
281*7c478bd9Sstevel@tonic-gate 	}
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	if (fsync(fd) != 0) {
284*7c478bd9Sstevel@tonic-gate 		rc = errno;
285*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
286*7c478bd9Sstevel@tonic-gate 		(void) unlink(fname);
287*7c478bd9Sstevel@tonic-gate 		return (rc);
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	return (0);
292*7c478bd9Sstevel@tonic-gate }
293