10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55331Samw * Common Development and Distribution License (the "License").
65331Samw * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11066Srafael.vanoni@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/cred.h>
300Sstevel@tonic-gate #include <sys/proc.h>
310Sstevel@tonic-gate #include <sys/user.h>
320Sstevel@tonic-gate #include <sys/vfs.h>
330Sstevel@tonic-gate #include <sys/vnode.h>
340Sstevel@tonic-gate #include <sys/pathname.h>
350Sstevel@tonic-gate #include <sys/uio.h>
360Sstevel@tonic-gate #include <sys/tiuser.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/kobj.h>
400Sstevel@tonic-gate #include <sys/mount.h>
410Sstevel@tonic-gate #include <sys/ioctl.h>
420Sstevel@tonic-gate #include <sys/statvfs.h>
430Sstevel@tonic-gate #include <sys/errno.h>
440Sstevel@tonic-gate #include <sys/debug.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/utsname.h>
470Sstevel@tonic-gate #include <sys/modctl.h>
480Sstevel@tonic-gate #include <sys/file.h>
490Sstevel@tonic-gate #include <sys/stat.h>
500Sstevel@tonic-gate #include <sys/fcntl.h>
510Sstevel@tonic-gate #include <sys/callb.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate #include <vm/hat.h>
540Sstevel@tonic-gate #include <vm/as.h>
550Sstevel@tonic-gate #include <vm/page.h>
560Sstevel@tonic-gate #include <vm/pvn.h>
570Sstevel@tonic-gate #include <vm/seg.h>
580Sstevel@tonic-gate #include <vm/seg_map.h>
590Sstevel@tonic-gate #include <vm/seg_vn.h>
600Sstevel@tonic-gate #include <vm/rm.h>
610Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate extern time_t time;
640Sstevel@tonic-gate
650Sstevel@tonic-gate /* forward references */
660Sstevel@tonic-gate int cachefs_rl_entry_get(cachefscache_t *, uint_t, rl_entry_t **);
670Sstevel@tonic-gate void cachefs_garbage_collect_queue(cachefscache_t *cachep);
680Sstevel@tonic-gate static time_t cachefs_gc_front_atime(cachefscache_t *cachep);
690Sstevel@tonic-gate static void cachefs_garbage_collect(cachefscache_t *cachep);
700Sstevel@tonic-gate static void cachefs_packed_pending(cachefscache_t *cachep);
710Sstevel@tonic-gate
720Sstevel@tonic-gate
730Sstevel@tonic-gate #define RL_HEAD(cachep, type) \
740Sstevel@tonic-gate (&(cachep->c_rlinfo.rl_items[CACHEFS_RL_INDEX(type)]))
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
775331Samw * This function moves an RL entry from wherever it currently is to
780Sstevel@tonic-gate * the back of the requested list.
790Sstevel@tonic-gate */
800Sstevel@tonic-gate void
cachefs_rlent_moveto(cachefscache_t * cachep,enum cachefs_rl_type type,uint_t entno,size_t blks)810Sstevel@tonic-gate cachefs_rlent_moveto(cachefscache_t *cachep,
820Sstevel@tonic-gate enum cachefs_rl_type type, uint_t entno, size_t blks)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
850Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
860Sstevel@tonic-gate cachefs_rlent_moveto_nolock(cachep, type, entno, blks);
870Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate void
cachefs_rlent_moveto_nolock(cachefscache_t * cachep,enum cachefs_rl_type type,uint_t entno,size_t blks)910Sstevel@tonic-gate cachefs_rlent_moveto_nolock(cachefscache_t *cachep,
920Sstevel@tonic-gate enum cachefs_rl_type type, uint_t entno, size_t blks)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate rl_entry_t *rl_ent;
950Sstevel@tonic-gate uint_t prev, next;
960Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
970Sstevel@tonic-gate enum cachefs_rl_type otype;
980Sstevel@tonic-gate int error;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate ASSERT(entno != 0);
1010Sstevel@tonic-gate ASSERT((cachep->c_flags & (CACHE_NOCACHE|CACHE_NOFILL)) == 0);
1020Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cachep->c_contentslock));
1030Sstevel@tonic-gate ASSERT((CACHEFS_RL_START <= type) && (type <= CACHEFS_RL_END));
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
1060Sstevel@tonic-gate if (error)
1070Sstevel@tonic-gate return;
1080Sstevel@tonic-gate next = rl_ent->rl_fwd_idx;
1090Sstevel@tonic-gate prev = rl_ent->rl_bkwd_idx;
1100Sstevel@tonic-gate otype = rl_ent->rl_current;
1110Sstevel@tonic-gate ASSERT((CACHEFS_RL_START <= otype) && (otype <= CACHEFS_RL_END));
1120Sstevel@tonic-gate rl_ent->rl_current = CACHEFS_RL_NONE;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate if (type == CACHEFS_RL_PACKED_PENDING) {
1150Sstevel@tonic-gate /* XXX sam: is this the right place to turn this on? */
1160Sstevel@tonic-gate cachep->c_flags |= CACHE_PACKED_PENDING;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* remove entry from its previous list */
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate lhp = RL_HEAD(cachep, otype);
1220Sstevel@tonic-gate if ((lhp->rli_back == 0) || (lhp->rli_front == 0))
1230Sstevel@tonic-gate ASSERT((lhp->rli_back == 0) && (lhp->rli_front == 0));
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if (lhp->rli_back == entno)
1260Sstevel@tonic-gate lhp->rli_back = next;
1270Sstevel@tonic-gate if (lhp->rli_front == entno)
1280Sstevel@tonic-gate lhp->rli_front = prev;
1290Sstevel@tonic-gate if (prev != 0) {
1300Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, prev, &rl_ent);
1310Sstevel@tonic-gate if (error)
1320Sstevel@tonic-gate return;
1330Sstevel@tonic-gate rl_ent->rl_fwd_idx = next;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate if (next != 0) {
1360Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, next, &rl_ent);
1370Sstevel@tonic-gate if (error)
1380Sstevel@tonic-gate return;
1390Sstevel@tonic-gate rl_ent->rl_bkwd_idx = prev;
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate lhp->rli_blkcnt -= blks;
1420Sstevel@tonic-gate lhp->rli_itemcnt--;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /* add entry to its new list */
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate lhp = RL_HEAD(cachep, type);
1470Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
1480Sstevel@tonic-gate if (error)
1490Sstevel@tonic-gate return;
1500Sstevel@tonic-gate rl_ent->rl_current = type;
1510Sstevel@tonic-gate rl_ent->rl_bkwd_idx = 0;
1520Sstevel@tonic-gate rl_ent->rl_fwd_idx = lhp->rli_back;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate if (lhp->rli_back != 0) {
1550Sstevel@tonic-gate ASSERT(lhp->rli_front != 0);
1560Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, lhp->rli_back, &rl_ent);
1570Sstevel@tonic-gate if (error)
1580Sstevel@tonic-gate return;
1590Sstevel@tonic-gate rl_ent->rl_bkwd_idx = entno;
1600Sstevel@tonic-gate } else {
1610Sstevel@tonic-gate ASSERT(lhp->rli_front == 0);
1620Sstevel@tonic-gate lhp->rli_front = entno;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate lhp->rli_back = entno;
1650Sstevel@tonic-gate lhp->rli_blkcnt += blks;
1660Sstevel@tonic-gate lhp->rli_itemcnt++;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * This function verifies that an rl entry is of the `correct' type.
1710Sstevel@tonic-gate * it's used for debugging (only?).
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /*ARGSUSED*/
1750Sstevel@tonic-gate void
cachefs_rlent_verify(cachefscache_t * cachep,enum cachefs_rl_type type,uint_t entno)1760Sstevel@tonic-gate cachefs_rlent_verify(cachefscache_t *cachep,
1770Sstevel@tonic-gate enum cachefs_rl_type type, uint_t entno)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate #ifdef CFSDEBUG
1800Sstevel@tonic-gate rl_entry_t *rl_ent;
1810Sstevel@tonic-gate int error;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate ASSERT((CACHEFS_RL_START <= type) && (type <= CACHEFS_RL_END));
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
1880Sstevel@tonic-gate if (!error && rl_ent->rl_current != type) {
1890Sstevel@tonic-gate #ifdef CFSRLDEBUG
1900Sstevel@tonic-gate printf("cachefs_rldebug: type should be %x\n", type);
1910Sstevel@tonic-gate cachefs_rl_debug_show(rl_ent);
1920Sstevel@tonic-gate debug_enter("cachefs_rlent_verify");
1930Sstevel@tonic-gate #else /* CFSRLDEBUG */
1940Sstevel@tonic-gate cmn_err(CE_WARN, "rl entry %x type = %x should be %x\n",
1950Sstevel@tonic-gate entno, rl_ent->rl_current, type);
1960Sstevel@tonic-gate #endif /* CFSRLDEBUG */
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
2000Sstevel@tonic-gate #endif /* CFSDEBUG */
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * Returns the rl data of the front of the specified resource list.
2050Sstevel@tonic-gate * Returns 0 for success, !0 if the list is empty.
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate int
cachefs_rlent_data(cachefscache_t * cachep,rl_entry_t * valp,uint_t * entnop)2080Sstevel@tonic-gate cachefs_rlent_data(cachefscache_t *cachep, rl_entry_t *valp, uint_t *entnop)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate uint_t entno;
2110Sstevel@tonic-gate rl_entry_t *rl_ent;
2120Sstevel@tonic-gate int error = 0;
2130Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
2140Sstevel@tonic-gate enum cachefs_rl_type type;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate ASSERT((cachep->c_flags & CACHE_NOCACHE) == 0);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (entnop == NULL)
2190Sstevel@tonic-gate entnop = &entno;
2200Sstevel@tonic-gate *entnop = 0;
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate type = valp->rl_current;
2250Sstevel@tonic-gate ASSERT((CACHEFS_RL_START <= type) && (type <= CACHEFS_RL_END));
2260Sstevel@tonic-gate lhp = RL_HEAD(cachep, type);
2270Sstevel@tonic-gate entno = lhp->rli_front;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate if (*entnop == 0) {
2300Sstevel@tonic-gate error = ENOENT;
2310Sstevel@tonic-gate } else {
2320Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, *entnop, &rl_ent);
2330Sstevel@tonic-gate if (!error)
2340Sstevel@tonic-gate *valp = *rl_ent;
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
2370Sstevel@tonic-gate return (error);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * This function plucks a slot from the RL free list and creates an RL entry.
2420Sstevel@tonic-gate */
2430Sstevel@tonic-gate int
cachefs_rl_alloc(struct cachefscache * cachep,rl_entry_t * valp,uint_t * entnop)2440Sstevel@tonic-gate cachefs_rl_alloc(struct cachefscache *cachep, rl_entry_t *valp, uint_t *entnop)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate int error = 0;
2470Sstevel@tonic-gate uint_t entno;
2480Sstevel@tonic-gate rl_entry_t *rl_ent;
2490Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate ASSERT((cachep->c_flags & (CACHE_NOCACHE|CACHE_NOFILL)) == 0);
2520Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
2550Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_FREE);
2560Sstevel@tonic-gate entno = lhp->rli_front;
2570Sstevel@tonic-gate if (entno == 0) {
2580Sstevel@tonic-gate if (cachep->c_rlinfo.rl_entries >=
259*11066Srafael.vanoni@sun.com cachep->c_label.cl_maxinodes) {
2600Sstevel@tonic-gate error = ENOMEM;
2610Sstevel@tonic-gate goto out;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate entno = ++(cachep->c_rlinfo.rl_entries);
2640Sstevel@tonic-gate lhp->rli_itemcnt++;
2650Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
2660Sstevel@tonic-gate if (error)
2670Sstevel@tonic-gate goto out;
2680Sstevel@tonic-gate rl_ent->rl_current = CACHEFS_RL_NONE;
2690Sstevel@tonic-gate rl_ent->rl_fwd_idx = 0;
2700Sstevel@tonic-gate rl_ent->rl_bkwd_idx = 0;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate cachefs_rlent_moveto_nolock(cachep, CACHEFS_RL_NONE, entno, 0);
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
2760Sstevel@tonic-gate if (error)
2770Sstevel@tonic-gate goto out;
2780Sstevel@tonic-gate rl_ent->rl_fsid = valp->rl_fsid;
2790Sstevel@tonic-gate rl_ent->rl_fileno = valp->rl_fileno;
2800Sstevel@tonic-gate rl_ent->rl_local = valp->rl_local;
2810Sstevel@tonic-gate rl_ent->rl_attrc = valp->rl_attrc;
2820Sstevel@tonic-gate rl_ent->rl_fsck = 0;
2830Sstevel@tonic-gate out:
2840Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
2850Sstevel@tonic-gate if (error == 0)
2860Sstevel@tonic-gate *entnop = entno;
2870Sstevel@tonic-gate return (error);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * Call to change a local fileno in an rl entry to a normal fileno.
2920Sstevel@tonic-gate */
2930Sstevel@tonic-gate void
cachefs_rl_changefileno(cachefscache_t * cachep,uint_t entno,ino64_t fileno)2940Sstevel@tonic-gate cachefs_rl_changefileno(cachefscache_t *cachep, uint_t entno, ino64_t fileno)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate rl_entry_t *rl_ent;
2970Sstevel@tonic-gate int error;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
3000Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
3010Sstevel@tonic-gate if (!error) {
3020Sstevel@tonic-gate ASSERT(rl_ent->rl_local);
3030Sstevel@tonic-gate rl_ent->rl_local = 0;
3040Sstevel@tonic-gate rl_ent->rl_fileno = fileno;
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * Moves the files on the modified list for this file system to
3110Sstevel@tonic-gate * the modified fix list.
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate void
cachefs_move_modified_to_mf(cachefscache_t * cachep,fscache_t * fscp)3140Sstevel@tonic-gate cachefs_move_modified_to_mf(cachefscache_t *cachep, fscache_t *fscp)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate rl_entry_t *list_ent;
3170Sstevel@tonic-gate uint_t curp, nextp;
3180Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
3190Sstevel@tonic-gate int error;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cachep->c_mflock));
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_MF);
3260Sstevel@tonic-gate ASSERT(lhp->rli_front == 0);
3270Sstevel@tonic-gate ASSERT(lhp->rli_back == 0);
3280Sstevel@tonic-gate ASSERT(lhp->rli_itemcnt == 0);
3290Sstevel@tonic-gate lhp->rli_blkcnt = 0;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /* walk the modified list */
3340Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_MODIFIED);
3350Sstevel@tonic-gate for (curp = lhp->rli_front; curp != 0; curp = nextp) {
3360Sstevel@tonic-gate /* get the next element */
3370Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, curp, &list_ent);
3380Sstevel@tonic-gate if (error) {
3390Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
3400Sstevel@tonic-gate return;
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate nextp = list_ent->rl_bkwd_idx;
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate /* skip if element is not in this file system */
3450Sstevel@tonic-gate if (list_ent->rl_fsid != fscp->fs_cfsid)
3460Sstevel@tonic-gate continue;
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate /* move from modified list to mf list */
3490Sstevel@tonic-gate cachefs_rlent_moveto_nolock(cachep, CACHEFS_RL_MF, curp, 0);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate * Moves the contents of the active list to the rl list.
3560Sstevel@tonic-gate * Leave modified files on the active list, so they are not
3570Sstevel@tonic-gate * garbage collected.
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate void
cachefs_rl_cleanup(cachefscache_t * cachep)3600Sstevel@tonic-gate cachefs_rl_cleanup(cachefscache_t *cachep)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
3630Sstevel@tonic-gate rl_entry_t *rlp;
3640Sstevel@tonic-gate uint_t entno, next;
3650Sstevel@tonic-gate int error;
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cachep->c_contentslock));
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate * if fsck ran, then both of these lists should be empty. the
3710Sstevel@tonic-gate * only time this isn't the case is when we've done a cachefs
3720Sstevel@tonic-gate * boot with a clean cache. then, the cache may have been
3730Sstevel@tonic-gate * clean, but files and attrfiles were left dangling.
3740Sstevel@tonic-gate *
3750Sstevel@tonic-gate * when this happens, we just fix the linked lists here. this
3760Sstevel@tonic-gate * means that the attrcache header and cnode metadata might
3770Sstevel@tonic-gate * have incorrect information about which resource lists an
3780Sstevel@tonic-gate * entity is currently on. so, we set CACHE_CHECK_RLTYPE,
3790Sstevel@tonic-gate * which says cache-wide to double-check and go with whatever
3800Sstevel@tonic-gate * is in the resource list at the time such an object is
3810Sstevel@tonic-gate * loaded into memory.
3820Sstevel@tonic-gate */
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_ACTIVE);
3850Sstevel@tonic-gate if (lhp->rli_itemcnt > 0) {
3860Sstevel@tonic-gate cachep->c_flags |= CACHE_CHECK_RLTYPE;
3870Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate for (entno = lhp->rli_front; entno != 0; entno = next) {
3900Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rlp);
3910Sstevel@tonic-gate if (error)
3920Sstevel@tonic-gate return;
3930Sstevel@tonic-gate next = rlp->rl_bkwd_idx;
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate ASSERT(rlp->rl_current == CACHEFS_RL_ACTIVE);
3960Sstevel@tonic-gate cachefs_rlent_moveto_nolock(cachep, CACHEFS_RL_GC, entno, 0);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate #if 0
4000Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_ATTRFILE);
4010Sstevel@tonic-gate if (lhp->rli_itemcnt > 0) {
4020Sstevel@tonic-gate cachep->c_flags |= CACHE_CHECK_RLTYPE;
4030Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate for (entno = lhp->rli_front; entno != 0; entno = next) {
4060Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rlp);
4070Sstevel@tonic-gate if (error)
4080Sstevel@tonic-gate return;
4090Sstevel@tonic-gate next = rlp->rl_bkwd_idx;
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate ASSERT(rlp->rl_current == CACHEFS_RL_ATTRFILE);
4120Sstevel@tonic-gate cachefs_rlent_moveto_nolock(cachep, CACHEFS_RL_GC, entno, 0);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate #endif
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate int
cachefs_allocfile(cachefscache_t * cachep)4180Sstevel@tonic-gate cachefs_allocfile(cachefscache_t *cachep)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate int error = 0;
4210Sstevel@tonic-gate int collect = 0;
4220Sstevel@tonic-gate struct statvfs64 sb;
4230Sstevel@tonic-gate fsfilcnt64_t used;
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate (void) VFS_STATVFS(cachep->c_dirvp->v_vfsp, &sb);
4260Sstevel@tonic-gate used = sb.f_files - sb.f_ffree;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate /* if there are no more available inodes */
4310Sstevel@tonic-gate if ((cachep->c_usage.cu_filesused >= cachep->c_label.cl_maxinodes) ||
4320Sstevel@tonic-gate ((cachep->c_usage.cu_filesused > cachep->c_label.cl_filemin) &&
4330Sstevel@tonic-gate (used > cachep->c_label.cl_filetresh))) {
4340Sstevel@tonic-gate error = ENOSPC;
4350Sstevel@tonic-gate if ((cachep->c_flags & CACHE_GARBAGE_COLLECT) == 0)
4360Sstevel@tonic-gate collect = 1;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate /* else if there are more available inodes */
4400Sstevel@tonic-gate else {
4410Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
4420Sstevel@tonic-gate cachep->c_usage.cu_filesused++;
4430Sstevel@tonic-gate if (((cachep->c_flags & CACHE_GARBAGE_COLLECT) == 0) &&
4440Sstevel@tonic-gate (cachep->c_usage.cu_filesused >=
4450Sstevel@tonic-gate cachep->c_label.cl_filehiwat))
4460Sstevel@tonic-gate collect = 1;
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate if (collect)
4520Sstevel@tonic-gate cachefs_garbage_collect_queue(cachep);
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate return (error);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate void
cachefs_freefile(cachefscache_t * cachep)4580Sstevel@tonic-gate cachefs_freefile(cachefscache_t *cachep)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
4610Sstevel@tonic-gate ASSERT(cachep->c_usage.cu_filesused > 0);
4620Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
4630Sstevel@tonic-gate cachep->c_usage.cu_filesused--;
4640Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate /*ARGSUSED*/
4680Sstevel@tonic-gate int
cachefs_allocblocks(cachefscache_t * cachep,size_t nblks,enum cachefs_rl_type type)4690Sstevel@tonic-gate cachefs_allocblocks(cachefscache_t *cachep, size_t nblks,
4700Sstevel@tonic-gate enum cachefs_rl_type type)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate int error = 0;
4730Sstevel@tonic-gate int collect = 0;
4740Sstevel@tonic-gate struct statvfs64 sb;
4750Sstevel@tonic-gate size_t used;
4760Sstevel@tonic-gate size_t blocks;
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate ASSERT(type != CACHEFS_RL_FREE);
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate (void) VFS_STATVFS(cachep->c_dirvp->v_vfsp, &sb);
4810Sstevel@tonic-gate used = ((sb.f_blocks - sb.f_bfree) * sb.f_frsize) / MAXBSIZE;
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /* if there are no more available blocks */
4860Sstevel@tonic-gate blocks = cachep->c_usage.cu_blksused + nblks;
4870Sstevel@tonic-gate if ((blocks >= cachep->c_label.cl_maxblks) ||
4880Sstevel@tonic-gate ((blocks > cachep->c_label.cl_blockmin) &&
4890Sstevel@tonic-gate (used > cachep->c_label.cl_blocktresh))) {
4900Sstevel@tonic-gate error = ENOSPC;
4910Sstevel@tonic-gate if ((cachep->c_flags & CACHE_GARBAGE_COLLECT) == 0)
4920Sstevel@tonic-gate collect = 1;
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate /* else if there are more available blocks */
4960Sstevel@tonic-gate else {
4970Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
4980Sstevel@tonic-gate cachep->c_usage.cu_blksused += (uint_t)nblks;
4990Sstevel@tonic-gate ASSERT((CACHEFS_RL_START <= type) && (type <= CACHEFS_RL_END));
5000Sstevel@tonic-gate RL_HEAD(cachep, type)->rli_blkcnt += nblks;
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate if (((cachep->c_flags & CACHE_GARBAGE_COLLECT) == 0) &&
5030Sstevel@tonic-gate (cachep->c_usage.cu_blksused >=
5040Sstevel@tonic-gate cachep->c_label.cl_blkhiwat))
5050Sstevel@tonic-gate collect = 1;
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate if (collect)
5110Sstevel@tonic-gate cachefs_garbage_collect_queue(cachep);
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate return (error);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate void
cachefs_freeblocks(cachefscache_t * cachep,size_t nblks,enum cachefs_rl_type type)5170Sstevel@tonic-gate cachefs_freeblocks(cachefscache_t *cachep, size_t nblks,
5180Sstevel@tonic-gate enum cachefs_rl_type type)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
5210Sstevel@tonic-gate cachefs_cache_dirty(cachep, 0);
5220Sstevel@tonic-gate cachep->c_usage.cu_blksused -= (uint_t)nblks;
5230Sstevel@tonic-gate ASSERT(cachep->c_usage.cu_blksused >= 0);
5240Sstevel@tonic-gate ASSERT((CACHEFS_RL_START <= type) && (type <= CACHEFS_RL_END));
5250Sstevel@tonic-gate ASSERT(type != CACHEFS_RL_FREE);
5260Sstevel@tonic-gate RL_HEAD(cachep, type)->rli_blkcnt -= nblks;
5270Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate int
cachefs_victim(cachefscache_t * cachep)5310Sstevel@tonic-gate cachefs_victim(cachefscache_t *cachep)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate uint_t entno;
5340Sstevel@tonic-gate rl_entry_t *rl_ent;
5350Sstevel@tonic-gate int error = 0;
5360Sstevel@tonic-gate ino64_t fsid;
5370Sstevel@tonic-gate cfs_cid_t cid;
5380Sstevel@tonic-gate struct fscache *fscp;
5390Sstevel@tonic-gate struct filegrp *fgp;
5400Sstevel@tonic-gate struct cachefs_metadata md;
5410Sstevel@tonic-gate struct cnode *cp;
5420Sstevel@tonic-gate int isattrc;
5430Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate ASSERT((cachep->c_flags & (CACHE_NOCACHE|CACHE_NOFILL)) == 0);
5460Sstevel@tonic-gate fscp = NULL;
5470Sstevel@tonic-gate fgp = NULL;
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate /* get the file and fsid of the first item on the rl list */
5500Sstevel@tonic-gate /* XXX call rlent_data() instead */
5510Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
5520Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_GC);
5530Sstevel@tonic-gate entno = lhp->rli_front;
5540Sstevel@tonic-gate if (entno == 0) {
5550Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
5560Sstevel@tonic-gate error = ENOSPC;
5570Sstevel@tonic-gate goto out;
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
5600Sstevel@tonic-gate if (error) {
5610Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
5620Sstevel@tonic-gate goto out;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate fsid = rl_ent->rl_fsid;
5650Sstevel@tonic-gate cid.cid_fileno = rl_ent->rl_fileno;
5660Sstevel@tonic-gate ASSERT(rl_ent->rl_local == 0);
5670Sstevel@tonic-gate cid.cid_flags = 0;
5680Sstevel@tonic-gate isattrc = rl_ent->rl_attrc;
5690Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
5700Sstevel@tonic-gate
5710Sstevel@tonic-gate /* get the file system cache object for this fsid */
5720Sstevel@tonic-gate mutex_enter(&cachep->c_fslistlock);
5730Sstevel@tonic-gate fscp = fscache_list_find(cachep, fsid);
5740Sstevel@tonic-gate if (fscp == NULL) {
5750Sstevel@tonic-gate fscp = fscache_create(cachep);
5760Sstevel@tonic-gate error = fscache_activate(fscp, fsid, NULL, NULL, 0);
5770Sstevel@tonic-gate if (error) {
5780Sstevel@tonic-gate cmn_err(CE_WARN,
5790Sstevel@tonic-gate "cachefs: cache corruption, run fsck\n");
5800Sstevel@tonic-gate fscache_destroy(fscp);
5810Sstevel@tonic-gate fscp = NULL;
5820Sstevel@tonic-gate mutex_exit(&cachep->c_fslistlock);
5830Sstevel@tonic-gate error = 0;
5840Sstevel@tonic-gate goto out;
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate fscache_list_add(cachep, fscp);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate fscache_hold(fscp);
5890Sstevel@tonic-gate mutex_exit(&cachep->c_fslistlock);
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /* get the file group object for this file */
5920Sstevel@tonic-gate mutex_enter(&fscp->fs_fslock);
5930Sstevel@tonic-gate fgp = filegrp_list_find(fscp, &cid);
5940Sstevel@tonic-gate if (fgp == NULL) {
5950Sstevel@tonic-gate fgp = filegrp_create(fscp, &cid);
5960Sstevel@tonic-gate filegrp_list_add(fscp, fgp);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate if (fgp->fg_flags & CFS_FG_ALLOC_ATTR) {
5990Sstevel@tonic-gate if (isattrc == 0) {
6000Sstevel@tonic-gate cmn_err(CE_WARN,
6010Sstevel@tonic-gate "cachefs: cache corruption, run fsck\n");
6020Sstevel@tonic-gate delay(5*hz);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate filegrp_list_remove(fscp, fgp);
6050Sstevel@tonic-gate filegrp_destroy(fgp);
6060Sstevel@tonic-gate error = 0;
6070Sstevel@tonic-gate fgp = NULL;
6080Sstevel@tonic-gate mutex_exit(&fscp->fs_fslock);
6090Sstevel@tonic-gate goto out;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate /* if we are victimizing an attrcache file */
6130Sstevel@tonic-gate if (isattrc) {
6140Sstevel@tonic-gate mutex_enter(&fgp->fg_mutex);
6150Sstevel@tonic-gate /* if the filegrp is not writable */
6160Sstevel@tonic-gate if ((fgp->fg_flags & CFS_FG_WRITE) == 0) {
6170Sstevel@tonic-gate mutex_exit(&fgp->fg_mutex);
6180Sstevel@tonic-gate error = EROFS;
6190Sstevel@tonic-gate fgp = NULL;
6200Sstevel@tonic-gate mutex_exit(&fscp->fs_fslock);
6210Sstevel@tonic-gate goto out;
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate /* if the filegrp did not go active on us */
6250Sstevel@tonic-gate if ((fgp->fg_count == 0) && (fgp->fg_header->ach_nffs == 0)) {
6260Sstevel@tonic-gate mutex_exit(&fgp->fg_mutex);
6270Sstevel@tonic-gate filegrp_list_remove(fscp, fgp);
6280Sstevel@tonic-gate fgp->fg_header->ach_count = 0;
6290Sstevel@tonic-gate filegrp_destroy(fgp);
6300Sstevel@tonic-gate } else {
6310Sstevel@tonic-gate #ifdef CFSDEBUG
6320Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
6330Sstevel@tonic-gate printf("c_victim: filegrp went active"
6340Sstevel@tonic-gate " %p %llu %d %d %lld\n",
6350Sstevel@tonic-gate (void *) fgp,
6360Sstevel@tonic-gate (u_longlong_t)fgp->fg_id.cid_fileno,
6370Sstevel@tonic-gate fgp->fg_header->ach_rlno,
6380Sstevel@tonic-gate fgp->fg_count, fgp->fg_header->ach_nffs);
6390Sstevel@tonic-gate #endif
6400Sstevel@tonic-gate ASSERT(fgp->fg_header->ach_rl_current !=
6410Sstevel@tonic-gate CACHEFS_RL_GC);
6420Sstevel@tonic-gate mutex_exit(&fgp->fg_mutex);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate fgp = NULL;
6450Sstevel@tonic-gate error = 0;
6460Sstevel@tonic-gate mutex_exit(&fscp->fs_fslock);
6470Sstevel@tonic-gate goto out;
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate ASSERT((fgp->fg_flags & CFS_FG_ALLOC_FILE) == 0);
6500Sstevel@tonic-gate filegrp_hold(fgp);
6510Sstevel@tonic-gate mutex_exit(&fscp->fs_fslock);
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate /* grab the cnode list lock */
6540Sstevel@tonic-gate mutex_enter(&fgp->fg_cnodelock);
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate /* see if a cnode exists for this file */
6570Sstevel@tonic-gate (void) cachefs_cnode_find(fgp, &cid, NULL, &cp, NULL, NULL);
6580Sstevel@tonic-gate if (cp) {
6590Sstevel@tonic-gate VN_HOLD(CTOV(cp));
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate /* move file from rl to active list */
6620Sstevel@tonic-gate cachefs_rlent_moveto(fscp->fs_cache,
6630Sstevel@tonic-gate CACHEFS_RL_ACTIVE, cp->c_metadata.md_rlno,
6640Sstevel@tonic-gate cp->c_metadata.md_frontblks);
6650Sstevel@tonic-gate cp->c_metadata.md_rltype = CACHEFS_RL_ACTIVE;
6660Sstevel@tonic-gate mutex_exit(&cp->c_statelock);
6670Sstevel@tonic-gate mutex_exit(&fgp->fg_cnodelock);
6680Sstevel@tonic-gate VN_RELE(CTOV(cp));
6690Sstevel@tonic-gate error = 0;
6700Sstevel@tonic-gate goto out;
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate * The cnode does not exist and since we hold the hashlock
6750Sstevel@tonic-gate * it cannot be created until we are done.
6760Sstevel@tonic-gate */
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate /* see if the item is no longer on the rl list, it could happen */
6790Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
6800Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_GC);
6810Sstevel@tonic-gate entno = lhp->rli_front;
6820Sstevel@tonic-gate if (entno == 0) {
6830Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
6840Sstevel@tonic-gate mutex_exit(&fgp->fg_cnodelock);
6850Sstevel@tonic-gate error = ENOSPC;
6860Sstevel@tonic-gate goto out;
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
6890Sstevel@tonic-gate if (error) {
6900Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
6910Sstevel@tonic-gate mutex_exit(&fgp->fg_cnodelock);
6920Sstevel@tonic-gate goto out;
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate if ((fsid != rl_ent->rl_fsid) ||
6950Sstevel@tonic-gate (cid.cid_fileno != rl_ent->rl_fileno)) {
6960Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
6970Sstevel@tonic-gate mutex_exit(&fgp->fg_cnodelock);
6980Sstevel@tonic-gate error = 0;
6990Sstevel@tonic-gate goto out;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate /* Get the metadata from the attrcache file */
7040Sstevel@tonic-gate ASSERT((fgp->fg_flags & CFS_FG_ALLOC_ATTR) == 0);
7050Sstevel@tonic-gate error = filegrp_read_metadata(fgp, &cid, &md);
7060Sstevel@tonic-gate ASSERT(error == 0);
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate /* md.md_rltype may be incorrect, but we know file isn't active. */
7090Sstevel@tonic-gate if (error) {
7100Sstevel@tonic-gate /* XXX this should never happen, fix on panic */
7110Sstevel@tonic-gate mutex_exit(&fgp->fg_cnodelock);
7120Sstevel@tonic-gate error = 0;
7130Sstevel@tonic-gate goto out;
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate /* destroy the frontfile */
7170Sstevel@tonic-gate cachefs_removefrontfile(&md, &cid, fgp);
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate /* remove the victim from the gc list */
7200Sstevel@tonic-gate cachefs_rlent_moveto(fscp->fs_cache, CACHEFS_RL_FREE, entno, 0);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate /* destroy the metadata */
7230Sstevel@tonic-gate (void) filegrp_destroy_metadata(fgp, &cid);
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate mutex_exit(&fgp->fg_cnodelock);
7260Sstevel@tonic-gate error = 0;
7270Sstevel@tonic-gate out:
7280Sstevel@tonic-gate if (fgp) {
7290Sstevel@tonic-gate filegrp_rele(fgp);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate if (fscp) {
7320Sstevel@tonic-gate fscache_rele(fscp);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate return (error);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate static void
cachefs_garbage_collect(cachefscache_t * cachep)7380Sstevel@tonic-gate cachefs_garbage_collect(cachefscache_t *cachep)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate fsfilcnt64_t filelowat, filelowatmax, maxfiles, threshfiles;
7410Sstevel@tonic-gate fsblkcnt64_t blocklowat, blocklowatmax, maxblks, threshblks;
7420Sstevel@tonic-gate int error;
7430Sstevel@tonic-gate struct cache_usage *cup = &cachep->c_usage;
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate ASSERT((cachep->c_flags & (CACHE_NOCACHE|CACHE_NOFILL)) == 0);
7460Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
7470Sstevel@tonic-gate ASSERT(cachep->c_flags & CACHE_GARBAGE_COLLECT);
7480Sstevel@tonic-gate filelowat = cachep->c_label.cl_filelowat;
7490Sstevel@tonic-gate blocklowat = cachep->c_label.cl_blklowat;
7500Sstevel@tonic-gate maxblks = cachep->c_label.cl_maxblks;
7510Sstevel@tonic-gate maxfiles = cachep->c_label.cl_maxinodes;
7520Sstevel@tonic-gate threshblks = cachep->c_label.cl_blocktresh;
7530Sstevel@tonic-gate threshfiles = cachep->c_label.cl_filetresh;
7540Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate cachep->c_gc_count++;
7570Sstevel@tonic-gate cachep->c_gc_time = time;
7580Sstevel@tonic-gate cachep->c_gc_before = cachefs_gc_front_atime(cachep);
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate /*
7610Sstevel@tonic-gate * since we're here, we're running out of blocks or files.
7620Sstevel@tonic-gate * file and block lowat are what determine how low we garbage
7630Sstevel@tonic-gate * collect. in order to do any good, we should drop below
7640Sstevel@tonic-gate * maxblocks, threshblocks, or the current blocks, whichever
7650Sstevel@tonic-gate * is smaller (same goes for files). however, we won't go
7660Sstevel@tonic-gate * below an arbitrary (small) minimum for each.
7670Sstevel@tonic-gate */
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate /* move down for maxfiles and maxblocks */
7700Sstevel@tonic-gate if ((filelowatmax = (maxfiles * 7) / 10) < filelowat)
7710Sstevel@tonic-gate filelowat = filelowatmax;
7720Sstevel@tonic-gate if ((blocklowatmax = (maxblks * 7) / 10) < blocklowat)
7730Sstevel@tonic-gate blocklowat = blocklowatmax;
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate /* move down for threshfiles and threshblocks */
7760Sstevel@tonic-gate if ((filelowatmax = (threshfiles * 7) / 10) < filelowat)
7770Sstevel@tonic-gate filelowat = filelowatmax;
7780Sstevel@tonic-gate if ((blocklowatmax = (threshblks * 7) / 10) < blocklowat)
7790Sstevel@tonic-gate blocklowat = blocklowatmax;
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate /* move down for current files and blocks */
7820Sstevel@tonic-gate if ((filelowatmax = ((fsfilcnt64_t)cup->cu_filesused * 7) / 10) <
7830Sstevel@tonic-gate filelowat)
7840Sstevel@tonic-gate filelowat = filelowatmax;
7850Sstevel@tonic-gate if ((blocklowatmax = ((fsblkcnt64_t)cup->cu_blksused * 7) / 10) <
7860Sstevel@tonic-gate blocklowat)
7870Sstevel@tonic-gate blocklowat = blocklowatmax;
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate /* move up for an arbitrary minimum */
7900Sstevel@tonic-gate #define MIN_BLKLO 640 /* 640*8192 == 5MB */
7910Sstevel@tonic-gate #define MIN_FILELO 1000
7920Sstevel@tonic-gate if (filelowat < MIN_FILELO)
7930Sstevel@tonic-gate filelowat = MIN_FILELO;
7940Sstevel@tonic-gate if (blocklowat < MIN_BLKLO)
7950Sstevel@tonic-gate blocklowat = MIN_BLKLO;
7960Sstevel@tonic-gate
7970Sstevel@tonic-gate while (cup->cu_filesused > filelowat || cup->cu_blksused > blocklowat) {
7980Sstevel@tonic-gate /* if the thread is to terminate */
7990Sstevel@tonic-gate if (cachep->c_flags & CACHE_CACHEW_THREADEXIT)
8000Sstevel@tonic-gate break;
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate error = cachefs_victim(cachep);
8030Sstevel@tonic-gate if (error)
8040Sstevel@tonic-gate break;
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate cachep->c_gc_after = cachefs_gc_front_atime(cachep);
8080Sstevel@tonic-gate CACHEFS_TIME_TO_CFS_TIME_COPY(cachep->c_gc_after,
809*11066Srafael.vanoni@sun.com cachep->c_rlinfo.rl_gctime, error);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate /*
8130Sstevel@tonic-gate * traverse the packed pending list, repacking files when possible.
8140Sstevel@tonic-gate */
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate static void
cachefs_packed_pending(cachefscache_t * cachep)8170Sstevel@tonic-gate cachefs_packed_pending(cachefscache_t *cachep)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate rl_entry_t rl;
8200Sstevel@tonic-gate int error = 0; /* not returned -- used as placeholder */
8210Sstevel@tonic-gate fscache_t *fscp = NULL;
8220Sstevel@tonic-gate cfs_cid_t cid;
8230Sstevel@tonic-gate cnode_t *cp;
8240Sstevel@tonic-gate uint_t entno;
8250Sstevel@tonic-gate int count = 0;
8260Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cachep->c_contentslock));
8290Sstevel@tonic-gate
8300Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_PACKED_PENDING);
8310Sstevel@tonic-gate count = lhp->rli_itemcnt;
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate rl.rl_current = CACHEFS_RL_PACKED_PENDING;
8360Sstevel@tonic-gate while (cachefs_rlent_data(cachep, &rl, &entno) == 0) {
8370Sstevel@tonic-gate if (count-- <= 0) {
8380Sstevel@tonic-gate #ifdef CFSDEBUG
8390Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
8400Sstevel@tonic-gate printf("cachefs_ppending: count exceeded\n");
8410Sstevel@tonic-gate #endif /* CFSDEBUG */
8420Sstevel@tonic-gate break;
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate if ((cachep->c_flags &
8450Sstevel@tonic-gate (CACHE_PACKED_PENDING | CACHE_CACHEW_THREADEXIT)) !=
8460Sstevel@tonic-gate CACHE_PACKED_PENDING) {
8470Sstevel@tonic-gate #ifdef CFSDEBUG
8480Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
8490Sstevel@tonic-gate printf("cachefs_ppending: early exit\n");
8500Sstevel@tonic-gate #endif /* CFSDEBUG */
8510Sstevel@tonic-gate break;
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate if (rl.rl_current != CACHEFS_RL_PACKED_PENDING) {
8540Sstevel@tonic-gate #ifdef CFSDEBUG
8550Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
8560Sstevel@tonic-gate printf("cachefs_ppending: gone from list\n");
8570Sstevel@tonic-gate #endif /* CFSDEBUG */
8580Sstevel@tonic-gate break;
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate /* if the fscp we have does not match */
8620Sstevel@tonic-gate if ((fscp == NULL) || (fscp->fs_cfsid != rl.rl_fsid)) {
8630Sstevel@tonic-gate if (fscp) {
8640Sstevel@tonic-gate cachefs_cd_release(fscp);
8650Sstevel@tonic-gate fscache_rele(fscp);
8660Sstevel@tonic-gate fscp = NULL;
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate /* get the file system cache object for this fsid */
8700Sstevel@tonic-gate mutex_enter(&cachep->c_fslistlock);
8710Sstevel@tonic-gate fscp = fscache_list_find(cachep, rl.rl_fsid);
8720Sstevel@tonic-gate if (fscp == NULL) {
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate * uh oh, the filesystem probably
8760Sstevel@tonic-gate * isn't mounted. we `move' this
8770Sstevel@tonic-gate * entry onto the same list that it's
8780Sstevel@tonic-gate * on, which really just moves it to
8790Sstevel@tonic-gate * the back of the list. we need not
8800Sstevel@tonic-gate * worry about an infinite loop, due
8810Sstevel@tonic-gate * to the counter.
8820Sstevel@tonic-gate */
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate cachefs_rlent_moveto(cachep,
8850Sstevel@tonic-gate CACHEFS_RL_PACKED_PENDING, entno, 0);
8860Sstevel@tonic-gate #ifdef CFSDEBUG
8870Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
8880Sstevel@tonic-gate printf("cachefs_ppending: "
8890Sstevel@tonic-gate "fscp find failed\n");
8900Sstevel@tonic-gate #endif /* CFSDEBUG */
8910Sstevel@tonic-gate continue;
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate fscache_hold(fscp);
8940Sstevel@tonic-gate mutex_exit(&cachep->c_fslistlock);
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate /* get access to the file system */
8970Sstevel@tonic-gate error = cachefs_cd_access(fscp, 0, 0);
8980Sstevel@tonic-gate if ((error) ||
8990Sstevel@tonic-gate (fscp->fs_cdconnected != CFS_CD_CONNECTED)) {
9000Sstevel@tonic-gate #ifdef CFSDEBUG
9010Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
9020Sstevel@tonic-gate printf("cachefs: "
9030Sstevel@tonic-gate "ppending: err %d con %d\n",
9040Sstevel@tonic-gate error, fscp->fs_cdconnected);
9050Sstevel@tonic-gate #endif /* CFSDEBUG */
9060Sstevel@tonic-gate fscache_rele(fscp);
9070Sstevel@tonic-gate fscp = NULL;
9080Sstevel@tonic-gate break;
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate /* get the cnode for the file */
9130Sstevel@tonic-gate cid.cid_fileno = rl.rl_fileno;
9140Sstevel@tonic-gate cid.cid_flags = rl.rl_local ? CFS_CID_LOCAL : 0;
9150Sstevel@tonic-gate error = cachefs_cnode_make(&cid, fscp,
9160Sstevel@tonic-gate NULL, NULL, NULL, kcred, 0, &cp);
9170Sstevel@tonic-gate if (error) {
9180Sstevel@tonic-gate #ifdef CFSDEBUG
9190Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
9200Sstevel@tonic-gate printf("cachefs: "
9210Sstevel@tonic-gate "ppending: could not find %llu\n",
9220Sstevel@tonic-gate (u_longlong_t)cid.cid_fileno);
9230Sstevel@tonic-gate delay(5*hz);
9240Sstevel@tonic-gate #endif /* CFSDEBUG */
9250Sstevel@tonic-gate break;
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate mutex_enter(&cp->c_statelock);
9290Sstevel@tonic-gate if (cp->c_flags & CN_STALE) {
9300Sstevel@tonic-gate /* back file went away behind our back */
9310Sstevel@tonic-gate ASSERT(cp->c_metadata.md_rlno == 0);
9320Sstevel@tonic-gate mutex_exit(&cp->c_statelock);
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate #ifdef CFSDEBUG
9350Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
9360Sstevel@tonic-gate printf("cachefs: ppending: stale\n");
9370Sstevel@tonic-gate #endif /* CFSDEBUG */
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate VN_RELE(CTOV(cp));
9400Sstevel@tonic-gate continue;
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate mutex_exit(&cp->c_statelock);
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate error = cachefs_pack_common(CTOV(cp),
9450Sstevel@tonic-gate (cp->c_cred) ? cp->c_cred : kcred);
9460Sstevel@tonic-gate VN_RELE(CTOV(cp));
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate if (error != 0) {
9490Sstevel@tonic-gate #ifdef CFSDEBUG
9500Sstevel@tonic-gate CFS_DEBUG(CFSDEBUG_RESOURCE)
9510Sstevel@tonic-gate printf("cachefs: "
9520Sstevel@tonic-gate "ppending: pack_common: error = %d\n",
9530Sstevel@tonic-gate error);
9540Sstevel@tonic-gate #endif /* CFSDEBUG */
9550Sstevel@tonic-gate break;
9560Sstevel@tonic-gate }
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate if (fscp != NULL) {
9600Sstevel@tonic-gate cachefs_cd_release(fscp);
9610Sstevel@tonic-gate fscache_rele(fscp);
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
9650Sstevel@tonic-gate if (lhp->rli_itemcnt == 0)
9660Sstevel@tonic-gate cachep->c_flags &= ~CACHE_PACKED_PENDING;
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate /* seconds; interval to do ppend list */
9700Sstevel@tonic-gate static time_t cachefs_ppend_time = 900;
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate /* main routine for the cachep worker thread */
9730Sstevel@tonic-gate void
cachefs_cachep_worker_thread(cachefscache_t * cachep)9740Sstevel@tonic-gate cachefs_cachep_worker_thread(cachefscache_t *cachep)
9750Sstevel@tonic-gate {
9760Sstevel@tonic-gate int error;
9770Sstevel@tonic-gate struct flock64 fl;
9780Sstevel@tonic-gate callb_cpr_t cprinfo;
9790Sstevel@tonic-gate kmutex_t cpr_lock;
980*11066Srafael.vanoni@sun.com clock_t wakeup;
9810Sstevel@tonic-gate
9820Sstevel@tonic-gate /* lock the lock file for exclusive write access */
9830Sstevel@tonic-gate fl.l_type = F_WRLCK;
9840Sstevel@tonic-gate fl.l_whence = 0;
9850Sstevel@tonic-gate fl.l_start = (offset_t)0;
9860Sstevel@tonic-gate fl.l_len = (offset_t)1024;
9870Sstevel@tonic-gate fl.l_sysid = 0;
9880Sstevel@tonic-gate fl.l_pid = 0;
989*11066Srafael.vanoni@sun.com error = VOP_FRLOCK(cachep->c_lockvp, F_SETLK, &fl, FWRITE, (offset_t)0,
990*11066Srafael.vanoni@sun.com NULL, kcred, NULL);
9910Sstevel@tonic-gate if (error) {
9920Sstevel@tonic-gate cmn_err(CE_WARN,
9930Sstevel@tonic-gate "cachefs: Can't lock Cache Lock File(r); Error %d\n",
9940Sstevel@tonic-gate error);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
9980Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &cpr_lock, callb_generic_cpr, "cfs_gct");
9990Sstevel@tonic-gate mutex_enter(&cpr_lock);
10000Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
10010Sstevel@tonic-gate
1002*11066Srafael.vanoni@sun.com wakeup = (clock_t)(cachefs_ppend_time * hz);
1003*11066Srafael.vanoni@sun.com
10040Sstevel@tonic-gate /* loop while the thread is allowed to run */
10050Sstevel@tonic-gate while ((cachep->c_flags & CACHE_CACHEW_THREADEXIT) == 0) {
10060Sstevel@tonic-gate /* wait for a wakeup call */
10070Sstevel@tonic-gate cachep->c_flags &= ~CACHE_GARBAGE_COLLECT;
10080Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
10090Sstevel@tonic-gate mutex_exit(&cpr_lock);
1010*11066Srafael.vanoni@sun.com (void) cv_reltimedwait(&cachep->c_cwcv,
1011*11066Srafael.vanoni@sun.com &cachep->c_contentslock, wakeup, TR_CLOCK_TICK);
10120Sstevel@tonic-gate mutex_enter(&cpr_lock);
10130Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &cpr_lock);
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate /* if the thread is to terminate */
10160Sstevel@tonic-gate if (cachep->c_flags & CACHE_CACHEW_THREADEXIT)
10170Sstevel@tonic-gate break;
10180Sstevel@tonic-gate
10190Sstevel@tonic-gate /* thread is running during nofill, but just to hold lock */
10200Sstevel@tonic-gate if (cachep->c_flags & CACHE_NOFILL)
10210Sstevel@tonic-gate continue;
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate /* if garbage collection is to run */
10240Sstevel@tonic-gate if (cachep->c_flags & CACHE_GARBAGE_COLLECT) {
10250Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
10260Sstevel@tonic-gate cachefs_garbage_collect(cachep);
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate /*
10290Sstevel@tonic-gate * Prevent garbage collection from running more
10300Sstevel@tonic-gate * than once every 30 seconds. This addresses
10310Sstevel@tonic-gate * those cases which do not allow removing
10320Sstevel@tonic-gate * an item from the rl by keeping gc from
10330Sstevel@tonic-gate * being a spin loop.
10340Sstevel@tonic-gate */
10350Sstevel@tonic-gate delay(30*hz); /* XXX sam: still do this? */
10360Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate
10390Sstevel@tonic-gate if (cachep->c_flags & CACHE_PACKED_PENDING)
10400Sstevel@tonic-gate cachefs_packed_pending(cachep);
10410Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cachep->c_contentslock));
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate cachep->c_flags &= ~CACHE_CACHEW_THREADRUN;
10450Sstevel@tonic-gate cv_broadcast(&cachep->c_cwhaltcv);
10460Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo);
10470Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
10480Sstevel@tonic-gate mutex_destroy(&cpr_lock);
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate /* unlock the lock file */
10510Sstevel@tonic-gate fl.l_type = F_UNLCK;
10520Sstevel@tonic-gate fl.l_whence = 0;
10530Sstevel@tonic-gate fl.l_start = (offset_t)0;
10540Sstevel@tonic-gate fl.l_len = (offset_t)1024;
10550Sstevel@tonic-gate fl.l_sysid = 0;
10560Sstevel@tonic-gate fl.l_pid = 0;
1057*11066Srafael.vanoni@sun.com error = VOP_FRLOCK(cachep->c_lockvp, F_SETLK, &fl, FWRITE, (offset_t)0,
1058*11066Srafael.vanoni@sun.com NULL, kcred, NULL);
10590Sstevel@tonic-gate if (error) {
10600Sstevel@tonic-gate cmn_err(CE_WARN, "cachefs: Can't unlock lock file\n");
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate
10630Sstevel@tonic-gate thread_exit();
10640Sstevel@tonic-gate /*NOTREACHED*/
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate
10670Sstevel@tonic-gate /* queues up a request to run the garbage collection */
10680Sstevel@tonic-gate void
cachefs_garbage_collect_queue(cachefscache_t * cachep)10690Sstevel@tonic-gate cachefs_garbage_collect_queue(cachefscache_t *cachep)
10700Sstevel@tonic-gate {
10710Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
10720Sstevel@tonic-gate
10730Sstevel@tonic-gate ASSERT((cachep->c_flags & (CACHE_NOCACHE|CACHE_NOFILL)) == 0);
10740Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
10750Sstevel@tonic-gate
10760Sstevel@tonic-gate /* quit if there is no garbage collection thread */
10770Sstevel@tonic-gate if ((cachep->c_flags & CACHE_CACHEW_THREADRUN) == 0) {
10780Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
10790Sstevel@tonic-gate return;
10800Sstevel@tonic-gate }
10810Sstevel@tonic-gate
10820Sstevel@tonic-gate /* quit if garbage collection is already in progress */
10830Sstevel@tonic-gate if (cachep->c_flags & CACHE_GARBAGE_COLLECT) {
10840Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
10850Sstevel@tonic-gate return;
10860Sstevel@tonic-gate }
10870Sstevel@tonic-gate
10880Sstevel@tonic-gate /* quit if there is no garbage to collect */
10890Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_GC);
10900Sstevel@tonic-gate if (lhp->rli_front == 0) {
10910Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
10920Sstevel@tonic-gate return;
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate /* indicate garbage collecting is in progress */
10960Sstevel@tonic-gate cachep->c_flags |= CACHE_GARBAGE_COLLECT;
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate /* wake up the garbage collection thread */
10990Sstevel@tonic-gate cv_signal(&cachep->c_cwcv);
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate #ifdef CFSRLDEBUG
11050Sstevel@tonic-gate time_t cachefs_dbvalid = 123; /* default to non-zero junk */
11060Sstevel@tonic-gate struct kmem_cache *cachefs_rl_debug_cache = NULL;
11070Sstevel@tonic-gate static int cachefs_rl_debug_maxcount = CACHEFS_RLDB_DEF_MAXCOUNT;
11080Sstevel@tonic-gate kmutex_t cachefs_rl_debug_mutex;
11090Sstevel@tonic-gate static int cachefs_rl_debug_inuse = 0;
11100Sstevel@tonic-gate
11110Sstevel@tonic-gate void
cachefs_rl_debug_reclaim(void * cdrarg)11120Sstevel@tonic-gate cachefs_rl_debug_reclaim(void *cdrarg)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate extern cachefscache_t *cachefs_cachelist;
11150Sstevel@tonic-gate cachefscache_t *cachep;
11160Sstevel@tonic-gate int index;
11170Sstevel@tonic-gate int error;
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate for (cachep = cachefs_cachelist; cachep != NULL;
1120*11066Srafael.vanoni@sun.com cachep = cachep->c_next) {
11210Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
11220Sstevel@tonic-gate
11230Sstevel@tonic-gate for (index = 0;
11240Sstevel@tonic-gate index <= cachep->c_rlinfo.rl_entries;
11250Sstevel@tonic-gate index++) {
11260Sstevel@tonic-gate rl_entry_t *rlent;
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, index, &rlent);
11290Sstevel@tonic-gate if (error)
11300Sstevel@tonic-gate break;
11310Sstevel@tonic-gate cachefs_rl_debug_destroy(rlent);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate void
cachefs_rl_debug_save(rl_entry_t * rlent)11390Sstevel@tonic-gate cachefs_rl_debug_save(rl_entry_t *rlent)
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate rl_debug_t *rldb, *prev, *next;
11420Sstevel@tonic-gate int count = 0;
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate mutex_enter(&cachefs_rl_debug_mutex);
11450Sstevel@tonic-gate if (cachefs_rl_debug_cache == NULL)
11460Sstevel@tonic-gate cachefs_rl_debug_cache =
11470Sstevel@tonic-gate kmem_cache_create("cachefs_rl_debug",
1148*11066Srafael.vanoni@sun.com sizeof (rl_debug_t), 0,
1149*11066Srafael.vanoni@sun.com NULL, NULL, cachefs_rl_debug_reclaim, NULL, NULL, 0);
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate rldb = kmem_cache_alloc(cachefs_rl_debug_cache, KM_SLEEP);
11520Sstevel@tonic-gate ++cachefs_rl_debug_inuse;
11530Sstevel@tonic-gate
11540Sstevel@tonic-gate rldb->db_hrtime = gethrtime();
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate rldb->db_attrc = rlent->rl_attrc;
11570Sstevel@tonic-gate rldb->db_fsck = rlent->rl_fsck;
11580Sstevel@tonic-gate rldb->db_fsid = rlent->rl_fsid;
11590Sstevel@tonic-gate rldb->db_fileno = rlent->rl_fileno;
11600Sstevel@tonic-gate rldb->db_current = rlent->rl_current;
11610Sstevel@tonic-gate
11620Sstevel@tonic-gate rldb->db_stackheight = getpcstack(rldb->db_stack,
11630Sstevel@tonic-gate CACHEFS_RLDB_STACKSIZE);
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate if (rlent->rl_dbvalid == cachefs_dbvalid) {
11660Sstevel@tonic-gate rldb->db_next = rlent->rl_debug;
11670Sstevel@tonic-gate } else {
11680Sstevel@tonic-gate rldb->db_next = NULL;
11690Sstevel@tonic-gate rlent->rl_dbvalid = cachefs_dbvalid;
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate rlent->rl_debug = rldb;
11720Sstevel@tonic-gate
11730Sstevel@tonic-gate prev = rldb;
11740Sstevel@tonic-gate for (rldb = rldb->db_next; rldb != NULL; rldb = next) {
11750Sstevel@tonic-gate next = rldb->db_next;
11760Sstevel@tonic-gate if (++count >= cachefs_rl_debug_maxcount) {
11770Sstevel@tonic-gate if (prev != NULL)
11780Sstevel@tonic-gate prev->db_next = NULL;
11790Sstevel@tonic-gate kmem_cache_free(cachefs_rl_debug_cache, rldb);
11800Sstevel@tonic-gate --cachefs_rl_debug_inuse;
11810Sstevel@tonic-gate prev = NULL;
11820Sstevel@tonic-gate } else {
11830Sstevel@tonic-gate prev = rldb;
11840Sstevel@tonic-gate }
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate mutex_exit(&cachefs_rl_debug_mutex);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate void
cachefs_rl_debug_show(rl_entry_t * rlent)11900Sstevel@tonic-gate cachefs_rl_debug_show(rl_entry_t *rlent)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate rl_debug_t *rldb;
11930Sstevel@tonic-gate hrtime_t now, elapse;
11940Sstevel@tonic-gate timestruc_t tv;
11950Sstevel@tonic-gate char *cname = NULL;
11960Sstevel@tonic-gate int i;
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate mutex_enter(&cachefs_rl_debug_mutex);
11990Sstevel@tonic-gate if (rlent->rl_dbvalid != cachefs_dbvalid) {
12000Sstevel@tonic-gate printf("cachefs_rldb: rl entry at %lx -- no info!\n",
12010Sstevel@tonic-gate (uintptr_t)rlent);
12020Sstevel@tonic-gate mutex_exit(&cachefs_rl_debug_mutex);
12030Sstevel@tonic-gate return;
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate now = gethrtime();
12070Sstevel@tonic-gate hrt2ts(now, &tv);
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate printf("===== cachefs_rldb start at %ld =====\n", tv.tv_sec);
12100Sstevel@tonic-gate printf("-==== i am thread id %lx ====-\n", (uintptr_t)curthread);
12110Sstevel@tonic-gate
12120Sstevel@tonic-gate for (rldb = rlent->rl_debug;
12130Sstevel@tonic-gate rldb != NULL;
12140Sstevel@tonic-gate rldb = rldb->db_next) {
12150Sstevel@tonic-gate printf("----- cachefs_rldb record start -----\n");
12160Sstevel@tonic-gate elapse = now - rldb->db_hrtime;
12170Sstevel@tonic-gate hrt2ts(elapse, &tv);
12180Sstevel@tonic-gate printf("cachefs_rldb: ago = %lds %ldus\n",
12190Sstevel@tonic-gate tv.tv_sec, tv.tv_nsec / 1000);
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate printf("cachefs_rldb: rl_attrc = %d\n", rldb->db_attrc);
12220Sstevel@tonic-gate printf("cachefs_rldb: rl_fsck = %d\n", rldb->db_fsck);
12230Sstevel@tonic-gate printf("cachefs_rldb: rl_fsid = %u\n", rldb->db_fsid);
12240Sstevel@tonic-gate printf("cachefs_rldb: rl_fileno = %lu\n", rldb->db_fileno);
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate switch (rldb->db_current) {
12270Sstevel@tonic-gate case CACHEFS_RL_NONE:
12280Sstevel@tonic-gate cname = "CACHEFS_RL_NONE";
12290Sstevel@tonic-gate break;
12300Sstevel@tonic-gate case CACHEFS_RL_FREE:
12310Sstevel@tonic-gate cname = "CACHEFS_RL_FREE";
12320Sstevel@tonic-gate break;
12330Sstevel@tonic-gate case CACHEFS_RL_GC:
12340Sstevel@tonic-gate cname = "CACHEFS_RL_GC";
12350Sstevel@tonic-gate break;
12360Sstevel@tonic-gate case CACHEFS_RL_ACTIVE:
12370Sstevel@tonic-gate cname = "CACHEFS_RL_ACTIVE";
12380Sstevel@tonic-gate break;
12390Sstevel@tonic-gate case CACHEFS_RL_ATTRFILE:
12400Sstevel@tonic-gate cname = "CACHEFS_RL_ATTRFILE";
12410Sstevel@tonic-gate break;
12420Sstevel@tonic-gate case CACHEFS_RL_MODIFIED:
12430Sstevel@tonic-gate cname = "CACHEFS_RL_MODIFIED";
12440Sstevel@tonic-gate break;
12450Sstevel@tonic-gate case CACHEFS_RL_PACKED:
12460Sstevel@tonic-gate cname = "CACHEFS_RL_PACKED";
12470Sstevel@tonic-gate break;
12480Sstevel@tonic-gate case CACHEFS_RL_PACKED_PENDING:
12490Sstevel@tonic-gate cname = "CACHEFS_RL_PACKED_PENDING";
12500Sstevel@tonic-gate break;
12510Sstevel@tonic-gate case CACHEFS_RL_MF:
12520Sstevel@tonic-gate cname = "CACHEFS_MF_GC";
12530Sstevel@tonic-gate break;
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate if (cname != NULL) {
12560Sstevel@tonic-gate printf("cachefs_rldb: state = %s\n", cname);
12570Sstevel@tonic-gate } else {
12580Sstevel@tonic-gate printf("cachefs_rldb: undefined state %x\n",
12590Sstevel@tonic-gate rldb->db_current);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate
12620Sstevel@tonic-gate printf("cachefs_rldb: stack trace\n");
12630Sstevel@tonic-gate for (i = 0; i < rldb->db_stackheight; i++) {
12640Sstevel@tonic-gate char *sym;
12650Sstevel@tonic-gate uint_t off;
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate sym = kobj_getsymname(rldb->db_stack[i], &off);
12680Sstevel@tonic-gate printf("cachefs_rldb: %s+%lx\n",
12690Sstevel@tonic-gate sym ? sym : "?", off);
12700Sstevel@tonic-gate delay(hz/4);
12710Sstevel@tonic-gate }
12720Sstevel@tonic-gate
12730Sstevel@tonic-gate printf("----- cachefs_rldb record end -----\n");
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate
12760Sstevel@tonic-gate mutex_exit(&cachefs_rl_debug_mutex);
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate void
cachefs_rl_debug_destroy(rl_entry_t * rlent)12800Sstevel@tonic-gate cachefs_rl_debug_destroy(rl_entry_t *rlent)
12810Sstevel@tonic-gate {
12820Sstevel@tonic-gate rl_debug_t *rldb, *next;
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate mutex_enter(&cachefs_rl_debug_mutex);
12850Sstevel@tonic-gate if (rlent->rl_dbvalid != cachefs_dbvalid) {
12860Sstevel@tonic-gate rlent->rl_debug = NULL;
12870Sstevel@tonic-gate mutex_exit(&cachefs_rl_debug_mutex);
12880Sstevel@tonic-gate return;
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate for (rldb = rlent->rl_debug; rldb != NULL; rldb = next) {
12920Sstevel@tonic-gate next = rldb->db_next;
12930Sstevel@tonic-gate kmem_cache_free(cachefs_rl_debug_cache, rldb);
12940Sstevel@tonic-gate --cachefs_rl_debug_inuse;
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate
12970Sstevel@tonic-gate rlent->rl_debug = NULL;
12980Sstevel@tonic-gate mutex_exit(&cachefs_rl_debug_mutex);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate #endif /* CFSRLDEBUG */
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate int
cachefs_rl_entry_get(cachefscache_t * cachep,uint_t entno,rl_entry_t ** ent)13030Sstevel@tonic-gate cachefs_rl_entry_get(cachefscache_t *cachep, uint_t entno, rl_entry_t **ent)
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate rl_entry_t *rl_ent;
13060Sstevel@tonic-gate uint_t whichwindow, winoffset;
13070Sstevel@tonic-gate int error = 0;
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cachep->c_contentslock));
13100Sstevel@tonic-gate ASSERT(entno <= cachep->c_label.cl_maxinodes); /* strictly less? */
13110Sstevel@tonic-gate #if 0
13120Sstevel@tonic-gate ASSERT((cachep->c_flags & CACHE_NOFILL) == 0);
13130Sstevel@tonic-gate #endif
13140Sstevel@tonic-gate
13150Sstevel@tonic-gate whichwindow = entno / CACHEFS_RLPMBS;
13160Sstevel@tonic-gate winoffset = entno % CACHEFS_RLPMBS;
13170Sstevel@tonic-gate
13180Sstevel@tonic-gate if ((cachep->c_rl_entries == NULL) ||
13190Sstevel@tonic-gate (cachep->c_rl_window != whichwindow)) {
13200Sstevel@tonic-gate if (cachep->c_rl_entries != NULL) {
13210Sstevel@tonic-gate error = vn_rdwr(UIO_WRITE, cachep->c_resfilevp,
13220Sstevel@tonic-gate (caddr_t)cachep->c_rl_entries, MAXBSIZE,
13230Sstevel@tonic-gate (offset_t)((cachep->c_rl_window + 1) * MAXBSIZE),
13240Sstevel@tonic-gate UIO_SYSSPACE, 0, RLIM_INFINITY, kcred, NULL);
13250Sstevel@tonic-gate if (error)
13260Sstevel@tonic-gate return (error);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate else
13290Sstevel@tonic-gate cachep->c_rl_entries = (rl_entry_t *)
13300Sstevel@tonic-gate cachefs_kmem_alloc(MAXBSIZE, KM_SLEEP);
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate error = vn_rdwr(UIO_READ, cachep->c_resfilevp,
13330Sstevel@tonic-gate (caddr_t)cachep->c_rl_entries, MAXBSIZE,
13340Sstevel@tonic-gate (offset_t)((whichwindow + 1) * MAXBSIZE),
13350Sstevel@tonic-gate UIO_SYSSPACE, 0, RLIM_INFINITY, kcred, NULL);
13360Sstevel@tonic-gate if (error) {
13370Sstevel@tonic-gate cachefs_kmem_free(cachep->c_rl_entries, MAXBSIZE);
13380Sstevel@tonic-gate cachep->c_rl_entries = NULL;
13390Sstevel@tonic-gate return (error);
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate cachep->c_rl_window = whichwindow;
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate rl_ent = &cachep->c_rl_entries[winoffset];
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate *ent = rl_ent;
13460Sstevel@tonic-gate #ifdef CFSRLDEBUG
13470Sstevel@tonic-gate cachefs_rl_debug_save(rl_ent);
13480Sstevel@tonic-gate #endif /* CFSRLDEBUG */
13490Sstevel@tonic-gate
13500Sstevel@tonic-gate return (error);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate static time_t
cachefs_gc_front_atime(cachefscache_t * cachep)13540Sstevel@tonic-gate cachefs_gc_front_atime(cachefscache_t *cachep)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate char namebuf[CFS_FRONTFILE_NAME_SIZE];
13570Sstevel@tonic-gate
13580Sstevel@tonic-gate rl_entry_t rl, *rl_ent;
13590Sstevel@tonic-gate uint_t entno, fgsize;
13600Sstevel@tonic-gate cfs_cid_t dircid, cid;
13610Sstevel@tonic-gate struct fscache *fscp;
13620Sstevel@tonic-gate cachefs_rl_listhead_t *lhp;
13630Sstevel@tonic-gate int error;
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate struct vnode *dirvp, *filevp;
13660Sstevel@tonic-gate struct vattr va;
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate int reledir = 0;
13690Sstevel@tonic-gate int gotfile = 0;
13700Sstevel@tonic-gate time_t rc = (time_t)0;
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate mutex_enter(&cachep->c_contentslock);
13730Sstevel@tonic-gate lhp = RL_HEAD(cachep, CACHEFS_RL_GC);
13740Sstevel@tonic-gate entno = lhp->rli_front;
13750Sstevel@tonic-gate if (entno == 0) {
13760Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
13770Sstevel@tonic-gate goto out;
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate
13800Sstevel@tonic-gate error = cachefs_rl_entry_get(cachep, entno, &rl_ent);
13810Sstevel@tonic-gate if (error) {
13820Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
13830Sstevel@tonic-gate goto out;
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate rl = *rl_ent;
13860Sstevel@tonic-gate mutex_exit(&cachep->c_contentslock);
13870Sstevel@tonic-gate cid.cid_fileno = rl.rl_fileno;
13880Sstevel@tonic-gate ASSERT(rl.rl_local == 0);
13890Sstevel@tonic-gate cid.cid_flags = 0;
13900Sstevel@tonic-gate dircid.cid_flags = 0;
13910Sstevel@tonic-gate mutex_enter(&cachep->c_fslistlock);
13920Sstevel@tonic-gate if ((fscp = fscache_list_find(cachep, rl.rl_fsid)) == NULL) {
13930Sstevel@tonic-gate mutex_exit(&cachep->c_fslistlock);
13940Sstevel@tonic-gate goto out;
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate
13970Sstevel@tonic-gate if (rl.rl_attrc) {
13980Sstevel@tonic-gate make_ascii_name(&cid, namebuf);
13990Sstevel@tonic-gate dirvp = fscp->fs_fsattrdir;
14000Sstevel@tonic-gate } else {
14010Sstevel@tonic-gate dirvp = NULL;
14020Sstevel@tonic-gate fgsize = fscp->fs_info.fi_fgsize;
14030Sstevel@tonic-gate dircid.cid_fileno = ((cid.cid_fileno / fgsize) * fgsize);
14040Sstevel@tonic-gate make_ascii_name(&dircid, namebuf);
14050Sstevel@tonic-gate if (VOP_LOOKUP(fscp->fs_fscdirvp, namebuf,
14060Sstevel@tonic-gate &dirvp, (struct pathname *)NULL, 0,
14075331Samw (vnode_t *)NULL, kcred, NULL, NULL, NULL) == 0) {
14080Sstevel@tonic-gate make_ascii_name(&cid, namebuf);
14090Sstevel@tonic-gate reledir++;
14100Sstevel@tonic-gate } else {
14110Sstevel@tonic-gate mutex_exit(&cachep->c_fslistlock);
14120Sstevel@tonic-gate goto out;
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate if (dirvp && VOP_LOOKUP(dirvp, namebuf, &filevp,
14160Sstevel@tonic-gate (struct pathname *)NULL, 0,
14175331Samw (vnode_t *)NULL, kcred, NULL, NULL, NULL) == 0) {
14180Sstevel@tonic-gate gotfile = 1;
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate if (reledir)
14210Sstevel@tonic-gate VN_RELE(dirvp);
14220Sstevel@tonic-gate mutex_exit(&cachep->c_fslistlock);
14230Sstevel@tonic-gate
14240Sstevel@tonic-gate if (gotfile) {
14250Sstevel@tonic-gate va.va_mask = AT_ATIME;
14265331Samw if (VOP_GETATTR(filevp, &va, 0, kcred, NULL) == 0)
14270Sstevel@tonic-gate rc = va.va_atime.tv_sec;
14280Sstevel@tonic-gate VN_RELE(filevp);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate
14310Sstevel@tonic-gate out:
14320Sstevel@tonic-gate return (rc);
14330Sstevel@tonic-gate }
1434