| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1994,1997 John S. Dyson | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice immediately at the beginning of the file, without modification, | |
| 10 | * this list of conditions, and the following disclaimer. | |
| 11 | * 2. Absolutely no warranty of function or purpose is made by the author | |
| 12 | * John S. Dyson. | |
| 13 | * | |
| 14 | * $FreeBSD: src/sys/kern/vfs_bio.c,v 1.242.2.20 2003/05/28 18:38:10 alc Exp $ | |
| ab2200aa | 15 | * $DragonFly: src/sys/kern/vfs_bio.c,v 1.115 2008/08/13 11:02:31 swildner Exp $ |
| 984263bc MD |
16 | */ |
| 17 | ||
| 18 | /* | |
| 19 | * this file contains a new buffer I/O scheme implementing a coherent | |
| 20 | * VM object and buffer cache scheme. Pains have been taken to make | |
| 21 | * sure that the performance degradation associated with schemes such | |
| 22 | * as this is not realized. | |
| 23 | * | |
| 24 | * Author: John S. Dyson | |
| 25 | * Significant help during the development and debugging phases | |
| 26 | * had been provided by David Greenman, also of the FreeBSD core team. | |
| 27 | * | |
| 28 | * see man buf(9) for more info. | |
| 29 | */ | |
| 30 | ||
| 31 | #include <sys/param.h> | |
| 32 | #include <sys/systm.h> | |
| 33 | #include <sys/buf.h> | |
| 34 | #include <sys/conf.h> | |
| 35 | #include <sys/eventhandler.h> | |
| 36 | #include <sys/lock.h> | |
| 37 | #include <sys/malloc.h> | |
| 38 | #include <sys/mount.h> | |
| 39 | #include <sys/kernel.h> | |
| 40 | #include <sys/kthread.h> | |
| 41 | #include <sys/proc.h> | |
| 42 | #include <sys/reboot.h> | |
| 43 | #include <sys/resourcevar.h> | |
| 44 | #include <sys/sysctl.h> | |
| 45 | #include <sys/vmmeter.h> | |
| 46 | #include <sys/vnode.h> | |
| 3020e3be | 47 | #include <sys/proc.h> |
| 984263bc MD |
48 | #include <vm/vm.h> |
| 49 | #include <vm/vm_param.h> | |
| 50 | #include <vm/vm_kern.h> | |
| 51 | #include <vm/vm_pageout.h> | |
| 52 | #include <vm/vm_page.h> | |
| 53 | #include <vm/vm_object.h> | |
| 54 | #include <vm/vm_extern.h> | |
| 55 | #include <vm/vm_map.h> | |
| 654a39f0 | 56 | |
| 3020e3be | 57 | #include <sys/buf2.h> |
| 654a39f0 | 58 | #include <sys/thread2.h> |
| f832287e | 59 | #include <sys/spinlock2.h> |
| 12e4aaff | 60 | #include <vm/vm_page2.h> |
| 984263bc | 61 | |
| 135bd6a8 MD |
62 | #include "opt_ddb.h" |
| 63 | #ifdef DDB | |
| 64 | #include <ddb/ddb.h> | |
| 65 | #endif | |
| 66 | ||
| b3098c79 HP |
67 | /* |
| 68 | * Buffer queues. | |
| 69 | */ | |
| b3098c79 HP |
70 | enum bufq_type { |
| 71 | BQUEUE_NONE, /* not on any queue */ | |
| 72 | BQUEUE_LOCKED, /* locked buffers */ | |
| 73 | BQUEUE_CLEAN, /* non-B_DELWRI buffers */ | |
| 74 | BQUEUE_DIRTY, /* B_DELWRI buffers */ | |
| 4b958e7b | 75 | BQUEUE_DIRTY_HW, /* B_DELWRI buffers - heavy weight */ |
| b3098c79 | 76 | BQUEUE_EMPTYKVA, /* empty buffer headers with KVA assignment */ |
| 4b958e7b MD |
77 | BQUEUE_EMPTY, /* empty buffer headers */ |
| 78 | ||
| 79 | BUFFER_QUEUES /* number of buffer queues */ | |
| b3098c79 | 80 | }; |
| 4b958e7b MD |
81 | |
| 82 | typedef enum bufq_type bufq_type_t; | |
| 83 | ||
| c4df9635 MD |
84 | #define BD_WAKE_SIZE 128 |
| 85 | #define BD_WAKE_MASK (BD_WAKE_SIZE - 1) | |
| 86 | ||
| b3098c79 HP |
87 | TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES]; |
| 88 | ||
| 984263bc MD |
89 | static MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer"); |
| 90 | ||
| 984263bc | 91 | struct buf *buf; /* buffer header pool */ |
| 984263bc | 92 | |
| 984263bc MD |
93 | static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, |
| 94 | int pageno, vm_page_t m); | |
| c8e4131d | 95 | static void vfs_clean_pages(struct buf *bp); |
| 984263bc MD |
96 | static void vfs_setdirty(struct buf *bp); |
| 97 | static void vfs_vmio_release(struct buf *bp); | |
| 4b958e7b | 98 | static int flushbufqueues(bufq_type_t q); |
| 4ecf7cc9 | 99 | static vm_page_t bio_page_alloc(vm_object_t obj, vm_pindex_t pg, int deficit); |
| 984263bc | 100 | |
| 868d24af | 101 | static void bd_signal(int totalspace); |
| 4b958e7b MD |
102 | static void buf_daemon(void); |
| 103 | static void buf_daemon_hw(void); | |
| c4df9635 | 104 | |
| 984263bc MD |
105 | /* |
| 106 | * bogus page -- for I/O to/from partially complete buffers | |
| 107 | * this is a temporary solution to the problem, but it is not | |
| 108 | * really that bad. it would be better to split the buffer | |
| 109 | * for input in the case of buffers partially already in memory, | |
| 110 | * but the code is intricate enough already. | |
| 111 | */ | |
| 112 | vm_page_t bogus_page; | |
| a0c36a34 | 113 | |
| 460426e6 MD |
114 | /* |
| 115 | * These are all static, but make the ones we export globals so we do | |
| 116 | * not need to use compiler magic. | |
| 117 | */ | |
| 118 | int bufspace, maxbufspace, | |
| 984263bc MD |
119 | bufmallocspace, maxbufmallocspace, lobufspace, hibufspace; |
| 120 | static int bufreusecnt, bufdefragcnt, buffreekvacnt; | |
| 984263bc | 121 | static int lorunningspace, hirunningspace, runningbufreq; |
| 868d24af | 122 | int dirtybufspace, dirtybufspacehw, lodirtybufspace, hidirtybufspace; |
| 70ac7d6c | 123 | int dirtybufcount, dirtybufcounthw; |
| 1b30fbcc | 124 | int runningbufspace, runningbufcount; |
| 984263bc MD |
125 | static int getnewbufcalls; |
| 126 | static int getnewbufrestarts; | |
| 4ecf7cc9 | 127 | static int recoverbufcalls; |
| f832287e MD |
128 | static int needsbuffer; /* locked by needsbuffer_spin */ |
| 129 | static int bd_request; /* locked by needsbuffer_spin */ | |
| 4b958e7b | 130 | static int bd_request_hw; /* locked by needsbuffer_spin */ |
| c4df9635 MD |
131 | static u_int bd_wake_ary[BD_WAKE_SIZE]; |
| 132 | static u_int bd_wake_index; | |
| f832287e MD |
133 | static struct spinlock needsbuffer_spin; |
| 134 | ||
| 4ecf7cc9 MD |
135 | static struct thread *bufdaemon_td; |
| 136 | static struct thread *bufdaemonhw_td; | |
| 137 | ||
| 138 | ||
| 3f779080 HP |
139 | /* |
| 140 | * Sysctls for operational control of the buffer cache. | |
| 141 | */ | |
| 868d24af | 142 | SYSCTL_INT(_vfs, OID_AUTO, lodirtybufspace, CTLFLAG_RW, &lodirtybufspace, 0, |
| 3f779080 | 143 | "Number of dirty buffers to flush before bufdaemon becomes inactive"); |
| 868d24af | 144 | SYSCTL_INT(_vfs, OID_AUTO, hidirtybufspace, CTLFLAG_RW, &hidirtybufspace, 0, |
| bb606263 | 145 | "High watermark used to trigger explicit flushing of dirty buffers"); |
| 3f779080 HP |
146 | SYSCTL_INT(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0, |
| 147 | "Minimum amount of buffer space required for active I/O"); | |
| 148 | SYSCTL_INT(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0, | |
| 149 | "Maximum amount of buffer space to usable for active I/O"); | |
| 3f779080 HP |
150 | /* |
| 151 | * Sysctls determining current state of the buffer cache. | |
| 152 | */ | |
| 17cde63e MD |
153 | SYSCTL_INT(_vfs, OID_AUTO, nbuf, CTLFLAG_RD, &nbuf, 0, |
| 154 | "Total number of buffers in buffer cache"); | |
| 868d24af | 155 | SYSCTL_INT(_vfs, OID_AUTO, dirtybufspace, CTLFLAG_RD, &dirtybufspace, 0, |
| 70ac7d6c | 156 | "Pending bytes of dirty buffers (all)"); |
| 868d24af | 157 | SYSCTL_INT(_vfs, OID_AUTO, dirtybufspacehw, CTLFLAG_RD, &dirtybufspacehw, 0, |
| 70ac7d6c MD |
158 | "Pending bytes of dirty buffers (heavy weight)"); |
| 159 | SYSCTL_INT(_vfs, OID_AUTO, dirtybufcount, CTLFLAG_RD, &dirtybufcount, 0, | |
| 160 | "Pending number of dirty buffers"); | |
| 161 | SYSCTL_INT(_vfs, OID_AUTO, dirtybufcounthw, CTLFLAG_RD, &dirtybufcounthw, 0, | |
| 4b958e7b | 162 | "Pending number of dirty buffers (heavy weight)"); |
| 3f779080 | 163 | SYSCTL_INT(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0, |
| bb606263 | 164 | "I/O bytes currently in progress due to asynchronous writes"); |
| 1b30fbcc MD |
165 | SYSCTL_INT(_vfs, OID_AUTO, runningbufcount, CTLFLAG_RD, &runningbufcount, 0, |
| 166 | "I/O buffers currently in progress due to asynchronous writes"); | |
| 3f779080 HP |
167 | SYSCTL_INT(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0, |
| 168 | "Hard limit on maximum amount of memory usable for buffer space"); | |
| 169 | SYSCTL_INT(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0, | |
| 170 | "Soft limit on maximum amount of memory usable for buffer space"); | |
| 171 | SYSCTL_INT(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0, | |
| 172 | "Minimum amount of memory to reserve for system buffer space"); | |
| 173 | SYSCTL_INT(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0, | |
| 174 | "Amount of memory available for buffers"); | |
| 175 | SYSCTL_INT(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RD, &maxbufmallocspace, | |
| bb606263 | 176 | 0, "Maximum amount of memory reserved for buffers using malloc"); |
| 3f779080 HP |
177 | SYSCTL_INT(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0, |
| 178 | "Amount of memory left for buffers using malloc-scheme"); | |
| 179 | SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RD, &getnewbufcalls, 0, | |
| 180 | "New buffer header acquisition requests"); | |
| 181 | SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RD, &getnewbufrestarts, | |
| 182 | 0, "New buffer header acquisition restarts"); | |
| 4ecf7cc9 MD |
183 | SYSCTL_INT(_vfs, OID_AUTO, recoverbufcalls, CTLFLAG_RD, &recoverbufcalls, 0, |
| 184 | "Recover VM space in an emergency"); | |
| 3f779080 | 185 | SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RD, &bufdefragcnt, 0, |
| bb606263 | 186 | "Buffer acquisition restarts due to fragmented buffer map"); |
| 3f779080 HP |
187 | SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RD, &buffreekvacnt, 0, |
| 188 | "Amount of time KVA space was deallocated in an arbitrary buffer"); | |
| 189 | SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RD, &bufreusecnt, 0, | |
| 190 | "Amount of time buffer re-use operations were successful"); | |
| 306ab3cb HP |
191 | SYSCTL_INT(_debug_sizeof, OID_AUTO, buf, CTLFLAG_RD, 0, sizeof(struct buf), |
| 192 | "sizeof(struct buf)"); | |
| 984263bc | 193 | |
| 984263bc MD |
194 | char *buf_wmesg = BUF_WMESG; |
| 195 | ||
| 196 | extern int vm_swap_size; | |
| 197 | ||
| 198 | #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */ | |
| c4df9635 | 199 | #define VFS_BIO_NEED_UNUSED02 0x02 |
| 868d24af | 200 | #define VFS_BIO_NEED_UNUSED04 0x04 |
| 984263bc MD |
201 | #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */ |
| 202 | ||
| 203 | /* | |
| 3f779080 | 204 | * bufspacewakeup: |
| 984263bc MD |
205 | * |
| 206 | * Called when buffer space is potentially available for recovery. | |
| 207 | * getnewbuf() will block on this flag when it is unable to free | |
| 208 | * sufficient buffer space. Buffer space becomes recoverable when | |
| 209 | * bp's get placed back in the queues. | |
| 210 | */ | |
| 211 | ||
| 212 | static __inline void | |
| 213 | bufspacewakeup(void) | |
| 214 | { | |
| 215 | /* | |
| 216 | * If someone is waiting for BUF space, wake them up. Even | |
| 217 | * though we haven't freed the kva space yet, the waiting | |
| 218 | * process will be able to now. | |
| 219 | */ | |
| 220 | if (needsbuffer & VFS_BIO_NEED_BUFSPACE) { | |
| f832287e | 221 | spin_lock_wr(&needsbuffer_spin); |
| 984263bc | 222 | needsbuffer &= ~VFS_BIO_NEED_BUFSPACE; |
| f832287e | 223 | spin_unlock_wr(&needsbuffer_spin); |
| 984263bc MD |
224 | wakeup(&needsbuffer); |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | /* | |
| 3f779080 HP |
229 | * runningbufwakeup: |
| 230 | * | |
| 231 | * Accounting for I/O in progress. | |
| 984263bc MD |
232 | * |
| 233 | */ | |
| 234 | static __inline void | |
| 235 | runningbufwakeup(struct buf *bp) | |
| 236 | { | |
| 868d24af MD |
237 | int totalspace; |
| 238 | ||
| 239 | if ((totalspace = bp->b_runningbufspace) != 0) { | |
| 240 | runningbufspace -= totalspace; | |
| 1b30fbcc | 241 | --runningbufcount; |
| 984263bc MD |
242 | bp->b_runningbufspace = 0; |
| 243 | if (runningbufreq && runningbufspace <= lorunningspace) { | |
| 244 | runningbufreq = 0; | |
| 245 | wakeup(&runningbufreq); | |
| 246 | } | |
| 868d24af | 247 | bd_signal(totalspace); |
| 984263bc MD |
248 | } |
| 249 | } | |
| 250 | ||
| 251 | /* | |
| 3f779080 | 252 | * bufcountwakeup: |
| 984263bc MD |
253 | * |
| 254 | * Called when a buffer has been added to one of the free queues to | |
| 255 | * account for the buffer and to wakeup anyone waiting for free buffers. | |
| 256 | * This typically occurs when large amounts of metadata are being handled | |
| 257 | * by the buffer cache ( else buffer space runs out first, usually ). | |
| 258 | */ | |
| 259 | ||
| 260 | static __inline void | |
| 261 | bufcountwakeup(void) | |
| 262 | { | |
| 984263bc | 263 | if (needsbuffer) { |
| f832287e | 264 | spin_lock_wr(&needsbuffer_spin); |
| 984263bc | 265 | needsbuffer &= ~VFS_BIO_NEED_ANY; |
| f832287e | 266 | spin_unlock_wr(&needsbuffer_spin); |
| 984263bc MD |
267 | wakeup(&needsbuffer); |
| 268 | } | |
| 269 | } | |
| 270 | ||
| 271 | /* | |
| 3f779080 | 272 | * waitrunningbufspace() |
| 984263bc | 273 | * |
| cd083340 | 274 | * Wait for the amount of running I/O to drop to a reasonable level. |
| 984263bc | 275 | * |
| cd083340 MD |
276 | * The caller may be using this function to block in a tight loop, we |
| 277 | * must block of runningbufspace is greater then the passed limit. | |
| 278 | * And even with that it may not be enough, due to the presence of | |
| 279 | * B_LOCKED dirty buffers, so also wait for at least one running buffer | |
| 280 | * to complete. | |
| 984263bc MD |
281 | */ |
| 282 | static __inline void | |
| cd083340 | 283 | waitrunningbufspace(int limit) |
| 984263bc | 284 | { |
| cd083340 MD |
285 | int lorun; |
| 286 | ||
| 287 | if (lorunningspace < limit) | |
| 288 | lorun = lorunningspace; | |
| 289 | else | |
| 290 | lorun = limit; | |
| 291 | ||
| 292 | crit_enter(); | |
| 293 | if (runningbufspace > lorun) { | |
| 294 | while (runningbufspace > lorun) { | |
| e43a034f MD |
295 | ++runningbufreq; |
| 296 | tsleep(&runningbufreq, 0, "wdrain", 0); | |
| 297 | } | |
| cd083340 MD |
298 | } else if (runningbufspace) { |
| 299 | ++runningbufreq; | |
| 300 | tsleep(&runningbufreq, 0, "wdrain2", 1); | |
| 984263bc | 301 | } |
| cd083340 | 302 | crit_exit(); |
| 984263bc MD |
303 | } |
| 304 | ||
| 305 | /* | |
| 3f779080 | 306 | * vfs_buf_test_cache: |
| 984263bc MD |
307 | * |
| 308 | * Called when a buffer is extended. This function clears the B_CACHE | |
| 309 | * bit if the newly extended portion of the buffer does not contain | |
| 310 | * valid data. | |
| 311 | */ | |
| 312 | static __inline__ | |
| 313 | void | |
| 314 | vfs_buf_test_cache(struct buf *bp, | |
| 315 | vm_ooffset_t foff, vm_offset_t off, vm_offset_t size, | |
| 316 | vm_page_t m) | |
| 317 | { | |
| 318 | if (bp->b_flags & B_CACHE) { | |
| 319 | int base = (foff + off) & PAGE_MASK; | |
| 320 | if (vm_page_is_valid(m, base, size) == 0) | |
| 321 | bp->b_flags &= ~B_CACHE; | |
| 322 | } | |
| 323 | } | |
| 324 | ||
| 3f779080 | 325 | /* |
| cd083340 | 326 | * bd_speedup() |
| 4b958e7b | 327 | * |
| cd083340 MD |
328 | * Spank the buf_daemon[_hw] if the total dirty buffer space exceeds the |
| 329 | * low water mark. | |
| 3f779080 | 330 | */ |
| 984263bc MD |
331 | static __inline__ |
| 332 | void | |
| c4df9635 | 333 | bd_speedup(void) |
| 984263bc | 334 | { |
| 70ac7d6c | 335 | if (dirtybufspace < lodirtybufspace && dirtybufcount < nbuf / 2) |
| cd083340 MD |
336 | return; |
| 337 | ||
| 338 | if (bd_request == 0 && | |
| 70ac7d6c MD |
339 | (dirtybufspace - dirtybufspacehw > lodirtybufspace / 2 || |
| 340 | dirtybufcount - dirtybufcounthw >= nbuf / 2)) { | |
| f832287e | 341 | spin_lock_wr(&needsbuffer_spin); |
| 984263bc | 342 | bd_request = 1; |
| f832287e | 343 | spin_unlock_wr(&needsbuffer_spin); |
| 984263bc MD |
344 | wakeup(&bd_request); |
| 345 | } | |
| cd083340 | 346 | if (bd_request_hw == 0 && |
| 70ac7d6c MD |
347 | (dirtybufspacehw > lodirtybufspace / 2 || |
| 348 | dirtybufcounthw >= nbuf / 2)) { | |
| 4b958e7b MD |
349 | spin_lock_wr(&needsbuffer_spin); |
| 350 | bd_request_hw = 1; | |
| 351 | spin_unlock_wr(&needsbuffer_spin); | |
| 352 | wakeup(&bd_request_hw); | |
| 353 | } | |
| 984263bc MD |
354 | } |
| 355 | ||
| 356 | /* | |
| c4df9635 | 357 | * bd_heatup() |
| 3f779080 | 358 | * |
| c4df9635 MD |
359 | * Get the buf_daemon heated up when the number of running and dirty |
| 360 | * buffers exceeds the mid-point. | |
| 984263bc | 361 | */ |
| c4df9635 MD |
362 | int |
| 363 | bd_heatup(void) | |
| 364 | { | |
| 365 | int mid1; | |
| 366 | int mid2; | |
| 868d24af | 367 | int totalspace; |
| 984263bc | 368 | |
| 868d24af | 369 | mid1 = lodirtybufspace + (hidirtybufspace - lodirtybufspace) / 2; |
| c4df9635 | 370 | |
| 868d24af | 371 | totalspace = runningbufspace + dirtybufspace; |
| 70ac7d6c | 372 | if (totalspace >= mid1 || dirtybufcount >= nbuf / 2) { |
| c4df9635 | 373 | bd_speedup(); |
| 868d24af MD |
374 | mid2 = mid1 + (hidirtybufspace - mid1) / 2; |
| 375 | if (totalspace >= mid2) | |
| 376 | return(totalspace - mid2); | |
| c4df9635 MD |
377 | } |
| 378 | return(0); | |
| 379 | } | |
| 380 | ||
| 381 | /* | |
| 382 | * bd_wait() | |
| 383 | * | |
| 868d24af MD |
384 | * Wait for the buffer cache to flush (totalspace) bytes worth of |
| 385 | * buffers, then return. | |
| c4df9635 MD |
386 | * |
| 387 | * Regardless this function blocks while the number of dirty buffers | |
| 868d24af | 388 | * exceeds hidirtybufspace. |
| c4df9635 | 389 | */ |
| 984263bc | 390 | void |
| 868d24af | 391 | bd_wait(int totalspace) |
| 984263bc | 392 | { |
| c4df9635 | 393 | u_int i; |
| 868d24af | 394 | int count; |
| c4df9635 | 395 | |
| 4ecf7cc9 MD |
396 | if (curthread == bufdaemonhw_td || curthread == bufdaemon_td) |
| 397 | return; | |
| 398 | ||
| 868d24af | 399 | while (totalspace > 0) { |
| c4df9635 MD |
400 | bd_heatup(); |
| 401 | crit_enter(); | |
| 868d24af MD |
402 | if (totalspace > runningbufspace + dirtybufspace) |
| 403 | totalspace = runningbufspace + dirtybufspace; | |
| 404 | count = totalspace / BKVASIZE; | |
| c4df9635 MD |
405 | if (count >= BD_WAKE_SIZE) |
| 406 | count = BD_WAKE_SIZE - 1; | |
| 407 | i = (bd_wake_index + count) & BD_WAKE_MASK; | |
| 408 | ++bd_wake_ary[i]; | |
| 409 | tsleep(&bd_wake_ary[i], 0, "flstik", hz); | |
| 410 | crit_exit(); | |
| 411 | ||
| 868d24af | 412 | totalspace = runningbufspace + dirtybufspace - hidirtybufspace; |
| c4df9635 MD |
413 | } |
| 414 | } | |
| 415 | ||
| 416 | /* | |
| 417 | * bd_signal() | |
| 418 | * | |
| 868d24af MD |
419 | * This function is called whenever runningbufspace or dirtybufspace |
| 420 | * is reduced. Track threads waiting for run+dirty buffer I/O | |
| c4df9635 MD |
421 | * complete. |
| 422 | */ | |
| 423 | static void | |
| 868d24af | 424 | bd_signal(int totalspace) |
| c4df9635 MD |
425 | { |
| 426 | u_int i; | |
| 427 | ||
| 868d24af MD |
428 | while (totalspace > 0) { |
| 429 | i = atomic_fetchadd_int(&bd_wake_index, 1); | |
| 430 | i &= BD_WAKE_MASK; | |
| 431 | if (bd_wake_ary[i]) { | |
| 432 | bd_wake_ary[i] = 0; | |
| 433 | wakeup(&bd_wake_ary[i]); | |
| 434 | } | |
| 435 | totalspace -= BKVASIZE; | |
| c4df9635 | 436 | } |
| 984263bc MD |
437 | } |
| 438 | ||
| 439 | /* | |
| 3f779080 HP |
440 | * bufinit: |
| 441 | * | |
| 442 | * Load time initialisation of the buffer cache, called from machine | |
| 443 | * dependant initialization code. | |
| 444 | */ | |
| 984263bc MD |
445 | void |
| 446 | bufinit(void) | |
| 447 | { | |
| 448 | struct buf *bp; | |
| b8bb0773 | 449 | vm_offset_t bogus_offset; |
| 984263bc MD |
450 | int i; |
| 451 | ||
| f832287e MD |
452 | spin_init(&needsbuffer_spin); |
| 453 | ||
| 984263bc MD |
454 | /* next, make a null set of free lists */ |
| 455 | for (i = 0; i < BUFFER_QUEUES; i++) | |
| 456 | TAILQ_INIT(&bufqueues[i]); | |
| 457 | ||
| 458 | /* finally, initialize each buffer header and stick on empty q */ | |
| 459 | for (i = 0; i < nbuf; i++) { | |
| 460 | bp = &buf[i]; | |
| 461 | bzero(bp, sizeof *bp); | |
| 462 | bp->b_flags = B_INVAL; /* we're just an empty header */ | |
| 10f3fee5 | 463 | bp->b_cmd = BUF_CMD_DONE; |
| b3098c79 | 464 | bp->b_qindex = BQUEUE_EMPTY; |
| 81b5c339 | 465 | initbufbio(bp); |
| 54f51aeb | 466 | xio_init(&bp->b_xio); |
| 408357d8 | 467 | buf_dep_init(bp); |
| 984263bc | 468 | BUF_LOCKINIT(bp); |
| b3098c79 | 469 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_EMPTY], bp, b_freelist); |
| 984263bc MD |
470 | } |
| 471 | ||
| 472 | /* | |
| 473 | * maxbufspace is the absolute maximum amount of buffer space we are | |
| 474 | * allowed to reserve in KVM and in real terms. The absolute maximum | |
| 475 | * is nominally used by buf_daemon. hibufspace is the nominal maximum | |
| 476 | * used by most other processes. The differential is required to | |
| 477 | * ensure that buf_daemon is able to run when other processes might | |
| 478 | * be blocked waiting for buffer space. | |
| 479 | * | |
| 480 | * maxbufspace is based on BKVASIZE. Allocating buffers larger then | |
| 481 | * this may result in KVM fragmentation which is not handled optimally | |
| 482 | * by the system. | |
| 483 | */ | |
| 484 | maxbufspace = nbuf * BKVASIZE; | |
| 485 | hibufspace = imax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10); | |
| 486 | lobufspace = hibufspace - MAXBSIZE; | |
| 487 | ||
| 488 | lorunningspace = 512 * 1024; | |
| 489 | hirunningspace = 1024 * 1024; | |
| 490 | ||
| 868d24af MD |
491 | /* |
| 492 | * Limit the amount of malloc memory since it is wired permanently | |
| 493 | * into the kernel space. Even though this is accounted for in | |
| 494 | * the buffer allocation, we don't want the malloced region to grow | |
| 495 | * uncontrolled. The malloc scheme improves memory utilization | |
| 496 | * significantly on average (small) directories. | |
| 497 | */ | |
| 984263bc MD |
498 | maxbufmallocspace = hibufspace / 20; |
| 499 | ||
| 868d24af MD |
500 | /* |
| 501 | * Reduce the chance of a deadlock occuring by limiting the number | |
| 502 | * of delayed-write dirty buffers we allow to stack up. | |
| 503 | */ | |
| 504 | hidirtybufspace = hibufspace / 2; | |
| 505 | dirtybufspace = 0; | |
| 506 | dirtybufspacehw = 0; | |
| 984263bc | 507 | |
| 868d24af | 508 | lodirtybufspace = hidirtybufspace / 2; |
| 984263bc | 509 | |
| 868d24af MD |
510 | /* |
| 511 | * Maximum number of async ops initiated per buf_daemon loop. This is | |
| 512 | * somewhat of a hack at the moment, we really need to limit ourselves | |
| 513 | * based on the number of bytes of I/O in-transit that were initiated | |
| 514 | * from buf_daemon. | |
| 515 | */ | |
| 984263bc | 516 | |
| e4846942 | 517 | bogus_offset = kmem_alloc_pageable(&kernel_map, PAGE_SIZE); |
| c439ad8f | 518 | bogus_page = vm_page_alloc(&kernel_object, |
| e4846942 MD |
519 | (bogus_offset >> PAGE_SHIFT), |
| 520 | VM_ALLOC_NORMAL); | |
| 12e4aaff | 521 | vmstats.v_wire_count++; |
| 984263bc MD |
522 | |
| 523 | } | |
| 524 | ||
| 525 | /* | |
| 81b5c339 MD |
526 | * Initialize the embedded bio structures |
| 527 | */ | |
| 528 | void | |
| 529 | initbufbio(struct buf *bp) | |
| 530 | { | |
| 531 | bp->b_bio1.bio_buf = bp; | |
| 532 | bp->b_bio1.bio_prev = NULL; | |
| 81b5c339 MD |
533 | bp->b_bio1.bio_offset = NOOFFSET; |
| 534 | bp->b_bio1.bio_next = &bp->b_bio2; | |
| 535 | bp->b_bio1.bio_done = NULL; | |
| 536 | ||
| 537 | bp->b_bio2.bio_buf = bp; | |
| 538 | bp->b_bio2.bio_prev = &bp->b_bio1; | |
| 81b5c339 MD |
539 | bp->b_bio2.bio_offset = NOOFFSET; |
| 540 | bp->b_bio2.bio_next = NULL; | |
| 541 | bp->b_bio2.bio_done = NULL; | |
| 542 | } | |
| 543 | ||
| 544 | /* | |
| 545 | * Reinitialize the embedded bio structures as well as any additional | |
| 546 | * translation cache layers. | |
| 547 | */ | |
| 548 | void | |
| 549 | reinitbufbio(struct buf *bp) | |
| 550 | { | |
| 551 | struct bio *bio; | |
| 552 | ||
| 553 | for (bio = &bp->b_bio1; bio; bio = bio->bio_next) { | |
| 554 | bio->bio_done = NULL; | |
| 81b5c339 MD |
555 | bio->bio_offset = NOOFFSET; |
| 556 | } | |
| 557 | } | |
| 558 | ||
| 559 | /* | |
| 560 | * Push another BIO layer onto an existing BIO and return it. The new | |
| 561 | * BIO layer may already exist, holding cached translation data. | |
| 562 | */ | |
| 563 | struct bio * | |
| 564 | push_bio(struct bio *bio) | |
| 565 | { | |
| 566 | struct bio *nbio; | |
| 567 | ||
| 568 | if ((nbio = bio->bio_next) == NULL) { | |
| 569 | int index = bio - &bio->bio_buf->b_bio_array[0]; | |
| bbd44c71 | 570 | if (index >= NBUF_BIO - 1) { |
| 81b5c339 MD |
571 | panic("push_bio: too many layers bp %p\n", |
| 572 | bio->bio_buf); | |
| 573 | } | |
| 574 | nbio = &bio->bio_buf->b_bio_array[index + 1]; | |
| 575 | bio->bio_next = nbio; | |
| 576 | nbio->bio_prev = bio; | |
| 577 | nbio->bio_buf = bio->bio_buf; | |
| 81b5c339 MD |
578 | nbio->bio_offset = NOOFFSET; |
| 579 | nbio->bio_done = NULL; | |
| 580 | nbio->bio_next = NULL; | |
| 581 | } | |
| 582 | KKASSERT(nbio->bio_done == NULL); | |
| 583 | return(nbio); | |
| 584 | } | |
| 585 | ||
| b77cfc40 MD |
586 | /* |
| 587 | * Pop a BIO translation layer, returning the previous layer. The | |
| 588 | * must have been previously pushed. | |
| 589 | */ | |
| 590 | struct bio * | |
| 81b5c339 MD |
591 | pop_bio(struct bio *bio) |
| 592 | { | |
| b77cfc40 | 593 | return(bio->bio_prev); |
| 81b5c339 MD |
594 | } |
| 595 | ||
| 596 | void | |
| 597 | clearbiocache(struct bio *bio) | |
| 598 | { | |
| 599 | while (bio) { | |
| 81b5c339 MD |
600 | bio->bio_offset = NOOFFSET; |
| 601 | bio = bio->bio_next; | |
| 602 | } | |
| 603 | } | |
| 604 | ||
| 605 | /* | |
| 3f779080 HP |
606 | * bfreekva: |
| 607 | * | |
| 608 | * Free the KVA allocation for buffer 'bp'. | |
| 984263bc | 609 | * |
| e43a034f | 610 | * Must be called from a critical section as this is the only locking for |
| 984263bc MD |
611 | * buffer_map. |
| 612 | * | |
| 613 | * Since this call frees up buffer space, we call bufspacewakeup(). | |
| 614 | */ | |
| 615 | static void | |
| 312dcd01 | 616 | bfreekva(struct buf *bp) |
| 984263bc | 617 | { |
| a108bf71 MD |
618 | int count; |
| 619 | ||
| 984263bc MD |
620 | if (bp->b_kvasize) { |
| 621 | ++buffreekvacnt; | |
| a108bf71 | 622 | count = vm_map_entry_reserve(MAP_RESERVE_COUNT); |
| e4846942 | 623 | vm_map_lock(&buffer_map); |
| 984263bc | 624 | bufspace -= bp->b_kvasize; |
| e4846942 | 625 | vm_map_delete(&buffer_map, |
| 984263bc | 626 | (vm_offset_t) bp->b_kvabase, |
| a108bf71 MD |
627 | (vm_offset_t) bp->b_kvabase + bp->b_kvasize, |
| 628 | &count | |
| 984263bc | 629 | ); |
| e4846942 | 630 | vm_map_unlock(&buffer_map); |
| a108bf71 | 631 | vm_map_entry_release(count); |
| 984263bc MD |
632 | bp->b_kvasize = 0; |
| 633 | bufspacewakeup(); | |
| 634 | } | |
| 635 | } | |
| 636 | ||
| 637 | /* | |
| 3f779080 | 638 | * bremfree: |
| 984263bc MD |
639 | * |
| 640 | * Remove the buffer from the appropriate free list. | |
| 641 | */ | |
| 642 | void | |
| c8e4131d | 643 | bremfree(struct buf *bp) |
| 984263bc | 644 | { |
| e43a034f | 645 | crit_enter(); |
| 984263bc | 646 | |
| b3098c79 | 647 | if (bp->b_qindex != BQUEUE_NONE) { |
| 77bb9400 MD |
648 | KASSERT(BUF_REFCNTNB(bp) == 1, |
| 649 | ("bremfree: bp %p not locked",bp)); | |
| 984263bc | 650 | TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist); |
| b3098c79 | 651 | bp->b_qindex = BQUEUE_NONE; |
| 984263bc | 652 | } else { |
| 77bb9400 | 653 | if (BUF_REFCNTNB(bp) <= 1) |
| 984263bc MD |
654 | panic("bremfree: removing a buffer not on a queue"); |
| 655 | } | |
| 656 | ||
| e43a034f | 657 | crit_exit(); |
| 984263bc MD |
658 | } |
| 659 | ||
| 660 | ||
| 661 | /* | |
| 3f779080 HP |
662 | * bread: |
| 663 | * | |
| 664 | * Get a buffer with the specified data. Look in the cache first. We | |
| 665 | * must clear B_ERROR and B_INVAL prior to initiating I/O. If B_CACHE | |
| 666 | * is set, the buffer is valid and we do not have to do anything ( see | |
| 667 | * getblk() ). | |
| 984263bc MD |
668 | */ |
| 669 | int | |
| c8e4131d | 670 | bread(struct vnode *vp, off_t loffset, int size, struct buf **bpp) |
| 984263bc MD |
671 | { |
| 672 | struct buf *bp; | |
| 673 | ||
| 54078292 | 674 | bp = getblk(vp, loffset, size, 0, 0); |
| 984263bc MD |
675 | *bpp = bp; |
| 676 | ||
| 677 | /* if not found in cache, do some I/O */ | |
| 678 | if ((bp->b_flags & B_CACHE) == 0) { | |
| 78a9b77f MD |
679 | KASSERT(!(bp->b_flags & B_ASYNC), |
| 680 | ("bread: illegal async bp %p", bp)); | |
| 984263bc | 681 | bp->b_flags &= ~(B_ERROR | B_INVAL); |
| 10f3fee5 MD |
682 | bp->b_cmd = BUF_CMD_READ; |
| 683 | vfs_busy_pages(vp, bp); | |
| 81b5c339 | 684 | vn_strategy(vp, &bp->b_bio1); |
| 984263bc MD |
685 | return (biowait(bp)); |
| 686 | } | |
| 687 | return (0); | |
| 688 | } | |
| 689 | ||
| 690 | /* | |
| 3f779080 HP |
691 | * breadn: |
| 692 | * | |
| 693 | * Operates like bread, but also starts asynchronous I/O on | |
| 694 | * read-ahead blocks. We must clear B_ERROR and B_INVAL prior | |
| 695 | * to initiating I/O . If B_CACHE is set, the buffer is valid | |
| 696 | * and we do not have to do anything. | |
| 984263bc MD |
697 | */ |
| 698 | int | |
| a8f169e2 | 699 | breadn(struct vnode *vp, off_t loffset, int size, off_t *raoffset, |
| c8e4131d | 700 | int *rabsize, int cnt, struct buf **bpp) |
| 984263bc MD |
701 | { |
| 702 | struct buf *bp, *rabp; | |
| 703 | int i; | |
| 704 | int rv = 0, readwait = 0; | |
| 705 | ||
| 54078292 | 706 | *bpp = bp = getblk(vp, loffset, size, 0, 0); |
| 984263bc MD |
707 | |
| 708 | /* if not found in cache, do some I/O */ | |
| 709 | if ((bp->b_flags & B_CACHE) == 0) { | |
| 984263bc | 710 | bp->b_flags &= ~(B_ERROR | B_INVAL); |
| 10f3fee5 MD |
711 | bp->b_cmd = BUF_CMD_READ; |
| 712 | vfs_busy_pages(vp, bp); | |
| 81b5c339 | 713 | vn_strategy(vp, &bp->b_bio1); |
| 984263bc MD |
714 | ++readwait; |
| 715 | } | |
| 716 | ||
| 54078292 MD |
717 | for (i = 0; i < cnt; i++, raoffset++, rabsize++) { |
| 718 | if (inmem(vp, *raoffset)) | |
| 984263bc | 719 | continue; |
| 54078292 | 720 | rabp = getblk(vp, *raoffset, *rabsize, 0, 0); |
| 984263bc MD |
721 | |
| 722 | if ((rabp->b_flags & B_CACHE) == 0) { | |
| 10f3fee5 | 723 | rabp->b_flags |= B_ASYNC; |
| 984263bc | 724 | rabp->b_flags &= ~(B_ERROR | B_INVAL); |
| 10f3fee5 MD |
725 | rabp->b_cmd = BUF_CMD_READ; |
| 726 | vfs_busy_pages(vp, rabp); | |
| 984263bc | 727 | BUF_KERNPROC(rabp); |
| 81b5c339 | 728 | vn_strategy(vp, &rabp->b_bio1); |
| 984263bc MD |
729 | } else { |
| 730 | brelse(rabp); | |
| 731 | } | |
| 732 | } | |
| 733 | ||
| 734 | if (readwait) { | |
| 735 | rv = biowait(bp); | |
| 736 | } | |
| 737 | return (rv); | |
| 738 | } | |
| 739 | ||
| 740 | /* | |
| 3f779080 HP |
741 | * bwrite: |
| 742 | * | |
| 743 | * Write, release buffer on completion. (Done by iodone | |
| 744 | * if async). Do not bother writing anything if the buffer | |
| 745 | * is invalid. | |
| 746 | * | |
| 747 | * Note that we set B_CACHE here, indicating that buffer is | |
| 748 | * fully valid and thus cacheable. This is true even of NFS | |
| 749 | * now so we set it generally. This could be set either here | |
| 750 | * or in biodone() since the I/O is synchronous. We put it | |
| 751 | * here. | |
| 984263bc MD |
752 | */ |
| 753 | int | |
| c8e4131d | 754 | bwrite(struct buf *bp) |
| 984263bc | 755 | { |
| e43a034f | 756 | int oldflags; |
| 984263bc MD |
757 | |
| 758 | if (bp->b_flags & B_INVAL) { | |
| 759 | brelse(bp); | |
| 760 | return (0); | |
| 761 | } | |
| 762 | ||
| 763 | oldflags = bp->b_flags; | |
| 764 | ||
| 77bb9400 | 765 | if (BUF_REFCNTNB(bp) == 0) |
| 984263bc | 766 | panic("bwrite: buffer is not busy???"); |
| e43a034f | 767 | crit_enter(); |
| 984263bc MD |
768 | |
| 769 | /* Mark the buffer clean */ | |
| 770 | bundirty(bp); | |
| 771 | ||
| 10f3fee5 | 772 | bp->b_flags &= ~B_ERROR; |
| 6bae6177 | 773 | bp->b_flags |= B_CACHE; |
| 10f3fee5 MD |
774 | bp->b_cmd = BUF_CMD_WRITE; |
| 775 | vfs_busy_pages(bp->b_vp, bp); | |
| 984263bc MD |
776 | |
| 777 | /* | |
| 9a71d53f MD |
778 | * Normal bwrites pipeline writes. NOTE: b_bufsize is only |
| 779 | * valid for vnode-backed buffers. | |
| 984263bc MD |
780 | */ |
| 781 | bp->b_runningbufspace = bp->b_bufsize; | |
| 1b30fbcc MD |
782 | if (bp->b_runningbufspace) { |
| 783 | runningbufspace += bp->b_runningbufspace; | |
| 784 | ++runningbufcount; | |
| 785 | } | |
| 984263bc | 786 | |
| e43a034f | 787 | crit_exit(); |
| 984263bc MD |
788 | if (oldflags & B_ASYNC) |
| 789 | BUF_KERNPROC(bp); | |
| 81b5c339 | 790 | vn_strategy(bp->b_vp, &bp->b_bio1); |
| 984263bc MD |
791 | |
| 792 | if ((oldflags & B_ASYNC) == 0) { | |
| 793 | int rtval = biowait(bp); | |
| 794 | brelse(bp); | |
| 795 | return (rtval); | |
| 984263bc | 796 | } |
| 984263bc MD |
797 | return (0); |
| 798 | } | |
| 799 | ||
| 984263bc | 800 | /* |
| 3f779080 HP |
801 | * bdwrite: |
| 802 | * | |
| 803 | * Delayed write. (Buffer is marked dirty). Do not bother writing | |
| 804 | * anything if the buffer is marked invalid. | |
| 984263bc | 805 | * |
| 3f779080 HP |
806 | * Note that since the buffer must be completely valid, we can safely |
| 807 | * set B_CACHE. In fact, we have to set B_CACHE here rather then in | |
| 808 | * biodone() in order to prevent getblk from writing the buffer | |
| 809 | * out synchronously. | |
| 984263bc MD |
810 | */ |
| 811 | void | |
| 493c516a | 812 | bdwrite(struct buf *bp) |
| 984263bc | 813 | { |
| 77bb9400 | 814 | if (BUF_REFCNTNB(bp) == 0) |
| 984263bc MD |
815 | panic("bdwrite: buffer is not busy"); |
| 816 | ||
| 817 | if (bp->b_flags & B_INVAL) { | |
| 818 | brelse(bp); | |
| 819 | return; | |
| 820 | } | |
| 821 | bdirty(bp); | |
| 822 | ||
| 823 | /* | |
| 824 | * Set B_CACHE, indicating that the buffer is fully valid. This is | |
| 825 | * true even of NFS now. | |
| 826 | */ | |
| 827 | bp->b_flags |= B_CACHE; | |
| 828 | ||
| 829 | /* | |
| 830 | * This bmap keeps the system from needing to do the bmap later, | |
| 831 | * perhaps when the system is attempting to do a sync. Since it | |
| 832 | * is likely that the indirect block -- or whatever other datastructure | |
| 833 | * that the filesystem needs is still in memory now, it is a good | |
| 834 | * thing to do this. Note also, that if the pageout daemon is | |
| 835 | * requesting a sync -- there might not be enough memory to do | |
| 836 | * the bmap then... So, this is important to do. | |
| 837 | */ | |
| 54078292 | 838 | if (bp->b_bio2.bio_offset == NOOFFSET) { |
| 08daea96 | 839 | VOP_BMAP(bp->b_vp, bp->b_loffset, &bp->b_bio2.bio_offset, |
| e92ca23a | 840 | NULL, NULL, BUF_CMD_WRITE); |
| 984263bc MD |
841 | } |
| 842 | ||
| 843 | /* | |
| 844 | * Set the *dirty* buffer range based upon the VM system dirty pages. | |
| 845 | */ | |
| 846 | vfs_setdirty(bp); | |
| 847 | ||
| 848 | /* | |
| 849 | * We need to do this here to satisfy the vnode_pager and the | |
| 850 | * pageout daemon, so that it thinks that the pages have been | |
| 851 | * "cleaned". Note that since the pages are in a delayed write | |
| 852 | * buffer -- the VFS layer "will" see that the pages get written | |
| 853 | * out on the next sync, or perhaps the cluster will be completed. | |
| 854 | */ | |
| 855 | vfs_clean_pages(bp); | |
| 856 | bqrelse(bp); | |
| 857 | ||
| 858 | /* | |
| 984263bc MD |
859 | * note: we cannot initiate I/O from a bdwrite even if we wanted to, |
| 860 | * due to the softdep code. | |
| 861 | */ | |
| 862 | } | |
| 863 | ||
| 864 | /* | |
| 3f779080 | 865 | * bdirty: |
| 984263bc | 866 | * |
| 10f3fee5 MD |
867 | * Turn buffer into delayed write request by marking it B_DELWRI. |
| 868 | * B_RELBUF and B_NOCACHE must be cleared. | |
| 984263bc | 869 | * |
| 10f3fee5 MD |
870 | * We reassign the buffer to itself to properly update it in the |
| 871 | * dirty/clean lists. | |
| 984263bc | 872 | * |
| e43a034f | 873 | * Must be called from a critical section. |
| b3098c79 | 874 | * The buffer must be on BQUEUE_NONE. |
| 984263bc MD |
875 | */ |
| 876 | void | |
| 493c516a | 877 | bdirty(struct buf *bp) |
| 984263bc | 878 | { |
| b3098c79 | 879 | KASSERT(bp->b_qindex == BQUEUE_NONE, ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex)); |
| 69f8c926 | 880 | if (bp->b_flags & B_NOCACHE) { |
| 6ea70f76 | 881 | kprintf("bdirty: clearing B_NOCACHE on buf %p\n", bp); |
| 69f8c926 MD |
882 | bp->b_flags &= ~B_NOCACHE; |
| 883 | } | |
| 884 | if (bp->b_flags & B_INVAL) { | |
| 6ea70f76 | 885 | kprintf("bdirty: warning, dirtying invalid buffer %p\n", bp); |
| 69f8c926 | 886 | } |
| 10f3fee5 | 887 | bp->b_flags &= ~B_RELBUF; |
| 984263bc MD |
888 | |
| 889 | if ((bp->b_flags & B_DELWRI) == 0) { | |
| 10f3fee5 | 890 | bp->b_flags |= B_DELWRI; |
| 1f1ea522 | 891 | reassignbuf(bp); |
| 70ac7d6c | 892 | ++dirtybufcount; |
| 868d24af | 893 | dirtybufspace += bp->b_bufsize; |
| 70ac7d6c MD |
894 | if (bp->b_flags & B_HEAVY) { |
| 895 | ++dirtybufcounthw; | |
| 868d24af | 896 | dirtybufspacehw += bp->b_bufsize; |
| 70ac7d6c | 897 | } |
| c4df9635 | 898 | bd_heatup(); |
| 984263bc MD |
899 | } |
| 900 | } | |
| 901 | ||
| 902 | /* | |
| 4b958e7b MD |
903 | * Set B_HEAVY, indicating that this is a heavy-weight buffer that |
| 904 | * needs to be flushed with a different buf_daemon thread to avoid | |
| 905 | * deadlocks. B_HEAVY also imposes restrictions in getnewbuf(). | |
| 906 | */ | |
| 907 | void | |
| 908 | bheavy(struct buf *bp) | |
| 909 | { | |
| 910 | if ((bp->b_flags & B_HEAVY) == 0) { | |
| 911 | bp->b_flags |= B_HEAVY; | |
| 70ac7d6c MD |
912 | if (bp->b_flags & B_DELWRI) { |
| 913 | ++dirtybufcounthw; | |
| 868d24af | 914 | dirtybufspacehw += bp->b_bufsize; |
| 70ac7d6c | 915 | } |
| 4b958e7b MD |
916 | } |
| 917 | } | |
| 918 | ||
| 919 | /* | |
| 3f779080 | 920 | * bundirty: |
| 984263bc MD |
921 | * |
| 922 | * Clear B_DELWRI for buffer. | |
| 923 | * | |
| e43a034f | 924 | * Must be called from a critical section. |
| eaaadca0 | 925 | * |
| b3098c79 | 926 | * The buffer is typically on BQUEUE_NONE but there is one case in |
| eaaadca0 MD |
927 | * brelse() that calls this function after placing the buffer on |
| 928 | * a different queue. | |
| 984263bc MD |
929 | */ |
| 930 | ||
| 931 | void | |
| 493c516a | 932 | bundirty(struct buf *bp) |
| 984263bc | 933 | { |
| 984263bc MD |
934 | if (bp->b_flags & B_DELWRI) { |
| 935 | bp->b_flags &= ~B_DELWRI; | |
| 1f1ea522 | 936 | reassignbuf(bp); |
| 70ac7d6c | 937 | --dirtybufcount; |
| 868d24af | 938 | dirtybufspace -= bp->b_bufsize; |
| 70ac7d6c MD |
939 | if (bp->b_flags & B_HEAVY) { |
| 940 | --dirtybufcounthw; | |
| 868d24af | 941 | dirtybufspacehw -= bp->b_bufsize; |
| 70ac7d6c | 942 | } |
| 868d24af | 943 | bd_signal(bp->b_bufsize); |
| 984263bc MD |
944 | } |
| 945 | /* | |
| 946 | * Since it is now being written, we can clear its deferred write flag. | |
| 947 | */ | |
| 948 | bp->b_flags &= ~B_DEFERRED; | |
| 949 | } | |
| 950 | ||
| 951 | /* | |
| 3f779080 | 952 | * bawrite: |
| 984263bc MD |
953 | * |
| 954 | * Asynchronous write. Start output on a buffer, but do not wait for | |
| 955 | * it to complete. The buffer is released when the output completes. | |
| 956 | * | |
| 957 | * bwrite() ( or the VOP routine anyway ) is responsible for handling | |
| 958 | * B_INVAL buffers. Not us. | |
| 959 | */ | |
| 960 | void | |
| c8e4131d | 961 | bawrite(struct buf *bp) |
| 984263bc MD |
962 | { |
| 963 | bp->b_flags |= B_ASYNC; | |
| 62cfda27 | 964 | bwrite(bp); |
| 984263bc MD |
965 | } |
| 966 | ||
| 967 | /* | |
| 3f779080 | 968 | * bowrite: |
| 984263bc MD |
969 | * |
| 970 | * Ordered write. Start output on a buffer, and flag it so that the | |
| 971 | * device will write it in the order it was queued. The buffer is | |
| 972 | * released when the output completes. bwrite() ( or the VOP routine | |
| 973 | * anyway ) is responsible for handling B_INVAL buffers. | |
| 974 | */ | |
| 975 | int | |
| c8e4131d | 976 | bowrite(struct buf *bp) |
| 984263bc MD |
977 | { |
| 978 | bp->b_flags |= B_ORDERED | B_ASYNC; | |
| 62cfda27 | 979 | return (bwrite(bp)); |
| 984263bc MD |
980 | } |
| 981 | ||
| 982 | /* | |
| 3f779080 HP |
983 | * buf_dirty_count_severe: |
| 984 | * | |
| 985 | * Return true if we have too many dirty buffers. | |
| 984263bc MD |
986 | */ |
| 987 | int | |
| 988 | buf_dirty_count_severe(void) | |
| 989 | { | |
| 70ac7d6c MD |
990 | return (runningbufspace + dirtybufspace >= hidirtybufspace || |
| 991 | dirtybufcount >= nbuf / 2); | |
| 984263bc MD |
992 | } |
| 993 | ||
| 994 | /* | |
| 3f779080 | 995 | * brelse: |
| 984263bc MD |
996 | * |
| 997 | * Release a busy buffer and, if requested, free its resources. The | |
| 998 | * buffer will be stashed in the appropriate bufqueue[] allowing it | |
| 999 | * to be accessed later as a cache entity or reused for other purposes. | |
| 1000 | */ | |
| 1001 | void | |
| c8e4131d | 1002 | brelse(struct buf *bp) |
| 984263bc | 1003 | { |
| 9188c711 MD |
1004 | #ifdef INVARIANTS |
| 1005 | int saved_flags = bp->b_flags; | |
| 1006 | #endif | |
| 1007 | ||
| 984263bc MD |
1008 | KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp)); |
| 1009 | ||
| e43a034f | 1010 | crit_enter(); |
| 984263bc | 1011 | |
| 135bd6a8 MD |
1012 | /* |
| 1013 | * If B_NOCACHE is set we are being asked to destroy the buffer and | |
| 1014 | * its backing store. Clear B_DELWRI. | |
| 1015 | * | |
| 1016 | * B_NOCACHE is set in two cases: (1) when the caller really wants | |
| 1017 | * to destroy the buffer and backing store and (2) when the caller | |
| 1018 | * wants to destroy the buffer and backing store after a write | |
| 1019 | * completes. | |
| 1020 | */ | |
| 1021 | if ((bp->b_flags & (B_NOCACHE|B_DELWRI)) == (B_NOCACHE|B_DELWRI)) { | |
| 1022 | bundirty(bp); | |
| 69f8c926 MD |
1023 | } |
| 1024 | ||
| 78a9b77f | 1025 | if ((bp->b_flags & (B_INVAL | B_DELWRI)) == B_DELWRI) { |
| 984263bc | 1026 | /* |
| 78a9b77f MD |
1027 | * A re-dirtied buffer is only subject to destruction |
| 1028 | * by B_INVAL. B_ERROR and B_NOCACHE are ignored. | |
| 984263bc | 1029 | */ |
| 78a9b77f | 1030 | /* leave buffer intact */ |
| 10f3fee5 | 1031 | } else if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR)) || |
| 78a9b77f | 1032 | (bp->b_bufsize <= 0)) { |
| 984263bc | 1033 | /* |
| 78a9b77f MD |
1034 | * Either a failed read or we were asked to free or not |
| 1035 | * cache the buffer. This path is reached with B_DELWRI | |
| 1036 | * set only if B_INVAL is already set. B_NOCACHE governs | |
| 1037 | * backing store destruction. | |
| 408357d8 MD |
1038 | * |
| 1039 | * NOTE: HAMMER will set B_LOCKED in buf_deallocate if the | |
| 1040 | * buffer cannot be immediately freed. | |
| 984263bc MD |
1041 | */ |
| 1042 | bp->b_flags |= B_INVAL; | |
| 408357d8 MD |
1043 | if (LIST_FIRST(&bp->b_dep) != NULL) |
| 1044 | buf_deallocate(bp); | |
| 984263bc | 1045 | if (bp->b_flags & B_DELWRI) { |
| 70ac7d6c | 1046 | --dirtybufcount; |
| 868d24af | 1047 | dirtybufspace -= bp->b_bufsize; |
| 70ac7d6c MD |
1048 | if (bp->b_flags & B_HEAVY) { |
| 1049 | --dirtybufcounthw; | |
| 868d24af | 1050 | dirtybufspacehw -= bp->b_bufsize; |
| 70ac7d6c | 1051 | } |
| 868d24af | 1052 | bd_signal(bp->b_bufsize); |
| 984263bc | 1053 | } |
| 10f3fee5 | 1054 | bp->b_flags &= ~(B_DELWRI | B_CACHE); |
| 984263bc MD |
1055 | } |
| 1056 | ||
| 1057 | /* | |
| 408357d8 MD |
1058 | * We must clear B_RELBUF if B_DELWRI or B_LOCKED is set. |
| 1059 | * If vfs_vmio_release() is called with either bit set, the | |
| 1060 | * underlying pages may wind up getting freed causing a previous | |
| 1061 | * write (bdwrite()) to get 'lost' because pages associated with | |
| 1062 | * a B_DELWRI bp are marked clean. Pages associated with a | |
| 1063 | * B_LOCKED buffer may be mapped by the filesystem. | |
| 4b958e7b MD |
1064 | * |
| 1065 | * If we want to release the buffer ourselves (rather then the | |
| 1066 | * originator asking us to release it), give the originator a | |
| 1067 | * chance to countermand the release by setting B_LOCKED. | |
| 984263bc MD |
1068 | * |
| 1069 | * We still allow the B_INVAL case to call vfs_vmio_release(), even | |
| 1070 | * if B_DELWRI is set. | |
| 1071 | * | |
| 1072 | * If B_DELWRI is not set we may have to set B_RELBUF if we are low | |
| 1073 | * on pages to return pages to the VM page queues. | |
| 1074 | */ | |
| 4b958e7b | 1075 | if (bp->b_flags & (B_DELWRI | B_LOCKED)) { |
| 984263bc | 1076 | bp->b_flags &= ~B_RELBUF; |
| 4b958e7b | 1077 | } else if (vm_page_count_severe()) { |
| 389ee6be | 1078 | if (LIST_FIRST(&bp->b_dep) != NULL) |
| 78a9b77f | 1079 | buf_deallocate(bp); /* can set B_LOCKED */ |
| 4b958e7b MD |
1080 | if (bp->b_flags & (B_DELWRI | B_LOCKED)) |
| 1081 | bp->b_flags &= ~B_RELBUF; | |
| 1082 | else | |
| 1083 | bp->b_flags |= B_RELBUF; | |
| 1084 | } | |
| 984263bc MD |
1085 | |
| 1086 | /* | |
| 78a9b77f MD |
1087 | * Make sure b_cmd is clear. It may have already been cleared by |
| 1088 | * biodone(). | |
| 1089 | * | |
| 9188c711 MD |
1090 | * At this point destroying the buffer is governed by the B_INVAL |
| 1091 | * or B_RELBUF flags. | |
| 1092 | */ | |
| 10f3fee5 | 1093 | bp->b_cmd = BUF_CMD_DONE; |
| 9188c711 MD |
1094 | |
| 1095 | /* | |
| 135bd6a8 MD |
1096 | * VMIO buffer rundown. Make sure the VM page array is restored |
| 1097 | * after an I/O may have replaces some of the pages with bogus pages | |
| 1098 | * in order to not destroy dirty pages in a fill-in read. | |
| 1099 | * | |
| 1100 | * Note that due to the code above, if a buffer is marked B_DELWRI | |
| 1101 | * then the B_RELBUF and B_NOCACHE bits will always be clear. | |
| 1102 | * B_INVAL may still be set, however. | |
| 984263bc | 1103 | * |
| 135bd6a8 MD |
1104 | * For clean buffers, B_INVAL or B_RELBUF will destroy the buffer |
| 1105 | * but not the backing store. B_NOCACHE will destroy the backing | |
| 1106 | * store. | |
| 984263bc | 1107 | * |
| 135bd6a8 MD |
1108 | * Note that dirty NFS buffers contain byte-granular write ranges |
| 1109 | * and should not be destroyed w/ B_INVAL even if the backing store | |
| 1110 | * is left intact. | |
| 984263bc | 1111 | */ |
| 135bd6a8 | 1112 | if (bp->b_flags & B_VMIO) { |
| 9188c711 MD |
1113 | /* |
| 1114 | * Rundown for VMIO buffers which are not dirty NFS buffers. | |
| 1115 | */ | |
| 984263bc MD |
1116 | int i, j, resid; |
| 1117 | vm_page_t m; | |
| 1118 | off_t foff; | |
| 1119 | vm_pindex_t poff; | |
| 1120 | vm_object_t obj; | |
| 1121 | struct vnode *vp; | |
| 1122 | ||
| 1123 | vp = bp->b_vp; | |
| 1124 | ||
| 1125 | /* | |
| 1126 | * Get the base offset and length of the buffer. Note that | |
| 1127 | * in the VMIO case if the buffer block size is not | |
| 1128 | * page-aligned then b_data pointer may not be page-aligned. | |
| 236b2b9f | 1129 | * But our b_xio.xio_pages array *IS* page aligned. |
| 984263bc MD |
1130 | * |
| 1131 | * block sizes less then DEV_BSIZE (usually 512) are not | |
| 1132 | * supported due to the page granularity bits (m->valid, | |
| 1133 | * m->dirty, etc...). | |
| 1134 | * | |
| 1135 | * See man buf(9) for more information | |
| 1136 | */ | |
| 1137 | ||
| 1138 | resid = bp->b_bufsize; | |
| 81b5c339 | 1139 | foff = bp->b_loffset; |
| 984263bc | 1140 | |
| 54f51aeb HP |
1141 | for (i = 0; i < bp->b_xio.xio_npages; i++) { |
| 1142 | m = bp->b_xio.xio_pages[i]; | |
| 984263bc MD |
1143 | vm_page_flag_clear(m, PG_ZERO); |
| 1144 | /* | |
| 1145 | * If we hit a bogus page, fixup *all* of them | |
| 06ecca5a MD |
1146 | * now. Note that we left these pages wired |
| 1147 | * when we removed them so they had better exist, | |
| 1148 | * and they cannot be ripped out from under us so | |
| e43a034f | 1149 | * no critical section protection is necessary. |
| 984263bc MD |
1150 | */ |
| 1151 | if (m == bogus_page) { | |
| 7540ab49 | 1152 | obj = vp->v_object; |
| 81b5c339 | 1153 | poff = OFF_TO_IDX(bp->b_loffset); |
| 984263bc | 1154 | |
| 54f51aeb | 1155 | for (j = i; j < bp->b_xio.xio_npages; j++) { |
| 984263bc MD |
1156 | vm_page_t mtmp; |
| 1157 | ||
| 54f51aeb | 1158 | mtmp = bp->b_xio.xio_pages[j]; |
| 984263bc MD |
1159 | if (mtmp == bogus_page) { |
| 1160 | mtmp = vm_page_lookup(obj, poff + j); | |
| 1161 | if (!mtmp) { | |
| fc92d4aa | 1162 | panic("brelse: page missing"); |
| 984263bc | 1163 | } |
| 54f51aeb | 1164 | bp->b_xio.xio_pages[j] = mtmp; |
| 984263bc MD |
1165 | } |
| 1166 | } | |
| 1167 | ||
| 1168 | if ((bp->b_flags & B_INVAL) == 0) { | |
| 54f51aeb HP |
1169 | pmap_qenter(trunc_page((vm_offset_t)bp->b_data), |
| 1170 | bp->b_xio.xio_pages, bp->b_xio.xio_npages); | |
| 984263bc | 1171 | } |
| 54f51aeb | 1172 | m = bp->b_xio.xio_pages[i]; |
| 984263bc | 1173 | } |
| 8d429613 MD |
1174 | |
| 1175 | /* | |
| 1176 | * Invalidate the backing store if B_NOCACHE is set | |
| 1177 | * (e.g. used with vinvalbuf()). If this is NFS | |
| 1178 | * we impose a requirement that the block size be | |
| 1179 | * a multiple of PAGE_SIZE and create a temporary | |
| 1180 | * hack to basically invalidate the whole page. The | |
| 1181 | * problem is that NFS uses really odd buffer sizes | |
| 1182 | * especially when tracking piecemeal writes and | |
| 1183 | * it also vinvalbuf()'s a lot, which would result | |
| 1184 | * in only partial page validation and invalidation | |
| 1185 | * here. If the file page is mmap()'d, however, | |
| 1186 | * all the valid bits get set so after we invalidate | |
| 1187 | * here we would end up with weird m->valid values | |
| 1188 | * like 0xfc. nfs_getpages() can't handle this so | |
| 1189 | * we clear all the valid bits for the NFS case | |
| 1190 | * instead of just some of them. | |
| 1191 | * | |
| 1192 | * The real bug is the VM system having to set m->valid | |
| 1193 | * to VM_PAGE_BITS_ALL for faulted-in pages, which | |
| 1194 | * itself is an artifact of the whole 512-byte | |
| 1195 | * granular mess that exists to support odd block | |
| 1196 | * sizes and UFS meta-data block sizes (e.g. 6144). | |
| 1197 | * A complete rewrite is required. | |
| 1198 | */ | |
| 984263bc MD |
1199 | if (bp->b_flags & (B_NOCACHE|B_ERROR)) { |
| 1200 | int poffset = foff & PAGE_MASK; | |
| 8d429613 MD |
1201 | int presid; |
| 1202 | ||
| 1203 | presid = PAGE_SIZE - poffset; | |
| 1204 | if (bp->b_vp->v_tag == VT_NFS && | |
| 1205 | bp->b_vp->v_type == VREG) { | |
| 1206 | ; /* entire page */ | |
| 1207 | } else if (presid > resid) { | |
| 1208 | presid = resid; | |
| 1209 | } | |
| 984263bc MD |
1210 | KASSERT(presid >= 0, ("brelse: extra page")); |
| 1211 | vm_page_set_invalid(m, poffset, presid); | |
| 1212 | } | |
| 1213 | resid -= PAGE_SIZE - (foff & PAGE_MASK); | |
| 1214 | foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; | |
| 1215 | } | |
| 984263bc MD |
1216 | if (bp->b_flags & (B_INVAL | B_RELBUF)) |
| 1217 | vfs_vmio_release(bp); | |
| 9188c711 MD |
1218 | } else { |
| 1219 | /* | |
| 1220 | * Rundown for non-VMIO buffers. | |
| 1221 | */ | |
| 1222 | if (bp->b_flags & (B_INVAL | B_RELBUF)) { | |
| 1223 | #if 0 | |
| 1224 | if (bp->b_vp) | |
| 6ea70f76 | 1225 | kprintf("brelse bp %p %08x/%08x: Warning, caught and fixed brelvp bug\n", bp, saved_flags, bp->b_flags); |
| 9188c711 MD |
1226 | #endif |
| 1227 | if (bp->b_bufsize) | |
| 1228 | allocbuf(bp, 0); | |
| f9a11477 | 1229 | KKASSERT (LIST_FIRST(&bp->b_dep) == NULL); |
| 9188c711 MD |
1230 | if (bp->b_vp) |
| 1231 | brelvp(bp); | |
| 1232 | } | |
| 984263bc MD |
1233 | } |
| 1234 | ||
| b3098c79 | 1235 | if (bp->b_qindex != BQUEUE_NONE) |
| 984263bc | 1236 | panic("brelse: free buffer onto another queue???"); |
| 77bb9400 | 1237 | if (BUF_REFCNTNB(bp) > 1) { |
| 984263bc MD |
1238 | /* Temporary panic to verify exclusive locking */ |
| 1239 | /* This panic goes away when we allow shared refs */ | |
| 1240 | panic("brelse: multiple refs"); | |
| 1241 | /* do not release to free list */ | |
| 1242 | BUF_UNLOCK(bp); | |
| e43a034f | 1243 | crit_exit(); |
| 984263bc MD |
1244 | return; |
| 1245 | } | |
| 1246 | ||
| 9188c711 MD |
1247 | /* |
| 1248 | * Figure out the correct queue to place the cleaned up buffer on. | |
| 1249 | * Buffers placed in the EMPTY or EMPTYKVA had better already be | |
| 1250 | * disassociated from their vnode. | |
| 1251 | */ | |
| 408357d8 MD |
1252 | if (bp->b_flags & B_LOCKED) { |
| 1253 | /* | |
| 27bc0cb1 MD |
1254 | * Buffers that are locked are placed in the locked queue |
| 1255 | * immediately, regardless of their state. | |
| 408357d8 | 1256 | */ |
| 27bc0cb1 MD |
1257 | bp->b_qindex = BQUEUE_LOCKED; |
| 1258 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_LOCKED], bp, b_freelist); | |
| 408357d8 | 1259 | } else if (bp->b_bufsize == 0) { |
| 9188c711 MD |
1260 | /* |
| 1261 | * Buffers with no memory. Due to conditionals near the top | |
| 1262 | * of brelse() such buffers should probably already be | |
| 1263 | * marked B_INVAL and disassociated from their vnode. | |
| 1264 | */ | |
| 984263bc | 1265 | bp->b_flags |= B_INVAL; |
| 54078292 | 1266 | KASSERT(bp->b_vp == NULL, ("bp1 %p flags %08x/%08x vnode %p unexpectededly still associated!", bp, saved_flags, bp->b_flags, bp->b_vp)); |
| 1f1ea522 | 1267 | KKASSERT((bp->b_flags & B_HASHED) == 0); |
| 984263bc | 1268 | if (bp->b_kvasize) { |
| b3098c79 | 1269 | bp->b_qindex = BQUEUE_EMPTYKVA; |
| 984263bc | 1270 | } else { |
| b3098c79 | 1271 | bp->b_qindex = BQUEUE_EMPTY; |
| 984263bc MD |
1272 | } |
| 1273 | TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist); | |
| 78a9b77f | 1274 | } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) { |
| 9188c711 MD |
1275 | /* |
| 1276 | * Buffers with junk contents. Again these buffers had better | |
| 1277 | * already be disassociated from their vnode. | |
| 1278 | */ | |
| 54078292 | 1279 | KASSERT(bp->b_vp == NULL, ("bp2 %p flags %08x/%08x vnode %p unexpectededly still associated!", bp, saved_flags, bp->b_flags, bp->b_vp)); |
| 1f1ea522 | 1280 | KKASSERT((bp->b_flags & B_HASHED) == 0); |
| 984263bc | 1281 | bp->b_flags |= B_INVAL; |
| b3098c79 HP |
1282 | bp->b_qindex = BQUEUE_CLEAN; |
| 1283 | TAILQ_INSERT_HEAD(&bufqueues[BQUEUE_CLEAN], bp, b_freelist); | |
| 984263bc | 1284 | } else { |
| 9188c711 MD |
1285 | /* |
| 1286 | * Remaining buffers. These buffers are still associated with | |
| 1287 | * their vnode. | |
| 1288 | */ | |
| b86460bf | 1289 | switch(bp->b_flags & (B_DELWRI|B_HEAVY)) { |
| 984263bc | 1290 | case B_DELWRI: |
| b3098c79 HP |
1291 | bp->b_qindex = BQUEUE_DIRTY; |
| 1292 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_DIRTY], bp, b_freelist); | |
| 984263bc | 1293 | break; |
| 4b958e7b MD |
1294 | case B_DELWRI | B_HEAVY: |
| 1295 | bp->b_qindex = BQUEUE_DIRTY_HW; | |
| 1296 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_DIRTY_HW], bp, | |
| 1297 | b_freelist); | |
| 1298 | break; | |
| 984263bc | 1299 | default: |
| b86460bf MD |
1300 | /* |
| 1301 | * NOTE: Buffers are always placed at the end of the | |
| 1302 | * queue. If B_AGE is not set the buffer will cycle | |
| 1303 | * through the queue twice. | |
| 1304 | */ | |
| b3098c79 HP |
1305 | bp->b_qindex = BQUEUE_CLEAN; |
| 1306 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN], bp, b_freelist); | |
| 984263bc MD |
1307 | break; |
| 1308 | } | |
| 1309 | } | |
| 1310 | ||
| 1311 | /* | |
| 1312 | * If B_INVAL, clear B_DELWRI. We've already placed the buffer | |
| 1313 | * on the correct queue. | |
| 1314 | */ | |
| 1315 | if ((bp->b_flags & (B_INVAL|B_DELWRI)) == (B_INVAL|B_DELWRI)) | |
| 1316 | bundirty(bp); | |
| 1317 | ||
| 1318 | /* | |
| 868d24af MD |
1319 | * The bp is on an appropriate queue unless locked. If it is not |
| 1320 | * locked or dirty we can wakeup threads waiting for buffer space. | |
| 1321 | * | |
| 984263bc MD |
1322 | * We've already handled the B_INVAL case ( B_DELWRI will be clear |
| 1323 | * if B_INVAL is set ). | |
| 1324 | */ | |
| 408357d8 | 1325 | if ((bp->b_flags & (B_LOCKED|B_DELWRI)) == 0) |
| 984263bc MD |
1326 | bufcountwakeup(); |
| 1327 | ||
| 1328 | /* | |
| 1329 | * Something we can maybe free or reuse | |
| 1330 | */ | |
| 1331 | if (bp->b_bufsize || bp->b_kvasize) | |
| 1332 | bufspacewakeup(); | |
| 1333 | ||
| 69f8c926 MD |
1334 | /* |
| 1335 | * Clean up temporary flags and unlock the buffer. | |
| 1336 | */ | |
| b86460bf | 1337 | bp->b_flags &= ~(B_ORDERED | B_ASYNC | B_NOCACHE | B_RELBUF | B_DIRECT); |
| 69f8c926 | 1338 | BUF_UNLOCK(bp); |
| e43a034f | 1339 | crit_exit(); |
| 984263bc MD |
1340 | } |
| 1341 | ||
| 1342 | /* | |
| 3f779080 HP |
1343 | * bqrelse: |
| 1344 | * | |
| 1345 | * Release a buffer back to the appropriate queue but do not try to free | |
| 1346 | * it. The buffer is expected to be used again soon. | |
| 984263bc | 1347 | * |
| 3f779080 HP |
1348 | * bqrelse() is used by bdwrite() to requeue a delayed write, and used by |
| 1349 | * biodone() to requeue an async I/O on completion. It is also used when | |
| 1350 | * known good buffers need to be requeued but we think we may need the data | |
| 1351 | * again soon. | |
| 984263bc | 1352 | * |
| 3f779080 | 1353 | * XXX we should be able to leave the B_RELBUF hint set on completion. |
| 984263bc MD |
1354 | */ |
| 1355 | void | |
| c8e4131d | 1356 | bqrelse(struct buf *bp) |
| 984263bc | 1357 | { |
| e43a034f | 1358 | crit_enter(); |
| 984263bc MD |
1359 | |
| 1360 | KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp)); | |
| 1361 | ||
| b3098c79 | 1362 | if (bp->b_qindex != BQUEUE_NONE) |
| 984263bc | 1363 | panic("bqrelse: free buffer onto another queue???"); |
| 77bb9400 | 1364 | if (BUF_REFCNTNB(bp) > 1) { |
| 984263bc MD |
1365 | /* do not release to free list */ |
| 1366 | panic("bqrelse: multiple refs"); | |
| 1367 | BUF_UNLOCK(bp); | |
| e43a034f | 1368 | crit_exit(); |
| 984263bc MD |
1369 | return; |
| 1370 | } | |
| 1371 | if (bp->b_flags & B_LOCKED) { | |
| 5e23ca53 MD |
1372 | /* |
| 1373 | * Locked buffers are released to the locked queue. However, | |
| 1374 | * if the buffer is dirty it will first go into the dirty | |
| 1375 | * queue and later on after the I/O completes successfully it | |
| 1376 | * will be released to the locked queue. | |
| 1377 | */ | |
| 27bc0cb1 MD |
1378 | bp->b_qindex = BQUEUE_LOCKED; |
| 1379 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_LOCKED], bp, b_freelist); | |
| 984263bc | 1380 | } else if (bp->b_flags & B_DELWRI) { |
| 4b958e7b MD |
1381 | bp->b_qindex = (bp->b_flags & B_HEAVY) ? |
| 1382 | BQUEUE_DIRTY_HW : BQUEUE_DIRTY; | |
| 1383 | TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist); | |
| 984263bc MD |
1384 | } else if (vm_page_count_severe()) { |
| 1385 | /* | |
| 1386 | * We are too low on memory, we have to try to free the | |
| 1387 | * buffer (most importantly: the wired pages making up its | |
| 1388 | * backing store) *now*. | |
| 1389 | */ | |
| e43a034f | 1390 | crit_exit(); |
| 984263bc MD |
1391 | brelse(bp); |
| 1392 | return; | |
| 1393 | } else { | |
| b3098c79 HP |
1394 | bp->b_qindex = BQUEUE_CLEAN; |
| 1395 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN], bp, b_freelist); | |
| 984263bc MD |
1396 | } |
| 1397 | ||
| 1398 | if ((bp->b_flags & B_LOCKED) == 0 && | |
| 408357d8 | 1399 | ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)) { |
| 984263bc MD |
1400 | bufcountwakeup(); |
| 1401 | } | |
| 1402 | ||
| 1403 | /* | |
| 1404 | * Something we can maybe free or reuse. | |
| 1405 | */ | |
| 1406 | if (bp->b_bufsize && !(bp->b_flags & B_DELWRI)) | |
| 1407 | bufspacewakeup(); | |
| 1408 | ||
| 9188c711 MD |
1409 | /* |
| 1410 | * Final cleanup and unlock. Clear bits that are only used while a | |
| 1411 | * buffer is actively locked. | |
| 1412 | */ | |
| b86460bf | 1413 | bp->b_flags &= ~(B_ORDERED | B_ASYNC | B_NOCACHE | B_RELBUF); |
| 9188c711 | 1414 | BUF_UNLOCK(bp); |
| e43a034f | 1415 | crit_exit(); |
| 984263bc MD |
1416 | } |
| 1417 | ||
| 3f779080 HP |
1418 | /* |
| 1419 | * vfs_vmio_release: | |
| 1420 | * | |
| 1421 | * Return backing pages held by the buffer 'bp' back to the VM system | |
| 1422 | * if possible. The pages are freed if they are no longer valid or | |
| 1423 | * attempt to free if it was used for direct I/O otherwise they are | |
| 1424 | * sent to the page cache. | |
| 1425 | * | |
| 1426 | * Pages that were marked busy are left alone and skipped. | |
| 1427 | * | |
| 1428 | * The KVA mapping (b_data) for the underlying pages is removed by | |
| 1429 | * this function. | |
| 1430 | */ | |
| 984263bc | 1431 | static void |
| 493c516a | 1432 | vfs_vmio_release(struct buf *bp) |
| 984263bc | 1433 | { |
| e43a034f | 1434 | int i; |
| 984263bc MD |
1435 | vm_page_t m; |
| 1436 | ||
| e43a034f | 1437 | crit_enter(); |
| 54f51aeb HP |
1438 | for (i = 0; i < bp->b_xio.xio_npages; i++) { |
| 1439 | m = bp->b_xio.xio_pages[i]; | |
| 1440 | bp->b_xio.xio_pages[i] = NULL; | |
| 984263bc MD |
1441 | /* |
| 1442 | * In order to keep page LRU ordering consistent, put | |
| 1443 | * everything on the inactive queue. | |
| 1444 | */ | |
| 1445 | vm_page_unwire(m, 0); | |
| 1446 | /* | |
| 1447 | * We don't mess with busy pages, it is | |
| 1448 | * the responsibility of the process that | |
| 1449 | * busied the pages to deal with them. | |
| 1450 | */ | |
| 1451 | if ((m->flags & PG_BUSY) || (m->busy != 0)) | |
| 1452 | continue; | |
| 1453 | ||
| 1454 | if (m->wire_count == 0) { | |
| 1455 | vm_page_flag_clear(m, PG_ZERO); | |
| 1456 | /* | |
| 1457 | * Might as well free the page if we can and it has | |
| 1458 | * no valid data. We also free the page if the | |
| 1459 | * buffer was used for direct I/O. | |
| 1460 | */ | |
| 3f779080 HP |
1461 | if ((bp->b_flags & B_ASYNC) == 0 && !m->valid && |
| 1462 | m->hold_count == 0) { | |
| 984263bc MD |
1463 | vm_page_busy(m); |
| 1464 | vm_page_protect(m, VM_PROT_NONE); | |
| 1465 | vm_page_free(m); | |
| 1466 | } else if (bp->b_flags & B_DIRECT) { | |
| 1467 | vm_page_try_to_free(m); | |
| 1468 | } else if (vm_page_count_severe()) { | |
| 1469 | vm_page_try_to_cache(m); | |
| 1470 | } | |
| 1471 | } | |
| 1472 | } | |
| e43a034f | 1473 | crit_exit(); |
| 54f51aeb | 1474 | pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_xio.xio_npages); |
| 984263bc MD |
1475 | if (bp->b_bufsize) { |
| 1476 | bufspacewakeup(); | |
| 1477 | bp->b_bufsize = 0; | |
| 1478 | } | |
| 54f51aeb | 1479 | bp->b_xio.xio_npages = 0; |
| 984263bc | 1480 | bp->b_flags &= ~B_VMIO; |
| f9a11477 | 1481 | KKASSERT (LIST_FIRST(&bp->b_dep) == NULL); |
| 984263bc MD |
1482 | if (bp->b_vp) |
| 1483 | brelvp(bp); | |
| 1484 | } | |
| 1485 | ||
| 1486 | /* | |
| 3f779080 | 1487 | * vfs_bio_awrite: |
| 984263bc MD |
1488 | * |
| 1489 | * Implement clustered async writes for clearing out B_DELWRI buffers. | |
| 1490 | * This is much better then the old way of writing only one buffer at | |
| 1491 | * a time. Note that we may not be presented with the buffers in the | |
| 1492 | * correct order, so we search for the cluster in both directions. | |
| 6f68d895 MD |
1493 | * |
| 1494 | * The buffer is locked on call. | |
| 984263bc MD |
1495 | */ |
| 1496 | int | |
| 6f68d895 | 1497 | vfs_bio_awrite(struct buf *bp) |
| 984263bc MD |
1498 | { |
| 1499 | int i; | |
| 1500 | int j; | |
| 54078292 | 1501 | off_t loffset = bp->b_loffset; |
| 984263bc | 1502 | struct vnode *vp = bp->b_vp; |
| 54078292 | 1503 | int nbytes; |
| 984263bc MD |
1504 | struct buf *bpa; |
| 1505 | int nwritten; | |
| 1506 | int size; | |
| 984263bc | 1507 | |
| e43a034f | 1508 | crit_enter(); |
| 984263bc MD |
1509 | /* |
| 1510 | * right now we support clustered writing only to regular files. If | |
| 1511 | * we find a clusterable block we could be in the middle of a cluster | |
| 1512 | * rather then at the beginning. | |
| 81b5c339 | 1513 | * |
| 54078292 MD |
1514 | * NOTE: b_bio1 contains the logical loffset and is aliased |
| 1515 | * to b_loffset. b_bio2 contains the translated block number. | |
| 984263bc MD |
1516 | */ |
| 1517 | if ((vp->v_type == VREG) && | |
| 1518 | (vp->v_mount != 0) && /* Only on nodes that have the size info */ | |
| 1519 | (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) { | |
| 1520 | ||
| 1521 | size = vp->v_mount->mnt_stat.f_iosize; | |
| 984263bc | 1522 | |
| 54078292 MD |
1523 | for (i = size; i < MAXPHYS; i += size) { |
| 1524 | if ((bpa = findblk(vp, loffset + i)) && | |
| 984263bc MD |
1525 | BUF_REFCNT(bpa) == 0 && |
| 1526 | ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) == | |
| 1527 | (B_DELWRI | B_CLUSTEROK)) && | |
| 1528 | (bpa->b_bufsize == size)) { | |
| 54078292 MD |
1529 | if ((bpa->b_bio2.bio_offset == NOOFFSET) || |
| 1530 | (bpa->b_bio2.bio_offset != | |
| 1531 | bp->b_bio2.bio_offset + i)) | |
| 984263bc MD |
1532 | break; |
| 1533 | } else { | |
| 1534 | break; | |
| 1535 | } | |
| 1536 | } | |
| 54078292 MD |
1537 | for (j = size; i + j <= MAXPHYS && j <= loffset; j += size) { |
| 1538 | if ((bpa = findblk(vp, loffset - j)) && | |
| 984263bc MD |
1539 | BUF_REFCNT(bpa) == 0 && |
| 1540 | ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) == | |
| 1541 | (B_DELWRI | B_CLUSTEROK)) && | |
| 1542 | (bpa->b_bufsize == size)) { | |
| 54078292 MD |
1543 | if ((bpa->b_bio2.bio_offset == NOOFFSET) || |
| 1544 | (bpa->b_bio2.bio_offset != | |
| 1545 | bp->b_bio2.bio_offset - j)) | |
| 984263bc MD |
1546 | break; |
| 1547 | } else { | |
| 1548 | break; | |
| 1549 | } | |
| 1550 | } | |
| 54078292 MD |
1551 | j -= size; |
| 1552 | nbytes = (i + j); | |
| 984263bc MD |
1553 | /* |
| 1554 | * this is a possible cluster write | |
| 1555 | */ | |
| 54078292 | 1556 | if (nbytes != size) { |
| 6f68d895 | 1557 | BUF_UNLOCK(bp); |
| 54078292 MD |
1558 | nwritten = cluster_wbuild(vp, size, |
| 1559 | loffset - j, nbytes); | |
| e43a034f | 1560 | crit_exit(); |
| 984263bc MD |
1561 | return nwritten; |
| 1562 | } | |
| 1563 | } | |
| 1564 | ||
| 984263bc MD |
1565 | bremfree(bp); |
| 1566 | bp->b_flags |= B_ASYNC; | |
| 1567 | ||
| e43a034f | 1568 | crit_exit(); |
| 984263bc MD |
1569 | /* |
| 1570 | * default (old) behavior, writing out only one block | |
| 1571 | * | |
| 1572 | * XXX returns b_bufsize instead of b_bcount for nwritten? | |
| 1573 | */ | |
| 1574 | nwritten = bp->b_bufsize; | |
| 62cfda27 | 1575 | bwrite(bp); |
| 984263bc MD |
1576 | |
| 1577 | return nwritten; | |
| 1578 | } | |
| 1579 | ||
| 1580 | /* | |
| 3f779080 | 1581 | * getnewbuf: |
| 984263bc MD |
1582 | * |
| 1583 | * Find and initialize a new buffer header, freeing up existing buffers | |
| 1584 | * in the bufqueues as necessary. The new buffer is returned locked. | |
| 1585 | * | |
| 1586 | * Important: B_INVAL is not set. If the caller wishes to throw the | |
| 1587 | * buffer away, the caller must set B_INVAL prior to calling brelse(). | |
| 1588 | * | |
| 1589 | * We block if: | |
| 1590 | * We have insufficient buffer headers | |
| 1591 | * We have insufficient buffer space | |
| 1592 | * buffer_map is too fragmented ( space reservation fails ) | |
| 1593 | * If we have to flush dirty buffers ( but we try to avoid this ) | |
| 1594 | * | |
| 1595 | * To avoid VFS layer recursion we do not flush dirty buffers ourselves. | |
| 1596 | * Instead we ask the buf daemon to do it for us. We attempt to | |
| 1597 | * avoid piecemeal wakeups of the pageout daemon. | |
| 1598 | */ | |
| 1599 | ||
| 1600 | static struct buf * | |
| 4b958e7b | 1601 | getnewbuf(int blkflags, int slptimeo, int size, int maxsize) |
| 984263bc MD |
1602 | { |
| 1603 | struct buf *bp; | |
| 1604 | struct buf *nbp; | |
| 1605 | int defrag = 0; | |
| 1606 | int nqindex; | |
| 4b958e7b | 1607 | int slpflags = (blkflags & GETBLK_PCATCH) ? PCATCH : 0; |
| 984263bc MD |
1608 | static int flushingbufs; |
| 1609 | ||
| 1610 | /* | |
| 1611 | * We can't afford to block since we might be holding a vnode lock, | |
| 1612 | * which may prevent system daemons from running. We deal with | |
| 1613 | * low-memory situations by proactively returning memory and running | |
| 1614 | * async I/O rather then sync I/O. | |
| 1615 | */ | |
| 1616 | ||
| 1617 | ++getnewbufcalls; | |
| 1618 | --getnewbufrestarts; | |
| 1619 | restart: | |
| 1620 | ++getnewbufrestarts; | |
| 1621 | ||
| 1622 | /* | |
| 1623 | * Setup for scan. If we do not have enough free buffers, | |
| 1624 | * we setup a degenerate case that immediately fails. Note | |
| 1625 | * that if we are specially marked process, we are allowed to | |
| 1626 | * dip into our reserves. | |
| 1627 | * | |
| 1628 | * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN | |
| 1629 | * | |
| 1630 | * We start with EMPTYKVA. If the list is empty we backup to EMPTY. | |
| 1631 | * However, there are a number of cases (defragging, reusing, ...) | |
| 1632 | * where we cannot backup. | |
| 1633 | */ | |
| b3098c79 HP |
1634 | nqindex = BQUEUE_EMPTYKVA; |
| 1635 | nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTYKVA]); | |
| 984263bc MD |
1636 | |
| 1637 | if (nbp == NULL) { | |
| 1638 | /* | |
| 1639 | * If no EMPTYKVA buffers and we are either | |
| 1640 | * defragging or reusing, locate a CLEAN buffer | |
| 1641 | * to free or reuse. If bufspace useage is low | |
| 1642 | * skip this step so we can allocate a new buffer. | |
| 1643 | */ | |
| 1644 | if (defrag || bufspace >= lobufspace) { | |
| b3098c79 HP |
1645 | nqindex = BQUEUE_CLEAN; |
| 1646 | nbp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN]); | |
| 984263bc MD |
1647 | } |
| 1648 | ||
| 1649 | /* | |
| 1650 | * If we could not find or were not allowed to reuse a | |
| 1651 | * CLEAN buffer, check to see if it is ok to use an EMPTY | |
| 1652 | * buffer. We can only use an EMPTY buffer if allocating | |
| 1653 | * its KVA would not otherwise run us out of buffer space. | |
| 1654 | */ | |
| 1655 | if (nbp == NULL && defrag == 0 && | |
| 1656 | bufspace + maxsize < hibufspace) { | |
| b3098c79 HP |
1657 | nqindex = BQUEUE_EMPTY; |
| 1658 | nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTY]); | |
| 984263bc MD |
1659 | } |
| 1660 | } | |
| 1661 | ||
| 1662 | /* | |
| 1663 | * Run scan, possibly freeing data and/or kva mappings on the fly | |
| 1664 | * depending. | |
| 1665 | */ | |
| 1666 | ||
| 1667 | while ((bp = nbp) != NULL) { | |
| 1668 | int qindex = nqindex; | |
| 1669 | ||
| b86460bf MD |
1670 | nbp = TAILQ_NEXT(bp, b_freelist); |
| 1671 | ||
| 1672 | /* | |
| 1673 | * BQUEUE_CLEAN - B_AGE special case. If not set the bp | |
| 1674 | * cycles through the queue twice before being selected. | |
| 1675 | */ | |
| 1676 | if (qindex == BQUEUE_CLEAN && | |
| 1677 | (bp->b_flags & B_AGE) == 0 && nbp) { | |
| 1678 | bp->b_flags |= B_AGE; | |
| 1679 | TAILQ_REMOVE(&bufqueues[qindex], bp, b_freelist); | |
| 1680 | TAILQ_INSERT_TAIL(&bufqueues[qindex], bp, b_freelist); | |
| 1681 | continue; | |
| 1682 | } | |
| 1683 | ||
| 984263bc MD |
1684 | /* |
| 1685 | * Calculate next bp ( we can only use it if we do not block | |
| 1686 | * or do other fancy things ). | |
| 1687 | */ | |
| b86460bf | 1688 | if (nbp == NULL) { |
| 984263bc | 1689 | switch(qindex) { |
| b3098c79 HP |
1690 | case BQUEUE_EMPTY: |
| 1691 | nqindex = BQUEUE_EMPTYKVA; | |
| 1692 | if ((nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTYKVA]))) | |
| 984263bc MD |
1693 | break; |
| 1694 | /* fall through */ | |
| b3098c79 HP |
1695 | case BQUEUE_EMPTYKVA: |
| 1696 | nqindex = BQUEUE_CLEAN; | |
| 1697 | if ((nbp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN]))) | |
| 984263bc MD |
1698 | break; |
| 1699 | /* fall through */ | |
| b3098c79 | 1700 | case BQUEUE_CLEAN: |
| 984263bc MD |
1701 | /* |
| 1702 | * nbp is NULL. | |
| 1703 | */ | |
| 1704 | break; | |
| 1705 | } | |
| 1706 | } | |
| 1707 | ||
| 1708 | /* | |
| 1709 | * Sanity Checks | |
| 1710 | */ | |
| 3f625015 | 1711 | KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistent queue %d bp %p", qindex, bp)); |
| 984263bc MD |
1712 | |
| 1713 | /* | |
| 1714 | * Note: we no longer distinguish between VMIO and non-VMIO | |
| 1715 | * buffers. | |
| 1716 | */ | |
| 1717 | ||
| 1718 | KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex)); | |
| 1719 | ||
| 1720 | /* | |
| 1721 | * If we are defragging then we need a buffer with | |
| 1722 | * b_kvasize != 0. XXX this situation should no longer | |
| 1723 | * occur, if defrag is non-zero the buffer's b_kvasize | |
| 1724 | * should also be non-zero at this point. XXX | |
| 1725 | */ | |
| 1726 | if (defrag && bp->b_kvasize == 0) { | |
| 6ea70f76 | 1727 | kprintf("Warning: defrag empty buffer %p\n", bp); |
| 984263bc MD |
1728 | continue; |
| 1729 | } | |
| 1730 | ||
| 1731 | /* | |
| 1732 | * Start freeing the bp. This is somewhat involved. nbp | |
| b3098c79 | 1733 | * remains valid only for BQUEUE_EMPTY[KVA] bp's. Buffers |
| 9188c711 MD |
1734 | * on the clean list must be disassociated from their |
| 1735 | * current vnode. Buffers on the empty[kva] lists have | |
| 1736 | * already been disassociated. | |
| 984263bc MD |
1737 | */ |
| 1738 | ||
| d9dba6f6 | 1739 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { |
| 6ea70f76 | 1740 | kprintf("getnewbuf: warning, locked buf %p, race corrected\n", bp); |
| d9dba6f6 MD |
1741 | tsleep(&bd_request, 0, "gnbxxx", hz / 100); |
| 1742 | goto restart; | |
| 1743 | } | |
| 1744 | if (bp->b_qindex != qindex) { | |
| 6ea70f76 | 1745 | kprintf("getnewbuf: warning, BUF_LOCK blocked unexpectedly on buf %p index %d->%d, race corrected\n", bp, qindex, bp->b_qindex); |
| d9dba6f6 MD |
1746 | BUF_UNLOCK(bp); |
| 1747 | goto restart; | |
| 1748 | } | |
| 984263bc MD |
1749 | bremfree(bp); |
| 1750 | ||
| 408357d8 MD |
1751 | /* |
| 1752 | * Dependancies must be handled before we disassociate the | |
| 1753 | * vnode. | |
| 1754 | * | |
| 1755 | * NOTE: HAMMER will set B_LOCKED if the buffer cannot | |
| 1756 | * be immediately disassociated. HAMMER then becomes | |
| 1757 | * responsible for releasing the buffer. | |
| 1758 | */ | |
| 1759 | if (LIST_FIRST(&bp->b_dep) != NULL) { | |
| 1760 | buf_deallocate(bp); | |
| 1761 | if (bp->b_flags & B_LOCKED) { | |
| 1762 | bqrelse(bp); | |
| 1763 | goto restart; | |
| 1764 | } | |
| 4b958e7b | 1765 | KKASSERT(LIST_FIRST(&bp->b_dep) == NULL); |
| 408357d8 MD |
1766 | } |
| 1767 | ||
| b3098c79 | 1768 | if (qindex == BQUEUE_CLEAN) { |
| 984263bc MD |
1769 | if (bp->b_flags & B_VMIO) { |
| 1770 | bp->b_flags &= ~B_ASYNC; | |
| 1771 | vfs_vmio_release(bp); | |
| 1772 | } | |
| 1773 | if (bp->b_vp) | |
| 1774 | brelvp(bp); | |
| 1775 | } | |
| 1776 | ||
| 1777 | /* | |
| 1778 | * NOTE: nbp is now entirely invalid. We can only restart | |
| 1779 | * the scan from this point on. | |
| 1780 | * | |
| 1781 | * Get the rest of the buffer freed up. b_kva* is still | |
| 1782 | * valid after this operation. | |
| 1783 | */ | |
| 1784 | ||
| 54078292 | 1785 | KASSERT(bp->b_vp == NULL, ("bp3 %p flags %08x vnode %p qindex %d unexpectededly still associated!", bp, bp->b_flags, bp->b_vp, qindex)); |
| 1f1ea522 | 1786 | KKASSERT((bp->b_flags & B_HASHED) == 0); |
| 984263bc | 1787 | |
| 06ecca5a | 1788 | /* |
| e43a034f MD |
1789 | * critical section protection is not required when |
| 1790 | * scrapping a buffer's contents because it is already | |
| 1791 | * wired. | |
| 06ecca5a | 1792 | */ |
| 984263bc MD |
1793 | if (bp->b_bufsize) |
| 1794 | allocbuf(bp, 0); | |
| 1795 | ||
| 4414f2c9 | 1796 | bp->b_flags = B_BNOCLIP; |
| 10f3fee5 | 1797 | bp->b_cmd = BUF_CMD_DONE; |
| 984263bc | 1798 | bp->b_vp = NULL; |
| 984263bc MD |
1799 | bp->b_error = 0; |
| 1800 | bp->b_resid = 0; | |
| 1801 | bp->b_bcount = 0; | |
| 54f51aeb | 1802 | bp->b_xio.xio_npages = 0; |
| 984263bc | 1803 | bp->b_dirtyoff = bp->b_dirtyend = 0; |
| 81b5c339 | 1804 | reinitbufbio(bp); |
| b86460bf | 1805 | KKASSERT(LIST_FIRST(&bp->b_dep) == NULL); |
| 408357d8 | 1806 | buf_dep_init(bp); |
| 4b958e7b MD |
1807 | if (blkflags & GETBLK_BHEAVY) |
| 1808 | bp->b_flags |= B_HEAVY; | |
| 984263bc MD |
1809 | |
| 1810 | /* | |
| 1811 | * If we are defragging then free the buffer. | |
| 1812 | */ | |
| 1813 | if (defrag) { | |
| 1814 | bp->b_flags |= B_INVAL; | |
| 1815 | bfreekva(bp); | |
| 1816 | brelse(bp); | |
| 1817 | defrag = 0; | |
| 1818 | goto restart; | |
| 1819 | } | |
| 1820 | ||
| 1821 | /* | |
| 1822 | * If we are overcomitted then recover the buffer and its | |
| 1823 | * KVM space. This occurs in rare situations when multiple | |
| 1824 | * processes are blocked in getnewbuf() or allocbuf(). | |
| 1825 | */ | |
| 1826 | if (bufspace >= hibufspace) | |
| 1827 | flushingbufs = 1; | |
| 1828 | if (flushingbufs && bp->b_kvasize != 0) { | |
| 1829 | bp->b_flags |= B_INVAL; | |
| 1830 | bfreekva(bp); | |
| 1831 | brelse(bp); | |
| 1832 | goto restart; | |
| 1833 | } | |
| 1834 | if (bufspace < lobufspace) | |
| 1835 | flushingbufs = 0; | |
| 1836 | break; | |
| 1837 | } | |
| 1838 | ||
| 1839 | /* | |
| 1840 | * If we exhausted our list, sleep as appropriate. We may have to | |
| 1841 | * wakeup various daemons and write out some dirty buffers. | |
| 1842 | * | |
| 1843 | * Generally we are sleeping due to insufficient buffer space. | |
| 1844 | */ | |
| 1845 | ||
| 1846 | if (bp == NULL) { | |
| 1847 | int flags; | |
| 1848 | char *waitmsg; | |
| 1849 | ||
| 1850 | if (defrag) { | |
| 1851 | flags = VFS_BIO_NEED_BUFSPACE; | |
| 1852 | waitmsg = "nbufkv"; | |
| 1853 | } else if (bufspace >= hibufspace) { | |
| 1854 | waitmsg = "nbufbs"; | |
| 1855 | flags = VFS_BIO_NEED_BUFSPACE; | |
| 1856 | } else { | |
| 1857 | waitmsg = "newbuf"; | |
| 1858 | flags = VFS_BIO_NEED_ANY; | |
| 1859 | } | |
| 1860 | ||
| 984263bc | 1861 | needsbuffer |= flags; |
| 4b958e7b | 1862 | bd_speedup(); /* heeeelp */ |
| 984263bc | 1863 | while (needsbuffer & flags) { |
| 4b958e7b | 1864 | if (tsleep(&needsbuffer, slpflags, waitmsg, slptimeo)) |
| 984263bc MD |
1865 | return (NULL); |
| 1866 | } | |
| 1867 | } else { | |
| 1868 | /* | |
| 1869 | * We finally have a valid bp. We aren't quite out of the | |
| 1870 | * woods, we still have to reserve kva space. In order | |
| 1871 | * to keep fragmentation sane we only allocate kva in | |
| 1872 | * BKVASIZE chunks. | |
| 1873 | */ | |
| 1874 | maxsize = (maxsize + BKVAMASK) & ~BKVAMASK; | |
| 1875 | ||
| 1876 | if (maxsize != bp->b_kvasize) { | |
| 1877 | vm_offset_t addr = 0; | |
| a108bf71 | 1878 | int count; |
| 984263bc MD |
1879 | |
| 1880 | bfreekva(bp); | |
| 1881 | ||
| a108bf71 | 1882 | count = vm_map_entry_reserve(MAP_RESERVE_COUNT); |
| e4846942 | 1883 | vm_map_lock(&buffer_map); |
| 984263bc | 1884 | |
| e4846942 MD |
1885 | if (vm_map_findspace(&buffer_map, |
| 1886 | vm_map_min(&buffer_map), maxsize, | |
| c809941b | 1887 | maxsize, 0, &addr)) { |
| 984263bc | 1888 | /* |
| 3f779080 | 1889 | * Uh oh. Buffer map is too fragmented. We |
| 984263bc MD |
1890 | * must defragment the map. |
| 1891 | */ | |
| e4846942 | 1892 | vm_map_unlock(&buffer_map); |
| a108bf71 | 1893 | vm_map_entry_release(count); |
| 984263bc MD |
1894 | ++bufdefragcnt; |
| 1895 | defrag = 1; | |
| 1896 | bp->b_flags |= B_INVAL; | |
| 1897 | brelse(bp); | |
| 1898 | goto restart; | |
| 1899 | } | |
| 1900 | if (addr) { | |
| e4846942 | 1901 | vm_map_insert(&buffer_map, &count, |
| a108bf71 | 1902 | NULL, 0, |
| 984263bc | 1903 | addr, addr + maxsize, |
| 1b874851 MD |
1904 | VM_MAPTYPE_NORMAL, |
| 1905 | VM_PROT_ALL, VM_PROT_ALL, | |
| 1906 | MAP_NOFAULT); | |
| 984263bc MD |
1907 | |
| 1908 | bp->b_kvabase = (caddr_t) addr; | |
| 1909 | bp->b_kvasize = maxsize; | |
| 1910 | bufspace += bp->b_kvasize; | |
| 1911 | ++bufreusecnt; | |
| 1912 | } | |
| e4846942 | 1913 | vm_map_unlock(&buffer_map); |
| a108bf71 | 1914 | vm_map_entry_release(count); |
| 984263bc MD |
1915 | } |
| 1916 | bp->b_data = bp->b_kvabase; | |
| 1917 | } | |
| 1918 | return(bp); | |
| 1919 | } | |
| 1920 | ||
| 1921 | /* | |
| 4ecf7cc9 MD |
1922 | * This routine is called in an emergency to recover VM pages from the |
| 1923 | * buffer cache by cashing in clean buffers. The idea is to recover | |
| 1924 | * enough pages to be able to satisfy a stuck bio_page_alloc(). | |
| 1925 | */ | |
| 1926 | static int | |
| 1927 | recoverbufpages(void) | |
| 1928 | { | |
| 1929 | struct buf *bp; | |
| 1930 | int bytes = 0; | |
| 1931 | ||
| 1932 | ++recoverbufcalls; | |
| 1933 | ||
| 1934 | while (bytes < MAXBSIZE) { | |
| 1935 | bp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN]); | |
| 1936 | if (bp == NULL) | |
| 1937 | break; | |
| 1938 | ||
| 1939 | /* | |
| 1940 | * BQUEUE_CLEAN - B_AGE special case. If not set the bp | |
| 1941 | * cycles through the queue twice before being selected. | |
| 1942 | */ | |
| 1943 | if ((bp->b_flags & B_AGE) == 0 && TAILQ_NEXT(bp, b_freelist)) { | |
| 1944 | bp->b_flags |= B_AGE; | |
| 1945 | TAILQ_REMOVE(&bufqueues[BQUEUE_CLEAN], bp, b_freelist); | |
| 1946 | TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN], | |
| 1947 | bp, b_freelist); | |
| 1948 | continue; | |
| 1949 | } | |
| 1950 | ||
| 1951 | /* | |
| 1952 | * Sanity Checks | |
| 1953 | */ | |
| 1954 | KKASSERT(bp->b_qindex == BQUEUE_CLEAN); | |
| 1955 | KKASSERT((bp->b_flags & B_DELWRI) == 0); | |
| 1956 | ||
| 1957 | /* | |
| 1958 | * Start freeing the bp. This is somewhat involved. | |
| 1959 | * | |
| 1960 | * Buffers on the clean list must be disassociated from | |
| 1961 | * their current vnode | |
| 1962 | */ | |
| 1963 | ||
| 1964 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { | |
| 1965 | kprintf("recoverbufpages: warning, locked buf %p, race corrected\n", bp); | |
| 1966 | tsleep(&bd_request, 0, "gnbxxx", hz / 100); | |
| 1967 | continue; | |
| 1968 | } | |
| 1969 | if (bp->b_qindex != BQUEUE_CLEAN) { | |
| 1970 | kprintf("recoverbufpages: warning, BUF_LOCK blocked unexpectedly on buf %p index %d, race corrected\n", bp, bp->b_qindex); | |
| 1971 | BUF_UNLOCK(bp); | |
| 1972 | continue; | |
| 1973 | } | |
| 1974 | bremfree(bp); | |
| 1975 | ||
| 1976 | /* | |
| 1977 | * Dependancies must be handled before we disassociate the | |
| 1978 | * vnode. | |
| 1979 | * | |
| 1980 | * NOTE: HAMMER will set B_LOCKED if the buffer cannot | |
| 1981 | * be immediately disassociated. HAMMER then becomes | |
| 1982 | * responsible for releasing the buffer. | |
| 1983 | */ | |
| 1984 | if (LIST_FIRST(&bp->b_dep) != NULL) { | |
| 1985 | buf_deallocate(bp); | |
| 1986 | if (bp->b_flags & B_LOCKED) { | |
| 1987 | bqrelse(bp); | |
| 1988 | continue; | |
| 1989 | } | |
| 1990 | KKASSERT(LIST_FIRST(&bp->b_dep) == NULL); | |
| 1991 | } | |
| 1992 | ||
| 1993 | bytes += bp->b_bufsize; | |
| 1994 | ||
| 1995 | if (bp->b_flags & B_VMIO) { | |
| 1996 | bp->b_flags &= ~B_ASYNC; | |
| 1997 | bp->b_flags |= B_DIRECT; /* try to free pages */ | |
| 1998 | vfs_vmio_release(bp); | |
| 1999 | } | |
| 2000 | if (bp->b_vp) | |
| 2001 | brelvp(bp); | |
| 2002 | ||
| 2003 | KKASSERT(bp->b_vp == NULL); | |
| 2004 | KKASSERT((bp->b_flags & B_HASHED) == 0); | |
| 2005 | ||
| 2006 | /* | |
| 2007 | * critical section protection is not required when | |
| 2008 | * scrapping a buffer's contents because it is already | |
| 2009 | * wired. | |
| 2010 | */ | |
| 2011 | if (bp->b_bufsize) | |
| 2012 | allocbuf(bp, 0); | |
| 2013 | ||
| 2014 | bp->b_flags = B_BNOCLIP; | |
| 2015 | bp->b_cmd = BUF_CMD_DONE; | |
| 2016 | bp->b_vp = NULL; | |
| 2017 | bp->b_error = 0; | |
| 2018 | bp->b_resid = 0; | |
| 2019 | bp->b_bcount = 0; | |
| 2020 | bp->b_xio.xio_npages = 0; | |
| 2021 | bp->b_dirtyoff = bp->b_dirtyend = 0; | |
| 2022 | reinitbufbio(bp); | |
| 2023 | KKASSERT(LIST_FIRST(&bp->b_dep) == NULL); | |
| 2024 | buf_dep_init(bp); | |
| 2025 | bp->b_flags |= B_INVAL; | |
| 2026 | /* bfreekva(bp); */ | |
| 2027 | brelse(bp); | |
| 2028 | } | |
| 2029 | return(bytes); | |
| 2030 | } | |
| 2031 | ||
| 2032 | /* | |
| 3f779080 | 2033 | * buf_daemon: |
| 984263bc | 2034 | * |
| 3f779080 | 2035 | * Buffer flushing daemon. Buffers are normally flushed by the |
| 984263bc MD |
2036 | * update daemon but if it cannot keep up this process starts to |
| 2037 | * take the load in an attempt to prevent getnewbuf() from blocking. | |
| 4b958e7b MD |
2038 | * |
| 2039 | * Once a flush is initiated it does not stop until the number | |
| 2040 | * of buffers falls below lodirtybuffers, but we will wake up anyone | |
| 2041 | * waiting at the mid-point. | |
| 984263bc MD |
2042 | */ |
| 2043 | ||
| 984263bc MD |
2044 | static struct kproc_desc buf_kp = { |
| 2045 | "bufdaemon", | |
| 2046 | buf_daemon, | |
| 4b958e7b MD |
2047 | &bufdaemon_td |
| 2048 | }; | |
| 2049 | SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, | |
| 2050 | kproc_start, &buf_kp) | |
| 2051 | ||
| 2052 | static struct kproc_desc bufhw_kp = { | |
| 2053 | "bufdaemon_hw", | |
| 2054 | buf_daemon_hw, | |
| 2055 | &bufdaemonhw_td | |
| 984263bc | 2056 | }; |
| 4b958e7b MD |
2057 | SYSINIT(bufdaemon_hw, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, |
| 2058 | kproc_start, &bufhw_kp) | |
| 984263bc MD |
2059 | |
| 2060 | static void | |
| c972a82f | 2061 | buf_daemon(void) |
| 984263bc | 2062 | { |
| cd083340 MD |
2063 | int limit; |
| 2064 | ||
| 984263bc MD |
2065 | /* |
| 2066 | * This process needs to be suspended prior to shutdown sync. | |
| 2067 | */ | |
| bc6dffab | 2068 | EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, |
| 4b958e7b | 2069 | bufdaemon_td, SHUTDOWN_PRI_LAST); |
| 4ecf7cc9 | 2070 | curthread->td_flags |= TDF_SYSTHREAD; |
| 984263bc MD |
2071 | |
| 2072 | /* | |
| 2073 | * This process is allowed to take the buffer cache to the limit | |
| 2074 | */ | |
| e43a034f | 2075 | crit_enter(); |
| 984263bc MD |
2076 | |
| 2077 | for (;;) { | |
| 0cfcada1 | 2078 | kproc_suspend_loop(); |
| 984263bc MD |
2079 | |
| 2080 | /* | |
| 2081 | * Do the flush. Limit the amount of in-transit I/O we | |
| 2082 | * allow to build up, otherwise we would completely saturate | |
| 2083 | * the I/O system. Wakeup any waiting processes before we | |
| 2084 | * normally would so they can run in parallel with our drain. | |
| cd083340 MD |
2085 | * |
| 2086 | * Our aggregate normal+HW lo water mark is lodirtybufspace, | |
| 2087 | * but because we split the operation into two threads we | |
| 2088 | * have to cut it in half for each thread. | |
| 984263bc | 2089 | */ |
| cd083340 MD |
2090 | limit = lodirtybufspace / 2; |
| 2091 | waitrunningbufspace(limit); | |
| 70ac7d6c MD |
2092 | while (runningbufspace + dirtybufspace > limit || |
| 2093 | dirtybufcount - dirtybufcounthw >= nbuf / 2) { | |
| 4b958e7b | 2094 | if (flushbufqueues(BQUEUE_DIRTY) == 0) |
| 984263bc | 2095 | break; |
| cd083340 | 2096 | waitrunningbufspace(limit); |
| 1b30fbcc | 2097 | } |
| 984263bc MD |
2098 | |
| 2099 | /* | |
| cd083340 MD |
2100 | * We reached our low water mark, reset the |
| 2101 | * request and sleep until we are needed again. | |
| 2102 | * The sleep is just so the suspend code works. | |
| 984263bc | 2103 | */ |
| cd083340 MD |
2104 | spin_lock_wr(&needsbuffer_spin); |
| 2105 | if (bd_request == 0) { | |
| 4b958e7b MD |
2106 | msleep(&bd_request, &needsbuffer_spin, 0, |
| 2107 | "psleep", hz); | |
| 984263bc | 2108 | } |
| cd083340 MD |
2109 | bd_request = 0; |
| 2110 | spin_unlock_wr(&needsbuffer_spin); | |
| 984263bc MD |
2111 | } |
| 2112 | } | |
| 2113 | ||
| 4b958e7b MD |
2114 | static void |
| 2115 | buf_daemon_hw(void) | |
| 2116 | { | |
| cd083340 MD |
2117 | int limit; |
| 2118 | ||
| 4b958e7b MD |
2119 | /* |
| 2120 | * This process needs to be suspended prior to shutdown sync. | |
| 2121 | */ | |
| 2122 | EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, | |
| 2123 | bufdaemonhw_td, SHUTDOWN_PRI_LAST); | |
| 4ecf7cc9 | 2124 | curthread->td_flags |= TDF_SYSTHREAD; |
| 4b958e7b MD |
2125 | |
| 2126 | /* | |
| 2127 | * This process is allowed to take the buffer cache to the limit | |
| 2128 | */ | |
| 2129 | crit_enter(); | |
| 2130 | ||
| 2131 | for (;;) { | |
| 2132 | kproc_suspend_loop(); | |
| 2133 | ||
| 2134 | /* | |
| 2135 | * Do the flush. Limit the amount of in-transit I/O we | |
| 2136 | * allow to build up, otherwise we would completely saturate | |
| 2137 | * the I/O system. Wakeup any waiting processes before we | |
| 2138 | * normally would so they can run in parallel with our drain. | |
| cd083340 MD |
2139 | * |
| 2140 | * Our aggregate normal+HW lo water mark is lodirtybufspace, | |
| 2141 | * but because we split the operation into two threads we | |
| 2142 | * have to cut it in half for each thread. | |
| 4b958e7b | 2143 | */ |
| cd083340 MD |
2144 | limit = lodirtybufspace / 2; |
| 2145 | waitrunningbufspace(limit); | |
| 70ac7d6c MD |
2146 | while (runningbufspace + dirtybufspacehw > limit || |
| 2147 | dirtybufcounthw >= nbuf / 2) { | |
| 4b958e7b MD |
2148 | if (flushbufqueues(BQUEUE_DIRTY_HW) == 0) |
| 2149 | break; | |
| cd083340 | 2150 | waitrunningbufspace(limit); |
| 1b30fbcc | 2151 | } |
| 4b958e7b MD |
2152 | |
| 2153 | /* | |
| cd083340 MD |
2154 | * We reached our low water mark, reset the |
| 2155 | * request and sleep until we are needed again. | |
| 2156 | * The sleep is just so the suspend code works. | |
| 4b958e7b | 2157 | */ |
| cd083340 MD |
2158 | spin_lock_wr(&needsbuffer_spin); |
| 2159 | if (bd_request_hw == 0) { | |
| 4b958e7b MD |
2160 | msleep(&bd_request_hw, &needsbuffer_spin, 0, |
| 2161 | "psleep", hz); | |
| 4b958e7b | 2162 | } |
| cd083340 MD |
2163 | bd_request_hw = 0; |
| 2164 | spin_unlock_wr(&needsbuffer_spin); | |
| 4b958e7b MD |
2165 | } |
| 2166 | } | |
| 2167 | ||
| 984263bc | 2168 | /* |
| 3f779080 | 2169 | * flushbufqueues: |
| 984263bc MD |
2170 | * |
| 2171 | * Try to flush a buffer in the dirty queue. We must be careful to | |
| 2172 | * free up B_INVAL buffers instead of write them, which NFS is | |
| 2173 | * particularly sensitive to. | |
| b86460bf MD |
2174 | * |
| 2175 | * B_RELBUF may only be set by VFSs. We do set B_AGE to indicate | |
| 2176 | * that we really want to try to get the buffer out and reuse it | |
| 2177 | * due to the write load on the machine. | |
| 984263bc MD |
2178 | */ |
| 2179 | ||
| 2180 | static int | |
| 4b958e7b | 2181 | flushbufqueues(bufq_type_t q) |
| 984263bc MD |
2182 | { |
| 2183 | struct buf *bp; | |
| 2184 | int r = 0; | |
| 2185 | ||
| 4b958e7b | 2186 | bp = TAILQ_FIRST(&bufqueues[q]); |
| 984263bc | 2187 | while (bp) { |
| 408357d8 MD |
2188 | KASSERT((bp->b_flags & B_DELWRI), |
| 2189 | ("unexpected clean buffer %p", bp)); | |
| b86460bf | 2190 | |
| 70371608 | 2191 | if (bp->b_flags & B_DELWRI) { |
| 984263bc MD |
2192 | if (bp->b_flags & B_INVAL) { |
| 2193 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) | |
| 2194 | panic("flushbufqueues: locked buf"); | |
| 2195 | bremfree(bp); | |
| 2196 | brelse(bp); | |
| 2197 | ++r; | |
| 2198 | break; | |
| 2199 | } | |
| 2200 | if (LIST_FIRST(&bp->b_dep) != NULL && | |
| 984263bc | 2201 | (bp->b_flags & B_DEFERRED) == 0 && |
| 408357d8 | 2202 | buf_countdeps(bp, 0)) { |
| 4b958e7b MD |
2203 | TAILQ_REMOVE(&bufqueues[q], bp, b_freelist); |
| 2204 | TAILQ_INSERT_TAIL(&bufqueues[q], bp, | |
| 2205 | b_freelist); | |
| 984263bc | 2206 | bp->b_flags |= B_DEFERRED; |
| 4b958e7b | 2207 | bp = TAILQ_FIRST(&bufqueues[q]); |
| 984263bc MD |
2208 | continue; |
| 2209 | } | |
| 6f68d895 MD |
2210 | |
| 2211 | /* | |
| 2212 | * Only write it out if we can successfully lock | |
| 27bc0cb1 | 2213 | * it. If the buffer has a dependancy, |
| 78a9b77f MD |
2214 | * buf_checkwrite must also return 0 for us to |
| 2215 | * be able to initate the write. | |
| 2216 | * | |
| 2217 | * If the buffer is flagged B_ERROR it may be | |
| 2218 | * requeued over and over again, we try to | |
| 2219 | * avoid a live lock. | |
| 6f68d895 MD |
2220 | */ |
| 2221 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { | |
| 27bc0cb1 MD |
2222 | if (LIST_FIRST(&bp->b_dep) != NULL && |
| 2223 | buf_checkwrite(bp)) { | |
| 2224 | bremfree(bp); | |
| 2225 | brelse(bp); | |
| 78a9b77f MD |
2226 | } else if (bp->b_flags & B_ERROR) { |
| 2227 | tsleep(bp, 0, "bioer", 1); | |
| 2228 | bp->b_flags &= ~B_AGE; | |
| 2229 | vfs_bio_awrite(bp); | |
| 27bc0cb1 | 2230 | } else { |
| b86460bf | 2231 | bp->b_flags |= B_AGE; |
| 27bc0cb1 MD |
2232 | vfs_bio_awrite(bp); |
| 2233 | } | |
| 6f68d895 MD |
2234 | ++r; |
| 2235 | break; | |
| 2236 | } | |
| 984263bc MD |
2237 | } |
| 2238 | bp = TAILQ_NEXT(bp, b_freelist); | |
| 2239 | } | |
| 2240 | return (r); | |
| 2241 | } | |
| 2242 | ||
| 2243 | /* | |
| 3f779080 HP |
2244 | * inmem: |
| 2245 | * | |
| 2246 | * Returns true if no I/O is needed to access the associated VM object. | |
| 1f1ea522 | 2247 | * This is like findblk except it also hunts around in the VM system for |
| 3f779080 | 2248 | * the data. |
| 06ecca5a | 2249 | * |
| 3f779080 HP |
2250 | * Note that we ignore vm_page_free() races from interrupts against our |
| 2251 | * lookup, since if the caller is not protected our return value will not | |
| 2252 | * be any more valid then otherwise once we exit the critical section. | |
| 984263bc | 2253 | */ |
| 984263bc | 2254 | int |
| 54078292 | 2255 | inmem(struct vnode *vp, off_t loffset) |
| 984263bc MD |
2256 | { |
| 2257 | vm_object_t obj; | |
| 2258 | vm_offset_t toff, tinc, size; | |
| 2259 | vm_page_t m; | |
| 984263bc | 2260 | |
| 54078292 | 2261 | if (findblk(vp, loffset)) |
| 984263bc MD |
2262 | return 1; |
| 2263 | if (vp->v_mount == NULL) | |
| 2264 | return 0; | |
| 7540ab49 MD |
2265 | if ((obj = vp->v_object) == NULL) |
| 2266 | return 0; | |
| 984263bc MD |
2267 | |
| 2268 | size = PAGE_SIZE; | |
| 2269 | if (size > vp->v_mount->mnt_stat.f_iosize) | |
| 2270 | size = vp->v_mount->mnt_stat.f_iosize; | |
| 984263bc MD |
2271 | |
| 2272 | for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) { | |
| 54078292 MD |
2273 | m = vm_page_lookup(obj, OFF_TO_IDX(loffset + toff)); |
| 2274 | if (m == NULL) | |
| 984263bc MD |
2275 | return 0; |
| 2276 | tinc = size; | |
| 54078292 MD |
2277 | if (tinc > PAGE_SIZE - ((toff + loffset) & PAGE_MASK)) |
| 2278 | tinc = PAGE_SIZE - ((toff + loffset) & PAGE_MASK); | |
| 984263bc | 2279 | if (vm_page_is_valid(m, |
| 54078292 | 2280 | (vm_offset_t) ((toff + loffset) & PAGE_MASK), tinc) == 0) |
| 984263bc MD |
2281 | return 0; |
| 2282 | } | |
| 2283 | return 1; | |
| 2284 | } | |
| 2285 | ||
| 2286 | /* | |
| 3f779080 | 2287 | * vfs_setdirty: |
| 984263bc MD |
2288 | * |
| 2289 | * Sets the dirty range for a buffer based on the status of the dirty | |
| 2290 | * bits in the pages comprising the buffer. | |
| 2291 | * | |
| 2292 | * The range is limited to the size of the buffer. | |
| 2293 | * | |
| 2294 | * This routine is primarily used by NFS, but is generalized for the | |
| 2295 | * B_VMIO case. | |
| 2296 | */ | |
| 2297 | static void | |
| 2298 | vfs_setdirty(struct buf *bp) | |
| 2299 | { | |
| 2300 | int i; | |
| 2301 | vm_object_t object; | |
| 2302 | ||
| 2303 | /* | |
| 2304 | * Degenerate case - empty buffer | |
| 2305 | */ | |
| 2306 | ||
| 2307 | if (bp->b_bufsize == 0) | |
| 2308 | return; | |
| 2309 | ||
| 2310 | /* | |
| 2311 | * We qualify the scan for modified pages on whether the | |
| 2312 | * object has been flushed yet. The OBJ_WRITEABLE flag | |
| 2313 | * is not cleared simply by protecting pages off. | |
| 2314 | */ | |
| 2315 | ||
| 2316 | if ((bp->b_flags & B_VMIO) == 0) | |
| 2317 | return; | |
| 2318 | ||
| 54f51aeb | 2319 | object = bp->b_xio.xio_pages[0]->object; |
| 984263bc MD |
2320 | |
| 2321 | if ((object->flags & OBJ_WRITEABLE) && !(object->flags & OBJ_MIGHTBEDIRTY)) | |
| 6ea70f76 | 2322 | kprintf("Warning: object %p writeable but not mightbedirty\n", object); |
| 984263bc | 2323 | if (!(object->flags & OBJ_WRITEABLE) && (object->flags & OBJ_MIGHTBEDIRTY)) |
| 6ea70f76 | 2324 | kprintf("Warning: object %p mightbedirty but not writeable\n", object); |
| 984263bc MD |
2325 | |
| 2326 | if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) { | |
| 2327 | vm_offset_t boffset; | |
| 2328 | vm_offset_t eoffset; | |
| 2329 | ||
| 2330 | /* | |
| 2331 | * test the pages to see if they have been modified directly | |
| 2332 | * by users through the VM system. | |
| 2333 | */ | |
| 54f51aeb HP |
2334 | for (i = 0; i < bp->b_xio.xio_npages; i++) { |
| 2335 | vm_page_flag_clear(bp->b_xio.xio_pages[i], PG_ZERO); | |
| 2336 | vm_page_test_dirty(bp->b_xio.xio_pages[i]); | |
| 984263bc MD |
2337 | } |
| 2338 | ||
| 2339 | /* | |
| 2340 | * Calculate the encompassing dirty range, boffset and eoffset, | |
| 2341 | * (eoffset - boffset) bytes. | |
| 2342 | */ | |
| 2343 | ||
| 54f51aeb HP |
2344 | for (i = 0; i < bp->b_xio.xio_npages; i++) { |
| 2345 | if (bp->b_xio.xio_pages[i]->dirty) | |
| 984263bc MD |
2346 | break; |
| 2347 | } | |
| 81b5c339 | 2348 | boffset = (i << PAGE_SHIFT) - (bp->b_loffset & PAGE_MASK); |
| 984263bc | 2349 | |
| 54f51aeb HP |
2350 | for (i = bp->b_xio.xio_npages - 1; i >= 0; --i) { |
| 2351 | if (bp->b_xio.xio_pages[i]->dirty) { | |
| 984263bc MD |
2352 | break; |
| 2353 | } | |
| 2354 | } | |
| 81b5c339 | 2355 | eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_loffset & PAGE_MASK); |
| 984263bc MD |
2356 | |
| 2357 | /* | |
| 2358 | * Fit it to the buffer. | |
| 2359 | */ | |
| 2360 | ||
| 2361 | if (eoffset > bp->b_bcount) | |
| 2362 | eoffset = bp->b_bcount; | |
| 2363 | ||
| 2364 | /* | |
| 2365 | * If we have a good dirty range, merge with the existing | |
| 2366 | * dirty range. | |
| 2367 | */ | |
| 2368 | ||
| 2369 | if (boffset < eoffset) { | |
| 2370 | if (bp->b_dirtyoff > boffset) | |
| 2371 | bp->b_dirtyoff = boffset; | |
| 2372 | if (bp->b_dirtyend < eoffset) | |
| 2373 | bp->b_dirtyend = eoffset; | |
| 2374 | } | |
| 2375 | } | |
| 2376 | } | |
| 2377 | ||
| 2378 | /* | |
| 1f1ea522 MD |
2379 | * findblk: |
| 2380 | * | |
| 2381 | * Locate and return the specified buffer, or NULL if the buffer does | |
| 2382 | * not exist. Do not attempt to lock the buffer or manipulate it in | |
| 2383 | * any way. The caller must validate that the correct buffer has been | |
| 2384 | * obtain after locking it. | |
| 2385 | */ | |
| 2386 | struct buf * | |
| 54078292 | 2387 | findblk(struct vnode *vp, off_t loffset) |
| 1f1ea522 MD |
2388 | { |
| 2389 | struct buf *bp; | |
| 2390 | ||
| 2391 | crit_enter(); | |
| 54078292 | 2392 | bp = buf_rb_hash_RB_LOOKUP(&vp->v_rbhash_tree, loffset); |
| 1f1ea522 MD |
2393 | crit_exit(); |
| 2394 | return(bp); | |
| 2395 | } | |
| 2396 | ||
| 2397 | /* | |
| 3f779080 | 2398 | * getblk: |
| 984263bc MD |
2399 | * |
| 2400 | * Get a block given a specified block and offset into a file/device. | |
| 10f3fee5 MD |
2401 | * B_INVAL may or may not be set on return. The caller should clear |
| 2402 | * B_INVAL prior to initiating a READ. | |
| 984263bc | 2403 | * |
| 77bb9400 MD |
2404 | * IT IS IMPORTANT TO UNDERSTAND THAT IF YOU CALL GETBLK() AND B_CACHE |
| 2405 | * IS NOT SET, YOU MUST INITIALIZE THE RETURNED BUFFER, ISSUE A READ, | |
| 2406 | * OR SET B_INVAL BEFORE RETIRING IT. If you retire a getblk'd buffer | |
| 2407 | * without doing any of those things the system will likely believe | |
| 2408 | * the buffer to be valid (especially if it is not B_VMIO), and the | |
| 2409 | * next getblk() will return the buffer with B_CACHE set. | |
| 2410 | * | |
| 984263bc MD |
2411 | * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for |
| 2412 | * an existing buffer. | |
| 2413 | * | |
| 2414 | * For a VMIO buffer, B_CACHE is modified according to the backing VM. | |
| 2415 | * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set | |
| 2416 | * and then cleared based on the backing VM. If the previous buffer is | |
| 2417 | * non-0-sized but invalid, B_CACHE will be cleared. | |
| 2418 | * | |
| 2419 | * If getblk() must create a new buffer, the new buffer is returned with | |
| 2420 | * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which | |
| 2421 | * case it is returned with B_INVAL clear and B_CACHE set based on the | |
| 2422 | * backing VM. | |
| 2423 | * | |
| 62cfda27 | 2424 | * getblk() also forces a bwrite() for any B_DELWRI buffer whos |
| 984263bc MD |
2425 | * B_CACHE bit is clear. |
| 2426 | * | |
| 2427 | * What this means, basically, is that the caller should use B_CACHE to | |
| 2428 | * determine whether the buffer is fully valid or not and should clear | |
| 2429 | * B_INVAL prior to issuing a read. If the caller intends to validate | |
| 2430 | * the buffer by loading its data area with something, the caller needs | |
| 2431 | * to clear B_INVAL. If the caller does this without issuing an I/O, | |
| 2432 | * the caller should set B_CACHE ( as an optimization ), else the caller | |
| 2433 | * should issue the I/O and biodone() will set B_CACHE if the I/O was | |
| 2434 | * a write attempt or if it was a successfull read. If the caller | |
| 2435 | * intends to issue a READ, the caller must clear B_INVAL and B_ERROR | |
| 2436 | * prior to issuing the READ. biodone() will *not* clear B_INVAL. | |
| 4b958e7b MD |
2437 | * |
| 2438 | * getblk flags: | |
| 2439 | * | |
| 2440 | * GETBLK_PCATCH - catch signal if blocked, can cause NULL return | |
| 2441 | * GETBLK_BHEAVY - heavy-weight buffer cache buffer | |
| 984263bc MD |
2442 | */ |
| 2443 | struct buf * | |
| 4b958e7b | 2444 | getblk(struct vnode *vp, off_t loffset, int size, int blkflags, int slptimeo) |
| 984263bc MD |
2445 | { |
| 2446 | struct buf *bp; | |
| 4b958e7b | 2447 | int slpflags = (blkflags & GETBLK_PCATCH) ? PCATCH : 0; |
| e92ca23a | 2448 | int error; |
| 984263bc MD |
2449 | |
| 2450 | if (size > MAXBSIZE) | |
| fc92d4aa | 2451 | panic("getblk: size(%d) > MAXBSIZE(%d)", size, MAXBSIZE); |
| 7540ab49 MD |
2452 | if (vp->v_object == NULL) |
| 2453 | panic("getblk: vnode %p has no object!", vp); | |
| 984263bc | 2454 | |
| e43a034f | 2455 | crit_enter(); |
| 984263bc | 2456 | loop: |
| 54078292 | 2457 | if ((bp = findblk(vp, loffset))) { |
| 984263bc | 2458 | /* |
| a0da602d MD |
2459 | * The buffer was found in the cache, but we need to lock it. |
| 2460 | * Even with LK_NOWAIT the lockmgr may break our critical | |
| 2461 | * section, so double-check the validity of the buffer | |
| 2462 | * once the lock has been obtained. | |
| 984263bc | 2463 | */ |
| 984263bc | 2464 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { |
| b77cfc40 MD |
2465 | if (blkflags & GETBLK_NOWAIT) { |
| 2466 | crit_exit(); | |
| 2467 | return(NULL); | |
| 2468 | } | |
| f2770c70 | 2469 | int lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL; |
| 4b958e7b | 2470 | if (blkflags & GETBLK_PCATCH) |
| f2770c70 | 2471 | lkflags |= LK_PCATCH; |
| e92ca23a MD |
2472 | error = BUF_TIMELOCK(bp, lkflags, "getblk", slptimeo); |
| 2473 | if (error) { | |
| 2474 | if (error == ENOLCK) | |
| 2475 | goto loop; | |
| 2476 | crit_exit(); | |
| 2477 | return (NULL); | |
| f2770c70 | 2478 | } |
| 984263bc MD |
2479 | } |
| 2480 | ||
| 2481 | /* | |
| a0da602d MD |
2482 | * Once the buffer has been locked, make sure we didn't race |
| 2483 | * a buffer recyclement. Buffers that are no longer hashed | |
| 2484 | * will have b_vp == NULL, so this takes care of that check | |
| 2485 | * as well. | |
| 2486 | */ | |
| 54078292 | 2487 | if (bp->b_vp != vp || bp->b_loffset != loffset) { |
| 973c11b9 MD |
2488 | kprintf("Warning buffer %p (vp %p loffset %lld) " |
| 2489 | "was recycled\n", | |
| 2490 | bp, vp, (long long)loffset); | |
| a9518ecf | 2491 | BUF_UNLOCK(bp); |
| a0da602d MD |
2492 | goto loop; |
| 2493 | } | |
| 2494 | ||
| 2495 | /* | |
| b77cfc40 MD |
2496 | * If SZMATCH any pre-existing buffer must be of the requested |
| 2497 | * size or NULL is returned. The caller absolutely does not | |
| 2498 | * want getblk() to bwrite() the buffer on a size mismatch. | |
| 2499 | */ | |
| 2500 | if ((blkflags & GETBLK_SZMATCH) && size != bp->b_bcount) { | |
| 2501 | BUF_UNLOCK(bp); | |
| 2502 | crit_exit(); | |
| 2503 | return(NULL); | |
| 2504 | } | |
| 2505 | ||
| 2506 | /* | |
| 4baec531 MD |
2507 | * All vnode-based buffers must be backed by a VM object. |
| 2508 | */ | |
| 2509 | KKASSERT(bp->b_flags & B_VMIO); | |
| 10f3fee5 | 2510 | KKASSERT(bp->b_cmd == BUF_CMD_DONE); |
| b86460bf | 2511 | bp->b_flags &= ~B_AGE; |
| 4baec531 MD |
2512 | |
| 2513 | /* | |
| a0da602d MD |
2514 | * Make sure that B_INVAL buffers do not have a cached |
| 2515 | * block number translation. | |
| 2516 | */ | |
| 54078292 | 2517 | if ((bp->b_flags & B_INVAL) && (bp->b_bio2.bio_offset != NOOFFSET)) { |
| 973c11b9 MD |
2518 | kprintf("Warning invalid buffer %p (vp %p loffset %lld)" |
| 2519 | " did not have cleared bio_offset cache\n", | |
| 2520 | bp, vp, (long long)loffset); | |
| 81b5c339 | 2521 | clearbiocache(&bp->b_bio2); |
| a0da602d MD |
2522 | } |
| 2523 | ||
| 2524 | /* | |
| 984263bc | 2525 | * The buffer is locked. B_CACHE is cleared if the buffer is |
| 4baec531 | 2526 | * invalid. |
| 984263bc MD |
2527 | */ |
| 2528 | if (bp->b_flags & B_INVAL) | |
| 2529 | bp->b_flags &= ~B_CACHE; | |
| 984263bc MD |
2530 | bremfree(bp); |
| 2531 | ||
| 2532 | /* | |
| 4baec531 MD |
2533 | * Any size inconsistancy with a dirty buffer or a buffer |
| 2534 | * with a softupdates dependancy must be resolved. Resizing | |
| 2535 | * the buffer in such circumstances can lead to problems. | |
| 984263bc | 2536 | */ |
| 4baec531 MD |
2537 | if (size != bp->b_bcount) { |
| 2538 | if (bp->b_flags & B_DELWRI) { | |
| 2539 | bp->b_flags |= B_NOCACHE; | |
| 62cfda27 | 2540 | bwrite(bp); |
| 4baec531 MD |
2541 | } else if (LIST_FIRST(&bp->b_dep)) { |
| 2542 | bp->b_flags |= B_NOCACHE; | |
| 62cfda27 | 2543 | bwrite(bp); |
| 4baec531 MD |
2544 | } else { |
| 2545 | bp->b_flags |= B_RELBUF; | |
| 2546 | brelse(bp); | |
| 984263bc | 2547 | } |
| 4baec531 | 2548 | goto loop; |
| 984263bc | 2549 | } |
| 4baec531 | 2550 | KKASSERT(size <= bp->b_kvasize); |
| 81b5c339 MD |
2551 | KASSERT(bp->b_loffset != NOOFFSET, |
| 2552 | ("getblk: no buffer offset")); | |
| 984263bc MD |
2553 | |
| 2554 | /* | |
| 2555 | * A buffer with B_DELWRI set and B_CACHE clear must | |
| 2556 | * be committed before we can return the buffer in | |
| 2557 | * order to prevent the caller from issuing a read | |
| 2558 | * ( due to B_CACHE not being set ) and overwriting | |
| 2559 | * it. | |
| 2560 | * | |
| 2561 | * Most callers, including NFS and FFS, need this to | |
| 2562 | * operate properly either because they assume they | |
| 2563 | * can issue a read if B_CACHE is not set, or because | |
| 2564 | * ( for example ) an uncached B_DELWRI might loop due | |
| 2565 | * to softupdates re-dirtying the buffer. In the latter | |
| 2566 | * case, B_CACHE is set after the first write completes, | |
| 2567 | * preventing further loops. | |
| 2568 | * | |
| 2569 | * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE | |
| 2570 | * above while extending the buffer, we cannot allow the | |
| 2571 | * buffer to remain with B_CACHE set after the write | |
| 2572 | * completes or it will represent a corrupt state. To | |
| 2573 | * deal with this we set B_NOCACHE to scrap the buffer | |
| 2574 | * after the write. | |
| 2575 | * | |
| 2576 | * We might be able to do something fancy, like setting | |
| 2577 | * B_CACHE in bwrite() except if B_DELWRI is already set, | |
| 2578 | * so the below call doesn't set B_CACHE, but that gets real | |
| 2579 | * confusing. This is much easier. | |
| 2580 | */ | |
| 2581 | ||
| 2582 | if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) { | |
| 2583 | bp->b_flags |= B_NOCACHE; | |
| 62cfda27 | 2584 | bwrite(bp); |
| 984263bc MD |
2585 | goto loop; |
| 2586 | } | |
| e43a034f | 2587 | crit_exit(); |
| 984263bc MD |
2588 | } else { |
| 2589 | /* | |
| 2590 | * Buffer is not in-core, create new buffer. The buffer | |
| 2591 | * returned by getnewbuf() is locked. Note that the returned | |
| 2592 | * buffer is also considered valid (not marked B_INVAL). | |
| 21ab32bd MD |
2593 | * |
| 2594 | * Calculating the offset for the I/O requires figuring out | |
| 2595 | * the block size. We use DEV_BSIZE for VBLK or VCHR and | |
| 2596 | * the mount's f_iosize otherwise. If the vnode does not | |
| 2597 | * have an associated mount we assume that the passed size is | |
| 2598 | * the block size. | |
| 2599 | * | |
| 2600 | * Note that vn_isdisk() cannot be used here since it may | |
| 2601 | * return a failure for numerous reasons. Note that the | |
| 2602 | * buffer size may be larger then the block size (the caller | |
| 2603 | * will use block numbers with the proper multiple). Beware | |
| 2604 | * of using any v_* fields which are part of unions. In | |
| 2605 | * particular, in DragonFly the mount point overloading | |
| 1d505369 MD |
2606 | * mechanism uses the namecache only and the underlying |
| 2607 | * directory vnode is not a special case. | |
| 984263bc | 2608 | */ |
| 7540ab49 | 2609 | int bsize, maxsize; |
| 984263bc | 2610 | |
| 21ab32bd | 2611 | if (vp->v_type == VBLK || vp->v_type == VCHR) |
| 984263bc | 2612 | bsize = DEV_BSIZE; |
| 984263bc MD |
2613 | else if (vp->v_mount) |
| 2614 | bsize = vp->v_mount->mnt_stat.f_iosize; | |
| 2615 | else | |
| 2616 | bsize = size; | |
| 2617 | ||
| 7540ab49 | 2618 | maxsize = size + (loffset & PAGE_MASK); |
| 984263bc MD |
2619 | maxsize = imax(maxsize, bsize); |
| 2620 | ||
| 4b958e7b MD |
2621 | if ((bp = getnewbuf(blkflags, slptimeo, size, maxsize)) == NULL) { |
| 2622 | if (slpflags || slptimeo) { | |
| e43a034f | 2623 | crit_exit(); |
| 984263bc MD |
2624 | return NULL; |
| 2625 | } | |
| 2626 | goto loop; | |
| 2627 | } | |
| 2628 | ||
| 2629 | /* | |
| 2630 | * This code is used to make sure that a buffer is not | |
| 2631 | * created while the getnewbuf routine is blocked. | |
| 2632 | * This can be a problem whether the vnode is locked or not. | |
| 2633 | * If the buffer is created out from under us, we have to | |
| 179024a5 | 2634 | * throw away the one we just created. There is no window |
| e43a034f MD |
2635 | * race because we are safely running in a critical section |
| 2636 | * from the point of the duplicate buffer creation through | |
| 2637 | * to here, and we've locked the buffer. | |
| 984263bc | 2638 | */ |
| 54078292 | 2639 | if (findblk(vp, loffset)) { |
| 984263bc MD |
2640 | bp->b_flags |= B_INVAL; |
| 2641 | brelse(bp); | |
| 2642 | goto loop; | |
| 2643 | } | |
| 2644 | ||
| 2645 | /* | |
| 2646 | * Insert the buffer into the hash, so that it can | |
| 1f1ea522 MD |
2647 | * be found by findblk(). |
| 2648 | * | |
| 2649 | * Make sure the translation layer has been cleared. | |
| 984263bc | 2650 | */ |
| 54078292 MD |
2651 | bp->b_loffset = loffset; |
| 2652 | bp->b_bio2.bio_offset = NOOFFSET; | |
| 1f1ea522 | 2653 | /* bp->b_bio2.bio_next = NULL; */ |
| 984263bc MD |
2654 | |
| 2655 | bgetvp(vp, bp); | |
| 984263bc MD |
2656 | |
| 2657 | /* | |
| 4baec531 | 2658 | * All vnode-based buffers must be backed by a VM object. |
| 984263bc | 2659 | */ |
| 4baec531 MD |
2660 | KKASSERT(vp->v_object != NULL); |
| 2661 | bp->b_flags |= B_VMIO; | |
| 10f3fee5 | 2662 | KKASSERT(bp->b_cmd == BUF_CMD_DONE); |
| 984263bc MD |
2663 | |
| 2664 | allocbuf(bp, size); | |
| 2665 | ||
| e43a034f | 2666 | crit_exit(); |
| 984263bc MD |
2667 | } |
| 2668 | return (bp); | |
| 2669 | } | |
| 2670 | ||
| 2671 | /* | |
| 5e23ca53 MD |
2672 | * regetblk(bp) |
| 2673 | * | |
| 27bc0cb1 MD |
2674 | * Reacquire a buffer that was previously released to the locked queue, |
| 2675 | * or reacquire a buffer which is interlocked by having bioops->io_deallocate | |
| 2676 | * set B_LOCKED (which handles the acquisition race). | |
| 5e23ca53 | 2677 | * |
| 27bc0cb1 MD |
2678 | * To this end, either B_LOCKED must be set or the dependancy list must be |
| 2679 | * non-empty. | |
| 5e23ca53 MD |
2680 | */ |
| 2681 | void | |
| 2682 | regetblk(struct buf *bp) | |
| 2683 | { | |
| 27bc0cb1 | 2684 | KKASSERT((bp->b_flags & B_LOCKED) || LIST_FIRST(&bp->b_dep) != NULL); |
| 5e23ca53 MD |
2685 | BUF_LOCK(bp, LK_EXCLUSIVE | LK_RETRY); |
| 2686 | crit_enter(); | |
| 2687 | bremfree(bp); | |
| 2688 | crit_exit(); | |
| 2689 | } | |
| 2690 | ||
| 2691 | /* | |
| 3f779080 HP |
2692 | * geteblk: |
| 2693 | * | |
| 2694 | * Get an empty, disassociated buffer of given size. The buffer is | |
| 2695 | * initially set to B_INVAL. | |
| 06ecca5a | 2696 | * |
| 3f779080 HP |
2697 | * critical section protection is not required for the allocbuf() |
| 2698 | * call because races are impossible here. | |
| 984263bc MD |
2699 | */ |
| 2700 | struct buf * | |
| 2701 | geteblk(int size) | |
| 2702 | { | |
| 2703 | struct buf *bp; | |
| 984263bc MD |
2704 | int maxsize; |
| 2705 | ||
| 2706 | maxsize = (size + BKVAMASK) & ~BKVAMASK; | |
| 2707 | ||
| e43a034f MD |
2708 | crit_enter(); |
| 2709 | while ((bp = getnewbuf(0, 0, size, maxsize)) == 0) | |
| 2710 | ; | |
| 2711 | crit_exit(); | |
| 984263bc MD |
2712 | allocbuf(bp, size); |
| 2713 | bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */ | |
| 2714 | return (bp); | |
| 2715 | } | |
| 2716 | ||
| 2717 | ||
| 2718 | /* | |
| 3f779080 | 2719 | * allocbuf: |
| 984263bc | 2720 | * |
| 3f779080 HP |
2721 | * This code constitutes the buffer memory from either anonymous system |
| 2722 | * memory (in the case of non-VMIO operations) or from an associated | |
| 2723 | * VM object (in the case of VMIO operations). This code is able to | |
| 2724 | * resize a buffer up or down. | |
| 984263bc | 2725 | * |
| 3f779080 HP |
2726 | * Note that this code is tricky, and has many complications to resolve |
| 2727 | * deadlock or inconsistant data situations. Tread lightly!!! | |
| 2728 | * There are B_CACHE and B_DELWRI interactions that must be dealt with by | |
| 2729 | * the caller. Calling this code willy nilly can result in the loss of data. | |
| 06ecca5a | 2730 | * |
| 3f779080 HP |
2731 | * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with |
| 2732 | * B_CACHE for the non-VMIO case. | |
| 2733 | * | |
| 2734 | * This routine does not need to be called from a critical section but you | |
| 2735 | * must own the buffer. | |
| 984263bc | 2736 | */ |
| 984263bc MD |
2737 | int |
| 2738 | allocbuf(struct buf *bp, int size) | |
| 2739 | { | |
| 2740 | int newbsize, mbsize; | |
| 2741 | int i; | |
| 2742 | ||
| 2743 | if (BUF_REFCNT(bp) == 0) | |
| 2744 | panic("allocbuf: buffer not busy"); | |
| 2745 | ||
| 2746 | if (bp->b_kvasize < size) | |
| 2747 | panic("allocbuf: buffer too small"); | |
| 2748 | ||
| 2749 | if ((bp->b_flags & B_VMIO) == 0) { | |
| 2750 | caddr_t origbuf; | |
| 2751 | int origbufsize; | |
| 2752 | /* | |
| 2753 | * Just get anonymous memory from the kernel. Don't | |
| 2754 | * mess with B_CACHE. | |
| 2755 | */ | |
| 2756 | mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); | |
| 984263bc MD |
2757 | if (bp->b_flags & B_MALLOC) |
| 2758 | newbsize = mbsize; | |
| 2759 | else | |
| 984263bc MD |
2760 | newbsize = round_page(size); |
| 2761 | ||
| 2762 | if (newbsize < bp->b_bufsize) { | |
| 984263bc | 2763 | /* |
| 312dcd01 | 2764 | * Malloced buffers are not shrunk |
| 984263bc MD |
2765 | */ |
| 2766 | if (bp->b_flags & B_MALLOC) { | |
| 2767 | if (newbsize) { | |
| 2768 | bp->b_bcount = size; | |
| 2769 | } else { | |
| efda3bd0 | 2770 | kfree(bp->b_data, M_BIOBUF); |
| 984263bc MD |
2771 | if (bp->b_bufsize) { |
| 2772 | bufmallocspace -= bp->b_bufsize; | |
| 2773 | bufspacewakeup(); | |
| 2774 | bp->b_bufsize = 0; | |
| 2775 | } | |
| 2776 | bp->b_data = bp->b_kvabase; | |
| 2777 | bp->b_bcount = 0; | |
| 2778 | bp->b_flags &= ~B_MALLOC; | |
| 2779 | } | |
| 2780 | return 1; | |
| 2781 | } | |
| 984263bc MD |
2782 | vm_hold_free_pages( |
| 2783 | bp, | |
| 2784 | (vm_offset_t) bp->b_data + newbsize, | |
| 2785 | (vm_offset_t) bp->b_data + bp->b_bufsize); | |
| 2786 | } else if (newbsize > bp->b_bufsize) { | |
| 984263bc MD |
2787 | /* |
| 2788 | * We only use malloced memory on the first allocation. | |
| 2789 | * and revert to page-allocated memory when the buffer | |
| 2790 | * grows. | |
| 2791 | */ | |
| 4baec531 | 2792 | if ((bufmallocspace < maxbufmallocspace) && |
| 984263bc MD |
2793 | (bp->b_bufsize == 0) && |
| 2794 | (mbsize <= PAGE_SIZE/2)) { | |
| 2795 | ||
| efda3bd0 | 2796 | bp->b_data = kmalloc(mbsize, M_BIOBUF, M_WAITOK); |
| 984263bc MD |
2797 | bp->b_bufsize = mbsize; |
| 2798 | bp->b_bcount = size; | |
| 2799 | bp->b_flags |= B_MALLOC; | |
| 2800 | bufmallocspace += mbsize; | |
| 2801 | return 1; | |
| 2802 | } | |
| 984263bc MD |
2803 | origbuf = NULL; |
| 2804 | origbufsize = 0; | |
| 984263bc | 2805 | /* |
| 4baec531 MD |
2806 | * If the buffer is growing on its other-than-first |
| 2807 | * allocation, then we revert to the page-allocation | |
| 2808 | * scheme. | |
| 984263bc MD |
2809 | */ |
| 2810 | if (bp->b_flags & B_MALLOC) { | |
| 2811 | origbuf = bp->b_data; | |
| 2812 | origbufsize = bp->b_bufsize; | |
| 2813 | bp->b_data = bp->b_kvabase; | |
| 2814 | if (bp->b_bufsize) { | |
| 2815 | bufmallocspace -= bp->b_bufsize; | |
| 2816 | bufspacewakeup(); | |
| 2817 | bp->b_bufsize = 0; | |
| 2818 | } | |
| 2819 | bp->b_flags &= ~B_MALLOC; | |
| 2820 | newbsize = round_page(newbsize); | |
| 2821 | } | |
| 984263bc MD |
2822 | vm_hold_load_pages( |
| 2823 | bp, | |
| 2824 | (vm_offset_t) bp->b_data + bp->b_bufsize, | |
| 2825 | (vm_offset_t) bp->b_data + newbsize); | |
| 984263bc MD |
2826 | if (origbuf) { |
| 2827 | bcopy(origbuf, bp->b_data, origbufsize); | |
| efda3bd0 | 2828 | kfree(origbuf, M_BIOBUF); |
| 984263bc | 2829 | } |
| 984263bc MD |
2830 | } |
| 2831 | } else { | |
| 2832 | vm_page_t m; | |
| 2833 | int desiredpages; | |
| 2834 | ||
| 2835 | newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); | |
| 4baec531 MD |
2836 | desiredpages = ((int)(bp->b_loffset & PAGE_MASK) + |
| 2837 | newbsize + PAGE_MASK) >> PAGE_SHIFT; | |
| 2838 | KKASSERT(desiredpages <= XIO_INTERNAL_PAGES); | |
| 984263bc | 2839 | |
| 984263bc MD |
2840 | if (bp->b_flags & B_MALLOC) |
| 2841 | panic("allocbuf: VMIO buffer can't be malloced"); | |
| 984263bc MD |
2842 | /* |
| 2843 | * Set B_CACHE initially if buffer is 0 length or will become | |
| 2844 | * 0-length. | |
| 2845 | */ | |
| 2846 | if (size == 0 || bp->b_bufsize == 0) | |
| 2847 | bp->b_flags |= B_CACHE; | |
| 2848 | ||
| 2849 | if (newbsize < bp->b_bufsize) { | |
| 2850 | /* | |
| 2851 | * DEV_BSIZE aligned new buffer size is less then the | |
| 2852 | * DEV_BSIZE aligned existing buffer size. Figure out | |
| 2853 | * if we have to remove any pages. | |
| 2854 | */ | |
| 54f51aeb HP |
2855 | if (desiredpages < bp->b_xio.xio_npages) { |
| 2856 | for (i = desiredpages; i < bp->b_xio.xio_npages; i++) { | |
| 984263bc MD |
2857 | /* |
| 2858 | * the page is not freed here -- it | |
| 2859 | * is the responsibility of | |
| 2860 | * vnode_pager_setsize | |
| 2861 | */ | |
| 54f51aeb | 2862 | m = bp->b_xio.xio_pages[i]; |
| 984263bc MD |
2863 | KASSERT(m != bogus_page, |
| 2864 | ("allocbuf: bogus page found")); | |
| 2865 | while (vm_page_sleep_busy(m, TRUE, "biodep")) | |
| 2866 | ; | |
| 2867 | ||
| 54f51aeb | 2868 | bp->b_xio.xio_pages[i] = NULL; |
| 984263bc MD |
2869 | vm_page_unwire(m, 0); |
| 2870 | } | |
| 2871 | pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) + | |
| 54f51aeb HP |
2872 | (desiredpages << PAGE_SHIFT), (bp->b_xio.xio_npages - desiredpages)); |
| 2873 | bp->b_xio.xio_npages = desiredpages; | |
| 984263bc MD |
2874 | } |
| 2875 | } else if (size > bp->b_bcount) { | |
| 2876 | /* | |
| 2877 | * We are growing the buffer, possibly in a | |
| 2878 | * byte-granular fashion. | |
| 2879 | */ | |
| 2880 | struct vnode *vp; | |
| 2881 | vm_object_t obj; | |
| 2882 | vm_offset_t toff; | |
| 2883 | vm_offset_t tinc; | |
| 2884 | ||
| 2885 | /* | |
| 2886 | * Step 1, bring in the VM pages from the object, | |
| 2887 | * allocating them if necessary. We must clear | |
| 2888 | * B_CACHE if these pages are not valid for the | |
| 2889 | * range covered by the buffer. | |
| 06ecca5a | 2890 | * |
| e43a034f MD |
2891 | * critical section protection is required to protect |
| 2892 | * against interrupts unbusying and freeing pages | |
| 2893 | * between our vm_page_lookup() and our | |
| 2894 | * busycheck/wiring call. | |
| 984263bc | 2895 | */ |
| 984263bc | 2896 | vp = bp->b_vp; |
| 7540ab49 | 2897 | obj = vp->v_object; |
| 984263bc | 2898 | |
| 654a39f0 | 2899 | crit_enter(); |
| 54f51aeb | 2900 | while (bp->b_xio.xio_npages < desiredpages) { |
| 984263bc MD |
2901 | vm_page_t m; |
| 2902 | vm_pindex_t pi; | |
| 2903 | ||
| 81b5c339 | 2904 | pi = OFF_TO_IDX(bp->b_loffset) + bp->b_xio.xio_npages; |
| 984263bc MD |
2905 | if ((m = vm_page_lookup(obj, pi)) == NULL) { |
| 2906 | /* | |
| 2907 | * note: must allocate system pages | |
| 2908 | * since blocking here could intefere | |
| 2909 | * with paging I/O, no matter which | |
| 2910 | * process we are. | |
| 2911 | */ | |
| 4ecf7cc9 MD |
2912 | m = bio_page_alloc(obj, pi, desiredpages - bp->b_xio.xio_npages); |
| 2913 | if (m) { | |
| 984263bc MD |
2914 | vm_page_wire(m); |
| 2915 | vm_page_wakeup(m); | |
| 2916 | bp->b_flags &= ~B_CACHE; | |
| 54f51aeb HP |
2917 | bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m; |
| 2918 | ++bp->b_xio.xio_npages; | |
| 984263bc MD |
2919 | } |
| 2920 | continue; | |
| 2921 | } | |
| 2922 | ||
| 2923 | /* | |
| 2924 | * We found a page. If we have to sleep on it, | |
| 2925 | * retry because it might have gotten freed out | |
| 2926 | * from under us. | |
| 2927 | * | |
| 2928 | * We can only test PG_BUSY here. Blocking on | |
| 2929 | * m->busy might lead to a deadlock: | |
| 2930 | * | |
| 2931 | * vm_fault->getpages->cluster_read->allocbuf | |
| 2932 | * | |
| 2933 | */ | |
| 2934 | ||
| 2935 | if (vm_page_sleep_busy(m, FALSE, "pgtblk")) | |
| 2936 | continue; | |
| 984263bc MD |
2937 | vm_page_flag_clear(m, PG_ZERO); |
| 2938 | vm_page_wire(m); | |
| 54f51aeb HP |
2939 | bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m; |
| 2940 | ++bp->b_xio.xio_npages; | |
| 984263bc | 2941 | } |
| 654a39f0 | 2942 | crit_exit(); |
| 984263bc MD |
2943 | |
| 2944 | /* | |
| 2945 | * Step 2. We've loaded the pages into the buffer, | |
| 2946 | * we have to figure out if we can still have B_CACHE | |
| 2947 | * set. Note that B_CACHE is set according to the | |
| 3f779080 | 2948 | * byte-granular range ( bcount and size ), not the |
| 984263bc MD |
2949 | * aligned range ( newbsize ). |
| 2950 | * | |
| 2951 | * The VM test is against m->valid, which is DEV_BSIZE | |
| 2952 | * aligned. Needless to say, the validity of the data | |
| 2953 | * needs to also be DEV_BSIZE aligned. Note that this | |
| 2954 | * fails with NFS if the server or some other client | |
| 2955 | * extends the file's EOF. If our buffer is resized, | |
| 2956 | * B_CACHE may remain set! XXX | |
| 2957 | */ | |
| 2958 | ||
| 2959 | toff = bp->b_bcount; | |
| 81b5c339 | 2960 | tinc = PAGE_SIZE - ((bp->b_loffset + toff) & PAGE_MASK); |
| 984263bc MD |
2961 | |
| 2962 | while ((bp->b_flags & B_CACHE) && toff < size) { | |
| 2963 | vm_pindex_t pi; | |
| 2964 | ||
| 2965 | if (tinc > (size - toff)) | |
| 2966 | tinc = size - toff; | |
| 2967 | ||
| 81b5c339 | 2968 | pi = ((bp->b_loffset & PAGE_MASK) + toff) >> |
| 984263bc MD |
2969 | PAGE_SHIFT; |
| 2970 | ||
| 2971 | vfs_buf_test_cache( | |
| 2972 | bp, | |
| 81b5c339 | 2973 | bp->b_loffset, |
| 984263bc MD |
2974 | toff, |
| 2975 | tinc, | |
| 54f51aeb | 2976 | bp->b_xio.xio_pages[pi] |
| 984263bc MD |
2977 | ); |
| 2978 | toff += tinc; | |
| 2979 | tinc = PAGE_SIZE; | |
| 2980 | } | |
| 2981 | ||
| 2982 | /* | |
| 2983 | * Step 3, fixup the KVM pmap. Remember that | |
| 81b5c339 MD |
2984 | * bp->b_data is relative to bp->b_loffset, but |
| 2985 | * bp->b_loffset may be offset into the first page. | |
| 984263bc MD |
2986 | */ |
| 2987 | ||
| 2988 | bp->b_data = (caddr_t) | |
| 2989 | trunc_page((vm_offset_t)bp->b_data); | |
| 2990 | pmap_qenter( | |
| 2991 | (vm_offset_t)bp->b_data, | |
| 54f51aeb HP |
2992 | bp->b_xio.xio_pages, |
| 2993 | bp->b_xio.xio_npages | |
| 984263bc MD |
2994 | ); |
| 2995 | bp->b_data = (caddr_t)((vm_offset_t)bp->b_data | | |
| 81b5c339 | 2996 | (vm_offset_t)(bp->b_loffset & PAGE_MASK)); |
| 984263bc MD |
2997 | } |
| 2998 | } | |
| 70ac7d6c MD |
2999 | |
| 3000 | /* adjust space use on already-dirty buffer */ | |
| 868d24af MD |
3001 | if (bp->b_flags & B_DELWRI) { |
| 3002 | dirtybufspace += newbsize - bp->b_bufsize; | |
| 3003 | if (bp->b_flags & B_HEAVY) | |
| 3004 | dirtybufspacehw += newbsize - bp->b_bufsize; | |
| 3005 | } | |
| 984263bc MD |
3006 | if (newbsize < bp->b_bufsize) |
| 3007 | bufspacewakeup(); | |
| 3008 | bp->b_bufsize = newbsize; /* actual buffer allocation */ | |
| 3009 | bp->b_bcount = size; /* requested buffer size */ | |
| 3010 | return 1; | |
| 3011 | } | |
| 3012 | ||
| 3013 | /* | |
| 3f779080 | 3014 | * biowait: |
| 984263bc MD |
3015 | * |
| 3016 | * Wait for buffer I/O completion, returning error status. The buffer | |
| 10f3fee5 MD |
3017 | * is left locked on return. B_EINTR is converted into an EINTR error |
| 3018 | * and cleared. | |
| 3019 | * | |
| 3020 | * NOTE! The original b_cmd is lost on return, since b_cmd will be | |
| 3021 | * set to BUF_CMD_DONE. | |
| 984263bc MD |
3022 | */ |
| 3023 | int | |
| c8e4131d | 3024 | biowait(struct buf *bp) |
| 984263bc | 3025 | { |
| e43a034f | 3026 | crit_enter(); |
| 10f3fee5 MD |
3027 | while (bp->b_cmd != BUF_CMD_DONE) { |
| 3028 | if (bp->b_cmd == BUF_CMD_READ) | |
| 377d4740 | 3029 | tsleep(bp, 0, "biord", 0); |
| 984263bc | 3030 | else |
| 377d4740 | 3031 | tsleep(bp, 0, "biowr", 0); |
| 984263bc | 3032 | } |
| e43a034f | 3033 | crit_exit(); |
| 984263bc MD |
3034 | if (bp->b_flags & B_EINTR) { |
| 3035 | bp->b_flags &= ~B_EINTR; | |
| 3036 | return (EINTR); | |
| 3037 | } | |
| 3038 | if (bp->b_flags & B_ERROR) { | |
| 3039 | return (bp->b_error ? bp->b_error : EIO); | |
| 3040 | } else { | |
| 3041 | return (0); | |
| 3042 | } | |
| 3043 | } | |
| 3044 | ||
| 3045 | /* | |
| 81b5c339 MD |
3046 | * This associates a tracking count with an I/O. vn_strategy() and |
| 3047 | * dev_dstrategy() do this automatically but there are a few cases | |
| 3048 | * where a vnode or device layer is bypassed when a block translation | |
| 3049 | * is cached. In such cases bio_start_transaction() may be called on | |
| 3050 | * the bypassed layers so the system gets an I/O in progress indication | |
| 3051 | * for those higher layers. | |
| 3052 | */ | |
| 3053 | void | |
| 3054 | bio_start_transaction(struct bio *bio, struct bio_track *track) | |
| 3055 | { | |
| 3056 | bio->bio_track = track; | |
| 3057 | atomic_add_int(&track->bk_active, 1); | |
| 3058 | } | |
| 3059 | ||
| 3060 | /* | |
| 3061 | * Initiate I/O on a vnode. | |
| 3062 | */ | |
| 3063 | void | |
| 3064 | vn_strategy(struct vnode *vp, struct bio *bio) | |
| 3065 | { | |
| 3066 | struct bio_track *track; | |
| 3067 | ||
| 10f3fee5 MD |
3068 | KKASSERT(bio->bio_buf->b_cmd != BUF_CMD_DONE); |
| 3069 | if (bio->bio_buf->b_cmd == BUF_CMD_READ) | |
| 81b5c339 MD |
3070 | track = &vp->v_track_read; |
| 3071 | else | |
| 3072 | track = &vp->v_track_write; | |
| 3073 | bio->bio_track = track; | |
| 3074 | atomic_add_int(&track->bk_active, 1); | |
| 3075 | vop_strategy(*vp->v_ops, vp, bio); | |
| 3076 | } | |
| 3077 | ||
| 3078 | ||
| 3079 | /* | |
| 3f779080 | 3080 | * biodone: |
| 984263bc MD |
3081 | * |
| 3082 | * Finish I/O on a buffer, optionally calling a completion function. | |
| 3083 | * This is usually called from an interrupt so process blocking is | |
| 3084 | * not allowed. | |
| 3085 | * | |
| 3086 | * biodone is also responsible for setting B_CACHE in a B_VMIO bp. | |
| 3087 | * In a non-VMIO bp, B_CACHE will be set on the next getblk() | |
| 3088 | * assuming B_INVAL is clear. | |
| 3089 | * | |
| 3090 | * For the VMIO case, we set B_CACHE if the op was a read and no | |
| 3091 | * read error occured, or if the op was a write. B_CACHE is never | |
| 3092 | * set if the buffer is invalid or otherwise uncacheable. | |
| 3093 | * | |
| 3094 | * biodone does not mess with B_INVAL, allowing the I/O routine or the | |
| 3095 | * initiator to leave B_INVAL set to brelse the buffer out of existance | |
| 3096 | * in the biodone routine. | |
| 3097 | */ | |
| 3098 | void | |
| 81b5c339 | 3099 | biodone(struct bio *bio) |
| 984263bc | 3100 | { |
| 81b5c339 | 3101 | struct buf *bp = bio->bio_buf; |
| 10f3fee5 | 3102 | buf_cmd_t cmd; |
| 984263bc | 3103 | |
| e43a034f | 3104 | crit_enter(); |
| 984263bc | 3105 | |
| 81b5c339 MD |
3106 | KASSERT(BUF_REFCNTNB(bp) > 0, |
| 3107 | ("biodone: bp %p not busy %d", bp, BUF_REFCNTNB(bp))); | |
| 10f3fee5 MD |
3108 | KASSERT(bp->b_cmd != BUF_CMD_DONE, |
| 3109 | ("biodone: bp %p already done!", bp)); | |
| 984263bc | 3110 | |
| 984263bc MD |
3111 | runningbufwakeup(bp); |
| 3112 | ||
| 81b5c339 | 3113 | /* |
| 10f3fee5 | 3114 | * Run up the chain of BIO's. Leave b_cmd intact for the duration. |
| 81b5c339 MD |
3115 | */ |
| 3116 | while (bio) { | |
| 3117 | biodone_t *done_func; | |
| 3118 | struct bio_track *track; | |
| 984263bc | 3119 | |
| 81b5c339 MD |
3120 | /* |
| 3121 | * BIO tracking. Most but not all BIOs are tracked. | |
| 3122 | */ | |
| 3123 | if ((track = bio->bio_track) != NULL) { | |
| 3124 | atomic_subtract_int(&track->bk_active, 1); | |
| 3125 | if (track->bk_active < 0) { | |
| 3126 | panic("biodone: bad active count bio %p\n", | |
| 3127 | bio); | |
| 3128 | } | |
| 3129 | if (track->bk_waitflag) { | |
| 3130 | track->bk_waitflag = 0; | |
| 3131 | wakeup(track); | |
| 3132 | } | |
| 3133 | bio->bio_track = NULL; | |
| 3134 | } | |
| 3135 | ||
| 3136 | /* | |
| 3137 | * A bio_done function terminates the loop. The function | |
| 3138 | * will be responsible for any further chaining and/or | |
| 3139 | * buffer management. | |
| 10f3fee5 MD |
3140 | * |
| 3141 | * WARNING! The done function can deallocate the buffer! | |
| 81b5c339 MD |
3142 | */ |
| 3143 | if ((done_func = bio->bio_done) != NULL) { | |
| 3144 | bio->bio_done = NULL; | |
| 3145 | done_func(bio); | |
| 3146 | crit_exit(); | |
| 3147 | return; | |
| 3148 | } | |
| 3149 | bio = bio->bio_prev; | |
| 984263bc MD |
3150 | } |
| 3151 | ||
| 10f3fee5 MD |
3152 | cmd = bp->b_cmd; |
| 3153 | bp->b_cmd = BUF_CMD_DONE; | |
| 3154 | ||
| 81b5c339 | 3155 | /* |
| 10f3fee5 | 3156 | * Only reads and writes are processed past this point. |
| 81b5c339 | 3157 | */ |
| 10f3fee5 | 3158 | if (cmd != BUF_CMD_READ && cmd != BUF_CMD_WRITE) { |
| 78a9b77f MD |
3159 | if (cmd == BUF_CMD_FREEBLKS) |
| 3160 | bp->b_flags |= B_NOCACHE; | |
| 81b5c339 | 3161 | brelse(bp); |
| e43a034f | 3162 | crit_exit(); |
| 984263bc MD |
3163 | return; |
| 3164 | } | |
| 81b5c339 | 3165 | |
| 69f8c926 | 3166 | /* |
| 78a9b77f MD |
3167 | * Warning: softupdates may re-dirty the buffer, and HAMMER can do |
| 3168 | * a lot worse. XXX - move this above the clearing of b_cmd | |
| 69f8c926 | 3169 | */ |
| 408357d8 MD |
3170 | if (LIST_FIRST(&bp->b_dep) != NULL) |
| 3171 | buf_complete(bp); | |
| 984263bc | 3172 | |
| 78a9b77f MD |
3173 | /* |
| 3174 | * A failed write must re-dirty the buffer unless B_INVAL | |
| 3175 | * was set. | |
| 3176 | */ | |
| 3177 | if (cmd == BUF_CMD_WRITE && | |
| 3178 | (bp->b_flags & (B_ERROR | B_INVAL)) == B_ERROR) { | |
| 3179 | bp->b_flags &= ~B_NOCACHE; | |
| 3180 | bdirty(bp); | |
| 3181 | } | |
| 3182 | ||
| 3183 | ||
| 984263bc MD |
3184 | if (bp->b_flags & B_VMIO) { |
| 3185 | int i; | |
| 3186 | vm_ooffset_t foff; | |
| 3187 | vm_page_t m; | |
| 3188 | vm_object_t obj; | |
| 3189 | int iosize; | |
| 3190 | struct vnode *vp = bp->b_vp; | |
| 3191 | ||
| 7540ab49 | 3192 | obj = vp->v_object; |
| 984263bc MD |
3193 | |
| 3194 | #if defined(VFS_BIO_DEBUG) | |
| 3c37c940 | 3195 | if (vp->v_auxrefs == 0) |
| 8a6722ed | 3196 | panic("biodone: zero vnode hold count"); |
| 7540ab49 | 3197 | if ((vp->v_flag & VOBJBUF) == 0) |
| 984263bc | 3198 | panic("biodone: vnode is not setup for merged cache"); |
| 984263bc MD |
3199 | #endif |
| 3200 | ||
| 81b5c339 MD |
3201 | foff = bp->b_loffset; |
| 3202 | KASSERT(foff != NOOFFSET, ("biodone: no buffer offset")); | |
| 7540ab49 | 3203 | KASSERT(obj != NULL, ("biodone: missing VM object")); |
| 984263bc | 3204 | |
| 984263bc | 3205 | #if defined(VFS_BIO_DEBUG) |
| 54f51aeb | 3206 | if (obj->paging_in_progress < bp->b_xio.xio_npages) { |
| 6ea70f76 | 3207 | kprintf("biodone: paging in progress(%d) < bp->b_xio.xio_npages(%d)\n", |
| 54f51aeb | 3208 | obj->paging_in_progress, bp->b_xio.xio_npages); |
| 984263bc MD |
3209 | } |
| 3210 | #endif | |
| 3211 | ||
| 3212 | /* | |
| 3213 | * Set B_CACHE if the op was a normal read and no error | |
| 3214 | * occured. B_CACHE is set for writes in the b*write() | |
| 3215 | * routines. | |
| 3216 | */ | |
| 3217 | iosize = bp->b_bcount - bp->b_resid; | |
| 10f3fee5 | 3218 | if (cmd == BUF_CMD_READ && (bp->b_flags & (B_INVAL|B_NOCACHE|B_ERROR)) == 0) { |
| 984263bc MD |
3219 | bp->b_flags |= B_CACHE; |
| 3220 | } | |
| 3221 | ||
| 54f51aeb | 3222 | for (i = 0; i < bp->b_xio.xio_npages; i++) { |
| 984263bc MD |
3223 | int bogusflag = 0; |
| 3224 | int resid; | |
| 3225 | ||
| 3226 | resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff; | |
| 3227 | if (resid > iosize) | |
| 3228 | resid = iosize; | |
| 3229 | ||
| 3230 | /* | |
| 06ecca5a MD |
3231 | * cleanup bogus pages, restoring the originals. Since |
| 3232 | * the originals should still be wired, we don't have | |
| 3233 | * to worry about interrupt/freeing races destroying | |
| 3234 | * the VM object association. | |
| 984263bc | 3235 | */ |
| 54f51aeb | 3236 | m = bp->b_xio.xio_pages[i]; |
| 984263bc MD |
3237 | if (m == bogus_page) { |
| 3238 | bogusflag = 1; | |
| 3239 | m = vm_page_lookup(obj, OFF_TO_IDX(foff)); | |
| 3240 | if (m == NULL) | |
| 3241 | panic("biodone: page disappeared"); | |
| 54f51aeb HP |
3242 | bp->b_xio.xio_pages[i] = m; |
| 3243 | pmap_qenter(trunc_page((vm_offset_t)bp->b_data), | |
| 3244 | bp->b_xio.xio_pages, bp->b_xio.xio_npages); | |
| 984263bc MD |
3245 | } |
| 3246 | #if defined(VFS_BIO_DEBUG) | |
| 3247 | if (OFF_TO_IDX(foff) != m->pindex) { | |
| 6ea70f76 | 3248 | kprintf( |
| 984263bc MD |
3249 | "biodone: foff(%lu)/m->pindex(%d) mismatch\n", |
| 3250 | (unsigned long)foff, m->pindex); | |
| 3251 | } | |
| 3252 | #endif | |
| 3253 | ||
| 3254 | /* | |
| 3255 | * In the write case, the valid and clean bits are | |
| 3256 | * already changed correctly ( see bdwrite() ), so we | |
| 3257 | * only need to do this here in the read case. | |
| 3258 | */ | |
| 10f3fee5 | 3259 | if (cmd == BUF_CMD_READ && !bogusflag && resid > 0) { |
| 984263bc MD |
3260 | vfs_page_set_valid(bp, foff, i, m); |
| 3261 | } | |
| 3262 | vm_page_flag_clear(m, PG_ZERO); | |
| 3263 | ||
| 3264 | /* | |
| 3265 | * when debugging new filesystems or buffer I/O methods, this | |
| 3266 | * is the most common error that pops up. if you see this, you | |
| 3267 | * have not set the page busy flag correctly!!! | |
| 3268 | */ | |
| 3269 | if (m->busy == 0) { | |
| 6ea70f76 | 3270 | kprintf("biodone: page busy < 0, " |
| 984263bc MD |
3271 | "pindex: %d, foff: 0x(%x,%x), " |
| 3272 | "resid: %d, index: %d\n", | |
| 3273 | (int) m->pindex, (int)(foff >> 32), | |
| 3274 | (int) foff & 0xffffffff, resid, i); | |
| 3275 | if (!vn_isdisk(vp, NULL)) | |
| 973c11b9 MD |
3276 | kprintf(" iosize: %ld, loffset: %lld, " |
| 3277 | "flags: 0x%08x, npages: %d\n", | |
| 984263bc | 3278 | bp->b_vp->v_mount->mnt_stat.f_iosize, |
| 973c11b9 | 3279 | (long long)bp->b_loffset, |
| 54f51aeb | 3280 | bp->b_flags, bp->b_xio.xio_npages); |
| 984263bc | 3281 | else |
| 6ea70f76 | 3282 | kprintf(" VDEV, loffset: %lld, flags: 0x%08x, npages: %d\n", |
| 973c11b9 | 3283 | (long long)bp->b_loffset, |
| 54f51aeb | 3284 | bp->b_flags, bp->b_xio.xio_npages); |
| 6ea70f76 | 3285 | kprintf(" valid: 0x%x, dirty: 0x%x, wired: %d\n", |
| 984263bc | 3286 | m->valid, m->dirty, m->wire_count); |
| fc92d4aa | 3287 | panic("biodone: page busy < 0"); |
| 984263bc MD |
3288 | } |
| 3289 | vm_page_io_finish(m); | |
| 3290 | vm_object_pip_subtract(obj, 1); | |
| 3291 | foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; | |
| 3292 | iosize -= resid; | |
| 3293 | } | |
| 3294 | if (obj) | |
| 3295 | vm_object_pip_wakeupn(obj, 0); | |
| 3296 | } | |
| 3297 | ||
| 3298 | /* | |
| 3299 | * For asynchronous completions, release the buffer now. The brelse | |
| 3300 | * will do a wakeup there if necessary - so no need to do a wakeup | |
| 3301 | * here in the async case. The sync case always needs to do a wakeup. | |
| 3302 | */ | |
| 3303 | ||
| 3304 | if (bp->b_flags & B_ASYNC) { | |
| 3305 | if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR | B_RELBUF)) != 0) | |
| 3306 | brelse(bp); | |
| 3307 | else | |
| 3308 | bqrelse(bp); | |
| 3309 | } else { | |
| 3310 | wakeup(bp); | |
| 3311 | } | |
| e43a034f | 3312 | crit_exit(); |
| 984263bc MD |
3313 | } |
| 3314 | ||
| 3315 | /* | |
| 3f779080 HP |
3316 | * vfs_unbusy_pages: |
| 3317 | * | |
| 3318 | * This routine is called in lieu of iodone in the case of | |
| 3319 | * incomplete I/O. This keeps the busy status for pages | |
| 3320 | * consistant. | |
| 984263bc MD |
3321 | */ |
| 3322 | void | |
| 493c516a | 3323 | vfs_unbusy_pages(struct buf *bp) |
| 984263bc MD |
3324 | { |
| 3325 | int i; | |
| 3326 | ||
| 3327 | runningbufwakeup(bp); | |
| 3328 | if (bp->b_flags & B_VMIO) { | |
| 3329 | struct vnode *vp = bp->b_vp; | |
| 3330 | vm_object_t obj; | |
| 3331 | ||
| 7540ab49 | 3332 | obj = vp->v_object; |
| 984263bc | 3333 | |
| 54f51aeb HP |
3334 | for (i = 0; i < bp->b_xio.xio_npages; i++) { |
| 3335 | vm_page_t m = bp->b_xio.xio_pages[i]; | |
| 984263bc | 3336 | |
| 06ecca5a MD |
3337 | /* |
| 3338 | * When restoring bogus changes the original pages | |
| 3339 | * should still be wired, so we are in no danger of | |
| 3340 | * losing the object association and do not need | |
| e43a034f | 3341 | * critical section protection particularly. |
| 06ecca5a | 3342 | */ |
| 984263bc | 3343 | if (m == bogus_page) { |
| 81b5c339 | 3344 | m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_loffset) + i); |
| 984263bc | 3345 | if (!m) { |
| fc92d4aa | 3346 | panic("vfs_unbusy_pages: page missing"); |
| 984263bc | 3347 | } |
| 54f51aeb HP |
3348 | bp->b_xio.xio_pages[i] = m; |
| 3349 | pmap_qenter(trunc_page((vm_offset_t)bp->b_data), | |
| 3350 | bp->b_xio.xio_pages, bp->b_xio.xio_npages); | |
| 984263bc MD |
3351 | } |
| 3352 | vm_object_pip_subtract(obj, 1); | |
| 3353 | vm_page_flag_clear(m, PG_ZERO); | |
| 3354 | vm_page_io_finish(m); | |
| 3355 | } | |
| 3356 | vm_object_pip_wakeupn(obj, 0); | |
| 3357 | } | |
| 3358 | } | |
| 3359 | ||
| 3360 | /* | |
| 3361 | * vfs_page_set_valid: | |
| 3362 | * | |
| 3363 | * Set the valid bits in a page based on the supplied offset. The | |
| 3364 | * range is restricted to the buffer's size. | |
| 3365 | * | |
| 3366 | * This routine is typically called after a read completes. | |
| 3367 | */ | |
| 3368 | static void | |
| 3369 | vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, int pageno, vm_page_t m) | |
| 3370 | { | |
| 3371 | vm_ooffset_t soff, eoff; | |
| 3372 | ||
| 3373 | /* | |
| 3374 | * Start and end offsets in buffer. eoff - soff may not cross a | |
| 3375 | * page boundry or cross the end of the buffer. The end of the | |
| 3376 | * buffer, in this case, is our file EOF, not the allocation size | |
| 3377 | * of the buffer. | |
| 3378 | */ | |
| 3379 | soff = off; | |
| 3380 | eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK; | |
| 81b5c339 MD |
3381 | if (eoff > bp->b_loffset + bp->b_bcount) |
| 3382 | eoff = bp->b_loffset + bp->b_bcount; | |
| 984263bc MD |
3383 | |
| 3384 | /* | |
| 3385 | * Set valid range. This is typically the entire buffer and thus the | |
| 3386 | * entire page. | |
| 3387 | */ | |
| 3388 | if (eoff > soff) { | |
| 3389 | vm_page_set_validclean( | |
| 3390 | m, | |
| 3391 | (vm_offset_t) (soff & PAGE_MASK), | |
| 3392 | (vm_offset_t) (eoff - soff) | |
| 3393 | ); | |
| 3394 | } | |
| 3395 | } | |
| 3396 | ||
| 3397 | /* | |
| 3f779080 HP |
3398 | * vfs_busy_pages: |
| 3399 | * | |
| 3400 | * This routine is called before a device strategy routine. | |
| 3401 | * It is used to tell the VM system that paging I/O is in | |
| 3402 | * progress, and treat the pages associated with the buffer | |
| 3403 | * almost as being PG_BUSY. Also the object 'paging_in_progress' | |
| 3404 | * flag is handled to make sure that the object doesn't become | |
| 3405 | * inconsistant. | |
| 3406 | * | |
| 3407 | * Since I/O has not been initiated yet, certain buffer flags | |
| 3408 | * such as B_ERROR or B_INVAL may be in an inconsistant state | |
| 3409 | * and should be ignored. | |
| 984263bc MD |
3410 | */ |
| 3411 | void | |
| 10f3fee5 | 3412 | vfs_busy_pages(struct vnode *vp, struct buf *bp) |
| 984263bc MD |
3413 | { |
| 3414 | int i, bogus; | |
| fde7ac71 | 3415 | struct lwp *lp = curthread->td_lwp; |
| 984263bc | 3416 | |
| a8f169e2 | 3417 | /* |
| 10f3fee5 MD |
3418 | * The buffer's I/O command must already be set. If reading, |
| 3419 | * B_CACHE must be 0 (double check against callers only doing | |
| 3420 | * I/O when B_CACHE is 0). | |
| a8f169e2 | 3421 | */ |
| 10f3fee5 MD |
3422 | KKASSERT(bp->b_cmd != BUF_CMD_DONE); |
| 3423 | KKASSERT(bp->b_cmd == BUF_CMD_WRITE || (bp->b_flags & B_CACHE) == 0); | |
| a8f169e2 | 3424 | |
| 984263bc | 3425 | if (bp->b_flags & B_VMIO) { |
| 984263bc MD |
3426 | vm_object_t obj; |
| 3427 | vm_ooffset_t foff; | |
| 3428 | ||
| 7540ab49 | 3429 | obj = vp->v_object; |
| 81b5c339 MD |
3430 | foff = bp->b_loffset; |
| 3431 | KASSERT(bp->b_loffset != NOOFFSET, | |
| 3432 | ("vfs_busy_pages: no buffer offset")); | |
| 984263bc MD |
3433 | vfs_setdirty(bp); |
| 3434 | ||
| 17cde63e MD |
3435 | /* |
| 3436 | * Loop until none of the pages are busy. | |
| 3437 | */ | |
| 984263bc | 3438 | retry: |
| 54f51aeb HP |
3439 | for (i = 0; i < bp->b_xio.xio_npages; i++) { |