Annotation of Net2/vm/swap_pager.c, revision 1.1.1.5

1.1       root        1: /*
                      2:  * Copyright (c) 1990 University of Utah.
                      3:  * Copyright (c) 1991 The Regents of the University of California.
                      4:  * All rights reserved.
                      5:  *
                      6:  * This code is derived from software contributed to Berkeley by
                      7:  * the Systems Programming Group of the University of Utah Computer
                      8:  * Science Department.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  *
                     38:  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
                     39:  *
1.1.1.5 ! root       40:  *     from: @(#)swap_pager.c  7.4 (Berkeley) 5/7/91
        !            41:  *     swap_pager.c,v 1.6.2.1 1993/07/31 12:20:49 cgd Exp
1.1       root       42:  */
                     43: 
                     44: /*
                     45:  * Quick hack to page to dedicated partition(s).
                     46:  * TODO:
                     47:  *     Add multiprocessor locks
                     48:  *     Deal with async writes in a better fashion
                     49:  */
                     50: 
                     51: #include "swappager.h"
                     52: #if NSWAPPAGER > 0
                     53: 
                     54: #include "param.h"
                     55: #include "proc.h"
                     56: #include "buf.h"
1.1.1.5 ! root       57: #include "map.h"
1.1       root       58: #include "systm.h"
                     59: #include "specdev.h"
                     60: #include "vnode.h"
                     61: #include "malloc.h"
                     62: #include "queue.h"
                     63: 
                     64: #include "vm_param.h"
                     65: #include "queue.h"
                     66: #include "lock.h"
                     67: #include "vm_prot.h"
                     68: #include "vm_object.h"
                     69: #include "vm_page.h"
                     70: #include "vm_pageout.h"
                     71: #include "swap_pager.h"
                     72: 
                     73: #define NSWSIZES       16      /* size of swtab */
                     74: #define NPENDINGIO     64      /* max # of pending cleans */
                     75: #define MAXDADDRS      64      /* max # of disk addrs for fixed allocations */
                     76: 
                     77: #ifdef DEBUG
