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 #include <afs/afs_assert.h>
69 #include <afs/afsint.h>
71 #include <afs/errors.h>
72 #include "daemon_com.h"
73 #include "daemon_com_inline.h"
75 #include "fssync_inline.h"
79 #include <afs/afssyscalls.h>
83 #include "volume_inline.h"
84 #include "partition.h"
90 #endif /* HAVE_POLL */
92 #ifdef USE_UNIX_SOCKETS
94 #include <afs/afsutil.h>
95 #endif /* USE_UNIX_SOCKETS */
97 #ifdef FSSYNC_BUILD_SERVER
99 int (*V_BreakVolumeCallbacks) (VolumeId volume);
101 #define MAXHANDLERS 4 /* Up to 4 clients; must be at least 2, so that
102 * move = dump+restore can run on single server */
103 #define MAXOFFLINEVOLUMES 128 /* This needs to be as big as the maximum
104 * number that would be offline for 1 operation.
105 * Current winner is salvage, which needs all
106 * cloned read-only copies offline when salvaging
107 * a single read-write volume */
111 static struct offlineInfo OfflineVolumes[MAXHANDLERS][MAXOFFLINEVOLUMES];
114 * fssync server socket handle.
116 static SYNC_server_state_t fssync_server_state =
117 { -1, /* file descriptor */
118 FSSYNC_ENDPOINT_DECL, /* server endpoint */
119 FSYNC_PROTO_VERSION, /* protocol version */
120 5, /* bind() retry limit */
121 100, /* listen() queue depth */
122 "FSSYNC", /* protocol name string */
125 #ifdef AFS_DEMAND_ATTACH_FS
127 * a queue of volume pointers to salvage in the background.
129 struct fsync_salv_node {
131 Volume *vp; /**< volume to salvage */
132 unsigned char update_salv_prio; /**< whether we should update the salvage priority or not */
135 struct rx_queue head;
139 static void * FSYNC_salvageThread(void *);
140 static void FSYNC_backgroundSalvage(Volume *vp);
141 #endif /* AFS_DEMAND_ATTACH_FS */
143 /* Forward declarations */
144 static void * FSYNC_sync(void *);
145 static void FSYNC_newconnection(osi_socket afd);
146 static void FSYNC_com(osi_socket fd);
147 static void FSYNC_Drop(osi_socket fd);
148 static void AcceptOn(void);
149 static void AcceptOff(void);
150 static void InitHandler(void);
151 static int AddHandler(osi_socket fd, void (*aproc)(osi_socket));
152 static int FindHandler(osi_socket afd);
153 static int FindHandler_r(osi_socket afd);
154 static int RemoveHandler(osi_socket afd);
155 #if defined(HAVE_POLL) && defined (AFS_PTHREAD_ENV)
156 static void CallHandler(struct pollfd *fds, int nfds, int mask);
157 static void GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds);
159 static void CallHandler(fd_set * fdsetp);
160 static void GetHandler(fd_set * fdsetp, int *maxfdp);
164 static afs_int32 FSYNC_com_VolOp(osi_socket fd, SYNC_command * com, SYNC_response * res);
166 #ifdef AFS_DEMAND_ATTACH_FS
167 static afs_int32 FSYNC_com_VolError(FSSYNC_VolOp_command * com, SYNC_response * res);
169 static afs_int32 FSYNC_com_VolOn(FSSYNC_VolOp_command * com, SYNC_response * res);
170 static afs_int32 FSYNC_com_VolOff(FSSYNC_VolOp_command * com, SYNC_response * res);
171 static afs_int32 FSYNC_com_VolMove(FSSYNC_VolOp_command * com, SYNC_response * res);
172 static afs_int32 FSYNC_com_VolBreakCBKs(FSSYNC_VolOp_command * com, SYNC_response * res);
173 static afs_int32 FSYNC_com_VolDone(FSSYNC_VolOp_command * com, SYNC_response * res);
174 static afs_int32 FSYNC_com_VolQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
175 static afs_int32 FSYNC_com_VolHdrQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
176 #ifdef AFS_DEMAND_ATTACH_FS
177 static afs_int32 FSYNC_com_VGUpdate(osi_socket fd, SYNC_command * com, SYNC_response * res);
178 static afs_int32 FSYNC_com_VolOpQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
179 static afs_int32 FSYNC_com_VGQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
180 static afs_int32 FSYNC_com_VGScan(FSSYNC_VolOp_command * com, SYNC_response * res);
181 static afs_int32 FSYNC_com_VGScanAll(FSSYNC_VolOp_command * com, SYNC_response * res);
182 #endif /* AFS_DEMAND_ATTACH_FS */
184 static afs_int32 FSYNC_com_VnQry(osi_socket fd, SYNC_command * com, SYNC_response * res);
186 static afs_int32 FSYNC_com_StatsOp(osi_socket fd, SYNC_command * com, SYNC_response * res);
188 static afs_int32 FSYNC_com_StatsOpGeneral(FSSYNC_StatsOp_command * scom, SYNC_response * res);
190 #ifdef AFS_DEMAND_ATTACH_FS
191 static afs_int32 FSYNC_com_StatsOpViceP(FSSYNC_StatsOp_command * scom, SYNC_response * res);
192 static afs_int32 FSYNC_com_StatsOpHash(FSSYNC_StatsOp_command * scom, SYNC_response * res);
193 static afs_int32 FSYNC_com_StatsOpHdr(FSSYNC_StatsOp_command * scom, SYNC_response * res);
194 static afs_int32 FSYNC_com_StatsOpVLRU(FSSYNC_StatsOp_command * scom, SYNC_response * res);
197 static void FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info);
199 static int FSYNC_partMatch(FSSYNC_VolOp_command * vcom, Volume * vp, int match_anon);
203 * This lock controls access to the handler array. The overhead
204 * is minimal in non-preemptive environments.
206 struct Lock FSYNC_handler_lock;
211 #ifdef AFS_PTHREAD_ENV
213 pthread_attr_t tattr;
214 #else /* AFS_PTHREAD_ENV */
216 #endif /* AFS_PTHREAD_ENV */
218 Lock_Init(&FSYNC_handler_lock);
220 #ifdef AFS_PTHREAD_ENV
221 osi_Assert(pthread_attr_init(&tattr) == 0);
222 osi_Assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
223 osi_Assert(pthread_create(&tid, &tattr, FSYNC_sync, NULL) == 0);
224 #else /* AFS_PTHREAD_ENV */
225 osi_Assert(LWP_CreateProcess
226 (FSYNC_sync, USUAL_STACK_SIZE, USUAL_PRIORITY, (void *)0,
227 "FSYNC_sync", &pid) == LWP_SUCCESS);
228 #endif /* AFS_PTHREAD_ENV */
230 #ifdef AFS_DEMAND_ATTACH_FS
231 queue_Init(&fsync_salv.head);
232 CV_INIT(&fsync_salv.cv, "fsync salv", CV_DEFAULT, 0);
233 osi_Assert(pthread_create(&tid, &tattr, FSYNC_salvageThread, NULL) == 0);
234 #endif /* AFS_DEMAND_ATTACH_FS */
237 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
238 static struct pollfd FSYNC_readfds[MAXHANDLERS];
240 static fd_set FSYNC_readfds;
245 FSYNC_sync(void * args)
249 #ifdef AFS_PTHREAD_ENV
252 SYNC_server_state_t * state = &fssync_server_state;
253 #ifdef AFS_DEMAND_ATTACH_FS
254 VThreadOptions_t * thread_opts;
258 * For non-DAFS, only wait until we begin attaching volumes (instead
259 * of waiting until all volumes are attached), since it can take
260 * awhile until VInit == 2.
263 #endif /* AFS_DEMAND_ATTACH_FS */
265 /* we must not be called before vol package initialization, since we use
266 * vol package mutexes and conds etc */
269 SYNC_getAddr(&state->endpoint, &state->addr);
270 SYNC_cleanupSock(state);
273 (void)signal(SIGPIPE, SIG_IGN);
276 #ifdef AFS_PTHREAD_ENV
277 /* set our 'thread-id' so that the host hold table works */
278 tid = rx_NewThreadId();
279 pthread_setspecific(rx_thread_id_key, (void *)(intptr_t)tid);
280 Log("Set thread id %d for FSYNC_sync\n", tid);
281 #endif /* AFS_PTHREAD_ENV */
285 while (VInit < min_vinit) {
286 /* Let somebody else run until all volumes have been preattached
287 * (DAFS), or we have started attaching volumes (non-DAFS). This
288 * doesn't mean that all volumes have been attached.
290 #ifdef AFS_PTHREAD_ENV
291 VOL_CV_WAIT(&vol_vinit_cond);
292 #else /* AFS_PTHREAD_ENV */
293 LWP_DispatchProcess();
294 #endif /* AFS_PTHREAD_ENV */
299 state->fd = SYNC_getSock(&state->endpoint);
300 code = SYNC_bindSock(state);
303 #ifdef AFS_DEMAND_ATTACH_FS
305 * make sure the volume package is incapable of recursively executing
306 * salvsync calls on this thread, since there is a possibility of
309 thread_opts = malloc(sizeof(VThreadOptions_t));
310 if (thread_opts == NULL) {
311 Log("failed to allocate memory for thread-specific volume package options structure\n");
314 memcpy(thread_opts, &VThread_defaults, sizeof(VThread_defaults));
315 thread_opts->disallow_salvsync = 1;
316 osi_Assert(pthread_setspecific(VThread_key, thread_opts) == 0);
318 code = VVGCache_PkgInit();
319 osi_Assert(code == 0);
326 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
328 GetHandler(FSYNC_readfds, MAXHANDLERS, POLLIN|POLLPRI, &nfds);
329 if (poll(FSYNC_readfds, nfds, -1) >=1)
330 CallHandler(FSYNC_readfds, nfds, POLLIN|POLLPRI);
333 GetHandler(&FSYNC_readfds, &maxfd);
334 /* Note: check for >= 1 below is essential since IOMGR_select
335 * doesn't have exactly same semantics as select.
337 #ifdef AFS_PTHREAD_ENV
338 if (select(maxfd + 1, &FSYNC_readfds, NULL, NULL, NULL) >= 1)
339 #else /* AFS_PTHREAD_ENV */
340 if (IOMGR_Select(maxfd + 1, &FSYNC_readfds, NULL, NULL, NULL) >= 1)
341 #endif /* AFS_PTHREAD_ENV */
342 CallHandler(&FSYNC_readfds);
345 return NULL; /* hush now, little gcc */
348 #ifdef AFS_DEMAND_ATTACH_FS
350 * thread for salvaging volumes in the background.
352 * Since FSSYNC handlers cannot issue SALVSYNC requests in order to avoid
353 * deadlock issues, this thread exists so code in the FSSYNC handler thread
354 * can hand off volumes to be salvaged in the background.
356 * @param[in] args unused
358 * @note DEMAND_ATTACH_FS only
361 FSYNC_salvageThread(void * args)
364 struct fsync_salv_node *node;
369 while (queue_IsEmpty(&fsync_salv.head)) {
370 VOL_CV_WAIT(&fsync_salv.cv);
373 node = queue_First(&fsync_salv.head, fsync_salv_node);
377 if (node->update_salv_prio) {
378 if (VUpdateSalvagePriority_r(vp)) {
379 ViceLog(0, ("FSYNC_salvageThread: unable to raise salvage priority "
380 "for volume %lu\n", afs_printable_uint32_lu(vp->hashid)));
387 VCancelReservation_r(vp);
396 * salvage a volume in the background.
398 * Salvages cannot be scheduled directly from the main FSYNC thread, so
399 * instead call this function to schedule a salvage asynchronously in the
400 * FSYNC_salvageThread thread.
402 * @param[in] vp volume to pointer to salvage
406 * @note DEMAND_ATTACH_FS only
409 FSYNC_backgroundSalvage(Volume *vp)
411 struct fsync_salv_node *node;
414 VCreateReservation_r(vp);
416 node = malloc(sizeof(struct fsync_salv_node));
419 /* Save this value, to know if we should VUpdateSalvagePriority_r.
420 * We need to save it here, snce VRequestSalvage_r will change it. */
421 node->update_salv_prio = vp->salvage.requested;
423 if (VRequestSalvage_r(&ec, vp, SALVSYNC_ERROR, 0)) {
424 ViceLog(0, ("FSYNC_backgroundSalvage: unable to request salvage for volume %lu\n",
425 afs_printable_uint32_lu(vp->hashid)));
428 queue_Append(&fsync_salv.head, node);
429 CV_BROADCAST(&fsync_salv.cv);
431 #endif /* AFS_DEMAND_ATTACH_FS */
434 FSYNC_newconnection(osi_socket afd)
436 #ifdef USE_UNIX_SOCKETS
437 struct sockaddr_un other;
438 #else /* USE_UNIX_SOCKETS */
439 struct sockaddr_in other;
443 junk = sizeof(other);
444 fd = accept(afd, (struct sockaddr *)&other, &junk);
446 Log("FSYNC_newconnection: accept failed, errno==%d\n", errno);
448 } else if (!AddHandler(fd, FSYNC_com)) {
450 osi_Assert(AddHandler(fd, FSYNC_com));
454 /* this function processes commands from an fssync file descriptor (fd) */
455 afs_int32 FS_cnt = 0;
457 FSYNC_com(osi_socket fd)
461 SYNC_PROTO_BUF_DECL(com_buf);
462 SYNC_PROTO_BUF_DECL(res_buf);
464 memset(&res.hdr, 0, sizeof(res.hdr));
466 com.payload.buf = (void *)com_buf;
467 com.payload.len = SYNC_PROTO_MAX_LEN;
468 res.hdr.response_len = sizeof(res.hdr);
469 res.payload.len = SYNC_PROTO_MAX_LEN;
470 res.payload.buf = (void *)res_buf;
473 if (SYNC_getCom(&fssync_server_state, fd, &com)) {
474 Log("FSYNC_com: read failed; dropping connection (cnt=%d)\n", FS_cnt);
479 if (com.recv_len < sizeof(com.hdr)) {
480 Log("FSSYNC_com: invalid protocol message length (%u)\n", com.recv_len);
481 res.hdr.response = SYNC_COM_ERROR;
482 res.hdr.reason = SYNC_REASON_MALFORMED_PACKET;
483 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
487 if (com.hdr.proto_version != FSYNC_PROTO_VERSION) {
488 Log("FSYNC_com: invalid protocol version (%u)\n", com.hdr.proto_version);
489 res.hdr.response = SYNC_COM_ERROR;
490 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
494 if (com.hdr.command == SYNC_COM_CHANNEL_CLOSE) {
495 res.hdr.response = SYNC_OK;
496 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
498 /* don't respond, just drop; senders of SYNC_COM_CHANNEL_CLOSE
499 * never wait for a response. */
503 ViceLog(125, ("FSYNC_com: from fd %d got command %ld (%s) reason %ld (%s) "
504 "pt %ld (%s) pid %ld\n", (int)fd,
505 afs_printable_int32_ld(com.hdr.command),
506 FSYNC_com2string(com.hdr.command),
507 afs_printable_int32_ld(com.hdr.reason),
508 FSYNC_reason2string(com.hdr.reason),
509 afs_printable_int32_ld(com.hdr.programType),
510 VPTypeToString(com.hdr.programType),
511 afs_printable_int32_ld(com.hdr.pid)));
513 res.hdr.com_seq = com.hdr.com_seq;
516 switch (com.hdr.command) {
518 case FSYNC_VOL_ATTACH:
519 case FSYNC_VOL_LEAVE_OFF:
521 case FSYNC_VOL_FORCE_ERROR:
522 case FSYNC_VOL_LISTVOLUMES:
523 case FSYNC_VOL_NEEDVOLUME:
525 case FSYNC_VOL_BREAKCBKS:
527 case FSYNC_VOL_QUERY:
528 case FSYNC_VOL_QUERY_HDR:
529 case FSYNC_VOL_QUERY_VOP:
530 #ifdef AFS_DEMAND_ATTACH_FS
533 case FSYNC_VG_SCAN_ALL:
535 res.hdr.response = FSYNC_com_VolOp(fd, &com, &res);
537 case FSYNC_VOL_STATS_GENERAL:
538 case FSYNC_VOL_STATS_VICEP:
539 case FSYNC_VOL_STATS_HASH:
540 case FSYNC_VOL_STATS_HDR:
541 case FSYNC_VOL_STATS_VLRU:
542 res.hdr.response = FSYNC_com_StatsOp(fd, &com, &res);
544 case FSYNC_VOL_QUERY_VNODE:
545 res.hdr.response = FSYNC_com_VnQry(fd, &com, &res);
547 #ifdef AFS_DEMAND_ATTACH_FS
550 res.hdr.response = FSYNC_com_VGUpdate(fd, &com, &res);
554 res.hdr.response = SYNC_BAD_COMMAND;
559 ViceLog(125, ("FSYNC_com: fd %d responding with code %ld (%s) reason %ld "
561 afs_printable_int32_ld(res.hdr.response),
562 SYNC_res2string(res.hdr.response),
563 afs_printable_int32_ld(res.hdr.reason),
564 FSYNC_reason2string(res.hdr.reason)));
567 SYNC_putRes(&fssync_server_state, fd, &res);
570 if (res.hdr.flags & SYNC_FLAG_CHANNEL_SHUTDOWN) {
576 FSYNC_com_VolOp(osi_socket fd, SYNC_command * com, SYNC_response * res)
579 afs_int32 code = SYNC_OK;
580 FSSYNC_VolOp_command vcom;
582 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_VolOp_hdr))) {
583 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
584 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
585 return SYNC_COM_ERROR;
588 vcom.hdr = &com->hdr;
589 vcom.vop = (FSSYNC_VolOp_hdr *) com->payload.buf;
592 vcom.volumes = OfflineVolumes[FindHandler(fd)];
593 for (vcom.v = NULL, i = 0; i < MAXOFFLINEVOLUMES; i++) {
594 if ((vcom.volumes[i].volumeID == vcom.vop->volume) &&
595 (strncmp(vcom.volumes[i].partName, vcom.vop->partName,
596 sizeof(vcom.volumes[i].partName)) == 0)) {
597 vcom.v = &vcom.volumes[i];
602 ViceLog(125, ("FSYNC_com_VolOp: fd %d got command for vol %lu part %.16s\n",
603 (int)fd, afs_printable_uint32_lu(vcom.vop->volume),
604 vcom.vop->partName));
606 switch (com->hdr.command) {
608 case FSYNC_VOL_ATTACH:
609 case FSYNC_VOL_LEAVE_OFF:
610 code = FSYNC_com_VolOn(&vcom, res);
613 case FSYNC_VOL_NEEDVOLUME:
614 code = FSYNC_com_VolOff(&vcom, res);
616 case FSYNC_VOL_LISTVOLUMES:
620 code = FSYNC_com_VolMove(&vcom, res);
622 case FSYNC_VOL_BREAKCBKS:
623 code = FSYNC_com_VolBreakCBKs(&vcom, res);
626 code = FSYNC_com_VolDone(&vcom, res);
628 case FSYNC_VOL_QUERY:
629 code = FSYNC_com_VolQuery(&vcom, res);
631 case FSYNC_VOL_QUERY_HDR:
632 code = FSYNC_com_VolHdrQuery(&vcom, res);
634 #ifdef AFS_DEMAND_ATTACH_FS
635 case FSYNC_VOL_FORCE_ERROR:
636 code = FSYNC_com_VolError(&vcom, res);
638 case FSYNC_VOL_QUERY_VOP:
639 code = FSYNC_com_VolOpQuery(&vcom, res);
642 code = FSYNC_com_VGQuery(&vcom, res);
645 code = FSYNC_com_VGScan(&vcom, res);
647 case FSYNC_VG_SCAN_ALL:
648 code = FSYNC_com_VGScanAll(&vcom, res);
650 #endif /* AFS_DEMAND_ATTACH_FS */
652 code = SYNC_BAD_COMMAND;
659 * service an FSYNC request to bring a volume online.
661 * @param[in] vcom pointer command object
662 * @param[out] res object in which to store response packet
664 * @return operation status
665 * @retval SYNC_OK volume transitioned online
666 * @retval SYNC_FAILED invalid command protocol message
667 * @retval SYNC_DENIED operation could not be completed
669 * @note this is an FSYNC RPC server stub
671 * @note this procedure handles the following FSSYNC command codes:
674 * - FSYNC_VOL_LEAVE_OFF
676 * @note the supplementary reason code contains additional details.
677 * When SYNC_DENIED is returned, the specific reason is
678 * placed in the response packet reason field.
683 FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
685 afs_int32 code = SYNC_OK;
686 #ifndef AFS_DEMAND_ATTACH_FS
687 char tvolName[VMAXPATHLEN];
692 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
693 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
698 /* so, we need to attach the volume */
700 #ifdef AFS_DEMAND_ATTACH_FS
701 /* check DAFS permissions */
702 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
704 FSYNC_partMatch(vcom, vp, 1) &&
705 vp->pending_vol_op &&
706 (vcom->hdr->programType != vp->pending_vol_op->com.programType)) {
707 /* a different program has this volume checked out. deny. */
708 Log("FSYNC_VolOn: WARNING: program type %u has attempted to manipulate "
709 "state for volume %u using command code %u while the volume is "
710 "checked out by program type %u for command code %u.\n",
711 vcom->hdr->programType,
714 vp->pending_vol_op->com.programType,
715 vp->pending_vol_op->com.command);
717 res->hdr.reason = FSYNC_EXCLUSIVE;
723 vcom->v->volumeID = 0;
726 if (vcom->hdr->command == FSYNC_VOL_LEAVE_OFF) {
727 /* nothing much to do if we're leaving the volume offline */
728 #ifdef AFS_DEMAND_ATTACH_FS
730 if (FSYNC_partMatch(vcom, vp, 1)) {
731 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
732 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
733 VChangeState_r(vp, VOL_STATE_UNATTACHED);
734 VDeregisterVolOp_r(vp);
737 res->hdr.reason = FSYNC_BAD_STATE;
741 res->hdr.reason = FSYNC_WRONG_PART;
745 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
751 #ifdef AFS_DEMAND_ATTACH_FS
752 /* first, check to see whether we have such a volume defined */
753 vp = VPreAttachVolumeById_r(&error,
757 VDeregisterVolOp_r(vp);
759 #else /* !AFS_DEMAND_ATTACH_FS */
761 snprintf(&tvolName[1], sizeof(tvolName)-1, VFORMAT, afs_printable_uint32_lu(vcom->vop->volume));
762 tvolName[sizeof(tvolName)-1] = '\0';
764 vp = VAttachVolumeByName_r(&error, vcom->vop->partName, tvolName,
770 res->hdr.reason = error;
772 #endif /* !AFS_DEMAND_ATTACH_FS */
779 * service an FSYNC request to take a volume offline.
781 * @param[in] vcom pointer command object
782 * @param[out] res object in which to store response packet
784 * @return operation status
785 * @retval SYNC_OK volume transitioned offline
786 * @retval SYNC_FAILED invalid command protocol message
787 * @retval SYNC_DENIED operation could not be completed
789 * @note this is an FSYNC RPC server stub
791 * @note this procedure handles the following FSSYNC command codes:
793 * - FSYNC_VOL_NEEDVOLUME
795 * @note the supplementary reason code contains additional details.
796 * When SYNC_DENIED is returned, the specific reason is
797 * placed in the response packet reason field.
802 FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
804 FSSYNC_VolOp_info info;
805 afs_int32 code = SYNC_OK;
809 #ifdef AFS_DEMAND_ATTACH_FS
810 Volume *nvp, *rvp = NULL;
813 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
814 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
819 /* not already offline, we need to find a slot for newly offline volume */
820 if (vcom->hdr->programType == debugUtility) {
821 /* debug utilities do not have their operations tracked */
825 for (i = 0; i < MAXOFFLINEVOLUMES; i++) {
826 if (vcom->volumes[i].volumeID == 0) {
827 vcom->v = &vcom->volumes[i];
837 FSYNC_com_to_info(vcom, &info);
839 #ifdef AFS_DEMAND_ATTACH_FS
840 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
842 vp = VGetVolume_r(&error, vcom->vop->volume);
846 if (!FSYNC_partMatch(vcom, vp, 1)) {
847 /* volume on desired partition is not online, so we
848 * should treat this as an offline volume.
850 #ifndef AFS_DEMAND_ATTACH_FS
858 #ifdef AFS_DEMAND_ATTACH_FS
860 ProgramType type = (ProgramType) vcom->hdr->programType;
862 /* do initial filtering of requests */
864 /* enforce mutual exclusion for volume ops */
865 if (vp->pending_vol_op) {
866 if (vp->pending_vol_op->com.programType != type) {
867 if (vp->pending_vol_op->com.command == FSYNC_VOL_OFF &&
868 vp->pending_vol_op->com.reason == FSYNC_SALVAGE) {
870 Log("denying offline request for volume %lu; volume is salvaging\n",
871 afs_printable_uint32_lu(vp->hashid));
873 res->hdr.reason = FSYNC_SALVAGE;
876 Log("volume %u already checked out\n", vp->hashid);
878 Log("vp->vop = { com = { ver=%u, prog=%d, com=%d, reason=%d, len=%u, flags=0x%x }, vop = { vol=%u, part='%s' } }\n",
879 vp->pending_vol_op->com.proto_version,
880 vp->pending_vol_op->com.programType,
881 vp->pending_vol_op->com.command,
882 vp->pending_vol_op->com.reason,
883 vp->pending_vol_op->com.command_len,
884 vp->pending_vol_op->com.flags,
885 vp->pending_vol_op->vop.volume,
886 vp->pending_vol_op->vop.partName );
887 Log("vcom = { com = { ver=%u, prog=%d, com=%d, reason=%d, len=%u, flags=0x%x } , vop = { vol=%u, part='%s' } }\n",
888 vcom->hdr->proto_version,
889 vcom->hdr->programType,
892 vcom->hdr->command_len,
895 vcom->vop->partName);
896 res->hdr.reason = FSYNC_EXCLUSIVE;
899 Log("warning: volume %u recursively checked out by programType id %d\n",
900 vp->hashid, vcom->hdr->programType);
904 /* wait for exclusive ops, so we have an accurate picture of the
905 * vol attach state */
906 VCreateReservation_r(vp);
907 VWaitExclusiveState_r(vp);
910 /* filter based upon requestor
912 * volume utilities / volserver are not allowed to check out
913 * volumes which are in an error state
915 * unknown utility programs will be denied on principal
919 /* it is possible for the salvageserver to checkout a
920 * volume for salvage before its scheduling request
921 * has been sent to the salvageserver */
922 if (vp->salvage.requested && !vp->salvage.scheduled) {
923 vp->salvage.scheduled = 1;
930 if (V_attachState(vp) == VOL_STATE_SALVAGING ||
931 vp->salvage.requested) {
933 Log("denying offline request for volume %lu; volume is in salvaging state\n",
934 afs_printable_uint32_lu(vp->hashid));
935 res->hdr.reason = FSYNC_SALVAGE;
937 /* the volume hasn't been checked out yet by the salvager,
938 * but we think the volume is salvaging; schedule a
939 * a salvage to update the salvage priority */
940 FSYNC_backgroundSalvage(vp);
944 if (VIsErrorState(V_attachState(vp))) {
950 Log("bad program type passed to FSSYNC\n");
954 /* short circuit for offline volume states
955 * so we can avoid I/O penalty of attachment */
956 switch (V_attachState(vp)) {
957 case VOL_STATE_UNATTACHED:
958 case VOL_STATE_PREATTACHED:
959 case VOL_STATE_SALVAGING:
960 case VOL_STATE_ERROR:
961 /* register the volume operation metadata with the volume
963 * if the volume is currently pre-attached, attach2()
964 * will evaluate the vol op metadata to determine whether
965 * attaching the volume would be safe */
966 VRegisterVolOp_r(vp, &info);
967 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
973 /* convert to heavyweight ref */
974 nvp = VGetVolumeByVp_r(&error, vp);
977 * It's possible for VGetVolumeByVp_r to have dropped and
978 * re-acquired VOL_LOCK, so volume state may have changed
979 * back to one of the states we tested for above. Since
980 * GetVolume can return NULL in some of those states, just
981 * test for the states again here.
983 switch (V_attachState(vp)) {
984 case VOL_STATE_UNATTACHED:
985 case VOL_STATE_PREATTACHED:
986 case VOL_STATE_SALVAGING:
987 case VOL_STATE_ERROR:
988 /* register the volume operation metadata with the volume
990 * if the volume is currently pre-attached, attach2()
991 * will evaluate the vol op metadata to determine whether
992 * attaching the volume would be safe */
993 VRegisterVolOp_r(vp, &info);
994 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
1000 Log("FSYNC_com_VolOff: failed to get heavyweight reference to volume %u\n",
1002 res->hdr.reason = FSYNC_VOL_PKG_ERROR;
1004 } else if (nvp != vp) {
1005 /* i don't think this should ever happen, but just in case... */
1006 Log("FSYNC_com_VolOff: warning: potentially dangerous race detected\n");
1010 /* kill off lightweight ref to ensure we can't deadlock against ourselves later... */
1011 VCancelReservation_r(rvp);
1014 /* register the volume operation metadata with the volume */
1015 VRegisterVolOp_r(vp, &info);
1018 #endif /* AFS_DEMAND_ATTACH_FS */
1021 if (VVolOpLeaveOnline_r(vp, &info)) {
1022 VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT); /* At least get volume stats right */
1024 Log("FSYNC: Volume %u (%s) was left on line for an external %s request\n",
1025 V_id(vp), V_name(vp),
1026 vcom->hdr->reason == V_CLONE ? "clone" :
1027 vcom->hdr->reason == V_READONLY ? "readonly" :
1028 vcom->hdr->reason == V_DUMP ? "dump" :
1029 vcom->hdr->reason == FSYNC_SALVAGE ? "salvage" :
1032 #ifdef AFS_DEMAND_ATTACH_FS
1033 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOnline;
1037 if (VVolOpSetVBusy_r(vp, &info)) {
1038 vp->specialStatus = VBUSY;
1041 /* remember what volume we got, so we can keep track of how
1042 * many volumes the volserver or whatever is using. Note that
1043 * vp is valid since leaveonline is only set when vp is valid.
1046 vcom->v->volumeID = vcom->vop->volume;
1047 strlcpy(vcom->v->partName, vp->partition->name, sizeof(vcom->v->partName));
1050 #ifdef AFS_DEMAND_ATTACH_FS
1051 VOfflineForVolOp_r(&error, vp, "A volume utility is running.");
1053 osi_Assert(vp->nUsers==0);
1054 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline;
1057 VDeregisterVolOp_r(vp);
1061 VOffline_r(vp, "A volume utility is running.");
1072 #ifdef AFS_DEMAND_ATTACH_FS
1074 VCancelReservation_r(rvp);
1081 * service an FSYNC request to mark a volume as moved.
1083 * @param[in] vcom pointer command object
1084 * @param[out] res object in which to store response packet
1086 * @return operation status
1087 * @retval SYNC_OK volume marked as moved to a remote server
1088 * @retval SYNC_FAILED invalid command protocol message
1089 * @retval SYNC_DENIED current volume state does not permit this operation
1091 * @note this is an FSYNC RPC server stub
1093 * @note this operation also breaks all callbacks for the given volume
1095 * @note this procedure handles the following FSSYNC command codes:
1098 * @note the supplementary reason code contains additional details. For
1099 * instance, SYNC_OK is still returned when the partition specified
1100 * does not match the one registered in the volume object -- reason
1101 * will be FSYNC_WRONG_PART in this case.
1106 FSYNC_com_VolMove(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1108 afs_int32 code = SYNC_DENIED;
1112 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1113 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1118 /* Yuch: the "reason" for the move is the site it got moved to... */
1119 /* still set specialStatus so we stop sending back VBUSY.
1120 * also should still break callbacks. Note that I don't know
1121 * how to tell if we should break all or not, so we just do it
1122 * since it doesn't matter much if we do an extra break
1123 * volume callbacks on a volume move within the same server */
1124 #ifdef AFS_DEMAND_ATTACH_FS
1125 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1127 vp = VGetVolume_r(&error, vcom->vop->volume);
1130 if (FSYNC_partMatch(vcom, vp, 1)) {
1131 #ifdef AFS_DEMAND_ATTACH_FS
1132 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1133 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
1136 vp->specialStatus = VMOVED;
1137 #ifdef AFS_DEMAND_ATTACH_FS
1139 res->hdr.reason = FSYNC_BAD_STATE;
1143 res->hdr.reason = FSYNC_WRONG_PART;
1145 #ifndef AFS_DEMAND_ATTACH_FS
1147 #endif /* !AFS_DEMAND_ATTACH_FS */
1149 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1152 if ((code == SYNC_OK) && (V_BreakVolumeCallbacks != NULL)) {
1153 Log("fssync: volume %u moved to %x; breaking all call backs\n",
1154 vcom->vop->volume, vcom->hdr->reason);
1156 (*V_BreakVolumeCallbacks) (vcom->vop->volume);
1166 * service an FSYNC request to mark a volume as destroyed.
1168 * @param[in] vcom pointer command object
1169 * @param[out] res object in which to store response packet
1171 * @return operation status
1172 * @retval SYNC_OK volume marked as destroyed
1173 * @retval SYNC_FAILED invalid command protocol message
1174 * @retval SYNC_DENIED current volume state does not permit this operation
1176 * @note this is an FSYNC RPC server stub
1178 * @note this procedure handles the following FSSYNC command codes:
1181 * @note the supplementary reason code contains additional details. For
1182 * instance, SYNC_OK is still returned when the partition specified
1183 * does not match the one registered in the volume object -- reason
1184 * will be FSYNC_WRONG_PART in this case.
1189 FSYNC_com_VolDone(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1191 afs_int32 code = SYNC_FAILED;
1195 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1196 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1200 /* don't try to put online, this call is made only after deleting
1201 * a volume, in which case we want to remove the vol # from the
1202 * OfflineVolumes array only */
1204 vcom->v->volumeID = 0;
1206 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1208 if (FSYNC_partMatch(vcom, vp, 1)) {
1209 #ifdef AFS_DEMAND_ATTACH_FS
1210 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1211 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
1213 /* Change state to DELETED, not UNATTACHED, so clients get
1214 * a VNOVOL error when they try to access from now on. */
1216 VChangeState_r(vp, VOL_STATE_DELETED);
1217 VDeregisterVolOp_r(vp);
1219 /* Someday we should free the vp, too, after about 2 hours,
1220 * possibly by putting the vp back on the VLRU. */
1225 res->hdr.reason = FSYNC_BAD_STATE;
1227 #else /* AFS_DEMAND_ATTACH_FS */
1228 if (!vp->specialStatus) {
1229 vp->specialStatus = VNOVOL;
1232 #endif /* !AFS_DEMAND_ATTACH_FS */
1234 code = SYNC_OK; /* XXX is this really a good idea? */
1235 res->hdr.reason = FSYNC_WRONG_PART;
1238 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1245 #ifdef AFS_DEMAND_ATTACH_FS
1247 * service an FSYNC request to transition a volume to the hard error state.
1249 * @param[in] vcom pointer command object
1250 * @param[out] res object in which to store response packet
1252 * @return operation status
1253 * @retval SYNC_OK volume transitioned to hard error state
1254 * @retval SYNC_FAILED invalid command protocol message
1255 * @retval SYNC_DENIED (see note)
1257 * @note this is an FSYNC RPC server stub
1259 * @note this procedure handles the following FSSYNC command codes:
1260 * - FSYNC_VOL_FORCE_ERROR
1262 * @note SYNC_DENIED is returned in the following cases:
1263 * - no partition name is specified (reason field set to
1264 * FSYNC_WRONG_PART).
1265 * - volume id not known to fileserver (reason field set
1266 * to FSYNC_UNKNOWN_VOLID).
1268 * @note demand attach fileserver only
1273 FSYNC_com_VolError(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1277 afs_int32 code = SYNC_FAILED;
1279 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1280 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1284 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1286 if (!vp && vcom->hdr->reason == FSYNC_SALVAGE) {
1287 /* The requested volume doesn't seem to exist. However, it is possible
1288 * that this is triggered by trying to create or clone a volume that
1289 * was prevented from succeeding by a half-created volume in the way.
1290 * (e.g. we tried to create volume X, but volume X exists except that
1291 * its .vol header was deleted for some reason) So, still try to
1292 * a salvage for that volume ID. */
1294 Log("FSYNC_com_VolError: attempting to schedule salvage for unknown "
1295 "volume %lu part %s\n", afs_printable_uint32_lu(vcom->vop->volume),
1296 vcom->vop->partName);
1297 vp = VPreAttachVolumeById_r(&error, vcom->vop->partName,
1302 if (FSYNC_partMatch(vcom, vp, 0)) {
1303 /* null out salvsync control state, as it's no longer relevant */
1304 memset(&vp->salvage, 0, sizeof(vp->salvage));
1305 VDeregisterVolOp_r(vp);
1307 if (vcom->hdr->reason == FSYNC_SALVAGE) {
1308 FSYNC_backgroundSalvage(vp);
1310 VChangeState_r(vp, VOL_STATE_ERROR);
1315 res->hdr.reason = FSYNC_WRONG_PART;
1318 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1324 #endif /* AFS_DEMAND_ATTACH_FS */
1327 * service an FSYNC request to break all callbacks for this volume.
1329 * @param[in] vcom pointer command object
1330 * @param[out] res object in which to store response packet
1332 * @return operation status
1333 * @retval SYNC_OK callback breaks scheduled for volume
1335 * @note this is an FSYNC RPC server stub
1337 * @note this procedure handles the following FSSYNC command codes:
1338 * - FSYNC_VOL_BREAKCBKS
1340 * @note demand attach fileserver only
1342 * @todo should do partition matching
1347 FSYNC_com_VolBreakCBKs(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1349 /* if the volume is being restored, break all callbacks on it */
1350 if (V_BreakVolumeCallbacks) {
1351 Log("fssync: breaking all call backs for volume %u\n",
1354 (*V_BreakVolumeCallbacks) (vcom->vop->volume);
1361 * service an FSYNC request to return the Volume object.
1363 * @param[in] vcom pointer command object
1364 * @param[out] res object in which to store response packet
1366 * @return operation status
1367 * @retval SYNC_OK volume object returned to caller
1368 * @retval SYNC_FAILED bad command packet, or failed to locate volume object
1370 * @note this is an FSYNC RPC server stub
1372 * @note this procedure handles the following FSSYNC command codes:
1378 FSYNC_com_VolQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1380 afs_int32 code = SYNC_FAILED;
1384 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1385 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1389 #ifdef AFS_DEMAND_ATTACH_FS
1390 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1391 #else /* !AFS_DEMAND_ATTACH_FS */
1392 vp = VGetVolume_r(&error, vcom->vop->volume);
1393 #endif /* !AFS_DEMAND_ATTACH_FS */
1396 if (FSYNC_partMatch(vcom, vp, 1)) {
1397 if (res->payload.len >= sizeof(Volume)) {
1398 memcpy(res->payload.buf, vp, sizeof(Volume));
1399 res->hdr.response_len += sizeof(Volume);
1402 res->hdr.reason = SYNC_REASON_PAYLOAD_TOO_BIG;
1405 res->hdr.reason = FSYNC_WRONG_PART;
1407 #ifndef AFS_DEMAND_ATTACH_FS
1411 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1419 * service an FSYNC request to return the Volume header.
1421 * @param[in] vcom pointer command object
1422 * @param[out] res object in which to store response packet
1424 * @return operation status
1425 * @retval SYNC_OK volume header returned to caller
1426 * @retval SYNC_FAILED bad command packet, or failed to locate volume header
1428 * @note this is an FSYNC RPC server stub
1430 * @note this procedure handles the following FSSYNC command codes:
1431 * - FSYNC_VOL_QUERY_HDR
1436 FSYNC_com_VolHdrQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1438 afs_int32 code = SYNC_FAILED;
1442 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1443 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1446 if (res->payload.len < sizeof(VolumeDiskData)) {
1447 res->hdr.reason = SYNC_REASON_PAYLOAD_TOO_BIG;
1451 #ifdef AFS_DEMAND_ATTACH_FS
1452 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1453 #else /* !AFS_DEMAND_ATTACH_FS */
1454 vp = VGetVolume_r(&error, vcom->vop->volume);
1458 if (FSYNC_partMatch(vcom, vp, 1)) {
1459 #ifdef AFS_DEMAND_ATTACH_FS
1460 if ((vp->header == NULL) ||
1461 !(V_attachFlags(vp) & VOL_HDR_ATTACHED) ||
1462 !(V_attachFlags(vp) & VOL_HDR_LOADED)) {
1463 res->hdr.reason = FSYNC_HDR_NOT_ATTACHED;
1466 #else /* !AFS_DEMAND_ATTACH_FS */
1467 if (!vp || !vp->header) {
1468 res->hdr.reason = FSYNC_HDR_NOT_ATTACHED;
1471 #endif /* !AFS_DEMAND_ATTACH_FS */
1473 res->hdr.reason = FSYNC_WRONG_PART;
1477 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1481 memcpy(res->payload.buf, &V_disk(vp), sizeof(VolumeDiskData));
1482 res->hdr.response_len += sizeof(VolumeDiskData);
1486 #ifndef AFS_DEMAND_ATTACH_FS
1494 #ifdef AFS_DEMAND_ATTACH_FS
1496 FSYNC_com_VolOpQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1498 afs_int32 code = SYNC_OK;
1502 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1504 if (vp && vp->pending_vol_op) {
1505 osi_Assert(sizeof(FSSYNC_VolOp_info) <= res->payload.len);
1506 memcpy(res->payload.buf, vp->pending_vol_op, sizeof(FSSYNC_VolOp_info));
1507 res->hdr.response_len += sizeof(FSSYNC_VolOp_info);
1510 res->hdr.reason = FSYNC_NO_PENDING_VOL_OP;
1512 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1520 FSYNC_com_VGQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1522 afs_int32 code = SYNC_FAILED;
1524 struct DiskPartition64 * dp;
1526 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1527 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1531 dp = VGetPartition_r(vcom->vop->partName, 0);
1533 res->hdr.reason = FSYNC_BAD_PART;
1537 osi_Assert(sizeof(FSSYNC_VGQry_response_t) <= res->payload.len);
1539 rc = VVGCache_query_r(dp, vcom->vop->volume, res->payload.buf);
1542 res->hdr.response_len += sizeof(FSSYNC_VGQry_response_t);
1546 res->hdr.reason = FSYNC_PART_SCANNING;
1549 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1560 FSYNC_com_VGUpdate(osi_socket fd, SYNC_command * com, SYNC_response * res)
1562 afs_int32 code = SYNC_FAILED;
1563 struct DiskPartition64 * dp;
1564 FSSYNC_VGUpdate_command_t * vgucom;
1567 if (com->recv_len != (sizeof(com->hdr) + sizeof(*vgucom))) {
1568 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1569 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1570 code = SYNC_COM_ERROR;
1574 vgucom = com->payload.buf;
1576 ViceLog(125, ("FSYNC_com_VGUpdate: fd %d got command for parent %lu child "
1577 "%lu partName %.16s\n", (int)fd,
1578 afs_printable_uint32_lu(vgucom->parent),
1579 afs_printable_uint32_lu(vgucom->child),
1582 if (SYNC_verifyProtocolString(vgucom->partName, sizeof(vgucom->partName))) {
1583 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1587 dp = VGetPartition_r(vgucom->partName, 0);
1589 res->hdr.reason = FSYNC_BAD_PART;
1593 switch(com->hdr.command) {
1595 rc = VVGCache_entry_add_r(dp, vgucom->parent, vgucom->child, NULL);
1599 rc = VVGCache_entry_del_r(dp, vgucom->parent, vgucom->child);
1603 Log("FSYNC_com_VGUpdate called improperly\n");
1608 /* EINVAL means the partition VGC doesn't exist at all; not really
1610 if (rc == 0 || rc == EINVAL) {
1615 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1617 res->hdr.reason = FSYNC_WHATEVER;
1625 FSYNC_com_VGScan(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1627 afs_int32 code = SYNC_FAILED;
1628 struct DiskPartition64 * dp;
1630 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1631 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1635 dp = VGetPartition_r(vcom->vop->partName, 0);
1637 res->hdr.reason = FSYNC_BAD_PART;
1641 if (VVGCache_scanStart_r(dp) == 0) {
1650 FSYNC_com_VGScanAll(FSSYNC_VolOp_command * com, SYNC_response * res)
1652 afs_int32 code = SYNC_FAILED;
1654 if (VVGCache_scanStart_r(NULL) == 0) {
1660 #endif /* AFS_DEMAND_ATTACH_FS */
1663 FSYNC_com_VnQry(osi_socket fd, SYNC_command * com, SYNC_response * res)
1665 afs_int32 code = SYNC_OK;
1666 FSSYNC_VnQry_hdr * qry = com->payload.buf;
1671 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_VnQry_hdr))) {
1672 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1673 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1674 return SYNC_COM_ERROR;
1677 ViceLog(125, ("FSYNC_com_VnQry: fd %d got command for vol %lu vnode %lu "
1678 "uniq %lu spare %lu partName %.16s\n", (int)fd,
1679 afs_printable_uint32_lu(qry->volume),
1680 afs_printable_uint32_lu(qry->vnode),
1681 afs_printable_uint32_lu(qry->unique),
1682 afs_printable_uint32_lu(qry->spare),
1685 #ifdef AFS_DEMAND_ATTACH_FS
1686 vp = VLookupVolume_r(&error, qry->volume, NULL);
1687 #else /* !AFS_DEMAND_ATTACH_FS */
1688 vp = VGetVolume_r(&error, qry->volume);
1689 #endif /* !AFS_DEMAND_ATTACH_FS */
1692 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1697 vnp = VLookupVnode(vp, qry->vnode);
1699 res->hdr.reason = FSYNC_UNKNOWN_VNID;
1704 if (Vn_class(vnp)->residentSize > res->payload.len) {
1705 res->hdr.reason = SYNC_REASON_ENCODING_ERROR;
1710 memcpy(res->payload.buf, vnp, Vn_class(vnp)->residentSize);
1711 res->hdr.response_len += Vn_class(vnp)->residentSize;
1714 #ifndef AFS_DEMAND_ATTACH_FS
1723 FSYNC_com_StatsOp(osi_socket fd, SYNC_command * com, SYNC_response * res)
1725 afs_int32 code = SYNC_OK;
1726 FSSYNC_StatsOp_command scom;
1728 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_StatsOp_hdr))) {
1729 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1730 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1731 return SYNC_COM_ERROR;
1734 scom.hdr = &com->hdr;
1735 scom.sop = (FSSYNC_StatsOp_hdr *) com->payload.buf;
1738 ViceLog(125, ("FSYNC_com_StatsOp: fd %d got command for stats: "
1739 "{vlru_generation = %lu, hash_bucket = %lu, partName = "
1740 "%.16s}\n", (int)fd,
1741 afs_printable_uint32_lu(scom.sop->args.vlru_generation),
1742 afs_printable_uint32_lu(scom.sop->args.hash_bucket),
1743 scom.sop->args.partName));
1745 switch (com->hdr.command) {
1746 case FSYNC_VOL_STATS_GENERAL:
1747 code = FSYNC_com_StatsOpGeneral(&scom, res);
1749 #ifdef AFS_DEMAND_ATTACH_FS
1750 /* statistics for the following subsystems are only tracked
1751 * for demand attach fileservers */
1752 case FSYNC_VOL_STATS_VICEP:
1753 code = FSYNC_com_StatsOpViceP(&scom, res);
1755 case FSYNC_VOL_STATS_HASH:
1756 code = FSYNC_com_StatsOpHash(&scom, res);
1758 case FSYNC_VOL_STATS_HDR:
1759 code = FSYNC_com_StatsOpHdr(&scom, res);
1761 case FSYNC_VOL_STATS_VLRU:
1762 code = FSYNC_com_StatsOpVLRU(&scom, res);
1764 #endif /* AFS_DEMAND_ATTACH_FS */
1766 code = SYNC_BAD_COMMAND;
1773 FSYNC_com_StatsOpGeneral(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1775 afs_int32 code = SYNC_OK;
1777 memcpy(res->payload.buf, &VStats, sizeof(VStats));
1778 res->hdr.response_len += sizeof(VStats);
1783 #ifdef AFS_DEMAND_ATTACH_FS
1785 FSYNC_com_StatsOpViceP(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1787 afs_int32 code = SYNC_OK;
1788 struct DiskPartition64 * dp;
1789 struct DiskPartitionStats64 * stats;
1791 if (SYNC_verifyProtocolString(scom->sop->args.partName, sizeof(scom->sop->args.partName))) {
1792 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1797 dp = VGetPartition_r(scom->sop->args.partName, 0);
1801 stats = (struct DiskPartitionStats64 *) res->payload.buf;
1802 stats->free = dp->free;
1803 stats->totalUsable = dp->totalUsable;
1804 stats->minFree = dp->minFree;
1805 stats->f_files = dp->f_files;
1806 stats->vol_list_len = dp->vol_list.len;
1808 res->hdr.response_len += sizeof(struct DiskPartitionStats64);
1816 FSYNC_com_StatsOpHash(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1818 afs_int32 code = SYNC_OK;
1819 struct VolumeHashChainStats * stats;
1820 struct VolumeHashChainHead * head;
1822 if (scom->sop->args.hash_bucket >= VolumeHashTable.Size) {
1826 head = &VolumeHashTable.Table[scom->sop->args.hash_bucket];
1827 stats = (struct VolumeHashChainStats *) res->payload.buf;
1828 stats->table_size = VolumeHashTable.Size;
1829 stats->chain_len = head->len;
1830 stats->chain_cacheCheck = head->cacheCheck;
1831 stats->chain_busy = head->busy;
1832 AssignInt64(head->looks, &stats->chain_looks);
1833 AssignInt64(head->gets, &stats->chain_gets);
1834 AssignInt64(head->reorders, &stats->chain_reorders);
1836 res->hdr.response_len += sizeof(struct VolumeHashChainStats);
1842 FSYNC_com_StatsOpHdr(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1844 afs_int32 code = SYNC_OK;
1846 memcpy(res->payload.buf, &volume_hdr_LRU.stats, sizeof(volume_hdr_LRU.stats));
1847 res->hdr.response_len += sizeof(volume_hdr_LRU.stats);
1853 FSYNC_com_StatsOpVLRU(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1855 afs_int32 code = SYNC_OK;
1857 code = SYNC_BAD_COMMAND;
1861 #endif /* AFS_DEMAND_ATTACH_FS */
1864 * populate an FSSYNC_VolOp_info object from a command packet object.
1866 * @param[in] vcom pointer to command packet
1867 * @param[out] info pointer to info object which will be populated
1869 * @note FSSYNC_VolOp_info objects are attached to Volume objects when
1870 * a volume operation is commenced.
1875 FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info)
1877 memcpy(&info->com, vcom->hdr, sizeof(SYNC_command_hdr));
1878 memcpy(&info->vop, vcom->vop, sizeof(FSSYNC_VolOp_hdr));
1879 info->vol_op_state = FSSYNC_VolOpPending;
1883 * check whether command packet partition name matches volume
1884 * object's partition name.
1886 * @param[in] vcom pointer to command packet
1887 * @param[in] vp pointer to volume object
1888 * @param[in] match_anon anon matching control flag (see note below)
1890 * @return whether partitions match
1891 * @retval 0 partitions do NOT match
1892 * @retval 1 partitions match
1894 * @note if match_anon is non-zero, then this function will return a
1895 * positive match for a zero-length partition string in the
1901 FSYNC_partMatch(FSSYNC_VolOp_command * vcom, Volume * vp, int match_anon)
1903 return ((match_anon && vcom->vop->partName[0] == 0) ||
1904 (strncmp(vcom->vop->partName, V_partition(vp)->name,
1905 sizeof(vcom->vop->partName)) == 0));
1910 FSYNC_Drop(osi_socket fd)
1912 struct offlineInfo *p;
1915 char tvolName[VMAXPATHLEN];
1918 p = OfflineVolumes[FindHandler(fd)];
1919 for (i = 0; i < MAXOFFLINEVOLUMES; i++) {
1920 if (p[i].volumeID) {
1925 sprintf(&tvolName[1], VFORMAT, afs_printable_uint32_lu(p[i].volumeID));
1926 vp = VAttachVolumeByName_r(&error, p[i].partName, tvolName,
1943 static int AcceptHandler = -1; /* handler id for accept, if turned on */
1948 if (AcceptHandler == -1) {
1949 osi_Assert(AddHandler(fssync_server_state.fd, FSYNC_newconnection));
1950 AcceptHandler = FindHandler(fssync_server_state.fd);
1957 if (AcceptHandler != -1) {
1958 osi_Assert(RemoveHandler(fssync_server_state.fd));
1963 /* The multiple FD handling code. */
1965 static osi_socket HandlerFD[MAXHANDLERS];
1966 static void (*HandlerProc[MAXHANDLERS]) (osi_socket);
1972 ObtainWriteLock(&FSYNC_handler_lock);
1973 for (i = 0; i < MAXHANDLERS; i++) {
1977 ReleaseWriteLock(&FSYNC_handler_lock);
1980 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
1982 CallHandler(struct pollfd *fds, int nfds, int mask)
1986 ObtainReadLock(&FSYNC_handler_lock);
1987 for (i = 0; i < nfds; i++) {
1988 if (fds[i].revents & mask) {
1989 handler = FindHandler_r(fds[i].fd);
1990 ReleaseReadLock(&FSYNC_handler_lock);
1991 (*HandlerProc[handler]) (fds[i].fd);
1992 ObtainReadLock(&FSYNC_handler_lock);
1995 ReleaseReadLock(&FSYNC_handler_lock);
1999 CallHandler(fd_set * fdsetp)
2002 ObtainReadLock(&FSYNC_handler_lock);
2003 for (i = 0; i < MAXHANDLERS; i++) {
2004 if (HandlerFD[i] >= 0 && FD_ISSET(HandlerFD[i], fdsetp)) {
2005 ReleaseReadLock(&FSYNC_handler_lock);
2006 (*HandlerProc[i]) (HandlerFD[i]);
2007 ObtainReadLock(&FSYNC_handler_lock);
2010 ReleaseReadLock(&FSYNC_handler_lock);
2015 AddHandler(osi_socket afd, void (*aproc) (osi_socket))
2018 ObtainWriteLock(&FSYNC_handler_lock);
2019 for (i = 0; i < MAXHANDLERS; i++)
2020 if (HandlerFD[i] == -1)
2022 if (i >= MAXHANDLERS) {
2023 ReleaseWriteLock(&FSYNC_handler_lock);
2027 HandlerProc[i] = aproc;
2028 ReleaseWriteLock(&FSYNC_handler_lock);
2033 FindHandler(osi_socket afd)
2036 ObtainReadLock(&FSYNC_handler_lock);
2037 for (i = 0; i < MAXHANDLERS; i++)
2038 if (HandlerFD[i] == afd) {
2039 ReleaseReadLock(&FSYNC_handler_lock);
2042 ReleaseReadLock(&FSYNC_handler_lock); /* just in case */
2044 return -1; /* satisfy compiler */
2048 FindHandler_r(osi_socket afd)
2051 for (i = 0; i < MAXHANDLERS; i++)
2052 if (HandlerFD[i] == afd) {
2056 return -1; /* satisfy compiler */
2060 RemoveHandler(osi_socket afd)
2062 ObtainWriteLock(&FSYNC_handler_lock);
2063 HandlerFD[FindHandler_r(afd)] = -1;
2064 ReleaseWriteLock(&FSYNC_handler_lock);
2068 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
2070 GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds)
2074 ObtainReadLock(&FSYNC_handler_lock);
2075 for (i = 0; i < MAXHANDLERS; i++)
2076 if (HandlerFD[i] != -1) {
2077 osi_Assert(fdi<maxfds);
2078 fds[fdi].fd = HandlerFD[i];
2079 fds[fdi].events = events;
2080 fds[fdi].revents = 0;
2084 ReleaseReadLock(&FSYNC_handler_lock);
2088 GetHandler(fd_set * fdsetp, int *maxfdp)
2093 ObtainReadLock(&FSYNC_handler_lock); /* just in case */
2094 for (i = 0; i < MAXHANDLERS; i++)
2095 if (HandlerFD[i] != -1) {
2096 FD_SET(HandlerFD[i], fdsetp);
2097 #ifndef AFS_NT40_ENV
2098 /* On Windows the nfds parameter to select() is ignored */
2099 if (maxfd < HandlerFD[i] || maxfd == (int)-1)
2100 maxfd = HandlerFD[i];
2101 #endif /* AFS_NT40_ENV */
2104 ReleaseReadLock(&FSYNC_handler_lock); /* just in case */
2106 #endif /* HAVE_POLL && AFS_PTHREAD_ENV */
2108 #endif /* FSSYNC_BUILD_SERVER */