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>
49 #include <afs/afsint.h>
51 #include <afs/errors.h>
52 #include "daemon_com.h"
53 #include "daemon_com_inline.h"
55 #include "fssync_inline.h"
59 #include <afs/afssyscalls.h>
63 #include "volume_inline.h"
64 #include "partition.h"
70 #endif /* HAVE_POLL */
72 #ifdef USE_UNIX_SOCKETS
74 #include <afs/afsutil.h>
75 #endif /* USE_UNIX_SOCKETS */
77 #ifdef FSSYNC_BUILD_SERVER
79 int (*V_BreakVolumeCallbacks) (VolumeId volume);
81 #define MAXHANDLERS 4 /* Up to 4 clients; must be at least 2, so that
82 * move = dump+restore can run on single server */
83 #define MAXOFFLINEVOLUMES 128 /* This needs to be as big as the maximum
84 * number that would be offline for 1 operation.
85 * Current winner is salvage, which needs all
86 * cloned read-only copies offline when salvaging
87 * a single read-write volume */
91 static struct offlineInfo OfflineVolumes[MAXHANDLERS][MAXOFFLINEVOLUMES];
94 * fssync server socket handle.
96 static SYNC_server_state_t fssync_server_state =
97 { OSI_NULLSOCKET, /* file descriptor */
98 FSSYNC_ENDPOINT_DECL, /* server endpoint */
99 FSYNC_PROTO_VERSION, /* protocol version */
100 5, /* bind() retry limit */
101 100, /* listen() queue depth */
102 "FSSYNC", /* protocol name string */
105 #ifdef AFS_DEMAND_ATTACH_FS
107 * a queue of volume pointers to salvage in the background.
109 struct fsync_salv_node {
111 Volume *vp; /**< volume to salvage */
112 unsigned char update_salv_prio; /**< whether we should update the salvage priority or not */
115 struct rx_queue head;
119 static void * FSYNC_salvageThread(void *);
120 static void FSYNC_backgroundSalvage(Volume *vp);
121 #endif /* AFS_DEMAND_ATTACH_FS */
123 /* Forward declarations */
124 static void * FSYNC_sync(void *);
125 static void FSYNC_newconnection(osi_socket afd);
126 static void FSYNC_com(osi_socket fd);
127 static void FSYNC_Drop(osi_socket fd);
128 static void AcceptOn(void);
129 static void AcceptOff(void);
130 static void InitHandler(void);
131 static int AddHandler(osi_socket fd, void (*aproc)(osi_socket));
132 static int FindHandler(osi_socket afd);
133 static int FindHandler_r(osi_socket afd);
134 static int RemoveHandler(osi_socket afd);
135 #if defined(HAVE_POLL) && defined (AFS_PTHREAD_ENV)
136 static void CallHandler(struct pollfd *fds, int nfds, int mask);
137 static void GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds);
139 static void CallHandler(fd_set * fdsetp);
140 static void GetHandler(fd_set * fdsetp, int *maxfdp);
144 static afs_int32 FSYNC_com_VolOp(osi_socket fd, SYNC_command * com, SYNC_response * res);
146 #ifdef AFS_DEMAND_ATTACH_FS
147 static afs_int32 FSYNC_com_VolError(FSSYNC_VolOp_command * com, SYNC_response * res);
149 static afs_int32 FSYNC_com_VolOn(FSSYNC_VolOp_command * com, SYNC_response * res);
150 static afs_int32 FSYNC_com_VolOff(FSSYNC_VolOp_command * com, SYNC_response * res);
151 static afs_int32 FSYNC_com_VolMove(FSSYNC_VolOp_command * com, SYNC_response * res);
152 static afs_int32 FSYNC_com_VolBreakCBKs(FSSYNC_VolOp_command * com, SYNC_response * res);
153 static afs_int32 FSYNC_com_VolDone(FSSYNC_VolOp_command * com, SYNC_response * res);
154 static afs_int32 FSYNC_com_VolQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
155 static afs_int32 FSYNC_com_VolHdrQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
156 #ifdef AFS_DEMAND_ATTACH_FS
157 static afs_int32 FSYNC_com_VGUpdate(osi_socket fd, SYNC_command * com, SYNC_response * res);
158 static afs_int32 FSYNC_com_VolOpQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
159 static afs_int32 FSYNC_com_VGQuery(FSSYNC_VolOp_command * com, SYNC_response * res);
160 static afs_int32 FSYNC_com_VGScan(FSSYNC_VolOp_command * com, SYNC_response * res);
161 static afs_int32 FSYNC_com_VGScanAll(FSSYNC_VolOp_command * com, SYNC_response * res);
162 #endif /* AFS_DEMAND_ATTACH_FS */
164 static afs_int32 FSYNC_com_VnQry(osi_socket fd, SYNC_command * com, SYNC_response * res);
166 static afs_int32 FSYNC_com_StatsOp(osi_socket fd, SYNC_command * com, SYNC_response * res);
168 static afs_int32 FSYNC_com_StatsOpGeneral(FSSYNC_StatsOp_command * scom, SYNC_response * res);
170 #ifdef AFS_DEMAND_ATTACH_FS
171 static afs_int32 FSYNC_com_StatsOpViceP(FSSYNC_StatsOp_command * scom, SYNC_response * res);
172 static afs_int32 FSYNC_com_StatsOpHash(FSSYNC_StatsOp_command * scom, SYNC_response * res);
173 static afs_int32 FSYNC_com_StatsOpHdr(FSSYNC_StatsOp_command * scom, SYNC_response * res);
174 static afs_int32 FSYNC_com_StatsOpVLRU(FSSYNC_StatsOp_command * scom, SYNC_response * res);
177 static void FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info);
179 static int FSYNC_partMatch(FSSYNC_VolOp_command * vcom, Volume * vp, int match_anon);
183 * This lock controls access to the handler array. The overhead
184 * is minimal in non-preemptive environments.
186 struct Lock FSYNC_handler_lock;
191 #ifdef AFS_PTHREAD_ENV
193 pthread_attr_t tattr;
194 #else /* AFS_PTHREAD_ENV */
196 #endif /* AFS_PTHREAD_ENV */
198 Lock_Init(&FSYNC_handler_lock);
200 #ifdef AFS_PTHREAD_ENV
201 osi_Assert(pthread_attr_init(&tattr) == 0);
202 osi_Assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
203 osi_Assert(pthread_create(&tid, &tattr, FSYNC_sync, NULL) == 0);
204 #else /* AFS_PTHREAD_ENV */
205 osi_Assert(LWP_CreateProcess
206 (FSYNC_sync, USUAL_STACK_SIZE, USUAL_PRIORITY, (void *)0,
207 "FSYNC_sync", &pid) == LWP_SUCCESS);
208 #endif /* AFS_PTHREAD_ENV */
210 #ifdef AFS_DEMAND_ATTACH_FS
211 queue_Init(&fsync_salv.head);
212 CV_INIT(&fsync_salv.cv, "fsync salv", CV_DEFAULT, 0);
213 osi_Assert(pthread_create(&tid, &tattr, FSYNC_salvageThread, NULL) == 0);
214 #endif /* AFS_DEMAND_ATTACH_FS */
217 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
218 static struct pollfd FSYNC_readfds[MAXHANDLERS];
220 static fd_set FSYNC_readfds;
225 FSYNC_sync(void * args)
229 #ifdef AFS_PTHREAD_ENV
232 SYNC_server_state_t * state = &fssync_server_state;
233 #ifdef AFS_DEMAND_ATTACH_FS
234 VThreadOptions_t * thread_opts;
238 * For non-DAFS, only wait until we begin attaching volumes (instead
239 * of waiting until all volumes are attached), since it can take
240 * awhile until VInit == 2.
243 #endif /* AFS_DEMAND_ATTACH_FS */
245 /* we must not be called before vol package initialization, since we use
246 * vol package mutexes and conds etc */
249 SYNC_getAddr(&state->endpoint, &state->addr);
250 SYNC_cleanupSock(state);
253 (void)signal(SIGPIPE, SIG_IGN);
256 #ifdef AFS_PTHREAD_ENV
257 /* set our 'thread-id' so that the host hold table works */
258 tid = rx_NewThreadId();
259 pthread_setspecific(rx_thread_id_key, (void *)(intptr_t)tid);
260 Log("Set thread id %d for FSYNC_sync\n", tid);
261 #endif /* AFS_PTHREAD_ENV */
265 while (VInit < min_vinit) {
266 /* Let somebody else run until all volumes have been preattached
267 * (DAFS), or we have started attaching volumes (non-DAFS). This
268 * doesn't mean that all volumes have been attached.
270 #ifdef AFS_PTHREAD_ENV
271 VOL_CV_WAIT(&vol_vinit_cond);
272 #else /* AFS_PTHREAD_ENV */
273 LWP_DispatchProcess();
274 #endif /* AFS_PTHREAD_ENV */
279 state->fd = SYNC_getSock(&state->endpoint);
280 code = SYNC_bindSock(state);
283 #ifdef AFS_DEMAND_ATTACH_FS
285 * make sure the volume package is incapable of recursively executing
286 * salvsync calls on this thread, since there is a possibility of
289 thread_opts = malloc(sizeof(VThreadOptions_t));
290 if (thread_opts == NULL) {
291 Log("failed to allocate memory for thread-specific volume package options structure\n");
294 memcpy(thread_opts, &VThread_defaults, sizeof(VThread_defaults));
295 thread_opts->disallow_salvsync = 1;
296 osi_Assert(pthread_setspecific(VThread_key, thread_opts) == 0);
298 code = VVGCache_PkgInit();
299 osi_Assert(code == 0);
306 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
308 GetHandler(FSYNC_readfds, MAXHANDLERS, POLLIN|POLLPRI, &nfds);
309 if (poll(FSYNC_readfds, nfds, -1) >=1)
310 CallHandler(FSYNC_readfds, nfds, POLLIN|POLLPRI);
313 #ifdef AFS_PTHREAD_ENV
314 struct timeval s_timeout;
316 GetHandler(&FSYNC_readfds, &maxfd);
317 /* Note: check for >= 1 below is essential since IOMGR_select
318 * doesn't have exactly same semantics as select.
320 #ifdef AFS_PTHREAD_ENV
321 s_timeout.tv_sec = SYNC_SELECT_TIMEOUT;
322 s_timeout.tv_usec = 0;
323 if (select(maxfd + 1, &FSYNC_readfds, NULL, NULL, &s_timeout) >= 1)
324 #else /* AFS_PTHREAD_ENV */
325 if (IOMGR_Select(maxfd + 1, &FSYNC_readfds, NULL, NULL, NULL) >= 1)
326 #endif /* AFS_PTHREAD_ENV */
327 CallHandler(&FSYNC_readfds);
330 return NULL; /* hush now, little gcc */
333 #ifdef AFS_DEMAND_ATTACH_FS
335 * thread for salvaging volumes in the background.
337 * Since FSSYNC handlers cannot issue SALVSYNC requests in order to avoid
338 * deadlock issues, this thread exists so code in the FSSYNC handler thread
339 * can hand off volumes to be salvaged in the background.
341 * @param[in] args unused
343 * @note DEMAND_ATTACH_FS only
346 FSYNC_salvageThread(void * args)
349 struct fsync_salv_node *node;
354 while (queue_IsEmpty(&fsync_salv.head)) {
355 VOL_CV_WAIT(&fsync_salv.cv);
358 node = queue_First(&fsync_salv.head, fsync_salv_node);
362 if (node->update_salv_prio) {
363 if (VUpdateSalvagePriority_r(vp)) {
364 ViceLog(0, ("FSYNC_salvageThread: unable to raise salvage priority "
365 "for volume %lu\n", afs_printable_uint32_lu(vp->hashid)));
372 VCancelReservation_r(vp);
381 * salvage a volume in the background.
383 * Salvages cannot be scheduled directly from the main FSYNC thread, so
384 * instead call this function to schedule a salvage asynchronously in the
385 * FSYNC_salvageThread thread.
387 * @param[in] vp volume to pointer to salvage
391 * @note DEMAND_ATTACH_FS only
394 FSYNC_backgroundSalvage(Volume *vp)
396 struct fsync_salv_node *node;
399 VCreateReservation_r(vp);
401 node = malloc(sizeof(struct fsync_salv_node));
404 /* Save this value, to know if we should VUpdateSalvagePriority_r.
405 * We need to save it here, snce VRequestSalvage_r will change it. */
406 node->update_salv_prio = vp->salvage.requested;
408 if (VRequestSalvage_r(&ec, vp, SALVSYNC_ERROR, 0)) {
409 ViceLog(0, ("FSYNC_backgroundSalvage: unable to request salvage for volume %lu\n",
410 afs_printable_uint32_lu(vp->hashid)));
413 queue_Append(&fsync_salv.head, node);
414 CV_BROADCAST(&fsync_salv.cv);
416 #endif /* AFS_DEMAND_ATTACH_FS */
419 FSYNC_newconnection(osi_socket afd)
421 #ifdef USE_UNIX_SOCKETS
422 struct sockaddr_un other;
423 #else /* USE_UNIX_SOCKETS */
424 struct sockaddr_in other;
428 junk = sizeof(other);
429 fd = accept(afd, (struct sockaddr *)&other, &junk);
430 if (fd == OSI_NULLSOCKET) {
431 Log("FSYNC_newconnection: accept failed, errno==%d\n", errno);
433 } else if (!AddHandler(fd, FSYNC_com)) {
435 osi_Assert(AddHandler(fd, FSYNC_com));
439 /* this function processes commands from an fssync file descriptor (fd) */
440 afs_int32 FS_cnt = 0;
442 FSYNC_com(osi_socket fd)
446 SYNC_PROTO_BUF_DECL(com_buf);
447 SYNC_PROTO_BUF_DECL(res_buf);
449 memset(&res.hdr, 0, sizeof(res.hdr));
451 com.payload.buf = (void *)com_buf;
452 com.payload.len = SYNC_PROTO_MAX_LEN;
453 res.hdr.response_len = sizeof(res.hdr);
454 res.payload.len = SYNC_PROTO_MAX_LEN;
455 res.payload.buf = (void *)res_buf;
458 if (SYNC_getCom(&fssync_server_state, fd, &com)) {
459 Log("FSYNC_com: read failed; dropping connection (cnt=%d)\n", FS_cnt);
464 if (com.recv_len < sizeof(com.hdr)) {
465 Log("FSSYNC_com: invalid protocol message length (%u)\n", com.recv_len);
466 res.hdr.response = SYNC_COM_ERROR;
467 res.hdr.reason = SYNC_REASON_MALFORMED_PACKET;
468 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
472 if (com.hdr.proto_version != FSYNC_PROTO_VERSION) {
473 Log("FSYNC_com: invalid protocol version (%u)\n", com.hdr.proto_version);
474 res.hdr.response = SYNC_COM_ERROR;
475 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
479 if (com.hdr.command == SYNC_COM_CHANNEL_CLOSE) {
480 res.hdr.response = SYNC_OK;
481 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
483 /* don't respond, just drop; senders of SYNC_COM_CHANNEL_CLOSE
484 * never wait for a response. */
488 ViceLog(125, ("FSYNC_com: from fd %d got command %ld (%s) reason %ld (%s) "
489 "pt %ld (%s) pid %ld\n", (int)fd,
490 afs_printable_int32_ld(com.hdr.command),
491 FSYNC_com2string(com.hdr.command),
492 afs_printable_int32_ld(com.hdr.reason),
493 FSYNC_reason2string(com.hdr.reason),
494 afs_printable_int32_ld(com.hdr.programType),
495 VPTypeToString(com.hdr.programType),
496 afs_printable_int32_ld(com.hdr.pid)));
498 res.hdr.com_seq = com.hdr.com_seq;
501 switch (com.hdr.command) {
503 case FSYNC_VOL_ATTACH:
504 case FSYNC_VOL_LEAVE_OFF:
506 case FSYNC_VOL_FORCE_ERROR:
507 case FSYNC_VOL_LISTVOLUMES:
508 case FSYNC_VOL_NEEDVOLUME:
510 case FSYNC_VOL_BREAKCBKS:
512 case FSYNC_VOL_QUERY:
513 case FSYNC_VOL_QUERY_HDR:
514 #ifdef AFS_DEMAND_ATTACH_FS
515 case FSYNC_VOL_QUERY_VOP:
518 case FSYNC_VG_SCAN_ALL:
520 res.hdr.response = FSYNC_com_VolOp(fd, &com, &res);
522 case FSYNC_VOL_STATS_GENERAL:
523 case FSYNC_VOL_STATS_VICEP:
524 case FSYNC_VOL_STATS_HASH:
525 case FSYNC_VOL_STATS_HDR:
526 case FSYNC_VOL_STATS_VLRU:
527 res.hdr.response = FSYNC_com_StatsOp(fd, &com, &res);
529 case FSYNC_VOL_QUERY_VNODE:
530 res.hdr.response = FSYNC_com_VnQry(fd, &com, &res);
532 #ifdef AFS_DEMAND_ATTACH_FS
535 res.hdr.response = FSYNC_com_VGUpdate(fd, &com, &res);
539 res.hdr.response = SYNC_BAD_COMMAND;
544 ViceLog(125, ("FSYNC_com: fd %d responding with code %ld (%s) reason %ld "
546 afs_printable_int32_ld(res.hdr.response),
547 SYNC_res2string(res.hdr.response),
548 afs_printable_int32_ld(res.hdr.reason),
549 FSYNC_reason2string(res.hdr.reason)));
552 SYNC_putRes(&fssync_server_state, fd, &res);
555 if (res.hdr.flags & SYNC_FLAG_CHANNEL_SHUTDOWN) {
561 FSYNC_com_VolOp(osi_socket fd, SYNC_command * com, SYNC_response * res)
564 afs_int32 code = SYNC_OK;
565 FSSYNC_VolOp_command vcom;
567 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_VolOp_hdr))) {
568 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
569 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
570 return SYNC_COM_ERROR;
573 vcom.hdr = &com->hdr;
574 vcom.vop = (FSSYNC_VolOp_hdr *) com->payload.buf;
577 vcom.volumes = OfflineVolumes[FindHandler(fd)];
578 for (vcom.v = NULL, i = 0; i < MAXOFFLINEVOLUMES; i++) {
579 if ((vcom.volumes[i].volumeID == vcom.vop->volume) &&
580 (strncmp(vcom.volumes[i].partName, vcom.vop->partName,
581 sizeof(vcom.volumes[i].partName)) == 0)) {
582 vcom.v = &vcom.volumes[i];
587 ViceLog(125, ("FSYNC_com_VolOp: fd %d got command for vol %lu part %.16s\n",
588 (int)fd, afs_printable_uint32_lu(vcom.vop->volume),
589 vcom.vop->partName));
591 switch (com->hdr.command) {
593 case FSYNC_VOL_ATTACH:
594 case FSYNC_VOL_LEAVE_OFF:
595 code = FSYNC_com_VolOn(&vcom, res);
598 case FSYNC_VOL_NEEDVOLUME:
599 code = FSYNC_com_VolOff(&vcom, res);
601 case FSYNC_VOL_LISTVOLUMES:
605 code = FSYNC_com_VolMove(&vcom, res);
607 case FSYNC_VOL_BREAKCBKS:
608 code = FSYNC_com_VolBreakCBKs(&vcom, res);
611 code = FSYNC_com_VolDone(&vcom, res);
613 case FSYNC_VOL_QUERY:
614 code = FSYNC_com_VolQuery(&vcom, res);
616 case FSYNC_VOL_QUERY_HDR:
617 code = FSYNC_com_VolHdrQuery(&vcom, res);
619 #ifdef AFS_DEMAND_ATTACH_FS
620 case FSYNC_VOL_FORCE_ERROR:
621 code = FSYNC_com_VolError(&vcom, res);
623 case FSYNC_VOL_QUERY_VOP:
624 code = FSYNC_com_VolOpQuery(&vcom, res);
627 code = FSYNC_com_VGQuery(&vcom, res);
630 code = FSYNC_com_VGScan(&vcom, res);
632 case FSYNC_VG_SCAN_ALL:
633 code = FSYNC_com_VGScanAll(&vcom, res);
635 #endif /* AFS_DEMAND_ATTACH_FS */
637 code = SYNC_BAD_COMMAND;
644 * service an FSYNC request to bring a volume online.
646 * @param[in] vcom pointer command object
647 * @param[out] res object in which to store response packet
649 * @return operation status
650 * @retval SYNC_OK volume transitioned online
651 * @retval SYNC_FAILED invalid command protocol message
652 * @retval SYNC_DENIED operation could not be completed
654 * @note this is an FSYNC RPC server stub
656 * @note this procedure handles the following FSSYNC command codes:
659 * - FSYNC_VOL_LEAVE_OFF
661 * @note the supplementary reason code contains additional details.
662 * When SYNC_DENIED is returned, the specific reason is
663 * placed in the response packet reason field.
668 FSYNC_com_VolOn(FSSYNC_VolOp_command * vcom, SYNC_response * res)
670 afs_int32 code = SYNC_OK;
671 #ifndef AFS_DEMAND_ATTACH_FS
672 char tvolName[VMAXPATHLEN];
677 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
678 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
683 /* so, we need to attach the volume */
685 #ifdef AFS_DEMAND_ATTACH_FS
686 /* check DAFS permissions */
687 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
689 FSYNC_partMatch(vcom, vp, 1) &&
690 vp->pending_vol_op &&
691 (vcom->hdr->programType != vp->pending_vol_op->com.programType)) {
692 /* a different program has this volume checked out. deny. */
693 Log("FSYNC_VolOn: WARNING: program type %u has attempted to manipulate "
694 "state for volume %u using command code %u while the volume is "
695 "checked out by program type %u for command code %u.\n",
696 vcom->hdr->programType,
699 vp->pending_vol_op->com.programType,
700 vp->pending_vol_op->com.command);
702 res->hdr.reason = FSYNC_EXCLUSIVE;
708 vcom->v->volumeID = 0;
711 if (vcom->hdr->command == FSYNC_VOL_LEAVE_OFF) {
712 /* nothing much to do if we're leaving the volume offline */
713 #ifdef AFS_DEMAND_ATTACH_FS
715 VCreateReservation_r(vp);
716 VWaitExclusiveState_r(vp);
718 if (vp && V_attachState(vp) != VOL_STATE_DELETED) {
719 if (FSYNC_partMatch(vcom, vp, 1)) {
720 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
721 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
722 VChangeState_r(vp, VOL_STATE_UNATTACHED);
723 VDeregisterVolOp_r(vp);
726 res->hdr.reason = FSYNC_BAD_STATE;
730 res->hdr.reason = FSYNC_WRONG_PART;
734 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
738 VCancelReservation_r(vp);
745 #ifdef AFS_DEMAND_ATTACH_FS
746 /* first, check to see whether we have such a volume defined */
747 vp = VPreAttachVolumeById_r(&error,
751 VCreateReservation_r(vp);
752 VWaitExclusiveState_r(vp);
753 VDeregisterVolOp_r(vp);
754 VCancelReservation_r(vp);
757 #else /* !AFS_DEMAND_ATTACH_FS */
758 tvolName[0] = OS_DIRSEPC;
759 snprintf(&tvolName[1], sizeof(tvolName)-1, VFORMAT, afs_printable_uint32_lu(vcom->vop->volume));
760 tvolName[sizeof(tvolName)-1] = '\0';
762 vp = VAttachVolumeByName_r(&error, vcom->vop->partName, tvolName,
768 res->hdr.reason = error;
770 #endif /* !AFS_DEMAND_ATTACH_FS */
777 * service an FSYNC request to take a volume offline.
779 * @param[in] vcom pointer command object
780 * @param[out] res object in which to store response packet
782 * @return operation status
783 * @retval SYNC_OK volume transitioned offline
784 * @retval SYNC_FAILED invalid command protocol message
785 * @retval SYNC_DENIED operation could not be completed
787 * @note this is an FSYNC RPC server stub
789 * @note this procedure handles the following FSSYNC command codes:
791 * - FSYNC_VOL_NEEDVOLUME
793 * @note the supplementary reason code contains additional details.
794 * When SYNC_DENIED is returned, the specific reason is
795 * placed in the response packet reason field.
800 FSYNC_com_VolOff(FSSYNC_VolOp_command * vcom, SYNC_response * res)
802 FSSYNC_VolOp_info info;
803 afs_int32 code = SYNC_OK;
807 #ifdef AFS_DEMAND_ATTACH_FS
808 Volume *nvp, *rvp = NULL;
811 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
812 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
817 /* not already offline, we need to find a slot for newly offline volume */
818 if (vcom->hdr->programType == debugUtility) {
819 /* debug utilities do not have their operations tracked */
823 for (i = 0; i < MAXOFFLINEVOLUMES; i++) {
824 if (vcom->volumes[i].volumeID == 0) {
825 vcom->v = &vcom->volumes[i];
835 FSYNC_com_to_info(vcom, &info);
837 #ifdef AFS_DEMAND_ATTACH_FS
838 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
840 vp = VGetVolume_r(&error, vcom->vop->volume);
844 if (!FSYNC_partMatch(vcom, vp, 1)) {
845 /* volume on desired partition is not online, so we
846 * should treat this as an offline volume.
848 #ifndef AFS_DEMAND_ATTACH_FS
856 #ifdef AFS_DEMAND_ATTACH_FS
858 ProgramType type = (ProgramType) vcom->hdr->programType;
860 /* do initial filtering of requests */
862 /* enforce mutual exclusion for volume ops */
863 if (vp->pending_vol_op) {
864 if (vp->pending_vol_op->com.programType != type) {
865 if (vp->pending_vol_op->com.command == FSYNC_VOL_OFF &&
866 vp->pending_vol_op->com.reason == FSYNC_SALVAGE) {
868 Log("denying offline request for volume %lu; volume is salvaging\n",
869 afs_printable_uint32_lu(vp->hashid));
871 res->hdr.reason = FSYNC_SALVAGE;
874 Log("volume %u already checked out\n", vp->hashid);
876 Log("vp->vop = { com = { ver=%u, prog=%d, com=%d, reason=%d, len=%u, flags=0x%x }, vop = { vol=%u, part='%s' } }\n",
877 vp->pending_vol_op->com.proto_version,
878 vp->pending_vol_op->com.programType,
879 vp->pending_vol_op->com.command,
880 vp->pending_vol_op->com.reason,
881 vp->pending_vol_op->com.command_len,
882 vp->pending_vol_op->com.flags,
883 vp->pending_vol_op->vop.volume,
884 vp->pending_vol_op->vop.partName );
885 Log("vcom = { com = { ver=%u, prog=%d, com=%d, reason=%d, len=%u, flags=0x%x } , vop = { vol=%u, part='%s' } }\n",
886 vcom->hdr->proto_version,
887 vcom->hdr->programType,
890 vcom->hdr->command_len,
893 vcom->vop->partName);
894 res->hdr.reason = FSYNC_EXCLUSIVE;
897 Log("warning: volume %u recursively checked out by programType id %d\n",
898 vp->hashid, vcom->hdr->programType);
902 /* wait for exclusive ops, so we have an accurate picture of the
903 * vol attach state */
904 VCreateReservation_r(vp);
905 VWaitExclusiveState_r(vp);
908 /* filter based upon requestor
910 * volume utilities / volserver are not allowed to check out
911 * volumes which are in an error state
913 * unknown utility programs will be denied on principal
918 /* it is possible for the salvageserver to checkout a
919 * volume for salvage before its scheduling request
920 * has been sent to the salvageserver */
921 if (vp->salvage.requested && !vp->salvage.scheduled) {
922 vp->salvage.scheduled = 1;
925 /* If the volume is in VOL_STATE_SALVAGE_REQ, we need to wait
926 * for the vol to go offline before we can give it away. Also
927 * make sure we don't come out with vp in an excl state. */
928 while (V_attachState(vp) == VOL_STATE_SALVAGE_REQ ||
929 VIsExclusiveState(V_attachState(vp))) {
931 VOL_CV_WAIT(&V_attachCV(vp));
939 if (VIsSalvaging(vp)) {
940 Log("denying offline request for volume %lu; volume is in salvaging state\n",
941 afs_printable_uint32_lu(vp->hashid));
942 res->hdr.reason = FSYNC_SALVAGE;
944 /* the volume hasn't been checked out yet by the salvager,
945 * but we think the volume is salvaging; schedule a
946 * a salvage to update the salvage priority */
947 FSYNC_backgroundSalvage(vp);
951 if (VIsErrorState(V_attachState(vp))) {
957 Log("bad program type passed to FSSYNC\n");
961 /* short circuit for offline volume states
962 * so we can avoid I/O penalty of attachment */
963 switch (V_attachState(vp)) {
964 case VOL_STATE_UNATTACHED:
965 case VOL_STATE_PREATTACHED:
966 case VOL_STATE_SALVAGING:
967 case VOL_STATE_ERROR:
968 /* register the volume operation metadata with the volume
970 * if the volume is currently pre-attached, attach2()
971 * will evaluate the vol op metadata to determine whether
972 * attaching the volume would be safe */
973 VRegisterVolOp_r(vp, &info);
974 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
977 case VOL_STATE_DELETED:
983 /* convert to heavyweight ref */
984 nvp = VGetVolumeByVp_r(&error, vp);
987 * It's possible for VGetVolumeByVp_r to have dropped and
988 * re-acquired VOL_LOCK, so volume state may have changed
989 * back to one of the states we tested for above. Since
990 * GetVolume can return NULL in some of those states, just
991 * test for the states again here.
993 switch (V_attachState(vp)) {
994 case VOL_STATE_UNATTACHED:
995 case VOL_STATE_PREATTACHED:
996 case VOL_STATE_SALVAGING:
997 case VOL_STATE_ERROR:
998 /* register the volume operation metadata with the volume
1000 * if the volume is currently pre-attached, attach2()
1001 * will evaluate the vol op metadata to determine whether
1002 * attaching the volume would be safe */
1003 VRegisterVolOp_r(vp, &info);
1004 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningUnknown;
1007 case VOL_STATE_DELETED:
1013 Log("FSYNC_com_VolOff: failed to get heavyweight reference to volume %u (state=%u, flags=0x%x)\n",
1014 vcom->vop->volume, V_attachState(vp), V_attachFlags(vp));
1015 res->hdr.reason = FSYNC_VOL_PKG_ERROR;
1017 } else if (nvp != vp) {
1018 /* i don't think this should ever happen, but just in case... */
1019 Log("FSYNC_com_VolOff: warning: potentially dangerous race detected\n");
1023 /* kill off lightweight ref to ensure we can't deadlock against ourselves later... */
1024 VCancelReservation_r(rvp);
1027 /* register the volume operation metadata with the volume */
1028 VRegisterVolOp_r(vp, &info);
1031 #endif /* AFS_DEMAND_ATTACH_FS */
1034 if (VVolOpLeaveOnline_r(vp, &info)) {
1035 VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT); /* At least get volume stats right */
1037 Log("FSYNC: Volume %u (%s) was left on line for an external %s request\n",
1038 V_id(vp), V_name(vp),
1039 vcom->hdr->reason == V_CLONE ? "clone" :
1040 vcom->hdr->reason == V_READONLY ? "readonly" :
1041 vcom->hdr->reason == V_DUMP ? "dump" :
1042 vcom->hdr->reason == FSYNC_SALVAGE ? "salvage" :
1045 #ifdef AFS_DEMAND_ATTACH_FS
1046 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOnline;
1050 if (VVolOpSetVBusy_r(vp, &info)) {
1051 vp->specialStatus = VBUSY;
1054 /* remember what volume we got, so we can keep track of how
1055 * many volumes the volserver or whatever is using. Note that
1056 * vp is valid since leaveonline is only set when vp is valid.
1059 vcom->v->volumeID = vcom->vop->volume;
1060 strlcpy(vcom->v->partName, vp->partition->name, sizeof(vcom->v->partName));
1063 #ifdef AFS_DEMAND_ATTACH_FS
1064 VCreateReservation_r(vp);
1065 VOfflineForVolOp_r(&error, vp, "A volume utility is running.");
1067 osi_Assert(vp->nUsers==0);
1068 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline;
1071 VWaitExclusiveState_r(vp);
1072 VDeregisterVolOp_r(vp);
1075 VCancelReservation_r(vp);
1077 VOffline_r(vp, "A volume utility is running.");
1088 #ifdef AFS_DEMAND_ATTACH_FS
1090 VCancelReservation_r(rvp);
1097 * service an FSYNC request to mark a volume as moved.
1099 * @param[in] vcom pointer command object
1100 * @param[out] res object in which to store response packet
1102 * @return operation status
1103 * @retval SYNC_OK volume marked as moved to a remote server
1104 * @retval SYNC_FAILED invalid command protocol message
1105 * @retval SYNC_DENIED current volume state does not permit this operation
1107 * @note this is an FSYNC RPC server stub
1109 * @note this operation also breaks all callbacks for the given volume
1111 * @note this procedure handles the following FSSYNC command codes:
1114 * @note the supplementary reason code contains additional details. For
1115 * instance, SYNC_OK is still returned when the partition specified
1116 * does not match the one registered in the volume object -- reason
1117 * will be FSYNC_WRONG_PART in this case.
1122 FSYNC_com_VolMove(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1124 afs_int32 code = SYNC_DENIED;
1128 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1129 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1134 /* Yuch: the "reason" for the move is the site it got moved to... */
1135 /* still set specialStatus so we stop sending back VBUSY.
1136 * also should still break callbacks. Note that I don't know
1137 * how to tell if we should break all or not, so we just do it
1138 * since it doesn't matter much if we do an extra break
1139 * volume callbacks on a volume move within the same server */
1140 #ifdef AFS_DEMAND_ATTACH_FS
1141 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1143 vp = VGetVolume_r(&error, vcom->vop->volume);
1146 if (FSYNC_partMatch(vcom, vp, 1)) {
1147 #ifdef AFS_DEMAND_ATTACH_FS
1148 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1149 (V_attachState(vp) == VOL_STATE_PREATTACHED)) {
1152 vp->specialStatus = VMOVED;
1153 #ifdef AFS_DEMAND_ATTACH_FS
1155 res->hdr.reason = FSYNC_BAD_STATE;
1159 res->hdr.reason = FSYNC_WRONG_PART;
1161 #ifndef AFS_DEMAND_ATTACH_FS
1163 #endif /* !AFS_DEMAND_ATTACH_FS */
1165 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1168 if ((code == SYNC_OK) && (V_BreakVolumeCallbacks != NULL)) {
1169 Log("fssync: volume %u moved to %x; breaking all call backs\n",
1170 vcom->vop->volume, vcom->hdr->reason);
1172 (*V_BreakVolumeCallbacks) (vcom->vop->volume);
1182 * service an FSYNC request to mark a volume as destroyed.
1184 * @param[in] vcom pointer command object
1185 * @param[out] res object in which to store response packet
1187 * @return operation status
1188 * @retval SYNC_OK volume marked as destroyed
1189 * @retval SYNC_FAILED invalid command protocol message
1190 * @retval SYNC_DENIED current volume state does not permit this operation
1192 * @note this is an FSYNC RPC server stub
1194 * @note this procedure handles the following FSSYNC command codes:
1197 * @note the supplementary reason code contains additional details. For
1198 * instance, SYNC_OK is still returned when the partition specified
1199 * does not match the one registered in the volume object -- reason
1200 * will be FSYNC_WRONG_PART in this case.
1205 FSYNC_com_VolDone(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1207 afs_int32 code = SYNC_FAILED;
1211 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1212 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1216 /* don't try to put online, this call is made only after deleting
1217 * a volume, in which case we want to remove the vol # from the
1218 * OfflineVolumes array only */
1220 vcom->v->volumeID = 0;
1222 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1224 if (FSYNC_partMatch(vcom, vp, 1)) {
1225 #ifdef AFS_DEMAND_ATTACH_FS
1226 VCreateReservation_r(vp);
1227 VWaitExclusiveState_r(vp);
1229 if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1230 (V_attachState(vp) == VOL_STATE_PREATTACHED) ||
1231 VIsErrorState(V_attachState(vp))) {
1233 /* Change state to DELETED, not UNATTACHED, so clients get
1234 * a VNOVOL error when they try to access from now on. */
1236 VChangeState_r(vp, VOL_STATE_DELETED);
1237 VDeregisterVolOp_r(vp);
1239 /* Volume is gone; clear out old salvage stats */
1240 memset(&vp->salvage, 0, sizeof(vp->salvage));
1242 /* Someday we should free the vp, too, after about 2 hours,
1243 * possibly by putting the vp back on the VLRU. */
1246 } else if (V_attachState(vp) == VOL_STATE_DELETED) {
1247 VDeregisterVolOp_r(vp);
1248 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1252 res->hdr.reason = FSYNC_BAD_STATE;
1255 VCancelReservation_r(vp);
1257 #else /* AFS_DEMAND_ATTACH_FS */
1258 if (!vp->specialStatus) {
1259 vp->specialStatus = VNOVOL;
1262 #endif /* !AFS_DEMAND_ATTACH_FS */
1264 code = SYNC_OK; /* XXX is this really a good idea? */
1265 res->hdr.reason = FSYNC_WRONG_PART;
1268 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1275 #ifdef AFS_DEMAND_ATTACH_FS
1277 * service an FSYNC request to transition a volume to the hard error state.
1279 * @param[in] vcom pointer command object
1280 * @param[out] res object in which to store response packet
1282 * @return operation status
1283 * @retval SYNC_OK volume transitioned to hard error state
1284 * @retval SYNC_FAILED invalid command protocol message
1285 * @retval SYNC_DENIED (see note)
1287 * @note this is an FSYNC RPC server stub
1289 * @note this procedure handles the following FSSYNC command codes:
1290 * - FSYNC_VOL_FORCE_ERROR
1292 * @note SYNC_DENIED is returned in the following cases:
1293 * - no partition name is specified (reason field set to
1294 * FSYNC_WRONG_PART).
1295 * - volume id not known to fileserver (reason field set
1296 * to FSYNC_UNKNOWN_VOLID).
1298 * @note demand attach fileserver only
1303 FSYNC_com_VolError(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1307 afs_int32 code = SYNC_FAILED;
1309 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1310 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1314 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1316 if (!vp && vcom->hdr->reason == FSYNC_SALVAGE) {
1317 /* The requested volume doesn't seem to exist. However, it is possible
1318 * that this is triggered by trying to create or clone a volume that
1319 * was prevented from succeeding by a half-created volume in the way.
1320 * (e.g. we tried to create volume X, but volume X exists except that
1321 * its .vol header was deleted for some reason) So, still try to
1322 * a salvage for that volume ID. */
1324 Log("FSYNC_com_VolError: attempting to schedule salvage for unknown "
1325 "volume %lu part %s\n", afs_printable_uint32_lu(vcom->vop->volume),
1326 vcom->vop->partName);
1327 vp = VPreAttachVolumeById_r(&error, vcom->vop->partName,
1332 if (FSYNC_partMatch(vcom, vp, 0)) {
1333 /* null out salvsync control state, as it's no longer relevant */
1334 memset(&vp->salvage, 0, sizeof(vp->salvage));
1336 VCreateReservation_r(vp);
1337 VWaitExclusiveState_r(vp);
1338 VDeregisterVolOp_r(vp);
1340 if (vcom->hdr->reason == FSYNC_SALVAGE) {
1341 FSYNC_backgroundSalvage(vp);
1343 VChangeState_r(vp, VOL_STATE_ERROR);
1346 VCancelReservation_r(vp);
1351 res->hdr.reason = FSYNC_WRONG_PART;
1354 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1360 #endif /* AFS_DEMAND_ATTACH_FS */
1363 * service an FSYNC request to break all callbacks for this volume.
1365 * @param[in] vcom pointer command object
1366 * @param[out] res object in which to store response packet
1368 * @return operation status
1369 * @retval SYNC_OK callback breaks scheduled for volume
1371 * @note this is an FSYNC RPC server stub
1373 * @note this procedure handles the following FSSYNC command codes:
1374 * - FSYNC_VOL_BREAKCBKS
1376 * @note demand attach fileserver only
1378 * @todo should do partition matching
1383 FSYNC_com_VolBreakCBKs(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1385 /* if the volume is being restored, break all callbacks on it */
1386 if (V_BreakVolumeCallbacks) {
1387 Log("fssync: breaking all call backs for volume %u\n",
1390 (*V_BreakVolumeCallbacks) (vcom->vop->volume);
1397 * service an FSYNC request to return the Volume object.
1399 * @param[in] vcom pointer command object
1400 * @param[out] res object in which to store response packet
1402 * @return operation status
1403 * @retval SYNC_OK volume object returned to caller
1404 * @retval SYNC_FAILED bad command packet, or failed to locate volume object
1406 * @note this is an FSYNC RPC server stub
1408 * @note this procedure handles the following FSSYNC command codes:
1414 FSYNC_com_VolQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1416 afs_int32 code = SYNC_FAILED;
1420 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1421 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1425 #ifdef AFS_DEMAND_ATTACH_FS
1426 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1427 #else /* !AFS_DEMAND_ATTACH_FS */
1428 vp = VGetVolume_r(&error, vcom->vop->volume);
1429 #endif /* !AFS_DEMAND_ATTACH_FS */
1432 if (FSYNC_partMatch(vcom, vp, 1)) {
1433 if (res->payload.len >= sizeof(Volume)) {
1434 memcpy(res->payload.buf, vp, sizeof(Volume));
1435 res->hdr.response_len += sizeof(Volume);
1438 res->hdr.reason = SYNC_REASON_PAYLOAD_TOO_BIG;
1441 res->hdr.reason = FSYNC_WRONG_PART;
1443 #ifndef AFS_DEMAND_ATTACH_FS
1447 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1455 * service an FSYNC request to return the Volume header.
1457 * @param[in] vcom pointer command object
1458 * @param[out] res object in which to store response packet
1460 * @return operation status
1461 * @retval SYNC_OK volume header returned to caller
1462 * @retval SYNC_FAILED bad command packet, or failed to locate volume header
1464 * @note this is an FSYNC RPC server stub
1466 * @note this procedure handles the following FSSYNC command codes:
1467 * - FSYNC_VOL_QUERY_HDR
1472 FSYNC_com_VolHdrQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1474 afs_int32 code = SYNC_FAILED;
1478 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1479 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1482 if (res->payload.len < sizeof(VolumeDiskData)) {
1483 res->hdr.reason = SYNC_REASON_PAYLOAD_TOO_BIG;
1487 #ifdef AFS_DEMAND_ATTACH_FS
1488 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1489 #else /* !AFS_DEMAND_ATTACH_FS */
1490 vp = VGetVolume_r(&error, vcom->vop->volume);
1494 if (FSYNC_partMatch(vcom, vp, 1)) {
1495 #ifdef AFS_DEMAND_ATTACH_FS
1496 if ((vp->header == NULL) ||
1497 !(V_attachFlags(vp) & VOL_HDR_ATTACHED) ||
1498 !(V_attachFlags(vp) & VOL_HDR_LOADED)) {
1499 res->hdr.reason = FSYNC_HDR_NOT_ATTACHED;
1502 #else /* !AFS_DEMAND_ATTACH_FS */
1503 if (!vp || !vp->header) {
1504 res->hdr.reason = FSYNC_HDR_NOT_ATTACHED;
1507 #endif /* !AFS_DEMAND_ATTACH_FS */
1509 res->hdr.reason = FSYNC_WRONG_PART;
1513 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1517 memcpy(res->payload.buf, &V_disk(vp), sizeof(VolumeDiskData));
1518 res->hdr.response_len += sizeof(VolumeDiskData);
1522 #ifndef AFS_DEMAND_ATTACH_FS
1530 #ifdef AFS_DEMAND_ATTACH_FS
1532 FSYNC_com_VolOpQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1534 afs_int32 code = SYNC_OK;
1538 vp = VLookupVolume_r(&error, vcom->vop->volume, NULL);
1541 VCreateReservation_r(vp);
1542 VWaitExclusiveState_r(vp);
1545 if (vp && vp->pending_vol_op) {
1546 if (!FSYNC_partMatch(vcom, vp, 1)) {
1547 res->hdr.reason = FSYNC_WRONG_PART;
1550 osi_Assert(sizeof(FSSYNC_VolOp_info) <= res->payload.len);
1551 memcpy(res->payload.buf, vp->pending_vol_op, sizeof(FSSYNC_VolOp_info));
1552 res->hdr.response_len += sizeof(FSSYNC_VolOp_info);
1555 if (!vp || V_attachState(vp) == VOL_STATE_DELETED) {
1556 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1557 } else if (!FSYNC_partMatch(vcom, vp, 1)) {
1558 res->hdr.reason = FSYNC_WRONG_PART;
1560 res->hdr.reason = FSYNC_NO_PENDING_VOL_OP;
1566 VCancelReservation_r(vp);
1572 FSYNC_com_VGQuery(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1574 afs_int32 code = SYNC_FAILED;
1576 struct DiskPartition64 * dp;
1578 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1579 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1583 dp = VGetPartition_r(vcom->vop->partName, 0);
1585 res->hdr.reason = FSYNC_BAD_PART;
1589 osi_Assert(sizeof(FSSYNC_VGQry_response_t) <= res->payload.len);
1591 rc = VVGCache_query_r(dp, vcom->vop->volume, res->payload.buf);
1594 res->hdr.response_len += sizeof(FSSYNC_VGQry_response_t);
1598 res->hdr.reason = FSYNC_PART_SCANNING;
1601 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1612 FSYNC_com_VGUpdate(osi_socket fd, SYNC_command * com, SYNC_response * res)
1614 afs_int32 code = SYNC_FAILED;
1615 struct DiskPartition64 * dp;
1616 FSSYNC_VGUpdate_command_t * vgucom;
1619 if (com->recv_len != (sizeof(com->hdr) + sizeof(*vgucom))) {
1620 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1621 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1622 code = SYNC_COM_ERROR;
1626 vgucom = com->payload.buf;
1628 ViceLog(125, ("FSYNC_com_VGUpdate: fd %d got command for parent %lu child "
1629 "%lu partName %.16s\n", (int)fd,
1630 afs_printable_uint32_lu(vgucom->parent),
1631 afs_printable_uint32_lu(vgucom->child),
1634 if (SYNC_verifyProtocolString(vgucom->partName, sizeof(vgucom->partName))) {
1635 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1639 dp = VGetPartition_r(vgucom->partName, 0);
1641 res->hdr.reason = FSYNC_BAD_PART;
1645 switch(com->hdr.command) {
1647 rc = VVGCache_entry_add_r(dp, vgucom->parent, vgucom->child, NULL);
1651 rc = VVGCache_entry_del_r(dp, vgucom->parent, vgucom->child);
1655 Log("FSYNC_com_VGUpdate called improperly\n");
1660 /* EINVAL means the partition VGC doesn't exist at all; not really
1662 if (rc == 0 || rc == EINVAL) {
1667 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1669 res->hdr.reason = FSYNC_WHATEVER;
1677 FSYNC_com_VGScan(FSSYNC_VolOp_command * vcom, SYNC_response * res)
1679 afs_int32 code = SYNC_FAILED;
1680 struct DiskPartition64 * dp;
1682 if (SYNC_verifyProtocolString(vcom->vop->partName, sizeof(vcom->vop->partName))) {
1683 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1687 dp = VGetPartition_r(vcom->vop->partName, 0);
1689 res->hdr.reason = FSYNC_BAD_PART;
1693 if (VVGCache_scanStart_r(dp) == 0) {
1702 FSYNC_com_VGScanAll(FSSYNC_VolOp_command * com, SYNC_response * res)
1704 afs_int32 code = SYNC_FAILED;
1706 if (VVGCache_scanStart_r(NULL) == 0) {
1712 #endif /* AFS_DEMAND_ATTACH_FS */
1715 FSYNC_com_VnQry(osi_socket fd, SYNC_command * com, SYNC_response * res)
1717 afs_int32 code = SYNC_OK;
1718 FSSYNC_VnQry_hdr * qry = com->payload.buf;
1723 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_VnQry_hdr))) {
1724 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1725 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1726 return SYNC_COM_ERROR;
1729 ViceLog(125, ("FSYNC_com_VnQry: fd %d got command for vol %lu vnode %lu "
1730 "uniq %lu spare %lu partName %.16s\n", (int)fd,
1731 afs_printable_uint32_lu(qry->volume),
1732 afs_printable_uint32_lu(qry->vnode),
1733 afs_printable_uint32_lu(qry->unique),
1734 afs_printable_uint32_lu(qry->spare),
1737 #ifdef AFS_DEMAND_ATTACH_FS
1738 vp = VLookupVolume_r(&error, qry->volume, NULL);
1739 #else /* !AFS_DEMAND_ATTACH_FS */
1740 vp = VGetVolume_r(&error, qry->volume);
1741 #endif /* !AFS_DEMAND_ATTACH_FS */
1744 res->hdr.reason = FSYNC_UNKNOWN_VOLID;
1749 vnp = VLookupVnode(vp, qry->vnode);
1751 res->hdr.reason = FSYNC_UNKNOWN_VNID;
1756 if (Vn_class(vnp)->residentSize > res->payload.len) {
1757 res->hdr.reason = SYNC_REASON_ENCODING_ERROR;
1762 memcpy(res->payload.buf, vnp, Vn_class(vnp)->residentSize);
1763 res->hdr.response_len += Vn_class(vnp)->residentSize;
1766 #ifndef AFS_DEMAND_ATTACH_FS
1775 FSYNC_com_StatsOp(osi_socket fd, SYNC_command * com, SYNC_response * res)
1777 afs_int32 code = SYNC_OK;
1778 FSSYNC_StatsOp_command scom;
1780 if (com->recv_len != (sizeof(com->hdr) + sizeof(FSSYNC_StatsOp_hdr))) {
1781 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1782 res->hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
1783 return SYNC_COM_ERROR;
1786 scom.hdr = &com->hdr;
1787 scom.sop = (FSSYNC_StatsOp_hdr *) com->payload.buf;
1790 ViceLog(125, ("FSYNC_com_StatsOp: fd %d got command for stats: "
1791 "{vlru_generation = %lu, hash_bucket = %lu, partName = "
1792 "%.16s}\n", (int)fd,
1793 afs_printable_uint32_lu(scom.sop->args.vlru_generation),
1794 afs_printable_uint32_lu(scom.sop->args.hash_bucket),
1795 scom.sop->args.partName));
1797 switch (com->hdr.command) {
1798 case FSYNC_VOL_STATS_GENERAL:
1799 code = FSYNC_com_StatsOpGeneral(&scom, res);
1801 #ifdef AFS_DEMAND_ATTACH_FS
1802 /* statistics for the following subsystems are only tracked
1803 * for demand attach fileservers */
1804 case FSYNC_VOL_STATS_VICEP:
1805 code = FSYNC_com_StatsOpViceP(&scom, res);
1807 case FSYNC_VOL_STATS_HASH:
1808 code = FSYNC_com_StatsOpHash(&scom, res);
1810 case FSYNC_VOL_STATS_HDR:
1811 code = FSYNC_com_StatsOpHdr(&scom, res);
1813 case FSYNC_VOL_STATS_VLRU:
1814 code = FSYNC_com_StatsOpVLRU(&scom, res);
1816 #endif /* AFS_DEMAND_ATTACH_FS */
1818 code = SYNC_BAD_COMMAND;
1825 FSYNC_com_StatsOpGeneral(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1827 afs_int32 code = SYNC_OK;
1829 memcpy(res->payload.buf, &VStats, sizeof(VStats));
1830 res->hdr.response_len += sizeof(VStats);
1835 #ifdef AFS_DEMAND_ATTACH_FS
1837 FSYNC_com_StatsOpViceP(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1839 afs_int32 code = SYNC_OK;
1840 struct DiskPartition64 * dp;
1841 struct DiskPartitionStats64 * stats;
1843 if (SYNC_verifyProtocolString(scom->sop->args.partName, sizeof(scom->sop->args.partName))) {
1844 res->hdr.reason = SYNC_REASON_MALFORMED_PACKET;
1849 dp = VGetPartition_r(scom->sop->args.partName, 0);
1853 stats = (struct DiskPartitionStats64 *) res->payload.buf;
1854 stats->free = dp->free;
1855 stats->totalUsable = dp->totalUsable;
1856 stats->minFree = dp->minFree;
1857 stats->f_files = dp->f_files;
1858 stats->vol_list_len = dp->vol_list.len;
1860 res->hdr.response_len += sizeof(struct DiskPartitionStats64);
1868 FSYNC_com_StatsOpHash(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1870 afs_int32 code = SYNC_OK;
1871 struct VolumeHashChainStats * stats;
1872 struct VolumeHashChainHead * head;
1874 if (scom->sop->args.hash_bucket >= VolumeHashTable.Size) {
1878 head = &VolumeHashTable.Table[scom->sop->args.hash_bucket];
1879 stats = (struct VolumeHashChainStats *) res->payload.buf;
1880 stats->table_size = VolumeHashTable.Size;
1881 stats->chain_len = head->len;
1882 stats->chain_cacheCheck = head->cacheCheck;
1883 stats->chain_busy = head->busy;
1884 AssignInt64(head->looks, &stats->chain_looks);
1885 AssignInt64(head->gets, &stats->chain_gets);
1886 AssignInt64(head->reorders, &stats->chain_reorders);
1888 res->hdr.response_len += sizeof(struct VolumeHashChainStats);
1894 FSYNC_com_StatsOpHdr(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1896 afs_int32 code = SYNC_OK;
1898 memcpy(res->payload.buf, &volume_hdr_LRU.stats, sizeof(volume_hdr_LRU.stats));
1899 res->hdr.response_len += sizeof(volume_hdr_LRU.stats);
1905 FSYNC_com_StatsOpVLRU(FSSYNC_StatsOp_command * scom, SYNC_response * res)
1907 afs_int32 code = SYNC_OK;
1909 code = SYNC_BAD_COMMAND;
1913 #endif /* AFS_DEMAND_ATTACH_FS */
1916 * populate an FSSYNC_VolOp_info object from a command packet object.
1918 * @param[in] vcom pointer to command packet
1919 * @param[out] info pointer to info object which will be populated
1921 * @note FSSYNC_VolOp_info objects are attached to Volume objects when
1922 * a volume operation is commenced.
1927 FSYNC_com_to_info(FSSYNC_VolOp_command * vcom, FSSYNC_VolOp_info * info)
1929 memcpy(&info->com, vcom->hdr, sizeof(SYNC_command_hdr));
1930 memcpy(&info->vop, vcom->vop, sizeof(FSSYNC_VolOp_hdr));
1931 info->vol_op_state = FSSYNC_VolOpPending;
1935 * check whether command packet partition name matches volume
1936 * object's partition name.
1938 * @param[in] vcom pointer to command packet
1939 * @param[in] vp pointer to volume object
1940 * @param[in] match_anon anon matching control flag (see note below)
1942 * @return whether partitions match
1943 * @retval 0 partitions do NOT match
1944 * @retval 1 partitions match
1946 * @note if match_anon is non-zero, then this function will return a
1947 * positive match for a zero-length partition string in the
1953 FSYNC_partMatch(FSSYNC_VolOp_command * vcom, Volume * vp, int match_anon)
1955 return ((match_anon && vcom->vop->partName[0] == 0) ||
1956 (strncmp(vcom->vop->partName, V_partition(vp)->name,
1957 sizeof(vcom->vop->partName)) == 0));
1962 FSYNC_Drop(osi_socket fd)
1964 struct offlineInfo *p;
1967 char tvolName[VMAXPATHLEN];
1970 p = OfflineVolumes[FindHandler(fd)];
1971 for (i = 0; i < MAXOFFLINEVOLUMES; i++) {
1972 if (p[i].volumeID) {
1976 tvolName[0] = OS_DIRSEPC;
1977 sprintf(&tvolName[1], VFORMAT, afs_printable_uint32_lu(p[i].volumeID));
1978 vp = VAttachVolumeByName_r(&error, p[i].partName, tvolName,
1995 static int AcceptHandler = -1; /* handler id for accept, if turned on */
2000 if (AcceptHandler == -1) {
2001 osi_Assert(AddHandler(fssync_server_state.fd, FSYNC_newconnection));
2002 AcceptHandler = FindHandler(fssync_server_state.fd);
2009 if (AcceptHandler != -1) {
2010 osi_Assert(RemoveHandler(fssync_server_state.fd));
2015 /* The multiple FD handling code. */
2017 static osi_socket HandlerFD[MAXHANDLERS];
2018 static void (*HandlerProc[MAXHANDLERS]) (osi_socket);
2024 ObtainWriteLock(&FSYNC_handler_lock);
2025 for (i = 0; i < MAXHANDLERS; i++) {
2026 HandlerFD[i] = OSI_NULLSOCKET;
2029 ReleaseWriteLock(&FSYNC_handler_lock);
2032 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
2034 CallHandler(struct pollfd *fds, int nfds, int mask)
2038 ObtainReadLock(&FSYNC_handler_lock);
2039 for (i = 0; i < nfds; i++) {
2040 if (fds[i].revents & mask) {
2041 handler = FindHandler_r(fds[i].fd);
2042 ReleaseReadLock(&FSYNC_handler_lock);
2043 (*HandlerProc[handler]) (fds[i].fd);
2044 ObtainReadLock(&FSYNC_handler_lock);
2047 ReleaseReadLock(&FSYNC_handler_lock);
2051 CallHandler(fd_set * fdsetp)
2054 ObtainReadLock(&FSYNC_handler_lock);
2055 for (i = 0; i < MAXHANDLERS; i++) {
2056 if (HandlerFD[i] >= 0 && FD_ISSET(HandlerFD[i], fdsetp)) {
2057 ReleaseReadLock(&FSYNC_handler_lock);
2058 (*HandlerProc[i]) (HandlerFD[i]);
2059 ObtainReadLock(&FSYNC_handler_lock);
2062 ReleaseReadLock(&FSYNC_handler_lock);
2067 AddHandler(osi_socket afd, void (*aproc) (osi_socket))
2070 ObtainWriteLock(&FSYNC_handler_lock);
2071 for (i = 0; i < MAXHANDLERS; i++)
2072 if (HandlerFD[i] == OSI_NULLSOCKET)
2074 if (i >= MAXHANDLERS) {
2075 ReleaseWriteLock(&FSYNC_handler_lock);
2079 HandlerProc[i] = aproc;
2080 ReleaseWriteLock(&FSYNC_handler_lock);
2085 FindHandler(osi_socket afd)
2088 ObtainReadLock(&FSYNC_handler_lock);
2089 for (i = 0; i < MAXHANDLERS; i++)
2090 if (HandlerFD[i] == afd) {
2091 ReleaseReadLock(&FSYNC_handler_lock);
2094 ReleaseReadLock(&FSYNC_handler_lock); /* just in case */
2096 return -1; /* satisfy compiler */
2100 FindHandler_r(osi_socket afd)
2103 for (i = 0; i < MAXHANDLERS; i++)
2104 if (HandlerFD[i] == afd) {
2108 return -1; /* satisfy compiler */
2112 RemoveHandler(osi_socket afd)
2114 ObtainWriteLock(&FSYNC_handler_lock);
2115 HandlerFD[FindHandler_r(afd)] = OSI_NULLSOCKET;
2116 ReleaseWriteLock(&FSYNC_handler_lock);
2120 #if defined(HAVE_POLL) && defined(AFS_PTHREAD_ENV)
2122 GetHandler(struct pollfd *fds, int maxfds, int events, int *nfds)
2126 ObtainReadLock(&FSYNC_handler_lock);
2127 for (i = 0; i < MAXHANDLERS; i++)
2128 if (HandlerFD[i] != OSI_NULLSOCKET) {
2129 osi_Assert(fdi<maxfds);
2130 fds[fdi].fd = HandlerFD[i];
2131 fds[fdi].events = events;
2132 fds[fdi].revents = 0;
2136 ReleaseReadLock(&FSYNC_handler_lock);
2140 GetHandler(fd_set * fdsetp, int *maxfdp)
2145 ObtainReadLock(&FSYNC_handler_lock); /* just in case */
2146 for (i = 0; i < MAXHANDLERS; i++)
2147 if (HandlerFD[i] != OSI_NULLSOCKET) {
2148 FD_SET(HandlerFD[i], fdsetp);
2149 #ifndef AFS_NT40_ENV
2150 /* On Windows the nfds parameter to select() is ignored */
2151 if (maxfd < HandlerFD[i] || maxfd == (int)-1)
2152 maxfd = HandlerFD[i];
2153 #endif /* AFS_NT40_ENV */
2156 ReleaseReadLock(&FSYNC_handler_lock); /* just in case */
2158 #endif /* HAVE_POLL && AFS_PTHREAD_ENV */
2160 #endif /* FSSYNC_BUILD_SERVER */