1.1.1.2   root       78: int    swpagerdebug = 0 /*0x100*/;
1.1       root       79: #define        SDB_FOLLOW      0x001
                     80: #define SDB_INIT       0x002
                     81: #define SDB_ALLOC      0x004
                     82: #define SDB_IO         0x008
                     83: #define SDB_WRITE      0x010
                     84: #define SDB_FAIL       0x020
                     85: #define SDB_ALLOCBLK   0x040
                     86: #define SDB_FULL       0x080
                     87: #define SDB_ANOM       0x100
                     88: #define SDB_ANOMPANIC  0x200
                     89: #endif
                     90: 
                     91: struct swpagerclean {
                     92:        queue_head_t            spc_list;
                     93:        int                     spc_flags;
                     94:        struct buf              *spc_bp;
                     95:        sw_pager_t              spc_swp;
                     96:        vm_offset_t             spc_kva;
                     97:        vm_page_t               spc_m;
                     98: } swcleanlist[NPENDINGIO];
                     99: typedef        struct swpagerclean     *swp_clean_t;
                    100: 
                    101: /* spc_flags values */
                    102: #define SPC_FREE       0x00
                    103: #define SPC_BUSY       0x01
                    104: #define SPC_DONE       0x02
                    105: #define SPC_ERROR      0x04
                    106: #define SPC_DIRTY      0x08
                    107: 
                    108: struct swtab {
                    109:        vm_size_t st_osize;     /* size of object (bytes) */
                    110:        int       st_bsize;     /* vs. size of swap block (DEV_BSIZE units) */
                    111: #ifdef DEBUG
                    112:        u_long    st_inuse;     /* number in this range in use */
                    113:        u_long    st_usecnt;    /* total used of this size */
                    114: #endif
                    115: } swtab[NSWSIZES+1];
                    116: 
                    117: #ifdef DEBUG
                    118: int            swap_pager_pendingio;   /* max pending async "clean" ops */
                    119: int            swap_pager_poip;        /* pageouts in progress */
                    120: int            swap_pager_piip;        /* pageins in progress */
                    121: #endif
                    122: 
                    123: queue_head_t   swap_pager_inuse;       /* list of pending page cleans */
                    124: queue_head_t   swap_pager_free;        /* list of free pager clean structs */
                    125: queue_head_t   swap_pager_list;        /* list of "named" anon regions */
                    126: 
                    127: void
                    128: swap_pager_init()
                    129: {
                    130:        register swp_clean_t spc;
                    131:        register int i, bsize;
                    132:        extern int dmmin, dmmax;
                    133:        int maxbsize;
                    134: 
                    135: #ifdef DEBUG
                    136:        if (swpagerdebug & (SDB_FOLLOW|SDB_INIT))
                    137:                printf("swpg_init()\n");
                    138: #endif
                    139:        dfltpagerops = &swappagerops;
                    140:        queue_init(&swap_pager_list);
                    141: 
                    142:        /*
                    143:         * Initialize clean lists
                    144:         */
                    145:        queue_init(&swap_pager_inuse);
                    146:        queue_init(&swap_pager_free);
                    147:        for (i = 0, spc = swcleanlist; i < NPENDINGIO; i++, spc++) {
                    148:                queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list);
                    149:                spc->spc_flags = SPC_FREE;
                    150:        }
                    151: 
                    152:        /*
                    153:         * Calculate the swap allocation constants.
                    154:         */
                    155:         if (dmmin == 0) {
                    156:                 dmmin = DMMIN;
                    157:                if (dmmin < CLBYTES/DEV_BSIZE)
                    158:                        dmmin = CLBYTES/DEV_BSIZE;
                    159:        }
                    160:         if (dmmax == 0)
                    161:                 dmmax = DMMAX;
                    162: 
                    163:        /*
                    164:         * Fill in our table of object size vs. allocation size
                    165:         */
                    166:        bsize = btodb(PAGE_SIZE);
                    167:        if (bsize < dmmin)
                    168:                bsize = dmmin;
                    169:        maxbsize = btodb(sizeof(sw_bm_t) * NBBY * PAGE_SIZE);
                    170:        if (maxbsize > dmmax)
                    171:                maxbsize = dmmax;
                    172:        for (i = 0; i < NSWSIZES; i++) {
                    173:                swtab[i].st_osize = (vm_size_t) (MAXDADDRS * dbtob(bsize));
                    174:                swtab[i].st_bsize = bsize;
                    175: #ifdef DEBUG
                    176:                if (swpagerdebug & SDB_INIT)
                    177:                        printf("swpg_init: ix %d, size %x, bsize %x\n",
                    178:                               i, swtab[i].st_osize, swtab[i].st_bsize);
                    179: #endif
                    180:                if (bsize >= maxbsize)
                    181:                        break;
                    182:                bsize *= 2;
                    183:        }
                    184:        swtab[i].st_osize = 0;
                    185:        swtab[i].st_bsize = bsize;
                    186: }
                    187: 
                    188: /*
                    189:  * Allocate a pager structure and associated resources.
                    190:  * Note that if we are called from the pageout daemon (handle == NULL)
                    191:  * we should not wait for memory as it could resulting in deadlock.
                    192:  */
                    193: vm_pager_t
                    194: swap_pager_alloc(handle, size, prot)
                    195:        caddr_t handle;
                    196:        register vm_size_t size;
                    197:        vm_prot_t prot;
                    198: {
                    199:        register vm_pager_t pager;
                    200:        register sw_pager_t swp;
                    201:        struct swtab *swt;
                    202:        int waitok;
                    203: 
                    204: #ifdef DEBUG
                    205:        if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC))
                    206:                printf("swpg_alloc(%x, %x, %x)\n", handle, size, prot);
                    207: #endif
                    208:        /*
                    209:         * If this is a "named" anonymous region, look it up and
                    210:         * return the appropriate pager if it exists.
                    211:         */
                    212:        if (handle) {
                    213:                pager = vm_pager_lookup(&swap_pager_list, handle);
                    214:                if (pager != NULL) {
                    215:                        /*
                    216:                         * Use vm_object_lookup to gain a reference
                    217:                         * to the object and also to remove from the
                    218:                         * object cache.
                    219:                         */
                    220:                        if (vm_object_lookup(pager) == NULL)
                    221:                                panic("swap_pager_alloc: bad object");
                    222:                        return(pager);
                    223:                }
                    224:        }
                    225:        /*
                    226:         * Pager doesn't exist, allocate swap management resources
                    227:         * and initialize.
                    228:         */
                    229:        waitok = handle ? M_WAITOK : M_NOWAIT;
                    230:        pager = (vm_pager_t)malloc(sizeof *pager, M_VMPAGER, waitok);
                    231:        if (pager == NULL)
                    232:                return(NULL);
                    233:        swp = (sw_pager_t)malloc(sizeof *swp, M_VMPGDATA, waitok);
                    234:        if (swp == NULL) {
                    235: #ifdef DEBUG
                    236:                if (swpagerdebug & SDB_FAIL)
                    237:                        printf("swpg_alloc: swpager malloc failed\n");
                    238: #endif
                    239:                free((caddr_t)pager, M_VMPAGER);
                    240:                return(NULL);
                    241:        }
                    242:        size = round_page(size);
                    243:        for (swt = swtab; swt->st_osize; swt++)
                    244:                if (size <= swt->st_osize)
                    245:                        break;
                    246: #ifdef DEBUG
                    247:        swt->st_inuse++;
                    248:        swt->st_usecnt++;
                    249: #endif
                    250:        swp->sw_osize = size;
                    251:        swp->sw_bsize = swt->st_bsize;
                    252:        swp->sw_nblocks = (btodb(size) + swp->sw_bsize - 1) / swp->sw_bsize;
                    253:        swp->sw_blocks = (sw_blk_t)
                    254:                malloc(swp->sw_nblocks*sizeof(*swp->sw_blocks),
                    255:                       M_VMPGDATA, M_NOWAIT);
                    256:        if (swp->sw_blocks == NULL) {
                    257:                free((caddr_t)swp, M_VMPGDATA);
                    258:                free((caddr_t)pager, M_VMPAGER);
                    259: #ifdef DEBUG
                    260:                if (swpagerdebug & SDB_FAIL)
                    261:                        printf("swpg_alloc: sw_blocks malloc failed\n");
                    262:                swt->st_inuse--;
                    263:                swt->st_usecnt--;
                    264: #endif
                    265:                return(FALSE);
                    266:        }
                    267:        bzero((caddr_t)swp->sw_blocks,
                    268:              swp->sw_nblocks * sizeof(*swp->sw_blocks));
                    269:        swp->sw_poip = 0;
                    270:        if (handle) {
                    271:                vm_object_t object;
                    272: 
                    273:                swp->sw_flags = SW_NAMED;
                    274:                queue_enter(&swap_pager_list, pager, vm_pager_t, pg_list);
                    275:                /*
                    276:                 * Consistant with other pagers: return with object
                    277:                 * referenced.  Can't do this with handle == NULL
                    278:                 * since it might be the pageout daemon calling.
                    279:                 */
                    280:                object = vm_object_allocate(size);
                    281:                vm_object_enter(object, pager);
                    282:                vm_object_setpager(object, pager, 0, FALSE);
                    283:        } else {
                    284:                swp->sw_flags = 0;
                    285:                queue_init(&pager->pg_list);
                    286:        }
                    287:        pager->pg_handle = handle;
                    288:        pager->pg_ops = &swappagerops;
                    289:        pager->pg_type = PG_SWAP;
                    290:        pager->pg_data = (caddr_t)swp;
                    291: 
                    292: #ifdef DEBUG
                    293:        if (swpagerdebug & SDB_ALLOC)
                    294:                printf("swpg_alloc: pg_data %x, %x of %x at %x\n",
                    295:                       swp, swp->sw_nblocks, swp->sw_bsize, swp->sw_blocks);
                    296: #endif
                    297:        return(pager);
                    298: }
                    299: 
                    300: void
                    301: swap_pager_dealloc(pager)
                    302:        vm_pager_t pager;
                    303: {
                    304:        register int i;
                    305:        register sw_blk_t bp;
                    306:        register sw_pager_t swp;
                    307:        struct swtab *swt;
                    308:        int s;
                    309: 
                    310: #ifdef DEBUG
                    311:        /* save panic time state */
                    312:        if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
                    313:                return;
                    314:        if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC))
                    315:                printf("swpg_dealloc(%x)\n", pager);
                    316: #endif
                    317:        /*
                    318:         * Remove from list right away so lookups will fail if we
                    319:         * block for pageout completion.
                    320:         */
                    321:        swp = (sw_pager_t) pager->pg_data;
                    322:        if (swp->sw_flags & SW_NAMED) {
                    323:                queue_remove(&swap_pager_list, pager, vm_pager_t, pg_list);
                    324:                swp->sw_flags &= ~SW_NAMED;
                    325:        }
                    326: #ifdef DEBUG
                    327:        for (swt = swtab; swt->st_osize; swt++)
                    328:                if (swp->sw_osize <= swt->st_osize)
                    329:                        break;
                    330:        swt->st_inuse--;
                    331: #endif
                    332: 
                    333:        /*
                    334:         * Wait for all pageouts to finish and remove
                    335:         * all entries from cleaning list.
                    336:         */
                    337:        s = splbio();
                    338:        while (swp->sw_poip) {
                    339:                swp->sw_flags |= SW_WANTED;
                    340:                assert_wait((int)swp);
                    341:                thread_block();
                    342:        }
                    343:        splx(s);
                    344:        (void) swap_pager_clean(NULL, B_WRITE);
                    345: 
                    346:        /*
                    347:         * Free left over swap blocks
                    348:         */
