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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Copyright (c) 2017 by Delphix. All rights reserved.
34 * Portions of this source code were derived from Berkeley 4.3 BSD
35 * under license from the Regents of the University of California.
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
43 #include <sys/vnode.h>
46 #include <sys/callb.h>
47 #include <sys/cpuvar.h>
48 #include <sys/fs/ufs_inode.h>
49 #include <sys/fs/ufs_log.h>
50 #include <sys/fs/ufs_trans.h>
51 #include <sys/fs/ufs_acl.h>
52 #include <sys/fs/ufs_bio.h>
53 #include <sys/fs/ufs_fsdir.h>
54 #include <sys/debug.h>
55 #include <sys/cmn_err.h>
56 #include <sys/sysmacros.h>
59 extern pri_t minclsyspri
;
60 extern int hash2ints();
61 extern struct kmem_cache
*inode_cache
; /* cache of free inodes */
62 extern int ufs_idle_waiters
;
63 extern struct instats ins
;
65 static void ufs_attr_purge(struct inode
*);
68 * initialize a thread's queue struct
71 ufs_thread_init(struct ufs_q
*uq
, int lowat
)
73 bzero((caddr_t
)uq
, sizeof (*uq
));
74 cv_init(&uq
->uq_cv
, NULL
, CV_DEFAULT
, NULL
);
75 mutex_init(&uq
->uq_mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
77 uq
->uq_hiwat
= 2 * lowat
;
78 uq
->uq_threadp
= NULL
;
82 * start a thread for a queue (assumes success)
85 ufs_thread_start(struct ufs_q
*uq
, void (*func
)(), struct vfs
*vfsp
)
87 mutex_enter(&uq
->uq_mutex
);
88 if (uq
->uq_threadp
== NULL
) {
89 uq
->uq_threadp
= thread_create(NULL
, 0, func
, vfsp
, 0, &p0
,
93 mutex_exit(&uq
->uq_mutex
);
97 * wait for the thread to exit
100 ufs_thread_exit(struct ufs_q
*uq
)
102 kt_did_t ufs_thread_did
= 0;
104 mutex_enter(&uq
->uq_mutex
);
105 uq
->uq_flags
&= ~(UQ_SUSPEND
| UQ_SUSPENDED
);
106 if (uq
->uq_threadp
!= NULL
) {
107 ufs_thread_did
= uq
->uq_threadp
->t_did
;
108 uq
->uq_flags
|= (UQ_EXIT
|UQ_WAIT
);
109 cv_broadcast(&uq
->uq_cv
);
111 mutex_exit(&uq
->uq_mutex
);
114 * It's safe to call thread_join() with an already-gone
115 * t_did, but we have to obtain it before the kernel
116 * thread structure is freed. We do so above under the
117 * protection of the uq_mutex when we're sure the thread
118 * still exists and it's save to de-reference it.
119 * We also have to check if ufs_thread_did is != 0
120 * before calling thread_join() since thread 0 in the system
124 thread_join(ufs_thread_did
);
128 * wait for a thread to suspend itself on the caller's behalf
129 * the caller is responsible for continuing the thread
132 ufs_thread_suspend(struct ufs_q
*uq
)
134 mutex_enter(&uq
->uq_mutex
);
135 if (uq
->uq_threadp
!= NULL
) {
137 * wait while another thread is suspending this thread.
138 * no need to do a cv_broadcast(), as whoever suspended
139 * the thread must continue it at some point.
141 while ((uq
->uq_flags
& UQ_SUSPEND
) &&
142 (uq
->uq_threadp
!= NULL
)) {
144 * We can't use cv_signal() because if our
145 * signal doesn't happen to hit the desired
146 * thread but instead some other waiter like
147 * ourselves, we'll wait forever for a
148 * response. Well, at least an indeterminate
149 * amount of time until we just happen to get
150 * lucky from whomever did get signalled doing
151 * a cv_signal() of their own. This is an
152 * unfortunate performance lossage.
154 uq
->uq_flags
|= UQ_WAIT
;
155 cv_wait(&uq
->uq_cv
, &uq
->uq_mutex
);
158 uq
->uq_flags
|= (UQ_SUSPEND
| UQ_WAIT
);
161 * wait for the thread to suspend itself
163 if ((uq
->uq_flags
& UQ_SUSPENDED
) == 0 &&
164 (uq
->uq_threadp
!= NULL
)) {
165 cv_broadcast(&uq
->uq_cv
);
168 while (((uq
->uq_flags
& UQ_SUSPENDED
) == 0) &&
169 (uq
->uq_threadp
!= NULL
)) {
170 cv_wait(&uq
->uq_cv
, &uq
->uq_mutex
);
173 mutex_exit(&uq
->uq_mutex
);
177 * allow a thread to continue from a ufs_thread_suspend()
178 * This thread must be the same as the thread that called
179 * ufs_thread_suspend.
182 ufs_thread_continue(struct ufs_q
*uq
)
184 mutex_enter(&uq
->uq_mutex
);
185 uq
->uq_flags
&= ~(UQ_SUSPEND
| UQ_SUSPENDED
);
186 cv_broadcast(&uq
->uq_cv
);
187 mutex_exit(&uq
->uq_mutex
);
191 * some common code for managing a threads execution
192 * uq is locked at entry and return
197 * Kind of a hack passing in the callb_cpr_t * here.
198 * It should really be part of the ufs_q structure.
199 * I did not put it in there because we are already in beta
200 * and I was concerned that changing ufs_inode.h to include
201 * callb.h might break something.
204 ufs_thread_run(struct ufs_q
*uq
, callb_cpr_t
*cprinfop
)
207 ASSERT(uq
->uq_ne
>= 0);
209 if (uq
->uq_flags
& UQ_SUSPEND
) {
210 uq
->uq_flags
|= UQ_SUSPENDED
;
211 } else if (uq
->uq_flags
& UQ_EXIT
) {
213 * exiting; empty the queue (may infinite loop)
217 uq
->uq_threadp
= NULL
;
218 if (uq
->uq_flags
& UQ_WAIT
) {
219 cv_broadcast(&uq
->uq_cv
);
221 uq
->uq_flags
&= ~(UQ_EXIT
| UQ_WAIT
);
222 CALLB_CPR_EXIT(cprinfop
);
224 } else if (uq
->uq_ne
>= uq
->uq_lowat
) {
226 * process a block of entries until below high water mark
228 return (uq
->uq_ne
- (uq
->uq_lowat
>> 1));
230 if (uq
->uq_flags
& UQ_WAIT
) {
231 uq
->uq_flags
&= ~UQ_WAIT
;
232 cv_broadcast(&uq
->uq_cv
);
234 CALLB_CPR_SAFE_BEGIN(cprinfop
);
235 cv_wait(&uq
->uq_cv
, &uq
->uq_mutex
);
236 CALLB_CPR_SAFE_END(cprinfop
, &uq
->uq_mutex
);
242 * The following routines implement the protocol for freeing the resources
243 * held by an idle and deleted inode.
246 ufs_delete(struct ufsvfs
*ufsvfsp
, struct inode
*ip
, int dolockfs
)
249 struct vnode
*vp
= ITOV(ip
);
252 int dorwlock
= ((ip
->i_mode
& IFMT
) == IFREG
);
256 struct ufs_q
*delq
= &ufsvfsp
->vfs_delete
;
257 struct ufs_delq_info
*delq_info
= &ufsvfsp
->vfs_delete_info
;
260 * Ignore if deletes are not allowed (wlock/hlock)
262 if (ULOCKFS_IS_NOIDEL(ITOUL(ip
))) {
263 mutex_enter(&delq
->uq_mutex
);
264 delq_info
->delq_unreclaimed_blocks
-= ip
->i_blocks
;
265 delq_info
->delq_unreclaimed_files
--;
266 mutex_exit(&delq
->uq_mutex
);
271 if ((vp
->v_count
> 1) || (ip
->i_mode
== 0)) {
272 mutex_enter(&delq
->uq_mutex
);
273 delq_info
->delq_unreclaimed_blocks
-= ip
->i_blocks
;
274 delq_info
->delq_unreclaimed_files
--;
275 mutex_exit(&delq
->uq_mutex
);
280 * If we are called as part of setting a fs lock, then only
281 * do part of the lockfs protocol. In other words, don't hang.
284 if (ufs_lockfs_begin(ufsvfsp
, &ulp
, ULOCKFS_DELETE_MASK
))
288 * check for recursive VOP call
290 if (curthread
->t_flag
& T_DONTBLOCK
) {
293 ulp
= &ufsvfsp
->vfs_ulockfs
;
294 curthread
->t_flag
|= T_DONTBLOCK
;
299 * Hold rwlock to synchronize with (nfs) writes
302 rw_enter(&ip
->i_rwlock
, RW_WRITER
);
305 * Delete the attribute directory.
307 if (ip
->i_oeftflag
!= 0) {
308 TRANS_BEGIN_CSYNC(ufsvfsp
, &issync
, TOP_REMOVE
,
309 trans_size
= (int)TOP_REMOVE_SIZE(ip
));
310 rw_enter(&ip
->i_contents
, RW_WRITER
);
311 err
= ufs_iget(ip
->i_vfs
, ip
->i_oeftflag
,
314 rw_enter(&dp
->i_rwlock
, RW_WRITER
);
315 rw_enter(&dp
->i_contents
, RW_WRITER
);
316 dp
->i_flag
|= IUPD
|ICHG
;
318 TRANS_INODE(dp
->i_ufsvfs
, dp
);
322 * Should get rid of any negative cache entries that
323 * might be lingering, as well as ``.'' and
324 * ``..''. If we don't, the VN_RELE() below
325 * won't actually put dp on the delete queue
326 * and it'll hang out until someone forces it
327 * (lockfs -f, umount, ...). The only reliable
328 * way of doing this at the moment is to call
329 * dnlc_purge_vp(ITOV(dp)), which is unacceptably
330 * slow, so we'll just note the problem in this
333 dnlc_remove(ITOV(dp
), ".");
334 dnlc_remove(ITOV(dp
), "..");
336 if (!TRANS_ISTRANS(ufsvfsp
)) {
337 ufs_iupdat(dp
, I_SYNC
);
339 rw_exit(&dp
->i_contents
);
340 rw_exit(&dp
->i_rwlock
);
344 * Clear out attribute pointer
347 rw_exit(&ip
->i_contents
);
348 TRANS_END_CSYNC(ufsvfsp
, &err
, issync
, TOP_REMOVE
, trans_size
);
349 dnlc_remove(ITOV(ip
), XATTR_DIR_NAME
);
352 if ((ip
->i_mode
& IFMT
) == IFATTRDIR
) {
356 (void) TRANS_ITRUNC(ip
, 0, I_FREE
| I_ACCT
, CRED());
359 * the inode's space has been freed; now free the inode
362 trans_size
= TOP_IFREE_SIZE(ip
);
363 TRANS_BEGIN_ASYNC(ufsvfsp
, TOP_IFREE
, trans_size
);
365 rw_enter(&ufsvfsp
->vfs_dqrwlock
, RW_READER
);
366 rw_enter(&ip
->i_contents
, RW_WRITER
);
367 TRANS_INODE(ufsvfsp
, ip
);
374 (void) ufs_si_free(ip
->i_ufs_acl
, vp
->v_vfsp
, CRED());
375 ip
->i_ufs_acl
= NULL
;
380 * This inode is torn down but still retains it's identity
381 * (inode number). It could get recycled soon so it's best
382 * to clean up the vnode just in case.
384 mutex_enter(&vp
->v_lock
);
386 mutex_exit(&vp
->v_lock
);
391 ufs_ifree(ip
, ip
->i_number
, mode
);
393 * release quota resources; can't fail
395 (void) chkiq((struct ufsvfs
*)vp
->v_vfsp
->vfs_data
, /* change */ -1,
396 ip
, (uid_t
)ip
->i_uid
, 0, CRED(), (char **)NULL
, NULL
);
399 ip
->i_flag
&= ~(IDEL
| IDIRECTIO
);
401 if (!TRANS_ISTRANS(ufsvfsp
)) {
402 ufs_iupdat(ip
, I_SYNC
);
404 mutex_enter(&delq
->uq_mutex
);
405 delq_info
->delq_unreclaimed_files
--;
406 mutex_exit(&delq
->uq_mutex
);
408 rw_exit(&ip
->i_contents
);
409 rw_exit(&ufsvfsp
->vfs_dqrwlock
);
411 rw_exit(&ip
->i_rwlock
);
418 TRANS_END_ASYNC(ufsvfsp
, TOP_IFREE
, trans_size
);
422 curthread
->t_flag
&= ~T_DONTBLOCK
;
427 * Create the delete thread and init the delq_info for this fs
430 ufs_delete_init(struct ufsvfs
*ufsvfsp
, int lowat
)
432 struct ufs_delq_info
*delq_info
= &ufsvfsp
->vfs_delete_info
;
434 ufs_thread_init(&ufsvfsp
->vfs_delete
, lowat
);
435 (void) memset(delq_info
, 0, sizeof (*delq_info
));
439 * thread that frees up deleted inodes
442 ufs_thread_delete(struct vfs
*vfsp
)
444 struct ufsvfs
*ufsvfsp
= (struct ufsvfs
*)vfsp
->vfs_data
;
445 struct ufs_q
*uq
= &ufsvfsp
->vfs_delete
;
450 CALLB_CPR_INIT(&cprinfo
, &uq
->uq_mutex
, callb_generic_cpr
,
453 mutex_enter(&uq
->uq_mutex
);
456 * Sleep until there is work to do. Only do one entry at
457 * a time, to reduce the wait time for checking for a suspend
458 * request. The ?: is for pedantic portability.
460 ne
= ufs_thread_run(uq
, &cprinfo
) ? 1 : 0;
463 * process an entry, if there are any
465 if (ne
&& (ip
= uq
->uq_ihead
)) {
467 * process first entry on queue. Assumed conditions are:
468 * ip is held (v_count >= 1)
469 * ip is referenced (i_flag & IREF)
470 * ip is free (i_nlink <= 0)
472 if ((uq
->uq_ihead
= ip
->i_freef
) == ip
)
474 ip
->i_freef
->i_freeb
= ip
->i_freeb
;
475 ip
->i_freeb
->i_freef
= ip
->i_freef
;
479 mutex_exit(&uq
->uq_mutex
);
480 ufs_delete(ufsvfsp
, ip
, 1);
481 mutex_enter(&uq
->uq_mutex
);
487 * drain ne entries off the delete queue. As new queue entries may
488 * be added while we're working, ne is interpreted as follows:
490 * ne > 0 => remove up to ne entries
491 * ne == 0 => remove all entries currently on the queue
492 * ne == -1 => remove entries until the queue is empty
495 ufs_delete_drain(struct vfs
*vfsp
, int ne
, int dolockfs
)
497 struct ufsvfs
*ufsvfsp
= (struct ufsvfs
*)vfsp
->vfs_data
;
504 * if forcibly unmounted; ignore
509 uq
= &ufsvfsp
->vfs_delete
;
510 mutex_enter(&uq
->uq_mutex
);
512 drain_cnt
= uq
->uq_ne
;
517 * process up to ne entries
521 while (!done
&& (ip
= uq
->uq_ihead
)) {
524 if (ne
!= -1 && drain_cnt
== 0)
526 if ((uq
->uq_ihead
= ip
->i_freef
) == ip
)
528 ip
->i_freef
->i_freeb
= ip
->i_freeb
;
529 ip
->i_freeb
->i_freef
= ip
->i_freef
;
533 mutex_exit(&uq
->uq_mutex
);
534 ufs_delete(ufsvfsp
, ip
, dolockfs
);
535 mutex_enter(&uq
->uq_mutex
);
537 mutex_exit(&uq
->uq_mutex
);
541 ufs_sync_with_thread(struct ufs_q
*uq
)
543 mutex_enter(&uq
->uq_mutex
);
546 * Wake up delete thread to free up space.
548 if ((uq
->uq_flags
& UQ_WAIT
) == 0) {
549 uq
->uq_flags
|= UQ_WAIT
;
550 cv_broadcast(&uq
->uq_cv
);
553 while ((uq
->uq_threadp
!= NULL
) && (uq
->uq_flags
& UQ_WAIT
)) {
554 cv_wait(&uq
->uq_cv
, &uq
->uq_mutex
);
557 mutex_exit(&uq
->uq_mutex
);
561 * Get rid of everything that's currently in the delete queue,
562 * plus whatever the delete thread is working on at the moment.
564 * This ability is required for providing true POSIX semantics
565 * regarding close(2), unlink(2), etc, even when logging is enabled.
566 * The standard requires that the released space be immediately
567 * observable (statvfs(2)) and allocatable (e.g., write(2)).
570 ufs_delete_drain_wait(struct ufsvfs
*ufsvfsp
, int dolockfs
)
572 struct ufs_q
*uq
= &ufsvfsp
->vfs_delete
;
574 struct ufs_q
*delq
= &ufsvfsp
->vfs_delete
;
575 struct ufs_delq_info
*delq_info
= &ufsvfsp
->vfs_delete_info
;
578 * If there is something on delq or delete thread
581 mutex_enter(&delq
->uq_mutex
);
582 if (delq_info
->delq_unreclaimed_files
> 0) {
583 mutex_exit(&delq
->uq_mutex
);
584 (void) ufs_delete_drain(ufsvfsp
->vfs_vfs
, 0, dolockfs
);
585 ufs_sync_with_thread(uq
);
587 ASSERT(delq_info
->delq_unreclaimed_files
== 0);
588 mutex_exit(&delq
->uq_mutex
);
593 * Commit any outstanding transactions to make sure
594 * any canceled freed blocks are available for allocation.
596 curthread
->t_flag
|= T_DONTBLOCK
;
597 TRANS_BEGIN_SYNC(ufsvfsp
, TOP_COMMIT_UPDATE
, TOP_COMMIT_SIZE
, &error
);
599 TRANS_END_SYNC(ufsvfsp
, &error
, TOP_COMMIT_UPDATE
,
602 curthread
->t_flag
&= ~T_DONTBLOCK
;
606 * Adjust the resource usage in a struct statvfs based on
607 * what's in the delete queue.
609 * We do not consider the impact of ACLs or extended attributes
610 * that may be deleted as a side-effect of deleting a file.
611 * Those are metadata, and their sizes aren't reflected in the
612 * sizes returned by stat(), so this is not a problem.
615 ufs_delete_adjust_stats(struct ufsvfs
*ufsvfsp
, struct statvfs64
*sp
)
617 struct ufs_q
*uq
= &ufsvfsp
->vfs_delete
;
618 struct ufs_delq_info
*delq_info
= &ufsvfsp
->vfs_delete_info
;
620 mutex_enter(&uq
->uq_mutex
);
622 * The blocks accounted for in the delete queue info are
623 * counted in DEV_BSIZE chunks, but ufs_statvfs counts in
624 * filesystem fragments, so a conversion is required here.
626 sp
->f_bfree
+= dbtofsb(ufsvfsp
->vfs_fs
,
627 delq_info
->delq_unreclaimed_blocks
);
628 sp
->f_ffree
+= delq_info
->delq_unreclaimed_files
;
629 mutex_exit(&uq
->uq_mutex
);
634 * The following routines implement the protocol for maintaining an
635 * LRU list of idle inodes and for moving the idle inodes to the
636 * reuse list when the number of allocated inodes exceeds the user
637 * tunable high-water mark (ufs_ninode).
641 * clean an idle inode and move it to the reuse list
644 ufs_idle_free(struct inode
*ip
)
649 struct ufsvfs
*ufsvfsp
= ip
->i_ufsvfs
;
650 struct vnode
*vp
= ITOV(ip
);
651 int vn_has_data
, vn_modified
;
658 * remember `pages' for stats below
660 pages
= (ip
->i_mode
&& vn_has_cached_data(vp
) && vp
->v_type
!= VCHR
);
663 * start the dirty pages to disk and then invalidate them
664 * unless the inode is invalid (ISTALE)
666 if ((ip
->i_flag
& ISTALE
) == 0) {
667 (void) TRANS_SYNCIP(ip
, B_ASYNC
, I_ASYNC
, TOP_SYNCIP_FREE
);
668 (void) TRANS_SYNCIP(ip
,
669 (TRANS_ISERROR(ufsvfsp
)) ? B_INVAL
| B_FORCE
: B_INVAL
,
670 I_ASYNC
, TOP_SYNCIP_FREE
);
674 * wait for any current ufs_iget to finish and block future ufs_igets
676 ASSERT(ip
->i_number
!= 0);
677 hno
= INOHASH(ip
->i_number
);
682 * It must be guaranteed that v_count >= 2, otherwise
683 * something must be wrong with this vnode already.
684 * That is why we use VN_RELE_LOCKED() instead of VN_RELE().
685 * Acquire the vnode lock in case another thread is in
688 mutex_enter(&vp
->v_lock
);
690 VERIFY3U(vp
->v_count
, >=, 2);
694 vn_has_data
= (vp
->v_type
!= VCHR
&& vn_has_cached_data(vp
));
695 vn_modified
= (ip
->i_flag
& (IMOD
|IMODACC
|IACC
|ICHG
|IUPD
|IATTCHG
));
697 if (vp
->v_count
!= 1 ||
698 ((vn_has_data
|| vn_modified
) &&
699 ((ip
->i_flag
& ISTALE
) == 0))) {
701 * Another thread has referenced this inode while
702 * we are trying to free it. Call VN_RELE() to
703 * release our reference, if v_count > 1 data is
704 * present or one of the modified etc. flags was
705 * set, whereby ISTALE wasn't set.
706 * If we'd proceed with ISTALE set here, we might
707 * get ourselves into a deadlock situation.
709 mutex_exit(&vp
->v_lock
);
714 * The inode is currently unreferenced and can not
715 * acquire further references because it has no pages
716 * and the hash is locked. Inodes acquire references
717 * via the hash list or via their pages.
720 mutex_exit(&vp
->v_lock
);
723 * remove it from the cache
728 * Stale inodes have no valid ufsvfs
730 if ((ip
->i_flag
& ISTALE
) == 0 && ip
->i_dquot
) {
731 TRANS_DQRELE(ufsvfsp
, ip
->i_dquot
);
734 if ((ip
->i_flag
& ISTALE
) &&
737 * ISTALE inodes may have data
738 * and this data needs to be
741 (void) pvn_vplist_dirty(vp
, 0,
742 ufs_putapage
, B_INVAL
| B_TRUNC
, NULL
);
746 CPU_STATS_ADDQ(CPU
, sys
, ufsipage
, 1);
748 CPU_STATS_ADDQ(CPU
, sys
, ufsinopage
, 1);
750 ASSERT((vp
->v_type
== VCHR
) || !vn_has_cached_data(vp
));
753 * We had better not have a vnode reference count > 1
754 * at this point, if we do then something is broken as
755 * this inode/vnode acquired a reference underneath of us.
757 ASSERT(vp
->v_count
== 1);
764 * this thread processes the global idle queue
766 iqhead_t
*ufs_junk_iq
;
767 iqhead_t
*ufs_useful_iq
;
768 int ufs_njunk_iq
= 0;
769 int ufs_nuseful_iq
= 0;
772 struct ufs_q ufs_idle_q
;
775 ufs_thread_idle(void)
781 ufs_niqhash
= (ufs_idle_q
.uq_lowat
>> 1) / IQHASHQLEN
;
782 ufs_niqhash
= 1 << highbit(ufs_niqhash
); /* round up to power of 2 */
783 ufs_iqhashmask
= ufs_niqhash
- 1;
784 ufs_junk_iq
= kmem_alloc(ufs_niqhash
* sizeof (*ufs_junk_iq
),
786 ufs_useful_iq
= kmem_alloc(ufs_niqhash
* sizeof (*ufs_useful_iq
),
789 /* Initialize hash queue headers */
790 for (i
= 0; i
< ufs_niqhash
; i
++) {
791 ufs_junk_iq
[i
].i_freef
= (inode_t
*)&ufs_junk_iq
[i
];
792 ufs_junk_iq
[i
].i_freeb
= (inode_t
*)&ufs_junk_iq
[i
];
793 ufs_useful_iq
[i
].i_freef
= (inode_t
*)&ufs_useful_iq
[i
];
794 ufs_useful_iq
[i
].i_freeb
= (inode_t
*)&ufs_useful_iq
[i
];
797 CALLB_CPR_INIT(&cprinfo
, &ufs_idle_q
.uq_mutex
, callb_generic_cpr
,
801 * Whenever the idle thread is awakened, it repeatedly gives
802 * back half of the idle queue until the idle queue falls
805 mutex_enter(&ufs_idle_q
.uq_mutex
);
806 if (ufs_idle_q
.uq_ne
< ufs_idle_q
.uq_lowat
) {
807 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
808 cv_wait(&ufs_idle_q
.uq_cv
, &ufs_idle_q
.uq_mutex
);
809 CALLB_CPR_SAFE_END(&cprinfo
, &ufs_idle_q
.uq_mutex
);
811 mutex_exit(&ufs_idle_q
.uq_mutex
);
814 * Give back 1/2 of the idle queue
816 ne
= ufs_idle_q
.uq_ne
>> 1;
817 ins
.in_tidles
.value
.ul
+= ne
;
823 * Reclaim callback for ufs inode cache.
824 * Invoked by the kernel memory allocator when memory gets tight.
828 ufs_inode_cache_reclaim(void *cdrarg
)
831 * If we are low on memory and the idle queue is over its
832 * halfway mark, then free 50% of the idle q
834 * We don't free all of the idle inodes because the inodes
835 * for popular NFS files may have been kicked from the dnlc.
836 * The inodes for these files will end up on the idle queue
837 * after every NFS access.
839 * If we repeatedly push them from the idle queue then
840 * NFS users may be unhappy as an extra buf cache operation
841 * is incurred for every NFS operation to these files.
843 * It's not common, but I have seen it happen.
846 if (ufs_idle_q
.uq_ne
< (ufs_idle_q
.uq_lowat
>> 1))
848 mutex_enter(&ufs_idle_q
.uq_mutex
);
849 cv_broadcast(&ufs_idle_q
.uq_cv
);
850 mutex_exit(&ufs_idle_q
.uq_mutex
);
854 * Free up some idle inodes
857 ufs_idle_some(int ne
)
862 static int junk_rotor
= 0;
863 static int useful_rotor
= 0;
865 for (i
= 0; i
< ne
; ++i
) {
866 mutex_enter(&ufs_idle_q
.uq_mutex
);
869 while (ufs_junk_iq
[junk_rotor
].i_freef
==
870 (inode_t
*)&ufs_junk_iq
[junk_rotor
]) {
871 junk_rotor
= IQNEXT(junk_rotor
);
873 ip
= ufs_junk_iq
[junk_rotor
].i_freef
;
874 ASSERT(ip
->i_flag
& IJUNKIQ
);
875 } else if (ufs_nuseful_iq
) {
876 while (ufs_useful_iq
[useful_rotor
].i_freef
==
877 (inode_t
*)&ufs_useful_iq
[useful_rotor
]) {
878 useful_rotor
= IQNEXT(useful_rotor
);
880 ip
= ufs_useful_iq
[useful_rotor
].i_freef
;
881 ASSERT(!(ip
->i_flag
& IJUNKIQ
));
883 mutex_exit(&ufs_idle_q
.uq_mutex
);
892 mutex_exit(&ufs_idle_q
.uq_mutex
);
893 rw_enter(&ip
->i_contents
, RW_WRITER
);
895 * VN_RELE should not be called if
896 * ufs_rmidle returns true, as it will
897 * effectively be done in ufs_idle_free.
899 if (ufs_rmidle(ip
)) {
900 rw_exit(&ip
->i_contents
);
903 rw_exit(&ip
->i_contents
);
910 * drain entries for vfsp from the idle queue
911 * vfsp == NULL means drain the entire thing
914 ufs_idle_drain(struct vfs
*vfsp
)
916 struct inode
*ip
, *nip
;
917 struct inode
*ianchor
= NULL
;
920 mutex_enter(&ufs_idle_q
.uq_mutex
);
922 /* for each hash q */
923 for (i
= 0; i
< ufs_niqhash
; i
++) {
924 /* search down the hash q */
925 for (ip
= ufs_junk_iq
[i
].i_freef
;
926 ip
!= (inode_t
*)&ufs_junk_iq
[i
];
928 if (ip
->i_vfs
== vfsp
|| vfsp
== NULL
) {
929 /* found a matching entry */
931 mutex_exit(&ufs_idle_q
.uq_mutex
);
932 rw_enter(&ip
->i_contents
, RW_WRITER
);
934 * See comments in ufs_idle_some()
935 * as we will call ufs_idle_free()
936 * after scanning both queues.
938 if (ufs_rmidle(ip
)) {
939 rw_exit(&ip
->i_contents
);
940 ip
->i_freef
= ianchor
;
943 rw_exit(&ip
->i_contents
);
946 /* restart this hash q */
947 ip
= (inode_t
*)&ufs_junk_iq
[i
];
948 mutex_enter(&ufs_idle_q
.uq_mutex
);
953 if (ufs_nuseful_iq
) {
954 /* for each hash q */
955 for (i
= 0; i
< ufs_niqhash
; i
++) {
956 /* search down the hash q */
957 for (ip
= ufs_useful_iq
[i
].i_freef
;
958 ip
!= (inode_t
*)&ufs_useful_iq
[i
];
960 if (ip
->i_vfs
== vfsp
|| vfsp
== NULL
) {
961 /* found a matching entry */
963 mutex_exit(&ufs_idle_q
.uq_mutex
);
964 rw_enter(&ip
->i_contents
, RW_WRITER
);
966 * See comments in ufs_idle_some()
967 * as we will call ufs_idle_free()
968 * after scanning both queues.
970 if (ufs_rmidle(ip
)) {
971 rw_exit(&ip
->i_contents
);
972 ip
->i_freef
= ianchor
;
975 rw_exit(&ip
->i_contents
);
978 /* restart this hash q */
979 ip
= (inode_t
*)&ufs_useful_iq
[i
];
980 mutex_enter(&ufs_idle_q
.uq_mutex
);
986 mutex_exit(&ufs_idle_q
.uq_mutex
);
987 /* no more matching entries, release those we have found (if any) */
988 for (ip
= ianchor
; ip
; ip
= nip
) {
996 * RECLAIM DELETED INODES
997 * The following thread scans the file system once looking for deleted files
1000 ufs_thread_reclaim(struct vfs
*vfsp
)
1002 struct ufsvfs
*ufsvfsp
= (struct ufsvfs
*)vfsp
->vfs_data
;
1003 struct ufs_q
*uq
= &ufsvfsp
->vfs_reclaim
;
1004 struct fs
*fs
= ufsvfsp
->vfs_fs
;
1011 callb_cpr_t cprinfo
;
1013 CALLB_CPR_INIT(&cprinfo
, &uq
->uq_mutex
, callb_generic_cpr
,
1017 * mount decided that we don't need a reclaim thread
1019 if ((fs
->fs_reclaim
& FS_RECLAIMING
) == 0)
1023 * don't reclaim if readonly
1028 for (ino
= 0; ino
< (fs
->fs_ncg
* fs
->fs_ipg
) && !err
; ++ino
) {
1031 * Check whether we are the target of another
1032 * thread having called ufs_thread_exit() or
1033 * ufs_thread_suspend().
1035 mutex_enter(&uq
->uq_mutex
);
1037 if (uq
->uq_flags
& UQ_EXIT
) {
1039 mutex_exit(&uq
->uq_mutex
);
1041 } else if (uq
->uq_flags
& UQ_SUSPEND
) {
1042 uq
->uq_flags
|= UQ_SUSPENDED
;
1044 * Release the buf before we cv_wait()
1045 * otherwise we may deadlock with the
1046 * thread that called ufs_thread_suspend().
1052 if (uq
->uq_flags
& UQ_WAIT
) {
1053 uq
->uq_flags
&= ~UQ_WAIT
;
1054 cv_broadcast(&uq
->uq_cv
);
1056 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1057 cv_wait(&uq
->uq_cv
, &uq
->uq_mutex
);
1058 CALLB_CPR_SAFE_END(&cprinfo
, &uq
->uq_mutex
);
1061 mutex_exit(&uq
->uq_mutex
);
1064 * if we don't already have the buf; get it
1066 bno
= fsbtodb(fs
, itod(fs
, ino
));
1067 if ((bp
== 0) || (bp
->b_blkno
!= bno
)) {
1070 bp
= UFS_BREAD(ufsvfsp
,
1071 ufsvfsp
->vfs_dev
, bno
, fs
->fs_bsize
);
1072 bp
->b_flags
|= B_AGE
;
1074 if (bp
->b_flags
& B_ERROR
) {
1079 * nlink <= 0 and mode != 0 means deleted
1081 dp
= (struct dinode
*)bp
->b_un
.b_addr
+ itoo(fs
, ino
);
1082 if ((dp
->di_nlink
<= 0) && (dp
->di_mode
!= 0)) {
1084 * can't hold the buf (deadlock)
1088 rw_enter(&ufsvfsp
->vfs_dqrwlock
, RW_READER
);
1090 * iget/iput sequence will put inode on ifree
1091 * thread queue if it is idle. This is a nop
1092 * for busy (open, deleted) inodes
1094 if (ufs_iget(vfsp
, ino
, &ip
, CRED()))
1098 rw_exit(&ufsvfsp
->vfs_dqrwlock
);
1106 * reset the reclaiming-bit
1108 mutex_enter(&ufsvfsp
->vfs_lock
);
1109 fs
->fs_reclaim
&= ~FS_RECLAIMING
;
1110 mutex_exit(&ufsvfsp
->vfs_lock
);
1111 TRANS_SBWRITE(ufsvfsp
, TOP_SBWRITE_RECLAIM
);
1115 * exit the reclaim thread
1117 mutex_enter(&uq
->uq_mutex
);
1118 uq
->uq_threadp
= NULL
;
1119 uq
->uq_flags
&= ~UQ_WAIT
;
1120 cv_broadcast(&uq
->uq_cv
);
1121 CALLB_CPR_EXIT(&cprinfo
);
1126 * hlock the file system's whose logs have device errors
1128 struct ufs_q ufs_hlock
;
1131 ufs_thread_hlock(void *ignore
)
1134 callb_cpr_t cprinfo
;
1136 CALLB_CPR_INIT(&cprinfo
, &ufs_hlock
.uq_mutex
, callb_generic_cpr
,
1141 * sleep until there is work to do
1143 mutex_enter(&ufs_hlock
.uq_mutex
);
1144 (void) ufs_thread_run(&ufs_hlock
, &cprinfo
);
1145 ufs_hlock
.uq_ne
= 0;
1146 mutex_exit(&ufs_hlock
.uq_mutex
);
1148 * hlock the error'ed fs's
1149 * retry after a bit if another app is doing lockfs stuff
1152 retry
= ufs_trans_hlock();
1154 mutex_enter(&ufs_hlock
.uq_mutex
);
1155 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1156 (void) cv_reltimedwait(&ufs_hlock
.uq_cv
,
1157 &ufs_hlock
.uq_mutex
, hz
, TR_CLOCK_TICK
);
1158 CALLB_CPR_SAFE_END(&cprinfo
,
1159 &ufs_hlock
.uq_mutex
);
1160 mutex_exit(&ufs_hlock
.uq_mutex
);
1167 ufs_attr_purge(struct inode
*dp
)
1171 off_t dirsize
; /* size of the directory */
1172 off_t offset
; /* offset in the directory */
1173 int entryoffsetinblk
; /* offset of ep in fbp's buffer */
1175 struct fbuf
*fbp
; /* pointer to directory block */
1176 struct direct
*ep
; /* directory entry */
1179 struct ufsvfs
*ufsvfsp
= dp
->i_ufsvfs
;
1181 rw_enter(&ufsvfsp
->vfs_dqrwlock
, RW_READER
);
1184 dirsize
= roundup(dp
->i_size
, DIRBLKSIZ
);
1186 entryoffsetinblk
= 0;
1189 * Purge directory cache
1192 dnlc_dir_purge(&dp
->i_danchor
);
1194 while (offset
< dirsize
) {
1196 * If offset is on a block boundary,
1197 * read the next directory block.
1198 * Release previous if it exists.
1200 if (blkoff(dp
->i_fs
, offset
) == 0) {
1202 fbrelse(fbp
, S_OTHER
);
1205 err
= blkatoff(dp
, offset
, (char **)0, &fbp
);
1209 entryoffsetinblk
= 0;
1211 ep
= (struct direct
*)(fbp
->fb_addr
+ entryoffsetinblk
);
1212 if (ep
->d_ino
== 0 || (ep
->d_name
[0] == '.' &&
1213 ep
->d_name
[1] == '\0') ||
1214 (ep
->d_name
[0] == '.' && ep
->d_name
[1] == '.' &&
1215 ep
->d_name
[2] == '\0')) {
1217 entryoffsetinblk
+= ep
->d_reclen
;
1221 if ((err
= ufs_iget(dp
->i_vfs
, ep
->d_ino
,
1222 &tp
, CRED())) != 0) {
1226 TRANS_BEGIN_CSYNC(ufsvfsp
, &issync
, TOP_REMOVE
,
1227 trans_size
= (int)TOP_REMOVE_SIZE(tp
));
1233 dnlc_remove(ITOV(dp
), ep
->d_name
);
1235 rw_enter(&tp
->i_contents
, RW_WRITER
);
1238 TRANS_INODE(tp
->i_ufsvfs
, tp
);
1242 rw_exit(&tp
->i_contents
);
1245 entryoffsetinblk
+= ep
->d_reclen
;
1246 TRANS_END_CSYNC(ufsvfsp
, &error
, issync
, TOP_REMOVE
,
1250 offset
+= ep
->d_reclen
;
1254 fbrelse(fbp
, S_OTHER
);
1258 rw_exit(&ufsvfsp
->vfs_dqrwlock
);