Connection.hxx 6.09 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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/TimeoutMonitor.hxx"
26 27 28 29
#include "event/DeferredMonitor.hxx"

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

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

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

46 47 48 49 50 51 52 53
		/**
		 * 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;

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

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

68 69 70 71 72 73
		void Stat(nfs_context *context, const char *path);
		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);
74

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

81 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.
		 */
		void PrepareDestroyContext();

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	private:
		static void Callback(int err, struct nfs_context *nfs,
				     void *data, void *private_data);
		void Callback(int err, void *data);
	};

	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;

104 105 106 107 108 109 110 111 112
	/**
	 * 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;

113
	std::exception_ptr postponed_mount_error;
114

115
#ifndef NDEBUG
116
	/**
117
	 * True when nfs_service() is being called.
118 119 120 121 122 123 124 125
	 */
	bool in_service;

	/**
	 * True when OnSocketReady() is being called.  During that,
	 * event updates are omitted.
	 */
	bool in_event;
126 127 128 129 130

	/**
	 * True when DestroyContext() is being called.
	 */
	bool in_destroy;
131
#endif
132 133 134 135 136 137 138

	bool mount_finished;

public:
	gcc_nonnull_all
	NfsConnection(EventLoop &_loop,
		      const char *_server, const char *_export_name)
139 140
		:SocketMonitor(_loop), TimeoutMonitor(_loop),
		 DeferredMonitor(_loop),
141 142 143
		 server(_server), export_name(_export_name),
		 context(nullptr) {}

144 145 146
	/**
	 * Must be run from EventLoop's thread.
	 */
147 148 149 150 151 152 153 154 155 156 157 158
	~NfsConnection();

	gcc_pure
	const char *GetServer() const {
		return server.c_str();
	}

	gcc_pure
	const char *GetExportName() const {
		return export_name.c_str();
	}

159 160 161 162
	EventLoop &GetEventLoop() {
		return SocketMonitor::GetEventLoop();
	}

163 164 165 166 167 168 169 170 171 172
	/**
	 * 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.
	 */
	void AddLease(NfsLease &lease);
	void RemoveLease(NfsLease &lease);

173
	void Stat(const char *path, NfsCallback &callback);
174

175
	void OpenDirectory(const char *path, NfsCallback &callback);
176 177 178
	const struct nfsdirent *ReadDirectory(struct nfsdir *dir);
	void CloseDirectory(struct nfsdir *dir);

179 180 181 182 183 184 185 186 187 188 189 190 191
	/**
	 * 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);

192 193 194
	void Cancel(NfsCallback &callback);

	void Close(struct nfsfh *fh);
195
	void CancelAndClose(struct nfsfh *fh, NfsCallback &callback);
196 197

protected:
198
	virtual void OnNfsConnectionError(std::exception_ptr &&e) = 0;
199 200 201

private:
	void DestroyContext();
202

203 204 205 206 207
	/**
	 * Wrapper for nfs_close_async().
	 */
	void InternalClose(struct nfsfh *fh);

208 209 210 211 212
	/**
	 * Invoke nfs_close_async() after nfs_service() returns.
	 */
	void DeferClose(struct nfsfh *fh);

213
	void MountInternal();
214
	void BroadcastMountSuccess();
215 216
	void BroadcastMountError(std::exception_ptr &&e);
	void BroadcastError(std::exception_ptr &&e);
217 218 219 220 221 222 223

	static void MountCallback(int status, nfs_context *nfs, void *data,
				  void *private_data);
	void MountCallback(int status, nfs_context *nfs, void *data);

	void ScheduleSocket();

224 225 226 227 228
	/**
	 * Wrapper for nfs_service().
	 */
	int Service(unsigned flags);

229 230 231
	/* virtual methods from SocketMonitor */
	virtual bool OnSocketReady(unsigned flags) override;

232 233 234
	/* virtual methods from TimeoutMonitor */
	void OnTimeout() final;

235 236 237 238 239
	/* virtual methods from DeferredMonitor */
	virtual void RunDeferred() override;
};

#endif