xref: /titanic_51/usr/src/uts/common/cpr/cpr_misc.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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 2004 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 #include <sys/types.h>
30 #include <sys/errno.h>
31 #include <sys/cpuvar.h>
32 #include <sys/vfs.h>
33 #include <sys/vnode.h>
34 #include <sys/pathname.h>
35 #include <sys/callb.h>
36 #include <sys/fs/ufs_inode.h>
37 #include <vm/anon.h>
38 #include <sys/fs/swapnode.h>	/* for swapfs_minfree */
39 #include <sys/kmem.h>
40 #include <sys/cpr.h>
41 #include <sys/conf.h>
42 
43 /*
44  * CPR miscellaneous support routines
45  */
46 #define	cpr_open(path, mode,  vpp)	(vn_open(path, UIO_SYSSPACE, \
47 		mode, 0600, vpp, CRCREAT, 0))
48 #define	cpr_rdwr(rw, vp, basep, cnt)	(vn_rdwr(rw, vp,  (caddr_t)(basep), \
49 		cnt, 0LL, UIO_SYSSPACE, 0, (rlim64_t)MAXOFF_T, CRED(), \
50 		(ssize_t *)NULL))
51 
52 extern void clkset(time_t);
53 extern cpu_t *i_cpr_bootcpu(void);
54 extern caddr_t i_cpr_map_setup(void);
55 extern void i_cpr_free_memory_resources(void);
56 
57 extern kmutex_t cpr_slock;
58 extern size_t cpr_buf_size;
59 extern char *cpr_buf;
60 extern size_t cpr_pagedata_size;
61 extern char *cpr_pagedata;
62 extern int cpr_bufs_allocated;
63 extern int cpr_bitmaps_allocated;
64 
65 static struct cprconfig cprconfig;
66 static int cprconfig_loaded = 0;
67 static int cpr_statefile_ok(vnode_t *, int);
68 static int cpr_p_online(cpu_t *, int);
69 static void cpr_save_mp_state(void);
70 int cpr_is_ufs(struct vfs *);
71 
72 char cpr_default_path[] = CPR_DEFAULT;
73 
74 #define	COMPRESS_PERCENT 40	/* approx compression ratio in percent */
75 #define	SIZE_RATE	115	/* increase size by 15% */
76 #define	INTEGRAL	100	/* for integer math */
77 
78 
79 /*
80  * cmn_err() followed by a 1/4 second delay; this gives the
81  * logging service a chance to flush messages and helps avoid
82  * intermixing output from prom_printf().
83  */
84 /*PRINTFLIKE2*/
85 void
86 cpr_err(int ce, const char *fmt, ...)
87 {
88 	va_list adx;
89 
90 	va_start(adx, fmt);
91 	vcmn_err(ce, fmt, adx);
92 	va_end(adx);
93 	drv_usecwait(MICROSEC >> 2);
94 }
95 
96 
97 int
98 cpr_init(int fcn)
99 {
100 	/*
101 	 * Allow only one suspend/resume process.
102 	 */
103 	if (mutex_tryenter(&cpr_slock) == 0)
104 		return (EBUSY);
105 
106 	CPR->c_flags = 0;
107 	CPR->c_substate = 0;
108 	CPR->c_cprboot_magic = 0;
109 	CPR->c_alloc_cnt = 0;
110 
111 	CPR->c_fcn = fcn;
112 	if (fcn == AD_CPR_REUSABLE)
113 		CPR->c_flags |= C_REUSABLE;
114 	else
115 		CPR->c_flags |= C_SUSPENDING;
116 	if (fcn != AD_CPR_NOCOMPRESS && fcn != AD_CPR_TESTNOZ)
117 		CPR->c_flags |= C_COMPRESSING;
118 	/*
119 	 * reserve CPR_MAXCONTIG virtual pages for cpr_dump()
120 	 */
121 	CPR->c_mapping_area = i_cpr_map_setup();
122 	if (CPR->c_mapping_area == 0) {		/* no space in kernelmap */
123 		cpr_err(CE_CONT, "Unable to alloc from kernelmap.\n");
124 		mutex_exit(&cpr_slock);
125 		return (EAGAIN);
126 	}
127 	DEBUG3(cpr_err(CE_CONT, "Reserved virtual range from 0x%p for writing "
128 	    "kas\n", (void *)CPR->c_mapping_area));
129 
130 	return (0);
131 }
132 
133 /*
134  * This routine releases any resources used during the checkpoint.
135  */
136 void
137 cpr_done(void)
138 {
139 	cpr_stat_cleanup();
140 	i_cpr_bitmap_cleanup();
141 
142 	/*
143 	 * Free pages used by cpr buffers.
144 	 */
145 	if (cpr_buf) {
146 		kmem_free(cpr_buf, cpr_buf_size);
147 		cpr_buf = NULL;
148 	}
149 	if (cpr_pagedata) {
150 		kmem_free(cpr_pagedata, cpr_pagedata_size);
151 		cpr_pagedata = NULL;
152 	}
153 
154 	i_cpr_free_memory_resources();
155 	mutex_exit(&cpr_slock);
156 	cpr_err(CE_CONT, "System has been resumed.\n");
157 }
158 
159 
160 /*
161  * reads config data into cprconfig
162  */
163 static int
164 cpr_get_config(void)
165 {
166 	static char config_path[] = CPR_CONFIG;
167 	struct cprconfig *cf = &cprconfig;
168 	struct vnode *vp;
169 	char *fmt;
170 	int err;
171 
172 	if (cprconfig_loaded)
173 		return (0);
174 
175 	fmt = "cannot %s config file \"%s\", error %d\n";
176 	if (err = vn_open(config_path, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0)) {
177 		cpr_err(CE_CONT, fmt, "open", config_path, err);
178 		return (err);
179 	}
180 
181 	err = cpr_rdwr(UIO_READ, vp, cf, sizeof (*cf));
182 	(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED());
183 	VN_RELE(vp);
184 	if (err) {
185 		cpr_err(CE_CONT, fmt, "read", config_path, err);
186 		return (err);
187 	}
188 
189 	if (cf->cf_magic == CPR_CONFIG_MAGIC)
190 		cprconfig_loaded = 1;
191 	else {
192 		cpr_err(CE_CONT, "invalid config file \"%s\", "
193 		    "rerun pmconfig(1M)\n", config_path);
194 		err = EINVAL;
195 	}
196 
197 	return (err);
198 }
199 
200 
201 /*
202  * concat fs and path fields of the cprconfig structure;
203  * returns pointer to the base of static data
204  */
205 static char *
206 cpr_cprconfig_to_path(void)
207 {
208 	static char full_path[MAXNAMELEN];
209 	struct cprconfig *cf = &cprconfig;
210 	char *ptr;
211 
212 	/*
213 	 * build /fs/path without extra '/'
214 	 */
215 	(void) strcpy(full_path, cf->cf_fs);
216 	if (strcmp(cf->cf_fs, "/"))
217 		(void) strcat(full_path, "/");
218 	ptr = cf->cf_path;
219 	if (*ptr == '/')
220 		ptr++;
221 	(void) strcat(full_path, ptr);
222 	return (full_path);
223 }
224 
225 
226 /*
227  * Verify that the information in the configuration file regarding the
228  * location for the statefile is still valid, depending on cf_type.
229  * for CFT_UFS, cf_fs must still be a mounted filesystem, it must be
230  *	mounted on the same device as when pmconfig was last run,
231  *	and the translation of that device to a node in the prom's
232  *	device tree must be the same as when pmconfig was last run.
233  * for CFT_SPEC, cf_path must be the path to a block special file,
234  *	it must have no file system mounted on it,
235  *	and the translation of that device to a node in the prom's
236  *	device tree must be the same as when pmconfig was last run.
237  */
238 static int
239 cpr_verify_statefile_path(void)
240 {
241 	struct cprconfig *cf = &cprconfig;
242 	static const char long_name[] = "Statefile pathname is too long.\n";
243 	static const char lookup_fmt[] = "Lookup failed for "
244 	    "cpr statefile device %s.\n";
245 	static const char path_chg_fmt[] = "Device path for statefile "
246 	    "has changed from %s to %s.\t%s\n";
247 	static const char rerun[] = "Please rerun pmconfig(1m).";
248 	struct vfs *vfsp = NULL, *vfsp_save = rootvfs;
249 	ufsvfs_t *ufsvfsp = (ufsvfs_t *)rootvfs->vfs_data;
250 	ufsvfs_t *ufsvfsp_save = ufsvfsp;
251 	int error;
252 	struct vnode *vp;
253 	char *slash, *tail, *longest;
254 	char *errstr;
255 	int found = 0;
256 	union {
257 		char un_devpath[OBP_MAXPATHLEN];
258 		char un_sfpath[MAXNAMELEN];
259 	} un;
260 #define	devpath	un.un_devpath
261 #define	sfpath	un.un_sfpath
262 
263 	ASSERT(cprconfig_loaded);
264 	/*
265 	 * We need not worry about locking or the timing of releasing
266 	 * the vnode, since we are single-threaded now.
267 	 */
268 
269 	switch (cf->cf_type) {
270 	case CFT_SPEC:
271 		if (strlen(cf->cf_path) > sizeof (sfpath)) {
272 			cpr_err(CE_CONT, long_name);
273 			return (ENAMETOOLONG);
274 		}
275 		if ((error = lookupname(cf->cf_devfs,
276 		    UIO_SYSSPACE, FOLLOW, NULLVPP, &vp)) != 0) {
277 			cpr_err(CE_CONT, lookup_fmt, cf->cf_devfs);
278 			return (error);
279 		}
280 		if (vp->v_type != VBLK)
281 			errstr = "statefile must be a block device";
282 		else if (vfs_devismounted(vp->v_rdev))
283 			errstr = "statefile device must not "
284 			    "have a file system mounted on it";
285 		else if (IS_SWAPVP(vp))
286 			errstr = "statefile device must not "
287 			    "be configured as swap file";
288 		else
289 			errstr = NULL;
290 
291 		VN_RELE(vp);
292 		if (errstr) {
293 			cpr_err(CE_CONT, "%s.\n", errstr);
294 			return (ENOTSUP);
295 		}
296 
297 		error = i_devname_to_promname(cf->cf_devfs, devpath,
298 		    OBP_MAXPATHLEN);
299 		if (error || strcmp(devpath, cf->cf_dev_prom)) {
300 			cpr_err(CE_CONT, path_chg_fmt,
301 			    cf->cf_dev_prom, devpath, rerun);
302 		}
303 		return (error);
304 	case CFT_UFS:
305 		break;		/* don't indent all the original code */
306 	default:
307 		cpr_err(CE_PANIC, "invalid cf_type");
308 	}
309 
310 	/*
311 	 * The original code for UFS statefile
312 	 */
313 	if (strlen(cf->cf_fs) + strlen(cf->cf_path) + 2 > sizeof (sfpath)) {
314 		cpr_err(CE_CONT, long_name);
315 		return (ENAMETOOLONG);
316 	}
317 
318 	bzero(sfpath, sizeof (sfpath));
319 	(void) strcpy(sfpath, cpr_cprconfig_to_path());
320 
321 	if (*sfpath != '/') {
322 		cpr_err(CE_CONT, "Statefile pathname %s "
323 		    "must begin with a /\n", sfpath);
324 		return (EINVAL);
325 	}
326 
327 	/*
328 	 * Find the longest prefix of the statefile pathname which
329 	 * is the mountpoint of a filesystem.  This string must
330 	 * match the cf_fs field we read from the config file.  Other-
331 	 * wise the user has changed things without running pmconfig.
332 	 */
333 	tail = longest = sfpath + 1;	/* pt beyond the leading "/" */
334 	while ((slash = strchr(tail, '/')) != NULL) {
335 		*slash = '\0';	  /* temporarily terminate the string */
336 		if ((error = lookupname(sfpath,
337 		    UIO_SYSSPACE, FOLLOW, NULLVPP, &vp)) != 0) {
338 			*slash = '/';
339 			cpr_err(CE_CONT, "A directory in the "
340 			    "statefile path %s was not found.\n", sfpath);
341 			VN_RELE(vp);
342 
343 			return (error);
344 		}
345 
346 		vfs_list_read_lock();
347 		vfsp = rootvfs;
348 		do {
349 			ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
350 			if (ufsvfsp != NULL && ufsvfsp->vfs_root == vp) {
351 				found = 1;
352 				break;
353 			}
354 			vfsp = vfsp->vfs_next;
355 		} while (vfsp != rootvfs);
356 		vfs_list_unlock();
357 
358 		/*
359 		 * If we have found a filesystem mounted on the current
360 		 * path prefix, remember the end of the string in
361 		 * "longest".  If it happens to be the the exact fs
362 		 * saved in the configuration file, save the current
363 		 * ufsvfsp so we can make additional checks further down.
364 		 */
365 		if (found) {
366 			longest = slash;
367 			if (strcmp(cf->cf_fs, sfpath) == 0) {
368 				ufsvfsp_save = ufsvfsp;
369 				vfsp_save = vfsp;
370 			}
371 			found = 0;
372 		}
373 
374 		VN_RELE(vp);
375 		*slash = '/';
376 		tail = slash + 1;
377 	}
378 	*longest = '\0';
379 	if (cpr_is_ufs(vfsp_save) == 0 || strcmp(cf->cf_fs, sfpath)) {
380 		cpr_err(CE_CONT, "Filesystem containing "
381 		    "the statefile when pmconfig was run (%s) has "
382 		    "changed to %s. %s\n", cf->cf_fs, sfpath, rerun);
383 		return (EINVAL);
384 	}
385 
386 	if ((error = lookupname(cf->cf_devfs,
387 	    UIO_SYSSPACE, FOLLOW, NULLVPP, &vp)) != 0) {
388 		cpr_err(CE_CONT, lookup_fmt, cf->cf_devfs);
389 		return (error);
390 	}
391 
392 	if (ufsvfsp_save->vfs_devvp->v_rdev != vp->v_rdev) {
393 		cpr_err(CE_CONT, "Filesystem containing "
394 		    "statefile no longer mounted on device %s. "
395 		    "See power.conf(4).", cf->cf_devfs);
396 		VN_RELE(vp);
397 		return (ENXIO);
398 	}
399 	VN_RELE(vp);
400 
401 	error = i_devname_to_promname(cf->cf_devfs, devpath, OBP_MAXPATHLEN);
402 	if (error || strcmp(devpath, cf->cf_dev_prom)) {
403 		cpr_err(CE_CONT, path_chg_fmt,
404 		    cf->cf_dev_prom, devpath, rerun);
405 		return (error);
406 	}
407 
408 	return (0);
409 }
410 
411 /*
412  * Make sure that the statefile can be used as a block special statefile
413  * (meaning that is exists and has nothing mounted on it)
414  * Returns errno if not a valid statefile.
415  */
416 int
417 cpr_check_spec_statefile(void)
418 {
419 	int err;
420 
421 	if (err = cpr_get_config())
422 		return (err);
423 	ASSERT(cprconfig.cf_type == CFT_SPEC);
424 
425 	if (cprconfig.cf_devfs == NULL)
426 		return (ENXIO);
427 
428 	return (cpr_verify_statefile_path());
429 
430 }
431 
432 int
433 cpr_alloc_statefile(int alloc_retry)
434 {
435 	register int rc = 0;
436 	char *str;
437 
438 	/*
439 	 * Statefile size validation. If checkpoint the first time, disk blocks
440 	 * allocation will be done; otherwise, just do file size check.
441 	 * if statefile allocation is being retried, C_VP will be inited
442 	 */
443 	if (alloc_retry) {
444 		str = "\n-->Retrying statefile allocation...";
445 		if (cpr_debug & (LEVEL1 | LEVEL7))
446 			errp(str);
447 		if (C_VP->v_type != VBLK)
448 			(void) VOP_DUMPCTL(C_VP, DUMP_FREE, NULL);
449 	} else {
450 		/*
451 		 * Open an exiting file for writing, the state file needs to be
452 		 * pre-allocated since we can't and don't want to do allocation
453 		 * during checkpoint (too much of the OS is disabled).
454 		 *    - do a preliminary size checking here, if it is too small,
455 		 *	allocate more space internally and retry.
456 		 *    - check the vp to make sure it's the right type.
457 		 */
458 		char *path = cpr_build_statefile_path();
459 
460 		if (path == NULL)
461 			return (ENXIO);
462 		else if (rc = cpr_verify_statefile_path())
463 			return (rc);
464 
465 		if (rc = vn_open(path, UIO_SYSSPACE,
466 		    FCREAT|FWRITE, 0600, &C_VP, CRCREAT, 0)) {
467 			cpr_err(CE_WARN, "cannot open statefile %s", path);
468 			return (rc);
469 		}
470 	}
471 
472 	/*
473 	 * Only ufs and block special statefiles supported
474 	 */
475 	if (C_VP->v_type != VREG && C_VP->v_type != VBLK) {
476 		cpr_err(CE_CONT,
477 		    "Statefile must be regular file or block special file.");
478 		return (EACCES);
479 	}
480 
481 	if (rc = cpr_statefile_ok(C_VP, alloc_retry))
482 		return (rc);
483 
484 	if (C_VP->v_type != VBLK) {
485 		/*
486 		 * sync out the fs change due to the statefile reservation.
487 		 */
488 		(void) VFS_SYNC(C_VP->v_vfsp, 0, CRED());
489 
490 		/*
491 		 * Validate disk blocks allocation for the state file.
492 		 * Ask the file system prepare itself for the dump operation.
493 		 */
494 		if (rc = VOP_DUMPCTL(C_VP, DUMP_ALLOC, NULL)) {
495 			cpr_err(CE_CONT, "Error allocating "
496 			    "blocks for cpr statefile.");
497 			return (rc);
498 		}
499 	}
500 	return (0);
501 }
502 
503 
504 /*
505  * lookup device size in blocks,
506  * and return available space in bytes
507  */
508 size_t
509 cpr_get_devsize(dev_t dev)
510 {
511 	size_t bytes = 0;
512 	int64_t Nblocks;
513 	int nblocks;
514 
515 	if ((Nblocks = bdev_Size(dev)) != -1)
516 		bytes = dbtob(Nblocks);
517 	else if ((nblocks = bdev_size(dev)) != -1)
518 		bytes = dbtob(nblocks);
519 
520 	if (bytes > CPR_SPEC_OFFSET)
521 		bytes -= CPR_SPEC_OFFSET;
522 	else
523 		bytes = 0;
524 
525 	return (bytes);
526 }
527 
528 
529 /*
530  * increase statefile size
531  */
532 static int
533 cpr_grow_statefile(vnode_t *vp, u_longlong_t newsize)
534 {
535 	extern uchar_t cpr_pagecopy[];
536 	struct inode *ip = VTOI(vp);
537 	u_longlong_t offset;
538 	int error, increase;
539 	ssize_t resid;
540 
541 	rw_enter(&ip->i_contents, RW_READER);
542 	increase = (ip->i_size < newsize);
543 	offset = ip->i_size;
544 	rw_exit(&ip->i_contents);
545 
546 	if (increase == 0)
547 		return (0);
548 
549 	/*
550 	 * write to each logical block to reserve disk space
551 	 */
552 	error = 0;
553 	cpr_pagecopy[0] = '1';
554 	for (; offset < newsize; offset += ip->i_fs->fs_bsize) {
555 		if (error = vn_rdwr(UIO_WRITE, vp, (caddr_t)cpr_pagecopy,
556 		    ip->i_fs->fs_bsize, (offset_t)offset, UIO_SYSSPACE, 0,
557 		    (rlim64_t)MAXOFF_T, CRED(), &resid)) {
558 			if (error == ENOSPC) {
559 				cpr_err(CE_WARN, "error %d while reserving "
560 				    "disk space for statefile %s\n"
561 				    "wanted %lld bytes, file is %lld short",
562 				    error, cpr_cprconfig_to_path(),
563 				    newsize, newsize - offset);
564 			}
565 			break;
566 		}
567 	}
568 	return (error);
569 }
570 
571 
572 /*
573  * do a simple estimate of the space needed to hold the statefile
574  * taking compression into account, but be fairly conservative
575  * so we have a better chance of completing; when dump fails,
576  * the retry cost is fairly high.
577  *
578  * Do disk blocks allocation for the state file if no space has
579  * been allocated yet. Since the state file will not be removed,
580  * allocation should only be done once.
581  */
582 static int
583 cpr_statefile_ok(vnode_t *vp, int alloc_retry)
584 {
585 	extern size_t cpr_bitmap_size;
586 	struct inode *ip = VTOI(vp);
587 	const int UCOMP_RATE = 20; /* comp. ratio*10 for user pages */
588 	u_longlong_t size, isize, ksize, raw_data;
589 	char *str, *est_fmt;
590 	size_t space;
591 	int error;
592 
593 	/*
594 	 * number of pages short for swapping.
595 	 */
596 	STAT->cs_nosw_pages = k_anoninfo.ani_mem_resv;
597 	if (STAT->cs_nosw_pages < 0)
598 		STAT->cs_nosw_pages = 0;
599 
600 	str = "cpr_statefile_ok:";
601 
602 	DEBUG9(errp("Phys swap: max=%lu resv=%lu\n",
603 	    k_anoninfo.ani_max, k_anoninfo.ani_phys_resv));
604 	DEBUG9(errp("Mem swap: max=%ld resv=%lu\n",
605 	    MAX(availrmem - swapfs_minfree, 0),
606 	    k_anoninfo.ani_mem_resv));
607 	DEBUG9(errp("Total available swap: %ld\n",
608 		CURRENT_TOTAL_AVAILABLE_SWAP));
609 
610 	/*
611 	 * try increasing filesize by 15%
612 	 */
613 	if (alloc_retry) {
614 		/*
615 		 * block device doesn't get any bigger
616 		 */
617 		if (vp->v_type == VBLK) {
618 			if (cpr_debug & (LEVEL1 | LEVEL6))
619 				errp("Retry statefile on special file\n");
620 			return (ENOMEM);
621 		} else {
622 			rw_enter(&ip->i_contents, RW_READER);
623 			size = (ip->i_size * SIZE_RATE) / INTEGRAL;
624 			rw_exit(&ip->i_contents);
625 		}
626 		if (cpr_debug & (LEVEL1 | LEVEL6))
627 			errp("Retry statefile size = %lld\n", size);
628 	} else {
629 		u_longlong_t cpd_size;
630 		pgcnt_t npages, nback;
631 		int ndvram;
632 
633 		ndvram = 0;
634 		(void) callb_execute_class(CB_CL_CPR_FB, (int)&ndvram);
635 		if (cpr_debug & (LEVEL1 | LEVEL6))
636 			errp("ndvram size = %d\n", ndvram);
637 
638 		/*
639 		 * estimate 1 cpd_t for every (CPR_MAXCONTIG / 2) pages
640 		 */
641 		npages = cpr_count_kpages(REGULAR_BITMAP, cpr_nobit);
642 		cpd_size = sizeof (cpd_t) * (npages / (CPR_MAXCONTIG / 2));
643 		raw_data = cpd_size + cpr_bitmap_size;
644 		ksize = ndvram + mmu_ptob(npages);
645 
646 		est_fmt = "%s estimated size with "
647 		    "%scompression %lld, ksize %lld\n";
648 		nback = mmu_ptob(STAT->cs_nosw_pages);
649 		if (CPR->c_flags & C_COMPRESSING) {
650 			size = ((ksize * COMPRESS_PERCENT) / INTEGRAL) +
651 			    raw_data + ((nback * 10) / UCOMP_RATE);
652 			DEBUG1(errp(est_fmt, str, "", size, ksize));
653 		} else {
654 			size = ksize + raw_data + nback;
655 			DEBUG1(errp(est_fmt, str, "no ", size, ksize));
656 		}
657 	}
658 
659 	/*
660 	 * All this is much simpler for a block device
661 	 */
662 	if (vp->v_type == VBLK) {
663 		space = cpr_get_devsize(vp->v_rdev);
664 		if (cpr_debug & (LEVEL1 | LEVEL6))
665 			errp("statefile dev size %lu\n", space);
666 
667 		/*
668 		 * Export the estimated filesize info, this value will be
669 		 * compared before dumping out the statefile in the case of
670 		 * no compression.
671 		 */
672 		STAT->cs_est_statefsz = size;
673 		if (cpr_debug & (LEVEL1 | LEVEL6))
674 			errp("%s Estimated statefile size %d, space %lu\n",
675 			    str, size, space);
676 		if (size > space) {
677 			cpr_err(CE_CONT, "Statefile partition too small.");
678 			return (ENOMEM);
679 		}
680 		return (0);
681 	} else {
682 		if (CPR->c_alloc_cnt++ > C_MAX_ALLOC_RETRY) {
683 			cpr_err(CE_CONT, "Statefile allocation retry failed\n");
684 			return (ENOMEM);
685 		}
686 
687 		/*
688 		 * Estimate space needed for the state file.
689 		 *
690 		 * State file size in bytes:
691 		 * 	kernel size + non-cache pte seg +
692 		 *	bitmap size + cpr state file headers size
693 		 * (round up to fs->fs_bsize)
694 		 */
695 		size = blkroundup(ip->i_fs, size);
696 
697 		/*
698 		 * Export the estimated filesize info, this value will be
699 		 * compared before dumping out the statefile in the case of
700 		 * no compression.
701 		 */
702 		STAT->cs_est_statefsz = size;
703 		error = cpr_grow_statefile(vp, size);
704 		if (cpr_debug & (LEVEL1 | LEVEL6)) {
705 			rw_enter(&ip->i_contents, RW_READER);
706 			isize = ip->i_size;
707 			rw_exit(&ip->i_contents);
708 			errp("%s Estimated statefile size %lld, i_size %lld\n",
709 			    str, size, isize);
710 		}
711 
712 		return (error);
713 	}
714 }
715 
716 
717 void
718 cpr_statef_close(void)
719 {
720 	if (C_VP) {
721 		if (!cpr_reusable_mode)
722 			(void) VOP_DUMPCTL(C_VP, DUMP_FREE, NULL);
723 		(void) VOP_CLOSE(C_VP, FWRITE, 1, (offset_t)0, CRED());
724 		VN_RELE(C_VP);
725 		C_VP = 0;
726 	}
727 }
728 
729 
730 /*
731  * open cpr default file and display error
732  */
733 int
734 cpr_open_deffile(int mode, vnode_t **vpp)
735 {
736 	int error;
737 
738 	if (error = cpr_open(cpr_default_path, mode, vpp))
739 		cpr_err(CE_CONT, "cannot open \"%s\", error %d\n",
740 		    cpr_default_path, error);
741 	return (error);
742 }
743 
744 
745 /*
746  * write cdef_t to disk.  This contains the original values of prom
747  * properties that we modify.  We fill in the magic number of the file
748  * here as a signal to the booter code that the state file is valid.
749  * Be sure the file gets synced, since we may be shutting down the OS.
750  */
751 int
752 cpr_write_deffile(cdef_t *cdef)
753 {
754 	struct vnode *vp;
755 	char *str;
756 	int rc;
757 
758 	if (rc = cpr_open_deffile(FCREAT|FWRITE, &vp))
759 		return (rc);
760 
761 	if (rc = cpr_rdwr(UIO_WRITE, vp, cdef, sizeof (*cdef)))
762 		str = "write";
763 	else if (rc = VOP_FSYNC(vp, FSYNC, CRED()))
764 		str = "fsync";
765 	(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED());
766 	VN_RELE(vp);
767 
768 	if (rc) {
769 		cpr_err(CE_WARN, "%s error %d, file \"%s\"",
770 		    str, rc, cpr_default_path);
771 	}
772 	return (rc);
773 }
774 
775 /*
776  * Clear the magic number in the defaults file.  This tells the booter
777  * program that the state file is not current and thus prevents
778  * any attempt to restore from an obsolete state file.
779  */
780 void
781 cpr_clear_definfo(void)
782 {
783 	struct vnode *vp;
784 	cmini_t mini;
785 
786 	if ((CPR->c_cprboot_magic != CPR_DEFAULT_MAGIC) ||
787 	    cpr_open_deffile(FCREAT|FWRITE, &vp))
788 		return;
789 	mini.magic = mini.reusable = 0;
790 	(void) cpr_rdwr(UIO_WRITE, vp, &mini, sizeof (mini));
791 	(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED());
792 	VN_RELE(vp);
793 }
794 
795 /*
796  * If the cpr default file is invalid, then we must not be in reusable mode
797  * if it is valid, it tells us our mode
798  */
799 int
800 cpr_get_reusable_mode(void)
801 {
802 	struct vnode *vp;
803 	cmini_t mini;
804 	int rc;
805 
806 	if (cpr_open(cpr_default_path, FREAD, &vp))
807 		return (0);
808 
809 	rc = cpr_rdwr(UIO_READ, vp, &mini, sizeof (mini));
810 	(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED());
811 	VN_RELE(vp);
812 	if (rc == 0 && mini.magic == CPR_DEFAULT_MAGIC)
813 		return (mini.reusable);
814 
815 	return (0);
816 }
817 
818 /*
819  * clock/time related routines
820  */
821 static time_t   cpr_time_stamp;
822 
823 
824 void
825 cpr_tod_get(cpr_time_t *ctp)
826 {
827 	timestruc_t ts;
828 
829 	mutex_enter(&tod_lock);
830 	ts = tod_get();
831 	mutex_exit(&tod_lock);
832 	ctp->tv_sec = (time32_t)ts.tv_sec;
833 	ctp->tv_nsec = (int32_t)ts.tv_nsec;
834 }
835 
836 void
837 cpr_tod_fault_reset(void)
838 {
839 	mutex_enter(&tod_lock);
840 	tod_fault_reset();
841 	mutex_exit(&tod_lock);
842 }
843 
844 void
845 cpr_save_time(void)
846 {
847 	cpr_time_stamp = gethrestime_sec();
848 }
849 
850 /*
851  * correct time based on saved time stamp or hardware clock
852  */
853 void
854 cpr_restore_time(void)
855 {
856 	clkset(cpr_time_stamp);
857 }
858 
859 /*
860  * CPU ONLINE/OFFLINE CODE
861  */
862 int
863 cpr_mp_offline(void)
864 {
865 	cpu_t *cp, *bootcpu;
866 	int rc = 0;
867 	int brought_up_boot = 0;
868 
869 	/*
870 	 * Do nothing for UP.
871 	 */
872 	if (ncpus == 1)
873 		return (0);
874 
875 	mutex_enter(&cpu_lock);
876 
877 	cpr_save_mp_state();
878 
879 	bootcpu = i_cpr_bootcpu();
880 	if (!CPU_ACTIVE(bootcpu)) {
881 		if ((rc = cpr_p_online(bootcpu, CPU_CPR_ONLINE))) {
882 			mutex_exit(&cpu_lock);
883 			return (rc);
884 		}
885 		brought_up_boot = 1;
886 	}
887 
888 	cp = cpu_list;
889 	do {
890 		if (cp == bootcpu)
891 			continue;
892 		if (cp->cpu_flags & CPU_OFFLINE)
893 			continue;
894 		if ((rc = cpr_p_online(cp, CPU_CPR_OFFLINE))) {
895 			mutex_exit(&cpu_lock);
896 			return (rc);
897 		}
898 	} while ((cp = cp->cpu_next) != cpu_list);
899 	if (brought_up_boot && (cpr_debug & (LEVEL1 | LEVEL6)))
900 		errp("changed cpu %d to state %d\n", bootcpu, CPU_CPR_ONLINE);
901 	mutex_exit(&cpu_lock);
902 
903 	return (rc);
904 }
905 
906 int
907 cpr_mp_online(void)
908 {
909 	cpu_t *cp, *bootcpu = CPU;
910 	int rc = 0;
911 
912 	/*
913 	 * Do nothing for UP.
914 	 */
915 	if (ncpus == 1)
916 		return (0);
917 
918 	/*
919 	 * cpr_save_mp_state() sets CPU_CPR_ONLINE in cpu_cpr_flags
920 	 * to indicate a cpu was online at the time of cpr_suspend();
921 	 * now restart those cpus that were marked as CPU_CPR_ONLINE
922 	 * and actually are offline.
923 	 */
924 	mutex_enter(&cpu_lock);
925 	for (cp = bootcpu->cpu_next; cp != bootcpu; cp = cp->cpu_next) {
926 		/*
927 		 * Clear the CPU_FROZEN flag in all cases.
928 		 */
929 		cp->cpu_flags &= ~CPU_FROZEN;
930 
931 		if (CPU_CPR_IS_OFFLINE(cp))
932 			continue;
933 		if (CPU_ACTIVE(cp))
934 			continue;
935 		if ((rc = cpr_p_online(cp, CPU_CPR_ONLINE))) {
936 			mutex_exit(&cpu_lock);
937 			return (rc);
938 		}
939 	}
940 
941 	/*
942 	 * turn off the boot cpu if it was offlined
943 	 */
944 	if (CPU_CPR_IS_OFFLINE(bootcpu)) {
945 		if ((rc = cpr_p_online(bootcpu, CPU_CPR_OFFLINE))) {
946 			mutex_exit(&cpu_lock);
947 			return (rc);
948 		}
949 	}
950 	mutex_exit(&cpu_lock);
951 	return (0);
952 }
953 
954 static void
955 cpr_save_mp_state(void)
956 {
957 	cpu_t *cp;
958 
959 	ASSERT(MUTEX_HELD(&cpu_lock));
960 
961 	cp = cpu_list;
962 	do {
963 		cp->cpu_cpr_flags &= ~CPU_CPR_ONLINE;
964 		if (CPU_ACTIVE(cp))
965 			CPU_SET_CPR_FLAGS(cp, CPU_CPR_ONLINE);
966 	} while ((cp = cp->cpu_next) != cpu_list);
967 }
968 
969 /*
970  * change cpu to online/offline
971  */
972 static int
973 cpr_p_online(cpu_t *cp, int state)
974 {
975 	int rc;
976 
977 	ASSERT(MUTEX_HELD(&cpu_lock));
978 
979 	switch (state) {
980 	case CPU_CPR_ONLINE:
981 		rc = cpu_online(cp);
982 		break;
983 	case CPU_CPR_OFFLINE:
984 		rc = cpu_offline(cp, CPU_FORCED);
985 		break;
986 	}
987 	if (rc) {
988 		cpr_err(CE_WARN, "Failed to change processor %d to "
989 		    "state %d, (errno %d)", cp->cpu_id, state, rc);
990 	}
991 	return (rc);
992 }
993 
994 /*
995  * Construct the pathname of the state file and return a pointer to
996  * caller.  Read the config file to get the mount point of the
997  * filesystem and the pathname within fs.
998  */
999 char *
1000 cpr_build_statefile_path(void)
1001 {
1002 	struct cprconfig *cf = &cprconfig;
1003 
1004 	if (cpr_get_config())
1005 		return (NULL);
1006 
1007 	switch (cf->cf_type) {
1008 	case CFT_UFS:
1009 		if (strlen(cf->cf_path) + strlen(cf->cf_fs) >= MAXNAMELEN - 1) {
1010 			cpr_err(CE_CONT, "Statefile path is too long.\n");
1011 			return (NULL);
1012 		}
1013 		return (cpr_cprconfig_to_path());
1014 	case CFT_SPEC:
1015 		return (cf->cf_devfs);
1016 	default:
1017 		cpr_err(CE_PANIC, "invalid statefile type");
1018 		/*NOTREACHED*/
1019 	}
1020 }
1021 
1022 int
1023 cpr_statefile_is_spec(void)
1024 {
1025 	if (cpr_get_config())
1026 		return (0);
1027 	return (cprconfig.cf_type == CFT_SPEC);
1028 }
1029 
1030 char *
1031 cpr_get_statefile_prom_path(void)
1032 {
1033 	struct cprconfig *cf = &cprconfig;
1034 
1035 	ASSERT(cprconfig_loaded);
1036 	ASSERT(cf->cf_magic == CPR_CONFIG_MAGIC);
1037 	ASSERT(cf->cf_type == CFT_SPEC);
1038 	return (cf->cf_dev_prom);
1039 }
1040 
1041 
1042 /*
1043  * XXX The following routines need to be in the vfs source code.
1044  */
1045 
1046 int
1047 cpr_is_ufs(struct vfs *vfsp)
1048 {
1049 	char *fsname;
1050 
1051 	fsname = vfssw[vfsp->vfs_fstype].vsw_name;
1052 	return (strcmp(fsname, "ufs") == 0);
1053 }
1054 
1055 /*
1056  * This is a list of file systems that are allowed to be writeable when a
1057  * reusable statefile checkpoint is taken.  They must not have any state that
1058  * cannot be restored to consistency by simply rebooting using the checkpoint.
1059  * (In contrast to ufs, cachefs and pcfs which have disk state that could get
1060  * out of sync with the in-kernel data).
1061  */
1062 int
1063 cpr_reusable_mount_check(void)
1064 {
1065 	struct vfs *vfsp;
1066 	char *fsname;
1067 	char **cpp;
1068 	static char *cpr_writeok_fss[] = {
1069 		"autofs", "devfs", "fd", "lofs", "mntfs", "namefs", "nfs",
1070 		"proc", "tmpfs", "ctfs", "objfs", NULL
1071 	};
1072 
1073 	vfs_list_read_lock();
1074 	vfsp = rootvfs;
1075 	do {
1076 		if (vfsp->vfs_flag & VFS_RDONLY) {
1077 			vfsp = vfsp->vfs_next;
1078 			continue;
1079 		}
1080 		fsname = vfssw[vfsp->vfs_fstype].vsw_name;
1081 		for (cpp = cpr_writeok_fss; *cpp; cpp++) {
1082 			if (strcmp(fsname, *cpp) == 0)
1083 				break;
1084 		}
1085 		/*
1086 		 * if the inner loop reached the NULL terminator,
1087 		 * the current fs-type does not match any OK-type
1088 		 */
1089 		if (*cpp == NULL) {
1090 			cpr_err(CE_CONT, "a filesystem of type %s is "
1091 			    "mounted read/write.\nReusable statefile requires "
1092 			    "no writeable filesystem of this type be mounted\n",
1093 			    fsname);
1094 			vfs_list_unlock();
1095 			return (EINVAL);
1096 		}
1097 		vfsp = vfsp->vfs_next;
1098 	} while (vfsp != rootvfs);
1099 	vfs_list_unlock();
1100 	return (0);
1101 }
1102 
1103 /*
1104  * Force a fresh read of the cprinfo per uadmin 3 call
1105  */
1106 void
1107 cpr_forget_cprconfig(void)
1108 {
1109 	cprconfig_loaded = 0;
1110 }
1111 
1112 
1113 /*
1114  * return statefile offset in DEV_BSIZE units
1115  */
1116 int
1117 cpr_statefile_offset(void)
1118 {
1119 	return (cpr_statefile_is_spec() ? btod(CPR_SPEC_OFFSET) : 0);
1120 }
1121