Home | History | Annotate | Line # | Download | only in amd
      1      1.1  christos /*	$NetBSD: srvr_amfs_auto.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4  1.1.1.3  christos  * Copyright (c) 1997-2014 Erez Zadok
      5      1.1  christos  * Copyright (c) 1989 Jan-Simon Pendry
      6      1.1  christos  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
      7      1.1  christos  * Copyright (c) 1989 The Regents of the University of California.
      8      1.1  christos  * All rights reserved.
      9      1.1  christos  *
     10      1.1  christos  * This code is derived from software contributed to Berkeley by
     11      1.1  christos  * Jan-Simon Pendry at Imperial College, London.
     12      1.1  christos  *
     13      1.1  christos  * Redistribution and use in source and binary forms, with or without
     14      1.1  christos  * modification, are permitted provided that the following conditions
     15      1.1  christos  * are met:
     16      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     17      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     18      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     19      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     20      1.1  christos  *    documentation and/or other materials provided with the distribution.
     21  1.1.1.3  christos  * 3. Neither the name of the University nor the names of its contributors
     22      1.1  christos  *    may be used to endorse or promote products derived from this software
     23      1.1  christos  *    without specific prior written permission.
     24      1.1  christos  *
     25      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28      1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35      1.1  christos  * SUCH DAMAGE.
     36      1.1  christos  *
     37      1.1  christos  *
     38      1.1  christos  * File: am-utils/amd/srvr_amfs_auto.c
     39      1.1  christos  *
     40      1.1  christos  */
     41      1.1  christos 
     42      1.1  christos /*
     43      1.1  christos  * Automount FS server ("localhost") modeling
     44      1.1  christos  */
     45      1.1  christos 
     46      1.1  christos #ifdef HAVE_CONFIG_H
     47      1.1  christos # include <config.h>
     48      1.1  christos #endif /* HAVE_CONFIG_H */
     49      1.1  christos #include <am_defs.h>
     50      1.1  christos #include <amd.h>
     51      1.1  christos 
     52      1.1  christos /* globals */
     53      1.1  christos 
     54      1.1  christos /* statics */
     55      1.1  christos static qelem amfs_auto_srvr_list = {&amfs_auto_srvr_list, &amfs_auto_srvr_list};
     56      1.1  christos static fserver *localhost;
     57      1.1  christos 
     58      1.1  christos 
     59      1.1  christos /*
     60      1.1  christos  * Find an nfs server for the local host
     61      1.1  christos  */
     62      1.1  christos fserver *
     63      1.1  christos amfs_generic_find_srvr(mntfs *mf)
     64      1.1  christos {
     65      1.1  christos   fserver *fs = localhost;
     66      1.1  christos 
     67      1.1  christos   if (!fs) {
     68      1.1  christos     fs = ALLOC(struct fserver);
     69      1.1  christos     fs->fs_refc = 0;
     70  1.1.1.3  christos     fs->fs_host = xstrdup("localhost");
     71      1.1  christos     fs->fs_ip = NULL;
     72      1.1  christos     fs->fs_cid = 0;
     73      1.1  christos     fs->fs_pinger = AM_PINGER;
     74      1.1  christos     fs->fs_flags = FSF_VALID | FSF_PING_UNINIT;
     75      1.1  christos     fs->fs_type = "local";
     76      1.1  christos     fs->fs_private = NULL;
     77      1.1  christos     fs->fs_prfree = NULL;
     78      1.1  christos 
     79      1.1  christos     ins_que(&fs->fs_q, &amfs_auto_srvr_list);
     80      1.1  christos 
     81      1.1  christos     srvrlog(fs, "starts up");
     82      1.1  christos 
     83      1.1  christos     localhost = fs;
     84      1.1  christos   }
     85      1.1  christos   fs->fs_refc++;
     86      1.1  christos 
     87      1.1  christos   return fs;
     88      1.1  christos }
     89      1.1  christos 
     90      1.1  christos 
     91      1.1  christos /*****************************************************************************
     92      1.1  christos  *** GENERIC ROUTINES FOLLOW
     93      1.1  christos  *****************************************************************************/
     94      1.1  christos 
     95      1.1  christos /*
     96      1.1  christos  * Wakeup anything waiting for this server
     97      1.1  christos  */
     98      1.1  christos void
     99      1.1  christos wakeup_srvr(fserver *fs)
    100      1.1  christos {
    101      1.1  christos   fs->fs_flags &= ~FSF_WANT;
    102      1.1  christos   wakeup((voidp) fs);
    103      1.1  christos }
    104      1.1  christos 
    105      1.1  christos 
    106      1.1  christos /*
    107      1.1  christos  * Called when final ttl of server has expired
    108      1.1  christos  */
    109      1.1  christos static void
    110      1.1  christos timeout_srvr(voidp v)
    111      1.1  christos {
    112      1.1  christos   fserver *fs = v;
    113      1.1  christos 
    114      1.1  christos   /*
    115      1.1  christos    * If the reference count is still zero then
    116      1.1  christos    * we are free to remove this node
    117      1.1  christos    */
    118      1.1  christos   if (fs->fs_refc == 0) {
    119      1.1  christos     dlog("Deleting file server %s", fs->fs_host);
    120      1.1  christos     if (fs->fs_flags & FSF_WANT)
    121      1.1  christos       wakeup_srvr(fs);
    122      1.1  christos 
    123      1.1  christos     /*
    124      1.1  christos      * Remove from queue.
    125      1.1  christos      */
    126      1.1  christos     rem_que(&fs->fs_q);
    127      1.1  christos     /*
    128      1.1  christos      * (Possibly) call the private free routine.
    129      1.1  christos      */
    130      1.1  christos     if (fs->fs_private && fs->fs_prfree)
    131      1.1  christos       (*fs->fs_prfree) (fs->fs_private);
    132      1.1  christos 
    133      1.1  christos     /*
    134      1.1  christos      * Free the net address
    135      1.1  christos      */
    136  1.1.1.3  christos     XFREE(fs->fs_ip);
    137      1.1  christos 
    138      1.1  christos     /*
    139      1.1  christos      * Free the host name.
    140      1.1  christos      */
    141      1.1  christos     XFREE(fs->fs_host);
    142      1.1  christos 
    143      1.1  christos     /*
    144      1.1  christos      * Discard the fserver object.
    145      1.1  christos      */
    146      1.1  christos     XFREE(fs);
    147      1.1  christos   }
    148      1.1  christos }
    149      1.1  christos 
    150      1.1  christos 
    151      1.1  christos /*
    152      1.1  christos  * Free a file server
    153      1.1  christos  */
    154      1.1  christos void
    155      1.1  christos free_srvr(fserver *fs)
    156      1.1  christos {
    157      1.1  christos   if (--fs->fs_refc == 0) {
    158      1.1  christos     /*
    159      1.1  christos      * The reference count is now zero,
    160      1.1  christos      * so arrange for this node to be
    161      1.1  christos      * removed in AM_TTL seconds if no
    162      1.1  christos      * other mntfs is referencing it.
    163      1.1  christos      */
    164      1.1  christos     int ttl = (FSRV_ERROR(fs) || FSRV_ISDOWN(fs)) ? 19 : AM_TTL;
    165      1.1  christos 
    166      1.1  christos     dlog("Last hard reference to file server %s - will timeout in %ds", fs->fs_host, ttl);
    167      1.1  christos     if (fs->fs_cid) {
    168      1.1  christos       untimeout(fs->fs_cid);
    169      1.1  christos       /*
    170      1.1  christos        * Turn off pinging - XXX
    171      1.1  christos        */
    172      1.1  christos       fs->fs_flags &= ~FSF_PINGING;
    173      1.1  christos     }
    174      1.1  christos 
    175      1.1  christos     /*
    176      1.1  christos      * Keep structure lying around for a while
    177      1.1  christos      */
    178      1.1  christos     fs->fs_cid = timeout(ttl, timeout_srvr, (voidp) fs);
    179      1.1  christos 
    180      1.1  christos     /*
    181      1.1  christos      * Mark the fileserver down and invalid again
    182      1.1  christos      */
    183      1.1  christos     fs->fs_flags &= ~FSF_VALID;
    184      1.1  christos     fs->fs_flags |= FSF_DOWN;
    185      1.1  christos   }
    186      1.1  christos }
    187      1.1  christos 
    188      1.1  christos 
    189      1.1  christos /*
    190      1.1  christos  * Make a duplicate fserver reference
    191      1.1  christos  */
    192      1.1  christos fserver *
    193      1.1  christos dup_srvr(fserver *fs)
    194      1.1  christos {
    195      1.1  christos   fs->fs_refc++;
    196      1.1  christos   return fs;
    197      1.1  christos }
    198      1.1  christos 
    199      1.1  christos 
    200      1.1  christos /*
    201      1.1  christos  * Log state change
    202      1.1  christos  */
    203      1.1  christos void
    204      1.1  christos srvrlog(fserver *fs, char *state)
    205      1.1  christos {
    206      1.1  christos   plog(XLOG_INFO, "file server %s, type %s, state %s", fs->fs_host, fs->fs_type, state);
    207      1.1  christos }
    208