eyedbsm_p.h

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
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   //static const int DBID_L_MASK =  ((1 << Oid_DBID_L)-1);
00061   static const int MAX_FREE_CELLS = 1000000;
00062 
00063 #define LOCK_COND
00064 #define MAXCLIENTS_PERDB 128
00065 
00066   // shm file structures ----------------------------------------------------
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   /*#define USE_SHM_REFCNT*/
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;      // database exclusive lock for writing
00114     DbLock dblock_RW;     // database exclusive lock for reading and writing
00115     DbLock dblock_Wtrans; // database exclusive lock for writing transaction
00116     TransHeader trs_hdr;
00117     DbMutexes mtx;
00118   };
00119   
00120   // end of shm file structures ---------------------------------------------
00121 
00122   // runtime structures -----------------------------------------------------
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     /* general information */
00180     int dbid, flags, shmfd;
00181     int lkfd;
00182     OpenHints hints;
00183     Boolean rsuser, suser;
00184     int uid, uid_ind;
00185 
00186     /* dbs file */
00187     m_Map *m_dbs;
00188     unsigned char *dbs_addr;
00189   
00190     /* shmg file */
00191     m_Map *m_shm;
00192     DbShmHeader *shm_addr;
00193 
00194     /* omp file */
00195     m_Map *m_omp;
00196     void *omp_addr;
00197 
00198     /* dmp files */
00199     m_Map *m_dmp[MAX_DATAFILES+1];
00200     char *dmp_addr[MAX_DATAFILES+1];
00201 
00202     /* dat files */
00203     DatDesc dmd[MAX_DATAFILES+1]; 
00204 
00205     /* registering */
00206     unsigned reg_mask;
00207     unsigned int reg_alloc;
00208     Register *reg;
00209 
00210     /* protection management */
00211     int nprot_uid;
00212     DbProtectionDescription *vol_uid;
00213     int nprot_list;
00214     Oid *vol_prot_list_oid;
00215     ProtectionDescriptionInternal **vol_desc_list;
00216 
00217     smdcli_conn_t *conn;
00218 
00219     /* transaction management */
00220     int tr_cnt;
00221     TransactionContext trctx[MAXTRS];
00222     unsigned int xid;
00223     unsigned int mapwide;
00224     unsigned int mapwide2;
00225     XMHandle *trs_mh;
00226     Mutex mp[MTX_CNT+1];
00227     CondWaitList cdw_list;
00228 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00229     int locked;
00230     int semkeys[ESM_NSEMS];
00231 #endif
00232   };
00233 
00234   struct DbHandle {
00235     DbDescription *vd;
00236     char *dbfile;
00237     int xid;
00238     int tr_cnt;
00239     int flags;
00240   };
00241 
00242 #define VD2MTX(VD, W)  ((VD) ? &(VD)->mp[(W)] : 0)
00243 #define MTX(DBH, W)    VD2MTX((DBH)->vd, W)
00244 
00245 #define MAP_MTX(DBH) MTX(DBH, MAP)
00246 #define TRS_MTX(DBH) MTX(DBH, TRS)
00247 #define NX_MTX(DBH)  MTX(DBH, NX)
00248 #define SLT_MTX(DBH) MTX(DBH, SLT)
00249 #define LSL_MTX(DBH) MTX(DBH, LSL)
00250 
00251 #define DBLOCK_MTX(VD)  VD2MTX(VD, MTX_CNT)
00252 #define DBLOCK_COND(VD) ((VD) ? &VD2MTX(VD, MTX_CNT)->cond : 0)
00253 
00254   extern Status
00255   ESM_transactionsRelease(DbDescription *vd, DbShmHeader *shmh,
00256                           const char *dbfile, int xid,
00257                           XMHandle *xmh, Boolean *);
00258 
00259   extern Status privilegeCheck(void);
00260 
00261   extern Boolean backend_interrupt;
00262 
00263   extern int lockTimeout;
00264 
00265 #define ESM_ASSERT_ABORT(X, MT, XID) \
00266 if (!(X)) \
00267 { \
00268   if (MT) MUTEX_UNLOCK(MT, XID); \
00269   utlog("ASSERT '%s' file \"%s\", line #%d\n", #X, __FILE__, __LINE__); \
00270   abort(); \
00271 }
00272 
00273 #define ESM_ASSERT(X, MT, XID) \
00274 if (!(X)) \
00275 { \
00276   return statusMake(INTERNAL_ERROR, "assertion failed `%s' file `%s', line #%d\n", #X, __FILE__, __LINE__); \
00277 }
00278 
00279 #define DBH2TRCTX(DBH) (&(DBH)->vd->trctx[(DBH)->vd->tr_cnt-1])
00280 
00281 #define NEED_LOCK(TRCTX) \
00282 ((TRCTX)->params.lockmode != DatabaseW)
00283 
00284 #define NEED_OBJLOCK(TRCTX) \
00285 ((TRCTX)->params.lockmode != ReadNWriteN && \
00286  (TRCTX)->params.lockmode != DatabaseW)
00287 
00288   static const char* msg_1 = __FILE__;
00289 #define TTT(S) (write( 1, S, strlen(S)))
00290   static int dummy_off_t_1 = (sizeof(off_t) != 8 ? (TTT(msg_1), *(char *)0 = 0) : 1);
00291 
00292 }
00293 
00294 #endif

Generated on Mon Dec 22 18:15:54 2008 for eyedb by  doxygen 1.5.3