File: chunk.cc

package info (click to toggle)
lizardfs 3.12.0+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 8,064 kB
  • sloc: cpp: 91,899; sh: 9,341; python: 3,878; ansic: 3,109; pascal: 128; makefile: 57
file content (211 lines) | stat: -rw-r--r-- 6,504 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
   Copyright 2013-2015 Skytechnology sp. z o.o.

   This file is part of LizardFS.

   LizardFS is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, version 3.

   LizardFS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LizardFS. If not, see <http://www.gnu.org/licenses/>.
 */

#include "common/platform.h"
#include "chunkserver/chunk.h"

#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <sstream>

#include "common/massert.h"
#include "common/slice_traits.h"

Chunk::Chunk(uint64_t chunkId, ChunkPartType type, ChunkState state)
	: testnext(NULL),
	  testprev(NULL),
	  next(NULL),
	  ccond(NULL),
	  owner(NULL),
	  chunkid(chunkId),
	  version(0),
	  fd(-1),
	  blocks(0),
	  refcount(0),
	  blockExpectedToBeReadNext(0),
	  type_(type),
	  filename_layout_(-1),
	  validattr(0),
	  todel(0),
	  state(state),
	  wasChanged(0) {
}

std::string Chunk::generateFilenameForVersion(uint32_t version, int layout_version) const {
	std::stringstream ss;
	char buffer[30];
	ss << owner->path << Chunk::getSubfolderNameGivenChunkId(chunkid, layout_version) << "/chunk_";
	if (slice_traits::isXor(type_)) {
		if (slice_traits::xors::isXorParity(type_)) {
			ss << "xor_parity_of_";
		} else {
			ss << "xor_" << (unsigned)slice_traits::xors::getXorPart(type_) << "_of_";
		}
		ss << (unsigned)slice_traits::xors::getXorLevel(type_) << "_";
	}
	if (slice_traits::isEC(type_)) {
		ss << "ec_" << (type_.getSlicePart() + 1) << "_of_"
		   << slice_traits::ec::getNumberOfDataParts(type_) << "_"
		   << slice_traits::ec::getNumberOfParityParts(type_) << "_";
	}
	sprintf(buffer, "%016" PRIX64 "_%08" PRIX32 ".mfs", chunkid, version);
	if (chunkFormat() == ChunkFormat::INTERLEAVED) {
		memcpy(buffer + 26, "liz", 3);
	}
	ss << buffer;
	return ss.str();
}

uint32_t Chunk::maxBlocksInFile() const {
	int data_part_count = slice_traits::getNumberOfDataParts(type_);
	return (MFSBLOCKSINCHUNK + data_part_count - 1) / data_part_count;
}

int Chunk::renameChunkFile(uint32_t new_version, int new_layout_version) {
	std::string old_file_name = filename();
	std::string new_file_name = generateFilenameForVersion(new_version, new_layout_version);

	int status = rename(old_file_name.c_str(), new_file_name.c_str());
	if (status < 0) {
		return status;
	}

	filename_layout_ = new_layout_version;
	version = new_version;

	return 0;
}

void Chunk::setBlockCountFromFizeSize(off_t fileSize) {
	sassert(isFileSizeValid(fileSize));
	blocks = fileSize / kHddBlockSize;
}

uint32_t Chunk::getSubfolderNumber(uint64_t chunkId, int layout_version) {
	// layout version 0 corresponds to current directory/chunk naming convention
	// values greater than 0 describe older versions (order is not important)
	return (layout_version == kCurrentDirectoryLayout ? chunkId >> 16 : chunkId) & 0xFF;
}

std::string Chunk::getSubfolderNameGivenNumber(uint32_t subfolderNumber, int layout_version) {
	sassert(subfolderNumber < Chunk::kNumberOfSubfolders);
	char buffer[16];
	// layout version 0 corresponds to current directory/chunk naming convention
	// values greater than 0 describe older versions (order is not important)
	if (layout_version == kCurrentDirectoryLayout) {
		sprintf(buffer, "chunks%02X", unsigned(subfolderNumber));
	} else {
		sprintf(buffer, "%02X", unsigned(subfolderNumber));
	}
	return std::string(buffer);
}

