xref: /minix3/tests/fs/puffs/h_dtfs/dtfs.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: dtfs.c,v 1.2 2010/07/21 06:58:25 pooka Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
5*11be35a1SLionel Sambuc  *
6*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
8*11be35a1SLionel Sambuc  * are met:
9*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
10*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
11*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
12*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
13*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
14*11be35a1SLionel Sambuc  *
15*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16*11be35a1SLionel Sambuc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17*11be35a1SLionel Sambuc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18*11be35a1SLionel Sambuc  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*11be35a1SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21*11be35a1SLionel Sambuc  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*11be35a1SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*11be35a1SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*11be35a1SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*11be35a1SLionel Sambuc  * SUCH DAMAGE.
26*11be35a1SLionel Sambuc  */
27*11be35a1SLionel Sambuc 
28*11be35a1SLionel Sambuc /*
29*11be35a1SLionel Sambuc  * Delectable Test File System: a simple in-memory file system which
30*11be35a1SLionel Sambuc  * demonstrates the use of puffs.
31*11be35a1SLionel Sambuc  * (a.k.a. Detrempe FS ...)
32*11be35a1SLionel Sambuc  */
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <sys/types.h>
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #include <err.h>
37*11be35a1SLionel Sambuc #include <mntopts.h>
38*11be35a1SLionel Sambuc #include <paths.h>
39*11be35a1SLionel Sambuc #include <puffs.h>
40*11be35a1SLionel Sambuc #include <signal.h>
41*11be35a1SLionel Sambuc #include <stdio.h>
42*11be35a1SLionel Sambuc #include <stdlib.h>
43*11be35a1SLionel Sambuc #include <string.h>
44*11be35a1SLionel Sambuc #include <unistd.h>
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc #include "dtfs.h"
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc #ifdef DEEP_ROOTED_CLUE
49*11be35a1SLionel Sambuc #define FSNAME "detrempe"
50*11be35a1SLionel Sambuc #else
51*11be35a1SLionel Sambuc #define FSNAME "dt"
52*11be35a1SLionel Sambuc #endif
53*11be35a1SLionel Sambuc #define MAXREQMAGIC -37
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc static struct puffs_usermount *gpu;
56*11be35a1SLionel Sambuc static struct dtfs_mount gdtm;
57*11be35a1SLionel Sambuc int dynamicfh;
58*11be35a1SLionel Sambuc int straightflush;
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc static void usage(void);
61*11be35a1SLionel Sambuc 
62*11be35a1SLionel Sambuc static void
usage()63*11be35a1SLionel Sambuc usage()
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc 
66*11be35a1SLionel Sambuc 	fprintf(stderr, "usage: %s [-bsdftl] [-c hashbuckets] [-m maxreqsize] "
67*11be35a1SLionel Sambuc 	    "[-n typename]\n       [-o mntopt] [-o puffsopt] [-p prot] "
68*11be35a1SLionel Sambuc 	    "[-r rootnodetype]\n       detrempe /mountpoint\n", getprogname());
69*11be35a1SLionel Sambuc 	exit(1);
70*11be35a1SLionel Sambuc }
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc static void
wipe_the_sleep_out_of_my_eyes(int v)73*11be35a1SLionel Sambuc wipe_the_sleep_out_of_my_eyes(int v)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 	gdtm.dtm_needwakeup++;
77*11be35a1SLionel Sambuc }
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc static void
loopfun(struct puffs_usermount * pu)80*11be35a1SLionel Sambuc loopfun(struct puffs_usermount *pu)
81*11be35a1SLionel Sambuc {
82*11be35a1SLionel Sambuc 	struct dtfs_mount *dtm = puffs_getspecific(pu);
83*11be35a1SLionel Sambuc 	struct dtfs_poll *dp;
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc 	while (dtm->dtm_needwakeup) {
86*11be35a1SLionel Sambuc 		dtm->dtm_needwakeup--;
87*11be35a1SLionel Sambuc 		dp = LIST_FIRST(&dtm->dtm_pollent);
88*11be35a1SLionel Sambuc 		if (dp == NULL)
89*11be35a1SLionel Sambuc 			return;
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc 		LIST_REMOVE(dp, dp_entries);
92*11be35a1SLionel Sambuc 		puffs_cc_continue(dp->dp_pcc);
93*11be35a1SLionel Sambuc 	}
94*11be35a1SLionel Sambuc }
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc int
main(int argc,char * argv[])97*11be35a1SLionel Sambuc main(int argc, char *argv[])
98*11be35a1SLionel Sambuc {
99*11be35a1SLionel Sambuc 	extern char *optarg;
100*11be35a1SLionel Sambuc 	extern int optind;
101*11be35a1SLionel Sambuc 	struct puffs_usermount *pu;
102*11be35a1SLionel Sambuc 	struct puffs_pathobj *po_root;
103*11be35a1SLionel Sambuc 	struct puffs_ops *pops;
104*11be35a1SLionel Sambuc 	struct timespec ts;
105*11be35a1SLionel Sambuc 	const char *typename;
106*11be35a1SLionel Sambuc 	char *rtstr;
107*11be35a1SLionel Sambuc 	mntoptparse_t mp;
108*11be35a1SLionel Sambuc 	int pflags, detach, mntflags;
109*11be35a1SLionel Sambuc 	int ch;
110*11be35a1SLionel Sambuc 	int khashbuckets;
111*11be35a1SLionel Sambuc 	int maxreqsize;
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 	setprogname(argv[0]);
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc 	rtstr = NULL;
116*11be35a1SLionel Sambuc 	detach = 1;
117*11be35a1SLionel Sambuc 	mntflags = 0;
118*11be35a1SLionel Sambuc 	khashbuckets = 256;
119*11be35a1SLionel Sambuc 	pflags = PUFFS_KFLAG_IAONDEMAND;
120*11be35a1SLionel Sambuc 	typename = FSNAME;
121*11be35a1SLionel Sambuc 	maxreqsize = MAXREQMAGIC;
122*11be35a1SLionel Sambuc 	gdtm.dtm_allowprot = VM_PROT_ALL;
123*11be35a1SLionel Sambuc 	while ((ch = getopt(argc, argv, "bc:dfilm:n:o:p:r:st")) != -1) {
124*11be35a1SLionel Sambuc 		switch (ch) {
125*11be35a1SLionel Sambuc 		case 'b': /* build paths, for debugging the feature */
126*11be35a1SLionel Sambuc 			pflags |= PUFFS_FLAG_BUILDPATH;
127*11be35a1SLionel Sambuc 			break;
128*11be35a1SLionel Sambuc 		case 'c':
129*11be35a1SLionel Sambuc 			khashbuckets = atoi(optarg);
130*11be35a1SLionel Sambuc 			break;
131*11be35a1SLionel Sambuc 		case 'd':
132*11be35a1SLionel Sambuc 			dynamicfh = 1;
133*11be35a1SLionel Sambuc 			break;
134*11be35a1SLionel Sambuc 		case 'f':
135*11be35a1SLionel Sambuc 			pflags |= PUFFS_KFLAG_LOOKUP_FULLPNBUF;
136*11be35a1SLionel Sambuc 			break;
137*11be35a1SLionel Sambuc 		case 'i':
138*11be35a1SLionel Sambuc 			pflags &= ~PUFFS_KFLAG_IAONDEMAND;
139*11be35a1SLionel Sambuc 			break;
140*11be35a1SLionel Sambuc 		case 'l':
141*11be35a1SLionel Sambuc 			straightflush = 1;
142*11be35a1SLionel Sambuc 			break;
143*11be35a1SLionel Sambuc 		case 'm':
144*11be35a1SLionel Sambuc 			maxreqsize = atoi(optarg);
145*11be35a1SLionel Sambuc 			break;
146*11be35a1SLionel Sambuc 		case 'n':
147*11be35a1SLionel Sambuc 			typename = optarg;
148*11be35a1SLionel Sambuc 			break;
149*11be35a1SLionel Sambuc 		case 'o':
150*11be35a1SLionel Sambuc 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
151*11be35a1SLionel Sambuc 			if (mp == NULL)
152*11be35a1SLionel Sambuc 				err(1, "getmntopts");
153*11be35a1SLionel Sambuc 			freemntopts(mp);
154*11be35a1SLionel Sambuc 			break;
155*11be35a1SLionel Sambuc 		case 'p':
156*11be35a1SLionel Sambuc 			gdtm.dtm_allowprot = atoi(optarg);
157*11be35a1SLionel Sambuc 			if ((gdtm.dtm_allowprot | VM_PROT_ALL) != VM_PROT_ALL)
158*11be35a1SLionel Sambuc 				usage();
159*11be35a1SLionel Sambuc 			break;
160*11be35a1SLionel Sambuc 		case 'r':
161*11be35a1SLionel Sambuc 			rtstr = optarg;
162*11be35a1SLionel Sambuc 			break;
163*11be35a1SLionel Sambuc 		case 's': /* stay on top */
164*11be35a1SLionel Sambuc 			detach = 0;
165*11be35a1SLionel Sambuc 			break;
166*11be35a1SLionel Sambuc 		case 't':
167*11be35a1SLionel Sambuc 			pflags |= PUFFS_KFLAG_WTCACHE;
168*11be35a1SLionel Sambuc 			break;
169*11be35a1SLionel Sambuc 		default:
170*11be35a1SLionel Sambuc 			usage();
171*11be35a1SLionel Sambuc 			/*NOTREACHED*/
172*11be35a1SLionel Sambuc 		}
173*11be35a1SLionel Sambuc 	}
174*11be35a1SLionel Sambuc 	if (pflags & PUFFS_FLAG_OPDUMP)
175*11be35a1SLionel Sambuc 		detach = 0;
176*11be35a1SLionel Sambuc 	argc -= optind;
177*11be35a1SLionel Sambuc 	argv += optind;
178*11be35a1SLionel Sambuc 
179*11be35a1SLionel Sambuc 	if (argc != 2)
180*11be35a1SLionel Sambuc 		usage();
181*11be35a1SLionel Sambuc 
182*11be35a1SLionel Sambuc 	PUFFSOP_INIT(pops);
183*11be35a1SLionel Sambuc 
184*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, fs, statvfs);
185*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, fs, unmount);
186*11be35a1SLionel Sambuc 	PUFFSOP_SETFSNOP(pops, sync);
187*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, fs, fhtonode);
188*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, fs, nodetofh);
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, lookup);
191*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, access);
192*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, puffs_genfs, node, getattr);
193*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, setattr);
194*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, create);
195*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, remove);
196*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, readdir);
197*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, poll);
198*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, mmap);
199*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, mkdir);
200*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, rmdir);
201*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, rename);
202*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, read);
203*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, write);
204*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, link);
205*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, symlink);
206*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, readlink);
207*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, mknod);
208*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, inactive);
209*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, pathconf);
210*11be35a1SLionel Sambuc 	PUFFSOP_SET(pops, dtfs, node, reclaim);
211*11be35a1SLionel Sambuc 
212*11be35a1SLionel Sambuc 	srandom(time(NULL)); /* for random generation numbers */
213*11be35a1SLionel Sambuc 
214*11be35a1SLionel Sambuc 	pu = puffs_init(pops, _PATH_PUFFS, typename, &gdtm, pflags);
215*11be35a1SLionel Sambuc 	if (pu == NULL)
216*11be35a1SLionel Sambuc 		err(1, "init");
217*11be35a1SLionel Sambuc 	gpu = pu;
218*11be35a1SLionel Sambuc 
219*11be35a1SLionel Sambuc 	puffs_setfhsize(pu, sizeof(struct dtfs_fid),
220*11be35a1SLionel Sambuc 	    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3
221*11be35a1SLionel Sambuc 	    | (dynamicfh ? PUFFS_FHFLAG_DYNAMIC : 0));
222*11be35a1SLionel Sambuc 	puffs_setncookiehash(pu, khashbuckets);
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc 	if (signal(SIGALRM, wipe_the_sleep_out_of_my_eyes) == SIG_ERR)
225*11be35a1SLionel Sambuc 		warn("cannot set alarm sighandler");
226*11be35a1SLionel Sambuc 
227*11be35a1SLionel Sambuc 	/* init */
228*11be35a1SLionel Sambuc 	if (dtfs_domount(pu, rtstr) != 0)
229*11be35a1SLionel Sambuc 		errx(1, "dtfs_domount failed");
230*11be35a1SLionel Sambuc 
231*11be35a1SLionel Sambuc 	po_root = puffs_getrootpathobj(pu);
232*11be35a1SLionel Sambuc 	po_root->po_path = argv[0];
233*11be35a1SLionel Sambuc 	po_root->po_len = strlen(argv[0]);
234*11be35a1SLionel Sambuc 
235*11be35a1SLionel Sambuc 	/* often enough for testing poll */
236*11be35a1SLionel Sambuc 	ts.tv_sec = 1;
237*11be35a1SLionel Sambuc 	ts.tv_nsec = 0;
238*11be35a1SLionel Sambuc 	puffs_ml_setloopfn(pu, loopfun);
239*11be35a1SLionel Sambuc 	puffs_ml_settimeout(pu, &ts);
240*11be35a1SLionel Sambuc 
241*11be35a1SLionel Sambuc 	if (maxreqsize != MAXREQMAGIC)
242*11be35a1SLionel Sambuc 		puffs_setmaxreqlen(pu, maxreqsize);
243*11be35a1SLionel Sambuc 
244*11be35a1SLionel Sambuc 	puffs_set_errnotify(pu, puffs_kernerr_abort);
245*11be35a1SLionel Sambuc 	if (detach)
246*11be35a1SLionel Sambuc 		if (puffs_daemon(pu, 1, 1) == -1)
247*11be35a1SLionel Sambuc 			err(1, "puffs_daemon");
248*11be35a1SLionel Sambuc 
249*11be35a1SLionel Sambuc 	if (puffs_mount(pu,  argv[1], mntflags, puffs_getroot(pu)) == -1)
250*11be35a1SLionel Sambuc 		err(1, "mount");
251*11be35a1SLionel Sambuc 	if (puffs_mainloop(pu) == -1)
252*11be35a1SLionel Sambuc 		err(1, "mainloop");
253*11be35a1SLionel Sambuc 
254*11be35a1SLionel Sambuc 	return 0;
255*11be35a1SLionel Sambuc }
256