1.1.1.2   root      349:        s = splbio();
1.1       root      350:        for (i = 0, bp = swp->sw_blocks; i < swp->sw_nblocks; i++, bp++)
                    351:                if (bp->swb_block) {
                    352: #ifdef DEBUG
                    353:                        if (swpagerdebug & (SDB_ALLOCBLK|SDB_FULL))
                    354:                                printf("swpg_dealloc: blk %x\n",
                    355:                                       bp->swb_block);
                    356: #endif
1.1.1.5 ! root      357:                        rmfree(swapmap, swp->sw_bsize, bp->swb_block);
1.1       root      358:                }
1.1.1.2   root      359:        splx(s);
1.1       root      360:        /*
                    361:         * Free swap management resources
                    362:         */
                    363:        free((caddr_t)swp->sw_blocks, M_VMPGDATA);
                    364:        free((caddr_t)swp, M_VMPGDATA);
                    365:        free((caddr_t)pager, M_VMPAGER);
                    366: }
                    367: 
                    368: swap_pager_getpage(pager, m, sync)
                    369:        vm_pager_t pager;
                    370:        vm_page_t m;
                    371:        boolean_t sync;
                    372: {
                    373: #ifdef DEBUG
                    374:        if (swpagerdebug & SDB_FOLLOW)
                    375:                printf("swpg_getpage(%x, %x, %d)\n", pager, m, sync);
                    376: #endif
                    377:        return(swap_pager_io((sw_pager_t)pager->pg_data, m, B_READ));
                    378: }
                    379: 
                    380: swap_pager_putpage(pager, m, sync)
                    381:        vm_pager_t pager;
                    382:        vm_page_t m;
                    383:        boolean_t sync;
                    384: {
                    385:        int flags;
                    386: 
                    387: #ifdef DEBUG
                    388:        if (swpagerdebug & SDB_FOLLOW)
                    389:                printf("swpg_putpage(%x, %x, %d)\n", pager, m, sync);
                    390: #endif
                    391:        if (pager == NULL) {
                    392:                (void) swap_pager_clean(NULL, B_WRITE);
                    393:                return;
                    394:        }
                    395:        flags = B_WRITE;
                    396:        if (!sync)
                    397:                flags |= B_ASYNC;
                    398:        return(swap_pager_io((sw_pager_t)pager->pg_data, m, flags));
                    399: }
                    400: 
                    401: boolean_t
                    402: swap_pager_haspage(pager, offset)
                    403:        vm_pager_t pager;
                    404:        vm_offset_t offset;
                    405: {
                    406:        register sw_pager_t swp;
                    407:        register sw_blk_t swb;
                    408:        int ix;
                    409: 
                    410: #ifdef DEBUG
                    411:        if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK))
                    412:                printf("swpg_haspage(%x, %x) ", pager, offset);
                    413: #endif
                    414:        swp = (sw_pager_t) pager->pg_data;
                    415:        ix = offset / dbtob(swp->sw_bsize);
                    416:        if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) {
                    417: #ifdef DEBUG
                    418:                if (swpagerdebug & (SDB_FAIL|SDB_FOLLOW|SDB_ALLOCBLK))
                    419:                        printf("swpg_haspage: %x bad offset %x, ix %x\n",
                    420:                               swp->sw_blocks, offset, ix);
                    421: #endif
                    422:                return(FALSE);
                    423:        }
                    424:        swb = &swp->sw_blocks[ix];
                    425:        if (swb->swb_block)
                    426:                ix = atop(offset % dbtob(swp->sw_bsize));
                    427: #ifdef DEBUG
                    428:        if (swpagerdebug & SDB_ALLOCBLK)
                    429:                printf("%x blk %x+%x ", swp->sw_blocks, swb->swb_block, ix);
                    430:        if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK))
                    431:                printf("-> %c\n",
                    432:                       "FT"[swb->swb_block && (swb->swb_mask & (1 << ix))]);
                    433: #endif
                    434:        if (swb->swb_block && (swb->swb_mask & (1 << ix)))
                    435:                return(TRUE);
                    436:        return(FALSE);
                    437: }
                    438: 
                    439: /*
                    440:  * Scaled down version of swap().
                    441:  * Assumes that PAGE_SIZE < MAXPHYS; i.e. only one operation needed.
                    442:  * BOGUS:  lower level IO routines expect a KVA so we have to map our
                    443:  * provided physical page into the KVA to keep them happy.
                    444:  */
                    445: swap_pager_io(swp, m, flags)
                    446:        register sw_pager_t swp;
                    447:        vm_page_t m;
                    448:        int flags;
                    449: {
                    450:        register struct buf *bp;
                    451:        register sw_blk_t swb;
                    452:        register int s;
                    453:        int ix;
                    454:        boolean_t rv;
                    455:        vm_offset_t kva, off;
                    456:        swp_clean_t spc;
                    457: 
                    458: #ifdef DEBUG
                    459:        /* save panic time state */
                    460:        if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
                    461:                return;
                    462:        if (swpagerdebug & (SDB_FOLLOW|SDB_IO))
                    463:                printf("swpg_io(%x, %x, %x)\n", swp, m, flags);
                    464: #endif
                    465: 
                    466:        /*
                    467:         * For reads (pageins) and synchronous writes, we clean up
                    468:         * all completed async pageouts.
                    469:         */
                    470:        if ((flags & B_ASYNC) == 0) {
                    471:                s = splbio();
                    472: #ifdef DEBUG
                    473:                /*
                    474:                 * Check to see if this page is currently being cleaned.
                    475:                 * If it is, we just wait til the operation is done before
                    476:                 * continuing.
                    477:                 */
                    478:                while (swap_pager_clean(m, flags&B_READ)) {
                    479:                        if (swpagerdebug & SDB_ANOM)
                    480:                                printf("swap_pager_io: page %x cleaning\n", m);
                    481: 
                    482:                        swp->sw_flags |= SW_WANTED;
                    483:                        assert_wait((int)swp);
                    484:                        thread_block();
                    485:                }
                    486: #else
                    487:                (void) swap_pager_clean(m, flags&B_READ);
                    488: #endif
                    489:                splx(s);
                    490:        }
                    491:        /*
                    492:         * For async writes (pageouts), we cleanup completed pageouts so
                    493:         * that all available resources are freed.  Also tells us if this
                    494:         * page is already being cleaned.  If it is, or no resources
                    495:         * are available, we try again later.
                    496:         */
                    497:        else if (swap_pager_clean(m, B_WRITE) ||
                    498:                 queue_empty(&swap_pager_free)) {
                    499: #ifdef DEBUG
                    500:                if ((swpagerdebug & SDB_ANOM) &&
                    501:                    !queue_empty(&swap_pager_free))
                    502:                        printf("swap_pager_io: page %x already cleaning\n", m);
                    503: #endif
                    504:                return(VM_PAGER_FAIL);
                    505:        }
                    506: 
                    507:        /*
                    508:         * Determine swap block and allocate as necessary.
                    509:         */
                    510:        off = m->offset + m->object->paging_offset;
                    511:        ix = off / dbtob(swp->sw_bsize);
                    512:        if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) {
                    513: #ifdef DEBUG
                    514:                if (swpagerdebug & SDB_FAIL)
                    515:                        printf("swpg_io: bad offset %x+%x(%d) in %x\n",
                    516:                               m->offset, m->object->paging_offset,
                    517:                               ix, swp->sw_blocks);
                    518: #endif
                    519:                return(VM_PAGER_FAIL);
                    520:        }
