Project

General

Profile

Statistics
| Revision:

root / lab4 / .minix-src / include / sys / buf.h @ 14

History | View | Annotate | Download (11.1 KB)

1
/*     $NetBSD: buf.h,v 1.121 2015/03/28 19:24:04 maxv Exp $ */
2

    
3
/*-
4
 * Copyright (c) 1999, 2000, 2007, 2008 The NetBSD Foundation, Inc.
5
 * All rights reserved.
6
 *
7
 * This code is derived from software contributed to The NetBSD Foundation
8
 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9
 * NASA Ames Research Center, and by Andrew Doran.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
 * POSSIBILITY OF SUCH DAMAGE.
31
 */
32

    
33
/*
34
 * Copyright (c) 1982, 1986, 1989, 1993
35
 *        The Regents of the University of California.  All rights reserved.
36
 * (c) UNIX System Laboratories, Inc.
37
 * All or some portions of this file are derived from material licensed
38
 * to the University of California by American Telephone and Telegraph
39
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40
 * the permission of UNIX System Laboratories, Inc.
41
 *
42
 * Redistribution and use in source and binary forms, with or without
43
 * modification, are permitted provided that the following conditions
44
 * are met:
45
 * 1. Redistributions of source code must retain the above copyright
46
 *    notice, this list of conditions and the following disclaimer.
47
 * 2. Redistributions in binary form must reproduce the above copyright
48
 *    notice, this list of conditions and the following disclaimer in the
49
 *    documentation and/or other materials provided with the distribution.
50
 * 3. Neither the name of the University nor the names of its contributors
51
 *    may be used to endorse or promote products derived from this software
52
 *    without specific prior written permission.
53
 *
54
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64
 * SUCH DAMAGE.
65
 *
66
 *        @(#)buf.h        8.9 (Berkeley) 3/30/95
67
 */
68

    
69
#ifndef _SYS_BUF_H_
70
#define        _SYS_BUF_H_
71

    
72
#include <sys/pool.h>
73
#include <sys/queue.h>
74
#include <sys/mutex.h>
75
#include <sys/condvar.h>
76
#include <sys/rbtree.h>
77
#if defined(_KERNEL)
78
#include <sys/workqueue.h>
79
#endif /* defined(_KERNEL) */
80

    
81
struct buf;
82
struct mount;
83
struct vnode;
84
struct kauth_cred;
85

    
86
#define NOLIST ((struct buf *)0x87654321)
87

    
88
extern kmutex_t bufcache_lock;
89
extern kmutex_t buffer_lock;
90

    
91
/*
92
 * The buffer header describes an I/O operation in the kernel.
93
 *
94
 * Field markings and the corresponding locks:
95
 *
96
 * b        thread of execution that holds BC_BUSY, does not correspond
97
 *          directly to any particular LWP
98
 * c        bufcache_lock
99
 * o        b_objlock
100
 *
101
 * For buffers associated with a vnode, b_objlock points to vp->v_interlock.
102
 * If not associated with a vnode, it points to the generic buffer_lock.
103
 */
104
struct buf {
105
        union {
106
                TAILQ_ENTRY(buf) u_actq;
107
                rb_node_t u_rbnode;
108
#if defined(_KERNEL) /* u_work is smaller than u_actq. XXX */
109
                struct work u_work;
110
#endif /* defined(_KERNEL) */
111
        } b_u;                                        /* b: device driver queue */
112
#define        b_actq        b_u.u_actq
113
#define        b_work        b_u.u_work
114
        void                        (*b_iodone)(struct buf *);/* b: call when done */
115
        int                        b_error;        /* b: errno value. */
116
        int                        b_resid;        /* b: remaining I/O. */
117
        u_int                        b_flags;        /* b: B_* flags */
118
        int                        b_prio;                /* b: priority for queue */
119
        int                        b_bufsize;        /* b: allocated size */
120
        int                        b_bcount;        /* b: valid bytes in buffer */
121
        dev_t                        b_dev;                /* b: associated device */
122
        void                        *b_data;        /* b: fs private data */
123
        daddr_t                        b_blkno;        /* b: physical block number
124
                                                      (partition relative) */
125
        daddr_t                        b_rawblkno;        /* b: raw physical block number
126
                                                      (volume relative) */
127
        struct proc                *b_proc;        /* b: proc if BB_PHYS */
128
        void                        *b_saveaddr;        /* b: saved b_data for physio */
129

    
130
        /*
131
         * b: private data for owner.
132
         *  - buffer cache buffers are owned by corresponding filesystem.
133
         *  - non-buffer cache buffers are owned by subsystem which
134
         *    allocated them. (filesystem, disk driver, etc)
135
         */
136
        void        *b_private;
137
        off_t        b_dcookie;                /* NFS: Offset cookie if dir block */
138

    
139
        kcondvar_t                b_busy;                /* c: threads waiting on buf */
140
        u_int                        b_refcnt;        /* c: refcount for b_busy */
141
        void                        *b_unused;        /*  : unused */
142
        LIST_ENTRY(buf)                b_hash;                /* c: hash chain */
143
        LIST_ENTRY(buf)                b_vnbufs;        /* c: associated vnode */
144
        TAILQ_ENTRY(buf)        b_freelist;        /* c: position if not active */
145
        LIST_ENTRY(buf)                b_wapbllist;        /* c: transaction buffer list */
146
        daddr_t                        b_lblkno;        /* c: logical block number */
147
        int                        b_freelistindex;/* c: free list index (BQ_) */
148
        u_int                        b_cflags;        /* c: BC_* flags */
149
        struct vnode                *b_vp;                /* c: file vnode */
150

    
151
        kcondvar_t                b_done;                /* o: waiting on completion */
152
        u_int                        b_oflags;        /* o: BO_* flags */
153
        kmutex_t                *b_objlock;        /* o: completion lock */
154
};
155

    
156
/*
157
 * For portability with historic industry practice, the cylinder number has
158
 * to be maintained in the `b_resid' field.
159
 */
