00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _EYEDBSM_EYEDBSM_P_H
00026 #define _EYEDBSM_EYEDBSM_P_H
00027
00028 #include "eyedbconfig.h"
00029
00030 #include <sys/types.h>
00031
00032 #include <eyedblib/m_mem.h>
00033 #include <eyedbsm/smd.h>
00034 #include <eyedblib/machtypes.h>
00035 #include "eyedbsm/transaction.h"
00036 #include <eyedbsm/eyedbsm.h>
00037 #include "eyedbsm/mutex.h"
00038 #include <eyedblib/log.h>
00039 #include <unistd.h>
00040
00041 namespace eyedbsm {
00042
00043 struct ObjectHeader {
00044 unsigned int unique;
00045 unsigned int size;
00046 Oid prot_oid;
00047 };
00048
00049 #define ESM_IND_OID
00050
00051 static const unsigned int MAGIC = 0xa81726e1U;
00052
00053 static const int DEFAULT_CREATION_MODE = 0600;
00054
00055 static const int MAX_ROOT_ENTRIES = 32;
00056
00057 static const int UNIQUE_BASE = 128;
00058 static const unsigned int UNIQUE_BOUND = (unsigned int)((1 << Oid_UNIQUE)-1);
00059
00060
00061 static const int MAX_FREE_CELLS = 1000000;
00062
00063 #define LOCK_COND
00064 #define MAXCLIENTS_PERDB 128
00065
00066
00067 struct DbLock {
00068 MutexP mp;
00069 CondWaitP cond_wait;
00070 unsigned short S, X;
00071 int wt_cnt;
00072 unsigned int xidX;
00073 unsigned int xidS[MAXCLIENTS_PERDB];
00074 };
00075
00076 enum {
00077 MAP,
00078 TRS,
00079 NX,
00080 SLT,
00081 LSL,
00082 MTX_CNT
00083 };
00084
00085 struct DbMutexes {
00086 MutexP mp[MTX_CNT];
00087 };
00088
00089 struct DbStat {
00090 unsigned int total_db_access_cnt;
00091 unsigned int current_db_access_cnt;
00092 unsigned int tr_begin_cnt;
00093 unsigned int tr_commit_cnt;
00094 unsigned int tr_abort_cnt;
00095 };
00096
00097
00098
00099 struct DbShmHeader {
00100 unsigned int magic;
00101 unsigned int version;
00102 DbStat stat;
00103 unsigned int xid;
00104 eyedblib::int64 hostid;
00105 char hostname[64];
00106 char arch[16];
00107 MutexP main_mp;
00108 #ifdef USE_SHM_REFCNT
00109 int refcnt;
00110 #else
00111 int dummy;
00112 #endif
00113 DbLock dblock_W;
00114 DbLock dblock_RW;
00115 DbLock dblock_Wtrans;
00116 TransHeader trs_hdr;
00117 DbMutexes mtx;
00118 };
00119
00120
00121
00122
00123
00124 struct MmapDesc {
00125 Boolean ismapped, locked;
00126 off_t s_start, s_end, a_start, a_end;
00127 char *mapaddr;
00128 unsigned int ref;
00129 int npts, nalloc;
00130 char ***pts;
00131 m_Map *m;
00132 };
00133
00134 struct MmapH {
00135 MmapDesc *mmd;
00136 int pos;
00137 char **pt;
00138 };
00139
00140 static const int MAX_MMAP_SEGMENTS = 256;
00141
00142 static const char OPENED_STATE = 1;
00143 static const char OPENING_STATE = 2;
00144
00145 struct DatDesc {
00146 int fd;
00147 const char *file;
00148 MmapDesc mmd[MAX_MMAP_SEGMENTS];
00149 m_Map *m_dat;
00150 char *addr;
00151 };
00152
00153 struct ProtectionHeader {
00154 int nprot_uid;
00155 Oid prot_uid_oid;
00156 int nprot_list;
00157 Oid prot_list_oid;
00158 };
00159
00160 #define OIDLOCSIZE 6
00161
00162 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00163 #define ESM_NSEMS 2
00164 #else
00165 #define ESM_NSEMS 0
00166 #endif
00167
00168 struct ProtectionDescriptionInternal {
00169 char name[PROT_NAME];
00170 unsigned int nprot;
00171 Protection prot[1];
00172 };
00173
00174 #define protectionDescriptionInternalSize(N) \
00175 (sizeof(ProtectionDescriptionInternal) + \
00176 ( ((N) - 1) * sizeof(Protection) ))
00177
00178 struct DbDescription {
00179
00180 int dbid, flags, shmfd;
00181 int lkfd;
00182 OpenHints hints;
00183 Boolean rsuser, suser;
00184 int uid, uid_ind;
00185 unsigned int version;
00186
00187
00188 m_Map *m_dbs;
00189 unsigned char *dbs_addr;
00190
00191
00192 m_Map *m_shm;
00193 DbShmHeader *shm_addr;
00194 DbShmHeader *shm_addr_b;
00195
00196
00197 m_Map *m_omp;
00198 void *omp_addr;
00199
00200
00201 m_Map *m_dmp[MAX_DATAFILES+1];
00202 char *dmp_addr[MAX_DATAFILES+1];
00203
00204
00205 DatDesc dmd[MAX_DATAFILES+1];
00206
00207
00208 unsigned reg_mask;
00209 unsigned int reg_alloc;
00210 Register *reg;
00211
00212
00213 int nprot_uid;
00214 DbProtectionDescription *vol_uid;
00215 int nprot_list;
00216 Oid *vol_prot_list_oid;
00217 ProtectionDescriptionInternal **vol_desc_list;
00218
00219 smdcli_conn_t *conn;
00220
00221
00222 int tr_cnt;
00223 TransactionContext trctx[MAXTRS];
00224 unsigned int xid;
00225 unsigned int mapwide;
00226 unsigned int mapwide2;
00227 XMHandle *trs_mh;
00228 Mutex mp[MTX_CNT+1];
00229 CondWaitList cdw_list;
00230 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00231 int locked;
00232 int semkeys[ESM_NSEMS];
00233 #endif
00234 };
00235
00236 struct DbHandle {
00237 DbDescription *vd;
00238 char *dbfile;
00239 int xid;
00240 int tr_cnt;
00241 int flags;
00242 };
00243
00244 #define VD2MTX(VD, W) ((VD) ? &(VD)->mp[(W)] : 0)
00245 #define MTX(DBH, W) VD2MTX((DBH)->vd, W)
00246
00247 #define MAP_MTX(DBH) MTX(DBH, MAP)
00248 #define TRS_MTX(DBH) MTX(DBH, TRS)
00249 #define NX_MTX(DBH) MTX(DBH, NX)
00250 #define SLT_MTX(DBH) MTX(DBH, SLT)
00251 #define LSL_MTX(DBH) MTX(DBH, LSL)
00252
00253 #define DBLOCK_MTX(VD) VD2MTX(VD, MTX_CNT)
00254 #define DBLOCK_COND(VD) ((VD) ? &VD2MTX(VD, MTX_CNT)->cond : 0)
00255
00256 extern Status
00257 ESM_transactionsRelease(DbDescription *vd, DbShmHeader *shmh,
00258 const char *dbfile, int xid,
00259 XMHandle *xmh, Boolean *);
00260
00261 extern Status privilegeCheck(void);
00262
00263 extern Boolean backend_interrupt;
00264
00265 extern int lockTimeout;
00266
00267 #define ESM_ASSERT_ABORT(X, MT, XID) \
00268 if (!(X)) \
00269 { \
00270 if (MT) MUTEX_UNLOCK(MT, XID); \
00271 utlog("ASSERT '%s' file \"%s\", line #%d\n", #X, __FILE__, __LINE__); \
00272 abort(); \
00273 }
00274
00275 #define ESM_ASSERT(X, MT, XID) \
00276 if (!(X)) \
00277 { \
00278 return statusMake(INTERNAL_ERROR, "assertion failed `%s' file `%s', line #%d\n", #X, __FILE__, __LINE__); \
00279 }
00280
00281 #define DBH2TRCTX(DBH) (&(DBH)->vd->trctx[(DBH)->vd->tr_cnt-1])
00282
00283 #define NEED_LOCK(TRCTX) \
00284 ((TRCTX)->params.lockmode != DatabaseW)
00285
00286 #define NEED_OBJLOCK(TRCTX) \
00287 ((TRCTX)->params.lockmode != ReadNWriteN && \
00288 (TRCTX)->params.lockmode != DatabaseW)
00289
00290 static const char* msg_1 = __FILE__;
00291 #define TTT(S) (write( 1, S, strlen(S)))
00292 static int dummy_off_t_1 = (sizeof(off_t) != 8 ? (TTT(msg_1), *(char *)0 = 0) : 1);
00293
00294 }
00295
00296 #endif