windows-64-bit-type-safety-20051105
[openafs.git] / src / WINNT / afsd / cm_buf.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* Copyright (C) 1994 Cazamar Systems, Inc. */
11
12 #ifndef _BUF_H__ENV_
13 #define _BUF_H__ENV_ 1
14
15 #include <osi.h>
16 #ifdef DISKCACHE95
17 #include "cm_diskcache.h"
18 #endif /* DISKCACHE95 */
19
20 /* default # of buffers if not changed */
21 #define CM_BUF_BUFFERS  100
22
23 /* default buffer size */
24 #define CM_BUF_BLOCKSIZE CM_CONFIGDEFAULT_BLOCKSIZE
25
26 /* default hash size */
27 #define CM_BUF_HASHSIZE 1024
28
29 /* cache type */
30 #define CM_BUF_CACHETYPE_FILE 1
31 #define CM_BUF_CACHETYPE_VIRTUAL 2
32 extern int buf_cacheType;
33
34 /* force it to be signed so that mod comes out positive or 0 */
35 #define BUF_HASH(fidp,offsetp) ((((fidp)->vnode+((fidp)->unique << 5)   \
36                                 +(fidp)->volume+(fidp)->cell            \
37                                 +((offsetp)->LowPart / cm_data.buf_blockSize))  \
38                                   & 0x7fffffff)                         \
39                                    % cm_data.buf_hashSize)
40
41 /* another hash fn */
42 #define BUF_FILEHASH(fidp) ((((fidp)->vnode+((fidp)->unique << 5)       \
43                                 +(fidp)->volume+(fidp)->cell)           \
44                                   & 0x7fffffff)                         \
45                                    % cm_data.buf_hashSize)
46
47 /* backup over pointer to the buffer */
48 #define BUF_OVERTOBUF(op) ((cm_buf_t *)(((char *)op) - ((long)(&((cm_buf_t *)0)->over))))
49
50 #ifdef notdef
51 /* pretend we have logs, too */
52 typedef char cm_log_t;
53 #endif
54
55 #define CM_BUF_MAGIC    ('B' | 'U' <<8 | 'F'<<16 | 'F'<<24)
56
57 /* represents a single buffer */
58 typedef struct cm_buf {
59     osi_queue_t q;              /* queue of all zero-refcount buffers */
60     afs_uint32     magic;
61     struct cm_buf *allp;        /* next in all list */
62     struct cm_buf *hashp;       /* hash bucket pointer */
63     struct cm_buf *fileHashp;   /* file hash bucket pointer */
64     struct cm_buf *fileHashBackp;       /* file hash bucket back pointer */
65                                 /*
66                                  * The file hash chain is doubly linked, since
67                                  * these chains can get rather long.  The
68                                  * regular hash chain is only singly linked,
69                                  * since the chains should be short if the
70                                  * hash function is good and if there are
71                                  * enough buckets for the size of the cache.
72                                  */
73     osi_mutex_t mx;             /* mutex protecting structure except refcount */
74     unsigned long refCount;     /* reference count (buf_globalLock) */
75     long idCounter;             /* counter for softrefs; bumped at each recycle */
76     long dirtyCounter;          /* bumped at each dirty->clean transition */
77 #ifdef notdef
78     cm_log_t *logp;             /* log for this buffer, if any */
79     osi_hyper_t lsn;            /* lsn to force to (last LSN changing this buffer) */
80 #endif /* notdef */
81     osi_hyper_t offset;         /* offset */
82     cm_fid_t fid;               /* file ID */
83     long flags;                 /* flags we're using */
84     long size;                  /* size in bytes of this buffer */
85     char *datap;                /* data in this buffer */
86     unsigned long error;        /* last error code, if CM_BUF_ERROR is set */
87     cm_user_t *userp;           /* user who wrote to the buffer last */
88 #ifndef DJGPP
89     OVERLAPPED over;            /* overlapped structure for I/O */
90 #endif
91         
92     /* fields added for the CM; locked by scp->mx */
93     long dataVersion;           /* data version of this page */
94     long cmFlags;               /* flags for cm */
95 #ifdef DISKCACHE95
96     cm_diskcache_t *dcp;        /* diskcache structure */
97 #endif /* DISKCACHE95 */
98
99     /* syncop state */
100     afs_uint32 waitCount;           /* number of threads waiting */
101     afs_uint32 waitRequests;        /* num of thread wait requests */
102 } cm_buf_t;
103
104 /* values for cmFlags */
105 #define CM_BUF_CMFETCHING       1       /* fetching this buffer */
106 #define CM_BUF_CMSTORING        2       /* storing this buffer */
107 #define CM_BUF_CMFULLYFETCHED   4       /* read-while-fetching optimization */
108 /* waiting is done based on scp->flags */
109
110 /* represents soft reference which is OK to lose on a recycle */
111 typedef struct cm_softRef {
112     cm_buf_t *bufp;     /* buffer (may get reused) */
113     long counter;               /* counter of changes to identity */
114 } cm_softRef_t;
115
116 #define CM_BUF_READING  1       /* now reading buffer to the disk */
117 #define CM_BUF_WRITING  2       /* now writing buffer to the disk */
118 #define CM_BUF_INHASH   4       /* in the hash table */
119 #define CM_BUF_DIRTY            8       /* buffer is dirty */
120 #define CM_BUF_INLRU            0x10    /* in lru queue */
121 #define CM_BUF_ERROR            0x20    /* something went wrong on delayed write */
122 #define CM_BUF_WAITING  0x40    /* someone's waiting for a flag to change */
123 #define CM_BUF_EVWAIT   0x80    /* someone's waiting for the buffer event */
124 #define CM_BUF_EOF              0x100   /* read 0 bytes; used for detecting EOF */
125
126 typedef struct cm_buf_ops {
127     long (*Writep)(void *, osi_hyper_t *, long, long, struct cm_user *,
128                         struct cm_req *);
129     long (*Readp)(cm_buf_t *, long, long *, struct cm_user *);
130     long (*Stabilizep)(void *, struct cm_user *, struct cm_req *);
131     long (*Unstabilizep)(void *, struct cm_user *);
132 } cm_buf_ops_t;
133
134 /* global locks */
135 extern osi_rwlock_t buf_globalLock;
136
137 extern long buf_Init(int newFile, cm_buf_ops_t *, afs_uint64 nbuffers);
138
139 extern void buf_Shutdown(void);
140
141 extern long buf_CountFreeList(void);
142
143 extern void buf_Release(cm_buf_t *);
144
145 extern void buf_Hold(cm_buf_t *);
146
147 extern void buf_WaitIO(cm_scache_t *, cm_buf_t *);
148
149 extern void buf_LockedRelease(cm_buf_t *);
150
151 extern cm_buf_t *buf_LockedFind(struct cm_scache *, osi_hyper_t *);
152
153 extern cm_buf_t *buf_Find(struct cm_scache *, osi_hyper_t *);
154
155 #ifndef DJGPP
156 extern HANDLE buf_GetFileHandle(long);
157 #endif /* !DJGPP */
158
159 extern void buf_LockedCleanAsync(cm_buf_t *, cm_req_t *);
160
161 extern long buf_GetNewLocked(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
162
163 extern long buf_Get(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
164
165 extern long buf_GetNew(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
166
167 extern void buf_CleanAsync(cm_buf_t *, cm_req_t *);
168
169 extern void buf_CleanWait(cm_scache_t *, cm_buf_t *);
170
171 extern void buf_SetDirty(cm_buf_t *);
172
173 extern long buf_CleanAndReset(void);
174
175 extern void buf_ReserveBuffers(afs_uint64);
176
177 extern int buf_TryReserveBuffers(afs_uint64);
178
179 extern void buf_UnreserveBuffers(afs_uint64);
180
181 #ifdef TESTING
182 extern void buf_ValidateBufQueues(void);
183 #endif /* TESTING */
184
185 extern osi_log_t *buf_logp;
186
187 extern long buf_Truncate(struct cm_scache *scp, cm_user_t *userp,
188         cm_req_t *reqp, osi_hyper_t *sizep);
189
190 extern long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp,
191         cm_req_t *reqp);
192
193 extern long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp,
194         cm_req_t *reqp);
195
196 extern long buf_SetNBuffers(afs_uint64 nbuffers);
197
198 extern long buf_ValidateBuffers(void);
199
200 extern void buf_ForceTrace(BOOL flush);
201
202 /* error codes */
203 #define CM_BUF_EXISTS   1       /* buffer exists, and shouldn't */
204 #endif /*  _BUF_H__ENV_ */