DAFS: Preattach, not attach, in FSYNC_Drop
[openafs.git] / src / vol / fssync-server.c
index b56eff6..c90d86e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
-
-#include <sys/types.h>
-#include <stdio.h>
-#ifdef HAVE_STDINT_H
-# include <stdint.h>
-#endif
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#include <time.h>
-#else
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <sys/time.h>
-#include <unistd.h>
-#endif
-#include <errno.h>
-#ifdef AFS_PTHREAD_ENV
-#include <assert.h>
-#else /* AFS_PTHREAD_ENV */
-#include <afs/assert.h>
-#endif /* AFS_PTHREAD_ENV */
-#include <signal.h>
-#include <string.h>
+#include <roken.h>
 
 #include <rx/xdr.h>
 #include <afs/afsint.h>
@@ -117,8 +93,8 @@ static struct offlineInfo OfflineVolumes[MAXHANDLERS][MAXOFFLINEVOLUMES];
 /**
  * fssync server socket handle.
  */
-static SYNC_server_state_t fssync_server_state = 
-    { -1,                       /* file descriptor */
+static SYNC_server_state_t fssync_server_state =
+    { OSI_NULLSOCKET,                       /* file descriptor */
       FSSYNC_ENDPOINT_DECL,     /* server endpoint */
       FSYNC_PROTO_VERSION,      /* protocol version */
       5,                        /* bind() retry limit */
@@ -222,19 +198,19 @@ FSYNC_fsInit(void)
     Lock_Init(&FSYNC_handler_lock);
 
 #ifdef AFS_PTHREAD_ENV
-    assert(pthread_attr_init(&tattr) == 0);
-    assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
-    assert(pthread_create(&tid, &tattr, FSYNC_sync, NULL) == 0);
+    osi_Assert(pthread_attr_init(&tattr) == 0);
+    osi_Assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
+    osi_Assert(pthread_create(&tid, &tattr, FSYNC_sync, NULL) == 0);
 #else /* AFS_PTHREAD_ENV */
-    assert(LWP_CreateProcess
+    osi_Assert(LWP_CreateProcess
           (FSYNC_sync, USUAL_STACK_SIZE, USUAL_PRIORITY, (void *)0,
            "FSYNC_sync", &pid) == LWP_SUCCESS);
 #endif /* AFS_PTHREAD_ENV */
 
 #ifdef AFS_DEMAND_ATTACH_FS
     queue_Init(&fsync_salv.head);
-    assert(pthread_cond_init(&fsync_salv.cv, NULL) == 0);
-    assert(pthread_create(&tid, &tattr, FSYNC_salvageThread, NULL) == 0);
+    CV_INIT(&fsync_salv.cv, "fsync salv", CV_DEFAULT, 0);
+    osi_Assert(pthread_create(&tid, &tattr, FSYNC_salvageThread, NULL) == 0);
 #endif /* AFS_DEMAND_ATTACH_FS */
 }
 
@@ -266,6 +242,10 @@ FSYNC_sync(void * args)
     int min_vinit = 1;
 #endif /* AFS_DEMAND_ATTACH_FS */
 
+    /* we must not be called before vol package initialization, since we use
+     * vol package mutexes and conds etc */
+    osi_Assert(VInit);
+
     SYNC_getAddr(&state->endpoint, &state->addr);
     SYNC_cleanupSock(state);
 
@@ -275,27 +255,30 @@ FSYNC_sync(void * args)
 
 #ifdef AFS_PTHREAD_ENV
     /* set our 'thread-id' so that the host hold table works */
-    MUTEX_ENTER(&rx_stats_mutex);      /* protects rxi_pthread_hinum */
-    tid = ++rxi_pthread_hinum;
-    MUTEX_EXIT(&rx_stats_mutex);
-    pthread_setspecific(rx_thread_id_key, (void *)(intptr_t)tid);
+    tid = rx_SetThreadNum();
     Log("Set thread id %d for FSYNC_sync\n", tid);
+    afs_pthread_setname_self("FSYNC_sync");
 #endif /* AFS_PTHREAD_ENV */
 
+    VOL_LOCK;
+
     while (VInit < min_vinit) {
        /* Let somebody else run until all volumes have been preattached
         * (DAFS), or we have started attaching volumes (non-DAFS). This
         * doesn't mean that all volumes have been attached.
         */
 #ifdef AFS_PTHREAD_ENV
-       pthread_yield();
+       VOL_CV_WAIT(&vol_vinit_cond);
 #else /* AFS_PTHREAD_ENV */
        LWP_DispatchProcess();
 #endif /* AFS_PTHREAD_ENV */
     }
+
+    VOL_UNLOCK;
+
     state->fd = SYNC_getSock(&state->endpoint);
     code = SYNC_bindSock(state);
-    assert(!code);
+    osi_Assert(!code);
 
 #ifdef AFS_DEMAND_ATTACH_FS
     /*
@@ -310,10 +293,10 @@ FSYNC_sync(void * args)
     }
     memcpy(thread_opts, &VThread_defaults, sizeof(VThread_defaults));
     thread_opts->disallow_salvsync = 1;
-    assert(pthread_setspecific(VThread_key, thread_opts) == 0);
+    osi_Assert(pthread_setspecific(VThread_key, thread_opts) == 0);
 
     code = VVGCache_PkgInit();
-    assert(code == 0);
+    osi_Assert(code == 0);
 #endif
 
     InitHandler();
@@ -327,12 +310,17 @@ FSYNC_sync(void * args)
            CallHandler(FSYNC_readfds, nfds, POLLIN|POLLPRI);
 #else
        int maxfd;
+#ifdef AFS_PTHREAD_ENV
+       struct timeval s_timeout;
+#endif
        GetHandler(&FSYNC_readfds, &maxfd);
        /* Note: check for >= 1 below is essential since IOMGR_select
         * doesn't have exactly same semantics as select.
         */
 #ifdef AFS_PTHREAD_ENV
-       if (select(maxfd + 1, &FSYNC_readfds, NULL, NULL, NULL) >= 1)
+       s_timeout.tv_sec = SYNC_SELECT_TIMEOUT;
+       s_timeout.tv_usec = 0;
+       if (select(maxfd + 1, &FSYNC_readfds, NULL, NULL, &s_timeout) >= 1)
 #else /* AFS_PTHREAD_ENV */
        if (IOMGR_Select(maxfd + 1, &FSYNC_readfds, NULL, NULL, NULL) >= 1)
 #endif /* AFS_PTHREAD_ENV */
@@ -423,7 +411,7 @@ FSYNC_backgroundSalvage(Volume *vp)
     }
 
     queue_Append(&fsync_salv.head, node);
-    assert(pthread_cond_broadcast(&fsync_salv.cv) == 0);
+    CV_BROADCAST(&fsync_salv.cv);
 }
 #endif /* AFS_DEMAND_ATTACH_FS */
 