1.1.1.2   root      521:        s = splbio();
1.1       root      522:        swb = &swp->sw_blocks[ix];
                    523:        off = off % dbtob(swp->sw_bsize);
                    524:        if (flags & B_READ) {
                    525:                if (swb->swb_block == 0 ||
                    526:                    (swb->swb_mask & (1 << atop(off))) == 0) {
                    527: #ifdef DEBUG
                    528:                        if (swpagerdebug & (SDB_ALLOCBLK|SDB_FAIL))
                    529:                                printf("swpg_io: %x bad read: blk %x+%x, mask %x, off %x+%x\n",
                    530:                                       swp->sw_blocks,
                    531:                                       swb->swb_block, atop(off),
                    532:                                       swb->swb_mask,
                    533:                                       m->offset, m->object->paging_offset);
                    534: #endif
                    535:                        /* XXX: should we zero page here?? */
1.1.1.2   root      536:        splx(s);
1.1       root      537:                        return(VM_PAGER_FAIL);
                    538:                }
                    539:        } else if (swb->swb_block == 0) {
                    540:                swb->swb_block = rmalloc(swapmap, swp->sw_bsize);
                    541:                if (swb->swb_block == 0) {
                    542: #ifdef DEBUG
                    543:                        if (swpagerdebug & SDB_FAIL)
                    544:                                printf("swpg_io: rmalloc of %x failed\n",
                    545:                                       swp->sw_bsize);
                    546: #endif
1.1.1.2   root      547:        splx(s);
1.1       root      548:                        return(VM_PAGER_FAIL);
                    549:                }
                    550: #ifdef DEBUG
                    551:                if (swpagerdebug & (SDB_FULL|SDB_ALLOCBLK))
                    552:                        printf("swpg_io: %x alloc blk %x at ix %x\n",
                    553:                               swp->sw_blocks, swb->swb_block, ix);
                    554: #endif
                    555:        }
