xref: /illumos-gate/usr/src/uts/common/sys/fs/ufs_lockfs.h (revision 51396a8ee7fb52fe0ab33bfe7b4f495ad431904a)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_FS_UFS_LOCKFS_H
28 #define	_SYS_FS_UFS_LOCKFS_H
29 
30 #include <sys/lockfs.h>
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 /*
37  * Sun ufs file system locking (lockfs)
38  *
39  * ufs file system supports the following lock types:
40  * 	unlock		- releasing existing locks, or do a file system flush
41  *	name lock	- no delete, no rename
42  *	write lock	- no update to file system, including delete
43  *	delete lock	- no delete, rename is allowed
44  *	hard lock	- no update, no access, cannot be unlocked
45  *			- for supporting forcible umount
46  *	error lock	- no update, no access, may only be unlocked
47  *			- once fs becomes clean, may be upgraded to
48  *			- a hard lock
49  *	error lock (read-only) -- not yet implemented --
50  *			- no write changes allowed to fs, may be upgraded
51  *			- to error or hard lock
52  *			- degrades to panic on subsequent failures
53  *
54  * ufs_vnodeops(es) that conflict with the above file system lock types
55  *	will get either suspended, or get a EAGAIN error,
56  *	or get an EIO error if the file system is hard locked,
57  *	or will block if the file system is error locked.
58  *
59  * There are exceptions.
60  *	The following ufs_vnops do not obey the locking protocol:
61  *	ufs_close, ufs_putpage, ufs_inactive, ufs_addmap, ufs_delmap,
62  *	ufs_rwlock, ufs_rwunlock, ufs_poll.
63  *
64  * ul_vnops_cnt will get increment by 1 when a ufs vnodeops is entered;
65  * it will be decremented by 1 when a ufs_vnodeops is exited.
66  * A file system is in a quiescent state if ufs_vnops_cnt is zero.
67  * Since ufs_pageio() has to change ul_vnops_cnt without using ul_lock
68  * all users of ul_vnops_cnt increment and decrement it via atomic_add_long().
69  */
70 
71 #include <sys/fs/ufs_trans.h>
72 #include <sys/thread.h>
73 
74 /*
75  * ul_flag
76  */
77 #define	ULOCKFS_BUSY	0x00000001	/* ul_fs_lock is being set */
78 #define	ULOCKFS_NOIACC	0x00000004	/* don't keep access times */
79 #define	ULOCKFS_NOIDEL	0x00000008	/* don't free deleted files */
80 #define	ULOCKFS_FALLOC	0x00000010	/* fallocate threads exist */
81 
82 #define	ULOCKFS_IS_BUSY(LF)	((LF)->ul_flag & ULOCKFS_BUSY)
83 #define	ULOCKFS_IS_NOIACC(LF)	((LF)->ul_flag & ULOCKFS_NOIACC)
84 #define	ULOCKFS_IS_NOIDEL(LF)	((LF)->ul_flag & ULOCKFS_NOIDEL)
85 #define	ULOCKFS_IS_FALLOC(LF)	((LF)->ul_flag & ULOCKFS_FALLOC)
86 
87 #define	ULOCKFS_CLR_BUSY(LF)	((LF)->ul_flag &= ~ULOCKFS_BUSY)
88 #define	ULOCKFS_SET_BUSY(LF)	((LF)->ul_flag |= ULOCKFS_BUSY)
89 
90 #define	ULOCKFS_CLR_FALLOC(LF)	((LF)->ul_flag &= ~ULOCKFS_FALLOC)
91 #define	ULOCKFS_SET_FALLOC(LF)	((LF)->ul_flag |= ULOCKFS_FALLOC)
92 
93 /*
94  * ul_fs_mod
95  */
96 #define	ULOCKFS_SET_MOD(LF)	((LF)->ul_fs_mod = 1)
97 #define	ULOCKFS_CLR_MOD(LF)	((LF)->ul_fs_mod = 0)
98 #define	ULOCKFS_IS_MOD(LF)	((LF)->ul_fs_mod)
99 
100 /*
101  * ul_fs_lock
102  *
103  * softlock will temporarily block most ufs_vnodeops.
104  * it is used so that a waiting lockfs command will not be starved
105  *
106  * fwlock will block other fallocate threads wanting to obtain a write lock
107  * on the file system.
108  */
109 #define	ULOCKFS_ULOCK	((1 << LOCKFS_ULOCK))	/* unlock */
110 #define	ULOCKFS_WLOCK	((1 << LOCKFS_WLOCK))	/* write  lock */
111 #define	ULOCKFS_NLOCK	((1 << LOCKFS_NLOCK))	/* name   lock */
112 #define	ULOCKFS_DLOCK	((1 << LOCKFS_DLOCK))	/* delete lock */
113 #define	ULOCKFS_HLOCK	((1 << LOCKFS_HLOCK))	/* hard   lock */
114 #define	ULOCKFS_ELOCK	((1 << LOCKFS_ELOCK))	/* error  lock */
115 #define	ULOCKFS_ROELOCK	((1 << LOCKFS_ROELOCK)) /* error lock (read-only) */
116 /* Maximum number of LOCKFS lockfs defined in sys/lockfs.h are 6 */
117 #define	ULOCKFS_FWLOCK	(1 << (LOCKFS_MAXLOCK + 1)) /* fallocate write lock */
118 #define	ULOCKFS_SLOCK	0x80000000		/* soft   lock */
119 
120 #define	ULOCKFS_IS_WLOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_WLOCK)
121 #define	ULOCKFS_IS_HLOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_HLOCK)
122 #define	ULOCKFS_IS_ELOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_ELOCK)
123 #define	ULOCKFS_IS_ROELOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_ROELOCK)
124 #define	ULOCKFS_IS_ULOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_ULOCK)
125 #define	ULOCKFS_IS_NLOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_NLOCK)
126 #define	ULOCKFS_IS_DLOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_DLOCK)
127 #define	ULOCKFS_IS_SLOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_SLOCK)
128 #define	ULOCKFS_IS_FWLOCK(LF)	((LF)->ul_fs_lock & ULOCKFS_FWLOCK)
129 #define	ULOCKFS_IS_JUSTULOCK(LF) \
130 	(((LF)->ul_fs_lock & (ULOCKFS_SLOCK | ULOCKFS_ULOCK)) == ULOCKFS_ULOCK)
131 
132 #define	ULOCKFS_SET_SLOCK(LF)	((LF)->ul_fs_lock |= ULOCKFS_SLOCK)
133 #define	ULOCKFS_CLR_SLOCK(LF)	((LF)->ul_fs_lock &= ~ULOCKFS_SLOCK)
134 
135 #define	ULOCKFS_SET_FWLOCK(LF)	((LF)->ul_fs_lock |= ULOCKFS_FWLOCK)
136 #define	ULOCKFS_CLR_FWLOCK(LF)	((LF)->ul_fs_lock &= ~ULOCKFS_FWLOCK)
137 
138 #define	ULOCKFS_READ_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
139 #define	ULOCKFS_WRITE_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
140 			ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
141 /* used by both ufs_getattr and ufs_getsecattr */
142 #define	ULOCKFS_GETATTR_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
143 /* used by both ufs_setattr and ufs_setsecattr */
144 #define	ULOCKFS_SETATTR_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
145 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
146 #define	ULOCKFS_ACCESS_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
147 #define	ULOCKFS_LOOKUP_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
148 #define	ULOCKFS_CREATE_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
149 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
150 #define	ULOCKFS_REMOVE_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
151 			ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK | \
152 					ULOCKFS_NLOCK | ULOCKFS_DLOCK)
153 #define	ULOCKFS_LINK_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
154 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
155 #define	ULOCKFS_RENAME_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
156 					ULOCKFS_SLOCK | ULOCKFS_WLOCK | \
157 					ULOCKFS_ROELOCK | ULOCKFS_NLOCK)
158 #define	ULOCKFS_MKDIR_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
159 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
160 #define	ULOCKFS_RMDIR_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
161 			ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK | \
162 					ULOCKFS_NLOCK | ULOCKFS_DLOCK)
163 #define	ULOCKFS_READDIR_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
164 #define	ULOCKFS_SYMLINK_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
165 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
166 #define	ULOCKFS_READLINK_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
167 #define	ULOCKFS_FSYNC_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
168 #define	ULOCKFS_FID_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
169 #define	ULOCKFS_RWLOCK_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
170 #define	ULOCKFS_RWUNLOCK_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
171 #define	ULOCKFS_SEEK_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
172 #define	ULOCKFS_FRLOCK_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
173 #define	ULOCKFS_SPACE_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
174 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
175 #define	ULOCKFS_FALLOCATE_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
176 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | \
177 				ULOCKFS_WLOCK | ULOCKFS_FWLOCK)
178 #define	ULOCKFS_QUOTA_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
179 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
180 /* GETPAGE breaks up into two masks */
181 #define	ULOCKFS_GETREAD_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
182 #define	ULOCKFS_GETWRITE_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
183 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
184 #define	ULOCKFS_MAP_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
185 #define	ULOCKFS_FIODUTIMES_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
186 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
187 #define	ULOCKFS_FIODIO_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
188 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
189 #define	ULOCKFS_FIODIOS_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
190 #define	ULOCKFS_PATHCONF_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
191 
192 #define	ULOCKFS_VGET_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | ULOCKFS_SLOCK)
193 #define	ULOCKFS_DELETE_MASK	(ULOCKFS_HLOCK | ULOCKFS_ELOCK | \
194 				ULOCKFS_ROELOCK | ULOCKFS_SLOCK | ULOCKFS_WLOCK)
195 
196 struct ulockfs {
197 	ulong_t		ul_flag;	/* flags */
198 	ulong_t		ul_fs_lock;	/* current file system lock state */
199 	ulong_t		ul_fs_mod;	/* for test; fs was modified */
200 	ulong_t		ul_vnops_cnt;	/* # of active ufs vnops outstanding */
201 	kmutex_t	ul_lock;	/* mutex to protect ulockfs structure */
202 	kcondvar_t 	ul_cv;
203 	kthread_id_t	ul_sbowner;	/* thread than can write superblock */
204 	struct lockfs	ul_lockfs;	/* ioctl lock struct */
205 	ulong_t		ul_falloc_cnt;	/* # of on-going fallocate ops */
206 };
207 
208 extern ulong_t ufs_quiesce_pend;
209 
210 #define	VTOUL(VP) \
211 	((struct ulockfs *) \
212 	&((struct ufsvfs *)((VP)->v_vfsp->vfs_data))->vfs_ulockfs)
213 #define	ITOUL(IP)	((struct ulockfs *)&((IP)->i_ufsvfs->vfs_ulockfs))
214 
215 #ifdef	__cplusplus
216 }
217 #endif
218 
219 #endif	/* _SYS_FS_UFS_LOCKFS_H */
220