Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / vol / salvsync-client.c
1 /*
2 * Copyright 2006-2008, Sine Nomine Associates 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
10 /*
11 * salvsync-client.c
12 *
13 * OpenAFS demand attach fileserver
14 * Salvage server synchronization with fileserver.
15 */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 #include <roken.h>
21
22 #include <afs/opr.h>
23 #include <opr/lock.h>
24 #include <afs/afsint.h>
25 #include <rx/rx_queue.h>
26 #include "nfs.h"
27 #include <afs/errors.h>
28 #include "salvsync.h"
29 #include "lock.h"
30 #include <afs/afssyscalls.h>
31 #include "ihandle.h"
32 #include "vnode.h"
33 #include "volume.h"
34 #include "partition.h"
35 #include "common.h"
36 #include <rx/rx_queue.h>
37
38 #ifdef AFS_DEMAND_ATTACH_FS
39 /*
40 * SALVSYNC is a feature specific to the demand attach fileserver
41 */
42
43 extern int VInit;
44 extern pthread_mutex_t vol_salvsync_mutex;
45
46 static SYNC_client_state salvsync_client_state =
47 { -1, /* file descriptor */
48 SALVSYNC_ENDPOINT_DECL, /* server endpoint */
49 SALVSYNC_PROTO_VERSION, /* protocol version */
50 5, /* connect retry limit */
51 120, /* hard timeout */
52 "SALVSYNC", /* protocol name string */
53 };
54
55 /*
56 * client-side routines
57 */
58
59 int
60 SALVSYNC_clientInit(void)
61 {
62 return SYNC_connect(&salvsync_client_state);
63 }
64
65 int
66 SALVSYNC_clientFinis(void)
67 {
68 SYNC_closeChannel(&salvsync_client_state);
69 return 1;
70 }
71
72 int
73 SALVSYNC_clientReconnect(void)
74 {
75 return SYNC_reconnect(&salvsync_client_state);
76 }
77
78 afs_int32
79 SALVSYNC_askSalv(SYNC_command * com, SYNC_response * res)
80 {
81 afs_int32 code;
82 SALVSYNC_command_hdr * scom = com->payload.buf;
83
84 scom->hdr_version = SALVSYNC_PROTO_VERSION;
85
86 VSALVSYNC_LOCK;
87 code = SYNC_ask(&salvsync_client_state, com, res);
88 VSALVSYNC_UNLOCK;
89
90 switch (code) {
91 case SYNC_OK:
92 case SYNC_FAILED:
93 break;
94 case SYNC_COM_ERROR:
95 case SYNC_BAD_COMMAND:
96 Log("SALVSYNC_askSalv: internal SALVSYNC protocol error %d\n", code);
97 break;
98 case SYNC_DENIED:
99 Log("SALVSYNC_askSalv: SALVSYNC request denied for reason=%d\n", res->hdr.reason);
100 break;
101 default:
102 Log("SALVSYNC_askSalv: unknown protocol response %d\n", code);
103 break;
104 }
105
106 return code;
107 }
108
109 afs_int32
110 SALVSYNC_SalvageVolume(VolumeId volume, char *partName, int command, int reason,
111 afs_uint32 prio, SYNC_response * res_in)
112 {
113 SYNC_command com;
114 SYNC_response res_l, *res;
115 SALVSYNC_command_hdr scom;
116 SALVSYNC_response_hdr sres;
117
118 memset(&com, 0, sizeof(com));
119 memset(&scom, 0, sizeof(scom));
120
121 if (res_in) {
122 res = res_in;
123 } else {
124 memset(&res_l, 0, sizeof(res_l));
125 memset(&sres, 0, sizeof(sres));
126 res_l.payload.buf = (void *) &sres;
127 res_l.payload.len = sizeof(sres);
128 res = &res_l;
129 }
130
131 com.payload.buf = (void *) &scom;
132 com.payload.len = sizeof(scom);
133 com.hdr.command = command;
134 com.hdr.reason = reason;
135 com.hdr.command_len = sizeof(com.hdr) + sizeof(scom);
136 scom.volume = volume;
137 scom.parent = volume;
138 scom.prio = prio;
139
140 if (partName) {
141 strlcpy(scom.partName, partName, sizeof(scom.partName));
142 } else {
143 scom.partName[0] = '\0';
144 }
145
146 return SALVSYNC_askSalv(&com, res);
147 }
148
149 afs_int32
150 SALVSYNC_LinkVolume(VolumeId parent,
151 VolumeId clone,
152 char * partName,
153 SYNC_response * res_in)
154 {
155 SYNC_command com;
156 SYNC_response res_l, *res;
157 SALVSYNC_command_hdr scom;
158 SALVSYNC_response_hdr sres;
159
160 memset(&com, 0, sizeof(com));
161 memset(&scom, 0, sizeof(scom));
162
163 if (res_in) {
164 res = res_in;
165 } else {
166 memset(&res_l, 0, sizeof(res_l));
167 memset(&sres, 0, sizeof(sres));
168 res_l.payload.buf = (void *) &sres;
169 res_l.payload.len = sizeof(sres);
170 res = &res_l;
171 }
172
173 com.payload.buf = (void *) &scom;
174 com.payload.len = sizeof(scom);
175 com.hdr.command = SALVSYNC_OP_LINK;
176 com.hdr.reason = SALVSYNC_REASON_WHATEVER;
177 com.hdr.command_len = sizeof(com.hdr) + sizeof(scom);
178 scom.volume = clone;
179 scom.parent = parent;
180
181 if (partName) {
182 strlcpy(scom.partName, partName, sizeof(scom.partName));
183 } else {
184 scom.partName[0] = '\0';
185 }
186
187 return SALVSYNC_askSalv(&com, res);
188 }
189
190 #endif /* AFS_DEMAND_ATTACH_FS */