Demand attach warning fixes
[openafs.git] / src / vol / fssync-client.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  *
9  * Portions Copyright (c) 2006,2008 Sine Nomine Associates
10  */
11
12 /*
13         System:         VICE-TWO
14         Module:         fssync.c
15         Institution:    The Information Technology Center, Carnegie-Mellon University
16
17  */
18
19 #ifndef AFS_PTHREAD_ENV
20 #define USUAL_PRIORITY (LWP_MAX_PRIORITY - 2)
21
22 /*
23  * stack size increased from 8K because the HP machine seemed to have trouble
24  * with the smaller stack
25  */
26 #define USUAL_STACK_SIZE        (24 * 1024)
27 #endif /* !AFS_PTHREAD_ENV */
28
29 /*
30    fssync-client.c
31    File server synchronization with external volume utilities.
32    client-side implementation
33  */
34
35 #include <afsconfig.h>
36 #include <afs/param.h>
37
38
39 #include <sys/types.h>
40 #include <stdio.h>
41 #ifdef AFS_NT40_ENV
42 #include <winsock2.h>
43 #include <time.h>
44 #else
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netdb.h>
49 #include <sys/time.h>
50 #endif
51 #include <errno.h>
52 #ifdef AFS_PTHREAD_ENV
53 #include <assert.h>
54 #else /* AFS_PTHREAD_ENV */
55 #include <afs/assert.h>
56 #endif /* AFS_PTHREAD_ENV */
57 #include <signal.h>
58 #include <string.h>
59
60 #include <rx/xdr.h>
61 #include <afs/afsint.h>
62 #include "nfs.h"
63 #include <afs/errors.h>
64 #include "daemon_com.h"
65 #include "fssync.h"
66 #include "lwp.h"
67 #include "lock.h"
68 #include <afs/afssyscalls.h>
69 #include "ihandle.h"
70 #include "vnode.h"
71 #include "volume.h"
72 #include "partition.h"
73
74 #ifdef FSSYNC_BUILD_CLIENT
75
76 /*@printflike@*/ extern void Log(const char *format, ...);
77
78 extern int LogLevel;
79
80 static SYNC_client_state fssync_state = 
81     { -1,                    /* file descriptor */
82       FSSYNC_ENDPOINT_DECL,  /* server endpoint */
83       FSYNC_PROTO_VERSION,   /* protocol version */
84       5,                     /* connect retry limit */
85       120,                   /* hard timeout */
86       "FSSYNC",              /* protocol name string */
87     };
88
89 #ifdef AFS_PTHREAD_ENV
90 static pthread_mutex_t vol_fsync_mutex;
91 static volatile int vol_fsync_mutex_init = 0;
92 #define VFSYNC_LOCK \
93     assert(pthread_mutex_lock(&vol_fsync_mutex) == 0)
94 #define VFSYNC_UNLOCK \
95     assert(pthread_mutex_unlock(&vol_fsync_mutex) == 0)
96 #else
97 #define VFSYNC_LOCK
98 #define VFSYNC_UNLOCK
99 #endif
100
101 int
102 FSYNC_clientInit(void)
103 {
104 #ifdef AFS_PTHREAD_ENV
105     /* this is safe since it gets called with VOL_LOCK held, or before we go multithreaded */
106     if (!vol_fsync_mutex_init) {
107         assert(pthread_mutex_init(&vol_fsync_mutex, NULL) == 0);
108         vol_fsync_mutex_init = 1;
109     }
110 #endif
111     return SYNC_connect(&fssync_state);
112 }
113
114 void
115 FSYNC_clientFinis(void)
116 {
117     SYNC_closeChannel(&fssync_state);
118 }
119
120 int
121 FSYNC_clientChildProcReconnect(void)
122 {
123     return SYNC_reconnect(&fssync_state);
124 }
125
126 /* fsync client interface */
127 afs_int32
128 FSYNC_askfs(SYNC_command * com, SYNC_response * res)
129 {
130     afs_int32 code;
131
132     VFSYNC_LOCK;
133     code = SYNC_ask(&fssync_state, com, res);
134     VFSYNC_UNLOCK;
135
136     switch (code) {
137     case SYNC_OK:
138     case SYNC_FAILED:
139         break;
140     case SYNC_COM_ERROR:
141     case SYNC_BAD_COMMAND:
142         Log("FSYNC_askfs: fatal FSSYNC protocol error; volume management functionality disabled until next fileserver restart\n");
143         break;
144     case SYNC_DENIED:
145         Log("FSYNC_askfs: FSSYNC request denied for reason=%d\n", res->hdr.reason);
146         break;
147     default:
148         Log("FSYNC_askfs: unknown protocol response %d\n", code);
149         break;
150     }
151     return code;
152 }
153
154 afs_int32
155 FSYNC_GenericOp(void * ext_hdr, size_t ext_len,
156               int command, int reason,
157               SYNC_response * res_in)
158 {
159     SYNC_response res_l, *res;
160     SYNC_command com;
161
162     if (res_in) {
163         res = res_in;
164     } else {
165         res = &res_l;
166         res_l.payload.buf = NULL;
167         res_l.payload.len = 0;
168     }
169
170     memset(&com, 0, sizeof(com));
171
172     com.hdr.programType = programType;
173     com.hdr.command = command;
174     com.hdr.reason = reason;
175     com.hdr.command_len = sizeof(com.hdr) + ext_len;
176     com.payload.buf = ext_hdr;
177     com.payload.len = ext_len;
178
179     return FSYNC_askfs(&com, res);
180 }
181
182 afs_int32
183 FSYNC_VolOp(VolumeId volume, char * partition, 
184             int command, int reason,
185             SYNC_response * res)
186 {
187     FSSYNC_VolOp_hdr vcom;
188
189     memset(&vcom, 0, sizeof(vcom));
190
191     vcom.volume = volume;
192     if (partition)
193         strlcpy(vcom.partName, partition, sizeof(vcom.partName));
194
195     return FSYNC_GenericOp(&vcom, sizeof(vcom), command, reason, res);
196 }
197
198 afs_int32
199 FSYNC_StatsOp(FSSYNC_StatsOp_hdr * scom, int command, int reason,
200               SYNC_response * res)
201 {
202     return FSYNC_GenericOp(scom, sizeof(*scom), command, reason, res);
203 }
204
205
206 #endif /* FSSYNC_BUILD_CLIENT */