1.1.1.4   root      556:        splx(s);
1.1       root      557: 
                    558:        /*
                    559:         * Allocate a kernel virtual address and initialize so that PTE
                    560:         * is available for lower level IO drivers.
                    561:         */
                    562:        kva = vm_pager_map_page(m);
                    563: 
                    564:        /*
                    565:         * Get a swap buffer header and perform the IO
                    566:         */
                    567:        s = splbio();
                    568:        while (bswlist.av_forw == NULL) {
                    569: #ifdef DEBUG
                    570:                if (swpagerdebug & SDB_ANOM)
                    571:                        printf("swap_pager_io: wait on swbuf for %x (%d)\n",
                    572:                               m, flags);
                    573: #endif
                    574:                bswlist.b_flags |= B_WANTED;
1.1.1.5 ! root      575:                tsleep((caddr_t)&bswlist, PSWP+1, "swpgio", 0);
1.1       root      576:        }
                    577:        bp = bswlist.av_forw;
                    578:        bswlist.av_forw = bp->av_forw;
                    579:        splx(s);
                    580:        bp->b_flags = B_BUSY | (flags & B_READ);
                    581:        bp->b_proc = &proc0;    /* XXX (but without B_PHYS set this is ok) */
                    582:        bp->b_un.b_addr = (caddr_t)kva;
