This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
NfsClient.h
287 lines (260 loc) · 7.5 KB
/
NfsClient.h
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#define FUSE_USE_VERSION 26
#define _FILE_OFFSET_BITS 64
#include <fuse/fuse_lowlevel.h>
#include <errno.h>
#include <atomic>
#include <ctime>
#include <mutex>
#include <random>
#include <set>
#include <thread>
#include <vector>
#include "ClientStats.h"
#include "InodeInternal.h"
#include "NfsConnectionPool.h"
#include "fuse_optype.h"
#include "logger.h"
class RpcContext;
class RpcContextParentName;
class RpcContextInode;
class RpcContextInodeFile;
class ReadRpcContext;
class WriteRpcContext;
class SetattrRpcContext;
class AccessRpcContext;
enum NfsClientPermissionMode {
InitialPerms, // always use initial permissions.
StrictPerms, // always use proper permissions.
SloppyPerms // use proper permissions when creating,
// use inital perms otherwise
};
/// @brief NfsClient implements fundamental FUSE operations on a pool of NFS
/// servers.
class NfsClient {
public:
NfsClient(
std::vector<std::string>& urls,
const char* hostscript,
const int scriptRefreshSeconds,
size_t targetConnections,
int nfsTimeoutMs,
std::shared_ptr<nfusr::Logger> logger,
std::shared_ptr<ClientStats> stats,
bool errorInjection,
NfsClientPermissionMode permMode);
virtual ~NfsClient();
bool start(std::shared_ptr<std::string> cacheRoot);
bool checkRpcCompletion(
RpcContext* info,
int rpc_status,
int nfs_status,
bool& retry);
bool checkRpcCall(RpcContext* info, int rpc_status);
NfsConnectionPool& getConnectionPool() {
return connPool_;
}
InodeInternal* inodeLookup(fuse_ino_t);
std::shared_ptr<nfusr::Logger> getLogger() const {
return logger_;
}
bool statsEnabled() const {
return stats_ != nullptr;
}
void recordOperationStats(
enum fuse_optype optype,
std::chrono::microseconds elapsed) {
if (stats_) {
stats_->recordOperation(optype, elapsed);
}
}
void recordOperationIssue() {
if (stats_) {
stats_->recordIssue();
}
}
void recordRpcFailure(bool isTimeout) {
if (stats_) {
stats_->recordRpcFailure(isTimeout);
}
}
bool shouldRetry(int rpc_status, RpcContext* ctx);
static void stat_from_fattr3(struct stat* st, const struct fattr3* attr);
virtual void replyEntry(
RpcContext* ctx,
const nfs_fh3* fh,
const struct fattr3* attr,
const struct fuse_file_info* file,
std::shared_ptr<std::string> local_cache_path,
const char* caller,
fuse_ino_t parent,
const char* name);
void getattrWithContext(RpcContextInodeFile* ctx);
void getattr(fuse_req_t req, fuse_ino_t inode, struct fuse_file_info* file);
void opendir(fuse_req_t req, fuse_ino_t inode, struct fuse_file_info* file);
void readdir(
fuse_req_t req,
fuse_ino_t inode,
size_t size,
off_t off,
struct fuse_file_info* file);
void
releasedir(fuse_req_t req, fuse_ino_t inode, struct fuse_file_info* file);
void lookupWithContext(RpcContextParentName* ctx);
void lookup(fuse_req_t req, fuse_ino_t parent, const char* name);
static void lookupCallback(
struct rpc_context*,
int rpc_status,
void* data,
void* private_data);
void openWithContext(RpcContextInodeFile* ctx);
void open(fuse_req_t req, fuse_ino_t inode, struct fuse_file_info* file);
void readWithContext(ReadRpcContext* ctx);
virtual void read(
fuse_req_t req,
fuse_ino_t inode,
size_t size,
off_t off,
struct fuse_file_info* file);
void writeWithContext(WriteRpcContext* ctx);
void write(
fuse_req_t req,
fuse_ino_t inode,
const char* buf,
size_t size,
off_t off,
struct fuse_file_info* file);
void static createCallback(
struct rpc_context*,
int rpc_status,
void* data,
void* private_data);
void create(
fuse_req_t req,
fuse_ino_t parent,
const char* name,
mode_t mode,
struct fuse_file_info* file);
void mknod(
fuse_req_t req,
fuse_ino_t parent,
const char* name,
mode_t mode,
dev_t rdev);
void forget(fuse_req_t req, fuse_ino_t ino, unsigned long nlookup);
void release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi);
void static mkdirCallback(
struct rpc_context*,
int rpc_status,
void* data,
void* private_data);
void mkdir(fuse_req_t req, fuse_ino_t parent, const char* name, mode_t mode);
void setattrWithContext(SetattrRpcContext* ctx);
void setattr(
fuse_req_t req,
fuse_ino_t ino,
struct stat* attr,
int valid,
struct fuse_file_info* fi);
void unlink(fuse_req_t req, fuse_ino_t parent, const char* name);
void rmdir(fuse_req_t req, fuse_ino_t parent, const char* name);
void static symlinkCallback(
struct rpc_context*,
int rpc_status,
void* data,
void* private_data);
void symlink(
fuse_req_t req,
const char* link,
fuse_ino_t parent,
const char* name);
void readlink(fuse_req_t req, fuse_ino_t inode);
void static linkCallback(
struct rpc_context*,
int rpc_status,
void* data,
void* private_data);
void link(
fuse_req_t req,
fuse_ino_t inode,
fuse_ino_t newparent,
const char* newname);
void rename(
fuse_req_t req,
fuse_ino_t parent,
const char* name,
fuse_ino_t newparent,
const char* newname);
void
fsync(fuse_req_t req, fuse_ino_t inode, int datasync, fuse_file_info* file);
void statfsWithContext(RpcContextInode* ctx);
void statfs(fuse_req_t req, fuse_ino_t inode);
void accessWithContext(AccessRpcContext* ctx);
void access(fuse_req_t req, fuse_ino_t inode, int mask);
uint64_t getMaxRead() {
return maxRead_;
}
std::shared_ptr<nfusr::Logger> getLogger() {
return logger_;
}
/// @brief Initialize error injection system.
///
/// @param min: minimum number of requests before injecting error.
/// @param max: max number of requests allowed before injecting error.
void initErrorInjection(uint32_t min = 50, uint32_t max = 100) {
assert(max > min);
eiMin_ = min;
eiMax_ = max;
eiCounter_ = 0;
eiRng_.seed(time(nullptr));
}
/// @brief Determine whether to inject error.
///
/// If error injection is enabled, randomly returns true
/// on somewhere between eiMin and eiMax calls.
bool errorInjection() {
if (eiMin_) {
if (++eiCounter_ >= eiMin_) {
auto range = eiMax_ - eiMin_;
if (eiRng_() % range <= eiCounter_ - eiMin_) {
eiCounter_ = 0;
return true;
}
}
}
return false;
}
static void setAttrTimeout(double to) {
attrTimeout_ = to;
}
static double getAttrTimeout() {
return attrTimeout_;
}
private:
void setUidGid(RpcContext const& ctx, bool creat);
void restoreUidGid(RpcContext const& ctx, bool creat);
NfsConnectionPool connPool_;
InodeInternal* rootInode_;
uint64_t maxRead_;
uint64_t maxWrite_;
std::shared_ptr<nfusr::Logger> logger_;
std::shared_ptr<ClientStats> stats_;
static double attrTimeout_;
// Error injection contol.
uint32_t eiMin_;
uint32_t eiMax_;
uint32_t eiCounter_;
std::mt19937 eiRng_;
// UID/GID fun
NfsClientPermissionMode permMode_;
uid_t initial_uid_;
gid_t initial_gid_;
};