std::string Chunk::getSubfolderNameGivenChunkId(uint64_t chunkId, int layout_version) {
	return Chunk::getSubfolderNameGivenNumber(Chunk::getSubfolderNumber(chunkId, layout_version), layout_version);
}

MooseFSChunk::MooseFSChunk(uint64_t chunkId, ChunkPartType type, ChunkState state) :
		Chunk(chunkId, type, state) {
}

off_t MooseFSChunk::getBlockOffset(uint16_t blockNumber) const {
	return getHeaderSize() + MFSBLOCKSIZE * blockNumber;
}

off_t MooseFSChunk::getFileSizeFromBlockCount(uint32_t blockCount) const {
	return getHeaderSize() + blockCount * MFSBLOCKSIZE;
}

void MooseFSChunk::setBlockCountFromFizeSize(off_t fileSize) {
	sassert(isFileSizeValid(fileSize));
	fileSize -= getHeaderSize();
	blocks = fileSize / MFSBLOCKSIZE;
}

bool MooseFSChunk::isFileSizeValid(off_t fileSize) const {
	if (fileSize < static_cast<off_t>(getHeaderSize())) {
		return false;
	}
	fileSize -= getHeaderSize();
	if (fileSize % MFSBLOCKSIZE != 0) {
		return false;
	}
	if (fileSize / MFSBLOCKSIZE > maxBlocksInFile()) {
		return false;
	}
	return true;
}

off_t MooseFSChunk::getSignatureOffset() const {
	return 0;
}

void MooseFSChunk::readaheadHeader() const {
#ifdef LIZARDFS_HAVE_POSIX_FADVISE
	posix_fadvise(fd, 0, getHeaderSize(), POSIX_FADV_WILLNEED);
#elif defined(__APPLE__)
	struct radvisory ra;
	ra.ra_offset = 0;
	ra.ra_count = getHeaderSize();
	fcntl(fd, F_RDADVISE, &ra);
#endif
}

size_t MooseFSChunk::getHeaderSize() const {
	if (slice_traits::isStandard(type_)) {
		return kMaxSignatureBlockSize + serializedSize(uint32_t()) * maxBlocksInFile();
	} else {
		assert(slice_traits::isXor(type_) || slice_traits::isEC(type_));

		uint32_t requiredHeaderSize = kMaxSignatureBlockSize + serializedSize(uint32_t()) * maxBlocksInFile();
		// header size is equal to the requiredHeaderSize rounded up to typical disk block size
		uint32_t diskBlockSize = kDiskBlockSize;
		off_t dataOffset = (requiredHeaderSize + diskBlockSize - 1) / diskBlockSize * diskBlockSize;
		return dataOffset;
	}
}

off_t MooseFSChunk::getCrcOffset() const {
	return kMaxSignatureBlockSize;
}

size_t MooseFSChunk::getCrcBlockSize() const {
	return serializedSize(uint32_t()) * maxBlocksInFile();
}

InterleavedChunk::InterleavedChunk(uint64_t chunkId, ChunkPartType type, ChunkState state) :
		Chunk(chunkId, type, state) {
}

off_t InterleavedChunk::getBlockOffset(uint16_t blockNumber) const {
	return static_cast<uint32_t>(blockNumber) * kHddBlockSize;
}

off_t InterleavedChunk::getFileSizeFromBlockCount(uint32_t blockCount) const {
	return blockCount * kHddBlockSize;
}

void InterleavedChunk::setBlockCountFromFizeSize(off_t fileSize) {
	sassert(isFileSizeValid(fileSize));
	blocks = fileSize / kHddBlockSize;
}

bool InterleavedChunk::isFileSizeValid(off_t fileSize) const {
	return fileSize % kHddBlockSize == 0;
}