1.1.1.5 ! root      583:        if (!swb->swb_block)
        !           584:                panic("swap_pager_io: page to first block\n");
1.1       root      585:        bp->b_blkno = swb->swb_block + btodb(off);
                    586:        VHOLD(swapdev_vp);
                    587:        bp->b_vp = swapdev_vp;
                    588:        if (swapdev_vp->v_type == VBLK)
                    589:                bp->b_dev = swapdev_vp->v_rdev;
                    590:        bp->b_bcount = PAGE_SIZE;
                    591:        if ((bp->b_flags & B_READ) == 0)
                    592:                swapdev_vp->v_numoutput++;
                    593: 
                    594:        /*
                    595:         * If this is an async write we set up additional buffer fields
                    596:         * and place a "cleaning" entry on the inuse queue.
                    597:         */
                    598:        if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) {
                    599: #ifdef DEBUG
                    600:                if (queue_empty(&swap_pager_free))
                    601:                        panic("swpg_io: lost spc");
                    602: #endif
                    603:                queue_remove_first(&swap_pager_free,
                    604:                                   spc, swp_clean_t, spc_list);
                    605: #ifdef DEBUG
                    606:                if (spc->spc_flags != SPC_FREE)
                    607:                        panic("swpg_io: bad free spc");
                    608: #endif
                    609:                spc->spc_flags = SPC_BUSY;
                    610:                spc->spc_bp = bp;
                    611:                spc->spc_swp = swp;
                    612:                spc->spc_kva = kva;
                    613:                spc->spc_m = m;
                    614:                bp->b_flags |= B_CALL;
                    615:                bp->b_iodone = swap_pager_iodone;
                    616:                s = splbio();
                    617:                swp->sw_poip++;
                    618:                queue_enter(&swap_pager_inuse, spc, swp_clean_t, spc_list);
                    619: 
                    620: #ifdef DEBUG
                    621:                swap_pager_poip++;
                    622:                if (swpagerdebug & SDB_WRITE)
                    623:                        printf("swpg_io: write: bp=%x swp=%x spc=%x poip=%d\n",
                    624:                               bp, swp, spc, swp->sw_poip);
                    625:                if ((swpagerdebug & SDB_ALLOCBLK) &&
                    626:                    (swb->swb_mask & (1 << atop(off))) == 0)
                    627:                        printf("swpg_io: %x write blk %x+%x\n",
                    628:                               swp->sw_blocks, swb->swb_block, atop(off));
                    629: #endif
                    630:                swb->swb_mask |= (1 << atop(off));
                    631:                splx(s);
                    632:        }
                    633: #ifdef DEBUG
                    634:        if (swpagerdebug & SDB_IO)
                    635:                printf("swpg_io: IO start: bp %x, db %x, va %x, pa %x\n",
                    636:                       bp, swb->swb_block+btodb(off), kva, VM_PAGE_TO_PHYS(m));
                    637: #endif
                    638:        VOP_STRATEGY(bp);
                    639:        if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) {
                    640: #ifdef DEBUG
                    641:                if (swpagerdebug & SDB_IO)
                    642:                        printf("swpg_io:  IO started: bp %x\n", bp);
                    643: #endif
                    644:                return(VM_PAGER_PEND);
                    645:        }
                    646:        s = splbio();
                    647: #ifdef DEBUG
                    648:        if (flags & B_READ)
                    649:                swap_pager_piip++;
                    650:        else
                    651:                swap_pager_poip++;
                    652: #endif
                    653:        while ((bp->b_flags & B_DONE) == 0) {
                    654:                assert_wait((int)bp);
                    655:                thread_block();
                    656:        }
                    657: #ifdef DEBUG
                    658:        if (flags & B_READ)
                    659:                --swap_pager_piip;
                    660:        else
                    661:                --swap_pager_poip;
                    662: #endif
                    663:        rv = (bp->b_flags & B_ERROR) ? VM_PAGER_FAIL : VM_PAGER_OK;
