Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | /* * linux/fs/lockd/svc.c * * This is the central lockd service. * * FIXME: Separate the lockd NFS server functionality from the lockd NFS * client functionality. Oh why didn't Sun create two separate * services in the first place? * * Authors: Olaf Kirch (okir@monad.swb.de) * * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */ #define __KERNEL_SYSCALLS__ #include <linux/config.h> #include <linux/module.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/errno.h> #include <linux/in.h> #include <linux/uio.h> #include <linux/version.h> #include <linux/unistd.h> #include <linux/slab.h> #include <linux/smp.h> #include <linux/smp_lock.h> #include <linux/sunrpc/types.h> #include <linux/sunrpc/stats.h> #include <linux/sunrpc/clnt.h> #include <linux/sunrpc/svc.h> #include <linux/sunrpc/svcsock.h> #include <linux/lockd/lockd.h> #include <linux/nfs.h> #define NLMDBG_FACILITY NLMDBG_SVC #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE) #define ALLOWED_SIGS (sigmask(SIGKILL)) extern struct svc_program nlmsvc_program; struct nlmsvc_binding * nlmsvc_ops; static DECLARE_MUTEX(nlmsvc_sema); static unsigned int nlmsvc_users; static pid_t nlmsvc_pid; int nlmsvc_grace_period; unsigned long nlmsvc_timeout; static DECLARE_MUTEX_LOCKED(lockd_start); static DECLARE_WAIT_QUEUE_HEAD(lockd_exit); /* * Currently the following can be set only at insmod time. * Ideally, they would be accessible through the sysctl interface. */ unsigned long nlm_grace_period; unsigned long nlm_timeout = LOCKD_DFLT_TIMEO; unsigned long nlm_udpport, nlm_tcpport; static unsigned long set_grace_period(void) { unsigned long grace_period; /* Note: nlm_timeout should always be nonzero */ if (nlm_grace_period) grace_period = ((nlm_grace_period + nlm_timeout - 1) / nlm_timeout) * nlm_timeout * HZ; else grace_period = nlm_timeout * 5 * HZ; nlmsvc_grace_period = 1; return grace_period + jiffies; } static inline void clear_grace_period(void) { nlmsvc_grace_period = 0; } /* * This is the lockd kernel thread */ static void lockd(struct svc_rqst *rqstp) { struct svc_serv *serv = rqstp->rq_server; int err = 0; unsigned long grace_period_expire; /* Lock module and set up kernel thread */ MOD_INC_USE_COUNT; lock_kernel(); /* * Let our maker know we're running. */ nlmsvc_pid = current->pid; up(&lockd_start); daemonize("lockd"); /* Process request with signals blocked, but allow SIGKILL. */ allow_signal(SIGKILL); /* kick rpciod */ rpciod_up(); dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n"); if (!nlm_timeout) nlm_timeout = LOCKD_DFLT_TIMEO; nlmsvc_timeout = nlm_timeout * HZ; grace_period_expire = set_grace_period(); /* * The main request loop. We don't terminate until the last * NFS mount or NFS daemon has gone away, and we've been sent a * signal, or else another process has taken over our job. */ while ((nlmsvc_users || !signalled()) && nlmsvc_pid == current->pid) { long timeout = MAX_SCHEDULE_TIMEOUT; if (signalled()) { flush_signals(current); if (nlmsvc_ops) { nlmsvc_invalidate_all(); grace_period_expire = set_grace_period(); } } /* * Retry any blocked locks that have been notified by * the VFS. Don't do this during grace period. * (Theoretically, there shouldn't even be blocked locks * during grace period). */ if (!nlmsvc_grace_period) { timeout = nlmsvc_retry_blocked(); } else if (time_before(grace_period_expire, jiffies)) clear_grace_period(); /* * Find a socket with data available and call its * recvfrom routine. */ err = svc_recv(serv, rqstp, timeout); if (err == -EAGAIN || err == -EINTR) continue; if (err < 0) { printk(KERN_WARNING "lockd: terminating on error %d\n", -err); break; } dprintk("lockd: request from %08x\n", (unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr)); svc_process(serv, rqstp); } /* * Check whether there's a new lockd process before * shutting down the hosts and clearing the slot. */ if (!nlmsvc_pid || current->pid == nlmsvc_pid) { if (nlmsvc_ops) nlmsvc_invalidate_all(); nlm_shutdown_hosts(); nlmsvc_pid = 0; } else printk(KERN_DEBUG "lockd: new process, skipping host shutdown\n"); wake_up(&lockd_exit); /* Exit the RPC thread */ svc_exit_thread(rqstp); /* release rpciod */ rpciod_down(); /* Release module */ unlock_kernel(); MOD_DEC_USE_COUNT; } /* * Bring up the lockd process if it's not already up. */ int lockd_up(void) { static int warned = 0; struct svc_serv * serv; int error = 0; down(&nlmsvc_sema); /* * Unconditionally increment the user count ... this is * the number of clients who _want_ a lockd process. */ nlmsvc_users++; /* * Check whether we're already up and running. */ if (nlmsvc_pid) goto out; /* * Sanity check: if there's no pid, * we should be the first user ... */ if (nlmsvc_users > 1) printk(KERN_WARNING "lockd_up: no pid, %d users??\n", nlmsvc_users); error = -ENOMEM; serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE); if (!serv) { printk(KERN_WARNING "lockd_up: create service failed\n"); goto out; } if ((error = svc_makesock(serv, IPPROTO_UDP, nlm_udpport)) < 0 #ifdef CONFIG_NFSD_TCP || (error = svc_makesock(serv, IPPROTO_TCP, nlm_tcpport)) < 0 #endif ) { if (warned++ == 0) printk(KERN_WARNING "lockd_up: makesock failed, error=%d\n", error); goto destroy_and_out; } warned = 0; /* * Create the kernel thread and wait for it to start. */ error = svc_create_thread(lockd, serv); if (error) { printk(KERN_WARNING "lockd_up: create thread failed, error=%d\n", error); goto destroy_and_out; } down(&lockd_start); /* * Note: svc_serv structures have an initial use count of 1, * so we exit through here on both success and failure. */ destroy_and_out: svc_destroy(serv); out: up(&nlmsvc_sema); return error; } /* * Decrement the user count and bring down lockd if we're the last. */ void lockd_down(void) { static int warned = 0; down(&nlmsvc_sema); if (nlmsvc_users) { if (--nlmsvc_users) goto out; } else printk(KERN_WARNING "lockd_down: no users! pid=%d\n", nlmsvc_pid); if (!nlmsvc_pid) { if (warned++ == 0) printk(KERN_WARNING "lockd_down: no lockd running.\n"); goto out; } warned = 0; kill_proc(nlmsvc_pid, SIGKILL, 1); /* * Wait for the lockd process to exit, but since we're holding * the lockd semaphore, we can't wait around forever ... */ clear_thread_flag(TIF_SIGPENDING); interruptible_sleep_on_timeout(&lockd_exit, HZ); if (nlmsvc_pid) { printk(KERN_WARNING "lockd_down: lockd failed to exit, clearing pid\n"); nlmsvc_pid = 0; } spin_lock_irq(¤t->sighand->siglock); recalc_sigpending(); spin_unlock_irq(¤t->sighand->siglock); out: up(&nlmsvc_sema); } #ifdef MODULE /* New module support in 2.1.18 */ MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION "."); MODULE_LICENSE("GPL"); MODULE_PARM(nlm_grace_period, "10-240l"); MODULE_PARM(nlm_timeout, "3-20l"); MODULE_PARM(nlm_udpport, "0-65535l"); MODULE_PARM(nlm_tcpport, "0-65535l"); int init_module(void) { /* Init the static variables */ init_MUTEX(&nlmsvc_sema); nlmsvc_users = 0; nlmsvc_pid = 0; return 0; } void cleanup_module(void) { /* FIXME: delete all NLM clients */ nlm_shutdown_hosts(); } #else /* not a module, so process bootargs * lockd.udpport and lockd.tcpport */ static int __init udpport_set(char *str) { nlm_udpport = simple_strtoul(str, NULL, 0); return 1; } static int __init tcpport_set(char *str) { nlm_tcpport = simple_strtoul(str, NULL, 0); return 1; } __setup("lockd.udpport=", udpport_set); __setup("lockd.tcpport=", tcpport_set); #endif /* * Define NLM program and procedures */ static struct svc_version nlmsvc_version1 = { .vs_vers = 1, .vs_nproc = 17, .vs_proc = nlmsvc_procedures, .vs_xdrsize = NLMSVC_XDRSIZE, }; static struct svc_version nlmsvc_version3 = { .vs_vers = 3, .vs_nproc = 24, .vs_proc = nlmsvc_procedures, .vs_xdrsize = NLMSVC_XDRSIZE, }; #ifdef CONFIG_LOCKD_V4 static struct svc_version nlmsvc_version4 = { .vs_vers = 4, .vs_nproc = 24, .vs_proc = nlmsvc_procedures4, .vs_xdrsize = NLMSVC_XDRSIZE, }; #endif static struct svc_version * nlmsvc_version[] = { [1] = &nlmsvc_version1, [3] = &nlmsvc_version3, #ifdef CONFIG_LOCKD_V4 [4] = &nlmsvc_version4, #endif }; static struct svc_stat nlmsvc_stats; #define NLM_NRVERS (sizeof(nlmsvc_version)/sizeof(nlmsvc_version[0])) struct svc_program nlmsvc_program = { .pg_prog = NLM_PROGRAM, /* program number */ .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */ .pg_vers = nlmsvc_version, /* version table */ .pg_name = "lockd", /* service name */ .pg_class = "nfsd", /* share authentication with nfsd */ .pg_stats = &nlmsvc_stats, /* stats table */ }; |