yast2-storage

PeContainer.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) [2004-2009] Novell, Inc.
00003  *
00004  * All Rights Reserved.
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of version 2 of the GNU General Public License as published
00008  * by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013  * more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, contact Novell, Inc.
00017  *
00018  * To contact Novell about this file by physical or electronic mail, you may
00019  * find current contact information at www.novell.com.
00020  */
00021 
00022 
00023 #ifndef PE_CONTAINER_H
00024 #define PE_CONTAINER_H
00025 
00026 #include "storage/Container.h"
00027 #include "storage/Dm.h"
00028 
00029 #define UNKNOWN_PV_DEVICE "unknown"
00030 
00031 namespace storage
00032 {
00033 
00034 class PeContainer : public Container
00035     {
00036     friend class Storage;
00037 
00038     public:
00039 
00040         PeContainer(Storage* s, const string& name, const string& device, CType t);
00041         PeContainer(Storage* s, const string& name, const string& device, CType t,
00042                     SystemInfo& systeminfo);
00043         PeContainer(Storage* s, CType t, const xmlNode* node);
00044         PeContainer(const PeContainer& c);
00045         virtual ~PeContainer();
00046 
00047         void saveData(xmlNode* node) const;
00048 
00049         unsigned long long peSize() const { return pe_size; }
00050         unsigned long peCount() const { return num_pe; }
00051         unsigned long peFree() const { return free_pe; }
00052         unsigned numPv() const { return pv.size(); }
00053         bool hasUnkownPv() const;
00054         friend std::ostream& operator<< (std::ostream&, const PeContainer& );
00055         bool addedPv( const string& dev ) const;
00056         unsigned long sizeToLe( unsigned long long sizeK ) const;
00057 
00058         int setPeSize( long long unsigned, bool lvm1 );
00059         void calcSize() { size_k = pe_size * num_pe; }
00060         void unuseDev() const;
00061         void changeDeviceName( const string& old, const string& nw );
00062 
00063         bool equalContent( const PeContainer& rhs, bool comp_vol=true ) const;
00064 
00065         void logDifference(std::ostream& log, const PeContainer& rhs) const;
00066 
00067         string getDeviceByNumber( const string& majmin ) const;
00068         string getDeviceByNumber( unsigned long mj, unsigned long mi ) const;
00069         static bool splitMajMin( const string& majmin, unsigned long& mj, 
00070                                  unsigned long & mi );
00071         virtual list<string> getUsing() const;
00072 
00073     protected:
00074         struct Pv
00075             {
00076             Pv() : num_pe(0), free_pe(0) {}
00077             Pv(const xmlNode* node);
00078 
00079             void saveData(xmlNode* node) const;
00080 
00081             string device;
00082             string dmcryptDevice;
00083             string uuid;
00084             string status;
00085             unsigned long num_pe;
00086             unsigned long free_pe;
00087 
00088             string realDevice() const { return dmcryptDevice.empty() ? device : dmcryptDevice; }
00089 
00090             static bool comp_le( const Pv& a, const Pv& b )
00091                 { return( a.free_pe<b.free_pe ); }
00092             static bool no_free( const Pv& a )
00093                 { return( a.free_pe==0 ); }
00094 
00095             bool operator== ( const Pv& rhs ) const
00096                 { return( device==rhs.device ); }
00097             bool operator== ( const string& dev ) const
00098                 { return( device==dev); }
00099             bool equalContent(const Pv& rhs) const
00100             {
00101                 return device == rhs.device && dmcryptDevice == rhs.dmcryptDevice && uuid == rhs.uuid &&
00102                     status == rhs.status && num_pe == rhs.num_pe && free_pe == rhs.free_pe; 
00103             }
00104             };
00105 
00106         // iterators over Dm volumes
00107         typedef CastIterator<VIter, Dm *> DmInter;
00108         typedef CastIterator<CVIter, const Dm *> DmCInter;
00109         template< class Pred >
00110             struct DmPI { typedef ContainerIter<Pred, DmInter> type; };
00111         template< class Pred >
00112             struct DmCPI { typedef ContainerIter<Pred, DmCInter> type; };
00113         typedef CheckFnc<const Dm> CheckFncDm;
00114         typedef CheckerIterator< CheckFncDm, DmPI<CheckFncDm>::type,
00115                                  DmInter, Dm > DmPIterator;
00116         typedef CheckerIterator< CheckFncDm, DmCPI<CheckFncDm>::type,
00117                                  DmCInter, const Dm > DmCPIterator;
00118         typedef DerefIterator<DmPIterator,Dm> DmIter;
00119         typedef DerefIterator<DmCPIterator,const Dm> ConstDmIter;
00120         typedef IterPair<DmIter> DmPair;
00121         typedef IterPair<ConstDmIter> ConstDmPair;
00122 
00123         DmPair dmPair( bool (* Check)( const Dm& )=NULL)
00124             {
00125             return( DmPair( dmBegin( Check ), dmEnd( Check ) ));
00126             }
00127         DmIter dmBegin( bool (* Check)( const Dm& )=NULL)
00128             {
00129             IterPair<DmInter> p( (DmInter(begin())), (DmInter(end())) );
00130             return( DmIter( DmPIterator( p, Check )) );
00131             }
00132         DmIter dmEnd( bool (* Check)( const Dm& )=NULL)
00133             {
00134             IterPair<DmInter> p( (DmInter(begin())), (DmInter(end())) );
00135             return( DmIter( DmPIterator( p, Check, true )) );
00136             }
00137 
00138         ConstDmPair dmPair( bool (* Check)( const Dm& )=NULL) const
00139             {
00140             return( ConstDmPair( dmBegin( Check ), dmEnd( Check ) ));
00141             }
00142         ConstDmIter dmBegin( bool (* Check)( const Dm& )=NULL) const
00143             {
00144             IterPair<DmCInter> p( (DmCInter(begin())), (DmCInter(end())) );
00145             return( ConstDmIter( DmCPIterator( p, Check )) );
00146             }
00147         ConstDmIter dmEnd( bool (* Check)( const Dm& )=NULL) const
00148             {
00149             IterPair<DmCInter> p( (DmCInter(begin())), (DmCInter(end())) );
00150             return( ConstDmIter( DmCPIterator( p, Check, true )) );
00151             }
00152 
00153 
00154         friend std::ostream& operator<< (std::ostream&, const Pv& );
00155         friend void printDevList (std::ostream&, const std::list<Pv>& );
00156 
00157         string addList() const;
00158         virtual void print( std::ostream& s ) const { s << *this; }
00159         virtual Container* getCopy() const = 0; // { return( new PeContainer( *this ) ); }
00160         bool findPe(const string& dev, const list<Pv>& pl, list<Pv>::const_iterator& i) const;
00161         bool findPe(const string& dev, list<Pv>& pl, list<Pv>::iterator& i) const;
00162         unsigned long leByLvRemove() const;
00163         int tryUnusePe( const string& dev, std::list<Pv>& pl, std::list<Pv>& pladd,
00164                         std::list<Pv>& plrem, unsigned long& removed_pe );
00165         bool checkCreateConstraints();
00166         static int addLvPeDistribution( unsigned long le, unsigned stripe,
00167                                         std::list<Pv>& pl, std::list<Pv>& pladd, 
00168                                         std::map<string,unsigned long>& pe_map );
00169         int remLvPeDistribution( unsigned long le, std::map<string,unsigned long>& pe_map,
00170                                  std::list<Pv>& pl, std::list<Pv>& pladd );
00171         virtual bool checkConsistency() const;
00172 
00173         void addPv(const Pv& p);
00174 
00175         unsigned long long pe_size;
00176         unsigned long num_pe;
00177         unsigned long free_pe;
00178         std::list<Pv> pv;
00179         std::list<Pv> pv_add;
00180         std::list<Pv> pv_remove;
00181 
00182     private:
00183 
00184         PeContainer& operator=(const PeContainer&); // disallow
00185 
00186     };
00187 
00188 }
00189 
00190 #endif