1.1.1.3   root      664:        bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_DIRTY);
1.1       root      665:        bp->av_forw = bswlist.av_forw;
                    666:        bswlist.av_forw = bp;
                    667:        if (bp->b_vp)
                    668:                brelvp(bp);
                    669:        if (bswlist.b_flags & B_WANTED) {
                    670:                bswlist.b_flags &= ~B_WANTED;
                    671:                thread_wakeup((int)&bswlist);
                    672:        }
                    673:        if ((flags & B_READ) == 0 && rv == VM_PAGER_OK) {
                    674:                m->clean = TRUE;
                    675:                pmap_clear_modify(VM_PAGE_TO_PHYS(m));
                    676:        }
                    677:        splx(s);
                    678: #ifdef DEBUG
                    679:        if (swpagerdebug & SDB_IO)
                    680:                printf("swpg_io:  IO done: bp %x, rv %d\n", bp, rv);
                    681:        if ((swpagerdebug & SDB_FAIL) && rv == VM_PAGER_FAIL)
                    682:                printf("swpg_io: IO error\n");
                    683: #endif
                    684:        vm_pager_unmap_page(kva);
                    685:        return(rv);
                    686: }
                    687: 
                    688: boolean_t
                    689: swap_pager_clean(m, rw)
                    690:        vm_page_t m;
                    691:        int rw;
                    692: {
                    693:        register swp_clean_t spc, tspc;
                    694:        register int s;
                    695: 
                    696: #ifdef DEBUG
                    697:        /* save panic time state */
                    698:        if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
                    699:                return;
                    700:        if (swpagerdebug & SDB_FOLLOW)
                    701:                printf("swpg_clean(%x, %d)\n", m, rw);
                    702: #endif
                    703:        tspc = NULL;
                    704:        for (;;) {
                    705:                /*
                    706:                 * Look up and removal from inuse list must be done
                    707:                 * at splbio() to avoid conflicts with swap_pager_iodone.
                    708:                 */
                    709:                s = splbio();
                    710:                spc = (swp_clean_t) queue_first(&swap_pager_inuse);
                    711:                while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) {
                    712:                        if ((spc->spc_flags & SPC_DONE) &&
                    713:                            swap_pager_finish(spc)) {
                    714:                                queue_remove(&swap_pager_inuse, spc,
                    715:                                             swp_clean_t, spc_list);
                    716:                                break;
                    717:                        }
                    718:                        if (m && m == spc->spc_m) {
                    719: #ifdef DEBUG
                    720:                                if (swpagerdebug & SDB_ANOM)
                    721:                                        printf("swap_pager_clean: page %x on list, flags %x\n",
                    722:                                               m, spc->spc_flags);
                    723: #endif
                    724:                                tspc = spc;
                    725:                        }
                    726:                        spc = (swp_clean_t) queue_next(&spc->spc_list);
                    727:                }
                    728: 
                    729:                /*
                    730:                 * No operations done, thats all we can do for now.
                    731:                 */
                    732:                if (queue_end(&swap_pager_inuse, (queue_entry_t)spc))
                    733:                        break;
                    734:                splx(s);
                    735: 
                    736:                /*
                    737:                 * The desired page was found to be busy earlier in
                    738:                 * the scan but has since completed.
                    739:                 */
                    740:                if (tspc && tspc == spc) {
                    741: #ifdef DEBUG
                    742:                        if (swpagerdebug & SDB_ANOM)
                    743:                                printf("swap_pager_clean: page %x done while looking\n",
                    744:                                       m);
                    745: #endif
                    746:                        tspc = NULL;
                    747:                }
                    748:                spc->spc_flags = SPC_FREE;
                    749:                vm_pager_unmap_page(spc->spc_kva);
                    750:                queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list);
                    751: #ifdef DEBUG
                    752:                if (swpagerdebug & SDB_WRITE)
                    753:                        printf("swpg_clean: free spc %x\n", spc);
                    754: #endif
                    755:        }
                    756: #ifdef DEBUG
                    757:        /*
                    758:         * If we found that the desired page is already being cleaned
                    759:         * mark it so that swap_pager_iodone() will not set the clean
                    760:         * flag before the pageout daemon has another chance to clean it.
                    761:         */
                    762:        if (tspc && rw == B_WRITE) {
                    763:                if (swpagerdebug & SDB_ANOM)
                    764:                        printf("swap_pager_clean: page %x on clean list\n",
                    765:                               tspc);
                    766:                tspc->spc_flags |= SPC_DIRTY;
                    767:        }
                    768: #endif
                    769:        splx(s);
                    770: 
                    771: #ifdef DEBUG
                    772:        if (swpagerdebug & SDB_WRITE)
                    773:                printf("swpg_clean: return %d\n", tspc ? TRUE : FALSE);
                    774:        if ((swpagerdebug & SDB_ANOM) && tspc)
                    775:                printf("swpg_clean: %s of cleaning page %x\n",
                    776:                       rw == B_READ ? "get" : "put", m);
                    777: #endif
                    778:        return(tspc ? TRUE : FALSE);
                    779: }
                    780: 
                    781: swap_pager_finish(spc)
                    782:        register swp_clean_t spc;
                    783: {
                    784:        vm_object_t object = spc->spc_m->object;
                    785: 
                    786:        /*
                    787:         * Mark the paging operation as done.
                    788:         * (XXX) If we cannot get the lock, leave it til later.
                    789:         * (XXX) Also we are assuming that an async write is a
                    790:         *       pageout operation that has incremented the counter.
                    791:         */
                    792:        if (!vm_object_lock_try(object))
                    793:                return(0);
                    794: 
                    795:        if (--object->paging_in_progress == 0)
                    796:                thread_wakeup((int) object);
                    797: 
                    798: #ifdef DEBUG
                    799:        /*
                    800:         * XXX: this isn't even close to the right thing to do,
                    801:         * introduces a variety of race conditions.
                    802:         *
                    803:         * If dirty, vm_pageout() has attempted to clean the page
                    804:         * again.  In this case we do not do anything as we will
                    805:         * see the page again shortly.
                    806:         */
                    807:        if (spc->spc_flags & SPC_DIRTY) {
                    808:                if (swpagerdebug & SDB_ANOM)
                    809:                        printf("swap_pager_finish: page %x dirty again\n",
                    810:                               spc->spc_m);
                    811:                spc->spc_m->busy = FALSE;
                    812:                PAGE_WAKEUP(spc->spc_m);
                    813:                vm_object_unlock(object);
                    814:                return(1);
                    815:        }
                    816: #endif
                    817:        /*
                    818:         * If no error mark as clean and inform the pmap system.
                    819:         * If error, mark as dirty so we will try again.
                    820:         * (XXX could get stuck doing this, should give up after awhile)
                    821:         */
                    822:        if (spc->spc_flags & SPC_ERROR) {
                    823:                printf("swap_pager_finish: clean of page %x failed\n",
                    824:                       VM_PAGE_TO_PHYS(spc->spc_m));
                    825:                spc->spc_m->laundry = TRUE;
                    826:        } else {
                    827:                spc->spc_m->clean = TRUE;
                    828:                pmap_clear_modify(VM_PAGE_TO_PHYS(spc->spc_m));
                    829:        }
                    830:        spc->spc_m->busy = FALSE;
                    831:        PAGE_WAKEUP(spc->spc_m);
                    832: 
                    833:        vm_object_unlock(object);
                    834:        return(1);
                    835: }
                    836: 
                    837: swap_pager_iodone(bp)
                    838:        register struct buf *bp;
                    839: {
                    840:        register swp_clean_t spc;
                    841:        daddr_t blk;
                    842:        int s;
                    843: 
                    844: #ifdef DEBUG
                    845:        /* save panic time state */
                    846:        if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
                    847:                return;
                    848:        if (swpagerdebug & SDB_FOLLOW)
                    849:                printf("swpg_iodone(%x)\n", bp);
                    850: #endif
                    851:        s = splbio();
                    852:        spc = (swp_clean_t) queue_first(&swap_pager_inuse);
                    853:        while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) {
                    854:                if (spc->spc_bp == bp)
                    855:                        break;
                    856:                spc = (swp_clean_t) queue_next(&spc->spc_list);
                    857:        }
                    858: #ifdef DEBUG
                    859:        if (queue_end(&swap_pager_inuse, (queue_entry_t)spc))
                    860:                panic("swap_pager_iodone: bp not found");
                    861: #endif
                    862: 
                    863:        spc->spc_flags &= ~SPC_BUSY;
                    864:        spc->spc_flags |= SPC_DONE;
