Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

DirShortId.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta 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 // Vesta 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 Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 //
00020 // DirShortId.C
00021 // Last modified on Mon May 23 21:37:56 EDT 2005 by ken@xorian.net  
00022 //      modified on Thu Jul 19 15:36:18 PDT 2001 by mann  
00023 //      modified on Mon Apr 28 17:30:28 PDT 1997 by heydon
00024 
00025 // ShortIds for immutable directories.  Server side only.
00026 
00027 // The ids must be assigned *deterministically*, so that the same id
00028 // gets assigned when a log is replayed during recovery as was
00029 // assigned before.  Therefore the deletion of an id, making it
00030 // available for reuse, must be logged.  We do this in a cute way to
00031 // save memory.  The deletion phase of weeding deletes all the *still
00032 // in use* ids from the table, then logs those that remain, which are
00033 // the ones that are made available for reuse.  It then empties the
00034 // table and repopulates it.
00035 
00036 #include "DirShortId.H"
00037 #include "ShortIdKey.H"
00038 #include "logging.H"
00039 #include "VestaLog.H"
00040 #include "VRConcurrency.H"
00041 #include "VDirChangeable.H"
00042 
00043 #include <VestaConfig.H>
00044 #include <Thread.H>
00045 
00046 using std::fstream;
00047 
00048 // Types
00049 struct DirShortIdInfo {
00050     Bit32 sp;
00051 };
00052 typedef Table<ShortIdKey, DirShortIdInfo>::Default DirShortIdTable;
00053 typedef Table<ShortIdKey, DirShortIdInfo>::Iterator DirShortIdIter;
00054 
00055 // Module globals
00056 static Basics::mutex mu; // protects the following:
00057 static DirShortIdTable* dsidTable;
00058 // end mutex
00059 
00060 void
00061 InitDirShortId() throw()
00062 {
00063     dsidTable = NEW(DirShortIdTable);
00064 }
00065 
00066 // Allocate a new directory sid (i.e., one not currently registered),
00067 // and register it as corresponding to a directory at short address
00068 // sp.
00069 ShortId
00070 NewDirShortId(const FP::Tag& fptag, Bit32 sp) throw()
00071 {
00072     mu.lock();
00073     DirShortIdInfo info;
00074     ShortIdKey sidkey;
00075     Word hash = fptag.Hash();
00076     do {
00077         sidkey.sid =
00078           (ShortId) ((hash & ~ShortIdBlock::leafFlag) | ShortIdBlock::dirFlag);
00079         hash++;
00080     } while (dsidTable->Get(sidkey, info));
00081     info.sp = sp;
00082     dsidTable->Put(sidkey, info);
00083     mu.unlock();
00084     return sidkey.sid;
00085 }
00086 
00087 // Lookup a sid in the table.
00088 Bit32 GetDirShortId(ShortId sid) throw()
00089 {
00090     mu.lock();
00091     ShortIdKey sidkey(sid);
00092     DirShortIdInfo info;    
00093     if (!dsidTable->Get(sidkey, info)) {
00094         info.sp = 0;
00095     }
00096     mu.unlock();
00097     return info.sp;
00098 }
00099 
00100 // Register or reregister an existing directory sid as being at short
00101 // address sp.
00102 void
00103 SetDirShortId(ShortId sid, Bit32 sp) throw()
00104 {
00105     mu.lock();
00106     ShortIdKey sidkey(sid);
00107     DirShortIdInfo info;    
00108     info.sp = sp;
00109     (void) dsidTable->Put(sidkey, info);
00110     mu.unlock();
00111 }
00112 
00113 // Delete one ShortId registration.
00114 void
00115 DeleteDirShortId(ShortId sid) throw()
00116 {
00117     mu.lock();
00118     ShortIdKey sidkey(sid);
00119     DirShortIdInfo info;    
00120     (void) dsidTable->Delete(sidkey, info);
00121     mu.unlock();
00122 }
00123 
00124 // Log all the ShortIds that are currently in the table.
00125 // Assumes the correct locks for log access are held.
00126 void
00127 LogAllDirShortIds(char* tag)
00128 {
00129     mu.lock();
00130     DirShortIdIter iter(dsidTable);
00131     ShortIdKey sidkey;
00132     DirShortIdInfo info;
00133     VRLog.start();
00134     VRLog.put("(");
00135     VRLog.put(tag);
00136     while (iter.Next(sidkey, info)) {
00137         char sid[16];
00138         sprintf(sid, " 0x%08x", sidkey.sid);
00139         VRLog.put(sid);
00140     }
00141     VRLog.put(")\n");
00142     VRLog.commit();
00143     mu.unlock();
00144 }
00145 
00146 // Checkpoint the ShortIds that are currently in the table and not
00147 // checkpointed already.
00148 void
00149 CheckpointAllDirShortIds(Bit32& nextSP, fstream& ckpt)
00150 {
00151     mu.lock();
00152     DirShortIdIter iter(dsidTable);
00153     ShortIdKey sidkey;
00154     DirShortIdInfo info;
00155     while (iter.Next(sidkey, info)) {
00156         VDirChangeable vs(VestaSource::immutableDirectory,
00157                           (Bit8*) VMemPool::lengthenPointer(info.sp));
00158         (void) vs.checkpoint(nextSP, ckpt);
00159     }
00160     mu.unlock();
00161 }
00162 
00163 // Delete all ShortId registrations, in preparation for rebuilding
00164 // the registration table.
00165 void
00166 DeleteAllDirShortIds() throw()
00167 {
00168     mu.lock();
00169     dsidTable->Init(dsidTable->Size());
00170     mu.unlock();    
00171 }
00172 

Generated on Mon May 8 00:48:43 2006 for Vesta by  doxygen 1.4.2