nfsiod.c (d476a036e28a06aadb6a33050af920f3f99fbb8c) nfsiod.c (51c20097df1a6a24511dcf0b2fdcd294e0bafd1a)
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 40 unchanged lines hidden (view full) ---

49#endif not lint
50
51#include <sys/param.h>
52#include <sys/syslog.h>
53#include <sys/wait.h>
54#include <sys/mount.h>
55#include <sys/time.h>
56
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 40 unchanged lines hidden (view full) ---

49#endif not lint
50
51#include <sys/param.h>
52#include <sys/syslog.h>
53#include <sys/wait.h>
54#include <sys/mount.h>
55#include <sys/time.h>
56
57#include <nfs/rpcv2.h>
58#include <nfs/nfs.h>
59
60#include <err.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <unistd.h>
64
57#include <err.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <unistd.h>
61
65/* Global defs */
66#ifdef DEBUG
67int debug = 1;
68#else
69int debug = 0;
70#endif
62#define MAXNFSDCNT 20
71
63
72void nonfs(int);
73void reapchild(int);
74void usage(void);
64static void
65usage(void)
66{
67 (void)fprintf(stderr, "usage: nfsiod [-n num_servers]\n");
68 exit(1);
69}
75
70
76/*
77 * Nfsiod does asynchronous buffered I/O on behalf of the NFS client.
78 * It does not have to be running for correct operation, but will
79 * improve throughput.
80 */
81int
71int
82main(argc, argv)
83 int argc;
84 char *argv[];
72main(int argc, char *argv[])
85{
86 int ch, num_servers;
87 struct vfsconf vfc;
88 int error;
73{
74 int ch, num_servers;
75 struct vfsconf vfc;
76 int error;
77 unsigned int iodmin;
78 unsigned int iodmax;
79 size_t len;
89
90 error = getvfsbyname("nfs", &vfc);
91 if (error && vfsisloadable("nfs")) {
92 if (vfsload("nfs"))
93 err(1, "vfsload(nfs)");
94 endvfsent(); /* flush cache */
95 error = getvfsbyname("nfs", &vfc);
96 }
80
81 error = getvfsbyname("nfs", &vfc);
82 if (error && vfsisloadable("nfs")) {
83 if (vfsload("nfs"))
84 err(1, "vfsload(nfs)");
85 endvfsent(); /* flush cache */
86 error = getvfsbyname("nfs", &vfc);
87 }
97 if(error)
88 if (error)
98 errx(1, "NFS support is not available in the running kernel");
99
89 errx(1, "NFS support is not available in the running kernel");
90
100#define MAXNFSDCNT 20
101#define DEFNFSDCNT 1
102 num_servers = DEFNFSDCNT;
91 num_servers = 0;
103 while ((ch = getopt(argc, argv, "n:")) != -1)
104 switch (ch) {
105 case 'n':
106 num_servers = atoi(optarg);
92 while ((ch = getopt(argc, argv, "n:")) != -1)
93 switch (ch) {
94 case 'n':
95 num_servers = atoi(optarg);
107 if (num_servers < 1 || num_servers > MAXNFSDCNT) {
96 if (num_servers < 1) {
108 warnx("nfsiod count %d; reset to %d",
97 warnx("nfsiod count %d; reset to %d",
109 num_servers, DEFNFSDCNT);
110 num_servers = DEFNFSDCNT;
98 num_servers, 1);
99 num_servers = 1;
111 }
100 }
101 if (num_servers > MAXNFSDCNT) {
102 warnx("nfsiod count %d; reset to %d",
103 num_servers, MAXNFSDCNT);
104 num_servers = MAXNFSDCNT;
105 }
112 break;
113 case '?':
114 default:
115 usage();
116 }
117 argc -= optind;
118 argv += optind;
119
106 break;
107 case '?':
108 default:
109 usage();
110 }
111 argc -= optind;
112 argv += optind;
113
120 /*
121 * XXX
122 * Backward compatibility, trailing number is the count of daemons.
123 */
124 if (argc > 1)
114 if (argc > 0)
125 usage();
115 usage();
126 if (argc == 1) {
127 num_servers = atoi(argv[0]);
128 if (num_servers < 1 || num_servers > MAXNFSDCNT) {
129 warnx("nfsiod count %d; reset to %d", num_servers,
130 DEFNFSDCNT);
131 num_servers = DEFNFSDCNT;
132 }
133 }
134
116
135 if (debug == 0) {
136 daemon(0, 0);
137 (void)signal(SIGHUP, SIG_IGN);
138 (void)signal(SIGINT, SIG_IGN);
139 (void)signal(SIGQUIT, SIG_IGN);
140 (void)signal(SIGSYS, nonfs);
141 }
142 (void)signal(SIGCHLD, reapchild);
117 if (num_servers == 0)
118 exit(0); /* no change */
143
119
144 openlog("nfsiod:", LOG_PID, LOG_DAEMON);
145
146 while (num_servers--)
147 switch (fork()) {
148 case -1:
149 syslog(LOG_ERR, "fork: %m");
150 exit (1);
151 case 0:
152 if (nfssvc(NFSSVC_BIOD, NULL) < 0) {
153 syslog(LOG_ERR, "nfssvc: %m");
154 exit (1);
155 }
156 exit(0);
157 }
158 exit (0);
120 len = sizeof iodmin;
121 error = sysctlbyname("vfs.nfs.iodmin", &iodmin, &len, NULL, 0);
122 if (error < 0)
123 err(1, "sysctlbyname(\"vfs.nfs.iodmin\")");
124 len = sizeof iodmax;
125 error = sysctlbyname("vfs.nfs.iodmax", &iodmax, &len, NULL, 0);
126 if (error < 0)
127 err(1, "sysctlbyname(\"vfs.nfs.iodmin\")");
128 /* Catch the case where we're lowering num_servers below iodmin */
129 if (iodmin > num_servers) {
130 iodmin = num_servers;
131 error = sysctlbyname("vfs.nfs.iodmin", NULL, 0, &iodmin,
132 sizeof iodmin);
133 if (error < 0)
134 err(1, "sysctlbyname(\"vfs.nfs.iodmin\")");
135 }
136 iodmax = num_servers;
137 error = sysctlbyname("vfs.nfs.iodmax", NULL, 0, &iodmax, sizeof iodmax);
138 if (error < 0)
139 err(1, "sysctlbyname(\"vfs.nfs.iodmax\")");
159}
160
140}
141
161void
162nonfs(signo)
163 int signo;
164{
165 syslog(LOG_ERR, "missing system call: NFS not available");
166}
167
168void
169reapchild(signo)
170 int signo;
171{
172
173 while (wait3(NULL, WNOHANG, NULL) > 0) {
174 /* nothing */
175 };
176}
177
178void
179usage()
180{
181 (void)fprintf(stderr, "usage: nfsiod [-n num_servers]\n");
182 exit(1);
183}