2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
9 * Portions Copyright (c) 2006-2010 Sine Nomine Associates
15 Institution: The Information Technology Center, Carnegie-Mellon University
18 #ifndef AFS_PTHREAD_ENV
19 #define USUAL_PRIORITY (LWP_MAX_PRIORITY - 2)
22 * stack size increased from 8K because the HP machine seemed to have trouble
23 * with the smaller stack
25 #define USUAL_STACK_SIZE (24 * 1024)
26 #endif /* !AFS_PTHREAD_ENV */
30 File server synchronization with external volume utilities.
31 server-side implementation
34 /* This controls the size of an fd_set; it must be defined early before
35 * the system headers define that type and the macros that operate on it.
36 * Its value should be as large as the maximum file descriptor limit we
37 * are likely to run into on any platform. Right now, that is 65536
38 * which is the default hard fd limit on Solaris 9 */
40 #define FD_SETSIZE 65536
43 #include <afsconfig.h>
44 #include <afs/param.h>
47 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
64 #ifdef AFS_PTHREAD_ENV
66 #else /* AFS_PTHREAD_ENV */
67 #include <afs/assert.h>
68 #endif /* AFS_PTHREAD_ENV */
73 #include <afs/afsint.h>
75 #include <afs/errors.h>
76 #include "daemon_com.h"
77 #include "daemon_com_inline.h"
79 #include "fssync_inline.h"
83 #include <afs/afssyscalls.h>
87 #include "volume_inline.h"
88 #include "partition.h"
94 #endif /* HAVE_POLL */
96 #ifdef USE_UNIX_SOCKETS
98 #include <afs/afsutil.h>
99 #endif /* USE_UNIX_SOCKETS */
101 #ifdef FSSYNC_BUILD_SERVER
103 int (*V_BreakVolumeCallbacks) (VolumeId volume);
105 #define MAXHANDLERS 4 /* Up to 4 clients; must be at least 2, so that
106 * move = dump+restore can run on single server */
107 #define MAXOFFLINEVOLUMES 128 /* This needs to be as big as the maximum
108 * number that would be offline for 1 operation.
109 * Current winner is salvage, which needs all
110 * cloned read-only copies offline when salvaging
111 * a single read-write volume */
115 static struct offlineInfo OfflineVolumes[MAXHANDLERS][MAXOFFLINEVOLUMES];
118 * fssync server socket handle.
120 static SYNC_server_state_t fssync_server_state =
121 { -1, /* file descriptor */
122 FSSYNC_ENDPOINT_DECL, /* server endpoint */
123 FSYNC_PROTO_VERSION, /* protocol version */
124 5, /* bind() retry limit */
125 100, /* listen() queue depth */
126 "FSSYNC", /* protocol name string */
129 #ifdef AFS_DEMAND_ATTACH_FS
131 * a queue of volume pointers to salvage in the background.
133 struct fsync_salv_node {
135 Volume *vp; /**< volume to salvage */
136 unsigned char update_salv_prio; /**< whether we should update the salvage priority or not */
139 struct rx_queue head;
143 static void * FSYNC_salvageThread(void *);
144 static void FSYNC_backgroundSalvage(Volume *vp);
145 #endif /* AFS_DEMAND_ATTACH_FS */
147 /* Forward declarations */
148 static void * FSYNC_sync(void *);
149 static void FSYNC_newconnection(osi_socket afd);
150 static void FSYNC_com(osi_socket fd);
151 static void FSYNC_Drop(osi_socket fd);
152 static void AcceptOn(void);
153 static void AcceptOff(void);
154 static void InitHandler(void);
155 static int AddHandler(osi_socket fd, void (*aproc)(osi_socket));
156 static int FindHandler(osi_socket afd);
157 static int FindHandler_r(osi_socket afd);
158 static int RemoveHandler(osi_socket afd);
159 #if defined(HAVE_POLL) && defined (AFS_PTHREAD_ENV)
160 static void CallHandler(struct pollfd *fds, int nfds, int mask);
161 static void GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds);
163 static void CallHandler(fd_set * fdsetp);
164 static void GetHandler(fd_set * fdsetp, int *maxfdp);
168 static afs_int32 FSYNC_com_VolOp(osi_socket fd, SYNC_command * com, SYNC_response * res);
170 #ifdef AFS_DEMAND_ATTACH_FS
171 static afs_int32 FSYNC_com_VolError(FSSYNC_VolOp_command * com, SYNC_response * res);
173 static afs_int32 FSYNC_com_VolOn(FSSYNC_VolOp_command * com, SYNC_response * res);
174 static afs_int32 FSYNC_com_VolOff(FSSYNC_VolOp_command * com, SYNC_response * res);
175 static afs_int32 FSYNC_com_VolMove(FSSYNC_VolOp_command * com, SYNC_response * res);
176 static afs_int32 FSYNC_com_VolBreakCBKs(FSSYNC_VolOp_command * com, SYNC_response * res);
177 static afs_int32 FSYNC_com_VolDone(FSSYNC_VolOp_command * com, SYNC_response * res);
178 static afs_int32 FSYNC_com_VolQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
179 static afs_int32 FSYNC_com_VolHdrQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
180 #ifdef AFS_DEMAND_ATTACH_FS
181 static afs_int32 FSYNC_com_VGUpdate(osi_socket fd, SYNC_command * com, SYNC_response * res);
182 static afs_int32 FSYNC_com_VolOpQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
183 static afs_int32 FSYNC_com_VGQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
184 static afs_int32 FSYNC_com_VGScan(FSSYNC_VolOp_command * com, SYNC_response * res);
185 static afs_int32 FSYNC_com_VGScanAll(FSSYNC_VolOp_command * com, SYNC_response * res);
186 #endif /* AFS_DEMAND_ATTACH_FS */
188 static afs_int32 FSYNC_com_VnQry(osi_socket fd, SYNC_command * com, SYNC_response * res);
190 static afs_int32 FSYNC_com_StatsOp(osi_socket fd, SYNC_command * com, SYNC_response * res);
192 static afs_int32 FSYNC_com_StatsOpGeneral(FSSYNC_StatsOp_command * scom, SYNC_response * res);
194 #ifdef AFS_DEMAND_ATTACH_FS
195 static afs_int32 FSYNC_com_StatsOpViceP(FSSYNC_StatsOp_command * scom, SYNC_response * res);
196 static afs_int32 FSYNC_com_StatsOpHash(FSSYNC_StatsOp_command * scom, SYNC_response * res);
197 static afs_int32 FSYNC_com_StatsOpHdr(FSSYNC_StatsOp_command * scom, SYNC_response * res);
198 static afs_int32 FSYNC_com_StatsOpVLRU(FSSYNC_StatsOp_command * scom, SYNC_response * res);
201 static void FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info);
203 static int FSYNC_partMatch(FSSYNC_VolOp_command * vcom, Volume * vp, int match_anon);
207 * This lock controls access to the handler array. The overhead
208 * is minimal in non-preemptive environments.
210 struct Lock FSYNC_handler_lock;
215 #ifdef AFS_PTHREAD_ENV
217 pthread_attr_t tattr;
218 #else /* AFS_PTHREAD_ENV */
220 #endif /* AFS_PTHREAD_ENV */
222 Lock_Init(&FSYNC_handler_lock);
224 #ifdef AFS_PTHREAD_ENV
225 assert(pthread_attr_init(&tattr) == 0);
226 assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
227 assert(pthread_create(&tid, &tattr, FSYNC_sync, NULL) == 0);
228 #else /* AFS_PTHREAD_ENV */
229 assert(LWP_CreateProcess
230 (FSYNC_sync, USUAL_STACK_SIZE, USUAL_PRIORITY, (void *)0,
231 "FSYNC_sync", &pid) == LWP_SUCCESS);
232 #endif /* AFS_PTHREAD_ENV */
234 #ifdef AFS_DEMAND_ATTACH_FS
235 queue_Init(&fsync_salv.head);
236 assert(pthread_cond_init(&fsync_salv.cv, NULL) == 0);
237 assert(pthread_create(&tid, &tattr, FSYNC_salvageThread, NULL) == 0);
238 #endif /* AFS_DEMAND_ATTACH_FS */
241 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
242 static struct pollfd FSYNC_readfds[MAXHANDLERS];
244 static fd_set FSYNC_readfds;
249 FSYNC_sync(void * args)
253 #ifdef AFS_PTHREAD_ENV
256 SYNC_server_state_t * state = &fssync_server_state;
257 #ifdef AFS_DEMAND_ATTACH_FS
258 VThreadOptions_t * thread_opts;
262 * For non-DAFS, only wait until we begin attaching volumes (instead
263 * of waiting until all volumes are attached), since it can take
264 * awhile until VInit == 2.
267 #endif /* AFS_DEMAND_ATTACH_FS */
269 /* we must not be called before vol package initialization, since we use
270 * vol package mutexes and conds etc */
273 SYNC_getAddr(&state->endpoint, &state->addr);
274 SYNC_cleanupSock(state);
277 (void)signal(SIGPIPE, SIG_IGN);
280 #ifdef AFS_PTHREAD_ENV
281 /* set our 'thread-id' so that the host hold table works */
282 tid = rx_NewThreadId();
283 pthread_setspecific(rx_thread_id_key, (void *)(intptr_t)tid);
284 Log("Set thread id %d for FSYNC_sync\n", tid);
285 #endif /* AFS_PTHREAD_ENV */
289 while (VInit < min_vinit) {
290 /* Let somebody else run until all volumes have been preattached
291 * (DAFS), or we have started attaching volumes (non-DAFS). This
292 * doesn't mean that all volumes have been attached.
294 #ifdef AFS_PTHREAD_ENV
295 VOL_CV_WAIT(&vol_vinit_cond);
296 #else /* AFS_PTHREAD_ENV */
297 LWP_DispatchProcess();
298 #endif /* AFS_PTHREAD_ENV */
303 state->fd = SYNC_getSock(&state->endpoint);
304 code = SYNC_bindSock(state);
307 #ifdef AFS_DEMAND_ATTACH_FS
309 * make sure the volume package is incapable of recursively executing
310 * salvsync calls on this thread, since there is a possibility of
313 thread_opts = malloc(sizeof(VThreadOptions_t));
314 if (thread_opts == NULL) {
315 Log("failed to allocate memory for thread-specific volume package options structure\n");
318 memcpy(thread_opts, &VThread_defaults, sizeof(VThread_defaults));
319 thread_opts->disallow_salvsync = 1;
320 assert(pthread_setspecific(VThread_key, thread_opts) == 0);
322 code = VVGCache_PkgInit();
330 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
332 GetHandler(FSYNC_readfds, MAXHANDLERS, POLLIN|POLLPRI, &nfds);
333 if (poll(FSYNC_readfds, nfds, -1) >=1)
334 CallHandler(FSYNC_readfds, nfds, POLLIN|POLLPRI);
337 GetHandler(&FSYNC_readfds, &maxfd);
338 /* Note: check for >= 1 below is essential since IOMGR_select
339 * doesn't have exactly same semantics as select.
341 #ifdef AFS_PTHREAD_ENV
342 if (select(maxfd + 1, &FSYNC_readfds, NULL, NULL, NULL) >= 1)
343 #else /* AFS_PTHREAD_ENV */
344 if (IOMGR_Select(maxfd + 1, &FSYNC_readfds, NULL, NULL, NULL) >= 1)
345 #endif /* AFS_PTHREAD_ENV */
346 CallHandler(&FSYNC_readfds);
349 return NULL; /* hush now, little gcc */
352 #ifdef AFS_DEMAND_ATTACH_FS
354 * thread for salvaging volumes in the background.
356 * Since FSSYNC handlers cannot issue SALVSYNC requests in order to avoid
357 * deadlock issues, this thread exists so code in the FSSYNC handler thread
358 * can hand off volumes to be salvaged in the background.
360 * @param[in] args unused
362 * @note DEMAND_ATTACH_FS only
365 FSYNC_salvageThread(void * args)
368 struct fsync_salv_node *node;
373 while (queue_IsEmpty(&fsync_salv.head)) {
374 VOL_CV_WAIT(&fsync_salv.cv);
377 node = queue_First(&fsync_salv.head, fsync_salv_node);
381 if (node->update_salv_prio) {
382 if (VUpdateSalvagePriority_r(vp)) {
383 ViceLog(0, ("FSYNC_salvageThread: unable to raise salvage priority "
384 "for volume %lu\n", afs_printable_uint32_lu(vp->hashid)));
391 VCancelReservation_r(vp);
400 * salvage a volume in the background.
402 * Salvages cannot be scheduled directly from the main FSYNC thread, so
403 * instead call this function to schedule a salvage asynchronously in the
404 * FSYNC_salvageThread thread.
406 * @param[in] vp volume to pointer to salvage
410 * @note DEMAND_ATTACH_FS only
413 FSYNC_backgroundSalvage(Volume *vp)
415 struct fsync_salv_node *node;
418 VCreateReservation_r(vp);
420 node = malloc(sizeof(struct fsync_salv_node));
423 /* Save this value, to know if we should VUpdateSalvagePriority_r.
424 * We need to save it here, snce VRequestSalvage_r will change it. */
425 node->update_salv_prio = vp->salvage.requested;
427 if (VRequestSalvage_r(&ec, vp, SALVSYNC_ERROR, 0)) {
428 ViceLog(0, ("FSYNC_backgroundSalvage: unable to request salvage for volume %lu\n",
429 afs_printable_uint32_lu(vp->hashid)));
432 queue_Append(&fsync_salv.head, node);
433 assert(pthread_cond_broadcast(&fsync_salv.cv) == 0);
435 #endif /* AFS_DEMAND_ATTACH_FS */
438 FSYNC_newconnection(osi_socket afd)
440 #ifdef USE_UNIX_SOCKETS
441 struct sockaddr_un other;
442 #else /* USE_UNIX_SOCKETS */
443 struct sockaddr_in other;
447 junk = sizeof(other);
448 fd = accept(afd, (struct sockaddr *)&other, &junk);
450 Log("FSYNC_newconnection: accept failed, errno==%d\n", errno);
452 } else if (!AddHandler(fd, FSYNC_com)) {
454 assert(AddHandler(fd, FSYNC_com));
458 /* this function processes commands from an fssync file descriptor (fd) */
459 afs_int32 FS_cnt = 0;
461 FSYNC_com(osi_socket fd)
465 SYNC_PROTO_BUF_DECL(com_buf);
466 SYNC_PROTO_BUF_DECL(res_buf);
468 memset(&res.hdr, 0, sizeof(res.hdr));
470 com.payload.buf = (void *)com_buf;
471 com.payload.len = SYNC_PROTO_MAX_LEN;
472 res.hdr.response_len = sizeof(res.hdr);
473 res.payload.len = SYNC_PROTO_MAX_LEN;
474 res.payload.buf = (void *)res_buf;
477 if (SYNC_getCom(&fssync_server_state, fd, &com)) {
478 Log("FSYNC_com: read failed; dropping connection (cnt=%d)\n", FS_cnt);
483 if (com.recv_len < sizeof(com.hdr)) {
484 Log("FSSYNC_com: invalid protocol message length (%u)\n", com.recv_len);
485 res.hdr.response = SYNC_COM_ERROR;
486 res.hdr.reason = SYNC_REASON_MALFORMED_PACKET;
487 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
491 if (com.hdr.proto_version != FSYNC_PROTO_VERSION) {
492 Log("FSYNC_com: invalid protocol version (%u)\n", com.hdr.proto_version);
493 res.hdr.response = SYNC_COM_ERROR;
494 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
498 if (com.hdr.command == SYNC_COM_CHANNEL_CLOSE) {
499 res.hdr.response = SYNC_OK;
500 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
502 /* don't respond, just drop; senders of SYNC_COM_CHANNEL_CLOSE
503 * never wait for a response. */
507 ViceLog(125, ("FSYNC_com: from fd %d got command %ld (%s) reason %ld (%s) "
508 "pt %ld (%s) pid %ld\n", (int)fd,
509 afs_printable_int32_ld(com.hdr.command),
510 FSYNC_com2string(com.hdr.command),
511 afs_printable_int32_ld(com.hdr.reason),
512 FSYNC_reason2string(com.hdr.reason),
513 afs_printable_int32_ld(com.hdr.programType),
514 VPTypeToString(com.hdr.programType),
515 afs_printable_int32_ld(com.hdr.pid)));
517 res.hdr.com_seq = com.hdr.com_seq;
520 switch (com.hdr.command) {
522 case FSYNC_VOL_ATTACH:
523 case FSYNC_VOL_LEAVE_OFF:
525 case FSYNC_VOL_FORCE_ERROR:
526 case FSYNC_VOL_LISTVOLUMES:
527 case FSYNC_VOL_NEEDVOLUME:
529 case FSYNC_VOL_BREAKCBKS:
531 case FSYNC_VOL_QUERY:
532 case FSYNC_VOL_QUERY_HDR:
533 case FSYNC_VOL_QUERY_VOP:
534 #ifdef AFS_DEMAND_ATTACH_FS
537 case FSYNC_VG_SCAN_ALL:
539 res.hdr.response = FSYNC_com_VolOp(fd, &com, &res);
541 case FSYNC_VOL_STATS_GENERAL:
542 case FSYNC_VOL_STATS_VICEP:
543 case FSYNC_VOL_STATS_HASH:
544 case FSYNC_VOL_STATS_HDR:
545 case FSYNC_VOL_STATS_VLRU:
546 res.hdr.response = FSYNC_com_StatsOp(fd, &com, &res);
548 case FSYNC_VOL_QUERY_VNODE:
549 res.hdr.response = FSYNC_com_VnQry(fd, &com, &res);
551 #ifdef AFS_DEMAND_ATTACH_FS
554 res.hdr.response = FSYNC_com_VGUpdate(fd, &com, &res);
558 res.hdr.response = SYNC_BAD_COMMAND;
563 ViceLog(125, ("FSYNC_com: fd %d responding with code %ld (%s) reason %ld "
565 afs_printable_int32_ld(res.hdr.response),
566 SYNC_res2string(res.hdr.response),
567 afs_printable_int32_ld(res.hdr.reason),
568 FSYNC_reason2string(res.hdr.reason)));
571 SYNC_putRes(&fssync_server_state, fd, &res);
574 if (res.hdr.flags & SYNC_FLAG_CHANNEL_SHUTDOWN) {
580 FSYNC_com_VolOp(osi_socket fd, SYNC_command * com, SYNC_response * res)
583 afs_int32 code = SYNC_OK;
584 FSSYNC_VolOp_command vcom;
586 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_VolOp_hdr))) {
587 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
588 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
589 return SYNC_COM_ERROR;
592 vcom.hdr = &com->hdr;
593 vcom.vop = (FSSYNC_VolOp_hdr *) com->payload.buf;
596 vcom.volumes = OfflineVolumes[FindHandler(fd)];
597 for (vcom.v = NULL, i = 0; i < MAXOFFLINEVOLUMES; i++) {
598 if ((vcom.volumes[i].volumeID == vcom.vop->volume) &&
599 (strncmp(vcom.volumes[i].partName, vcom.vop->partName,
600 sizeof(vcom.volumes[i].partName)) == 0)) {
601 vcom.v = &vcom.volumes[i];
606 ViceLog(125, ("FSYNC_com_VolOp: fd %d got command for vol %lu part %.16s\n",
607 (int)fd, afs_printable_uint32_lu(vcom.vop->volume),
608 vcom.vop->partName));
610 switch (com->hdr.command) {
612 case FSYNC_VOL_ATTACH:
613 case FSYNC_VOL_LEAVE_OFF:
614 code = FSYNC_com_VolOn(&vcom, res);
617 case FSYNC_VOL_NEEDVOLUME:
618 code = FSYNC_com_VolOff(&vcom, res);
620 case FSYNC_VOL_LISTVOLUMES:
624 code = FSYNC_com_VolMove(&vcom, res);
626 case FSYNC_VOL_BREAKCBKS:
627 code = FSYNC_com_VolBreakCBKs(&vcom, res);
630 code = FSYNC_com_VolDone(&vcom, res);
632 case FSYNC_VOL_QUERY:
633 code = FSYNC_com_VolQuery(&vcom, res);
635 case FSYNC_VOL_QUERY_HDR:
636 code = FSYNC_com_VolHdrQuery(&vcom, res);
638 #ifdef AFS_DEMAND_ATTACH_FS
639 case FSYNC_VOL_FORCE_ERROR:
640 code = FSYNC_com_VolError(&vcom, res);
642 case FSYNC_VOL_QUERY_VOP:
643 code = FSYNC_com_VolOpQuery(&vcom, res);
646 code = FSYNC_com_VGQuery(&vcom, res);
649 code = FSYNC_com_VGScan(&vcom, res);
651 case FSYNC_VG_SCAN_ALL:
652 code = FSYNC_com_VGScanAll(&vcom, res);
654 #endif /* AFS_DEMAND_ATTACH_FS */
656 code = SYNC_BAD_COMMAND;
663 * service an FSYNC request to bring a volume online.
665 * @param[in] vcom pointer command object
666 * @param[out] res object in which to store response packet
668 * @return operation status
669 * @retval SYNC_OK volume transitioned online
670 * @retval SYNC_FAILED invalid command protocol message
671 * @retval SYNC_DENIED operation could not be completed
673 * @note this is an FSYNC RPC server stub
675 * @note this procedure handles the following FSSYNC command codes:
678 * - FSYNC_VOL_LEAVE_OFF
680 * @note the supplementary reason code contains additional details.
681 * When SYNC_DENIED is returned, the specific reason is
682 * placed in the response packet reason field.
687 FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
689 afs_int32 code = SYNC_OK;
690 #ifndef AFS_DEMAND_ATTACH_FS
691 char tvolName[VMAXPATHLEN];
696 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
697 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
702 /* so, we need to attach the volume */
704 #ifdef AFS_DEMAND_ATTACH_FS
705 /* check DAFS permissions */
706 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
708 FSYNC_partMatch(vcom, vp, 1) &&
709 vp->pending_vol_op &&
710 (vcom->hdr->programType != vp->pending_vol_op->com.programType)) {
711 /* a different program has this volume checked out. deny. */
712 Log("FSYNC_VolOn: WARNING: program type %u has attempted to manipulate "
713 "state for volume %u using command code %u while the volume is "
714 "checked out by program type %u for command code %u.\n",
715 vcom->hdr->programType,
718 vp->pending_vol_op->com.programType,
719 vp->pending_vol_op->com.command);
721 res->hdr.reason = FSYNC_EXCLUSIVE;
727 vcom->v->volumeID = 0;
730 if (vcom->hdr->command == FSYNC_VOL_LEAVE_OFF) {
731 /* nothing much to do if we're leaving the volume offline */
732 #ifdef AFS_DEMAND_ATTACH_FS
734 if (FSYNC_partMatch(vcom, vp, 1)) {
735 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
736 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
737 VChangeState_r(vp, VOL_STATE_UNATTACHED);
738 VDeregisterVolOp_r(vp);
741 res->hdr.reason = FSYNC_BAD_STATE;
745 res->hdr.reason = FSYNC_WRONG_PART;
749 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
755 #ifdef AFS_DEMAND_ATTACH_FS
756 /* first, check to see whether we have such a volume defined */
757 vp = VPreAttachVolumeById_r(&error,
761 VDeregisterVolOp_r(vp);
763 #else /* !AFS_DEMAND_ATTACH_FS */
765 snprintf(&tvolName[1], sizeof(tvolName)-1, VFORMAT, afs_printable_uint32_lu(vcom->vop->volume));
766 tvolName[sizeof(tvolName)-1] = '\0';
768 vp = VAttachVolumeByName_r(&error, vcom->vop->partName, tvolName,
774 res->hdr.reason = error;
776 #endif /* !AFS_DEMAND_ATTACH_FS */
783 * service an FSYNC request to take a volume offline.
785 * @param[in] vcom pointer command object
786 * @param[out] res object in which to store response packet
788 * @return operation status
789 * @retval SYNC_OK volume transitioned offline
790 * @retval SYNC_FAILED invalid command protocol message
791 * @retval SYNC_DENIED operation could not be completed
793 * @note this is an FSYNC RPC server stub
795 * @note this procedure handles the following FSSYNC command codes:
797 * - FSYNC_VOL_NEEDVOLUME
799 * @note the supplementary reason code contains additional details.
800 * When SYNC_DENIED is returned, the specific reason is
801 * placed in the response packet reason field.
806 FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
808 FSSYNC_VolOp_info info;
809 afs_int32 code = SYNC_OK;
813 #ifdef AFS_DEMAND_ATTACH_FS
814 Volume *nvp, *rvp = NULL;
817 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
818 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
823 /* not already offline, we need to find a slot for newly offline volume */
824 if (vcom->hdr->programType == debugUtility) {
825 /* debug utilities do not have their operations tracked */
829 for (i = 0; i < MAXOFFLINEVOLUMES; i++) {
830 if (vcom->volumes[i].volumeID == 0) {
831 vcom->v = &vcom->volumes[i];
841 FSYNC_com_to_info(vcom, &info);
843 #ifdef AFS_DEMAND_ATTACH_FS
844 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
846 vp = VGetVolume_r(&error, vcom->vop->volume);
850 if (!FSYNC_partMatch(vcom, vp, 1)) {
851 /* volume on desired partition is not online, so we
852 * should treat this as an offline volume.
854 #ifndef AFS_DEMAND_ATTACH_FS
862 #ifdef AFS_DEMAND_ATTACH_FS
864 ProgramType type = (ProgramType) vcom->hdr->programType;
866 /* do initial filtering of requests */
868 /* enforce mutual exclusion for volume ops */
869 if (vp->pending_vol_op) {
870 if (vp->pending_vol_op->com.programType != type) {
871 if (vp->pending_vol_op->com.command == FSYNC_VOL_OFF &&
872 vp->pending_vol_op->com.reason == FSYNC_SALVAGE) {
874 Log("denying offline request for volume %lu; volume is salvaging\n",
875 afs_printable_uint32_lu(vp->hashid));
877 res->hdr.reason = FSYNC_SALVAGE;
880 Log("volume %u already checked out\n", vp->hashid);
882 Log("vp->vop = { com = { ver=%u, prog=%d, com=%d, reason=%d, len=%u, flags=0x%x }, vop = { vol=%u, part='%s' } }\n",
883 vp->pending_vol_op->com.proto_version,
884 vp->pending_vol_op->com.programType,
885 vp->pending_vol_op->com.command,
886 vp->pending_vol_op->com.reason,
887 vp->pending_vol_op->com.command_len,
888 vp->pending_vol_op->com.flags,
889 vp->pending_vol_op->vop.volume,
890 vp->pending_vol_op->vop.partName );
891 Log("vcom = { com = { ver=%u, prog=%d, com=%d, reason=%d, len=%u, flags=0x%x } , vop = { vol=%u, part='%s' } }\n",
892 vcom->hdr->proto_version,
893 vcom->hdr->programType,
896 vcom->hdr->command_len,
899 vcom->vop->partName);
900 res->hdr.reason = FSYNC_EXCLUSIVE;
903 Log("warning: volume %u recursively checked out by programType id %d\n",
904 vp->hashid, vcom->hdr->programType);
908 /* wait for exclusive ops, so we have an accurate picture of the
909 * vol attach state */
910 VCreateReservation_r(vp);
911 VWaitExclusiveState_r(vp);
914 /* filter based upon requestor
916 * volume utilities / volserver are not allowed to check out
917 * volumes which are in an error state
919 * unknown utility programs will be denied on principal
923 /* it is possible for the salvageserver to checkout a
924 * volume for salvage before its scheduling request
925 * has been sent to the salvageserver */
926 if (vp->salvage.requested && !vp->salvage.scheduled) {
927 vp->salvage.scheduled = 1;
934 if (V_attachState(vp) == VOL_STATE_SALVAGING ||
935 vp->salvage.requested) {
937 Log("denying offline request for volume %lu; volume is in salvaging state\n",
938 afs_printable_uint32_lu(vp->hashid));
939 res->hdr.reason = FSYNC_SALVAGE;
941 /* the volume hasn't been checked out yet by the salvager,
942 * but we think the volume is salvaging; schedule a
943 * a salvage to update the salvage priority */
944 FSYNC_backgroundSalvage(vp);
948 if (VIsErrorState(V_attachState(vp))) {
954 Log("bad program type passed to FSSYNC\n");
958 /* short circuit for offline volume states
959 * so we can avoid I/O penalty of attachment */
960 switch (V_attachState(vp)) {
961 case VOL_STATE_UNATTACHED:
962 case VOL_STATE_PREATTACHED:
963 case VOL_STATE_SALVAGING:
964 case VOL_STATE_ERROR:
965 /* register the volume operation metadata with the volume
967 * if the volume is currently pre-attached, attach2()
968 * will evaluate the vol op metadata to determine whether
969 * attaching the volume would be safe */
970 VRegisterVolOp_r(vp, &info);
971 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
977 /* convert to heavyweight ref */
978 nvp = VGetVolumeByVp_r(&error, vp);
981 * It's possible for VGetVolumeByVp_r to have dropped and
982 * re-acquired VOL_LOCK, so volume state may have changed
983 * back to one of the states we tested for above. Since
984 * GetVolume can return NULL in some of those states, just
985 * test for the states again here.
987 switch (V_attachState(vp)) {
988 case VOL_STATE_UNATTACHED:
989 case VOL_STATE_PREATTACHED:
990 case VOL_STATE_SALVAGING:
991 case VOL_STATE_ERROR:
992 /* register the volume operation metadata with the volume
994 * if the volume is currently pre-attached, attach2()
995 * will evaluate the vol op metadata to determine whether
996 * attaching the volume would be safe */
997 VRegisterVolOp_r(vp, &info);
998 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
1004 Log("FSYNC_com_VolOff: failed to get heavyweight reference to volume %u\n",
1006 res->hdr.reason = FSYNC_VOL_PKG_ERROR;
1008 } else if (nvp != vp) {
1009 /* i don't think this should ever happen, but just in case... */
1010 Log("FSYNC_com_VolOff: warning: potentially dangerous race detected\n");
1014 /* kill off lightweight ref to ensure we can't deadlock against ourselves later... */
1015 VCancelReservation_r(rvp);
1018 /* register the volume operation metadata with the volume */
1019 VRegisterVolOp_r(vp, &info);
1022 #endif /* AFS_DEMAND_ATTACH_FS */
1025 if (VVolOpLeaveOnline_r(vp, &info)) {
1026 VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT); /* At least get volume stats right */
1028 Log("FSYNC: Volume %u (%s) was left on line for an external %s request\n",
1029 V_id(vp), V_name(vp),
1030 vcom->hdr->reason == V_CLONE ? "clone" :
1031 vcom->hdr->reason == V_READONLY ? "readonly" :
1032 vcom->hdr->reason == V_DUMP ? "dump" :
1033 vcom->hdr->reason == FSYNC_SALVAGE ? "salvage" :
1036 #ifdef AFS_DEMAND_ATTACH_FS
1037 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOnline;
1041 if (VVolOpSetVBusy_r(vp, &info)) {
1042 vp->specialStatus = VBUSY;
1045 /* remember what volume we got, so we can keep track of how
1046 * many volumes the volserver or whatever is using. Note that
1047 * vp is valid since leaveonline is only set when vp is valid.
1050 vcom->v->volumeID = vcom->vop->volume;
1051 strlcpy(vcom->v->partName, vp->partition->name, sizeof(vcom->v->partName));
1054 #ifdef AFS_DEMAND_ATTACH_FS
1055 VOfflineForVolOp_r(&error, vp, "A volume utility is running.");
1057 assert(vp->nUsers==0);
1058 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline;
1061 VDeregisterVolOp_r(vp);
1065 VOffline_r(vp, "A volume utility is running.");
1076 #ifdef AFS_DEMAND_ATTACH_FS
1078 VCancelReservation_r(rvp);
1085 * service an FSYNC request to mark a volume as moved.
1087 * @param[in] vcom pointer command object
1088 * @param[out] res object in which to store response packet
1090 * @return operation status
1091 * @retval SYNC_OK volume marked as moved to a remote server
1092 * @retval SYNC_FAILED invalid command protocol message
1093 * @retval SYNC_DENIED current volume state does not permit this operation
1095 * @note this is an FSYNC RPC server stub
1097 * @note this operation also breaks all callbacks for the given volume
1099 * @note this procedure handles the following FSSYNC command codes:
1102 * @note the supplementary reason code contains additional details. For
1103 * instance, SYNC_OK is still returned when the partition specified
1104 * does not match the one registered in the volume object -- reason
1105 * will be FSYNC_WRONG_PART in this case.
1110 FSYNC_com_VolMove(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1112 afs_int32 code = SYNC_DENIED;
1116 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1117 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1122 /* Yuch: the "reason" for the move is the site it got moved to... */
1123 /* still set specialStatus so we stop sending back VBUSY.
1124 * also should still break callbacks. Note that I don't know
1125 * how to tell if we should break all or not, so we just do it
1126 * since it doesn't matter much if we do an extra break
1127 * volume callbacks on a volume move within the same server */
1128 #ifdef AFS_DEMAND_ATTACH_FS
1129 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1131 vp = VGetVolume_r(&error, vcom->vop->volume);
1134 if (FSYNC_partMatch(vcom, vp, 1)) {
1135 #ifdef AFS_DEMAND_ATTACH_FS
1136 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1137 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
1140 vp->specialStatus = VMOVED;
1141 #ifdef AFS_DEMAND_ATTACH_FS
1143 res->hdr.reason = FSYNC_BAD_STATE;
1147 res->hdr.reason = FSYNC_WRONG_PART;
1149 #ifndef AFS_DEMAND_ATTACH_FS
1151 #endif /* !AFS_DEMAND_ATTACH_FS */
1153 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1156 if ((code == SYNC_OK) && (V_BreakVolumeCallbacks != NULL)) {
1157 Log("fssync: volume %u moved to %x; breaking all call backs\n",
1158 vcom->vop->volume, vcom->hdr->reason);
1160 (*V_BreakVolumeCallbacks) (vcom->vop->volume);
1170 * service an FSYNC request to mark a volume as destroyed.
1172 * @param[in] vcom pointer command object
1173 * @param[out] res object in which to store response packet
1175 * @return operation status
1176 * @retval SYNC_OK volume marked as destroyed
1177 * @retval SYNC_FAILED invalid command protocol message
1178 * @retval SYNC_DENIED current volume state does not permit this operation
1180 * @note this is an FSYNC RPC server stub
1182 * @note this procedure handles the following FSSYNC command codes:
1185 * @note the supplementary reason code contains additional details. For
1186 * instance, SYNC_OK is still returned when the partition specified
1187 * does not match the one registered in the volume object -- reason
1188 * will be FSYNC_WRONG_PART in this case.
1193 FSYNC_com_VolDone(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1195 afs_int32 code = SYNC_FAILED;
1199 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1200 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1204 /* don't try to put online, this call is made only after deleting
1205 * a volume, in which case we want to remove the vol # from the
1206 * OfflineVolumes array only */
1208 vcom->v->volumeID = 0;
1210 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1212 if (FSYNC_partMatch(vcom, vp, 1)) {
1213 #ifdef AFS_DEMAND_ATTACH_FS
1214 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1215 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
1217 /* Change state to DELETED, not UNATTACHED, so clients get
1218 * a VNOVOL error when they try to access from now on. */
1220 VChangeState_r(vp, VOL_STATE_DELETED);
1221 VDeregisterVolOp_r(vp);
1223 /* Someday we should free the vp, too, after about 2 hours,
1224 * possibly by putting the vp back on the VLRU. */
1229 res->hdr.reason = FSYNC_BAD_STATE;
1231 #else /* AFS_DEMAND_ATTACH_FS */
1232 if (!vp->specialStatus) {
1233 vp->specialStatus = VNOVOL;
1236 #endif /* !AFS_DEMAND_ATTACH_FS */
1238 code = SYNC_OK; /* XXX is this really a good idea? */
1239 res->hdr.reason = FSYNC_WRONG_PART;
1242 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1249 #ifdef AFS_DEMAND_ATTACH_FS
1251 * service an FSYNC request to transition a volume to the hard error state.
1253 * @param[in] vcom pointer command object
1254 * @param[out] res object in which to store response packet
1256 * @return operation status
1257 * @retval SYNC_OK volume transitioned to hard error state
1258 * @retval SYNC_FAILED invalid command protocol message
1259 * @retval SYNC_DENIED (see note)
1261 * @note this is an FSYNC RPC server stub
1263 * @note this procedure handles the following FSSYNC command codes:
1264 * - FSYNC_VOL_FORCE_ERROR
1266 * @note SYNC_DENIED is returned in the following cases:
1267 * - no partition name is specified (reason field set to
1268 * FSYNC_WRONG_PART).
1269 * - volume id not known to fileserver (reason field set
1270 * to FSYNC_UNKNOWN_VOLID).
1272 * @note demand attach fileserver only
1277 FSYNC_com_VolError(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1281 afs_int32 code = SYNC_FAILED;
1283 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1284 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1288 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1290 if (!vp && vcom->hdr->reason == FSYNC_SALVAGE) {
1291 /* The requested volume doesn't seem to exist. However, it is possible
1292 * that this is triggered by trying to create or clone a volume that
1293 * was prevented from succeeding by a half-created volume in the way.
1294 * (e.g. we tried to create volume X, but volume X exists except that
1295 * its .vol header was deleted for some reason) So, still try to
1296 * a salvage for that volume ID. */
1298 Log("FSYNC_com_VolError: attempting to schedule salvage for unknown "
1299 "volume %lu part %s\n", afs_printable_uint32_lu(vcom->vop->volume),
1300 vcom->vop->partName);
1301 vp = VPreAttachVolumeById_r(&error, vcom->vop->partName,
1306 if (FSYNC_partMatch(vcom, vp, 0)) {
1307 /* null out salvsync control state, as it's no longer relevant */
1308 memset(&vp->salvage, 0, sizeof(vp->salvage));
1309 VDeregisterVolOp_r(vp);
1311 if (vcom->hdr->reason == FSYNC_SALVAGE) {
1312 FSYNC_backgroundSalvage(vp);
1314 VChangeState_r(vp, VOL_STATE_ERROR);
1319 res->hdr.reason = FSYNC_WRONG_PART;
1322 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1328 #endif /* AFS_DEMAND_ATTACH_FS */
1331 * service an FSYNC request to break all callbacks for this volume.
1333 * @param[in] vcom pointer command object
1334 * @param[out] res object in which to store response packet
1336 * @return operation status
1337 * @retval SYNC_OK callback breaks scheduled for volume
1339 * @note this is an FSYNC RPC server stub
1341 * @note this procedure handles the following FSSYNC command codes:
1342 * - FSYNC_VOL_BREAKCBKS
1344 * @note demand attach fileserver only
1346 * @todo should do partition matching
1351 FSYNC_com_VolBreakCBKs(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1353 /* if the volume is being restored, break all callbacks on it */
1354 if (V_BreakVolumeCallbacks) {
1355 Log("fssync: breaking all call backs for volume %u\n",
1358 (*V_BreakVolumeCallbacks) (vcom->vop->volume);
1365 * service an FSYNC request to return the Volume object.
1367 * @param[in] vcom pointer command object
1368 * @param[out] res object in which to store response packet
1370 * @return operation status
1371 * @retval SYNC_OK volume object returned to caller
1372 * @retval SYNC_FAILED bad command packet, or failed to locate volume object
1374 * @note this is an FSYNC RPC server stub
1376 * @note this procedure handles the following FSSYNC command codes:
1382 FSYNC_com_VolQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1384 afs_int32 code = SYNC_FAILED;
1388 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1389 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1393 #ifdef AFS_DEMAND_ATTACH_FS
1394 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1395 #else /* !AFS_DEMAND_ATTACH_FS */
1396 vp = VGetVolume_r(&error, vcom->vop->volume);
1397 #endif /* !AFS_DEMAND_ATTACH_FS */
1400 if (FSYNC_partMatch(vcom, vp, 1)) {
1401 if (res->payload.len >= sizeof(Volume)) {
1402 memcpy(res->payload.buf, vp, sizeof(Volume));
1403 res->hdr.response_len += sizeof(Volume);
1406 res->hdr.reason = SYNC_REASON_PAYLOAD_TOO_BIG;
1409 res->hdr.reason = FSYNC_WRONG_PART;
1411 #ifndef AFS_DEMAND_ATTACH_FS
1415 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1423 * service an FSYNC request to return the Volume header.
1425 * @param[in] vcom pointer command object
1426 * @param[out] res object in which to store response packet
1428 * @return operation status
1429 * @retval SYNC_OK volume header returned to caller
1430 * @retval SYNC_FAILED bad command packet, or failed to locate volume header
1432 * @note this is an FSYNC RPC server stub
1434 * @note this procedure handles the following FSSYNC command codes:
1435 * - FSYNC_VOL_QUERY_HDR
1440 FSYNC_com_VolHdrQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1442 afs_int32 code = SYNC_FAILED;
1446 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1447 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1450 if (res->payload.len < sizeof(VolumeDiskData)) {
1451 res->hdr.reason = SYNC_REASON_PAYLOAD_TOO_BIG;
1455 #ifdef AFS_DEMAND_ATTACH_FS
1456 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1457 #else /* !AFS_DEMAND_ATTACH_FS */
1458 vp = VGetVolume_r(&error, vcom->vop->volume);
1462 if (FSYNC_partMatch(vcom, vp, 1)) {
1463 #ifdef AFS_DEMAND_ATTACH_FS
1464 if ((vp->header == NULL) ||
1465 !(V_attachFlags(vp) & VOL_HDR_ATTACHED) ||
1466 !(V_attachFlags(vp) & VOL_HDR_LOADED)) {
1467 res->hdr.reason = FSYNC_HDR_NOT_ATTACHED;
1470 #else /* !AFS_DEMAND_ATTACH_FS */
1471 if (!vp || !vp->header) {
1472 res->hdr.reason = FSYNC_HDR_NOT_ATTACHED;
1475 #endif /* !AFS_DEMAND_ATTACH_FS */
1477 res->hdr.reason = FSYNC_WRONG_PART;
1481 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1485 memcpy(res->payload.buf, &V_disk(vp), sizeof(VolumeDiskData));
1486 res->hdr.response_len += sizeof(VolumeDiskData);
1490 #ifndef AFS_DEMAND_ATTACH_FS
1498 #ifdef AFS_DEMAND_ATTACH_FS
1500 FSYNC_com_VolOpQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1502 afs_int32 code = SYNC_OK;
1506 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1508 if (vp && vp->pending_vol_op) {
1509 assert(sizeof(FSSYNC_VolOp_info) <= res->payload.len);
1510 memcpy(res->payload.buf, vp->pending_vol_op, sizeof(FSSYNC_VolOp_info));
1511 res->hdr.response_len += sizeof(FSSYNC_VolOp_info);
1514 res->hdr.reason = FSYNC_NO_PENDING_VOL_OP;
1516 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1524 FSYNC_com_VGQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1526 afs_int32 code = SYNC_FAILED;
1528 struct DiskPartition64 * dp;
1530 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1531 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1535 dp = VGetPartition_r(vcom->vop->partName, 0);
1537 res->hdr.reason = FSYNC_BAD_PART;
1541 assert(sizeof(FSSYNC_VGQry_response_t) <= res->payload.len);
1543 rc = VVGCache_query_r(dp, vcom->vop->volume, res->payload.buf);
1546 res->hdr.response_len += sizeof(FSSYNC_VGQry_response_t);
1550 res->hdr.reason = FSYNC_PART_SCANNING;
1553 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1564 FSYNC_com_VGUpdate(osi_socket fd, SYNC_command * com, SYNC_response * res)
1566 afs_int32 code = SYNC_FAILED;
1567 struct DiskPartition64 * dp;
1568 FSSYNC_VGUpdate_command_t * vgucom;
1571 if (com->recv_len != (sizeof(com->hdr) + sizeof(*vgucom))) {
1572 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1573 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1574 code = SYNC_COM_ERROR;
1578 vgucom = com->payload.buf;
1580 ViceLog(125, ("FSYNC_com_VGUpdate: fd %d got command for parent %lu child "
1581 "%lu partName %.16s\n", (int)fd,
1582 afs_printable_uint32_lu(vgucom->parent),
1583 afs_printable_uint32_lu(vgucom->child),
1586 if (SYNC_verifyProtocolString(vgucom->partName, sizeof(vgucom->partName))) {
1587 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1591 dp = VGetPartition_r(vgucom->partName, 0);
1593 res->hdr.reason = FSYNC_BAD_PART;
1597 switch(com->hdr.command) {
1599 rc = VVGCache_entry_add_r(dp, vgucom->parent, vgucom->child, NULL);
1603 rc = VVGCache_entry_del_r(dp, vgucom->parent, vgucom->child);
1607 Log("FSYNC_com_VGUpdate called improperly\n");
1612 /* EINVAL means the partition VGC doesn't exist at all; not really
1614 if (rc == 0 || rc == EINVAL) {
1619 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1621 res->hdr.reason = FSYNC_WHATEVER;
1629 FSYNC_com_VGScan(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1631 afs_int32 code = SYNC_FAILED;
1632 struct DiskPartition64 * dp;
1634 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1635 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1639 dp = VGetPartition_r(vcom->vop->partName, 0);
1641 res->hdr.reason = FSYNC_BAD_PART;
1645 if (VVGCache_scanStart_r(dp) == 0) {
1654 FSYNC_com_VGScanAll(FSSYNC_VolOp_command * com, SYNC_response * res)
1656 afs_int32 code = SYNC_FAILED;
1658 if (VVGCache_scanStart_r(NULL) == 0) {
1664 #endif /* AFS_DEMAND_ATTACH_FS */
1667 FSYNC_com_VnQry(osi_socket fd, SYNC_command * com, SYNC_response * res)
1669 afs_int32 code = SYNC_OK;
1670 FSSYNC_VnQry_hdr * qry = com->payload.buf;
1675 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_VnQry_hdr))) {
1676 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1677 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1678 return SYNC_COM_ERROR;
1681 ViceLog(125, ("FSYNC_com_VnQry: fd %d got command for vol %lu vnode %lu "
1682 "uniq %lu spare %lu partName %.16s\n", (int)fd,
1683 afs_printable_uint32_lu(qry->volume),
1684 afs_printable_uint32_lu(qry->vnode),
1685 afs_printable_uint32_lu(qry->unique),
1686 afs_printable_uint32_lu(qry->spare),
1689 #ifdef AFS_DEMAND_ATTACH_FS
1690 vp = VLookupVolume_r(&error, qry->volume, NULL);
1691 #else /* !AFS_DEMAND_ATTACH_FS */
1692 vp = VGetVolume_r(&error, qry->volume);
1693 #endif /* !AFS_DEMAND_ATTACH_FS */
1696 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1701 vnp = VLookupVnode(vp, qry->vnode);
1703 res->hdr.reason = FSYNC_UNKNOWN_VNID;
1708 if (Vn_class(vnp)->residentSize > res->payload.len) {
1709 res->hdr.reason = SYNC_REASON_ENCODING_ERROR;
1714 memcpy(res->payload.buf, vnp, Vn_class(vnp)->residentSize);
1715 res->hdr.response_len += Vn_class(vnp)->residentSize;
1718 #ifndef AFS_DEMAND_ATTACH_FS
1727 FSYNC_com_StatsOp(osi_socket fd, SYNC_command * com, SYNC_response * res)
1729 afs_int32 code = SYNC_OK;
1730 FSSYNC_StatsOp_command scom;
1732 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_StatsOp_hdr))) {
1733 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1734 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1735 return SYNC_COM_ERROR;
1738 scom.hdr = &com->hdr;
1739 scom.sop = (FSSYNC_StatsOp_hdr *) com->payload.buf;
1742 ViceLog(125, ("FSYNC_com_StatsOp: fd %d got command for stats: "
1743 "{vlru_generation = %lu, hash_bucket = %lu, partName = "
1744 "%.16s}\n", (int)fd,
1745 afs_printable_uint32_lu(scom.sop->args.vlru_generation),
1746 afs_printable_uint32_lu(scom.sop->args.hash_bucket),
1747 scom.sop->args.partName));
1749 switch (com->hdr.command) {
1750 case FSYNC_VOL_STATS_GENERAL:
1751 code = FSYNC_com_StatsOpGeneral(&scom, res);
1753 #ifdef AFS_DEMAND_ATTACH_FS
1754 /* statistics for the following subsystems are only tracked
1755 * for demand attach fileservers */
1756 case FSYNC_VOL_STATS_VICEP:
1757 code = FSYNC_com_StatsOpViceP(&scom, res);
1759 case FSYNC_VOL_STATS_HASH:
1760 code = FSYNC_com_StatsOpHash(&scom, res);
1762 case FSYNC_VOL_STATS_HDR:
1763 code = FSYNC_com_StatsOpHdr(&scom, res);
1765 case FSYNC_VOL_STATS_VLRU:
1766 code = FSYNC_com_StatsOpVLRU(&scom, res);
1768 #endif /* AFS_DEMAND_ATTACH_FS */
1770 code = SYNC_BAD_COMMAND;
1777 FSYNC_com_StatsOpGeneral(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1779 afs_int32 code = SYNC_OK;
1781 memcpy(res->payload.buf, &VStats, sizeof(VStats));
1782 res->hdr.response_len += sizeof(VStats);
1787 #ifdef AFS_DEMAND_ATTACH_FS
1789 FSYNC_com_StatsOpViceP(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1791 afs_int32 code = SYNC_OK;
1792 struct DiskPartition64 * dp;
1793 struct DiskPartitionStats64 * stats;
1795 if (SYNC_verifyProtocolString(scom->sop->args.partName, sizeof(scom->sop->args.partName))) {
1796 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1801 dp = VGetPartition_r(scom->sop->args.partName, 0);
1805 stats = (struct DiskPartitionStats64 *) res->payload.buf;
1806 stats->free = dp->free;
1807 stats->totalUsable = dp->totalUsable;
1808 stats->minFree = dp->minFree;
1809 stats->f_files = dp->f_files;
1810 stats->vol_list_len = dp->vol_list.len;
1812 res->hdr.response_len += sizeof(struct DiskPartitionStats64);
1820 FSYNC_com_StatsOpHash(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1822 afs_int32 code = SYNC_OK;
1823 struct VolumeHashChainStats * stats;
1824 struct VolumeHashChainHead * head;
1826 if (scom->sop->args.hash_bucket >= VolumeHashTable.Size) {
1830 head = &VolumeHashTable.Table[scom->sop->args.hash_bucket];
1831 stats = (struct VolumeHashChainStats *) res->payload.buf;
1832 stats->table_size = VolumeHashTable.Size;
1833 stats->chain_len = head->len;
1834 stats->chain_cacheCheck = head->cacheCheck;
1835 stats->chain_busy = head->busy;
1836 AssignInt64(head->looks, &stats->chain_looks);
1837 AssignInt64(head->gets, &stats->chain_gets);
1838 AssignInt64(head->reorders, &stats->chain_reorders);
1840 res->hdr.response_len += sizeof(struct VolumeHashChainStats);
1846 FSYNC_com_StatsOpHdr(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1848 afs_int32 code = SYNC_OK;
1850 memcpy(res->payload.buf, &volume_hdr_LRU.stats, sizeof(volume_hdr_LRU.stats));
1851 res->hdr.response_len += sizeof(volume_hdr_LRU.stats);
1857 FSYNC_com_StatsOpVLRU(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1859 afs_int32 code = SYNC_OK;
1861 code = SYNC_BAD_COMMAND;
1865 #endif /* AFS_DEMAND_ATTACH_FS */
1868 * populate an FSSYNC_VolOp_info object from a command packet object.
1870 * @param[in] vcom pointer to command packet
1871 * @param[out] info pointer to info object which will be populated
1873 * @note FSSYNC_VolOp_info objects are attached to Volume objects when
1874 * a volume operation is commenced.
1879 FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info)
1881 memcpy(&info->com, vcom->hdr, sizeof(SYNC_command_hdr));
1882 memcpy(&info->vop, vcom->vop, sizeof(FSSYNC_VolOp_hdr));
1883 info->vol_op_state = FSSYNC_VolOpPending;
1887 * check whether command packet partition name matches volume
1888 * object's partition name.
1890 * @param[in] vcom pointer to command packet
1891 * @param[in] vp pointer to volume object
1892 * @param[in] match_anon anon matching control flag (see note below)
1894 * @return whether partitions match
1895 * @retval 0 partitions do NOT match
1896 * @retval 1 partitions match
1898 * @note if match_anon is non-zero, then this function will return a
1899 * positive match for a zero-length partition string in the
1905 FSYNC_partMatch(FSSYNC_VolOp_command * vcom, Volume * vp, int match_anon)
1907 return ((match_anon && vcom->vop->partName[0] == 0) ||
1908 (strncmp(vcom->vop->partName, V_partition(vp)->name,
1909 sizeof(vcom->vop->partName)) == 0));
1914 FSYNC_Drop(osi_socket fd)
1916 struct offlineInfo *p;
1919 char tvolName[VMAXPATHLEN];
1922 p = OfflineVolumes[FindHandler(fd)];
1923 for (i = 0; i < MAXOFFLINEVOLUMES; i++) {
1924 if (p[i].volumeID) {
1929 sprintf(&tvolName[1], VFORMAT, afs_printable_uint32_lu(p[i].volumeID));
1930 vp = VAttachVolumeByName_r(&error, p[i].partName, tvolName,
1947 static int AcceptHandler = -1; /* handler id for accept, if turned on */
1952 if (AcceptHandler == -1) {
1953 assert(AddHandler(fssync_server_state.fd, FSYNC_newconnection));
1954 AcceptHandler = FindHandler(fssync_server_state.fd);
1961 if (AcceptHandler != -1) {
1962 assert(RemoveHandler(fssync_server_state.fd));
1967 /* The multiple FD handling code. */
1969 static osi_socket HandlerFD[MAXHANDLERS];
1970 static void (*HandlerProc[MAXHANDLERS]) (osi_socket);
1976 ObtainWriteLock(&FSYNC_handler_lock);
1977 for (i = 0; i < MAXHANDLERS; i++) {
1981 ReleaseWriteLock(&FSYNC_handler_lock);
1984 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
1986 CallHandler(struct pollfd *fds, int nfds, int mask)
1990 ObtainReadLock(&FSYNC_handler_lock);
1991 for (i = 0; i < nfds; i++) {
1992 if (fds[i].revents & mask) {
1993 handler = FindHandler_r(fds[i].fd);
1994 ReleaseReadLock(&FSYNC_handler_lock);
1995 (*HandlerProc[handler]) (fds[i].fd);
1996 ObtainReadLock(&FSYNC_handler_lock);
1999 ReleaseReadLock(&FSYNC_handler_lock);
2003 CallHandler(fd_set * fdsetp)
2006 ObtainReadLock(&FSYNC_handler_lock);
2007 for (i = 0; i < MAXHANDLERS; i++) {
2008 if (HandlerFD[i] >= 0 && FD_ISSET(HandlerFD[i], fdsetp)) {
2009 ReleaseReadLock(&FSYNC_handler_lock);
2010 (*HandlerProc[i]) (HandlerFD[i]);
2011 ObtainReadLock(&FSYNC_handler_lock);
2014 ReleaseReadLock(&FSYNC_handler_lock);
2019 AddHandler(osi_socket afd, void (*aproc) (osi_socket))
2022 ObtainWriteLock(&FSYNC_handler_lock);
2023 for (i = 0; i < MAXHANDLERS; i++)
2024 if (HandlerFD[i] == -1)
2026 if (i >= MAXHANDLERS) {
2027 ReleaseWriteLock(&FSYNC_handler_lock);
2031 HandlerProc[i] = aproc;
2032 ReleaseWriteLock(&FSYNC_handler_lock);
2037 FindHandler(osi_socket afd)
2040 ObtainReadLock(&FSYNC_handler_lock);
2041 for (i = 0; i < MAXHANDLERS; i++)
2042 if (HandlerFD[i] == afd) {
2043 ReleaseReadLock(&FSYNC_handler_lock);
2046 ReleaseReadLock(&FSYNC_handler_lock); /* just in case */
2048 return -1; /* satisfy compiler */
2052 FindHandler_r(osi_socket afd)
2055 for (i = 0; i < MAXHANDLERS; i++)
2056 if (HandlerFD[i] == afd) {
2060 return -1; /* satisfy compiler */
2064 RemoveHandler(osi_socket afd)
2066 ObtainWriteLock(&FSYNC_handler_lock);
2067 HandlerFD[FindHandler_r(afd)] = -1;
2068 ReleaseWriteLock(&FSYNC_handler_lock);
2072 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
2074 GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds)
2078 ObtainReadLock(&FSYNC_handler_lock);
2079 for (i = 0; i < MAXHANDLERS; i++)
2080 if (HandlerFD[i] != -1) {
2082 fds[fdi].fd = HandlerFD[i];
2083 fds[fdi].events = events;
2084 fds[fdi].revents = 0;
2088 ReleaseReadLock(&FSYNC_handler_lock);
2092 GetHandler(fd_set * fdsetp, int *maxfdp)
2097 ObtainReadLock(&FSYNC_handler_lock); /* just in case */
2098 for (i = 0; i < MAXHANDLERS; i++)
2099 if (HandlerFD[i] != -1) {
2100 FD_SET(HandlerFD[i], fdsetp);
2101 #ifndef AFS_NT40_ENV
2102 /* On Windows the nfds parameter to select() is ignored */
2103 if (maxfd < HandlerFD[i] || maxfd == (int)-1)
2104 maxfd = HandlerFD[i];
2105 #endif /* AFS_NT40_ENV */
2108 ReleaseReadLock(&FSYNC_handler_lock); /* just in case */
2110 #endif /* HAVE_POLL && AFS_PTHREAD_ENV */
2112 #endif /* FSSYNC_BUILD_SERVER */