xref: /illumos-gate/usr/src/uts/common/fs/ufs/ufs_lockfs.c (revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/t_lock.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/signal.h>
36 #include <sys/cred.h>
37 #include <sys/user.h>
38 #include <sys/buf.h>
39 #include <sys/vfs.h>
40 #include <sys/vnode.h>
41 #include <sys/proc.h>
42 #include <sys/disp.h>
43 #include <sys/file.h>
44 #include <sys/fcntl.h>
45 #include <sys/flock.h>
46 #include <sys/atomic.h>
47 #include <sys/kmem.h>
48 #include <sys/uio.h>
49 #include <sys/conf.h>
50 #include <sys/mman.h>
51 #include <sys/pathname.h>
52 #include <sys/debug.h>
53 #include <sys/vmmeter.h>
54 #include <sys/vmsystm.h>
55 #include <sys/cmn_err.h>
56 #include <sys/acct.h>
57 #include <sys/dnlc.h>
58 #include <sys/swap.h>
59 
60 #include <sys/fs/ufs_fs.h>
61 #include <sys/fs/ufs_inode.h>
62 #include <sys/fs/ufs_fsdir.h>
63 #include <sys/fs/ufs_trans.h>
64 #include <sys/fs/ufs_panic.h>
65 #include <sys/fs/ufs_mount.h>
66 #include <sys/fs/ufs_bio.h>
67 #include <sys/fs/ufs_log.h>
68 #include <sys/fs/ufs_quota.h>
69 #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
70 #include <sys/errno.h>
71 #include <sys/sysinfo.h>
72 
73 #include <vm/hat.h>
74 #include <vm/pvn.h>
75 #include <vm/as.h>
76 #include <vm/seg.h>
77 #include <vm/seg_map.h>
78 #include <vm/seg_vn.h>
79 #include <vm/rm.h>
80 #include <vm/anon.h>
81 #include <sys/swap.h>
82 #include <sys/dnlc.h>
83 
84 extern struct vnode *common_specvp(struct vnode *vp);
85 
86 /* error lock status */
87 #define	UN_ERRLCK	(-1)
88 #define	SET_ERRLCK	1
89 #define	RE_ERRLCK	2
90 #define	NO_ERRLCK	0
91 
92 /*
93  * Index to be used in TSD for storing lockfs data
94  */
95 uint_t ufs_lockfs_key;
96 
97 typedef struct _ulockfs_info {
98 	struct _ulockfs_info *next;
99 	struct ulockfs *ulp;
100 	uint_t flags;
101 } ulockfs_info_t;
102 
103 #define	ULOCK_INFO_FALLOCATE	0x00000001	/* fallocate thread */
104 
105 /*
106  * Check in TSD that whether we are already doing any VOP on this filesystem
107  */
108 #define	IS_REC_VOP(found, head, ulp, free)		\
109 {							\
110 	ulockfs_info_t *_curr;				\
111 							\
112 	for (found = 0, free = NULL, _curr = head;	\
113 	    _curr != NULL; _curr = _curr->next) {	\
114 		if ((free == NULL) &&			\
115 		    (_curr->ulp == NULL))		\
116 			free = _curr;			\
117 		if (_curr->ulp == ulp) {		\
118 			found = 1;			\
119 			break;				\
120 		}					\
121 	}						\
122 }
123 
124 /*
125  * Get the lockfs data from TSD so that lockfs handles the recursive VOP
126  * properly
127  */
128 #define	SEARCH_ULOCKFSP(head, ulp, info)		\
129 {							\
130 	ulockfs_info_t *_curr;				\
131 							\
132 	for (_curr = head; _curr != NULL;		\
133 	    _curr = _curr->next) {			\
134 		if (_curr->ulp == ulp) {		\
135 			break;				\
136 		}					\
137 	}						\
138 							\
139 	info = _curr;					\
140 }
141 
142 /*
143  * Validate lockfs request
144  */
145 static int
146 ufs_getlfd(
147 	struct lockfs *lockfsp,		/* new lock request */
148 	struct lockfs *ul_lockfsp)	/* old lock state */
149 {
150 	int	error = 0;
151 
152 	/*
153 	 * no input flags defined
154 	 */
155 	if (lockfsp->lf_flags != 0) {
156 		error = EINVAL;
157 		goto errout;
158 	}
159 
160 	/*
161 	 * check key
162 	 */
163 	if (!LOCKFS_IS_ULOCK(ul_lockfsp))
164 		if (lockfsp->lf_key != ul_lockfsp->lf_key) {
165 			error = EINVAL;
166 			goto errout;
167 	}
168 
169 	lockfsp->lf_key = ul_lockfsp->lf_key + 1;
170 
171 errout:
172 	return (error);
173 }
174 
175 /*
176  * ufs_checkaccton
177  *	check if accounting is turned on on this fs
178  */
179 
180 int
181 ufs_checkaccton(struct vnode *vp)
182 {
183 	if (acct_fs_in_use(vp))
184 		return (EDEADLK);
185 	return (0);
186 }
187 
188 /*
189  * ufs_checkswapon
190  *	check if local swapping is to file on this fs
191  */
192 int
193 ufs_checkswapon(struct vnode *vp)
194 {
195 	struct swapinfo	*sip;
196 
197 	mutex_enter(&swapinfo_lock);
198 	for (sip = swapinfo; sip; sip = sip->si_next)
199 		if (sip->si_vp->v_vfsp == vp->v_vfsp) {
200 			mutex_exit(&swapinfo_lock);
201 			return (EDEADLK);
202 		}
203 	mutex_exit(&swapinfo_lock);
204 	return (0);
205 }
206 
207 /*
208  * ufs_freeze
209  *	pend future accesses for current lock and desired lock
210  */
211 void
212 ufs_freeze(struct ulockfs *ulp, struct lockfs *lockfsp)
213 {
214 	/*
215 	 * set to new lock type
216 	 */
217 	ulp->ul_lockfs.lf_lock = lockfsp->lf_lock;
218 	ulp->ul_lockfs.lf_key = lockfsp->lf_key;
219 	ulp->ul_lockfs.lf_comlen = lockfsp->lf_comlen;
220 	ulp->ul_lockfs.lf_comment = lockfsp->lf_comment;
221 
222 	ulp->ul_fs_lock = (1 << ulp->ul_lockfs.lf_lock);
223 }
224 
225 /*
226  * All callers of ufs_quiesce() atomically increment ufs_quiesce_pend before
227  * starting ufs_quiesce() protocol and decrement it only when a file system no
228  * longer has to be in quiescent state. This allows ufs_pageio() to detect
229  * that another thread wants to quiesce a file system. See more comments in
230  * ufs_pageio().
231  */
232 ulong_t ufs_quiesce_pend = 0;
233 
234 /*
235  * ufs_quiesce
236  *	wait for outstanding accesses to finish
237  */
238 int
239 ufs_quiesce(struct ulockfs *ulp)
240 {
241 	int error = 0;
242 	ulockfs_info_t *head;
243 	ulockfs_info_t *info;
244 
245 	head = (ulockfs_info_t *)tsd_get(ufs_lockfs_key);
246 	SEARCH_ULOCKFSP(head, ulp, info);
247 
248 	/*
249 	 * Set a softlock to suspend future ufs_vnops so that
250 	 * this lockfs request will not be starved
251 	 */
252 	ULOCKFS_SET_SLOCK(ulp);
253 	ASSERT(ufs_quiesce_pend);
254 
255 	/* check if there is any outstanding ufs vnodeops calls */
256 	while (ulp->ul_vnops_cnt || ulp->ul_falloc_cnt) {
257 		/*
258 		 * use timed version of cv_wait_sig() to make sure we don't
259 		 * miss a wake up call from ufs_pageio() when it doesn't use
260 		 * ul_lock.
261 		 *
262 		 * when a fallocate thread comes in, the only way it returns
263 		 * from this function is if there are no other vnode operations
264 		 * going on (remember fallocate threads are tracked using
265 		 * ul_falloc_cnt not ul_vnops_cnt), and another fallocate thread
266 		 * hasn't already grabbed the fs write lock.
267 		 */
268 		if (info && (info->flags & ULOCK_INFO_FALLOCATE)) {
269 			if (!ulp->ul_vnops_cnt && !ULOCKFS_IS_FWLOCK(ulp))
270 				goto out;
271 		}
272 		if (!cv_timedwait_sig(&ulp->ul_cv, &ulp->ul_lock, lbolt + hz)) {
273 			error = EINTR;
274 			goto out;
275 		}
276 	}
277 
278 out:
279 	/*
280 	 * unlock the soft lock
281 	 */
282 	ULOCKFS_CLR_SLOCK(ulp);
283 
284 	return (error);
285 }
286 
287 /*
288  * ufs_flush_inode
289  */
290 int
291 ufs_flush_inode(struct inode *ip, void *arg)
292 {
293 	int	error;
294 	int	saverror	= 0;
295 
296 	/*
297 	 * wrong file system; keep looking
298 	 */
299 	if (ip->i_ufsvfs != (struct ufsvfs *)arg)
300 		return (0);
301 
302 	/*
303 	 * asynchronously push all the dirty pages
304 	 */
305 	if (((error = TRANS_SYNCIP(ip, B_ASYNC, 0, TOP_SYNCIP_FLUSHI)) != 0) &&
306 	    (error != EAGAIN))
307 		saverror = error;
308 	/*
309 	 * wait for io and discard all mappings
310 	 */
311 	if (error = TRANS_SYNCIP(ip, B_INVAL, 0, TOP_SYNCIP_FLUSHI))
312 		saverror = error;
313 
314 	if (ITOV(ip)->v_type == VDIR) {
315 		dnlc_dir_purge(&ip->i_danchor);
316 	}
317 
318 	return (saverror);
319 }
320 
321 /*
322  * ufs_flush
323  *	Flush everything that is currently dirty; this includes invalidating
324  *	any mappings.
325  */
326 int
327 ufs_flush(struct vfs *vfsp)
328 {
329 	int		error;
330 	int		saverror = 0;
331 	struct ufsvfs	*ufsvfsp	= (struct ufsvfs *)vfsp->vfs_data;
332 	struct fs	*fs		= ufsvfsp->vfs_fs;
333 	int		tdontblock = 0;
334 
335 	ASSERT(vfs_lock_held(vfsp));
336 
337 	/*
338 	 * purge dnlc
339 	 */
340 	(void) dnlc_purge_vfsp(vfsp, 0);
341 
342 	/*
343 	 * drain the delete and idle threads
344 	 */
345 	ufs_delete_drain(vfsp, 0, 0);
346 	ufs_idle_drain(vfsp);
347 
348 	/*
349 	 * flush and invalidate quota records
350 	 */
351 	(void) qsync(ufsvfsp);
352 
353 	/*
354 	 * flush w/invalidate the inodes for vfsp
355 	 */
356 	if (error = ufs_scan_inodes(0, ufs_flush_inode, ufsvfsp, ufsvfsp))
357 		saverror = error;
358 
359 	/*
360 	 * synchronously flush superblock and summary info
361 	 */
362 	if (fs->fs_ronly == 0 && fs->fs_fmod) {
363 		fs->fs_fmod = 0;
364 		TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_FLUSH);
365 	}
366 	/*
367 	 * flush w/invalidate block device pages and buf cache
368 	 */
369 	if ((error = VOP_PUTPAGE(common_specvp(ufsvfsp->vfs_devvp),
370 	    (offset_t)0, 0, B_INVAL, CRED(), NULL)) > 0)
371 		saverror = error;
372 
373 	(void) bflush((dev_t)vfsp->vfs_dev);
374 	(void) bfinval((dev_t)vfsp->vfs_dev, 0);
375 
376 	/*
377 	 * drain the delete and idle threads again
378 	 */
379 	ufs_delete_drain(vfsp, 0, 0);
380 	ufs_idle_drain(vfsp);
381 
382 	/*
383 	 * play with the clean flag
384 	 */
385 	if (saverror == 0)
386 		ufs_checkclean(vfsp);
387 
388 	/*
389 	 * Flush any outstanding transactions and roll the log
390 	 * only if we are supposed to do, i.e. LDL_NOROLL not set.
391 	 * We can not simply check for fs_ronly here since fsck also may
392 	 * use this code to roll the log on a read-only filesystem, e.g.
393 	 * root during early stages of boot, if other then a sanity check is
394 	 * done, it will clear LDL_NOROLL before.
395 	 * In addition we assert that the deltamap does not contain any deltas
396 	 * in case LDL_NOROLL is set since this is not supposed to happen.
397 	 */
398 	if (TRANS_ISTRANS(ufsvfsp)) {
399 		ml_unit_t	*ul	= ufsvfsp->vfs_log;
400 		mt_map_t	*mtm	= ul->un_deltamap;
401 
402 		if (ul->un_flags & LDL_NOROLL) {
403 			ASSERT(mtm->mtm_nme == 0);
404 		} else {
405 			/*
406 			 * Do not set T_DONTBLOCK if there is a
407 			 * transaction opened by caller.
408 			 */
409 			if (curthread->t_flag & T_DONTBLOCK)
410 				tdontblock = 1;
411 			else
412 				curthread->t_flag |= T_DONTBLOCK;
413 
414 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_FLUSH,
415 			    TOP_COMMIT_SIZE, error);
416 
417 			if (!error) {
418 				TRANS_END_SYNC(ufsvfsp, saverror,
419 				    TOP_COMMIT_FLUSH, TOP_COMMIT_SIZE);
420 			}
421 
422 			if (tdontblock == 0)
423 				curthread->t_flag &= ~T_DONTBLOCK;
424 
425 			logmap_roll_dev(ufsvfsp->vfs_log);
426 		}
427 	}
428 
429 	return (saverror);
430 }
431 
432 /*
433  * ufs_thaw_wlock
434  *	special processing when thawing down to wlock
435  */
436 static int
437 ufs_thaw_wlock(struct inode *ip, void *arg)
438 {
439 	/*
440 	 * wrong file system; keep looking
441 	 */
442 	if (ip->i_ufsvfs != (struct ufsvfs *)arg)
443 		return (0);
444 
445 	/*
446 	 * iupdat refuses to clear flags if the fs is read only.  The fs
447 	 * may become read/write during the lock and we wouldn't want
448 	 * these inodes being written to disk.  So clear the flags.
449 	 */
450 	rw_enter(&ip->i_contents, RW_WRITER);
451 	ip->i_flag &= ~(IMOD|IMODACC|IACC|IUPD|ICHG|IATTCHG);
452 	rw_exit(&ip->i_contents);
453 
454 	/*
455 	 * pages are mlocked -- fail wlock
456 	 */
457 	if (ITOV(ip)->v_type != VCHR && vn_has_cached_data(ITOV(ip)))
458 		return (EBUSY);
459 
460 	return (0);
461 }
462 
463 /*
464  * ufs_thaw_hlock
465  *	special processing when thawing down to hlock or elock
466  */
467 static int
468 ufs_thaw_hlock(struct inode *ip, void *arg)
469 {
470 	struct vnode	*vp	= ITOV(ip);
471 
472 	/*
473 	 * wrong file system; keep looking
474 	 */
475 	if (ip->i_ufsvfs != (struct ufsvfs *)arg)
476 		return (0);
477 
478 	/*
479 	 * blow away all pages - even if they are mlocked
480 	 */
481 	do {
482 		(void) TRANS_SYNCIP(ip, B_INVAL | B_FORCE, 0, TOP_SYNCIP_HLOCK);
483 	} while ((vp->v_type != VCHR) && vn_has_cached_data(vp));
484 	rw_enter(&ip->i_contents, RW_WRITER);
485 	ip->i_flag &= ~(IMOD|IMODACC|IACC|IUPD|ICHG|IATTCHG);
486 	rw_exit(&ip->i_contents);
487 
488 	return (0);
489 }
490 
491 /*
492  * ufs_thaw
493  *	thaw file system lock down to current value
494  */
495 int
496 ufs_thaw(struct vfs *vfsp, struct ufsvfs *ufsvfsp, struct ulockfs *ulp)
497 {
498 	int		error	= 0;
499 	int		noidel	= (int)(ulp->ul_flag & ULOCKFS_NOIDEL);
500 
501 	/*
502 	 * if wlock or hlock or elock
503 	 */
504 	if (ULOCKFS_IS_WLOCK(ulp) || ULOCKFS_IS_HLOCK(ulp) ||
505 	    ULOCKFS_IS_ELOCK(ulp)) {
506 
507 		/*
508 		 * don't keep access times
509 		 * don't free deleted files
510 		 * if superblock writes are allowed, limit them to me for now
511 		 */
512 		ulp->ul_flag |= (ULOCKFS_NOIACC|ULOCKFS_NOIDEL);
513 		if (ulp->ul_sbowner != (kthread_id_t)-1)
514 			ulp->ul_sbowner = curthread;
515 
516 		/*
517 		 * wait for writes for deleted files and superblock updates
518 		 */
519 		(void) ufs_flush(vfsp);
520 
521 		/*
522 		 * now make sure the quota file is up-to-date
523 		 *	expensive; but effective
524 		 */
525 		error = ufs_flush(vfsp);
526 		/*
527 		 * no one can write the superblock
528 		 */
529 		ulp->ul_sbowner = (kthread_id_t)-1;
530 
531 		/*
532 		 * special processing for wlock/hlock/elock
533 		 */
534 		if (ULOCKFS_IS_WLOCK(ulp)) {
535 			if (error)
536 				goto errout;
537 			error = bfinval(ufsvfsp->vfs_dev, 0);
538 			if (error)
539 				goto errout;
540 			error = ufs_scan_inodes(0, ufs_thaw_wlock,
541 			    (void *)ufsvfsp, ufsvfsp);
542 			if (error)
543 				goto errout;
544 		}
545 		if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
546 			error = 0;
547 			(void) ufs_scan_inodes(0, ufs_thaw_hlock,
548 			    (void *)ufsvfsp, ufsvfsp);
549 			(void) bfinval(ufsvfsp->vfs_dev, 1);
550 		}
551 	} else {
552 
553 		/*
554 		 * okay to keep access times
555 		 * okay to free deleted files
556 		 * okay to write the superblock
557 		 */
558 		ulp->ul_flag &= ~(ULOCKFS_NOIACC|ULOCKFS_NOIDEL);
559 		ulp->ul_sbowner = NULL;
560 
561 		/*
562 		 * flush in case deleted files are in memory
563 		 */
564 		if (noidel) {
565 			if (error = ufs_flush(vfsp))
566 				goto errout;
567 		}
568 	}
569 
570 errout:
571 	cv_broadcast(&ulp->ul_cv);
572 	return (error);
573 }
574 
575 /*
576  * ufs_reconcile_fs
577  *	reconcile incore superblock with ondisk superblock
578  */
579 int
580 ufs_reconcile_fs(struct vfs *vfsp, struct ufsvfs *ufsvfsp, int errlck)
581 {
582 	struct fs	*mfs; 	/* in-memory superblock */
583 	struct fs	*dfs;	/* on-disk   superblock */
584 	struct buf	*bp;	/* on-disk   superblock buf */
585 	int		 needs_unlock;
586 	char		 finished_fsclean;
587 
588 	mfs = ufsvfsp->vfs_fs;
589 
590 	/*
591 	 * get the on-disk copy of the superblock
592 	 */
593 	bp = UFS_BREAD(ufsvfsp, vfsp->vfs_dev, SBLOCK, SBSIZE);
594 	bp->b_flags |= (B_STALE|B_AGE);
595 	if (bp->b_flags & B_ERROR) {
596 		brelse(bp);
597 		return (EIO);
598 	}
599 	dfs = bp->b_un.b_fs;
600 
601 	/* error locks may only unlock after the fs has been made consistent */
602 	if (errlck == UN_ERRLCK) {
603 		if (dfs->fs_clean == FSFIX) {	/* being repaired */
604 			brelse(bp);
605 			return (EAGAIN);
606 		}
607 		/* repair not yet started? */
608 		finished_fsclean = TRANS_ISTRANS(ufsvfsp)? FSLOG: FSCLEAN;
609 		if (dfs->fs_clean != finished_fsclean) {
610 			brelse(bp);
611 			return (EBUSY);
612 		}
613 	}
614 
615 	/*
616 	 * if superblock has changed too much, abort
617 	 */
618 	if ((mfs->fs_sblkno		!= dfs->fs_sblkno) ||
619 	    (mfs->fs_cblkno		!= dfs->fs_cblkno) ||
620 	    (mfs->fs_iblkno		!= dfs->fs_iblkno) ||
621 	    (mfs->fs_dblkno		!= dfs->fs_dblkno) ||
622 	    (mfs->fs_cgoffset		!= dfs->fs_cgoffset) ||
623 	    (mfs->fs_cgmask		!= dfs->fs_cgmask) ||
624 	    (mfs->fs_bsize		!= dfs->fs_bsize) ||
625 	    (mfs->fs_fsize		!= dfs->fs_fsize) ||
626 	    (mfs->fs_frag		!= dfs->fs_frag) ||
627 	    (mfs->fs_bmask		!= dfs->fs_bmask) ||
628 	    (mfs->fs_fmask		!= dfs->fs_fmask) ||
629 	    (mfs->fs_bshift		!= dfs->fs_bshift) ||
630 	    (mfs->fs_fshift		!= dfs->fs_fshift) ||
631 	    (mfs->fs_fragshift		!= dfs->fs_fragshift) ||
632 	    (mfs->fs_fsbtodb		!= dfs->fs_fsbtodb) ||
633 	    (mfs->fs_sbsize		!= dfs->fs_sbsize) ||
634 	    (mfs->fs_nindir		!= dfs->fs_nindir) ||
635 	    (mfs->fs_nspf		!= dfs->fs_nspf) ||
636 	    (mfs->fs_trackskew		!= dfs->fs_trackskew) ||
637 	    (mfs->fs_cgsize		!= dfs->fs_cgsize) ||
638 	    (mfs->fs_ntrak		!= dfs->fs_ntrak) ||
639 	    (mfs->fs_nsect		!= dfs->fs_nsect) ||
640 	    (mfs->fs_spc		!= dfs->fs_spc) ||
641 	    (mfs->fs_cpg		!= dfs->fs_cpg) ||
642 	    (mfs->fs_ipg		!= dfs->fs_ipg) ||
643 	    (mfs->fs_fpg		!= dfs->fs_fpg) ||
644 	    (mfs->fs_postblformat	!= dfs->fs_postblformat) ||
645 	    (mfs->fs_magic		!= dfs->fs_magic)) {
646 		brelse(bp);
647 		return (EACCES);
648 	}
649 	if (dfs->fs_clean == FSBAD || FSOKAY != dfs->fs_state + dfs->fs_time)
650 		if (mfs->fs_clean == FSLOG) {
651 			brelse(bp);
652 			return (EACCES);
653 		}
654 
655 	/*
656 	 * get new summary info
657 	 */
658 	if (ufs_getsummaryinfo(vfsp->vfs_dev, ufsvfsp, dfs)) {
659 		brelse(bp);
660 		return (EIO);
661 	}
662 
663 	/*
664 	 * release old summary info and update in-memory superblock
665 	 */
666 	kmem_free(mfs->fs_u.fs_csp, mfs->fs_cssize);
667 	mfs->fs_u.fs_csp = dfs->fs_u.fs_csp;	/* Only entry 0 used */
668 
669 	/*
670 	 * update fields allowed to change
671 	 */
672 	mfs->fs_size		= dfs->fs_size;
673 	mfs->fs_dsize		= dfs->fs_dsize;
674 	mfs->fs_ncg		= dfs->fs_ncg;
675 	mfs->fs_minfree		= dfs->fs_minfree;
676 	mfs->fs_rotdelay	= dfs->fs_rotdelay;
677 	mfs->fs_rps		= dfs->fs_rps;
678 	mfs->fs_maxcontig	= dfs->fs_maxcontig;
679 	mfs->fs_maxbpg		= dfs->fs_maxbpg;
680 	mfs->fs_csmask		= dfs->fs_csmask;
681 	mfs->fs_csshift		= dfs->fs_csshift;
682 	mfs->fs_optim		= dfs->fs_optim;
683 	mfs->fs_csaddr		= dfs->fs_csaddr;
684 	mfs->fs_cssize		= dfs->fs_cssize;
685 	mfs->fs_ncyl		= dfs->fs_ncyl;
686 	mfs->fs_cstotal		= dfs->fs_cstotal;
687 	mfs->fs_reclaim		= dfs->fs_reclaim;
688 
689 	if (mfs->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
690 		mfs->fs_reclaim &= ~FS_RECLAIM;
691 		mfs->fs_reclaim |=  FS_RECLAIMING;
692 		ufs_thread_start(&ufsvfsp->vfs_reclaim,
693 		    ufs_thread_reclaim, vfsp);
694 	}
695 
696 	/* XXX What to do about sparecon? */
697 
698 	/* XXX need to copy volume label */
699 
700 	/*
701 	 * ondisk clean flag overrides inmemory clean flag iff == FSBAD
702 	 * or if error-locked and ondisk is now clean
703 	 */
704 	needs_unlock = !MUTEX_HELD(&ufsvfsp->vfs_lock);
705 	if (needs_unlock)
706 		mutex_enter(&ufsvfsp->vfs_lock);
707 
708 	if (errlck == UN_ERRLCK) {
709 		if (finished_fsclean == dfs->fs_clean)
710 			mfs->fs_clean = finished_fsclean;
711 		else
712 			mfs->fs_clean = FSBAD;
713 		mfs->fs_state = FSOKAY - dfs->fs_time;
714 	}
715 
716 	if (FSOKAY != dfs->fs_state + dfs->fs_time ||
717 	    (dfs->fs_clean == FSBAD))
718 		mfs->fs_clean = FSBAD;
719 
720 	if (needs_unlock)
721 		mutex_exit(&ufsvfsp->vfs_lock);
722 
723 	brelse(bp);
724 
725 	return (0);
726 }
727 
728 /*
729  * ufs_reconcile_inode
730  *	reconcile ondisk inode with incore inode
731  */
732 static int
733 ufs_reconcile_inode(struct inode *ip, void *arg)
734 {
735 	int		i;
736 	int		ndaddr;
737 	int		niaddr;
738 	struct dinode	*dp;		/* ondisk inode */
739 	struct buf	*bp	= NULL;
740 	uid_t		d_uid;
741 	gid_t		d_gid;
742 	int		error = 0;
743 	struct fs	*fs;
744 
745 	/*
746 	 * not an inode we care about
747 	 */
748 	if (ip->i_ufsvfs != (struct ufsvfs *)arg)
749 		return (0);
750 
751 	fs = ip->i_fs;
752 
753 	/*
754 	 * Inode reconciliation fails: we made the filesystem quiescent
755 	 * and we did a ufs_flush() before calling ufs_reconcile_inode()
756 	 * and thus the inode should not have been changed inbetween.
757 	 * Any discrepancies indicate a logic error and a pretty
758 	 * significant run-state inconsistency we should complain about.
759 	 */
760 	if (ip->i_flag & (IMOD|IMODACC|IACC|IUPD|ICHG|IATTCHG)) {
761 		cmn_err(CE_WARN, "%s: Inode reconciliation failed for"
762 		    "inode %llu", fs->fs_fsmnt, (u_longlong_t)ip->i_number);
763 		return (EINVAL);
764 	}
765 
766 	/*
767 	 * get the dinode
768 	 */
769 	bp = UFS_BREAD(ip->i_ufsvfs,
770 	    ip->i_dev, (daddr_t)fsbtodb(fs, itod(fs, ip->i_number)),
771 	    (int)fs->fs_bsize);
772 	if (bp->b_flags & B_ERROR) {
773 		brelse(bp);
774 		return (EIO);
775 	}
776 	dp  = bp->b_un.b_dino;
777 	dp += itoo(fs, ip->i_number);
778 
779 	/*
780 	 * handle Sun's implementation of EFT
781 	 */
782 	d_uid = (dp->di_suid == UID_LONG) ? dp->di_uid : (uid_t)dp->di_suid;
783 	d_gid = (dp->di_sgid == GID_LONG) ? dp->di_gid : (uid_t)dp->di_sgid;
784 
785 	rw_enter(&ip->i_contents, RW_WRITER);
786 
787 	/*
788 	 * some fields are not allowed to change
789 	 */
790 	if ((ip->i_mode  != dp->di_mode) ||
791 	    (ip->i_gen   != dp->di_gen) ||
792 	    (ip->i_uid   != d_uid) ||
793 	    (ip->i_gid   != d_gid)) {
794 		error = EACCES;
795 		goto out;
796 	}
797 
798 	/*
799 	 * and some are allowed to change
800 	 */
801 	ip->i_size		= dp->di_size;
802 	ip->i_ic.ic_flags	= dp->di_ic.ic_flags;
803 	ip->i_blocks		= dp->di_blocks;
804 	ip->i_nlink		= dp->di_nlink;
805 	if (ip->i_flag & IFASTSYMLNK) {
806 		ndaddr = 1;
807 		niaddr = 0;
808 	} else {
809 		ndaddr = NDADDR;
810 		niaddr = NIADDR;
811 	}
812 	for (i = 0; i < ndaddr; ++i)
813 		ip->i_db[i] = dp->di_db[i];
814 	for (i = 0; i < niaddr; ++i)
815 		ip->i_ib[i] = dp->di_ib[i];
816 
817 out:
818 	rw_exit(&ip->i_contents);
819 	brelse(bp);
820 	return (error);
821 }
822 
823 /*
824  * ufs_reconcile
825  *	reconcile ondisk superblock/inodes with any incore
826  */
827 static int
828 ufs_reconcile(struct vfs *vfsp, struct ufsvfs *ufsvfsp, int errlck)
829 {
830 	int	error = 0;
831 
832 	/*
833 	 * get rid of as much inmemory data as possible
834 	 */
835 	(void) ufs_flush(vfsp);
836 
837 	/*
838 	 * reconcile the superblock and inodes
839 	 */
840 	if (error = ufs_reconcile_fs(vfsp, ufsvfsp, errlck))
841 		return (error);
842 	if (error = ufs_scan_inodes(0, ufs_reconcile_inode, ufsvfsp, ufsvfsp))
843 		return (error);
844 	/*
845 	 * allocation blocks may be incorrect; get rid of them
846 	 */
847 	(void) ufs_flush(vfsp);
848 
849 	return (error);
850 }
851 
852 /*
853  * File system locking
854  */
855 int
856 ufs_fiolfs(struct vnode *vp, struct lockfs *lockfsp, int from_log)
857 {
858 	return (ufs__fiolfs(vp, lockfsp, /* from_user */ 1, from_log));
859 }
860 
861 /* kernel-internal interface, also used by fix-on-panic */
862 int
863 ufs__fiolfs(
864 	struct vnode *vp,
865 	struct lockfs *lockfsp,
866 	int from_user,
867 	int from_log)
868 {
869 	struct ulockfs	*ulp;
870 	struct lockfs	lfs;
871 	int		error;
872 	struct vfs	*vfsp;
873 	struct ufsvfs	*ufsvfsp;
874 	int		 errlck		= NO_ERRLCK;
875 	int		 poll_events	= POLLPRI;
876 	extern struct pollhead ufs_pollhd;
877 	ulockfs_info_t *head;
878 	ulockfs_info_t *info;
879 	int signal = 0;
880 
881 	/* check valid lock type */
882 	if (!lockfsp || lockfsp->lf_lock > LOCKFS_MAXLOCK)
883 		return (EINVAL);
884 
885 	if (!vp || !vp->v_vfsp || !vp->v_vfsp->vfs_data)
886 		return (EIO);
887 
888 	if (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED) /* has been unmounted */
889 		return (EIO);
890 
891 	/* take the lock and check again */
892 	vfs_lock_wait(vp->v_vfsp);
893 	if (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED) {
894 		vfs_unlock(vp->v_vfsp);
895 		return (EIO);
896 	}
897 
898 	vfsp = vp->v_vfsp;
899 	ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
900 	ulp = &ufsvfsp->vfs_ulockfs;
901 
902 	head = (ulockfs_info_t *)tsd_get(ufs_lockfs_key);
903 	SEARCH_ULOCKFSP(head, ulp, info);
904 
905 	/*
906 	 * Suspend both the reclaim thread and the delete thread.
907 	 * This must be done outside the lockfs locking protocol.
908 	 */
909 	ufs_thread_suspend(&ufsvfsp->vfs_reclaim);
910 	ufs_thread_suspend(&ufsvfsp->vfs_delete);
911 
912 	mutex_enter(&ulp->ul_lock);
913 	atomic_add_long(&ufs_quiesce_pend, 1);
914 
915 	/*
916 	 * Quit if there is another lockfs request in progress
917 	 * that is waiting for existing ufs_vnops to complete.
918 	 */
919 	if (ULOCKFS_IS_BUSY(ulp)) {
920 		error = EBUSY;
921 		goto errexit;
922 	}
923 
924 	/* cannot ulocked or downgrade a hard-lock */
925 	if (ULOCKFS_IS_HLOCK(ulp)) {
926 		error = EIO;
927 		goto errexit;
928 	}
929 
930 	/* an error lock may be unlocked or relocked, only */
931 	if (ULOCKFS_IS_ELOCK(ulp)) {
932 		if (!LOCKFS_IS_ULOCK(lockfsp) && !LOCKFS_IS_ELOCK(lockfsp)) {
933 			error = EBUSY;
934 			goto errexit;
935 		}
936 	}
937 
938 	/*
939 	 * a read-only error lock may only be upgraded to an
940 	 * error lock or hard lock
941 	 */
942 	if (ULOCKFS_IS_ROELOCK(ulp)) {
943 		if (!LOCKFS_IS_HLOCK(lockfsp) && !LOCKFS_IS_ELOCK(lockfsp)) {
944 			error = EBUSY;
945 			goto errexit;
946 		}
947 	}
948 
949 	/*
950 	 * until read-only error locks are fully implemented
951 	 * just return EINVAL
952 	 */
953 	if (LOCKFS_IS_ROELOCK(lockfsp)) {
954 		error = EINVAL;
955 		goto errexit;
956 	}
957 
958 	/*
959 	 * an error lock may only be applied if the file system is
960 	 * unlocked or already error locked.
961 	 * (this is to prevent the case where a fs gets changed out from
962 	 * underneath a fs that is locked for backup,
963 	 * that is, name/delete/write-locked.)
964 	 */
965 	if ((!ULOCKFS_IS_ULOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp) &&
966 	    !ULOCKFS_IS_ROELOCK(ulp)) &&
967 	    (LOCKFS_IS_ELOCK(lockfsp) || LOCKFS_IS_ROELOCK(lockfsp))) {
968 		error = EBUSY;
969 		goto errexit;
970 	}
971 
972 	/* get and validate the input lockfs request */
973 	if (error = ufs_getlfd(lockfsp, &ulp->ul_lockfs))
974 		goto errexit;
975 
976 	/*
977 	 * save current ulockfs struct
978 	 */
979 	bcopy(&ulp->ul_lockfs, &lfs, sizeof (struct lockfs));
980 
981 	/*
982 	 * Freeze the file system (pend future accesses)
983 	 */
984 	ufs_freeze(ulp, lockfsp);
985 
986 	/*
987 	 * Set locking in progress because ufs_quiesce may free the
988 	 * ul_lock mutex.
989 	 */
990 	ULOCKFS_SET_BUSY(ulp);
991 	/* update the ioctl copy */
992 	LOCKFS_SET_BUSY(&ulp->ul_lockfs);
993 
994 	/*
995 	 * We  need to unset FWLOCK status before we call ufs_quiesce
996 	 * so that the thread doesnt get suspended. We do this only if
997 	 * this (fallocate) thread requested an unlock operation.
998 	 */
999 	if (info && (info->flags & ULOCK_INFO_FALLOCATE)) {
1000 		if (!ULOCKFS_IS_WLOCK(ulp))
1001 			ULOCKFS_CLR_FWLOCK(ulp);
1002 	}
1003 
1004 	/*
1005 	 * Quiesce (wait for outstanding accesses to finish)
1006 	 */
1007 	if (error = ufs_quiesce(ulp)) {
1008 		/*
1009 		 * Interrupted due to signal. There could still be
1010 		 * pending vnops.
1011 		 */
1012 		signal = 1;
1013 
1014 		/*
1015 		 * We do broadcast because lock-status
1016 		 * could be reverted to old status.
1017 		 */
1018 		cv_broadcast(&ulp->ul_cv);
1019 		goto errout;
1020 	}
1021 
1022 	/*
1023 	 * If the fallocate thread requested a write fs lock operation
1024 	 * then we set fwlock status in the ulp.
1025 	 */
1026 	if (info && (info->flags & ULOCK_INFO_FALLOCATE)) {
1027 		if (ULOCKFS_IS_WLOCK(ulp))
1028 			ULOCKFS_SET_FWLOCK(ulp);
1029 	}
1030 
1031 	/*
1032 	 * can't wlock or (ro)elock fs with accounting or local swap file
1033 	 */
1034 	if ((ULOCKFS_IS_WLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp) ||
1035 	    ULOCKFS_IS_ROELOCK(ulp)) && !from_log) {
1036 		if (error = ufs_checkaccton(vp))
1037 			goto errout;
1038 		if (error = ufs_checkswapon(vp))
1039 			goto errout;
1040 	}
1041 
1042 	/*
1043 	 * save error lock status to pass down to reconcilation
1044 	 * routines and for later cleanup
1045 	 */
1046 	if (LOCKFS_IS_ELOCK(&lfs) && ULOCKFS_IS_ULOCK(ulp))
1047 		errlck = UN_ERRLCK;
1048 
1049 	if (ULOCKFS_IS_ELOCK(ulp) || ULOCKFS_IS_ROELOCK(ulp)) {
1050 		int needs_unlock;
1051 		int needs_sbwrite;
1052 
1053 		poll_events |= POLLERR;
1054 		errlck = LOCKFS_IS_ELOCK(&lfs) || LOCKFS_IS_ROELOCK(&lfs) ?
1055 		    RE_ERRLCK : SET_ERRLCK;
1056 
1057 		needs_unlock = !MUTEX_HELD(&ufsvfsp->vfs_lock);
1058 		if (needs_unlock)
1059 			mutex_enter(&ufsvfsp->vfs_lock);
1060 
1061 		/* disable delayed i/o */
1062 		needs_sbwrite = 0;
1063 
1064 		if (errlck == SET_ERRLCK) {
1065 			ufsvfsp->vfs_fs->fs_clean = FSBAD;
1066 			needs_sbwrite = 1;
1067 		}
1068 
1069 		needs_sbwrite |= ufsvfsp->vfs_dio;
1070 		ufsvfsp->vfs_dio = 0;
1071 
1072 		if (needs_unlock)
1073 			mutex_exit(&ufsvfsp->vfs_lock);
1074 
1075 		if (needs_sbwrite) {
1076 			ulp->ul_sbowner = curthread;
1077 			TRANS_SBWRITE(ufsvfsp, TOP_SBWRITE_STABLE);
1078 
1079 			if (needs_unlock)
1080 				mutex_enter(&ufsvfsp->vfs_lock);
1081 
1082 			ufsvfsp->vfs_fs->fs_fmod = 0;
1083 
1084 			if (needs_unlock)
1085 				mutex_exit(&ufsvfsp->vfs_lock);
1086 		}
1087 	}
1088 
1089 	/*
1090 	 * reconcile superblock and inodes if was wlocked
1091 	 */
1092 	if (LOCKFS_IS_WLOCK(&lfs) || LOCKFS_IS_ELOCK(&lfs)) {
1093 		if (error = ufs_reconcile(vfsp, ufsvfsp, errlck))
1094 			goto errout;
1095 		/*
1096 		 * in case the fs grew; reset the metadata map for logging tests
1097 		 */
1098 		TRANS_MATA_UMOUNT(ufsvfsp);
1099 		TRANS_MATA_MOUNT(ufsvfsp);
1100 		TRANS_MATA_SI(ufsvfsp, ufsvfsp->vfs_fs);
1101 	}
1102 
1103 	/*
1104 	 * At least everything *currently* dirty goes out.
1105 	 */
1106 
1107 	if ((error = ufs_flush(vfsp)) != 0 && !ULOCKFS_IS_HLOCK(ulp) &&
1108 	    !ULOCKFS_IS_ELOCK(ulp))
1109 		goto errout;
1110 
1111 	/*
1112 	 * thaw file system and wakeup pended processes
1113 	 */
1114 	if (error = ufs_thaw(vfsp, ufsvfsp, ulp))
1115 		if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp))
1116 			goto errout;
1117 
1118 	/*
1119 	 * reset modified flag if not already write locked
1120 	 */
1121 	if (!LOCKFS_IS_WLOCK(&lfs))
1122 		ULOCKFS_CLR_MOD(ulp);
1123 
1124 	/*
1125 	 * idle the lock struct
1126 	 */
1127 	ULOCKFS_CLR_BUSY(ulp);
1128 	/* update the ioctl copy */
1129 	LOCKFS_CLR_BUSY(&ulp->ul_lockfs);
1130 
1131 	/*
1132 	 * free current comment
1133 	 */
1134 	if (lfs.lf_comment && lfs.lf_comlen != 0) {
1135 		kmem_free(lfs.lf_comment, lfs.lf_comlen);
1136 		lfs.lf_comment = NULL;
1137 		lfs.lf_comlen = 0;
1138 	}
1139 
1140 	/* do error lock cleanup */
1141 	if (errlck == UN_ERRLCK)
1142 		ufsfx_unlockfs(ufsvfsp);
1143 
1144 	else if (errlck == RE_ERRLCK)
1145 		ufsfx_lockfs(ufsvfsp);
1146 
1147 	/* don't allow error lock from user to invoke panic */
1148 	else if (from_user && errlck == SET_ERRLCK &&
1149 	    !(ufsvfsp->vfs_fsfx.fx_flags & (UFSMNT_ONERROR_PANIC >> 4)))
1150 		(void) ufs_fault(ufsvfsp->vfs_root,
1151 		    ulp->ul_lockfs.lf_comment && ulp->ul_lockfs.lf_comlen > 0 ?
1152 		    ulp->ul_lockfs.lf_comment: "user-applied error lock");
1153 
1154 	atomic_add_long(&ufs_quiesce_pend, -1);
1155 	mutex_exit(&ulp->ul_lock);
1156 	vfs_unlock(vfsp);
1157 
1158 	if (ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs))
1159 		poll_events |= POLLERR;
1160 
1161 	pollwakeup(&ufs_pollhd, poll_events);
1162 
1163 	/*
1164 	 * Allow both the delete thread and the reclaim thread to
1165 	 * continue.
1166 	 */
1167 	ufs_thread_continue(&ufsvfsp->vfs_delete);
1168 	ufs_thread_continue(&ufsvfsp->vfs_reclaim);
1169 
1170 	return (0);
1171 
1172 errout:
1173 	/*
1174 	 * Lock failed. Reset the old lock in ufsvfs if not hard locked.
1175 	 */
1176 	if (!LOCKFS_IS_HLOCK(&ulp->ul_lockfs)) {
1177 		bcopy(&lfs, &ulp->ul_lockfs, sizeof (struct lockfs));
1178 		ulp->ul_fs_lock = (1 << lfs.lf_lock);
1179 	}
1180 
1181 	/*
1182 	 * Don't call ufs_thaw() when there's a signal during
1183 	 * ufs quiesce operation as it can lead to deadlock
1184 	 * with getpage.
1185 	 */
1186 	if (signal == 0)
1187 		(void) ufs_thaw(vfsp, ufsvfsp, ulp);
1188 
1189 	ULOCKFS_CLR_BUSY(ulp);
1190 	LOCKFS_CLR_BUSY(&ulp->ul_lockfs);
1191 
1192 errexit:
1193 	atomic_add_long(&ufs_quiesce_pend, -1);
1194 	mutex_exit(&ulp->ul_lock);
1195 	vfs_unlock(vfsp);
1196 
1197 	/*
1198 	 * Allow both the delete thread and the reclaim thread to
1199 	 * continue.
1200 	 */
1201 	ufs_thread_continue(&ufsvfsp->vfs_delete);
1202 	ufs_thread_continue(&ufsvfsp->vfs_reclaim);
1203 
1204 	return (error);
1205 }
1206 
1207 /*
1208  * fiolfss
1209  * 	return the current file system locking state info
1210  */
1211 int
1212 ufs_fiolfss(struct vnode *vp, struct lockfs *lockfsp)
1213 {
1214 	struct ulockfs	*ulp;
1215 
1216 	if (!vp || !vp->v_vfsp || !VTOI(vp))
1217 		return (EINVAL);
1218 
1219 	/* file system has been forcibly unmounted */
1220 	if (VTOI(vp)->i_ufsvfs == NULL)
1221 		return (EIO);
1222 
1223 	ulp = VTOUL(vp);
1224 
1225 	if (ULOCKFS_IS_HLOCK(ulp)) {
1226 		*lockfsp = ulp->ul_lockfs;	/* structure assignment */
1227 		return (0);
1228 	}
1229 
1230 	mutex_enter(&ulp->ul_lock);
1231 
1232 	*lockfsp = ulp->ul_lockfs;	/* structure assignment */
1233 
1234 	if (ULOCKFS_IS_MOD(ulp))
1235 		lockfsp->lf_flags |= LOCKFS_MOD;
1236 
1237 	mutex_exit(&ulp->ul_lock);
1238 
1239 	return (0);
1240 }
1241 
1242 /*
1243  * ufs_check_lockfs
1244  *	check whether a ufs_vnops conflicts with the file system lock
1245  */
1246 int
1247 ufs_check_lockfs(struct ufsvfs *ufsvfsp, struct ulockfs *ulp, ulong_t mask)
1248 {
1249 	k_sigset_t	smask;
1250 	int		sig, slock;
1251 
1252 	ASSERT(MUTEX_HELD(&ulp->ul_lock));
1253 
1254 	while (ulp->ul_fs_lock & mask) {
1255 		slock = (int)ULOCKFS_IS_SLOCK(ulp);
1256 		if ((curthread->t_flag & T_DONTPEND) && !slock) {
1257 			curthread->t_flag |= T_WOULDBLOCK;
1258 			return (EAGAIN);
1259 		}
1260 		curthread->t_flag &= ~T_WOULDBLOCK;
1261 
1262 		/*
1263 		 * In the case of an onerr umount of the fs, threads could
1264 		 * have blocked before coming into ufs_check_lockfs and
1265 		 * need to check for the special case of ELOCK and
1266 		 * vfs_dontblock being set which would indicate that the fs
1267 		 * is on its way out and will not return therefore making
1268 		 * EIO the appropriate response.
1269 		 */
1270 		if (ULOCKFS_IS_HLOCK(ulp) ||
1271 		    (ULOCKFS_IS_ELOCK(ulp) && ufsvfsp->vfs_dontblock))
1272 			return (EIO);
1273 
1274 		/*
1275 		 * wait for lock status to change
1276 		 */
1277 		if (slock || ufsvfsp->vfs_nointr) {
1278 			cv_wait(&ulp->ul_cv, &ulp->ul_lock);
1279 		} else {
1280 			sigintr(&smask, 1);
1281 			sig = cv_wait_sig(&ulp->ul_cv, &ulp->ul_lock);
1282 			sigunintr(&smask);
1283 			if ((!sig && (ulp->ul_fs_lock & mask)) ||
1284 			    ufsvfsp->vfs_dontblock)
1285 				return (EINTR);
1286 		}
1287 	}
1288 
1289 	if (mask & ULOCKFS_FWLOCK) {
1290 		atomic_add_long(&ulp->ul_falloc_cnt, 1);
1291 		ULOCKFS_SET_FALLOC(ulp);
1292 	} else {
1293 		atomic_add_long(&ulp->ul_vnops_cnt, 1);
1294 	}
1295 
1296 	return (0);
1297 }
1298 
1299 /*
1300  * Check whether we came across the handcrafted lockfs protocol path. We can't
1301  * simply check for T_DONTBLOCK here as one would assume since this can also
1302  * falsely catch recursive VOP's going to a different filesystem, instead we
1303  * check if we already hold the ulockfs->ul_lock mutex.
1304  */
1305 static int
1306 ufs_lockfs_is_under_rawlockfs(struct ulockfs *ulp)
1307 {
1308 	return ((mutex_owner(&ulp->ul_lock) != curthread) ? 0 : 1);
1309 }
1310 
1311 /*
1312  * ufs_lockfs_begin - start the lockfs locking protocol
1313  */
1314 int
1315 ufs_lockfs_begin(struct ufsvfs *ufsvfsp, struct ulockfs **ulpp, ulong_t mask)
1316 {
1317 	int 		error;
1318 	int		rec_vop;
1319 	ushort_t	op_cnt_incremented = 0;
1320 	ulong_t		*ctr;
1321 	struct ulockfs *ulp;
1322 	ulockfs_info_t	*ulockfs_info;
1323 	ulockfs_info_t	*ulockfs_info_free;
1324 	ulockfs_info_t	*ulockfs_info_temp;
1325 
1326 	/*
1327 	 * file system has been forcibly unmounted
1328 	 */
1329 	if (ufsvfsp == NULL)
1330 		return (EIO);
1331 
1332 	*ulpp = ulp = &ufsvfsp->vfs_ulockfs;
1333 
1334 	/*
1335 	 * Do lockfs protocol
1336 	 */
1337 	ulockfs_info = (ulockfs_info_t *)tsd_get(ufs_lockfs_key);
1338 	IS_REC_VOP(rec_vop, ulockfs_info, ulp, ulockfs_info_free);
1339 
1340 	/*
1341 	 * Detect recursive VOP call or handcrafted internal lockfs protocol
1342 	 * path and bail out in that case.
1343 	 */
1344 	if (rec_vop || ufs_lockfs_is_under_rawlockfs(ulp)) {
1345 		*ulpp = NULL;
1346 		return (0);
1347 	} else {
1348 		if (ulockfs_info_free == NULL) {
1349 			if ((ulockfs_info_temp = (ulockfs_info_t *)
1350 			    kmem_zalloc(sizeof (ulockfs_info_t),
1351 			    KM_NOSLEEP)) == NULL) {
1352 				*ulpp = NULL;
1353 				return (ENOMEM);
1354 			}
1355 		}
1356 	}
1357 
1358 	/*
1359 	 * First time VOP call
1360 	 *
1361 	 * Increment the ctr irrespective of the lockfs state. If the lockfs
1362 	 * state is not ULOCKFS_ULOCK, we can decrement it later. However,
1363 	 * before incrementing we need to check if there is a pending quiesce
1364 	 * request because if we have a continuous stream of ufs_lockfs_begin
1365 	 * requests pounding on a few cpu's then the ufs_quiesce thread might
1366 	 * never see the value of zero for ctr - a livelock kind of scenario.
1367 	 */
1368 	ctr = (mask & ULOCKFS_FWLOCK) ?
1369 	    &ulp->ul_falloc_cnt : &ulp->ul_vnops_cnt;
1370 	if (!ULOCKFS_IS_SLOCK(ulp)) {
1371 		atomic_add_long(ctr, 1);
1372 		op_cnt_incremented++;
1373 	}
1374 
1375 	/*
1376 	 * If the lockfs state (indicated by ul_fs_lock) is not just
1377 	 * ULOCKFS_ULOCK, then we will be routed through ufs_check_lockfs
1378 	 * where there is a check with an appropriate mask to selectively allow
1379 	 * operations permitted for that kind of lockfs state.
1380 	 *
1381 	 * Even these selective operations should not be allowed to go through
1382 	 * if a lockfs request is in progress because that could result in inode
1383 	 * modifications during a quiesce and could hence result in inode
1384 	 * reconciliation failures. ULOCKFS_SLOCK alone would not be sufficient,
1385 	 * so make use of ufs_quiesce_pend to disallow vnode operations when a
1386 	 * quiesce is in progress.
1387 	 */
1388 	if (!ULOCKFS_IS_JUSTULOCK(ulp) || ufs_quiesce_pend) {
1389 		if (op_cnt_incremented)
1390 			if (!atomic_add_long_nv(ctr, -1))
1391 				cv_broadcast(&ulp->ul_cv);
1392 		mutex_enter(&ulp->ul_lock);
1393 		error = ufs_check_lockfs(ufsvfsp, ulp, mask);
1394 		mutex_exit(&ulp->ul_lock);
1395 		if (error) {
1396 			if (ulockfs_info_free == NULL)
1397 				kmem_free(ulockfs_info_temp,
1398 				    sizeof (ulockfs_info_t));
1399 			return (error);
1400 		}
1401 	} else {
1402 		/*
1403 		 * This is the common case of file system in a unlocked state.
1404 		 *
1405 		 * If a file system is unlocked, we would expect the ctr to have
1406 		 * been incremented by now. But this will not be true when a
1407 		 * quiesce is winding up - SLOCK was set when we checked before
1408 		 * incrementing the ctr, but by the time we checked for
1409 		 * ULOCKFS_IS_JUSTULOCK, the quiesce thread was gone. It is okay
1410 		 * to take ul_lock and go through the slow path in this uncommon
1411 		 * case.
1412 		 */
1413 		if (op_cnt_incremented == 0) {
1414 			mutex_enter(&ulp->ul_lock);
1415 			error = ufs_check_lockfs(ufsvfsp, ulp, mask);
1416 			if (error) {
1417 				mutex_exit(&ulp->ul_lock);
1418 				if (ulockfs_info_free == NULL)
1419 					kmem_free(ulockfs_info_temp,
1420 					    sizeof (ulockfs_info_t));
1421 				return (error);
1422 			}
1423 			if (mask & ULOCKFS_FWLOCK)
1424 				ULOCKFS_SET_FALLOC(ulp);
1425 			mutex_exit(&ulp->ul_lock);
1426 		} else if (mask & ULOCKFS_FWLOCK) {
1427 			mutex_enter(&ulp->ul_lock);
1428 			ULOCKFS_SET_FALLOC(ulp);
1429 			mutex_exit(&ulp->ul_lock);
1430 		}
1431 	}
1432 
1433 	if (ulockfs_info_free != NULL) {
1434 		ulockfs_info_free->ulp = ulp;
1435 		if (mask & ULOCKFS_FWLOCK)
1436 			ulockfs_info_free->flags |= ULOCK_INFO_FALLOCATE;
1437 	} else {
1438 		ulockfs_info_temp->ulp = ulp;
1439 		ulockfs_info_temp->next = ulockfs_info;
1440 		if (mask & ULOCKFS_FWLOCK)
1441 			ulockfs_info_temp->flags |= ULOCK_INFO_FALLOCATE;
1442 		ASSERT(ufs_lockfs_key != 0);
1443 		(void) tsd_set(ufs_lockfs_key, (void *)ulockfs_info_temp);
1444 	}
1445 
1446 	curthread->t_flag |= T_DONTBLOCK;
1447 	return (0);
1448 }
1449 
1450 /*
1451  * Check whether we are returning from the top level VOP.
1452  */
1453 static int
1454 ufs_lockfs_top_vop_return(ulockfs_info_t *head)
1455 {
1456 	ulockfs_info_t *info;
1457 	int result = 1;
1458 
1459 	for (info = head; info != NULL; info = info->next) {
1460 		if (info->ulp != NULL) {
1461 			result = 0;
1462 			break;
1463 		}
1464 	}
1465 
1466 	return (result);
1467 }
1468 
1469 /*
1470  * ufs_lockfs_end - terminate the lockfs locking protocol
1471  */
1472 void
1473 ufs_lockfs_end(struct ulockfs *ulp)
1474 {
1475 	ulockfs_info_t *info;
1476 	ulockfs_info_t *head;
1477 
1478 	/*
1479 	 * end-of-VOP protocol
1480 	 */
1481 	if (ulp == NULL)
1482 		return;
1483 
1484 	head = (ulockfs_info_t *)tsd_get(ufs_lockfs_key);
1485 	SEARCH_ULOCKFSP(head, ulp, info);
1486 
1487 	/*
1488 	 * If we're called from a first level VOP, we have to have a
1489 	 * valid ulockfs record in the TSD.
1490 	 */
1491 	ASSERT(info != NULL);
1492 
1493 	/*
1494 	 * Invalidate the ulockfs record.
1495 	 */
1496 	info->ulp = NULL;
1497 
1498 	if (ufs_lockfs_top_vop_return(head))
1499 		curthread->t_flag &= ~T_DONTBLOCK;
1500 
1501 	/* fallocate thread */
1502 	if (ULOCKFS_IS_FALLOC(ulp) && info->flags & ULOCK_INFO_FALLOCATE) {
1503 		/* Clear the thread's fallocate state */
1504 		info->flags &= ~ULOCK_INFO_FALLOCATE;
1505 		if (!atomic_add_long_nv(&ulp->ul_falloc_cnt, -1)) {
1506 			mutex_enter(&ulp->ul_lock);
1507 			ULOCKFS_CLR_FALLOC(ulp);
1508 			cv_broadcast(&ulp->ul_cv);
1509 			mutex_exit(&ulp->ul_lock);
1510 		}
1511 	} else  { /* normal thread */
1512 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
1513 			cv_broadcast(&ulp->ul_cv);
1514 	}
1515 }
1516 
1517 /*
1518  * ufs_lockfs_trybegin - try to start the lockfs locking protocol without
1519  * blocking.
1520  */
1521 int
1522 ufs_lockfs_trybegin(struct ufsvfs *ufsvfsp, struct ulockfs **ulpp, ulong_t mask)
1523 {
1524 	int 		error = 0;
1525 	int		rec_vop;
1526 	ushort_t	op_cnt_incremented = 0;
1527 	ulong_t		*ctr;
1528 	struct ulockfs *ulp;
1529 	ulockfs_info_t	*ulockfs_info;
1530 	ulockfs_info_t	*ulockfs_info_free;
1531 	ulockfs_info_t	*ulockfs_info_temp;
1532 
1533 	/*
1534 	 * file system has been forcibly unmounted
1535 	 */
1536 	if (ufsvfsp == NULL)
1537 		return (EIO);
1538 
1539 	*ulpp = ulp = &ufsvfsp->vfs_ulockfs;
1540 
1541 	/*
1542 	 * Do lockfs protocol
1543 	 */
1544 	ulockfs_info = (ulockfs_info_t *)tsd_get(ufs_lockfs_key);
1545 	IS_REC_VOP(rec_vop, ulockfs_info, ulp, ulockfs_info_free);
1546 
1547 	/*
1548 	 * Detect recursive VOP call or handcrafted internal lockfs protocol
1549 	 * path and bail out in that case.
1550 	 */
1551 	if (rec_vop || ufs_lockfs_is_under_rawlockfs(ulp)) {
1552 		*ulpp = NULL;
1553 		return (0);
1554 	} else {
1555 		if (ulockfs_info_free == NULL) {
1556 			if ((ulockfs_info_temp = (ulockfs_info_t *)
1557 			    kmem_zalloc(sizeof (ulockfs_info_t),
1558 			    KM_NOSLEEP)) == NULL) {
1559 				*ulpp = NULL;
1560 				return (ENOMEM);
1561 			}
1562 		}
1563 	}
1564 
1565 	/*
1566 	 * First time VOP call
1567 	 *
1568 	 * Increment the ctr irrespective of the lockfs state. If the lockfs
1569 	 * state is not ULOCKFS_ULOCK, we can decrement it later. However,
1570 	 * before incrementing we need to check if there is a pending quiesce
1571 	 * request because if we have a continuous stream of ufs_lockfs_begin
1572 	 * requests pounding on a few cpu's then the ufs_quiesce thread might
1573 	 * never see the value of zero for ctr - a livelock kind of scenario.
1574 	 */
1575 	ctr = (mask & ULOCKFS_FWLOCK) ?
1576 	    &ulp->ul_falloc_cnt : &ulp->ul_vnops_cnt;
1577 	if (!ULOCKFS_IS_SLOCK(ulp)) {
1578 		atomic_add_long(ctr, 1);
1579 		op_cnt_incremented++;
1580 	}
1581 
1582 	if (!ULOCKFS_IS_JUSTULOCK(ulp) || ufs_quiesce_pend) {
1583 		/*
1584 		 * Non-blocking version of ufs_check_lockfs() code.
1585 		 *
1586 		 * If the file system is not hard locked or error locked
1587 		 * and if ulp->ul_fs_lock allows this operation, increment
1588 		 * the appropriate counter and proceed (For eg., In case the
1589 		 * file system is delete locked, a mmap can still go through).
1590 		 */
1591 		if (op_cnt_incremented)
1592 			if (!atomic_add_long_nv(ctr, -1))
1593 				cv_broadcast(&ulp->ul_cv);
1594 		mutex_enter(&ulp->ul_lock);
1595 		if (ULOCKFS_IS_HLOCK(ulp) ||
1596 		    (ULOCKFS_IS_ELOCK(ulp) && ufsvfsp->vfs_dontblock))
1597 			error = EIO;
1598 		else if (ulp->ul_fs_lock & mask)
1599 			error = EAGAIN;
1600 
1601 		if (error) {
1602 			mutex_exit(&ulp->ul_lock);
1603 			if (ulockfs_info_free == NULL)
1604 				kmem_free(ulockfs_info_temp,
1605 				    sizeof (ulockfs_info_t));
1606 			return (error);
1607 		}
1608 		atomic_add_long(ctr, 1);
1609 		if (mask & ULOCKFS_FWLOCK)
1610 			ULOCKFS_SET_FALLOC(ulp);
1611 		mutex_exit(&ulp->ul_lock);
1612 	} else {
1613 		/*
1614 		 * This is the common case of file system in a unlocked state.
1615 		 *
1616 		 * If a file system is unlocked, we would expect the ctr to have
1617 		 * been incremented by now. But this will not be true when a
1618 		 * quiesce is winding up - SLOCK was set when we checked before
1619 		 * incrementing the ctr, but by the time we checked for
1620 		 * ULOCKFS_IS_JUSTULOCK, the quiesce thread was gone. Take
1621 		 * ul_lock and go through the non-blocking version of
1622 		 * ufs_check_lockfs() code.
1623 		 */
1624 		if (op_cnt_incremented == 0) {
1625 			mutex_enter(&ulp->ul_lock);
1626 			if (ULOCKFS_IS_HLOCK(ulp) ||
1627 			    (ULOCKFS_IS_ELOCK(ulp) && ufsvfsp->vfs_dontblock))
1628 				error = EIO;
1629 			else if (ulp->ul_fs_lock & mask)
1630 				error = EAGAIN;
1631 
1632 			if (error) {
1633 				mutex_exit(&ulp->ul_lock);
1634 				if (ulockfs_info_free == NULL)
1635 					kmem_free(ulockfs_info_temp,
1636 					    sizeof (ulockfs_info_t));
1637 				return (error);
1638 			}
1639 			atomic_add_long(ctr, 1);
1640 			if (mask & ULOCKFS_FWLOCK)
1641 				ULOCKFS_SET_FALLOC(ulp);
1642 			mutex_exit(&ulp->ul_lock);
1643 		} else if (mask & ULOCKFS_FWLOCK) {
1644 			mutex_enter(&ulp->ul_lock);
1645 			ULOCKFS_SET_FALLOC(ulp);
1646 			mutex_exit(&ulp->ul_lock);
1647 		}
1648 	}
1649 
1650 	if (ulockfs_info_free != NULL) {
1651 		ulockfs_info_free->ulp = ulp;
1652 		if (mask & ULOCKFS_FWLOCK)
1653 			ulockfs_info_free->flags |= ULOCK_INFO_FALLOCATE;
1654 	} else {
1655 		ulockfs_info_temp->ulp = ulp;
1656 		ulockfs_info_temp->next = ulockfs_info;
1657 		if (mask & ULOCKFS_FWLOCK)
1658 			ulockfs_info_temp->flags |= ULOCK_INFO_FALLOCATE;
1659 		ASSERT(ufs_lockfs_key != 0);
1660 		(void) tsd_set(ufs_lockfs_key, (void *)ulockfs_info_temp);
1661 	}
1662 
1663 	curthread->t_flag |= T_DONTBLOCK;
1664 	return (0);
1665 }
1666 
1667 /*
1668  * specialized version of ufs_lockfs_begin() called by ufs_getpage().
1669  */
1670 int
1671 ufs_lockfs_begin_getpage(
1672 	struct ufsvfs	*ufsvfsp,
1673 	struct ulockfs	**ulpp,
1674 	struct seg	*seg,
1675 	int		read_access,
1676 	uint_t		*protp)
1677 {
1678 	ulong_t			mask;
1679 	int 			error;
1680 	int			rec_vop;
1681 	struct ulockfs		*ulp;
1682 	ulockfs_info_t		*ulockfs_info;
1683 	ulockfs_info_t		*ulockfs_info_free;
1684 	ulockfs_info_t		*ulockfs_info_temp;
1685 
1686 	/*
1687 	 * file system has been forcibly unmounted
1688 	 */
1689 	if (ufsvfsp == NULL)
1690 		return (EIO);
1691 
1692 	*ulpp = ulp = &ufsvfsp->vfs_ulockfs;
1693 
1694 	/*
1695 	 * Do lockfs protocol
1696 	 */
1697 	ulockfs_info = (ulockfs_info_t *)tsd_get(ufs_lockfs_key);
1698 	IS_REC_VOP(rec_vop, ulockfs_info, ulp, ulockfs_info_free);
1699 
1700 	/*
1701 	 * Detect recursive VOP call or handcrafted internal lockfs protocol
1702 	 * path and bail out in that case.
1703 	 */
1704 	if (rec_vop || ufs_lockfs_is_under_rawlockfs(ulp)) {
1705 		*ulpp = NULL;
1706 		return (0);
1707 	} else {
1708 		if (ulockfs_info_free == NULL) {
1709 			if ((ulockfs_info_temp = (ulockfs_info_t *)
1710 			    kmem_zalloc(sizeof (ulockfs_info_t),
1711 			    KM_NOSLEEP)) == NULL) {
1712 				*ulpp = NULL;
1713 				return (ENOMEM);
1714 			}
1715 		}
1716 	}
1717 
1718 	/*
1719 	 * First time VOP call
1720 	 */
1721 	atomic_add_long(&ulp->ul_vnops_cnt, 1);
1722 	if (!ULOCKFS_IS_JUSTULOCK(ulp) || ufs_quiesce_pend) {
1723 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
1724 			cv_broadcast(&ulp->ul_cv);
1725 		mutex_enter(&ulp->ul_lock);
1726 		if (seg->s_ops == &segvn_ops &&
1727 		    ((struct segvn_data *)seg->s_data)->type != MAP_SHARED) {
1728 			mask = (ulong_t)ULOCKFS_GETREAD_MASK;
1729 		} else if (protp && read_access) {
1730 			/*
1731 			 * Restrict the mapping to readonly.
1732 			 * Writes to this mapping will cause
1733 			 * another fault which will then
1734 			 * be suspended if fs is write locked
1735 			 */
1736 			*protp &= ~PROT_WRITE;
1737 			mask = (ulong_t)ULOCKFS_GETREAD_MASK;
1738 		} else
1739 			mask = (ulong_t)ULOCKFS_GETWRITE_MASK;
1740 
1741 		/*
1742 		 * will sleep if this fs is locked against this VOP
1743 		 */
1744 		error = ufs_check_lockfs(ufsvfsp, ulp, mask);
1745 		mutex_exit(&ulp->ul_lock);
1746 		if (error) {
1747 			if (ulockfs_info_free == NULL)
1748 				kmem_free(ulockfs_info_temp,
1749 				    sizeof (ulockfs_info_t));
1750 			return (error);
1751 		}
1752 	}
1753 
1754 	if (ulockfs_info_free != NULL) {
1755 		ulockfs_info_free->ulp = ulp;
1756 	} else {
1757 		ulockfs_info_temp->ulp = ulp;
1758 		ulockfs_info_temp->next = ulockfs_info;
1759 		ASSERT(ufs_lockfs_key != 0);
1760 		(void) tsd_set(ufs_lockfs_key, (void *)ulockfs_info_temp);
1761 	}
1762 
1763 	curthread->t_flag |= T_DONTBLOCK;
1764 	return (0);
1765 }
1766 
1767 void
1768 ufs_lockfs_tsd_destructor(void *head)
1769 {
1770 	ulockfs_info_t *curr = (ulockfs_info_t *)head;
1771 	ulockfs_info_t *temp;
1772 
1773 	for (; curr != NULL; ) {
1774 		/*
1775 		 * The TSD destructor is being called when the thread exits
1776 		 * (via thread_exit()). At that time it must have cleaned up
1777 		 * all VOPs via ufs_lockfs_end() and there must not be a
1778 		 * valid ulockfs record exist while a thread is exiting.
1779 		 */
1780 		temp = curr;
1781 		curr = curr->next;
1782 		ASSERT(temp->ulp == NULL);
1783 		kmem_free(temp, sizeof (ulockfs_info_t));
1784 	}
1785 }
1786