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