xref: /illumos-gate/usr/src/uts/common/sys/fssnap.h (revision 08045defdf65ee890fef6e20510a093a17feb8fe)
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 #ifndef	_SYS_FSSNAP_H
28 #define	_SYS_FSSNAP_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/sysmacros.h>
33 #include <sys/devops.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #include <sys/buf.h>
40 #include <sys/taskq.h>
41 #include <sys/fs/ufs_inode.h>
42 
43 /* snapshot backend interfaces, macros, and data structures */
44 
45 #if defined(_KERNEL)
46 
47 /*
48  * defines for the number of threads used to handle tasks, the maximum
49  * number of chunks in memory at a time, and the maximum number of tasks to
50  * allow before the taskqs start throttling. MAXTASKS should be greater than
51  * or equal to MAX_MEM_CHUNKS.
52  */
53 #define	FSSNAP_TASKQ_THREADS	(2)
54 #define	FSSNAP_MAX_MEM_CHUNKS	(32)
55 #define	FSSNAP_TASKQ_MAXTASKS	(FSSNAP_MAX_MEM_CHUNKS)
56 
57 /*
58  * It is assumed that a chunk is a multiple of a disk sector so that
59  * the chunk size can be reduced before using it as a conversion
60  * factor.  Therefore the number of chunks on a file system will
61  * always be less than the number of blocks it occupies, and these
62  * conversions will not overflow. (do not convert to bytes first!)
63  */
64 
65 typedef unsigned long long	chunknumber_t;
66 
67 /* disk blocks to snapshot chunks */
68 #define	dbtocowchunk(cmap, dblkno) ((dblkno) / \
69 	((cmap)->cmap_chunksz >> DEV_BSHIFT))
70 
71 /* snapshot chunks to disk blocks */
72 #define	cowchunktodb(cmap, cowchunk) ((cowchunk) * \
73 	((cmap)->cmap_chunksz >> DEV_BSHIFT))
74 
75 /*
76  * A snapshot_id is the shared structure between the snapshot driver
77  * and the file system.
78  */
79 typedef struct snapshot_id {
80 	struct snapshot_id	*sid_next;	/* next snapshot in list */
81 	krwlock_t		sid_rwlock;	/* protects enable/disable */
82 	struct cow_info		*sid_cowinfo;	/* pointer to cow state */
83 	uint_t			sid_snapnumber;	/* snapshot number */
84 	uint_t			sid_flags;	/* general flags */
85 	struct vnode		*sid_fvp;	/* root vnode to snapshot */
86 } snapshot_id_t;
87 
88 /* snapshot_id flags */
89 #define	SID_DISABLED	(0x01)	/* this snapshot has been disabled */
90 #define	SID_DISABLING	(0x02)	/* this snapshot is being disabled */
91 #define	SID_BLOCK_BUSY	(0x04)	/* snapshot block driver is attached */
92 #define	SID_CHAR_BUSY	(0x08)	/* snapshot character driver is attached */
93 #define	SID_CREATING	(0x10)	/* snapshot is being created */
94 #define	SID_DELETE	(0x20)	/* error condition found, delete snapshot */
95 
96 /* true if snapshot device is open */
97 #define	SID_BUSY(sidp)	(((sidp)->sid_flags & SID_BLOCK_BUSY) || \
98 	((sidp)->sid_flags & SID_CHAR_BUSY))
99 
100 /* true if snapshot can not be used */
101 #define	SID_INACTIVE(sidp)	(((sidp)->sid_flags & SID_DISABLED) || \
102 	((sidp)->sid_flags & SID_DISABLING) || \
103 	((sidp)->sid_flags & SID_CREATING) || \
104 	((sidp)->sid_flags & SID_DELETE) || \
105 	((sidp)->sid_cowinfo == NULL))
106 
107 /* true if snapshot can be reused now */
108 #define	SID_AVAILABLE(sidp)	(!SID_BUSY(sidp) && \
109 	((sidp)->sid_flags & SID_DISABLED))
110 
111 /*
112  * The cow_map keeps track of all translations, and two bitmaps to
113  * determine whether the chunk is eligible for translation, and if so
114  * whether or not it already has a translation.  The candidate bitmap
115  * is read-only and does not require a lock, the hastrans bitmap and
116  * the translation table are protected by the cmap_rwlock.
117  */
118 typedef struct cow_map {
119 	krwlock_t	cmap_rwlock;	/* protects this structure */
120 	ksema_t		cmap_throttle_sem; /* used to throttle writes */
121 	uint32_t	cmap_waiters;	/* semaphore waiters */
122 	uint_t		cmap_chunksz;	/* granularity of COW operations */
123 	chunknumber_t	cmap_chunksperbf; /* chunks in max backing file */
124 	chunknumber_t	cmap_nchunks;	/* number of chunks in backing file */
125 	u_offset_t	cmap_maxsize;	/* max bytes allowed (0 is no limit) */
126 	size_t		cmap_bmsize;	/* size of bitmaps (in bytes) */
127 	caddr_t		cmap_candidate;	/* 1 = block is a candidate for COW */
128 	caddr_t		cmap_hastrans;	/* 1 = an entry exists in the table */
129 	struct cow_map_node	*cmap_table;	/* translation table */
130 } cow_map_t;
131 
132 /*
133  * The cow_map_node keeps track of chunks that are still in memory.
134  */
135 
136 typedef struct cow_map_node {
137 	struct cow_map_node	*cmn_next;
138 	struct cow_map_node	*cmn_prev;
139 	struct snapshot_id	*cmn_sid;	/* backpointer to snapshot */
140 	chunknumber_t		cmn_chunk;	/* original chunk number */
141 	caddr_t			cmn_buf;	/* the data itself */
142 	int			release_sem;	/* flag to release */
143 						/* cmap_throttle_sem */
144 } cow_map_node_t;
145 
146 /*
147  * The cow_info structure holds basic snapshot state information. It is
148  * mostly read-only once the snapshot is created so no locking is required.
149  * The exception is cow_nextchunk, which is ever-increasing and updated with
150  * atomic_add(). This structure is allocated dynamically, and creation and
151  * deletion of the snapshot is protected by the snapshot_mutex variable.
152  */
153 typedef struct cow_info {
154 	int		cow_backcount;	/* number of backing files */
155 	vnode_t		**cow_backfile_array; /* array of backing files */
156 	u_offset_t	cow_backfile_sz;	/* max size of a backfile */
157 	taskq_t		*cow_taskq;	/* task queue for async writes */
158 	struct kstat	*cow_kstat_mntpt;	/* kstat for mount point */
159 	struct kstat	*cow_kstat_bfname;	/* kstat for backing file */
160 	struct kstat	*cow_kstat_num;	/* named numeric kstats */
161 	struct cow_map	cow_map;	/* block translation table */
162 } cow_info_t;
163 
164 /* kstat information */
165 struct cow_kstat_num {
166 	kstat_named_t	ckn_state;	/* state of the snapshot device */
167 	kstat_named_t	ckn_bfsize;	/* sum of sizes of backing files */
168 	kstat_named_t	ckn_maxsize;	/* maximum backing file size */
169 	kstat_named_t	ckn_createtime;	/* snapshot creation time */
170 	kstat_named_t	ckn_chunksize;	/* chunk size */
171 };
172 
173 /* ckn_state values */
174 #define	COWSTATE_CREATING	(0)	/* snapshot being created */
175 #define	COWSTATE_IDLE		(1)	/* snapshot exists, but not open */
176 #define	COWSTATE_ACTIVE		(2)	/* snapshot open */
177 #define	COWSTATE_DISABLED	(3)	/* snapshot deleted (pending close) */
178 
179 extern	uint_t	bypass_snapshot_throttle_key;
180 
181 #endif /* _KERNEL */
182 
183 #ifdef	__cplusplus
184 }
185 #endif
186 
187 #endif /* _SYS_FSSNAP_H */
188