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