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,2008 Sine Nomine Associates
15 Institution: The Information Technology Center, Carnegie-Mellon University
19 #ifndef AFS_PTHREAD_ENV
20 #define USUAL_PRIORITY (LWP_MAX_PRIORITY - 2)
23 * stack size increased from 8K because the HP machine seemed to have trouble
24 * with the smaller stack
26 #define USUAL_STACK_SIZE (24 * 1024)
27 #endif /* !AFS_PTHREAD_ENV */
31 File server synchronization with external volume utilities.
32 client-side implementation
35 #include <afsconfig.h>
36 #include <afs/param.h>
40 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
53 #include <afs/afs_assert.h>
58 #include <afs/afsint.h>
60 #include <afs/errors.h>
61 #include "daemon_com.h"
65 #include <afs/afssyscalls.h>
69 #include "partition.h"
72 #ifdef FSSYNC_BUILD_CLIENT
76 static SYNC_client_state fssync_state =
77 { -1, /* file descriptor */
78 FSSYNC_ENDPOINT_DECL, /* server endpoint */
79 FSYNC_PROTO_VERSION, /* protocol version */
80 5, /* connect retry limit */
81 120, /* hard timeout */
82 "FSSYNC", /* protocol name string */
85 #ifdef AFS_PTHREAD_ENV
86 static pthread_mutex_t vol_fsync_mutex;
87 static volatile int vol_fsync_mutex_init = 0;
88 #define VFSYNC_LOCK MUTEX_ENTER(&vol_fsync_mutex)
89 #define VFSYNC_UNLOCK MUTEX_EXIT(&vol_fsync_mutex)
96 FSYNC_clientInit(void)
98 #ifdef AFS_PTHREAD_ENV
99 /* this is safe since it gets called with VOL_LOCK held, or before we go multithreaded */
100 if (!vol_fsync_mutex_init) {
101 MUTEX_INIT(&vol_fsync_mutex, "vol fsync", MUTEX_DEFAULT, 0);
102 vol_fsync_mutex_init = 1;
105 return SYNC_connect(&fssync_state);
109 FSYNC_clientFinis(void)
111 SYNC_closeChannel(&fssync_state);
115 FSYNC_clientChildProcReconnect(void)
117 return SYNC_reconnect(&fssync_state);
120 /* fsync client interface */
122 FSYNC_askfs(SYNC_command * com, SYNC_response * res)
127 code = SYNC_ask(&fssync_state, com, res);
135 case SYNC_BAD_COMMAND:
136 Log("FSYNC_askfs: fatal FSSYNC protocol error; volume management functionality disabled until next fileserver restart\n");
139 Log("FSYNC_askfs: FSSYNC request denied for reason=%d\n", res->hdr.reason);
142 Log("FSYNC_askfs: unknown protocol response %d\n", code);
150 * FSSYNC volume operations client interface.
152 * @param[in] volume volume id
153 * @param[in] partName partition name string
154 * @param[in] com FSSYNC command code
155 * @param[in] reason FSSYNC reason sub-code
156 * @param[out] res response message
158 * @return operation status
159 * @retval SYNC_OK success
162 FSYNC_GenericOp(void * ext_hdr, size_t ext_len,
163 int command, int reason,
164 SYNC_response * res_in)
166 SYNC_response res_l, *res;
173 res_l.payload.buf = NULL;
174 res_l.payload.len = 0;
177 memset(&com, 0, sizeof(com));
179 com.hdr.programType = programType;
180 com.hdr.command = command;
181 com.hdr.reason = reason;
182 com.hdr.command_len = sizeof(com.hdr) + ext_len;
183 com.payload.buf = ext_hdr;
184 com.payload.len = ext_len;
186 return FSYNC_askfs(&com, res);
190 FSYNC_VolOp(VolumeId volume, char * partition,
191 int command, int reason,
194 FSSYNC_VolOp_hdr vcom;
196 memset(&vcom, 0, sizeof(vcom));
198 vcom.volume = volume;
200 strlcpy(vcom.partName, partition, sizeof(vcom.partName));
202 return FSYNC_GenericOp(&vcom, sizeof(vcom), command, reason, res);
206 * verify that the fileserver still thinks we have a volume checked out.
208 * In DAFS, a non-fileserver program accesses a volume by checking it out from
209 * the fileserver (FSYNC_VOL_OFF or FSYNC_VOL_NEEDVOLUME), and then locks the
210 * volume. There is a possibility that the fileserver crashes or restarts for
211 * some reason between volume checkout and locking; if this happens, the
212 * fileserver could attach the volume before we had a chance to lock it. This
213 * function serves to detect if this has happened; it must be called after
214 * volume checkout and locking to make sure the fileserver still thinks we
215 * have the volume. (If it doesn't, we should try to check it out again.)
217 * @param[in] volume volume ID
218 * @param[in] partition partition name string
219 * @param[in] command the command that was used to checkout the volume
220 * @param[in] reason the reason code used to checkout the volume
222 * @return operation status
223 * @retval SYNC_OK the fileserver could not have attached the volume since
224 * it was checked out (either it thinks it is still checked
225 * out, or it doesn't know about the volume)
226 * @retval SYNC_DENIED fileserver may have restarted since checkout; checkout
227 * should be reattempted
228 * @retval SYNC_COM_ERROR internal/fatal error
231 FSYNC_VerifyCheckout(VolumeId volume, char * partition,
232 afs_int32 command, afs_int32 reason)
235 FSSYNC_VolOp_info vop;
239 res.hdr.response_len = sizeof(res.hdr);
240 res.payload.buf = &vop;
241 res.payload.len = sizeof(vop);
243 code = FSYNC_VolOp(volume, partition, FSYNC_VOL_QUERY_VOP, FSYNC_WHATEVER, &res);
244 if (code != SYNC_OK) {
245 if (res.hdr.reason == FSYNC_NO_PENDING_VOL_OP) {
246 Log("FSYNC_VerifyCheckout: fileserver claims no vop for vol %lu "
247 "part %s; fileserver may have restarted since checkout\n",
248 afs_printable_uint32_lu(volume), partition);
252 if (res.hdr.reason == FSYNC_UNKNOWN_VOLID) {
253 /* if the fileserver does not know about this volume, there's no
254 * way it could have attached it, so we're fine */
258 Log("FSYNC_VerifyCheckout: FSYNC_VOL_QUERY_VOP failed for vol %lu "
259 "part %s with code %ld reason %ld\n",
260 afs_printable_uint32_lu(volume), partition,
261 afs_printable_int32_ld(code),
262 afs_printable_int32_ld(res.hdr.reason));
263 return SYNC_COM_ERROR;
268 /* Check if the current vol op is us. Checking pid is probably enough, but
269 * be a little bit paranoid. We could also probably check tid, but I'm not
270 * completely confident of its reliability on all platforms (on pthread
271 * envs, we coerce a pthread_t to an afs_int32, which is not guaranteed
272 * to mean anything significant). */
274 if (vop.com.programType == programType && vop.com.pid == pid &&
275 vop.com.command == command && vop.com.reason == reason) {
277 /* looks like the current pending vol op is the same one as the one
278 * with which we checked it out. success. */
282 Log("FSYNC_VerifyCheckout: vop for vol %lu part %s does not match "
283 "expectations (got pt %ld pid %ld cmd %ld reason %ld, but expected "
284 "pt %ld pid %ld cmd %ld reason %ld); fileserver may have restarted "
285 "since checkout\n", afs_printable_uint32_lu(volume), partition,
286 afs_printable_int32_ld(vop.com.programType),
287 afs_printable_int32_ld(vop.com.pid),
288 afs_printable_int32_ld(vop.com.command),
289 afs_printable_int32_ld(vop.com.reason),
290 afs_printable_int32_ld(programType),
291 afs_printable_int32_ld(pid),
292 afs_printable_int32_ld(command),
293 afs_printable_int32_ld(reason));
299 FSYNC_StatsOp(FSSYNC_StatsOp_hdr * scom, int command, int reason,
302 return FSYNC_GenericOp(scom, sizeof(*scom), command, reason, res);
306 * query the volume group cache.
308 * @param[in] part vice partition path
309 * @param[in] volid volume id
310 * @param[out] qry query response object
311 * @param[out] res SYNC response message
313 * @return operation status
314 * @retval SYNC_OK success
317 FSYNC_VGCQuery(char * part,
319 FSSYNC_VGQry_response_t * qry,
328 res->hdr.response_len = sizeof(res->hdr);
329 res->payload.buf = qry;
330 res->payload.len = sizeof(*qry);
332 return FSYNC_VolOp(volid, part, FSYNC_VG_QUERY, 0, res);
336 * perform an update operation on the VGC.
338 * @param[in] parent rw volume
339 * @param[in] child volume id to add
340 * @param[in] partition name of vice partition on which this VG resides
341 * @param[in] opcode FSSYNC VG cache opcode
342 * @param[in] reason FSSYNC reason code
343 * @param[out] res SYNC response message
345 * @return operation status
346 * @retval SYNC_OK success
351 _FSYNC_VGCUpdate(char * partition,
358 FSSYNC_VGUpdate_command_t vcom;
360 memset(&vcom, 0, sizeof(vcom));
362 vcom.parent = parent;
365 strlcpy(vcom.partName, partition, sizeof(vcom.partName));
367 return FSYNC_GenericOp(&vcom, sizeof(vcom), opcode, reason, res);
371 * Add volume to volume group cache.
373 * @param[in] parent rw volume
374 * @param[in] child volume id to add
375 * @param[in] partition name of vice partition on which this VG resides
376 * @param[in] reason FSSYNC reason code
377 * @param[out] res SYNC response message
379 * @return operation status
380 * @retval SYNC_OK success
383 FSYNC_VGCAdd(char * partition,
389 return _FSYNC_VGCUpdate(partition, parent, child, FSYNC_VG_ADD, reason, res);
393 * Delete volume from volume group cache.
395 * @param[in] parent rw volume
396 * @param[in] child volume id to add
397 * @param[in] partition name of vice partition on which this VG resides
398 * @param[in] reason FSSYNC reason code
399 * @param[out] res SYNC response message
401 * @return operation status
402 * @retval SYNC_OK success
405 FSYNC_VGCDel(char * partition,
411 return _FSYNC_VGCUpdate(partition, parent, child, FSYNC_VG_DEL, reason, res);
415 * perform an asynchronous volume group scan.
417 * @param[in] partition vice partition string
418 * @param[in] reason FSSYNC reason code
420 * @note if partition is NULL, all vice partitions will be scanned.
422 * @return operation status
423 * @retval SYNC_OK success
426 FSYNC_VGCScan(char * partition, int reason)
430 if (partition == NULL) {
431 command = FSYNC_VG_SCAN_ALL;
434 command = FSYNC_VG_SCAN;
437 return FSYNC_VolOp(0, partition, command, reason, NULL);
440 #endif /* FSSYNC_BUILD_CLIENT */