@@ -439,12 +427,12 @@ FSYNC_newconnection(osi_socket afd)
     socklen_t junk;
     junk = sizeof(other);
     fd = accept(afd, (struct sockaddr *)&other, &junk);
-    if (fd == -1) {
+    if (fd == OSI_NULLSOCKET) {
        Log("FSYNC_newconnection:  accept failed, errno==%d\n", errno);
-       assert(1 == 2);
+       osi_Assert(1 == 2);
     } else if (!AddHandler(fd, FSYNC_com)) {
        AcceptOff();
-       assert(AddHandler(fd, FSYNC_com));
+       osi_Assert(AddHandler(fd, FSYNC_com));
     }
 }
 
@@ -523,8 +511,8 @@ FSYNC_com(osi_socket fd)
     case FSYNC_VOL_DONE:
     case FSYNC_VOL_QUERY:
     case FSYNC_VOL_QUERY_HDR:
-    case FSYNC_VOL_QUERY_VOP:
 #ifdef AFS_DEMAND_ATTACH_FS
+    case FSYNC_VOL_QUERY_VOP:
     case FSYNC_VG_QUERY:
     case FSYNC_VG_SCAN:
     case FSYNC_VG_SCAN_ALL:
@@ -686,6 +674,7 @@ FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
     Volume * vp;
     Error error;
 