160
#define        b_cylinder b_resid                /* Cylinder number for disksort(). */
161

    
162
/*
163
 * These flags are kept in b_cflags (owned by buffer cache).
164
 */
165
#define        BC_AGE                0x00000001        /* Move to age queue when I/O done. */
166
#define        BC_BUSY                0x00000010        /* I/O in progress. */
167
#define        BC_INVAL        0x00002000        /* Does not contain valid info. */
168
#define        BC_NOCACHE        0x00008000        /* Do not cache block after use. */
169
#define        BC_WANTED        0x00800000        /* Process wants this buffer. */
170
#define        BC_VFLUSH        0x04000000        /* Buffer is being synced. */
171

    
172
/*
173
 * These flags are kept in b_oflags (owned by associated object).
174
 */
175
#define        BO_DELWRI        0x00000080        /* Delay I/O until buffer reused. */
176
#define        BO_DONE                0x00000200        /* I/O completed. */
177

    
178
/*
179
 * These flags are kept in b_flags (owned by buffer holder).
180
 */
181
#define        B_WRITE                0x00000000        /* Write buffer (pseudo flag). */
182
#define        B_ASYNC                0x00000004        /* Start I/O, do not wait. */
183
#define        B_COWDONE        0x00000400        /* Copy-on-write already done. */
184
#define        B_GATHERED        0x00001000        /* LFS: already in a segment. */
185
#define        B_LOCKED        0x00004000        /* Locked in core (not reusable). */
186
#define        B_PHYS                0x00040000        /* I/O to user memory. */
187
#define        B_RAW                0x00080000        /* Set by physio for raw transfers. */
188
#define        B_READ                0x00100000        /* Read buffer. */
189
#define        B_DEVPRIVATE        0x02000000        /* Device driver private flag. */
190

    
191
#define BUF_FLAGBITS \
192
    "\20\1AGE\3ASYNC\4BAD\5BUSY\10DELWRI" \
193
    "\12DONE\13COWDONE\15GATHERED\16INVAL\17LOCKED\20NOCACHE" \
194
    "\23PHYS\24RAW\25READ\32DEVPRIVATE\33VFLUSH"
195

    
196
/* Avoid weird code due to B_WRITE being a "pseudo flag" */
197
#define BUF_ISREAD(bp)        (((bp)->b_flags & B_READ) == B_READ)
198
#define BUF_ISWRITE(bp)        (((bp)->b_flags & B_READ) == B_WRITE)
199

    
200
/*
201
 * This structure describes a clustered I/O.  It is stored in the b_saveaddr
202
 * field of the buffer on which I/O is done.  At I/O completion, cluster
203
 * callback uses the structure to parcel I/O's to individual buffers, and
204
 * then free's this structure.
205
 */
206
struct cluster_save {
207
        long        bs_bcount;                /* Saved b_bcount. */
208
        long        bs_bufsize;                /* Saved b_bufsize. */
209
        void        *bs_saveaddr;                /* Saved b_addr. */
210
        int        bs_nchildren;                /* Number of associated buffers. */
211
        struct buf *bs_children;        /* List of associated buffers. */
212
};
213

    
214
/*
215
 * Zero out the buffer's data area.
216
 */
