don't bother resolving onbld python module deps
[unleashed.git] / kernel / fs / ufs / ufs_thread.c
blobc58c95c992925fc50485df51acb9e5993ddd9755
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 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>
41 #include <sys/kmem.h>
42 #include <sys/buf.h>
43 #include <sys/vnode.h>
44 #include <sys/vfs.h>
45 #include <sys/user.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>
57 #include <vm/pvn.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
70 void
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);
76 uq->uq_lowat = lowat;
77 uq->uq_hiwat = 2 * lowat;
78 uq->uq_threadp = NULL;
82 * start a thread for a queue (assumes success)
84 void
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,
90 TS_RUN, minclsyspri);
91 uq->uq_flags = 0;
93 mutex_exit(&uq->uq_mutex);
97 * wait for the thread to exit
99 void
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
121 * gets a t_did of 0.
123 if (ufs_thread_did)
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
131 void
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.
181 void
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
193 * may sleep
194 * may exit
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)
206 again:
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)
215 if (uq->uq_ne)
216 return (uq->uq_ne);
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);
223 thread_exit();
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);
237 goto again;
241 * DELETE INODE
242 * The following routines implement the protocol for freeing the resources
243 * held by an idle and deleted inode.
245 void
246 ufs_delete(struct ufsvfs *ufsvfsp, struct inode *ip, int dolockfs)
248 ushort_t mode;
249 struct vnode *vp = ITOV(ip);
250 struct ulockfs *ulp;
251 int trans_size;
252 int dorwlock = ((ip->i_mode & IFMT) == IFREG);
253 int issync;
254 int err;
255 struct inode *dp;
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);
267 VN_RELE(vp);
268 return;
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);
276 VN_RELE(vp);
277 return;
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.
283 if (dolockfs) {
284 if (ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_DELETE_MASK))
285 return;
286 } else {
288 * check for recursive VOP call
290 if (curthread->t_flag & T_DONTBLOCK) {
291 ulp = NULL;
292 } else {
293 ulp = &ufsvfsp->vfs_ulockfs;
294 curthread->t_flag |= T_DONTBLOCK;
299 * Hold rwlock to synchronize with (nfs) writes
301 if (dorwlock)
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,
312 &dp, CRED());
313 if (err == 0) {
314 rw_enter(&dp->i_rwlock, RW_WRITER);
315 rw_enter(&dp->i_contents, RW_WRITER);
316 dp->i_flag |= IUPD|ICHG;
317 dp->i_seq++;
318 TRANS_INODE(dp->i_ufsvfs, dp);
319 dp->i_nlink -= 2;
320 ufs_setreclaim(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
331 * comment for now.
333 dnlc_remove(ITOV(dp), ".");
334 dnlc_remove(ITOV(dp), "..");
335 ITIMES_NOLOCK(dp);
336 if (!TRANS_ISTRANS(ufsvfsp)) {
337 ufs_iupdat(dp, I_SYNC);
339 rw_exit(&dp->i_contents);
340 rw_exit(&dp->i_rwlock);
341 VN_RELE(ITOV(dp));
344 * Clear out attribute pointer
346 ip->i_oeftflag = 0;
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) {
353 ufs_attr_purge(ip);
356 (void) TRANS_ITRUNC(ip, 0, I_FREE | I_ACCT, CRED());
359 * the inode's space has been freed; now free the inode
361 if (ulp) {
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);
368 mode = ip->i_mode;
369 ip->i_mode = 0;
370 ip->i_rdev = 0;
371 ip->i_ordev = 0;
372 ip->i_flag |= IMOD;
373 if (ip->i_ufs_acl) {
374 (void) ufs_si_free(ip->i_ufs_acl, vp->v_vfsp, CRED());
375 ip->i_ufs_acl = NULL;
376 ip->i_shadow = 0;
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);
385 vn_recycle(vp);
386 mutex_exit(&vp->v_lock);
389 * free the inode
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);
397 dqrele(ip->i_dquot);
398 ip->i_dquot = NULL;
399 ip->i_flag &= ~(IDEL | IDIRECTIO);
400 ip->i_cflags = 0;
401 if (!TRANS_ISTRANS(ufsvfsp)) {
402 ufs_iupdat(ip, I_SYNC);
403 } else {
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);
410 if (dorwlock)
411 rw_exit(&ip->i_rwlock);
412 VN_RELE(vp);
415 * End of transaction
417 if (ulp) {
418 TRANS_END_ASYNC(ufsvfsp, TOP_IFREE, trans_size);
419 if (dolockfs)
420 ufs_lockfs_end(ulp);
421 else
422 curthread->t_flag &= ~T_DONTBLOCK;
427 * Create the delete thread and init the delq_info for this fs
429 void
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
441 void
442 ufs_thread_delete(struct vfs *vfsp)
444 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
445 struct ufs_q *uq = &ufsvfsp->vfs_delete;
446 struct inode *ip;
447 long ne;
448 callb_cpr_t cprinfo;
450 CALLB_CPR_INIT(&cprinfo, &uq->uq_mutex, callb_generic_cpr,
451 "ufsdelete");
453 mutex_enter(&uq->uq_mutex);
454 again:
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)
473 uq->uq_ihead = NULL;
474 ip->i_freef->i_freeb = ip->i_freeb;
475 ip->i_freeb->i_freef = ip->i_freef;
476 ip->i_freef = ip;
477 ip->i_freeb = ip;
478 uq->uq_ne--;
479 mutex_exit(&uq->uq_mutex);
480 ufs_delete(ufsvfsp, ip, 1);
481 mutex_enter(&uq->uq_mutex);
483 goto again;
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
494 void
495 ufs_delete_drain(struct vfs *vfsp, int ne, int dolockfs)
497 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
498 struct ufs_q *uq;
499 struct inode *ip;
500 int drain_cnt = 0;
501 int done;
504 * if forcibly unmounted; ignore
506 if (ufsvfsp == NULL)
507 return;
509 uq = &ufsvfsp->vfs_delete;
510 mutex_enter(&uq->uq_mutex);
511 if (ne == 0)
512 drain_cnt = uq->uq_ne;
513 else if (ne > 0)
514 drain_cnt = ne;
517 * process up to ne entries
520 done = 0;
521 while (!done && (ip = uq->uq_ihead)) {
522 if (ne != -1)
523 drain_cnt--;
524 if (ne != -1 && drain_cnt == 0)
525 done = 1;
526 if ((uq->uq_ihead = ip->i_freef) == ip)
527 uq->uq_ihead = NULL;
528 ip->i_freef->i_freeb = ip->i_freeb;
529 ip->i_freeb->i_freef = ip->i_freef;
530 ip->i_freef = ip;
531 ip->i_freeb = ip;
532 uq->uq_ne--;
533 mutex_exit(&uq->uq_mutex);
534 ufs_delete(ufsvfsp, ip, dolockfs);
535 mutex_enter(&uq->uq_mutex);
537 mutex_exit(&uq->uq_mutex);
540 void
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)).
569 void
570 ufs_delete_drain_wait(struct ufsvfs *ufsvfsp, int dolockfs)
572 struct ufs_q *uq = &ufsvfsp->vfs_delete;
573 int error;
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
579 * working on delq.
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);
586 } else {
587 ASSERT(delq_info->delq_unreclaimed_files == 0);
588 mutex_exit(&delq->uq_mutex);
589 return;
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);
598 if (!error) {
599 TRANS_END_SYNC(ufsvfsp, &error, TOP_COMMIT_UPDATE,
600 TOP_COMMIT_SIZE);
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.
614 void
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);
633 * IDLE INODE
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
643 static void
644 ufs_idle_free(struct inode *ip)
646 int pages;
647 int hno;
648 kmutex_t *ihm;
649 struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
650 struct vnode *vp = ITOV(ip);
651 int vn_has_data, vn_modified;
654 * inode is held
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);
678 ihm = &ih_lock[hno];
679 mutex_enter(ihm);
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
686 * VN_RELE().
688 mutex_enter(&vp->v_lock);
690 VERIFY3U(vp->v_count, >=, 2);
692 VN_RELE_LOCKED(vp);
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);
710 mutex_exit(ihm);
711 VN_RELE(vp);
712 } else {
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
725 remque(ip);
726 mutex_exit(ihm);
728 * Stale inodes have no valid ufsvfs
730 if ((ip->i_flag & ISTALE) == 0 && ip->i_dquot) {
731 TRANS_DQRELE(ufsvfsp, ip->i_dquot);
732 ip->i_dquot = NULL;
734 if ((ip->i_flag & ISTALE) &&
735 vn_has_data) {
737 * ISTALE inodes may have data
738 * and this data needs to be
739 * cleaned up.
741 (void) pvn_vplist_dirty(vp, 0,
742 ufs_putapage, B_INVAL | B_TRUNC, NULL);
744 ufs_si_del(ip);
745 if (pages) {
746 CPU_STATS_ADDQ(CPU, sys, ufsipage, 1);
747 } else {
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);
759 ufs_free_inode(ip);
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;
770 int ufs_niqhash;
771 int ufs_iqhashmask;
772 struct ufs_q ufs_idle_q;
774 void
775 ufs_thread_idle(void)
777 callb_cpr_t cprinfo;
778 int i;
779 int ne;
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),
785 KM_SLEEP);
786 ufs_useful_iq = kmem_alloc(ufs_niqhash * sizeof (*ufs_useful_iq),
787 KM_SLEEP);
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,
798 "ufsidle");
799 again:
801 * Whenever the idle thread is awakened, it repeatedly gives
802 * back half of the idle queue until the idle queue falls
803 * below lowat.
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;
818 ufs_idle_some(ne);
819 goto again;
823 * Reclaim callback for ufs inode cache.
824 * Invoked by the kernel memory allocator when memory gets tight.
826 /*ARGSUSED*/
827 void
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))
847 return;
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
856 void
857 ufs_idle_some(int ne)
859 int i;
860 struct inode *ip;
861 struct vnode *vp;
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);
868 if (ufs_njunk_iq) {
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));
882 } else {
883 mutex_exit(&ufs_idle_q.uq_mutex);
884 return;
888 * emulate ufs_iget
890 vp = ITOV(ip);
891 VN_HOLD(vp);
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);
901 ufs_idle_free(ip);
902 } else {
903 rw_exit(&ip->i_contents);
904 VN_RELE(vp);
910 * drain entries for vfsp from the idle queue
911 * vfsp == NULL means drain the entire thing
913 void
914 ufs_idle_drain(struct vfs *vfsp)
916 struct inode *ip, *nip;
917 struct inode *ianchor = NULL;
918 int i;
920 mutex_enter(&ufs_idle_q.uq_mutex);
921 if (ufs_njunk_iq) {
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];
927 ip = ip->i_freef) {
928 if (ip->i_vfs == vfsp || vfsp == NULL) {
929 /* found a matching entry */
930 VN_HOLD(ITOV(ip));
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;
941 ianchor = ip;
942 } else {
943 rw_exit(&ip->i_contents);
944 VN_RELE(ITOV(ip));
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];
959 ip = ip->i_freef) {
960 if (ip->i_vfs == vfsp || vfsp == NULL) {
961 /* found a matching entry */
962 VN_HOLD(ITOV(ip));
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;
973 ianchor = ip;
974 } else {
975 rw_exit(&ip->i_contents);
976 VN_RELE(ITOV(ip));
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) {
989 nip = ip->i_freef;
990 ip->i_freef = ip;
991 ufs_idle_free(ip);
996 * RECLAIM DELETED INODES
997 * The following thread scans the file system once looking for deleted files
999 void
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;
1005 struct buf *bp = 0;
1006 int err = 0;
1007 daddr_t bno;
1008 ino_t ino;
1009 struct dinode *dp;
1010 struct inode *ip;
1011 callb_cpr_t cprinfo;
1013 CALLB_CPR_INIT(&cprinfo, &uq->uq_mutex, callb_generic_cpr,
1014 "ufsreclaim");
1017 * mount decided that we don't need a reclaim thread
1019 if ((fs->fs_reclaim & FS_RECLAIMING) == 0)
1020 err++;
1023 * don't reclaim if readonly
1025 if (fs->fs_ronly)
1026 err++;
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);
1036 again:
1037 if (uq->uq_flags & UQ_EXIT) {
1038 err++;
1039 mutex_exit(&uq->uq_mutex);
1040 break;
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().
1048 if (bp) {
1049 brelse(bp);
1050 bp = 0;
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);
1059 goto again;
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)) {
1068 if (bp)
1069 brelse(bp);
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) {
1075 err++;
1076 continue;
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)
1086 brelse(bp);
1087 bp = 0;
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()))
1095 err++;
1096 else
1097 VN_RELE(ITOV(ip));
1098 rw_exit(&ufsvfsp->vfs_dqrwlock);
1102 if (bp)
1103 brelse(bp);
1104 if (!err) {
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);
1122 thread_exit();
1125 * HLOCK FILE SYSTEM
1126 * hlock the file system's whose logs have device errors
1128 struct ufs_q ufs_hlock;
1129 /*ARGSUSED*/
1130 void
1131 ufs_thread_hlock(void *ignore)
1133 int retry;
1134 callb_cpr_t cprinfo;
1136 CALLB_CPR_INIT(&cprinfo, &ufs_hlock.uq_mutex, callb_generic_cpr,
1137 "ufshlock");
1139 for (;;) {
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
1151 do {
1152 retry = ufs_trans_hlock();
1153 if (retry) {
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);
1162 } while (retry);
1166 static void
1167 ufs_attr_purge(struct inode *dp)
1169 int err;
1170 int error;
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 */
1174 struct inode *tp;
1175 struct fbuf *fbp; /* pointer to directory block */
1176 struct direct *ep; /* directory entry */
1177 int trans_size;
1178 int issync;
1179 struct ufsvfs *ufsvfsp = dp->i_ufsvfs;
1181 rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1183 fbp = NULL;
1184 dirsize = roundup(dp->i_size, DIRBLKSIZ);
1185 offset = 0;
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) {
1201 if (fbp != NULL) {
1202 fbrelse(fbp, S_OTHER);
1205 err = blkatoff(dp, offset, (char **)0, &fbp);
1206 if (err) {
1207 goto out;
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;
1219 } else {
1221 if ((err = ufs_iget(dp->i_vfs, ep->d_ino,
1222 &tp, CRED())) != 0) {
1223 goto out;
1226 TRANS_BEGIN_CSYNC(ufsvfsp, &issync, TOP_REMOVE,
1227 trans_size = (int)TOP_REMOVE_SIZE(tp));
1230 * Delete inode.
1233 dnlc_remove(ITOV(dp), ep->d_name);
1235 rw_enter(&tp->i_contents, RW_WRITER);
1236 tp->i_flag |= ICHG;
1237 tp->i_seq++;
1238 TRANS_INODE(tp->i_ufsvfs, tp);
1239 tp->i_nlink--;
1240 ufs_setreclaim(tp);
1241 ITIMES_NOLOCK(tp);
1242 rw_exit(&tp->i_contents);
1244 VN_RELE(ITOV(tp));
1245 entryoffsetinblk += ep->d_reclen;
1246 TRANS_END_CSYNC(ufsvfsp, &error, issync, TOP_REMOVE,
1247 trans_size);
1250 offset += ep->d_reclen;
1253 if (fbp) {
1254 fbrelse(fbp, S_OTHER);
1257 out:
1258 rw_exit(&ufsvfsp->vfs_dqrwlock);