xref: /haiku/src/add-ons/kernel/file_systems/packagefs/volume/PackageFSRoot.h (revision 9f81ca838ce7b92b5689e57d3f86765db4705a7b)
1*1e7416d9SIngo Weinhold /*
2*1e7416d9SIngo Weinhold  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3*1e7416d9SIngo Weinhold  * Distributed under the terms of the MIT License.
4*1e7416d9SIngo Weinhold  */
5*1e7416d9SIngo Weinhold #ifndef PACKAGE_FS_ROOT_H
6*1e7416d9SIngo Weinhold #define PACKAGE_FS_ROOT_H
7*1e7416d9SIngo Weinhold 
8*1e7416d9SIngo Weinhold 
9*1e7416d9SIngo Weinhold #include <Referenceable.h>
10*1e7416d9SIngo Weinhold 
11*1e7416d9SIngo Weinhold #include <util/AutoLock.h>
12*1e7416d9SIngo Weinhold #include <util/DoublyLinkedList.h>
13*1e7416d9SIngo Weinhold 
14*1e7416d9SIngo Weinhold #include <lock.h>
15*1e7416d9SIngo Weinhold 
16*1e7416d9SIngo Weinhold #include "DependencyFamily.h"
17*1e7416d9SIngo Weinhold #include "ResolvableFamily.h"
18*1e7416d9SIngo Weinhold #include "Volume.h"
19*1e7416d9SIngo Weinhold 
20*1e7416d9SIngo Weinhold 
21*1e7416d9SIngo Weinhold class PackageLinksDirectory;
22*1e7416d9SIngo Weinhold 
23*1e7416d9SIngo Weinhold 
24*1e7416d9SIngo Weinhold class PackageFSRoot : private BReferenceable,
25*1e7416d9SIngo Weinhold 	public DoublyLinkedListLinkImpl<PackageFSRoot> {
26*1e7416d9SIngo Weinhold public:
27*1e7416d9SIngo Weinhold 	// constructor and destructor are conceptually private
28*1e7416d9SIngo Weinhold 								PackageFSRoot(dev_t deviceID, ino_t nodeID);
29*1e7416d9SIngo Weinhold 	virtual						~PackageFSRoot();
30*1e7416d9SIngo Weinhold 
31*1e7416d9SIngo Weinhold 	static	status_t			GlobalInit();
32*1e7416d9SIngo Weinhold 	static	void				GlobalUninit();
33*1e7416d9SIngo Weinhold 
34*1e7416d9SIngo Weinhold 	inline	bool				ReadLock() const;
35*1e7416d9SIngo Weinhold 	inline	void				ReadUnlock() const;
36*1e7416d9SIngo Weinhold 	inline	bool				WriteLock();
37*1e7416d9SIngo Weinhold 	inline	void				WriteUnlock();
38*1e7416d9SIngo Weinhold 
39*1e7416d9SIngo Weinhold 			status_t			Init();
40*1e7416d9SIngo Weinhold 
41*1e7416d9SIngo Weinhold 	static	status_t			RegisterVolume(Volume* volume);
42*1e7416d9SIngo Weinhold 			void				UnregisterVolume(Volume* volume);
43*1e7416d9SIngo Weinhold 
44*1e7416d9SIngo Weinhold 			status_t			AddPackage(Package* package);
45*1e7416d9SIngo Weinhold 			void				RemovePackage(Package* package);
46*1e7416d9SIngo Weinhold 
DeviceID()47*1e7416d9SIngo Weinhold 			dev_t				DeviceID() const	{ return fDeviceID; }
NodeID()48*1e7416d9SIngo Weinhold 			ino_t				NodeID() const		{ return fNodeID; }
IsCustom()49*1e7416d9SIngo Weinhold 			bool				IsCustom() const	{ return fDeviceID < 0; }
50*1e7416d9SIngo Weinhold 
51*1e7416d9SIngo Weinhold 			Volume*				SystemVolume() const;
GetPackageLinksDirectory()52*1e7416d9SIngo Weinhold 			PackageLinksDirectory* GetPackageLinksDirectory() const
53*1e7416d9SIngo Weinhold 									{ return fPackageLinksDirectory; }
54*1e7416d9SIngo Weinhold 
55*1e7416d9SIngo Weinhold private:
56*1e7416d9SIngo Weinhold 			typedef DoublyLinkedList<PackageFSRoot> RootList;
57*1e7416d9SIngo Weinhold 			typedef DoublyLinkedList<Volume> VolumeList;
58*1e7416d9SIngo Weinhold 
59*1e7416d9SIngo Weinhold private:
60*1e7416d9SIngo Weinhold 			status_t			_AddVolume(Volume* volume);
61*1e7416d9SIngo Weinhold 			void				_RemoveVolume(Volume* volume);
62*1e7416d9SIngo Weinhold 
63*1e7416d9SIngo Weinhold 			status_t			_AddPackage(Package* package);
64*1e7416d9SIngo Weinhold 			void				_RemovePackage(Package* package);
65*1e7416d9SIngo Weinhold 
66*1e7416d9SIngo Weinhold 			void				_ResolveDependencies(
67*1e7416d9SIngo Weinhold 									ResolvableDependencyList& dependencies);
68*1e7416d9SIngo Weinhold 			void				_ResolveDependency(Dependency* dependency);
69*1e7416d9SIngo Weinhold 
70*1e7416d9SIngo Weinhold 	static	status_t			_GetOrCreateRoot(dev_t deviceID, ino_t nodeID,
71*1e7416d9SIngo Weinhold 									PackageFSRoot*& _root);
72*1e7416d9SIngo Weinhold 	static	PackageFSRoot*		_FindRootLocked(dev_t deviceID, ino_t nodeID);
73*1e7416d9SIngo Weinhold 	static	void				_PutRoot(PackageFSRoot* root);
74*1e7416d9SIngo Weinhold 
75*1e7416d9SIngo Weinhold private:
76*1e7416d9SIngo Weinhold 	static	mutex				sRootListLock;
77*1e7416d9SIngo Weinhold 	static	RootList			sRootList;
78*1e7416d9SIngo Weinhold 
79*1e7416d9SIngo Weinhold 	mutable	rw_lock				fLock;
80*1e7416d9SIngo Weinhold 			dev_t				fDeviceID;
81*1e7416d9SIngo Weinhold 			ino_t				fNodeID;
82*1e7416d9SIngo Weinhold 			VolumeList			fVolumes;
83*1e7416d9SIngo Weinhold 			Volume*				fSystemVolume;
84*1e7416d9SIngo Weinhold 			PackageLinksDirectory* fPackageLinksDirectory;
85*1e7416d9SIngo Weinhold 			ResolvableFamilyHashTable fResolvables;
86*1e7416d9SIngo Weinhold 			DependencyFamilyHashTable fDependencies;
87*1e7416d9SIngo Weinhold };
88*1e7416d9SIngo Weinhold 
89*1e7416d9SIngo Weinhold 
90*1e7416d9SIngo Weinhold bool
ReadLock()91*1e7416d9SIngo Weinhold PackageFSRoot::ReadLock() const
92*1e7416d9SIngo Weinhold {
93*1e7416d9SIngo Weinhold 	return rw_lock_read_lock(&fLock) == B_OK;
94*1e7416d9SIngo Weinhold }
95*1e7416d9SIngo Weinhold 
96*1e7416d9SIngo Weinhold 
97*1e7416d9SIngo Weinhold void
ReadUnlock()98*1e7416d9SIngo Weinhold PackageFSRoot::ReadUnlock() const
99*1e7416d9SIngo Weinhold {
100*1e7416d9SIngo Weinhold 	rw_lock_read_unlock(&fLock);
101*1e7416d9SIngo Weinhold }
102*1e7416d9SIngo Weinhold 
103*1e7416d9SIngo Weinhold 
104*1e7416d9SIngo Weinhold bool
WriteLock()105*1e7416d9SIngo Weinhold PackageFSRoot::WriteLock()
106*1e7416d9SIngo Weinhold {
107*1e7416d9SIngo Weinhold 	return rw_lock_write_lock(&fLock) == B_OK;
108*1e7416d9SIngo Weinhold }
109*1e7416d9SIngo Weinhold 
110*1e7416d9SIngo Weinhold 
111*1e7416d9SIngo Weinhold void
WriteUnlock()112*1e7416d9SIngo Weinhold PackageFSRoot::WriteUnlock()
113*1e7416d9SIngo Weinhold {
114*1e7416d9SIngo Weinhold 	rw_lock_write_unlock(&fLock);
115*1e7416d9SIngo Weinhold }
116*1e7416d9SIngo Weinhold 
117*1e7416d9SIngo Weinhold 
118*1e7416d9SIngo Weinhold typedef AutoLocker<const PackageFSRoot,
119*1e7416d9SIngo Weinhold 	AutoLockerReadLocking<const PackageFSRoot> > PackageFSRootReadLocker;
120*1e7416d9SIngo Weinhold typedef AutoLocker<PackageFSRoot, AutoLockerWriteLocking<PackageFSRoot> >
121*1e7416d9SIngo Weinhold 	PackageFSRootWriteLocker;
122*1e7416d9SIngo Weinhold 
123*1e7416d9SIngo Weinhold 
124*1e7416d9SIngo Weinhold #endif	// PACKAGE_FS_ROOT_H
125