File: open_chunk.h

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 (111 lines) | stat: -rw-r--r-- 2,891 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
/*
   Copyright 2016 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/>.
 */

#pragma once

#include "common/platform.h"

#include <unistd.h>
#include <array>

#include "chunkserver/chunk.h"
#include "chunkserver/hddspacemgr.h"

#include <unistd.h>

/*!
 * Class representing an open chunk with acquired resources.
 * In its destructor, it is assumed that hdd_chunk_find/hdd_chunk_trylock was called
 * and chunk is in locked state, so its resources can be safely freed.
 */
class OpenChunk {
public:
	OpenChunk() : chunk_(), fd_(-1), crc_() {
	}

	OpenChunk(Chunk *chunk) : chunk_(chunk), fd_(chunk ? chunk->fd : -1), crc_() {
		if (chunk && chunk->chunkFormat() == ChunkFormat::MOOSEFS) {
			crc_.reset(new MooseFSChunk::CrcDataContainer{{}});
		}
	}

	OpenChunk(OpenChunk &&other) noexcept
	    : chunk_(other.chunk_), fd_(other.fd_), crc_(std::move(other.crc_)) {
		other.chunk_ = nullptr;
		other.fd_ = -1;
	}

	/*!
	 * OpenChunk destructor.
	 * It is assumed that chunk_, if it exists, is properly locked.
	 */
	~OpenChunk() {
		if (chunk_) {
			if (chunk_->fd >= 0) {
				if (::close(chunk_->fd) < 0) {
					hdd_error_occured(chunk_);
					lzfs_silent_errlog(LOG_WARNING,"open_chunk: file:%s - close error",
							   chunk_->filename().c_str());
					hdd_report_damaged_chunk(chunk_->chunkid, chunk_->type());
				}
			}
			chunk_->fd = -1;
			hdd_chunk_release(chunk_);
		} else if (fd_ >= 0) {
			::close(fd_);
		}
	}

	OpenChunk &operator=(OpenChunk &&other) noexcept {
		chunk_ = other.chunk_;
		fd_ = other.fd_;
		crc_ = std::move(other.crc_);
		other.chunk_ = nullptr;
		other.fd_ = -1;
		return *this;
	}

	/*!
	 * Try to lock chunk in order to be able to remove it.
	 * \return true if chunk was successfully locked and can be removed, false otherwise.
	 */
	bool canRemove() {
		return hdd_chunk_trylock(chunk_);
	}

	/*!
	 * Remove a connection to chunk and prepare its resources to be freed.
	 * This function is called if chunk is to be inaccessible (deleted) in near future,
	 * but its resources (e.g. file descriptor) still need to be freed properly.
	 */
	void purge() {
		assert(chunk_);
		fd_ = chunk_->fd;
		chunk_ = nullptr;
	}

	uint8_t *crc_data() {
		assert(crc_);
		return crc_->data();
	}

private:
	Chunk *chunk_;
	int fd_;
	std::unique_ptr<MooseFSChunk::CrcDataContainer> crc_;
};