summaryrefslogtreecommitdiffstats
path: root/contrib/fuse-lib/mount.c
blob: c6de43607f3d485fad50bbaca5154493805e058a (plain)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
  FUSE: Filesystem in Userspace
  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
  Copyright (c) 2009 Z RESEARCH, Inc. <http://www.zresearch.com>

  This program can be distributed under the terms of the GNU LGPLv2.
  See the file COPYING.LIB.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <mntent.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <sys/mount.h>

#ifdef FUSE_UTIL
#define MALLOC(size) malloc (size)
#define FREE(ptr) free (ptr)
#define GFFUSE_LOGERR(...) fprintf (stderr, ## __VA_ARGS__)
#else /* FUSE_UTIL */
#include "glusterfs.h"
#include "logging.h"
#include "common-utils.h"

#ifdef GF_FUSERMOUNT
#define FUSERMOUNT_PROG FUSERMOUNT_DIR "/fusermount-glusterfs"
#else
#define FUSERMOUNT_PROG "fusermount"
#endif
#define FUSE_COMMFD_ENV "_FUSE_COMMFD"

#define GFFUSE_LOGERR(...) \
        gf_log ("glusterfs-fuse", GF_LOG_ERROR, ## __VA_ARGS__)
#endif /* !FUSE_UTIL */

/*
 * Functions below, until following note, were taken from libfuse
 * (http://git.gluster.com/?p=users/csaba/fuse.git;a=commit;h=b988bbf9)
 * almost verbatim. What has been changed:
 * - style adopted to that of glusterfs
 * - s/fprintf/gf_log/
 * - s/free/FREE/, s/malloc/MALLOC/
 * - there are some other minor things
 */

static int
mtab_needs_update (const char *mnt)
{
        int res;
        struct stat stbuf;

        /* If mtab is within new mount, don't touch it */
        if (strncmp (mnt, _PATH_MOUNTED, strlen (mnt)) == 0 &&
            _PATH_MOUNTED[strlen (mnt)] == '/')
                return 0;

        /*
         * Skip mtab update if /etc/mtab:
         *
         *  - doesn't exist,
         *  - is a symlink,
         *  - is on a read-only filesystem.
         */
        res = lstat (_PATH_MOUNTED, &stbuf);
        if (res == -1) {
                if (errno == ENOENT)
                        return 0;
        } else {
                if (S_ISLNK (stbuf.st_mode))
                        return 0;

                res = access (_PATH_MOUNTED, W_OK);
                if (res == -1 && errno == EROFS)
                        return 0;
        }

        return 1;
}

#ifndef FUSE_UTIL
static
#endif
int
fuse_mnt_add_mount (const char *progname, const char *fsname,
                    const char *mnt, const char *type, const char *opts)
{
        int res;
        int status;
        sigset_t blockmask;
        sigset_t oldmask;

        if (!mtab_needs_update (mnt))
                return 0;

        sigemptyset (&blockmask);
        sigaddset (&blockmask, SIGCHLD);
        res = sigprocmask (SIG_BLOCK, &blockmask, &oldmask);
        if (res == -1) {
                GFFUSE_LOGERR ("%s: sigprocmask: %s",
                               progname, strerror (errno));
                return -1;
        }

        res = fork ();
        if (res == -1) {
                GFFUSE_LOGERR ("%s: fork: %s", progname, strerror (errno));
                goto out_restore;
        }
        if (res == 0) {
                char templ[] = "/tmp/fusermountXXXXXX";
                char *tmp;

                sigprocmask (SIG_SETMASK, &oldmask, NULL);
                setuid (geteuid ());

                /*
                 * hide in a directory, where mount isn't able to resolve
                 * fsname as a valid path
                 */
                tmp = mkdtemp (templ);
                if (!tmp) {
                        GFFUSE_LOGERR ("%s: failed to create temporary directory",
                                       progname);
                        exit (1);
                }
                if (chdir (tmp)) {
                        GFFUSE_LOGERR ("%s: failed to chdir to %s: %s",
                                       progname, tmp, strerror (errno));
                        exit (1);
                }
                rmdir (tmp);
                execl ("/bin/mount", "/bin/mount", "-i", "-f", "-t", type,
                       "-o", opts, fsname, mnt, NULL);
                GFFUSE_LOGERR ("%s: failed to execute /bin/mount: %s",
                               progname, strerror (errno));
                exit (1);
        }
        res = waitpid (res, &status, 0);
        if (res == -1)
                GFFUSE_LOGERR ("%s: waitpid: %s", progname, strerror (errno));

        if (status != 0)
                res = -1;

 out_restore:
        sigprocmask (SIG_SETMASK, &oldmask, NULL);
        return res;
}

#ifndef FUSE_UTIL
static
#endif
char
*fuse_mnt_resolve_path (const char *progname, const char *orig)
{
        char buf[PATH_MAX];
        char *copy;
        char *dst;
        char *end;
        char *lastcomp;
        const char *toresolv;

        if (!orig[0]) {
                GFFUSE_LOGERR ("%s: invalid mountpoint '%s'", progname, orig);
                return NULL;
        }

        copy = strdup (orig);
        if (copy == NULL) {
                GFFUSE_LOGERR ("%s: failed to allocate memory", progname);
                return NULL;
        }

        toresolv = copy;
        lastcomp = NULL;
        for (end = copy + strlen (copy) - 1; end > copy && *end == '/'; end --);
        if (end[0] != '/') {
                char *tmp;
                end[1] = '\0';
                tmp = strrchr (copy, '/');
                if (tmp == NULL) {
                        lastcomp = copy;
                        toresolv = ".";
                } else {
                        lastcomp = tmp + 1;
                        if (tmp == copy)
                                toresolv = "/";
                }
                if (strcmp (lastcomp, ".") == 0 || strcmp (lastcomp, "..") == 0) {
                        lastcomp = NULL;
                        toresolv = copy;
                }
                else if (tmp)
                        tmp[0] = '\0';
        }
        if (realpath (toresolv, buf) == NULL) {
                GFFUSE_LOGERR ("%s: bad mount point %s: %s", progname, orig,
                               strerror (errno));
                FREE (copy);
                return NULL;
        }
        if (lastcomp == NULL)
                dst = strdup (buf);
        else {
                dst = (char *) MALLOC (strlen (buf) + 1 + strlen (lastcomp) + 1);
                if (dst) {
                        unsigned buflen = strlen (buf);
                        if (buflen && buf[buflen-1] == '/')
                                sprintf (dst, "%s%s", buf, lastcomp);
                        else
                                sprintf (dst, "%s/%s", buf, lastcomp);
                }
        }
        FREE (copy);
        if (dst == NULL)
                GFFUSE_LOGERR ("%s: failed to allocate memory", progname);
        return dst;
}

#ifndef FUSE_UTIL
/* return value:
 * >= 0         => fd
 * -1         => error
 */
static int
receive_fd (int fd)
{
        struct msghdr msg;
        struct iovec iov;
        char buf[1];
        int rv;
        size_t ccmsg[CMSG_SPACE (sizeof (int)) / sizeof (size_t)];
        struct cmsghdr *cmsg;

        iov.iov_base = buf;
        iov.iov_len = 1;

        msg.msg_name = 0;
        msg.msg_namelen = 0;
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
        /* old BSD implementations should use msg_accrights instead of
         * msg_control; the interface is different. */
        msg.msg_control = ccmsg;
        msg.msg_controllen = sizeof (ccmsg);

        while (((rv = recvmsg (fd, &msg, 0)) == -1) && errno == EINTR);
        if (rv == -1) {
                GFFUSE_LOGERR ("recvmsg failed: %s", strerror (errno));
                return -1;
        }
        if (!rv) {
                /* EOF */
                return -1;
        }

        cmsg = CMSG_FIRSTHDR (&msg);
        if (!cmsg->cmsg_type == SCM_RIGHTS) {
                GFFUSE_LOGERR ("got control message of unknown type %d",
                               cmsg->cmsg_type);
                return -1;
        }
        return *(int*)CMSG_DATA (cmsg);
}

static int
fuse_mount_fusermount (const char *mountpoint, const char *opts)
{
        int fds[2], pid;
        int res;
        int rv;

        res = socketpair (PF_UNIX, SOCK_STREAM, 0, fds);
        if (res == -1) {
                GFFUSE_LOGERR ("socketpair() failed: %s", strerror (errno));
                return -1;
        }

        pid = fork ();
        if (pid == -1) {
                GFFUSE_LOGERR ("fork() failed: %s", strerror (errno));
                close (fds[0]);
                close (fds[1]);
                return -1;
        }

        if (pid == 0) {
                char env[10];
                const char *argv[32];
                int a = 0;

                argv[a++] = FUSERMOUNT_PROG;
                if (opts) {
                        argv[a++] = "-o";
                        argv[a++] = opts;
                }
                argv[a++] = "--";
                argv[a++] = mountpoint;
                argv[a++] = NULL;

                close (fds[1]);
                fcntl (fds[0], F_SETFD, 0);
                snprintf (env, sizeof (env), "%i", fds[0]);
                setenv (FUSE_COMMFD_ENV, env, 1);
                execvp (FUSERMOUNT_PROG, (char **)argv);
                GFFUSE_LOGERR ("failed to exec fusermount: %s",
                               strerror (errno));
                _exit (1);
        }

        close (fds[0]);
        rv = receive_fd (fds[1]);
        close (fds[1]);
        waitpid (pid, NULL, 0); /* bury zombie */

        return rv;
}
#endif

#ifndef FUSE_UTIL
static
#endif
int
fuse_mnt_umount (const char *progname, const char *mnt, int lazy)
{
        int res;
        int status;
        sigset_t blockmask;
        sigset_t oldmask;

        if (!mtab_needs_update (mnt)) {
                res = umount2 (mnt, lazy ? 2 : 0);
                if (res == -1)
                        GFFUSE_LOGERR ("%s: failed to unmount %s: %s",
                                       progname, mnt, strerror (errno));
                return res;
        }

        sigemptyset (&blockmask);
        sigaddset (&blockmask, SIGCHLD);
        res = sigprocmask (SIG_BLOCK, &blockmask, &oldmask);
        if (res == -1) {
                GFFUSE_LOGERR ("%s: sigprocmask: %s", progname,
                               strerror (errno));
                return -1;
        }

        res = fork ();
        if (res == -1) {
                GFFUSE_LOGERR ("%s: fork: %s", progname, strerror (errno));
                goto out_restore;
        }
        if (res == 0) {
                sigprocmask (SIG_SETMASK, &oldmask, NULL);
                setuid (geteuid ());
                execl ("/bin/umount", "/bin/umount", "-i", mnt,
                      lazy ? "-l" : NULL, NULL);
                GFFUSE_LOGERR ("%s: failed to execute /bin/umount: %s",
                               progname, strerror (errno));
                exit (1);
        }
        res = waitpid (res, &status, 0);
        if (res == -1)
                GFFUSE_LOGERR ("%s: waitpid: %s", progname, strerror (errno));

        if (status != 0)
                res = -1;

 out_restore:
        sigprocmask (SIG_SETMASK, &oldmask, NULL);
        return res;
}

#ifdef FUSE_UTIL
int
fuse_mnt_check_empty (const char *progname, const char *mnt,
                      mode_t rootmode, off_t rootsize)
{
        int isempty = 1;

        if (S_ISDIR (rootmode)) {
                struct dirent *ent;
                DIR *dp = opendir (mnt);
                if (dp == NULL) {
                        fprintf (stderr,
                                 "%s: failed to open mountpoint for reading: %s\n",
                                 progname, strerror (errno));
                        return -1;
                }
                while ((ent = readdir (dp)) != NULL) {
                        if (strcmp (ent->d_name, ".") != 0 &&
                            strcmp (ent->d_name, "..") != 0) {
                                isempty = 0;
                                break;
                        }
                }
                closedir (dp);
        } else if (rootsize)
                isempty = 0;

        if (!isempty) {
                fprintf (stderr, "%s: mountpoint is not empty\n", progname);
                fprintf (stderr, "%s: if you are sure this is safe, "
                         "use the 'nonempty' mount option\n", progname);
                return -1;
        }
        return 0;
}

int
fuse_mnt_check_fuseblk (void)
{
        char buf[256];
        FILE *f = fopen ("/proc/filesystems", "r");
        if (!f)
                return 1;

        while (fgets (buf, sizeof (buf), f))
                if (strstr (buf, "fuseblk\n")) {
                        fclose (f);
                        return 1;
                }

        fclose (f);
        return 0;
}
#endif

#ifndef FUSE_UTIL
void
gf_fuse_unmount (const char *mountpoint, int fd)
{
        int res;
        int pid;

        if (!mountpoint)
                return;

        if (fd != -1) {
                struct pollfd pfd;

                pfd.fd = fd;
                pfd.events = 0;
                res = poll (&pfd, 1, 0);
                /* If file poll returns POLLERR on the device file descriptor,
                   then the filesystem is already unmounted */
                if (res == 1 && (pfd.revents & POLLERR))
                        return;

                /* Need to close file descriptor, otherwise synchronous umount
                   would recurse into filesystem, and deadlock */
                close (fd);
        }

        if (geteuid () == 0) {
                fuse_mnt_umount ("fuse", mountpoint, 1);
                return;
        }

        res = umount2 (mountpoint, 2);
        if (res == 0)
                return;

        pid = fork ();
        if (pid == -1)
                return;

        if (pid == 0) {
                const char *argv[] = { FUSERMOUNT_PROG, "-u", "-q", "-z",
                                       "--", mountpoint, NULL };

                execvp (FUSERMOUNT_PROG, (char **)argv);
                _exit (1);
        }
        waitpid (pid, NULL, 0);
}
#endif

/*
 * Functions below are loosely modelled after similar functions of libfuse
 */

#ifndef FUSE_UTIL
static int
fuse_mount_sys (const char *mountpoint, char *fsname, char *mnt_param)
{
        int fd = -1, ret = -1;
        unsigned mounted = 0;
        char *mnt_param_mnt = NULL;
        char *fstype = "fuse.glusterfs";
        char *source = fsname;

        fd = open ("/dev/fuse", O_RDWR);
        if (fd == -1) {
                GFFUSE_LOGERR ("cannot open /dev/fuse (%s)", strerror (errno));

                return -1;
        }

        ret = asprintf (&mnt_param_mnt,
                        "%s,fd=%i,rootmode=%o,user_id=%i,group_id=%i",
                        mnt_param, fd, S_IFDIR, getuid (), getgid ());
        if (ret == -1) {
                GFFUSE_LOGERR ("Out of memory");

                goto out;
        }
        ret = mount (source, mountpoint, fstype, 0,
                     mnt_param_mnt);
        if (ret == -1 && errno == ENODEV) {
                /* fs subtype support was added by 79c0b2df aka
                   v2.6.21-3159-g79c0b2d. Probably we have an
                   older kernel ... */
                fstype = "fuse";
                ret = asprintf (&source, "glusterfs#%s", fsname);
                if (ret == -1) {
                        GFFUSE_LOGERR ("Out of memory");

                        goto out;
                }
                ret = mount (source, mountpoint, fstype, 0,
                             mnt_param_mnt);
        }
        if (ret == -1)
                goto out;
        else
                mounted = 1;

        if (geteuid () == 0) {
                char *newmnt = fuse_mnt_resolve_path ("fuse", mountpoint);

                if (!newmnt) {
                        ret = -1;

                        goto out;
                }

                ret = fuse_mnt_add_mount ("fuse", source, newmnt, fstype,
                                          mnt_param);
                FREE (newmnt);
                if (ret == -1) {
                        GFFUSE_LOGERR ("failed to add mtab entry");

                        goto out;
                }
        }

 out:
        if (ret == -1) {
                if (mounted)
                        umount2 (mountpoint, 2); /* lazy umount */
                close (fd);
                fd = -1;
        }
        FREE (mnt_param_mnt);
        if (source != fsname)
                FREE (source);
        return fd;
}

int
gf_fuse_mount (const char *mountpoint, char *fsname, char *mnt_param)
{
        int fd = -1, rv = -1;
        char *fm_mnt_params = NULL, *p = NULL;

        fd = fuse_mount_sys (mountpoint, fsname, mnt_param);
        if (fd == -1) {
                gf_log ("glusterfs-fuse", GF_LOG_NORMAL,
                        "direct mount failed (%s), "
                        "retry to mount via fusermount",
                        strerror (errno));

                rv = asprintf (&fm_mnt_params,
                               "%s,fsname=%s,nonempty,subtype=glusterfs",
                               mnt_param, fsname);

                if (rv == -1) {
                        GFFUSE_LOGERR ("Out of memory");

                        return -1;
                }

                fd = fuse_mount_fusermount (mountpoint, fm_mnt_params);
                if (fd == -1) {
                        p = fm_mnt_params + strlen (fm_mnt_params);
                        while (*--p != ',');
                        *p = '\0';

                        fd = fuse_mount_fusermount (mountpoint, fm_mnt_params);
                }

                FREE (fm_mnt_params);

                if (fd == -1)
                       GFFUSE_LOGERR ("mount failed");
        }

        return fd;
}
#endif