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