Connection.hxx 6.5 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * http://www.musicpd.org
 *
 * This program 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_NFS_CONNECTION_HXX
#define MPD_NFS_CONNECTION_HXX

#include "Cancellable.hxx"
#include "event/SocketMonitor.hxx"
25
#include "event/TimerEvent.hxx"
26
#include "event/DeferEvent.hxx"
27
#include "util/Compiler.h"
28 29 30

#include <string>
#include <list>
31
#include <forward_list>
32
#include <exception>
33 34

struct nfs_context;
35 36
struct nfsdir;
struct nfsdirent;
37
class NfsCallback;
38
class NfsLease;
39 40 41 42

/**
 * An asynchronous connection to a NFS server.
 */
43
class NfsConnection : SocketMonitor {
44 45 46
	class CancellableCallback : public CancellablePointer<NfsCallback> {
		NfsConnection &connection;

47 48 49 50 51 52 53 54
		/**
		 * Is this a nfs_open_async() operation?  If yes, then
		 * we need to call nfs_close_async() on the new file
		 * handle as soon as the callback is invoked
		 * successfully.
		 */
		const bool open;

55 56 57 58 59 60
		/**
		 * The file handle scheduled to be closed as soon as
		 * the operation finishes.
		 */
		struct nfsfh *close_fh;

61
	public:
62
		explicit CancellableCallback(NfsCallback &_callback,
63
					     NfsConnection &_connection,
64
					     bool _open) noexcept
65
			:CancellablePointer<NfsCallback>(_callback),
66
			 connection(_connection),
67
			 open(_open), close_fh(nullptr) {}
68

69
		void Stat(nfs_context *context, const char *path);
70
		void Lstat(nfs_context *context, const char *path);
71 72 73 74 75
		void OpenDirectory(nfs_context *context, const char *path);
		void Open(nfs_context *context, const char *path, int flags);
		void Stat(nfs_context *context, struct nfsfh *fh);
		void Read(nfs_context *context, struct nfsfh *fh,
			  uint64_t offset, size_t size);
76

77 78 79 80
		/**
		 * Cancel the operation and schedule a call to
		 * nfs_close_async() with the given file handle.
		 */
81
		void CancelAndScheduleClose(struct nfsfh *fh) noexcept;
82

83 84 85 86 87
		/**
		 * Called by NfsConnection::DestroyContext() right
		 * before nfs_destroy_context().  This object is given
		 * a chance to prepare for the latter.
		 */
88
		void PrepareDestroyContext() noexcept;
89

90 91
	private:
		static void Callback(int err, struct nfs_context *nfs,
92 93
				     void *data, void *private_data) noexcept;
		void Callback(int err, void *data) noexcept;
94 95
	};

96
	DeferEvent defer_new_lease;
97 98
	TimerEvent mount_timeout_event;

99 100 101 102 103 104 105 106 107 108
	std::string server, export_name;

	nfs_context *context;

	typedef std::list<NfsLease *> LeaseList;
	LeaseList new_leases, active_leases;

	typedef CancellableList<NfsCallback, CancellableCallback> CallbackList;
	CallbackList callbacks;

109 110 111 112 113 114 115 116 117
	/**
	 * A list of NFS file handles (struct nfsfh *) which shall be
	 * closed as soon as nfs_service() returns.  If we close the
	 * file handle while in nfs_service(), libnfs may crash, and
	 * deferring this call to after nfs_service() avoids this
	 * problem.
	 */
	std::forward_list<struct nfsfh *> deferred_close;

118
	std::exception_ptr postponed_mount_error;
119

120
#ifndef NDEBUG
121
	/**
122
	 * True when nfs_service() is being called.
123 124 125 126 127 128 129 130
	 */
	bool in_service;

	/**
	 * True when OnSocketReady() is being called.  During that,
	 * event updates are omitted.
	 */
	bool in_event;
131 132 133 134 135

