pnullfs.c revision 1.2
1/*	$NetBSD: pnullfs.c,v 1.2 2007/01/11 11:52:53 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 * 3. The name of the company nor the name of the author may be used to
15 *    endorse or promote products derived from this software without specific
16 *    prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*
32 * pnullfs: puffs nullfs example
33 */
34
35#include <err.h>
36#include <puffs.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40
41PUFFSOP_PROTOS(puffs_null) /* XXX */
42
43static void usage(void);
44
45static void
46usage()
47{
48
49	errx(1, "usage: %s [-o mntopts]�nullpath mountpath", getprogname());
50}
51
52int
53main(int argc, char *argv[])
54{
55	struct puffs_usermount *pu;
56	struct puffs_ops *pops;
57	struct statvfs svfsb;
58	struct stat sb;
59	mntoptparse_t mp;
60	int mntflags, pflags, lflags;
61	int ch;
62
63	setprogname(argv[0]);
64
65	if (argc < 3)
66		usage();
67
68	pflags = lflags = mntflags = 0;
69	while ((ch = getopt(argc, argv, "o:")) != -1) {
70		switch (ch) {
71		case 'o':
72			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
73			if (mp == NULL)
74				err(1, "getmntopts");
75			freemntopts(mp);
76			break;
77		}
78	}
79	pflags |= PUFFS_FLAG_BUILDPATH;
80	argv += optind;
81	argc -= optind;
82
83	if (pflags & PUFFS_FLAG_OPDUMP)
84		lflags = PUFFSLOOP_NODAEMON;
85
86	if (argc != 2)
87		usage();
88
89	PUFFSOP_INIT(pops);
90
91	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
92	PUFFSOP_SETFSNOP(pops, unmount);
93	PUFFSOP_SETFSNOP(pops, sync);
94
95	PUFFSOP_SET(pops, puffs_null, node, lookup);
96	PUFFSOP_SET(pops, puffs_null, node, create);
97	PUFFSOP_SET(pops, puffs_null, node, mknod);
98	PUFFSOP_SET(pops, puffs_null, node, getattr);
99	PUFFSOP_SET(pops, puffs_null, node, setattr);
100	PUFFSOP_SET(pops, puffs_null, node, remove);
101	PUFFSOP_SET(pops, puffs_null, node, link);
102	PUFFSOP_SET(pops, puffs_null, node, rename);
103	PUFFSOP_SET(pops, puffs_null, node, mkdir);
104	PUFFSOP_SET(pops, puffs_null, node, rmdir);
105	PUFFSOP_SET(pops, puffs_null, node, symlink);
106	PUFFSOP_SET(pops, puffs_null, node, readlink);
107	PUFFSOP_SET(pops, puffs_null, node, readdir);
108	PUFFSOP_SET(pops, puffs_null, node, read);
109	PUFFSOP_SET(pops, puffs_null, node, write);
110
111	if ((pu = puffs_mount(pops, argv[1], mntflags, "pnullfs", NULL,
112	    pflags, 0)) == NULL)
113		err(1, "mount");
114
115	if (statvfs(argv[0], &svfsb) == -1)
116		err(1, "statvfs %s", argv[0]);
117
118	pu->pu_pn_root = puffs_pn_new(pu, NULL);
119	if (pu->pu_pn_root == NULL)
120		err(1, "puffs_pn_new");
121	puffs_setrootpath(pu, argv[0]);
122	if (stat(argv[0], &sb) == -1)
123		err(1, "stat %s", argv[0]);
124	puffs_stat2vattr(&pu->pu_pn_root->pn_va, &sb);
125
126	if (puffs_start(pu, pu->pu_pn_root, &svfsb) == -1)
127		err(1, "puffs_start");
128
129	if (puffs_mainloop(pu, lflags) == -1)
130		err(1, "mainloop");
131
132	return 0;
133}
134