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