xref: /haiku/src/add-ons/kernel/file_systems/ufs2/Volume.cpp (revision 04d1d2da0b27294f0f1e623071df310a0820d4b6)
1 /*
2  * Copyright 2020 Suhel Mehta, mehtasuhel@gmail.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "Volume.h"
6 
7 //#define TRACE_UFS2
8 #ifdef TRACE_UFS2
9 #	define TRACE(x...) dprintf("\33[34mufs2:\33[0m " x)
10 #else
11 #	define TRACE(x...) ;
12 #endif
13 #   define ERROR(x...) dprintf("\33[34mexfat:\33[0m " x)
14 
15 class DeviceOpner {
16 	public:
17 								DeviceOpner(int fd, int mode);
18 								DeviceOpner(const char* device, int mode);
19 								~DeviceOpner();
20 
21 				int				Open(const char* device, int mode);
22 				int				Open(int fd, int mode);
23 				void*			InitCache(off_t numBlocks, uint32 blockSize);
24 				void			RemoveCache(bool allowWrites);
25 
26 				void			Keep();
27 
28 				int				Device() const { return fDevice; }
29 				int				Mode() const { return fMode; }
30 				bool			IsReadOnly() const { return _IsReadOnly(fMode); }
31 
32 				status_t		GetSize(off_t* _size, uint32* _blockSize = NULL);
33 
34 	private:
35 			static	bool		_IsReadOnly(int mode)
36 									{ return (mode & O_RWMASK) == O_RDONLY; }
37 			static	bool		_IsReadWrite(int mode)
38 									{ return (mode & O_RWMASK) == O_RDWR; }
39 
40 					int			fDevice;
41 					int			fMode;
42 					void*		fBlockCache;
43 };
44 
45 bool
46 ufs2_super_block::IsValid()
47 {
48 	if (ufs2_magic != UFS2_SUPER_BLOCK_MAGIC)
49 		return false;
50 
51 	return true;
52 }
53 
54 status_t
55 Volume::Identify(int fd, ufs2_super_block *superBlock)
56 {
57 	if (read_pos(fd, UFS2_SUPER_BLOCK_OFFSET, superBlock,
58 		sizeof(ufs2_super_block)) != sizeof(ufs2_super_block))
59 		return B_IO_ERROR;
60 
61 	if (!superBlock->IsValid()) {
62 		ERROR("invalid superblock! Identify failed!!\n");
63 		return B_BAD_VALUE;
64 	}
65 
66 	return B_OK;
67 }
68