vfs_bio.c revision 136927
1147072Sbrooks/* 2147072Sbrooks * Copyright (c) 1994,1997 John S. Dyson 3147072Sbrooks * All rights reserved. 4147072Sbrooks * 5147072Sbrooks * Redistribution and use in source and binary forms, with or without 6147072Sbrooks * modification, are permitted provided that the following conditions 7147072Sbrooks * are met: 8147072Sbrooks * 1. Redistributions of source code must retain the above copyright 9147072Sbrooks * notice immediately at the beginning of the file, without modification, 10147072Sbrooks * this list of conditions, and the following disclaimer. 11147072Sbrooks * 2. Absolutely no warranty of function or purpose is made by the author 12147072Sbrooks * John S. Dyson. 13147072Sbrooks */ 14147072Sbrooks 15147072Sbrooks/* 16147072Sbrooks * this file contains a new buffer I/O scheme implementing a coherent 17147072Sbrooks * VM object and buffer cache scheme. Pains have been taken to make 18147072Sbrooks * sure that the performance degradation associated with schemes such 19147072Sbrooks * as this is not realized. 20147072Sbrooks * 21147072Sbrooks * Author: John S. Dyson 22147072Sbrooks * Significant help during the development and debugging phases 23147072Sbrooks * had been provided by David Greenman, also of the FreeBSD core team. 24147072Sbrooks * 25147072Sbrooks * see man buf(9) for more info. 26147072Sbrooks */ 27147072Sbrooks 28147072Sbrooks#include <sys/cdefs.h> 29147072Sbrooks__FBSDID("$FreeBSD: head/sys/kern/vfs_bio.c 136927 2004-10-24 20:03:41Z phk $"); 30147072Sbrooks 31147072Sbrooks#include <sys/param.h> 32147072Sbrooks#include <sys/systm.h> 33147072Sbrooks#include <sys/bio.h> 34147072Sbrooks#include <sys/conf.h> 35147072Sbrooks#include <sys/buf.h> 36147072Sbrooks#include <sys/devicestat.h> 37147072Sbrooks#include <sys/eventhandler.h> 38147072Sbrooks#include <sys/lock.h> 39147072Sbrooks#include <sys/malloc.h> 40147072Sbrooks#include <sys/mount.h> 41147072Sbrooks#include <sys/mutex.h> 42147072Sbrooks#include <sys/kernel.h> 43147072Sbrooks#include <sys/kthread.h> 44147072Sbrooks#include <sys/proc.h> 45147072Sbrooks#include <sys/resourcevar.h> 46147072Sbrooks#include <sys/sysctl.h> 47147072Sbrooks#include <sys/vmmeter.h> 48147072Sbrooks#include <sys/vnode.h> 49147072Sbrooks#include <vm/vm.h> 50147072Sbrooks#include <vm/vm_param.h> 51147072Sbrooks#include <vm/vm_kern.h> 52147072Sbrooks#include <vm/vm_pageout.h> 53147072Sbrooks#include <vm/vm_page.h> 54147072Sbrooks#include <vm/vm_object.h> 55147072Sbrooks#include <vm/vm_extern.h> 56147072Sbrooks#include <vm/vm_map.h> 57147072Sbrooks#include "opt_directio.h" 58147072Sbrooks#include "opt_swap.h" 59147072Sbrooks 60147072Sbrooksstatic MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer"); 61147072Sbrooks 62147072Sbrooksstruct bio_ops bioops; /* I/O operation notification */ 63147072Sbrooks 64147072Sbrooksstruct buf_ops buf_ops_bio = { 65147072Sbrooks .bop_name = "buf_ops_bio", 66147072Sbrooks .bop_write = bufwrite, 67147072Sbrooks .bop_strategy = bufstrategy, 68147072Sbrooks}; 69147072Sbrooks 70147072Sbrooks/* 71147072Sbrooks * XXX buf is global because kern_shutdown.c and ffs_checkoverlap has 72147072Sbrooks * carnal knowledge of buffers. This knowledge should be moved to vfs_bio.c. 73147072Sbrooks */ 74147072Sbrooksstruct buf *buf; /* buffer header pool */ 75147072Sbrooks 76147072Sbrooksstatic struct proc *bufdaemonproc; 77147072Sbrooks 78147072Sbrooksstatic int inmem(struct vnode * vp, daddr_t blkno); 79147072Sbrooksstatic void vm_hold_free_pages(struct buf *bp, vm_offset_t from, 80147072Sbrooks vm_offset_t to); 81147072Sbrooksstatic void vm_hold_load_pages(struct buf *bp, vm_offset_t from, 82147072Sbrooks vm_offset_t to); 83147072Sbrooksstatic void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, 84147072Sbrooks int pageno, vm_page_t m); 85147072Sbrooksstatic void vfs_clean_pages(struct buf *bp); 86147072Sbrooksstatic void vfs_setdirty(struct buf *bp); 87147072Sbrooksstatic void vfs_vmio_release(struct buf *bp); 88147072Sbrooksstatic void vfs_backgroundwritedone(struct buf *bp); 89147072Sbrooksstatic int vfs_bio_clcheck(struct vnode *vp, int size, 90147072Sbrooks daddr_t lblkno, daddr_t blkno); 91147072Sbrooksstatic int flushbufqueues(int flushdeps); 92147072Sbrooksstatic void buf_daemon(void); 93147072Sbrooksvoid bremfreel(struct buf *bp); 94147072Sbrooks 95147072Sbrooksint vmiodirenable = TRUE; 96147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0, 97147072Sbrooks "Use the VM system for directory writes"); 98147072Sbrooksint runningbufspace; 99147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0, 100147072Sbrooks "Amount of presently outstanding async buffer io"); 101147072Sbrooksstatic int bufspace; 102147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0, 103147072Sbrooks "KVA memory used for bufs"); 104147072Sbrooksstatic int maxbufspace; 105147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0, 106147072Sbrooks "Maximum allowed value of bufspace (including buf_daemon)"); 107147072Sbrooksstatic int bufmallocspace; 108147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0, 109147072Sbrooks "Amount of malloced memory for buffers"); 110147072Sbrooksstatic int maxbufmallocspace; 111147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0, 112147072Sbrooks "Maximum amount of malloced memory for buffers"); 113147072Sbrooksstatic int lobufspace; 114147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0, 115147072Sbrooks "Minimum amount of buffers we want to have"); 116147072Sbrooksstatic int hibufspace; 117147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0, 118147072Sbrooks "Maximum allowed value of bufspace (excluding buf_daemon)"); 119147072Sbrooksstatic int bufreusecnt; 120147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0, 121147072Sbrooks "Number of times we have reused a buffer"); 122147072Sbrooksstatic int buffreekvacnt; 123147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0, 124147072Sbrooks "Number of times we have freed the KVA space from some buffer"); 125147072Sbrooksstatic int bufdefragcnt; 126147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0, 127147072Sbrooks "Number of times we have had to repeat buffer allocation to defragment"); 128147072Sbrooksstatic int lorunningspace; 129147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0, 130147072Sbrooks "Minimum preferred space used for in-progress I/O"); 131147072Sbrooksstatic int hirunningspace; 132147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0, 133147072Sbrooks "Maximum amount of space to use for in-progress I/O"); 134147072Sbrooksstatic int dirtybufferflushes; 135147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes, 136147072Sbrooks 0, "Number of bdwrite to bawrite conversions to limit dirty buffers"); 137147072Sbrooksstatic int altbufferflushes; 138147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes, 139147072Sbrooks 0, "Number of fsync flushes to limit dirty buffers"); 140147072Sbrooksstatic int recursiveflushes; 141147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes, 142147072Sbrooks 0, "Number of flushes skipped due to being recursive"); 143147072Sbrooksstatic int numdirtybuffers; 144147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0, 145147072Sbrooks "Number of buffers that are dirty (has unwritten changes) at the moment"); 146147072Sbrooksstatic int lodirtybuffers; 147147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0, 148147072Sbrooks "How many buffers we want to have free before bufdaemon can sleep"); 149147072Sbrooksstatic int hidirtybuffers; 150147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0, 151147072Sbrooks "When the number of dirty buffers is considered severe"); 152147072Sbrooksstatic int dirtybufthresh; 153147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh, 154147072Sbrooks 0, "Number of bdwrite to bawrite conversions to clear dirty buffers"); 155147072Sbrooksstatic int numfreebuffers; 156147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0, 157147072Sbrooks "Number of free buffers"); 158147072Sbrooksstatic int lofreebuffers; 159147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0, 160147072Sbrooks "XXX Unused"); 161147072Sbrooksstatic int hifreebuffers; 162147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0, 163147072Sbrooks "XXX Complicatedly unused"); 164147072Sbrooksstatic int getnewbufcalls; 165147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0, 166147072Sbrooks "Number of calls to getnewbuf"); 167147072Sbrooksstatic int getnewbufrestarts; 168147072SbrooksSYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0, 169147072Sbrooks "Number of times getnewbuf has had to restart a buffer aquisition"); 170147072Sbrooksstatic int dobkgrdwrite = 1; 171147072SbrooksSYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0, 172147072Sbrooks "Do background writes (honoring the BV_BKGRDWRITE flag)?"); 173147072Sbrooks 174147072Sbrooks/* 175147072Sbrooks * Wakeup point for bufdaemon, as well as indicator of whether it is already 176147072Sbrooks * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it 177147072Sbrooks * is idling. 178147072Sbrooks */ 179147072Sbrooksstatic int bd_request; 180147072Sbrooks 181147072Sbrooks/* 182147072Sbrooks * This lock synchronizes access to bd_request. 183147072Sbrooks */ 184147072Sbrooksstatic struct mtx bdlock; 185147072Sbrooks 186147072Sbrooks/* 187147072Sbrooks * bogus page -- for I/O to/from partially complete buffers 188147072Sbrooks * this is a temporary solution to the problem, but it is not 189147072Sbrooks * really that bad. it would be better to split the buffer 190147072Sbrooks * for input in the case of buffers partially already in memory, 191147072Sbrooks * but the code is intricate enough already. 192147072Sbrooks */ 193147072Sbrooksvm_page_t bogus_page; 194147072Sbrooks 195147072Sbrooks/* 196147072Sbrooks * Synchronization (sleep/wakeup) variable for active buffer space requests. 197147072Sbrooks * Set when wait starts, cleared prior to wakeup(). 198147072Sbrooks * Used in runningbufwakeup() and waitrunningbufspace(). 199147072Sbrooks */ 200147072Sbrooksstatic int runningbufreq; 201147072Sbrooks 202147072Sbrooks/* 203147072Sbrooks * This lock protects the runningbufreq and synchronizes runningbufwakeup and 204147072Sbrooks * waitrunningbufspace(). 205147072Sbrooks */ 206147072Sbrooksstatic struct mtx rbreqlock; 207147072Sbrooks 208147072Sbrooks/* 209147072Sbrooks * Synchronization (sleep/wakeup) variable for buffer requests. 210147072Sbrooks * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done 211147072Sbrooks * by and/or. 212147072Sbrooks * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(), 213147072Sbrooks * getnewbuf(), and getblk(). 214147072Sbrooks */ 215147072Sbrooksstatic int needsbuffer; 216147072Sbrooks 217147072Sbrooks/* 218147072Sbrooks * Lock that protects needsbuffer and the sleeps/wakeups surrounding it. 219147072Sbrooks */ 220147072Sbrooksstatic struct mtx nblock; 221147072Sbrooks 222147072Sbrooks/* 223147072Sbrooks * Lock that protects against bwait()/bdone()/B_DONE races. 224147072Sbrooks */ 225147072Sbrooks 226147072Sbrooksstatic struct mtx bdonelock; 227147072Sbrooks 228147072Sbrooks/* 229147072Sbrooks * Definitions for the buffer free lists. 230147072Sbrooks */ 231147072Sbrooks#define BUFFER_QUEUES 5 /* number of free buffer queues */ 232147072Sbrooks 233147072Sbrooks#define QUEUE_NONE 0 /* on no queue */ 234147072Sbrooks#define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */ 235147072Sbrooks#define QUEUE_DIRTY 2 /* B_DELWRI buffers */ 236147072Sbrooks#define QUEUE_EMPTYKVA 3 /* empty buffer headers w/KVA assignment */ 237147072Sbrooks#define QUEUE_EMPTY 4 /* empty buffer headers */ 238147072Sbrooks 239147072Sbrooks/* Queues for free buffers with various properties */ 240147072Sbrooksstatic TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } }; 241147072Sbrooks 242147072Sbrooks/* Lock for the bufqueues */ 243147072Sbrooksstatic struct mtx bqlock; 244147072Sbrooks 245147072Sbrooks/* 246147072Sbrooks * Single global constant for BUF_WMESG, to avoid getting multiple references. 247147072Sbrooks * buf_wmesg is referred from macros. 248147072Sbrooks */ 249147072Sbrooksconst char *buf_wmesg = BUF_WMESG; 250147072Sbrooks 251147072Sbrooks#define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */ 252147072Sbrooks#define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */ 253147072Sbrooks#define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */ 254147072Sbrooks#define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */ 255147072Sbrooks 256147072Sbrooks#ifdef DIRECTIO 257147072Sbrooksextern void ffs_rawread_setup(void); 258147072Sbrooks#endif /* DIRECTIO */ 259147072Sbrooks/* 260147072Sbrooks * numdirtywakeup: 261147072Sbrooks * 262147072Sbrooks * If someone is blocked due to there being too many dirty buffers, 263147072Sbrooks * and numdirtybuffers is now reasonable, wake them up. 264147072Sbrooks */ 265147072Sbrooks 266147072Sbrooksstatic __inline void 267147072Sbrooksnumdirtywakeup(int level) 268147072Sbrooks{ 269147072Sbrooks 270147072Sbrooks if (numdirtybuffers <= level) { 271147072Sbrooks mtx_lock(&nblock); 272147072Sbrooks if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) { 273147072Sbrooks needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH; 274147072Sbrooks wakeup(&needsbuffer); 275147072Sbrooks } 276147072Sbrooks mtx_unlock(&nblock); 277147072Sbrooks } 278147072Sbrooks} 279147072Sbrooks 280147072Sbrooks/* 281147072Sbrooks * bufspacewakeup: 282147072Sbrooks * 283147072Sbrooks * Called when buffer space is potentially available for recovery. 284147072Sbrooks * getnewbuf() will block on this flag when it is unable to free 285147072Sbrooks * sufficient buffer space. Buffer space becomes recoverable when 286147072Sbrooks * bp's get placed back in the queues. 287147072Sbrooks */ 288147072Sbrooks 289147072Sbrooksstatic __inline void 290147072Sbrooksbufspacewakeup(void) 291147072Sbrooks{ 292147072Sbrooks 293147072Sbrooks /* 294147072Sbrooks * If someone is waiting for BUF space, wake them up. Even 295147072Sbrooks * though we haven't freed the kva space yet, the waiting 296147072Sbrooks * process will be able to now. 297147072Sbrooks */ 298147072Sbrooks mtx_lock(&nblock); 299147072Sbrooks if (needsbuffer & VFS_BIO_NEED_BUFSPACE) { 300147072Sbrooks needsbuffer &= ~VFS_BIO_NEED_BUFSPACE; 301147072Sbrooks wakeup(&needsbuffer); 302147072Sbrooks } 303147072Sbrooks mtx_unlock(&nblock); 304147072Sbrooks} 305147072Sbrooks 306147072Sbrooks/* 307147072Sbrooks * runningbufwakeup() - in-progress I/O accounting. 308147072Sbrooks * 309147072Sbrooks */ 310147072Sbrooksstatic __inline void 311147072Sbrooksrunningbufwakeup(struct buf *bp) 312147072Sbrooks{ 313147072Sbrooks 314147072Sbrooks if (bp->b_runningbufspace) { 315147072Sbrooks atomic_subtract_int(&runningbufspace, bp->b_runningbufspace); 316147072Sbrooks bp->b_runningbufspace = 0; 317147072Sbrooks mtx_lock(&rbreqlock); 318147072Sbrooks if (runningbufreq && runningbufspace <= lorunningspace) { 319147072Sbrooks runningbufreq = 0; 320147072Sbrooks wakeup(&runningbufreq); 321147072Sbrooks } 322147072Sbrooks mtx_unlock(&rbreqlock); 323147072Sbrooks } 324147072Sbrooks} 325147072Sbrooks 326147072Sbrooks/* 327147072Sbrooks * bufcountwakeup: 328147072Sbrooks * 329147072Sbrooks * Called when a buffer has been added to one of the free queues to 330147072Sbrooks * account for the buffer and to wakeup anyone waiting for free buffers. 331147072Sbrooks * This typically occurs when large amounts of metadata are being handled 332147072Sbrooks * by the buffer cache ( else buffer space runs out first, usually ). 333147072Sbrooks */ 334147072Sbrooks 335147072Sbrooksstatic __inline void 336147072Sbrooksbufcountwakeup(void) 337147072Sbrooks{ 338147072Sbrooks 339147072Sbrooks atomic_add_int(&numfreebuffers, 1); 340147072Sbrooks mtx_lock(&nblock); 341147072Sbrooks if (needsbuffer) { 342147072Sbrooks needsbuffer &= ~VFS_BIO_NEED_ANY; 343147072Sbrooks if (numfreebuffers >= hifreebuffers) 344147072Sbrooks needsbuffer &= ~VFS_BIO_NEED_FREE; 345147072Sbrooks wakeup(&needsbuffer); 346147072Sbrooks } 347147072Sbrooks mtx_unlock(&nblock); 348147072Sbrooks} 349147072Sbrooks 350147072Sbrooks/* 351147072Sbrooks * waitrunningbufspace() 352147072Sbrooks * 353147072Sbrooks * runningbufspace is a measure of the amount of I/O currently 354147072Sbrooks * running. This routine is used in async-write situations to 355147072Sbrooks * prevent creating huge backups of pending writes to a device. 356147072Sbrooks * Only asynchronous writes are governed by this function. 357147072Sbrooks * 358147072Sbrooks * Reads will adjust runningbufspace, but will not block based on it. 359147072Sbrooks * The read load has a side effect of reducing the allowed write load. 360147072Sbrooks * 361147072Sbrooks * This does NOT turn an async write into a sync write. It waits 362147072Sbrooks * for earlier writes to complete and generally returns before the 363147072Sbrooks * caller's write has reached the device. 364147072Sbrooks */ 365147072Sbrooksstatic __inline void 366147072Sbrookswaitrunningbufspace(void) 367147072Sbrooks{ 368147072Sbrooks 369147072Sbrooks mtx_lock(&rbreqlock); 370147072Sbrooks while (runningbufspace > hirunningspace) { 371147072Sbrooks ++runningbufreq; 372147072Sbrooks msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0); 373147072Sbrooks } 374147072Sbrooks mtx_unlock(&rbreqlock); 375147072Sbrooks} 376147072Sbrooks 377147072Sbrooks 378147072Sbrooks/* 379147072Sbrooks * vfs_buf_test_cache: 380147072Sbrooks * 381147072Sbrooks * Called when a buffer is extended. This function clears the B_CACHE 382147072Sbrooks * bit if the newly extended portion of the buffer does not contain 383147072Sbrooks * valid data. 384147072Sbrooks */ 385147072Sbrooksstatic __inline 386147072Sbrooksvoid 387147072Sbrooksvfs_buf_test_cache(struct buf *bp, 388147072Sbrooks vm_ooffset_t foff, vm_offset_t off, vm_offset_t size, 389147072Sbrooks vm_page_t m) 390147072Sbrooks{ 391147072Sbrooks 392147072Sbrooks GIANT_REQUIRED; 393147072Sbrooks 394147072Sbrooks VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 395147072Sbrooks if (bp->b_flags & B_CACHE) { 396147072Sbrooks int base = (foff + off) & PAGE_MASK; 397147072Sbrooks if (vm_page_is_valid(m, base, size) == 0) 398147072Sbrooks bp->b_flags &= ~B_CACHE; 399147072Sbrooks } 400147072Sbrooks} 401147072Sbrooks 402147072Sbrooks/* Wake up the buffer deamon if necessary */ 403147072Sbrooksstatic __inline 404147072Sbrooksvoid 405147072Sbrooksbd_wakeup(int dirtybuflevel) 406147072Sbrooks{ 407147072Sbrooks 408147072Sbrooks mtx_lock(&bdlock); 409147072Sbrooks if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) { 410147072Sbrooks bd_request = 1; 411147072Sbrooks wakeup(&bd_request); 412147072Sbrooks } 413147072Sbrooks mtx_unlock(&bdlock); 414147072Sbrooks} 415147072Sbrooks 416147072Sbrooks/* 417147072Sbrooks * bd_speedup - speedup the buffer cache flushing code 418147072Sbrooks */ 419147072Sbrooks 420147072Sbrooksstatic __inline 421147072Sbrooksvoid 422147072Sbrooksbd_speedup(void) 423147072Sbrooks{ 424147072Sbrooks 425147072Sbrooks bd_wakeup(1); 426147072Sbrooks} 427147072Sbrooks 428147072Sbrooks/* 429147072Sbrooks * Calculating buffer cache scaling values and reserve space for buffer 430147072Sbrooks * headers. This is called during low level kernel initialization and 431 * may be called more then once. We CANNOT write to the memory area 432 * being reserved at this time. 433 */ 434caddr_t 435kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est) 436{ 437 438 /* 439 * physmem_est is in pages. Convert it to kilobytes (assumes 440 * PAGE_SIZE is >= 1K) 441 */ 442 physmem_est = physmem_est * (PAGE_SIZE / 1024); 443 444 /* 445 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE. 446 * For the first 64MB of ram nominally allocate sufficient buffers to 447 * cover 1/4 of our ram. Beyond the first 64MB allocate additional 448 * buffers to cover 1/20 of our ram over 64MB. When auto-sizing 449 * the buffer cache we limit the eventual kva reservation to 450 * maxbcache bytes. 451 * 452 * factor represents the 1/4 x ram conversion. 453 */ 454 if (nbuf == 0) { 455 int factor = 4 * BKVASIZE / 1024; 456 457 nbuf = 50; 458 if (physmem_est > 4096) 459 nbuf += min((physmem_est - 4096) / factor, 460 65536 / factor); 461 if (physmem_est > 65536) 462 nbuf += (physmem_est - 65536) * 2 / (factor * 5); 463 464 if (maxbcache && nbuf > maxbcache / BKVASIZE) 465 nbuf = maxbcache / BKVASIZE; 466 } 467 468#if 0 469 /* 470 * Do not allow the buffer_map to be more then 1/2 the size of the 471 * kernel_map. 472 */ 473 if (nbuf > (kernel_map->max_offset - kernel_map->min_offset) / 474 (BKVASIZE * 2)) { 475 nbuf = (kernel_map->max_offset - kernel_map->min_offset) / 476 (BKVASIZE * 2); 477 printf("Warning: nbufs capped at %d\n", nbuf); 478 } 479#endif 480 481 /* 482 * swbufs are used as temporary holders for I/O, such as paging I/O. 483 * We have no less then 16 and no more then 256. 484 */ 485 nswbuf = max(min(nbuf/4, 256), 16); 486#ifdef NSWBUF_MIN 487 if (nswbuf < NSWBUF_MIN) 488 nswbuf = NSWBUF_MIN; 489#endif 490#ifdef DIRECTIO 491 ffs_rawread_setup(); 492#endif 493 494 /* 495 * Reserve space for the buffer cache buffers 496 */ 497 swbuf = (void *)v; 498 v = (caddr_t)(swbuf + nswbuf); 499 buf = (void *)v; 500 v = (caddr_t)(buf + nbuf); 501 502 return(v); 503} 504 505/* Initialize the buffer subsystem. Called before use of any buffers. */ 506void 507bufinit(void) 508{ 509 struct buf *bp; 510 int i; 511 512 GIANT_REQUIRED; 513 514 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF); 515 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF); 516 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF); 517 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF); 518 mtx_init(&bdonelock, "bdone lock", NULL, MTX_DEF); 519 520 /* next, make a null set of free lists */ 521 for (i = 0; i < BUFFER_QUEUES; i++) 522 TAILQ_INIT(&bufqueues[i]); 523 524 /* finally, initialize each buffer header and stick on empty q */ 525 for (i = 0; i < nbuf; i++) { 526 bp = &buf[i]; 527 bzero(bp, sizeof *bp); 528 bp->b_flags = B_INVAL; /* we're just an empty header */ 529 bp->b_dev = NULL; 530 bp->b_rcred = NOCRED; 531 bp->b_wcred = NOCRED; 532 bp->b_qindex = QUEUE_EMPTY; 533 bp->b_vflags = 0; 534 bp->b_xflags = 0; 535 LIST_INIT(&bp->b_dep); 536 BUF_LOCKINIT(bp); 537 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist); 538 } 539 540 /* 541 * maxbufspace is the absolute maximum amount of buffer space we are 542 * allowed to reserve in KVM and in real terms. The absolute maximum 543 * is nominally used by buf_daemon. hibufspace is the nominal maximum 544 * used by most other processes. The differential is required to 545 * ensure that buf_daemon is able to run when other processes might 546 * be blocked waiting for buffer space. 547 * 548 * maxbufspace is based on BKVASIZE. Allocating buffers larger then 549 * this may result in KVM fragmentation which is not handled optimally 550 * by the system. 551 */ 552 maxbufspace = nbuf * BKVASIZE; 553 hibufspace = imax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10); 554 lobufspace = hibufspace - MAXBSIZE; 555 556 lorunningspace = 512 * 1024; 557 hirunningspace = 1024 * 1024; 558 559/* 560 * Limit the amount of malloc memory since it is wired permanently into 561 * the kernel space. Even though this is accounted for in the buffer 562 * allocation, we don't want the malloced region to grow uncontrolled. 563 * The malloc scheme improves memory utilization significantly on average 564 * (small) directories. 565 */ 566 maxbufmallocspace = hibufspace / 20; 567 568/* 569 * Reduce the chance of a deadlock occuring by limiting the number 570 * of delayed-write dirty buffers we allow to stack up. 571 */ 572 hidirtybuffers = nbuf / 4 + 20; 573 dirtybufthresh = hidirtybuffers * 9 / 10; 574 numdirtybuffers = 0; 575/* 576 * To support extreme low-memory systems, make sure hidirtybuffers cannot 577 * eat up all available buffer space. This occurs when our minimum cannot 578 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming 579 * BKVASIZE'd (8K) buffers. 580 */ 581 while (hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) { 582 hidirtybuffers >>= 1; 583 } 584 lodirtybuffers = hidirtybuffers / 2; 585 586/* 587 * Try to keep the number of free buffers in the specified range, 588 * and give special processes (e.g. like buf_daemon) access to an 589 * emergency reserve. 590 */ 591 lofreebuffers = nbuf / 18 + 5; 592 hifreebuffers = 2 * lofreebuffers; 593 numfreebuffers = nbuf; 594 595/* 596 * Maximum number of async ops initiated per buf_daemon loop. This is 597 * somewhat of a hack at the moment, we really need to limit ourselves 598 * based on the number of bytes of I/O in-transit that were initiated 599 * from buf_daemon. 600 */ 601 602 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | 603 VM_ALLOC_NORMAL | VM_ALLOC_WIRED); 604} 605 606/* 607 * bfreekva() - free the kva allocation for a buffer. 608 * 609 * Must be called at splbio() or higher as this is the only locking for 610 * buffer_map. 611 * 612 * Since this call frees up buffer space, we call bufspacewakeup(). 613 */ 614static void 615bfreekva(struct buf *bp) 616{ 617 618 GIANT_REQUIRED; 619 620 if (bp->b_kvasize) { 621 atomic_add_int(&buffreekvacnt, 1); 622 atomic_subtract_int(&bufspace, bp->b_kvasize); 623 vm_map_delete(buffer_map, 624 (vm_offset_t) bp->b_kvabase, 625 (vm_offset_t) bp->b_kvabase + bp->b_kvasize 626 ); 627 bp->b_kvasize = 0; 628 bufspacewakeup(); 629 } 630} 631 632/* 633 * bremfree: 634 * 635 * Remove the buffer from the appropriate free list. 636 */ 637void 638bremfree(struct buf *bp) 639{ 640 641 mtx_lock(&bqlock); 642 bremfreel(bp); 643 mtx_unlock(&bqlock); 644} 645 646void 647bremfreel(struct buf *bp) 648{ 649 int s = splbio(); 650 int old_qindex = bp->b_qindex; 651 652 GIANT_REQUIRED; 653 654 if (bp->b_qindex != QUEUE_NONE) { 655 KASSERT(BUF_REFCNT(bp) == 1, ("bremfree: bp %p not locked",bp)); 656 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist); 657 bp->b_qindex = QUEUE_NONE; 658 } else { 659 if (BUF_REFCNT(bp) <= 1) 660 panic("bremfree: removing a buffer not on a queue"); 661 } 662 663 /* 664 * Fixup numfreebuffers count. If the buffer is invalid or not 665 * delayed-write, and it was on the EMPTY, LRU, or AGE queues, 666 * the buffer was free and we must decrement numfreebuffers. 667 */ 668 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) { 669 switch(old_qindex) { 670 case QUEUE_DIRTY: 671 case QUEUE_CLEAN: 672 case QUEUE_EMPTY: 673 case QUEUE_EMPTYKVA: 674 atomic_subtract_int(&numfreebuffers, 1); 675 break; 676 default: 677 break; 678 } 679 } 680 splx(s); 681} 682 683 684/* 685 * Get a buffer with the specified data. Look in the cache first. We 686 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE 687 * is set, the buffer is valid and we do not have to do anything ( see 688 * getblk() ). This is really just a special case of breadn(). 689 */ 690int 691bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred, 692 struct buf **bpp) 693{ 694 695 return (breadn(vp, blkno, size, 0, 0, 0, cred, bpp)); 696} 697 698/* 699 * Operates like bread, but also starts asynchronous I/O on 700 * read-ahead blocks. We must clear BIO_ERROR and B_INVAL prior 701 * to initiating I/O . If B_CACHE is set, the buffer is valid 702 * and we do not have to do anything. 703 */ 704int 705breadn(struct vnode * vp, daddr_t blkno, int size, 706 daddr_t * rablkno, int *rabsize, 707 int cnt, struct ucred * cred, struct buf **bpp) 708{ 709 struct buf *bp, *rabp; 710 int i; 711 int rv = 0, readwait = 0; 712 713 *bpp = bp = getblk(vp, blkno, size, 0, 0, 0); 714 715 /* if not found in cache, do some I/O */ 716 if ((bp->b_flags & B_CACHE) == 0) { 717 if (curthread != PCPU_GET(idlethread)) 718 curthread->td_proc->p_stats->p_ru.ru_inblock++; 719 bp->b_iocmd = BIO_READ; 720 bp->b_flags &= ~B_INVAL; 721 bp->b_ioflags &= ~BIO_ERROR; 722 if (bp->b_rcred == NOCRED && cred != NOCRED) 723 bp->b_rcred = crhold(cred); 724 vfs_busy_pages(bp, 0); 725 bp->b_iooffset = dbtob(bp->b_blkno); 726 bstrategy(bp); 727 ++readwait; 728 } 729 730 for (i = 0; i < cnt; i++, rablkno++, rabsize++) { 731 if (inmem(vp, *rablkno)) 732 continue; 733 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0); 734 735 if ((rabp->b_flags & B_CACHE) == 0) { 736 if (curthread != PCPU_GET(idlethread)) 737 curthread->td_proc->p_stats->p_ru.ru_inblock++; 738 rabp->b_flags |= B_ASYNC; 739 rabp->b_flags &= ~B_INVAL; 740 rabp->b_ioflags &= ~BIO_ERROR; 741 rabp->b_iocmd = BIO_READ; 742 if (rabp->b_rcred == NOCRED && cred != NOCRED) 743 rabp->b_rcred = crhold(cred); 744 vfs_busy_pages(rabp, 0); 745 BUF_KERNPROC(rabp); 746 rabp->b_iooffset = dbtob(rabp->b_blkno); 747 bstrategy(rabp); 748 } else { 749 brelse(rabp); 750 } 751 } 752 753 if (readwait) { 754 rv = bufwait(bp); 755 } 756 return (rv); 757} 758 759/* 760 * Write, release buffer on completion. (Done by iodone 761 * if async). Do not bother writing anything if the buffer 762 * is invalid. 763 * 764 * Note that we set B_CACHE here, indicating that buffer is 765 * fully valid and thus cacheable. This is true even of NFS 766 * now so we set it generally. This could be set either here 767 * or in biodone() since the I/O is synchronous. We put it 768 * here. 769 */ 770int 771bufwrite(struct buf *bp) 772{ 773 int oldflags, s; 774 struct buf *newbp; 775 776 if (bp->b_flags & B_INVAL) { 777 brelse(bp); 778 return (0); 779 } 780 781 oldflags = bp->b_flags; 782 783 if (BUF_REFCNT(bp) == 0) 784 panic("bufwrite: buffer is not busy???"); 785 s = splbio(); 786 /* 787 * If a background write is already in progress, delay 788 * writing this block if it is asynchronous. Otherwise 789 * wait for the background write to complete. 790 */ 791 BO_LOCK(bp->b_bufobj); 792 if (bp->b_vflags & BV_BKGRDINPROG) { 793 if (bp->b_flags & B_ASYNC) { 794 BO_UNLOCK(bp->b_bufobj); 795 splx(s); 796 bdwrite(bp); 797 return (0); 798 } 799 bp->b_vflags |= BV_BKGRDWAIT; 800 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0); 801 if (bp->b_vflags & BV_BKGRDINPROG) 802 panic("bufwrite: still writing"); 803 } 804 BO_UNLOCK(bp->b_bufobj); 805 806 /* Mark the buffer clean */ 807 bundirty(bp); 808 809 /* 810 * If this buffer is marked for background writing and we 811 * do not have to wait for it, make a copy and write the 812 * copy so as to leave this buffer ready for further use. 813 * 814 * This optimization eats a lot of memory. If we have a page 815 * or buffer shortfall we can't do it. 816 */ 817 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) && 818 (bp->b_flags & B_ASYNC) && 819 !vm_page_count_severe() && 820 !buf_dirty_count_severe()) { 821 KASSERT(bp->b_iodone == NULL, 822 ("bufwrite: needs chained iodone (%p)", bp->b_iodone)); 823 824 /* get a new block */ 825 newbp = geteblk(bp->b_bufsize); 826 827 /* 828 * set it to be identical to the old block. We have to 829 * set b_lblkno and BKGRDMARKER before calling bgetvp() 830 * to avoid confusing the splay tree and gbincore(). 831 */ 832 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize); 833 newbp->b_lblkno = bp->b_lblkno; 834 newbp->b_xflags |= BX_BKGRDMARKER; 835 BO_LOCK(bp->b_bufobj); 836 bp->b_vflags |= BV_BKGRDINPROG; 837 bgetvp(bp->b_vp, newbp); 838 BO_UNLOCK(bp->b_bufobj); 839 newbp->b_bufobj = &bp->b_vp->v_bufobj; 840 newbp->b_blkno = bp->b_blkno; 841 newbp->b_offset = bp->b_offset; 842 newbp->b_iodone = vfs_backgroundwritedone; 843 newbp->b_flags |= B_ASYNC; 844 newbp->b_flags &= ~B_INVAL; 845 846 /* move over the dependencies */ 847 if (LIST_FIRST(&bp->b_dep) != NULL) 848 buf_movedeps(bp, newbp); 849 850 /* 851 * Initiate write on the copy, release the original to 852 * the B_LOCKED queue so that it cannot go away until 853 * the background write completes. If not locked it could go 854 * away and then be reconstituted while it was being written. 855 * If the reconstituted buffer were written, we could end up 856 * with two background copies being written at the same time. 857 */ 858 bqrelse(bp); 859 bp = newbp; 860 } 861 862 bp->b_flags &= ~B_DONE; 863 bp->b_ioflags &= ~BIO_ERROR; 864 bp->b_flags |= B_CACHE; 865 bp->b_iocmd = BIO_WRITE; 866 867 bufobj_wref(bp->b_bufobj); 868 vfs_busy_pages(bp, 1); 869 870 /* 871 * Normal bwrites pipeline writes 872 */ 873 bp->b_runningbufspace = bp->b_bufsize; 874 atomic_add_int(&runningbufspace, bp->b_runningbufspace); 875 876 if (curthread != PCPU_GET(idlethread)) 877 curthread->td_proc->p_stats->p_ru.ru_oublock++; 878 splx(s); 879 if (oldflags & B_ASYNC) 880 BUF_KERNPROC(bp); 881 bp->b_iooffset = dbtob(bp->b_blkno); 882 bstrategy(bp); 883 884 if ((oldflags & B_ASYNC) == 0) { 885 int rtval = bufwait(bp); 886 brelse(bp); 887 return (rtval); 888 } else { 889 /* 890 * don't allow the async write to saturate the I/O 891 * system. We will not deadlock here because 892 * we are blocking waiting for I/O that is already in-progress 893 * to complete. We do not block here if it is the update 894 * or syncer daemon trying to clean up as that can lead 895 * to deadlock. 896 */ 897 if (curthread->td_proc != bufdaemonproc && 898 curthread->td_proc != updateproc) 899 waitrunningbufspace(); 900 } 901 902 return (0); 903} 904 905/* 906 * Complete a background write started from bwrite. 907 */ 908static void 909vfs_backgroundwritedone(struct buf *bp) 910{ 911 struct buf *origbp; 912 913 /* 914 * Find the original buffer that we are writing. 915 */ 916 BO_LOCK(bp->b_bufobj); 917 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL) 918 panic("backgroundwritedone: lost buffer"); 919 920 /* 921 * Clear the BV_BKGRDINPROG flag in the original buffer 922 * and awaken it if it is waiting for the write to complete. 923 * If BV_BKGRDINPROG is not set in the original buffer it must 924 * have been released and re-instantiated - which is not legal. 925 */ 926 KASSERT((origbp->b_vflags & BV_BKGRDINPROG), 927 ("backgroundwritedone: lost buffer2")); 928 origbp->b_vflags &= ~BV_BKGRDINPROG; 929 if (origbp->b_vflags & BV_BKGRDWAIT) { 930 origbp->b_vflags &= ~BV_BKGRDWAIT; 931 wakeup(&origbp->b_xflags); 932 } 933 BO_UNLOCK(bp->b_bufobj); 934 /* 935 * Process dependencies then return any unfinished ones. 936 */ 937 if (LIST_FIRST(&bp->b_dep) != NULL) 938 buf_complete(bp); 939 if (LIST_FIRST(&bp->b_dep) != NULL) 940 buf_movedeps(bp, origbp); 941 942 /* 943 * This buffer is marked B_NOCACHE, so when it is released 944 * by biodone, it will be tossed. We mark it with BIO_READ 945 * to avoid biodone doing a second bufobj_wdrop. 946 */ 947 bp->b_flags |= B_NOCACHE; 948 bp->b_iocmd = BIO_READ; 949 bp->b_flags &= ~(B_CACHE | B_DONE); 950 bp->b_iodone = 0; 951 bufdone(bp); 952} 953 954/* 955 * Delayed write. (Buffer is marked dirty). Do not bother writing 956 * anything if the buffer is marked invalid. 957 * 958 * Note that since the buffer must be completely valid, we can safely 959 * set B_CACHE. In fact, we have to set B_CACHE here rather then in 960 * biodone() in order to prevent getblk from writing the buffer 961 * out synchronously. 962 */ 963void 964bdwrite(struct buf *bp) 965{ 966 struct thread *td = curthread; 967 struct vnode *vp; 968 struct buf *nbp; 969 struct bufobj *bo; 970 971 GIANT_REQUIRED; 972 973 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 974 KASSERT(BUF_REFCNT(bp) != 0, ("bdwrite: buffer is not busy")); 975 976 if (bp->b_flags & B_INVAL) { 977 brelse(bp); 978 return; 979 } 980 981 /* 982 * If we have too many dirty buffers, don't create any more. 983 * If we are wildly over our limit, then force a complete 984 * cleanup. Otherwise, just keep the situation from getting 985 * out of control. Note that we have to avoid a recursive 986 * disaster and not try to clean up after our own cleanup! 987 */ 988 vp = bp->b_vp; 989 bo = bp->b_bufobj; 990 BO_LOCK(bo); 991 if (td->td_pflags & TDP_COWINPROGRESS) { 992 recursiveflushes++; 993 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) { 994 BO_UNLOCK(bo); 995 (void) VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td); 996 BO_LOCK(bo); 997 altbufferflushes++; 998 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) { 999 /* 1000 * Try to find a buffer to flush. 1001 */ 1002 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) { 1003 if ((nbp->b_vflags & BV_BKGRDINPROG) || 1004 buf_countdeps(nbp, 0) || 1005 BUF_LOCK(nbp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 1006 continue; 1007 if (bp == nbp) 1008 panic("bdwrite: found ourselves"); 1009 BO_UNLOCK(bo); 1010 if (nbp->b_flags & B_CLUSTEROK) { 1011 vfs_bio_awrite(nbp); 1012 } else { 1013 bremfree(nbp); 1014 bawrite(nbp); 1015 } 1016 BO_LOCK(bo); 1017 dirtybufferflushes++; 1018 break; 1019 } 1020 } 1021 BO_UNLOCK(bo); 1022 1023 bdirty(bp); 1024 /* 1025 * Set B_CACHE, indicating that the buffer is fully valid. This is 1026 * true even of NFS now. 1027 */ 1028 bp->b_flags |= B_CACHE; 1029 1030 /* 1031 * This bmap keeps the system from needing to do the bmap later, 1032 * perhaps when the system is attempting to do a sync. Since it 1033 * is likely that the indirect block -- or whatever other datastructure 1034 * that the filesystem needs is still in memory now, it is a good 1035 * thing to do this. Note also, that if the pageout daemon is 1036 * requesting a sync -- there might not be enough memory to do 1037 * the bmap then... So, this is important to do. 1038 */ 1039 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) { 1040 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL); 1041 } 1042 1043 /* 1044 * Set the *dirty* buffer range based upon the VM system dirty pages. 1045 */ 1046 vfs_setdirty(bp); 1047 1048 /* 1049 * We need to do this here to satisfy the vnode_pager and the 1050 * pageout daemon, so that it thinks that the pages have been 1051 * "cleaned". Note that since the pages are in a delayed write 1052 * buffer -- the VFS layer "will" see that the pages get written 1053 * out on the next sync, or perhaps the cluster will be completed. 1054 */ 1055 vfs_clean_pages(bp); 1056 bqrelse(bp); 1057 1058 /* 1059 * Wakeup the buffer flushing daemon if we have a lot of dirty 1060 * buffers (midpoint between our recovery point and our stall 1061 * point). 1062 */ 1063 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2); 1064 1065 /* 1066 * note: we cannot initiate I/O from a bdwrite even if we wanted to, 1067 * due to the softdep code. 1068 */ 1069} 1070 1071/* 1072 * bdirty: 1073 * 1074 * Turn buffer into delayed write request. We must clear BIO_READ and 1075 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to 1076 * itself to properly update it in the dirty/clean lists. We mark it 1077 * B_DONE to ensure that any asynchronization of the buffer properly 1078 * clears B_DONE ( else a panic will occur later ). 1079 * 1080 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which 1081 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty() 1082 * should only be called if the buffer is known-good. 1083 * 1084 * Since the buffer is not on a queue, we do not update the numfreebuffers 1085 * count. 1086 * 1087 * Must be called at splbio(). 1088 * The buffer must be on QUEUE_NONE. 1089 */ 1090void 1091bdirty(struct buf *bp) 1092{ 1093 1094 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1095 KASSERT(bp->b_qindex == QUEUE_NONE, 1096 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex)); 1097 bp->b_flags &= ~(B_RELBUF); 1098 bp->b_iocmd = BIO_WRITE; 1099 1100 if ((bp->b_flags & B_DELWRI) == 0) { 1101 bp->b_flags |= B_DONE | B_DELWRI; 1102 reassignbuf(bp); 1103 atomic_add_int(&numdirtybuffers, 1); 1104 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2); 1105 } 1106} 1107 1108/* 1109 * bundirty: 1110 * 1111 * Clear B_DELWRI for buffer. 1112 * 1113 * Since the buffer is not on a queue, we do not update the numfreebuffers 1114 * count. 1115 * 1116 * Must be called at splbio(). 1117 * The buffer must be on QUEUE_NONE. 1118 */ 1119 1120void 1121bundirty(struct buf *bp) 1122{ 1123 1124 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1125 KASSERT(bp->b_qindex == QUEUE_NONE, 1126 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex)); 1127 1128 if (bp->b_flags & B_DELWRI) { 1129 bp->b_flags &= ~B_DELWRI; 1130 reassignbuf(bp); 1131 atomic_subtract_int(&numdirtybuffers, 1); 1132 numdirtywakeup(lodirtybuffers); 1133 } 1134 /* 1135 * Since it is now being written, we can clear its deferred write flag. 1136 */ 1137 bp->b_flags &= ~B_DEFERRED; 1138} 1139 1140/* 1141 * bawrite: 1142 * 1143 * Asynchronous write. Start output on a buffer, but do not wait for 1144 * it to complete. The buffer is released when the output completes. 1145 * 1146 * bwrite() ( or the VOP routine anyway ) is responsible for handling 1147 * B_INVAL buffers. Not us. 1148 */ 1149void 1150bawrite(struct buf *bp) 1151{ 1152 1153 bp->b_flags |= B_ASYNC; 1154 (void) bwrite(bp); 1155} 1156 1157/* 1158 * bwillwrite: 1159 * 1160 * Called prior to the locking of any vnodes when we are expecting to 1161 * write. We do not want to starve the buffer cache with too many 1162 * dirty buffers so we block here. By blocking prior to the locking 1163 * of any vnodes we attempt to avoid the situation where a locked vnode 1164 * prevents the various system daemons from flushing related buffers. 1165 */ 1166 1167void 1168bwillwrite(void) 1169{ 1170 1171 if (numdirtybuffers >= hidirtybuffers) { 1172 int s; 1173 1174 mtx_lock(&Giant); 1175 s = splbio(); 1176 mtx_lock(&nblock); 1177 while (numdirtybuffers >= hidirtybuffers) { 1178 bd_wakeup(1); 1179 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH; 1180 msleep(&needsbuffer, &nblock, 1181 (PRIBIO + 4), "flswai", 0); 1182 } 1183 splx(s); 1184 mtx_unlock(&nblock); 1185 mtx_unlock(&Giant); 1186 } 1187} 1188 1189/* 1190 * Return true if we have too many dirty buffers. 1191 */ 1192int 1193buf_dirty_count_severe(void) 1194{ 1195 1196 return(numdirtybuffers >= hidirtybuffers); 1197} 1198 1199/* 1200 * brelse: 1201 * 1202 * Release a busy buffer and, if requested, free its resources. The 1203 * buffer will be stashed in the appropriate bufqueue[] allowing it 1204 * to be accessed later as a cache entity or reused for other purposes. 1205 */ 1206void 1207brelse(struct buf *bp) 1208{ 1209 int s; 1210 1211 GIANT_REQUIRED; 1212 1213 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), 1214 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp)); 1215 1216 s = splbio(); 1217 1218 if (bp->b_iocmd == BIO_WRITE && 1219 (bp->b_ioflags & BIO_ERROR) && 1220 !(bp->b_flags & B_INVAL)) { 1221 /* 1222 * Failed write, redirty. Must clear BIO_ERROR to prevent 1223 * pages from being scrapped. If B_INVAL is set then 1224 * this case is not run and the next case is run to 1225 * destroy the buffer. B_INVAL can occur if the buffer 1226 * is outside the range supported by the underlying device. 1227 */ 1228 bp->b_ioflags &= ~BIO_ERROR; 1229 bdirty(bp); 1230 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) || 1231 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) { 1232 /* 1233 * Either a failed I/O or we were asked to free or not 1234 * cache the buffer. 1235 */ 1236 bp->b_flags |= B_INVAL; 1237 if (LIST_FIRST(&bp->b_dep) != NULL) 1238 buf_deallocate(bp); 1239 if (bp->b_flags & B_DELWRI) { 1240 atomic_subtract_int(&numdirtybuffers, 1); 1241 numdirtywakeup(lodirtybuffers); 1242 } 1243 bp->b_flags &= ~(B_DELWRI | B_CACHE); 1244 if ((bp->b_flags & B_VMIO) == 0) { 1245 if (bp->b_bufsize) 1246 allocbuf(bp, 0); 1247 if (bp->b_vp) 1248 brelvp(bp); 1249 } 1250 } 1251 1252 /* 1253 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release() 1254 * is called with B_DELWRI set, the underlying pages may wind up 1255 * getting freed causing a previous write (bdwrite()) to get 'lost' 1256 * because pages associated with a B_DELWRI bp are marked clean. 1257 * 1258 * We still allow the B_INVAL case to call vfs_vmio_release(), even 1259 * if B_DELWRI is set. 1260 * 1261 * If B_DELWRI is not set we may have to set B_RELBUF if we are low 1262 * on pages to return pages to the VM page queues. 1263 */ 1264 if (bp->b_flags & B_DELWRI) 1265 bp->b_flags &= ~B_RELBUF; 1266 else if (vm_page_count_severe()) { 1267 /* 1268 * XXX This lock may not be necessary since BKGRDINPROG 1269 * cannot be set while we hold the buf lock, it can only be 1270 * cleared if it is already pending. 1271 */ 1272 if (bp->b_vp) { 1273 BO_LOCK(bp->b_bufobj); 1274 if (!(bp->b_vflags & BV_BKGRDINPROG)) 1275 bp->b_flags |= B_RELBUF; 1276 BO_UNLOCK(bp->b_bufobj); 1277 } else 1278 bp->b_flags |= B_RELBUF; 1279 } 1280 1281 /* 1282 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer 1283 * constituted, not even NFS buffers now. Two flags effect this. If 1284 * B_INVAL, the struct buf is invalidated but the VM object is kept 1285 * around ( i.e. so it is trivial to reconstitute the buffer later ). 1286 * 1287 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be 1288 * invalidated. BIO_ERROR cannot be set for a failed write unless the 1289 * buffer is also B_INVAL because it hits the re-dirtying code above. 1290 * 1291 * Normally we can do this whether a buffer is B_DELWRI or not. If 1292 * the buffer is an NFS buffer, it is tracking piecemeal writes or 1293 * the commit state and we cannot afford to lose the buffer. If the 1294 * buffer has a background write in progress, we need to keep it 1295 * around to prevent it from being reconstituted and starting a second 1296 * background write. 1297 */ 1298 if ((bp->b_flags & B_VMIO) 1299 && !(bp->b_vp->v_mount != NULL && 1300 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 1301 !vn_isdisk(bp->b_vp, NULL) && 1302 (bp->b_flags & B_DELWRI)) 1303 ) { 1304 1305 int i, j, resid; 1306 vm_page_t m; 1307 off_t foff; 1308 vm_pindex_t poff; 1309 vm_object_t obj; 1310 1311 obj = bp->b_object; 1312 1313 /* 1314 * Get the base offset and length of the buffer. Note that 1315 * in the VMIO case if the buffer block size is not 1316 * page-aligned then b_data pointer may not be page-aligned. 1317 * But our b_pages[] array *IS* page aligned. 1318 * 1319 * block sizes less then DEV_BSIZE (usually 512) are not 1320 * supported due to the page granularity bits (m->valid, 1321 * m->dirty, etc...). 1322 * 1323 * See man buf(9) for more information 1324 */ 1325 resid = bp->b_bufsize; 1326 foff = bp->b_offset; 1327 VM_OBJECT_LOCK(obj); 1328 for (i = 0; i < bp->b_npages; i++) { 1329 int had_bogus = 0; 1330 1331 m = bp->b_pages[i]; 1332 1333 /* 1334 * If we hit a bogus page, fixup *all* the bogus pages 1335 * now. 1336 */ 1337 if (m == bogus_page) { 1338 poff = OFF_TO_IDX(bp->b_offset); 1339 had_bogus = 1; 1340 1341 for (j = i; j < bp->b_npages; j++) { 1342 vm_page_t mtmp; 1343 mtmp = bp->b_pages[j]; 1344 if (mtmp == bogus_page) { 1345 mtmp = vm_page_lookup(obj, poff + j); 1346 if (!mtmp) { 1347 panic("brelse: page missing\n"); 1348 } 1349 bp->b_pages[j] = mtmp; 1350 } 1351 } 1352 1353 if ((bp->b_flags & B_INVAL) == 0) { 1354 pmap_qenter( 1355 trunc_page((vm_offset_t)bp->b_data), bp->b_pages, bp->b_npages); 1356 } 1357 m = bp->b_pages[i]; 1358 } 1359 if ((bp->b_flags & B_NOCACHE) || 1360 (bp->b_ioflags & BIO_ERROR)) { 1361 int poffset = foff & PAGE_MASK; 1362 int presid = resid > (PAGE_SIZE - poffset) ? 1363 (PAGE_SIZE - poffset) : resid; 1364 1365 KASSERT(presid >= 0, ("brelse: extra page")); 1366 vm_page_lock_queues(); 1367 vm_page_set_invalid(m, poffset, presid); 1368 vm_page_unlock_queues(); 1369 if (had_bogus) 1370 printf("avoided corruption bug in bogus_page/brelse code\n"); 1371 } 1372 resid -= PAGE_SIZE - (foff & PAGE_MASK); 1373 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 1374 } 1375 VM_OBJECT_UNLOCK(obj); 1376 if (bp->b_flags & (B_INVAL | B_RELBUF)) 1377 vfs_vmio_release(bp); 1378 1379 } else if (bp->b_flags & B_VMIO) { 1380 1381 if (bp->b_flags & (B_INVAL | B_RELBUF)) { 1382 vfs_vmio_release(bp); 1383 } 1384 1385 } 1386 1387 if (bp->b_qindex != QUEUE_NONE) 1388 panic("brelse: free buffer onto another queue???"); 1389 if (BUF_REFCNT(bp) > 1) { 1390 /* do not release to free list */ 1391 BUF_UNLOCK(bp); 1392 splx(s); 1393 return; 1394 } 1395 1396 /* enqueue */ 1397 mtx_lock(&bqlock); 1398 1399 /* buffers with no memory */ 1400 if (bp->b_bufsize == 0) { 1401 bp->b_flags |= B_INVAL; 1402 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA); 1403 if (bp->b_vflags & BV_BKGRDINPROG) 1404 panic("losing buffer 1"); 1405 if (bp->b_kvasize) { 1406 bp->b_qindex = QUEUE_EMPTYKVA; 1407 } else { 1408 bp->b_qindex = QUEUE_EMPTY; 1409 } 1410 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist); 1411 bp->b_dev = NULL; 1412 /* buffers with junk contents */ 1413 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) || 1414 (bp->b_ioflags & BIO_ERROR)) { 1415 bp->b_flags |= B_INVAL; 1416 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA); 1417 if (bp->b_vflags & BV_BKGRDINPROG) 1418 panic("losing buffer 2"); 1419 bp->b_qindex = QUEUE_CLEAN; 1420 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist); 1421 bp->b_dev = NULL; 1422 /* remaining buffers */ 1423 } else { 1424 if (bp->b_flags & B_DELWRI) 1425 bp->b_qindex = QUEUE_DIRTY; 1426 else 1427 bp->b_qindex = QUEUE_CLEAN; 1428 if (bp->b_flags & B_AGE) 1429 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist); 1430 else 1431 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist); 1432 } 1433 mtx_unlock(&bqlock); 1434 1435 /* 1436 * If B_INVAL and B_DELWRI is set, clear B_DELWRI. We have already 1437 * placed the buffer on the correct queue. We must also disassociate 1438 * the device and vnode for a B_INVAL buffer so gbincore() doesn't 1439 * find it. 1440 */ 1441 if (bp->b_flags & B_INVAL) { 1442 if (bp->b_flags & B_DELWRI) 1443 bundirty(bp); 1444 if (bp->b_vp) 1445 brelvp(bp); 1446 } 1447 1448 /* 1449 * Fixup numfreebuffers count. The bp is on an appropriate queue 1450 * unless locked. We then bump numfreebuffers if it is not B_DELWRI. 1451 * We've already handled the B_INVAL case ( B_DELWRI will be clear 1452 * if B_INVAL is set ). 1453 */ 1454 1455 if (!(bp->b_flags & B_DELWRI)) 1456 bufcountwakeup(); 1457 1458 /* 1459 * Something we can maybe free or reuse 1460 */ 1461 if (bp->b_bufsize || bp->b_kvasize) 1462 bufspacewakeup(); 1463 1464 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT); 1465 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY)) 1466 panic("brelse: not dirty"); 1467 /* unlock */ 1468 BUF_UNLOCK(bp); 1469 splx(s); 1470} 1471 1472/* 1473 * Release a buffer back to the appropriate queue but do not try to free 1474 * it. The buffer is expected to be used again soon. 1475 * 1476 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by 1477 * biodone() to requeue an async I/O on completion. It is also used when 1478 * known good buffers need to be requeued but we think we may need the data 1479 * again soon. 1480 * 1481 * XXX we should be able to leave the B_RELBUF hint set on completion. 1482 */ 1483void 1484bqrelse(struct buf *bp) 1485{ 1486 int s; 1487 1488 s = splbio(); 1489 1490 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), 1491 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp)); 1492 1493 if (bp->b_qindex != QUEUE_NONE) 1494 panic("bqrelse: free buffer onto another queue???"); 1495 if (BUF_REFCNT(bp) > 1) { 1496 /* do not release to free list */ 1497 BUF_UNLOCK(bp); 1498 splx(s); 1499 return; 1500 } 1501 mtx_lock(&bqlock); 1502 /* buffers with stale but valid contents */ 1503 if (bp->b_flags & B_DELWRI) { 1504 bp->b_qindex = QUEUE_DIRTY; 1505 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], bp, b_freelist); 1506 } else { 1507 /* 1508 * XXX This lock may not be necessary since BKGRDINPROG 1509 * cannot be set while we hold the buf lock, it can only be 1510 * cleared if it is already pending. 1511 */ 1512 BO_LOCK(bp->b_bufobj); 1513 if (!vm_page_count_severe() || bp->b_vflags & BV_BKGRDINPROG) { 1514 BO_UNLOCK(bp->b_bufobj); 1515 bp->b_qindex = QUEUE_CLEAN; 1516 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp, 1517 b_freelist); 1518 } else { 1519 /* 1520 * We are too low on memory, we have to try to free 1521 * the buffer (most importantly: the wired pages 1522 * making up its backing store) *now*. 1523 */ 1524 BO_UNLOCK(bp->b_bufobj); 1525 mtx_unlock(&bqlock); 1526 splx(s); 1527 brelse(bp); 1528 return; 1529 } 1530 } 1531 mtx_unlock(&bqlock); 1532 1533 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI)) 1534 bufcountwakeup(); 1535 1536 /* 1537 * Something we can maybe free or reuse. 1538 */ 1539 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI)) 1540 bufspacewakeup(); 1541 1542 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF); 1543 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY)) 1544 panic("bqrelse: not dirty"); 1545 /* unlock */ 1546 BUF_UNLOCK(bp); 1547 splx(s); 1548} 1549 1550/* Give pages used by the bp back to the VM system (where possible) */ 1551static void 1552vfs_vmio_release(struct buf *bp) 1553{ 1554 int i; 1555 vm_page_t m; 1556 1557 GIANT_REQUIRED; 1558 VM_OBJECT_LOCK(bp->b_object); 1559 vm_page_lock_queues(); 1560 for (i = 0; i < bp->b_npages; i++) { 1561 m = bp->b_pages[i]; 1562 bp->b_pages[i] = NULL; 1563 /* 1564 * In order to keep page LRU ordering consistent, put 1565 * everything on the inactive queue. 1566 */ 1567 vm_page_unwire(m, 0); 1568 /* 1569 * We don't mess with busy pages, it is 1570 * the responsibility of the process that 1571 * busied the pages to deal with them. 1572 */ 1573 if ((m->flags & PG_BUSY) || (m->busy != 0)) 1574 continue; 1575 1576 if (m->wire_count == 0) { 1577 /* 1578 * Might as well free the page if we can and it has 1579 * no valid data. We also free the page if the 1580 * buffer was used for direct I/O 1581 */ 1582 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid && 1583 m->hold_count == 0) { 1584 vm_page_busy(m); 1585 pmap_remove_all(m); 1586 vm_page_free(m); 1587 } else if (bp->b_flags & B_DIRECT) { 1588 vm_page_try_to_free(m); 1589 } else if (vm_page_count_severe()) { 1590 vm_page_try_to_cache(m); 1591 } 1592 } 1593 } 1594 vm_page_unlock_queues(); 1595 VM_OBJECT_UNLOCK(bp->b_object); 1596 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages); 1597 1598 if (bp->b_bufsize) { 1599 bufspacewakeup(); 1600 bp->b_bufsize = 0; 1601 } 1602 bp->b_npages = 0; 1603 bp->b_flags &= ~B_VMIO; 1604 if (bp->b_vp) 1605 brelvp(bp); 1606} 1607 1608/* 1609 * Check to see if a block at a particular lbn is available for a clustered 1610 * write. 1611 */ 1612static int 1613vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno) 1614{ 1615 struct buf *bpa; 1616 int match; 1617 1618 match = 0; 1619 1620 /* If the buf isn't in core skip it */ 1621 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL) 1622 return (0); 1623 1624 /* If the buf is busy we don't want to wait for it */ 1625 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) 1626 return (0); 1627 1628 /* Only cluster with valid clusterable delayed write buffers */ 1629 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) != 1630 (B_DELWRI | B_CLUSTEROK)) 1631 goto done; 1632 1633 if (bpa->b_bufsize != size) 1634 goto done; 1635 1636 /* 1637 * Check to see if it is in the expected place on disk and that the 1638 * block has been mapped. 1639 */ 1640 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno)) 1641 match = 1; 1642done: 1643 BUF_UNLOCK(bpa); 1644 return (match); 1645} 1646 1647/* 1648 * vfs_bio_awrite: 1649 * 1650 * Implement clustered async writes for clearing out B_DELWRI buffers. 1651 * This is much better then the old way of writing only one buffer at 1652 * a time. Note that we may not be presented with the buffers in the 1653 * correct order, so we search for the cluster in both directions. 1654 */ 1655int 1656vfs_bio_awrite(struct buf *bp) 1657{ 1658 int i; 1659 int j; 1660 daddr_t lblkno = bp->b_lblkno; 1661 struct vnode *vp = bp->b_vp; 1662 int s; 1663 int ncl; 1664 int nwritten; 1665 int size; 1666 int maxcl; 1667 1668 s = splbio(); 1669 /* 1670 * right now we support clustered writing only to regular files. If 1671 * we find a clusterable block we could be in the middle of a cluster 1672 * rather then at the beginning. 1673 */ 1674 if ((vp->v_type == VREG) && 1675 (vp->v_mount != 0) && /* Only on nodes that have the size info */ 1676 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) { 1677 1678 size = vp->v_mount->mnt_stat.f_iosize; 1679 maxcl = MAXPHYS / size; 1680 1681 VI_LOCK(vp); 1682 for (i = 1; i < maxcl; i++) 1683 if (vfs_bio_clcheck(vp, size, lblkno + i, 1684 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0) 1685 break; 1686 1687 for (j = 1; i + j <= maxcl && j <= lblkno; j++) 1688 if (vfs_bio_clcheck(vp, size, lblkno - j, 1689 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0) 1690 break; 1691 1692 VI_UNLOCK(vp); 1693 --j; 1694 ncl = i + j; 1695 /* 1696 * this is a possible cluster write 1697 */ 1698 if (ncl != 1) { 1699 BUF_UNLOCK(bp); 1700 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl); 1701 splx(s); 1702 return nwritten; 1703 } 1704 } 1705 1706 bremfree(bp); 1707 bp->b_flags |= B_ASYNC; 1708 1709 splx(s); 1710 /* 1711 * default (old) behavior, writing out only one block 1712 * 1713 * XXX returns b_bufsize instead of b_bcount for nwritten? 1714 */ 1715 nwritten = bp->b_bufsize; 1716 (void) bwrite(bp); 1717 1718 return nwritten; 1719} 1720 1721/* 1722 * getnewbuf: 1723 * 1724 * Find and initialize a new buffer header, freeing up existing buffers 1725 * in the bufqueues as necessary. The new buffer is returned locked. 1726 * 1727 * Important: B_INVAL is not set. If the caller wishes to throw the 1728 * buffer away, the caller must set B_INVAL prior to calling brelse(). 1729 * 1730 * We block if: 1731 * We have insufficient buffer headers 1732 * We have insufficient buffer space 1733 * buffer_map is too fragmented ( space reservation fails ) 1734 * If we have to flush dirty buffers ( but we try to avoid this ) 1735 * 1736 * To avoid VFS layer recursion we do not flush dirty buffers ourselves. 1737 * Instead we ask the buf daemon to do it for us. We attempt to 1738 * avoid piecemeal wakeups of the pageout daemon. 1739 */ 1740 1741static struct buf * 1742getnewbuf(int slpflag, int slptimeo, int size, int maxsize) 1743{ 1744 struct buf *bp; 1745 struct buf *nbp; 1746 int defrag = 0; 1747 int nqindex; 1748 static int flushingbufs; 1749 1750 GIANT_REQUIRED; 1751 1752 /* 1753 * We can't afford to block since we might be holding a vnode lock, 1754 * which may prevent system daemons from running. We deal with 1755 * low-memory situations by proactively returning memory and running 1756 * async I/O rather then sync I/O. 1757 */ 1758 1759 atomic_add_int(&getnewbufcalls, 1); 1760 atomic_subtract_int(&getnewbufrestarts, 1); 1761restart: 1762 atomic_add_int(&getnewbufrestarts, 1); 1763 1764 /* 1765 * Setup for scan. If we do not have enough free buffers, 1766 * we setup a degenerate case that immediately fails. Note 1767 * that if we are specially marked process, we are allowed to 1768 * dip into our reserves. 1769 * 1770 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN 1771 * 1772 * We start with EMPTYKVA. If the list is empty we backup to EMPTY. 1773 * However, there are a number of cases (defragging, reusing, ...) 1774 * where we cannot backup. 1775 */ 1776 mtx_lock(&bqlock); 1777 nqindex = QUEUE_EMPTYKVA; 1778 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]); 1779 1780 if (nbp == NULL) { 1781 /* 1782 * If no EMPTYKVA buffers and we are either 1783 * defragging or reusing, locate a CLEAN buffer 1784 * to free or reuse. If bufspace useage is low 1785 * skip this step so we can allocate a new buffer. 1786 */ 1787 if (defrag || bufspace >= lobufspace) { 1788 nqindex = QUEUE_CLEAN; 1789 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]); 1790 } 1791 1792 /* 1793 * If we could not find or were not allowed to reuse a 1794 * CLEAN buffer, check to see if it is ok to use an EMPTY 1795 * buffer. We can only use an EMPTY buffer if allocating 1796 * its KVA would not otherwise run us out of buffer space. 1797 */ 1798 if (nbp == NULL && defrag == 0 && 1799 bufspace + maxsize < hibufspace) { 1800 nqindex = QUEUE_EMPTY; 1801 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]); 1802 } 1803 } 1804 1805 /* 1806 * Run scan, possibly freeing data and/or kva mappings on the fly 1807 * depending. 1808 */ 1809 1810 while ((bp = nbp) != NULL) { 1811 int qindex = nqindex; 1812 1813 /* 1814 * Calculate next bp ( we can only use it if we do not block 1815 * or do other fancy things ). 1816 */ 1817 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) { 1818 switch(qindex) { 1819 case QUEUE_EMPTY: 1820 nqindex = QUEUE_EMPTYKVA; 1821 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]))) 1822 break; 1823 /* FALLTHROUGH */ 1824 case QUEUE_EMPTYKVA: 1825 nqindex = QUEUE_CLEAN; 1826 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]))) 1827 break; 1828 /* FALLTHROUGH */ 1829 case QUEUE_CLEAN: 1830 /* 1831 * nbp is NULL. 1832 */ 1833 break; 1834 } 1835 } 1836 if (bp->b_vp) { 1837 BO_LOCK(bp->b_bufobj); 1838 if (bp->b_vflags & BV_BKGRDINPROG) { 1839 BO_UNLOCK(bp->b_bufobj); 1840 continue; 1841 } 1842 BO_UNLOCK(bp->b_bufobj); 1843 } 1844 1845 /* 1846 * Sanity Checks 1847 */ 1848 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp)); 1849 1850 /* 1851 * Note: we no longer distinguish between VMIO and non-VMIO 1852 * buffers. 1853 */ 1854 1855 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex)); 1856 1857 /* 1858 * If we are defragging then we need a buffer with 1859 * b_kvasize != 0. XXX this situation should no longer 1860 * occur, if defrag is non-zero the buffer's b_kvasize 1861 * should also be non-zero at this point. XXX 1862 */ 1863 if (defrag && bp->b_kvasize == 0) { 1864 printf("Warning: defrag empty buffer %p\n", bp); 1865 continue; 1866 } 1867 1868 /* 1869 * Start freeing the bp. This is somewhat involved. nbp 1870 * remains valid only for QUEUE_EMPTY[KVA] bp's. 1871 */ 1872 1873 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) 1874 panic("getnewbuf: locked buf"); 1875 bremfreel(bp); 1876 mtx_unlock(&bqlock); 1877 1878 if (qindex == QUEUE_CLEAN) { 1879 if (bp->b_flags & B_VMIO) { 1880 bp->b_flags &= ~B_ASYNC; 1881 vfs_vmio_release(bp); 1882 } 1883 if (bp->b_vp) 1884 brelvp(bp); 1885 } 1886 1887 /* 1888 * NOTE: nbp is now entirely invalid. We can only restart 1889 * the scan from this point on. 1890 * 1891 * Get the rest of the buffer freed up. b_kva* is still 1892 * valid after this operation. 1893 */ 1894 1895 if (bp->b_rcred != NOCRED) { 1896 crfree(bp->b_rcred); 1897 bp->b_rcred = NOCRED; 1898 } 1899 if (bp->b_wcred != NOCRED) { 1900 crfree(bp->b_wcred); 1901 bp->b_wcred = NOCRED; 1902 } 1903 if (LIST_FIRST(&bp->b_dep) != NULL) 1904 buf_deallocate(bp); 1905 if (bp->b_vflags & BV_BKGRDINPROG) 1906 panic("losing buffer 3"); 1907 1908 if (bp->b_bufsize) 1909 allocbuf(bp, 0); 1910 1911 bp->b_flags = 0; 1912 bp->b_ioflags = 0; 1913 bp->b_xflags = 0; 1914 bp->b_vflags = 0; 1915 bp->b_dev = NULL; 1916 bp->b_vp = NULL; 1917 bp->b_blkno = bp->b_lblkno = 0; 1918 bp->b_offset = NOOFFSET; 1919 bp->b_iodone = 0; 1920 bp->b_error = 0; 1921 bp->b_resid = 0; 1922 bp->b_bcount = 0; 1923 bp->b_npages = 0; 1924 bp->b_dirtyoff = bp->b_dirtyend = 0; 1925 bp->b_magic = B_MAGIC_BIO; 1926 bp->b_object = NULL; 1927 bp->b_bufobj = NULL; 1928 1929 LIST_INIT(&bp->b_dep); 1930 1931 /* 1932 * If we are defragging then free the buffer. 1933 */ 1934 if (defrag) { 1935 bp->b_flags |= B_INVAL; 1936 bfreekva(bp); 1937 brelse(bp); 1938 defrag = 0; 1939 goto restart; 1940 } 1941 1942 /* 1943 * If we are overcomitted then recover the buffer and its 1944 * KVM space. This occurs in rare situations when multiple 1945 * processes are blocked in getnewbuf() or allocbuf(). 1946 */ 1947 if (bufspace >= hibufspace) 1948 flushingbufs = 1; 1949 if (flushingbufs && bp->b_kvasize != 0) { 1950 bp->b_flags |= B_INVAL; 1951 bfreekva(bp); 1952 brelse(bp); 1953 goto restart; 1954 } 1955 if (bufspace < lobufspace) 1956 flushingbufs = 0; 1957 break; 1958 } 1959 1960 /* 1961 * If we exhausted our list, sleep as appropriate. We may have to 1962 * wakeup various daemons and write out some dirty buffers. 1963 * 1964 * Generally we are sleeping due to insufficient buffer space. 1965 */ 1966 1967 if (bp == NULL) { 1968 int flags; 1969 char *waitmsg; 1970 1971 mtx_unlock(&bqlock); 1972 if (defrag) { 1973 flags = VFS_BIO_NEED_BUFSPACE; 1974 waitmsg = "nbufkv"; 1975 } else if (bufspace >= hibufspace) { 1976 waitmsg = "nbufbs"; 1977 flags = VFS_BIO_NEED_BUFSPACE; 1978 } else { 1979 waitmsg = "newbuf"; 1980 flags = VFS_BIO_NEED_ANY; 1981 } 1982 1983 bd_speedup(); /* heeeelp */ 1984 1985 mtx_lock(&nblock); 1986 needsbuffer |= flags; 1987 while (needsbuffer & flags) { 1988 if (msleep(&needsbuffer, &nblock, 1989 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) { 1990 mtx_unlock(&nblock); 1991 return (NULL); 1992 } 1993 } 1994 mtx_unlock(&nblock); 1995 } else { 1996 /* 1997 * We finally have a valid bp. We aren't quite out of the 1998 * woods, we still have to reserve kva space. In order 1999 * to keep fragmentation sane we only allocate kva in 2000 * BKVASIZE chunks. 2001 */ 2002 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK; 2003 2004 if (maxsize != bp->b_kvasize) { 2005 vm_offset_t addr = 0; 2006 2007 bfreekva(bp); 2008 2009 if (vm_map_findspace(buffer_map, 2010 vm_map_min(buffer_map), maxsize, &addr)) { 2011 /* 2012 * Uh oh. Buffer map is to fragmented. We 2013 * must defragment the map. 2014 */ 2015 atomic_add_int(&bufdefragcnt, 1); 2016 defrag = 1; 2017 bp->b_flags |= B_INVAL; 2018 brelse(bp); 2019 goto restart; 2020 } 2021 if (addr) { 2022 vm_map_insert(buffer_map, NULL, 0, 2023 addr, addr + maxsize, 2024 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 2025 2026 bp->b_kvabase = (caddr_t) addr; 2027 bp->b_kvasize = maxsize; 2028 atomic_add_int(&bufspace, bp->b_kvasize); 2029 atomic_add_int(&bufreusecnt, 1); 2030 } 2031 } 2032 bp->b_saveaddr = bp->b_kvabase; 2033 bp->b_data = bp->b_saveaddr; 2034 } 2035 return(bp); 2036} 2037 2038/* 2039 * buf_daemon: 2040 * 2041 * buffer flushing daemon. Buffers are normally flushed by the 2042 * update daemon but if it cannot keep up this process starts to 2043 * take the load in an attempt to prevent getnewbuf() from blocking. 2044 */ 2045 2046static struct kproc_desc buf_kp = { 2047 "bufdaemon", 2048 buf_daemon, 2049 &bufdaemonproc 2050}; 2051SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp) 2052 2053static void 2054buf_daemon() 2055{ 2056 int s; 2057 2058 mtx_lock(&Giant); 2059 2060 /* 2061 * This process needs to be suspended prior to shutdown sync. 2062 */ 2063 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc, 2064 SHUTDOWN_PRI_LAST); 2065 2066 /* 2067 * This process is allowed to take the buffer cache to the limit 2068 */ 2069 s = splbio(); 2070 mtx_lock(&bdlock); 2071 2072 for (;;) { 2073 bd_request = 0; 2074 mtx_unlock(&bdlock); 2075 2076 kthread_suspend_check(bufdaemonproc); 2077 2078 /* 2079 * Do the flush. Limit the amount of in-transit I/O we 2080 * allow to build up, otherwise we would completely saturate 2081 * the I/O system. Wakeup any waiting processes before we 2082 * normally would so they can run in parallel with our drain. 2083 */ 2084 while (numdirtybuffers > lodirtybuffers) { 2085 if (flushbufqueues(0) == 0) { 2086 /* 2087 * Could not find any buffers without rollback 2088 * dependencies, so just write the first one 2089 * in the hopes of eventually making progress. 2090 */ 2091 flushbufqueues(1); 2092 break; 2093 } 2094 waitrunningbufspace(); 2095 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2); 2096 } 2097 2098 /* 2099 * Only clear bd_request if we have reached our low water 2100 * mark. The buf_daemon normally waits 1 second and 2101 * then incrementally flushes any dirty buffers that have 2102 * built up, within reason. 2103 * 2104 * If we were unable to hit our low water mark and couldn't 2105 * find any flushable buffers, we sleep half a second. 2106 * Otherwise we loop immediately. 2107 */ 2108 mtx_lock(&bdlock); 2109 if (numdirtybuffers <= lodirtybuffers) { 2110 /* 2111 * We reached our low water mark, reset the 2112 * request and sleep until we are needed again. 2113 * The sleep is just so the suspend code works. 2114 */ 2115 bd_request = 0; 2116 msleep(&bd_request, &bdlock, PVM, "psleep", hz); 2117 } else { 2118 /* 2119 * We couldn't find any flushable dirty buffers but 2120 * still have too many dirty buffers, we 2121 * have to sleep and try again. (rare) 2122 */ 2123 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10); 2124 } 2125 } 2126} 2127 2128/* 2129 * flushbufqueues: 2130 * 2131 * Try to flush a buffer in the dirty queue. We must be careful to 2132 * free up B_INVAL buffers instead of write them, which NFS is 2133 * particularly sensitive to. 2134 */ 2135int flushwithdeps = 0; 2136SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps, 2137 0, "Number of buffers flushed with dependecies that require rollbacks"); 2138 2139static int 2140flushbufqueues(int flushdeps) 2141{ 2142 struct thread *td = curthread; 2143 struct vnode *vp; 2144 struct mount *mp; 2145 struct buf *bp; 2146 int hasdeps; 2147 2148 mtx_lock(&bqlock); 2149 TAILQ_FOREACH(bp, &bufqueues[QUEUE_DIRTY], b_freelist) { 2150 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) 2151 continue; 2152 KASSERT((bp->b_flags & B_DELWRI), 2153 ("unexpected clean buffer %p", bp)); 2154 BO_LOCK(bp->b_bufobj); 2155 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 2156 BO_UNLOCK(bp->b_bufobj); 2157 BUF_UNLOCK(bp); 2158 continue; 2159 } 2160 BO_UNLOCK(bp->b_bufobj); 2161 if (bp->b_flags & B_INVAL) { 2162 bremfreel(bp); 2163 mtx_unlock(&bqlock); 2164 brelse(bp); 2165 return (1); 2166 } 2167 2168 if (LIST_FIRST(&bp->b_dep) != NULL && buf_countdeps(bp, 0)) { 2169 if (flushdeps == 0) { 2170 BUF_UNLOCK(bp); 2171 continue; 2172 } 2173 hasdeps = 1; 2174 } else 2175 hasdeps = 0; 2176 /* 2177 * We must hold the lock on a vnode before writing 2178 * one of its buffers. Otherwise we may confuse, or 2179 * in the case of a snapshot vnode, deadlock the 2180 * system. 2181 * 2182 * The lock order here is the reverse of the normal 2183 * of vnode followed by buf lock. This is ok because 2184 * the NOWAIT will prevent deadlock. 2185 */ 2186 vp = bp->b_vp; 2187 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2188 BUF_UNLOCK(bp); 2189 continue; 2190 } 2191 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) == 0) { 2192 mtx_unlock(&bqlock); 2193 vfs_bio_awrite(bp); 2194 vn_finished_write(mp); 2195 VOP_UNLOCK(vp, 0, td); 2196 flushwithdeps += hasdeps; 2197 return (1); 2198 } 2199 vn_finished_write(mp); 2200 BUF_UNLOCK(bp); 2201 } 2202 mtx_unlock(&bqlock); 2203 return (0); 2204} 2205 2206/* 2207 * Check to see if a block is currently memory resident. 2208 */ 2209struct buf * 2210incore(struct bufobj *bo, daddr_t blkno) 2211{ 2212 struct buf *bp; 2213 2214 int s = splbio(); 2215 BO_LOCK(bo); 2216 bp = gbincore(bo, blkno); 2217 BO_UNLOCK(bo); 2218 splx(s); 2219 return (bp); 2220} 2221 2222/* 2223 * Returns true if no I/O is needed to access the 2224 * associated VM object. This is like incore except 2225 * it also hunts around in the VM system for the data. 2226 */ 2227 2228static int 2229inmem(struct vnode * vp, daddr_t blkno) 2230{ 2231 vm_object_t obj; 2232 vm_offset_t toff, tinc, size; 2233 vm_page_t m; 2234 vm_ooffset_t off; 2235 2236 GIANT_REQUIRED; 2237 ASSERT_VOP_LOCKED(vp, "inmem"); 2238 2239 if (incore(&vp->v_bufobj, blkno)) 2240 return 1; 2241 if (vp->v_mount == NULL) 2242 return 0; 2243 if (VOP_GETVOBJECT(vp, &obj) != 0 || (vp->v_vflag & VV_OBJBUF) == 0) 2244 return 0; 2245 2246 size = PAGE_SIZE; 2247 if (size > vp->v_mount->mnt_stat.f_iosize) 2248 size = vp->v_mount->mnt_stat.f_iosize; 2249 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize; 2250 2251 VM_OBJECT_LOCK(obj); 2252 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) { 2253 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff)); 2254 if (!m) 2255 goto notinmem; 2256 tinc = size; 2257 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK)) 2258 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK); 2259 if (vm_page_is_valid(m, 2260 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0) 2261 goto notinmem; 2262 } 2263 VM_OBJECT_UNLOCK(obj); 2264 return 1; 2265 2266notinmem: 2267 VM_OBJECT_UNLOCK(obj); 2268 return (0); 2269} 2270 2271/* 2272 * vfs_setdirty: 2273 * 2274 * Sets the dirty range for a buffer based on the status of the dirty 2275 * bits in the pages comprising the buffer. 2276 * 2277 * The range is limited to the size of the buffer. 2278 * 2279 * This routine is primarily used by NFS, but is generalized for the 2280 * B_VMIO case. 2281 */ 2282static void 2283vfs_setdirty(struct buf *bp) 2284{ 2285 int i; 2286 vm_object_t object; 2287 2288 GIANT_REQUIRED; 2289 /* 2290 * Degenerate case - empty buffer 2291 */ 2292 2293 if (bp->b_bufsize == 0) 2294 return; 2295 2296 /* 2297 * We qualify the scan for modified pages on whether the 2298 * object has been flushed yet. The OBJ_WRITEABLE flag 2299 * is not cleared simply by protecting pages off. 2300 */ 2301 2302 if ((bp->b_flags & B_VMIO) == 0) 2303 return; 2304 2305 object = bp->b_pages[0]->object; 2306 VM_OBJECT_LOCK(object); 2307 if ((object->flags & OBJ_WRITEABLE) && !(object->flags & OBJ_MIGHTBEDIRTY)) 2308 printf("Warning: object %p writeable but not mightbedirty\n", object); 2309 if (!(object->flags & OBJ_WRITEABLE) && (object->flags & OBJ_MIGHTBEDIRTY)) 2310 printf("Warning: object %p mightbedirty but not writeable\n", object); 2311 2312 if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) { 2313 vm_offset_t boffset; 2314 vm_offset_t eoffset; 2315 2316 vm_page_lock_queues(); 2317 /* 2318 * test the pages to see if they have been modified directly 2319 * by users through the VM system. 2320 */ 2321 for (i = 0; i < bp->b_npages; i++) 2322 vm_page_test_dirty(bp->b_pages[i]); 2323 2324 /* 2325 * Calculate the encompassing dirty range, boffset and eoffset, 2326 * (eoffset - boffset) bytes. 2327 */ 2328 2329 for (i = 0; i < bp->b_npages; i++) { 2330 if (bp->b_pages[i]->dirty) 2331 break; 2332 } 2333 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK); 2334 2335 for (i = bp->b_npages - 1; i >= 0; --i) { 2336 if (bp->b_pages[i]->dirty) { 2337 break; 2338 } 2339 } 2340 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK); 2341 2342 vm_page_unlock_queues(); 2343 /* 2344 * Fit it to the buffer. 2345 */ 2346 2347 if (eoffset > bp->b_bcount) 2348 eoffset = bp->b_bcount; 2349 2350 /* 2351 * If we have a good dirty range, merge with the existing 2352 * dirty range. 2353 */ 2354 2355 if (boffset < eoffset) { 2356 if (bp->b_dirtyoff > boffset) 2357 bp->b_dirtyoff = boffset; 2358 if (bp->b_dirtyend < eoffset) 2359 bp->b_dirtyend = eoffset; 2360 } 2361 } 2362 VM_OBJECT_UNLOCK(object); 2363} 2364 2365/* 2366 * getblk: 2367 * 2368 * Get a block given a specified block and offset into a file/device. 2369 * The buffers B_DONE bit will be cleared on return, making it almost 2370 * ready for an I/O initiation. B_INVAL may or may not be set on 2371 * return. The caller should clear B_INVAL prior to initiating a 2372 * READ. 2373 * 2374 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for 2375 * an existing buffer. 2376 * 2377 * For a VMIO buffer, B_CACHE is modified according to the backing VM. 2378 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set 2379 * and then cleared based on the backing VM. If the previous buffer is 2380 * non-0-sized but invalid, B_CACHE will be cleared. 2381 * 2382 * If getblk() must create a new buffer, the new buffer is returned with 2383 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which 2384 * case it is returned with B_INVAL clear and B_CACHE set based on the 2385 * backing VM. 2386 * 2387 * getblk() also forces a bwrite() for any B_DELWRI buffer whos 2388 * B_CACHE bit is clear. 2389 * 2390 * What this means, basically, is that the caller should use B_CACHE to 2391 * determine whether the buffer is fully valid or not and should clear 2392 * B_INVAL prior to issuing a read. If the caller intends to validate 2393 * the buffer by loading its data area with something, the caller needs 2394 * to clear B_INVAL. If the caller does this without issuing an I/O, 2395 * the caller should set B_CACHE ( as an optimization ), else the caller 2396 * should issue the I/O and biodone() will set B_CACHE if the I/O was 2397 * a write attempt or if it was a successfull read. If the caller 2398 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR 2399 * prior to issuing the READ. biodone() will *not* clear B_INVAL. 2400 */ 2401struct buf * 2402getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo, 2403 int flags) 2404{ 2405 struct buf *bp; 2406 struct bufobj *bo; 2407 int s; 2408 int error; 2409 ASSERT_VOP_LOCKED(vp, "getblk"); 2410 2411 if (size > MAXBSIZE) 2412 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE); 2413 2414 bo = &vp->v_bufobj; 2415 s = splbio(); 2416loop: 2417 /* 2418 * Block if we are low on buffers. Certain processes are allowed 2419 * to completely exhaust the buffer cache. 2420 * 2421 * If this check ever becomes a bottleneck it may be better to 2422 * move it into the else, when gbincore() fails. At the moment 2423 * it isn't a problem. 2424 * 2425 * XXX remove if 0 sections (clean this up after its proven) 2426 */ 2427 if (numfreebuffers == 0) { 2428 if (curthread == PCPU_GET(idlethread)) 2429 return NULL; 2430 mtx_lock(&nblock); 2431 needsbuffer |= VFS_BIO_NEED_ANY; 2432 mtx_unlock(&nblock); 2433 } 2434 2435 VI_LOCK(vp); 2436 bp = gbincore(bo, blkno); 2437 if (bp != NULL) { 2438 int lockflags; 2439 /* 2440 * Buffer is in-core. If the buffer is not busy, it must 2441 * be on a queue. 2442 */ 2443 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK; 2444 2445 if (flags & GB_LOCK_NOWAIT) 2446 lockflags |= LK_NOWAIT; 2447 2448 error = BUF_TIMELOCK(bp, lockflags, 2449 VI_MTX(vp), "getblk", slpflag, slptimeo); 2450 2451 /* 2452 * If we slept and got the lock we have to restart in case 2453 * the buffer changed identities. 2454 */ 2455 if (error == ENOLCK) 2456 goto loop; 2457 /* We timed out or were interrupted. */ 2458 else if (error) 2459 return (NULL); 2460 2461 /* 2462 * The buffer is locked. B_CACHE is cleared if the buffer is 2463 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set 2464 * and for a VMIO buffer B_CACHE is adjusted according to the 2465 * backing VM cache. 2466 */ 2467 if (bp->b_flags & B_INVAL) 2468 bp->b_flags &= ~B_CACHE; 2469 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0) 2470 bp->b_flags |= B_CACHE; 2471 bremfree(bp); 2472 2473 /* 2474 * check for size inconsistancies for non-VMIO case. 2475 */ 2476 2477 if (bp->b_bcount != size) { 2478 if ((bp->b_flags & B_VMIO) == 0 || 2479 (size > bp->b_kvasize)) { 2480 if (bp->b_flags & B_DELWRI) { 2481 bp->b_flags |= B_NOCACHE; 2482 bwrite(bp); 2483 } else { 2484 if ((bp->b_flags & B_VMIO) && 2485 (LIST_FIRST(&bp->b_dep) == NULL)) { 2486 bp->b_flags |= B_RELBUF; 2487 brelse(bp); 2488 } else { 2489 bp->b_flags |= B_NOCACHE; 2490 bwrite(bp); 2491 } 2492 } 2493 goto loop; 2494 } 2495 } 2496 2497 /* 2498 * If the size is inconsistant in the VMIO case, we can resize 2499 * the buffer. This might lead to B_CACHE getting set or 2500 * cleared. If the size has not changed, B_CACHE remains 2501 * unchanged from its previous state. 2502 */ 2503 2504 if (bp->b_bcount != size) 2505 allocbuf(bp, size); 2506 2507 KASSERT(bp->b_offset != NOOFFSET, 2508 ("getblk: no buffer offset")); 2509 2510 /* 2511 * A buffer with B_DELWRI set and B_CACHE clear must 2512 * be committed before we can return the buffer in 2513 * order to prevent the caller from issuing a read 2514 * ( due to B_CACHE not being set ) and overwriting 2515 * it. 2516 * 2517 * Most callers, including NFS and FFS, need this to 2518 * operate properly either because they assume they 2519 * can issue a read if B_CACHE is not set, or because 2520 * ( for example ) an uncached B_DELWRI might loop due 2521 * to softupdates re-dirtying the buffer. In the latter 2522 * case, B_CACHE is set after the first write completes, 2523 * preventing further loops. 2524 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE 2525 * above while extending the buffer, we cannot allow the 2526 * buffer to remain with B_CACHE set after the write 2527 * completes or it will represent a corrupt state. To 2528 * deal with this we set B_NOCACHE to scrap the buffer 2529 * after the write. 2530 * 2531 * We might be able to do something fancy, like setting 2532 * B_CACHE in bwrite() except if B_DELWRI is already set, 2533 * so the below call doesn't set B_CACHE, but that gets real 2534 * confusing. This is much easier. 2535 */ 2536 2537 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) { 2538 bp->b_flags |= B_NOCACHE; 2539 bwrite(bp); 2540 goto loop; 2541 } 2542 2543 splx(s); 2544 bp->b_flags &= ~B_DONE; 2545 } else { 2546 int bsize, maxsize, vmio; 2547 off_t offset; 2548 2549 /* 2550 * Buffer is not in-core, create new buffer. The buffer 2551 * returned by getnewbuf() is locked. Note that the returned 2552 * buffer is also considered valid (not marked B_INVAL). 2553 */ 2554 VI_UNLOCK(vp); 2555 /* 2556 * If the user does not want us to create the buffer, bail out 2557 * here. 2558 */ 2559 if (flags & GB_NOCREAT) { 2560 splx(s); 2561 return NULL; 2562 } 2563 if (vn_isdisk(vp, NULL)) 2564 bsize = DEV_BSIZE; 2565 else if (vp->v_mountedhere) 2566 bsize = vp->v_mountedhere->mnt_stat.f_iosize; 2567 else if (vp->v_mount) 2568 bsize = vp->v_mount->mnt_stat.f_iosize; 2569 else 2570 bsize = size; 2571 2572 if (vp->v_bsize != bsize) { 2573#if 0 2574 printf("WARNING: Wrong block size on vnode: %d should be %d\n", vp->v_bsize, bsize); 2575#endif 2576 vp->v_bsize = bsize; 2577 } 2578 2579 offset = blkno * bsize; 2580 vmio = (VOP_GETVOBJECT(vp, NULL) == 0) && 2581 (vp->v_vflag & VV_OBJBUF); 2582 maxsize = vmio ? size + (offset & PAGE_MASK) : size; 2583 maxsize = imax(maxsize, bsize); 2584 2585 bp = getnewbuf(slpflag, slptimeo, size, maxsize); 2586 if (bp == NULL) { 2587 if (slpflag || slptimeo) { 2588 splx(s); 2589 return NULL; 2590 } 2591 goto loop; 2592 } 2593 2594 /* 2595 * This code is used to make sure that a buffer is not 2596 * created while the getnewbuf routine is blocked. 2597 * This can be a problem whether the vnode is locked or not. 2598 * If the buffer is created out from under us, we have to 2599 * throw away the one we just created. There is now window 2600 * race because we are safely running at splbio() from the 2601 * point of the duplicate buffer creation through to here, 2602 * and we've locked the buffer. 2603 * 2604 * Note: this must occur before we associate the buffer 2605 * with the vp especially considering limitations in 2606 * the splay tree implementation when dealing with duplicate 2607 * lblkno's. 2608 */ 2609 BO_LOCK(bo); 2610 if (gbincore(bo, blkno)) { 2611 BO_UNLOCK(bo); 2612 bp->b_flags |= B_INVAL; 2613 brelse(bp); 2614 goto loop; 2615 } 2616 2617 /* 2618 * Insert the buffer into the hash, so that it can 2619 * be found by incore. 2620 */ 2621 bp->b_blkno = bp->b_lblkno = blkno; 2622 bp->b_offset = offset; 2623 2624 bgetvp(vp, bp); 2625 BO_UNLOCK(bo); 2626 2627 /* 2628 * set B_VMIO bit. allocbuf() the buffer bigger. Since the 2629 * buffer size starts out as 0, B_CACHE will be set by 2630 * allocbuf() for the VMIO case prior to it testing the 2631 * backing store for validity. 2632 */ 2633 2634 if (vmio) { 2635 bp->b_flags |= B_VMIO; 2636#if defined(VFS_BIO_DEBUG) 2637 if (vn_canvmio(vp) != TRUE) 2638 printf("getblk: VMIO on vnode type %d\n", 2639 vp->v_type); 2640#endif 2641 VOP_GETVOBJECT(vp, &bp->b_object); 2642 } else { 2643 bp->b_flags &= ~B_VMIO; 2644 bp->b_object = NULL; 2645 } 2646 2647 allocbuf(bp, size); 2648 2649 splx(s); 2650 bp->b_flags &= ~B_DONE; 2651 } 2652 KASSERT(BUF_REFCNT(bp) == 1, ("getblk: bp %p not locked",bp)); 2653 KASSERT(bp->b_bufobj == bo, 2654 ("wrong b_bufobj %p should be %p", bp->b_bufobj, bo)); 2655 return (bp); 2656} 2657 2658/* 2659 * Get an empty, disassociated buffer of given size. The buffer is initially 2660 * set to B_INVAL. 2661 */ 2662struct buf * 2663geteblk(int size) 2664{ 2665 struct buf *bp; 2666 int s; 2667 int maxsize; 2668 2669 maxsize = (size + BKVAMASK) & ~BKVAMASK; 2670 2671 s = splbio(); 2672 while ((bp = getnewbuf(0, 0, size, maxsize)) == 0) 2673 continue; 2674 splx(s); 2675 allocbuf(bp, size); 2676 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */ 2677 KASSERT(BUF_REFCNT(bp) == 1, ("geteblk: bp %p not locked",bp)); 2678 return (bp); 2679} 2680 2681 2682/* 2683 * This code constitutes the buffer memory from either anonymous system 2684 * memory (in the case of non-VMIO operations) or from an associated 2685 * VM object (in the case of VMIO operations). This code is able to 2686 * resize a buffer up or down. 2687 * 2688 * Note that this code is tricky, and has many complications to resolve 2689 * deadlock or inconsistant data situations. Tread lightly!!! 2690 * There are B_CACHE and B_DELWRI interactions that must be dealt with by 2691 * the caller. Calling this code willy nilly can result in the loss of data. 2692 * 2693 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with 2694 * B_CACHE for the non-VMIO case. 2695 */ 2696 2697int 2698allocbuf(struct buf *bp, int size) 2699{ 2700 int newbsize, mbsize; 2701 int i; 2702 2703 GIANT_REQUIRED; 2704 2705 if (BUF_REFCNT(bp) == 0) 2706 panic("allocbuf: buffer not busy"); 2707 2708 if (bp->b_kvasize < size) 2709 panic("allocbuf: buffer too small"); 2710 2711 if ((bp->b_flags & B_VMIO) == 0) { 2712 caddr_t origbuf; 2713 int origbufsize; 2714 /* 2715 * Just get anonymous memory from the kernel. Don't 2716 * mess with B_CACHE. 2717 */ 2718 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 2719 if (bp->b_flags & B_MALLOC) 2720 newbsize = mbsize; 2721 else 2722 newbsize = round_page(size); 2723 2724 if (newbsize < bp->b_bufsize) { 2725 /* 2726 * malloced buffers are not shrunk 2727 */ 2728 if (bp->b_flags & B_MALLOC) { 2729 if (newbsize) { 2730 bp->b_bcount = size; 2731 } else { 2732 free(bp->b_data, M_BIOBUF); 2733 if (bp->b_bufsize) { 2734 atomic_subtract_int( 2735 &bufmallocspace, 2736 bp->b_bufsize); 2737 bufspacewakeup(); 2738 bp->b_bufsize = 0; 2739 } 2740 bp->b_saveaddr = bp->b_kvabase; 2741 bp->b_data = bp->b_saveaddr; 2742 bp->b_bcount = 0; 2743 bp->b_flags &= ~B_MALLOC; 2744 } 2745 return 1; 2746 } 2747 vm_hold_free_pages( 2748 bp, 2749 (vm_offset_t) bp->b_data + newbsize, 2750 (vm_offset_t) bp->b_data + bp->b_bufsize); 2751 } else if (newbsize > bp->b_bufsize) { 2752 /* 2753 * We only use malloced memory on the first allocation. 2754 * and revert to page-allocated memory when the buffer 2755 * grows. 2756 */ 2757 /* 2758 * There is a potential smp race here that could lead 2759 * to bufmallocspace slightly passing the max. It 2760 * is probably extremely rare and not worth worrying 2761 * over. 2762 */ 2763 if ( (bufmallocspace < maxbufmallocspace) && 2764 (bp->b_bufsize == 0) && 2765 (mbsize <= PAGE_SIZE/2)) { 2766 2767 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK); 2768 bp->b_bufsize = mbsize; 2769 bp->b_bcount = size; 2770 bp->b_flags |= B_MALLOC; 2771 atomic_add_int(&bufmallocspace, mbsize); 2772 return 1; 2773 } 2774 origbuf = NULL; 2775 origbufsize = 0; 2776 /* 2777 * If the buffer is growing on its other-than-first allocation, 2778 * then we revert to the page-allocation scheme. 2779 */ 2780 if (bp->b_flags & B_MALLOC) { 2781 origbuf = bp->b_data; 2782 origbufsize = bp->b_bufsize; 2783 bp->b_data = bp->b_kvabase; 2784 if (bp->b_bufsize) { 2785 atomic_subtract_int(&bufmallocspace, 2786 bp->b_bufsize); 2787 bufspacewakeup(); 2788 bp->b_bufsize = 0; 2789 } 2790 bp->b_flags &= ~B_MALLOC; 2791 newbsize = round_page(newbsize); 2792 } 2793 vm_hold_load_pages( 2794 bp, 2795 (vm_offset_t) bp->b_data + bp->b_bufsize, 2796 (vm_offset_t) bp->b_data + newbsize); 2797 if (origbuf) { 2798 bcopy(origbuf, bp->b_data, origbufsize); 2799 free(origbuf, M_BIOBUF); 2800 } 2801 } 2802 } else { 2803 int desiredpages; 2804 2805 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 2806 desiredpages = (size == 0) ? 0 : 2807 num_pages((bp->b_offset & PAGE_MASK) + newbsize); 2808 2809 if (bp->b_flags & B_MALLOC) 2810 panic("allocbuf: VMIO buffer can't be malloced"); 2811 /* 2812 * Set B_CACHE initially if buffer is 0 length or will become 2813 * 0-length. 2814 */ 2815 if (size == 0 || bp->b_bufsize == 0) 2816 bp->b_flags |= B_CACHE; 2817 2818 if (newbsize < bp->b_bufsize) { 2819 /* 2820 * DEV_BSIZE aligned new buffer size is less then the 2821 * DEV_BSIZE aligned existing buffer size. Figure out 2822 * if we have to remove any pages. 2823 */ 2824 if (desiredpages < bp->b_npages) { 2825 vm_page_t m; 2826 2827 vm_page_lock_queues(); 2828 for (i = desiredpages; i < bp->b_npages; i++) { 2829 /* 2830 * the page is not freed here -- it 2831 * is the responsibility of 2832 * vnode_pager_setsize 2833 */ 2834 m = bp->b_pages[i]; 2835 KASSERT(m != bogus_page, 2836 ("allocbuf: bogus page found")); 2837 while (vm_page_sleep_if_busy(m, TRUE, "biodep")) 2838 vm_page_lock_queues(); 2839 2840 bp->b_pages[i] = NULL; 2841 vm_page_unwire(m, 0); 2842 } 2843 vm_page_unlock_queues(); 2844 pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) + 2845 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages)); 2846 bp->b_npages = desiredpages; 2847 } 2848 } else if (size > bp->b_bcount) { 2849 /* 2850 * We are growing the buffer, possibly in a 2851 * byte-granular fashion. 2852 */ 2853 struct vnode *vp; 2854 vm_object_t obj; 2855 vm_offset_t toff; 2856 vm_offset_t tinc; 2857 2858 /* 2859 * Step 1, bring in the VM pages from the object, 2860 * allocating them if necessary. We must clear 2861 * B_CACHE if these pages are not valid for the 2862 * range covered by the buffer. 2863 */ 2864 2865 vp = bp->b_vp; 2866 obj = bp->b_object; 2867 2868 VM_OBJECT_LOCK(obj); 2869 while (bp->b_npages < desiredpages) { 2870 vm_page_t m; 2871 vm_pindex_t pi; 2872 2873 pi = OFF_TO_IDX(bp->b_offset) + bp->b_npages; 2874 if ((m = vm_page_lookup(obj, pi)) == NULL) { 2875 /* 2876 * note: must allocate system pages 2877 * since blocking here could intefere 2878 * with paging I/O, no matter which 2879 * process we are. 2880 */ 2881 m = vm_page_alloc(obj, pi, 2882 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED); 2883 if (m == NULL) { 2884 atomic_add_int(&vm_pageout_deficit, 2885 desiredpages - bp->b_npages); 2886 VM_OBJECT_UNLOCK(obj); 2887 VM_WAIT; 2888 VM_OBJECT_LOCK(obj); 2889 } else { 2890 vm_page_lock_queues(); 2891 vm_page_wakeup(m); 2892 vm_page_unlock_queues(); 2893 bp->b_flags &= ~B_CACHE; 2894 bp->b_pages[bp->b_npages] = m; 2895 ++bp->b_npages; 2896 } 2897 continue; 2898 } 2899 2900 /* 2901 * We found a page. If we have to sleep on it, 2902 * retry because it might have gotten freed out 2903 * from under us. 2904 * 2905 * We can only test PG_BUSY here. Blocking on 2906 * m->busy might lead to a deadlock: 2907 * 2908 * vm_fault->getpages->cluster_read->allocbuf 2909 * 2910 */ 2911 vm_page_lock_queues(); 2912 if (vm_page_sleep_if_busy(m, FALSE, "pgtblk")) 2913 continue; 2914 2915 /* 2916 * We have a good page. Should we wakeup the 2917 * page daemon? 2918 */ 2919 if ((curproc != pageproc) && 2920 ((m->queue - m->pc) == PQ_CACHE) && 2921 ((cnt.v_free_count + cnt.v_cache_count) < 2922 (cnt.v_free_min + cnt.v_cache_min))) { 2923 pagedaemon_wakeup(); 2924 } 2925 vm_page_wire(m); 2926 vm_page_unlock_queues(); 2927 bp->b_pages[bp->b_npages] = m; 2928 ++bp->b_npages; 2929 } 2930 2931 /* 2932 * Step 2. We've loaded the pages into the buffer, 2933 * we have to figure out if we can still have B_CACHE 2934 * set. Note that B_CACHE is set according to the 2935 * byte-granular range ( bcount and size ), new the 2936 * aligned range ( newbsize ). 2937 * 2938 * The VM test is against m->valid, which is DEV_BSIZE 2939 * aligned. Needless to say, the validity of the data 2940 * needs to also be DEV_BSIZE aligned. Note that this 2941 * fails with NFS if the server or some other client 2942 * extends the file's EOF. If our buffer is resized, 2943 * B_CACHE may remain set! XXX 2944 */ 2945 2946 toff = bp->b_bcount; 2947 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK); 2948 2949 while ((bp->b_flags & B_CACHE) && toff < size) { 2950 vm_pindex_t pi; 2951 2952 if (tinc > (size - toff)) 2953 tinc = size - toff; 2954 2955 pi = ((bp->b_offset & PAGE_MASK) + toff) >> 2956 PAGE_SHIFT; 2957 2958 vfs_buf_test_cache( 2959 bp, 2960 bp->b_offset, 2961 toff, 2962 tinc, 2963 bp->b_pages[pi] 2964 ); 2965 toff += tinc; 2966 tinc = PAGE_SIZE; 2967 } 2968 VM_OBJECT_UNLOCK(obj); 2969 2970 /* 2971 * Step 3, fixup the KVM pmap. Remember that 2972 * bp->b_data is relative to bp->b_offset, but 2973 * bp->b_offset may be offset into the first page. 2974 */ 2975 2976 bp->b_data = (caddr_t) 2977 trunc_page((vm_offset_t)bp->b_data); 2978 pmap_qenter( 2979 (vm_offset_t)bp->b_data, 2980 bp->b_pages, 2981 bp->b_npages 2982 ); 2983 2984 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data | 2985 (vm_offset_t)(bp->b_offset & PAGE_MASK)); 2986 } 2987 } 2988 if (newbsize < bp->b_bufsize) 2989 bufspacewakeup(); 2990 bp->b_bufsize = newbsize; /* actual buffer allocation */ 2991 bp->b_bcount = size; /* requested buffer size */ 2992 return 1; 2993} 2994 2995void 2996biodone(struct bio *bp) 2997{ 2998 2999 mtx_lock(&bdonelock); 3000 bp->bio_flags |= BIO_DONE; 3001 if (bp->bio_done == NULL) 3002 wakeup(bp); 3003 mtx_unlock(&bdonelock); 3004 if (bp->bio_done != NULL) 3005 bp->bio_done(bp); 3006} 3007 3008/* 3009 * Wait for a BIO to finish. 3010 * 3011 * XXX: resort to a timeout for now. The optimal locking (if any) for this 3012 * case is not yet clear. 3013 */ 3014int 3015biowait(struct bio *bp, const char *wchan) 3016{ 3017 3018 mtx_lock(&bdonelock); 3019 while ((bp->bio_flags & BIO_DONE) == 0) 3020 msleep(bp, &bdonelock, PRIBIO, wchan, hz / 10); 3021 mtx_unlock(&bdonelock); 3022 if (bp->bio_error != 0) 3023 return (bp->bio_error); 3024 if (!(bp->bio_flags & BIO_ERROR)) 3025 return (0); 3026 return (EIO); 3027} 3028 3029void 3030biofinish(struct bio *bp, struct devstat *stat, int error) 3031{ 3032 3033 if (error) { 3034 bp->bio_error = error; 3035 bp->bio_flags |= BIO_ERROR; 3036 } 3037 if (stat != NULL) 3038 devstat_end_transaction_bio(stat, bp); 3039 biodone(bp); 3040} 3041 3042/* 3043 * bufwait: 3044 * 3045 * Wait for buffer I/O completion, returning error status. The buffer 3046 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR 3047 * error and cleared. 3048 */ 3049int 3050bufwait(struct buf *bp) 3051{ 3052 int s; 3053 3054 s = splbio(); 3055 if (bp->b_iocmd == BIO_READ) 3056 bwait(bp, PRIBIO, "biord"); 3057 else 3058 bwait(bp, PRIBIO, "biowr"); 3059 splx(s); 3060 if (bp->b_flags & B_EINTR) { 3061 bp->b_flags &= ~B_EINTR; 3062 return (EINTR); 3063 } 3064 if (bp->b_ioflags & BIO_ERROR) { 3065 return (bp->b_error ? bp->b_error : EIO); 3066 } else { 3067 return (0); 3068 } 3069} 3070 3071 /* 3072 * Call back function from struct bio back up to struct buf. 3073 */ 3074static void 3075bufdonebio(struct bio *bp) 3076{ 3077 3078 /* Device drivers may or may not hold giant, hold it here. */ 3079 mtx_lock(&Giant); 3080 bufdone(bp->bio_caller2); 3081 mtx_unlock(&Giant); 3082} 3083 3084void 3085dev_strategy(struct buf *bp) 3086{ 3087 struct cdevsw *csw; 3088 struct cdev *dev; 3089 3090 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1))) 3091 panic("b_iocmd botch"); 3092 bp->b_io.bio_done = bufdonebio; 3093 bp->b_io.bio_caller2 = bp; 3094 dev = bp->b_io.bio_dev; 3095 KASSERT(dev->si_refcount > 0, 3096 ("dev_strategy on un-referenced struct cdev *(%s)", 3097 devtoname(dev))); 3098 csw = dev_refthread(dev); 3099 if (csw == NULL) { 3100 bp->b_error = ENXIO; 3101 bp->b_ioflags = BIO_ERROR; 3102 mtx_lock(&Giant); /* XXX: too defensive ? */ 3103 bufdone(bp); 3104 mtx_unlock(&Giant); /* XXX: too defensive ? */ 3105 return; 3106 } 3107 (*csw->d_strategy)(&bp->b_io); 3108 dev_relthread(dev); 3109} 3110 3111/* 3112 * bufdone: 3113 * 3114 * Finish I/O on a buffer, optionally calling a completion function. 3115 * This is usually called from an interrupt so process blocking is 3116 * not allowed. 3117 * 3118 * biodone is also responsible for setting B_CACHE in a B_VMIO bp. 3119 * In a non-VMIO bp, B_CACHE will be set on the next getblk() 3120 * assuming B_INVAL is clear. 3121 * 3122 * For the VMIO case, we set B_CACHE if the op was a read and no 3123 * read error occured, or if the op was a write. B_CACHE is never 3124 * set if the buffer is invalid or otherwise uncacheable. 3125 * 3126 * biodone does not mess with B_INVAL, allowing the I/O routine or the 3127 * initiator to leave B_INVAL set to brelse the buffer out of existance 3128 * in the biodone routine. 3129 */ 3130void 3131bufdone(struct buf *bp) 3132{ 3133 int s; 3134 void (*biodone)(struct buf *); 3135 3136 3137 s = splbio(); 3138 3139 KASSERT(BUF_REFCNT(bp) > 0, ("biodone: bp %p not busy %d", bp, BUF_REFCNT(bp))); 3140 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp)); 3141 3142 bp->b_flags |= B_DONE; 3143 runningbufwakeup(bp); 3144 3145 if (bp->b_iocmd == BIO_WRITE && bp->b_bufobj != NULL) 3146 bufobj_wdrop(bp->b_bufobj); 3147 3148 /* call optional completion function if requested */ 3149 if (bp->b_iodone != NULL) { 3150 biodone = bp->b_iodone; 3151 bp->b_iodone = NULL; 3152 (*biodone) (bp); 3153 splx(s); 3154 return; 3155 } 3156 if (LIST_FIRST(&bp->b_dep) != NULL) 3157 buf_complete(bp); 3158 3159 if (bp->b_flags & B_VMIO) { 3160 int i; 3161 vm_ooffset_t foff; 3162 vm_page_t m; 3163 vm_object_t obj; 3164 int iosize; 3165 struct vnode *vp = bp->b_vp; 3166 3167 obj = bp->b_object; 3168 3169#if defined(VFS_BIO_DEBUG) 3170 mp_fixme("usecount and vflag accessed without locks."); 3171 if (vp->v_usecount == 0) { 3172 panic("biodone: zero vnode ref count"); 3173 } 3174 3175 if ((vp->v_vflag & VV_OBJBUF) == 0) { 3176 panic("biodone: vnode is not setup for merged cache"); 3177 } 3178#endif 3179 3180 foff = bp->b_offset; 3181 KASSERT(bp->b_offset != NOOFFSET, 3182 ("biodone: no buffer offset")); 3183 3184 VM_OBJECT_LOCK(obj); 3185#if defined(VFS_BIO_DEBUG) 3186 if (obj->paging_in_progress < bp->b_npages) { 3187 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n", 3188 obj->paging_in_progress, bp->b_npages); 3189 } 3190#endif 3191 3192 /* 3193 * Set B_CACHE if the op was a normal read and no error 3194 * occured. B_CACHE is set for writes in the b*write() 3195 * routines. 3196 */ 3197 iosize = bp->b_bcount - bp->b_resid; 3198 if (bp->b_iocmd == BIO_READ && 3199 !(bp->b_flags & (B_INVAL|B_NOCACHE)) && 3200 !(bp->b_ioflags & BIO_ERROR)) { 3201 bp->b_flags |= B_CACHE; 3202 } 3203 vm_page_lock_queues(); 3204 for (i = 0; i < bp->b_npages; i++) { 3205 int bogusflag = 0; 3206 int resid; 3207 3208 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff; 3209 if (resid > iosize) 3210 resid = iosize; 3211 3212 /* 3213 * cleanup bogus pages, restoring the originals 3214 */ 3215 m = bp->b_pages[i]; 3216 if (m == bogus_page) { 3217 bogusflag = 1; 3218 m = vm_page_lookup(obj, OFF_TO_IDX(foff)); 3219 if (m == NULL) 3220 panic("biodone: page disappeared!"); 3221 bp->b_pages[i] = m; 3222 pmap_qenter(trunc_page((vm_offset_t)bp->b_data), bp->b_pages, bp->b_npages); 3223 } 3224#if defined(VFS_BIO_DEBUG) 3225 if (OFF_TO_IDX(foff) != m->pindex) { 3226 printf( 3227"biodone: foff(%jd)/m->pindex(%ju) mismatch\n", 3228 (intmax_t)foff, (uintmax_t)m->pindex); 3229 } 3230#endif 3231 3232 /* 3233 * In the write case, the valid and clean bits are 3234 * already changed correctly ( see bdwrite() ), so we 3235 * only need to do this here in the read case. 3236 */ 3237 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) { 3238 vfs_page_set_valid(bp, foff, i, m); 3239 } 3240 3241 /* 3242 * when debugging new filesystems or buffer I/O methods, this 3243 * is the most common error that pops up. if you see this, you 3244 * have not set the page busy flag correctly!!! 3245 */ 3246 if (m->busy == 0) { 3247 printf("biodone: page busy < 0, " 3248 "pindex: %d, foff: 0x(%x,%x), " 3249 "resid: %d, index: %d\n", 3250 (int) m->pindex, (int)(foff >> 32), 3251 (int) foff & 0xffffffff, resid, i); 3252 if (!vn_isdisk(vp, NULL)) 3253 printf(" iosize: %jd, lblkno: %jd, flags: 0x%x, npages: %d\n", 3254 (intmax_t)bp->b_vp->v_mount->mnt_stat.f_iosize, 3255 (intmax_t) bp->b_lblkno, 3256 bp->b_flags, bp->b_npages); 3257 else 3258 printf(" VDEV, lblkno: %jd, flags: 0x%x, npages: %d\n", 3259 (intmax_t) bp->b_lblkno, 3260 bp->b_flags, bp->b_npages); 3261 printf(" valid: 0x%lx, dirty: 0x%lx, wired: %d\n", 3262 (u_long)m->valid, (u_long)m->dirty, 3263 m->wire_count); 3264 panic("biodone: page busy < 0\n"); 3265 } 3266 vm_page_io_finish(m); 3267 vm_object_pip_subtract(obj, 1); 3268 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3269 iosize -= resid; 3270 } 3271 vm_page_unlock_queues(); 3272 vm_object_pip_wakeupn(obj, 0); 3273 VM_OBJECT_UNLOCK(obj); 3274 } 3275 3276 /* 3277 * For asynchronous completions, release the buffer now. The brelse 3278 * will do a wakeup there if necessary - so no need to do a wakeup 3279 * here in the async case. The sync case always needs to do a wakeup. 3280 */ 3281 3282 if (bp->b_flags & B_ASYNC) { 3283 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR)) 3284 brelse(bp); 3285 else 3286 bqrelse(bp); 3287 } else { 3288 bdone(bp); 3289 } 3290 splx(s); 3291} 3292 3293/* 3294 * This routine is called in lieu of iodone in the case of 3295 * incomplete I/O. This keeps the busy status for pages 3296 * consistant. 3297 */ 3298void 3299vfs_unbusy_pages(struct buf *bp) 3300{ 3301 int i; 3302 vm_object_t obj; 3303 vm_page_t m; 3304 3305 runningbufwakeup(bp); 3306 if (!(bp->b_flags & B_VMIO)) 3307 return; 3308 3309 obj = bp->b_object; 3310 VM_OBJECT_LOCK(obj); 3311 vm_page_lock_queues(); 3312 for (i = 0; i < bp->b_npages; i++) { 3313 m = bp->b_pages[i]; 3314 if (m == bogus_page) { 3315 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i); 3316 if (!m) 3317 panic("vfs_unbusy_pages: page missing\n"); 3318 bp->b_pages[i] = m; 3319 pmap_qenter(trunc_page((vm_offset_t)bp->b_data), 3320 bp->b_pages, bp->b_npages); 3321 } 3322 vm_object_pip_subtract(obj, 1); 3323 vm_page_io_finish(m); 3324 } 3325 vm_page_unlock_queues(); 3326 vm_object_pip_wakeupn(obj, 0); 3327 VM_OBJECT_UNLOCK(obj); 3328} 3329 3330/* 3331 * vfs_page_set_valid: 3332 * 3333 * Set the valid bits in a page based on the supplied offset. The 3334 * range is restricted to the buffer's size. 3335 * 3336 * This routine is typically called after a read completes. 3337 */ 3338static void 3339vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, int pageno, vm_page_t m) 3340{ 3341 vm_ooffset_t soff, eoff; 3342 3343 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 3344 /* 3345 * Start and end offsets in buffer. eoff - soff may not cross a 3346 * page boundry or cross the end of the buffer. The end of the 3347 * buffer, in this case, is our file EOF, not the allocation size 3348 * of the buffer. 3349 */ 3350 soff = off; 3351 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3352 if (eoff > bp->b_offset + bp->b_bcount) 3353 eoff = bp->b_offset + bp->b_bcount; 3354 3355 /* 3356 * Set valid range. This is typically the entire buffer and thus the 3357 * entire page. 3358 */ 3359 if (eoff > soff) { 3360 vm_page_set_validclean( 3361 m, 3362 (vm_offset_t) (soff & PAGE_MASK), 3363 (vm_offset_t) (eoff - soff) 3364 ); 3365 } 3366} 3367 3368/* 3369 * This routine is called before a device strategy routine. 3370 * It is used to tell the VM system that paging I/O is in 3371 * progress, and treat the pages associated with the buffer 3372 * almost as being PG_BUSY. Also the object paging_in_progress 3373 * flag is handled to make sure that the object doesn't become 3374 * inconsistant. 3375 * 3376 * Since I/O has not been initiated yet, certain buffer flags 3377 * such as BIO_ERROR or B_INVAL may be in an inconsistant state 3378 * and should be ignored. 3379 */ 3380void 3381vfs_busy_pages(struct buf *bp, int clear_modify) 3382{ 3383 int i, bogus; 3384 vm_object_t obj; 3385 vm_ooffset_t foff; 3386 vm_page_t m; 3387 3388 if (!(bp->b_flags & B_VMIO)) 3389 return; 3390 3391 obj = bp->b_object; 3392 foff = bp->b_offset; 3393 KASSERT(bp->b_offset != NOOFFSET, 3394 ("vfs_busy_pages: no buffer offset")); 3395 vfs_setdirty(bp); 3396 VM_OBJECT_LOCK(obj); 3397retry: 3398 vm_page_lock_queues(); 3399 for (i = 0; i < bp->b_npages; i++) { 3400 m = bp->b_pages[i]; 3401 3402 if (vm_page_sleep_if_busy(m, FALSE, "vbpage")) 3403 goto retry; 3404 } 3405 bogus = 0; 3406 for (i = 0; i < bp->b_npages; i++) { 3407 m = bp->b_pages[i]; 3408 3409 if ((bp->b_flags & B_CLUSTER) == 0) { 3410 vm_object_pip_add(obj, 1); 3411 vm_page_io_start(m); 3412 } 3413 /* 3414 * When readying a buffer for a read ( i.e 3415 * clear_modify == 0 ), it is important to do 3416 * bogus_page replacement for valid pages in 3417 * partially instantiated buffers. Partially 3418 * instantiated buffers can, in turn, occur when 3419 * reconstituting a buffer from its VM backing store 3420 * base. We only have to do this if B_CACHE is 3421 * clear ( which causes the I/O to occur in the 3422 * first place ). The replacement prevents the read 3423 * I/O from overwriting potentially dirty VM-backed 3424 * pages. XXX bogus page replacement is, uh, bogus. 3425 * It may not work properly with small-block devices. 3426 * We need to find a better way. 3427 */ 3428 pmap_remove_all(m); 3429 if (clear_modify) 3430 vfs_page_set_valid(bp, foff, i, m); 3431 else if (m->valid == VM_PAGE_BITS_ALL && 3432 (bp->b_flags & B_CACHE) == 0) { 3433 bp->b_pages[i] = bogus_page; 3434 bogus++; 3435 } 3436 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3437 } 3438 vm_page_unlock_queues(); 3439 VM_OBJECT_UNLOCK(obj); 3440 if (bogus) 3441 pmap_qenter(trunc_page((vm_offset_t)bp->b_data), 3442 bp->b_pages, bp->b_npages); 3443} 3444 3445/* 3446 * Tell the VM system that the pages associated with this buffer 3447 * are clean. This is used for delayed writes where the data is 3448 * going to go to disk eventually without additional VM intevention. 3449 * 3450 * Note that while we only really need to clean through to b_bcount, we 3451 * just go ahead and clean through to b_bufsize. 3452 */ 3453static void 3454vfs_clean_pages(struct buf *bp) 3455{ 3456 int i; 3457 vm_ooffset_t foff, noff, eoff; 3458 vm_page_t m; 3459 3460 if (!(bp->b_flags & B_VMIO)) 3461 return; 3462 3463 foff = bp->b_offset; 3464 KASSERT(bp->b_offset != NOOFFSET, 3465 ("vfs_clean_pages: no buffer offset")); 3466 VM_OBJECT_LOCK(bp->b_object); 3467 vm_page_lock_queues(); 3468 for (i = 0; i < bp->b_npages; i++) { 3469 m = bp->b_pages[i]; 3470 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3471 eoff = noff; 3472 3473 if (eoff > bp->b_offset + bp->b_bufsize) 3474 eoff = bp->b_offset + bp->b_bufsize; 3475 vfs_page_set_valid(bp, foff, i, m); 3476 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */ 3477 foff = noff; 3478 } 3479 vm_page_unlock_queues(); 3480 VM_OBJECT_UNLOCK(bp->b_object); 3481} 3482 3483/* 3484 * vfs_bio_set_validclean: 3485 * 3486 * Set the range within the buffer to valid and clean. The range is 3487 * relative to the beginning of the buffer, b_offset. Note that b_offset 3488 * itself may be offset from the beginning of the first page. 3489 * 3490 */ 3491 3492void 3493vfs_bio_set_validclean(struct buf *bp, int base, int size) 3494{ 3495 int i, n; 3496 vm_page_t m; 3497 3498 if (!(bp->b_flags & B_VMIO)) 3499 return; 3500 3501 /* 3502 * Fixup base to be relative to beginning of first page. 3503 * Set initial n to be the maximum number of bytes in the 3504 * first page that can be validated. 3505 */ 3506 3507 base += (bp->b_offset & PAGE_MASK); 3508 n = PAGE_SIZE - (base & PAGE_MASK); 3509 3510 VM_OBJECT_LOCK(bp->b_object); 3511 vm_page_lock_queues(); 3512 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) { 3513 m = bp->b_pages[i]; 3514 3515 if (n > size) 3516 n = size; 3517 3518 vm_page_set_validclean(m, base & PAGE_MASK, n); 3519 base += n; 3520 size -= n; 3521 n = PAGE_SIZE; 3522 } 3523 vm_page_unlock_queues(); 3524 VM_OBJECT_UNLOCK(bp->b_object); 3525} 3526 3527/* 3528 * vfs_bio_clrbuf: 3529 * 3530 * clear a buffer. This routine essentially fakes an I/O, so we need 3531 * to clear BIO_ERROR and B_INVAL. 3532 * 3533 * Note that while we only theoretically need to clear through b_bcount, 3534 * we go ahead and clear through b_bufsize. 3535 */ 3536 3537void 3538vfs_bio_clrbuf(struct buf *bp) 3539{ 3540 int i, j, mask = 0; 3541 caddr_t sa, ea; 3542 3543 GIANT_REQUIRED; 3544 3545 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) { 3546 clrbuf(bp); 3547 return; 3548 } 3549 bp->b_flags &= ~B_INVAL; 3550 bp->b_ioflags &= ~BIO_ERROR; 3551 VM_OBJECT_LOCK(bp->b_object); 3552 if( (bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) && 3553 (bp->b_offset & PAGE_MASK) == 0) { 3554 if (bp->b_pages[0] == bogus_page) 3555 goto unlock; 3556 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1; 3557 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED); 3558 if ((bp->b_pages[0]->valid & mask) == mask) 3559 goto unlock; 3560 if (((bp->b_pages[0]->flags & PG_ZERO) == 0) && 3561 ((bp->b_pages[0]->valid & mask) == 0)) { 3562 bzero(bp->b_data, bp->b_bufsize); 3563 bp->b_pages[0]->valid |= mask; 3564 goto unlock; 3565 } 3566 } 3567 ea = sa = bp->b_data; 3568 for(i = 0; i < bp->b_npages; i++, sa = ea) { 3569 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE); 3570 ea = (caddr_t)(vm_offset_t)ulmin( 3571 (u_long)(vm_offset_t)ea, 3572 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize); 3573 if (bp->b_pages[i] == bogus_page) 3574 continue; 3575 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE; 3576 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j; 3577 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED); 3578 if ((bp->b_pages[i]->valid & mask) == mask) 3579 continue; 3580 if ((bp->b_pages[i]->valid & mask) == 0) { 3581 if ((bp->b_pages[i]->flags & PG_ZERO) == 0) 3582 bzero(sa, ea - sa); 3583 } else { 3584 for (; sa < ea; sa += DEV_BSIZE, j++) { 3585 if (((bp->b_pages[i]->flags & PG_ZERO) == 0) && 3586 (bp->b_pages[i]->valid & (1<<j)) == 0) 3587 bzero(sa, DEV_BSIZE); 3588 } 3589 } 3590 bp->b_pages[i]->valid |= mask; 3591 } 3592unlock: 3593 VM_OBJECT_UNLOCK(bp->b_object); 3594 bp->b_resid = 0; 3595} 3596 3597/* 3598 * vm_hold_load_pages and vm_hold_free_pages get pages into 3599 * a buffers address space. The pages are anonymous and are 3600 * not associated with a file object. 3601 */ 3602static void 3603vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to) 3604{ 3605 vm_offset_t pg; 3606 vm_page_t p; 3607 int index; 3608 3609 to = round_page(to); 3610 from = round_page(from); 3611 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT; 3612 3613 VM_OBJECT_LOCK(kernel_object); 3614 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 3615tryagain: 3616 /* 3617 * note: must allocate system pages since blocking here 3618 * could intefere with paging I/O, no matter which 3619 * process we are. 3620 */ 3621 p = vm_page_alloc(kernel_object, 3622 ((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT), 3623 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED); 3624 if (!p) { 3625 atomic_add_int(&vm_pageout_deficit, 3626 (to - pg) >> PAGE_SHIFT); 3627 VM_OBJECT_UNLOCK(kernel_object); 3628 VM_WAIT; 3629 VM_OBJECT_LOCK(kernel_object); 3630 goto tryagain; 3631 } 3632 p->valid = VM_PAGE_BITS_ALL; 3633 pmap_qenter(pg, &p, 1); 3634 bp->b_pages[index] = p; 3635 vm_page_lock_queues(); 3636 vm_page_wakeup(p); 3637 vm_page_unlock_queues(); 3638 } 3639 VM_OBJECT_UNLOCK(kernel_object); 3640 bp->b_npages = index; 3641} 3642 3643/* Return pages associated with this buf to the vm system */ 3644static void 3645vm_hold_free_pages(struct buf *bp, vm_offset_t from, vm_offset_t to) 3646{ 3647 vm_offset_t pg; 3648 vm_page_t p; 3649 int index, newnpages; 3650 3651 GIANT_REQUIRED; 3652 3653 from = round_page(from); 3654 to = round_page(to); 3655 newnpages = index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT; 3656 3657 VM_OBJECT_LOCK(kernel_object); 3658 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 3659 p = bp->b_pages[index]; 3660 if (p && (index < bp->b_npages)) { 3661 if (p->busy) { 3662 printf( 3663 "vm_hold_free_pages: blkno: %jd, lblkno: %jd\n", 3664 (intmax_t)bp->b_blkno, 3665 (intmax_t)bp->b_lblkno); 3666 } 3667 bp->b_pages[index] = NULL; 3668 pmap_qremove(pg, 1); 3669 vm_page_lock_queues(); 3670 vm_page_busy(p); 3671 vm_page_unwire(p, 0); 3672 vm_page_free(p); 3673 vm_page_unlock_queues(); 3674 } 3675 } 3676 VM_OBJECT_UNLOCK(kernel_object); 3677 bp->b_npages = newnpages; 3678} 3679 3680/* 3681 * Map an IO request into kernel virtual address space. 3682 * 3683 * All requests are (re)mapped into kernel VA space. 3684 * Notice that we use b_bufsize for the size of the buffer 3685 * to be mapped. b_bcount might be modified by the driver. 3686 * 3687 * Note that even if the caller determines that the address space should 3688 * be valid, a race or a smaller-file mapped into a larger space may 3689 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST 3690 * check the return value. 3691 */ 3692int 3693vmapbuf(struct buf *bp) 3694{ 3695 caddr_t addr, kva; 3696 vm_prot_t prot; 3697 int pidx, i; 3698 struct vm_page *m; 3699 struct pmap *pmap = &curproc->p_vmspace->vm_pmap; 3700 3701 if (bp->b_bufsize < 0) 3702 return (-1); 3703 prot = VM_PROT_READ; 3704 if (bp->b_iocmd == BIO_READ) 3705 prot |= VM_PROT_WRITE; /* Less backwards than it looks */ 3706 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data), pidx = 0; 3707 addr < bp->b_data + bp->b_bufsize; 3708 addr += PAGE_SIZE, pidx++) { 3709 /* 3710 * Do the vm_fault if needed; do the copy-on-write thing 3711 * when reading stuff off device into memory. 3712 * 3713 * NOTE! Must use pmap_extract() because addr may be in 3714 * the userland address space, and kextract is only guarenteed 3715 * to work for the kernland address space (see: sparc64 port). 3716 */ 3717retry: 3718 if (vm_fault_quick(addr >= bp->b_data ? addr : bp->b_data, 3719 prot) < 0) { 3720 vm_page_lock_queues(); 3721 for (i = 0; i < pidx; ++i) { 3722 vm_page_unhold(bp->b_pages[i]); 3723 bp->b_pages[i] = NULL; 3724 } 3725 vm_page_unlock_queues(); 3726 return(-1); 3727 } 3728 m = pmap_extract_and_hold(pmap, (vm_offset_t)addr, prot); 3729 if (m == NULL) 3730 goto retry; 3731 bp->b_pages[pidx] = m; 3732 } 3733 if (pidx > btoc(MAXPHYS)) 3734 panic("vmapbuf: mapped more than MAXPHYS"); 3735 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx); 3736 3737 kva = bp->b_saveaddr; 3738 bp->b_npages = pidx; 3739 bp->b_saveaddr = bp->b_data; 3740 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 3741 return(0); 3742} 3743 3744/* 3745 * Free the io map PTEs associated with this IO operation. 3746 * We also invalidate the TLB entries and restore the original b_addr. 3747 */ 3748void 3749vunmapbuf(struct buf *bp) 3750{ 3751 int pidx; 3752 int npages; 3753 3754 npages = bp->b_npages; 3755 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages); 3756 vm_page_lock_queues(); 3757 for (pidx = 0; pidx < npages; pidx++) 3758 vm_page_unhold(bp->b_pages[pidx]); 3759 vm_page_unlock_queues(); 3760 3761 bp->b_data = bp->b_saveaddr; 3762} 3763 3764void 3765bdone(struct buf *bp) 3766{ 3767 3768 mtx_lock(&bdonelock); 3769 bp->b_flags |= B_DONE; 3770 wakeup(bp); 3771 mtx_unlock(&bdonelock); 3772} 3773 3774void 3775bwait(struct buf *bp, u_char pri, const char *wchan) 3776{ 3777 3778 mtx_lock(&bdonelock); 3779 while ((bp->b_flags & B_DONE) == 0) 3780 msleep(bp, &bdonelock, pri, wchan, 0); 3781 mtx_unlock(&bdonelock); 3782} 3783 3784void 3785bufstrategy(struct bufobj *bo, struct buf *bp) 3786{ 3787 int i = 0; 3788 struct vnode *vp; 3789 3790 vp = bp->b_vp; 3791#if 0 3792 KASSERT(vp == bo->bo_vnode, ("Inconsistent vnode bufstrategy")); 3793 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 3794 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp)); 3795#endif 3796 if (vp->v_type == VCHR) { 3797 if (!buf_prewrite(bp->b_vp, bp)) 3798 i = VOP_SPECSTRATEGY(vp, bp); 3799 } else { 3800 i = VOP_STRATEGY(vp, bp); 3801 } 3802 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp)); 3803} 3804 3805void 3806bufobj_wref(struct bufobj *bo) 3807{ 3808 3809 KASSERT(bo != NULL, ("NULL bo in bufobj_wref")); 3810 BO_LOCK(bo); 3811 bo->bo_numoutput++; 3812 BO_UNLOCK(bo); 3813} 3814 3815void 3816bufobj_wdrop(struct bufobj *bo) 3817{ 3818 3819 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop")); 3820 BO_LOCK(bo); 3821 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count")); 3822 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) { 3823 bo->bo_flag &= ~BO_WWAIT; 3824 wakeup(&bo->bo_numoutput); 3825 } 3826 BO_UNLOCK(bo); 3827} 3828 3829int 3830bufobj_wwait(struct bufobj *bo, int slpflag, int timeo) 3831{ 3832 int error; 3833 3834 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait")); 3835 ASSERT_BO_LOCKED(bo); 3836 error = 0; 3837 while (bo->bo_numoutput) { 3838 bo->bo_flag |= BO_WWAIT; 3839 error = msleep(&bo->bo_numoutput, BO_MTX(bo), 3840 slpflag | (PRIBIO + 1), "bo_wwait", timeo); 3841 if (error) 3842 break; 3843 } 3844 return (error); 3845} 3846 3847#include "opt_ddb.h" 3848#ifdef DDB 3849#include <ddb/ddb.h> 3850 3851/* DDB command to show buffer data */ 3852DB_SHOW_COMMAND(buffer, db_show_buffer) 3853{ 3854 /* get args */ 3855 struct buf *bp = (struct buf *)addr; 3856 3857 if (!have_addr) { 3858 db_printf("usage: show buffer <addr>\n"); 3859 return; 3860 } 3861 3862 db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS); 3863 db_printf( 3864 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n" 3865 "b_dev = (%d,%d), b_data = %p, b_blkno = %jd\n", 3866 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid, 3867 major(bp->b_dev), minor(bp->b_dev), bp->b_data, 3868 (intmax_t)bp->b_blkno); 3869 if (bp->b_npages) { 3870 int i; 3871 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages); 3872 for (i = 0; i < bp->b_npages; i++) { 3873 vm_page_t m; 3874 m = bp->b_pages[i]; 3875 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object, 3876 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m)); 3877 if ((i + 1) < bp->b_npages) 3878 db_printf(","); 3879 } 3880 db_printf("\n"); 3881 } 3882} 3883#endif /* DDB */ 3884