Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / usr / src / cmd / fs.d / nfs / mountd / rmtab.c
blob3bb66738580ee59db853f75223cf25daa7769e44
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <sys/types.h>
35 #include <string.h>
36 #include <sys/param.h>
37 #include <sys/stat.h>
38 #include <sys/file.h>
39 #include <sys/time.h>
40 #include <errno.h>
41 #include <rpcsvc/mount.h>
42 #include <sys/pathconf.h>
43 #include <sys/systeminfo.h>
44 #include <sys/utsname.h>
45 #include <signal.h>
46 #include <locale.h>
47 #include <unistd.h>
48 #include <thread.h>
49 #include <syslog.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <sharefs/share.h>
54 #include "../lib/sharetab.h"
55 #include "hashset.h"
56 #include "mountd.h"
58 static char RMTAB[] = "/etc/rmtab";
59 static FILE *rmtabf = NULL;
62 * There is nothing magic about the value selected here. Too low,
63 * and mountd might spend too much time rewriting the rmtab file.
64 * Too high, it won't do it frequently enough.
66 static int rmtab_del_thresh = 250;
68 #define RMTAB_TOOMANY_DELETED() \
69 ((rmtab_deleted > rmtab_del_thresh) && (rmtab_deleted > rmtab_inuse))
72 * mountd's version of a "struct mountlist". It is the same except
73 * for the added ml_pos field.
75 struct mntentry {
76 char *m_host;
77 char *m_path;
78 long m_pos;
81 static HASHSET mntlist;
83 static int mntentry_equal(const void *, const void *);
84 static uint32_t mntentry_hash(const void *);
85 static int mntlist_contains(char *, char *);
86 static void rmtab_delete(long);
87 static long rmtab_insert(char *, char *);
88 static void rmtab_rewrite(void);
89 static void rmtab_parse(char *buf);
90 static bool_t xdr_mntlistencode(XDR * xdrs, HASHSET * mntlist);
92 #define exstrdup(s) \
93 strcpy(exmalloc(strlen(s)+1), s)
96 static int rmtab_inuse;
97 static int rmtab_deleted;
99 static rwlock_t rmtab_lock; /* lock to protect rmtab list */
103 * Check whether the given client/path combination
104 * already appears in the mount list.
107 static int
108 mntlist_contains(char *host, char *path)
110 struct mntentry m;
112 m.m_host = host;
113 m.m_path = path;
115 return (h_get(mntlist, &m) != NULL);
120 * Add an entry to the mount list.
121 * First check whether it's there already - the client
122 * may have crashed and be rebooting.
125 static void
126 mntlist_insert(char *host, char *path)
128 if (!mntlist_contains(host, path)) {
129 struct mntentry *m;
131 m = exmalloc(sizeof (struct mntentry));
133 m->m_host = exstrdup(host);
134 m->m_path = exstrdup(path);
135 m->m_pos = rmtab_insert(host, path);
136 (void) h_put(mntlist, m);
140 void
141 mntlist_new(char *host, char *path)
143 (void) rw_wrlock(&rmtab_lock);
144 mntlist_insert(host, path);
145 (void) rw_unlock(&rmtab_lock);
149 * Delete an entry from the mount list.
152 void
153 mntlist_delete(char *host, char *path)
155 struct mntentry *m, mm;
157 mm.m_host = host;
158 mm.m_path = path;
160 (void) rw_wrlock(&rmtab_lock);
162 if ((m = (struct mntentry *)h_get(mntlist, &mm)) != NULL) {
163 rmtab_delete(m->m_pos);
165 (void) h_delete(mntlist, m);
167 free(m->m_path);
168 free(m->m_host);
169 free(m);
171 if (RMTAB_TOOMANY_DELETED())
172 rmtab_rewrite();
174 (void) rw_unlock(&rmtab_lock);
178 * Delete all entries for a host from the mount list
181 void
182 mntlist_delete_all(char *host)
184 HASHSET_ITERATOR iterator;
185 struct mntentry *m;
187 (void) rw_wrlock(&rmtab_lock);
189 iterator = h_iterator(mntlist);
191 while ((m = (struct mntentry *)h_next(iterator)) != NULL) {
192 if (strcasecmp(m->m_host, host))
193 continue;
195 rmtab_delete(m->m_pos);
197 (void) h_delete(mntlist, m);
199 free(m->m_path);
200 free(m->m_host);
201 free(m);
204 if (RMTAB_TOOMANY_DELETED())
205 rmtab_rewrite();
207 (void) rw_unlock(&rmtab_lock);
209 free(iterator);
213 * Equivalent to xdr_mountlist from librpcsvc but for HASHSET
214 * rather that for a linked list. It is used only to encode data
215 * from HASHSET before sending it over the wire.
218 static bool_t
219 xdr_mntlistencode(XDR *xdrs, HASHSET *mntlist)
221 HASHSET_ITERATOR iterator = h_iterator(*mntlist);
223 for (;;) {
224 struct mntentry *m = (struct mntentry *)h_next(iterator);
225 bool_t more_data = (m != NULL);
227 if (!xdr_bool(xdrs, &more_data)) {
228 free(iterator);
229 return (FALSE);
232 if (!more_data)
233 break;
235 if ((!xdr_name(xdrs, &m->m_host)) ||
236 (!xdr_dirpath(xdrs, &m->m_path))) {
237 free(iterator);
238 return (FALSE);
243 free(iterator);
245 return (TRUE);
248 void
249 mntlist_send(SVCXPRT *transp)
251 (void) rw_rdlock(&rmtab_lock);
253 errno = 0;
254 if (!svc_sendreply(transp, xdr_mntlistencode, (char *)&mntlist))
255 log_cant_reply(transp);
257 (void) rw_unlock(&rmtab_lock);
261 * Compute a 32 bit hash value for an mntlist entry.
265 * The string hashing algorithm is from the "Dragon Book" --
266 * "Compilers: Principles, Tools & Techniques", by Aho, Sethi, Ullman
268 * And is modified for this application from kernel/os/modhash.c
271 static uint_t
272 mntentry_str_hash(char *s, uint_t hash)
274 uint_t g;
276 for (; *s != '\0'; s++) {
277 hash = (hash << 4) + *s;
278 if ((g = (hash & 0xf0000000)) != 0) {
279 hash ^= (g >> 24);
280 hash ^= g;
284 return (hash);
287 static uint32_t
288 mntentry_hash(const void *p)
290 struct mntentry *m = (struct mntentry *)p;
291 uint_t hash;
293 hash = mntentry_str_hash(m->m_host, 0);
294 hash = mntentry_str_hash(m->m_path, hash);
296 return (hash);
300 * Compare mntlist entries.
301 * The comparison ignores a value of m_pos.
304 static int
305 mntentry_equal(const void *p1, const void *p2)
307 struct mntentry *m1 = (struct mntentry *)p1;
308 struct mntentry *m2 = (struct mntentry *)p2;
310 return ((strcasecmp(m1->m_host, m2->m_host) ||
311 strcmp(m1->m_path, m2->m_path)) ? 0 : 1);
315 * Rewrite /etc/rmtab with a current content of mntlist.
317 static void
318 rmtab_rewrite()
320 if (rmtabf)
321 (void) fclose(rmtabf);
323 /* Rewrite the file. */
324 if ((rmtabf = fopen(RMTAB, "w+")) != NULL) {
325 HASHSET_ITERATOR iterator;
326 struct mntentry *m;
328 (void) fchmod(fileno(rmtabf),
329 (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH));
330 rmtab_inuse = rmtab_deleted = 0;
332 iterator = h_iterator(mntlist);
334 while ((m = (struct mntentry *)h_next(iterator)) != NULL)
335 m->m_pos = rmtab_insert(m->m_host, m->m_path);
336 free(iterator);
341 * Parse the content of /etc/rmtab and insert the entries into mntlist.
342 * The buffer s should be ended with a NUL char.
345 static void
346 rmtab_parse(char *s)
348 char *host;
349 char *path;
350 char *tmp;
351 struct in6_addr ipv6addr;
353 host_part:
354 if (*s == '#')
355 goto skip_rest;
357 host = s;
358 for (;;) {
359 switch (*s++) {
360 case '\0':
361 return;
362 case '\n':
363 goto host_part;
364 case ':':
365 s[-1] = '\0';
366 goto path_part;
367 case '[':
368 tmp = strchr(s, ']');
369 if (tmp) {
370 *tmp = '\0';
371 if (inet_pton(AF_INET6, s, &ipv6addr) > 0) {
372 host = s;
373 s = ++tmp;
374 } else
375 *tmp = ']';
377 /* FALLTHROUGH */
378 default:
379 continue;
383 path_part:
384 path = s;
385 for (;;) {
386 switch (*s++) {
387 case '\n':
388 s[-1] = '\0';
389 if (*host && *path)
390 mntlist_insert(host, path);
391 goto host_part;
392 case '\0':
393 if (*host && *path)
394 mntlist_insert(host, path);
395 return;
396 default:
397 continue;
401 skip_rest:
402 for (;;) {
403 switch (*++s) {
404 case '\n':
405 goto host_part;
406 case '\0':
407 return;
408 default:
409 continue;
415 * Read in contents of rmtab.
416 * Call rmtab_parse to parse the file and store entries in mntlist.
417 * Rewrites the file to get rid of unused entries.
420 #define RMTAB_LOADLEN (16*2024) /* Max bytes to read at a time */
422 void
423 rmtab_load()
425 FILE *fp;
427 (void) rwlock_init(&rmtab_lock, USYNC_THREAD, NULL);
430 * Don't need to lock the list at this point
431 * because there's only a single thread running.
433 mntlist = h_create(mntentry_hash, mntentry_equal, 101, 0.75);
435 if ((fp = fopen(RMTAB, "r")) != NULL) {
436 char buf[RMTAB_LOADLEN+1];
437 size_t len;
440 * Read at most RMTAB_LOADLEN bytes from /etc/rmtab.
441 * - if fread returns RMTAB_LOADLEN we can be in the middle
442 * of a line so change the last newline character into NUL
443 * and seek back to the next character after newline.
444 * - otherwise set NUL behind the last character read.
446 while ((len = fread(buf, 1, RMTAB_LOADLEN, fp)) > 0) {
447 if (len == RMTAB_LOADLEN) {
448 int i;
450 for (i = 1; i < len; i++) {
451 if (buf[len-i] == '\n') {
452 buf[len-i] = '\0';
453 (void) fseek(fp, -i + 1,
454 SEEK_CUR);
455 goto parse;
460 /* Put a NUL character at the end of buffer */
461 buf[len] = '\0';
462 parse:
463 rmtab_parse(buf);
465 (void) fclose(fp);
467 rmtab_rewrite();
471 * Write an entry at the current location in rmtab
472 * for the given client and path.
474 * Returns the starting position of the entry
475 * or -1 if there was an error.
478 long
479 rmtab_insert(char *host, char *path)
481 long pos;
482 struct in6_addr ipv6addr;
484 if (rmtabf == NULL || fseek(rmtabf, 0L, 2) == -1) {
485 return (-1);
487 pos = ftell(rmtabf);
490 * Check if host is an IPv6 literal
493 if (inet_pton(AF_INET6, host, &ipv6addr) > 0) {
494 if (fprintf(rmtabf, "[%s]:%s\n", host, path) == EOF) {
495 return (-1);
497 } else {
498 if (fprintf(rmtabf, "%s:%s\n", host, path) == EOF) {
499 return (-1);
502 if (fflush(rmtabf) == EOF) {
503 return (-1);
505 rmtab_inuse++;
506 return (pos);
510 * Mark as unused the rmtab entry at the given offset in the file.
513 void
514 rmtab_delete(long pos)
516 if (rmtabf != NULL && pos != -1 && fseek(rmtabf, pos, 0) == 0) {
517 (void) fprintf(rmtabf, "#");
518 (void) fflush(rmtabf);
520 rmtab_inuse--;
521 rmtab_deleted++;