vfs_bio.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 871 行 · 第 1/2 页
C
871 行
*/ /* If it's locked, don't report an error; try again later. */ if (ISSET(bp->b_flags, (B_LOCKED|B_ERROR)) == (B_LOCKED|B_ERROR)) CLR(bp->b_flags, B_ERROR); /* If it's not cacheable, or an error, mark it invalid. */ if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR))) SET(bp->b_flags, B_INVAL); if ((bp->b_bufsize <= 0) || ISSET(bp->b_flags, B_INVAL)) { /* * If it's invalid or empty, dissociate it from its vnode * and put on the head of the appropriate queue. */ if (bp->b_vp) brelvp(bp); CLR(bp->b_flags, B_DELWRI); if (bp->b_bufsize <= 0) /* no data */ bufq = &bufqueues[BQ_EMPTY]; else /* invalid data */ bufq = &bufqueues[BQ_AGE]; binsheadfree(bp, bufq); } else { /* * It has valid data. Put it on the end of the appropriate * queue, so that it'll stick around for as long as possible. */ if (ISSET(bp->b_flags, B_LOCKED)) /* locked in core */ bufq = &bufqueues[BQ_LOCKED]; else if (ISSET(bp->b_flags, B_AGE)) /* stale but valid data */ bufq = &bufqueues[BQ_AGE]; else /* valid data */ bufq = &bufqueues[BQ_LRU]; binstailfree(bp, bufq); } /* Unlock the buffer. */ CLR(bp->b_flags, (B_AGE | B_ASYNC | B_BUSY | B_NOCACHE)); /* Allow disk interrupts. */ splx(s);}/* * Determine if a block is in the cache. * Just look on what would be its hash chain. If it's there, return * a pointer to it, unless it's marked invalid. If it's marked invalid, * we normally don't return the buffer, unless the caller explicitly * wants us to. */struct buf *incore(vp, blkno) struct vnode *vp; daddr_t blkno;{ struct buf *bp; bp = BUFHASH(vp, blkno)->lh_first; /* Search hash chain */ for (; bp != NULL; bp = bp->b_hash.le_next) { if (bp->b_lblkno == blkno && bp->b_vp == vp && !ISSET(bp->b_flags, B_INVAL)) return (bp); } return (0);}/* * Get a block of requested size that is associated with * a given vnode and block offset. If it is found in the * block cache, mark it as having been found, make it busy * and return it. Otherwise, return an empty block of the * correct size. It is up to the caller to insure that the * cached blocks be of the correct size. */struct buf *getblk(vp, blkno, size, slpflag, slptimeo) register struct vnode *vp; daddr_t blkno; int size, slpflag, slptimeo;{ struct bufhashhdr *bh; struct buf *bp; int s, err; /* * XXX * The following is an inlined version of 'incore()', but with * the 'invalid' test moved to after the 'busy' test. It's * necessary because there are some cases in which the NFS * code sets B_INVAL prior to writing data to the server, but * in which the buffers actually contain valid data. In this * case, we can't allow the system to allocate a new buffer for * the block until the write is finished. */ bh = BUFHASH(vp, blkno);start: bp = bh->lh_first; for (; bp != NULL; bp = bp->b_hash.le_next) { if (bp->b_lblkno != blkno || bp->b_vp != vp) continue; s = splbio(); if (ISSET(bp->b_flags, B_BUSY)) { SET(bp->b_flags, B_WANTED); err = tsleep(bp, slpflag | (PRIBIO + 1), "getblk", slptimeo); splx(s); if (err) return (NULL); goto start; } if (!ISSET(bp->b_flags, B_INVAL)) { SET(bp->b_flags, (B_BUSY | B_CACHE)); bremfree(bp); splx(s); break; } splx(s); } if (bp == NULL) { if ((bp = getnewbuf(slpflag, slptimeo)) == NULL) goto start; binshash(bp, bh); bp->b_blkno = bp->b_lblkno = blkno; s = splbio(); bgetvp(vp, bp); splx(s); } allocbuf(bp, size); return (bp);}/* * Get an empty, disassociated buffer of given size. */struct buf *geteblk(size) int size;{ struct buf *bp; while ((bp = getnewbuf(0, 0)) == 0) ; SET(bp->b_flags, B_INVAL); binshash(bp, &invalhash); allocbuf(bp, size); return (bp);}/* * Expand or contract the actual memory allocated to a buffer. * * If the buffer shrinks, data is lost, so it's up to the * caller to have written it out *first*; this routine will not * start a write. If the buffer grows, it's the callers * responsibility to fill out the buffer's additional contents. */voidallocbuf(bp, size) struct buf *bp; int size;{ struct buf *nbp; vm_size_t desired_size; int s; desired_size = roundup(size, CLBYTES); if (desired_size > MAXBSIZE) panic("allocbuf: buffer larger than MAXBSIZE requested"); if (bp->b_bufsize == desired_size) goto out;#ifdef OSKIT if (bp->b_bufsize > desired_size) goto out; panic("allocbuf: not enough space");#else /* * If the buffer is smaller than the desired size, we need to snarf * it from other buffers. Get buffers (via getnewbuf()), and * steal their pages. */ while (bp->b_bufsize < desired_size) { int amt; /* find a buffer */ while ((nbp = getnewbuf(0, 0)) == NULL) ; SET(nbp->b_flags, B_INVAL); binshash(nbp, &invalhash); /* and steal its pages, up to the amount we need */ amt = min(nbp->b_bufsize, (desired_size - bp->b_bufsize)); pagemove((nbp->b_data + nbp->b_bufsize - amt), bp->b_data + bp->b_bufsize, amt); bp->b_bufsize += amt; nbp->b_bufsize -= amt; /* reduce transfer count if we stole some data */ if (nbp->b_bcount > nbp->b_bufsize) nbp->b_bcount = nbp->b_bufsize;#ifdef DIAGNOSTIC if (nbp->b_bufsize < 0) panic("allocbuf: negative bufsize");#endif brelse(nbp); } /* * If we want a buffer smaller than the current size, * shrink this buffer. Grab a buf head from the EMPTY queue, * move a page onto it, and put it on front of the AGE queue. * If there are no free buffer headers, leave the buffer alone. */ if (bp->b_bufsize > desired_size) { s = splbio(); if ((nbp = bufqueues[BQ_EMPTY].tqh_first) == NULL) { /* No free buffer head */ splx(s); goto out; } bremfree(nbp); SET(nbp->b_flags, B_BUSY); splx(s); /* move the page to it and note this change */ pagemove(bp->b_data + desired_size, nbp->b_data, bp->b_bufsize - desired_size); nbp->b_bufsize = bp->b_bufsize - desired_size; bp->b_bufsize = desired_size; nbp->b_bcount = 0; SET(nbp->b_flags, B_INVAL); /* release the newly-filled buffer and leave */ brelse(nbp); }#endif OSKITout: bp->b_bcount = size;}/* * Find a buffer which is available for use. * Select something from a free list. * Preference is to AGE list, then LRU list. */struct buf *getnewbuf(slpflag, slptimeo) int slpflag, slptimeo;{ register struct buf *bp; int s;start: s = splbio(); if ((bp = bufqueues[BQ_AGE].tqh_first) != NULL || (bp = bufqueues[BQ_LRU].tqh_first) != NULL) { bremfree(bp); } else { /* wait for a free buffer of any kind */ needbuffer = 1; tsleep(&needbuffer, slpflag|(PRIBIO+1), "getnewbuf", slptimeo); splx(s); return (0); } /* Buffer is no longer on free lists. */ SET(bp->b_flags, B_BUSY); /* If buffer was a delayed write, start it, and go back to the top. */ if (ISSET(bp->b_flags, B_DELWRI)) { splx(s); bawrite (bp); goto start; } /* disassociate us from our vnode, if we had one... */ if (bp->b_vp) brelvp(bp); splx(s); /* clear out various other fields */ bp->b_flags = B_BUSY; bp->b_dev = NODEV; bp->b_blkno = bp->b_lblkno = 0; bp->b_iodone = 0; bp->b_error = 0; bp->b_resid = 0; bp->b_bcount = 0; bp->b_dirtyoff = bp->b_dirtyend = 0; bp->b_validoff = bp->b_validend = 0; /* nuke any credentials we were holding */ if (bp->b_rcred != NOCRED) { crfree(bp->b_rcred); bp->b_rcred = NOCRED; } if (bp->b_wcred != NOCRED) { crfree(bp->b_wcred); bp->b_wcred = NOCRED; } bremhash(bp); return (bp); }/* * Wait for operations on the buffer to complete. * When they do, extract and return the I/O's error value. */intbiowait(bp) struct buf *bp;{ int s; s = splbio(); while (!ISSET(bp->b_flags, B_DONE)) tsleep(bp, PRIBIO + 1, "biowait", 0); splx(s); /* check for interruption of I/O (e.g. via NFS), then errors. */ if (ISSET(bp->b_flags, B_EINTR)) { CLR(bp->b_flags, B_EINTR); return (EINTR); } else if (ISSET(bp->b_flags, B_ERROR)) return (bp->b_error ? bp->b_error : EIO); else return (0);}/* * Mark I/O complete on a buffer. * * If a callback has been requested, e.g. the pageout * daemon, do so. Otherwise, awaken waiting processes. * * [ Leffler, et al., says on p.247: * "This routine wakes up the blocked process, frees the buffer * for an asynchronous write, or, for a request by the pagedaemon * process, invokes a procedure specified in the buffer structure" ] * * In real life, the pagedaemon (or other system processes) wants * to do async stuff to, and doesn't want the buffer brelse()'d. * (for swap pager, that puts swap buffers on the free lists (!!!), * for the vn device, that puts malloc'd buffers on the free lists!) */voidbiodone(bp) struct buf *bp;{ if (ISSET(bp->b_flags, B_DONE)) panic("biodone already"); SET(bp->b_flags, B_DONE); /* note that it's done */ if (!ISSET(bp->b_flags, B_READ)) /* wake up reader */ vwakeup(bp); if (ISSET(bp->b_flags, B_CALL)) { /* if necessary, call out */ CLR(bp->b_flags, B_CALL); /* but note callout done */ (*bp->b_iodone)(bp); } else if (ISSET(bp->b_flags, B_ASYNC)) /* if async, release it */ brelse(bp); else { /* or just wakeup the buffer */ CLR(bp->b_flags, B_WANTED); wakeup(bp); }}/* * Return a count of buffers on the "locked" queue. */intcount_lock_queue(){ register struct buf *bp; register int n = 0; for (bp = bufqueues[BQ_LOCKED].tqh_first; bp; bp = bp->b_freelist.tqe_next) n++; return (n);}#ifdef DEBUG/* * Print out statistics on the current allocation of the buffer pool. * Can be enabled to print out on every ``sync'' by setting "syncprt" * in vfs_syscalls.c using sysctl. */voidvfs_bufstats(){ int s, i, j, count; register struct buf *bp; register struct bqueues *dp; int counts[MAXBSIZE/CLBYTES+1]; static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" }; for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) { count = 0; for (j = 0; j <= MAXBSIZE/CLBYTES; j++) counts[j] = 0; s = splbio(); for (bp = dp->tqh_first; bp; bp = bp->b_freelist.tqe_next) { counts[bp->b_bufsize/CLBYTES]++; count++; } splx(s); printf("%s: total-%d", bname[i], count); for (j = 0; j <= MAXBSIZE/CLBYTES; j++) if (counts[j] != 0) printf(", %d-%d", j * CLBYTES, counts[j]); printf("\n"); }}#endif /* DEBUG */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?