pnullfs.c revision 1.11
1/*	$NetBSD: pnullfs.c,v 1.11 2007/06/24 18:37:40 pooka Exp $	*/
2
3/*
4 * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * pnullfs: puffs nullfs example
30 */
31
32#include <err.h>
33#include <puffs.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37
38PUFFSOP_PROTOS(puffs_null) /* XXX */
39
40static void usage(void);
41
42static void
43usage()
44{
45
46	errx(1, "usage: %s [-s]�[-o mntopts]�nullpath mountpath",
47	    getprogname());
48}
49
50int
51main(int argc, char *argv[])
52{
53	struct puffs_usermount *pu;
54	struct puffs_ops *pops;
55	struct puffs_pathobj *po_root;
56	struct puffs_node *pn_root;
57	struct stat sb;
58	mntoptparse_t mp;
59	int mntflags, pflags, lflags;
60	int ch;
61
62	setprogname(argv[0]);
63
64	if (argc < 3)
65		usage();
66
67	pflags = lflags = mntflags = 0;
68	while ((ch = getopt(argc, argv, "o:s")) != -1) {
69		switch (ch) {
70		case 'o':
71			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
72			if (mp == NULL)
73				err(1, "getmntopts");
74			freemntopts(mp);
75			break;
76		case 's':
77			lflags |= PUFFSLOOP_NODAEMON;
78			break;
79		}
80	}
81	pflags |= PUFFS_FLAG_BUILDPATH;
82	argv += optind;
83	argc -= optind;
84
85	if (pflags & PUFFS_FLAG_OPDUMP)
86		lflags = PUFFSLOOP_NODAEMON;
87
88	if (argc != 2)
89		usage();
90
91	if (lstat(argv[0], &sb) == -1)
92		err(1, "stat %s", argv[0]);
93	if ((sb.st_mode & S_IFDIR) == 0)
94		errx(1, "%s is not a directory", argv[0]);
95
96	PUFFSOP_INIT(pops);
97
98	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
99	PUFFSOP_SETFSNOP(pops, unmount);
100	PUFFSOP_SETFSNOP(pops, sync);
101
102	PUFFSOP_SET(pops, puffs_null, node, lookup);
103	PUFFSOP_SET(pops, puffs_null, node, create);
104	PUFFSOP_SET(pops, puffs_null, node, mknod);
105	PUFFSOP_SET(pops, puffs_null, node, getattr);
106	PUFFSOP_SET(pops, puffs_null, node, setattr);
107	PUFFSOP_SET(pops, puffs_null, node, fsync);
108	PUFFSOP_SET(pops, puffs_null, node, remove);
109	PUFFSOP_SET(pops, puffs_null, node, link);
110	PUFFSOP_SET(pops, puffs_null, node, rename);
111	PUFFSOP_SET(pops, puffs_null, node, mkdir);
112	PUFFSOP_SET(pops, puffs_null, node, rmdir);
113	PUFFSOP_SET(pops, puffs_null, node, symlink);
114	PUFFSOP_SET(pops, puffs_null, node, readlink);
115	PUFFSOP_SET(pops, puffs_null, node, readdir);
116	PUFFSOP_SET(pops, puffs_null, node, read);
117	PUFFSOP_SET(pops, puffs_null, node, write);
118	PUFFSOP_SET(pops, puffs_null, node, reclaim);
119
120	if ((pu = puffs_init(pops, "pnullfs", NULL, pflags)) == NULL)
121		err(1, "init");
122
123	pn_root = puffs_pn_new(pu, NULL);
124	if (pn_root == NULL)
125		err(1, "puffs_pn_new");
126	puffs_setroot(pu, pn_root);
127
128	po_root = puffs_getrootpathobj(pu);
129	if (po_root == NULL)
130		err(1, "getrootpathobj");
131	po_root->po_path = argv[0];
132	po_root->po_len = strlen(argv[0]);
133	puffs_stat2vattr(&pn_root->pn_va, &sb);
134
135	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
136		err(1, "puffs_mount");
137
138	if (puffs_mainloop(pu, lflags) == -1)
139		err(1, "mainloop");
140
141	return 0;
142}
143