pnullfs.c revision 1.9
1/*	$NetBSD: pnullfs.c,v 1.9 2007/05/17 14:13:05 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 [-s]�[-o mntopts]�nullpath mountpath",
50	    getprogname());
51}
52
53int
54main(int argc, char *argv[])
55{
56	struct puffs_usermount *pu;
57	struct puffs_ops *pops;
58	struct puffs_pathobj *po_root;
59	struct puffs_node *pn_root;
60	struct stat sb;
61	mntoptparse_t mp;
62	int mntflags, pflags, lflags;
63	int ch;
64
65	setprogname(argv[0]);
66
67	if (argc < 3)
68		usage();
69
70	pflags = lflags = mntflags = 0;
71	while ((ch = getopt(argc, argv, "o:s")) != -1) {
72		switch (ch) {
73		case 'o':
74			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
75			if (mp == NULL)
76				err(1, "getmntopts");
77			freemntopts(mp);
78			break;
79		case 's':
80			lflags |= PUFFSLOOP_NODAEMON;
81			break;
82		}
83	}
84	pflags |= PUFFS_FLAG_BUILDPATH;
85	argv += optind;
86	argc -= optind;
87
88	if (pflags & PUFFS_FLAG_OPDUMP)
89		lflags = PUFFSLOOP_NODAEMON;
90
91	if (argc != 2)
92		usage();
93
94	if (lstat(argv[0], &sb) == -1)
95		err(1, "stat %s", argv[0]);
96	if ((sb.st_mode & S_IFDIR) == 0)
97		errx(1, "%s is not a directory", argv[0]);
98
99	PUFFSOP_INIT(pops);
100
101	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
102	PUFFSOP_SETFSNOP(pops, unmount);
103	PUFFSOP_SETFSNOP(pops, sync);
104
105	PUFFSOP_SET(pops, puffs_null, node, lookup);
106	PUFFSOP_SET(pops, puffs_null, node, create);
107	PUFFSOP_SET(pops, puffs_null, node, mknod);
108	PUFFSOP_SET(pops, puffs_null, node, getattr);
109	PUFFSOP_SET(pops, puffs_null, node, setattr);
110	PUFFSOP_SET(pops, puffs_null, node, fsync);
111	PUFFSOP_SET(pops, puffs_null, node, remove);
112	PUFFSOP_SET(pops, puffs_null, node, link);
113	PUFFSOP_SET(pops, puffs_null, node, rename);
114	PUFFSOP_SET(pops, puffs_null, node, mkdir);
115	PUFFSOP_SET(pops, puffs_null, node, rmdir);
116	PUFFSOP_SET(pops, puffs_null, node, symlink);
117	PUFFSOP_SET(pops, puffs_null, node, readlink);
118	PUFFSOP_SET(pops, puffs_null, node, readdir);
119	PUFFSOP_SET(pops, puffs_null, node, read);
120	PUFFSOP_SET(pops, puffs_null, node, write);
121	PUFFSOP_SET(pops, puffs_null, node, reclaim);
122
123	if ((pu = puffs_init(pops, "pnullfs", NULL, pflags)) == NULL)
124		err(1, "init");
125
126	pn_root = puffs_pn_new(pu, NULL);
127	if (pn_root == NULL)
128		err(1, "puffs_pn_new");
129	puffs_setroot(pu, pn_root);
130
131	po_root = puffs_getrootpathobj(pu);
132	if (po_root == NULL)
133		err(1, "getrootpathobj");
134	po_root->po_path = argv[0];
135	po_root->po_len = strlen(argv[0]);
136	puffs_stat2vattr(&pn_root->pn_va, &sb);
137
138	if (puffs_mount(pu, argv[0], mntflags, pn_root) == -1)
139		err(1, "puffs_mount");
140
141	if (puffs_mainloop(pu, lflags) == -1)
142		err(1, "mainloop");
143
144	return 0;
145}
146