forked from openafs/openafs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvol_sets.c
711 lines (597 loc) · 17.1 KB
/
vol_sets.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
* Copyright 2000, International Business Machines Corporation and others.
* All Rights Reserved.
*
* This software has been released under the terms of the IBM Public
* License. For details, see the LICENSE file in the top-level source
* directory or online at http://www.openafs.org/dl/license10.html
*/
#include <afsconfig.h>
#include <afs/param.h>
#include <afs/stds.h>
#include <sys/types.h>
#ifdef AFS_NT40_ENV
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#include <errno.h>
#include <afs/budb_client.h>
#include <afs/cmd.h>
#include <afs/com_err.h>
#include <afs/bubasics.h>
#include "bc.h"
#include "error_macros.h"
#include "bucoord_internal.h"
#include "bucoord_prototypes.h"
/* code to manage volumesets
* specific to the ubik database implementation
*/
extern struct bc_config *bc_globalConfig;
extern struct udbHandleS udbHandle;
extern char *whoami;
static int ListVolSet(struct bc_volumeSet *aset);
/* ------------------------------------
* command level routines
* ------------------------------------
*/
/* bc_AddVolEntryCmd
* add a volume (or volume expression) to a volume set
* params:
* parm 0 is vol set name.
* parm 1 is server name
* parm 2 is partition name
* parm 3 is volume regexp
*/
int
bc_AddVolEntryCmd(struct cmd_syndesc *as, void *arock)
{
int code;
char *volSetName, *serverName, *partitionName, *volRegExp;
udbClientTextP ctPtr;
struct bc_volumeSet *tset;
volSetName = as->parms[0].items->data;
serverName = as->parms[1].items->data;
partitionName = as->parms[2].items->data;
volRegExp = as->parms[3].items->data;
code = bc_UpdateVolumeSet();
if (code) {
afs_com_err(whoami, code, "; Can't retrieve volume sets");
return (code);
}
ctPtr = &bc_globalConfig->configText[TB_VOLUMESET];
tset = bc_FindVolumeSet(bc_globalConfig, volSetName);
if (!tset) {
afs_com_err(whoami, code, "; Volume entry not added");
ERROR(code);
}
if (!(tset->flags & VSFLAG_TEMPORARY)) {
code = bc_LockText(ctPtr);
if (code)
ERROR(code);
}
code = bc_UpdateVolumeSet();
if (code) {
afs_com_err(whoami, code, "; Can't retrieve volume sets");
return (code);
}
code =
bc_AddVolumeItem(bc_globalConfig, volSetName, serverName,
partitionName, volRegExp);
if (code) {
afs_com_err(whoami, code, "; Volume entry not added");
ERROR(code);
}
if (!(tset->flags & VSFLAG_TEMPORARY)) {
code = bc_SaveVolumeSet();
if (code) {
afs_com_err(whoami, code, "Cannot save volume set file");
afs_com_err(whoami, 0,
"Changes are temporary - for this session only");
}
}
error_exit:
if (ctPtr->lockHandle)
bc_UnlockText(ctPtr);
return (code);
}
/* bc_AddVolSetCmd
* create a new volume set, writing out the new volumeset
* file in a safe manner
* params:
* name of new volume set
*/
int
bc_AddVolSetCmd(struct cmd_syndesc *as, void *arock)
{
/* parm 0 is vol set name */
int code;
struct cmd_item *ti;
udbClientTextP ctPtr;
afs_int32 flags;
/* lock schedules and check validity */
ctPtr = &bc_globalConfig->configText[TB_VOLUMESET];
flags = (as->parms[1].items ? VSFLAG_TEMPORARY : 0);
/* Don't lock if adding a temporary volumeset */
if (!(flags & VSFLAG_TEMPORARY)) {
code = bc_LockText(ctPtr);
if (code)
ERROR(code);
}
code = bc_UpdateVolumeSet();
if (code) {
afs_com_err(whoami, code, "; Can't retrieve volume sets");
return (code);
}
/* validate size of volumeset name */
code =
bc_CreateVolumeSet(bc_globalConfig, (ti = as->parms[0].items)->data,
flags);
if (code) {
if (code == -1)
afs_com_err(whoami, 0, "Volume set '%s' already exists", ti->data);
else
afs_com_err(whoami, 0, "Unknown problem");
} else if (!(flags & VSFLAG_TEMPORARY)) {
code = bc_SaveVolumeSet();
if (code) {
afs_com_err(whoami, code, "Cannot save new volume set file");
afs_com_err(whoami, 0,
"Changes are temporary - for this session only");
}
}
error_exit:
if (ctPtr->lockHandle != 0)
bc_UnlockText(ctPtr);
return (code);
}
/* bc_DeleteVolEntryCmd
* delete a volume specification from a volume set
* params:
* parm 0 is vol set name
* parm 1 is entry # (integer, 1 based)
*/
int
bc_DeleteVolEntryCmd(struct cmd_syndesc *as, void *arock)
{
int code;
afs_int32 entry;
char *vsname;
udbClientTextP ctPtr;
struct bc_volumeSet *tset;
vsname = as->parms[0].items->data;
code = bc_UpdateVolumeSet();
if (code) {
afs_com_err(whoami, code, "; Can't retrieve volume sets");
return (code);
}
/* lock schedules and check validity */
ctPtr = &bc_globalConfig->configText[TB_VOLUMESET];
tset = bc_FindVolumeSet(bc_globalConfig, vsname);
if (!tset) {
afs_com_err(whoami, 0, "No such volume set as '%s'", vsname);
ERROR(code);
}
if (!(tset->flags & VSFLAG_TEMPORARY)) {
code = bc_LockText(ctPtr);
if (code)
ERROR(code);
}
code = bc_UpdateVolumeSet();
if (code) {
afs_com_err(whoami, code, "; Can't retrieve volume sets");
return (code);
}
entry = bc_SafeATOI(as->parms[1].items->data);
if (entry < 0) {
afs_com_err(whoami, 0, "Can't parse entry number '%s' as decimal integer",
as->parms[1].items->data);
ERROR(BC_BADARG);
}
code = bc_DeleteVolumeItem(bc_globalConfig, vsname, entry);
if (code) {
if (code == -1) {
afs_com_err(whoami, 0, "No such volume set as '%s'", vsname);
} else if (code == -2) {
afs_com_err(whoami, 0,
"There aren't %d volume items for this volume set",
entry);
afs_com_err(whoami, 0,
"Use the 'listvolsets' command to examine the volume set");
}
ERROR(code);
}
if (!(tset->flags & VSFLAG_TEMPORARY)) {
code = bc_SaveVolumeSet();
if (code == 0) {
printf("backup: deleted volume entry %d from volume set %s\n",
entry, vsname);
} else {
afs_com_err(whoami, code, "Cannot save volume set file");
afs_com_err(whoami, 0,
"Deletion is temporary - for this session only");
}
}
error_exit:
if (ctPtr->lockHandle != 0)
bc_UnlockText(ctPtr);
return (code);
}
/* bc_DeleteVolSetCmd
* delete a volume set, writing out a new configuration file when
* completed
* params:
* name of volumeset to delete
*/
int
bc_DeleteVolSetCmd(struct cmd_syndesc *as, void *arock)
{
/* parm 0 is vol set name */
int code;
struct cmd_item *ti;
udbClientTextP ctPtr;
afs_int32 c;
afs_int32 flags, tosave = 0;
/* lock schedules and check validity */
ctPtr = &bc_globalConfig->configText[TB_VOLUMESET];
code = bc_LockText(ctPtr);
if (code)
ERROR(code);
code = bc_UpdateVolumeSet();
if (code) {
afs_com_err(whoami, code, "; Can't retrieve volume sets");
return (code);
}
for (ti = as->parms[0].items; ti; ti = ti->next) {
code = bc_DeleteVolumeSet(bc_globalConfig, ti->data, &flags);
if (code) {
if (code == -1)
afs_com_err(whoami, 0, "Can't find volume set '%s'", ti->data);
else
afs_com_err(whoami, code,
"; Unknown problem deleting volume set '%s'",
ti->data);
} else {
if (!(flags & VSFLAG_TEMPORARY))
tosave = 1;
printf("backup: deleted volume set '%s'\n", ti->data);
}
}
/* now write out the file */
if (tosave) {
c = bc_SaveVolumeSet();
if (c) {
if (!code)
code = c;
afs_com_err(whoami, c, "Cannot save updated volume set file");
afs_com_err(whoami, 0, "Deletion effective for this session only");
}
}
error_exit:
if (ctPtr->lockHandle)
bc_UnlockText(ctPtr);
return (code);
}
static int
ListVolSet(struct bc_volumeSet *aset)
{
struct bc_volumeEntry *tentry;
int i;
printf("Volume set %s", aset->name);
if (aset->flags & VSFLAG_TEMPORARY)
printf(" (temporary)");
printf(":\n");
i = 1;
for (tentry = aset->ventries; tentry; tentry = tentry->next, i++) {
printf(" Entry %3d: server %s, partition %s, volumes: %s\n", i,
tentry->serverName, tentry->partname, tentry->name);
}
return 0;
}
/* bc_ListVolSetCmd
* list out all the information (?) about a volumeset or about all
* volumesets
* entry:
* optional parameter specifies a volumeset name
*/
int
bc_ListVolSetCmd(struct cmd_syndesc *as, void *arock)
{
/* parm 0 is optional volume set to display */
struct bc_volumeSet *tset;
struct cmd_item *ti;
int code = 0;
code = bc_UpdateVolumeSet();
if (code) {
afs_com_err(whoami, code, "; Can't retrieve volume sets");
return (code);
}
/* figure out volume set to list */
if ((ti = as->parms[0].items)) {
/* for each volume set in the command item list */
for (; ti; ti = ti->next) {
tset = bc_FindVolumeSet(bc_globalConfig, ti->data);
if (tset) {
ListVolSet(tset);
printf("\n");
} else {
afs_com_err(whoami, 0, "Can't find volume set '%s'", ti->data);
code = 1;
}
}
} else {
/* no command parameters specified, show entire list */
for (tset = bc_globalConfig->vset; tset; tset = tset->next) {
ListVolSet(tset);
printf("\n");
}
}
return code;
}
/* ------------------------------------
* support routines
* ------------------------------------
*/
int
bc_ClearVolumeSets(void)
{
struct bc_volumeSet *vsPtr, *vsNextPtr, **vsPrev;
extern struct bc_config *bc_globalConfig;
vsPrev = &(bc_globalConfig->vset);
for (vsPtr = bc_globalConfig->vset; vsPtr; vsPtr = vsNextPtr) {
vsNextPtr = vsPtr->next;
if (vsPtr->flags & VSFLAG_TEMPORARY) { /* Skip temporary volumeSet */
vsPrev = &(vsPtr->next);
continue;
}
*vsPrev = vsPtr->next; /* Remove volumeSet from the chain */
FreeVolumeSet(vsPtr);
}
return (0);
}
/* bc_ParseVolumeSet
* Open up the volume set configuration file as specified in our argument,
* then parse the file to set up our internal representation.
* exit:
* 0 on successful processing,
* -1 otherwise.
*/
int
bc_ParseVolumeSet(void)
{
char tbuffer[1024]; /*Buffer for reading config file */
char vsname[256]; /*Volume set name */
char serverName[256]; /*Server name */
char partName[256]; /*Partition name */
struct bc_volumeEntry *tve; /*Ptr to generated volume spec struct */
struct bc_volumeSet *tvs = NULL; /*Ptr to volume set struct */
struct bc_volumeEntry **ppve, *pve;
struct bc_volumeSet **ppvs, *pvs;
afs_int32 code; /*Generalized return code */
char *tp; /*Result of fgets(), malloc() */
int readHeader; /*Is next thing to read a volume set hdr? */
udbClientTextP ctPtr;
FILE *stream;
extern struct bc_config *bc_globalConfig;
ctPtr = &bc_globalConfig->configText[TB_VOLUMESET];
stream = ctPtr->textStream;
/*
* Open up the volume set configuration file, fail if it can't be done.
*/
if (ctPtr->textSize == 0) /* empty is ok */
return (0);
/* stream checks and initialization */
if (stream == NULL)
return (BC_INTERNALERROR);
rewind(stream);
readHeader = 1;
while (1) {
/* Read in and process the next line of the volume set description
* file.
*/
tp = fgets(tbuffer, sizeof(tbuffer), stream);
if (!tp)
break;
if (readHeader) { /*r */
/*
* Scan a header entry.
*/
readHeader = 0;
code = sscanf(tbuffer, "%s %s", serverName, vsname);
if ((code != 2)
|| (strcmp(serverName, "volumeset") != 0)
) {
afs_com_err(whoami, 0, "Bad volume header line: '%s'", tbuffer);
return (-1);
}
/* Create and fill in the volume set descriptor structure from
* the info just read placing it at the head of its queue in the
* global configuration structure.
*/
tvs = (struct bc_volumeSet *)malloc(sizeof(struct bc_volumeSet));
memset(tvs, 0, sizeof(*tvs));
tvs->name = (char *)malloc(strlen(vsname) + 1);
strcpy(tvs->name, vsname);
/* append to the end */
for (ppvs = &bc_globalConfig->vset, pvs = *ppvs; pvs;
ppvs = &pvs->next, pvs = *ppvs);
*ppvs = tvs;
tvs->next = (struct bc_volumeSet *)0;
} /*r */
else { /*e */
/* Scan a volume name entry, which contains the server name,
* partition pattern, and volume pattern.
*/
code = sscanf(tbuffer, "%s %s %s", serverName, partName, vsname);
if (code == 1 && strcmp(serverName, "end") == 0) {
/* This was really a line delimiting the current volume set.
* Next time around, we should try to read a header.
*/
readHeader = 1;
continue;
}
/* The line just read in is a volume spec. Create a new volume
* spec record, then get the rest of the information regarding
* the host, and stuff everything into place.
*/
tve = (struct bc_volumeEntry *)
malloc(sizeof(struct bc_volumeEntry));
if (!tve) {
afs_com_err(whoami, 0,
"Can't malloc() a new volume spec record!");
return (-1);
}
memset(tve, 0, sizeof(*tve));
if (bc_ParseHost(serverName, &(tve->server)))
afs_com_err(whoami, 0, "Can't get required info on host '%s'",
serverName);
/* The above code has filled in the server sockaddr, now fill in
* the rest of the fields.
*/
tve->serverName = (char *)malloc(strlen(serverName) + 1);
if (!tve->serverName) {
afs_com_err(whoami, 0,
"Can't malloc() a new volume spec server name field!");
return (-1);
}
strcpy(tve->serverName, serverName);
tve->partname = (char *)malloc(strlen(partName) + 1);
if (!tve->partname) {
afs_com_err(whoami, 0,
"Can't malloc() a new volume spec partition pattern field!");
return (-1);
}
strcpy(tve->partname, partName);
code = bc_GetPartitionID(partName, &tve->partition);
if (code) {
afs_com_err(whoami, 0, "Can't parse partition '%s'", partName);
return -1;
}
tp = (char *)malloc(strlen(vsname) + 1);
if (!tp) {
afs_com_err(whoami, 0,
"Can't malloc() a new volume spec volume pattern field!");
return (-1);
}
strcpy(tp, vsname);
tve->name = tp;
/* Now, thread it onto the list of other volume spec entries for
* the current volume set.
*/
for (ppve = &tvs->ventries, pve = *ppve; pve;
ppve = &pve->next, pve = *ppve);
*ppve = tve;
tve->next = (struct bc_volumeEntry *)0;
}
} /*forever loop */
/* If we hit an EOF in the middle of a volume set record, we bitch and
* moan.
*/
if (!readHeader)
return (-1);
/*
* Well, we did it. Return successfully.
*/
return (0);
} /*bc_ParseVolumeSet */
/* bc_SaveVolumeSet
* save the current volume set information to disk
*/
int
bc_SaveVolumeSet(void)
{
afs_int32 code = 0;
struct bc_volumeSet *tset;
struct bc_volumeEntry *tentry;
udbClientTextP ctPtr;
FILE *stream;
extern struct bc_config *bc_globalConfig;
ctPtr = &bc_globalConfig->configText[TB_VOLUMESET];
stream = ctPtr->textStream;
/* must be locked */
if (ctPtr->lockHandle == 0)
return (BC_INTERNALERROR);
/* truncate the file */
code = ftruncate(fileno(stream), 0);
if (code)
ERROR(errno);
rewind(stream);
/* now write the volumeset information */
for (tset = bc_globalConfig->vset; tset != 0; tset = tset->next) {
if (tset->flags & VSFLAG_TEMPORARY)
continue; /* skip temporary entries */
fprintf(stream, "volumeset %s\n", tset->name);
for (tentry = tset->ventries; tentry; tentry = tentry->next) {
fprintf(stream, "%s %s %s\n", tentry->serverName,
tentry->partname, tentry->name);
}
fprintf(stream, "end\n");
}
if (ferror(stream))
return (BC_INTERNALERROR);
/* send to server */
code = bcdb_SaveTextFile(ctPtr);
if (code)
ERROR(code);
/* do this on bcdb_SaveTextFile */
/* increment local version number */
ctPtr->textVersion++;
/* update locally stored file size */
ctPtr->textSize = filesize(ctPtr->textStream);
error_exit:
return (code);
}
afs_int32
bc_UpdateVolumeSet(void)
{
struct udbHandleS *uhptr = &udbHandle;
udbClientTextP ctPtr;
afs_int32 code;
int lock = 0;
/* lock schedules and check validity */
ctPtr = &bc_globalConfig->configText[TB_VOLUMESET];
/* Don't need a lock to check the version */
code = bc_CheckTextVersion(ctPtr);
if (code != BC_VERSIONMISMATCH) {
ERROR(code); /* version matches or some other error */
}
/* Must update the volume sets */
/* If no lock alredy taken, then lock it */
if (ctPtr->lockHandle == 0) {
code = bc_LockText(ctPtr);
if (code)
ERROR(code);
lock = 1;
}
if (ctPtr->textVersion != -1) {
printf("backup: obsolete volumesets - updating\n");
bc_ClearVolumeSets();
}
/* open a temp file to store the config text received from buserver *
* The open file stream is stored in ctPtr->textStream */
code =
bc_openTextFile(ctPtr,
&bc_globalConfig->tmpTextFileNames[TB_VOLUMESET][0]);
if (code)
ERROR(code);
/* now get a fresh set of information from the database */
code = bcdb_GetTextFile(ctPtr);
if (code)
ERROR(code);
/* fetch the version number */
code =
ubik_BUDB_GetTextVersion(uhptr->uh_client, 0, ctPtr->textType,
&ctPtr->textVersion);
if (code)
ERROR(code);
/* parse the file */
code = bc_ParseVolumeSet();
if (code)
ERROR(code);
error_exit:
if (lock && ctPtr->lockHandle)
bc_UnlockText(ctPtr);
return (code);
}