/
buf2.h
392 lines (352 loc) · 9.22 KB
/
buf2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright (c) 1982, 1986, 1989, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)buf.h 8.9 (Berkeley) 3/30/95
* $FreeBSD: src/sys/sys/buf.h,v 1.88.2.10 2003/01/25 19:02:23 dillon Exp $
*/
#ifndef _SYS_BUF2_H_
#define _SYS_BUF2_H_
#ifdef _KERNEL
#ifndef _SYS_BUF_H_
#include <sys/buf.h>
#endif
#ifndef _SYS_MOUNT_H_
#include <sys/mount.h>
#endif
#ifndef _SYS_VNODE_H_
#include <sys/vnode.h>
#endif
#ifndef _VM_VM_PAGE_H_
#include <vm/vm_page.h>
#endif
/*
* Initialize a lock.
*/
#define BUF_LOCKINIT(bp) \
lockinit(&(bp)->b_lock, buf_wmesg, 0, LK_NOCOLLSTATS)
/*
*
* Get a lock sleeping non-interruptably until it becomes available.
*
* XXX lk_wmesg can race, but should not result in any operational issues.
*/
static __inline int
BUF_LOCK(struct buf *bp, int locktype)
{
bp->b_lock.lk_wmesg = buf_wmesg;
return (lockmgr(&(bp)->b_lock, locktype));
}
/*
* Get a lock sleeping with specified interruptably and timeout.
*
* XXX lk_timo can race against other entities calling BUF_TIMELOCK,
* but will not interfere with entities calling BUF_LOCK since LK_TIMELOCK
* will not be set in that case.
*
* XXX lk_wmesg can race, but should not result in any operational issues.
*/
static __inline int
BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int timo)
{
bp->b_lock.lk_wmesg = wmesg;
bp->b_lock.lk_timo = timo;
return (lockmgr(&(bp)->b_lock, locktype | LK_TIMELOCK));
}
/*
* Release a lock. Only the acquiring process may free the lock unless
* it has been handed off to biodone.
*/
static __inline void
BUF_UNLOCK(struct buf *bp)
{
lockmgr(&(bp)->b_lock, LK_RELEASE);
}
/*
* When initiating asynchronous I/O, change ownership of the lock to the
* kernel. Once done, the lock may legally released by biodone. The
* original owning process can no longer acquire it recursively, but must
* wait until the I/O is completed and the lock has been freed by biodone.
*/
static __inline void
BUF_KERNPROC(struct buf *bp)
{
lockmgr_kernproc(&(bp)->b_lock);
}
/*
* Find out the number of references to a lock.
*
* The non-blocking version should only be used for assertions in cases
* where the buffer is expected to be owned or otherwise data stable.
*/
static __inline int
BUF_LOCKINUSE(struct buf *bp)
{
return (lockinuse(&(bp)->b_lock));
}
/*
* Free a buffer lock.
*/
#define BUF_LOCKFREE(bp) \
if (BUF_LOCKINUSE(bp)) \
panic("free locked buf")
static __inline void
bioq_init(struct bio_queue_head *bioq)
{
TAILQ_INIT(&bioq->queue);
bioq->off_unused = 0;
bioq->reorder = 0;
bioq->transition = NULL;
bioq->bio_unused = NULL;
}
static __inline void
bioq_insert_tail(struct bio_queue_head *bioq, struct bio *bio)
{
bioq->transition = NULL;
TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
}
static __inline void
bioq_remove(struct bio_queue_head *bioq, struct bio *bio)
{
/*
* Adjust read insertion point when removing the bioq. The
* bio after the insert point is a write so move backwards
* one (NULL will indicate all the reads have cleared).
*/
if (bio == bioq->transition)
bioq->transition = TAILQ_NEXT(bio, bio_act);
TAILQ_REMOVE(&bioq->queue, bio, bio_act);
}
static __inline struct bio *
bioq_first(struct bio_queue_head *bioq)
{
return (TAILQ_FIRST(&bioq->queue));
}
static __inline struct bio *
bioq_takefirst(struct bio_queue_head *bioq)
{
struct bio *bp;
bp = TAILQ_FIRST(&bioq->queue);
if (bp != NULL)
bioq_remove(bioq, bp);
return (bp);
}
/*
* Adjust buffer cache buffer's activity count. This
* works similarly to vm_page->act_count.
*/
static __inline void
buf_act_advance(struct buf *bp)
{
if (bp->b_act_count > ACT_MAX - ACT_ADVANCE)
bp->b_act_count = ACT_MAX;
else
bp->b_act_count += ACT_ADVANCE;
}
static __inline void
buf_act_decline(struct buf *bp)
{
if (bp->b_act_count < ACT_DECLINE)
bp->b_act_count = 0;
else
bp->b_act_count -= ACT_DECLINE;
}
/*
* biodeps inlines - used by softupdates and HAMMER.
*
* All bioops are MPSAFE
*/
static __inline void
buf_dep_init(struct buf *bp)
{
bp->b_ops = NULL;
LIST_INIT(&bp->b_dep);
}
/*
* Precondition: the buffer has some dependencies.
*
* MPSAFE
*/
static __inline void
buf_deallocate(struct buf *bp)
{
struct bio_ops *ops = bp->b_ops;
KKASSERT(! LIST_EMPTY(&bp->b_dep));
if (ops)
ops->io_deallocate(bp);
}
/*
* This callback is made from flushbufqueues() which uses BUF_LOCK().
* Since it isn't going through a normal buffer aquisition mechanic
* and calling the filesystem back enforce the vnode's KVABIO support.
*/
static __inline int
buf_countdeps(struct buf *bp, int n)
{
struct bio_ops *ops = bp->b_ops;
int r;
if (ops) {
if (bp->b_vp == NULL || (bp->b_vp->v_flag & VKVABIO) == 0)
bkvasync_all(bp);
r = ops->io_countdeps(bp, n);
} else {
r = 0;
}
return(r);
}
/*
* MPSAFE
*/
static __inline void
buf_start(struct buf *bp)
{
struct bio_ops *ops = bp->b_ops;
if (ops)
ops->io_start(bp);
}
/*
* MPSAFE
*/
static __inline void
buf_complete(struct buf *bp)
{
struct bio_ops *ops = bp->b_ops;
if (ops)
ops->io_complete(bp);
}
/*
* MPSAFE
*/
static __inline int
buf_fsync(struct vnode *vp)
{
struct bio_ops *ops = vp->v_mount->mnt_bioops;
int r;
if (ops)
r = ops->io_fsync(vp);
else
r = 0;
return(r);
}
/*
* MPSAFE
*/
static __inline void
buf_movedeps(struct buf *bp1, struct buf *bp2)
{
struct bio_ops *ops = bp1->b_ops;
if (ops)
ops->io_movedeps(bp1, bp2);
}
/*
* MPSAFE
*/
static __inline int
buf_checkread(struct buf *bp)
{
struct bio_ops *ops = bp->b_ops;
if (ops)
return(ops->io_checkread(bp));
return(0);
}
/*
* This callback is made from flushbufqueues() which uses BUF_LOCK().
* Since it isn't going through a normal buffer aquisition mechanic
* and calling the filesystem back enforce the vnode's KVABIO support.
*/
static __inline int
buf_checkwrite(struct buf *bp)
{
struct bio_ops *ops = bp->b_ops;
if (ops) {
if (bp->b_vp == NULL || (bp->b_vp->v_flag & VKVABIO) == 0)
bkvasync_all(bp);
return(ops->io_checkwrite(bp));
}
return(0);
}
/*
* Chained biodone. The bio callback was made and the callback function
* wishes to chain the biodone. If no BIO's are left we call bpdone()
* with elseit=TRUE (asynchronous completion).
*
* MPSAFE
*/
static __inline void
biodone_chain(struct bio *bio)
{
if (bio->bio_prev)
biodone(bio->bio_prev);
else
bpdone(bio->bio_buf, 1);
}
static __inline int
bread(struct vnode *vp, off_t loffset, int size, struct buf **bpp)
{
*bpp = NULL;
return(breadnx(vp, loffset, size, B_NOTMETA,
NULL, NULL, 0, bpp));
}
static __inline int
bread_kvabio(struct vnode *vp, off_t loffset, int size, struct buf **bpp)
{
*bpp = NULL;
return(breadnx(vp, loffset, size, B_NOTMETA | B_KVABIO,
NULL, NULL, 0, bpp));
}
static __inline int
breadn(struct vnode *vp, off_t loffset, int size, off_t *raoffset,
int *rabsize, int cnt, struct buf **bpp)
{
*bpp = NULL;
return(breadnx(vp, loffset, size, B_NOTMETA, raoffset,
rabsize, cnt, bpp));
}
static __inline int
cluster_read(struct vnode *vp, off_t filesize, off_t loffset,
int blksize, size_t minreq, size_t maxreq, struct buf **bpp)
{
*bpp = NULL;
return(cluster_readx(vp, filesize, loffset, blksize, B_NOTMETA,
minreq, maxreq, bpp));
}
static __inline int
cluster_read_kvabio(struct vnode *vp, off_t filesize, off_t loffset,
int blksize, size_t minreq, size_t maxreq, struct buf **bpp)
{
*bpp = NULL;
return(cluster_readx(vp, filesize, loffset, blksize,
B_NOTMETA | B_KVABIO,
minreq, maxreq, bpp));
}
#endif /* _KERNEL */
#endif /* !_SYS_BUF2_H_ */