217
#define        clrbuf(bp)                                                        \
218
do {                                                                        \
219
        memset((bp)->b_data, 0, (u_int)(bp)->b_bcount);                        \
220
        (bp)->b_resid = 0;                                                \
221
} while (/* CONSTCOND */ 0)
222

    
223
/* Flags to low-level allocation routines. */
224
#define B_CLRBUF        0x01        /* Request allocated buffer be cleared. */
225
#define B_SYNC                0x02        /* Do all allocations synchronously. */
226
#define B_METAONLY        0x04        /* Return indirect block buffer. */
227
#define B_CONTIG        0x08        /* Allocate file contiguously. */
228

    
229
/* Flags to bread() and breadn(). */
230
#define B_MODIFY        0x01        /* Hint: caller might modify buffer */
231

    
232
#ifdef _KERNEL
233

    
234
#define        BIO_GETPRIO(bp)                ((bp)->b_prio)
235
#define        BIO_SETPRIO(bp, prio)        (bp)->b_prio = (prio)
236
#define        BIO_COPYPRIO(bp1, bp2)        BIO_SETPRIO(bp1, BIO_GETPRIO(bp2))
237

    
238
#define        BPRIO_NPRIO                3
239
#define        BPRIO_TIMECRITICAL        2
240
#define        BPRIO_TIMELIMITED        1
241
#define        BPRIO_TIMENONCRITICAL        0
242
#define        BPRIO_DEFAULT                BPRIO_TIMELIMITED
243

    
244
extern        u_int nbuf;                /* The number of buffer headers */
245

    
246
/*
247
 * Definitions for the buffer free lists.
248
 */
249
#define        BQUEUES                4                /* number of free buffer queues */
250

    
251
#define        BQ_LOCKED        0                /* super-blocks &c */
252
#define        BQ_LRU                1                /* lru, useful buffers */
253
#define        BQ_AGE                2                /* rubbish */
254
#define        BQ_EMPTY        3                /* buffer headers with no memory */
255

    
256
struct bqueue {
257
        TAILQ_HEAD(, buf) bq_queue;
258
        uint64_t bq_bytes;
259
        buf_t *bq_marker;
260
};
261

    
262
extern struct bqueue bufqueues[BQUEUES];
263

    
264
__BEGIN_DECLS
265
int        allocbuf(buf_t *, int, int);
266
void        bawrite(buf_t *);
267
void        bdwrite(buf_t *);
268
void        biodone(buf_t *);
269
int        biowait(buf_t *);
270
int        bread(struct vnode *, daddr_t, int, int, buf_t **);
271
int        breadn(struct vnode *, daddr_t, int, daddr_t *, int *, int,
272
               int, buf_t **);
273
void        brelsel(buf_t *, int);
274
void        brelse(buf_t *, int);
275
void        bremfree(buf_t *);
276
void        bufinit(void);
277
void        bufinit2(void);
278
int        bwrite(buf_t *);
279
buf_t        *getblk(struct vnode *, daddr_t, int, int, int);
280
buf_t        *geteblk(int);
281
buf_t        *incore(struct vnode *, daddr_t);
282

    
283
void        minphys(buf_t *);
284
int        physio(void (*)(buf_t *), buf_t *, dev_t, int,
285
               void (*)(buf_t *), struct uio *);
286

    
287
void        brelvp(buf_t *);
288
void        reassignbuf(buf_t *, struct vnode *);
289
void        bgetvp(struct vnode *, buf_t *);
290
int        buf_syncwait(void);
291
u_long        buf_memcalc(void);
292
int        buf_drain(int);
293
int        buf_setvalimit(vsize_t);
294
#if defined(DDB) || defined(DEBUGPRINT)
295
void        vfs_buf_print(buf_t *, int, void (*)(const char *, ...)
296
    __printflike(1, 2));
297
#endif
298
buf_t        *getiobuf(struct vnode *, bool);
299
void        putiobuf(buf_t *);
300
void        buf_init(buf_t *);
301
void        buf_destroy(buf_t *);
302
int        bbusy(buf_t *, bool, int, kmutex_t *);
303

    
304
void        nestiobuf_iodone(buf_t *);
305
void        nestiobuf_setup(buf_t *, buf_t *, int, size_t);
306
void        nestiobuf_done(buf_t *, int, int);
307

    
308
__END_DECLS
309
#endif /* _KERNEL */
310
#endif /* !_SYS_BUF_H_ */