Windows: split cm_buf_t.flags field to ensure proper locking
[openafs.git] / src / WINNT / afsd / cm_buf.h
index 3d92b8c..5f80ba6 100644 (file)
@@ -23,9 +23,6 @@
 /* default buffer size */
 #define CM_BUF_BLOCKSIZE CM_CONFIGDEFAULT_BLOCKSIZE
 
-/* default hash size */
-#define CM_BUF_HASHSIZE        1024
-
 /* cache type */
 #define CM_BUF_CACHETYPE_FILE 1
 #define CM_BUF_CACHETYPE_VIRTUAL 2
@@ -39,9 +36,6 @@ extern int buf_cacheType;
 /* another hash fn */
 #define BUF_FILEHASH(fidp) ((fidp)->hash % cm_data.buf_hashSize)
 
-/* backup over pointer to the buffer */
-#define BUF_OVERTOBUF(op) ((cm_buf_t *)(((char *)op) - ((long)(&((cm_buf_t *)0)->over))))
-
 #define CM_BUF_MAGIC    ('B' | 'U' <<8 | 'F'<<16 | 'F'<<24)
 
 #define CM_BUF_VERSION_BAD 0xFFFFFFFFFFFFFFFF
@@ -65,13 +59,13 @@ typedef struct cm_buf {
     struct cm_buf *dirtyp;     /* next in the dirty list */
     osi_mutex_t mx;            /* mutex protecting structure except refcount */
     afs_int32 refCount;                /* reference count (buf_globalLock) */
-    long idCounter;            /* counter for softrefs; bumped at each recycle */
-    long dirtyCounter;         /* bumped at each dirty->clean transition */
+    afs_uint32 dirtyCounter;   /* bumped at each dirty->clean transition */
     osi_hyper_t offset;                /* offset */
     cm_fid_t fid;              /* file ID */
-    afs_uint32 flags;          /* flags we're using */
+    afs_uint16 flags;          /* flags we're using - mx */
+    afs_uint16 qFlags;         /* queue/hash state flags - buf_globalLock */
     char *datap;               /* data in this buffer */
-    unsigned long error;       /* last error code, if CM_BUF_ERROR is set */
+    afs_uint32 error;          /* last error code, if CM_BUF_ERROR is set */
     cm_user_t *userp;          /* user who wrote to the buffer last */
         
     /* fields added for the CM; locked by scp->mx */
@@ -101,24 +95,24 @@ typedef struct cm_buf {
 #define CM_BUF_CMSTORING       2       /* storing this buffer */
 #define CM_BUF_CMFULLYFETCHED  4       /* read-while-fetching optimization */
 #define CM_BUF_CMWRITING        8       /* writing to this buffer */
+#define CM_BUF_CMBKGFETCH      16       /* background fetch queued by
+                                         * prefetch or redirector */
 /* waiting is done based on scp->flags.  Removing bits from cmFlags
    should be followed by waking the scp. */
 
-/* represents soft reference which is OK to lose on a recycle */
-typedef struct cm_softRef {
-    cm_buf_t *bufp;    /* buffer (may get reused) */
-    long counter;              /* counter of changes to identity */
-} cm_softRef_t;
+/* values for qFlags */
+#define CM_BUF_QINHASH 1       /* in the hash table */
+#define CM_BUF_QINLRU  2       /* in lru queue (aka free list) */
+#define CM_BUF_QINDL    4       /* in the dirty list */
+#define CM_BUF_QREDIR   8       /* buffer held by the redirector */
 
+/* values for flags */
 #define CM_BUF_READING 1       /* now reading buffer from the disk */
 #define CM_BUF_WRITING 2       /* now writing buffer to the disk */
-#define CM_BUF_INHASH  4       /* in the hash table */
-#define CM_BUF_DIRTY           8       /* buffer is dirty */
-#define CM_BUF_INLRU           0x10    /* in lru queue */
-#define CM_BUF_ERROR           0x20    /* something went wrong on delayed write */
+#define CM_BUF_DIRTY   8       /* buffer is dirty */
+#define CM_BUF_ERROR   0x20    /* something went wrong on delayed write */
 #define CM_BUF_WAITING 0x40    /* someone's waiting for a flag to change */
-#define CM_BUF_EVWAIT  0x80    /* someone's waiting for the buffer event */
-#define CM_BUF_EOF             0x100   /* read 0 bytes; used for detecting EOF */
+#define CM_BUF_EOF     0x80    /* read 0 bytes; used for detecting EOF */
 
 typedef struct cm_buf_ops {
     long (*Writep)(void *, osi_hyper_t *, long, long, struct cm_user *,
@@ -137,33 +131,52 @@ extern void buf_Shutdown(void);
 
 extern long buf_CountFreeList(void);
 
+#ifdef DEBUG_REFCOUNT
+extern void buf_ReleaseDbg(cm_buf_t *, char *, long);
+
+extern void buf_HoldDbg(cm_buf_t *, char *, long);
+
+extern void buf_ReleaseLockedDbg(cm_buf_t *, afs_uint32, char *, long);
+
+extern void buf_HoldLockedDbg(cm_buf_t *, char *, long);
+
+#define buf_Release(bufp) buf_ReleaseDbg(bufp, __FILE__, __LINE__)
+#define buf_Hold(bufp)    buf_HoldDbg(bufp, __FILE__, __LINE__)
+#define buf_ReleaseLocked(bufp, lock) buf_ReleaseLockedDbg(bufp, lock, __FILE__, __LINE__)
+#define buf_HoldLocked(bufp) buf_HoldLockedDbg(bufp, __FILE__, __LINE__)
+#else
 extern void buf_Release(cm_buf_t *);
 
 extern void buf_Hold(cm_buf_t *);
 
-extern void buf_WaitIO(cm_scache_t *, cm_buf_t *);
-
 extern void buf_ReleaseLocked(cm_buf_t *, afs_uint32);
 
 extern void buf_HoldLocked(cm_buf_t *);
+#endif
+
+extern void buf_WaitIO(cm_scache_t *, cm_buf_t *);
 
 extern cm_buf_t *buf_FindLocked(struct cm_scache *, osi_hyper_t *);
 
 extern cm_buf_t *buf_Find(struct cm_scache *, osi_hyper_t *);
 
-extern long buf_GetNewLocked(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
+extern cm_buf_t *buf_FindAllLocked(struct cm_scache *, osi_hyper_t *, afs_uint32 flags);
+extern cm_buf_t *buf_FindAll(struct cm_scache *, osi_hyper_t *, afs_uint32 flags);
+extern long buf_GetNewLocked(struct cm_scache *, osi_hyper_t *, cm_req_t *, cm_buf_t **);
 
-extern long buf_Get(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
+extern long buf_Get(struct cm_scache *, osi_hyper_t *, cm_req_t *, cm_buf_t **);
 
-extern long buf_GetNew(struct cm_scache *, osi_hyper_t *, cm_buf_t **);
+extern long buf_GetNew(struct cm_scache *, osi_hyper_t *, cm_req_t *, cm_buf_t **);
 
-extern long buf_CleanAsyncLocked(cm_buf_t *, cm_req_t *);
+extern afs_uint32 buf_CleanAsyncLocked(cm_buf_t *, cm_req_t *, afs_uint32 *);
 
-extern long buf_CleanAsync(cm_buf_t *, cm_req_t *);
+extern afs_uint32 buf_CleanAsync(cm_buf_t *, cm_req_t *, afs_uint32 *);
 
-extern void buf_CleanWait(cm_scache_t *, cm_buf_t *);
+extern void buf_CleanWait(cm_scache_t *, cm_buf_t *, afs_uint32 locked);
 
-extern void buf_SetDirty(cm_buf_t *, afs_uint32 offset, afs_uint32 length);
+extern void buf_SetDirty(cm_buf_t *, afs_uint32 offset, afs_uint32 length, cm_user_t *);
 
 extern long buf_CleanAndReset(void);
 
@@ -200,6 +213,8 @@ extern long buf_CleanDirtyBuffers(cm_scache_t *scp);
 
 extern long buf_ForceDataVersion(cm_scache_t * scp, afs_uint64 fromVersion, afs_uint64 toVersion);
 
+extern int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock);
+
 /* error codes */
 #define CM_BUF_EXISTS  1       /* buffer exists, and shouldn't */
 #endif /*  _BUF_H__ENV_ */