+    /* Verify the partition name is null terminated. */
     if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
        res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
        code = SYNC_FAILED;
@@ -695,15 +684,22 @@ FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
     /* so, we need to attach the volume */
 
 #ifdef AFS_DEMAND_ATTACH_FS
+    /* Verify the partition name is not empty. */
+    if (*vcom->vop->partName == 0) {
+       res->hdr.reason = FSYNC_BAD_PART;
+       code = SYNC_FAILED;
+       goto done;
+    }
+
     /* check DAFS permissions */
     vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
     if (vp &&
        FSYNC_partMatch(vcom, vp, 1) &&
-       vp->pending_vol_op && 
+       vp->pending_vol_op &&
        (vcom->hdr->programType != vp->pending_vol_op->com.programType)) {
        /* a different program has this volume checked out. deny. */
        Log("FSYNC_VolOn: WARNING: program type %u has attempted to manipulate "
-           "state for volume %u using command code %u while the volume is " 
+           "state for volume %u using command code %u while the volume is "
            "checked out by program type %u for command code %u.\n",
            vcom->hdr->programType,
            vcom->vop->volume,
@@ -724,6 +720,10 @@ FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
        /* nothing much to do if we're leaving the volume offline */
 #ifdef AFS_DEMAND_ATTACH_FS
        if (vp) {
+           VCreateReservation_r(vp);
+           VWaitExclusiveState_r(vp);
+       }
+       if (vp && V_attachState(vp) != VOL_STATE_DELETED) {
            if (FSYNC_partMatch(vcom, vp, 1)) {
                if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
                    (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
@@ -738,23 +738,47 @@ FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
                res->hdr.reason = FSYNC_WRONG_PART;
            }
        } else {
-           code = SYNC_DENIED;
+           code = SYNC_FAILED;
            res->hdr.reason = FSYNC_UNKNOWN_VOLID;
        }
+
+       if (vp) {
+           VCancelReservation_r(vp);
+           vp = NULL;
+       }
 #endif
        goto done;
     }
 
 #ifdef AFS_DEMAND_ATTACH_FS
-    /* first, check to see whether we have such a volume defined */
-    vp = VPreAttachVolumeById_r(&error,
-                               vcom->vop->partName,
-                               vcom->vop->volume);
+
+    if (vp &&
+        FSYNC_partMatch(vcom, vp, 0) &&
+        vp->pending_vol_op &&
+        vp->pending_vol_op->vol_op_state == FSSYNC_VolOpRunningOnline &&
+        V_attachState(vp) == VOL_STATE_ATTACHED) {
+
+       /* noop; the volume stayed online for the volume operation and we were
+        * simply told that the vol op is done. The vp we already have is fine,
+        * so avoid confusing volume routines with trying to preattach an
+        * attached volume. */
+
+    } else {
+       /* first, check to see whether we have such a volume defined */
+       vp = VPreAttachVolumeById_r(&error,
+                                   vcom->vop->partName,
+                                   vcom->vop->volume);
+    }
+
     if (vp) {
+       VCreateReservation_r(vp);
+       VWaitExclusiveState_r(vp);
        VDeregisterVolOp_r(vp);
+       VCancelReservation_r(vp);
+       vp = NULL;
     }
 #else /* !AFS_DEMAND_ATTACH_FS */
-    tvolName[0] = '/';
+    tvolName[0] = OS_DIRSEPC;
     snprintf(&tvolName[1], sizeof(tvolName)-1, VFORMAT, afs_printable_uint32_lu(vcom->vop->volume));
     tvolName[sizeof(tvolName)-1] = '\0';
 
@@ -762,11 +786,11 @@ FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
                               V_VOLUPD);
     if (vp)
        VPutVolume_r(vp);
+#endif /* !AFS_DEMAND_ATTACH_FS */
     if (error) {
        code = SYNC_DENIED;
        res->hdr.reason = error;
     }
-#endif /* !AFS_DEMAND_ATTACH_FS */
 
  done:
     return code;
@@ -786,7 +810,7 @@ FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
  * @note this is an FSYNC RPC server stub
  *
  * @note this procedure handles the following FSSYNC command codes:
- *       - FSYNC_VOL_OFF 
+ *       - FSYNC_VOL_OFF
  *       - FSYNC_VOL_NEEDVOLUME
  *
  * @note the supplementary reason code contains additional details.
@@ -873,7 +897,7 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
                Log("volume %u already checked out\n", vp->hashid);
                /* XXX debug */
                Log("vp->vop = { com = { ver=%u, prog=%d, com=%d, reason=%d, len=%u, flags=0x%x }, vop = { vol=%u, part='%s' } }\n",
-                   vp->pending_vol_op->com.proto_version, 
+                   vp->pending_vol_op->com.proto_version,
                    vp->pending_vol_op->com.programType,
                    vp->pending_vol_op->com.command,
                    vp->pending_vol_op->com.reason,
@@ -913,20 +937,29 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
         */
        switch (type) {
        case salvageServer:
-           /* it is possible for the salvageserver to checkout a 
+       case volumeSalvager:
+           /* it is possible for the salvageserver to checkout a
             * volume for salvage before its scheduling request
             * has been sent to the salvageserver */
            if (vp->salvage.requested && !vp->salvage.scheduled) {
                vp->salvage.scheduled = 1;
            }
+
+           /* If the volume is in VOL_STATE_SALVAGE_REQ, we need to wait
+            * for the vol to go offline before we can give it away. Also
+            * make sure we don't come out with vp in an excl state. */
+           while (V_attachState(vp) == VOL_STATE_SALVAGE_REQ ||
+                  VIsExclusiveState(V_attachState(vp))) {
+
+               VOL_CV_WAIT(&V_attachCV(vp));
+           }
+
        case debugUtility:
            break;
 
        case volumeUtility:
        case volumeServer:
-            if (V_attachState(vp) == VOL_STATE_SALVAGING ||
-               vp->salvage.requested) {
-
+            if (VIsSalvaging(vp)) {
                 Log("denying offline request for volume %lu; volume is in salvaging state\n",
                    afs_printable_uint32_lu(vp->hashid));
                 res->hdr.reason = FSYNC_SALVAGE;
@@ -962,6 +995,9 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
             * attaching the volume would be safe */
            VRegisterVolOp_r(vp, &info);
            vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
+           /* fall through */
+
+       case VOL_STATE_DELETED:
            goto done;
        default:
            break;
@@ -969,9 +1005,6 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
 
        /* convert to heavyweight ref */
        nvp = VGetVolumeByVp_r(&error, vp);
-       VCancelReservation_r(rvp);
-       rvp = NULL;
-
        if (!nvp) {
             /*
              * It's possible for VGetVolumeByVp_r to have dropped and
@@ -992,13 +1025,16 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
                  * attaching the volume would be safe */
                 VRegisterVolOp_r(vp, &info);
                 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
+               /* fall through */
+
+            case VOL_STATE_DELETED:
                 goto done;
             default:
                 break;
             }
 
-           Log("FSYNC_com_VolOff: failed to get heavyweight reference to volume %u\n",
-               vcom->vop->volume);
+           Log("FSYNC_com_VolOff: failed to get heavyweight reference to volume %u (state=%u, flags=0x%x)\n",
+               vcom->vop->volume, V_attachState(vp), V_attachFlags(vp));
            res->hdr.reason = FSYNC_VOL_PKG_ERROR;
            goto deny;
        } else if (nvp != vp) {
@@ -1007,6 +1043,10 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
            vp = nvp;
        }
 
+       /* kill off lightweight ref to ensure we can't deadlock against ourselves later... */
+       VCancelReservation_r(rvp);
+       rvp = NULL;
+
        /* register the volume operation metadata with the volume */
        VRegisterVolOp_r(vp, &info);
 
@@ -1017,11 +1057,11 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
        if (VVolOpLeaveOnline_r(vp, &info)) {
            VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT);       /* At least get volume stats right */
            if (LogLevel) {
-               Log("FSYNC: Volume %u (%s) was left on line for an external %s request\n", 
-                   V_id(vp), V_name(vp), 
-                   vcom->hdr->reason == V_CLONE ? "clone" : 
-                   vcom->hdr->reason == V_READONLY ? "readonly" : 
-                   vcom->hdr->reason == V_DUMP ? "dump" : 
+               Log("FSYNC: Volume %u (%s) was left on line for an external %s request\n",
+                   V_id(vp), V_name(vp),
+                   vcom->hdr->reason == V_CLONE ? "clone" :
+                   vcom->hdr->reason == V_READONLY ? "readonly" :
+                   vcom->hdr->reason == V_DUMP ? "dump" :
                    vcom->hdr->reason == FSYNC_SALVAGE ? "salvage" :
                    "UNKNOWN");
            }
@@ -1044,15 +1084,18 @@ FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
            }
 
 #ifdef AFS_DEMAND_ATTACH_FS
+           VCreateReservation_r(vp);
             VOfflineForVolOp_r(&error, vp, "A volume utility is running.");
             if (error==0) {
-                assert(vp->nUsers==0);
-                vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline; 
+                osi_Assert(vp->nUsers==0);
+                vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline;
             }
             else {
+               VWaitExclusiveState_r(vp);
                VDeregisterVolOp_r(vp);
                 code = SYNC_DENIED;
             }
+           VCancelReservation_r(vp);
 #else
            VOffline_r(vp, "A volume utility is running.");
 #endif
@@ -1203,8 +1246,12 @@ FSYNC_com_VolDone(FSSYNC_VolOp_command * vcom, SYNC_response * res)
     if (vp) {
        if (FSYNC_partMatch(vcom, vp, 1)) {
 #ifdef AFS_DEMAND_ATTACH_FS
+           VCreateReservation_r(vp);
+           VWaitExclusiveState_r(vp);
+
            if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
-               (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
+               (V_attachState(vp) == VOL_STATE_PREATTACHED) ||
+               VIsErrorState(V_attachState(vp))) {
 
                /* Change state to DELETED, not UNATTACHED, so clients get
                 * a VNOVOL error when they try to access from now on. */
@@ -1212,14 +1259,24 @@ FSYNC_com_VolDone(FSSYNC_VolOp_command * vcom, SYNC_response * res)
                VChangeState_r(vp, VOL_STATE_DELETED);
                VDeregisterVolOp_r(vp);
 
+               /* Volume is gone; clear out old salvage stats */
+               memset(&vp->salvage, 0, sizeof(vp->salvage));
+
                /* Someday we should free the vp, too, after about 2 hours,
                 * possibly by putting the vp back on the VLRU. */
 
                code = SYNC_OK;
+           } else if (V_attachState(vp) == VOL_STATE_DELETED) {
+               VDeregisterVolOp_r(vp);
+               res->hdr.reason = FSYNC_UNKNOWN_VOLID;
+
            } else {
                code = SYNC_DENIED;
                res->hdr.reason = FSYNC_BAD_STATE;
            }
+
+           VCancelReservation_r(vp);
+           vp = NULL;
 #else /* AFS_DEMAND_ATTACH_FS */
            if (!vp->specialStatus) {
                vp->specialStatus = VNOVOL;
@@ -1296,16 +1353,21 @@ FSYNC_com_VolError(FSSYNC_VolOp_command * vcom, SYNC_response * res)
 
     if (vp) {
        if (FSYNC_partMatch(vcom, vp, 0)) {
-           /* null out salvsync control state, as it's no longer relevant */
-           memset(&vp->salvage, 0, sizeof(vp->salvage));
+           VCreateReservation_r(vp);
+           VWaitExclusiveState_r(vp);
             VDeregisterVolOp_r(vp);
 
             if (vcom->hdr->reason == FSYNC_SALVAGE) {
                FSYNC_backgroundSalvage(vp);
             } else {
+               /* null out salvsync control state, as it's no longer relevant */
+               memset(&vp->salvage, 0, sizeof(vp->salvage));
                VChangeState_r(vp, VOL_STATE_ERROR);
             }
 
+           VCancelReservation_r(vp);
+           vp = NULL;
+
            code = SYNC_OK;
        } else {
            res->hdr.reason = FSYNC_WRONG_PART;
@@ -1497,18 +1559,34 @@ FSYNC_com_VolOpQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
 
     vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
 
+    if (vp) {
+       VCreateReservation_r(vp);
+       VWaitExclusiveState_r(vp);
+    }
+
     if (vp && vp->pending_vol_op) {
-       assert(sizeof(FSSYNC_VolOp_info) <= res->payload.len);
-       memcpy(res->payload.buf, vp->pending_vol_op, sizeof(FSSYNC_VolOp_info));
-       res->hdr.response_len += sizeof(FSSYNC_VolOp_info);
-    } else {
-       if (vp) {
-           res->hdr.reason = FSYNC_NO_PENDING_VOL_OP;
+       if (!FSYNC_partMatch(vcom, vp, 1)) {
+           res->hdr.reason = FSYNC_WRONG_PART;
+           code = SYNC_FAILED;
        } else {
+           osi_Assert(sizeof(FSSYNC_VolOp_info) <= res->payload.len);
+           memcpy(res->payload.buf, vp->pending_vol_op, sizeof(FSSYNC_VolOp_info));
+           res->hdr.response_len += sizeof(FSSYNC_VolOp_info);
+       }
+    } else {
+       if (!vp || V_attachState(vp) == VOL_STATE_DELETED) {
            res->hdr.reason = FSYNC_UNKNOWN_VOLID;
+       } else if (!FSYNC_partMatch(vcom, vp, 1)) {
+           res->hdr.reason = FSYNC_WRONG_PART;
+       } else {
+           res->hdr.reason = FSYNC_NO_PENDING_VOL_OP;
        }
        code = SYNC_FAILED;
     }
+
+    if (vp) {
+       VCancelReservation_r(vp);
+    }
     return code;
 }
 
@@ -1530,7 +1608,7 @@ FSYNC_com_VGQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
        goto done;
     }
 
-    assert(sizeof(FSSYNC_VGQry_response_t) <= res->payload.len);
+    osi_Assert(sizeof(FSSYNC_VGQry_response_t) <= res->payload.len);
 
     rc = VVGCache_query_r(dp, vcom->vop->volume, res->payload.buf);
     switch (rc) {
@@ -1800,7 +1878,7 @@ FSYNC_com_StatsOpViceP(FSSYNC_StatsOp_command * scom, SYNC_response * res)
        stats->minFree = dp->minFree;
        stats->f_files = dp->f_files;
        stats->vol_list_len = dp->vol_list.len;
-       
+
        res->hdr.response_len += sizeof(struct DiskPartitionStats64);
     }
 
@@ -1830,7 +1908,7 @@ FSYNC_com_StatsOpHash(FSSYNC_StatsOp_command * scom, SYNC_response * res)
     AssignInt64(head->reorders, &stats->chain_reorders);
 
     res->hdr.response_len += sizeof(struct VolumeHashChainStats);
-    
+
     return code;
 }
 
@@ -1876,7 +1954,7 @@ FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info)
 }
 
 /**
- * check whether command packet partition name matches volume 
+ * check whether command packet partition name matches volume
  * object's partition name.
  *
  * @param[in] vcom        pointer to command packet
@@ -1893,11 +1971,11 @@ FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info)
  *
  * @internal
  */
-static int 
+static int
 FSYNC_partMatch(FSSYNC_VolOp_command * vcom, Volume * vp, int match_anon)
 {
     return ((match_anon && vcom->vop->partName[0] == 0) ||
-           (strncmp(vcom->vop->partName, V_partition(vp)->name, 
+           (strncmp(vcom->vop->partName, V_partition(vp)->name,
                     sizeof(vcom->vop->partName)) == 0));
 }
 
@@ -1908,31 +1986,38 @@ FSYNC_Drop(osi_socket fd)
     struct offlineInfo *p;
     int i;
     Error error;
+#ifndef AFS_DEMAND_ATTACH_FS
     char tvolName[VMAXPATHLEN];
+#endif
 
     VOL_LOCK;
     p = OfflineVolumes[FindHandler(fd)];
     for (i = 0; i < MAXOFFLINEVOLUMES; i++) {
        if (p[i].volumeID) {
-
            Volume *vp;
 
-           tvolName[0] = '/';
+#ifdef AFS_DEMAND_ATTACH_FS
+           vp = VPreAttachVolumeById_r(&error, p[i].partName, p[i].volumeID);
+           if (vp) {
+               VCreateReservation_r(vp);
+               VWaitExclusiveState_r(vp);
+               VDeregisterVolOp_r(vp);
+               VCancelReservation_r(vp);
+           }
+#else
+           tvolName[0] = OS_DIRSEPC;
            sprintf(&tvolName[1], VFORMAT, afs_printable_uint32_lu(p[i].volumeID));
            vp = VAttachVolumeByName_r(&error, p[i].partName, tvolName,
                                       V_VOLUPD);
            if (vp)
                VPutVolume_r(vp);
+#endif /* !AFS_DEMAND_ATTACH_FS */
            p[i].volumeID = 0;
        }
     }
     VOL_UNLOCK;
     RemoveHandler(fd);
-#ifdef AFS_NT40_ENV
-    closesocket(fd);
-#else
-    close(fd);
-#endif
+    rk_closesocket(fd);
     AcceptOn();
 }
 
@@ -1942,7 +2027,7 @@ static void
 AcceptOn(void)
 {
     if (AcceptHandler == -1) {
-       assert(AddHandler(fssync_server_state.fd, FSYNC_newconnection));
+       osi_Assert(AddHandler(fssync_server_state.fd, FSYNC_newconnection));
        AcceptHandler = FindHandler(fssync_server_state.fd);
     }
 }
@@ -1951,7 +2036,7 @@ static void
 AcceptOff(void)
 {
     if (AcceptHandler != -1) {
-       assert(RemoveHandler(fssync_server_state.fd));
+       osi_Assert(RemoveHandler(fssync_server_state.fd));
        AcceptHandler = -1;
     }
 }
@@ -1964,10 +2049,10 @@ static void (*HandlerProc[MAXHANDLERS]) (osi_socket);
 static void
 InitHandler(void)
 {
-    register int i;
+    int i;
     ObtainWriteLock(&FSYNC_handler_lock);
     for (i = 0; i < MAXHANDLERS; i++) {
-       HandlerFD[i] = -1;
+       HandlerFD[i] = OSI_NULLSOCKET;
        HandlerProc[i] = 0;
     }
     ReleaseWriteLock(&FSYNC_handler_lock);
@@ -1994,7 +2079,7 @@ CallHandler(struct pollfd *fds, int nfds, int mask)
 static void
 CallHandler(fd_set * fdsetp)
 {
-    register int i;
+    int i;
     ObtainReadLock(&FSYNC_handler_lock);
     for (i = 0; i < MAXHANDLERS; i++) {
        if (HandlerFD[i] >= 0 && FD_ISSET(HandlerFD[i], fdsetp)) {
@@ -2010,10 +2095,10 @@ CallHandler(fd_set * fdsetp)
 static int
 AddHandler(osi_socket afd, void (*aproc) (osi_socket))
 {
-    register int i;
+    int i;
     ObtainWriteLock(&FSYNC_handler_lock);
     for (i = 0; i < MAXHANDLERS; i++)
-       if (HandlerFD[i] == -1)
+       if (HandlerFD[i] == OSI_NULLSOCKET)
            break;
     if (i >= MAXHANDLERS) {
        ReleaseWriteLock(&FSYNC_handler_lock);
@@ -2026,9 +2111,9 @@ AddHandler(osi_socket afd, void (*aproc) (osi_socket))
 }
 
 static int
-FindHandler(register osi_socket afd)
+FindHandler(osi_socket afd)
 {
-    register int i;
+    int i;
     ObtainReadLock(&FSYNC_handler_lock);
     for (i = 0; i < MAXHANDLERS; i++)
        if (HandlerFD[i] == afd) {
@@ -2036,27 +2121,27 @@ FindHandler(register osi_socket afd)
            return i;
        }
     ReleaseReadLock(&FSYNC_handler_lock);      /* just in case */
-    assert(1 == 2);
+    osi_Assert(1 == 2);
     return -1;                 /* satisfy compiler */
 }
 
 static int
-FindHandler_r(register osi_socket afd)
+FindHandler_r(osi_socket afd)
 {
-    register int i;
+    int i;
     for (i = 0; i < MAXHANDLERS; i++)
        if (HandlerFD[i] == afd) {
            return i;
        }
-    assert(1 == 2);
+    osi_Assert(1 == 2);
     return -1;                 /* satisfy compiler */
 }
 
 static int
-RemoveHandler(register osi_socket afd)
+RemoveHandler(osi_socket afd)
 {
     ObtainWriteLock(&FSYNC_handler_lock);
-    HandlerFD[FindHandler_r(afd)] = -1;
+    HandlerFD[FindHandler_r(afd)] = OSI_NULLSOCKET;
     ReleaseWriteLock(&FSYNC_handler_lock);
     return 1;
 }
@@ -2069,8 +2154,8 @@ GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds)
     int fdi = 0;
     ObtainReadLock(&FSYNC_handler_lock);
     for (i = 0; i < MAXHANDLERS; i++)
-       if (HandlerFD[i] != -1) {
-           assert(fdi<maxfds);
+       if (HandlerFD[i] != OSI_NULLSOCKET) {
+           osi_Assert(fdi<maxfds);
            fds[fdi].fd = HandlerFD[i];
            fds[fdi].events = events;
            fds[fdi].revents = 0;
@@ -2083,12 +2168,12 @@ GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds)
 static void
 GetHandler(fd_set * fdsetp, int *maxfdp)
 {
-    register int i;
-    register int maxfd = -1;
+    int i;
+    int maxfd = -1;
     FD_ZERO(fdsetp);
     ObtainReadLock(&FSYNC_handler_lock);       /* just in case */
     for (i = 0; i < MAXHANDLERS; i++)
-       if (HandlerFD[i] != -1) {
+       if (HandlerFD[i] != OSI_NULLSOCKET) {
            FD_SET(HandlerFD[i], fdsetp);
 #ifndef AFS_NT40_ENV
             /* On Windows the nfds parameter to select() is ignored */