su.static: link with proto area libs (esp. libc)
[unleashed.git] / kernel / cpr / cpr_misc.c
blobd4d1f789b629bee819d45004f1d34637d6c0d83e
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 #include <sys/types.h>
28 #include <sys/errno.h>
29 #include <sys/cpuvar.h>
30 #include <sys/vfs.h>
31 #include <sys/vnode.h>
32 #include <sys/pathname.h>
33 #include <sys/callb.h>
34 #include <sys/fs/ufs_inode.h>
35 #include <vm/anon.h>
36 #include <sys/fs/swapnode.h> /* for swapfs_minfree */
37 #include <sys/kmem.h>
38 #include <sys/cpr.h>
39 #include <sys/conf.h>
40 #include <sys/machclock.h>
43 * CPR miscellaneous support routines
45 #define cpr_open(path, mode, vpp) (vn_open(path, UIO_SYSSPACE, \
46 mode, 0600, vpp, CRCREAT, 0))
47 #define cpr_rdwr(rw, vp, basep, cnt) (vn_rdwr(rw, vp, (caddr_t)(basep), \
48 cnt, 0LL, UIO_SYSSPACE, 0, (rlim64_t)MAXOFF_T, CRED(), \
49 NULL))
51 extern void clkset(time_t);
52 extern cpu_t *i_cpr_bootcpu(void);
53 extern caddr_t i_cpr_map_setup(void);
54 extern void i_cpr_free_memory_resources(void);
56 extern kmutex_t cpr_slock;
57 extern size_t cpr_buf_size;
58 extern char *cpr_buf;
59 extern size_t cpr_pagedata_size;
60 extern char *cpr_pagedata;
61 extern int cpr_bufs_allocated;
62 extern int cpr_bitmaps_allocated;
65 int cpr_is_ufs(struct vfs *);
66 int cpr_is_zfs(struct vfs *);
68 char cpr_default_path[] = CPR_DEFAULT;
70 #define COMPRESS_PERCENT 40 /* approx compression ratio in percent */
71 #define SIZE_RATE 115 /* increase size by 15% */
72 #define INTEGRAL 100 /* for integer math */
76 * cmn_err() followed by a 1/4 second delay; this gives the
77 * logging service a chance to flush messages and helps avoid
78 * intermixing output from prom_printf().
80 /*PRINTFLIKE2*/
81 void
82 cpr_err(int ce, const char *fmt, ...)
84 va_list adx;
86 va_start(adx, fmt);
87 vcmn_err(ce, fmt, adx);
88 va_end(adx);
89 drv_usecwait(MICROSEC >> 2);
93 int
94 cpr_init(int fcn)
97 * Allow only one suspend/resume process.
99 if (mutex_tryenter(&cpr_slock) == 0)
100 return (EBUSY);
102 CPR->c_flags = 0;
103 CPR->c_substate = 0;
104 CPR->c_cprboot_magic = 0;
105 CPR->c_alloc_cnt = 0;
107 CPR->c_fcn = fcn;
108 if (fcn == AD_CPR_REUSABLE)
109 CPR->c_flags |= C_REUSABLE;
110 else
111 CPR->c_flags |= C_SUSPENDING;
112 if (fcn == AD_SUSPEND_TO_RAM || fcn == DEV_SUSPEND_TO_RAM) {
113 return (0);
116 return (0);
120 * This routine releases any resources used during the checkpoint.
122 void
123 cpr_done(void)
125 cpr_stat_cleanup();
126 i_cpr_bitmap_cleanup();
129 * Free pages used by cpr buffers.
131 if (cpr_buf) {
132 kmem_free(cpr_buf, cpr_buf_size);
133 cpr_buf = NULL;
135 if (cpr_pagedata) {
136 kmem_free(cpr_pagedata, cpr_pagedata_size);
137 cpr_pagedata = NULL;
140 i_cpr_free_memory_resources();
141 mutex_exit(&cpr_slock);
142 cpr_err(CE_CONT, "System has been resumed.\n");
148 * clock/time related routines
150 static time_t cpr_time_stamp;
153 void
154 cpr_tod_get(cpr_time_t *ctp)
156 timestruc_t ts;
158 mutex_enter(&tod_lock);
159 ts = TODOP_GET(tod_ops);
160 mutex_exit(&tod_lock);
161 ctp->tv_sec = (time32_t)ts.tv_sec;
162 ctp->tv_nsec = (int32_t)ts.tv_nsec;
165 void
166 cpr_tod_status_set(int tod_flag)
168 mutex_enter(&tod_lock);
169 tod_status_set(tod_flag);
170 mutex_exit(&tod_lock);
173 void
174 cpr_save_time(void)
176 cpr_time_stamp = gethrestime_sec();
180 * correct time based on saved time stamp or hardware clock
182 void
183 cpr_restore_time(void)
185 clkset(cpr_time_stamp);