3c5e293cead5a2983d9f6d7c18db0343ee4734c8
[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 OPENAFS_WINNT_AFSD_BUF_H
13 #define OPENAFS_WINNT_AFSD_BUF_H 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 /* cache type */
27 #define CM_BUF_CACHETYPE_FILE 1
28 #define CM_BUF_CACHETYPE_VIRTUAL 2
29 extern int buf_cacheType;
30
31 /* force it to be signed so that mod comes out positive or 0 */
32 #define BUF_HASH(fidp,offsetp) ((((fidp)->hash \
33                                 +(offsetp)->LowPart) / cm_data.buf_blockSize)   \
34                                    % cm_data.buf_hashSize)
35
36 /* another hash fn */
37 #define BUF_FILEHASH(fidp) ((fidp)->hash % cm_data.buf_hashSize)
38
39 #define CM_BUF_MAGIC    ('B' | 'U' <<8 | 'F'<<16 | 'F'<<24)
40
41 #define CM_BUF_VERSION_BAD 0xFFFFFFFFFFFFFFFF
42
43 /* represents a single buffer */
44 typedef struct cm_buf {
45     osi_queue_t    q;           /* queue: buf_freeList and buf_redirList */
46     afs_uint32     qFlags;      /* queue/hash state flags - buf_globalLock */
47     afs_uint32     magic;
48     struct cm_buf *allp;        /* next in all list */
49     struct cm_buf *hashp;       /* hash bucket pointer */
50     struct cm_buf *fileHashp;   /* file hash bucket pointer */
51     struct cm_buf *fileHashBackp;       /* file hash bucket back pointer */
52                                 /*
53                                  * The file hash chain is doubly linked, since
54                                  * these chains can get rather long.  The
55                                  * regular hash chain is only singly linked,
56                                  * since the chains should be short if the
57                                  * hash function is good and if there are
58                                  * enough buckets for the size of the cache.
59                                  */
60     struct cm_buf *dirtyp;      /* next in the dirty list */
61     osi_mutex_t mx;             /* mutex protecting structure except refcount */
62     afs_int32 refCount;         /* reference count (buf_globalLock) */
63     afs_uint32 dirtyCounter;    /* bumped at each dirty->clean transition */
64     osi_hyper_t offset;         /* offset */
65     cm_fid_t fid;               /* file ID */
66     char *datap;                /* data in this buffer */
67     afs_uint32 flags;           /* flags we're using - mx */
68     afs_uint32 error;           /* last error code, if CM_BUF_ERROR is set */
69     cm_user_t *userp;           /* user who wrote to the buffer last */
70
71     /* fields added for the CM; locked by scp->mx */
72     afs_uint64 dataVersion;     /* data version of this page */
73     afs_uint32 cmFlags;         /* flags for cm */
74
75     /* syncop state */
76     afs_uint32 waitCount;       /* number of threads waiting */
77     afs_uint32 waitRequests;    /* num of thread wait requests */
78
79     afs_uint32 dirty_offset;    /* offset from beginning of buffer containing dirty bytes */
80     afs_uint32 dirty_length;    /* number of dirty bytes within the buffer */
81
82 #ifdef DISKCACHE95
83     cm_diskcache_t *dcp;        /* diskcache structure */
84 #endif /* DISKCACHE95 */
85 #ifdef DEBUG
86     cm_scache_t *scp;           /* for debugging, the scache object belonging to */
87                                 /* the fid at the time of fid assignment. */
88 #else
89     void * dummy;
90 #endif
91
92     /* redirector state - protected by buf_globalLock */
93     osi_queue_t redirq;         /* queue: cm_scache_t redirList */
94     time_t      redirLastAccess;/* last time redir accessed the buffer */
95     time_t      redirReleaseRequested;
96
97     unsigned char md5cksum[16]; /* md5 checksum of the block pointed to by datap */
98 } cm_buf_t;
99
100 #define redirq_to_cm_buf_t(q) ((q) ? (cm_buf_t *)((char *) (q) - offsetof(cm_buf_t, redirq)) : NULL)
101
102 /* values for cmFlags */
103 #define CM_BUF_CMFETCHING       1       /* fetching this buffer */
104 #define CM_BUF_CMSTORING        2       /* storing this buffer */
105 #define CM_BUF_CMFULLYFETCHED   4       /* read-while-fetching optimization */
106 #define CM_BUF_CMWRITING        8       /* writing to this buffer */
107 /* waiting is done based on scp->flags.  Removing bits from cmFlags
108    should be followed by waking the scp. */
109
110 /* values for qFlags */
111 #define CM_BUF_QINHASH  1       /* in the hash table */
112 #define CM_BUF_QINLRU   2       /* in lru queue (aka free list) */
113 #define CM_BUF_QINDL    4       /* in the dirty list */
114 #define CM_BUF_QREDIR   8       /* buffer held by the redirector */
115
116 /* values for flags */
117 #define CM_BUF_READING  1       /* now reading buffer from the disk */
118 #define CM_BUF_WRITING  2       /* now writing buffer to the disk */
119 #define CM_BUF_DIRTY    8       /* buffer is dirty */
120 #define CM_BUF_ERROR    0x20    /* something went wrong on delayed write */
121 #define CM_BUF_WAITING  0x40    /* someone's waiting for a flag to change */
122 #define CM_BUF_EOF      0x80    /* read 0 bytes; used for detecting EOF */
123
124 typedef struct cm_buf_ops {
125     long (*Writep)(void *vscp, osi_hyper_t *offsetp,
126                    long length, long flags,
127                    struct cm_user *userp,
128                    struct cm_req *reqp);
129     long (*Readp)(cm_buf_t *bufp, long length,
130                   long *bytesReadp, struct cm_user *userp);
131     long (*Stabilizep)(void *vscp, struct cm_user *userp, struct cm_req *reqp);
132     long (*Unstabilizep)(void *vscp, struct cm_user *userp);
133 } cm_buf_ops_t;
134
135 #define CM_BUF_WRITE_SCP_LOCKED 0x1
136
137 /* global locks */
138 extern osi_rwlock_t buf_globalLock;
139
140 extern long buf_Init(int newFile, cm_buf_ops_t *, afs_uint64 nbuffers);
141
142 extern void buf_Shutdown(void);
143
144 #ifdef DEBUG_REFCOUNT
145 extern void buf_ReleaseDbg(cm_buf_t *, char *, long);
146
147 extern void buf_HoldDbg(cm_buf_t *, char *, long);
148
149 extern void buf_ReleaseLockedDbg(cm_buf_t *, afs_uint32, char *, long);
150
151 extern void buf_HoldLockedDbg(cm_buf_t *, char *, long);
152
153 #define buf_Release(bufp) buf_ReleaseDbg(bufp, __FILE__, __LINE__)
154 #define buf_Hold(bufp)    buf_HoldDbg(bufp, __FILE__, __LINE__)
155 #define buf_ReleaseLocked(bufp, lock) buf_ReleaseLockedDbg(bufp, lock, __FILE__, __LINE__)
156 #define buf_HoldLocked(bufp) buf_HoldLockedDbg(bufp, __FILE__, __LINE__)
157 #else
158 extern void buf_Release(cm_buf_t *);
159
160 extern void buf_Hold(cm_buf_t *);
161
162 extern void buf_ReleaseLocked(cm_buf_t *, afs_uint32);
163
164 extern void buf_HoldLocked(cm_buf_t *);
165 #endif
166
167 extern void buf_WaitIO(cm_scache_t *, cm_buf_t *);
168
169 extern cm_buf_t *buf_FindLocked(struct cm_fid *, osi_hyper_t *);
170
171 extern cm_buf_t *buf_Find(struct cm_fid *, osi_hyper_t *);
172
173 extern cm_buf_t *buf_FindAllLocked(struct cm_fid *, osi_hyper_t *, afs_uint32 flags);
174
175 extern cm_buf_t *buf_FindAll(struct cm_fid *, osi_hyper_t *, afs_uint32 flags);
176
177 extern long buf_GetNewLocked(struct cm_scache *, osi_hyper_t *, cm_req_t *, cm_buf_t **);
178
179 extern long buf_Get(struct cm_scache *, osi_hyper_t *, cm_req_t *, cm_buf_t **);
180
181 extern afs_uint32 buf_CleanAsyncLocked(cm_scache_t *, cm_buf_t *, cm_req_t *, afs_uint32 flags, afs_uint32 *);
182
183 extern afs_uint32 buf_CleanAsync(cm_scache_t *, cm_buf_t *, cm_req_t *, afs_uint32 flags, afs_uint32 *);
184
185 extern void buf_CleanWait(cm_scache_t *, cm_buf_t *, afs_uint32 locked);
186
187 extern void buf_SetDirty(cm_buf_t *, cm_req_t *, afs_uint32 offset, afs_uint32 length, cm_user_t *);
188
189 extern long buf_CleanAndReset(void);
190
191 extern void buf_ReserveBuffers(afs_uint64);
192
193 extern int buf_TryReserveBuffers(afs_uint64);
194
195 extern void buf_UnreserveBuffers(afs_uint64);
196
197 #ifdef TESTING
198 extern void buf_ValidateBufQueues(void);
199 #endif /* TESTING */
200
201 extern osi_log_t *buf_logp;
202
203 extern long buf_Truncate(struct cm_scache *scp, cm_user_t *userp,
204         cm_req_t *reqp, osi_hyper_t *sizep);
205
206 extern long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp,
207         cm_req_t *reqp);
208
209 extern long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp,
210         cm_req_t *reqp);
211
212 extern long buf_SetNBuffers(afs_uint64 nbuffers);
213
214 extern long buf_ValidateBuffers(void);
215
216 extern void buf_ForceTrace(BOOL flush);
217
218 extern long buf_DirtyBuffersExist(cm_fid_t * fidp);
219
220 extern long buf_CleanDirtyBuffers(cm_scache_t *scp);
221
222 extern long buf_RDRBuffersExist(cm_fid_t *fidp);
223
224 extern long buf_ClearRDRFlag(cm_scache_t *scp, char * reason);
225
226 extern long buf_ForceDataVersion(cm_scache_t * scp, afs_uint64 fromVersion, afs_uint64 toVersion);
227
228 extern int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock);
229
230 extern void buf_ComputeCheckSum(cm_buf_t *bp);
231
232 extern int  buf_ValidateCheckSum(cm_buf_t *bp);
233
234 extern const char *buf_HexCheckSum(cm_buf_t * bp);
235
236 extern afs_uint32
237 buf_RDRShakeSomeExtentsFree(cm_req_t *reqp, afs_uint32 oneFid, afs_uint32 minage);
238
239 extern afs_uint32
240 buf_RDRShakeAnExtentFree(cm_buf_t *bufp, cm_req_t *reqp);
241
242 extern afs_uint32
243 buf_RDRShakeFileExtentsFree(cm_scache_t *scp, cm_req_t *reqp);
244
245 extern void
246 buf_InsertToRedirQueue(cm_scache_t *scp, cm_buf_t *bufp);
247
248 extern void
249 buf_RemoveFromRedirQueue(cm_scache_t *scp, cm_buf_t *bufp);
250
251 extern void
252 buf_MoveToHeadOfRedirQueue(cm_scache_t *scp, cm_buf_t *bufp);
253
254 #ifdef _M_IX86
255 #define buf_IncrementRedirCount()  InterlockedIncrement(&cm_data.buf_redirCount)
256 #define buf_DecrementRedirCount()  InterlockedDecrement(&cm_data.buf_redirCount)
257 #define buf_IncrementFreeCount()   InterlockedIncrement(&cm_data.buf_freeCount)
258 #define buf_DecrementFreeCount()   InterlockedDecrement(&cm_data.buf_freeCount)
259 #else
260 #define buf_IncrementRedirCount()  InterlockedIncrement64(&cm_data.buf_redirCount)
261 #define buf_DecrementRedirCount()  InterlockedDecrement64(&cm_data.buf_redirCount)
262 #define buf_IncrementFreeCount()   InterlockedIncrement64(&cm_data.buf_freeCount)
263 #define buf_DecrementFreeCount()   InterlockedDecrement64(&cm_data.buf_freeCount)
264 #endif
265
266 /* error codes */
267 #define CM_BUF_EXISTS   1       /* buffer exists, and shouldn't */
268
269 #endif /* OPENAFS_WINNT_AFSD_BUF_H */