2 * Copyright 2006-2008, Sine Nomine Associates 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
13 * OpenAFS demand attach fileserver
14 * Salvage server synchronization with fileserver.
17 /* This controls the size of an fd_set; it must be defined early before
18 * the system headers define that type and the macros that operate on it.
19 * Its value should be as large as the maximum file descriptor limit we
20 * are likely to run into on any platform. Right now, that is 65536
21 * which is the default hard fd limit on Solaris 9 */
23 #define FD_SETSIZE 65536
26 #include <afsconfig.h>
27 #include <afs/param.h>
32 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
51 #include <afs/afsint.h>
53 #include <afs/errors.h>
57 #include <afs/afssyscalls.h>
61 #include "partition.h"
62 #include <rx/rx_queue.h>
63 #include <afs/procmgmt.h>
65 #if !defined(offsetof)
69 #ifdef USE_UNIX_SOCKETS
70 #include <afs/afsutil.h>
75 /*@printflike@*/ extern void Log(const char *format, ...);
77 #define MAXHANDLERS 4 /* Up to 4 clients; must be at least 2, so that
78 * move = dump+restore can run on single server */
80 /* Forward declarations */
81 static void * SALVSYNC_syncThread(void *);
82 static void SALVSYNC_newconnection(int fd);
83 static void SALVSYNC_com(int fd);
84 static void SALVSYNC_Drop(int fd);
85 static void AcceptOn(void);
86 static void AcceptOff(void);
87 static void InitHandler(void);
88 static void CallHandler(fd_set * fdsetp);
89 static int AddHandler(int afd, void (*aproc) (int));
90 static int FindHandler(register int afd);
91 static int FindHandler_r(register int afd);
92 static int RemoveHandler(register int afd);
93 static void GetHandler(fd_set * fdsetp, int *maxfdp);
97 * This lock controls access to the handler array.
99 struct Lock SALVSYNC_handler_lock;
102 #ifdef AFS_DEMAND_ATTACH_FS
104 * SALVSYNC is a feature specific to the demand attach fileserver
107 static int AllocNode(struct SalvageQueueNode ** node);
109 static int AddToSalvageQueue(struct SalvageQueueNode * node);
110 static void DeleteFromSalvageQueue(struct SalvageQueueNode * node);
111 static void AddToPendingQueue(struct SalvageQueueNode * node);
112 static void DeleteFromPendingQueue(struct SalvageQueueNode * node);
113 static struct SalvageQueueNode * LookupPendingCommand(SALVSYNC_command_hdr * qry);
114 static struct SalvageQueueNode * LookupPendingCommandByPid(int pid);
115 static void UpdateCommandPrio(struct SalvageQueueNode * node);
116 static void HandlePrio(struct SalvageQueueNode * clone,
117 struct SalvageQueueNode * parent,
118 afs_uint32 new_prio);
120 static int LinkNode(struct SalvageQueueNode * parent,
121 struct SalvageQueueNode * clone);
123 static struct SalvageQueueNode * LookupNode(VolumeId vid, char * partName,
124 struct SalvageQueueNode ** parent);
125 static struct SalvageQueueNode * LookupNodeByCommand(SALVSYNC_command_hdr * qry,
126 struct SalvageQueueNode ** parent);
127 static void AddNodeToHash(struct SalvageQueueNode * node);
128 static void DeleteNodeFromHash(struct SalvageQueueNode * node);
130 static afs_int32 SALVSYNC_com_Salvage(SALVSYNC_command * com, SALVSYNC_response * res);
131 static afs_int32 SALVSYNC_com_Cancel(SALVSYNC_command * com, SALVSYNC_response * res);
132 static afs_int32 SALVSYNC_com_Query(SALVSYNC_command * com, SALVSYNC_response * res);
133 static afs_int32 SALVSYNC_com_CancelAll(SALVSYNC_command * com, SALVSYNC_response * res);
134 static afs_int32 SALVSYNC_com_Link(SALVSYNC_command * com, SALVSYNC_response * res);
139 extern pthread_mutex_t vol_salvsync_mutex;
142 * salvsync server socket handle.
144 static SYNC_server_state_t salvsync_server_state =
145 { -1, /* file descriptor */
146 SALVSYNC_ENDPOINT_DECL, /* server endpoint */
147 SALVSYNC_PROTO_VERSION, /* protocol version */
148 5, /* bind() retry limit */
149 100, /* listen() queue depth */
150 "SALVSYNC", /* protocol name string */
155 * queue of all volumes waiting to be salvaged.
157 struct SalvageQueue {
158 volatile int total_len;
159 volatile afs_int32 last_insert; /**< id of last partition to have a salvage node inserted */
160 volatile int len[VOLMAXPARTS+1];
161 volatile struct rx_queue part[VOLMAXPARTS+1]; /**< per-partition queues of pending salvages */
164 static struct SalvageQueue salvageQueue; /* volumes waiting to be salvaged */
167 * queue of all volumes currently being salvaged.
170 volatile struct rx_queue q; /**< queue of salvages in progress */
171 volatile int len; /**< length of in-progress queue */
172 pthread_cond_t queue_change_cv;
174 static struct QueueHead pendingQueue; /* volumes being salvaged */
177 * whether a partition has a salvage in progress
179 * the salvager code only permits one salvage per partition at a time
181 * the following hack tries to keep salvaged parallelism high by
182 * only permitting one salvage dispatch per partition at a time
184 * unfortunately, the parallel salvager currently
185 * has a rather braindead routine that won't permit
186 * multiple salvages on the same "device". this
187 * function happens to break pretty badly on lvm, raid luns, etc.
189 * this hack isn't good enough to stop the device limiting code from
190 * crippling performance. someday that code needs to be rewritten
192 static int partition_salvaging[VOLMAXPARTS+1];
194 #define VSHASH_SIZE 64
195 #define VSHASH_MASK (VSHASH_SIZE-1)
196 #define VSHASH(vid) ((vid)&VSHASH_MASK)
198 static struct QueueHead SalvageHashTable[VSHASH_SIZE];
200 static struct SalvageQueueNode *
201 LookupNode(afs_uint32 vid, char * partName,
202 struct SalvageQueueNode ** parent)
204 struct rx_queue *qp, *nqp;
205 struct SalvageQueueNode *vsp;
206 int idx = VSHASH(vid);
208 for (queue_Scan(&SalvageHashTable[idx], qp, nqp, rx_queue)) {
209 vsp = (struct SalvageQueueNode *)((char *)qp - offsetof(struct SalvageQueueNode, hash_chain));
210 if ((vsp->command.sop.volume == vid) &&
211 !strncmp(vsp->command.sop.partName, partName, sizeof(vsp->command.sop.partName))) {
216 if (queue_IsEnd(&SalvageHashTable[idx], qp)) {
222 *parent = (vsp->type == SALVSYNC_VOLGROUP_CLONE) ?
223 vsp->volgroup.parent : vsp;
232 static struct SalvageQueueNode *
233 LookupNodeByCommand(SALVSYNC_command_hdr * qry,
234 struct SalvageQueueNode ** parent)
236 return LookupNode(qry->volume, qry->partName, parent);
240 AddNodeToHash(struct SalvageQueueNode * node)
242 int idx = VSHASH(node->command.sop.volume);
244 if (queue_IsOnQueue(&node->hash_chain)) {
248 queue_Append(&SalvageHashTable[idx], &node->hash_chain);
249 SalvageHashTable[idx].len++;
253 DeleteNodeFromHash(struct SalvageQueueNode * node)
255 int idx = VSHASH(node->command.sop.volume);
257 if (queue_IsNotOnQueue(&node->hash_chain)) {
261 queue_Remove(&node->hash_chain);
262 SalvageHashTable[idx].len--;
266 SALVSYNC_salvInit(void)
270 pthread_attr_t tattr;
272 /* initialize the queues */
273 Lock_Init(&SALVSYNC_handler_lock);
274 assert(pthread_cond_init(&salvageQueue.cv, NULL) == 0);
275 for (i = 0; i <= VOLMAXPARTS; i++) {
276 queue_Init(&salvageQueue.part[i]);
277 salvageQueue.len[i] = 0;
279 assert(pthread_cond_init(&pendingQueue.queue_change_cv, NULL) == 0);
280 queue_Init(&pendingQueue);
281 salvageQueue.total_len = pendingQueue.len = 0;
282 salvageQueue.last_insert = -1;
283 memset(partition_salvaging, 0, sizeof(partition_salvaging));
285 for (i = 0; i < VSHASH_SIZE; i++) {
286 assert(pthread_cond_init(&SalvageHashTable[i].queue_change_cv, NULL) == 0);
287 SalvageHashTable[i].len = 0;
288 queue_Init(&SalvageHashTable[i]);
291 /* start the salvsync thread */
292 assert(pthread_attr_init(&tattr) == 0);
293 assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
294 assert(pthread_create(&tid, &tattr, SALVSYNC_syncThread, NULL) == 0);
298 static fd_set SALVSYNC_readfds;
301 SALVSYNC_syncThread(void * args)
307 SYNC_server_state_t * state = &salvsync_server_state;
309 SYNC_getAddr(&state->endpoint, &state->addr);
310 SYNC_cleanupSock(state);
313 (void)signal(SIGPIPE, SIG_IGN);
316 state->fd = SYNC_getSock(&state->endpoint);
317 code = SYNC_bindSock(state);
325 GetHandler(&SALVSYNC_readfds, &maxfd);
326 /* Note: check for >= 1 below is essential since IOMGR_select
327 * doesn't have exactly same semantics as select.
329 if (select(maxfd + 1, &SALVSYNC_readfds, NULL, NULL, NULL) >= 1)
330 CallHandler(&SALVSYNC_readfds);
337 SALVSYNC_newconnection(int afd)
339 #ifdef USE_UNIX_SOCKETS
340 struct sockaddr_un other;
341 #else /* USE_UNIX_SOCKETS */
342 struct sockaddr_in other;
345 junk = sizeof(other);
346 fd = accept(afd, (struct sockaddr *)&other, &junk);
348 Log("SALVSYNC_newconnection: accept failed, errno==%d\n", errno);
350 } else if (!AddHandler(fd, SALVSYNC_com)) {
352 assert(AddHandler(fd, SALVSYNC_com));
356 /* this function processes commands from an salvsync file descriptor (fd) */
357 static afs_int32 SALV_cnt = 0;
363 SALVSYNC_response_hdr sres_hdr;
364 SALVSYNC_command scom;
365 SALVSYNC_response sres;
366 SYNC_PROTO_BUF_DECL(buf);
368 com.payload.buf = (void *)buf;
369 com.payload.len = SYNC_PROTO_MAX_LEN;
370 res.payload.buf = (void *) &sres_hdr;
371 res.payload.len = sizeof(sres_hdr);
372 res.hdr.response_len = sizeof(res.hdr) + sizeof(sres_hdr);
373 res.hdr.proto_version = SALVSYNC_PROTO_VERSION;
376 scom.sop = (SALVSYNC_command_hdr *) buf;
379 sres.sop = &sres_hdr;
383 if (SYNC_getCom(fd, &com)) {
384 Log("SALVSYNC_com: read failed; dropping connection (cnt=%d)\n", SALV_cnt);
389 if (com.recv_len < sizeof(com.hdr)) {
390 Log("SALVSYNC_com: invalid protocol message length (%u)\n", com.recv_len);
391 res.hdr.response = SYNC_COM_ERROR;
392 res.hdr.reason = SYNC_REASON_MALFORMED_PACKET;
393 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
397 if (com.hdr.proto_version != SALVSYNC_PROTO_VERSION) {
398 Log("SALVSYNC_com: invalid protocol version (%u)\n", com.hdr.proto_version);
399 res.hdr.response = SYNC_COM_ERROR;
400 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
404 if (com.hdr.command == SYNC_COM_CHANNEL_CLOSE) {
405 res.hdr.response = SYNC_OK;
406 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
410 if (com.recv_len != (sizeof(com.hdr) + sizeof(SALVSYNC_command_hdr))) {
411 Log("SALVSYNC_com: invalid protocol message length (%u)\n", com.recv_len);
412 res.hdr.response = SYNC_COM_ERROR;
413 res.hdr.reason = SYNC_REASON_MALFORMED_PACKET;
414 res.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
419 switch (com.hdr.command) {
422 case SALVSYNC_SALVAGE:
423 case SALVSYNC_RAISEPRIO:
424 res.hdr.response = SALVSYNC_com_Salvage(&scom, &sres);
426 case SALVSYNC_CANCEL:
427 /* cancel a salvage */
428 res.hdr.response = SALVSYNC_com_Cancel(&scom, &sres);
430 case SALVSYNC_CANCELALL:
431 /* cancel all queued salvages */
432 res.hdr.response = SALVSYNC_com_CancelAll(&scom, &sres);
435 /* query whether a volume is done salvaging */
436 res.hdr.response = SALVSYNC_com_Query(&scom, &sres);
438 case SALVSYNC_OP_LINK:
439 /* link a clone to its parent in the scheduler */
440 res.hdr.response = SALVSYNC_com_Link(&scom, &sres);
443 res.hdr.response = SYNC_BAD_COMMAND;
447 sres_hdr.sq_len = salvageQueue.total_len;
448 sres_hdr.pq_len = pendingQueue.len;
452 SYNC_putRes(fd, &res);
453 if (res.hdr.flags & SYNC_FLAG_CHANNEL_SHUTDOWN) {
459 * request that a volume be salvaged.
461 * @param[in] com inbound command object
462 * @param[out] res outbound response object
464 * @return operation status
465 * @retval SYNC_OK success
466 * @retval SYNC_DENIED failed to enqueue request
467 * @retval SYNC_FAILED malformed command packet
469 * @note this is a SALVSYNC protocol rpc handler
473 * @post the volume is enqueued in the to-be-salvaged queue.
474 * if the volume was already in the salvage queue, its
475 * priority (and thus its location in the queue) are
479 SALVSYNC_com_Salvage(SALVSYNC_command * com, SALVSYNC_response * res)
481 afs_int32 code = SYNC_OK;
482 struct SalvageQueueNode * node, * clone;
485 if (SYNC_verifyProtocolString(com->sop->partName, sizeof(com->sop->partName))) {
487 res->hdr->reason = SYNC_REASON_MALFORMED_PACKET;
491 clone = LookupNodeByCommand(com->sop, &node);
494 if (AllocNode(&node)) {
496 res->hdr->reason = SYNC_REASON_NOMEM;
503 HandlePrio(clone, node, com->sop->prio);
505 switch (node->state) {
506 case SALVSYNC_STATE_QUEUED:
507 UpdateCommandPrio(node);
510 case SALVSYNC_STATE_ERROR:
511 case SALVSYNC_STATE_DONE:
512 case SALVSYNC_STATE_UNKNOWN:
513 memcpy(&clone->command.com, com->hdr, sizeof(SYNC_command_hdr));
514 memcpy(&clone->command.sop, com->sop, sizeof(SALVSYNC_command_hdr));
517 * make sure volgroup parent partition path is kept coherent
519 * If we ever want to support non-COW clones on a machine holding
520 * the RW site, please note that this code does not work under the
521 * conditions where someone zaps a COW clone on partition X, and
522 * subsequently creates a full clone on partition Y -- we'd need
523 * an inverse to SALVSYNC_com_Link.
524 * -- tkeiser 11/28/2007
526 strcpy(node->command.sop.partName, com->sop->partName);
528 if (AddToSalvageQueue(node)) {
541 res->hdr->flags |= SALVSYNC_FLAG_VOL_STATS_VALID;
542 res->sop->state = node->state;
543 res->sop->prio = node->command.sop.prio;
550 * cancel a pending salvage request.
552 * @param[in] com inbound command object
553 * @param[out] res outbound response object
555 * @return operation status
556 * @retval SYNC_OK success
557 * @retval SYNC_FAILED malformed command packet
559 * @note this is a SALVSYNC protocol rpc handler
564 SALVSYNC_com_Cancel(SALVSYNC_command * com, SALVSYNC_response * res)
566 afs_int32 code = SYNC_OK;
567 struct SalvageQueueNode * node;
569 if (SYNC_verifyProtocolString(com->sop->partName, sizeof(com->sop->partName))) {
571 res->hdr->reason = SYNC_REASON_MALFORMED_PACKET;
575 node = LookupNodeByCommand(com->sop, NULL);
578 res->sop->state = SALVSYNC_STATE_UNKNOWN;
581 res->hdr->flags |= SALVSYNC_FLAG_VOL_STATS_VALID;
582 res->sop->prio = node->command.sop.prio;
583 res->sop->state = node->state;
584 if ((node->type == SALVSYNC_VOLGROUP_PARENT) &&
585 (node->state == SALVSYNC_STATE_QUEUED)) {
586 DeleteFromSalvageQueue(node);
595 * cancel all pending salvage requests.
597 * @param[in] com incoming command object
598 * @param[out] res outbound response object
600 * @return operation status
601 * @retval SYNC_OK success
603 * @note this is a SALVSYNC protocol rpc handler
608 SALVSYNC_com_CancelAll(SALVSYNC_command * com, SALVSYNC_response * res)
610 struct SalvageQueueNode * np, *nnp;
611 struct DiskPartition64 * dp;
613 for (dp = DiskPartitionList ; dp ; dp = dp->next) {
614 for (queue_Scan(&salvageQueue.part[dp->index], np, nnp, SalvageQueueNode)) {
615 DeleteFromSalvageQueue(np);
623 * link a queue node for a clone to its parent volume.
625 * @param[in] com inbound command object
626 * @param[out] res outbound response object
628 * @return operation status
629 * @retval SYNC_OK success
630 * @retval SYNC_FAILED malformed command packet
631 * @retval SYNC_DENIED the request could not be completed
633 * @note this is a SALVSYNC protocol rpc handler
635 * @post the requested volume is marked as a child of another volume.
636 * thus, future salvage requests for this volume will result in the
637 * parent of the volume group being scheduled for salvage instead
643 SALVSYNC_com_Link(SALVSYNC_command * com, SALVSYNC_response * res)
645 afs_int32 code = SYNC_OK;
646 struct SalvageQueueNode * clone, * parent;
648 if (SYNC_verifyProtocolString(com->sop->partName, sizeof(com->sop->partName))) {
650 res->hdr->reason = SYNC_REASON_MALFORMED_PACKET;
654 /* lookup clone's salvage scheduling node */
655 clone = LookupNodeByCommand(com->sop, NULL);
658 res->hdr->reason = SALVSYNC_REASON_ERROR;
662 /* lookup parent's salvage scheduling node */
663 parent = LookupNode(com->sop->parent, com->sop->partName, NULL);
664 if (parent == NULL) {
665 if (AllocNode(&parent)) {
667 res->hdr->reason = SYNC_REASON_NOMEM;
670 memcpy(&parent->command.com, com->hdr, sizeof(SYNC_command_hdr));
671 memcpy(&parent->command.sop, com->sop, sizeof(SALVSYNC_command_hdr));
672 parent->command.sop.volume = parent->command.sop.parent = com->sop->parent;
673 AddNodeToHash(parent);
676 if (LinkNode(parent, clone)) {
686 * query the status of a volume salvage request.
688 * @param[in] com inbound command object
689 * @param[out] res outbound response object
691 * @return operation status
692 * @retval SYNC_OK success
693 * @retval SYNC_FAILED malformed command packet
695 * @note this is a SALVSYNC protocol rpc handler
700 SALVSYNC_com_Query(SALVSYNC_command * com, SALVSYNC_response * res)
702 afs_int32 code = SYNC_OK;
703 struct SalvageQueueNode * node;
705 if (SYNC_verifyProtocolString(com->sop->partName, sizeof(com->sop->partName))) {
707 res->hdr->reason = SYNC_REASON_MALFORMED_PACKET;
711 LookupNodeByCommand(com->sop, &node);
713 /* query whether a volume is done salvaging */
715 res->sop->state = SALVSYNC_STATE_UNKNOWN;
718 res->hdr->flags |= SALVSYNC_FLAG_VOL_STATS_VALID;
719 res->sop->state = node->state;
720 res->sop->prio = node->command.sop.prio;
728 SALVSYNC_Drop(int fd)
739 static int AcceptHandler = -1; /* handler id for accept, if turned on */
744 if (AcceptHandler == -1) {
745 assert(AddHandler(salvsync_server_state.fd, SALVSYNC_newconnection));
746 AcceptHandler = FindHandler(salvsync_server_state.fd);
753 if (AcceptHandler != -1) {
754 assert(RemoveHandler(salvsync_server_state.fd));
759 /* The multiple FD handling code. */
761 static int HandlerFD[MAXHANDLERS];
762 static void (*HandlerProc[MAXHANDLERS]) (int);
768 ObtainWriteLock(&SALVSYNC_handler_lock);
769 for (i = 0; i < MAXHANDLERS; i++) {
771 HandlerProc[i] = NULL;
773 ReleaseWriteLock(&SALVSYNC_handler_lock);
777 CallHandler(fd_set * fdsetp)
780 ObtainReadLock(&SALVSYNC_handler_lock);
781 for (i = 0; i < MAXHANDLERS; i++) {
782 if (HandlerFD[i] >= 0 && FD_ISSET(HandlerFD[i], fdsetp)) {
783 ReleaseReadLock(&SALVSYNC_handler_lock);
784 (*HandlerProc[i]) (HandlerFD[i]);
785 ObtainReadLock(&SALVSYNC_handler_lock);
788 ReleaseReadLock(&SALVSYNC_handler_lock);
792 AddHandler(int afd, void (*aproc) (int))
795 ObtainWriteLock(&SALVSYNC_handler_lock);
796 for (i = 0; i < MAXHANDLERS; i++)
797 if (HandlerFD[i] == -1)
799 if (i >= MAXHANDLERS) {
800 ReleaseWriteLock(&SALVSYNC_handler_lock);
804 HandlerProc[i] = aproc;
805 ReleaseWriteLock(&SALVSYNC_handler_lock);
810 FindHandler(register int afd)
813 ObtainReadLock(&SALVSYNC_handler_lock);
814 for (i = 0; i < MAXHANDLERS; i++)
815 if (HandlerFD[i] == afd) {
816 ReleaseReadLock(&SALVSYNC_handler_lock);
819 ReleaseReadLock(&SALVSYNC_handler_lock); /* just in case */
821 return -1; /* satisfy compiler */
825 FindHandler_r(register int afd)
828 for (i = 0; i < MAXHANDLERS; i++)
829 if (HandlerFD[i] == afd) {
833 return -1; /* satisfy compiler */
837 RemoveHandler(register int afd)
839 ObtainWriteLock(&SALVSYNC_handler_lock);
840 HandlerFD[FindHandler_r(afd)] = -1;
841 ReleaseWriteLock(&SALVSYNC_handler_lock);
846 GetHandler(fd_set * fdsetp, int *maxfdp)
849 register int maxfd = -1;
851 ObtainReadLock(&SALVSYNC_handler_lock); /* just in case */
852 for (i = 0; i < MAXHANDLERS; i++)
853 if (HandlerFD[i] != -1) {
854 FD_SET(HandlerFD[i], fdsetp);
855 if (maxfd < HandlerFD[i])
856 maxfd = HandlerFD[i];
859 ReleaseReadLock(&SALVSYNC_handler_lock); /* just in case */
863 * allocate a salvage queue node.
865 * @param[out] node_out address in which to store new node pointer
867 * @return operation status
869 * @retval 1 failed to allocate node
874 AllocNode(struct SalvageQueueNode ** node_out)
877 struct SalvageQueueNode * node;
879 *node_out = node = (struct SalvageQueueNode *)
880 malloc(sizeof(struct SalvageQueueNode));
886 memset(node, 0, sizeof(struct SalvageQueueNode));
887 node->type = SALVSYNC_VOLGROUP_PARENT;
888 node->state = SALVSYNC_STATE_UNKNOWN;
895 * link a salvage queue node to its parent.
897 * @param[in] parent pointer to queue node for parent of volume group
898 * @param[in] clone pointer to queue node for a clone
900 * @return operation status
907 LinkNode(struct SalvageQueueNode * parent,
908 struct SalvageQueueNode * clone)
913 /* check for attaching a clone to a clone */
914 if (parent->type != SALVSYNC_VOLGROUP_PARENT) {
919 /* check for pre-existing registration and openings */
920 for (idx = 0; idx < VOLMAXTYPES; idx++) {
921 if (parent->volgroup.children[idx] == clone) {
924 if (parent->volgroup.children[idx] == NULL) {
928 if (idx == VOLMAXTYPES) {
933 /* link parent and child */
934 parent->volgroup.children[idx] = clone;
935 clone->type = SALVSYNC_VOLGROUP_CLONE;
936 clone->volgroup.parent = parent;
940 switch (clone->state) {
941 case SALVSYNC_STATE_QUEUED:
942 DeleteFromSalvageQueue(clone);
944 case SALVSYNC_STATE_SALVAGING:
945 switch (parent->state) {
946 case SALVSYNC_STATE_UNKNOWN:
947 case SALVSYNC_STATE_ERROR:
948 case SALVSYNC_STATE_DONE:
949 parent->command.sop.prio = clone->command.sop.prio;
950 AddToSalvageQueue(parent);
953 case SALVSYNC_STATE_QUEUED:
954 if (clone->command.sop.prio) {
955 parent->command.sop.prio += clone->command.sop.prio;
956 UpdateCommandPrio(parent);
974 HandlePrio(struct SalvageQueueNode * clone,
975 struct SalvageQueueNode * node,
980 switch (node->state) {
981 case SALVSYNC_STATE_ERROR:
982 case SALVSYNC_STATE_DONE:
983 case SALVSYNC_STATE_UNKNOWN:
984 node->command.sop.prio = 0;
988 if (new_prio < clone->command.sop.prio) {
989 /* strange. let's just set our delta to 1 */
992 delta = new_prio - clone->command.sop.prio;
995 if (clone->type == SALVSYNC_VOLGROUP_CLONE) {
996 clone->command.sop.prio = new_prio;
999 node->command.sop.prio += delta;
1003 AddToSalvageQueue(struct SalvageQueueNode * node)
1006 struct SalvageQueueNode * last = NULL;
1008 id = volutil_GetPartitionID(node->command.sop.partName);
1009 if (id < 0 || id > VOLMAXPARTS) {
1012 if (!VGetPartitionById_r(id, 0)) {
1013 /* don't enqueue salvage requests for unmounted partitions */
1016 if (queue_IsOnQueue(node)) {
1020 if (queue_IsNotEmpty(&salvageQueue.part[id])) {
1021 last = queue_Last(&salvageQueue.part[id], SalvageQueueNode);
1023 queue_Append(&salvageQueue.part[id], node);
1024 salvageQueue.len[id]++;
1025 salvageQueue.total_len++;
1026 salvageQueue.last_insert = id;
1027 node->partition_id = id;
1028 node->state = SALVSYNC_STATE_QUEUED;
1030 /* reorder, if necessary */
1031 if (last && last->command.sop.prio < node->command.sop.prio) {
1032 UpdateCommandPrio(node);
1035 assert(pthread_cond_broadcast(&salvageQueue.cv) == 0);
1040 DeleteFromSalvageQueue(struct SalvageQueueNode * node)
1042 if (queue_IsOnQueue(node)) {
1044 salvageQueue.len[node->partition_id]--;
1045 salvageQueue.total_len--;
1046 node->state = SALVSYNC_STATE_UNKNOWN;
1047 assert(pthread_cond_broadcast(&salvageQueue.cv) == 0);
1052 AddToPendingQueue(struct SalvageQueueNode * node)
1054 queue_Append(&pendingQueue, node);
1056 node->state = SALVSYNC_STATE_SALVAGING;
1057 assert(pthread_cond_broadcast(&pendingQueue.queue_change_cv) == 0);
1061 DeleteFromPendingQueue(struct SalvageQueueNode * node)
1063 if (queue_IsOnQueue(node)) {
1066 node->state = SALVSYNC_STATE_UNKNOWN;
1067 assert(pthread_cond_broadcast(&pendingQueue.queue_change_cv) == 0);
1071 static struct SalvageQueueNode *
1072 LookupPendingCommand(SALVSYNC_command_hdr * qry)
1074 struct SalvageQueueNode * np, * nnp;
1076 for (queue_Scan(&pendingQueue, np, nnp, SalvageQueueNode)) {
1077 if ((np->command.sop.volume == qry->volume) &&
1078 !strncmp(np->command.sop.partName, qry->partName,
1079 sizeof(qry->partName)))
1083 if (queue_IsEnd(&pendingQueue, np))
1088 static struct SalvageQueueNode *
1089 LookupPendingCommandByPid(int pid)
1091 struct SalvageQueueNode * np, * nnp;
1093 for (queue_Scan(&pendingQueue, np, nnp, SalvageQueueNode)) {
1098 if (queue_IsEnd(&pendingQueue, np))
1104 /* raise the priority of a previously scheduled salvage */
1106 UpdateCommandPrio(struct SalvageQueueNode * node)
1108 struct SalvageQueueNode *np, *nnp;
1112 assert(queue_IsOnQueue(node));
1114 prio = node->command.sop.prio;
1115 id = node->partition_id;
1116 if (queue_First(&salvageQueue.part[id], SalvageQueueNode)->command.sop.prio < prio) {
1118 queue_Prepend(&salvageQueue.part[id], node);
1120 for (queue_ScanBackwardsFrom(&salvageQueue.part[id], node, np, nnp, SalvageQueueNode)) {
1121 if (np->command.sop.prio > prio)
1124 if (queue_IsEnd(&salvageQueue.part[id], np)) {
1126 queue_Prepend(&salvageQueue.part[id], node);
1127 } else if (node != np) {
1129 queue_InsertAfter(np, node);
1134 /* this will need to be rearchitected if we ever want more than one thread
1135 * to wait for new salvage nodes */
1136 struct SalvageQueueNode *
1137 SALVSYNC_getWork(void)
1140 struct DiskPartition64 * dp = NULL, * fdp;
1141 static afs_int32 next_part_sched = 0;
1142 struct SalvageQueueNode *node = NULL, *np;
1147 * wait for work to be scheduled
1148 * if there are no disk partitions, just sit in this wait loop forever
1150 while (!salvageQueue.total_len || !DiskPartitionList) {
1151 VOL_CV_WAIT(&salvageQueue.cv);
1155 * short circuit for simple case where only one partition has
1156 * scheduled salvages
1158 if (salvageQueue.last_insert >= 0 && salvageQueue.last_insert <= VOLMAXPARTS &&
1159 (salvageQueue.total_len == salvageQueue.len[salvageQueue.last_insert])) {
1160 node = queue_First(&salvageQueue.part[salvageQueue.last_insert], SalvageQueueNode);
1166 * ok, more than one partition has scheduled salvages.
1167 * now search for partitions with scheduled salvages, but no pending salvages.
1169 dp = VGetPartitionById_r(next_part_sched, 0);
1171 dp = DiskPartitionList;
1177 dp = (dp->next) ? dp->next : DiskPartitionList, i++ ) {
1178 if (!partition_salvaging[dp->index] && salvageQueue.len[dp->index]) {
1179 node = queue_First(&salvageQueue.part[dp->index], SalvageQueueNode);
1186 * all partitions with scheduled salvages have at least one pending.
1187 * now do an exhaustive search for a scheduled salvage.
1193 dp = (dp->next) ? dp->next : DiskPartitionList, i++ ) {
1194 if (salvageQueue.len[dp->index]) {
1195 node = queue_First(&salvageQueue.part[dp->index], SalvageQueueNode);
1200 /* we should never reach this line */
1204 assert(node != NULL);
1206 partition_salvaging[node->partition_id]++;
1207 DeleteFromSalvageQueue(node);
1208 AddToPendingQueue(node);
1211 /* update next_part_sched field */
1213 next_part_sched = dp->next->index;
1214 } else if (DiskPartitionList) {
1215 next_part_sched = DiskPartitionList->index;
1217 next_part_sched = -1;
1227 * update internal scheduler state to reflect completion of a work unit.
1229 * @param[in] node salvage queue node object pointer
1230 * @param[in] result worker process result code
1232 * @post scheduler state is updated.
1237 SALVSYNC_doneWork_r(struct SalvageQueueNode * node, int result)
1242 DeleteFromPendingQueue(node);
1243 partid = node->partition_id;
1244 if (partid >=0 && partid <= VOLMAXPARTS) {
1245 partition_salvaging[partid]--;
1248 node->state = SALVSYNC_STATE_DONE;
1249 } else if (result != SALSRV_EXIT_VOLGROUP_LINK) {
1250 node->state = SALVSYNC_STATE_ERROR;
1253 if (node->type == SALVSYNC_VOLGROUP_PARENT) {
1254 for (idx = 0; idx < VOLMAXTYPES; idx++) {
1255 if (node->volgroup.children[idx]) {
1256 node->volgroup.children[idx]->state = node->state;
1263 * check whether worker child failed.
1265 * @param[in] status status bitfield return by wait()
1267 * @return boolean failure code
1268 * @retval 0 child succeeded
1269 * @retval 1 child failed
1274 ChildFailed(int status)
1276 return (WCOREDUMP(status) ||
1277 WIFSIGNALED(status) ||
1278 ((WEXITSTATUS(status) != 0) &&
1279 (WEXITSTATUS(status) != SALSRV_EXIT_VOLGROUP_LINK)));
1284 * notify salvsync scheduler of node completion, by child pid.
1286 * @param[in] pid pid of worker child
1287 * @param[in] status worker status bitfield from wait()
1289 * @post scheduler state is updated.
1290 * if status code is a failure, fileserver notification was attempted
1292 * @see SALVSYNC_doneWork_r
1295 SALVSYNC_doneWorkByPid(int pid, int status)
1297 struct SalvageQueueNode * node;
1299 afs_uint32 volids[VOLMAXTYPES+1];
1302 memset(volids, 0, sizeof(volids));
1305 node = LookupPendingCommandByPid(pid);
1307 SALVSYNC_doneWork_r(node, status);
1309 if (ChildFailed(status)) {
1310 /* populate volume id list for later processing outside the glock */
1311 volids[0] = node->command.sop.volume;
1312 strcpy(partName, node->command.sop.partName);
1313 if (node->type == SALVSYNC_VOLGROUP_PARENT) {
1314 for (idx = 0; idx < VOLMAXTYPES; idx++) {
1315 if (node->volgroup.children[idx]) {
1316 volids[idx+1] = node->volgroup.children[idx]->command.sop.volume;
1325 * if necessary, notify fileserver of
1326 * failure to salvage volume group
1327 * [we cannot guarantee that the child made the
1328 * appropriate notifications (e.g. SIGSEGV)]
1329 * -- tkeiser 11/28/2007
1331 if (ChildFailed(status)) {
1332 for (idx = 0; idx <= VOLMAXTYPES; idx++) {
1334 FSYNC_VolOp(volids[idx],
1336 FSYNC_VOL_FORCE_ERROR,
1344 #endif /* AFS_DEMAND_ATTACH_FS */