Home | History | Annotate | Line # | Download | only in pgfs
mount.c revision 1.1
      1  1.1  yamt /*	$NetBSD: mount.c,v 1.1 2011/10/12 01:05:00 yamt Exp $	*/
      2  1.1  yamt 
      3  1.1  yamt /*-
      4  1.1  yamt  * Copyright (c)2010,2011 YAMAMOTO Takashi,
      5  1.1  yamt  * All rights reserved.
      6  1.1  yamt  *
      7  1.1  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.1  yamt  * modification, are permitted provided that the following conditions
      9  1.1  yamt  * are met:
     10  1.1  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.1  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.1  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.1  yamt  *
     16  1.1  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  yamt  * SUCH DAMAGE.
     27  1.1  yamt  */
     28  1.1  yamt 
     29  1.1  yamt #include <sys/cdefs.h>
     30  1.1  yamt #ifndef lint
     31  1.1  yamt __RCSID("$NetBSD: mount.c,v 1.1 2011/10/12 01:05:00 yamt Exp $");
     32  1.1  yamt #endif /* not lint */
     33  1.1  yamt 
     34  1.1  yamt #include <err.h>
     35  1.1  yamt #include <errno.h>
     36  1.1  yamt #include <mntopts.h>
     37  1.1  yamt #include <paths.h>
     38  1.1  yamt #include <puffs.h>
     39  1.1  yamt #include <stdbool.h>
     40  1.1  yamt #include <stdlib.h>
     41  1.1  yamt #include <unistd.h>
     42  1.1  yamt 
     43  1.1  yamt #include "pgfs.h"
     44  1.1  yamt #include "pgfs_db.h"
     45  1.1  yamt 
     46  1.1  yamt #define	PGFS_MNT_ALT_DUMMY	1
     47  1.1  yamt #define	PGFS_MNT_ALT_DEBUG	2
     48  1.1  yamt 
     49  1.1  yamt int
     50  1.1  yamt main(int argc, char *argv[])
     51  1.1  yamt {
     52  1.1  yamt 	extern char *optarg;
     53  1.1  yamt 	extern int optind;
     54  1.1  yamt 	mntoptparse_t mp;
     55  1.1  yamt 	struct puffs_usermount *pu;
     56  1.1  yamt 	struct puffs_ops *pops;
     57  1.1  yamt 	int mntflags;
     58  1.1  yamt 	int altmntflags;
     59  1.1  yamt 	int ch;
     60  1.1  yamt 	int error;
     61  1.1  yamt 	const char *dbname = NULL;
     62  1.1  yamt 	const char *dbuser = NULL;
     63  1.1  yamt 	static const struct mntopt mopts[] = {
     64  1.1  yamt 		MOPT_STDOPTS,
     65  1.1  yamt 		MOPT_SYNC,
     66  1.1  yamt 		{ .m_option = "dbname", .m_inverse = 0,
     67  1.1  yamt 		  .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
     68  1.1  yamt 		{ .m_option = "dbuser", .m_inverse = 0,
     69  1.1  yamt 		  .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
     70  1.1  yamt 		{ .m_option = "debug", .m_inverse = 0,
     71  1.1  yamt 		  .m_flag = PGFS_MNT_ALT_DEBUG, .m_altloc = 1, },
     72  1.1  yamt 		{ .m_option = "nconn", .m_inverse = 0,
     73  1.1  yamt 		  .m_flag = PGFS_MNT_ALT_DUMMY, .m_altloc = 1, },
     74  1.1  yamt 		MOPT_NULL,
     75  1.1  yamt 	};
     76  1.1  yamt 	uint32_t pflags = PUFFS_KFLAG_NOCACHE_NAME|PUFFS_KFLAG_WTCACHE;
     77  1.1  yamt 	unsigned int nconn = 8;
     78  1.1  yamt 	bool debug = false;
     79  1.1  yamt 	bool dosync;
     80  1.1  yamt 
     81  1.1  yamt 	mntflags = 0;
     82  1.1  yamt 	altmntflags = 0;
     83  1.1  yamt 	while ((ch = getopt(argc, argv, "o:")) != -1) {
     84  1.1  yamt 		long v;
     85  1.1  yamt 
     86  1.1  yamt 		switch (ch) {
     87  1.1  yamt 		case 'o':
     88  1.1  yamt 			mp = getmntopts(optarg, mopts, &mntflags,
     89  1.1  yamt 			    &altmntflags);
     90  1.1  yamt 			if (mp == NULL) {
     91  1.1  yamt 				err(EXIT_FAILURE, "getmntopts");
     92  1.1  yamt 			}
     93  1.1  yamt 			getmnt_silent = 1; /* XXX silly api */
     94  1.1  yamt 			dbname = getmntoptstr(mp, "dbname");
     95  1.1  yamt 			dbuser = getmntoptstr(mp, "dbuser");
     96  1.1  yamt 			v = getmntoptnum(mp, "nconn");
     97  1.1  yamt 			getmnt_silent = 0;
     98  1.1  yamt 			if (v != -1) {
     99  1.1  yamt 				nconn = v;
    100  1.1  yamt 			}
    101  1.1  yamt 			if ((altmntflags & PGFS_MNT_ALT_DEBUG) != 0) {
    102  1.1  yamt 				debug = true;
    103  1.1  yamt 			}
    104  1.1  yamt 			freemntopts(mp);
    105  1.1  yamt 			break;
    106  1.1  yamt 		}
    107  1.1  yamt 	}
    108  1.1  yamt 	argc -= optind;
    109  1.1  yamt 	argv += optind;
    110  1.1  yamt 
    111  1.1  yamt 	PUFFSOP_INIT(pops);
    112  1.1  yamt 	PUFFSOP_SETFSNOP(pops, unmount);
    113  1.1  yamt 	PUFFSOP_SETFSNOP(pops, sync);
    114  1.1  yamt 	PUFFSOP_SET(pops, pgfs, fs, statvfs);
    115  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, readdir);
    116  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, getattr);
    117  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, lookup);
    118  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, mkdir);
    119  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, create);
    120  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, read);
    121  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, write);
    122  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, link);
    123  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, remove);
    124  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, rmdir);
    125  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, inactive);
    126  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, setattr);
    127  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, rename);
    128  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, symlink);
    129  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, readlink);
    130  1.1  yamt 	PUFFSOP_SET(pops, pgfs, node, access);
    131  1.1  yamt 	dosync = (mntflags & MNT_SYNCHRONOUS) != 0;
    132  1.1  yamt 	if (!dosync) {
    133  1.1  yamt 		PUFFSOP_SET(pops, pgfs, node, fsync);
    134  1.1  yamt 	}
    135  1.1  yamt 	if (debug) {
    136  1.1  yamt 		pflags |= PUFFS_FLAG_OPDUMP;
    137  1.1  yamt 	}
    138  1.1  yamt 	pu = puffs_init(pops, _PATH_PUFFS, "pgfs", NULL, pflags);
    139  1.1  yamt 	if (pu == NULL) {
    140  1.1  yamt 		err(EXIT_FAILURE, "puffs_init");
    141  1.1  yamt 	}
    142  1.1  yamt 	error = pgfs_connectdb(pu, dbname, dbuser, debug, dosync, nconn);
    143  1.1  yamt 	if (error != 0) {
    144  1.1  yamt 		errno = error;
    145  1.1  yamt 		err(EXIT_FAILURE, "pgfs_connectdb");
    146  1.1  yamt 	}
    147  1.1  yamt 	if (!debug) {
    148  1.1  yamt 		if (puffs_daemon(pu, 1, 1)) {
    149  1.1  yamt 			err(EXIT_FAILURE, "puffs_daemon");
    150  1.1  yamt 		}
    151  1.1  yamt 	}
    152  1.1  yamt 	if (puffs_mount(pu, argv[1], mntflags, pgfs_root_cookie()) == -1) {
    153  1.1  yamt 		err(EXIT_FAILURE, "puffs_mount");
    154  1.1  yamt 	}
    155  1.1  yamt 	if (!debug) {
    156  1.1  yamt 		char tmpl[] = "/tmp/pgfs.XXXXXXXX";
    157  1.1  yamt 		const char *path;
    158  1.1  yamt 		int fd;
    159  1.1  yamt 		int ret;
    160  1.1  yamt 
    161  1.1  yamt 		path = mkdtemp(tmpl);
    162  1.1  yamt 		if (path == NULL) {
    163  1.1  yamt 			err(EXIT_FAILURE, "mkdtemp");
    164  1.1  yamt 		}
    165  1.1  yamt 		fd = open(path, O_RDONLY | O_DIRECTORY);
    166  1.1  yamt 		if (fd == -1) {
    167  1.1  yamt 			err(EXIT_FAILURE, "open %s", path);
    168  1.1  yamt 		}
    169  1.1  yamt 		ret = rmdir(path);
    170  1.1  yamt 		if (ret != 0) {
    171  1.1  yamt 			err(EXIT_FAILURE, "rmdir %s", path);
    172  1.1  yamt 		}
    173  1.1  yamt 		ret = fchroot(fd);
    174  1.1  yamt 		if (ret != 0) {
    175  1.1  yamt 			err(EXIT_FAILURE, "fchroot");
    176  1.1  yamt 		}
    177  1.1  yamt 		ret = close(fd);
    178  1.1  yamt 		if (ret != 0) {
    179  1.1  yamt 			err(EXIT_FAILURE, "close");
    180  1.1  yamt 		}
    181  1.1  yamt 	}
    182  1.1  yamt 	if (puffs_mainloop(pu) == -1) {
    183  1.1  yamt 		err(EXIT_FAILURE, "puffs_mainloop");
    184  1.1  yamt 	}
    185  1.1  yamt 	exit(EXIT_SUCCESS);
    186  1.1  yamt }
    187