	/**
	 * True when DestroyContext() is being called.
	 */
	bool in_destroy;
136
#endif
137 138 139 140 141 142

	bool mount_finished;

public:
	gcc_nonnull_all
	NfsConnection(EventLoop &_loop,
143
		      const char *_server, const char *_export_name) noexcept
144
		:SocketMonitor(_loop),
145
		 defer_new_lease(_loop, BIND_THIS_METHOD(RunDeferred)),
146
		 mount_timeout_event(_loop, BIND_THIS_METHOD(OnMountTimeout)),
147 148 149
		 server(_server), export_name(_export_name),
		 context(nullptr) {}

150 151 152
	/**
	 * Must be run from EventLoop's thread.
	 */
153
	~NfsConnection() noexcept;
154 155

	gcc_pure
156
	const char *GetServer() const noexcept {
157 158 159 160
		return server.c_str();
	}

	gcc_pure
161
	const char *GetExportName() const noexcept {
162 163 164
		return export_name.c_str();
	}

165
	using SocketMonitor::GetEventLoop;
166

167 168 169 170 171 172 173
	/**
	 * Ensure that the connection is established.  The connection
	 * is kept up while at least one #NfsLease is registered.
	 *
	 * This method is thread-safe.  However, #NfsLease's methods
	 * will be invoked from within the #EventLoop's thread.
	 */
174 175
	void AddLease(NfsLease &lease) noexcept;
	void RemoveLease(NfsLease &lease) noexcept;
176

177
	void Stat(const char *path, NfsCallback &callback);
178
	void Lstat(const char *path, NfsCallback &callback);
179

180
	void OpenDirectory(const char *path, NfsCallback &callback);
181 182
	const struct nfsdirent *ReadDirectory(struct nfsdir *dir) noexcept;
	void CloseDirectory(struct nfsdir *dir) noexcept;
183

184 185 186 187 188 189 190 191 192 193 194 195 196
	/**
	 * Throws std::runtime_error on error.
	 */
	void Open(const char *path, int flags, NfsCallback &callback);

	void Stat(struct nfsfh *fh, NfsCallback &callback);

	/**
	 * Throws std::runtime_error on error.
	 */
	void Read(struct nfsfh *fh, uint64_t offset, size_t size,
		  NfsCallback &callback);

197
	void Cancel(NfsCallback &callback) noexcept;
198

199 200
	void Close(struct nfsfh *fh) noexcept;
	void CancelAndClose(struct nfsfh *fh, NfsCallback &callback) noexcept;
201 202

protected:
203
	virtual void OnNfsConnectionError(std::exception_ptr &&e) noexcept = 0;
204 205

private:
206
	void DestroyContext() noexcept;
207

208 209 210
	/**
	 * Wrapper for nfs_close_async().
	 */
211
	void InternalClose(struct nfsfh *fh) noexcept;
212

213 214 215
	/**
	 * Invoke nfs_close_async() after nfs_service() returns.
	 */
216
	void DeferClose(struct nfsfh *fh) noexcept;
217

218
	void MountInternal();
219 220 221
	void BroadcastMountSuccess() noexcept;
	void BroadcastMountError(std::exception_ptr &&e) noexcept;
	void BroadcastError(std::exception_ptr &&e) noexcept;
222 223

	static void MountCallback(int status, nfs_context *nfs, void *data,
224 225
				  void *private_data) noexcept;
	void MountCallback(int status, nfs_context *nfs, void *data) noexcept;
226

227
	void ScheduleSocket() noexcept;
228

229 230 231
	/**
	 * Wrapper for nfs_service().
	 */
232
	int Service(unsigned flags) noexcept;
233

234
	/* virtual methods from SocketMonitor */
235
	bool OnSocketReady(unsigned flags) noexcept override;
236

237
	/* callback for #mount_timeout_event */
238
	void OnMountTimeout() noexcept;
239

240 241
	/* DeferEvent callback */
	void RunDeferred() noexcept;
242 243 244
};

#endif