reindent-20030715
[openafs.git] / src / volser / volprocs.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
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <errno.h>
19 #ifdef AFS_NT40_ENV
20 #include <fcntl.h>
21 #include <winsock2.h>
22 #else
23 #include <sys/file.h>
24 #include <netinet/in.h>
25 #include <unistd.h>
26 #endif
27
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #else
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34 #endif
35
36 #include <dirent.h>
37 #include <sys/stat.h>
38 #include <rx/xdr.h>
39 #include <rx/rx.h>
40 #include <rx/rxkad.h>
41 #include <afs/afsint.h>
42 #include <signal.h>
43 #include <afs/assert.h>
44 #include <afs/prs_fs.h>
45 #include <afs/nfs.h>
46 #include <lwp.h>
47 #include <lock.h>
48 #include <afs/auth.h>
49 #include <afs/cellconfig.h>
50 #include <afs/keys.h>
51 #include <ubik.h>
52 #include <afs/ihandle.h>
53 #ifdef AFS_NT40_ENV
54 #include <afs/ntops.h>
55 #endif
56 #include <afs/vnode.h>
57 #include <afs/volume.h>
58 #include <afs/partition.h>
59 #include "vol.h"
60 #include <afs/fssync.h>
61 #include <afs/acl.h>
62 #include "afs/audit.h"
63 #include <afs/dir.h>
64
65 #include "volser.h"
66 #include "volint.h"
67
68 #include <volser_prototypes.h>
69
70 extern int DoLogging;
71 extern struct volser_trans *FindTrans(), *NewTrans(), *TransList();
72 extern struct afsconf_dir *tdir;
73 extern char *volutil_PartitionName();
74
75 extern void LogError(afs_int32 errcode);
76
77 /* Forward declarations */
78 static int GetPartName(afs_int32 partid, char *pname);
79
80 #define OneDay (24*60*60)
81
82 #ifdef AFS_NT40_ENV
83 #define ENOTCONN 134
84 #endif
85
86 afs_int32 localTid = 1;
87 afs_int32 VolPartitionInfo(), VolNukeVolume(), VolCreateVolume(),
88 VolDeleteVolume(), VolClone();
89 afs_int32 VolReClone(), VolTransCreate(), VolGetNthVolume(), VolGetFlags(),
90 VolForward(), VolDump();
91 afs_int32 VolForwardMultiple();
92 afs_int32 VolRestore(), VolEndTrans(), VolSetForwarding(), VolGetStatus(),
93 VolSetInfo(), VolGetName();
94 afs_int32 VolSignalRestore(), VolListPartitions(), VolListOneVolume(),
95 VolXListOneVolume(), VolXListVolumes();
96 afs_int32 VolListVolumes(), XVolListPartitions(), VolMonitor(),
97 VolSetIdsTypes(), VolSetDate(), VolSetFlags();
98
99 /* this call unlocks all of the partition locks we've set */
100 VPFullUnlock()
101 {
102     register struct DiskPartition *tp;
103     for (tp = DiskPartitionList; tp; tp = tp->next) {
104         if (tp->lock_fd != -1) {
105             close(tp->lock_fd); /* releases flock held on this partition */
106             tp->lock_fd = -1;
107         }
108     }
109     return 0;
110 }
111
112 /* get partition id from a name */
113 afs_int32
114 PartitionID(aname)
115      char *aname;
116 {
117     register char tc;
118     register int code = 0;
119     char ascii[3];
120
121     tc = *aname;
122     if (tc == 0)
123         return -1;              /* unknown */
124
125     /* otherwise check for vicepa or /vicepa, or just plain "a" */
126     ascii[2] = 0;
127     if (!strncmp(aname, "/vicep", 6)) {
128         strncpy(ascii, aname + 6, 2);
129     } else
130         return -1;              /* bad partition name */
131     /* now partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab, .../vicepzz, and are numbered
132      * from 0.  Do the appropriate conversion */
133     if (ascii[1] == 0) {
134         /* one char name, 0..25 */
135         if (ascii[0] < 'a' || ascii[0] > 'z')
136             return -1;          /* wrongo */
137         return ascii[0] - 'a';
138     } else {
139         /* two char name, 26 .. <whatever> */
140         if (ascii[0] < 'a' || ascii[0] > 'z')
141             return -1;          /* wrongo */
142         if (ascii[1] < 'a' || ascii[1] > 'z')
143             return -1;          /* just as bad */
144         code = (ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26;
145         if (code > VOLMAXPARTS)
146             return -1;
147         return code;
148     }
149 }
150
151 static int
152 ConvertVolume(avol, aname, asize)
153      afs_int32 avol;
154      char *aname;
155      afs_int32 asize;
156 {
157     if (asize < 18)
158         return -1;
159     /* It's better using the Generic VFORMAT since otherwise we have to make changes to too many places... The 14 char limitation in names hits us again in AIX; print in field of 9 digits (still 10 for the rest), right justified with 0 padding */
160     (void)afs_snprintf(aname, asize, VFORMAT, (unsigned long)avol);
161     return 0;
162 }
163
164 static int
165 ConvertPartition(apartno, aname, asize)
166      int apartno;
167      char *aname;
168      int asize;
169 {
170     if (asize < 10)
171         return E2BIG;
172     if (apartno < 0)
173         return EINVAL;
174     strcpy(aname, "/vicep");
175     if (apartno < 26) {
176         aname[6] = 'a' + apartno;
177         aname[7] = 0;
178     } else {
179         apartno -= 26;
180         aname[6] = 'a' + (apartno / 26);
181         aname[7] = 'a' + (apartno % 26);
182         aname[8] = 0;
183     }
184     return 0;
185 }
186
187 /* the only attach function that takes a partition is "...ByName", so we use it */
188 struct Volume *
189 XAttachVolume(error, avolid, apartid, amode)
190      afs_int32 *error;
191      afs_int32 avolid;
192      afs_int32 apartid;
193      int amode;
194 {
195     char pbuf[30], vbuf[20];
196     register struct Volume *tv;
197
198     if (ConvertPartition(apartid, pbuf, sizeof(pbuf))) {
199         *error = EINVAL;
200         return NULL;
201     }
202     if (ConvertVolume(avolid, vbuf, sizeof(vbuf))) {
203         *error = EINVAL;
204         return NULL;
205     }
206     tv = VAttachVolumeByName(error, pbuf, vbuf, amode);
207     return tv;
208 }
209
210 /* Adapted from the file server; create a root directory for this volume */
211 static
212 ViceCreateRoot(vp)
213      Volume *vp;
214 {
215     DirHandle dir;
216     struct acl_accessList *ACL;
217     ViceFid did;
218     Inode inodeNumber, nearInode;
219     char buf[SIZEOF_LARGEDISKVNODE];
220     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
221     struct VnodeClassInfo *vcp = &VnodeClassInfo[vLarge];
222     IHandle_t *h;
223     FdHandle_t *fdP;
224     int code;
225     afs_fsize_t length;
226
227     memset(vnode, 0, SIZEOF_LARGEDISKVNODE);
228
229     V_pref(vp, nearInode);
230     inodeNumber =
231         IH_CREATE(V_linkHandle(vp), V_device(vp),
232                   VPartitionPath(V_partition(vp)), nearInode, V_parentId(vp),
233                   1, 1, 0);
234     assert(VALID_INO(inodeNumber));
235
236     SetSalvageDirHandle(&dir, V_parentId(vp), vp->device, inodeNumber);
237     did.Volume = V_id(vp);
238     did.Vnode = (VnodeId) 1;
239     did.Unique = 1;
240
241     assert(!(MakeDir(&dir, &did, &did)));
242     DFlush();                   /* flush all modified dir buffers out */
243     DZap(&dir);                 /* Remove all buffers for this dir */
244     length = Length(&dir);      /* Remember size of this directory */
245
246     FidZap(&dir);               /* Done with the dir handle obtained via SetSalvageDirHandle() */
247
248     /* build a single entry ACL that gives all rights to system:administrators */
249     /* this section of code assumes that access list format is not going to
250      * change
251      */
252     ACL = VVnodeDiskACL(vnode);
253     ACL->size = sizeof(struct acl_accessList);
254     ACL->version = ACL_ACLVERSION;
255     ACL->total = 1;
256     ACL->positive = 1;
257     ACL->negative = 0;
258     ACL->entries[0].id = -204;  /* this assumes System:administrators is group -204 */
259     ACL->entries[0].rights =
260         PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT | PRSFS_LOOKUP | PRSFS_DELETE
261         | PRSFS_LOCK | PRSFS_ADMINISTER;
262
263     vnode->type = vDirectory;
264     vnode->cloned = 0;
265     vnode->modeBits = 0777;
266     vnode->linkCount = 2;
267     VNDISK_SET_LEN(vnode, length);
268     vnode->uniquifier = 1;
269     V_uniquifier(vp) = vnode->uniquifier + 1;
270     vnode->dataVersion = 1;
271     VNDISK_SET_INO(vnode, inodeNumber);
272     vnode->unixModifyTime = vnode->serverModifyTime = V_creationDate(vp);
273     vnode->author = 0;
274     vnode->owner = 0;
275     vnode->parent = 0;
276     vnode->vnodeMagic = vcp->magic;
277
278     IH_INIT(h, vp->device, V_parentId(vp),
279             vp->vnodeIndex[vLarge].handle->ih_ino);
280     fdP = IH_OPEN(h);
281     assert(fdP != NULL);
282     code = FDH_SEEK(fdP, vnodeIndexOffset(vcp, 1), SEEK_SET);
283     assert(code >= 0);
284     code = FDH_WRITE(fdP, vnode, SIZEOF_LARGEDISKVNODE);
285     assert(code == SIZEOF_LARGEDISKVNODE);
286     FDH_REALLYCLOSE(fdP);
287     IH_RELEASE(h);
288     VNDISK_GET_LEN(length, vnode);
289     V_diskused(vp) = nBlocks(length);
290
291     return 1;
292 }
293
294 afs_int32
295 SAFSVolPartitionInfo(acid, pname, partition)
296      struct rx_call *acid;
297      char *pname;
298      struct diskPartition *partition;
299 {
300     afs_int32 code;
301
302     code = VolPartitionInfo(acid, pname, partition);
303     osi_auditU(acid, VS_ParInfEvent, code, AUD_STR, pname, AUD_END);
304     return code;
305 }
306
307 afs_int32
308 VolPartitionInfo(acid, pname, partition)
309      struct rx_call *acid;
310      char *pname;
311      struct diskPartition *partition;
312 {
313     register struct DiskPartition *dp;
314
315 /*
316     if (!afsconf_SuperUser(tdir, acid, caller)) return VOLSERBAD_ACCESS;
317 */
318     VResetDiskUsage();
319     dp = VGetPartition(pname, 0);
320     if (dp) {
321         strncpy(partition->name, dp->name, 32);
322         strncpy(partition->devName, dp->devName, 32);
323         partition->lock_fd = dp->lock_fd;
324         partition->free = dp->free;
325         partition->minFree = dp->totalUsable;
326         return 0;
327     } else
328         return VOLSERILLEGAL_PARTITION;
329 }
330
331 /* obliterate a volume completely, and slowly. */
332 afs_int32
333 SAFSVolNukeVolume(acid, apartID, avolID)
334      struct rx_call *acid;
335      afs_int32 apartID, avolID;
336 {
337     afs_int32 code;
338
339     code = VolNukeVolume(acid, apartID, avolID);
340     osi_auditU(acid, VS_NukVolEvent, code, AUD_LONG, avolID, AUD_END);
341     return code;
342 }
343
344 afs_int32
345 VolNukeVolume(acid, apartID, avolID)
346      struct rx_call *acid;
347      afs_int32 apartID, avolID;
348 {
349     register char *tp;
350     char partName[50];
351     afs_int32 error;
352     register afs_int32 code;
353     struct Volume *tvp;
354     char caller[MAXKTCNAMELEN];
355
356     /* check for access */
357     if (!afsconf_SuperUser(tdir, acid, caller))
358         return VOLSERBAD_ACCESS;
359     if (DoLogging)
360         Log("%s is executing VolNukeVolume %u\n", caller, avolID);
361
362     tp = volutil_PartitionName(apartID);
363     if (!tp)
364         return VOLSERNOVOL;
365     strcpy(partName, tp);       /* remember it for later */
366     /* we first try to attach the volume in update mode, so that the file
367      * server doesn't try to use it (and abort) while (or after) we delete it.
368      * If we don't get the volume, that's fine, too.  We just won't put it back.
369      */
370     tvp = XAttachVolume(&error, avolID, apartID, V_VOLUPD);
371     code = nuke(partName, avolID);
372     if (tvp)
373         VDetachVolume(&error, tvp);
374     return code;
375 }
376
377 /* create a new volume, with name aname, on the specified partition (1..n)
378  * and of type atype (readwriteVolume, readonlyVolume, backupVolume).
379  * As input, if *avolid is 0, we allocate a new volume id, otherwise we use *avolid
380  * for the volume id (useful for things like volume restore).
381  * Return the new volume id in *avolid.
382  */
383 afs_int32
384 SAFSVolCreateVolume(acid, apart, aname, atype, aparent, avolid, atrans)
385      struct rx_call *acid;
386      afs_int32 apart;
387      afs_int32 atype;
388      char *aname;
389      afs_int32 aparent;
390      afs_int32 *atrans;
391      afs_int32 *avolid;
392 {
393     afs_int32 code;
394
395     code =
396         VolCreateVolume(acid, apart, aname, atype, aparent, avolid, atrans);
397     osi_auditU(acid, VS_CrVolEvent, code, AUD_LONG, *atrans, AUD_LONG,
398                *avolid, AUD_STR, aname, AUD_LONG, atype, AUD_LONG, aparent,
399                AUD_END);
400     return code;
401 }
402
403 afs_int32
404 VolCreateVolume(acid, apart, aname, atype, aparent, avolid, atrans)
405      struct rx_call *acid;
406      afs_int32 apart;
407      afs_int32 atype;
408      char *aname;
409      afs_int32 aparent;
410      afs_int32 *atrans;
411      afs_int32 *avolid;
412 {
413     afs_int32 error;
414     register Volume *vp;
415     afs_int32 junk;             /* discardable error code */
416     register afs_int32 volumeID, doCreateRoot = 1;
417     register struct volser_trans *tt;
418     char ppath[30];
419     char caller[MAXKTCNAMELEN];
420
421     if (strlen(aname) > 31)
422         return VOLSERBADNAME;
423     if (!afsconf_SuperUser(tdir, acid, caller))
424         return VOLSERBAD_ACCESS;
425     if (DoLogging)
426         Log("%s is executing CreateVolume '%s'\n", caller, aname);
427     if ((error = ConvertPartition(apart, ppath, sizeof(ppath))))
428         return error;           /*a standard unix error */
429     if (atype != readwriteVolume && atype != readonlyVolume
430         && atype != backupVolume)
431         return EINVAL;
432     if ((volumeID = *avolid) == 0) {
433
434         Log("1 Volser: CreateVolume: missing volume number; %s volume not created\n", aname);
435         return E2BIG;
436
437     }
438     if ((aparent == volumeID) && (atype == readwriteVolume)) {
439         doCreateRoot = 0;
440     }
441     if (aparent == 0)
442         aparent = volumeID;
443     tt = NewTrans(volumeID, apart);
444     if (!tt) {
445         Log("1 createvolume: failed to create trans\n");
446         return VOLSERVOLBUSY;   /* volume already busy! */
447     }
448     vp = VCreateVolume(&error, ppath, volumeID, aparent);
449     if (error) {
450         Log("1 Volser: CreateVolume: Unable to create the volume; aborted, error code %u\n", error);
451         LogError(error);
452         DeleteTrans(tt);
453         return EIO;
454     }
455     V_uniquifier(vp) = 1;
456     V_creationDate(vp) = V_copyDate(vp);
457     V_inService(vp) = V_blessed(vp) = 1;
458     V_type(vp) = atype;
459     AssignVolumeName(&V_disk(vp), aname, 0);
460     if (doCreateRoot)
461         ViceCreateRoot(vp);
462     V_destroyMe(vp) = DESTROY_ME;
463     V_inService(vp) = 0;
464     V_maxquota(vp) = 5000;      /* set a quota of 5000 at init time */
465     VUpdateVolume(&error, vp);
466     if (error) {
467         Log("1 Volser: create UpdateVolume failed, code %d\n", error);
468         LogError(error);
469         DeleteTrans(tt);
470         VDetachVolume(&junk, vp);       /* rather return the real error code */
471         return error;
472     }
473     tt->volume = vp;
474     *atrans = tt->tid;
475     strcpy(tt->lastProcName, "CreateVolume");
476     tt->rxCallPtr = acid;
477     Log("1 Volser: CreateVolume: volume %u (%s) created\n", volumeID, aname);
478     tt->rxCallPtr = (struct rx_call *)0;
479     if (TRELE(tt))
480         return VOLSERTRELE_ERROR;
481     return 0;
482 }
483
484 /* delete the volume associated with this transaction */
485 afs_int32
486 SAFSVolDeleteVolume(acid, atrans)
487      afs_int32 atrans;
488      struct rx_call *acid;
489 {
490     afs_int32 code;
491
492     code = VolDeleteVolume(acid, atrans);
493     osi_auditU(acid, VS_DelVolEvent, code, AUD_LONG, atrans, AUD_END);
494     return code;
495 }
496
497 afs_int32
498 VolDeleteVolume(acid, atrans)
499      afs_int32 atrans;
500      struct rx_call *acid;
501 {
502     register struct volser_trans *tt;
503     afs_int32 error;
504     char caller[MAXKTCNAMELEN];
505
506     if (!afsconf_SuperUser(tdir, acid, caller))
507         return VOLSERBAD_ACCESS;
508     tt = FindTrans(atrans);
509     if (!tt)
510         return ENOENT;
511     if (tt->vflags & VTDeleted) {
512         Log("1 Volser: Delete: volume %u already deleted \n", tt->volid);
513         TRELE(tt);
514         return ENOENT;
515     }
516     if (DoLogging)
517         Log("%s is executing Delete Volume %u\n", caller, tt->volid);
518     strcpy(tt->lastProcName, "DeleteVolume");
519     tt->rxCallPtr = acid;
520     VPurgeVolume(&error, tt->volume);   /* don't check error code, it is not set! */
521     tt->vflags |= VTDeleted;    /* so we know not to do anything else to it */
522     tt->rxCallPtr = (struct rx_call *)0;
523     if (TRELE(tt))
524         return VOLSERTRELE_ERROR;
525
526     Log("1 Volser: Delete: volume %u deleted \n", tt->volid);
527     return 0;                   /* vpurgevolume doesn't set an error code */
528 }
529
530 /* make a clone of the volume associated with atrans, possibly giving it a new
531  * number (allocate a new number if *newNumber==0, otherwise use *newNumber
532  * for the clone's id).  The new clone is given the name newName.  Finally, due to
533  * efficiency considerations, if purgeId is non-zero, we purge that volume when doing
534  * the clone operation.  This may be useful when making new backup volumes, for instance
535  * since the net result of a clone and a purge generally leaves many inode ref counts
536  * the same, while doing them separately would result in far more iincs and idecs being
537  * peformed (and they are slow operations).
538  */
539 afs_int32
540 SAFSVolClone(acid, atrans, purgeId, newType, newName, newNumber)
541      struct rx_call *acid;
542      afs_int32 atrans;
543      afs_int32 newType;
544      afs_int32 *newNumber;
545      afs_int32 purgeId;         /* for efficiency reasons, sometimes faster to piggyback a purge here */
546      char *newName;
547 {
548     afs_int32 code;
549
550     code = VolClone(acid, atrans, purgeId, newType, newName, newNumber);
551     osi_auditU(acid, VS_CloneEvent, code, AUD_LONG, atrans, AUD_LONG, purgeId,
552                AUD_STR, newName, AUD_LONG, newType, AUD_LONG, *newNumber,
553                AUD_END);
554     return code;
555 }
556
557 afs_int32
558 VolClone(acid, atrans, purgeId, newType, newName, newNumber)
559      struct rx_call *acid;
560      afs_int32 atrans;
561      afs_int32 newType;
562      afs_int32 *newNumber;
563      afs_int32 purgeId;         /* for efficiency reasons, sometimes faster to piggyback a purge here */
564      char *newName;
565 {
566     VolumeId newId;
567     register struct Volume *originalvp, *purgevp, *newvp;
568     Error error, code;
569     register struct volser_trans *tt, *ttc;
570     char caller[MAXKTCNAMELEN];
571
572     if (strlen(newName) > 31)
573         return VOLSERBADNAME;
574     if (!afsconf_SuperUser(tdir, acid, caller))
575         return VOLSERBAD_ACCESS;        /*not a super user */
576     if (DoLogging)
577         Log("%s is executing Clone Volume new name=%s\n", caller, newName);
578     error = 0;
579     originalvp = (Volume *) 0;
580     purgevp = (Volume *) 0;
581     newvp = (Volume *) 0;
582     tt = ttc = (struct volser_trans *)0;
583
584     if (!newNumber || !*newNumber) {
585         Log("1 Volser: Clone: missing volume number for the clone; aborted\n");
586         goto fail;
587     }
588     newId = *newNumber;
589
590     if (newType != readonlyVolume && newType != backupVolume)
591         return EINVAL;
592     tt = FindTrans(atrans);
593     if (!tt)
594         return ENOENT;
595     if (tt->vflags & VTDeleted) {
596         Log("1 Volser: Clone: volume %u has been deleted \n", tt->volid);
597         TRELE(tt);
598         return ENOENT;
599     }
600     ttc = NewTrans(newId, tt->partition);
601     if (!ttc) {                 /* someone is messing with the clone already */
602         TRELE(tt);
603         return VBUSY;
604     }
605     strcpy(tt->lastProcName, "Clone");
606     tt->rxCallPtr = acid;
607
608
609     if (purgeId) {
610         purgevp = VAttachVolume(&error, purgeId, V_VOLUPD);
611         if (error) {
612             Log("1 Volser: Clone: Could not attach 'purge' volume %u; clone aborted\n", purgeId);
613             goto fail;
614         }
615     } else {
616         purgevp = NULL;
617     }
618     originalvp = tt->volume;
619     if ((V_type(originalvp) == backupVolume)
620         || (V_type(originalvp) == readonlyVolume)) {
621         Log("1 Volser: Clone: The volume to be cloned must be a read/write; aborted\n");
622         error = EROFS;
623         goto fail;
624     }
625     if ((V_destroyMe(originalvp) == DESTROY_ME) || !V_inService(originalvp)) {
626         Log("1 Volser: Clone: Volume %d is offline and cannot be cloned\n",
627             V_id(originalvp));
628         error = VOFFLINE;
629         goto fail;
630     }
631     if (purgevp) {
632         if (originalvp->device != purgevp->device) {
633             Log("1 Volser: Clone: Volumes %u and %u are on different devices\n", tt->volid, purgeId);
634             error = EXDEV;
635             goto fail;
636         }
637         if (V_type(purgevp) != readonlyVolume) {
638             Log("1 Volser: Clone: The \"purge\" volume must be a read only volume; aborted\n");
639             error = EINVAL;
640             goto fail;
641         }
642         if (V_type(originalvp) == readonlyVolume
643             && V_parentId(originalvp) != V_parentId(purgevp)) {
644             Log("1 Volser: Clone: Volume %u and volume %u were not cloned from the same parent volume; aborted\n", tt->volid, purgeId);
645             error = EXDEV;
646             goto fail;
647         }
648         if (V_type(originalvp) == readwriteVolume
649             && tt->volid != V_parentId(purgevp)) {
650             Log("1 Volser: Clone: Volume %u was not originally cloned from volume %u; aborted\n", purgeId, tt->volid);
651             error = EXDEV;
652             goto fail;
653         }
654     }
655
656     error = 0;
657
658     newvp =
659         VCreateVolume(&error, originalvp->partition->name, newId,
660                       V_parentId(originalvp));
661     if (error) {
662         Log("1 Volser: Clone: Couldn't create new volume; clone aborted\n");
663         newvp = (Volume *) 0;
664         goto fail;
665     }
666     if (newType == readonlyVolume)
667         V_cloneId(originalvp) = newId;
668     Log("1 Volser: Clone: Cloning volume %u to new volume %u\n", tt->volid,
669         newId);
670     if (purgevp)
671         Log("1 Volser: Clone: Purging old read only volume %u\n", purgeId);
672     CloneVolume(&error, originalvp, newvp, purgevp);
673     purgevp = NULL;             /* clone releases it, maybe even if error */
674     if (error) {
675         Log("1 Volser: Clone: clone operation failed with code %u\n", error);
676         LogError(error);
677         goto fail;
678     }
679     if (newType == readonlyVolume) {
680         AssignVolumeName(&V_disk(newvp), V_name(originalvp), ".readonly");
681         V_type(newvp) = readonlyVolume;
682     } else if (newType == backupVolume) {
683         AssignVolumeName(&V_disk(newvp), V_name(originalvp), ".backup");
684         V_type(newvp) = backupVolume;
685         V_backupId(originalvp) = newId;
686     }
687     strcpy(newvp->header->diskstuff.name, newName);
688     V_creationDate(newvp) = V_copyDate(newvp);
689     ClearVolumeStats(&V_disk(newvp));
690     V_destroyMe(newvp) = DESTROY_ME;
691     V_inService(newvp) = 0;
692     if (newType == backupVolume) {
693         V_backupDate(originalvp) = V_copyDate(newvp);
694         V_backupDate(newvp) = V_copyDate(newvp);
695     }
696     V_inUse(newvp) = 0;
697     VUpdateVolume(&error, newvp);
698     if (error) {
699         Log("1 Volser: Clone: VUpdate failed code %u\n", error);
700         LogError(error);
701         goto fail;
702     }
703     VDetachVolume(&error, newvp);       /* allow file server to get it's hands on it */
704     newvp = NULL;
705     VUpdateVolume(&error, originalvp);
706     if (error) {
707         Log("1 Volser: Clone: original update %u\n", error);
708         LogError(error);
709         goto fail;
710     }
711     tt->rxCallPtr = (struct rx_call *)0;
712     if (TRELE(tt)) {
713         tt = (struct volser_trans *)0;
714         error = VOLSERTRELE_ERROR;
715         goto fail;
716     }
717     DeleteTrans(ttc);
718     return 0;
719
720   fail:
721     if (purgevp)
722         VDetachVolume(&code, purgevp);
723     if (newvp)
724         VDetachVolume(&code, newvp);
725     if (tt) {
726         tt->rxCallPtr = (struct rx_call *)0;
727         TRELE(tt);
728     }
729     if (ttc)
730         DeleteTrans(ttc);
731     return error;
732 }
733
734 /* reclone this volume into the specified id */
735 afs_int32
736 SAFSVolReClone(acid, atrans, cloneId)
737      struct rx_call *acid;
738      afs_int32 atrans;
739      afs_int32 cloneId;
740 {
741     afs_int32 code;
742
743     code = VolReClone(acid, atrans, cloneId);
744     osi_auditU(acid, VS_ReCloneEvent, code, AUD_LONG, atrans, AUD_LONG,
745                cloneId, AUD_END);
746     return code;
747 }
748
749 afs_int32
750 VolReClone(acid, atrans, cloneId)
751      struct rx_call *acid;
752      afs_int32 atrans;
753      afs_int32 cloneId;
754 {
755     register struct Volume *originalvp, *clonevp;
756     Error error, code;
757     afs_int32 newType;
758     register struct volser_trans *tt, *ttc;
759     char caller[MAXKTCNAMELEN];
760
761     /*not a super user */
762     if (!afsconf_SuperUser(tdir, acid, caller))
763         return VOLSERBAD_ACCESS;
764     if (DoLogging)
765         Log("%s is executing Reclone Volume %u\n", caller, cloneId);
766     error = 0;
767     clonevp = originalvp = (Volume *) 0;
768     tt = (struct volser_trans *)0;
769
770     tt = FindTrans(atrans);
771     if (!tt)
772         return ENOENT;
773     if (tt->vflags & VTDeleted) {
774         Log("1 Volser: VolReClone: volume %u has been deleted \n", tt->volid);
775         TRELE(tt);
776         return ENOENT;
777     }
778     ttc = NewTrans(cloneId, tt->partition);
779     if (!ttc) {                 /* someone is messing with the clone already */
780         TRELE(tt);
781         return VBUSY;
782     }
783     strcpy(tt->lastProcName, "ReClone");
784     tt->rxCallPtr = acid;
785
786     originalvp = tt->volume;
787     if ((V_type(originalvp) == backupVolume)
788         || (V_type(originalvp) == readonlyVolume)) {
789         Log("1 Volser: Clone: The volume to be cloned must be a read/write; aborted\n");
790         error = EROFS;
791         goto fail;
792     }
793     if ((V_destroyMe(originalvp) == DESTROY_ME) || !V_inService(originalvp)) {
794         Log("1 Volser: Clone: Volume %d is offline and cannot be cloned\n",
795             V_id(originalvp));
796         error = VOFFLINE;
797         goto fail;
798     }
799
800     clonevp = VAttachVolume(&error, cloneId, V_VOLUPD);
801     if (error) {
802         Log("1 Volser: can't attach clone %d\n", cloneId);
803         goto fail;
804     }
805
806     newType = V_type(clonevp);  /* type of the new volume */
807
808     if (originalvp->device != clonevp->device) {
809         Log("1 Volser: Clone: Volumes %u and %u are on different devices\n",
810             tt->volid, cloneId);
811         error = EXDEV;
812         goto fail;
813     }
814     if (V_type(clonevp) != readonlyVolume && V_type(clonevp) != backupVolume) {
815         Log("1 Volser: Clone: The \"recloned\" volume must be a read only volume; aborted\n");
816         error = EINVAL;
817         goto fail;
818     }
819     if (V_type(originalvp) == readonlyVolume
820         && V_parentId(originalvp) != V_parentId(clonevp)) {
821         Log("1 Volser: Clone: Volume %u and volume %u were not cloned from the same parent volume; aborted\n", tt->volid, cloneId);
822         error = EXDEV;
823         goto fail;
824     }
825     if (V_type(originalvp) == readwriteVolume
826         && tt->volid != V_parentId(clonevp)) {
827         Log("1 Volser: Clone: Volume %u was not originally cloned from volume %u; aborted\n", cloneId, tt->volid);
828         error = EXDEV;
829         goto fail;
830     }
831
832     error = 0;
833     Log("1 Volser: Clone: Recloning volume %u to volume %u\n", tt->volid,
834         cloneId);
835     CloneVolume(&error, originalvp, clonevp, clonevp);
836     if (error) {
837         Log("1 Volser: Clone: reclone operation failed with code %d\n",
838             error);
839         LogError(error);
840         goto fail;
841     }
842
843     /* fix up volume name and type, CloneVolume just propagated RW's */
844     if (newType == readonlyVolume) {
845         AssignVolumeName(&V_disk(clonevp), V_name(originalvp), ".readonly");
846         V_type(clonevp) = readonlyVolume;
847     } else if (newType == backupVolume) {
848         AssignVolumeName(&V_disk(clonevp), V_name(originalvp), ".backup");
849         V_type(clonevp) = backupVolume;
850         V_backupId(originalvp) = cloneId;
851     }
852     /* don't do strcpy onto diskstuff.name, it's still OK from 1st clone */
853
854     /* pretend recloned volume is a totally new instance */
855     V_copyDate(clonevp) = time(0);
856     V_creationDate(clonevp) = V_copyDate(clonevp);
857     ClearVolumeStats(&V_disk(clonevp));
858     V_destroyMe(clonevp) = 0;
859     V_inService(clonevp) = 0;
860     if (newType == backupVolume) {
861         V_backupDate(originalvp) = V_copyDate(clonevp);
862         V_backupDate(clonevp) = V_copyDate(clonevp);
863     }
864     V_inUse(clonevp) = 0;
865     VUpdateVolume(&error, clonevp);
866     if (error) {
867         Log("1 Volser: Clone: VUpdate failed code %u\n", error);
868         LogError(error);
869         goto fail;
870     }
871     VDetachVolume(&error, clonevp);     /* allow file server to get it's hands on it */
872     clonevp = NULL;
873     VUpdateVolume(&error, originalvp);
874     if (error) {
875         Log("1 Volser: Clone: original update %u\n", error);
876         LogError(error);
877         goto fail;
878     }
879     tt->rxCallPtr = (struct rx_call *)0;
880     if (TRELE(tt)) {
881         tt = (struct volser_trans *)0;
882         error = VOLSERTRELE_ERROR;
883         goto fail;
884     }
885
886     DeleteTrans(ttc);
887
888     {
889         struct DiskPartition *tpartp = originalvp->partition;
890         FSYNC_askfs(cloneId, tpartp->name, FSYNC_RESTOREVOLUME, 0);
891     }
892     return 0;
893
894   fail:
895     if (clonevp)
896         VDetachVolume(&code, clonevp);
897     if (tt) {
898         tt->rxCallPtr = (struct rx_call *)0;
899         TRELE(tt);
900     }
901     if (ttc)
902         DeleteTrans(ttc);
903     return error;
904 }
905
906 /* create a new transaction, associated with volume and partition.  Type of
907  * volume transaction is spec'd by iflags.  New trans id is returned in ttid.
908  * See volser.h for definition of iflags (the constants are named IT*).
909  */
910 afs_int32
911 SAFSVolTransCreate(acid, volume, partition, iflags, ttid)
912      struct rx_call *acid;
913      afs_int32 volume;
914      afs_int32 partition;
915      afs_int32 iflags;
916      afs_int32 *ttid;
917 {
918     afs_int32 code;
919
920     code = VolTransCreate(acid, volume, partition, iflags, ttid);
921     osi_auditU(acid, VS_TransCrEvent, code, AUD_LONG, *ttid, AUD_LONG, volume,
922                AUD_END);
923     return code;
924 }
925
926 afs_int32
927 VolTransCreate(acid, volume, partition, iflags, ttid)
928      struct rx_call *acid;
929      afs_int32 volume;
930      afs_int32 partition;
931      afs_int32 iflags;
932      afs_int32 *ttid;
933 {
934     register struct volser_trans *tt;
935     register Volume *tv;
936     afs_int32 error, code;
937     afs_int32 mode;
938     char caller[MAXKTCNAMELEN];
939
940     if (!afsconf_SuperUser(tdir, acid, caller))
941         return VOLSERBAD_ACCESS;        /*not a super user */
942     if (iflags & ITCreate)
943         mode = V_SECRETLY;
944     else if (iflags & ITBusy)
945         mode = V_CLONE;
946     else if (iflags & ITReadOnly)
947         mode = V_READONLY;
948     else if (iflags & ITOffline)
949         mode = V_VOLUPD;
950     else {
951         Log("1 Volser: TransCreate: Could not create trans, error %u\n",
952             EINVAL);
953         LogError(EINVAL);
954         return EINVAL;
955     }
956     tt = NewTrans(volume, partition);
957     if (!tt) {
958         /* can't create a transaction? put the volume back */
959         Log("1 transcreate: can't create transaction\n");
960         return VOLSERVOLBUSY;
961     }
962     tv = XAttachVolume(&error, volume, partition, mode);
963     if (error) {
964         /* give up */
965         if (tv)
966             VDetachVolume(&code, tv);
967         DeleteTrans(tt);
968         return error;
969     }
970     tt->volume = tv;
971     *ttid = tt->tid;
972     tt->iflags = iflags;
973     tt->vflags = 0;
974     strcpy(tt->lastProcName, "TransCreate");
975     if (TRELE(tt))
976         return VOLSERTRELE_ERROR;
977
978     return 0;
979 }
980
981 /* using aindex as a 0-based index, return the aindex'th volume on this server
982  * Both the volume number and partition number (one-based) are returned.
983  */
984 afs_int32
985 SAFSVolGetNthVolume(acid, aindex, avolume, apart)
986      struct rx_call *acid;
987      afs_int32 aindex;
988      afs_int32 *avolume;
989      afs_int32 *apart;
990 {
991     afs_int32 code;
992
993     code = VolGetNthVolume(acid, aindex, avolume, apart);
994     osi_auditU(acid, VS_GetNVolEvent, code, AUD_LONG, *avolume, AUD_END);
995     return code;
996 }
997
998 afs_int32
999 VolGetNthVolume(acid, aindex, avolume, apart)
1000      struct rx_call *acid;
1001      afs_int32 aindex;
1002      afs_int32 *avolume;
1003      afs_int32 *apart;
1004 {
1005     Log("1 Volser: GetNthVolume: Not yet implemented\n");
1006     return VOLSERNO_OP;
1007 }
1008
1009 /* return the volume flags (VT* constants in volser.h) associated with this
1010  * transaction.
1011  */
1012 afs_int32
1013 SAFSVolGetFlags(acid, atid, aflags)
1014      struct rx_call *acid;
1015      afs_int32 atid;
1016      afs_int32 *aflags;
1017 {
1018     afs_int32 code;
1019
1020     code = VolGetFlags(acid, atid, aflags);
1021     osi_auditU(acid, VS_GetFlgsEvent, code, AUD_LONG, atid, AUD_END);
1022     return code;
1023 }
1024
1025 afs_int32
1026 VolGetFlags(acid, atid, aflags)
1027      struct rx_call *acid;
1028      afs_int32 atid;
1029      afs_int32 *aflags;
1030 {
1031     register struct volser_trans *tt;
1032
1033     tt = FindTrans(atid);
1034     if (!tt)
1035         return ENOENT;
1036     if (tt->vflags & VTDeleted) {
1037         Log("1 Volser: VolGetFlags: volume %u has been deleted \n",
1038             tt->volid);
1039         TRELE(tt);
1040         return ENOENT;
1041     }
1042     strcpy(tt->lastProcName, "GetFlags");
1043     tt->rxCallPtr = acid;
1044     *aflags = tt->vflags;
1045     tt->rxCallPtr = (struct rx_call *)0;
1046     if (TRELE(tt))
1047         return VOLSERTRELE_ERROR;
1048
1049     return 0;
1050 }
1051
1052 /* Change the volume flags (VT* constants in volser.h) associated with this
1053  * transaction.  Effects take place immediately on volume, although volume
1054  * remains attached as usual by the transaction.
1055  */
1056 afs_int32
1057 SAFSVolSetFlags(acid, atid, aflags)
1058      struct rx_call *acid;
1059      afs_int32 atid;
1060      afs_int32 aflags;
1061 {
1062     afs_int32 code;
1063
1064     code = VolSetFlags(acid, atid, aflags);
1065     osi_auditU(acid, VS_SetFlgsEvent, code, AUD_LONG, atid, AUD_LONG, aflags,
1066                AUD_END);
1067     return code;
1068 }
1069
1070 afs_int32
1071 VolSetFlags(acid, atid, aflags)
1072      struct rx_call *acid;
1073      afs_int32 atid;
1074      afs_int32 aflags;
1075 {
1076     register struct volser_trans *tt;
1077     register struct Volume *vp;
1078     afs_int32 error;
1079     char caller[MAXKTCNAMELEN];
1080
1081     if (!afsconf_SuperUser(tdir, acid, caller))
1082         return VOLSERBAD_ACCESS;        /*not a super user */
1083     /* find the trans */
1084     tt = FindTrans(atid);
1085     if (!tt)
1086         return ENOENT;
1087     if (tt->vflags & VTDeleted) {
1088         Log("1 Volser: VolSetFlags: volume %u has been deleted \n",
1089             tt->volid);
1090         TRELE(tt);
1091         return ENOENT;
1092     }
1093     strcpy(tt->lastProcName, "SetFlags");
1094     tt->rxCallPtr = acid;
1095     vp = tt->volume;            /* pull volume out of transaction */
1096
1097     /* check if we're allowed to make any updates */
1098     if (tt->iflags & ITReadOnly) {
1099         TRELE(tt);
1100         return EROFS;
1101     }
1102
1103     /* handle delete-on-salvage flag */
1104     if (aflags & VTDeleteOnSalvage) {
1105         V_destroyMe(tt->volume) = DESTROY_ME;
1106     } else {
1107         V_destroyMe(tt->volume) = 0;
1108     }
1109
1110     if (aflags & VTOutOfService) {
1111         V_inService(vp) = 0;
1112     } else {
1113         V_inService(vp) = 1;
1114     }
1115     VUpdateVolume(&error, vp);
1116     tt->vflags = aflags;
1117     tt->rxCallPtr = (struct rx_call *)0;
1118     if (TRELE(tt) && !error)
1119         return VOLSERTRELE_ERROR;
1120
1121     return error;
1122 }
1123
1124 /* dumpS the volume associated with a particular transaction from a particular
1125  * date.  Send the dump to a different transaction (destTrans) on the server
1126  * specified by the destServer structure.
1127  */
1128 afs_int32
1129 SAFSVolForward(acid, fromTrans, fromDate, destination, destTrans, cookie)
1130      struct rx_call *acid;
1131      afs_int32 fromTrans;
1132      afs_int32 fromDate;
1133      struct destServer *destination;
1134      struct restoreCookie *cookie;
1135      afs_int32 destTrans;
1136 {
1137     afs_int32 code;
1138
1139     code =
1140         VolForward(acid, fromTrans, fromDate, destination, destTrans, cookie);
1141     osi_auditU(acid, VS_ForwardEvent, code, AUD_LONG, fromTrans, AUD_HOST,
1142                destination->destHost, AUD_LONG, destTrans, AUD_END);
1143     return code;
1144 }
1145
1146 afs_int32
1147 VolForward(acid, fromTrans, fromDate, destination, destTrans, cookie)
1148      struct rx_call *acid;
1149      afs_int32 fromTrans;
1150      afs_int32 fromDate;
1151      struct destServer *destination;
1152      struct restoreCookie *cookie;
1153      afs_int32 destTrans;
1154 {
1155     register struct volser_trans *tt;
1156     register afs_int32 code;
1157     register struct rx_connection *tcon;
1158     struct rx_call *tcall;
1159     register struct Volume *vp;
1160     struct rx_securityClass *securityObject;
1161     afs_int32 securityIndex;
1162     char caller[MAXKTCNAMELEN];
1163
1164     if (!afsconf_SuperUser(tdir, acid, caller))
1165         return VOLSERBAD_ACCESS;        /*not a super user */
1166     /* initialize things */
1167     tcon = (struct rx_connection *)0;
1168     tt = (struct volser_trans *)0;
1169
1170     /* find the local transaction */
1171     tt = FindTrans(fromTrans);
1172     if (!tt)
1173         return ENOENT;
1174     if (tt->vflags & VTDeleted) {
1175         Log("1 Volser: VolForward: volume %u has been deleted \n", tt->volid);
1176         TRELE(tt);
1177         return ENOENT;
1178     }
1179     vp = tt->volume;
1180     strcpy(tt->lastProcName, "Forward");
1181
1182     /* get auth info for the this connection (uses afs from ticket file) */
1183     code = afsconf_ClientAuth(tdir, &securityObject, &securityIndex);
1184     if (code) {
1185         TRELE(tt);
1186         return code;
1187     }
1188
1189     /* make an rpc connection to the other server */
1190     tcon =
1191         rx_NewConnection(htonl(destination->destHost),
1192                          htons(destination->destPort), VOLSERVICE_ID,
1193                          securityObject, securityIndex);
1194     if (!tcon) {
1195         tt->rxCallPtr = (struct rx_call *)0;
1196         TRELE(tt);
1197         return ENOTCONN;
1198     }
1199     tcall = rx_NewCall(tcon);
1200     tt->rxCallPtr = tcall;
1201     /* start restore going.  fromdate == 0 --> doing an incremental dump/restore */
1202     code = StartAFSVolRestore(tcall, destTrans, (fromDate ? 1 : 0), cookie);
1203     if (code) {
1204         goto fail;
1205     }
1206
1207     /* these next calls implictly call rx_Write when writing out data */
1208     code = DumpVolume(tcall, vp, fromDate, 0);  /* last field = don't dump all dirs */
1209     if (code)
1210         goto fail;
1211     EndAFSVolRestore(tcall);    /* probably doesn't do much */
1212     tt->rxCallPtr = (struct rx_call *)0;
1213     code = rx_EndCall(tcall, 0);
1214     rx_DestroyConnection(tcon); /* done with the connection */
1215     tcon = NULL;
1216     if (code)
1217         goto fail;
1218     if (TRELE(tt))
1219         return VOLSERTRELE_ERROR;
1220
1221     return 0;
1222
1223   fail:
1224     if (tcon) {
1225         (void)rx_EndCall(tcall, 0);
1226         rx_DestroyConnection(tcon);
1227     }
1228     if (tt) {
1229         tt->rxCallPtr = (struct rx_call *)0;
1230         TRELE(tt);
1231     }
1232     return code;
1233 }
1234
1235 /* Start a dump and send it to multiple places simultaneously.
1236  * If this returns an error (eg, return ENOENT), it means that
1237  * none of the releases worked.  If this returns 0, that means 
1238  * that one or more of the releases worked, and the caller has
1239  * to examine the results array to see which one(s).
1240  * This will only do EITHER incremental or full, not both, so it's
1241  * the caller's responsibility to be sure that all the destinations
1242  * need just an incremental (and from the same time), if that's 
1243  * what we're doing. 
1244  */
1245 afs_int32
1246 SAFSVolForwardMultiple(acid, fromTrans, fromDate, destinations, spare, cookie,
1247                        results)
1248      struct rx_call *acid;
1249      afs_int32 fromTrans;
1250      afs_int32 fromDate;
1251      afs_int32 spare;
1252      manyDests *destinations;
1253      struct restoreCookie *cookie;
1254      manyResults *results;
1255 {
1256     afs_int32 securityIndex;
1257     struct rx_securityClass *securityObject;
1258     char caller[MAXKTCNAMELEN];
1259     struct volser_trans *tt;
1260     afs_int32 ec, code, *codes;
1261     struct rx_connection **tcons;
1262     struct rx_call **tcalls;
1263     struct Volume *vp;
1264     int i, nconns, is_incremental;
1265
1266     if (results)
1267         memset(results, 0, sizeof(manyResults));
1268
1269     if (!afsconf_SuperUser(tdir, acid, caller))
1270         return VOLSERBAD_ACCESS;        /*not a super user */
1271     tt = FindTrans(fromTrans);
1272     if (!tt)
1273         return ENOENT;
1274     if (tt->vflags & VTDeleted) {
1275         Log("1 Volser: VolForward: volume %u has been deleted \n", tt->volid);
1276         TRELE(tt);
1277         return ENOENT;
1278     }
1279     vp = tt->volume;
1280     strcpy(tt->lastProcName, "ForwardMulti");
1281
1282     /* (fromDate == 0) ==> incremental dump */
1283     is_incremental = (fromDate ? 1 : 0);
1284
1285     i = results->manyResults_len = destinations->manyDests_len;
1286     results->manyResults_val = codes =
1287         (afs_int32 *) malloc(i * sizeof(afs_int32));
1288     tcons =
1289         (struct rx_connection **)malloc(i * sizeof(struct rx_connection *));
1290     tcalls = (struct rx_call **)malloc(i * sizeof(struct rx_call *));
1291
1292     /* get auth info for this connection (uses afs from ticket file) */
1293     code = afsconf_ClientAuth(tdir, &securityObject, &securityIndex);
1294     if (code) {
1295         goto fail;              /* in order to audit each failure */
1296     }
1297
1298     /* make connections to all the other servers */
1299     for (i = 0; i < destinations->manyDests_len; i++) {
1300         struct replica *dest = &(destinations->manyDests_val[i]);
1301         tcons[i] =
1302             rx_NewConnection(htonl(dest->server.destHost),
1303                              htons(dest->server.destPort), VOLSERVICE_ID,
1304                              securityObject, securityIndex);
1305         if (!tcons[i]) {
1306             codes[i] = ENOTCONN;
1307         } else {
1308             if (!(tcalls[i] = rx_NewCall(tcons[i])))
1309                 codes[i] = ENOTCONN;
1310             else {
1311                 codes[i] =
1312                     StartAFSVolRestore(tcalls[i], dest->trans, is_incremental,
1313                                        cookie);
1314                 if (codes[i]) {
1315                     (void)rx_EndCall(tcalls[i], 0);
1316                     tcalls[i] = 0;
1317                     rx_DestroyConnection(tcons[i]);
1318                     tcons[i] = 0;
1319                 }
1320             }
1321         }
1322     }
1323
1324     /* these next calls implictly call rx_Write when writing out data */
1325     code = DumpVolMulti(tcalls, i, vp, fromDate, 0, codes);
1326
1327
1328   fail:
1329     for (i--; i >= 0; i--) {
1330         struct replica *dest = &(destinations->manyDests_val[i]);
1331
1332         if (!code && tcalls[i] && !codes[i]) {
1333             EndAFSVolRestore(tcalls[i]);
1334         }
1335         if (tcalls[i]) {
1336             ec = rx_EndCall(tcalls[i], 0);
1337             if (!codes[i])
1338                 codes[i] = ec;
1339         }
1340         if (tcons[i]) {
1341             rx_DestroyConnection(tcons[i]);     /* done with the connection */
1342         }
1343
1344         osi_auditU(acid, VS_ForwardEvent, (code ? code : codes[i]), AUD_LONG,
1345                    fromTrans, AUD_HOST, dest->server.destHost, AUD_LONG,
1346                    dest->trans, AUD_END);
1347     }
1348     free(tcons);
1349     free(tcalls);
1350
1351     if (tt) {
1352         tt->rxCallPtr = (struct rx_call *)0;
1353         if (TRELE(tt) && !code) /* return the first code if it's set */
1354             return VOLSERTRELE_ERROR;
1355     }
1356
1357     return code;
1358 }
1359
1360 afs_int32
1361 SAFSVolDump(acid, fromTrans, fromDate)
1362      struct rx_call *acid;
1363      afs_int32 fromTrans;
1364      afs_int32 fromDate;
1365 {
1366     afs_int32 code;
1367
1368     code = VolDump(acid, fromTrans, fromDate);
1369     osi_auditU(acid, VS_DumpEvent, code, AUD_LONG, fromTrans, AUD_END);
1370     return code;
1371 }
1372
1373 afs_int32
1374 VolDump(acid, fromTrans, fromDate)
1375      struct rx_call *acid;
1376      afs_int32 fromTrans;
1377      afs_int32 fromDate;
1378 {
1379     int code = 0;
1380     register struct volser_trans *tt;
1381     char caller[MAXKTCNAMELEN];
1382
1383     if (!afsconf_SuperUser(tdir, acid, caller))
1384         return VOLSERBAD_ACCESS;        /*not a super user */
1385     tt = FindTrans(fromTrans);
1386     if (!tt)
1387         return ENOENT;
1388     if (tt->vflags & VTDeleted) {
1389         Log("1 Volser: VolDump: volume %u has been deleted \n", tt->volid);
1390         TRELE(tt);
1391         return ENOENT;
1392     }
1393     strcpy(tt->lastProcName, "Dump");
1394     tt->rxCallPtr = acid;
1395     code = DumpVolume(acid, tt->volume, fromDate, 1);   /* squirt out the volume's data, too */
1396     if (code) {
1397         tt->rxCallPtr = (struct rx_call *)0;
1398         TRELE(tt);
1399         return code;
1400     }
1401     tt->rxCallPtr = (struct rx_call *)0;
1402
1403     if (TRELE(tt))
1404         return VOLSERTRELE_ERROR;
1405
1406     return 0;
1407 }
1408
1409 /* 
1410  * Ha!  No more helper process!
1411  */
1412 afs_int32
1413 SAFSVolRestore(acid, atrans, aflags, cookie)
1414      struct rx_call *acid;
1415      afs_int32 aflags;
1416      afs_int32 atrans;
1417      struct restoreCookie *cookie;
1418 {
1419     afs_int32 code;
1420
1421     code = VolRestore(acid, atrans, aflags, cookie);
1422     osi_auditU(acid, VS_RestoreEvent, code, AUD_LONG, atrans, AUD_END);
1423     return code;
1424 }
1425
1426 afs_int32
1427 VolRestore(acid, atrans, aflags, cookie)
1428      struct rx_call *acid;
1429      int aflags;
1430      afs_int32 atrans;
1431      struct restoreCookie *cookie;
1432 {
1433     register struct volser_trans *tt;
1434     register afs_int32 code, tcode;
1435     char caller[MAXKTCNAMELEN];
1436
1437     if (!afsconf_SuperUser(tdir, acid, caller))
1438         return VOLSERBAD_ACCESS;        /*not a super user */
1439     tt = FindTrans(atrans);
1440     if (!tt)
1441         return ENOENT;
1442     if (tt->vflags & VTDeleted) {
1443         Log("1 Volser: VolRestore: volume %u has been deleted \n", tt->volid);
1444         TRELE(tt);
1445         return ENOENT;
1446     }
1447     strcpy(tt->lastProcName, "Restore");
1448     tt->rxCallPtr = acid;
1449     code = RestoreVolume(acid, tt->volume, (aflags & 1), cookie);       /* last is incrementalp */
1450     FSYNC_askfs(tt->volid, NULL, FSYNC_RESTOREVOLUME, 0l);      /*break call backs on the
1451                                                                  * restored volume */
1452     tt->rxCallPtr = (struct rx_call *)0;
1453     tcode = TRELE(tt);
1454
1455     return (code ? code : tcode);
1456 }
1457
1458 /* end a transaction, returning the transaction's final error code in rcode */
1459 afs_int32
1460 SAFSVolEndTrans(acid, destTrans, rcode)
1461      struct rx_call *acid;
1462      afs_int32 destTrans;
1463      afs_int32 *rcode;
1464 {
1465     afs_int32 code;
1466
1467     code = VolEndTrans(acid, destTrans, rcode);
1468     osi_auditU(acid, VS_EndTrnEvent, code, AUD_LONG, destTrans, AUD_END);
1469     return code;
1470 }
1471
1472 afs_int32
1473 VolEndTrans(acid, destTrans, rcode)
1474      struct rx_call *acid;
1475      afs_int32 destTrans;
1476      afs_int32 *rcode;
1477 {
1478     register struct volser_trans *tt;
1479     char caller[MAXKTCNAMELEN];
1480
1481     if (!afsconf_SuperUser(tdir, acid, caller))
1482         return VOLSERBAD_ACCESS;        /*not a super user */
1483     tt = FindTrans(destTrans);
1484     if (!tt) {
1485         return ENOENT;
1486     }
1487     *rcode = tt->returnCode;
1488     DeleteTrans(tt);            /* this does an implicit TRELE */
1489
1490     return 0;
1491 }
1492
1493 afs_int32
1494 SAFSVolSetForwarding(acid, atid, anewsite)
1495      struct rx_call *acid;
1496      afs_int32 atid;
1497      afs_int32 anewsite;
1498 {
1499     afs_int32 code;
1500
1501     code = VolSetForwarding(acid, atid, anewsite);
1502     osi_auditU(acid, VS_SetForwEvent, code, AUD_LONG, atid, AUD_HOST,
1503                anewsite, AUD_END);
1504     return code;
1505 }
1506
1507 afs_int32
1508 VolSetForwarding(acid, atid, anewsite)
1509      struct rx_call *acid;
1510      afs_int32 atid;
1511      afs_int32 anewsite;
1512 {
1513
1514     register struct volser_trans *tt;
1515     char caller[MAXKTCNAMELEN];
1516
1517     if (!afsconf_SuperUser(tdir, acid, caller))
1518         return VOLSERBAD_ACCESS;        /*not a super user */
1519     tt = FindTrans(atid);
1520     if (!tt)
1521         return ENOENT;
1522     if (tt->vflags & VTDeleted) {
1523         Log("1 Volser: VolSetForwarding: volume %u has been deleted \n",
1524             tt->volid);
1525         TRELE(tt);
1526         return ENOENT;
1527     }
1528     strcpy(tt->lastProcName, "SetForwarding");
1529     tt->rxCallPtr = acid;
1530     FSYNC_askfs(tt->volid, NULL, FSYNC_MOVEVOLUME, anewsite);
1531     tt->rxCallPtr = (struct rx_call *)0;
1532     if (TRELE(tt))
1533         return VOLSERTRELE_ERROR;
1534
1535     return 0;
1536 }
1537
1538 afs_int32
1539 SAFSVolGetStatus(acid, atrans, astatus)
1540      struct rx_call *acid;
1541      afs_int32 atrans;
1542      register struct volser_status *astatus;
1543 {
1544     afs_int32 code;
1545
1546     code = VolGetStatus(acid, atrans, astatus);
1547     osi_auditU(acid, VS_GetStatEvent, code, AUD_LONG, atrans, AUD_END);
1548     return code;
1549 }
1550
1551 afs_int32
1552 VolGetStatus(acid, atrans, astatus)
1553      struct rx_call *acid;
1554      afs_int32 atrans;
1555      register struct volser_status *astatus;
1556 {
1557     register struct Volume *tv;
1558     register struct VolumeDiskData *td;
1559     struct volser_trans *tt;
1560
1561
1562     tt = FindTrans(atrans);
1563     if (!tt)
1564         return ENOENT;
1565     if (tt->vflags & VTDeleted) {
1566         Log("1 Volser: VolGetStatus: volume %u has been deleted \n",
1567             tt->volid);
1568         TRELE(tt);
1569         return ENOENT;
1570     }
1571     strcpy(tt->lastProcName, "GetStatus");
1572     tt->rxCallPtr = acid;
1573     tv = tt->volume;
1574     if (!tv) {
1575         tt->rxCallPtr = (struct rx_call *)0;
1576         TRELE(tt);
1577         return ENOENT;
1578     }
1579
1580     td = &tv->header->diskstuff;
1581     astatus->volID = td->id;
1582     astatus->nextUnique = td->uniquifier;
1583     astatus->type = td->type;
1584     astatus->parentID = td->parentId;
1585     astatus->cloneID = td->cloneId;
1586     astatus->backupID = td->backupId;
1587     astatus->restoredFromID = td->restoredFromId;
1588     astatus->maxQuota = td->maxquota;
1589     astatus->minQuota = td->minquota;
1590     astatus->owner = td->owner;
1591     astatus->creationDate = td->creationDate;
1592     astatus->accessDate = td->accessDate;
1593     astatus->updateDate = td->updateDate;
1594     astatus->expirationDate = td->expirationDate;
1595     astatus->backupDate = td->backupDate;
1596     astatus->copyDate = td->copyDate;
1597     tt->rxCallPtr = (struct rx_call *)0;
1598     if (TRELE(tt))
1599         return VOLSERTRELE_ERROR;
1600
1601     return 0;
1602 }
1603
1604 afs_int32
1605 SAFSVolSetInfo(acid, atrans, astatus)
1606      struct rx_call *acid;
1607      afs_int32 atrans;
1608      register struct volintInfo *astatus;
1609 {
1610     afs_int32 code;
1611
1612     code = VolSetInfo(acid, atrans, astatus);
1613     osi_auditU(acid, VS_SetInfoEvent, code, AUD_LONG, atrans, AUD_END);
1614     return code;
1615 }
1616
1617 afs_int32
1618 VolSetInfo(acid, atrans, astatus)
1619      struct rx_call *acid;
1620      afs_int32 atrans;
1621      register struct volintInfo *astatus;
1622 {
1623     register struct Volume *tv;
1624     register struct VolumeDiskData *td;
1625     struct volser_trans *tt;
1626     char caller[MAXKTCNAMELEN];
1627     afs_int32 error;
1628
1629     if (!afsconf_SuperUser(tdir, acid, caller))
1630         return VOLSERBAD_ACCESS;        /*not a super user */
1631     tt = FindTrans(atrans);
1632     if (!tt)
1633         return ENOENT;
1634     if (tt->vflags & VTDeleted) {
1635         Log("1 Volser: VolSetInfo: volume %u has been deleted \n", tt->volid);
1636         TRELE(tt);
1637         return ENOENT;
1638     }
1639     strcpy(tt->lastProcName, "SetStatus");
1640     tt->rxCallPtr = acid;
1641     tv = tt->volume;
1642     if (!tv) {
1643         tt->rxCallPtr = (struct rx_call *)0;
1644         TRELE(tt);
1645         return ENOENT;
1646     }
1647
1648     td = &tv->header->diskstuff;
1649     /*
1650      * Add more fields as necessary
1651      */
1652     if (astatus->maxquota != -1)
1653         td->maxquota = astatus->maxquota;
1654     if (astatus->dayUse != -1)
1655         td->dayUse = astatus->dayUse;
1656     VUpdateVolume(&error, tv);
1657     tt->rxCallPtr = (struct rx_call *)0;
1658     if (TRELE(tt))
1659         return VOLSERTRELE_ERROR;
1660     return 0;
1661 }
1662
1663
1664 afs_int32
1665 SAFSVolGetName(acid, atrans, aname)
1666      struct rx_call *acid;
1667      afs_int32 atrans;
1668      char **aname;
1669 {
1670     afs_int32 code;
1671
1672     code = VolGetName(acid, atrans, aname);
1673     osi_auditU(acid, VS_GetNameEvent, code, AUD_LONG, atrans, AUD_END);
1674     return code;
1675 }
1676
1677 afs_int32
1678 VolGetName(acid, atrans, aname)
1679      struct rx_call *acid;
1680      afs_int32 atrans;
1681      char **aname;
1682 {
1683     register struct Volume *tv;
1684     register struct VolumeDiskData *td;
1685     struct volser_trans *tt;
1686     register int len;
1687
1688     *aname = NULL;
1689     tt = FindTrans(atrans);
1690     if (!tt)
1691         return ENOENT;
1692     if (tt->vflags & VTDeleted) {
1693         Log("1 Volser: VolGetName: volume %u has been deleted \n", tt->volid);
1694         TRELE(tt);
1695         return ENOENT;
1696     }
1697     strcpy(tt->lastProcName, "GetName");
1698     tt->rxCallPtr = acid;
1699     tv = tt->volume;
1700     if (!tv) {
1701         tt->rxCallPtr = (struct rx_call *)0;
1702         TRELE(tt);
1703         return ENOENT;
1704     }
1705
1706     td = &tv->header->diskstuff;
1707     len = strlen(td->name) + 1; /* don't forget the null */
1708     if (len >= SIZE) {
1709         tt->rxCallPtr = (struct rx_call *)0;
1710         TRELE(tt);
1711         return E2BIG;
1712     }
1713     *aname = (char *)malloc(len);
1714     strcpy(*aname, td->name);
1715     tt->rxCallPtr = (struct rx_call *)0;
1716     if (TRELE(tt))
1717         return VOLSERTRELE_ERROR;
1718
1719     return 0;
1720 }
1721
1722 /*this is a handshake to indicate that the next call will be SAFSVolRestore
1723  * - a noop now !*/
1724 afs_int32
1725 SAFSVolSignalRestore(acid, volname, volType, parentId, cloneId)
1726      struct rx_call *acid;
1727      char volname[];
1728      afs_int32 parentId, cloneId;
1729      int volType;
1730 {
1731     return 0;
1732 }
1733
1734
1735 /*return a list of all partitions on the server. The non mounted
1736  *partitions are returned as -1 in the corresponding slot in partIds*/
1737 afs_int32
1738 SAFSVolListPartitions(acid, partIds)
1739      struct rx_call *acid;
1740      struct pIDs *partIds;
1741 {
1742     afs_int32 code;
1743
1744     code = VolListPartitions(acid, partIds);
1745     osi_auditU(acid, VS_ListParEvent, code, AUD_END);
1746     return code;
1747 }
1748
1749 afs_int32
1750 VolListPartitions(acid, partIds)
1751      struct rx_call *acid;
1752      struct pIDs *partIds;
1753 {
1754     char namehead[9];
1755     int code;
1756     char i;
1757
1758     strcpy(namehead, "/vicep"); /*7 including null terminator */
1759
1760     /* Just return attached partitions. */
1761     namehead[7] = '\0';
1762     for (i = 0; i < 26; i++) {
1763         namehead[6] = i + 'a';
1764         if (VGetPartition(namehead, 0))
1765             partIds->partIds[i] = VGetPartition(namehead, 0) ? i : -1;
1766     }
1767
1768     return 0;
1769 }
1770
1771 /*return a list of all partitions on the server. The non mounted
1772  *partitions are returned as -1 in the corresponding slot in partIds*/
1773 afs_int32
1774 SAFSVolXListPartitions(acid, pEntries)
1775      struct rx_call *acid;
1776      struct partEntries *pEntries;
1777 {
1778     afs_int32 code;
1779
1780     code = XVolListPartitions(acid, pEntries);
1781     osi_auditU(acid, VS_ListParEvent, code, AUD_END);
1782     return code;
1783 }
1784
1785 afs_int32
1786 XVolListPartitions(acid, pEntries)
1787      struct rx_call *acid;
1788      struct partEntries *pEntries;
1789 {
1790     struct stat rbuf, pbuf;
1791     char namehead[9];
1792     struct partList partList;
1793     struct DiskPartition *dp;
1794     int i, j = 0, k;
1795
1796     strcpy(namehead, "/vicep"); /*7 including null terminator */
1797
1798     /* Only report attached partitions */
1799     for (i = 0; i < VOLMAXPARTS; i++) {
1800         if (i < 26) {
1801             namehead[6] = i + 'a';
1802             namehead[7] = '\0';
1803         } else {
1804             k = i - 26;
1805             namehead[6] = 'a' + (k / 26);
1806             namehead[7] = 'a' + (k % 26);
1807             namehead[8] = '\0';
1808         }
1809         dp = VGetPartition(namehead, 0);
1810         if (dp)
1811             partList.partId[j++] = i;
1812     }
1813     pEntries->partEntries_val = (afs_int32 *) malloc(j * sizeof(int));
1814     memcpy((char *)pEntries->partEntries_val, (char *)&partList,
1815            j * sizeof(int));
1816     pEntries->partEntries_len = j;
1817     return 0;
1818
1819 }
1820
1821 /*extract the volume id from string vname. Its of the form " V0*<id>.vol  "*/
1822 afs_int32
1823 ExtractVolId(vname)
1824      char vname[];
1825 {
1826     int i;
1827     char name[VOLSER_MAXVOLNAME + 1];
1828
1829     strcpy(name, vname);
1830     i = 0;
1831     while (name[i] == 'V' || name[i] == '0')
1832         i++;
1833
1834     name[11] = '\0';            /* smash the "." */
1835     return (atol(&name[i]));
1836 }
1837
1838 /*return the name of the next volume header in the directory associated with dirp and dp.
1839 *the volume id is  returned in volid, and volume header name is returned in volname*/
1840 GetNextVol(DIR * dirp, char *volname, afs_int32 * volid)
1841 {
1842     struct dirent *dp;
1843
1844     dp = readdir(dirp);         /*read next entry in the directory */
1845     if (dp) {
1846
1847         if ((dp->d_name[0] == 'V') && !strcmp(&(dp->d_name[11]), VHDREXT)) {
1848             *volid = ExtractVolId(dp->d_name);
1849             strcpy(volname, dp->d_name);
1850             return 0;           /*return the name of the file representing a volume */
1851         } else {
1852             strcpy(volname, "");
1853             return 0;           /*volname doesnot represent a volume */
1854         }
1855     } else {
1856         strcpy(volname, "EOD");
1857         return 0;               /*end of directory */
1858     }
1859
1860 }
1861
1862 /*return the header information about the <volid> */
1863 afs_int32
1864 SAFSVolListOneVolume(acid, partid, volumeId, volumeInfo)
1865      struct rx_call *acid;
1866      afs_int32 volumeId, partid;
1867      volEntries *volumeInfo;
1868 {
1869     afs_int32 code;
1870
1871     code = VolListOneVolume(acid, partid, volumeId, volumeInfo);
1872     osi_auditU(acid, VS_Lst1VolEvent, code, AUD_LONG, volumeId, AUD_END);
1873     return code;
1874 }
1875
1876 afs_int32
1877 VolListOneVolume(acid, partid, volumeId, volumeInfo)
1878      struct rx_call *acid;
1879      afs_int32 volumeId, partid;
1880      volEntries *volumeInfo;
1881 {
1882     volintInfo *pntr;
1883     register struct Volume *tv;
1884     struct DiskPartition *partP;
1885     struct volser_trans *ttc;
1886     char pname[9], volname[20];
1887     afs_int32 error = 0;
1888     DIR *dirp;
1889     afs_int32 volid;
1890     int found = 0;
1891     unsigned int now;
1892
1893     volumeInfo->volEntries_val = (volintInfo *) malloc(sizeof(volintInfo));
1894     pntr = volumeInfo->volEntries_val;
1895     volumeInfo->volEntries_len = 1;
1896     if (GetPartName(partid, pname))
1897         return VOLSERILLEGAL_PARTITION;
1898     if (!(partP = VGetPartition(pname, 0)))
1899         return VOLSERILLEGAL_PARTITION;
1900     dirp = opendir(VPartitionPath(partP));
1901     if (dirp == NULL)
1902         return VOLSERILLEGAL_PARTITION;
1903     strcpy(volname, "");
1904     ttc = (struct volser_trans *)0;
1905     tv = (Volume *) 0;          /* volume not attached */
1906
1907     while (strcmp(volname, "EOD") && !found) {  /*while there are more volumes in the partition */
1908
1909         if (!strcmp(volname, "")) {     /* its not a volume, fetch next file */
1910             GetNextVol(dirp, volname, &volid);
1911             continue;           /*back to while loop */
1912         }
1913
1914         if (volid == volumeId) {        /*copy other things too */
1915             found = 1;
1916             IOMGR_Poll();       /*make sure that the client doesnot time out */
1917             ttc = NewTrans(volid, partid);
1918             if (!ttc) {
1919                 pntr->status = VBUSY;
1920                 pntr->volid = volid;
1921                 goto drop;
1922             }
1923             tv = VAttachVolumeByName(&error, pname, volname, V_READONLY);
1924             if (error) {
1925                 pntr->status = 0;       /*things are messed up */
1926                 strcpy(pntr->name, volname);
1927                 pntr->volid = volid;
1928                 Log("1 Volser: ListVolumes: Could not attach volume %u (%s:%s), error=%d\n", volid, pname, volname, error);
1929                 goto drop;
1930             }
1931             if (tv->header->diskstuff.destroyMe == DESTROY_ME) {
1932                 /*this volume will be salvaged */
1933                 pntr->status = 0;
1934                 strcpy(pntr->name, volname);
1935                 pntr->volid = volid;
1936                 Log("1 Volser: ListVolumes: Volume %u (%s) will be destroyed on next salvage\n", volid, volname);
1937                 goto drop;
1938             }
1939
1940             if (tv->header->diskstuff.needsSalvaged) {
1941                 /*this volume will be salvaged */
1942                 pntr->status = 0;
1943                 strcpy(pntr->name, volname);
1944                 pntr->volid = volid;
1945                 Log("1 Volser: ListVolumes: Volume %u (%s) needs to be salvaged\n", volid, volname);
1946                 goto drop;
1947             }
1948
1949             /*read in the relevant info */
1950             pntr->status = VOK; /*its ok */
1951             pntr->volid = tv->header->diskstuff.id;
1952             strcpy(pntr->name, tv->header->diskstuff.name);
1953             pntr->type = tv->header->diskstuff.type;    /*if ro volume */
1954             pntr->cloneID = tv->header->diskstuff.cloneId;      /*if rw volume */
1955             pntr->backupID = tv->header->diskstuff.backupId;
1956             pntr->parentID = tv->header->diskstuff.parentId;
1957             pntr->copyDate = tv->header->diskstuff.copyDate;
1958             pntr->inUse = tv->header->diskstuff.inUse;
1959             pntr->size = tv->header->diskstuff.diskused;
1960             pntr->needsSalvaged = tv->header->diskstuff.needsSalvaged;
1961             pntr->destroyMe = tv->header->diskstuff.destroyMe;
1962             pntr->maxquota = tv->header->diskstuff.maxquota;
1963             pntr->filecount = tv->header->diskstuff.filecount;
1964             now = FT_ApproxTime();
1965             if (now - tv->header->diskstuff.dayUseDate > OneDay)
1966                 pntr->dayUse = 0;
1967             else
1968                 pntr->dayUse = tv->header->diskstuff.dayUse;
1969             pntr->creationDate = tv->header->diskstuff.creationDate;
1970             pntr->accessDate = tv->header->diskstuff.accessDate;
1971             pntr->updateDate = tv->header->diskstuff.updateDate;
1972             pntr->backupDate = tv->header->diskstuff.backupDate;
1973             pntr->spare0 = tv->header->diskstuff.minquota;
1974             pntr->spare1 =
1975                 (long)tv->header->diskstuff.weekUse[0] +
1976                 (long)tv->header->diskstuff.weekUse[1] +
1977                 (long)tv->header->diskstuff.weekUse[2] +
1978                 (long)tv->header->diskstuff.weekUse[3] +
1979                 (long)tv->header->diskstuff.weekUse[4] +
1980                 (long)tv->header->diskstuff.weekUse[5] +
1981                 (long)tv->header->diskstuff.weekUse[6];
1982             pntr->flags = pntr->spare2 = pntr->spare3 = (long)0;
1983             VDetachVolume(&error, tv);  /*free the volume */
1984             tv = (Volume *) 0;
1985             if (error) {
1986                 pntr->status = 0;       /*things are messed up */
1987                 strcpy(pntr->name, volname);
1988                 Log("1 Volser: ListVolumes: Could not detach volume %s\n",
1989                     volname);
1990                 goto drop;
1991             }
1992         }
1993         GetNextVol(dirp, volname, &volid);
1994     }
1995   drop:
1996     if (tv) {
1997         VDetachVolume(&error, tv);
1998         tv = (Volume *) 0;
1999     }
2000     if (ttc) {
2001         DeleteTrans(ttc);
2002         ttc = (struct volser_trans *)0;
2003     }
2004
2005     closedir(dirp);
2006     if (found)
2007         return 0;
2008     else
2009         return ENODEV;
2010 }
2011
2012 /*------------------------------------------------------------------------
2013  * EXPORTED SAFSVolXListOneVolume
2014  *
2015  * Description:
2016  *      Returns extended info on volume a_volID on partition a_partID.
2017  *
2018  * Arguments:
2019  *      a_rxCidP       : Pointer to the Rx call we're performing.
2020  *      a_partID       : Partition for which we want the extended list.
2021  *      a_volID        : Volume ID we wish to know about.
2022  *      a_volumeXInfoP : Ptr to the extended info blob.
2023  *
2024  * Returns:
2025  *      0                       Successful operation
2026  *      VOLSERILLEGAL_PARTITION if we got a bogus partition ID
2027  *
2028  * Environment:
2029  *      Nothing interesting.
2030  *
2031  * Side Effects:
2032  *      As advertised.
2033  *------------------------------------------------------------------------*/
2034
2035 afs_int32
2036 SAFSVolXListOneVolume(a_rxCidP, a_partID, a_volID, a_volumeXInfoP)
2037      struct rx_call *a_rxCidP;
2038      afs_int32 a_partID;
2039      afs_int32 a_volID;
2040      volXEntries *a_volumeXInfoP;
2041 {
2042     afs_int32 code;
2043
2044     code = VolXListOneVolume(a_rxCidP, a_partID, a_volID, a_volumeXInfoP);
2045     osi_auditU(a_rxCidP, VS_XLst1VlEvent, code, AUD_LONG, a_volID, AUD_END);
2046     return code;
2047 }
2048
2049 afs_int32
2050 VolXListOneVolume(a_rxCidP, a_partID, a_volID, a_volumeXInfoP)
2051      struct rx_call *a_rxCidP;
2052      afs_int32 a_partID;
2053      afs_int32 a_volID;
2054      volXEntries *a_volumeXInfoP;
2055
2056 {                               /*SAFSVolXListOneVolume */
2057
2058     volintXInfo *xInfoP;        /*Ptr to the extended vol info */
2059     register struct Volume *tv; /*Volume ptr */
2060     struct volser_trans *ttc;   /*Volume transaction ptr */
2061     struct DiskPartition *partP;        /*Ptr to partition */
2062     char pname[9], volname[20]; /*Partition, volume names */
2063     afs_int32 error;            /*Error code */
2064     afs_int32 code;             /*Return code */
2065     DIR *dirp;                  /*Partition directory ptr */
2066     afs_int32 currVolID;        /*Current volume ID */
2067     int found = 0;              /*Did we find the volume we need? */
2068     struct VolumeDiskData *volDiskDataP;        /*Ptr to on-disk volume data */
2069     int numStatBytes;           /*Num stat bytes to copy per volume */
2070     unsigned int now;
2071
2072     /*
2073      * Set up our pointers for action, marking our structure to hold exactly
2074      * one entry.  Also, assume we'll fail in our quest.
2075      */
2076     a_volumeXInfoP->volXEntries_val =
2077         (volintXInfo *) malloc(sizeof(volintXInfo));
2078     xInfoP = a_volumeXInfoP->volXEntries_val;
2079     a_volumeXInfoP->volXEntries_len = 1;
2080     code = ENODEV;
2081
2082     /*
2083      * If the partition name we've been given is bad, bogue out.
2084      */
2085     if (GetPartName(a_partID, pname))
2086         return (VOLSERILLEGAL_PARTITION);
2087
2088     /*
2089      * Open the directory representing the given AFS parttion.  If we can't
2090      * do that, we lose.
2091      */
2092     if (!(partP = VGetPartition(pname, 0)))
2093         return VOLSERILLEGAL_PARTITION;
2094     dirp = opendir(VPartitionPath(partP));
2095     if (dirp == NULL)
2096         return (VOLSERILLEGAL_PARTITION);
2097
2098     /*
2099      * Sweep through the partition directory, looking for the desired entry.
2100      * First, of course, figure out how many stat bytes to copy out of each
2101      * volume.
2102      */
2103     numStatBytes =
2104         4 * ((2 * VOLINT_STATS_NUM_RWINFO_FIELDS) +
2105              (4 * VOLINT_STATS_NUM_TIME_FIELDS));
2106     strcpy(volname, "");
2107     ttc = (struct volser_trans *)0;     /*No transaction yet */
2108     tv = (Volume *) 0;          /*Volume not yet attached */
2109
2110     while (strcmp(volname, "EOD") && !found) {
2111         /*
2112          * If this is not a volume, move on to the next entry in the
2113          * partition's directory.
2114          */
2115         if (!strcmp(volname, "")) {
2116             GetNextVol(dirp, volname, &currVolID);
2117             continue;
2118         }
2119
2120         if (currVolID == a_volID) {
2121             /*
2122              * We found the volume entry we're interested.  Pull out the
2123              * extended information, remembering to poll (so that the client
2124              * doesn't time out) and to set up a transaction on the volume.
2125              */
2126             found = 1;
2127             IOMGR_Poll();
2128             ttc = NewTrans(currVolID, a_partID);
2129             if (!ttc) {
2130                 /*
2131                  * Couldn't get a transaction on this volume; let our caller
2132                  * know it's busy.
2133                  */
2134                 xInfoP->status = VBUSY;
2135                 xInfoP->volid = currVolID;
2136                 goto drop;
2137             }
2138
2139             /*
2140              * Attach the volume, give up on the volume if we can't.
2141              */
2142             tv = VAttachVolumeByName(&error, pname, volname, V_READONLY);
2143             if (error) {
2144                 xInfoP->status = 0;     /*things are messed up */
2145                 strcpy(xInfoP->name, volname);
2146                 xInfoP->volid = currVolID;
2147                 Log("1 Volser: XListOneVolume: Could not attach volume %u\n",
2148                     currVolID);
2149                 goto drop;
2150             }
2151
2152             /*
2153              * Also bag out on this volume if it's been marked as needing a
2154              * salvage or to-be-destroyed.
2155              */
2156             volDiskDataP = &(tv->header->diskstuff);
2157             if (volDiskDataP->destroyMe == DESTROY_ME) {
2158                 xInfoP->status = 0;
2159                 strcpy(xInfoP->name, volname);
2160                 xInfoP->volid = currVolID;
2161                 Log("1 Volser: XListOneVolume: Volume %u will be destroyed on next salvage\n", currVolID);
2162                 goto drop;
2163             }
2164
2165             if (volDiskDataP->needsSalvaged) {
2166                 xInfoP->status = 0;
2167                 strcpy(xInfoP->name, volname);
2168                 xInfoP->volid = currVolID;
2169                 Log("1 Volser: XListOneVolume: Volume %u needs to be salvaged\n", currVolID);
2170                 goto drop;
2171             }
2172
2173             /*
2174              * Pull out the desired info and stuff it into the area we'll be
2175              * returning to our caller.
2176              */
2177             strcpy(xInfoP->name, volDiskDataP->name);
2178             xInfoP->volid = volDiskDataP->id;
2179             xInfoP->type = volDiskDataP->type;
2180             xInfoP->backupID = volDiskDataP->backupId;
2181             xInfoP->parentID = volDiskDataP->parentId;
2182             xInfoP->cloneID = volDiskDataP->cloneId;
2183             xInfoP->status = VOK;
2184             xInfoP->copyDate = volDiskDataP->copyDate;
2185             xInfoP->inUse = volDiskDataP->inUse;
2186             xInfoP->creationDate = volDiskDataP->creationDate;
2187             xInfoP->accessDate = volDiskDataP->accessDate;
2188             xInfoP->updateDate = volDiskDataP->updateDate;
2189             xInfoP->backupDate = volDiskDataP->backupDate;
2190             now = FT_ApproxTime();
2191             if (now - volDiskDataP->dayUseDate > OneDay)
2192                 xInfoP->dayUse = 0;
2193             else
2194                 xInfoP->dayUse = volDiskDataP->dayUse;
2195             xInfoP->filecount = volDiskDataP->filecount;
2196             xInfoP->maxquota = volDiskDataP->maxquota;
2197             xInfoP->size = volDiskDataP->diskused;
2198
2199             /*
2200              * Copy out the stat fields in a single operation.
2201              */
2202             memcpy((char *)&(xInfoP->stat_reads[0]),
2203                    (char *)&(volDiskDataP->stat_reads[0]), numStatBytes);
2204
2205             /*
2206              * We're done copying.  Detach the volume and iterate (at this
2207              * point, since we found our volume, we'll then drop out of the
2208              * loop).
2209              */
2210             VDetachVolume(&error, tv);
2211             tv = (Volume *) 0;
2212             if (error) {
2213                 xInfoP->status = 0;
2214                 strcpy(xInfoP->name, volname);
2215                 Log("1 Volser: XListOneVolumes Couldn't detach volume %s\n",
2216                     volname);
2217                 goto drop;
2218             }
2219
2220             /*
2221              * At this point, we're golden.
2222              */
2223             code = 0;
2224         }                       /*Found desired volume */
2225         GetNextVol(dirp, volname, &currVolID);
2226     }
2227
2228     /*
2229      * Drop the transaction we have for this volume.
2230      */
2231   drop:
2232     if (tv) {
2233         VDetachVolume(&error, tv);
2234         tv = (Volume *) 0;
2235     }
2236     if (ttc) {
2237         DeleteTrans(ttc);
2238         ttc = (struct volser_trans *)0;
2239     }
2240
2241     /*
2242      * Clean up before going to dinner: close the partition directory,
2243      * return the proper value.
2244      */
2245     closedir(dirp);
2246     return (code);
2247
2248 }                               /*SAFSVolXListOneVolume */
2249
2250 /*returns all the volumes on partition partid. If flags = 1 then all the 
2251 * relevant info about the volumes  is also returned */
2252 afs_int32
2253 SAFSVolListVolumes(acid, partid, flags, volumeInfo)
2254      struct rx_call *acid;
2255      afs_int32 flags, partid;
2256      volEntries *volumeInfo;
2257 {
2258     afs_int32 code;
2259
2260     code = VolListVolumes(acid, partid, flags, volumeInfo);
2261     osi_auditU(acid, VS_ListVolEvent, code, AUD_END);
2262     return code;
2263 }
2264
2265 afs_int32
2266 VolListVolumes(acid, partid, flags, volumeInfo)
2267      struct rx_call *acid;
2268      afs_int32 flags, partid;
2269      volEntries *volumeInfo;
2270 {
2271     volintInfo *pntr;
2272     register struct Volume *tv;
2273     struct DiskPartition *partP;
2274     struct volser_trans *ttc;
2275     afs_int32 allocSize = 1000; /*to be changed to a larger figure */
2276     char pname[9], volname[20];
2277     afs_int32 error = 0;
2278     DIR *dirp;
2279     afs_int32 volid;
2280     unsigned int now;
2281
2282     volumeInfo->volEntries_val =
2283         (volintInfo *) malloc(allocSize * sizeof(volintInfo));
2284     pntr = volumeInfo->volEntries_val;
2285     volumeInfo->volEntries_len = 0;
2286     if (GetPartName(partid, pname))
2287         return VOLSERILLEGAL_PARTITION;
2288     if (!(partP = VGetPartition(pname, 0)))
2289         return VOLSERILLEGAL_PARTITION;
2290     dirp = opendir(VPartitionPath(partP));
2291     if (dirp == NULL)
2292         return VOLSERILLEGAL_PARTITION;
2293     strcpy(volname, "");
2294     while (strcmp(volname, "EOD")) {    /*while there are more partitions in the partition */
2295         ttc = (struct volser_trans *)0; /* new one for each pass */
2296         tv = (Volume *) 0;      /* volume not attached */
2297
2298         if (!strcmp(volname, "")) {     /* its not a volume, fetch next file */
2299             GetNextVol(dirp, volname, &volid);
2300             continue;           /*back to while loop */
2301         }
2302
2303         if (flags) {            /*copy other things too */
2304             IOMGR_Poll();       /*make sure that the client doesnot time out */
2305             ttc = NewTrans(volid, partid);
2306             if (!ttc) {
2307                 pntr->status = VBUSY;
2308                 pntr->volid = volid;
2309                 goto drop;
2310             }
2311             tv = VAttachVolumeByName(&error, pname, volname, V_READONLY);
2312             if (error) {
2313                 pntr->status = 0;       /*things are messed up */
2314                 strcpy(pntr->name, volname);
2315                 pntr->volid = volid;
2316                 Log("1 Volser: ListVolumes: Could not attach volume %u (%s) error=%d\n", volid, volname, error);
2317                 goto drop;
2318             }
2319             if (tv->header->diskstuff.needsSalvaged) {
2320                 /*this volume will be salvaged */
2321                 pntr->status = 0;
2322                 strcpy(pntr->name, volname);
2323                 pntr->volid = volid;
2324                 Log("1 Volser: ListVolumes: Volume %u (%s) needs to be salvaged\n", volid, volname);
2325                 goto drop;
2326             }
2327
2328             if (tv->header->diskstuff.destroyMe == DESTROY_ME) {
2329                 /*this volume will be salvaged */
2330                 goto drop2;
2331             }
2332             /*read in the relevant info */
2333             pntr->status = VOK; /*its ok */
2334             pntr->volid = tv->header->diskstuff.id;
2335             strcpy(pntr->name, tv->header->diskstuff.name);
2336             pntr->type = tv->header->diskstuff.type;    /*if ro volume */
2337             pntr->cloneID = tv->header->diskstuff.cloneId;      /*if rw volume */
2338             pntr->backupID = tv->header->diskstuff.backupId;
2339             pntr->parentID = tv->header->diskstuff.parentId;
2340             pntr->copyDate = tv->header->diskstuff.copyDate;
2341             pntr->inUse = tv->header->diskstuff.inUse;
2342             pntr->size = tv->header->diskstuff.diskused;
2343             pntr->needsSalvaged = tv->header->diskstuff.needsSalvaged;
2344             pntr->maxquota = tv->header->diskstuff.maxquota;
2345             pntr->filecount = tv->header->diskstuff.filecount;
2346             now = FT_ApproxTime();
2347             if (now - tv->header->diskstuff.dayUseDate > OneDay)
2348                 pntr->dayUse = 0;
2349             else
2350                 pntr->dayUse = tv->header->diskstuff.dayUse;
2351             pntr->creationDate = tv->header->diskstuff.creationDate;
2352             pntr->accessDate = tv->header->diskstuff.accessDate;
2353             pntr->updateDate = tv->header->diskstuff.updateDate;
2354             pntr->backupDate = tv->header->diskstuff.backupDate;
2355             pntr->spare0 = tv->header->diskstuff.minquota;
2356             pntr->spare1 =
2357                 (long)tv->header->diskstuff.weekUse[0] +
2358                 (long)tv->header->diskstuff.weekUse[1] +
2359                 (long)tv->header->diskstuff.weekUse[2] +
2360                 (long)tv->header->diskstuff.weekUse[3] +
2361                 (long)tv->header->diskstuff.weekUse[4] +
2362                 (long)tv->header->diskstuff.weekUse[5] +
2363                 (long)tv->header->diskstuff.weekUse[6];
2364             pntr->flags = pntr->spare2 = pntr->spare3 = (long)0;
2365             VDetachVolume(&error, tv);  /*free the volume */
2366             tv = (Volume *) 0;
2367             if (error) {
2368                 pntr->status = 0;       /*things are messed up */
2369                 strcpy(pntr->name, volname);
2370                 Log("1 Volser: ListVolumes: Could not detach volume %s\n",
2371                     volname);
2372                 goto drop;
2373             }
2374         } else {
2375             pntr->volid = volid;
2376             /*just volids are needed */
2377         }
2378
2379       drop:
2380         if (ttc) {
2381             DeleteTrans(ttc);
2382             ttc = (struct volser_trans *)0;
2383         }
2384         pntr++;
2385         volumeInfo->volEntries_len += 1;
2386         if ((allocSize - volumeInfo->volEntries_len) < 5) {
2387             /*running out of space, allocate more space */
2388             allocSize = (allocSize * 3) / 2;
2389             pntr =
2390                 (volintInfo *) realloc((char *)volumeInfo->volEntries_val,
2391                                        allocSize * sizeof(volintInfo));
2392             if (pntr == NULL) {
2393                 if (tv) {
2394                     VDetachVolume(&error, tv);
2395                     tv = (Volume *) 0;
2396                 }
2397                 if (ttc) {
2398                     DeleteTrans(ttc);
2399                     ttc = (struct volser_trans *)0;
2400                 }
2401                 closedir(dirp);
2402                 return VOLSERNO_MEMORY;
2403             }
2404             volumeInfo->volEntries_val = pntr;  /* point to new block */
2405             /* set pntr to the right position */
2406             pntr = volumeInfo->volEntries_val + volumeInfo->volEntries_len;
2407
2408         }
2409
2410       drop2:
2411         if (tv) {
2412             VDetachVolume(&error, tv);
2413             tv = (Volume *) 0;
2414         }
2415         if (ttc) {
2416             DeleteTrans(ttc);
2417             ttc = (struct volser_trans *)0;
2418         }
2419         GetNextVol(dirp, volname, &volid);
2420
2421     }
2422     closedir(dirp);
2423     if (ttc)
2424         DeleteTrans(ttc);
2425
2426     return 0;
2427 }
2428
2429 /*------------------------------------------------------------------------
2430  * EXPORTED SAFSVolXListVolumes
2431  *
2432  * Description:
2433  *      Returns all the volumes on partition a_partID.  If a_flags
2434  *      is set to 1, then all the relevant extended volume information
2435  *      is also returned.
2436  *
2437  * Arguments:
2438  *      a_rxCidP       : Pointer to the Rx call we're performing.
2439  *      a_partID       : Partition for which we want the extended list.
2440  *      a_flags        : Various flags.
2441  *      a_volumeXInfoP : Ptr to the extended info blob.
2442  *
2443  * Returns:
2444  *      0                       Successful operation
2445  *      VOLSERILLEGAL_PARTITION if we got a bogus partition ID
2446  *      VOLSERNO_MEMORY         if we ran out of memory allocating
2447  *                              our return blob
2448  *
2449  * Environment:
2450  *      Nothing interesting.
2451  *
2452  * Side Effects:
2453  *      As advertised.
2454  *------------------------------------------------------------------------*/
2455
2456 afs_int32
2457 SAFSVolXListVolumes(a_rxCidP, a_partID, a_flags, a_volumeXInfoP)
2458      struct rx_call *a_rxCidP;
2459      afs_int32 a_partID;
2460      afs_int32 a_flags;
2461      volXEntries *a_volumeXInfoP;
2462 {
2463     afs_int32 code;
2464
2465     code = VolXListVolumes(a_rxCidP, a_partID, a_flags, a_volumeXInfoP);
2466     osi_auditU(a_rxCidP, VS_XLstVolEvent, code, AUD_END);
2467     return code;
2468 }
2469
2470 afs_int32
2471 VolXListVolumes(a_rxCidP, a_partID, a_flags, a_volumeXInfoP)
2472      struct rx_call *a_rxCidP;
2473      afs_int32 a_partID;
2474      afs_int32 a_flags;
2475      volXEntries *a_volumeXInfoP;
2476
2477 {                               /*SAFSVolXListVolumes */
2478
2479     volintXInfo *xInfoP;        /*Ptr to the extended vol info */
2480     register struct Volume *tv; /*Volume ptr */
2481     struct DiskPartition *partP;        /*Ptr to partition */
2482     struct volser_trans *ttc;   /*Volume transaction ptr */
2483     afs_int32 allocSize = 1000; /*To be changed to a larger figure */
2484     char pname[9], volname[20]; /*Partition, volume names */
2485     afs_int32 error = 0;        /*Return code */
2486     DIR *dirp;                  /*Partition directory ptr */
2487     afs_int32 volid;            /*Current volume ID */
2488     struct VolumeDiskData *volDiskDataP;        /*Ptr to on-disk volume data */
2489     int numStatBytes;           /*Num stat bytes to copy per volume */
2490     unsigned int now;
2491
2492     /*
2493      * Allocate a large array of extended volume info structures, then
2494      * set it up for action.
2495      */
2496     a_volumeXInfoP->volXEntries_val =
2497         (volintXInfo *) malloc(allocSize * sizeof(volintXInfo));
2498     xInfoP = a_volumeXInfoP->volXEntries_val;
2499     a_volumeXInfoP->volXEntries_len = 0;
2500
2501     /*
2502      * If the partition name we've been given is bad, bogue out.
2503      */
2504     if (GetPartName(a_partID, pname))
2505         return (VOLSERILLEGAL_PARTITION);
2506
2507     /*
2508      * Open the directory representing the given AFS parttion.  If we can't
2509      * do that, we lose.
2510      */
2511     if (!(partP = VGetPartition(pname, 0)))
2512         return VOLSERILLEGAL_PARTITION;
2513     dirp = opendir(VPartitionPath(partP));
2514     if (dirp == NULL)
2515         return (VOLSERILLEGAL_PARTITION);
2516
2517     /*
2518      * Sweep through the partition directory, acting on each entry.  First,
2519      * of course, figure out how many stat bytes to copy out of each volume.
2520      */
2521     numStatBytes =
2522         4 * ((2 * VOLINT_STATS_NUM_RWINFO_FIELDS) +
2523              (4 * VOLINT_STATS_NUM_TIME_FIELDS));
2524     strcpy(volname, "");
2525     while (strcmp(volname, "EOD")) {
2526         ttc = (struct volser_trans *)0; /*New one for each pass */
2527         tv = (Volume *) 0;      /*Volume not yet attached */
2528
2529         /*
2530          * If this is not a volume, move on to the next entry in the
2531          * partition's directory.
2532          */
2533         if (!strcmp(volname, "")) {
2534             GetNextVol(dirp, volname, &volid);
2535             continue;
2536         }
2537
2538         if (a_flags) {
2539             /*
2540              * Full info about the volume desired.  Poll to make sure the
2541              * client doesn't time out, then start up a new transaction.
2542              */
2543             IOMGR_Poll();
2544             ttc = NewTrans(volid, a_partID);
2545             if (!ttc) {
2546                 /*
2547                  * Couldn't get a transaction on this volume; let our caller
2548                  * know it's busy.
2549                  */
2550                 xInfoP->status = VBUSY;
2551                 xInfoP->volid = volid;
2552                 goto drop;
2553             }
2554
2555             /*
2556              * Attach the volume, give up on this volume if we can't.
2557              */
2558             tv = VAttachVolumeByName(&error, pname, volname, V_READONLY);
2559             if (error) {
2560                 xInfoP->status = 0;     /*things are messed up */
2561                 strcpy(xInfoP->name, volname);
2562                 xInfoP->volid = volid;
2563                 Log("1 Volser: XListVolumes: Could not attach volume %u\n",
2564                     volid);
2565                 goto drop;
2566             }
2567
2568             /*
2569              * Also bag out on this volume if it's been marked as needing a
2570              * salvage or to-be-destroyed.
2571              */
2572             volDiskDataP = &(tv->header->diskstuff);
2573             if (volDiskDataP->needsSalvaged) {
2574                 xInfoP->status = 0;
2575                 strcpy(xInfoP->name, volname);
2576                 xInfoP->volid = volid;
2577                 Log("1 Volser: XListVolumes: Volume %u needs to be salvaged\n", volid);
2578                 goto drop;
2579             }
2580
2581             if (volDiskDataP->destroyMe == DESTROY_ME)
2582                 goto drop2;
2583
2584             /*
2585              * Pull out the desired info and stuff it into the area we'll be
2586              * returning to our caller.
2587              */
2588             strcpy(xInfoP->name, volDiskDataP->name);
2589             xInfoP->volid = volDiskDataP->id;
2590             xInfoP->type = volDiskDataP->type;
2591             xInfoP->backupID = volDiskDataP->backupId;
2592             xInfoP->parentID = volDiskDataP->parentId;
2593             xInfoP->cloneID = volDiskDataP->cloneId;
2594             xInfoP->status = VOK;
2595             xInfoP->copyDate = volDiskDataP->copyDate;
2596             xInfoP->inUse = volDiskDataP->inUse;
2597             xInfoP->creationDate = volDiskDataP->creationDate;
2598             xInfoP->accessDate = volDiskDataP->accessDate;
2599             xInfoP->updateDate = volDiskDataP->updateDate;
2600             xInfoP->backupDate = volDiskDataP->backupDate;
2601             now = FT_ApproxTime();
2602             if (now - volDiskDataP->dayUseDate > OneDay)
2603                 xInfoP->dayUse = 0;
2604             else
2605                 xInfoP->dayUse = volDiskDataP->dayUse;
2606             xInfoP->filecount = volDiskDataP->filecount;
2607             xInfoP->maxquota = volDiskDataP->maxquota;
2608             xInfoP->size = volDiskDataP->diskused;
2609
2610             /*
2611              * Copy out the stat fields in a single operation.
2612              */
2613             memcpy((char *)&(xInfoP->stat_reads[0]),
2614                    (char *)&(volDiskDataP->stat_reads[0]), numStatBytes);
2615
2616             /*
2617              * We're done copying.  Detach the volume and iterate.
2618              */
2619             VDetachVolume(&error, tv);
2620             tv = (Volume *) 0;
2621             if (error) {
2622                 xInfoP->status = 0;
2623                 strcpy(xInfoP->name, volname);
2624                 Log("1 Volser: XListVolumes: Could not detach volume %s\n",
2625                     volname);
2626                 goto drop;
2627             }
2628         } /*Full contents desired */
2629         else
2630             /*
2631              * Just volume IDs are needed.
2632              */
2633             xInfoP->volid = volid;
2634
2635       drop:
2636         /*
2637          * Drop the transaction we have for this volume.
2638          */
2639         if (ttc) {
2640             DeleteTrans(ttc);
2641             ttc = (struct volser_trans *)0;
2642         }
2643
2644         /*
2645          * Bump the pointer in the data area we're building, along with
2646          * the count of the number of entries it contains.
2647          */
2648         xInfoP++;
2649         (a_volumeXInfoP->volXEntries_len)++;
2650         if ((allocSize - a_volumeXInfoP->volXEntries_len) < 5) {
2651             /*
2652              * We're running out of space in the area we've built.  Grow it.
2653              */
2654             allocSize = (allocSize * 3) / 2;
2655             xInfoP = (volintXInfo *)
2656                 realloc((char *)a_volumeXInfoP->volXEntries_val,
2657                         (allocSize * sizeof(volintXInfo)));
2658             if (xInfoP == NULL) {
2659                 /*
2660                  * Bummer, no memory. Bag it, tell our caller what went wrong.
2661                  */
2662                 if (tv) {
2663                     VDetachVolume(&error, tv);
2664                     tv = (Volume *) 0;
2665                 }
2666                 if (ttc) {
2667                     DeleteTrans(ttc);
2668                     ttc = (struct volser_trans *)0;
2669                 }
2670                 closedir(dirp);
2671                 return (VOLSERNO_MEMORY);
2672             }
2673
2674             /*
2675              * Memory reallocation worked.  Correct our pointers so they
2676              * now point to the new block and the current open position within
2677              * the new block.
2678              */
2679             a_volumeXInfoP->volXEntries_val = xInfoP;
2680             xInfoP =
2681                 a_volumeXInfoP->volXEntries_val +
2682                 a_volumeXInfoP->volXEntries_len;
2683         }
2684         /*Need more space */
2685       drop2:
2686         /*
2687          * Detach our current volume and the transaction on it, then move on
2688          * to the next volume in the partition directory.
2689          */
2690         if (tv) {
2691             VDetachVolume(&error, tv);
2692             tv = (Volume *) 0;
2693         }
2694         if (ttc) {
2695             DeleteTrans(ttc);
2696             ttc = (struct volser_trans *)0;
2697         }
2698         GetNextVol(dirp, volname, &volid);
2699     }                           /*Sweep through the partition directory */
2700
2701     /*
2702      * We've examined all entries in the partition directory.  Close it,
2703      * delete our transaction (if any), and go home happy.
2704      */
2705     closedir(dirp);
2706     if (ttc)
2707         DeleteTrans(ttc);
2708     return (0);
2709
2710 }                               /*SAFSVolXListVolumes */
2711
2712 /*this call is used to monitor the status of volser for debugging purposes.
2713  *information about all the active transactions is returned in transInfo*/
2714 afs_int32
2715 SAFSVolMonitor(acid, transInfo)
2716      struct rx_call *acid;
2717      transDebugEntries *transInfo;
2718 {
2719     afs_int32 code;
2720
2721     code = VolMonitor(acid, transInfo);
2722     osi_auditU(acid, VS_MonitorEvent, code, AUD_END);
2723     return code;
2724 }
2725
2726 afs_int32
2727 VolMonitor(acid, transInfo)
2728      struct rx_call *acid;
2729      transDebugEntries *transInfo;
2730 {
2731     transDebugInfo *pntr;
2732     afs_int32 allocSize = 50;
2733     struct volser_trans *tt, *allTrans;
2734
2735     transInfo->transDebugEntries_val =
2736         (transDebugInfo *) malloc(allocSize * sizeof(transDebugInfo));
2737     pntr = transInfo->transDebugEntries_val;
2738     transInfo->transDebugEntries_len = 0;
2739     allTrans = TransList();
2740     if (allTrans == (struct volser_trans *)0)
2741         return 0;               /*no active transactions */
2742     for (tt = allTrans; tt; tt = tt->next) {    /*copy relevant info into pntr */
2743         pntr->tid = tt->tid;
2744         pntr->time = tt->time;
2745         pntr->creationTime = tt->creationTime;
2746         pntr->returnCode = tt->returnCode;
2747         pntr->volid = tt->volid;
2748         pntr->partition = tt->partition;
2749         pntr->iflags = tt->iflags;
2750         pntr->vflags = tt->vflags;
2751         pntr->tflags = tt->tflags;
2752         strcpy(pntr->lastProcName, tt->lastProcName);
2753         pntr->callValid = 0;
2754         if (tt->rxCallPtr) {    /*record call related info */
2755             pntr->callValid = 1;
2756             pntr->readNext = tt->rxCallPtr->rnext;
2757             pntr->transmitNext = tt->rxCallPtr->tnext;
2758             pntr->lastSendTime = tt->rxCallPtr->lastSendTime;
2759             pntr->lastReceiveTime = tt->rxCallPtr->lastReceiveTime;
2760         }
2761         pntr++;
2762         transInfo->transDebugEntries_len += 1;
2763         if ((allocSize - transInfo->transDebugEntries_len) < 5) {       /*alloc some more space */
2764             allocSize = (allocSize * 3) / 2;
2765             pntr =
2766                 (transDebugInfo *) realloc((char *)transInfo->
2767                                            transDebugEntries_val,
2768                                            allocSize *
2769                                            sizeof(transDebugInfo));
2770             transInfo->transDebugEntries_val = pntr;
2771             pntr =
2772                 transInfo->transDebugEntries_val +
2773                 transInfo->transDebugEntries_len;
2774             /*set pntr to right position */
2775         }
2776
2777     }
2778
2779     return 0;
2780 }
2781
2782 afs_int32
2783 SAFSVolSetIdsTypes(acid, atid, name, type, pId, cloneId, backupId)
2784      struct rx_call *acid;
2785      afs_int32 type, pId, cloneId, backupId, atid;
2786      char name[];
2787 {
2788     afs_int32 code;
2789
2790     code = VolSetIdsTypes(acid, atid, name, type, pId, cloneId, backupId);
2791     osi_auditU(acid, VS_SetIdTyEvent, code, AUD_LONG, atid, AUD_STR, name,
2792                AUD_STR, type, AUD_LONG, pId, AUD_LONG, cloneId, AUD_LONG,
2793                backupId, AUD_END);
2794     return code;
2795 }
2796
2797 afs_int32
2798 VolSetIdsTypes(acid, atid, name, type, pId, cloneId, backupId)
2799      struct rx_call *acid;
2800      afs_int32 type, pId, cloneId, backupId, atid;
2801      char name[];
2802 {
2803     struct Volume *tv;
2804     afs_int32 error = 0;
2805     register struct volser_trans *tt;
2806     char caller[MAXKTCNAMELEN];
2807
2808     if (strlen(name) > 31)
2809         return VOLSERBADNAME;
2810     if (!afsconf_SuperUser(tdir, acid, caller))
2811         return VOLSERBAD_ACCESS;        /*not a super user */
2812     /* find the trans */
2813     tt = FindTrans(atid);
2814     if (!tt)
2815         return ENOENT;
2816     if (tt->vflags & VTDeleted) {
2817         Log("1 Volser: VolSetIds: volume %u has been deleted \n", tt->volid);
2818         TRELE(tt);
2819         return ENOENT;
2820     }
2821     strcpy(tt->lastProcName, "SetIdsTypes");
2822     tt->rxCallPtr = acid;
2823     tv = tt->volume;
2824
2825     V_type(tv) = type;
2826     V_backupId(tv) = backupId;
2827     V_cloneId(tv) = cloneId;
2828     V_parentId(tv) = pId;
2829     strcpy((&V_disk(tv))->name, name);
2830     VUpdateVolume(&error, tv);
2831     if (error) {
2832         Log("1 Volser: SetIdsTypes: VUpdate failed code %d\n", error);
2833         LogError(error);
2834         goto fail;
2835     }
2836     tt->rxCallPtr = (struct rx_call *)0;
2837     if (TRELE(tt) && !error)
2838         return VOLSERTRELE_ERROR;
2839
2840     return error;
2841   fail:
2842     tt->rxCallPtr = (struct rx_call *)0;
2843     if (TRELE(tt) && !error)
2844         return VOLSERTRELE_ERROR;
2845     return error;
2846 }
2847
2848 afs_int32
2849 SAFSVolSetDate(acid, atid, cdate)
2850      struct rx_call *acid;
2851      afs_int32 atid, cdate;
2852 {
2853     afs_int32 code;
2854
2855     code = VolSetDate(acid, atid, cdate);
2856     osi_auditU(acid, VS_SetDateEvent, code, AUD_LONG, atid, AUD_LONG, cdate,
2857                AUD_END);
2858     return code;
2859 }
2860
2861 afs_int32
2862 VolSetDate(acid, atid, cdate)
2863      struct rx_call *acid;
2864      afs_int32 atid, cdate;
2865 {
2866     struct Volume *tv;
2867     afs_int32 error = 0;
2868     register struct volser_trans *tt;
2869     char caller[MAXKTCNAMELEN];
2870
2871     if (!afsconf_SuperUser(tdir, acid, caller))
2872         return VOLSERBAD_ACCESS;        /*not a super user */
2873     /* find the trans */
2874     tt = FindTrans(atid);
2875     if (!tt)
2876         return ENOENT;
2877     if (tt->vflags & VTDeleted) {
2878         Log("1 Volser: VolSetDate: volume %u has been deleted \n", tt->volid);
2879         TRELE(tt);
2880         return ENOENT;
2881     }
2882     strcpy(tt->lastProcName, "SetDate");
2883     tt->rxCallPtr = acid;
2884     tv = tt->volume;
2885
2886     V_creationDate(tv) = cdate;
2887     VUpdateVolume(&error, tv);
2888     if (error) {
2889         Log("1 Volser: SetDate: VUpdate failed code %d\n", error);
2890         LogError(error);
2891         goto fail;
2892     }
2893     tt->rxCallPtr = (struct rx_call *)0;
2894     if (TRELE(tt) && !error)
2895         return VOLSERTRELE_ERROR;
2896
2897     return error;
2898   fail:
2899     tt->rxCallPtr = (struct rx_call *)0;
2900     if (TRELE(tt) && !error)
2901         return VOLSERTRELE_ERROR;
2902     return error;
2903 }
2904
2905 #ifdef AFS_NAMEI_ENV
2906 /* 
2907  * Inode number format  (from namei_ops.c): 
2908  * low 26 bits - vnode number - all 1's if volume special file.
2909  * next 3 bits - tag
2910  * next 3 bits spare (0's)
2911  * high 32 bits - uniquifier (regular) or type if spare
2912  */
2913 #define NAMEI_VNODEMASK    0x003ffffff
2914 #define NAMEI_TAGMASK      0x7
2915 #define NAMEI_TAGSHIFT     26
2916 #define NAMEI_UNIQMASK     0xffffffff
2917 #define NAMEI_UNIQSHIFT    32
2918 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
2919 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
2920 #endif /* AFS_NAMEI_ENV */
2921
2922 afs_int32
2923 SAFSVolConvertROtoRWvolume(acid, partId, volumeId)
2924      struct rx_call *acid;
2925      afs_int32 partId;
2926      afs_int32 volumeId;
2927 {
2928 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
2929     DIR *dirp;
2930     char pname[16];
2931     char volname[20];
2932     afs_int32 error = 0;
2933     afs_int32 volid;
2934     int found = 0;
2935     char caller[MAXKTCNAMELEN];
2936     char headername[16];
2937     char opath[256];
2938     char npath[256];
2939     struct VolumeDiskHeader h;
2940     int fd;
2941     IHandle_t *ih;
2942     Inode ino;
2943     struct DiskPartition *dp;
2944
2945     if (!afsconf_SuperUser(tdir, acid, caller))
2946         return VOLSERBAD_ACCESS;        /*not a super user */
2947     if (GetPartName(partId, pname))
2948         return VOLSERILLEGAL_PARTITION;
2949     dirp = opendir(pname);
2950     if (dirp == NULL)
2951         return VOLSERILLEGAL_PARTITION;
2952     strcpy(volname, "");
2953
2954     while (strcmp(volname, "EOD") && !found) {  /*while there are more volumes in the partition */
2955         GetNextVol(dirp, volname, &volid);
2956         if (strcmp(volname, "")) {      /* its a volume */
2957             if (volid == volumeId)
2958                 found = 1;
2959         }
2960     }
2961     if (!found)
2962         return ENOENT;
2963     (void)afs_snprintf(headername, sizeof headername, VFORMAT, volumeId);
2964     (void)afs_snprintf(opath, sizeof opath, "%s/%s", pname, headername);
2965     fd = open(opath, O_RDONLY);
2966     if (fd < 0) {
2967         Log("1 SAFS_VolConvertROtoRWvolume: Couldn't open header for RO-volume %lu.\n", volumeId);
2968         return ENOENT;
2969     }
2970     if (read(fd, &h, sizeof(h)) != sizeof(h)) {
2971         Log("1 SAFS_VolConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n", volumeId);
2972         close(fd);
2973         return EIO;
2974     }
2975     close(fd);
2976     FSYNC_askfs(volumeId, pname, FSYNC_RESTOREVOLUME, 0);
2977
2978     for (dp = DiskPartitionList; dp && strcmp(dp->name, pname);
2979          dp = dp->next);
2980     if (!dp) {
2981         Log("1 SAFS_VolConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
2982         return EIO;
2983     }
2984     ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
2985     IH_INIT(ih, dp->device, h.parent, ino);
2986
2987     error = namei_ConvertROtoRWvolume(ih, volumeId);
2988     if (error)
2989         return error;
2990     h.id = h.parent;
2991     h.volumeInfo_hi = h.id;
2992     h.smallVnodeIndex_hi = h.id;
2993     h.largeVnodeIndex_hi = h.id;
2994     h.linkTable_hi = h.id;
2995     (void)afs_snprintf(headername, sizeof headername, VFORMAT, h.id);
2996     (void)afs_snprintf(npath, sizeof npath, "%s/%s", pname, headername);
2997     fd = open(npath, O_CREAT | O_EXCL | O_RDWR, 0644);
2998     if (fd < 0) {
2999         Log("1 SAFS_VolConvertROtoRWvolume: Couldn't create header for RW-volume %lu.\n", h.id);
3000         return EIO;
3001     }
3002     if (write(fd, &h, sizeof(h)) != sizeof(h)) {
3003         Log("1 SAFS_VolConvertROtoRWvolume: Couldn't write header for RW-volume %lu.\n", h.id);
3004         close(fd);
3005         return EIO;
3006     }
3007     close(fd);
3008     if (unlink(opath) < 0) {
3009         Log("1 SAFS_VolConvertROtoRWvolume: Couldn't unlink RO header, error = %d\n", error);
3010     }
3011     FSYNC_askfs(volumeId, pname, FSYNC_DONE, 0);
3012     FSYNC_askfs(h.id, pname, FSYNC_ON, 0);
3013     return 0;
3014 #else /* AFS_NAMEI_ENV */
3015     return EINVAL;
3016 #endif /* AFS_NAMEI_ENV */
3017 }
3018
3019 afs_int32
3020 SAFSVolGetSize(acid, fromTrans, fromDate, size)
3021      struct rx_call *acid;
3022      afs_int32 fromTrans;
3023      afs_int32 fromDate;
3024      register struct volintSize *size;
3025 {
3026     int code = 0;
3027     register struct volser_trans *tt;
3028     char caller[MAXKTCNAMELEN];
3029
3030     if (!afsconf_SuperUser(tdir, acid, caller))
3031         return VOLSERBAD_ACCESS;        /*not a super user */
3032     tt = FindTrans(fromTrans);
3033     if (!tt)
3034         return ENOENT;
3035     if (tt->vflags & VTDeleted) {
3036         TRELE(tt);
3037         return ENOENT;
3038     }
3039     strcpy(tt->lastProcName, "GetSize");
3040     tt->rxCallPtr = acid;
3041     code = SizeDumpVolume(acid, tt->volume, fromDate, 1, size); /* measure volume's data */
3042     tt->rxCallPtr = (struct rx_call *)0;
3043     if (TRELE(tt))
3044         return VOLSERTRELE_ERROR;
3045
3046 /*    osi_auditU(acid, VS_DumpEvent, code, AUD_LONG, fromTrans, AUD_END);  */
3047     return code;
3048 }
3049
3050 /* GetPartName - map partid (a decimal number) into pname (a string)
3051  * Since for NT we actually want to return the drive name, we map through the
3052  * partition struct.
3053  */
3054 static int
3055 GetPartName(afs_int32 partid, char *pname)
3056 {
3057     if (partid < 0)
3058         return -1;
3059     if (partid < 26) {
3060         strcpy(pname, "/vicep");
3061         pname[6] = 'a' + partid;
3062         pname[7] = '\0';
3063         return 0;
3064     } else if (partid < VOLMAXPARTS) {
3065         strcpy(pname, "/vicep");
3066         partid -= 26;
3067         pname[6] = 'a' + (partid / 26);
3068         pname[7] = 'a' + (partid % 26);
3069         pname[8] = '\0';
3070         return 0;
3071     } else
3072         return -1;
3073 }