xref: /haiku/src/bin/bfs_tools/lib/Disk.h (revision 8f087d87f9d334aaf43de5fec4a6253918332eae)
1647cff2eSAxel Dörfler #ifndef DISK_H
2647cff2eSAxel Dörfler #define DISK_H
3647cff2eSAxel Dörfler /* Disk - handles BFS superblock, disk access etc.
4647cff2eSAxel Dörfler **
5647cff2eSAxel Dörfler ** Copyright (c) 2001-2003 pinc Software. All Rights Reserved.
6*8f087d87SAlexander von Gluck IV ** Released under the terms of the MIT license.
7647cff2eSAxel Dörfler */
8647cff2eSAxel Dörfler 
9647cff2eSAxel Dörfler 
10fd919c2cSAxel Dörfler #include <BufferIO.h>
11647cff2eSAxel Dörfler #include <File.h>
12647cff2eSAxel Dörfler #include <Path.h>
13647cff2eSAxel Dörfler 
14a59d56faSFrançois Revol #include <string.h>
15a59d56faSFrançois Revol 
16647cff2eSAxel Dörfler #include "Bitmap.h"
17647cff2eSAxel Dörfler #include "bfs.h"
18647cff2eSAxel Dörfler #include "Cache.h"
19647cff2eSAxel Dörfler 
20647cff2eSAxel Dörfler 
21647cff2eSAxel Dörfler class Disk;
22647cff2eSAxel Dörfler 
23647cff2eSAxel Dörfler class BlockRunCache : public Cache<block_run>
24647cff2eSAxel Dörfler {
25647cff2eSAxel Dörfler 	public:
26647cff2eSAxel Dörfler 		BlockRunCache(Disk *disk);
27647cff2eSAxel Dörfler //		~BlockRunCache();
28647cff2eSAxel Dörfler 
29647cff2eSAxel Dörfler 		virtual Cacheable *NewCacheable(block_run run);
30647cff2eSAxel Dörfler 
31647cff2eSAxel Dörfler 	protected:
32647cff2eSAxel Dörfler 		Disk	*fDisk;
33647cff2eSAxel Dörfler };
34647cff2eSAxel Dörfler 
35647cff2eSAxel Dörfler 
36647cff2eSAxel Dörfler class Disk : public BPositionIO
37647cff2eSAxel Dörfler {
38647cff2eSAxel Dörfler 	public:
39647cff2eSAxel Dörfler 		Disk(const char *device, bool rawMode = false, off_t start = 0, off_t stop = -1);
40647cff2eSAxel Dörfler 		virtual ~Disk();
41647cff2eSAxel Dörfler 
42647cff2eSAxel Dörfler 		status_t			InitCheck();
Path()43647cff2eSAxel Dörfler 		const BPath			&Path() const { return fPath; }
44647cff2eSAxel Dörfler 
Size()45647cff2eSAxel Dörfler 		off_t				Size() const { return fSize; }
NumBlocks()46647cff2eSAxel Dörfler 		off_t				NumBlocks() const { return fSuperBlock.num_blocks; }
BlockSize()47647cff2eSAxel Dörfler 		uint32				BlockSize() const { return fSuperBlock.block_size; }
BlockShift()48647cff2eSAxel Dörfler 		uint32				BlockShift() const { return fSuperBlock.block_shift; }
AllocationGroups()49647cff2eSAxel Dörfler 		uint32				AllocationGroups() const { return fSuperBlock.num_ags; }
AllocationGroupShift()50647cff2eSAxel Dörfler 		uint32				AllocationGroupShift() const { return fSuperBlock.ag_shift; }
BitmapSize()51647cff2eSAxel Dörfler 		uint32				BitmapSize() const { return fBitmap.Size(); }
52647cff2eSAxel Dörfler 		off_t				LogSize() const;
53647cff2eSAxel Dörfler 
SuperBlock()54647cff2eSAxel Dörfler 		disk_super_block	*SuperBlock() { return &fSuperBlock; }
Root()55647cff2eSAxel Dörfler 		block_run			Root() const { return fSuperBlock.root_dir; }
Indices()56647cff2eSAxel Dörfler 		block_run			Indices() const { return fSuperBlock.indices; }
Log()57647cff2eSAxel Dörfler 		block_run			Log() const { return fSuperBlock.log_blocks; }
BlockBitmap()58647cff2eSAxel Dörfler 		Bitmap				*BlockBitmap() { return &fBitmap; }
59647cff2eSAxel Dörfler 
Name()60647cff2eSAxel Dörfler 		const char			*Name() const { return fSuperBlock.name; }
SetName(const char * name)61647cff2eSAxel Dörfler 		void				SetName(const char *name) { strcpy(fSuperBlock.name,name); }
62647cff2eSAxel Dörfler 
ToOffset(block_run run)63647cff2eSAxel Dörfler 		off_t				ToOffset(block_run run) const { return ToBlock(run) << fSuperBlock.block_shift; }
ToBlock(block_run run)64647cff2eSAxel Dörfler 		off_t				ToBlock(block_run run) const { return ((((off_t)run.allocation_group) << fSuperBlock.ag_shift) | (off_t)run.start); }
65647cff2eSAxel Dörfler 		block_run			ToBlockRun(off_t start,int16 length = 1) const;
66647cff2eSAxel Dörfler 
67647cff2eSAxel Dörfler 		uint8				*ReadBlockRun(block_run run);
68647cff2eSAxel Dörfler 
69647cff2eSAxel Dörfler 		status_t			ScanForSuperBlock(off_t start = 0,off_t stop = -1);
70647cff2eSAxel Dörfler 		status_t			ValidateSuperBlock();
71647cff2eSAxel Dörfler 		status_t			RecreateSuperBlock();
72647cff2eSAxel Dörfler 
73647cff2eSAxel Dörfler 		status_t			DumpBootBlockToFile();
74647cff2eSAxel Dörfler 
75647cff2eSAxel Dörfler 		// BPositionIO methods
76647cff2eSAxel Dörfler 		virtual ssize_t		Read(void *buffer, size_t size);
77647cff2eSAxel Dörfler 		virtual ssize_t		Write(const void *buffer, size_t size);
78647cff2eSAxel Dörfler 
79647cff2eSAxel Dörfler 		virtual ssize_t		ReadAt(off_t pos, void *buffer, size_t size);
80647cff2eSAxel Dörfler 		virtual ssize_t		WriteAt(off_t pos, const void *buffer, size_t size);
81647cff2eSAxel Dörfler 
82647cff2eSAxel Dörfler 		virtual off_t		Seek(off_t position, uint32 seek_mode);
83647cff2eSAxel Dörfler 		virtual off_t		Position() const;
84647cff2eSAxel Dörfler 
85647cff2eSAxel Dörfler 		virtual status_t	SetSize(off_t size);
86647cff2eSAxel Dörfler 
87647cff2eSAxel Dörfler 	protected:
88647cff2eSAxel Dörfler 		status_t			GetNextSpecialInode(char *,off_t *,off_t,bool);
89647cff2eSAxel Dörfler 		void				SaveInode(bfs_inode *,bool *,bfs_inode *,bool *,bfs_inode *);
90647cff2eSAxel Dörfler 		status_t			ScanForIndexAndRoot(bfs_inode *,bfs_inode *);
91647cff2eSAxel Dörfler 		status_t			DetermineBlockSize();
92647cff2eSAxel Dörfler 		status_t			ValidateSuperBlock(disk_super_block &superBlock);
93647cff2eSAxel Dörfler 
94647cff2eSAxel Dörfler 		status_t			LoadBootBlock();
95647cff2eSAxel Dörfler 
96fd919c2cSAxel Dörfler 	protected:
97647cff2eSAxel Dörfler 		BFile				fFile;
98fd919c2cSAxel Dörfler 		BBufferIO*			fBufferedFile;
99647cff2eSAxel Dörfler 		BPath				fPath;
100647cff2eSAxel Dörfler 		off_t				fRawDiskOffset;
101647cff2eSAxel Dörfler 		off_t				fSize;
102647cff2eSAxel Dörfler 		disk_super_block	fSuperBlock;
103647cff2eSAxel Dörfler 		Bitmap				fBitmap;
104647cff2eSAxel Dörfler 
105647cff2eSAxel Dörfler 		block_run			fValidBlockRun;
106647cff2eSAxel Dörfler 		off_t				fValidOffset;
107647cff2eSAxel Dörfler 		off_t				fLogStart;
108647cff2eSAxel Dörfler 
109647cff2eSAxel Dörfler 		BlockRunCache		fCache;
110647cff2eSAxel Dörfler 
111647cff2eSAxel Dörfler 		bool				fRawMode;
112647cff2eSAxel Dörfler };
113647cff2eSAxel Dörfler 
114647cff2eSAxel Dörfler #endif	/* DISK_H */
115