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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/*
Copyright (c) 2010-2011 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS 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 3 of the License,
or (at your option) any later version.
GlusterFS 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, see
<http://www.gnu.org/licenses/>.
*/
#include "fuse-bridge.h"
#if defined(GF_SOLARIS_HOST_OS)
#include <sys/procfs.h>
#else
#include <sys/sysctl.h>
#endif
static void
fuse_resolve_wipe (fuse_resolve_t *resolve)
{
if (resolve->path)
GF_FREE ((void *)resolve->path);
if (resolve->bname)
GF_FREE ((void *)resolve->bname);
if (resolve->resolved)
GF_FREE ((void *)resolve->resolved);
if (resolve->fd)
fd_unref (resolve->fd);
loc_wipe (&resolve->resolve_loc);
if (resolve->hint) {
inode_unref (resolve->hint);
resolve->hint = 0;
}
if (resolve->parhint) {
inode_unref (resolve->parhint);
resolve->parhint = 0;
}
}
void
free_fuse_state (fuse_state_t *state)
{
xlator_t *this = NULL;
fuse_private_t *priv = NULL;
uint64_t winds = 0;
char switched = 0;
this = state->this;
priv = this->private;
loc_wipe (&state->loc);
loc_wipe (&state->loc2);
if (state->xdata) {
dict_unref (state->xdata);
state->xdata = (void *)0xaaaaeeee;
}
if (state->xattr)
dict_unref (state->xattr);
if (state->name) {
GF_FREE (state->name);
state->name = NULL;
}
if (state->fd) {
fd_unref (state->fd);
state->fd = (void *)0xfdfdfdfd;
}
if (state->finh) {
GF_FREE (state->finh);
state->finh = NULL;
}
fuse_resolve_wipe (&state->resolve);
fuse_resolve_wipe (&state->resolve2);
pthread_mutex_lock (&priv->sync_mutex);
{
winds = --state->active_subvol->winds;
switched = state->active_subvol->switched;
}
pthread_mutex_unlock (&priv->sync_mutex);
if ((winds == 0) && (switched)) {
xlator_notify (state->active_subvol, GF_EVENT_PARENT_DOWN,
state->active_subvol, NULL);
}
#ifdef DEBUG
memset (state, 0x90, sizeof (*state));
#endif
GF_FREE (state);
state = NULL;
}
fuse_state_t *
get_fuse_state (xlator_t *this, fuse_in_header_t *finh)
{
fuse_state_t *state = NULL;
xlator_t *active_subvol = NULL;
fuse_private_t *priv = NULL;
state = (void *)GF_CALLOC (1, sizeof (*state),
gf_fuse_mt_fuse_state_t);
if (!state)
return NULL;
state->this = THIS;
priv = this->private;
pthread_mutex_lock (&priv->sync_mutex);
{
active_subvol = fuse_active_subvol (state->this);
active_subvol->winds++;
}
pthread_mutex_unlock (&priv->sync_mutex);
state->active_subvol = active_subvol;
state->itable = active_subvol->itable;
state->pool = this->ctx->pool;
state->finh = finh;
state->this = this;
LOCK_INIT (&state->lock);
return state;
}
void
frame_fill_groups (call_frame_t *frame)
{
#if defined(GF_LINUX_HOST_OS)
char filename[32];
char line[4096];
char *ptr = NULL;
FILE *fp = NULL;
int idx = 0;
long int id = 0;
char *saveptr = NULL;
char *endptr = NULL;
int ret = 0;
ret = snprintf (filename, sizeof filename, "/proc/%d/status", frame->root->pid);
if (ret >= sizeof filename)
goto out;
fp = fopen (filename, "r");
if (!fp)
goto out;
while ((ptr = fgets (line, sizeof line, fp))) {
if (strncmp (ptr, "Groups:", 7) != 0)
continue;
ptr = line + 8;
for (ptr = strtok_r (ptr, " \t\r\n", &saveptr);
ptr;
ptr = strtok_r (NULL, " \t\r\n", &saveptr)) {
errno = 0;
id = strtol (ptr, &endptr, 0);
if (errno == ERANGE)
break;
if (!endptr || *endptr)
break;
frame->root->groups[idx++] = id;
if (idx == GF_MAX_AUX_GROUPS)
break;
}
frame->root->ngrps = idx;
break;
}
out:
if (fp)
fclose (fp);
#elif defined(GF_SOLARIS_HOST_OS)
char filename[32];
char scratch[128];
prcred_t *prcred = (prcred_t *) scratch;
FILE *fp = NULL;
int ret = 0;
ret = snprintf (filename, sizeof filename,
"/proc/%d/cred", frame->root->pid);
if (ret < sizeof filename) {
fp = fopen (filename, "r");
if (fp != NULL) {
if (fgets (scratch, sizeof scratch, fp) != NULL) {
frame->root->ngrps = MIN(prcred->pr_ngroups,
GF_REQUEST_MAXGROUPS);
}
fclose (fp);
}
}
#elif defined(CTL_KERN) /* DARWIN and *BSD */
/*
N.B. CTL_KERN is an enum on Linux. (Meaning, if it's not
obvious, that it's not subject to preprocessor directives
like '#if defined'.)
Unlike Linux, on Mac OS and the BSDs it is a #define. We
could test to see that KERN_PROC is defined, but, barring any
evidence to the contrary, I think that's overkill.
We might also test that GF_DARWIN_HOST_OS is defined, why
limit this to just Mac OS. It's equally valid for the BSDs
and we do have people building on NetBSD and FreeBSD.
*/
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, frame->root->pid };
size_t namelen = sizeof name / sizeof name[0];
struct kinfo_proc kp;
size_t kplen = sizeof(kp);
int i, ngroups;
if (sysctl(name, namelen, &kp, &kplen, NULL, 0) != 0)
return;
ngroups = MIN(kp.kp_eproc.e_ucred.cr_ngroups, GF_REQUEST_MAXGROUPS);
for (i = 0; i < ngroups; i++)
frame->root->groups[i] = kp.kp_eproc.e_ucred.cr_groups[i];
frame->root->ngrps = ngroups;
#else
frame->root->ngrps = 0;
#endif /* GF_LINUX_HOST_OS */
}
call_frame_t *
get_call_frame_for_req (fuse_state_t *state)
{
call_pool_t *pool = NULL;
fuse_in_header_t *finh = NULL;
call_frame_t *frame = NULL;
xlator_t *this = NULL;
fuse_private_t *priv = NULL;
pool = state->pool;
finh = state->finh;
this = state->this;
priv = this->private;
frame = create_frame (this, pool);
if (!frame)
return NULL;
if (finh) {
frame->root->uid = finh->uid;
frame->root->gid = finh->gid;
frame->root->pid = finh->pid;
frame->root->unique = finh->unique;
set_lk_owner_from_uint64 (&frame->root->lk_owner,
state->lk_owner);
}
frame_fill_groups (frame);
if (priv && priv->client_pid_set)
frame->root->pid = priv->client_pid;
frame->root->type = GF_OP_TYPE_FOP;
return frame;
}
inode_t *
fuse_ino_to_inode (uint64_t ino, xlator_t *fuse)
{
inode_t *inode = NULL;
xlator_t *active_subvol = NULL;
if (ino == 1) {
active_subvol = fuse_active_subvol (fuse);
if (active_subvol)
inode = active_subvol->itable->root;
} else {
inode = (inode_t *) (unsigned long) ino;
inode_ref (inode);
}
return inode;
}
uint64_t
inode_to_fuse_nodeid (inode_t *inode)
{
if (!inode || __is_root_gfid (inode->gfid))
return 1;
return (unsigned long) inode;
}
GF_MUST_CHECK int32_t
fuse_loc_fill (loc_t *loc, fuse_state_t *state, ino_t ino,
ino_t par, const char *name)
{
inode_t *inode = NULL;
inode_t *parent = NULL;
int32_t ret = -1;
char *path = NULL;
uuid_t null_gfid = {0,};
/* resistance against multiple invocation of loc_fill not to get
reference leaks via inode_search() */
if (name) {
parent = loc->parent;
if (!parent) {
parent = fuse_ino_to_inode (par, state->this);
loc->parent = parent;
if (parent)
uuid_copy (loc->pargfid, parent->gfid);
}
inode = loc->inode;
if (!inode) {
inode = inode_grep (parent->table, parent, name);
loc->inode = inode;
}
ret = inode_path (parent, name, &path);
if (ret <= 0) {
gf_log ("glusterfs-fuse", GF_LOG_DEBUG,
"inode_path failed for %s/%s",
(parent)?uuid_utoa (parent->gfid):"0", name);
goto fail;
}
loc->path = path;
} else {
inode = loc->inode;
if (!inode) {
inode = fuse_ino_to_inode (ino, state->this);
loc->inode = inode;
if (inode)
uuid_copy (loc->gfid, inode->gfid);
}
parent = loc->parent;
if (!parent) {
parent = inode_parent (inode, null_gfid, NULL);
loc->parent = parent;
if (parent)
uuid_copy (loc->pargfid, parent->gfid);
}
ret = inode_path (inode, NULL, &path);
if (ret <= 0) {
gf_log ("glusterfs-fuse", GF_LOG_DEBUG,
"inode_path failed for %s",
(inode) ? uuid_utoa (inode->gfid) : "0");
goto fail;
}
loc->path = path;
}
if (loc->path) {
loc->name = strrchr (loc->path, '/');
if (loc->name)
loc->name++;
else
loc->name = "";
}
if ((ino != 1) && (parent == NULL)) {
gf_log ("fuse-bridge", GF_LOG_DEBUG,
"failed to search parent for %"PRId64"/%s (%"PRId64")",
(ino_t)par, name, (ino_t)ino);
ret = -1;
goto fail;
}
ret = 0;
fail:
return ret;
}
/* courtesy of folly */
void
gf_fuse_stat2attr (struct iatt *st, struct fuse_attr *fa)
{
fa->ino = st->ia_ino;
fa->size = st->ia_size;
fa->blocks = st->ia_blocks;
fa->atime = st->ia_atime;
fa->mtime = st->ia_mtime;
fa->ctime = st->ia_ctime;
fa->atimensec = st->ia_atime_nsec;
fa->mtimensec = st->ia_mtime_nsec;
fa->ctimensec = st->ia_ctime_nsec;
fa->mode = st_mode_from_ia (st->ia_prot, st->ia_type);
fa->nlink = st->ia_nlink;
fa->uid = st->ia_uid;
fa->gid = st->ia_gid;
fa->rdev = makedev (ia_major (st->ia_rdev),
ia_minor (st->ia_rdev));
#if FUSE_KERNEL_MINOR_VERSION >= 9
fa->blksize = st->ia_blksize;
#endif
#ifdef GF_DARWIN_HOST_OS
fa->crtime = (uint64_t)-1;
fa->crtimensec = (uint32_t)-1;
fa->flags = 0;
#endif
}
static int
fuse_do_flip_xattr_ns (char *okey, const char *nns, char **nkey)
{
int ret = 0;
char *key = NULL;
okey = strchr (okey, '.');
GF_ASSERT (okey);
key = GF_CALLOC (1, strlen (nns) + strlen(okey) + 1,
gf_common_mt_char);
if (!key) {
ret = -1;
goto out;
}
strcpy (key, nns);
strcat (key, okey);
*nkey = key;
out:
return ret;
}
int
fuse_xattr_alloc_default (char *okey, char **nkey)
{
int ret = 0;
*nkey = gf_strdup (okey);
if (!*nkey)
ret = -1;
return ret;
}
#define PRIV_XA_NS "trusted"
#define UNPRIV_XA_NS "system"
int
fuse_flip_xattr_ns (fuse_private_t *priv, char *okey, char **nkey)
{
int ret = 0;
gf_boolean_t need_flip = _gf_false;
gf_client_pid_t npid = 0;
npid = priv->client_pid;
if (gf_client_pid_check (npid)) {
ret = fuse_xattr_alloc_default (okey, nkey);
goto out;
}
switch (npid) {
/*
* These two cases will never execute as we check the
* pid range above, but are kept to keep the compiler
* happy.
*/
case GF_CLIENT_PID_MAX:
case GF_CLIENT_PID_MIN:
goto out;
case GF_CLIENT_PID_GSYNCD:
/* valid xattr(s): *xtime, volume-mark* */
gf_log("glusterfs-fuse", GF_LOG_DEBUG, "PID: %d, checking xattr(s): "
"volume-mark*, *xtime", npid);
if ( (strcmp (okey, UNPRIV_XA_NS".glusterfs.volume-mark") == 0)
|| (fnmatch (UNPRIV_XA_NS".glusterfs.volume-mark.*", okey, FNM_PERIOD) == 0)
|| (fnmatch (UNPRIV_XA_NS".glusterfs.*.xtime", okey, FNM_PERIOD) == 0) )
need_flip = _gf_true;
break;
case GF_CLIENT_PID_HADOOP:
/* valid xattr(s): pathinfo */
gf_log("glusterfs-fuse", GF_LOG_DEBUG, "PID: %d, checking xattr(s): "
"pathinfo", npid);
if (strcmp (okey, UNPRIV_XA_NS".glusterfs.pathinfo") == 0)
need_flip = _gf_true;
break;
}
if (need_flip) {
gf_log ("glusterfs-fuse", GF_LOG_DEBUG, "flipping %s to "PRIV_XA_NS" equivalent",
okey);
ret = fuse_do_flip_xattr_ns (okey, PRIV_XA_NS, nkey);
} else {
/* if we cannot match, continue with what we got */
ret = fuse_xattr_alloc_default (okey, nkey);
}
out:
return ret;
}
|