windows-unicode-20080626
[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 /* 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 of all zero-refcount buffers */
46     afs_uint32     magic;
47     struct cm_buf *allp;        /* next in all list */
48     struct cm_buf *hashp;       /* hash bucket pointer */
49     struct cm_buf *fileHashp;   /* file hash bucket pointer */
50     struct cm_buf *fileHashBackp;       /* file hash bucket back pointer */
51                                 /*
52                                  * The file hash chain is doubly linked, since
53                                  * these chains can get rather long.  The
54                                  * regular hash chain is only singly linked,
55                                  * since the chains should be short if the
56                                  * hash function is good and if there are
57                                  * enough buckets for the size of the cache.
58                                  */
59     struct cm_buf *dirtyp;      /* next in the dirty list */
60     osi_mutex_t mx;             /* mutex protecting structure except refcount */
61     afs_int32 refCount;         /* reference count (buf_globalLock) */
62     afs_uint32 dirtyCounter;    /* bumped at each dirty->clean transition */
63     osi_hyper_t offset;         /* offset */
64     cm_fid_t fid;               /* file ID */
65     afs_uint32 flags;           /* flags we're using */
66     char *datap;                /* data in this buffer */
67     afs_uint32 error;           /* last error code, if CM_BUF_ERROR is set */
68     cm_user_t *userp;           /* user who wrote to the buffer last */
69         
70     /* fields added for the CM; locked by scp->mx */
71     afs_uint64 dataVersion;     /* data version of this page */
72     afs_uint32 cmFlags;         /* flags for cm */
73
74     /* syncop state */
75     afs_uint32 waitCount;       /* number of threads waiting */
76     afs_uint32 waitRequests;    /* num of thread wait requests */
77
78     afs_uint32 dirty_offset;    /* offset from beginning of buffer containing dirty bytes */
79     afs_uint32 dirty_length;      /* number of dirty bytes within the buffer */
80
81 #ifdef DISKCACHE95
82     cm_diskcache_t *dcp;        /* diskcache structure */
83 #endif /* DISKCACHE95 */
84 #ifdef DEBUG
85     cm_scache_t *scp;           /* for debugging, the scache object belonging to */
86                                 /* the fid at the time of fid assignment. */
87 #else
88     void * dummy;
89 #endif
90 } cm_buf_t;
91
92 /* values for cmFlags */
93 #define CM_BUF_CMFETCHING       1       /* fetching this buffer */
94 #define CM_BUF_CMSTORING        2       /* storing this buffer */
95 #define CM_BUF_CMFULLYFETCHED   4       /* read-while-fetching optimization */
96 #define CM_BUF_CMWRITING        8       /* writing to this buffer */
97 /* waiting is done based on scp->flags.  Removing bits from cmFlags
98    should be followed by waking the scp. */
99
100 #define CM_BUF_READING  1       /* now reading buffer from the disk */
101 #define CM_BUF_WRITING  2       /* now writing buffer to the disk */
102 #define CM_BUF_INHASH   4       /* in the hash table */
103 #define CM_BUF_DIRTY    8       /* buffer is dirty */
104 #define CM_BUF_INLRU    0x10    /* in lru queue */
105 #define CM_BUF_ERROR    0x20    /* something went wrong on delayed write */
106 #define CM_BUF_WAITING  0x40    /* someone's waiting for a flag to change */
107 #define CM_BUF_INDL     0x80    /* in the dirty list */
108 #define CM_BUF_EOF      0x100   /* read 0 bytes; used for detecting EOF */
109 #define CM_BUF_REDIR    0x200   /* buffer held by the redirector */
110
111 typedef struct cm_buf_ops {
112     long (*Writep)(void *, osi_hyper_t *, long, long, struct cm_user *,
113                         struct cm_req *);
114     long (*Readp)(cm_buf_t *, long, long *, struct cm_user *);
115     long (*Stabilizep)(void *, struct cm_user *, struct cm_req *);
116     long (*Unstabilizep)(void *, struct cm_user *);
117 } cm_buf_ops_t;
118
119 /* global locks */
120 extern osi_rwlock_t buf_globalLock;
121
122 extern long buf_Init(int newFile, cm_buf_ops_t *, afs_uint64 nbuffers);
123
124 extern void buf_Shutdown(void);
125
126 extern long buf_CountFreeList(void);
127
128 #ifdef DEBUG_REFCOUNT
129 extern void buf_ReleaseDbg(cm_buf_t *, char *, long);
130
131 extern void buf_HoldDbg(cm_buf_t *, char *, long);
132
133 extern void buf_ReleaseLockedDbg(cm_buf_t *, afs_uint32, char *, long);
134
135 extern void buf_HoldLockedDbg(cm_buf_t *, char *, long);
136
137 #define buf_Release(bufp) buf_ReleaseDbg(bufp, __FILE__, __LINE__)
138 #define buf_Hold(bufp)    buf_HoldDbg(bufp, __FILE__, __LINE__)
139 #define buf_ReleaseLocked(bufp, lock) buf_ReleaseLockedDbg(bufp, lock, __FILE__, __LINE__)
140 #define buf_HoldLocked(bufp) buf_HoldLockedDbg(bufp, __FILE__, __LINE__)
141 #else
142 extern void buf_Release(cm_buf_t *);
143
144 extern void buf_Hold(cm_buf_t *);
145
146 extern void buf_ReleaseLocked(cm_buf_t *, afs_uint32);
147
148 extern void buf_HoldLocked(cm_buf_t *);
149 #endif
150
151 extern void buf_WaitIO(cm_scache_t *, cm_buf_t *);
152
153 extern cm_buf_t *buf_FindLocked(struct cm_scache *, osi_hyper_t *);
154
155 extern cm_buf_t *buf_Find(struct cm_scache *, osi_hyper_t *);
156
157 extern long buf_GetNewLocked(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
158
159 extern long buf_Get(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
160
161 extern long buf_GetNew(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
162
163 extern long buf_CleanAsyncLocked(cm_buf_t *, cm_req_t *);
164
165 extern long buf_CleanAsync(cm_buf_t *, cm_req_t *);
166
167 extern void buf_CleanWait(cm_scache_t *, cm_buf_t *, afs_uint32 locked);
168
169 extern void buf_SetDirty(cm_buf_t *, afs_uint32 offset, afs_uint32 length);
170
171 extern long buf_CleanAndReset(void);
172
173 extern void buf_ReserveBuffers(afs_uint64);
174
175 extern int buf_TryReserveBuffers(afs_uint64);
176
177 extern void buf_UnreserveBuffers(afs_uint64);
178
179 #ifdef TESTING
180 extern void buf_ValidateBufQueues(void);
181 #endif /* TESTING */
182
183 extern osi_log_t *buf_logp;
184
185 extern long buf_Truncate(struct cm_scache *scp, cm_user_t *userp,
186         cm_req_t *reqp, osi_hyper_t *sizep);
187
188 extern long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp,
189         cm_req_t *reqp);
190
191 extern long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp,
192         cm_req_t *reqp);
193
194 extern long buf_SetNBuffers(afs_uint64 nbuffers);
195
196 extern long buf_ValidateBuffers(void);
197
198 extern void buf_ForceTrace(BOOL flush);
199
200 extern long buf_DirtyBuffersExist(cm_fid_t * fidp);
201
202 extern long buf_CleanDirtyBuffers(cm_scache_t *scp);
203
204 extern long buf_ForceDataVersion(cm_scache_t * scp, afs_uint64 fromVersion, afs_uint64 toVersion);
205
206 extern int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock);
207
208 /* error codes */
209 #define CM_BUF_EXISTS   1       /* buffer exists, and shouldn't */
210 #endif /*  _BUF_H__ENV_ */