File: nfs-ganesha-nfsrefer.patch

package info (click to toggle)
nfs-ganesha 2.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,044 kB
  • sloc: ansic: 144,377; cpp: 6,985; perl: 4,985; python: 2,766; sh: 2,604; lex: 606; yacc: 411; makefile: 67; awk: 22; lisp: 20
file content (495 lines) | stat: -rw-r--r-- 14,373 bytes parent folder | download | duplicates (2)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
--- a/src/FSAL/FSAL_VFS/handle.c
+++ b/src/FSAL/FSAL_VFS/handle.c
@@ -47,6 +47,9 @@
 #include "vfs_methods.h"
 #include <os/subr.h>
 #include "subfsal.h"
+#include "city.h"
+#include "nfs_core.h"
+#include "nfs_proto_tools.h"
 
 /* helpers
  */
@@ -95,6 +98,9 @@ struct vfs_fsal_obj_handle *alloc_handle
 	if (hdl->obj_handle.type == REGULAR_FILE) {
 		hdl->u.file.fd.fd = -1;	/* no open on this yet */
 		hdl->u.file.fd.openflags = FSAL_O_CLOSED;
+	} else if (hdl->obj_handle.type == DIRECTORY) {
+		hdl->u.directory.path = NULL;
+		hdl->u.directory.fs_location = NULL;
 	} else if (hdl->obj_handle.type == SYMBOLIC_LINK) {
 		ssize_t retlink;
 		size_t len = stat->st_size + 1;
@@ -160,13 +166,14 @@ static fsal_status_t lookup_with_fd(stru
 				    struct attrlist *attrs_out)
 {
 	struct vfs_fsal_obj_handle *hdl;
-	int retval;
+	int retval, fd;
 	struct stat stat;
 	vfs_file_handle_t *fh = NULL;
 	fsal_dev_t dev;
 	struct fsal_filesystem *fs;
 	bool xfsal = false;
 	fsal_status_t status;
+	fsal_errors_t fsal_error = ERR_FSAL_NO_ERROR;
 
 	vfs_alloc_handle(fh);
 
@@ -263,6 +270,110 @@ static fsal_status_t lookup_with_fd(stru
 		posix2fsal_attributes(&stat, attrs_out);
 	}
 
+	/* if it is a directory and the sticky bit is set
+	 * let's look for referral information
+	 */
+
+	if (hdl->obj_handle.type == DIRECTORY &&
+	    attrs_out != NULL &&
+	    is_sticky_bit_set(&hdl->obj_handle, attrs_out) &&
+	    hdl->obj_handle.fs->private_data != NULL) {
+
+		caddr_t xattr_content;
+		size_t attrsize = 0;
+		char proclnk[MAXPATHLEN];
+		char path[MAXPATHLEN];
+		char *spath, *fspath;
+		ssize_t r;
+		uint64 hash;
+		fsal_status_t st;
+
+		struct vfs_filesystem *vfs_fs =
+			hdl->obj_handle.fs->private_data;
+
+		/* the real path of the referral directory is needed.
+		 * it get's stored in u.directory.path
+		 */
+
+		fd = vfs_fsal_open(hdl, O_DIRECTORY, &fsal_error);
+		if (fd < 0) {
+			close(fd);
+			return fsalstat(fsal_error, -fd);
+		}
+
+		sprintf(proclnk, "/proc/self/fd/%d", fd);
+		r = readlink(proclnk, path, MAXPATHLEN);
+		if (r < 0) {
+			LogEvent(COMPONENT_FSAL, "failed to readlink");
+		}
+		path[r] = '\0';
+		LogDebug(COMPONENT_FSAL, "fd -> path: %d -> %s",
+			 fd, path);
+		if (hdl->u.directory.path != NULL) {
+			LogFullDebug(COMPONENT_FSAL,
+				     "freeing old directory.path: %s",
+				     hdl->u.directory.path);
+			gsh_free(hdl->u.directory.path);
+		}
+
+		fspath = vfs_fs->fs->path;
+
+		spath = path;
+		if (strncmp(path, fspath, strlen(fspath)) == 0) {
+			spath += strlen(fspath);
+		}
+		hdl->u.directory.path = gsh_strdup(spath);
+
+		/* referral configuration is in a xattr "user.fs_location"
+		 * on the directory in the form
+		 * server:/path/to/referred/directory.
+		 * It gets storeded in u.directory.fs_location
+		 */
+
+		xattr_content = gsh_calloc(XATTR_BUFFERSIZE, sizeof(char));
+
+		st = vfs_getextattr_value_by_name((struct fsal_obj_handle *)hdl,
+						  "user.fs_location",
+						  xattr_content,
+						  XATTR_BUFFERSIZE,
+						  &attrsize);
+
+		if (!FSAL_IS_ERROR(st)) {
+			LogDebug(COMPONENT_FSAL, "user.fs_location: %s",
+				 xattr_content);
+			if (hdl->u.directory.fs_location != NULL) {
+				LogFullDebug(COMPONENT_FSAL,
+					     "freeing old directory.fs_location: %s",
+					     hdl->u.directory.fs_location);
+				gsh_free(hdl->u.directory.fs_location);
+			}
+			hdl->u.directory.fs_location =
+				gsh_strdup(xattr_content);
+
+			/* on a referral the filesystem id has to change
+			 * it get's calculated via a hash from the referral
+			 * and stored in the fsid object of the fsal_obj_handle
+			 */
+
+			hash = CityHash64(xattr_content, attrsize);
+			hdl->obj_handle.fsid.major = hash;
+			hdl->obj_handle.fsid.minor = hash;
+			LogDebug(COMPONENT_NFS_V4,
+				 "fsid.major = %"PRIu64", fsid.minor = %"PRIu64,
+				 hdl->obj_handle.fsid.major,
+				 hdl->obj_handle.fsid.minor);
+		}
+		gsh_free(xattr_content);
+		close(fd);
+
+	} else {
+		/* reset fsid if the sticky bit is not set,
+		 * because a referral was removed
+		 */
+
+		hdl->obj_handle.fsid = hdl->obj_handle.fs->fsid;
+	}
+
 	*handle = &hdl->obj_handle;
 	return fsalstat(ERR_FSAL_NO_ERROR, 0);
 }
@@ -1513,6 +1624,11 @@ static void release(struct fsal_obj_hand
 
 		handle_to_key(obj_hdl, &key);
 		vfs_state_release(&key);
+	} else if (type == DIRECTORY) {
+		if (myself->u.directory.path != NULL)
+			gsh_free(myself->u.directory.path);
+		if (myself->u.directory.fs_location != NULL)
+			gsh_free(myself->u.directory.fs_location);
 	} else if (vfs_unopenable_type(type)) {
 		if (myself->u.unopenable.name != NULL)
 			gsh_free(myself->u.unopenable.name);
@@ -1527,6 +1643,65 @@ static void release(struct fsal_obj_hand
 	gsh_free(myself);
 }
 
+/* vfs_fs_locations
+ * returns the saved referral information to NFS protocol layer
+ */
+
+static fsal_status_t vfs_fs_locations(struct fsal_obj_handle *obj_hdl,
+					struct fs_locations4 *fs_locs)
+{
+	struct vfs_fsal_obj_handle *myself;
+
+	myself = container_of(obj_hdl, struct vfs_fsal_obj_handle, obj_handle);
+	struct vfs_filesystem *vfs_fs = myself->obj_handle.fs->private_data;
+
+	LogFullDebug(COMPONENT_FSAL,
+		     "vfs_fs = %s root_fd = %d major = %d minor = %d",
+		     vfs_fs->fs->path, vfs_fs->root_fd,
+		     (int)vfs_fs->fs->fsid.major,
+		     (int)vfs_fs->fs->fsid.minor);
+
+	LogDebug(COMPONENT_FSAL,
+		 "fs_location = %p:%s",
+		 myself->u.directory.fs_location,
+		 myself->u.directory.fs_location);
+	if (myself->u.directory.fs_location != NULL) {
+		char *server;
+		char *path_sav, *path_work;
+
+		path_sav = gsh_strdup(myself->u.directory.fs_location);
+		path_work = path_sav;
+		server = strsep(&path_work, ":");
+
+		LogDebug(COMPONENT_FSAL,
+			 "fs_location server %s",
+			 server);
+		LogDebug(COMPONENT_FSAL,
+			 "fs_location path %s",
+			 path_work);
+
+		nfs4_pathname4_free(&fs_locs->fs_root);
+		nfs4_pathname4_alloc(&fs_locs->fs_root,
+				     myself->u.directory.path);
+		strncpy(fs_locs->locations.locations_val->
+			server.server_val->utf8string_val,
+			server, strlen(server));
+		fs_locs->locations.locations_val->
+			server.server_val->utf8string_len = strlen(server);
+		nfs4_pathname4_free(&fs_locs->
+				    locations.locations_val->rootpath);
+		nfs4_pathname4_alloc(&fs_locs->
+				     locations.locations_val->rootpath,
+				     path_work);
+
+		gsh_free(path_sav);
+	} else {
+		return fsalstat(ERR_FSAL_NOTSUPP, -1);
+	}
+
+	return fsalstat(ERR_FSAL_NO_ERROR, 0);
+}
+
 void vfs_handle_ops_init(struct fsal_obj_ops *ops)
 {
 	ops->release = release;
@@ -1541,6 +1716,7 @@ void vfs_handle_ops_init(struct fsal_obj
 	ops->link = linkfile;
 	ops->rename = renamefile;
 	ops->unlink = file_unlink;
+	ops->fs_locations = vfs_fs_locations;
 	ops->close = vfs_close;
 	ops->handle_digest = handle_digest;
 	ops->handle_to_key = handle_to_key;
--- a/src/FSAL/FSAL_VFS/vfs_methods.h
+++ b/src/FSAL/FSAL_VFS/vfs_methods.h
@@ -152,6 +152,10 @@ struct vfs_fsal_obj_handle {
 			struct vfs_fd fd;
 		} file;
 		struct {
+			char *path;
+			char *fs_location;
+		} directory;
+		struct {
 			unsigned char *link_content;
 			int link_size;
 		} symlink;
--- a/src/Protocols/NFS/nfs4_op_getattr.c
+++ b/src/Protocols/NFS/nfs4_op_getattr.c
@@ -47,25 +47,15 @@ static inline bool check_fs_locations(st
 	fsal_status_t st;
 	fs_locations4 fs_locs;
 	fs_location4 fs_loc;
-	component4 fs_path;
-	component4 fs_root;
 	component4 fs_server;
-	char root[MAXPATHLEN];
-	char path[MAXPATHLEN];
 	char server[MAXHOSTNAMELEN];
 
-	fs_root.utf8string_len = sizeof(root);
-	fs_root.utf8string_val = root;
-	fs_path.utf8string_len = sizeof(path);
-	fs_path.utf8string_val = path;
-	fs_locs.fs_root.pathname4_len = 1;
-	fs_locs.fs_root.pathname4_val = &fs_path;
+	nfs4_pathname4_alloc(&fs_locs.fs_root, NULL);
 	fs_server.utf8string_len = sizeof(server);
 	fs_server.utf8string_val = server;
 	fs_loc.server.server_len = 1;
 	fs_loc.server.server_val = &fs_server;
-	fs_loc.rootpath.pathname4_len = 1;
-	fs_loc.rootpath.pathname4_val = &fs_root;
+	nfs4_pathname4_alloc(&fs_loc.rootpath, NULL);
 	fs_locs.locations.locations_len = 1;
 	fs_locs.locations.locations_val = &fs_loc;
 
@@ -76,6 +66,9 @@ static inline bool check_fs_locations(st
 	*/
 	st = obj->obj_ops.fs_locations(obj, &fs_locs);
 
+	nfs4_pathname4_free(&fs_locs.fs_root);
+	nfs4_pathname4_free(&fs_loc.rootpath);
+
 	return !FSAL_IS_ERROR(st);
 }
 
@@ -145,9 +138,9 @@ int nfs4_op_getattr(struct nfs_argop4 *o
 	if (data->current_obj->type == DIRECTORY &&
 	    is_sticky_bit_set(data->current_obj, &attrs) &&
 	    !attribute_is_set(&arg_GETATTR4->attr_request,
-			       FATTR4_FS_LOCATIONS) &&
+			      FATTR4_FS_LOCATIONS) &&
 	    check_fs_locations(data->current_obj))
-			res_GETATTR4->status = NFS4ERR_MOVED;
+		res_GETATTR4->status = NFS4ERR_MOVED;
 
 	/* Done with the attrs */
 	fsal_release_attrs(&attrs);
--- a/src/Protocols/NFS/nfs_proto_tools.c
+++ b/src/Protocols/NFS/nfs_proto_tools.c
@@ -600,6 +600,9 @@ static fattr_xdr_result encode_fsid(XDR
 		fsid.major = args->fsid.major;
 		fsid.minor = args->fsid.minor;
 	}
+	LogDebug(COMPONENT_NFS_V4,
+		 "fsid.major = %"PRIu64", fsid.minor = %"PRIu64,
+		 fsid.major, fsid.minor);
 
 	if (!xdr_u_int64_t(xdr, &fsid.major))
 		return FATTR_XDR_FAILED;
@@ -1187,6 +1190,94 @@ static fattr_xdr_result decode_files_tot
 }
 
 /*
+ * allocate a dynamic pathname4 structure out of a filesystem path
+ */
+
+void nfs4_pathname4_alloc(pathname4 *pathname4, char *path)
+{
+	char *path_sav, *token, *path_work;
+	int i = 0;
+
+	if (path == NULL) {
+		pathname4->pathname4_val = gsh_malloc(sizeof(component4));
+		pathname4->pathname4_len = 1;
+		pathname4->pathname4_val->utf8string_val =
+			gsh_calloc(MAXPATHLEN, sizeof(char));
+		pathname4->pathname4_val->utf8string_len = MAXPATHLEN;
+	} else {
+		path_sav = gsh_strdup(path);
+		/* count tokens */
+		path_work = path_sav;
+		while ((token = strsep(&path_work, "/")) != NULL) {
+			if (strlen(token) > 0) {
+				i++;
+			}
+		}
+		LogDebug(COMPONENT_NFSPROTO, "%s has %d tokens", path, i);
+		/* reset content of path_sav */
+		strcpy(path_sav, path);
+		path_work = path_sav;
+
+		/* fill component4 */
+		pathname4->pathname4_val = gsh_malloc(i * sizeof(component4));
+		i = 0;
+		while ((token = strsep(&path_work, "/")) != NULL) {
+			if (strlen(token) > 0) {
+				LogDebug(COMPONENT_NFSPROTO,
+					 "token %d is %s", i, token);
+				pathname4->pathname4_val[i].utf8string_val =
+					gsh_strdup(token);
+				pathname4->pathname4_val[i].utf8string_len =
+					strlen(token);
+				i++;
+			}
+		}
+		pathname4->pathname4_len = i;
+		gsh_free(path_sav);
+	}
+}
+
+/*
+ * free dynamic pathname4 structure
+ */
+
+void nfs4_pathname4_free(pathname4 *pathname4)
+{
+	int i;
+
+	if (pathname4 != NULL) {
+
+		i = pathname4->pathname4_len;
+		LogFullDebug(COMPONENT_NFSPROTO,
+			     "number of pathname components to free: %d", i);
+
+		if (pathname4->pathname4_val != NULL) {
+			while (i-- > 0) {
+				if (pathname4->pathname4_val[i].utf8string_val
+				    != NULL) {
+					LogFullDebug(COMPONENT_NFSPROTO,
+						     "freeing component %d: %s",
+						     i+1,
+						     pathname4->
+						     pathname4_val[i].
+						     utf8string_val);
+					if (i > 0) {
+						gsh_free(pathname4->
+							 pathname4_val[i]
+							 .utf8string_val);
+						pathname4->
+							pathname4_val[i]
+							.utf8string_val = NULL;
+					}
+				}
+			}
+			gsh_free(pathname4->pathname4_val);
+			pathname4->pathname4_val = NULL;
+		}
+	}
+}
+
+/*
  * FATTR4_FS_LOCATIONS
  */
 
@@ -1196,11 +1287,7 @@ static fattr_xdr_result encode_fs_locati
 	fsal_status_t st;
 	fs_locations4 fs_locs;
 	fs_location4 fs_loc;
-	component4 fs_path;
-	component4 fs_root;
 	component4 fs_server;
-	char root[MAXPATHLEN];
-	char path[MAXPATHLEN];
 	char server[MAXHOSTNAMELEN];
 
 	if (args->data == NULL || args->data->current_obj == NULL)
@@ -1209,18 +1296,12 @@ static fattr_xdr_result encode_fs_locati
 	if (args->data->current_obj->type != DIRECTORY)
 		return FATTR_XDR_NOOP;
 
-	fs_root.utf8string_len = sizeof(root);
-	fs_root.utf8string_val = root;
-	fs_path.utf8string_len = sizeof(path);
-	fs_path.utf8string_val = path;
-	fs_locs.fs_root.pathname4_len = 1;
-	fs_locs.fs_root.pathname4_val = &fs_path;
+	nfs4_pathname4_alloc(&fs_locs.fs_root, NULL);
 	fs_server.utf8string_len = sizeof(server);
 	fs_server.utf8string_val = server;
 	fs_loc.server.server_len = 1;
 	fs_loc.server.server_val = &fs_server;
-	fs_loc.rootpath.pathname4_len = 1;
-	fs_loc.rootpath.pathname4_val = &fs_root;
+	nfs4_pathname4_alloc(&fs_loc.rootpath, NULL);
 	fs_locs.locations.locations_len = 1;
 	fs_locs.locations.locations_val = &fs_loc;
 
@@ -1233,16 +1314,37 @@ static fattr_xdr_result encode_fs_locati
 					args->data->current_obj,
 					&fs_locs);
 	if (FSAL_IS_ERROR(st)) {
-		strcpy(root, "not_supported");
-		strcpy(path, "not_supported");
+		strcpy(fs_locs.fs_root.pathname4_val->utf8string_val,
+		       "not_supported");
+		strcpy(fs_loc.rootpath.pathname4_val->utf8string_val,
+		       "not_supported");
 		strcpy(server, "not_supported");
-		fs_root.utf8string_len = strlen(root);
-		fs_path.utf8string_len = strlen(path);
+		fs_locs.fs_root.pathname4_val->utf8string_len =
+			strlen(fs_locs.fs_root.pathname4_val->utf8string_val);
+		fs_loc.rootpath.pathname4_val->utf8string_len =
+			strlen(fs_loc.rootpath.pathname4_val->utf8string_val);
 		fs_server.utf8string_len = strlen(server);
+
+		LogEvent(COMPONENT_NFSPROTO,
+			 "encode_fs_locations obj_ops.fs_locations failed %s, %s, %s",
+			 fs_locs.fs_root.pathname4_val->utf8string_val,
+			 fs_loc.rootpath.pathname4_val->utf8string_val,
+			 server);
+
 	}
 
-	if (!xdr_fs_locations4(xdr, &fs_locs))
+	if (!xdr_fs_locations4(xdr, &fs_locs)) {
+		LogEvent(COMPONENT_NFSPROTO,
+			 "encode_fs_locations xdr_fs_locations failed %s, %s, %s",
+			 fs_locs.fs_root.pathname4_val->utf8string_val,
+			 fs_loc.rootpath.pathname4_val->utf8string_val,
+			 server);
+
 		return FATTR_XDR_FAILED;
+	}
+
+	nfs4_pathname4_free(&fs_locs.fs_root);
+	nfs4_pathname4_free(&fs_loc.rootpath);
 
 	return FATTR_XDR_SUCCESS;
 }
--- a/src/include/nfs_proto_tools.h
+++ b/src/include/nfs_proto_tools.h
@@ -303,4 +303,9 @@ enum nfs4_minor_vers {
 	NFS4_MINOR_VERS_1,
 	NFS4_MINOR_VERS_2
 };
+
+void nfs4_pathname4_alloc(pathname4 *, char *);
+
+void nfs4_pathname4_free(pathname4 *);
+
 #endif				/* _NFS_PROTO_TOOLS_H */