1.1.1.2   root      865:        if (bp->b_flags & B_ERROR) {
1.1       root      866:                spc->spc_flags |= SPC_ERROR;
1.1.1.2   root      867: printf("error %d blkno %d sz %d ", bp->b_error, bp->b_blkno, bp->b_bcount);
                    868:        }
1.1       root      869:        spc->spc_bp = NULL;
                    870:        blk = bp->b_blkno;
                    871: 
                    872: #ifdef DEBUG
                    873:        --swap_pager_poip;
                    874:        if (swpagerdebug & SDB_WRITE)
                    875:                printf("swpg_iodone: bp=%x swp=%x flags=%x spc=%x poip=%x\n",
                    876:                       bp, spc->spc_swp, spc->spc_swp->sw_flags,
                    877:                       spc, spc->spc_swp->sw_poip);
                    878: #endif
                    879: 
                    880:        spc->spc_swp->sw_poip--;
                    881:        if (spc->spc_swp->sw_flags & SW_WANTED) {
                    882:                spc->spc_swp->sw_flags &= ~SW_WANTED;
                    883:                thread_wakeup((int)spc->spc_swp);
                    884:        }
                    885:                
1.1.1.3   root      886:        bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_DIRTY);
1.1       root      887:        bp->av_forw = bswlist.av_forw;
                    888:        bswlist.av_forw = bp;
                    889:        if (bp->b_vp)
                    890:                brelvp(bp);
                    891:        if (bswlist.b_flags & B_WANTED) {
                    892:                bswlist.b_flags &= ~B_WANTED;
                    893:                thread_wakeup((int)&bswlist);
                    894:        }
                    895:        thread_wakeup((int) &vm_pages_needed);
                    896:        splx(s);
                    897: }
                    898: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.