DAFS: Do not attach a specialStatus'd vol
[openafs.git] / src / vol / volume.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) 2005-2008 Sine Nomine Associates
10  */
11
12 /* 1/1/89: NB:  this stuff is all going to be replaced.  Don't take it too seriously */
13 /*
14
15         System:         VICE-TWO
16         Module:         volume.c
17         Institution:    The Information Technology Center, Carnegie-Mellon University
18
19  */
20
21 #include <afsconfig.h>
22 #include <afs/param.h>
23
24 #include <roken.h>
25
26 #include <ctype.h>
27 #include <stddef.h>
28
29 #ifdef HAVE_SYS_FILE_H
30 #include <sys/file.h>
31 #endif
32
33 #include <rx/xdr.h>
34 #include <afs/afsint.h>
35
36 #ifndef AFS_NT40_ENV
37 #if !defined(AFS_SGI_ENV)
38 #ifdef  AFS_OSF_ENV
39 #include <ufs/fs.h>
40 #else /* AFS_OSF_ENV */
41 #ifdef AFS_VFSINCL_ENV
42 #define VFS
43 #ifdef  AFS_SUN5_ENV
44 #include <sys/fs/ufs_fs.h>
45 #else
46 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ffs/fs.h>
49 #else
50 #include <ufs/fs.h>
51 #endif
52 #endif
53 #else /* AFS_VFSINCL_ENV */
54 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV) && !defined(AFS_DARWIN_ENV)
55 #include <sys/fs.h>
56 #endif
57 #endif /* AFS_VFSINCL_ENV */
58 #endif /* AFS_OSF_ENV */
59 #endif /* AFS_SGI_ENV */
60 #endif /* !AFS_NT40_ENV */
61
62 #ifdef  AFS_AIX_ENV
63 #include <sys/vfs.h>
64 #else
65 #ifdef  AFS_HPUX_ENV
66 #include <mntent.h>
67 #else
68 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
69 #ifdef  AFS_SUN5_ENV
70 #include <sys/mnttab.h>
71 #include <sys/mntent.h>
72 #else
73 #include <mntent.h>
74 #endif
75 #else
76 #ifndef AFS_NT40_ENV
77 #if defined(AFS_SGI_ENV)
78 #include <mntent.h>
79 #else
80 #ifndef AFS_LINUX20_ENV
81 #include <fstab.h>              /* Need to find in libc 5, present in libc 6 */
82 #endif
83 #endif
84 #endif /* AFS_SGI_ENV */
85 #endif
86 #endif /* AFS_HPUX_ENV */
87 #endif
88
89 #include "nfs.h"
90 #include <afs/errors.h>
91 #include "lock.h"
92 #include "lwp.h"
93 #include <afs/afssyscalls.h>
94 #include "ihandle.h"
95 #include <afs/afsutil.h>
96 #include "daemon_com.h"
97 #include "fssync.h"
98 #include "salvsync.h"
99 #include "vnode.h"
100 #include "volume.h"
101 #include "partition.h"
102 #include "volume_inline.h"
103 #include "common.h"
104 #include "afs/afs_assert.h"
105 #include "vutils.h"
106 #include <afs/dir.h>
107
108 #ifdef AFS_PTHREAD_ENV
109 pthread_mutex_t vol_glock_mutex;
110 pthread_mutex_t vol_trans_mutex;
111 pthread_cond_t vol_put_volume_cond;
112 pthread_cond_t vol_sleep_cond;
113 pthread_cond_t vol_init_attach_cond;
114 pthread_cond_t vol_vinit_cond;
115 int vol_attach_threads = 1;
116 #endif /* AFS_PTHREAD_ENV */
117
118 /* start-time configurable I/O parameters */
119 ih_init_params vol_io_params;
120
121 #ifdef AFS_DEMAND_ATTACH_FS
122 pthread_mutex_t vol_salvsync_mutex;
123
124 /*
125  * Set this to 1 to disallow SALVSYNC communication in all threads; used
126  * during shutdown, since the salvageserver may have gone away.
127  */
128 static volatile sig_atomic_t vol_disallow_salvsync = 0;
129 #endif /* AFS_DEMAND_ATTACH_FS */
130
131 /**
132  * has VShutdown_r been called / is VShutdown_r running?
133  */
134 static int vol_shutting_down = 0;
135
136 #ifdef  AFS_OSF_ENV
137 extern void *calloc(), *realloc();
138 #endif
139
140 /* Forward declarations */
141 static Volume *attach2(Error * ec, VolId volumeId, char *path,
142                        struct DiskPartition64 *partp, Volume * vp,
143                        int isbusy, int mode, int *acheckedOut);
144 static void ReallyFreeVolume(Volume * vp);
145 #ifdef AFS_DEMAND_ATTACH_FS
146 static void FreeVolume(Volume * vp);
147 #else /* !AFS_DEMAND_ATTACH_FS */
148 #define FreeVolume(vp) ReallyFreeVolume(vp)
149 static void VScanUpdateList(void);
150 #endif /* !AFS_DEMAND_ATTACH_FS */
151 static void VInitVolumeHeaderCache(afs_uint32 howMany);
152 static int GetVolumeHeader(Volume * vp);
153 static void ReleaseVolumeHeader(struct volHeader *hd);
154 static void FreeVolumeHeader(Volume * vp);
155 static void AddVolumeToHashTable(Volume * vp, int hashid);
156 static void DeleteVolumeFromHashTable(Volume * vp);
157 #if 0
158 static int VHold(Volume * vp);
159 #endif
160 static int VHold_r(Volume * vp);
161 static void VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class);
162 static void VReleaseVolumeHandles_r(Volume * vp);
163 static void VCloseVolumeHandles_r(Volume * vp);
164 static void LoadVolumeHeader(Error * ec, Volume * vp);
165 static int VCheckOffline(Volume * vp);
166 static int VCheckDetach(Volume * vp);
167 static Volume * GetVolume(Error * ec, Error * client_ec, VolId volumeId,
168                           Volume * hint, const struct timespec *ts);
169
170 int LogLevel;                   /* Vice loglevel--not defined as extern so that it will be
171                                  * defined when not linked with vice, XXXX */
172 ProgramType programType;        /* The type of program using the package */
173 static VolumePackageOptions vol_opts;
174
175 /* extended volume package statistics */
176 VolPkgStats VStats;
177
178 #ifdef VOL_LOCK_DEBUG
179 pthread_t vol_glock_holder = 0;
180 #endif
181
182
183 #define VOLUME_BITMAP_GROWSIZE  16      /* bytes, => 128vnodes */
184                                         /* Must be a multiple of 4 (1 word) !! */
185
186 /* this parameter needs to be tunable at runtime.
187  * 128 was really inadequate for largish servers -- at 16384 volumes this
188  * puts average chain length at 128, thus an average 65 deref's to find a volptr.
189  * talk about bad spatial locality...
190  *
191  * an AVL or splay tree might work a lot better, but we'll just increase
192  * the default hash table size for now
193  */
194 #define DEFAULT_VOLUME_HASH_SIZE 256   /* Must be a power of 2!! */
195 #define DEFAULT_VOLUME_HASH_MASK (DEFAULT_VOLUME_HASH_SIZE-1)
196 #define VOLUME_HASH(volumeId) (volumeId&(VolumeHashTable.Mask))
197
198 /*
199  * turn volume hash chains into partially ordered lists.
200  * when the threshold is exceeded between two adjacent elements,
201  * perform a chain rebalancing operation.
202  *
203  * keep the threshold high in order to keep cache line invalidates
204  * low "enough" on SMPs
205  */
206 #define VOLUME_HASH_REORDER_THRESHOLD 200
207
208 /*
209  * when possible, don't just reorder single elements, but reorder
210  * entire chains of elements at once.  a chain of elements that
211  * exceed the element previous to the pivot by at least CHAIN_THRESH
212  * accesses are moved in front of the chain whose elements have at
213  * least CHAIN_THRESH less accesses than the pivot element
214  */
215 #define VOLUME_HASH_REORDER_CHAIN_THRESH (VOLUME_HASH_REORDER_THRESHOLD / 2)
216
217 #include "rx/rx_queue.h"
218
219
220 VolumeHashTable_t VolumeHashTable = {
221     DEFAULT_VOLUME_HASH_SIZE,
222     DEFAULT_VOLUME_HASH_MASK,
223     NULL
224 };
225
226
227 static void VInitVolumeHash(void);
228
229
230 #ifndef AFS_HAVE_FFS
231 /* This macro is used where an ffs() call does not exist. Was in util/ffs.c */
232 ffs(x)
233 {
234     afs_int32 ffs_i;
235     afs_int32 ffs_tmp = x;
236     if (ffs_tmp == 0)
237         return (-1);
238     else
239         for (ffs_i = 1;; ffs_i++) {
240             if (ffs_tmp & 1)
241                 return (ffs_i);
242             else
243                 ffs_tmp >>= 1;
244         }
245 }
246 #endif /* !AFS_HAVE_FFS */
247
248 #ifdef AFS_PTHREAD_ENV
249 /**
250  * disk partition queue element
251  */
252 typedef struct diskpartition_queue_t {
253     struct rx_queue queue;             /**< queue header */
254     struct DiskPartition64 *diskP;     /**< disk partition table entry */
255 } diskpartition_queue_t;
256
257 #ifndef AFS_DEMAND_ATTACH_FS
258
259 typedef struct vinitvolumepackage_thread_t {
260     struct rx_queue queue;
261     pthread_cond_t thread_done_cv;
262     int n_threads_complete;
263 } vinitvolumepackage_thread_t;
264 static void * VInitVolumePackageThread(void * args);
265
266 #else  /* !AFS_DEMAND_ATTTACH_FS */
267 #define VINIT_BATCH_MAX_SIZE 512
268
269 /**
270  * disk partition work queue
271  */
272 struct partition_queue {
273     struct rx_queue head;              /**< diskpartition_queue_t queue */
274     pthread_mutex_t mutex;
275     pthread_cond_t cv;
276 };
277
278 /**
279  * volumes parameters for preattach
280  */
281 struct volume_init_batch {
282     struct rx_queue queue;               /**< queue header */
283     int thread;                          /**< posting worker thread */
284     int last;                            /**< indicates thread is done */
285     int size;                            /**< number of volume ids in batch */
286     Volume *batch[VINIT_BATCH_MAX_SIZE]; /**< volumes ids to preattach */
287 };
288
289 /**
290  * volume parameters work queue
291  */
292 struct volume_init_queue {
293     struct rx_queue head;                /**< volume_init_batch queue */
294     pthread_mutex_t mutex;
295     pthread_cond_t cv;
296 };
297
298 /**
299  * volume init worker thread parameters
300  */
301 struct vinitvolumepackage_thread_param {
302     int nthreads;                        /**< total number of worker threads */
303     int thread;                          /**< thread number for this worker thread */
304     struct partition_queue *pq;          /**< queue partitions to scan */
305     struct volume_init_queue *vq;        /**< queue of volume to preattach */
306 };
307
308 static void *VInitVolumePackageThread(void *args);
309 static struct DiskPartition64 *VInitNextPartition(struct partition_queue *pq);
310 static VolId VInitNextVolumeId(DIR *dirp);
311 static int VInitPreAttachVolumes(int nthreads, struct volume_init_queue *vq);
312
313 #endif /* !AFS_DEMAND_ATTACH_FS */
314 #endif /* AFS_PTHREAD_ENV */
315
316 #ifndef AFS_DEMAND_ATTACH_FS
317 static int VAttachVolumesByPartition(struct DiskPartition64 *diskP,
318                                      int * nAttached, int * nUnattached);
319 #endif /* AFS_DEMAND_ATTACH_FS */
320
321
322 #ifdef AFS_DEMAND_ATTACH_FS
323 /* demand attach fileserver extensions */
324
325 /* XXX
326  * in the future we will support serialization of VLRU state into the fs_state
327  * disk dumps
328  *
329  * these structures are the beginning of that effort
330  */
331 struct VLRU_DiskHeader {
332     struct versionStamp stamp;            /* magic and structure version number */
333     afs_uint32 mtime;                     /* time of dump to disk */
334     afs_uint32 num_records;               /* number of VLRU_DiskEntry records */
335 };
336
337 struct VLRU_DiskEntry {
338     afs_uint32 vid;                       /* volume ID */
339     afs_uint32 idx;                       /* generation */
340     afs_uint32 last_get;                  /* timestamp of last get */
341 };
342
343 struct VLRU_StartupQueue {
344     struct VLRU_DiskEntry * entry;
345     int num_entries;
346     int next_idx;
347 };
348
349 typedef struct vshutdown_thread_t {
350     struct rx_queue q;
351     pthread_mutex_t lock;
352     pthread_cond_t cv;
353     pthread_cond_t master_cv;
354     int n_threads;
355     int n_threads_complete;
356     int vol_remaining;
357     int schedule_version;
358     int pass;
359     byte n_parts;
360     byte n_parts_done_pass;
361     byte part_thread_target[VOLMAXPARTS+1];
362     byte part_done_pass[VOLMAXPARTS+1];
363     struct rx_queue * part_pass_head[VOLMAXPARTS+1];
364     int stats[4][VOLMAXPARTS+1];
365 } vshutdown_thread_t;
366 static void * VShutdownThread(void * args);
367
368
369 static Volume * VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode);
370 static int VCheckFree(Volume * vp);
371
372 /* VByP List */
373 static void AddVolumeToVByPList_r(Volume * vp);
374 static void DeleteVolumeFromVByPList_r(Volume * vp);
375 static void VVByPListBeginExclusive_r(struct DiskPartition64 * dp);
376 static void VVByPListEndExclusive_r(struct DiskPartition64 * dp);
377 static void VVByPListWait_r(struct DiskPartition64 * dp);
378
379 /* online salvager */
380 static int VCheckSalvage(Volume * vp);
381 #if defined(SALVSYNC_BUILD_CLIENT) || defined(FSSYNC_BUILD_CLIENT)
382 static int VScheduleSalvage_r(Volume * vp);
383 #endif
384
385 /* Volume hash table */
386 static void VReorderHash_r(VolumeHashChainHead * head, Volume * pp, Volume * vp);
387 static void VHashBeginExclusive_r(VolumeHashChainHead * head);
388 static void VHashEndExclusive_r(VolumeHashChainHead * head);
389 static void VHashWait_r(VolumeHashChainHead * head);
390
391 /* shutdown */
392 static int ShutdownVByPForPass_r(struct DiskPartition64 * dp, int pass);
393 static int ShutdownVolumeWalk_r(struct DiskPartition64 * dp, int pass,
394                                 struct rx_queue ** idx);
395 static void ShutdownController(vshutdown_thread_t * params);
396 static void ShutdownCreateSchedule(vshutdown_thread_t * params);
397
398 /* VLRU */
399 static void VLRU_ComputeConstants(void);
400 static void VInitVLRU(void);
401 static void VLRU_Init_Node_r(Volume * vp);
402 static void VLRU_Add_r(Volume * vp);
403 static void VLRU_Delete_r(Volume * vp);
404 static void VLRU_UpdateAccess_r(Volume * vp);
405 static void * VLRU_ScannerThread(void * args);
406 static void VLRU_Scan_r(int idx);
407 static void VLRU_Promote_r(int idx);
408 static void VLRU_Demote_r(int idx);
409 static void VLRU_SwitchQueues(Volume * vp, int new_idx, int append);
410
411 /* soft detach */
412 static int VCheckSoftDetach(Volume * vp, afs_uint32 thresh);
413 static int VCheckSoftDetachCandidate(Volume * vp, afs_uint32 thresh);
414 static int VSoftDetachVolume_r(Volume * vp, afs_uint32 thresh);
415
416
417 pthread_key_t VThread_key;
418 VThreadOptions_t VThread_defaults = {
419     0                           /**< allow salvsync */
420 };
421 #endif /* AFS_DEMAND_ATTACH_FS */
422
423
424 struct Lock vol_listLock;       /* Lock obtained when listing volumes:
425                                  * prevents a volume from being missed
426                                  * if the volume is attached during a
427                                  * list volumes */
428
429
430 /* Common message used when the volume goes off line */
431 char *VSalvageMessage =
432     "Files in this volume are currently unavailable; call operations";
433
434 int VInit;                      /* 0 - uninitialized,
435                                  * 1 - initialized but not all volumes have been attached,
436                                  * 2 - initialized and all volumes have been attached,
437                                  * 3 - initialized, all volumes have been attached, and
438                                  * VConnectFS() has completed. */
439
440 static int vinit_attach_abort = 0;
441
442 bit32 VolumeCacheCheck;         /* Incremented everytime a volume goes on line--
443                                  * used to stamp volume headers and in-core
444                                  * vnodes.  When the volume goes on-line the
445                                  * vnode will be invalidated
446                                  * access only with VOL_LOCK held */
447
448
449
450
451 /***************************************************/
452 /* Startup routines                                */
453 /***************************************************/
454
455 #if defined(FAST_RESTART) && defined(AFS_DEMAND_ATTACH_FS)
456 # error FAST_RESTART and DAFS are incompatible. For the DAFS equivalent \
457         of FAST_RESTART, use the -unsafe-nosalvage fileserver argument
458 #endif
459
460 /**
461  * assign default values to a VolumePackageOptions struct.
462  *
463  * Always call this on a VolumePackageOptions struct first, then set any
464  * specific options you want, then call VInitVolumePackage2.
465  *
466  * @param[in]  pt   caller's program type
467  * @param[out] opts volume package options
468  */
469 void
470 VOptDefaults(ProgramType pt, VolumePackageOptions *opts)
471 {
472     opts->nLargeVnodes = opts->nSmallVnodes = 5;
473     opts->volcache = 0;
474
475     opts->canScheduleSalvage = 0;
476     opts->canUseFSSYNC = 0;
477     opts->canUseSALVSYNC = 0;
478
479     opts->interrupt_rxcall = NULL;
480     opts->offline_timeout = -1;
481     opts->offline_shutdown_timeout = -1;
482
483 #ifdef FAST_RESTART
484     opts->unsafe_attach = 1;
485 #else /* !FAST_RESTART */
486     opts->unsafe_attach = 0;
487 #endif /* !FAST_RESTART */
488
489     switch (pt) {
490     case fileServer:
491         opts->canScheduleSalvage = 1;
492         opts->canUseSALVSYNC = 1;
493         break;
494
495     case salvageServer:
496         opts->canUseFSSYNC = 1;
497         break;
498
499     case volumeServer:
500         opts->nLargeVnodes = 0;
501         opts->nSmallVnodes = 0;
502
503         opts->canScheduleSalvage = 1;
504         opts->canUseFSSYNC = 1;
505         break;
506
507     default:
508         /* noop */
509         break;
510     }
511 }
512
513 /**
514  * Set VInit to a certain value, and signal waiters.
515  *
516  * @param[in] value  the value to set VInit to
517  *
518  * @pre VOL_LOCK held
519  */
520 static void
521 VSetVInit_r(int value)
522 {
523     VInit = value;
524     CV_BROADCAST(&vol_vinit_cond);
525 }
526
527 static_inline void
528 VLogOfflineTimeout(const char *type, afs_int32 timeout)
529 {
530     if (timeout < 0) {
531         return;
532     }
533     if (timeout == 0) {
534         Log("VInitVolumePackage: Interrupting clients accessing %s "
535             "immediately\n", type);
536     } else {
537         Log("VInitVolumePackage: Interrupting clients accessing %s "
538             "after %ld second%s\n", type, (long)timeout, timeout==1?"":"s");
539     }
540 }
541
542 int
543 VInitVolumePackage2(ProgramType pt, VolumePackageOptions * opts)
544 {
545     int errors = 0;             /* Number of errors while finding vice partitions. */
546
547     programType = pt;
548     vol_opts = *opts;
549
550 #ifndef AFS_PTHREAD_ENV
551     if (opts->offline_timeout != -1 || opts->offline_shutdown_timeout != -1) {
552         Log("VInitVolumePackage: offline_timeout and/or "
553             "offline_shutdown_timeout was specified, but the volume package "
554             "does not support these for LWP builds\n");
555         return -1;
556     }
557 #endif
558     VLogOfflineTimeout("volumes going offline", opts->offline_timeout);
559     VLogOfflineTimeout("volumes going offline during shutdown",
560                        opts->offline_shutdown_timeout);
561
562     memset(&VStats, 0, sizeof(VStats));
563     VStats.hdr_cache_size = 200;
564
565     VInitPartitionPackage();
566     VInitVolumeHash();
567 #ifdef AFS_DEMAND_ATTACH_FS
568     if (programType == fileServer) {
569         VInitVLRU();
570     } else {
571         VLRU_SetOptions(VLRU_SET_ENABLED, 0);
572     }
573     osi_Assert(pthread_key_create(&VThread_key, NULL) == 0);
574 #endif
575
576     MUTEX_INIT(&vol_glock_mutex, "vol glock", MUTEX_DEFAULT, 0);
577     MUTEX_INIT(&vol_trans_mutex, "vol trans", MUTEX_DEFAULT, 0);
578     CV_INIT(&vol_put_volume_cond, "vol put", CV_DEFAULT, 0);
579     CV_INIT(&vol_sleep_cond, "vol sleep", CV_DEFAULT, 0);
580     CV_INIT(&vol_init_attach_cond, "vol init attach", CV_DEFAULT, 0);
581     CV_INIT(&vol_vinit_cond, "vol init", CV_DEFAULT, 0);
582 #ifndef AFS_PTHREAD_ENV
583     IOMGR_Initialize();
584 #endif /* AFS_PTHREAD_ENV */
585     Lock_Init(&vol_listLock);
586
587     srandom(time(0));           /* For VGetVolumeInfo */
588
589 #ifdef AFS_DEMAND_ATTACH_FS
590     MUTEX_INIT(&vol_salvsync_mutex, "salvsync", MUTEX_DEFAULT, 0);
591 #endif /* AFS_DEMAND_ATTACH_FS */
592
593     /* Ok, we have done enough initialization that fileserver can
594      * start accepting calls, even though the volumes may not be
595      * available just yet.
596      */
597     VInit = 1;
598
599 #if defined(AFS_DEMAND_ATTACH_FS) && defined(SALVSYNC_BUILD_SERVER)
600     if (programType == salvageServer) {
601         SALVSYNC_salvInit();
602     }
603 #endif /* AFS_DEMAND_ATTACH_FS */
604 #ifdef FSSYNC_BUILD_SERVER
605     if (programType == fileServer) {
606         FSYNC_fsInit();
607     }
608 #endif
609 #if defined(AFS_DEMAND_ATTACH_FS) && defined(SALVSYNC_BUILD_CLIENT)
610     if (VCanUseSALVSYNC()) {
611         /* establish a connection to the salvager at this point */
612         osi_Assert(VConnectSALV() != 0);
613     }
614 #endif /* AFS_DEMAND_ATTACH_FS */
615
616     if (opts->volcache > VStats.hdr_cache_size)
617         VStats.hdr_cache_size = opts->volcache;
618     VInitVolumeHeaderCache(VStats.hdr_cache_size);
619
620     VInitVnodes(vLarge, opts->nLargeVnodes);
621     VInitVnodes(vSmall, opts->nSmallVnodes);
622
623
624     errors = VAttachPartitions();
625     if (errors)
626         return -1;
627
628     if (programType != fileServer) {
629         errors = VInitAttachVolumes(programType);
630         if (errors) {
631             return -1;
632         }
633     }
634
635 #ifdef FSSYNC_BUILD_CLIENT
636     if (VCanUseFSSYNC()) {
637         if (!VConnectFS()) {
638 #ifdef AFS_DEMAND_ATTACH_FS
639             if (programType == salvageServer) {
640                 Log("Unable to connect to file server; aborted\n");
641                 exit(1);
642             }
643 #endif /* AFS_DEMAND_ATTACH_FS */
644             Log("Unable to connect to file server; will retry at need\n");
645         }
646     }
647 #endif /* FSSYNC_BUILD_CLIENT */
648     return 0;
649 }
650
651
652 #if !defined(AFS_PTHREAD_ENV)
653 /**
654  * Attach volumes in vice partitions
655  *
656  * @param[in]  pt         calling program type
657  *
658  * @return 0
659  * @note This is the original, non-threaded version of attach parititions.
660  *
661  * @post VInit state is 2
662  */
663 int
664 VInitAttachVolumes(ProgramType pt)
665 {
666     osi_Assert(VInit==1);
667     if (pt == fileServer) {
668         struct DiskPartition64 *diskP;
669         /* Attach all the volumes in this partition */
670         for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
671             int nAttached = 0, nUnattached = 0;
672             osi_Assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
673         }
674     }
675     VOL_LOCK;
676     VSetVInit_r(2);                     /* Initialized, and all volumes have been attached */
677     LWP_NoYieldSignal(VInitAttachVolumes);
678     VOL_UNLOCK;
679     return 0;
680 }
681 #endif /* !AFS_PTHREAD_ENV */
682
683 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_DEMAND_ATTACH_FS)
684 /**
685  * Attach volumes in vice partitions
686  *
687  * @param[in]  pt         calling program type
688  *
689  * @return 0
690  * @note Threaded version of attach parititions.
691  *
692  * @post VInit state is 2
693  */
694 int
695 VInitAttachVolumes(ProgramType pt)
696 {
697     osi_Assert(VInit==1);
698     if (pt == fileServer) {
699         struct DiskPartition64 *diskP;
700         struct vinitvolumepackage_thread_t params;
701         struct diskpartition_queue_t * dpq;
702         int i, threads, parts;
703         pthread_t tid;
704         pthread_attr_t attrs;
705
706         CV_INIT(&params.thread_done_cv, "thread done", CV_DEFAULT, 0);
707         queue_Init(&params);
708         params.n_threads_complete = 0;
709
710         /* create partition work queue */
711         for (parts=0, diskP = DiskPartitionList; diskP; diskP = diskP->next, parts++) {
712             dpq = (diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
713             osi_Assert(dpq != NULL);
714             dpq->diskP = diskP;
715             queue_Append(&params,dpq);
716         }
717
718         threads = MIN(parts, vol_attach_threads);
719
720         if (threads > 1) {
721             /* spawn off a bunch of initialization threads */
722             osi_Assert(pthread_attr_init(&attrs) == 0);
723             osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
724
725             Log("VInitVolumePackage: beginning parallel fileserver startup\n");
726             Log("VInitVolumePackage: using %d threads to attach volumes on %d partitions\n",
727                 threads, parts);
728
729             VOL_LOCK;
730             for (i=0; i < threads; i++) {
731                 AFS_SIGSET_DECL;
732                 AFS_SIGSET_CLEAR();
733                 osi_Assert(pthread_create
734                        (&tid, &attrs, &VInitVolumePackageThread,
735                         &params) == 0);
736                 AFS_SIGSET_RESTORE();
737             }
738
739             while(params.n_threads_complete < threads) {
740                 VOL_CV_WAIT(&params.thread_done_cv);
741             }
742             VOL_UNLOCK;
743
744             osi_Assert(pthread_attr_destroy(&attrs) == 0);
745         } else {
746             /* if we're only going to run one init thread, don't bother creating
747              * another LWP */
748             Log("VInitVolumePackage: beginning single-threaded fileserver startup\n");
749             Log("VInitVolumePackage: using 1 thread to attach volumes on %d partition(s)\n",
750                 parts);
751
752             VInitVolumePackageThread(&params);
753         }
754
755         CV_DESTROY(&params.thread_done_cv);
756     }
757     VOL_LOCK;
758     VSetVInit_r(2);                     /* Initialized, and all volumes have been attached */
759     CV_BROADCAST(&vol_init_attach_cond);
760     VOL_UNLOCK;
761     return 0;
762 }
763
764 static void *
765 VInitVolumePackageThread(void * args) {
766
767     struct DiskPartition64 *diskP;
768     struct vinitvolumepackage_thread_t * params;
769     struct diskpartition_queue_t * dpq;
770
771     params = (vinitvolumepackage_thread_t *) args;
772
773
774     VOL_LOCK;
775     /* Attach all the volumes in this partition */
776     while (queue_IsNotEmpty(params)) {
777         int nAttached = 0, nUnattached = 0;
778
779         if (vinit_attach_abort) {
780             Log("Aborting initialization\n");
781             goto done;
782         }
783
784         dpq = queue_First(params,diskpartition_queue_t);
785         queue_Remove(dpq);
786         VOL_UNLOCK;
787         diskP = dpq->diskP;
788         free(dpq);
789
790         osi_Assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
791
792         VOL_LOCK;
793     }
794
795 done:
796     params->n_threads_complete++;
797     CV_SIGNAL(&params->thread_done_cv);
798     VOL_UNLOCK;
799     return NULL;
800 }
801 #endif /* AFS_PTHREAD_ENV && !AFS_DEMAND_ATTACH_FS */
802
803 #if defined(AFS_DEMAND_ATTACH_FS)
804 /**
805  * Attach volumes in vice partitions
806  *
807  * @param[in]  pt         calling program type
808  *
809  * @return 0
810  * @note Threaded version of attach partitions.
811  *
812  * @post VInit state is 2
813  */
814 int
815 VInitAttachVolumes(ProgramType pt)
816 {
817     osi_Assert(VInit==1);
818     if (pt == fileServer) {
819
820         struct DiskPartition64 *diskP;
821         struct partition_queue pq;
822         struct volume_init_queue vq;
823
824         int i, threads, parts;
825         pthread_t tid;
826         pthread_attr_t attrs;
827
828         /* create partition work queue */
829         queue_Init(&pq);
830         CV_INIT(&(pq.cv), "partq", CV_DEFAULT, 0);
831         MUTEX_INIT(&(pq.mutex), "partq", MUTEX_DEFAULT, 0);
832         for (parts = 0, diskP = DiskPartitionList; diskP; diskP = diskP->next, parts++) {
833             struct diskpartition_queue_t *dp;
834             dp = (struct diskpartition_queue_t*)malloc(sizeof(struct diskpartition_queue_t));
835             osi_Assert(dp != NULL);
836             dp->diskP = diskP;
837             queue_Append(&pq, dp);
838         }
839
840         /* number of worker threads; at least one, not to exceed the number of partitions */
841         threads = MIN(parts, vol_attach_threads);
842
843         /* create volume work queue */
844         queue_Init(&vq);
845         CV_INIT(&(vq.cv), "volq", CV_DEFAULT, 0);
846         MUTEX_INIT(&(vq.mutex), "volq", MUTEX_DEFAULT, 0);
847
848         osi_Assert(pthread_attr_init(&attrs) == 0);
849         osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
850
851         Log("VInitVolumePackage: beginning parallel fileserver startup\n");
852         Log("VInitVolumePackage: using %d threads to pre-attach volumes on %d partitions\n",
853                 threads, parts);
854
855         /* create threads to scan disk partitions. */
856         for (i=0; i < threads; i++) {
857             struct vinitvolumepackage_thread_param *params;
858             AFS_SIGSET_DECL;
859
860             params = (struct vinitvolumepackage_thread_param *)malloc(sizeof(struct vinitvolumepackage_thread_param));
861             osi_Assert(params);
862             params->pq = &pq;
863             params->vq = &vq;
864             params->nthreads = threads;
865             params->thread = i+1;
866
867             AFS_SIGSET_CLEAR();
868             osi_Assert(pthread_create (&tid, &attrs, &VInitVolumePackageThread, (void*)params) == 0);
869             AFS_SIGSET_RESTORE();
870         }
871
872         VInitPreAttachVolumes(threads, &vq);
873
874         osi_Assert(pthread_attr_destroy(&attrs) == 0);
875         CV_DESTROY(&pq.cv);
876         MUTEX_DESTROY(&pq.mutex);
877         CV_DESTROY(&vq.cv);
878         MUTEX_DESTROY(&vq.mutex);
879     }
880
881     VOL_LOCK;
882     VSetVInit_r(2);                     /* Initialized, and all volumes have been attached */
883     CV_BROADCAST(&vol_init_attach_cond);
884     VOL_UNLOCK;
885
886     return 0;
887 }
888
889 /**
890  * Volume package initialization worker thread. Scan partitions for volume
891  * header files. Gather batches of volume ids and dispatch them to
892  * the main thread to be preattached.  The volume preattachement is done
893  * in the main thread to avoid global volume lock contention.
894  */
895 static void *
896 VInitVolumePackageThread(void *args)
897 {
898     struct vinitvolumepackage_thread_param *params;
899     struct DiskPartition64 *partition;
900     struct partition_queue *pq;
901     struct volume_init_queue *vq;
902     struct volume_init_batch *vb;
903
904     osi_Assert(args);
905     params = (struct vinitvolumepackage_thread_param *)args;
906     pq = params->pq;
907     vq = params->vq;
908     osi_Assert(pq);
909     osi_Assert(vq);
910
911     vb = (struct volume_init_batch*)malloc(sizeof(struct volume_init_batch));
912     osi_Assert(vb);
913     vb->thread = params->thread;
914     vb->last = 0;
915     vb->size = 0;
916
917     Log("Scanning partitions on thread %d of %d\n", params->thread, params->nthreads);
918     while((partition = VInitNextPartition(pq))) {
919         DIR *dirp;
920         VolId vid;
921
922         Log("Partition %s: pre-attaching volumes\n", partition->name);
923         dirp = opendir(VPartitionPath(partition));
924         if (!dirp) {
925             Log("opendir on Partition %s failed, errno=%d!\n", partition->name, errno);
926             continue;
927         }
928         while ((vid = VInitNextVolumeId(dirp))) {
929             Volume *vp = (Volume*)malloc(sizeof(Volume));
930             osi_Assert(vp);
931             memset(vp, 0, sizeof(Volume));
932             vp->device = partition->device;
933             vp->partition = partition;
934             vp->hashid = vid;
935             queue_Init(&vp->vnode_list);
936             queue_Init(&vp->rx_call_list);
937             CV_INIT(&V_attachCV(vp), "partattach", CV_DEFAULT, 0);
938
939             vb->batch[vb->size++] = vp;
940             if (vb->size == VINIT_BATCH_MAX_SIZE) {
941                 MUTEX_ENTER(&vq->mutex);
942                 queue_Append(vq, vb);
943                 CV_BROADCAST(&vq->cv);
944                 MUTEX_EXIT(&vq->mutex);
945
946                 vb = (struct volume_init_batch*)malloc(sizeof(struct volume_init_batch));
947                 osi_Assert(vb);
948                 vb->thread = params->thread;
949                 vb->size = 0;
950                 vb->last = 0;
951             }
952         }
953         closedir(dirp);
954     }
955
956     vb->last = 1;
957     MUTEX_ENTER(&vq->mutex);
958     queue_Append(vq, vb);
959     CV_BROADCAST(&vq->cv);
960     MUTEX_EXIT(&vq->mutex);
961
962     Log("Partition scan thread %d of %d ended\n", params->thread, params->nthreads);
963     free(params);
964     return NULL;
965 }
966
967 /**
968  * Read next element from the pre-populated partition list.
969  */
970 static struct DiskPartition64*
971 VInitNextPartition(struct partition_queue *pq)
972 {
973     struct DiskPartition64 *partition;
974     struct diskpartition_queue_t *dp; /* queue element */
975
976     if (vinit_attach_abort) {
977         Log("Aborting volume preattach thread.\n");
978         return NULL;
979     }
980
981     /* get next partition to scan */
982     MUTEX_ENTER(&pq->mutex);
983     if (queue_IsEmpty(pq)) {
984         MUTEX_EXIT(&pq->mutex);
985         return NULL;
986     }
987     dp = queue_First(pq, diskpartition_queue_t);
988     queue_Remove(dp);
989     MUTEX_EXIT(&pq->mutex);
990
991     osi_Assert(dp);
992     osi_Assert(dp->diskP);
993
994     partition = dp->diskP;
995     free(dp);
996     return partition;
997 }
998
999 /**
1000  * Find next volume id on the partition.
1001  */
1002 static VolId
1003 VInitNextVolumeId(DIR *dirp)
1004 {
1005     struct dirent *d;
1006     VolId vid = 0;
1007     char *ext;
1008
1009     while((d = readdir(dirp))) {
1010         if (vinit_attach_abort) {
1011             Log("Aborting volume preattach thread.\n");
1012             break;
1013         }
1014         ext = strrchr(d->d_name, '.');
1015         if (d->d_name[0] == 'V' && ext && strcmp(ext, VHDREXT) == 0) {
1016             vid = VolumeNumber(d->d_name);
1017             if (vid) {
1018                break;
1019             }
1020             Log("Warning: bogus volume header file: %s\n", d->d_name);
1021         }
1022     }
1023     return vid;
1024 }
1025
1026 /**
1027  * Preattach volumes in batches to avoid lock contention.
1028  */
1029 static int
1030 VInitPreAttachVolumes(int nthreads, struct volume_init_queue *vq)
1031 {
1032     struct volume_init_batch *vb;
1033     int i;
1034
1035     while (nthreads) {
1036         /* dequeue next volume */
1037         MUTEX_ENTER(&vq->mutex);
1038         if (queue_IsEmpty(vq)) {
1039             CV_WAIT(&vq->cv, &vq->mutex);
1040         }
1041         vb = queue_First(vq, volume_init_batch);
1042         queue_Remove(vb);
1043         MUTEX_EXIT(&vq->mutex);
1044
1045         if (vb->size) {
1046             VOL_LOCK;
1047             for (i = 0; i<vb->size; i++) {
1048                 Volume *vp;
1049                 Volume *dup;
1050                 Error ec = 0;
1051
1052                 vp = vb->batch[i];
1053                 dup = VLookupVolume_r(&ec, vp->hashid, NULL);
1054                 if (ec) {
1055                     Log("Error looking up volume, code=%d\n", ec);
1056                 }
1057                 else if (dup) {
1058                     Log("Warning: Duplicate volume id %d detected.\n", vp->hashid);
1059                 }
1060                 else {
1061                     /* put pre-attached volume onto the hash table
1062                      * and bring it up to the pre-attached state */
1063                     AddVolumeToHashTable(vp, vp->hashid);
1064                     AddVolumeToVByPList_r(vp);
1065                     VLRU_Init_Node_r(vp);
1066                     VChangeState_r(vp, VOL_STATE_PREATTACHED);
1067                 }
1068             }
1069             VOL_UNLOCK;
1070         }
1071
1072         if (vb->last) {
1073             nthreads--;
1074         }
1075         free(vb);
1076     }
1077     return 0;
1078 }
1079 #endif /* AFS_DEMAND_ATTACH_FS */
1080
1081 #if !defined(AFS_DEMAND_ATTACH_FS)
1082 /*
1083  * attach all volumes on a given disk partition
1084  */
1085 static int
1086 VAttachVolumesByPartition(struct DiskPartition64 *diskP, int * nAttached, int * nUnattached)
1087 {
1088   DIR * dirp;
1089   struct dirent * dp;
1090   int ret = 0;
1091
1092   Log("Partition %s: attaching volumes\n", diskP->name);
1093   dirp = opendir(VPartitionPath(diskP));
1094   if (!dirp) {
1095     Log("opendir on Partition %s failed!\n", diskP->name);
1096     return 1;
1097   }
1098
1099   while ((dp = readdir(dirp))) {
1100     char *p;
1101     p = strrchr(dp->d_name, '.');
1102
1103     if (vinit_attach_abort) {
1104       Log("Partition %s: abort attach volumes\n", diskP->name);
1105       goto done;
1106     }
1107
1108     if (p != NULL && strcmp(p, VHDREXT) == 0) {
1109       Error error;
1110       Volume *vp;
1111       vp = VAttachVolumeByName(&error, diskP->name, dp->d_name,
1112                                V_VOLUPD);
1113       (*(vp ? nAttached : nUnattached))++;
1114       if (error == VOFFLINE)
1115         Log("Volume %d stays offline (/vice/offline/%s exists)\n", VolumeNumber(dp->d_name), dp->d_name);
1116       else if (LogLevel >= 5) {
1117         Log("Partition %s: attached volume %d (%s)\n",
1118             diskP->name, VolumeNumber(dp->d_name),
1119             dp->d_name);
1120       }
1121       if (vp) {
1122         VPutVolume(vp);
1123       }
1124     }
1125   }
1126
1127   Log("Partition %s: attached %d volumes; %d volumes not attached\n", diskP->name, *nAttached, *nUnattached);
1128 done:
1129   closedir(dirp);
1130   return ret;
1131 }
1132 #endif /* !AFS_DEMAND_ATTACH_FS */
1133
1134 /***************************************************/
1135 /* Shutdown routines                               */
1136 /***************************************************/
1137
1138 /*
1139  * demand attach fs
1140  * highly multithreaded volume package shutdown
1141  *
1142  * with the demand attach fileserver extensions,
1143  * VShutdown has been modified to be multithreaded.
1144  * In order to achieve optimal use of many threads,
1145  * the shutdown code involves one control thread and
1146  * n shutdown worker threads.  The control thread
1147  * periodically examines the number of volumes available
1148  * for shutdown on each partition, and produces a worker
1149  * thread allocation schedule.  The idea is to eliminate
1150  * redundant scheduling computation on the workers by
1151  * having a single master scheduler.
1152  *
1153  * The scheduler's objectives are:
1154  * (1) fairness
1155  *   each partition with volumes remaining gets allocated
1156  *   at least 1 thread (assuming sufficient threads)
1157  * (2) performance
1158  *   threads are allocated proportional to the number of
1159  *   volumes remaining to be offlined.  This ensures that
1160  *   the OS I/O scheduler has many requests to elevator
1161  *   seek on partitions that will (presumably) take the
1162  *   longest amount of time (from now) to finish shutdown
1163  * (3) keep threads busy
1164  *   when there are extra threads, they are assigned to
1165  *   partitions using a simple round-robin algorithm
1166  *
1167  * In the future, we may wish to add the ability to adapt
1168  * to the relative performance patterns of each disk
1169  * partition.
1170  *
1171  *
1172  * demand attach fs
1173  * multi-step shutdown process
1174  *
1175  * demand attach shutdown is a four-step process. Each
1176  * shutdown "pass" shuts down increasingly more difficult
1177  * volumes.  The main purpose is to achieve better cache
1178  * utilization during shutdown.
1179  *
1180  * pass 0
1181  *   shutdown volumes in the unattached, pre-attached
1182  *   and error states
1183  * pass 1
1184  *   shutdown attached volumes with cached volume headers
1185  * pass 2
1186  *   shutdown all volumes in non-exclusive states
1187  * pass 3
1188  *   shutdown all remaining volumes
1189  */
1190
1191 #ifdef AFS_DEMAND_ATTACH_FS
1192
1193 void
1194 VShutdown_r(void)
1195 {
1196     int i;
1197     struct DiskPartition64 * diskP;
1198     struct diskpartition_queue_t * dpq;
1199     vshutdown_thread_t params;
1200     pthread_t tid;
1201     pthread_attr_t attrs;
1202
1203     memset(&params, 0, sizeof(vshutdown_thread_t));
1204
1205     if (VInit < 2) {
1206         Log("VShutdown:  aborting attach volumes\n");
1207         vinit_attach_abort = 1;
1208         VOL_CV_WAIT(&vol_init_attach_cond);
1209     }
1210
1211     for (params.n_parts=0, diskP = DiskPartitionList;
1212          diskP; diskP = diskP->next, params.n_parts++);
1213
1214     Log("VShutdown:  shutting down on-line volumes on %d partition%s...\n",
1215         params.n_parts, params.n_parts > 1 ? "s" : "");
1216
1217     vol_shutting_down = 1;
1218
1219     if (vol_attach_threads > 1) {
1220         /* prepare for parallel shutdown */
1221         params.n_threads = vol_attach_threads;
1222         MUTEX_INIT(&params.lock, "params", MUTEX_DEFAULT, 0);
1223         CV_INIT(&params.cv, "params", CV_DEFAULT, 0);
1224         CV_INIT(&params.master_cv, "params master", CV_DEFAULT, 0);
1225         osi_Assert(pthread_attr_init(&attrs) == 0);
1226         osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
1227         queue_Init(&params);
1228
1229         /* setup the basic partition information structures for
1230          * parallel shutdown */
1231         for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1232             /* XXX debug */
1233             struct rx_queue * qp, * nqp;
1234             Volume * vp;
1235             int count = 0;
1236
1237             VVByPListWait_r(diskP);
1238             VVByPListBeginExclusive_r(diskP);
1239
1240             /* XXX debug */
1241             for (queue_Scan(&diskP->vol_list, qp, nqp, rx_queue)) {
1242                 vp = (Volume *)((char *)qp - offsetof(Volume, vol_list));
1243                 if (vp->header)
1244                     count++;
1245             }
1246             Log("VShutdown: partition %s has %d volumes with attached headers\n",
1247                 VPartitionPath(diskP), count);
1248
1249
1250             /* build up the pass 0 shutdown work queue */
1251             dpq = (struct diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
1252             osi_Assert(dpq != NULL);
1253             dpq->diskP = diskP;
1254             queue_Prepend(&params, dpq);
1255
1256             params.part_pass_head[diskP->index] = queue_First(&diskP->vol_list, rx_queue);
1257         }
1258
1259         Log("VShutdown:  beginning parallel fileserver shutdown\n");
1260         Log("VShutdown:  using %d threads to offline volumes on %d partition%s\n",
1261             vol_attach_threads, params.n_parts, params.n_parts > 1 ? "s" : "" );
1262
1263         /* do pass 0 shutdown */
1264         MUTEX_ENTER(&params.lock);
1265         for (i=0; i < params.n_threads; i++) {
1266             osi_Assert(pthread_create
1267                    (&tid, &attrs, &VShutdownThread,
1268                     &params) == 0);
1269         }
1270
1271         /* wait for all the pass 0 shutdowns to complete */
1272         while (params.n_threads_complete < params.n_threads) {
1273             CV_WAIT(&params.master_cv, &params.lock);
1274         }
1275         params.n_threads_complete = 0;
1276         params.pass = 1;
1277         CV_BROADCAST(&params.cv);
1278         MUTEX_EXIT(&params.lock);
1279
1280         Log("VShutdown:  pass 0 completed using the 1 thread per partition algorithm\n");
1281         Log("VShutdown:  starting passes 1 through 3 using finely-granular mp-fast algorithm\n");
1282
1283         /* run the parallel shutdown scheduler. it will drop the glock internally */
1284         ShutdownController(&params);
1285
1286         /* wait for all the workers to finish pass 3 and terminate */
1287         while (params.pass < 4) {
1288             VOL_CV_WAIT(&params.cv);
1289         }
1290
1291         osi_Assert(pthread_attr_destroy(&attrs) == 0);
1292         CV_DESTROY(&params.cv);
1293         CV_DESTROY(&params.master_cv);
1294         MUTEX_DESTROY(&params.lock);
1295
1296         /* drop the VByPList exclusive reservations */
1297         for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1298             VVByPListEndExclusive_r(diskP);
1299             Log("VShutdown:  %s stats : (pass[0]=%d, pass[1]=%d, pass[2]=%d, pass[3]=%d)\n",
1300                 VPartitionPath(diskP),
1301                 params.stats[0][diskP->index],
1302                 params.stats[1][diskP->index],
1303                 params.stats[2][diskP->index],
1304                 params.stats[3][diskP->index]);
1305         }
1306
1307         Log("VShutdown:  shutdown finished using %d threads\n", params.n_threads);
1308     } else {
1309         /* if we're only going to run one shutdown thread, don't bother creating
1310          * another LWP */
1311         Log("VShutdown:  beginning single-threaded fileserver shutdown\n");
1312
1313         for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1314             VShutdownByPartition_r(diskP);
1315         }
1316     }
1317
1318     Log("VShutdown:  complete.\n");
1319 }
1320
1321 #else /* AFS_DEMAND_ATTACH_FS */
1322
1323 void
1324 VShutdown_r(void)
1325 {
1326     int i;
1327     Volume *vp, *np;
1328     afs_int32 code;
1329
1330     if (VInit < 2) {
1331         Log("VShutdown:  aborting attach volumes\n");
1332         vinit_attach_abort = 1;
1333 #ifdef AFS_PTHREAD_ENV
1334         VOL_CV_WAIT(&vol_init_attach_cond);
1335 #else
1336         LWP_WaitProcess(VInitAttachVolumes);
1337 #endif /* AFS_PTHREAD_ENV */
1338     }
1339
1340     Log("VShutdown:  shutting down on-line volumes...\n");
1341     vol_shutting_down = 1;
1342     for (i = 0; i < VolumeHashTable.Size; i++) {
1343         /* try to hold first volume in the hash table */
1344         for (queue_Scan(&VolumeHashTable.Table[i],vp,np,Volume)) {
1345             code = VHold_r(vp);
1346             if (code == 0) {
1347                 if (LogLevel >= 5)
1348                     Log("VShutdown:  Attempting to take volume %u offline.\n",
1349                         vp->hashid);
1350
1351                 /* next, take the volume offline (drops reference count) */
1352                 VOffline_r(vp, "File server was shut down");
1353             }
1354         }
1355     }
1356     Log("VShutdown:  complete.\n");
1357 }
1358 #endif /* AFS_DEMAND_ATTACH_FS */
1359
1360
1361 void
1362 VShutdown(void)
1363 {
1364     osi_Assert(VInit>0);
1365     VOL_LOCK;
1366     VShutdown_r();
1367     VOL_UNLOCK;
1368 }
1369
1370 /**
1371  * stop new activity (e.g. SALVSYNC) from occurring
1372  *
1373  * Use this to make the volume package less busy; for example, during
1374  * shutdown. This doesn't actually shutdown/detach anything in the
1375  * volume package, but prevents certain processes from ocurring. For
1376  * example, preventing new SALVSYNC communication in DAFS. In theory, we
1377  * could also use this to prevent new volume attachment, or prevent
1378  * other programs from checking out volumes, etc.
1379  */
1380 void
1381 VSetTranquil(void)
1382 {
1383 #ifdef AFS_DEMAND_ATTACH_FS
1384     /* make sure we don't try to contact the salvageserver, since it may
1385      * not be around anymore */
1386     vol_disallow_salvsync = 1;
1387 #endif
1388 }
1389
1390 #ifdef AFS_DEMAND_ATTACH_FS
1391 /*
1392  * demand attach fs
1393  * shutdown control thread
1394  */
1395 static void
1396 ShutdownController(vshutdown_thread_t * params)
1397 {
1398     /* XXX debug */
1399     struct DiskPartition64 * diskP;
1400     Device id;
1401     vshutdown_thread_t shadow;
1402
1403     ShutdownCreateSchedule(params);
1404
1405     while ((params->pass < 4) &&
1406            (params->n_threads_complete < params->n_threads)) {
1407         /* recompute schedule once per second */
1408
1409         memcpy(&shadow, params, sizeof(vshutdown_thread_t));
1410
1411         VOL_UNLOCK;
1412         /* XXX debug */
1413         Log("ShutdownController:  schedule version=%d, vol_remaining=%d, pass=%d\n",
1414             shadow.schedule_version, shadow.vol_remaining, shadow.pass);
1415         Log("ShutdownController:  n_threads_complete=%d, n_parts_done_pass=%d\n",
1416             shadow.n_threads_complete, shadow.n_parts_done_pass);
1417         for (diskP = DiskPartitionList; diskP; diskP=diskP->next) {
1418             id = diskP->index;
1419             Log("ShutdownController:  part[%d] : (len=%d, thread_target=%d, done_pass=%d, pass_head=%p)\n",
1420                 id,
1421                 diskP->vol_list.len,
1422                 shadow.part_thread_target[id],
1423                 shadow.part_done_pass[id],
1424                 shadow.part_pass_head[id]);
1425         }
1426
1427         sleep(1);
1428         VOL_LOCK;
1429
1430         ShutdownCreateSchedule(params);
1431     }
1432 }
1433
1434 /* create the shutdown thread work schedule.
1435  * this scheduler tries to implement fairness
1436  * by allocating at least 1 thread to each
1437  * partition with volumes to be shutdown,
1438  * and then it attempts to allocate remaining
1439  * threads based upon the amount of work left
1440  */
1441 static void
1442 ShutdownCreateSchedule(vshutdown_thread_t * params)
1443 {
1444     struct DiskPartition64 * diskP;
1445     int sum, thr_workload, thr_left;
1446     int part_residue[VOLMAXPARTS+1];
1447     Device id;
1448
1449     /* compute the total number of outstanding volumes */
1450     sum = 0;
1451     for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1452         sum += diskP->vol_list.len;
1453     }
1454
1455     params->schedule_version++;
1456     params->vol_remaining = sum;
1457
1458     if (!sum)
1459         return;
1460
1461     /* compute average per-thread workload */
1462     thr_workload = sum / params->n_threads;
1463     if (sum % params->n_threads)
1464         thr_workload++;
1465
1466     thr_left = params->n_threads;
1467     memset(&part_residue, 0, sizeof(part_residue));
1468
1469     /* for fairness, give every partition with volumes remaining
1470      * at least one thread */
1471     for (diskP = DiskPartitionList; diskP && thr_left; diskP = diskP->next) {
1472         id = diskP->index;
1473         if (diskP->vol_list.len) {
1474             params->part_thread_target[id] = 1;
1475             thr_left--;
1476         } else {
1477             params->part_thread_target[id] = 0;
1478         }
1479     }
1480
1481     if (thr_left && thr_workload) {
1482         /* compute length-weighted workloads */
1483         int delta;
1484
1485         for (diskP = DiskPartitionList; diskP && thr_left; diskP = diskP->next) {
1486             id = diskP->index;
1487             delta = (diskP->vol_list.len / thr_workload) -
1488                 params->part_thread_target[id];
1489             if (delta < 0) {
1490                 continue;
1491             }
1492             if (delta < thr_left) {
1493                 params->part_thread_target[id] += delta;
1494                 thr_left -= delta;
1495             } else {
1496                 params->part_thread_target[id] += thr_left;
1497                 thr_left = 0;
1498                 break;
1499             }
1500         }
1501     }
1502
1503     if (thr_left) {
1504         /* try to assign any leftover threads to partitions that
1505          * had volume lengths closer to needing thread_target+1 */
1506         int max_residue, max_id = 0;
1507
1508         /* compute the residues */
1509         for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1510             id = diskP->index;
1511             part_residue[id] = diskP->vol_list.len -
1512                 (params->part_thread_target[id] * thr_workload);
1513         }
1514
1515         /* now try to allocate remaining threads to partitions with the
1516          * highest residues */
1517         while (thr_left) {
1518             max_residue = 0;
1519             for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1520                 id = diskP->index;
1521                 if (part_residue[id] > max_residue) {
1522                     max_residue = part_residue[id];
1523                     max_id = id;
1524                 }
1525             }
1526
1527             if (!max_residue) {
1528                 break;
1529             }
1530
1531             params->part_thread_target[max_id]++;
1532             thr_left--;
1533             part_residue[max_id] = 0;
1534         }
1535     }
1536
1537     if (thr_left) {
1538         /* punt and give any remaining threads equally to each partition */
1539         int alloc;
1540         if (thr_left >= params->n_parts) {
1541             alloc = thr_left / params->n_parts;
1542             for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1543                 id = diskP->index;
1544                 params->part_thread_target[id] += alloc;
1545                 thr_left -= alloc;
1546             }
1547         }
1548
1549         /* finish off the last of the threads */
1550         for (diskP = DiskPartitionList; thr_left && diskP; diskP = diskP->next) {
1551             id = diskP->index;
1552             params->part_thread_target[id]++;
1553             thr_left--;
1554         }
1555     }
1556 }
1557
1558 /* worker thread for parallel shutdown */
1559 static void *
1560 VShutdownThread(void * args)
1561 {
1562     vshutdown_thread_t * params;
1563     int found, pass, schedule_version_save, count;
1564     struct DiskPartition64 *diskP;
1565     struct diskpartition_queue_t * dpq;
1566     Device id;
1567
1568     params = (vshutdown_thread_t *) args;
1569
1570     /* acquire the shutdown pass 0 lock */
1571     MUTEX_ENTER(&params->lock);
1572
1573     /* if there's still pass 0 work to be done,
1574      * get a work entry, and do a pass 0 shutdown */
1575     if (queue_IsNotEmpty(params)) {
1576         dpq = queue_First(params, diskpartition_queue_t);
1577         queue_Remove(dpq);
1578         MUTEX_EXIT(&params->lock);
1579         diskP = dpq->diskP;
1580         free(dpq);
1581         id = diskP->index;
1582
1583         count = 0;
1584         while (ShutdownVolumeWalk_r(diskP, 0, &params->part_pass_head[id]))
1585             count++;
1586         params->stats[0][diskP->index] = count;
1587         MUTEX_ENTER(&params->lock);
1588     }
1589
1590     params->n_threads_complete++;
1591     if (params->n_threads_complete == params->n_threads) {
1592         /* notify control thread that all workers have completed pass 0 */
1593         CV_SIGNAL(&params->master_cv);
1594     }
1595     while (params->pass == 0) {
1596         CV_WAIT(&params->cv, &params->lock);
1597     }
1598
1599     /* switch locks */
1600     MUTEX_EXIT(&params->lock);
1601     VOL_LOCK;
1602
1603     pass = params->pass;
1604     osi_Assert(pass > 0);
1605
1606     /* now escalate through the more complicated shutdowns */
1607     while (pass <= 3) {
1608         schedule_version_save = params->schedule_version;
1609         found = 0;
1610         /* find a disk partition to work on */
1611         for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1612             id = diskP->index;
1613             if (params->part_thread_target[id] && !params->part_done_pass[id]) {
1614                 params->part_thread_target[id]--;
1615                 found = 1;
1616                 break;
1617             }
1618         }
1619
1620         if (!found) {
1621             /* hmm. for some reason the controller thread couldn't find anything for
1622              * us to do. let's see if there's anything we can do */
1623             for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1624                 id = diskP->index;
1625                 if (diskP->vol_list.len && !params->part_done_pass[id]) {
1626                     found = 1;
1627                     break;
1628                 } else if (!params->part_done_pass[id]) {
1629                     params->part_done_pass[id] = 1;
1630                     params->n_parts_done_pass++;
1631                     if (pass == 3) {
1632                         Log("VShutdown:  done shutting down volumes on partition %s.\n",
1633                             VPartitionPath(diskP));
1634                     }
1635                 }
1636             }
1637         }
1638
1639         /* do work on this partition until either the controller
1640          * creates a new schedule, or we run out of things to do
1641          * on this partition */
1642         if (found) {
1643             count = 0;
1644             while (!params->part_done_pass[id] &&
1645                    (schedule_version_save == params->schedule_version)) {
1646                 /* ShutdownVolumeWalk_r will drop the glock internally */
1647                 if (!ShutdownVolumeWalk_r(diskP, pass, &params->part_pass_head[id])) {
1648                     if (!params->part_done_pass[id]) {
1649                         params->part_done_pass[id] = 1;
1650                         params->n_parts_done_pass++;
1651                         if (pass == 3) {
1652                             Log("VShutdown:  done shutting down volumes on partition %s.\n",
1653                                 VPartitionPath(diskP));
1654                         }
1655                     }
1656                     break;
1657                 }
1658                 count++;
1659             }
1660
1661             params->stats[pass][id] += count;
1662         } else {
1663             /* ok, everyone is done this pass, proceed */
1664
1665             /* barrier lock */
1666             params->n_threads_complete++;
1667             while (params->pass == pass) {
1668                 if (params->n_threads_complete == params->n_threads) {
1669                     /* we are the last thread to complete, so we will
1670                      * reinitialize worker pool state for the next pass */
1671                     params->n_threads_complete = 0;
1672                     params->n_parts_done_pass = 0;
1673                     params->pass++;
1674                     for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1675                         id = diskP->index;
1676                         params->part_done_pass[id] = 0;
1677                         params->part_pass_head[id] = queue_First(&diskP->vol_list, rx_queue);
1678                     }
1679
1680                     /* compute a new thread schedule before releasing all the workers */
1681                     ShutdownCreateSchedule(params);
1682
1683                     /* wake up all the workers */
1684                     CV_BROADCAST(&params->cv);
1685
1686                     VOL_UNLOCK;
1687                     Log("VShutdown:  pass %d completed using %d threads on %d partitions\n",
1688                         pass, params->n_threads, params->n_parts);
1689                     VOL_LOCK;
1690                 } else {
1691                     VOL_CV_WAIT(&params->cv);
1692                 }
1693             }
1694             pass = params->pass;
1695         }
1696
1697         /* for fairness */
1698         VOL_UNLOCK;
1699         pthread_yield();
1700         VOL_LOCK;
1701     }
1702
1703     VOL_UNLOCK;
1704
1705     return NULL;
1706 }
1707
1708 /* shut down all volumes on a given disk partition
1709  *
1710  * note that this function will not allow mp-fast
1711  * shutdown of a partition */
1712 int
1713 VShutdownByPartition_r(struct DiskPartition64 * dp)
1714 {
1715     int pass;
1716     int pass_stats[4];
1717     int total;
1718
1719     /* wait for other exclusive ops to finish */
1720     VVByPListWait_r(dp);
1721
1722     /* begin exclusive access */
1723     VVByPListBeginExclusive_r(dp);
1724
1725     /* pick the low-hanging fruit first,
1726      * then do the complicated ones last
1727      * (has the advantage of keeping
1728      *  in-use volumes up until the bitter end) */
1729     for (pass = 0, total=0; pass < 4; pass++) {
1730         pass_stats[pass] = ShutdownVByPForPass_r(dp, pass);
1731         total += pass_stats[pass];
1732     }
1733
1734     /* end exclusive access */
1735     VVByPListEndExclusive_r(dp);
1736
1737     Log("VShutdownByPartition:  shut down %d volumes on %s (pass[0]=%d, pass[1]=%d, pass[2]=%d, pass[3]=%d)\n",
1738         total, VPartitionPath(dp), pass_stats[0], pass_stats[1], pass_stats[2], pass_stats[3]);
1739
1740     return 0;
1741 }
1742
1743 /* internal shutdown functionality
1744  *
1745  * for multi-pass shutdown:
1746  * 0 to only "shutdown" {pre,un}attached and error state volumes
1747  * 1 to also shutdown attached volumes w/ volume header loaded
1748  * 2 to also shutdown attached volumes w/o volume header loaded
1749  * 3 to also shutdown exclusive state volumes
1750  *
1751  * caller MUST hold exclusive access on the hash chain
1752  * because we drop vol_glock_mutex internally
1753  *
1754  * this function is reentrant for passes 1--3
1755  * (e.g. multiple threads can cooperate to
1756  *  shutdown a partition mp-fast)
1757  *
1758  * pass 0 is not scaleable because the volume state data is
1759  * synchronized by vol_glock mutex, and the locking overhead
1760  * is too high to drop the lock long enough to do linked list
1761  * traversal
1762  */
1763 static int
1764 ShutdownVByPForPass_r(struct DiskPartition64 * dp, int pass)
1765 {
1766     struct rx_queue * q = queue_First(&dp->vol_list, rx_queue);
1767     int i = 0;
1768
1769     while (ShutdownVolumeWalk_r(dp, pass, &q))
1770         i++;
1771
1772     return i;
1773 }
1774
1775 /* conditionally shutdown one volume on partition dp
1776  * returns 1 if a volume was shutdown in this pass,
1777  * 0 otherwise */
1778 static int
1779 ShutdownVolumeWalk_r(struct DiskPartition64 * dp, int pass,
1780                      struct rx_queue ** idx)
1781 {
1782     struct rx_queue *qp, *nqp;
1783     Volume * vp;
1784
1785     qp = *idx;
1786
1787     for (queue_ScanFrom(&dp->vol_list, qp, qp, nqp, rx_queue)) {
1788         vp = (Volume *) (((char *)qp) - offsetof(Volume, vol_list));
1789
1790         switch (pass) {
1791         case 0:
1792             if ((V_attachState(vp) != VOL_STATE_UNATTACHED) &&
1793                 (V_attachState(vp) != VOL_STATE_ERROR) &&
1794                 (V_attachState(vp) != VOL_STATE_DELETED) &&
1795                 (V_attachState(vp) != VOL_STATE_PREATTACHED)) {
1796                 break;
1797             }
1798         case 1:
1799             if ((V_attachState(vp) == VOL_STATE_ATTACHED) &&
1800                 (vp->header == NULL)) {
1801                 break;
1802             }
1803         case 2:
1804             if (VIsExclusiveState(V_attachState(vp))) {
1805                 break;
1806             }
1807         case 3:
1808             *idx = nqp;
1809             DeleteVolumeFromVByPList_r(vp);
1810             VShutdownVolume_r(vp);
1811             vp = NULL;
1812             return 1;
1813         }
1814     }
1815
1816     return 0;
1817 }
1818
1819 /*
1820  * shutdown a specific volume
1821  */
1822 /* caller MUST NOT hold a heavyweight ref on vp */
1823 int
1824 VShutdownVolume_r(Volume * vp)
1825 {
1826     int code;
1827
1828     VCreateReservation_r(vp);
1829
1830     if (LogLevel >= 5) {
1831         Log("VShutdownVolume_r:  vid=%u, device=%d, state=%hu\n",
1832             vp->hashid, vp->partition->device, V_attachState(vp));
1833     }
1834
1835     /* wait for other blocking ops to finish */
1836     VWaitExclusiveState_r(vp);
1837
1838     osi_Assert(VIsValidState(V_attachState(vp)));
1839
1840     switch(V_attachState(vp)) {
1841     case VOL_STATE_SALVAGING:
1842         /* Leave salvaging volumes alone. Any in-progress salvages will
1843          * continue working after viced shuts down. This is intentional.
1844          */
1845
1846     case VOL_STATE_PREATTACHED:
1847     case VOL_STATE_ERROR:
1848         VChangeState_r(vp, VOL_STATE_UNATTACHED);
1849     case VOL_STATE_UNATTACHED:
1850     case VOL_STATE_DELETED:
1851         break;
1852     case VOL_STATE_GOING_OFFLINE:
1853     case VOL_STATE_SHUTTING_DOWN:
1854     case VOL_STATE_ATTACHED:
1855         code = VHold_r(vp);
1856         if (!code) {
1857             if (LogLevel >= 5)
1858                 Log("VShutdown:  Attempting to take volume %u offline.\n",
1859                     vp->hashid);
1860
1861             /* take the volume offline (drops reference count) */
1862             VOffline_r(vp, "File server was shut down");
1863         }
1864         break;
1865     default:
1866         break;
1867     }
1868
1869     VCancelReservation_r(vp);
1870     vp = NULL;
1871     return 0;
1872 }
1873 #endif /* AFS_DEMAND_ATTACH_FS */
1874
1875
1876 /***************************************************/
1877 /* Header I/O routines                             */
1878 /***************************************************/
1879
1880 /* open a descriptor for the inode (h),
1881  * read in an on-disk structure into buffer (to) of size (size),
1882  * verify versionstamp in structure has magic (magic) and
1883  * optionally verify version (version) if (version) is nonzero
1884  */
1885 static void
1886 ReadHeader(Error * ec, IHandle_t * h, char *to, int size, bit32 magic,
1887            bit32 version)
1888 {
1889     struct versionStamp *vsn;
1890     FdHandle_t *fdP;
1891
1892     *ec = 0;
1893     if (h == NULL) {
1894         *ec = VSALVAGE;
1895         return;
1896     }
1897
1898     fdP = IH_OPEN(h);
1899     if (fdP == NULL) {
1900         *ec = VSALVAGE;
1901         return;
1902     }
1903
1904     vsn = (struct versionStamp *)to;
1905     if (FDH_PREAD(fdP, to, size, 0) != size || vsn->magic != magic) {
1906         *ec = VSALVAGE;
1907         FDH_REALLYCLOSE(fdP);
1908         return;
1909     }
1910     FDH_CLOSE(fdP);
1911
1912     /* Check is conditional, in case caller wants to inspect version himself */
1913     if (version && vsn->version != version) {
1914         *ec = VSALVAGE;
1915     }
1916 }
1917
1918 void
1919 WriteVolumeHeader_r(Error * ec, Volume * vp)
1920 {
1921     IHandle_t *h = V_diskDataHandle(vp);
1922     FdHandle_t *fdP;
1923
1924     *ec = 0;
1925
1926     fdP = IH_OPEN(h);
1927     if (fdP == NULL) {
1928         *ec = VSALVAGE;
1929         return;
1930     }
1931     if (FDH_PWRITE(fdP, (char *)&V_disk(vp), sizeof(V_disk(vp)), 0)
1932         != sizeof(V_disk(vp))) {
1933         *ec = VSALVAGE;
1934         FDH_REALLYCLOSE(fdP);
1935         return;
1936     }
1937     FDH_CLOSE(fdP);
1938 }
1939
1940 /* VolumeHeaderToDisk
1941  * Allows for storing 64 bit inode numbers in on-disk volume header
1942  * file.
1943  */
1944 /* convert in-memory representation of a volume header to the
1945  * on-disk representation of a volume header */
1946 void
1947 VolumeHeaderToDisk(VolumeDiskHeader_t * dh, VolumeHeader_t * h)
1948 {
1949
1950     memset(dh, 0, sizeof(VolumeDiskHeader_t));
1951     dh->stamp = h->stamp;
1952     dh->id = h->id;
1953     dh->parent = h->parent;
1954
1955 #ifdef AFS_64BIT_IOPS_ENV
1956     dh->volumeInfo_lo = (afs_int32) h->volumeInfo & 0xffffffff;
1957     dh->volumeInfo_hi = (afs_int32) (h->volumeInfo >> 32) & 0xffffffff;
1958     dh->smallVnodeIndex_lo = (afs_int32) h->smallVnodeIndex & 0xffffffff;
1959     dh->smallVnodeIndex_hi =
1960         (afs_int32) (h->smallVnodeIndex >> 32) & 0xffffffff;
1961     dh->largeVnodeIndex_lo = (afs_int32) h->largeVnodeIndex & 0xffffffff;
1962     dh->largeVnodeIndex_hi =
1963         (afs_int32) (h->largeVnodeIndex >> 32) & 0xffffffff;
1964     dh->linkTable_lo = (afs_int32) h->linkTable & 0xffffffff;
1965     dh->linkTable_hi = (afs_int32) (h->linkTable >> 32) & 0xffffffff;
1966 #else
1967     dh->volumeInfo_lo = h->volumeInfo;
1968     dh->smallVnodeIndex_lo = h->smallVnodeIndex;
1969     dh->largeVnodeIndex_lo = h->largeVnodeIndex;
1970     dh->linkTable_lo = h->linkTable;
1971 #endif
1972 }
1973
1974 /* DiskToVolumeHeader
1975  * Converts an on-disk representation of a volume header to
1976  * the in-memory representation of a volume header.
1977  *
1978  * Makes the assumption that AFS has *always*
1979  * zero'd the volume header file so that high parts of inode
1980  * numbers are 0 in older (SGI EFS) volume header files.
1981  */
1982 void
1983 DiskToVolumeHeader(VolumeHeader_t * h, VolumeDiskHeader_t * dh)
1984 {
1985     memset(h, 0, sizeof(VolumeHeader_t));
1986     h->stamp = dh->stamp;
1987     h->id = dh->id;
1988     h->parent = dh->parent;
1989
1990 #ifdef AFS_64BIT_IOPS_ENV
1991     h->volumeInfo =
1992         (Inode) dh->volumeInfo_lo | ((Inode) dh->volumeInfo_hi << 32);
1993
1994     h->smallVnodeIndex =
1995         (Inode) dh->smallVnodeIndex_lo | ((Inode) dh->
1996                                           smallVnodeIndex_hi << 32);
1997
1998     h->largeVnodeIndex =
1999         (Inode) dh->largeVnodeIndex_lo | ((Inode) dh->
2000                                           largeVnodeIndex_hi << 32);
2001     h->linkTable =
2002         (Inode) dh->linkTable_lo | ((Inode) dh->linkTable_hi << 32);
2003 #else
2004     h->volumeInfo = dh->volumeInfo_lo;
2005     h->smallVnodeIndex = dh->smallVnodeIndex_lo;
2006     h->largeVnodeIndex = dh->largeVnodeIndex_lo;
2007     h->linkTable = dh->linkTable_lo;
2008 #endif
2009 }
2010
2011
2012 /***************************************************/
2013 /* Volume Attachment routines                      */
2014 /***************************************************/
2015
2016 #ifdef AFS_DEMAND_ATTACH_FS
2017 /**
2018  * pre-attach a volume given its path.
2019  *
2020  * @param[out] ec         outbound error code
2021  * @param[in]  partition  partition path string
2022  * @param[in]  name       volume id string
2023  *
2024  * @return volume object pointer
2025  *
2026  * @note A pre-attached volume will only have its partition
2027  *       and hashid fields initialized.  At first call to
2028  *       VGetVolume, the volume will be fully attached.
2029  *
2030  */
2031 Volume *
2032 VPreAttachVolumeByName(Error * ec, char *partition, char *name)
2033 {
2034     Volume * vp;
2035     VOL_LOCK;
2036     vp = VPreAttachVolumeByName_r(ec, partition, name);
2037     VOL_UNLOCK;
2038     return vp;
2039 }
2040
2041 /**
2042  * pre-attach a volume given its path.
2043  *
2044  * @param[out] ec         outbound error code
2045  * @param[in]  partition  path to vice partition
2046  * @param[in]  name       volume id string
2047  *
2048  * @return volume object pointer
2049  *
2050  * @pre VOL_LOCK held
2051  *
2052  * @internal volume package internal use only.
2053  */
2054 Volume *
2055 VPreAttachVolumeByName_r(Error * ec, char *partition, char *name)
2056 {
2057     return VPreAttachVolumeById_r(ec,
2058                                   partition,
2059                                   VolumeNumber(name));
2060 }
2061
2062 /**
2063  * pre-attach a volume given its path and numeric volume id.
2064  *
2065  * @param[out] ec          error code return
2066  * @param[in]  partition   path to vice partition
2067  * @param[in]  volumeId    numeric volume id
2068  *
2069  * @return volume object pointer
2070  *
2071  * @pre VOL_LOCK held
2072  *
2073  * @internal volume package internal use only.
2074  */
2075 Volume *
2076 VPreAttachVolumeById_r(Error * ec,
2077                        char * partition,
2078                        VolId volumeId)
2079 {
2080     Volume *vp;
2081     struct DiskPartition64 *partp;
2082
2083     *ec = 0;
2084
2085     osi_Assert(programType == fileServer);
2086
2087     if (!(partp = VGetPartition_r(partition, 0))) {
2088         *ec = VNOVOL;
2089         Log("VPreAttachVolumeById_r:  Error getting partition (%s)\n", partition);
2090         return NULL;
2091     }
2092
2093     vp = VLookupVolume_r(ec, volumeId, NULL);
2094     if (*ec) {
2095         return NULL;
2096     }
2097
2098     return VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
2099 }
2100
2101 /**
2102  * preattach a volume.
2103  *
2104  * @param[out] ec     outbound error code
2105  * @param[in]  partp  pointer to partition object
2106  * @param[in]  vp     pointer to volume object
2107  * @param[in]  vid    volume id
2108  *
2109  * @return volume object pointer
2110  *
2111  * @pre VOL_LOCK is held.
2112  *
2113  * @warning Returned volume object pointer does not have to
2114  *          equal the pointer passed in as argument vp.  There
2115  *          are potential race conditions which can result in
2116  *          the pointers having different values.  It is up to
2117  *          the caller to make sure that references are handled
2118  *          properly in this case.
2119  *
2120  * @note If there is already a volume object registered with
2121  *       the same volume id, its pointer MUST be passed as
2122  *       argument vp.  Failure to do so will result in a silent
2123  *       failure to preattach.
2124  *
2125  * @internal volume package internal use only.
2126  */
2127 Volume *
2128 VPreAttachVolumeByVp_r(Error * ec,
2129                        struct DiskPartition64 * partp,
2130                        Volume * vp,
2131                        VolId vid)
2132 {
2133     Volume *nvp = NULL;
2134
2135     *ec = 0;
2136
2137     /* check to see if pre-attach already happened */
2138     if (vp &&
2139         (V_attachState(vp) != VOL_STATE_UNATTACHED) &&
2140         (V_attachState(vp) != VOL_STATE_DELETED) &&
2141         (V_attachState(vp) != VOL_STATE_PREATTACHED) &&
2142         !VIsErrorState(V_attachState(vp))) {
2143         /*
2144          * pre-attach is a no-op in all but the following cases:
2145          *
2146          *   - volume is unattached
2147          *   - volume is in an error state
2148          *   - volume is pre-attached
2149          */
2150         Log("VPreattachVolumeByVp_r: volume %u not in quiescent state\n", vid);
2151         goto done;
2152     } else if (vp) {
2153         /* we're re-attaching a volume; clear out some old state */
2154         memset(&vp->salvage, 0, sizeof(struct VolumeOnlineSalvage));
2155
2156         if (V_partition(vp) != partp) {
2157             /* XXX potential race */
2158             DeleteVolumeFromVByPList_r(vp);
2159         }
2160     } else {
2161         /* if we need to allocate a new Volume struct,
2162          * go ahead and drop the vol glock, otherwise
2163          * do the basic setup synchronised, as it's
2164          * probably not worth dropping the lock */
2165         VOL_UNLOCK;
2166
2167         /* allocate the volume structure */
2168         vp = nvp = (Volume *) malloc(sizeof(Volume));
2169         osi_Assert(vp != NULL);
2170         memset(vp, 0, sizeof(Volume));
2171         queue_Init(&vp->vnode_list);
2172         queue_Init(&vp->rx_call_list);
2173         CV_INIT(&V_attachCV(vp), "vp attach", CV_DEFAULT, 0);
2174     }
2175
2176     /* link the volume with its associated vice partition */
2177     vp->device = partp->device;
2178     vp->partition = partp;
2179
2180     vp->hashid = vid;
2181     vp->specialStatus = 0;
2182
2183     /* if we dropped the lock, reacquire the lock,
2184      * check for pre-attach races, and then add
2185      * the volume to the hash table */
2186     if (nvp) {
2187         VOL_LOCK;
2188         nvp = VLookupVolume_r(ec, vid, NULL);
2189         if (*ec) {
2190             free(vp);
2191             vp = NULL;
2192             goto done;
2193         } else if (nvp) { /* race detected */
2194             free(vp);
2195             vp = nvp;
2196             goto done;
2197         } else {
2198           /* hack to make up for VChangeState_r() decrementing
2199            * the old state counter */
2200           VStats.state_levels[0]++;
2201         }
2202     }
2203
2204     /* put pre-attached volume onto the hash table
2205      * and bring it up to the pre-attached state */
2206     AddVolumeToHashTable(vp, vp->hashid);
2207     AddVolumeToVByPList_r(vp);
2208     VLRU_Init_Node_r(vp);
2209     VChangeState_r(vp, VOL_STATE_PREATTACHED);
2210
2211     if (LogLevel >= 5)
2212         Log("VPreAttachVolumeByVp_r:  volume %u pre-attached\n", vp->hashid);
2213
2214   done:
2215     if (*ec)
2216         return NULL;
2217     else
2218         return vp;
2219 }
2220 #endif /* AFS_DEMAND_ATTACH_FS */
2221
2222 /* Attach an existing volume, given its pathname, and return a
2223    pointer to the volume header information.  The volume also
2224    normally goes online at this time.  An offline volume
2225    must be reattached to make it go online */
2226 Volume *
2227 VAttachVolumeByName(Error * ec, char *partition, char *name, int mode)
2228 {
2229     Volume *retVal;
2230     VOL_LOCK;
2231     retVal = VAttachVolumeByName_r(ec, partition, name, mode);
2232     VOL_UNLOCK;
2233     return retVal;
2234 }
2235
2236 Volume *
2237 VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
2238 {
2239     Volume *vp = NULL;
2240     struct DiskPartition64 *partp;
2241     char path[64];
2242     int isbusy = 0;
2243     VolId volumeId;
2244     int checkedOut;
2245 #ifdef AFS_DEMAND_ATTACH_FS
2246     VolumeStats stats_save;
2247     Volume *svp = NULL;
2248 #endif /* AFS_DEMAND_ATTACH_FS */
2249
2250     *ec = 0;
2251
2252     volumeId = VolumeNumber(name);
2253
2254     if (!(partp = VGetPartition_r(partition, 0))) {
2255         *ec = VNOVOL;
2256         Log("VAttachVolume: Error getting partition (%s)\n", partition);
2257         goto done;
2258     }
2259
2260     if (VRequiresPartLock()) {
2261         osi_Assert(VInit == 3);
2262         VLockPartition_r(partition);
2263     } else if (programType == fileServer) {
2264 #ifdef AFS_DEMAND_ATTACH_FS
2265         /* lookup the volume in the hash table */
2266         vp = VLookupVolume_r(ec, volumeId, NULL);
2267         if (*ec) {
2268             return NULL;
2269         }
2270
2271         if (vp) {
2272             /* save any counters that are supposed to
2273              * be monotonically increasing over the
2274              * lifetime of the fileserver */
2275             memcpy(&stats_save, &vp->stats, sizeof(VolumeStats));
2276         } else {
2277             memset(&stats_save, 0, sizeof(VolumeStats));
2278         }
2279
2280         /* if there's something in the hash table, and it's not
2281          * in the pre-attach state, then we may need to detach
2282          * it before proceeding */
2283         if (vp && (V_attachState(vp) != VOL_STATE_PREATTACHED)) {
2284             VCreateReservation_r(vp);
2285             VWaitExclusiveState_r(vp);
2286
2287             /* at this point state must be one of:
2288              *   - UNATTACHED
2289              *   - ATTACHED
2290              *   - SHUTTING_DOWN
2291              *   - GOING_OFFLINE
2292              *   - SALVAGING
2293              *   - ERROR
2294              *   - DELETED
2295              */
2296
2297             if (vp->specialStatus == VBUSY)
2298                 isbusy = 1;
2299
2300             /* if it's already attached, see if we can return it */
2301             if (V_attachState(vp) == VOL_STATE_ATTACHED) {
2302                 VGetVolumeByVp_r(ec, vp);
2303                 if (V_inUse(vp) == fileServer) {
2304                     VCancelReservation_r(vp);
2305                     return vp;
2306                 }
2307
2308                 /* otherwise, we need to detach, and attempt to re-attach */
2309                 VDetachVolume_r(ec, vp);
2310                 if (*ec) {
2311                     Log("VAttachVolume: Error detaching old volume instance (%s)\n", name);
2312                 }
2313             } else {
2314                 /* if it isn't fully attached, delete from the hash tables,
2315                    and let the refcounter handle the rest */
2316                 DeleteVolumeFromHashTable(vp);
2317                 DeleteVolumeFromVByPList_r(vp);
2318             }
2319
2320             VCancelReservation_r(vp);
2321             vp = NULL;
2322         }
2323
2324         /* pre-attach volume if it hasn't been done yet */
2325         if (!vp ||
2326             (V_attachState(vp) == VOL_STATE_UNATTACHED) ||
2327             (V_attachState(vp) == VOL_STATE_DELETED) ||
2328             (V_attachState(vp) == VOL_STATE_ERROR)) {
2329             svp = vp;
2330             vp = VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
2331             if (*ec) {
2332                 return NULL;
2333             }
2334         }
2335
2336         osi_Assert(vp != NULL);
2337
2338         /* handle pre-attach races
2339          *
2340          * multiple threads can race to pre-attach a volume,
2341          * but we can't let them race beyond that
2342          *
2343          * our solution is to let the first thread to bring
2344          * the volume into an exclusive state win; the other
2345          * threads just wait until it finishes bringing the
2346          * volume online, and then they do a vgetvolumebyvp
2347          */
2348         if (svp && (svp != vp)) {
2349             /* wait for other exclusive ops to finish */
2350             VCreateReservation_r(vp);
2351             VWaitExclusiveState_r(vp);
2352
2353             /* get a heavyweight ref, kill the lightweight ref, and return */
2354             VGetVolumeByVp_r(ec, vp);
2355             VCancelReservation_r(vp);
2356             return vp;
2357         }
2358
2359         /* at this point, we are chosen as the thread to do
2360          * demand attachment for this volume. all other threads
2361          * doing a getvolume on vp->hashid will block until we finish */
2362
2363         /* make sure any old header cache entries are invalidated
2364          * before proceeding */
2365         FreeVolumeHeader(vp);
2366
2367         VChangeState_r(vp, VOL_STATE_ATTACHING);
2368
2369         /* restore any saved counters */
2370         memcpy(&vp->stats, &stats_save, sizeof(VolumeStats));
2371 #else /* AFS_DEMAND_ATTACH_FS */
2372         vp = VGetVolume_r(ec, volumeId);
2373         if (vp) {
2374             if (V_inUse(vp) == fileServer)
2375                 return vp;
2376             if (vp->specialStatus == VBUSY)
2377                 isbusy = 1;
2378             VDetachVolume_r(ec, vp);
2379             if (*ec) {
2380                 Log("VAttachVolume: Error detaching volume (%s)\n", name);
2381             }
2382             vp = NULL;
2383         }
2384 #endif /* AFS_DEMAND_ATTACH_FS */
2385     }
2386
2387     *ec = 0;
2388     strcpy(path, VPartitionPath(partp));
2389
2390     VOL_UNLOCK;
2391
2392     strcat(path, OS_DIRSEP);
2393     strcat(path, name);
2394
2395     if (!vp) {
2396       vp = (Volume *) calloc(1, sizeof(Volume));
2397       osi_Assert(vp != NULL);
2398       vp->hashid = volumeId;
2399       vp->device = partp->device;
2400       vp->partition = partp;
2401       queue_Init(&vp->vnode_list);
2402       queue_Init(&vp->rx_call_list);
2403 #ifdef AFS_DEMAND_ATTACH_FS
2404       CV_INIT(&V_attachCV(vp), "vp attach", CV_DEFAULT, 0);
2405 #endif /* AFS_DEMAND_ATTACH_FS */
2406     }
2407
2408     /* attach2 is entered without any locks, and returns
2409      * with vol_glock_mutex held */
2410     vp = attach2(ec, volumeId, path, partp, vp, isbusy, mode, &checkedOut);
2411
2412     if (VCanUseFSSYNC() && vp) {
2413 #ifdef AFS_DEMAND_ATTACH_FS
2414         if ((mode == V_VOLUPD) || (VolumeWriteable(vp) && (mode == V_CLONE))) {
2415             /* mark volume header as in use so that volser crashes lead to a
2416              * salvage attempt */
2417             VUpdateVolume_r(ec, vp, 0);
2418         }
2419         /* for dafs, we should tell the fileserver, except for V_PEEK
2420          * where we know it is not necessary */
2421         if (mode == V_PEEK) {
2422             vp->needsPutBack = 0;
2423         } else {
2424             vp->needsPutBack = VOL_PUTBACK;
2425         }
2426 #else /* !AFS_DEMAND_ATTACH_FS */
2427         /* duplicate computation in fssync.c about whether the server
2428          * takes the volume offline or not.  If the volume isn't
2429          * offline, we must not return it when we detach the volume,
2430          * or the server will abort */
2431         if (mode == V_READONLY || mode == V_PEEK
2432             || (!VolumeWriteable(vp) && (mode == V_CLONE || mode == V_DUMP)))
2433             vp->needsPutBack = 0;
2434         else
2435             vp->needsPutBack = VOL_PUTBACK;
2436 #endif /* !AFS_DEMAND_ATTACH_FS */
2437     }
2438 #ifdef FSSYNC_BUILD_CLIENT
2439     /* Only give back the vol to the fileserver if we checked it out; attach2
2440      * will set checkedOut only if we successfully checked it out from the
2441      * fileserver. */
2442     if (VCanUseFSSYNC() && vp == NULL && checkedOut) {
2443
2444 #ifdef AFS_DEMAND_ATTACH_FS
2445         /* If we couldn't attach but we scheduled a salvage, we already
2446          * notified the fileserver; don't online it now */
2447         if (*ec != VSALVAGING)
2448 #endif /* AFS_DEMAND_ATTACH_FS */
2449         FSYNC_VolOp(volumeId, partition, FSYNC_VOL_ON, 0, NULL);
2450     } else
2451 #endif
2452     if (programType == fileServer && vp) {
2453 #ifdef AFS_DEMAND_ATTACH_FS
2454         /*
2455          * we can get here in cases where we don't "own"
2456          * the volume (e.g. volume owned by a utility).
2457          * short circuit around potential disk header races.
2458          */
2459         if (V_attachState(vp) != VOL_STATE_ATTACHED) {
2460             goto done;
2461         }
2462 #endif
2463         VUpdateVolume_r(ec, vp, 0);
2464         if (*ec) {
2465             Log("VAttachVolume: Error updating volume\n");
2466             if (vp)
2467                 VPutVolume_r(vp);
2468             goto done;
2469         }
2470         if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
2471 #ifndef AFS_DEMAND_ATTACH_FS
2472             /* This is a hack: by temporarily setting the incore
2473              * dontSalvage flag ON, the volume will be put back on the
2474              * Update list (with dontSalvage OFF again).  It will then
2475              * come back in N minutes with DONT_SALVAGE eventually
2476              * set.  This is the way that volumes that have never had
2477              * it set get it set; or that volumes that have been
2478              * offline without DONT SALVAGE having been set also
2479              * eventually get it set */
2480             V_dontSalvage(vp) = DONT_SALVAGE;
2481 #endif /* !AFS_DEMAND_ATTACH_FS */
2482             VAddToVolumeUpdateList_r(ec, vp);
2483             if (*ec) {
2484                 Log("VAttachVolume: Error adding volume to update list\n");
2485                 if (vp)
2486                     VPutVolume_r(vp);
2487                 goto done;
2488             }
2489         }
2490         if (LogLevel)
2491             Log("VOnline:  volume %u (%s) attached and online\n", V_id(vp),
2492                 V_name(vp));
2493     }
2494
2495   done:
2496     if (VRequiresPartLock()) {
2497         VUnlockPartition_r(partition);
2498     }
2499     if (*ec) {
2500 #ifdef AFS_DEMAND_ATTACH_FS
2501         /* attach failed; make sure we're in error state */
2502         if (vp && !VIsErrorState(V_attachState(vp))) {
2503             VChangeState_r(vp, VOL_STATE_ERROR);
2504         }
2505 #endif /* AFS_DEMAND_ATTACH_FS */
2506         return NULL;
2507     } else {
2508         return vp;
2509     }
2510 }
2511
2512 #ifdef AFS_DEMAND_ATTACH_FS
2513 /* VAttachVolumeByVp_r
2514  *
2515  * finish attaching a volume that is
2516  * in a less than fully attached state
2517  */
2518 /* caller MUST hold a ref count on vp */
2519 static Volume *
2520 VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
2521 {
2522     char name[VMAXPATHLEN];
2523     int reserve = 0;
2524     struct DiskPartition64 *partp;
2525     char path[64];
2526     int isbusy = 0;
2527     VolId volumeId;
2528     Volume * nvp = NULL;
2529     VolumeStats stats_save;
2530     int checkedOut;
2531     *ec = 0;
2532
2533     /* volume utility should never call AttachByVp */
2534     osi_Assert(programType == fileServer);
2535
2536     volumeId = vp->hashid;
2537     partp = vp->partition;
2538     VolumeExternalName_r(volumeId, name, sizeof(name));
2539
2540
2541     /* if another thread is performing a blocking op, wait */
2542     VWaitExclusiveState_r(vp);
2543
2544     memcpy(&stats_save, &vp->stats, sizeof(VolumeStats));
2545
2546     /* if it's already attached, see if we can return it */
2547     if (V_attachState(vp) == VOL_STATE_ATTACHED) {
2548         VGetVolumeByVp_r(ec, vp);
2549         if (V_inUse(vp) == fileServer) {
2550             return vp;
2551         } else {
2552             if (vp->specialStatus == VBUSY)
2553                 isbusy = 1;
2554             VDetachVolume_r(ec, vp);
2555             if (*ec) {
2556                 Log("VAttachVolume: Error detaching volume (%s)\n", name);
2557             }
2558             vp = NULL;
2559         }
2560     }
2561
2562     /* pre-attach volume if it hasn't been done yet */
2563     if (!vp ||
2564         (V_attachState(vp) == VOL_STATE_UNATTACHED) ||
2565         (V_attachState(vp) == VOL_STATE_DELETED) ||
2566         (V_attachState(vp) == VOL_STATE_ERROR)) {
2567         nvp = VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
2568         if (*ec) {
2569             return NULL;
2570         }
2571         if (nvp != vp) {
2572             reserve = 1;
2573             VCreateReservation_r(nvp);
2574             vp = nvp;
2575         }
2576     }
2577
2578     osi_Assert(vp != NULL);
2579     VChangeState_r(vp, VOL_STATE_ATTACHING);
2580
2581     /* restore monotonically increasing stats */
2582     memcpy(&vp->stats, &stats_save, sizeof(VolumeStats));
2583
2584     *ec = 0;
2585
2586     /* compute path to disk header */
2587     strcpy(path, VPartitionPath(partp));
2588
2589     VOL_UNLOCK;
2590
2591     strcat(path, OS_DIRSEP);
2592     strcat(path, name);
2593
2594     /* do volume attach
2595      *
2596      * NOTE: attach2 is entered without any locks, and returns
2597      * with vol_glock_mutex held */
2598     vp = attach2(ec, volumeId, path, partp, vp, isbusy, mode, &checkedOut);
2599
2600     /*
2601      * the event that an error was encountered, or
2602      * the volume was not brought to an attached state
2603      * for any reason, skip to the end.  We cannot
2604      * safely call VUpdateVolume unless we "own" it.
2605      */
2606     if (*ec ||
2607         (vp == NULL) ||
2608         (V_attachState(vp) != VOL_STATE_ATTACHED)) {
2609         goto done;
2610     }
2611
2612     VUpdateVolume_r(ec, vp, 0);
2613     if (*ec) {
2614         Log("VAttachVolume: Error updating volume %u\n", vp->hashid);
2615         VPutVolume_r(vp);
2616         goto done;
2617     }
2618     if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
2619 #ifndef AFS_DEMAND_ATTACH_FS
2620         /* This is a hack: by temporarily setting the incore
2621          * dontSalvage flag ON, the volume will be put back on the
2622          * Update list (with dontSalvage OFF again).  It will then
2623          * come back in N minutes with DONT_SALVAGE eventually
2624          * set.  This is the way that volumes that have never had
2625          * it set get it set; or that volumes that have been
2626          * offline without DONT SALVAGE having been set also
2627          * eventually get it set */
2628         V_dontSalvage(vp) = DONT_SALVAGE;
2629 #endif /* !AFS_DEMAND_ATTACH_FS */
2630         VAddToVolumeUpdateList_r(ec, vp);
2631         if (*ec) {
2632             Log("VAttachVolume: Error adding volume %u to update list\n", vp->hashid);
2633             if (vp)
2634                 VPutVolume_r(vp);
2635             goto done;
2636         }
2637     }
2638     if (LogLevel)
2639         Log("VOnline:  volume %u (%s) attached and online\n", V_id(vp),
2640             V_name(vp));
2641   done:
2642     if (reserve) {
2643         VCancelReservation_r(nvp);
2644         reserve = 0;
2645     }
2646     if (*ec && (*ec != VOFFLINE) && (*ec != VSALVAGE)) {
2647         if (vp && !VIsErrorState(V_attachState(vp))) {
2648             VChangeState_r(vp, VOL_STATE_ERROR);
2649         }
2650         return NULL;
2651     } else {
2652         return vp;
2653     }
2654 }
2655
2656 /**
2657  * lock a volume on disk (non-blocking).
2658  *
2659  * @param[in] vp  The volume to lock
2660  * @param[in] locktype READ_LOCK or WRITE_LOCK
2661  *
2662  * @return operation status
2663  *  @retval 0 success, lock was obtained
2664  *  @retval EBUSY a conflicting lock was held by another process
2665  *  @retval EIO   error acquiring lock
2666  *
2667  * @pre If we're in the fileserver, vp is in an exclusive state
2668  *
2669  * @pre vp is not already locked
2670  */
2671 static int
2672 VLockVolumeNB(Volume *vp, int locktype)
2673 {
2674     int code;
2675
2676     osi_Assert(programType != fileServer || VIsExclusiveState(V_attachState(vp)));
2677     osi_Assert(!(V_attachFlags(vp) & VOL_LOCKED));
2678
2679     code = VLockVolumeByIdNB(vp->hashid, vp->partition, locktype);
2680     if (code == 0) {
2681         V_attachFlags(vp) |= VOL_LOCKED;
2682     }
2683
2684     return code;
2685 }
2686
2687 /**
2688  * unlock a volume on disk that was locked with VLockVolumeNB.
2689  *
2690  * @param[in] vp  volume to unlock
2691  *
2692  * @pre If we're in the fileserver, vp is in an exclusive state
2693  *
2694  * @pre vp has already been locked
2695  */
2696 static void
2697 VUnlockVolume(Volume *vp)
2698 {
2699     osi_Assert(programType != fileServer || VIsExclusiveState(V_attachState(vp)));
2700     osi_Assert((V_attachFlags(vp) & VOL_LOCKED));
2701
2702     VUnlockVolumeById(vp->hashid, vp->partition);
2703
2704     V_attachFlags(vp) &= ~VOL_LOCKED;
2705 }
2706 #endif /* AFS_DEMAND_ATTACH_FS */
2707
2708 /**
2709  * read in a vol header, possibly lock the vol header, and possibly check out
2710  * the vol header from the fileserver, as part of volume attachment.
2711  *
2712  * @param[out] ec     error code
2713  * @param[in] vp      volume pointer object
2714  * @param[in] partp   disk partition object of the attaching partition
2715  * @param[in] mode    attachment mode such as V_VOLUPD, V_DUMP, etc (see
2716  *                    volume.h)
2717  * @param[in] peek    1 to just try to read in the volume header and make sure
2718  *                    we don't try to lock the vol, or check it out from
2719  *                    FSSYNC or anything like that; 0 otherwise, for 'normal'
2720  *                    operation
2721  * @param[out] acheckedOut   If we successfully checked-out the volume from
2722  *                           the fileserver (if we needed to), this is set
2723  *                           to 1, otherwise it is untouched.
2724  *
2725  * @note As part of DAFS volume attachment, the volume header may be either
2726  *       read- or write-locked to ensure mutual exclusion of certain volume
2727  *       operations. In some cases in order to determine whether we need to
2728  *       read- or write-lock the header, we need to read in the header to see
2729  *       if the volume is RW or not. So, if we read in the header under a
2730  *       read-lock and determine that we actually need a write-lock on the
2731  *       volume header, this function will drop the read lock, acquire a write
2732  *       lock, and read the header in again.
2733  */
2734 static void
2735 attach_volume_header(Error *ec, Volume *vp, struct DiskPartition64 *partp,
2736                      int mode, int peek, int *acheckedOut)
2737 {
2738     struct VolumeDiskHeader diskHeader;
2739     struct VolumeHeader header;
2740     int code;
2741     int first_try = 1;
2742     int lock_tries = 0, checkout_tries = 0;
2743     int retry;
2744     VolumeId volid = vp->hashid;
2745 #ifdef FSSYNC_BUILD_CLIENT
2746     int checkout, done_checkout = 0;
2747 #endif /* FSSYNC_BUILD_CLIENT */
2748 #ifdef AFS_DEMAND_ATTACH_FS
2749     int locktype = 0, use_locktype = -1;
2750 #endif /* AFS_DEMAND_ATTACH_FS */
2751
2752  retry:
2753     retry = 0;
2754     *ec = 0;
2755
2756     if (lock_tries > VOL_MAX_CHECKOUT_RETRIES) {
2757         Log("VAttachVolume: retried too many times trying to lock header for "
2758             "vol %lu part %s; giving up\n", afs_printable_uint32_lu(volid),
2759             VPartitionPath(partp));
2760         *ec = VNOVOL;
2761         goto done;
2762     }
2763     if (checkout_tries > VOL_MAX_CHECKOUT_RETRIES) {
2764         Log("VAttachVolume: retried too many times trying to checkout "
2765             "vol %lu part %s; giving up\n", afs_printable_uint32_lu(volid),
2766             VPartitionPath(partp));
2767         *ec = VNOVOL;
2768         goto done;
2769     }
2770
2771     if (VReadVolumeDiskHeader(volid, partp, NULL)) {
2772         /* short-circuit the 'volume does not exist' case */
2773         *ec = VNOVOL;
2774         goto done;
2775     }
2776
2777 #ifdef FSSYNC_BUILD_CLIENT
2778     checkout = !done_checkout;
2779     done_checkout = 1;
2780     if (!peek && checkout && VMustCheckoutVolume(mode)) {
2781         SYNC_response res;
2782         memset(&res, 0, sizeof(res));
2783
2784         if (FSYNC_VolOp(volid, VPartitionPath(partp), FSYNC_VOL_NEEDVOLUME, mode, &res)
2785             != SYNC_OK) {
2786
2787             if (res.hdr.reason == FSYNC_SALVAGE) {
2788                 Log("VAttachVolume: file server says volume %lu is salvaging\n",
2789                      afs_printable_uint32_lu(volid));
2790                 *ec = VSALVAGING;
2791             } else {
2792                 Log("VAttachVolume: attach of volume %lu apparently denied by file server\n",
2793                      afs_printable_uint32_lu(volid));
2794                 *ec = VNOVOL;   /* XXXX */
2795             }
2796             goto done;
2797         }
2798         *acheckedOut = 1;
2799     }
2800 #endif
2801
2802 #ifdef AFS_DEMAND_ATTACH_FS
2803     if (use_locktype < 0) {
2804         /* don't know whether vol is RO or RW; assume it's RO and we can retry
2805          * if it turns out to be RW */
2806         locktype = VVolLockType(mode, 0);
2807
2808     } else {
2809         /* a previous try says we should use use_locktype to lock the volume,
2810          * so use that */
2811         locktype = use_locktype;
2812     }
2813
2814     if (!peek && locktype) {
2815         code = VLockVolumeNB(vp, locktype);
2816         if (code) {
2817             if (code == EBUSY) {
2818                 Log("VAttachVolume: another program has vol %lu locked\n",
2819                     afs_printable_uint32_lu(volid));
2820             } else {
2821                 Log("VAttachVolume: error %d trying to lock vol %lu\n",
2822                     code, afs_printable_uint32_lu(volid));
2823             }
2824
2825             *ec = VNOVOL;
2826             goto done;
2827         }
2828     }
2829 #endif /* AFS_DEMAND_ATTACH_FS */
2830
2831     code = VReadVolumeDiskHeader(volid, partp, &diskHeader);
2832     if (code) {
2833         if (code == EIO) {
2834             *ec = VSALVAGE;
2835         } else {
2836             *ec = VNOVOL;
2837         }
2838         goto done;
2839     }
2840
2841     DiskToVolumeHeader(&header, &diskHeader);
2842
2843     IH_INIT(vp->vnodeIndex[vLarge].handle, partp->device, header.parent,
2844             header.largeVnodeIndex);
2845     IH_INIT(vp->vnodeIndex[vSmall].handle, partp->device, header.parent,
2846             header.smallVnodeIndex);
2847     IH_INIT(vp->diskDataHandle, partp->device, header.parent,
2848             header.volumeInfo);
2849     IH_INIT(vp->linkHandle, partp->device, header.parent, header.linkTable);
2850
2851     if (first_try) {
2852         /* only need to do this once */
2853         VOL_LOCK;
2854         GetVolumeHeader(vp);
2855         VOL_UNLOCK;
2856     }
2857
2858 #if defined(AFS_DEMAND_ATTACH_FS) && defined(FSSYNC_BUILD_CLIENT)
2859     /* demand attach changes the V_PEEK mechanism
2860      *
2861      * we can now suck the current disk data structure over
2862      * the fssync interface without going to disk
2863      *
2864      * (technically, we don't need to restrict this feature
2865      *  to demand attach fileservers.  However, I'm trying
2866      *  to limit the number of common code changes)
2867      */
2868     if (VCanUseFSSYNC() && (mode == V_PEEK || peek)) {
2869         SYNC_response res;
2870         res.payload.len = sizeof(VolumeDiskData);
2871         res.payload.buf = &vp->header->diskstuff;
2872
2873         if (FSYNC_VolOp(vp->hashid,
2874                         partp->name,
2875                         FSYNC_VOL_QUERY_HDR,
2876                         FSYNC_WHATEVER,
2877                         &res) == SYNC_OK) {
2878             goto disk_header_loaded;
2879         }
2880     }
2881 #endif /* AFS_DEMAND_ATTACH_FS && FSSYNC_BUILD_CLIENT */
2882     (void)ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
2883                      sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
2884
2885 #ifdef AFS_DEMAND_ATTACH_FS
2886     /* update stats */
2887     VOL_LOCK;
2888     IncUInt64(&VStats.hdr_loads);
2889     IncUInt64(&vp->stats.hdr_loads);
2890     VOL_UNLOCK;
2891 #endif /* AFS_DEMAND_ATTACH_FS */
2892
2893     if (*ec) {
2894         Log("VAttachVolume: Error reading diskDataHandle header for vol %lu; "
2895             "error=%u\n", afs_printable_uint32_lu(volid), *ec);
2896         goto done;
2897     }
2898
2899 #ifdef AFS_DEMAND_ATTACH_FS
2900 # ifdef FSSYNC_BUILD_CLIENT
2901  disk_header_loaded:
2902 # endif /* FSSYNC_BUILD_CLIENT */
2903
2904     /* if the lock type we actually used to lock the volume is different than
2905      * the lock type we should have used, retry with the lock type we should
2906      * use */
2907     use_locktype = VVolLockType(mode, VolumeWriteable(vp));
2908     if (locktype != use_locktype) {
2909         retry = 1;
2910         lock_tries++;
2911     }
2912 #endif /* AFS_DEMAND_ATTACH_FS */
2913
2914     *ec = 0;
2915
2916  done:
2917 #if defined(AFS_DEMAND_ATTACH_FS) && defined(FSSYNC_BUILD_CLIENT)
2918     if (!peek && *ec == 0 && retry == 0 && VMustCheckoutVolume(mode)) {
2919
2920         code = FSYNC_VerifyCheckout(volid, VPartitionPath(partp), FSYNC_VOL_NEEDVOLUME, mode);
2921
2922         if (code == SYNC_DENIED) {
2923             /* must retry checkout; fileserver no longer thinks we have
2924              * the volume */
2925             retry = 1;
2926             checkout_tries++;
2927             done_checkout = 0;
2928
2929         } else if (code != SYNC_OK) {
2930             *ec = VNOVOL;
2931         }
2932     }
2933 #endif /* AFS_DEMAND_ATTACH_FS && FSSYNC_BUILD_CLIENT */
2934
2935     if (*ec || retry) {
2936         /* either we are going to be called again for a second pass, or we
2937          * encountered an error; clean up in either case */
2938
2939 #ifdef AFS_DEMAND_ATTACH_FS
2940         if ((V_attachFlags(vp) & VOL_LOCKED)) {
2941             VUnlockVolume(vp);
2942         }
2943 #endif /* AFS_DEMAND_ATTACH_FS */
2944         if (vp->linkHandle) {
2945             IH_RELEASE(vp->vnodeIndex[vLarge].handle);
2946             IH_RELEASE(vp->vnodeIndex[vSmall].handle);
2947             IH_RELEASE(vp->diskDataHandle);
2948             IH_RELEASE(vp->linkHandle);
2949         }
2950     }
2951
2952     if (*ec) {
2953         return;
2954     }
2955     if (retry) {
2956         first_try = 0;
2957         goto retry;
2958     }
2959 }
2960
2961 #ifdef AFS_DEMAND_ATTACH_FS
2962 static void
2963 attach_check_vop(Error *ec, VolumeId volid, struct DiskPartition64 *partp,
2964                  Volume *vp, int *acheckedOut)
2965 {
2966     *ec = 0;
2967
2968     if (vp->pending_vol_op) {
2969
2970         VOL_LOCK;
2971
2972         if (vp->pending_vol_op->vol_op_state == FSSYNC_VolOpRunningUnknown) {
2973             int code;
2974             code = VVolOpLeaveOnlineNoHeader_r(vp, vp->pending_vol_op);
2975             if (code == 1) {
2976                 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOnline;
2977             } else if (code == 0) {
2978                 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline;
2979
2980             } else {
2981                 /* we need the vol header to determine if the volume can be
2982                  * left online for the vop, so... get the header */
2983
2984                 VOL_UNLOCK;
2985
2986                 /* attach header with peek=1 to avoid checking out the volume
2987                  * or locking it; we just want the header info, we're not
2988                  * messing with the volume itself at all */
2989                 attach_volume_header(ec, vp, partp, V_PEEK, 1, acheckedOut);
2990                 if (*ec) {
2991                     return;
2992                 }
2993
2994                 VOL_LOCK;
2995
2996                 if (VVolOpLeaveOnline_r(vp, vp->pending_vol_op)) {
2997                     vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOnline;
2998                 } else {
2999                     vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline;
3000                 }
3001
3002                 /* make sure we grab a new vol header and re-open stuff on
3003                  * actual attachment; we can't keep the data we grabbed, since
3004                  * it was not done under a lock and thus not safe */
3005                 FreeVolumeHeader(vp);
3006                 VReleaseVolumeHandles_r(vp);
3007             }
3008         }
3009         /* see if the pending volume op requires exclusive access */
3010         switch (vp->pending_vol_op->vol_op_state) {
3011         case FSSYNC_VolOpPending:
3012             /* this should never happen */
3013             osi_Assert(vp->pending_vol_op->vol_op_state != FSSYNC_VolOpPending);
3014             break;
3015
3016         case FSSYNC_VolOpRunningUnknown:
3017             /* this should never happen; we resolved 'unknown' above */
3018             osi_Assert(vp->pending_vol_op->vol_op_state != FSSYNC_VolOpRunningUnknown);
3019             break;
3020
3021         case FSSYNC_VolOpRunningOffline:
3022             /* mark the volume down */
3023             *ec = VOFFLINE;
3024             VChangeState_r(vp, VOL_STATE_UNATTACHED);
3025
3026             /* do not set V_offlineMessage here; we don't have ownership of
3027              * the volume (and probably do not have the header loaded), so we
3028              * can't alter the disk header */
3029
3030             /* check to see if we should set the specialStatus flag */
3031             if (VVolOpSetVBusy_r(vp, vp->pending_vol_op)) {
3032                 /* don't overwrite specialStatus if it was already set to
3033                  * something else (e.g. VMOVED) */
3034                 if (!vp->specialStatus) {
3035                     vp->specialStatus = VBUSY;
3036                 }
3037             }
3038             break;
3039
3040         default:
3041             break;
3042         }
3043
3044         VOL_UNLOCK;
3045     }
3046 }
3047 #endif /* AFS_DEMAND_ATTACH_FS */
3048
3049 /**
3050  * volume attachment helper function.
3051  *
3052  * @param[out] ec      error code
3053  * @param[in] volumeId volume ID of the attaching volume
3054  * @param[in] path     full path to the volume header .vol file
3055  * @param[in] partp    disk partition object for the attaching partition
3056  * @param[in] vp       volume object; vp->hashid, vp->device, vp->partition,
3057  *                     vp->vnode_list, vp->rx_call_list, and V_attachCV (for
3058  *                     DAFS) should already be initialized
3059  * @param[in] isbusy   1 if vp->specialStatus should be set to VBUSY; that is,
3060  *                     if there is a volume operation running for this volume
3061  *                     that should set the volume to VBUSY during its run. 0
3062  *                     otherwise. (see VVolOpSetVBusy_r)
3063  * @param[in] mode     attachment mode such as V_VOLUPD, V_DUMP, etc (see
3064  *                     volume.h)
3065  * @param[out] acheckedOut   If we successfully checked-out the volume from
3066  *                           the fileserver (if we needed to), this is set
3067  *                           to 1, otherwise it is 0.
3068  *
3069  * @return pointer to the semi-attached volume pointer
3070  *  @retval NULL an error occurred (check value of *ec)
3071  *  @retval vp volume successfully attaching
3072  *
3073  * @pre no locks held
3074  *
3075  * @post VOL_LOCK held
3076  */
3077 static Volume *
3078 attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
3079         Volume * vp, int isbusy, int mode, int *acheckedOut)
3080 {
3081     /* have we read in the header successfully? */
3082     int read_header = 0;
3083
3084 #ifdef AFS_DEMAND_ATTACH_FS
3085     /* should we FreeVolume(vp) instead of VCheckFree(vp) in the error
3086      * cleanup? */
3087     int forcefree = 0;
3088
3089     /* in the case of an error, to what state should the volume be
3090      * transitioned? */
3091     VolState error_state = VOL_STATE_ERROR;
3092 #endif /* AFS_DEMAND_ATTACH_FS */
3093
3094     *ec = 0;
3095
3096     vp->vnodeIndex[vLarge].handle = NULL;
3097     vp->vnodeIndex[vSmall].handle = NULL;
3098     vp->diskDataHandle = NULL;
3099     vp->linkHandle = NULL;
3100
3101     *acheckedOut = 0;
3102
3103 #ifdef AFS_DEMAND_ATTACH_FS
3104     attach_check_vop(ec, volumeId, partp, vp, acheckedOut);
3105     if (!*ec) {
3106         attach_volume_header(ec, vp, partp, mode, 0, acheckedOut);
3107     }
3108 #else
3109     attach_volume_header(ec, vp, partp, mode, 0, acheckedOut);
3110 #endif /* !AFS_DEMAND_ATTACH_FS */
3111
3112     if (*ec == VNOVOL) {
3113         /* if the volume doesn't exist, skip straight to 'error' so we don't
3114          * request a salvage */
3115         goto unlocked_error;
3116     }
3117
3118     if (!*ec) {
3119         read_header = 1;
3120
3121         /* ensure that we don't override specialStatus if it was set to
3122          * something else (e.g. VMOVED) */
3123         if (isbusy && !vp->specialStatus) {
3124             vp->specialStatus = VBUSY;
3125         }
3126         vp->shuttingDown = 0;
3127         vp->goingOffline = 0;
3128         vp->nUsers = 1;
3129 #ifdef AFS_DEMAND_ATTACH_FS
3130         vp->stats.last_attach = FT_ApproxTime();
3131         vp->stats.attaches++;
3132 #endif
3133
3134         VOL_LOCK;
3135         IncUInt64(&VStats.attaches);
3136         vp->cacheCheck = ++VolumeCacheCheck;
3137         /* just in case this ever rolls over */
3138         if (!vp->cacheCheck)
3139             vp->cacheCheck = ++VolumeCacheCheck;
3140         VOL_UNLOCK;
3141
3142 #ifdef AFS_DEMAND_ATTACH_FS
3143         V_attachFlags(vp) |= VOL_HDR_LOADED;
3144         vp->stats.last_hdr_load = vp->stats.last_attach;
3145 #endif /* AFS_DEMAND_ATTACH_FS */
3146     }
3147
3148     if (!*ec) {
3149         struct IndexFileHeader iHead;
3150
3151 #if OPENAFS_VOL_STATS
3152         /*
3153          * We just read in the diskstuff part of the header.  If the detailed
3154          * volume stats area has not yet been initialized, we should bzero the
3155          * area and mark it as initialized.
3156          */
3157         if (!(V_stat_initialized(vp))) {
3158             memset((V_stat_area(vp)), 0, VOL_STATS_BYTES);
3159             V_stat_initialized(vp) = 1;
3160         }
3161 #endif /* OPENAFS_VOL_STATS */
3162
3163         (void)ReadHeader(ec, vp->vnodeIndex[vSmall].handle,
3164                          (char *)&iHead, sizeof(iHead),
3165                          SMALLINDEXMAGIC, SMALLINDEXVERSION);
3166
3167         if (*ec) {
3168             Log("VAttachVolume: Error reading smallVnode vol header %s; error=%u\n", path, *ec);
3169         }
3170     }
3171
3172     if (!*ec) {
3173         struct IndexFileHeader iHead;
3174
3175         (void)ReadHeader(ec, vp->vnodeIndex[vLarge].handle,
3176                          (char *)&iHead, sizeof(iHead),
3177                          LARGEINDEXMAGIC, LARGEINDEXVERSION);
3178
3179         if (*ec) {
3180             Log("VAttachVolume: Error reading largeVnode vol header %s; error=%u\n", path, *ec);
3181         }
3182     }
3183
3184 #ifdef AFS_NAMEI_ENV
3185     if (!*ec) {
3186         struct versionStamp stamp;
3187
3188         (void)ReadHeader(ec, V_linkHandle(vp), (char *)&stamp,
3189                          sizeof(stamp), LINKTABLEMAGIC, LINKTABLEVERSION);
3190
3191         if (*ec) {
3192             Log("VAttachVolume: Error reading namei vol header %s; error=%u\n", path, *ec);
3193         }
3194     }
3195 #endif /* AFS_NAMEI_ENV */
3196
3197 #if defined(AFS_DEMAND_ATTACH_FS)
3198     if (*ec && ((*ec != VOFFLINE) || (V_attachState(vp) != VOL_STATE_UNATTACHED))) {
3199         VOL_LOCK;
3200         if (!VCanScheduleSalvage()) {
3201             Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
3202         }
3203         VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
3204                                                   VOL_SALVAGE_NO_OFFLINE);
3205         vp->nUsers = 0;
3206
3207         goto locked_error;
3208     } else if (*ec) {
3209         /* volume operation in progress */
3210         goto unlocked_error;
3211     }
3212 #else /* AFS_DEMAND_ATTACH_FS */
3213     if (*ec) {
3214         Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
3215         goto unlocked_error;
3216     }
3217 #endif /* AFS_DEMAND_ATTACH_FS */
3218
3219     if (V_needsSalvaged(vp)) {
3220         if (vp->specialStatus)
3221             vp->specialStatus = 0;
3222         VOL_LOCK;
3223 #if defined(AFS_DEMAND_ATTACH_FS)
3224         if (!VCanScheduleSalvage()) {
3225             Log("VAttachVolume: volume salvage flag is ON for %s; volume needs salvage\n", path);
3226         }
3227         VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER |
3228                                                    VOL_SALVAGE_NO_OFFLINE);
3229         vp->nUsers = 0;
3230
3231 #else /* AFS_DEMAND_ATTACH_FS */
3232         *ec = VSALVAGE;
3233 #endif /* AFS_DEMAND_ATTACH_FS */
3234
3235         goto locked_error;
3236     }
3237
3238     VOL_LOCK;
3239     vp->nextVnodeUnique = V_uniquifier(vp);
3240
3241     if (VShouldCheckInUse(mode) && V_inUse(vp) && VolumeWriteable(vp)) {
3242         if (!V_needsSalvaged(vp)) {
3243             V_needsSalvaged(vp) = 1;
3244             VUpdateVolume_r(ec, vp, 0);
3245         }
3246 #if defined(AFS_DEMAND_ATTACH_FS)
3247         if (!VCanScheduleSalvage()) {
3248             Log("VAttachVolume: volume %s needs to be salvaged; not attached.\n", path);
3249         }
3250         VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER |
3251                                                    VOL_SALVAGE_NO_OFFLINE);
3252         vp->nUsers = 0;
3253
3254 #else /* AFS_DEMAND_ATTACH_FS */
3255         Log("VAttachVolume: volume %s needs to be salvaged; not attached.\n", path);
3256         *ec = VSALVAGE;
3257 #endif /* AFS_DEMAND_ATTACH_FS */
3258
3259         goto locked_error;
3260     }
3261
3262     if (programType == fileServer && V_destroyMe(vp) == DESTROY_ME) {
3263         /* Only check destroyMe if we are the fileserver, since the
3264          * volserver et al sometimes need to work with volumes with
3265          * destroyMe set. Examples are 'temporary' volumes the
3266          * volserver creates, and when we create a volume (destroyMe
3267          * is set on creation; sometimes a separate volserver
3268          * transaction is created to clear destroyMe).
3269          */
3270
3271 #if defined(AFS_DEMAND_ATTACH_FS)
3272         /* schedule a salvage so the volume goes away on disk */
3273         VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
3274                                                   VOL_SALVAGE_NO_OFFLINE);
3275         VChangeState_r(vp, VOL_STATE_ERROR);
3276         vp->nUsers = 0;
3277         forcefree = 1;
3278 #endif /* AFS_DEMAND_ATTACH_FS */
3279         Log("VAttachVolume: volume %s is junk; it should be destroyed at next salvage\n", path);
3280         *ec = VNOVOL;
3281         goto locked_error;
3282     }
3283
3284     vp->vnodeIndex[vSmall].bitmap = vp->vnodeIndex[vLarge].bitmap = NULL;
3285 #ifndef BITMAP_LATER
3286     if (programType == fileServer && VolumeWriteable(vp)) {
3287         int i;
3288         for (i = 0; i < nVNODECLASSES; i++) {
3289             VGetBitmap_r(ec, vp, i);
3290             if (*ec) {
3291 #ifdef AFS_DEMAND_ATTACH_FS
3292                 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
3293                                                           VOL_SALVAGE_NO_OFFLINE);
3294                 vp->nUsers = 0;
3295 #endif /* AFS_DEMAND_ATTACH_FS */
3296                 Log("VAttachVolume: error getting bitmap for volume (%s)\n",
3297                     path);
3298                 goto locked_error;
3299             }
3300         }
3301     }
3302 #endif /* BITMAP_LATER */
3303
3304     if (VInit >= 2 && V_needsCallback(vp)) {
3305         if (V_BreakVolumeCallbacks) {
3306             Log("VAttachVolume: Volume %lu was changed externally; breaking callbacks\n",
3307                 afs_printable_uint32_lu(V_id(vp)));
3308             V_needsCallback(vp) = 0;
3309             VOL_UNLOCK;
3310             (*V_BreakVolumeCallbacks) (V_id(vp));
3311             VOL_LOCK;
3312
3313             VUpdateVolume_r(ec, vp, 0);
3314         }
3315 #ifdef FSSYNC_BUILD_CLIENT
3316         else if (VCanUseFSSYNC()) {
3317             afs_int32 fsync_code;
3318
3319             V_needsCallback(vp) = 0;
3320             VOL_UNLOCK;
3321             fsync_code = FSYNC_VolOp(V_id(vp), NULL, FSYNC_VOL_BREAKCBKS, FSYNC_WHATEVER, NULL);
3322             VOL_LOCK;
3323
3324             if (fsync_code) {
3325                 V_needsCallback(vp) = 1;
3326                 Log("Error trying to tell the fileserver to break callbacks for "
3327                     "changed volume %lu; error code %ld\n",
3328                     afs_printable_uint32_lu(V_id(vp)),
3329                     afs_printable_int32_ld(fsync_code));
3330             } else {
3331                 VUpdateVolume_r(ec, vp, 0);
3332             }
3333         }
3334 #endif /* FSSYNC_BUILD_CLIENT */
3335
3336         if (*ec) {
3337             Log("VAttachVolume: error %d clearing needsCallback on volume "
3338                 "%lu; needs salvage\n", (int)*ec,
3339                 afs_printable_uint32_lu(V_id(vp)));
3340 #ifdef AFS_DEMAND_ATTACH_FS
3341             VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
3342                                                       VOL_SALVAGE_NO_OFFLINE);
3343             vp->nUsers = 0;
3344 #else /* !AFS_DEMAND_ATTACH_FS */
3345             *ec = VSALVAGE;
3346 #endif /* !AFS_DEMAND_ATTACh_FS */
3347             goto locked_error;
3348         }
3349     }
3350
3351     if (programType == fileServer) {
3352         if (vp->specialStatus)
3353             vp->specialStatus = 0;
3354         if (V_blessed(vp) && V_inService(vp) && !V_needsSalvaged(vp)) {
3355             V_inUse(vp) = fileServer;
3356             V_offlineMessage(vp)[0] = '\0';
3357         }
3358         if (!V_inUse(vp)) {
3359             *ec = VNOVOL;
3360 #ifdef AFS_DEMAND_ATTACH_FS
3361             /* Put the vol into PREATTACHED state, so if someone tries to
3362              * access it again, we try to attach, see that we're not blessed,
3363              * and give a VNOVOL error again. Putting it into UNATTACHED state
3364              * would result in a VOFFLINE error instead. */
3365             error_state = VOL_STATE_PREATTACHED;
3366 #endif /* AFS_DEMAND_ATTACH_FS */
3367
3368             /* mimic e.g. GetVolume errors */
3369             if (!V_blessed(vp)) {
3370                 Log("Volume %lu offline: not blessed\n", afs_printable_uint32_lu(V_id(vp)));
3371                 FreeVolumeHeader(vp);
3372             } else if (!V_inService(vp)) {
3373                 Log("Volume %lu offline: not in service\n", afs_printable_uint32_lu(V_id(vp)));
3374                 FreeVolumeHeader(vp);
3375             } else {
3376                 Log("Volume %lu offline: needs salvage\n", afs_printable_uint32_lu(V_id(vp)));
3377                 *ec = VSALVAGE;
3378 #ifdef AFS_DEMAND_ATTACH_FS
3379                 error_state = VOL_STATE_ERROR;
3380                 /* see if we can recover */
3381                 VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER);
3382 #endif
3383             }
3384 #ifdef AFS_DEMAND_ATTACH_FS
3385             vp->nUsers = 0;
3386 #endif
3387             goto locked_error;
3388         }
3389     } else {
3390 #ifdef AFS_DEMAND_ATTACH_FS
3391         if ((mode != V_PEEK) && (mode != V_SECRETLY))
3392             V_inUse(vp) = programType;
3393 #endif /* AFS_DEMAND_ATTACH_FS */
3394         V_checkoutMode(vp) = mode;
3395     }
3396
3397     AddVolumeToHashTable(vp, V_id(vp));
3398 #ifdef AFS_DEMAND_ATTACH_FS
3399     if (VCanUnlockAttached() && (V_attachFlags(vp) & VOL_LOCKED)) {
3400         VUnlockVolume(vp);
3401     }
3402     if ((programType != fileServer) ||
3403         (V_inUse(vp) == fileServer)) {
3404         AddVolumeToVByPList_r(vp);
3405         VLRU_Add_r(vp);
3406         VChangeState_r(vp, VOL_STATE_ATTACHED);
3407     } else {
3408         VChangeState_r(vp, VOL_STATE_UNATTACHED);
3409     }
3410 #endif
3411
3412     return vp;
3413
3414 unlocked_error:
3415     VOL_LOCK;
3416 locked_error:
3417 #ifdef AFS_DEMAND_ATTACH_FS
3418     if (!VIsErrorState(V_attachState(vp))) {
3419         VChangeState_r(vp, error_state);
3420     }
3421 #endif /* AFS_DEMAND_ATTACH_FS */
3422
3423     if (read_header) {
3424         VReleaseVolumeHandles_r(vp);
3425     }
3426
3427 #ifdef AFS_DEMAND_ATTACH_FS
3428     VCheckSalvage(vp);
3429     if (forcefree) {
3430         FreeVolume(vp);
3431     } else {
3432         VCheckFree(vp);
3433     }
3434 #else /* !AFS_DEMAND_ATTACH_FS */
3435     FreeVolume(vp);
3436 #endif /* !AFS_DEMAND_ATTACH_FS */
3437     return NULL;
3438 }
3439
3440 /* Attach an existing volume.
3441    The volume also normally goes online at this time.
3442    An offline volume must be reattached to make it go online.
3443  */
3444
3445 Volume *
3446 VAttachVolume(Error * ec, VolumeId volumeId, int mode)
3447 {
3448     Volume *retVal;
3449     VOL_LOCK;
3450     retVal = VAttachVolume_r(ec, volumeId, mode);
3451     VOL_UNLOCK;
3452     return retVal;
3453 }
3454
3455 Volume *
3456 VAttachVolume_r(Error * ec, VolumeId volumeId, int mode)
3457 {
3458     char *part, *name;
3459     VGetVolumePath(ec, volumeId, &part, &name);
3460     if (*ec) {
3461         Volume *vp;
3462         Error error;
3463         vp = VGetVolume_r(&error, volumeId);
3464         if (vp) {
3465             osi_Assert(V_inUse(vp) == 0);
3466             VDetachVolume_r(ec, vp);
3467         }
3468         return NULL;
3469     }
3470     return VAttachVolumeByName_r(ec, part, name, mode);
3471 }
3472
3473 /* Increment a reference count to a volume, sans context swaps.  Requires
3474  * possibly reading the volume header in from the disk, since there's
3475  * an invariant in the volume package that nUsers>0 ==> vp->header is valid.
3476  *
3477  * N.B. This call can fail if we can't read in the header!!  In this case
3478  * we still guarantee we won't context swap, but the ref count won't be
3479  * incremented (otherwise we'd violate the invariant).
3480  */
3481 /* NOTE: with the demand attach fileserver extensions, the global lock
3482  * is dropped within VHold */
3483 #ifdef AFS_DEMAND_ATTACH_FS
3484 static int
3485 VHold_r(Volume * vp)
3486 {
3487     Error error;
3488
3489     VCreateReservation_r(vp);
3490     VWaitExclusiveState_r(vp);
3491
3492     LoadVolumeHeader(&error, vp);
3493     if (error) {
3494         VCancelReservation_r(vp);
3495         return error;
3496     }
3497     vp->nUsers++;
3498     VCancelReservation_r(vp);
3499     return 0;
3500 }
3501 #else /* AFS_DEMAND_ATTACH_FS */
3502 static int
3503 VHold_r(Volume * vp)
3504 {
3505     Error error;
3506
3507     LoadVolumeHeader(&error, vp);
3508     if (error)
3509         return error;
3510     vp->nUsers++;
3511     return 0;
3512 }
3513 #endif /* AFS_DEMAND_ATTACH_FS */
3514
3515 /**** volume timeout-related stuff ****/
3516
3517 #ifdef AFS_PTHREAD_ENV
3518
3519 static struct timespec *shutdown_timeout;
3520 static pthread_once_t shutdown_timeout_once = PTHREAD_ONCE_INIT;
3521
3522 static_inline int
3523 VTimedOut(const struct timespec *ts)
3524 {
3525     struct timeval tv;
3526     int code;
3527
3528     if (ts->tv_sec == 0) {
3529         /* short-circuit; this will have always timed out */
3530         return 1;
3531     }
3532
3533     code = gettimeofday(&tv, NULL);
3534     if (code) {
3535         Log("Error %d from gettimeofday, assuming we have not timed out\n", errno);
3536         /* assume no timeout; failure mode is we just wait longer than normal
3537          * instead of returning errors when we shouldn't */
3538         return 0;
3539     }
3540
3541     if (tv.tv_sec < ts->tv_sec ||
3542         (tv.tv_sec == ts->tv_sec && tv.tv_usec*1000 < ts->tv_nsec)) {
3543
3544         return 0;
3545     }
3546
3547     return 1;
3548 }
3549
3550 /**
3551  * Calculate an absolute timeout.
3552  *
3553  * @param[out] ts  A timeout that is "timeout" seconds from now, if we return
3554  *                 NULL, the memory is not touched
3555  * @param[in]  timeout  How long the timeout should be from now
3556  *
3557  * @return timeout to use
3558  *  @retval NULL      no timeout; wait forever
3559  *  @retval non-NULL  the given value for "ts"
3560  *
3561  * @internal
3562  */
3563 static struct timespec *
3564 VCalcTimeout(struct timespec *ts, afs_int32 timeout)
3565 {
3566     struct timeval now;
3567     int code;
3568
3569     if (timeout < 0) {
3570         return NULL;
3571     }
3572
3573     if (timeout == 0) {
3574         ts->tv_sec = ts->tv_nsec = 0;
3575         return ts;
3576     }
3577
3578     code = gettimeofday(&now, NULL);
3579     if (code) {
3580         Log("Error %d from gettimeofday, falling back to 'forever' timeout\n", errno);
3581         return NULL;
3582     }
3583
3584     ts->tv_sec = now.tv_sec + timeout;
3585     ts->tv_nsec = now.tv_usec * 1000;
3586
3587     return ts;
3588 }
3589
3590 /**
3591  * Initialize the shutdown_timeout global.
3592  */
3593 static void
3594 VShutdownTimeoutInit(void)
3595 {
3596     struct timespec *ts;
3597
3598     ts = malloc(sizeof(*ts));
3599
3600     shutdown_timeout = VCalcTimeout(ts, vol_opts.offline_shutdown_timeout);
3601
3602     if (!shutdown_timeout) {
3603         free(ts);
3604     }
3605 }
3606
3607 /**
3608  * Figure out the timeout that should be used for waiting for offline volumes.
3609  *
3610  * @param[out] ats  Storage space for a local timeout value if needed
3611  *
3612  * @return The timeout value that should be used
3613  *   @retval NULL      No timeout; wait forever for offlining volumes
3614  *   @retval non-NULL  A pointer to the absolute time that should be used as
3615  *                     the deadline for waiting for offlining volumes.
3616  *
3617  * @note If we return non-NULL, the pointer we return may or may not be the
3618  *       same as "ats"
3619  */
3620 static const struct timespec *
3621 VOfflineTimeout(struct timespec *ats)
3622 {
3623     if (vol_shutting_down) {
3624         osi_Assert(pthread_once(&shutdown_timeout_once, VShutdownTimeoutInit) == 0);
3625         return shutdown_timeout;
3626     } else {
3627         return VCalcTimeout(ats, vol_opts.offline_timeout);
3628     }
3629 }
3630
3631 #else /* AFS_PTHREAD_ENV */
3632
3633 /* Waiting a certain amount of time for offlining volumes is not supported
3634  * for LWP due to a lack of primitives. So, we never time out */
3635 # define VTimedOut(x) (0)
3636 # define VOfflineTimeout(x) (NULL)
3637
3638 #endif /* !AFS_PTHREAD_ENV */
3639
3640 #if 0
3641 static int
3642 VHold(Volume * vp)
3643 {
3644     int retVal;
3645     VOL_LOCK;
3646     retVal = VHold_r(vp);
3647     VOL_UNLOCK;
3648     return retVal;
3649 }
3650 #endif
3651
3652 static afs_int32
3653 VIsGoingOffline_r(struct Volume *vp)
3654 {
3655     afs_int32 code = 0;
3656
3657     if (vp->goingOffline) {
3658         if (vp->specialStatus) {
3659             code = vp->specialStatus;
3660         } else if (V_inService(vp) == 0 || V_blessed(vp) == 0) {
3661             code = VNOVOL;
3662         } else {
3663             code = VOFFLINE;
3664         }
3665     }
3666
3667     return code;
3668 }
3669
3670 /**
3671  * Tell the caller if a volume is waiting to go offline.
3672  *
3673  * @param[in] vp  The volume we want to know about
3674  *
3675  * @return volume status
3676  *   @retval 0 volume is not waiting to go offline, go ahead and use it
3677  *   @retval nonzero volume is waiting to offline, and give the returned code
3678  *           as an error to anyone accessing the volume
3679  *
3680  * @pre VOL_LOCK is NOT held
3681  * @pre caller holds a heavyweight reference on vp
3682  */
3683 afs_int32
3684 VIsGoingOffline(struct Volume *vp)
3685 {
3686     afs_int32 code;
3687
3688     VOL_LOCK;
3689     code = VIsGoingOffline_r(vp);
3690     VOL_UNLOCK;
3691
3692     return code;
3693 }
3694
3695 /**
3696  * Register an RX call with a volume.
3697  *
3698  * @param[inout] ec        Error code; if unset when passed in, may be set if
3699  *                         the volume starts going offline
3700  * @param[out]   client_ec @see GetVolume
3701  * @param[in] vp   Volume struct
3702  * @param[in] cbv  VCallByVol struct containing the RX call to register
3703  *
3704  * @pre VOL_LOCK held
3705  * @pre caller holds heavy ref on vp
3706  *
3707  * @internal
3708  */
3709 static void
3710 VRegisterCall_r(Error *ec, Error *client_ec, Volume *vp, struct VCallByVol *cbv)
3711 {
3712     if (vp && cbv) {
3713 #ifdef AFS_DEMAND_ATTACH_FS
3714         if (!*ec) {
3715             /* just in case the volume started going offline after we got the
3716              * reference to it... otherwise, if the volume started going
3717              * offline right at the end of GetVolume(), we might race with the
3718              * RX call scanner, and return success and add our cbv to the
3719              * rx_call_list _after_ the scanner has scanned the list. */
3720             *ec = VIsGoingOffline_r(vp);
3721             if (client_ec) {
3722                 *client_ec = *ec;
3723             }
3724         }
3725
3726         while (V_attachState(vp) == VOL_STATE_SCANNING_RXCALLS) {
3727             VWaitStateChange_r(vp);
3728         }
3729 #endif /* AFS_DEMAND_ATTACH_FS */
3730
3731         queue_Prepend(&vp->rx_call_list, cbv);
3732     }
3733 }
3734
3735 /**
3736  * Deregister an RX call with a volume.
3737  *
3738  * @param[in] vp   Volume struct
3739  * @param[in] cbv  VCallByVol struct containing the RX call to deregister
3740  *
3741  * @pre VOL_LOCK held
3742  * @pre caller holds heavy ref on vp
3743  *
3744  * @internal
3745  */
3746 static void
3747 VDeregisterCall_r(Volume *vp, struct VCallByVol *cbv)
3748 {
3749     if (cbv && queue_IsOnQueue(cbv)) {
3750 #ifdef AFS_DEMAND_ATTACH_FS
3751         while (V_attachState(vp) == VOL_STATE_SCANNING_RXCALLS) {
3752             VWaitStateChange_r(vp);
3753         }
3754 #endif /* AFS_DEMAND_ATTACH_FS */
3755
3756         queue_Remove(cbv);
3757     }
3758 }
3759
3760 /***************************************************/
3761 /* get and put volume routines                     */
3762 /***************************************************/
3763
3764 /**
3765  * put back a heavyweight reference to a volume object.
3766  *
3767  * @param[in] vp  volume object pointer
3768  *
3769  * @pre VOL_LOCK held
3770  *
3771  * @post heavyweight volume reference put back.
3772  *       depending on state, volume may have been taken offline,
3773  *       detached, salvaged, freed, etc.
3774  *
3775  * @internal volume package internal use only
3776  */
3777 void
3778 VPutVolume_r(Volume * vp)
3779 {
3780     osi_Assert(--vp->nUsers >= 0);
3781     if (vp->nUsers == 0) {
3782         VCheckOffline(vp);
3783         ReleaseVolumeHeader(vp->header);
3784 #ifdef AFS_DEMAND_ATTACH_FS
3785         if (!VCheckDetach(vp)) {
3786             VCheckSalvage(vp);
3787             VCheckFree(vp);
3788         }
3789 #else /* AFS_DEMAND_ATTACH_FS */
3790         VCheckDetach(vp);
3791 #endif /* AFS_DEMAND_ATTACH_FS */
3792     }
3793 }
3794
3795 void
3796 VPutVolume(Volume * vp)
3797 {
3798     VOL_LOCK;
3799     VPutVolume_r(vp);
3800     VOL_UNLOCK;
3801 }
3802
3803 /**
3804  * Puts a volume reference obtained with VGetVolumeWithCall.
3805  *
3806  * @param[in] vp  Volume struct
3807  * @param[in] cbv VCallByVol struct given to VGetVolumeWithCall, or NULL if none
3808  *
3809  * @pre VOL_LOCK is NOT held
3810  */
3811 void
3812 VPutVolumeWithCall(Volume *vp, struct VCallByVol *cbv)
3813 {
3814     VOL_LOCK;
3815     VDeregisterCall_r(vp, cbv);
3816     VPutVolume_r(vp);
3817     VOL_UNLOCK;
3818 }
3819
3820 /* Get a pointer to an attached volume.  The pointer is returned regardless
3821    of whether or not the volume is in service or on/off line.  An error
3822    code, however, is returned with an indication of the volume's status */
3823 Volume *
3824 VGetVolume(Error * ec, Error * client_ec, VolId volumeId)
3825 {
3826     Volume *retVal;
3827     VOL_LOCK;
3828     retVal = GetVolume(ec, client_ec, volumeId, NULL, 0);
3829     VOL_UNLOCK;
3830     return retVal;
3831 }
3832
3833 /**
3834  * Get a volume reference associated with an RX call.
3835  *
3836  * @param[out] ec @see GetVolume
3837  * @param[out] client_ec @see GetVolume
3838  * @param[in] volumeId @see GetVolume
3839  * @param[in] ts  How long to wait for going-offline volumes (absolute time).
3840  *                If NULL, wait forever. If ts->tv_sec == 0, return immediately
3841  *                with an error if the volume is going offline.
3842  * @param[in] cbv Contains an RX call to be associated with this volume
3843  *                reference. This call may be interrupted if the volume is
3844  *                requested to go offline while we hold a ref on it. Give NULL
3845  *                to not associate an RX call with this reference.
3846  *
3847  * @return @see GetVolume
3848  *
3849  * @note for LWP builds, ts must be NULL
3850  *
3851  * @note A reference obtained with this function MUST be put back with
3852  *       VPutVolumeWithCall
3853  */
3854 Volume *
3855 VGetVolumeWithCall(Error * ec, Error * client_ec, VolId volumeId,
3856                    const struct timespec *ts, struct VCallByVol *cbv)
3857 {
3858     Volume *retVal;
3859     VOL_LOCK;
3860     retVal = GetVolume(ec, client_ec, volumeId, NULL, ts);
3861     VRegisterCall_r(ec, client_ec, retVal, cbv);
3862     VOL_UNLOCK;
3863     return retVal;
3864 }
3865
3866 Volume *
3867 VGetVolume_r(Error * ec, VolId volumeId)
3868 {
3869     return GetVolume(ec, NULL, volumeId, NULL, NULL);
3870 }
3871
3872 /* try to get a volume we've previously looked up */
3873 /* for demand attach fs, caller MUST NOT hold a ref count on vp */
3874 Volume *
3875 VGetVolumeByVp_r(Error * ec, Volume * vp)
3876 {
3877     return GetVolume(ec, NULL, vp->hashid, vp, NULL);
3878 }
3879
3880 /**
3881  * private interface for getting a volume handle
3882  *
3883  * @param[out] ec         error code (0 if no error)
3884  * @param[out] client_ec  wire error code to be given to clients
3885  * @param[in]  volumeId   ID of the volume we want
3886  * @param[in]  hint       optional hint for hash lookups, or NULL
3887  * @param[in]  timeout    absolute deadline for waiting for the volume to go
3888  *                        offline, if it is going offline. NULL to wait forever.
3889  *
3890  * @return a volume handle for the specified volume
3891  *  @retval NULL an error occurred, or the volume is in such a state that
3892  *               we cannot load a header or return any volume struct
3893  *
3894  * @note for DAFS, caller must NOT hold a ref count on 'hint'
3895  *
3896  * @note 'timeout' is only checked if the volume is actually going offline; so
3897  *       if you pass timeout->tv_sec = 0, this will exhibit typical
3898  *       nonblocking behavior.
3899  *
3900  * @note for LWP builds, 'timeout' must be NULL
3901  */
3902 static Volume *
3903 GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
3904           const struct timespec *timeout)
3905 {
3906     Volume *vp = hint;
3907     /* pull this profiling/debugging code out of regular builds */
3908 #ifdef notdef
3909 #define VGET_CTR_INC(x) x++
3910     unsigned short V0 = 0, V1 = 0, V2 = 0, V3 = 0, V5 = 0, V6 =
3911         0, V7 = 0, V8 = 0, V9 = 0;
3912     unsigned short V10 = 0, V11 = 0, V12 = 0, V13 = 0, V14 = 0, V15 = 0;
3913 #else
3914 #define VGET_CTR_INC(x)
3915 #endif
3916 #ifdef AFS_DEMAND_ATTACH_FS
3917     Volume *avp, * rvp = hint;
3918 #endif
3919
3920     /*
3921      * if VInit is zero, the volume package dynamic
3922      * data structures have not been initialized yet,
3923      * and we must immediately return an error
3924      */
3925     if (VInit == 0) {
3926         vp = NULL;
3927         *ec = VOFFLINE;
3928         if (client_ec) {
3929             *client_ec = VOFFLINE;
3930         }
3931         goto not_inited;
3932     }
3933
3934 #ifdef AFS_DEMAND_ATTACH_FS
3935     if (rvp) {
3936         VCreateReservation_r(rvp);
3937     }
3938 #endif /* AFS_DEMAND_ATTACH_FS */
3939
3940     for (;;) {
3941         *ec = 0;
3942         if (client_ec)
3943             *client_ec = 0;
3944         VGET_CTR_INC(V0);
3945
3946         vp = VLookupVolume_r(ec, volumeId, vp);
3947         if (*ec) {
3948             vp = NULL;
3949             break;
3950         }
3951
3952 #ifdef AFS_DEMAND_ATTACH_FS
3953         if (rvp && (rvp != vp)) {
3954             /* break reservation on old vp */
3955             VCancelReservation_r(rvp);
3956             rvp = NULL;
3957         }
3958 #endif /* AFS_DEMAND_ATTACH_FS */
3959
3960         if (!vp) {
3961             VGET_CTR_INC(V1);
3962             if (VInit < 2) {
3963                 VGET_CTR_INC(V2);
3964                 /* Until we have reached an initialization level of 2
3965                  * we don't know whether this volume exists or not.
3966                  * We can't sleep and retry later because before a volume
3967                  * is attached, the caller tries to get it first.  Just
3968                  * return VOFFLINE and the caller can choose whether to
3969                  * retry the command or not. */
3970                 *ec = VOFFLINE;
3971                 break;
3972             }
3973
3974             *ec = VNOVOL;
3975             break;
3976         }
3977
3978         VGET_CTR_INC(V3);
3979         IncUInt64(&VStats.hdr_gets);
3980
3981 #ifdef AFS_DEMAND_ATTACH_FS
3982         /* block if someone else is performing an exclusive op on this volume */
3983         if (rvp != vp) {
3984             rvp = vp;
3985             VCreateReservation_r(rvp);
3986         }
3987         VWaitExclusiveState_r(vp);
3988
3989         /* short circuit with VNOVOL in the following circumstances:
3990          *
3991          *   - VOL_STATE_ERROR
3992          *   - VOL_STATE_SHUTTING_DOWN
3993          */
3994         if ((V_attachState(vp) == VOL_STATE_ERROR) ||
3995             (V_attachState(vp) == VOL_STATE_SHUTTING_DOWN) ||
3996             (V_attachState(vp) == VOL_STATE_GOING_OFFLINE)) {
3997             *ec = VNOVOL;
3998             vp = NULL;
3999             break;
4000         }
4001
4002         /*
4003          * short circuit with VOFFLINE for VOL_STATE_UNATTACHED and
4004          *                    VNOVOL   for VOL_STATE_DELETED
4005          */
4006        if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
4007            (V_attachState(vp) == VOL_STATE_DELETED)) {
4008            if (vp->specialStatus) {
4009                *ec = vp->specialStatus;
4010            } else if (V_attachState(vp) == VOL_STATE_DELETED) {
4011                *ec = VNOVOL;
4012            } else {
4013                *ec = VOFFLINE;
4014            }
4015            vp = NULL;
4016            break;
4017        }
4018
4019         /* allowable states:
4020          *   - PREATTACHED
4021          *   - ATTACHED
4022          *   - SALVAGING
4023          *   - SALVAGE_REQ
4024          */
4025
4026         if (vp->salvage.requested) {
4027             VUpdateSalvagePriority_r(vp);
4028         }
4029
4030         if (V_attachState(vp) == VOL_STATE_PREATTACHED) {
4031             if (vp->specialStatus) {
4032                 *ec = vp->specialStatus;
4033                 vp = NULL;
4034                 break;
4035             }
4036             avp = VAttachVolumeByVp_r(ec, vp, 0);
4037             if (avp) {
4038                 if (vp != avp) {
4039                     /* VAttachVolumeByVp_r can return a pointer
4040                      * != the vp passed to it under certain
4041                      * conditions; make sure we don't leak
4042                      * reservations if that happens */
4043                     vp = avp;
4044                     VCancelReservation_r(rvp);
4045                     rvp = avp;
4046                     VCreateReservation_r(rvp);
4047                 }
4048                 VPutVolume_r(avp);
4049             }
4050             if (*ec) {
4051                 int endloop = 0;
4052                 switch (*ec) {
4053                 case VSALVAGING:
4054                     break;
4055                 case VOFFLINE:
4056                     if (!vp->pending_vol_op) {
4057                         endloop = 1;
4058                     }
4059                     break;
4060                 default:
4061                     *ec = VNOVOL;
4062                     endloop = 1;
4063                 }
4064                 if (endloop) {
4065                     vp = NULL;
4066                     break;
4067                 }
4068             }
4069         }
4070
4071         if (VIsSalvaging(vp) || (*ec == VSALVAGING)) {
4072             if (client_ec) {
4073                 /* see CheckVnode() in afsfileprocs.c for an explanation
4074                  * of this error code logic */
4075                 afs_uint32 now = FT_ApproxTime();
4076                 if ((vp->stats.last_salvage + (10 * 60)) >= now) {
4077                     *client_ec = VBUSY;
4078                 } else {
4079                     *client_ec = VRESTARTING;
4080                 }
4081             }
4082             *ec = VSALVAGING;
4083             vp = NULL;
4084             break;
4085         }
4086 #endif
4087
4088 #ifdef AFS_DEMAND_ATTACH_FS
4089         /*
4090          * this test MUST happen after VAttachVolymeByVp, so vol_op_state is
4091          * not VolOpRunningUnknown (attach2 would have converted it to Online
4092          * or Offline)
4093          */
4094
4095          /* only valid before/during demand attachment */
4096          osi_Assert(!vp->pending_vol_op || vp->pending_vol_op->vol_op_state != FSSYNC_VolOpRunningUnknown);
4097
4098          /* deny getvolume due to running mutually exclusive vol op */
4099          if (vp->pending_vol_op && vp->pending_vol_op->vol_op_state==FSSYNC_VolOpRunningOffline) {
4100            /*
4101             * volume cannot remain online during this volume operation.
4102             * notify client.
4103             */
4104            if (vp->specialStatus) {
4105                /*
4106                 * special status codes outrank normal VOFFLINE code
4107                 */
4108                *ec = vp->specialStatus;
4109                if (client_ec) {
4110                    *client_ec = vp->specialStatus;
4111                }
4112            } else {
4113                if (client_ec) {
4114                    /* see CheckVnode() in afsfileprocs.c for an explanation
4115                     * of this error code logic */
4116                    afs_uint32 now = FT_ApproxTime();
4117                    if ((vp->stats.last_vol_op + (10 * 60)) >= now) {
4118                        *client_ec = VBUSY;
4119                    } else {
4120                        *client_ec = VRESTARTING;
4121                    }
4122                }
4123                *ec = VOFFLINE;
4124            }
4125            VChangeState_r(vp, VOL_STATE_UNATTACHED);
4126            FreeVolumeHeader(vp);
4127            vp = NULL;
4128            break;
4129         }
4130 #endif /* AFS_DEMAND_ATTACH_FS */
4131
4132         LoadVolumeHeader(ec, vp);
4133         if (*ec) {
4134             VGET_CTR_INC(V6);
4135             /* Only log the error if it was a totally unexpected error.  Simply
4136              * a missing inode is likely to be caused by the volume being deleted */
4137             if (errno != ENXIO || LogLevel)
4138                 Log("Volume %u: couldn't reread volume header\n",
4139                     vp->hashid);
4140 #ifdef AFS_DEMAND_ATTACH_FS
4141             if (VCanScheduleSalvage()) {
4142                 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
4143             } else {
4144                 FreeVolume(vp);
4145                 vp = NULL;
4146             }
4147 #else /* AFS_DEMAND_ATTACH_FS */
4148             FreeVolume(vp);
4149             vp = NULL;
4150 #endif /* AFS_DEMAND_ATTACH_FS */
4151             break;
4152         }
4153
4154         VGET_CTR_INC(V7);
4155         if (vp->shuttingDown) {
4156             VGET_CTR_INC(V8);
4157             *ec = VNOVOL;
4158             vp = NULL;
4159             break;
4160         }
4161
4162         if (programType == fileServer) {
4163             VGET_CTR_INC(V9);
4164             if (vp->goingOffline) {
4165                 if (timeout && VTimedOut(timeout)) {
4166                     /* we've timed out; don't wait for the vol */
4167                 } else {
4168                     VGET_CTR_INC(V10);
4169 #ifdef AFS_DEMAND_ATTACH_FS
4170                     /* wait for the volume to go offline */
4171                     if (V_attachState(vp) == VOL_STATE_GOING_OFFLINE) {
4172                         VTimedWaitStateChange_r(vp, timeout, NULL);
4173                     }
4174 #elif defined(AFS_PTHREAD_ENV)
4175                     VOL_CV_TIMEDWAIT(&vol_put_volume_cond, timeout, NULL);
4176 #else /* AFS_PTHREAD_ENV */
4177                     /* LWP has no timed wait, so the caller better not be
4178                      * expecting one */
4179                     osi_Assert(!timeout);
4180                     LWP_WaitProcess(VPutVolume);
4181 #endif /* AFS_PTHREAD_ENV */
4182                     continue;
4183                 }
4184             }
4185             if (vp->specialStatus) {
4186                 VGET_CTR_INC(V11);
4187                 *ec = vp->specialStatus;
4188             } else if (V_inService(vp) == 0 || V_blessed(vp) == 0) {
4189                 VGET_CTR_INC(V12);
4190                 *ec = VNOVOL;
4191             } else if (V_inUse(vp) == 0 || vp->goingOffline) {
4192                 VGET_CTR_INC(V13);
4193                 *ec = VOFFLINE;
4194             } else {
4195                 VGET_CTR_INC(V14);
4196             }
4197         }
4198         break;
4199     }
4200     VGET_CTR_INC(V15);
4201
4202 #ifdef AFS_DEMAND_ATTACH_FS
4203     /* if no error, bump nUsers */
4204     if (vp) {
4205         vp->nUsers++;
4206         VLRU_UpdateAccess_r(vp);
4207     }
4208     if (rvp) {
4209         VCancelReservation_r(rvp);
4210         rvp = NULL;
4211     }
4212     if (client_ec && !*client_ec) {
4213         *client_ec = *ec;
4214     }
4215 #else /* AFS_DEMAND_ATTACH_FS */
4216     /* if no error, bump nUsers */
4217     if (vp) {
4218         vp->nUsers++;
4219     }
4220     if (client_ec) {
4221         *client_ec = *ec;
4222     }
4223 #endif /* AFS_DEMAND_ATTACH_FS */
4224
4225  not_inited:
4226     osi_Assert(vp || *ec);
4227     return vp;
4228 }
4229
4230
4231 /***************************************************/
4232 /* Volume offline/detach routines                  */
4233 /***************************************************/
4234
4235 /* caller MUST hold a heavyweight ref on vp */
4236 #ifdef AFS_DEMAND_ATTACH_FS
4237 void
4238 VTakeOffline_r(Volume * vp)
4239 {
4240     Error error;
4241
4242     osi_Assert(vp->nUsers > 0);
4243     osi_Assert(programType == fileServer);
4244
4245     VCreateReservation_r(vp);
4246     VWaitExclusiveState_r(vp);
4247
4248     vp->goingOffline = 1;
4249     V_needsSalvaged(vp) = 1;
4250
4251     VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, 0);
4252     VCancelReservation_r(vp);
4253 }
4254 #else /* AFS_DEMAND_ATTACH_FS */
4255 void
4256 VTakeOffline_r(Volume * vp)
4257 {
4258     osi_Assert(vp->nUsers > 0);
4259     osi_Assert(programType == fileServer);
4260
4261     vp->goingOffline = 1;
4262     V_needsSalvaged(vp) = 1;
4263 }
4264 #endif /* AFS_DEMAND_ATTACH_FS */
4265
4266 void
4267 VTakeOffline(Volume * vp)
4268 {
4269     VOL_LOCK;
4270     VTakeOffline_r(vp);
4271     VOL_UNLOCK;
4272 }
4273
4274 /**
4275  * force a volume offline.
4276  *
4277  * @param[in] vp     volume object pointer
4278  * @param[in] flags  flags (see note below)
4279  *
4280  * @note the flag VOL_FORCEOFF_NOUPDATE is a recursion control flag
4281  *       used when VUpdateVolume_r needs to call VForceOffline_r
4282  *       (which in turn would normally call VUpdateVolume_r)
4283  *
4284  * @see VUpdateVolume_r
4285  *
4286  * @pre VOL_LOCK must be held.
4287  *      for DAFS, caller must hold ref.
4288  *
4289  * @note for DAFS, it _is safe_ to call this function from an
4290  *       exclusive state
4291  *
4292  * @post needsSalvaged flag is set.
4293  *       for DAFS, salvage is requested.
4294  *       no further references to the volume through the volume
4295  *       package will be honored.
4296  *       all file descriptor and vnode caches are invalidated.
4297  *
4298  * @warning this is a heavy-handed interface.  it results in
4299  *          a volume going offline regardless of the current
4300  *          reference count state.
4301  *
4302  * @internal  volume package internal use only
4303  */
4304 void
4305 VForceOffline_r(Volume * vp, int flags)
4306 {
4307     Error error;
4308     if (!V_inUse(vp)) {
4309 #ifdef AFS_DEMAND_ATTACH_FS
4310         VChangeState_r(vp, VOL_STATE_ERROR);
4311 #endif
4312         return;
4313     }
4314
4315     strcpy(V_offlineMessage(vp),
4316            "Forced offline due to internal error: volume needs to be salvaged");
4317     Log("Volume %u forced offline:  it needs salvaging!\n", V_id(vp));
4318
4319     V_inUse(vp) = 0;
4320     vp->goingOffline = 0;
4321     V_needsSalvaged(vp) = 1;
4322     if (!(flags & VOL_FORCEOFF_NOUPDATE)) {
4323         VUpdateVolume_r(&error, vp, VOL_UPDATE_NOFORCEOFF);
4324     }
4325
4326 #ifdef AFS_DEMAND_ATTACH_FS
4327     VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
4328 #endif /* AFS_DEMAND_ATTACH_FS */
4329
4330 #ifdef AFS_PTHREAD_ENV
4331     CV_BROADCAST(&vol_put_volume_cond);
4332 #else /* AFS_PTHREAD_ENV */
4333     LWP_NoYieldSignal(VPutVolume);
4334 #endif /* AFS_PTHREAD_ENV */
4335
4336     VReleaseVolumeHandles_r(vp);
4337 }
4338
4339 /**
4340  * force a volume offline.
4341  *
4342  * @param[in] vp  volume object pointer
4343  *
4344  * @see VForceOffline_r
4345  */
4346 void
4347 VForceOffline(Volume * vp)
4348 {
4349     VOL_LOCK;
4350     VForceOffline_r(vp, 0);
4351     VOL_UNLOCK;
4352 }
4353
4354 /**
4355  * Iterate over the RX calls associated with a volume, and interrupt them.
4356  *
4357  * @param[in] vp The volume whose RX calls we want to scan
4358  *
4359  * @pre VOL_LOCK held
4360  */
4361 static void
4362 VScanCalls_r(struct Volume *vp)
4363 {
4364     struct VCallByVol *cbv, *ncbv;
4365     afs_int32 err;
4366 #ifdef AFS_DEMAND_ATTACH_FS
4367     VolState state_save;
4368 #endif
4369
4370     if (queue_IsEmpty(&vp->rx_call_list))
4371         return; /* no calls to interrupt */
4372     if (!vol_opts.interrupt_rxcall)
4373         return; /* we have no function with which to interrupt calls */
4374     err = VIsGoingOffline_r(vp);
4375     if (!err)
4376         return; /* we're not going offline anymore */
4377
4378 #ifdef AFS_DEMAND_ATTACH_FS
4379     VWaitExclusiveState_r(vp);
4380     state_save = VChangeState_r(vp, VOL_STATE_SCANNING_RXCALLS);
4381     VOL_UNLOCK;
4382 #endif /* AFS_DEMAND_ATTACH_FS */
4383
4384     for(queue_Scan(&vp->rx_call_list, cbv, ncbv, VCallByVol)) {
4385         if (LogLevel > 0) {
4386             struct rx_peer *peer;
4387             char hoststr[16];
4388             peer = rx_PeerOf(rx_ConnectionOf(cbv->call));
4389
4390             Log("Offlining volume %lu while client %s:%u is trying to read "
4391                 "from it; kicking client off with error %ld\n",
4392                 (long unsigned) vp->hashid,
4393                 afs_inet_ntoa_r(rx_HostOf(peer), hoststr),
4394                 (unsigned) ntohs(rx_PortOf(peer)),
4395                 (long) err);
4396         }
4397         (*vol_opts.interrupt_rxcall) (cbv->call, err);
4398     }
4399
4400 #ifdef AFS_DEMAND_ATTACH_FS
4401     VOL_LOCK;
4402     VChangeState_r(vp, state_save);
4403 #endif /* AFS_DEMAND_ATTACH_FS */
4404 }
4405
4406 #ifdef AFS_DEMAND_ATTACH_FS
4407 /**
4408  * Wait for a vp to go offline.
4409  *
4410  * @param[out] ec 1 if a salvage on the volume has been requested and
4411  *                salvok == 0, 0 otherwise
4412  * @param[in] vp  The volume to wait for
4413  * @param[in] salvok  If 0, we return immediately with *ec = 1 if the volume
4414  *                    has been requested to salvage. Otherwise we keep waiting
4415  *                    until the volume has gone offline.
4416  *
4417  * @pre VOL_LOCK held
4418  * @pre caller holds a lightweight ref on vp
4419  *
4420  * @note DAFS only
4421  */
4422 static void
4423 VWaitForOfflineByVp_r(Error *ec, struct Volume *vp, int salvok)
4424 {
4425     struct timespec timeout_ts;
4426     const struct timespec *ts;
4427     int timedout = 0;
4428
4429     ts = VOfflineTimeout(&timeout_ts);
4430
4431     *ec = 0;
4432
4433     while (!VIsOfflineState(V_attachState(vp)) && !timedout) {
4434         if (!salvok && vp->salvage.requested) {
4435             *ec = 1;
4436             return;
4437         }
4438         VTimedWaitStateChange_r(vp, ts, &timedout);
4439     }
4440     if (!timedout) {
4441         /* we didn't time out, so the volume must be offline, so we're done */
4442         return;
4443     }
4444
4445     /* If we got here, we timed out waiting for the volume to go offline.
4446      * Kick off the accessing RX calls and wait again */
4447
4448     VScanCalls_r(vp);
4449
4450     while (!VIsOfflineState(V_attachState(vp))) {
4451         if (!salvok && vp->salvage.requested) {
4452             *ec = 1;
4453             return;
4454         }
4455
4456         VWaitStateChange_r(vp);
4457     }
4458 }
4459
4460 #else /* AFS_DEMAND_ATTACH_FS */
4461
4462 /**
4463  * Wait for a volume to go offline.
4464  *
4465  * @pre VOL_LOCK held
4466  *
4467  * @note non-DAFS only (for DAFS, use @see WaitForOfflineByVp_r)
4468  */
4469 static void
4470 VWaitForOffline_r(Error *ec, VolumeId volid)
4471 {
4472     struct Volume *vp;
4473     const struct timespec *ts;
4474 #ifdef AFS_PTHREAD_ENV
4475     struct timespec timeout_ts;
4476 #endif
4477
4478     ts = VOfflineTimeout(&timeout_ts);
4479
4480     vp = GetVolume(ec, NULL, volid, NULL, ts);
4481     if (!vp) {
4482         /* error occurred so bad that we can't even get a vp; we have no
4483          * information on the vol so we don't know whether to wait, so just
4484          * return */
4485         return;
4486     }
4487     if (!VIsGoingOffline_r(vp)) {
4488         /* volume is no longer going offline, so we're done */
4489         VPutVolume_r(vp);
4490         return;
4491     }
4492
4493     /* If we got here, we timed out waiting for the volume to go offline.
4494      * Kick off the accessing RX calls and wait again */
4495
4496     VScanCalls_r(vp);
4497     VPutVolume_r(vp);
4498     vp = NULL;
4499
4500     vp = VGetVolume_r(ec, volid);
4501     if (vp) {
4502         /* In case it was reattached... */
4503         VPutVolume_r(vp);
4504     }
4505 }
4506 #endif /* !AFS_DEMAND_ATTACH_FS */
4507
4508 /* The opposite of VAttachVolume.  The volume header is written to disk, with
4509    the inUse bit turned off.  A copy of the header is maintained in memory,
4510    however (which is why this is VOffline, not VDetach).
4511  */
4512 void
4513 VOffline_r(Volume * vp, char *message)
4514 {
4515     Error error;
4516 #ifndef AFS_DEMAND_ATTACH_FS
4517     VolumeId vid = V_id(vp);
4518 #endif
4519
4520     osi_Assert(programType != volumeUtility && programType != volumeServer);
4521     if (!V_inUse(vp)) {
4522         VPutVolume_r(vp);
4523         return;
4524     }
4525     if (V_offlineMessage(vp)[0] == '\0')
4526         strncpy(V_offlineMessage(vp), message, sizeof(V_offlineMessage(vp)));
4527     V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
4528
4529     vp->goingOffline = 1;
4530 #ifdef AFS_DEMAND_ATTACH_FS
4531     VChangeState_r(vp, VOL_STATE_GOING_OFFLINE);
4532     VCreateReservation_r(vp);
4533     VPutVolume_r(vp);
4534     VWaitForOfflineByVp_r(&error, vp, 1);
4535     VCancelReservation_r(vp);
4536 #else /* AFS_DEMAND_ATTACH_FS */
4537     VPutVolume_r(vp);
4538     VWaitForOffline_r(&error, vid);
4539 #endif /* AFS_DEMAND_ATTACH_FS */
4540 }
4541
4542 #ifdef AFS_DEMAND_ATTACH_FS
4543 /**
4544  * Take a volume offline in order to perform a volume operation.
4545  *
4546  * @param[inout] ec       address in which to store error code
4547  * @param[in]    vp       volume object pointer
4548  * @param[in]    message  volume offline status message
4549  *
4550  * @pre
4551  *    - VOL_LOCK is held
4552  *    - caller MUST hold a heavyweight ref on vp
4553  *
4554  * @post
4555  *    - volume is taken offline
4556  *    - if possible, volume operation is promoted to running state
4557  *    - on failure, *ec is set to nonzero
4558  *
4559  * @note Although this function does not return any value, it may
4560  *       still fail to promote our pending volume operation to
4561  *       a running state.  Any caller MUST check the value of *ec,
4562  *       and MUST NOT blindly assume success.
4563  *
4564  * @warning if the caller does not hold a lightweight ref on vp,
4565  *          then it MUST NOT reference vp after this function
4566  *          returns to the caller.
4567  *
4568  * @internal volume package internal use only
4569  */
4570 void
4571 VOfflineForVolOp_r(Error *ec, Volume *vp, char *message)
4572 {
4573     int salvok = 1;
4574     osi_Assert(vp->pending_vol_op);
4575     if (!V_inUse(vp)) {
4576         VPutVolume_r(vp);
4577         *ec = 1;
4578         return;
4579     }
4580     if (V_offlineMessage(vp)[0] == '\0')
4581         strncpy(V_offlineMessage(vp), message, sizeof(V_offlineMessage(vp)));
4582     V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
4583
4584     vp->goingOffline = 1;
4585     VChangeState_r(vp, VOL_STATE_GOING_OFFLINE);
4586     VCreateReservation_r(vp);
4587     VPutVolume_r(vp);
4588
4589     if (vp->pending_vol_op->com.programType != salvageServer) {
4590         /* do not give corrupted volumes to the volserver */
4591         salvok = 0;
4592     }
4593
4594     *ec = 0;
4595     VWaitForOfflineByVp_r(ec, vp, salvok);
4596
4597     VCancelReservation_r(vp);
4598 }
4599 #endif /* AFS_DEMAND_ATTACH_FS */
4600
4601 void
4602 VOffline(Volume * vp, char *message)
4603 {
4604     VOL_LOCK;
4605     VOffline_r(vp, message);
4606     VOL_UNLOCK;
4607 }
4608
4609 /* This gets used for the most part by utility routines that don't want
4610  * to keep all the volume headers around.  Generally, the file server won't
4611  * call this routine, because then the offline message in the volume header
4612  * (or other information) won't be available to clients. For NAMEI, also
4613  * close the file handles.  However, the fileserver does call this during
4614  * an attach following a volume operation.
4615  */
4616 void
4617 VDetachVolume_r(Error * ec, Volume * vp)
4618 {
4619 #ifdef FSSYNC_BUILD_CLIENT
4620     VolumeId volume;
4621     struct DiskPartition64 *tpartp;
4622     int notifyServer = 0;
4623     int  useDone = FSYNC_VOL_ON;
4624
4625     if (VCanUseFSSYNC()) {
4626         notifyServer = vp->needsPutBack;
4627         if (V_destroyMe(vp) == DESTROY_ME)
4628             useDone = FSYNC_VOL_LEAVE_OFF;
4629 # ifdef AFS_DEMAND_ATTACH_FS
4630         else if (!V_blessed(vp) || !V_inService(vp))
4631             useDone = FSYNC_VOL_LEAVE_OFF;
4632 # endif
4633     }
4634 # ifdef AFS_DEMAND_ATTACH_FS
4635     if (V_needsSalvaged(vp)) {
4636         notifyServer = 0;
4637         VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, 0);
4638     }
4639 # endif
4640     tpartp = vp->partition;
4641     volume = V_id(vp);
4642 #endif /* FSSYNC_BUILD_CLIENT */
4643
4644     *ec = 0;                    /* always "succeeds" */
4645     DeleteVolumeFromHashTable(vp);
4646     vp->shuttingDown = 1;
4647 #ifdef AFS_DEMAND_ATTACH_FS
4648     DeleteVolumeFromVByPList_r(vp);
4649     VLRU_Delete_r(vp);
4650     VChangeState_r(vp, VOL_STATE_SHUTTING_DOWN);
4651 #else
4652     if (programType != fileServer)
4653         V_inUse(vp) = 0;
4654 #endif /* AFS_DEMAND_ATTACH_FS */
4655     VPutVolume_r(vp);
4656     /* Will be detached sometime in the future--this is OK since volume is offline */
4657
4658     /* XXX the following code should really be moved to VCheckDetach() since the volume
4659      * is not technically detached until the refcounts reach zero
4660      */
4661 #ifdef FSSYNC_BUILD_CLIENT
4662     if (VCanUseFSSYNC() && notifyServer) {
4663         if (notifyServer == VOL_PUTBACK_DELETE) {
4664             /* Only send FSYNC_VOL_DONE if the volume was actually deleted.
4665              * volserver code will set needsPutBack to VOL_PUTBACK_DELETE
4666              * to signify a deleted volume. */
4667             useDone = FSYNC_VOL_DONE;
4668         }
4669         /*
4670          * Note:  The server is not notified in the case of a bogus volume
4671          * explicitly to make it possible to create a volume, do a partial
4672          * restore, then abort the operation without ever putting the volume
4673          * online.  This is essential in the case of a volume move operation
4674          * between two partitions on the same server.  In that case, there
4675          * would be two instances of the same volume, one of them bogus,
4676          * which the file server would attempt to put on line
4677          */
4678         FSYNC_VolOp(volume, tpartp->name, useDone, 0, NULL);
4679         /* XXX this code path is only hit by volume utilities, thus
4680          * V_BreakVolumeCallbacks will always be NULL.  if we really
4681          * want to break callbacks in this path we need to use FSYNC_VolOp() */
4682 #ifdef notdef
4683         /* Dettaching it so break all callbacks on it */
4684         if (V_BreakVolumeCallbacks) {
4685             Log("volume %u detached; breaking all call backs\n", volume);
4686             (*V_BreakVolumeCallbacks) (volume);
4687         }
4688 #endif
4689     }
4690 #endif /* FSSYNC_BUILD_CLIENT */
4691 }
4692
4693 void
4694 VDetachVolume(Error * ec, Volume * vp)
4695 {
4696     VOL_LOCK;
4697     VDetachVolume_r(ec, vp);
4698     VOL_UNLOCK;
4699 }
4700
4701
4702 /***************************************************/
4703 /* Volume fd/inode handle closing routines         */
4704 /***************************************************/
4705
4706 /* For VDetachVolume, we close all cached file descriptors, but keep
4707  * the Inode handles in case we need to read from a busy volume.
4708  */
4709 /* for demand attach, caller MUST hold ref count on vp */
4710 static void
4711 VCloseVolumeHandles_r(Volume * vp)
4712 {
4713 #ifdef AFS_DEMAND_ATTACH_FS
4714     VolState state_save;
4715
4716     state_save = VChangeState_r(vp, VOL_STATE_OFFLINING);
4717
4718     VOL_UNLOCK;
4719 #endif
4720
4721     DFlushVolume(vp->hashid);
4722
4723 #ifdef AFS_DEMAND_ATTACH_FS
4724     VOL_LOCK;
4725 #endif
4726
4727     /* DAFS: VCloseVnodeFiles_r drops the glock internally */
4728     VCloseVnodeFiles_r(vp);
4729
4730 #ifdef AFS_DEMAND_ATTACH_FS
4731     VOL_UNLOCK;
4732 #endif
4733
4734     /* Too time consuming and unnecessary for the volserver */
4735     if (programType == fileServer) {
4736         IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
4737         IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
4738         IH_CONDSYNC(vp->diskDataHandle);
4739 #ifdef AFS_NT40_ENV
4740         IH_CONDSYNC(vp->linkHandle);
4741 #endif /* AFS_NT40_ENV */
4742     }
4743
4744     IH_REALLYCLOSE(vp->vnodeIndex[vLarge].handle);
4745     IH_REALLYCLOSE(vp->vnodeIndex[vSmall].handle);
4746     IH_REALLYCLOSE(vp->diskDataHandle);
4747     IH_REALLYCLOSE(vp->linkHandle);
4748
4749 #ifdef AFS_DEMAND_ATTACH_FS
4750     if ((V_attachFlags(vp) & VOL_LOCKED)) {
4751         VUnlockVolume(vp);
4752     }
4753
4754     VOL_LOCK;
4755     VChangeState_r(vp, state_save);
4756 #endif
4757 }
4758
4759 /* For both VForceOffline and VOffline, we close all relevant handles.
4760  * For VOffline, if we re-attach the volume, the files may possible be
4761  * different than before.
4762  */
4763 /* for demand attach, caller MUST hold a ref count on vp */
4764 static void
4765 VReleaseVolumeHandles_r(Volume * vp)
4766 {
4767 #ifdef AFS_DEMAND_ATTACH_FS
4768     VolState state_save;
4769
4770     state_save = VChangeState_r(vp, VOL_STATE_DETACHING);
4771
4772     VOL_UNLOCK;
4773 #endif
4774
4775     DFlushVolume(vp->hashid);
4776
4777 #ifdef AFS_DEMAND_ATTACH_FS
4778     VOL_LOCK;
4779 #endif
4780
4781     VReleaseVnodeFiles_r(vp); /* DAFS: releases the glock internally */
4782
4783 #ifdef AFS_DEMAND_ATTACH_FS
4784     VOL_UNLOCK;
4785 #endif
4786
4787     /* Too time consuming and unnecessary for the volserver */
4788     if (programType == fileServer) {
4789         IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
4790         IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
4791         IH_CONDSYNC(vp->diskDataHandle);
4792 #ifdef AFS_NT40_ENV
4793         IH_CONDSYNC(vp->linkHandle);
4794 #endif /* AFS_NT40_ENV */
4795     }
4796
4797     IH_RELEASE(vp->vnodeIndex[vLarge].handle);
4798     IH_RELEASE(vp->vnodeIndex[vSmall].handle);
4799     IH_RELEASE(vp->diskDataHandle);
4800     IH_RELEASE(vp->linkHandle);
4801
4802 #ifdef AFS_DEMAND_ATTACH_FS
4803     if ((V_attachFlags(vp) & VOL_LOCKED)) {
4804         VUnlockVolume(vp);
4805     }
4806
4807     VOL_LOCK;
4808     VChangeState_r(vp, state_save);
4809 #endif
4810 }
4811
4812
4813 /***************************************************/
4814 /* Volume write and fsync routines                 */
4815 /***************************************************/
4816
4817 void
4818 VUpdateVolume_r(Error * ec, Volume * vp, int flags)
4819 {
4820 #ifdef AFS_DEMAND_ATTACH_FS
4821     VolState state_save;
4822
4823     if (flags & VOL_UPDATE_WAIT) {
4824         VCreateReservation_r(vp);
4825         VWaitExclusiveState_r(vp);
4826     }
4827 #endif
4828
4829     *ec = 0;
4830     if (programType == fileServer)
4831         V_uniquifier(vp) =
4832             (V_inUse(vp) ? V_nextVnodeUnique(vp) +
4833              200 : V_nextVnodeUnique(vp));
4834
4835 #ifdef AFS_DEMAND_ATTACH_FS
4836     state_save = VChangeState_r(vp, VOL_STATE_UPDATING);
4837     VOL_UNLOCK;
4838 #endif
4839
4840     WriteVolumeHeader_r(ec, vp);
4841
4842 #ifdef AFS_DEMAND_ATTACH_FS
4843     VOL_LOCK;
4844     VChangeState_r(vp, state_save);
4845     if (flags & VOL_UPDATE_WAIT) {
4846         VCancelReservation_r(vp);
4847     }
4848 #endif
4849
4850     if (*ec) {
4851         Log("VUpdateVolume: error updating volume header, volume %u (%s)\n",
4852             V_id(vp), V_name(vp));
4853         /* try to update on-disk header,
4854          * while preventing infinite recursion */
4855         if (!(flags & VOL_UPDATE_NOFORCEOFF)) {
4856             VForceOffline_r(vp, VOL_FORCEOFF_NOUPDATE);
4857         }
4858     }
4859 }
4860
4861 void
4862 VUpdateVolume(Error * ec, Volume * vp)
4863 {
4864     VOL_LOCK;
4865     VUpdateVolume_r(ec, vp, VOL_UPDATE_WAIT);
4866     VOL_UNLOCK;
4867 }
4868
4869 void
4870 VSyncVolume_r(Error * ec, Volume * vp, int flags)
4871 {
4872     FdHandle_t *fdP;
4873     int code;
4874 #ifdef AFS_DEMAND_ATTACH_FS
4875     VolState state_save;
4876 #endif
4877
4878     if (flags & VOL_SYNC_WAIT) {
4879         VUpdateVolume_r(ec, vp, VOL_UPDATE_WAIT);
4880     } else {
4881         VUpdateVolume_r(ec, vp, 0);
4882     }
4883     if (!*ec) {
4884 #ifdef AFS_DEMAND_ATTACH_FS
4885         state_save = VChangeState_r(vp, VOL_STATE_UPDATING);
4886         VOL_UNLOCK;
4887 #endif
4888         fdP = IH_OPEN(V_diskDataHandle(vp));
4889         osi_Assert(fdP != NULL);
4890         code = FDH_SYNC(fdP);
4891         osi_Assert(code == 0);
4892         FDH_CLOSE(fdP);
4893 #ifdef AFS_DEMAND_ATTACH_FS
4894         VOL_LOCK;
4895         VChangeState_r(vp, state_save);
4896 #endif
4897     }
4898 }
4899
4900 void
4901 VSyncVolume(Error * ec, Volume * vp)
4902 {
4903     VOL_LOCK;
4904     VSyncVolume_r(ec, vp, VOL_SYNC_WAIT);
4905     VOL_UNLOCK;
4906 }
4907
4908
4909 /***************************************************/
4910 /* Volume dealloaction routines                    */
4911 /***************************************************/
4912
4913 #ifdef AFS_DEMAND_ATTACH_FS
4914 static void
4915 FreeVolume(Volume * vp)
4916 {
4917     /* free the heap space, iff it's safe.
4918      * otherwise, pull it out of the hash table, so it
4919      * will get deallocated when all refs to it go away */
4920     if (!VCheckFree(vp)) {
4921         DeleteVolumeFromHashTable(vp);
4922         DeleteVolumeFromVByPList_r(vp);
4923
4924         /* make sure we invalidate the header cache entry */
4925         FreeVolumeHeader(vp);
4926     }
4927 }
4928 #endif /* AFS_DEMAND_ATTACH_FS */
4929
4930 static void
4931 ReallyFreeVolume(Volume * vp)
4932 {
4933     int i;
4934     if (!vp)
4935         return;
4936 #ifdef AFS_DEMAND_ATTACH_FS
4937     /* debug */
4938     VChangeState_r(vp, VOL_STATE_FREED);
4939     if (vp->pending_vol_op)
4940         free(vp->pending_vol_op);
4941 #endif /* AFS_DEMAND_ATTACH_FS */
4942     for (i = 0; i < nVNODECLASSES; i++)
4943         if (vp->vnodeIndex[i].bitmap)
4944             free(vp->vnodeIndex[i].bitmap);
4945     FreeVolumeHeader(vp);
4946 #ifndef AFS_DEMAND_ATTACH_FS
4947     DeleteVolumeFromHashTable(vp);
4948 #endif /* AFS_DEMAND_ATTACH_FS */
4949     free(vp);
4950 }
4951
4952 /* check to see if we should shutdown this volume
4953  * returns 1 if volume was freed, 0 otherwise */
4954 #ifdef AFS_DEMAND_ATTACH_FS
4955 static int
4956 VCheckDetach(Volume * vp)
4957 {
4958     int ret = 0;
4959     Error ec = 0;
4960
4961     if (vp->nUsers || vp->nWaiters)
4962         return ret;
4963
4964     if (vp->shuttingDown) {
4965         ret = 1;
4966         if ((programType != fileServer) &&
4967             (V_inUse(vp) == programType) &&
4968             ((V_checkoutMode(vp) == V_VOLUPD) ||
4969              (V_checkoutMode(vp) == V_SECRETLY) ||
4970              ((V_checkoutMode(vp) == V_CLONE) &&
4971               (VolumeWriteable(vp))))) {
4972             V_inUse(vp) = 0;
4973             VUpdateVolume_r(&ec, vp, VOL_UPDATE_NOFORCEOFF);
4974             if (ec) {
4975                 Log("VCheckDetach: volume header update for volume %u "
4976                     "failed with errno %d\n", vp->hashid, errno);
4977             }
4978         }
4979         VReleaseVolumeHandles_r(vp);
4980         VCheckSalvage(vp);
4981         ReallyFreeVolume(vp);
4982         if (programType == fileServer) {
4983             CV_BROADCAST(&vol_put_volume_cond);
4984         }
4985     }
4986     return ret;
4987 }
4988 #else /* AFS_DEMAND_ATTACH_FS */
4989 static int
4990 VCheckDetach(Volume * vp)
4991 {
4992     int ret = 0;
4993     Error ec = 0;
4994
4995     if (vp->nUsers)
4996         return ret;
4997
4998     if (vp->shuttingDown) {
4999         ret = 1;
5000         if ((programType != fileServer) &&
5001             (V_inUse(vp) == programType) &&
5002             ((V_checkoutMode(vp) == V_VOLUPD) ||
5003              (V_checkoutMode(vp) == V_SECRETLY) ||
5004              ((V_checkoutMode(vp) == V_CLONE) &&
5005               (VolumeWriteable(vp))))) {
5006             V_inUse(vp) = 0;
5007             VUpdateVolume_r(&ec, vp, VOL_UPDATE_NOFORCEOFF);
5008             if (ec) {
5009                 Log("VCheckDetach: volume header update for volume %u failed with errno %d\n",
5010                     vp->hashid, errno);
5011             }
5012         }
5013         VReleaseVolumeHandles_r(vp);
5014         ReallyFreeVolume(vp);
5015         if (programType == fileServer) {
5016 #if defined(AFS_PTHREAD_ENV)
5017             CV_BROADCAST(&vol_put_volume_cond);
5018 #else /* AFS_PTHREAD_ENV */
5019             LWP_NoYieldSignal(VPutVolume);
5020 #endif /* AFS_PTHREAD_ENV */
5021         }
5022     }
5023     return ret;
5024 }
5025 #endif /* AFS_DEMAND_ATTACH_FS */
5026
5027 /* check to see if we should offline this volume
5028  * return 1 if volume went offline, 0 otherwise */
5029 #ifdef AFS_DEMAND_ATTACH_FS
5030 static int
5031 VCheckOffline(Volume * vp)
5032 {
5033     int ret = 0;
5034
5035     if (vp->goingOffline && !vp->nUsers) {
5036         Error error;
5037         osi_Assert(programType == fileServer);
5038         osi_Assert((V_attachState(vp) != VOL_STATE_ATTACHED) &&
5039                (V_attachState(vp) != VOL_STATE_FREED) &&
5040                (V_attachState(vp) != VOL_STATE_PREATTACHED) &&
5041                (V_attachState(vp) != VOL_STATE_UNATTACHED) &&
5042                (V_attachState(vp) != VOL_STATE_DELETED));
5043
5044         /* valid states:
5045          *
5046          * VOL_STATE_GOING_OFFLINE
5047          * VOL_STATE_SHUTTING_DOWN
5048          * VIsErrorState(V_attachState(vp))
5049          * VIsExclusiveState(V_attachState(vp))
5050          */
5051
5052         VCreateReservation_r(vp);
5053         VChangeState_r(vp, VOL_STATE_OFFLINING);
5054
5055         ret = 1;
5056         /* must clear the goingOffline flag before we drop the glock */
5057         vp->goingOffline = 0;
5058         V_inUse(vp) = 0;
5059
5060         VLRU_Delete_r(vp);
5061
5062         /* perform async operations */
5063         VUpdateVolume_r(&error, vp, 0);
5064         VCloseVolumeHandles_r(vp);
5065
5066         if (LogLevel) {
5067             if (V_offlineMessage(vp)[0]) {
5068                 Log("VOffline: Volume %lu (%s) is now offline (%s)\n",
5069                     afs_printable_uint32_lu(V_id(vp)), V_name(vp),
5070                     V_offlineMessage(vp));
5071             } else {
5072                 Log("VOffline: Volume %lu (%s) is now offline\n",
5073                     afs_printable_uint32_lu(V_id(vp)), V_name(vp));
5074             }
5075         }
5076
5077         /* invalidate the volume header cache entry */
5078         FreeVolumeHeader(vp);
5079
5080         /* if nothing changed state to error or salvaging,
5081          * drop state to unattached */
5082         if (!VIsErrorState(V_attachState(vp))) {
5083             VChangeState_r(vp, VOL_STATE_UNATTACHED);
5084         }
5085         VCancelReservation_r(vp);
5086         /* no usage of vp is safe beyond this point */
5087     }
5088     return ret;
5089 }
5090 #else /* AFS_DEMAND_ATTACH_FS */
5091 static int
5092 VCheckOffline(Volume * vp)
5093 {
5094     int ret = 0;
5095
5096     if (vp->goingOffline && !vp->nUsers) {
5097         Error error;
5098         osi_Assert(programType == fileServer);
5099
5100         ret = 1;
5101         vp->goingOffline = 0;
5102         V_inUse(vp) = 0;
5103         VUpdateVolume_r(&error, vp, 0);
5104         VCloseVolumeHandles_r(vp);
5105         if (LogLevel) {
5106             if (V_offlineMessage(vp)[0]) {
5107                 Log("VOffline: Volume %lu (%s) is now offline (%s)\n",
5108                     afs_printable_uint32_lu(V_id(vp)), V_name(vp),
5109                     V_offlineMessage(vp));
5110             } else {
5111                 Log("VOffline: Volume %lu (%s) is now offline\n",
5112                     afs_printable_uint32_lu(V_id(vp)), V_name(vp));
5113             }
5114         }
5115         FreeVolumeHeader(vp);
5116 #ifdef AFS_PTHREAD_ENV
5117         CV_BROADCAST(&vol_put_volume_cond);
5118 #else /* AFS_PTHREAD_ENV */
5119         LWP_NoYieldSignal(VPutVolume);
5120 #endif /* AFS_PTHREAD_ENV */
5121     }
5122     return ret;
5123 }
5124 #endif /* AFS_DEMAND_ATTACH_FS */
5125
5126 /***************************************************/
5127 /* demand attach fs ref counting routines          */
5128 /***************************************************/
5129
5130 #ifdef AFS_DEMAND_ATTACH_FS
5131 /* the following two functions handle reference counting for
5132  * asynchronous operations on volume structs.
5133  *
5134  * their purpose is to prevent a VDetachVolume or VShutdown
5135  * from free()ing the Volume struct during an async i/o op */
5136
5137 /* register with the async volume op ref counter */
5138 /* VCreateReservation_r moved into inline code header because it
5139  * is now needed in vnode.c -- tkeiser 11/20/2007
5140  */
5141
5142 /**
5143  * decrement volume-package internal refcount.
5144  *
5145  * @param vp  volume object pointer
5146  *
5147  * @internal volume package internal use only
5148  *
5149  * @pre
5150  *    @arg VOL_LOCK is held
5151  *    @arg lightweight refcount held
5152  *
5153  * @post volume waiters refcount is decremented; volume may
5154  *       have been deallocated/shutdown/offlined/salvaged/
5155  *       whatever during the process
5156  *
5157  * @warning once you have tossed your last reference (you can acquire
5158  *          lightweight refs recursively) it is NOT SAFE to reference
5159  *          a volume object pointer ever again
5160  *
5161  * @see VCreateReservation_r
5162  *
5163  * @note DEMAND_ATTACH_FS only
5164  */
5165 void
5166 VCancelReservation_r(Volume * vp)
5167 {
5168     osi_Assert(--vp->nWaiters >= 0);
5169     if (vp->nWaiters == 0) {
5170         VCheckOffline(vp);
5171         if (!VCheckDetach(vp)) {
5172             VCheckSalvage(vp);
5173             VCheckFree(vp);
5174         }
5175     }
5176 }
5177
5178 /* check to see if we should free this volume now
5179  * return 1 if volume was freed, 0 otherwise */
5180 static int
5181 VCheckFree(Volume * vp)
5182 {
5183     int ret = 0;
5184     if ((vp->nUsers == 0) &&
5185         (vp->nWaiters == 0) &&
5186         !(V_attachFlags(vp) & (VOL_IN_HASH |
5187                                VOL_ON_VBYP_LIST |
5188                                VOL_IS_BUSY |
5189                                VOL_ON_VLRU))) {
5190         ReallyFreeVolume(vp);
5191         ret = 1;
5192     }
5193     return ret;
5194 }
5195 #endif /* AFS_DEMAND_ATTACH_FS */
5196
5197
5198 /***************************************************/
5199 /* online volume operations routines               */
5200 /***************************************************/
5201
5202 #ifdef AFS_DEMAND_ATTACH_FS
5203 /**
5204  * register a volume operation on a given volume.
5205  *
5206  * @param[in] vp       volume object
5207  * @param[in] vopinfo  volume operation info object
5208  *
5209  * @pre VOL_LOCK is held
5210  *
5211  * @post volume operation info object attached to volume object.
5212  *       volume operation statistics updated.
5213  *
5214  * @note by "attached" we mean a copy of the passed in object is made
5215  *
5216  * @internal volume package internal use only
5217  */
5218 int
5219 VRegisterVolOp_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
5220 {
5221     FSSYNC_VolOp_info * info;
5222
5223     /* attach a vol op info node to the volume struct */
5224     info = (FSSYNC_VolOp_info *) malloc(sizeof(FSSYNC_VolOp_info));
5225     osi_Assert(info != NULL);
5226     memcpy(info, vopinfo, sizeof(FSSYNC_VolOp_info));
5227     vp->pending_vol_op = info;
5228
5229     /* update stats */
5230     vp->stats.last_vol_op = FT_ApproxTime();
5231     vp->stats.vol_ops++;
5232     IncUInt64(&VStats.vol_ops);
5233
5234     return 0;
5235 }
5236
5237 /**
5238  * deregister the volume operation attached to this volume.
5239  *
5240  * @param[in] vp  volume object pointer
5241  *
5242  * @pre VOL_LOCK is held
5243  *
5244  * @post the volume operation info object is detached from the volume object
5245  *
5246  * @internal volume package internal use only
5247  */
5248 int
5249 VDeregisterVolOp_r(Volume * vp)
5250 {
5251     if (vp->pending_vol_op) {
5252         free(vp->pending_vol_op);
5253         vp->pending_vol_op = NULL;
5254     }
5255     return 0;
5256 }
5257 #endif /* AFS_DEMAND_ATTACH_FS */
5258
5259 /**
5260  * determine whether it is safe to leave a volume online during
5261  * the volume operation described by the vopinfo object.
5262  *
5263  * @param[in] vp        volume object
5264  * @param[in] vopinfo   volume operation info object
5265  *
5266  * @return whether it is safe to leave volume online
5267  *    @retval 0  it is NOT SAFE to leave the volume online
5268  *    @retval 1  it is safe to leave the volume online during the operation
5269  *
5270  * @pre
5271  *    @arg VOL_LOCK is held
5272  *    @arg disk header attached to vp (heavyweight ref on vp will guarantee
5273  *         this condition is met)
5274  *
5275  * @internal volume package internal use only
5276  */
5277 int
5278 VVolOpLeaveOnline_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
5279 {
5280     return (vopinfo->vol_op_state == FSSYNC_VolOpRunningOnline ||
5281             (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
5282             (vopinfo->com.reason == V_READONLY ||
5283              (!VolumeWriteable(vp) &&
5284               (vopinfo->com.reason == V_CLONE ||
5285                vopinfo->com.reason == V_DUMP)))));
5286 }
5287
5288 /**
5289  * same as VVolOpLeaveOnline_r, but does not require a volume with an attached
5290  * header.
5291  *
5292  * @param[in] vp        volume object
5293  * @param[in] vopinfo   volume operation info object
5294  *
5295  * @return whether it is safe to leave volume online
5296  *    @retval 0  it is NOT SAFE to leave the volume online
5297  *    @retval 1  it is safe to leave the volume online during the operation
5298  *    @retval -1 unsure; volume header is required in order to know whether or
5299  *               not is is safe to leave the volume online
5300  *
5301  * @pre VOL_LOCK is held
5302  *
5303  * @internal volume package internal use only
5304  */
5305 int
5306 VVolOpLeaveOnlineNoHeader_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
5307 {
5308     /* follow the logic in VVolOpLeaveOnline_r; this is the same, except
5309      * assume that we don't know VolumeWriteable; return -1 if the answer
5310      * depends on VolumeWriteable */
5311
5312     if (vopinfo->vol_op_state == FSSYNC_VolOpRunningOnline) {
5313         return 1;
5314     }
5315     if (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
5316         vopinfo->com.reason == V_READONLY) {
5317
5318         return 1;
5319     }
5320     if (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
5321         (vopinfo->com.reason == V_CLONE ||
5322          vopinfo->com.reason == V_DUMP)) {
5323
5324         /* must know VolumeWriteable */
5325         return -1;
5326     }
5327     return 0;
5328 }
5329
5330 /**
5331  * determine whether VBUSY should be set during this volume operation.
5332  *
5333  * @param[in] vp        volume object
5334  * @param[in] vopinfo   volume operation info object
5335  *
5336  * @return whether VBUSY should be set
5337  *   @retval 0  VBUSY does NOT need to be set
5338  *   @retval 1  VBUSY SHOULD be set
5339  *
5340  * @pre VOL_LOCK is held
5341  *
5342  * @internal volume package internal use only
5343  */
5344 int
5345 VVolOpSetVBusy_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
5346 {
5347     return ((vopinfo->com.command == FSYNC_VOL_OFF &&
5348             vopinfo->com.reason == FSYNC_SALVAGE) ||
5349             (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
5350             (vopinfo->com.reason == V_CLONE ||
5351              vopinfo->com.reason == V_DUMP)));
5352 }
5353
5354
5355 /***************************************************/
5356 /* online salvager routines                        */
5357 /***************************************************/
5358 #if defined(AFS_DEMAND_ATTACH_FS)
5359
5360 /**
5361  * offline a volume to let it be salvaged.
5362  *
5363  * @param[in] vp  Volume to offline
5364  *
5365  * @return whether we offlined the volume successfully
5366  *  @retval 0 volume was not offlined
5367  *  @retval 1 volume is now offline
5368  *
5369  * @note This is similar to VCheckOffline, but slightly different. We do not
5370  *       deal with vp->goingOffline, and we try to avoid touching the volume
5371  *       header except just to set needsSalvaged
5372  *
5373  * @pre VOL_LOCK held
5374  * @pre vp->nUsers == 0
5375  * @pre V_attachState(vp) == VOL_STATE_SALVAGE_REQ
5376  */
5377 static int
5378 VOfflineForSalvage_r(struct Volume *vp)
5379 {
5380     Error error;
5381
5382     VCreateReservation_r(vp);
5383     VWaitExclusiveState_r(vp);
5384
5385     if (vp->nUsers || V_attachState(vp) == VOL_STATE_SALVAGING) {
5386         /* Someone's using the volume, or someone got to scheduling the salvage
5387          * before us. I don't think either of these should be possible, as we
5388          * should gain no new heavyweight references while we're trying to
5389          * salvage, but just to be sure... */
5390         VCancelReservation_r(vp);
5391         return 0;
5392     }
5393
5394     VChangeState_r(vp, VOL_STATE_OFFLINING);
5395
5396     VLRU_Delete_r(vp);
5397     if (vp->header) {
5398         V_needsSalvaged(vp) = 1;
5399         /* ignore error; updating needsSalvaged is just best effort */
5400         VUpdateVolume_r(&error, vp, VOL_UPDATE_NOFORCEOFF);
5401     }
5402     VCloseVolumeHandles_r(vp);
5403
5404     FreeVolumeHeader(vp);
5405
5406     /* volume has been effectively offlined; we can mark it in the SALVAGING
5407      * state now, which lets FSSYNC give it away */
5408     VChangeState_r(vp, VOL_STATE_SALVAGING);
5409
5410     VCancelReservation_r(vp);
5411
5412     return 1;
5413 }
5414
5415 /**
5416  * check whether a salvage needs to be performed on this volume.
5417  *
5418  * @param[in] vp   pointer to volume object
5419  *
5420  * @return status code
5421  *    @retval 0 no salvage scheduled
5422  *    @retval 1 a salvage has been scheduled with the salvageserver
5423  *
5424  * @pre VOL_LOCK is held
5425  *
5426  * @post if salvage request flag is set and nUsers and nWaiters are zero,
5427  *       then a salvage will be requested
5428  *
5429  * @note this is one of the event handlers called by VCancelReservation_r
5430  *
5431  * @note the caller must check if the volume needs to be freed after calling
5432  *       this; the volume may not have any references or be on any lists after
5433  *       we return, and we do not free it
5434  *
5435  * @see VCancelReservation_r
5436  *
5437  * @internal volume package internal use only.
5438  */
5439 static int
5440 VCheckSalvage(Volume * vp)
5441 {
5442     int ret = 0;
5443 #if defined(SALVSYNC_BUILD_CLIENT) || defined(FSSYNC_BUILD_CLIENT)
5444     if (vp->nUsers)
5445         return ret;
5446     if (!vp->salvage.requested) {
5447         return ret;
5448     }
5449
5450     /* prevent recursion; some of the code below creates and removes
5451      * lightweight refs, which can call VCheckSalvage */
5452     if (vp->salvage.scheduling) {
5453         return ret;
5454     }
5455     vp->salvage.scheduling = 1;
5456
5457     if (V_attachState(vp) == VOL_STATE_SALVAGE_REQ) {
5458         if (!VOfflineForSalvage_r(vp)) {
5459             vp->salvage.scheduling = 0;
5460             return ret;
5461         }
5462     }
5463
5464     if (vp->salvage.requested) {
5465         VScheduleSalvage_r(vp);
5466         ret = 1;
5467     }
5468     vp->salvage.scheduling = 0;
5469 #endif /* SALVSYNC_BUILD_CLIENT || FSSYNC_BUILD_CLIENT */
5470     return ret;
5471 }
5472
5473 /**
5474  * request volume salvage.
5475  *
5476  * @param[out] ec      computed client error code
5477  * @param[in]  vp      volume object pointer
5478  * @param[in]  reason  reason code (passed to salvageserver via SALVSYNC)
5479  * @param[in]  flags   see flags note below
5480  *
5481  * @note flags:
5482  *       VOL_SALVAGE_INVALIDATE_HEADER causes volume header cache entry
5483  *                                     to be invalidated.
5484  *
5485  * @pre VOL_LOCK is held.
5486  *
5487  * @post volume state is changed.
5488  *       for fileserver, salvage will be requested once refcount reaches zero.
5489  *
5490  * @return operation status code
5491  *   @retval 0  volume salvage will occur
5492  *   @retval 1  volume salvage could not be scheduled
5493  *
5494  * @note DAFS only
5495  *
5496  * @note in the fileserver, this call does not synchronously schedule a volume
5497  *       salvage. rather, it sets volume state so that when volume refcounts
5498  *       reach zero, a volume salvage will occur. by "refcounts", we mean both
5499  *       nUsers and nWaiters must be zero.
5500  *
5501  * @internal volume package internal use only.
5502  */
5503 int
5504 VRequestSalvage_r(Error * ec, Volume * vp, int reason, int flags)
5505 {
5506     int code = 0;
5507     /*
5508      * for DAFS volume utilities that are not supposed to schedule salvages,
5509      * just transition to error state instead
5510      */
5511     if (!VCanScheduleSalvage()) {
5512         VChangeState_r(vp, VOL_STATE_ERROR);
5513         *ec = VSALVAGE;
5514         return 1;
5515     }
5516
5517     if (programType != fileServer && !VCanUseFSSYNC()) {
5518         VChangeState_r(vp, VOL_STATE_ERROR);
5519         *ec = VSALVAGE;
5520         return 1;
5521     }
5522
5523     if (!vp->salvage.requested) {
5524         vp->salvage.requested = 1;
5525         vp->salvage.reason = reason;
5526         vp->stats.last_salvage = FT_ApproxTime();
5527
5528         /* Note that it is not possible for us to reach this point if a
5529          * salvage is already running on this volume (even if the fileserver
5530          * was restarted during the salvage). If a salvage were running, the
5531          * salvager would have write-locked the volume header file, so when
5532          * we tried to lock the volume header, the lock would have failed,
5533          * and we would have failed during attachment prior to calling
5534          * VRequestSalvage. So we know that we can schedule salvages without
5535          * fear of a salvage already running for this volume. */
5536
5537         if (vp->stats.salvages < SALVAGE_COUNT_MAX) {
5538
5539             /* if we don't need to offline the volume, we can go directly
5540              * to SALVAGING. SALVAGING says the volume is offline and is
5541              * either salvaging or ready to be handed to the salvager.
5542              * SALVAGE_REQ says that we want to salvage the volume, but we
5543              * are waiting for it to go offline first. */
5544             if (flags & VOL_SALVAGE_NO_OFFLINE) {
5545                 VChangeState_r(vp, VOL_STATE_SALVAGING);
5546             } else {
5547                 VChangeState_r(vp, VOL_STATE_SALVAGE_REQ);
5548                 if (vp->nUsers == 0) {
5549                     /* normally VOfflineForSalvage_r would be called from
5550                      * PutVolume et al when nUsers reaches 0, but if
5551                      * it's already 0, just do it ourselves, since PutVolume
5552                      * isn't going to get called */
5553                     VOfflineForSalvage_r(vp);
5554                 }
5555             }
5556             /* If we are non-fileserver, we're telling the fileserver to
5557              * salvage the vol, so we don't need to give it back separately. */
5558             vp->needsPutBack = 0;
5559
5560             *ec = VSALVAGING;
5561         } else {
5562             Log("VRequestSalvage: volume %u online salvaged too many times; forced offline.\n", vp->hashid);
5563
5564             /* make sure neither VScheduleSalvage_r nor
5565              * VUpdateSalvagePriority_r try to schedule another salvage */
5566             vp->salvage.requested = vp->salvage.scheduled = 0;
5567
5568             VChangeState_r(vp, VOL_STATE_ERROR);
5569             *ec = VSALVAGE;
5570             code = 1;
5571         }
5572         if (flags & VOL_SALVAGE_INVALIDATE_HEADER) {
5573             /* Instead of ReleaseVolumeHeader, we do FreeVolumeHeader()
5574                so that the the next VAttachVolumeByVp_r() invocation
5575                of attach2() will pull in a cached header
5576                entry and fail, then load a fresh one from disk and attach
5577                it to the volume.
5578             */
5579             FreeVolumeHeader(vp);
5580         }
5581     }
5582     return code;
5583 }
5584
5585 /**
5586  * update salvageserver scheduling priority for a volume.
5587  *
5588  * @param[in] vp  pointer to volume object
5589  *
5590  * @return operation status
5591  *   @retval 0  success
5592  *   @retval 1  request denied, or SALVSYNC communications failure
5593  *
5594  * @pre VOL_LOCK is held.
5595  *
5596  * @post in-core salvage priority counter is incremented.  if at least
5597  *       SALVAGE_PRIO_UPDATE_INTERVAL seconds have elapsed since the
5598  *       last SALVSYNC_RAISEPRIO request, we contact the salvageserver
5599  *       to update its priority queue.  if no salvage is scheduled,
5600  *       this function is a no-op.
5601  *
5602  * @note DAFS fileserver only
5603  *
5604  * @note this should be called whenever a VGetVolume fails due to a
5605  *       pending salvage request
5606  *
5607  * @todo should set exclusive state and drop glock around salvsync call
5608  *
5609  * @internal volume package internal use only.
5610  */
5611 int
5612 VUpdateSalvagePriority_r(Volume * vp)
5613 {
5614     int ret=0;
5615
5616 #ifdef SALVSYNC_BUILD_CLIENT
5617     afs_uint32 now;
5618     int code;
5619
5620     vp->salvage.prio++;
5621     now = FT_ApproxTime();
5622
5623     /* update the salvageserver priority queue occasionally so that
5624      * frequently requested volumes get moved to the head of the queue
5625      */
5626     if ((vp->salvage.scheduled) &&
5627         (vp->stats.last_salvage_req < (now-SALVAGE_PRIO_UPDATE_INTERVAL))) {
5628         code = SALVSYNC_SalvageVolume(vp->hashid,
5629                                       VPartitionPath(vp->partition),
5630                                       SALVSYNC_RAISEPRIO,
5631                                       vp->salvage.reason,
5632                                       vp->salvage.prio,
5633                                       NULL);
5634         vp->stats.last_salvage_req = now;
5635         if (code != SYNC_OK) {
5636             ret = 1;
5637         }
5638     }
5639 #endif /* SALVSYNC_BUILD_CLIENT */
5640     return ret;
5641 }
5642
5643
5644 #if defined(SALVSYNC_BUILD_CLIENT) || defined(FSSYNC_BUILD_CLIENT)
5645
5646 /* A couple of little helper functions. These return true if we tried to
5647  * use this mechanism to schedule a salvage, false if we haven't tried.
5648  * If we did try a salvage then the results are contained in code.
5649  */
5650
5651 static_inline int
5652 try_SALVSYNC(Volume *vp, char *partName, int *code) {
5653 #ifdef SALVSYNC_BUILD_CLIENT
5654     if (VCanUseSALVSYNC()) {
5655         Log("Scheduling salvage for volume %lu on part %s over SALVSYNC\n",
5656             afs_printable_uint32_lu(vp->hashid), partName);
5657
5658         /* can't use V_id() since there's no guarantee
5659          * we have the disk data header at this point */
5660         *code = SALVSYNC_SalvageVolume(vp->hashid,
5661                                        partName,
5662                                        SALVSYNC_SALVAGE,
5663                                        vp->salvage.reason,
5664                                        vp->salvage.prio,
5665                                        NULL);
5666         return 1;
5667     }
5668 #endif
5669     return 0;
5670 }
5671
5672 static_inline int
5673 try_FSSYNC(Volume *vp, char *partName, int *code) {
5674 #ifdef FSSYNC_BUILD_CLIENT
5675     if (VCanUseFSSYNC()) {
5676         Log("Scheduling salvage for volume %lu on part %s over FSSYNC\n",
5677             afs_printable_uint32_lu(vp->hashid), partName);
5678
5679         /*
5680          * If we aren't the fileserver, tell the fileserver the volume
5681          * needs to be salvaged. We could directly tell the
5682          * salvageserver, but the fileserver keeps track of some stats
5683          * related to salvages, and handles some other salvage-related
5684          * complications for us.
5685          */
5686         *code = FSYNC_VolOp(vp->hashid, partName,
5687                             FSYNC_VOL_FORCE_ERROR, FSYNC_SALVAGE, NULL);
5688         return 1;
5689     }
5690 #endif /* FSSYNC_BUILD_CLIENT */
5691     return 0;
5692 }
5693
5694 /**
5695  * schedule a salvage with the salvage server or fileserver.
5696  *
5697  * @param[in] vp  pointer to volume object
5698  *
5699  * @return operation status
5700  *    @retval 0 salvage scheduled successfully
5701  *    @retval 1 salvage not scheduled, or SALVSYNC/FSSYNC com error
5702  *
5703  * @pre
5704  *    @arg VOL_LOCK is held.
5705  *    @arg nUsers and nWaiters should be zero.
5706  *
5707  * @post salvageserver or fileserver is sent a salvage request
5708  *
5709  * @note If we are the fileserver, the request will be sent to the salvage
5710  * server over SALVSYNC. If we are not the fileserver, the request will be
5711  * sent to the fileserver over FSSYNC (FSYNC_VOL_FORCE_ERROR/FSYNC_SALVAGE).
5712  *
5713  * @note the caller must check if the volume needs to be freed after calling
5714  *       this; the volume may not have any references or be on any lists after
5715  *       we return, and we do not free it
5716  *
5717  * @note DAFS only
5718  *
5719  * @internal volume package internal use only.
5720  */
5721 static int
5722 VScheduleSalvage_r(Volume * vp)
5723 {
5724     int ret=0;
5725     int code = 0;
5726     VolState state_save;
5727     VThreadOptions_t * thread_opts;
5728     char partName[16];
5729
5730     osi_Assert(VCanUseSALVSYNC() || VCanUseFSSYNC());
5731
5732     if (vp->nWaiters || vp->nUsers) {
5733         return 1;
5734     }
5735
5736     /* prevent endless salvage,attach,salvage,attach,... loops */
5737     if (vp->stats.salvages >= SALVAGE_COUNT_MAX)
5738         return 1;
5739
5740     /*
5741      * don't perform salvsync ops on certain threads
5742      */
5743     thread_opts = pthread_getspecific(VThread_key);
5744     if (thread_opts == NULL) {
5745         thread_opts = &VThread_defaults;
5746     }
5747     if (thread_opts->disallow_salvsync || vol_disallow_salvsync) {
5748         return 1;
5749     }
5750
5751     if (vp->salvage.scheduled) {
5752         return ret;
5753     }
5754
5755     VCreateReservation_r(vp);
5756     VWaitExclusiveState_r(vp);
5757
5758     /*
5759      * XXX the scheduling process should really be done asynchronously
5760      *     to avoid fssync deadlocks
5761      */
5762     if (!vp->salvage.scheduled) {
5763         /* if we haven't previously scheduled a salvage, do so now
5764          *
5765          * set the volume to an exclusive state and drop the lock
5766          * around the SALVSYNC call
5767          */
5768         strlcpy(partName, VPartitionPath(vp->partition), sizeof(partName));
5769         state_save = VChangeState_r(vp, VOL_STATE_SALVSYNC_REQ);
5770         VOL_UNLOCK;
5771
5772         osi_Assert(try_SALVSYNC(vp, partName, &code) ||
5773                try_FSSYNC(vp, partName, &code));
5774
5775         VOL_LOCK;
5776         VChangeState_r(vp, state_save);
5777
5778         if (code == SYNC_OK) {
5779             vp->salvage.scheduled = 1;
5780             vp->stats.last_salvage_req = FT_ApproxTime();
5781             if (VCanUseSALVSYNC()) {
5782                 /* don't record these stats for non-fileservers; let the
5783                  * fileserver take care of these */
5784                 vp->stats.salvages++;
5785                 IncUInt64(&VStats.salvages);
5786             }
5787         } else {
5788             ret = 1;
5789             switch(code) {
5790             case SYNC_BAD_COMMAND:
5791             case SYNC_COM_ERROR:
5792                 break;
5793             case SYNC_DENIED:
5794                 Log("VScheduleSalvage_r: Salvage request for volume %lu "
5795                     "denied\n", afs_printable_uint32_lu(vp->hashid));
5796                 break;
5797             default:
5798                 Log("VScheduleSalvage_r: Salvage request for volume %lu "
5799                     "received unknown protocol error %d\n",
5800                     afs_printable_uint32_lu(vp->hashid), code);
5801                 break;
5802             }
5803
5804             if (VCanUseFSSYNC()) {
5805                 VChangeState_r(vp, VOL_STATE_ERROR);
5806             }
5807         }
5808     }
5809
5810     /* NB: this is cancelling the reservation we obtained above, but we do
5811      * not call VCancelReservation_r, since that may trigger the vp dtor,
5812      * possibly free'ing the vp. We need to keep the vp around after
5813      * this, as the caller may reference vp without any refs. Instead, it
5814      * is the duty of the caller to inspect 'vp' after we return to see if
5815      * needs to be freed. */
5816     osi_Assert(--vp->nWaiters >= 0);
5817     return ret;
5818 }
5819 #endif /* SALVSYNC_BUILD_CLIENT || FSSYNC_BUILD_CLIENT */
5820
5821 #ifdef SALVSYNC_BUILD_CLIENT
5822
5823 /**
5824  * connect to the salvageserver SYNC service.
5825  *
5826  * @return operation status
5827  *    @retval 0 failure
5828  *    @retval 1 success
5829  *
5830  * @post connection to salvageserver SYNC service established
5831  *
5832  * @see VConnectSALV_r
5833  * @see VDisconnectSALV
5834  * @see VReconnectSALV
5835  */
5836 int
5837 VConnectSALV(void)
5838 {
5839     int retVal;
5840     VOL_LOCK;
5841     retVal = VConnectSALV_r();
5842     VOL_UNLOCK;
5843     return retVal;
5844 }
5845
5846 /**
5847  * connect to the salvageserver SYNC service.
5848  *
5849  * @return operation status
5850  *    @retval 0 failure
5851  *    @retval 1 success
5852  *
5853  * @pre VOL_LOCK is held.
5854  *
5855  * @post connection to salvageserver SYNC service established
5856  *
5857  * @see VConnectSALV
5858  * @see VDisconnectSALV_r
5859  * @see VReconnectSALV_r
5860  * @see SALVSYNC_clientInit
5861  *
5862  * @internal volume package internal use only.
5863  */
5864 int
5865 VConnectSALV_r(void)
5866 {
5867     return SALVSYNC_clientInit();
5868 }
5869
5870 /**
5871  * disconnect from the salvageserver SYNC service.
5872  *
5873  * @return operation status
5874  *    @retval 0 success
5875  *
5876  * @pre client should have a live connection to the salvageserver
5877  *
5878  * @post connection to salvageserver SYNC service destroyed
5879  *
5880  * @see VDisconnectSALV_r
5881  * @see VConnectSALV
5882  * @see VReconnectSALV
5883  */
5884 int
5885 VDisconnectSALV(void)
5886 {
5887     VOL_LOCK;
5888     VDisconnectSALV_r();
5889     VOL_UNLOCK;
5890     return 0;
5891 }
5892
5893 /**
5894  * disconnect from the salvageserver SYNC service.
5895  *
5896  * @return operation status
5897  *    @retval 0 success
5898  *
5899  * @pre
5900  *    @arg VOL_LOCK is held.
5901  *    @arg client should have a live connection to the salvageserver.
5902  *
5903  * @post connection to salvageserver SYNC service destroyed
5904  *
5905  * @see VDisconnectSALV
5906  * @see VConnectSALV_r
5907  * @see VReconnectSALV_r
5908  * @see SALVSYNC_clientFinis
5909  *
5910  * @internal volume package internal use only.
5911  */
5912 int
5913 VDisconnectSALV_r(void)
5914 {
5915     return SALVSYNC_clientFinis();
5916 }
5917
5918 /**
5919  * disconnect and then re-connect to the salvageserver SYNC service.
5920  *
5921  * @return operation status
5922  *    @retval 0 failure
5923  *    @retval 1 success
5924  *
5925  * @pre client should have a live connection to the salvageserver
5926  *
5927  * @post old connection is dropped, and a new one is established
5928  *
5929  * @see VConnectSALV
5930  * @see VDisconnectSALV
5931  * @see VReconnectSALV_r
5932  */
5933 int
5934 VReconnectSALV(void)
5935 {
5936     int retVal;
5937     VOL_LOCK;
5938     retVal = VReconnectSALV_r();
5939     VOL_UNLOCK;
5940     return retVal;
5941 }
5942
5943 /**
5944  * disconnect and then re-connect to the salvageserver SYNC service.
5945  *
5946  * @return operation status
5947  *    @retval 0 failure
5948  *    @retval 1 success
5949  *
5950  * @pre
5951  *    @arg VOL_LOCK is held.
5952  *    @arg client should have a live connection to the salvageserver.
5953  *
5954  * @post old connection is dropped, and a new one is established
5955  *
5956  * @see VConnectSALV_r
5957  * @see VDisconnectSALV
5958  * @see VReconnectSALV
5959  * @see SALVSYNC_clientReconnect
5960  *
5961  * @internal volume package internal use only.
5962  */
5963 int
5964 VReconnectSALV_r(void)
5965 {
5966     return SALVSYNC_clientReconnect();
5967 }
5968 #endif /* SALVSYNC_BUILD_CLIENT */
5969 #endif /* AFS_DEMAND_ATTACH_FS */
5970
5971
5972 /***************************************************/
5973 /* FSSYNC routines                                 */
5974 /***************************************************/
5975
5976 /* This must be called by any volume utility which needs to run while the
5977    file server is also running.  This is separated from VInitVolumePackage2 so
5978    that a utility can fork--and each of the children can independently
5979    initialize communication with the file server */
5980 #ifdef FSSYNC_BUILD_CLIENT
5981 /**
5982  * connect to the fileserver SYNC service.
5983  *
5984  * @return operation status
5985  *    @retval 0 failure
5986  *    @retval 1 success
5987  *
5988  * @pre
5989  *    @arg VInit must equal 2.
5990  *    @arg Program Type must not be fileserver or salvager.
5991  *
5992  * @post connection to fileserver SYNC service established
5993  *
5994  * @see VConnectFS_r
5995  * @see VDisconnectFS
5996  * @see VChildProcReconnectFS
5997  */
5998 int
5999 VConnectFS(void)
6000 {
6001     int retVal;
6002     VOL_LOCK;
6003     retVal = VConnectFS_r();
6004     VOL_UNLOCK;
6005     return retVal;
6006 }
6007
6008 /**
6009  * connect to the fileserver SYNC service.
6010  *
6011  * @return operation status
6012  *    @retval 0 failure
6013  *    @retval 1 success
6014  *
6015  * @pre
6016  *    @arg VInit must equal 2.
6017  *    @arg Program Type must not be fileserver or salvager.
6018  *    @arg VOL_LOCK is held.
6019  *
6020  * @post connection to fileserver SYNC service established
6021  *
6022  * @see VConnectFS
6023  * @see VDisconnectFS_r
6024  * @see VChildProcReconnectFS_r
6025  *
6026  * @internal volume package internal use only.
6027  */
6028 int
6029 VConnectFS_r(void)
6030 {
6031     int rc;
6032     osi_Assert((VInit == 2) &&
6033            (programType != fileServer) &&
6034            (programType != salvager));
6035     rc = FSYNC_clientInit();
6036     if (rc) {
6037         VSetVInit_r(3);
6038     }
6039     return rc;
6040 }
6041
6042 /**
6043  * disconnect from the fileserver SYNC service.
6044  *
6045  * @pre
6046  *    @arg client should have a live connection to the fileserver.
6047  *    @arg VOL_LOCK is held.
6048  *    @arg Program Type must not be fileserver or salvager.
6049  *
6050  * @post connection to fileserver SYNC service destroyed
6051  *
6052  * @see VDisconnectFS
6053  * @see VConnectFS_r
6054  * @see VChildProcReconnectFS_r
6055  *
6056  * @internal volume package internal use only.
6057  */
6058 void
6059 VDisconnectFS_r(void)
6060 {
6061     osi_Assert((programType != fileServer) &&
6062            (programType != salvager));
6063     FSYNC_clientFinis();
6064     VSetVInit_r(2);
6065 }
6066
6067 /**
6068  * disconnect from the fileserver SYNC service.
6069  *
6070  * @pre
6071  *    @arg client should have a live connection to the fileserver.
6072  *    @arg Program Type must not be fileserver or salvager.
6073  *
6074  * @post connection to fileserver SYNC service destroyed
6075  *
6076  * @see VDisconnectFS_r
6077  * @see VConnectFS
6078  * @see VChildProcReconnectFS
6079  */
6080 void
6081 VDisconnectFS(void)
6082 {
6083     VOL_LOCK;
6084     VDisconnectFS_r();
6085     VOL_UNLOCK;
6086 }
6087
6088 /**
6089  * connect to the fileserver SYNC service from a child process following a fork.
6090  *
6091  * @return operation status
6092  *    @retval 0 failure
6093  *    @retval 1 success
6094  *
6095  * @pre
6096  *    @arg VOL_LOCK is held.
6097  *    @arg current FSYNC handle is shared with a parent process
6098  *
6099  * @post current FSYNC handle is discarded and a new connection to the
6100  *       fileserver SYNC service is established
6101  *
6102  * @see VChildProcReconnectFS
6103  * @see VConnectFS_r
6104  * @see VDisconnectFS_r
6105  *
6106  * @internal volume package internal use only.
6107  */
6108 int
6109 VChildProcReconnectFS_r(void)
6110 {
6111     return FSYNC_clientChildProcReconnect();
6112 }
6113
6114 /**
6115  * connect to the fileserver SYNC service from a child process following a fork.
6116  *
6117  * @return operation status
6118  *    @retval 0 failure
6119  *    @retval 1 success
6120  *
6121  * @pre current FSYNC handle is shared with a parent process
6122  *
6123  * @post current FSYNC handle is discarded and a new connection to the
6124  *       fileserver SYNC service is established
6125  *
6126  * @see VChildProcReconnectFS_r
6127  * @see VConnectFS
6128  * @see VDisconnectFS
6129  */
6130 int
6131 VChildProcReconnectFS(void)
6132 {
6133     int ret;
6134     VOL_LOCK;
6135     ret = VChildProcReconnectFS_r();
6136     VOL_UNLOCK;
6137     return ret;
6138 }
6139 #endif /* FSSYNC_BUILD_CLIENT */
6140
6141
6142 /***************************************************/
6143 /* volume bitmap routines                          */
6144 /***************************************************/
6145
6146 /**
6147  * allocate a vnode bitmap number for the vnode
6148  *
6149  * @param[out] ec  error code
6150  * @param[in] vp   volume object pointer
6151  * @param[in] index vnode index number for the vnode
6152  * @param[in] flags flag values described in note
6153  *
6154  * @note for DAFS, flags parameter controls locking behavior.
6155  * If (flags & VOL_ALLOC_BITMAP_WAIT) is set, then this function
6156  * will create a reservation and block on any other exclusive
6157  * operations.  Otherwise, this function assumes the caller
6158  * already has exclusive access to vp, and we just change the
6159  * volume state.
6160  *
6161  * @pre VOL_LOCK held
6162  *
6163  * @return bit number allocated
6164  */
6165 /*
6166
6167  */
6168 int
6169 VAllocBitmapEntry_r(Error * ec, Volume * vp,
6170                     struct vnodeIndex *index, int flags)
6171 {
6172     int ret = 0;
6173     byte *bp, *ep;
6174 #ifdef AFS_DEMAND_ATTACH_FS
6175     VolState state_save;
6176 #endif /* AFS_DEMAND_ATTACH_FS */
6177
6178     *ec = 0;
6179
6180     /* This test is probably redundant */
6181     if (!VolumeWriteable(vp)) {
6182         *ec = (bit32) VREADONLY;
6183         return ret;
6184     }
6185
6186 #ifdef AFS_DEMAND_ATTACH_FS
6187     if (flags & VOL_ALLOC_BITMAP_WAIT) {
6188         VCreateReservation_r(vp);
6189         VWaitExclusiveState_r(vp);
6190     }
6191     state_save = VChangeState_r(vp, VOL_STATE_GET_BITMAP);
6192 #endif /* AFS_DEMAND_ATTACH_FS */
6193
6194 #ifdef BITMAP_LATER
6195     if ((programType == fileServer) && !index->bitmap) {
6196         int i;
6197 #ifndef AFS_DEMAND_ATTACH_FS
6198         /* demand attach fs uses the volume state to avoid races.
6199          * specialStatus field is not used at all */
6200         int wasVBUSY = 0;
6201         if (vp->specialStatus == VBUSY) {
6202             if (vp->goingOffline) {     /* vos dump waiting for the volume to
6203                                          * go offline. We probably come here
6204                                          * from AddNewReadableResidency */
6205                 wasVBUSY = 1;
6206             } else {
6207                 while (vp->specialStatus == VBUSY) {
6208 #ifdef AFS_PTHREAD_ENV
6209                     VOL_UNLOCK;
6210                     sleep(2);
6211                     VOL_LOCK;
6212 #else /* !AFS_PTHREAD_ENV */
6213                     IOMGR_Sleep(2);
6214 #endif /* !AFS_PTHREAD_ENV */
6215                 }
6216             }
6217         }
6218 #endif /* !AFS_DEMAND_ATTACH_FS */
6219
6220         if (!index->bitmap) {
6221 #ifndef AFS_DEMAND_ATTACH_FS
6222             vp->specialStatus = VBUSY;  /* Stop anyone else from using it. */
6223 #endif /* AFS_DEMAND_ATTACH_FS */
6224             for (i = 0; i < nVNODECLASSES; i++) {
6225                 VGetBitmap_r(ec, vp, i);
6226                 if (*ec) {
6227 #ifdef AFS_DEMAND_ATTACH_FS
6228                     VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
6229 #else /* AFS_DEMAND_ATTACH_FS */
6230                     DeleteVolumeFromHashTable(vp);
6231                     vp->shuttingDown = 1;       /* Let who has it free it. */
6232                     vp->specialStatus = 0;
6233 #endif /* AFS_DEMAND_ATTACH_FS */
6234                     goto done;
6235                 }
6236             }
6237 #ifndef AFS_DEMAND_ATTACH_FS
6238             if (!wasVBUSY)
6239                 vp->specialStatus = 0;  /* Allow others to have access. */
6240 #endif /* AFS_DEMAND_ATTACH_FS */
6241         }
6242     }
6243 #endif /* BITMAP_LATER */
6244
6245 #ifdef AFS_DEMAND_ATTACH_FS
6246     VOL_UNLOCK;
6247 #endif /* AFS_DEMAND_ATTACH_FS */
6248     bp = index->bitmap + index->bitmapOffset;
6249     ep = index->bitmap + index->bitmapSize;
6250     while (bp < ep) {
6251         if ((*(bit32 *) bp) != (bit32) 0xffffffff) {
6252             int o;
6253             index->bitmapOffset = (afs_uint32) (bp - index->bitmap);
6254             while (*bp == 0xff)
6255                 bp++;
6256             o = ffs(~*bp) - 1;  /* ffs is documented in BSTRING(3) */
6257             *bp |= (1 << o);
6258             ret = ((bp - index->bitmap) * 8 + o);
6259 #ifdef AFS_DEMAND_ATTACH_FS
6260             VOL_LOCK;
6261 #endif /* AFS_DEMAND_ATTACH_FS */
6262             goto done;
6263         }
6264         bp += sizeof(bit32) /* i.e. 4 */ ;
6265     }
6266     /* No bit map entry--must grow bitmap */
6267     bp = (byte *)
6268         realloc(index->bitmap, index->bitmapSize + VOLUME_BITMAP_GROWSIZE);
6269     osi_Assert(bp != NULL);
6270     index->bitmap = bp;
6271     bp += index->bitmapSize;
6272     memset(bp, 0, VOLUME_BITMAP_GROWSIZE);
6273     index->bitmapOffset = index->bitmapSize;
6274     index->bitmapSize += VOLUME_BITMAP_GROWSIZE;
6275     *bp = 1;
6276     ret = index->bitmapOffset * 8;
6277 #ifdef AFS_DEMAND_ATTACH_FS
6278     VOL_LOCK;
6279 #endif /* AFS_DEMAND_ATTACH_FS */
6280
6281  done:
6282 #ifdef AFS_DEMAND_ATTACH_FS
6283     VChangeState_r(vp, state_save);
6284     if (flags & VOL_ALLOC_BITMAP_WAIT) {
6285         VCancelReservation_r(vp);
6286     }
6287 #endif /* AFS_DEMAND_ATTACH_FS */
6288     return ret;
6289 }
6290
6291 int
6292 VAllocBitmapEntry(Error * ec, Volume * vp, struct vnodeIndex * index)
6293 {
6294     int retVal;
6295     VOL_LOCK;
6296     retVal = VAllocBitmapEntry_r(ec, vp, index, VOL_ALLOC_BITMAP_WAIT);
6297     VOL_UNLOCK;
6298     return retVal;
6299 }
6300
6301 void
6302 VFreeBitMapEntry_r(Error * ec, Volume *vp, struct vnodeIndex *index,
6303                    unsigned bitNumber, int flags)
6304 {
6305     unsigned int offset;
6306
6307     *ec = 0;
6308
6309 #ifdef AFS_DEMAND_ATTACH_FS
6310     if (flags & VOL_FREE_BITMAP_WAIT) {
6311         /* VAllocBitmapEntry_r allocs bitmap entries under an exclusive volume
6312          * state, so ensure we're not in an exclusive volume state when we update
6313          * the bitmap */
6314         VCreateReservation_r(vp);
6315         VWaitExclusiveState_r(vp);
6316     }
6317 #endif
6318
6319 #ifdef BITMAP_LATER
6320     if (!index->bitmap)
6321         goto done;
6322 #endif /* BITMAP_LATER */
6323
6324     offset = bitNumber >> 3;
6325     if (offset >= index->bitmapSize) {
6326         *ec = VNOVNODE;
6327         goto done;
6328     }
6329     if (offset < index->bitmapOffset)
6330         index->bitmapOffset = offset & ~3;      /* Truncate to nearest bit32 */
6331     *(index->bitmap + offset) &= ~(1 << (bitNumber & 0x7));
6332
6333  done:
6334 #ifdef AFS_DEMAND_ATTACH_FS
6335     VCancelReservation_r(vp);
6336 #endif
6337     return; /* make the compiler happy for non-DAFS */
6338 }
6339
6340 void
6341 VFreeBitMapEntry(Error * ec, Volume *vp, struct vnodeIndex *index,
6342                  unsigned bitNumber)
6343 {
6344     VOL_LOCK;
6345     VFreeBitMapEntry_r(ec, vp, index, bitNumber, VOL_FREE_BITMAP_WAIT);
6346     VOL_UNLOCK;
6347 }
6348
6349 /* this function will drop the glock internally.
6350  * for old pthread fileservers, this is safe thanks to vbusy.
6351  *
6352  * for demand attach fs, caller must have already called
6353  * VCreateReservation_r and VWaitExclusiveState_r */
6354 static void
6355 VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class)
6356 {
6357     StreamHandle_t *file;
6358     afs_sfsize_t nVnodes, size;
6359     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
6360     struct vnodeIndex *vip = &vp->vnodeIndex[class];
6361     struct VnodeDiskObject *vnode;
6362     unsigned int unique = 0;
6363     FdHandle_t *fdP;
6364 #ifdef BITMAP_LATER
6365     byte *BitMap = 0;
6366 #endif /* BITMAP_LATER */
6367 #ifdef AFS_DEMAND_ATTACH_FS
6368     VolState state_save;
6369 #endif /* AFS_DEMAND_ATTACH_FS */
6370
6371     *ec = 0;
6372
6373 #ifdef AFS_DEMAND_ATTACH_FS
6374     state_save = VChangeState_r(vp, VOL_STATE_GET_BITMAP);
6375 #endif /* AFS_DEMAND_ATTACH_FS */
6376     VOL_UNLOCK;
6377
6378     fdP = IH_OPEN(vip->handle);
6379     osi_Assert(fdP != NULL);
6380     file = FDH_FDOPEN(fdP, "r");
6381     osi_Assert(file != NULL);
6382     vnode = (VnodeDiskObject *) malloc(vcp->diskSize);
6383     osi_Assert(vnode != NULL);
6384     size = OS_SIZE(fdP->fd_fd);
6385     osi_Assert(size != -1);
6386     nVnodes = (size <= vcp->diskSize ? 0 : size - vcp->diskSize)
6387         >> vcp->logSize;
6388     vip->bitmapSize = ((nVnodes / 8) + 10) / 4 * 4;     /* The 10 is a little extra so
6389                                                          * a few files can be created in this volume,
6390                                                          * the whole thing is rounded up to nearest 4
6391                                                          * bytes, because the bit map allocator likes
6392                                                          * it that way */
6393 #ifdef BITMAP_LATER
6394     BitMap = (byte *) calloc(1, vip->bitmapSize);
6395     osi_Assert(BitMap != NULL);
6396 #else /* BITMAP_LATER */
6397     vip->bitmap = (byte *) calloc(1, vip->bitmapSize);
6398     osi_Assert(vip->bitmap != NULL);
6399     vip->bitmapOffset = 0;
6400 #endif /* BITMAP_LATER */
6401     if (STREAM_ASEEK(file, vcp->diskSize) != -1) {
6402         int bitNumber = 0;
6403         for (bitNumber = 0; bitNumber < nVnodes + 100; bitNumber++) {
6404             if (STREAM_READ(vnode, vcp->diskSize, 1, file) != 1)
6405                 break;
6406             if (vnode->type != vNull) {
6407                 if (vnode->vnodeMagic != vcp->magic) {
6408                     Log("GetBitmap: addled vnode index in volume %s; volume needs salvage\n", V_name(vp));
6409                     *ec = VSALVAGE;
6410                     break;
6411                 }
6412 #ifdef BITMAP_LATER
6413                 *(BitMap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
6414 #else /* BITMAP_LATER */
6415                 *(vip->bitmap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
6416 #endif /* BITMAP_LATER */
6417                 if (unique <= vnode->uniquifier)
6418                     unique = vnode->uniquifier + 1;
6419             }
6420 #ifndef AFS_PTHREAD_ENV
6421             if ((bitNumber & 0x00ff) == 0x0ff) {        /* every 256 iterations */
6422                 IOMGR_Poll();
6423             }
6424 #endif /* !AFS_PTHREAD_ENV */
6425         }
6426     }
6427     if (vp->nextVnodeUnique < unique) {
6428         Log("GetBitmap: bad volume uniquifier for volume %s; volume needs salvage\n", V_name(vp));
6429         *ec = VSALVAGE;
6430     }
6431     /* Paranoia, partly justified--I think fclose after fdopen
6432      * doesn't seem to close fd.  In any event, the documentation
6433      * doesn't specify, so it's safer to close it twice.
6434      */
6435     STREAM_CLOSE(file);
6436     FDH_CLOSE(fdP);
6437     free(vnode);
6438
6439     VOL_LOCK;
6440 #ifdef BITMAP_LATER
6441     /* There may have been a racing condition with some other thread, both
6442      * creating the bitmaps for this volume. If the other thread was faster
6443      * the pointer to bitmap should already be filled and we can free ours.
6444      */
6445     if (vip->bitmap == NULL) {
6446         vip->bitmap = BitMap;
6447         vip->bitmapOffset = 0;
6448     } else
6449         free((byte *) BitMap);
6450 #endif /* BITMAP_LATER */
6451 #ifdef AFS_DEMAND_ATTACH_FS
6452     VChangeState_r(vp, state_save);
6453 #endif /* AFS_DEMAND_ATTACH_FS */
6454 }
6455
6456
6457 /***************************************************/
6458 /* Volume Path and Volume Number utility routines  */
6459 /***************************************************/
6460
6461 /**
6462  * find the first occurrence of a volume header file and return the path.
6463  *
6464  * @param[out] ec          outbound error code
6465  * @param[in]  volumeId    volume id to find
6466  * @param[out] partitionp  pointer to disk partition path string
6467  * @param[out] namep       pointer to volume header file name string
6468  *
6469  * @post path to first occurrence of volume header is returned in partitionp
6470  *       and namep, or ec is set accordingly.
6471  *
6472  * @warning this function is NOT re-entrant -- partitionp and namep point to
6473  *          static data segments
6474  *
6475  * @note if a volume utility inadvertently leaves behind a stale volume header
6476  *       on a vice partition, it is possible for callers to get the wrong one,
6477  *       depending on the order of the disk partition linked list.
6478  *
6479  */
6480 void
6481 VGetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
6482 {
6483     static char partition[VMAXPATHLEN], name[VMAXPATHLEN];
6484     char path[VMAXPATHLEN];
6485     int found = 0;
6486     struct DiskPartition64 *dp;
6487
6488     *ec = 0;
6489     name[0] = OS_DIRSEPC;
6490     snprintf(&name[1], (sizeof name) - 1, VFORMAT,
6491              afs_printable_uint32_lu(volumeId));
6492     for (dp = DiskPartitionList; dp; dp = dp->next) {
6493         struct afs_stat_st status;
6494         strcpy(path, VPartitionPath(dp));
6495         strcat(path, name);
6496         if (afs_stat(path, &status) == 0) {
6497             strcpy(partition, dp->name);
6498             found = 1;
6499             break;
6500         }
6501     }
6502     if (!found) {
6503         *ec = VNOVOL;
6504         *partitionp = *namep = NULL;
6505     } else {
6506         *partitionp = partition;
6507         *namep = name;
6508     }
6509 }
6510
6511 /**
6512  * extract a volume number from a volume header filename string.
6513  *
6514  * @param[in] name  volume header filename string
6515  *
6516  * @return volume number
6517  *
6518  * @note the string must be of the form VFORMAT.  the only permissible
6519  *       deviation is a leading OS_DIRSEPC character.
6520  *
6521  * @see VFORMAT
6522  */
6523 int
6524 VolumeNumber(char *name)
6525 {
6526     if (*name == OS_DIRSEPC)
6527         name++;
6528     return strtoul(name + 1, NULL, 10);
6529 }
6530
6531 /**
6532  * compute the volume header filename.
6533  *
6534  * @param[in] volumeId
6535  *
6536  * @return volume header filename
6537  *
6538  * @post volume header filename string is constructed
6539  *
6540  * @warning this function is NOT re-entrant -- the returned string is
6541  *          stored in a static char array.  see VolumeExternalName_r
6542  *          for a re-entrant equivalent.
6543  *
6544  * @see VolumeExternalName_r
6545  *
6546  * @deprecated due to the above re-entrancy warning, this interface should
6547  *             be considered deprecated.  Please use VolumeExternalName_r
6548  *             in its stead.
6549  */
6550 char *
6551 VolumeExternalName(VolumeId volumeId)
6552 {
6553     static char name[VMAXPATHLEN];
6554     snprintf(name, sizeof name, VFORMAT, afs_printable_uint32_lu(volumeId));
6555     return name;
6556 }
6557
6558 /**
6559  * compute the volume header filename.
6560  *
6561  * @param[in]     volumeId
6562  * @param[inout]  name       array in which to store filename
6563  * @param[in]     len        length of name array
6564  *
6565  * @return result code from afs_snprintf
6566  *
6567  * @see VolumeExternalName
6568  * @see afs_snprintf
6569  *
6570  * @note re-entrant equivalent of VolumeExternalName
6571  */
6572 int
6573 VolumeExternalName_r(VolumeId volumeId, char * name, size_t len)
6574 {
6575     return snprintf(name, len, VFORMAT, afs_printable_uint32_lu(volumeId));
6576 }
6577
6578
6579 /***************************************************/
6580 /* Volume Usage Statistics routines                */
6581 /***************************************************/
6582
6583 #if OPENAFS_VOL_STATS
6584 #define OneDay  (86400)         /* 24 hours' worth of seconds */
6585 #else
6586 #define OneDay  (24*60*60)      /* 24 hours */
6587 #endif /* OPENAFS_VOL_STATS */
6588
6589 static time_t
6590 Midnight(time_t t) {
6591     struct tm local, *l;
6592     time_t midnight;
6593
6594 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
6595     l = localtime_r(&t, &local);
6596 #else
6597     l = localtime(&t);
6598 #endif
6599
6600     if (l != NULL) {
6601         /* the following is strictly speaking problematic on the
6602            switching day to daylight saving time, after the switch,
6603            as tm_isdst does not match.  Similarly, on the looong day when
6604            switching back the OneDay check will not do what naively expected!
6605            The effects are minor, though, and more a matter of interpreting
6606            the numbers. */
6607 #ifndef AFS_PTHREAD_ENV
6608         local = *l;
6609 #endif
6610         local.tm_hour = local.tm_min=local.tm_sec = 0;
6611         midnight = mktime(&local);
6612         if (midnight != (time_t) -1) return(midnight);
6613     }
6614     return( (t/OneDay)*OneDay );
6615
6616 }
6617
6618 /*------------------------------------------------------------------------
6619  * [export] VAdjustVolumeStatistics
6620  *
6621  * Description:
6622  *      If we've passed midnight, we need to update all the day use
6623  *      statistics as well as zeroing the detailed volume statistics
6624  *      (if we are implementing them).
6625  *
6626  * Arguments:
6627  *      vp : Pointer to the volume structure describing the lucky
6628  *              volume being considered for update.
6629  *
6630  * Returns:
6631  *      0 (always!)
6632  *
6633  * Environment:
6634  *      Nothing interesting.
6635  *
6636  * Side Effects:
6637  *      As described.
6638  *------------------------------------------------------------------------*/
6639
6640 int
6641 VAdjustVolumeStatistics_r(Volume * vp)
6642 {
6643     unsigned int now = FT_ApproxTime();
6644
6645     if (now - V_dayUseDate(vp) > OneDay) {
6646         int ndays, i;
6647
6648         ndays = (now - V_dayUseDate(vp)) / OneDay;
6649         for (i = 6; i > ndays - 1; i--)
6650             V_weekUse(vp)[i] = V_weekUse(vp)[i - ndays];
6651         for (i = 0; i < ndays - 1 && i < 7; i++)
6652             V_weekUse(vp)[i] = 0;
6653         if (ndays <= 7)
6654             V_weekUse(vp)[ndays - 1] = V_dayUse(vp);
6655         V_dayUse(vp) = 0;
6656         V_dayUseDate(vp) = Midnight(now);
6657
6658 #if OPENAFS_VOL_STATS
6659         /*
6660          * All we need to do is bzero the entire VOL_STATS_BYTES of
6661          * the detailed volume statistics area.
6662          */
6663         memset((V_stat_area(vp)), 0, VOL_STATS_BYTES);
6664 #endif /* OPENAFS_VOL_STATS */
6665     }
6666
6667     /*It's been more than a day of collection */
6668     /*
6669      * Always return happily.
6670      */
6671     return (0);
6672 }                               /*VAdjustVolumeStatistics */
6673
6674 int
6675 VAdjustVolumeStatistics(Volume * vp)
6676 {
6677     int retVal;
6678     VOL_LOCK;
6679     retVal = VAdjustVolumeStatistics_r(vp);
6680     VOL_UNLOCK;
6681     return retVal;
6682 }
6683
6684 void
6685 VBumpVolumeUsage_r(Volume * vp)
6686 {
6687     unsigned int now = FT_ApproxTime();
6688     V_accessDate(vp) = now;
6689     if (now - V_dayUseDate(vp) > OneDay)
6690         VAdjustVolumeStatistics_r(vp);
6691     /*
6692      * Save the volume header image to disk after every 128 bumps to dayUse.
6693      */
6694     if ((V_dayUse(vp)++ & 127) == 0) {
6695         Error error;
6696         VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT);
6697     }
6698 }
6699
6700 void
6701 VBumpVolumeUsage(Volume * vp)
6702 {
6703     VOL_LOCK;
6704     VBumpVolumeUsage_r(vp);
6705     VOL_UNLOCK;
6706 }
6707
6708 void
6709 VSetDiskUsage_r(void)
6710 {
6711 #ifndef AFS_DEMAND_ATTACH_FS
6712     static int FifteenMinuteCounter = 0;
6713 #endif
6714
6715     while (VInit < 2) {
6716         /* NOTE: Don't attempt to access the partitions list until the
6717          * initialization level indicates that all volumes are attached,
6718          * which implies that all partitions are initialized. */
6719 #ifdef AFS_PTHREAD_ENV
6720         VOL_CV_WAIT(&vol_vinit_cond);
6721 #else /* AFS_PTHREAD_ENV */
6722         IOMGR_Sleep(10);
6723 #endif /* AFS_PTHREAD_ENV */
6724     }
6725
6726     VResetDiskUsage_r();
6727
6728 #ifndef AFS_DEMAND_ATTACH_FS
6729     if (++FifteenMinuteCounter == 3) {
6730         FifteenMinuteCounter = 0;
6731         VScanUpdateList();
6732     }
6733 #endif /* !AFS_DEMAND_ATTACH_FS */
6734 }
6735
6736 void
6737 VSetDiskUsage(void)
6738 {
6739     VOL_LOCK;
6740     VSetDiskUsage_r();
6741     VOL_UNLOCK;
6742 }
6743
6744
6745 /***************************************************/
6746 /* Volume Update List routines                     */
6747 /***************************************************/
6748
6749 /* The number of minutes that a volume hasn't been updated before the
6750  * "Dont salvage" flag in the volume header will be turned on */
6751 #define SALVAGE_INTERVAL        (10*60)
6752
6753 /*
6754  * demand attach fs
6755  *
6756  * volume update list functionality has been moved into the VLRU
6757  * the DONT_SALVAGE flag is now set during VLRU demotion
6758  */
6759
6760 #ifndef AFS_DEMAND_ATTACH_FS
6761 static VolumeId *UpdateList = NULL;     /* Pointer to array of Volume ID's */
6762 static int nUpdatedVolumes = 0;         /* Updated with entry in UpdateList, salvage after crash flag on */
6763 static int updateSize = 0;              /* number of entries possible */
6764 #define UPDATE_LIST_SIZE 128            /* initial size increment (must be a power of 2!) */
6765 #endif /* !AFS_DEMAND_ATTACH_FS */
6766
6767 void
6768 VAddToVolumeUpdateList_r(Error * ec, Volume * vp)
6769 {
6770     *ec = 0;
6771     vp->updateTime = FT_ApproxTime();
6772     if (V_dontSalvage(vp) == 0)
6773         return;
6774     V_dontSalvage(vp) = 0;
6775     VSyncVolume_r(ec, vp, 0);
6776 #ifdef AFS_DEMAND_ATTACH_FS
6777     V_attachFlags(vp) &= ~(VOL_HDR_DONTSALV);
6778 #else /* !AFS_DEMAND_ATTACH_FS */
6779     if (*ec)
6780         return;
6781     if (UpdateList == NULL) {
6782         updateSize = UPDATE_LIST_SIZE;
6783         UpdateList = (VolumeId *) malloc(sizeof(VolumeId) * updateSize);
6784     } else {
6785         if (nUpdatedVolumes == updateSize) {
6786             updateSize <<= 1;
6787             if (updateSize > 524288) {
6788                 Log("warning: there is likely a bug in the volume update scanner\n");
6789                 return;
6790             }
6791             UpdateList =
6792                 (VolumeId *) realloc(UpdateList,
6793                                      sizeof(VolumeId) * updateSize);
6794         }
6795     }
6796     osi_Assert(UpdateList != NULL);
6797     UpdateList[nUpdatedVolumes++] = V_id(vp);
6798 #endif /* !AFS_DEMAND_ATTACH_FS */
6799 }
6800
6801 #ifndef AFS_DEMAND_ATTACH_FS
6802 static void
6803 VScanUpdateList(void)
6804 {
6805     int i, gap;
6806     Volume *vp;
6807     Error error;
6808     afs_uint32 now = FT_ApproxTime();
6809     /* Be careful with this code, since it works with interleaved calls to AddToVolumeUpdateList */
6810     for (i = gap = 0; i < nUpdatedVolumes; i++) {
6811         if (gap)
6812             UpdateList[i - gap] = UpdateList[i];
6813
6814         /* XXX this routine needlessly messes up the Volume LRU by
6815          * breaking the LRU temporal-locality assumptions.....
6816          * we should use a special volume header allocator here */
6817         vp = VGetVolume_r(&error, UpdateList[i - gap] = UpdateList[i]);
6818         if (error) {
6819             gap++;
6820         } else if (vp->nUsers == 1 && now - vp->updateTime > SALVAGE_INTERVAL) {
6821             V_dontSalvage(vp) = DONT_SALVAGE;
6822             VUpdateVolume_r(&error, vp, 0);     /* No need to fsync--not critical */
6823             gap++;
6824         }
6825
6826         if (vp) {
6827             VPutVolume_r(vp);
6828         }
6829
6830 #ifndef AFS_PTHREAD_ENV
6831         IOMGR_Poll();
6832 #endif /* !AFS_PTHREAD_ENV */
6833     }
6834     nUpdatedVolumes -= gap;
6835 }
6836 #endif /* !AFS_DEMAND_ATTACH_FS */
6837
6838
6839 /***************************************************/
6840 /* Volume LRU routines                             */
6841 /***************************************************/
6842
6843 /* demand attach fs
6844  * volume LRU
6845  *
6846  * with demand attach fs, we attempt to soft detach(1)
6847  * volumes which have not been accessed in a long time
6848  * in order to speed up fileserver shutdown
6849  *
6850  * (1) by soft detach we mean a process very similar
6851  *     to VOffline, except the final state of the
6852  *     Volume will be VOL_STATE_PREATTACHED, instead
6853  *     of the usual VOL_STATE_UNATTACHED
6854  */
6855 #ifdef AFS_DEMAND_ATTACH_FS
6856
6857 /* implementation is reminiscent of a generational GC
6858  *
6859  * queue 0 is newly attached volumes. this queue is
6860  * sorted by attach timestamp
6861  *
6862  * queue 1 is volumes that have been around a bit
6863  * longer than queue 0. this queue is sorted by
6864  * attach timestamp
6865  *
6866  * queue 2 is volumes tha have been around the longest.
6867  * this queue is unsorted
6868  *
6869  * queue 3 is volumes that have been marked as
6870  * candidates for soft detachment. this queue is
6871  * unsorted
6872  */
6873 #define VLRU_GENERATIONS  3   /**< number of generations in VLRU */
6874 #define VLRU_QUEUES       5   /**< total number of VLRU queues */
6875
6876 /**
6877  * definition of a VLRU queue.
6878  */
6879 struct VLRU_q {
6880     volatile struct rx_queue q;
6881     volatile int len;
6882     volatile int busy;
6883     pthread_cond_t cv;
6884 };
6885
6886 /**
6887  * main VLRU data structure.
6888  */
6889 struct VLRU {
6890     struct VLRU_q q[VLRU_QUEUES];   /**< VLRU queues */
6891
6892     /* VLRU config */
6893     /** time interval (in seconds) between promotion passes for
6894      *  each young generation queue. */
6895     afs_uint32 promotion_interval[VLRU_GENERATIONS-1];
6896
6897     /** time interval (in seconds) between soft detach candidate
6898      *  scans for each generation queue.
6899      *
6900      *  scan_interval[VLRU_QUEUE_CANDIDATE] defines how frequently
6901      *  we perform a soft detach pass. */
6902     afs_uint32 scan_interval[VLRU_GENERATIONS+1];
6903
6904     /* scheduler state */
6905     int next_idx;                                       /**< next queue to receive attention */
6906     afs_uint32 last_promotion[VLRU_GENERATIONS-1];      /**< timestamp of last promotion scan */
6907     afs_uint32 last_scan[VLRU_GENERATIONS+1];           /**< timestamp of last detach scan */
6908
6909     int scanner_state;                                  /**< state of scanner thread */
6910     pthread_cond_t cv;                                  /**< state transition CV */
6911 };
6912
6913 /** global VLRU state */
6914 static struct VLRU volume_LRU;
6915
6916 /**
6917  * defined states for VLRU scanner thread.
6918  */
6919 typedef enum {
6920     VLRU_SCANNER_STATE_OFFLINE        = 0,    /**< vlru scanner thread is offline */
6921     VLRU_SCANNER_STATE_ONLINE         = 1,    /**< vlru scanner thread is online */
6922     VLRU_SCANNER_STATE_SHUTTING_DOWN  = 2,    /**< vlru scanner thread is shutting down */
6923     VLRU_SCANNER_STATE_PAUSING        = 3,    /**< vlru scanner thread is getting ready to pause */
6924     VLRU_SCANNER_STATE_PAUSED         = 4     /**< vlru scanner thread is paused */
6925 } vlru_thread_state_t;
6926
6927 /* vlru disk data header stuff */
6928 #define VLRU_DISK_MAGIC      0x7a8b9cad        /**< vlru disk entry magic number */
6929 #define VLRU_DISK_VERSION    1                 /**< vlru disk entry version number */
6930
6931 /** vlru default expiration time (for eventual fs state serialization of vlru data) */
6932 #define VLRU_DUMP_EXPIRATION_TIME   (60*60*24*7)  /* expire vlru data after 1 week */
6933
6934
6935 /** minimum volume inactivity (in seconds) before a volume becomes eligible for
6936  *  soft detachment. */
6937 static afs_uint32 VLRU_offline_thresh = VLRU_DEFAULT_OFFLINE_THRESH;
6938
6939 /** time interval (in seconds) between VLRU scanner thread soft detach passes. */
6940 static afs_uint32 VLRU_offline_interval = VLRU_DEFAULT_OFFLINE_INTERVAL;
6941
6942 /** maximum number of volumes to soft detach in a VLRU soft detach pass. */
6943 static afs_uint32 VLRU_offline_max = VLRU_DEFAULT_OFFLINE_MAX;
6944
6945 /** VLRU control flag.  non-zero value implies VLRU subsystem is activated. */
6946 static afs_uint32 VLRU_enabled = 1;
6947
6948 /* queue synchronization routines */
6949 static void VLRU_BeginExclusive_r(struct VLRU_q * q);
6950 static void VLRU_EndExclusive_r(struct VLRU_q * q);
6951 static void VLRU_Wait_r(struct VLRU_q * q);
6952
6953 /**
6954  * set VLRU subsystem tunable parameters.
6955  *
6956  * @param[in] option  tunable option to modify
6957  * @param[in] val     new value for tunable parameter
6958  *
6959  * @pre @c VInitVolumePackage2 has not yet been called.
6960  *
6961  * @post tunable parameter is modified
6962  *
6963  * @note DAFS only
6964  *
6965  * @note valid option parameters are:
6966  *    @arg @c VLRU_SET_THRESH
6967  *         set the period of inactivity after which
6968  *         volumes are eligible for soft detachment
6969  *    @arg @c VLRU_SET_INTERVAL
6970  *         set the time interval between calls
6971  *         to the volume LRU "garbage collector"
6972  *    @arg @c VLRU_SET_MAX
6973  *         set the max number of volumes to deallocate
6974  *         in one GC pass
6975  */
6976 void
6977 VLRU_SetOptions(int option, afs_uint32 val)
6978 {
6979     if (option == VLRU_SET_THRESH) {
6980         VLRU_offline_thresh = val;
6981     } else if (option == VLRU_SET_INTERVAL) {
6982         VLRU_offline_interval = val;
6983     } else if (option == VLRU_SET_MAX) {
6984         VLRU_offline_max = val;
6985     } else if (option == VLRU_SET_ENABLED) {
6986         VLRU_enabled = val;
6987     }
6988     VLRU_ComputeConstants();
6989 }
6990
6991 /**
6992  * compute VLRU internal timing parameters.
6993  *
6994  * @post VLRU scanner thread internal timing parameters are computed
6995  *
6996  * @note computes internal timing parameters based upon user-modifiable
6997  *       tunable parameters.
6998  *
6999  * @note DAFS only
7000  *
7001  * @internal volume package internal use only.
7002  */
7003 static void
7004 VLRU_ComputeConstants(void)
7005 {
7006     afs_uint32 factor = VLRU_offline_thresh / VLRU_offline_interval;
7007
7008     /* compute the candidate scan interval */
7009     volume_LRU.scan_interval[VLRU_QUEUE_CANDIDATE] = VLRU_offline_interval;
7010
7011     /* compute the promotion intervals */
7012     volume_LRU.promotion_interval[VLRU_QUEUE_NEW] = VLRU_offline_thresh * 2;
7013     volume_LRU.promotion_interval[VLRU_QUEUE_MID] = VLRU_offline_thresh * 4;
7014
7015     if (factor > 16) {
7016         /* compute the gen 0 scan interval */
7017         volume_LRU.scan_interval[VLRU_QUEUE_NEW] = VLRU_offline_thresh / 8;
7018     } else {
7019         /* compute the gen 0 scan interval */
7020         volume_LRU.scan_interval[VLRU_QUEUE_NEW] = VLRU_offline_interval * 2;
7021     }
7022 }
7023
7024 /**
7025  * initialize VLRU subsystem.
7026  *
7027  * @pre this function has not yet been called
7028  *
7029  * @post VLRU subsystem is initialized and VLRU scanner thread is starting
7030  *
7031  * @note DAFS only
7032  *
7033  * @internal volume package internal use only.
7034  */
7035 static void
7036 VInitVLRU(void)
7037 {
7038     pthread_t tid;
7039     pthread_attr_t attrs;
7040     int i;
7041
7042     if (!VLRU_enabled) {
7043         Log("VLRU: disabled\n");
7044         return;
7045     }
7046
7047     /* initialize each of the VLRU queues */
7048     for (i = 0; i < VLRU_QUEUES; i++) {
7049         queue_Init(&volume_LRU.q[i]);
7050         volume_LRU.q[i].len = 0;
7051         volume_LRU.q[i].busy = 0;
7052         CV_INIT(&volume_LRU.q[i].cv, "vol lru", CV_DEFAULT, 0);
7053     }
7054
7055     /* setup the timing constants */
7056     VLRU_ComputeConstants();
7057
7058     /* XXX put inside LogLevel check? */
7059     Log("VLRU: starting scanner with the following configuration parameters:\n");
7060     Log("VLRU:  offlining volumes after minimum of %d seconds of inactivity\n", VLRU_offline_thresh);
7061     Log("VLRU:  running VLRU soft detach pass every %d seconds\n", VLRU_offline_interval);
7062     Log("VLRU:  taking up to %d volumes offline per pass\n", VLRU_offline_max);
7063     Log("VLRU:  scanning generation 0 for inactive volumes every %d seconds\n", volume_LRU.scan_interval[0]);
7064     Log("VLRU:  scanning for promotion/demotion between generations 0 and 1 every %d seconds\n", volume_LRU.promotion_interval[0]);
7065     Log("VLRU:  scanning for promotion/demotion between generations 1 and 2 every %d seconds\n", volume_LRU.promotion_interval[1]);
7066
7067     /* start up the VLRU scanner */
7068     volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
7069     if (programType == fileServer) {
7070         CV_INIT(&volume_LRU.cv, "vol lru", CV_DEFAULT, 0);
7071         osi_Assert(pthread_attr_init(&attrs) == 0);
7072         osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
7073         osi_Assert(pthread_create(&tid, &attrs, &VLRU_ScannerThread, NULL) == 0);
7074     }
7075 }
7076
7077 /**
7078  * initialize the VLRU-related fields of a newly allocated volume object.
7079  *
7080  * @param[in] vp  pointer to volume object
7081  *
7082  * @pre
7083  *    @arg @c VOL_LOCK is held.
7084  *    @arg volume object is not on a VLRU queue.
7085  *
7086  * @post VLRU fields are initialized to indicate that volume object is not
7087  *       currently registered with the VLRU subsystem
7088  *
7089  * @note DAFS only
7090  *
7091  * @internal volume package interal use only.
7092  */
7093 static void
7094 VLRU_Init_Node_r(Volume * vp)
7095 {
7096     if (!VLRU_enabled)
7097         return;
7098
7099     osi_Assert(queue_IsNotOnQueue(&vp->vlru));
7100     vp->vlru.idx = VLRU_QUEUE_INVALID;
7101 }
7102
7103 /**
7104  * add a volume object to a VLRU queue.
7105  *
7106  * @param[in] vp  pointer to volume object
7107  *
7108  * @pre
7109  *    @arg @c VOL_LOCK is held.
7110  *    @arg caller MUST hold a lightweight ref on @p vp.
7111  *    @arg caller MUST NOT hold exclusive ownership of the VLRU queue.
7112  *
7113  * @post the volume object is added to the appropriate VLRU queue
7114  *
7115  * @note if @c vp->vlru.idx contains the index of a valid VLRU queue,
7116  *       then the volume is added to that queue.  Otherwise, the value
7117  *       @c VLRU_QUEUE_NEW is stored into @c vp->vlru.idx and the
7118  *       volume is added to the NEW generation queue.
7119  *
7120  * @note @c VOL_LOCK may be dropped internally
7121  *
7122  * @note Volume state is temporarily set to @c VOL_STATE_VLRU_ADD
7123  *       during the add operation, and is restored to the previous
7124  *       state prior to return.
7125  *
7126  * @note DAFS only
7127  *
7128  * @internal volume package internal use only.
7129  */
7130 static void
7131 VLRU_Add_r(Volume * vp)
7132 {
7133     int idx;
7134     VolState state_save;
7135
7136     if (!VLRU_enabled)
7137         return;
7138
7139     if (queue_IsOnQueue(&vp->vlru))
7140         return;
7141
7142     state_save = VChangeState_r(vp, VOL_STATE_VLRU_ADD);
7143
7144     idx = vp->vlru.idx;
7145     if ((idx < 0) || (idx >= VLRU_QUEUE_INVALID)) {
7146         idx = VLRU_QUEUE_NEW;
7147     }
7148
7149     VLRU_Wait_r(&volume_LRU.q[idx]);
7150
7151     /* repeat check since VLRU_Wait_r may have dropped
7152      * the glock */
7153     if (queue_IsNotOnQueue(&vp->vlru)) {
7154         vp->vlru.idx = idx;
7155         queue_Prepend(&volume_LRU.q[idx], &vp->vlru);
7156         volume_LRU.q[idx].len++;
7157         V_attachFlags(vp) |= VOL_ON_VLRU;
7158         vp->stats.last_promote = FT_ApproxTime();
7159     }
7160
7161     VChangeState_r(vp, state_save);
7162 }
7163
7164 /**
7165  * delete a volume object from a VLRU queue.
7166  *
7167  * @param[in] vp  pointer to volume object
7168  *
7169  * @pre
7170  *    @arg @c VOL_LOCK is held.
7171  *    @arg caller MUST hold a lightweight ref on @p vp.
7172  *    @arg caller MUST NOT hold exclusive ownership of the VLRU queue.
7173  *
7174  * @post volume object is removed from the VLRU queue
7175  *
7176  * @note @c VOL_LOCK may be dropped internally
7177  *
7178  * @note DAFS only
7179  *
7180  * @todo We should probably set volume state to something exlcusive
7181  *       (as @c VLRU_Add_r does) prior to dropping @c VOL_LOCK.
7182  *
7183  * @internal volume package internal use only.
7184  */
7185 static void
7186 VLRU_Delete_r(Volume * vp)
7187 {
7188     int idx;
7189
7190     if (!VLRU_enabled)
7191         return;
7192
7193     if (queue_IsNotOnQueue(&vp->vlru))
7194         return;
7195
7196     /* handle races */
7197     do {
7198       idx = vp->vlru.idx;
7199       if (idx == VLRU_QUEUE_INVALID)
7200           return;
7201       VLRU_Wait_r(&volume_LRU.q[idx]);
7202     } while (idx != vp->vlru.idx);
7203
7204     /* now remove from the VLRU and update
7205      * the appropriate counter */
7206     queue_Remove(&vp->vlru);
7207     volume_LRU.q[idx].len--;
7208     vp->vlru.idx = VLRU_QUEUE_INVALID;
7209     V_attachFlags(vp) &= ~(VOL_ON_VLRU);
7210 }
7211
7212 /**
7213  * tell the VLRU subsystem that a volume was just accessed.
7214  *
7215  * @param[in] vp  pointer to volume object
7216  *
7217  * @pre
7218  *    @arg @c VOL_LOCK is held
7219  *    @arg caller MUST hold a lightweight ref on @p vp
7220  *    @arg caller MUST NOT hold exclusive ownership of any VLRU queue
7221  *
7222  * @post volume VLRU access statistics are updated.  If the volume was on
7223  *       the VLRU soft detach candidate queue, it is moved to the NEW
7224  *       generation queue.
7225  *
7226  * @note @c VOL_LOCK may be dropped internally
7227  *
7228  * @note DAFS only
7229  *
7230  * @internal volume package internal use only.
7231  */
7232 static void
7233 VLRU_UpdateAccess_r(Volume * vp)
7234 {
7235     Volume * rvp = NULL;
7236
7237     if (!VLRU_enabled)
7238         return;
7239
7240     if (queue_IsNotOnQueue(&vp->vlru))
7241         return;
7242
7243     osi_Assert(V_attachFlags(vp) & VOL_ON_VLRU);
7244
7245     /* update the access timestamp */
7246     vp->stats.last_get = FT_ApproxTime();
7247
7248     /*
7249      * if the volume is on the soft detach candidate
7250      * list, we need to safely move it back to a
7251      * regular generation.  this has to be done
7252      * carefully so we don't race against the scanner
7253      * thread.
7254      */
7255
7256     /* if this volume is on the soft detach candidate queue,
7257      * then grab exclusive access to the necessary queues */
7258     if (vp->vlru.idx == VLRU_QUEUE_CANDIDATE) {
7259         rvp = vp;
7260         VCreateReservation_r(rvp);
7261
7262         VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
7263         VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
7264         VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
7265         VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
7266     }
7267
7268     /* make sure multiple threads don't race to update */
7269     if (vp->vlru.idx == VLRU_QUEUE_CANDIDATE) {
7270         VLRU_SwitchQueues(vp, VLRU_QUEUE_NEW, 1);
7271     }
7272
7273     if (rvp) {
7274       VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
7275       VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
7276       VCancelReservation_r(rvp);
7277     }
7278 }
7279
7280 /**
7281  * switch a volume between two VLRU queues.
7282  *
7283  * @param[in] vp       pointer to volume object
7284  * @param[in] new_idx  index of VLRU queue onto which the volume will be moved
7285  * @param[in] append   controls whether the volume will be appended or
7286  *                     prepended to the queue.  A nonzero value means it will
7287  *                     be appended; zero means it will be prepended.
7288  *
7289  * @pre The new (and old, if applicable) queue(s) must either be owned
7290  *      exclusively by the calling thread for asynchronous manipulation,
7291  *      or the queue(s) must be quiescent and VOL_LOCK must be held.
7292  *      Please see VLRU_BeginExclusive_r, VLRU_EndExclusive_r and VLRU_Wait_r
7293  *      for further details of the queue asynchronous processing mechanism.
7294  *
7295  * @post If the volume object was already on a VLRU queue, it is
7296  *       removed from the queue.  Depending on the value of the append
7297  *       parameter, the volume object is either appended or prepended
7298  *       to the VLRU queue referenced by the new_idx parameter.
7299  *
7300  * @note DAFS only
7301  *
7302  * @see VLRU_BeginExclusive_r
7303  * @see VLRU_EndExclusive_r
7304  * @see VLRU_Wait_r
7305  *
7306  * @internal volume package internal use only.
7307  */
7308 static void
7309 VLRU_SwitchQueues(Volume * vp, int new_idx, int append)
7310 {
7311     if (queue_IsNotOnQueue(&vp->vlru))
7312         return;
7313
7314     queue_Remove(&vp->vlru);
7315     volume_LRU.q[vp->vlru.idx].len--;
7316
7317     /* put the volume back on the correct generational queue */
7318     if (append) {
7319         queue_Append(&volume_LRU.q[new_idx], &vp->vlru);
7320     } else {
7321         queue_Prepend(&volume_LRU.q[new_idx], &vp->vlru);
7322     }
7323
7324     volume_LRU.q[new_idx].len++;
7325     vp->vlru.idx = new_idx;
7326 }
7327
7328 /**
7329  * VLRU background thread.
7330  *
7331  * The VLRU Scanner Thread is responsible for periodically scanning through
7332  * each VLRU queue looking for volumes which should be moved to another
7333  * queue, or soft detached.
7334  *
7335  * @param[in] args  unused thread arguments parameter
7336  *
7337  * @return unused thread return value
7338  *    @retval NULL always
7339  *
7340  * @internal volume package internal use only.
7341  */
7342 static void *
7343 VLRU_ScannerThread(void * args)
7344 {
7345     afs_uint32 now, min_delay, delay;
7346     int i, min_idx, min_op, overdue, state;
7347
7348     /* set t=0 for promotion cycle to be
7349      * fileserver startup */
7350     now = FT_ApproxTime();
7351     for (i=0; i < VLRU_GENERATIONS-1; i++) {
7352         volume_LRU.last_promotion[i] = now;
7353     }
7354
7355     /* don't start the scanner until VLRU_offline_thresh
7356      * plus a small delay for VInitVolumePackage2 to finish
7357      * has gone by */
7358
7359     sleep(VLRU_offline_thresh + 60);
7360
7361     /* set t=0 for scan cycle to be now */
7362     now = FT_ApproxTime();
7363     for (i=0; i < VLRU_GENERATIONS+1; i++) {
7364         volume_LRU.last_scan[i] = now;
7365     }
7366
7367     VOL_LOCK;
7368     if (volume_LRU.scanner_state == VLRU_SCANNER_STATE_OFFLINE) {
7369         volume_LRU.scanner_state = VLRU_SCANNER_STATE_ONLINE;
7370     }
7371
7372     while ((state = volume_LRU.scanner_state) != VLRU_SCANNER_STATE_SHUTTING_DOWN) {
7373         /* check to see if we've been asked to pause */
7374         if (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSING) {
7375             volume_LRU.scanner_state = VLRU_SCANNER_STATE_PAUSED;
7376             CV_BROADCAST(&volume_LRU.cv);
7377             do {
7378                 VOL_CV_WAIT(&volume_LRU.cv);
7379             } while (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSED);
7380         }
7381
7382         /* scheduling can happen outside the glock */
7383         VOL_UNLOCK;
7384
7385         /* figure out what is next on the schedule */
7386
7387         /* figure out a potential schedule for the new generation first */
7388         overdue = 0;
7389         min_delay = volume_LRU.scan_interval[0] + volume_LRU.last_scan[0] - now;
7390         min_idx = 0;
7391         min_op = 0;
7392         if (min_delay > volume_LRU.scan_interval[0]) {
7393             /* unsigned overflow -- we're overdue to run this scan */
7394             min_delay = 0;
7395             overdue = 1;
7396         }
7397
7398         /* if we're not overdue for gen 0, figure out schedule for candidate gen */
7399         if (!overdue) {
7400             i = VLRU_QUEUE_CANDIDATE;
7401             delay = volume_LRU.scan_interval[i] + volume_LRU.last_scan[i] - now;
7402             if (delay < min_delay) {
7403                 min_delay = delay;
7404                 min_idx = i;
7405             }
7406             if (delay > volume_LRU.scan_interval[i]) {
7407                 /* unsigned overflow -- we're overdue to run this scan */
7408                 min_delay = 0;
7409                 min_idx = i;
7410                 overdue = 1;
7411             }
7412         }
7413
7414         /* if we're still not overdue for something, figure out schedules for promotions */
7415         for (i=0; !overdue && i < VLRU_GENERATIONS-1; i++) {
7416             delay = volume_LRU.promotion_interval[i] + volume_LRU.last_promotion[i] - now;
7417             if (delay < min_delay) {
7418                 min_delay = delay;
7419                 min_idx = i;
7420                 min_op = 1;
7421             }
7422             if (delay > volume_LRU.promotion_interval[i]) {
7423                 /* unsigned overflow -- we're overdue to run this promotion */
7424                 min_delay = 0;
7425                 min_idx = i;
7426                 min_op = 1;
7427                 overdue = 1;
7428                 break;
7429             }
7430         }
7431
7432         /* sleep as needed */
7433         if (min_delay) {
7434             sleep(min_delay);
7435         }
7436
7437         /* do whatever is next */
7438         VOL_LOCK;
7439         if (min_op) {
7440             VLRU_Promote_r(min_idx);
7441             VLRU_Demote_r(min_idx+1);
7442         } else {
7443             VLRU_Scan_r(min_idx);
7444         }
7445         now = FT_ApproxTime();
7446     }
7447
7448     Log("VLRU scanner asked to go offline (scanner_state=%d)\n", state);
7449
7450     /* signal that scanner is down */
7451     volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
7452     CV_BROADCAST(&volume_LRU.cv);
7453     VOL_UNLOCK;
7454     return NULL;
7455 }
7456
7457 /**
7458  * promote volumes from one VLRU generation to the next.
7459  *
7460  * This routine scans a VLRU generation looking for volumes which are
7461  * eligible to be promoted to the next generation.  All volumes which
7462  * meet the eligibility requirement are promoted.
7463  *
7464  * Promotion eligibility is based upon meeting both of the following
7465  * requirements:
7466  *
7467  *    @arg The volume has been accessed since the last promotion:
7468  *         @c (vp->stats.last_get >= vp->stats.last_promote)
7469  *    @arg The last promotion occurred at least
7470  *         @c volume_LRU.promotion_interval[idx] seconds ago
7471  *
7472  * As a performance optimization, promotions are "globbed".  In other
7473  * words, we promote arbitrarily large contiguous sublists of elements
7474  * as one operation.
7475  *
7476  * @param[in] idx  VLRU queue index to scan
7477  *
7478  * @note DAFS only
7479  *
7480  * @internal VLRU internal use only.
7481  */
7482 static void
7483 VLRU_Promote_r(int idx)
7484 {
7485     int len, chaining, promote;
7486     afs_uint32 now, thresh;
7487     struct rx_queue *qp, *nqp;
7488     Volume * vp, *start = NULL, *end = NULL;
7489
7490     /* get exclusive access to two chains, and drop the glock */
7491     VLRU_Wait_r(&volume_LRU.q[idx]);
7492     VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
7493     VLRU_Wait_r(&volume_LRU.q[idx+1]);
7494     VLRU_BeginExclusive_r(&volume_LRU.q[idx+1]);
7495     VOL_UNLOCK;
7496
7497     thresh = volume_LRU.promotion_interval[idx];
7498     now = FT_ApproxTime();
7499
7500     len = chaining = 0;
7501     for (queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
7502         vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
7503         promote = (((vp->stats.last_promote + thresh) <= now) &&
7504                    (vp->stats.last_get >= vp->stats.last_promote));
7505
7506         if (chaining) {
7507             if (promote) {
7508                 vp->vlru.idx++;
7509                 len++;
7510                 start = vp;
7511             } else {
7512                 /* promote and prepend chain */
7513                 queue_MoveChainAfter(&volume_LRU.q[idx+1], &start->vlru, &end->vlru);
7514                 chaining = 0;
7515             }
7516         } else {
7517             if (promote) {
7518                 vp->vlru.idx++;
7519                 len++;
7520                 chaining = 1;
7521                 start = end = vp;
7522             }
7523         }
7524     }
7525
7526     if (chaining) {
7527         /* promote and prepend */
7528         queue_MoveChainAfter(&volume_LRU.q[idx+1], &start->vlru, &end->vlru);
7529     }
7530
7531     if (len) {
7532         volume_LRU.q[idx].len -= len;
7533         volume_LRU.q[idx+1].len += len;
7534     }
7535
7536     /* release exclusive access to the two chains */
7537     VOL_LOCK;
7538     volume_LRU.last_promotion[idx] = now;
7539     VLRU_EndExclusive_r(&volume_LRU.q[idx+1]);
7540     VLRU_EndExclusive_r(&volume_LRU.q[idx]);
7541 }
7542
7543 /* run the demotions */
7544 static void
7545 VLRU_Demote_r(int idx)
7546 {
7547     Error ec;
7548     int len, chaining, demote;
7549     afs_uint32 now, thresh;
7550     struct rx_queue *qp, *nqp;
7551     Volume * vp, *start = NULL, *end = NULL;
7552     Volume ** salv_flag_vec = NULL;
7553     int salv_vec_offset = 0;
7554
7555     osi_Assert(idx == VLRU_QUEUE_MID || idx == VLRU_QUEUE_OLD);
7556
7557     /* get exclusive access to two chains, and drop the glock */
7558     VLRU_Wait_r(&volume_LRU.q[idx-1]);
7559     VLRU_BeginExclusive_r(&volume_LRU.q[idx-1]);
7560     VLRU_Wait_r(&volume_LRU.q[idx]);
7561     VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
7562     VOL_UNLOCK;
7563
7564     /* no big deal if this allocation fails */
7565     if (volume_LRU.q[idx].len) {
7566         salv_flag_vec = (Volume **) malloc(volume_LRU.q[idx].len * sizeof(Volume *));
7567     }
7568
7569     now = FT_ApproxTime();
7570     thresh = volume_LRU.promotion_interval[idx-1];
7571
7572     len = chaining = 0;
7573     for (queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
7574         vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
7575         demote = (((vp->stats.last_promote + thresh) <= now) &&
7576                   (vp->stats.last_get < (now - thresh)));
7577
7578         /* we now do volume update list DONT_SALVAGE flag setting during
7579          * demotion passes */
7580         if (salv_flag_vec &&
7581             !(V_attachFlags(vp) & VOL_HDR_DONTSALV) &&
7582             demote &&
7583             (vp->updateTime < (now - SALVAGE_INTERVAL)) &&
7584             (V_attachState(vp) == VOL_STATE_ATTACHED)) {
7585             salv_flag_vec[salv_vec_offset++] = vp;
7586             VCreateReservation_r(vp);
7587         }
7588
7589         if (chaining) {
7590             if (demote) {
7591                 vp->vlru.idx--;
7592                 len++;
7593                 start = vp;
7594             } else {
7595                 /* demote and append chain */
7596                 queue_MoveChainBefore(&volume_LRU.q[idx-1], &start->vlru, &end->vlru);
7597                 chaining = 0;
7598             }
7599         } else {
7600             if (demote) {
7601                 vp->vlru.idx--;
7602                 len++;
7603                 chaining = 1;
7604                 start = end = vp;
7605             }
7606         }
7607     }
7608
7609     if (chaining) {
7610         queue_MoveChainBefore(&volume_LRU.q[idx-1], &start->vlru, &end->vlru);
7611     }
7612
7613     if (len) {
7614         volume_LRU.q[idx].len -= len;
7615         volume_LRU.q[idx-1].len += len;
7616     }
7617
7618     /* release exclusive access to the two chains */
7619     VOL_LOCK;
7620     VLRU_EndExclusive_r(&volume_LRU.q[idx]);
7621     VLRU_EndExclusive_r(&volume_LRU.q[idx-1]);
7622
7623     /* now go back and set the DONT_SALVAGE flags as appropriate */
7624     if (salv_flag_vec) {
7625         int i;
7626         for (i = 0; i < salv_vec_offset; i++) {
7627             vp = salv_flag_vec[i];
7628             if (!(V_attachFlags(vp) & VOL_HDR_DONTSALV) &&
7629                 (vp->updateTime < (now - SALVAGE_INTERVAL)) &&
7630                 (V_attachState(vp) == VOL_STATE_ATTACHED)) {
7631                 ec = VHold_r(vp);
7632                 if (!ec) {
7633                     V_attachFlags(vp) |= VOL_HDR_DONTSALV;
7634                     V_dontSalvage(vp) = DONT_SALVAGE;
7635                     VUpdateVolume_r(&ec, vp, 0);
7636                     VPutVolume_r(vp);
7637                 }
7638             }
7639             VCancelReservation_r(vp);
7640         }
7641         free(salv_flag_vec);
7642     }
7643 }
7644
7645 /* run a pass of the VLRU GC scanner */
7646 static void
7647 VLRU_Scan_r(int idx)
7648 {
7649     afs_uint32 now, thresh;
7650     struct rx_queue *qp, *nqp;
7651     Volume * vp;
7652     int i, locked = 1;
7653
7654     osi_Assert(idx == VLRU_QUEUE_NEW || idx == VLRU_QUEUE_CANDIDATE);
7655
7656     /* gain exclusive access to the idx VLRU */
7657     VLRU_Wait_r(&volume_LRU.q[idx]);
7658     VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
7659
7660     if (idx != VLRU_QUEUE_CANDIDATE) {
7661         /* gain exclusive access to the candidate VLRU */
7662         VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
7663         VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
7664     }
7665
7666     now = FT_ApproxTime();
7667     thresh = now - VLRU_offline_thresh;
7668
7669     /* perform candidate selection and soft detaching */
7670     if (idx == VLRU_QUEUE_CANDIDATE) {
7671         /* soft detach some volumes from the candidate pool */
7672         VOL_UNLOCK;
7673         locked = 0;
7674
7675         for (i=0,queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
7676             vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
7677             if (i >= VLRU_offline_max) {
7678                 break;
7679             }
7680             /* check timestamp to see if it's a candidate for soft detaching */
7681             if (vp->stats.last_get <= thresh) {
7682                 VOL_LOCK;
7683                 if (VCheckSoftDetach(vp, thresh))
7684                     i++;
7685                 VOL_UNLOCK;
7686             }
7687         }
7688     } else {
7689         /* scan for volumes to become soft detach candidates */
7690         for (i=1,queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue),i++) {
7691             vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
7692
7693             /* check timestamp to see if it's a candidate for soft detaching */
7694             if (vp->stats.last_get <= thresh) {
7695                 VCheckSoftDetachCandidate(vp, thresh);
7696             }
7697
7698             if (!(i&0x7f)) {   /* lock coarsening optimization */
7699                 VOL_UNLOCK;
7700                 pthread_yield();
7701                 VOL_LOCK;
7702             }
7703         }
7704     }
7705
7706     /* relinquish exclusive access to the VLRU chains */
7707     if (!locked) {
7708         VOL_LOCK;
7709     }
7710     volume_LRU.last_scan[idx] = now;
7711     if (idx != VLRU_QUEUE_CANDIDATE) {
7712         VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
7713     }
7714     VLRU_EndExclusive_r(&volume_LRU.q[idx]);
7715 }
7716
7717 /* check whether volume is safe to soft detach
7718  * caller MUST NOT hold a ref count on vp */
7719 static int
7720 VCheckSoftDetach(Volume * vp, afs_uint32 thresh)
7721 {
7722     int ret=0;
7723
7724     if (vp->nUsers || vp->nWaiters)
7725         return 0;
7726
7727     if (vp->stats.last_get <= thresh) {
7728         ret = VSoftDetachVolume_r(vp, thresh);
7729     }
7730
7731     return ret;
7732 }
7733
7734 /* check whether volume should be made a
7735  * soft detach candidate */
7736 static int
7737 VCheckSoftDetachCandidate(Volume * vp, afs_uint32 thresh)
7738 {
7739     int idx, ret = 0;
7740     if (vp->nUsers || vp->nWaiters)
7741         return 0;
7742
7743     idx = vp->vlru.idx;
7744
7745     osi_Assert(idx == VLRU_QUEUE_NEW);
7746
7747     if (vp->stats.last_get <= thresh) {
7748         /* move to candidate pool */
7749         queue_Remove(&vp->vlru);
7750         volume_LRU.q[VLRU_QUEUE_NEW].len--;
7751         queue_Prepend(&volume_LRU.q[VLRU_QUEUE_CANDIDATE], &vp->vlru);
7752         vp->vlru.idx = VLRU_QUEUE_CANDIDATE;
7753         volume_LRU.q[VLRU_QUEUE_CANDIDATE].len++;
7754         ret = 1;
7755     }
7756
7757     return ret;
7758 }
7759
7760
7761 /* begin exclusive access on VLRU */
7762 static void
7763 VLRU_BeginExclusive_r(struct VLRU_q * q)
7764 {
7765     osi_Assert(q->busy == 0);
7766     q->busy = 1;
7767 }
7768
7769 /* end exclusive access on VLRU */
7770 static void
7771 VLRU_EndExclusive_r(struct VLRU_q * q)
7772 {
7773     osi_Assert(q->busy);
7774     q->busy = 0;
7775     CV_BROADCAST(&q->cv);
7776 }
7777
7778 /* wait for another thread to end exclusive access on VLRU */
7779 static void
7780 VLRU_Wait_r(struct VLRU_q * q)
7781 {
7782     while(q->busy) {
7783         VOL_CV_WAIT(&q->cv);
7784     }
7785 }
7786
7787 /* demand attach fs
7788  * volume soft detach
7789  *
7790  * caller MUST NOT hold a ref count on vp */
7791 static int
7792 VSoftDetachVolume_r(Volume * vp, afs_uint32 thresh)
7793 {
7794     afs_uint32 ts_save;
7795     int ret = 0;
7796
7797     osi_Assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
7798
7799     ts_save = vp->stats.last_get;
7800     if (ts_save > thresh)
7801         return 0;
7802
7803     if (vp->nUsers || vp->nWaiters)
7804         return 0;
7805
7806     if (VIsExclusiveState(V_attachState(vp))) {
7807         return 0;
7808     }
7809
7810     switch (V_attachState(vp)) {
7811     case VOL_STATE_UNATTACHED:
7812     case VOL_STATE_PREATTACHED:
7813     case VOL_STATE_ERROR:
7814     case VOL_STATE_GOING_OFFLINE:
7815     case VOL_STATE_SHUTTING_DOWN:
7816     case VOL_STATE_SALVAGING:
7817     case VOL_STATE_DELETED:
7818         volume_LRU.q[vp->vlru.idx].len--;
7819
7820         /* create and cancel a reservation to
7821          * give the volume an opportunity to
7822          * be deallocated */
7823         VCreateReservation_r(vp);
7824         queue_Remove(&vp->vlru);
7825         vp->vlru.idx = VLRU_QUEUE_INVALID;
7826         V_attachFlags(vp) &= ~(VOL_ON_VLRU);
7827         VCancelReservation_r(vp);
7828         return 0;
7829     default:
7830         break;
7831     }
7832
7833     /* hold the volume and take it offline.
7834      * no need for reservations, as VHold_r
7835      * takes care of that internally. */
7836     if (VHold_r(vp) == 0) {
7837         /* vhold drops the glock, so now we should
7838          * check to make sure we aren't racing against
7839          * other threads.  if we are racing, offlining vp
7840          * would be wasteful, and block the scanner for a while
7841          */
7842         if (vp->nWaiters ||
7843             (vp->nUsers > 1) ||
7844             (vp->shuttingDown) ||
7845             (vp->goingOffline) ||
7846             (vp->stats.last_get != ts_save)) {
7847             /* looks like we're racing someone else. bail */
7848             VPutVolume_r(vp);
7849             vp = NULL;
7850         } else {
7851             /* pull it off the VLRU */
7852             osi_Assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
7853             volume_LRU.q[VLRU_QUEUE_CANDIDATE].len--;
7854             queue_Remove(&vp->vlru);
7855             vp->vlru.idx = VLRU_QUEUE_INVALID;
7856             V_attachFlags(vp) &= ~(VOL_ON_VLRU);
7857
7858             /* take if offline */
7859             VOffline_r(vp, "volume has been soft detached");
7860
7861             /* invalidate the volume header cache */
7862             FreeVolumeHeader(vp);
7863
7864             /* update stats */
7865             IncUInt64(&VStats.soft_detaches);
7866             vp->stats.soft_detaches++;
7867
7868             /* put in pre-attached state so demand
7869              * attacher can work on it */
7870             VChangeState_r(vp, VOL_STATE_PREATTACHED);
7871             ret = 1;
7872         }
7873     }
7874     return ret;
7875 }
7876 #endif /* AFS_DEMAND_ATTACH_FS */
7877
7878
7879 /***************************************************/
7880 /* Volume Header Cache routines                    */
7881 /***************************************************/
7882
7883 /**
7884  * volume header cache.
7885  */
7886 struct volume_hdr_LRU_t volume_hdr_LRU;
7887
7888 /**
7889  * initialize the volume header cache.
7890  *
7891  * @param[in] howMany  number of header cache entries to preallocate
7892  *
7893  * @pre VOL_LOCK held.  Function has never been called before.
7894  *
7895  * @post howMany cache entries are allocated, initialized, and added
7896  *       to the LRU list.  Header cache statistics are initialized.
7897  *
7898  * @note only applicable to fileServer program type.  Should only be
7899  *       called once during volume package initialization.
7900  *
7901  * @internal volume package internal use only.
7902  */
7903 static void
7904 VInitVolumeHeaderCache(afs_uint32 howMany)
7905 {
7906     struct volHeader *hp;
7907     if (programType != fileServer)
7908         return;
7909     queue_Init(&volume_hdr_LRU);
7910     volume_hdr_LRU.stats.free = 0;
7911     volume_hdr_LRU.stats.used = howMany;
7912     volume_hdr_LRU.stats.attached = 0;
7913     hp = (struct volHeader *)(calloc(howMany, sizeof(struct volHeader)));
7914     osi_Assert(hp != NULL);
7915
7916     while (howMany--)
7917         /* We are using ReleaseVolumeHeader to initialize the values on the header list
7918          * to ensure they have the right values
7919          */
7920         ReleaseVolumeHeader(hp++);
7921 }
7922
7923 /**
7924  * get a volume header and attach it to the volume object.
7925  *
7926  * @param[in] vp  pointer to volume object
7927  *
7928  * @return cache entry status
7929  *    @retval 0  volume header was newly attached; cache data is invalid
7930  *    @retval 1  volume header was previously attached; cache data is valid
7931  *
7932  * @pre VOL_LOCK held.  For DAFS, lightweight ref must be held on volume object.
7933  *
7934  * @post volume header attached to volume object.  if necessary, header cache
7935  *       entry on LRU is synchronized to disk.  Header is removed from LRU list.
7936  *
7937  * @note VOL_LOCK may be dropped
7938  *
7939  * @warning this interface does not load header data from disk.  it merely
7940  *          attaches a header object to the volume object, and may sync the old
7941  *          header cache data out to disk in the process.
7942  *
7943  * @internal volume package internal use only.
7944  */
7945 static int
7946 GetVolumeHeader(Volume * vp)
7947 {
7948     Error error;
7949     struct volHeader *hd;
7950     int old;
7951     static int everLogged = 0;
7952
7953 #ifdef AFS_DEMAND_ATTACH_FS
7954     VolState vp_save = 0, back_save = 0;
7955
7956     /* XXX debug 9/19/05 we've apparently got
7957      * a ref counting bug somewhere that's
7958      * breaking the nUsers == 0 => header on LRU
7959      * assumption */
7960     if (vp->header && queue_IsNotOnQueue(vp->header)) {
7961         Log("nUsers == 0, but header not on LRU\n");
7962         return 1;
7963     }
7964 #endif
7965
7966     old = (vp->header != NULL); /* old == volume already has a header */
7967
7968     if (programType != fileServer) {
7969         /* for volume utilities, we allocate volHeaders as needed */
7970         if (!vp->header) {
7971             hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
7972             osi_Assert(hd != NULL);
7973             vp->header = hd;
7974             hd->back = vp;
7975 #ifdef AFS_DEMAND_ATTACH_FS
7976             V_attachFlags(vp) |= VOL_HDR_ATTACHED;
7977 #endif
7978         }
7979     } else {
7980         /* for the fileserver, we keep a volume header cache */
7981         if (old) {
7982             /* the header we previously dropped in the lru is
7983              * still available. pull it off the lru and return */
7984             hd = vp->header;
7985             queue_Remove(hd);
7986             osi_Assert(hd->back == vp);
7987 #ifdef AFS_DEMAND_ATTACH_FS
7988             V_attachFlags(vp) &= ~(VOL_HDR_IN_LRU);
7989 #endif
7990         } else {
7991             /* we need to grab a new element off the LRU */
7992             if (queue_IsNotEmpty(&volume_hdr_LRU)) {
7993                 /* grab an element and pull off of LRU */
7994                 hd = queue_First(&volume_hdr_LRU, volHeader);
7995                 queue_Remove(hd);
7996             } else {
7997                 /* LRU is empty, so allocate a new volHeader
7998                  * this is probably indicative of a leak, so let the user know */
7999                 hd = (struct volHeader *)calloc(1, sizeof(struct volHeader));
8000                 osi_Assert(hd != NULL);
8001                 if (!everLogged) {
8002                     Log("****Allocated more volume headers, probably leak****\n");
8003                     everLogged = 1;
8004                 }
8005                 volume_hdr_LRU.stats.free++;
8006             }
8007             if (hd->back) {
8008                 /* this header used to belong to someone else.
8009                  * we'll need to check if the header needs to
8010                  * be sync'd out to disk */
8011
8012 #ifdef AFS_DEMAND_ATTACH_FS
8013                 /* if hd->back were in an exclusive state, then
8014                  * its volHeader would not be on the LRU... */
8015                 osi_Assert(!VIsExclusiveState(V_attachState(hd->back)));
8016 #endif
8017
8018                 if (hd->diskstuff.inUse) {
8019                     /* volume was in use, so we'll need to sync
8020                      * its header to disk */
8021
8022 #ifdef AFS_DEMAND_ATTACH_FS
8023                     back_save = VChangeState_r(hd->back, VOL_STATE_UPDATING);
8024                     vp_save = VChangeState_r(vp, VOL_STATE_HDR_ATTACHING);
8025                     VCreateReservation_r(hd->back);
8026                     VOL_UNLOCK;
8027 #endif
8028
8029                     WriteVolumeHeader_r(&error, hd->back);
8030                     /* Ignore errors; catch them later */
8031
8032 #ifdef AFS_DEMAND_ATTACH_FS
8033                     VOL_LOCK;
8034 #endif
8035                 }
8036
8037                 hd->back->header = NULL;
8038 #ifdef AFS_DEMAND_ATTACH_FS
8039                 V_attachFlags(hd->back) &= ~(VOL_HDR_ATTACHED | VOL_HDR_LOADED | VOL_HDR_IN_LRU);
8040
8041                 if (hd->diskstuff.inUse) {
8042                     VChangeState_r(hd->back, back_save);
8043                     VCancelReservation_r(hd->back);
8044                     VChangeState_r(vp, vp_save);
8045                 }
8046 #endif
8047             } else {
8048                 volume_hdr_LRU.stats.attached++;
8049             }
8050             hd->back = vp;
8051             vp->header = hd;
8052 #ifdef AFS_DEMAND_ATTACH_FS
8053             V_attachFlags(vp) |= VOL_HDR_ATTACHED;
8054 #endif
8055         }
8056         volume_hdr_LRU.stats.free--;
8057         volume_hdr_LRU.stats.used++;
8058     }
8059     IncUInt64(&VStats.hdr_gets);
8060 #ifdef AFS_DEMAND_ATTACH_FS
8061     IncUInt64(&vp->stats.hdr_gets);
8062     vp->stats.last_hdr_get = FT_ApproxTime();
8063 #endif
8064     return old;
8065 }
8066
8067
8068 /**
8069  * make sure volume header is attached and contains valid cache data.
8070  *
8071  * @param[out] ec  outbound error code
8072  * @param[in]  vp  pointer to volume object
8073  *
8074  * @pre VOL_LOCK held.  For DAFS, lightweight ref held on vp.
8075  *
8076  * @post header cache entry attached, and loaded with valid data, or
8077  *       *ec is nonzero, and the header is released back into the LRU.
8078  *
8079  * @internal volume package internal use only.
8080  */
8081 static void
8082 LoadVolumeHeader(Error * ec, Volume * vp)
8083 {
8084 #ifdef AFS_DEMAND_ATTACH_FS
8085     VolState state_save;
8086     afs_uint32 now;
8087     *ec = 0;
8088
8089     if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
8090         IncUInt64(&VStats.hdr_loads);
8091         state_save = VChangeState_r(vp, VOL_STATE_HDR_LOADING);
8092         VOL_UNLOCK;
8093
8094         ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
8095                    sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
8096                    VOLUMEINFOVERSION);
8097         IncUInt64(&vp->stats.hdr_loads);
8098         now = FT_ApproxTime();
8099
8100         VOL_LOCK;
8101         if (!*ec) {
8102             V_attachFlags(vp) |= VOL_HDR_LOADED;
8103             vp->stats.last_hdr_load = now;
8104         }
8105         VChangeState_r(vp, state_save);
8106     }
8107 #else /* AFS_DEMAND_ATTACH_FS */
8108     *ec = 0;
8109     if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
8110         IncUInt64(&VStats.hdr_loads);
8111
8112         ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
8113                    sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
8114                    VOLUMEINFOVERSION);
8115     }
8116 #endif /* AFS_DEMAND_ATTACH_FS */
8117     if (*ec) {
8118         /* maintain (nUsers==0) => header in LRU invariant */
8119         FreeVolumeHeader(vp);
8120     }
8121 }
8122
8123 /**
8124  * release a header cache entry back into the LRU list.
8125  *
8126  * @param[in] hd  pointer to volume header cache object
8127  *
8128  * @pre VOL_LOCK held.
8129  *
8130  * @post header cache object appended onto end of LRU list.
8131  *
8132  * @note only applicable to fileServer program type.
8133  *
8134  * @note used to place a header cache entry back into the
8135  *       LRU pool without invalidating it as a cache entry.
8136  *
8137  * @internal volume package internal use only.
8138  */
8139 static void
8140 ReleaseVolumeHeader(struct volHeader *hd)
8141 {
8142     if (programType != fileServer)
8143         return;
8144     if (!hd || queue_IsOnQueue(hd))     /* no header, or header already released */
8145         return;
8146     queue_Append(&volume_hdr_LRU, hd);
8147 #ifdef AFS_DEMAND_ATTACH_FS
8148     if (hd->back) {
8149         V_attachFlags(hd->back) |= VOL_HDR_IN_LRU;
8150     }
8151 #endif
8152     volume_hdr_LRU.stats.free++;
8153     volume_hdr_LRU.stats.used--;
8154 }
8155
8156 /**
8157  * free/invalidate a volume header cache entry.
8158  *
8159  * @param[in] vp  pointer to volume object
8160  *
8161  * @pre VOL_LOCK is held.
8162  *
8163  * @post For fileserver, header cache entry is returned to LRU, and it is
8164  *       invalidated as a cache entry.  For volume utilities, the header
8165  *       cache entry is freed.
8166  *
8167  * @note For fileserver, this should be utilized instead of ReleaseVolumeHeader
8168  *       whenever it is necessary to invalidate the header cache entry.
8169  *
8170  * @see ReleaseVolumeHeader
8171  *
8172  * @internal volume package internal use only.
8173  */
8174 static void
8175 FreeVolumeHeader(Volume * vp)
8176 {
8177     struct volHeader *hd = vp->header;
8178     if (!hd)
8179         return;
8180     if (programType == fileServer) {
8181         ReleaseVolumeHeader(hd);
8182         hd->back = NULL;
8183     } else {
8184         free(hd);
8185     }
8186 #ifdef AFS_DEMAND_ATTACH_FS
8187     V_attachFlags(vp) &= ~(VOL_HDR_ATTACHED | VOL_HDR_IN_LRU | VOL_HDR_LOADED);
8188 #endif
8189     volume_hdr_LRU.stats.attached--;
8190     vp->header = NULL;
8191 }
8192
8193
8194 /***************************************************/
8195 /* Volume Hash Table routines                      */
8196 /***************************************************/
8197
8198 /**
8199  * set size of volume object hash table.
8200  *
8201  * @param[in] logsize   log(2) of desired hash table size
8202  *
8203  * @return operation status
8204  *    @retval 0 success
8205  *    @retval -1 failure
8206  *
8207  * @pre MUST be called prior to VInitVolumePackage2
8208  *
8209  * @post Volume Hash Table will have 2^logsize buckets
8210  */
8211 int
8212 VSetVolHashSize(int logsize)
8213 {
8214     /* 64 to 268435456 hash buckets seems like a reasonable range */
8215     if ((logsize < 6 ) || (logsize > 28)) {
8216         return -1;
8217     }
8218
8219     if (!VInit) {
8220         VolumeHashTable.Size = 1 << logsize;
8221         VolumeHashTable.Mask = VolumeHashTable.Size - 1;
8222     } else {
8223         /* we can't yet support runtime modification of this
8224          * parameter. we'll need a configuration rwlock to
8225          * make runtime modification feasible.... */
8226         return -1;
8227     }
8228     return 0;
8229 }
8230
8231 /**
8232  * initialize dynamic data structures for volume hash table.
8233  *
8234  * @post hash table is allocated, and fields are initialized.
8235  *
8236  * @internal volume package internal use only.
8237  */
8238 static void
8239 VInitVolumeHash(void)
8240 {
8241     int i;
8242
8243     VolumeHashTable.Table = (VolumeHashChainHead *) calloc(VolumeHashTable.Size,
8244                                                            sizeof(VolumeHashChainHead));
8245     osi_Assert(VolumeHashTable.Table != NULL);
8246
8247     for (i=0; i < VolumeHashTable.Size; i++) {
8248         queue_Init(&VolumeHashTable.Table[i]);
8249 #ifdef AFS_DEMAND_ATTACH_FS
8250         CV_INIT(&VolumeHashTable.Table[i].chain_busy_cv, "vhash busy", CV_DEFAULT, 0);
8251 #endif /* AFS_DEMAND_ATTACH_FS */
8252     }
8253 }
8254
8255 /**
8256  * add a volume object to the hash table.
8257  *
8258  * @param[in] vp      pointer to volume object
8259  * @param[in] hashid  hash of volume id
8260  *
8261  * @pre VOL_LOCK is held.  For DAFS, caller must hold a lightweight
8262  *      reference on vp.
8263  *
8264  * @post volume is added to hash chain.
8265  *
8266  * @internal volume package internal use only.
8267  *
8268  * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
8269  *       asynchronous hash chain reordering to finish.
8270  */
8271 static void
8272 AddVolumeToHashTable(Volume * vp, int hashid)
8273 {
8274     VolumeHashChainHead * head;
8275
8276     if (queue_IsOnQueue(vp))
8277         return;
8278
8279     head = &VolumeHashTable.Table[VOLUME_HASH(hashid)];
8280
8281 #ifdef AFS_DEMAND_ATTACH_FS
8282     /* wait for the hash chain to become available */
8283     VHashWait_r(head);
8284
8285     V_attachFlags(vp) |= VOL_IN_HASH;
8286     vp->chainCacheCheck = ++head->cacheCheck;
8287 #endif /* AFS_DEMAND_ATTACH_FS */
8288
8289     head->len++;
8290     vp->hashid = hashid;
8291     queue_Append(head, vp);
8292     vp->vnodeHashOffset = VolumeHashOffset_r();
8293 }
8294
8295 /**
8296  * delete a volume object from the hash table.
8297  *
8298  * @param[in] vp  pointer to volume object
8299  *
8300  * @pre VOL_LOCK is held.  For DAFS, caller must hold a lightweight
8301  *      reference on vp.
8302  *
8303  * @post volume is removed from hash chain.
8304  *
8305  * @internal volume package internal use only.
8306  *
8307  * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
8308  *       asynchronous hash chain reordering to finish.
8309  */
8310 static void
8311 DeleteVolumeFromHashTable(Volume * vp)
8312 {
8313     VolumeHashChainHead * head;
8314
8315     if (!queue_IsOnQueue(vp))
8316         return;
8317
8318     head = &VolumeHashTable.Table[VOLUME_HASH(vp->hashid)];
8319
8320 #ifdef AFS_DEMAND_ATTACH_FS
8321     /* wait for the hash chain to become available */
8322     VHashWait_r(head);
8323
8324     V_attachFlags(vp) &= ~(VOL_IN_HASH);
8325     head->cacheCheck++;
8326 #endif /* AFS_DEMAND_ATTACH_FS */
8327
8328     head->len--;
8329     queue_Remove(vp);
8330     /* do NOT reset hashid to zero, as the online
8331      * salvager package may need to know the volume id
8332      * after the volume is removed from the hash */
8333 }
8334
8335 /**
8336  * lookup a volume object in the hash table given a volume id.
8337  *
8338  * @param[out] ec        error code return
8339  * @param[in]  volumeId  volume id
8340  * @param[in]  hint      volume object which we believe could be the correct
8341                          mapping
8342  *
8343  * @return volume object pointer
8344  *    @retval NULL  no such volume id is registered with the hash table.
8345  *
8346  * @pre VOL_LOCK is held.  For DAFS, caller must hold a lightweight
8347         ref on hint.
8348  *
8349  * @post volume object with the given id is returned.  volume object and
8350  *       hash chain access statistics are updated.  hash chain may have
8351  *       been reordered.
8352  *
8353  * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
8354  *       asynchronous hash chain reordering operation to finish, or
8355  *       in order for us to perform an asynchronous chain reordering.
8356  *
8357  * @note Hash chain reorderings occur when the access count for the
8358  *       volume object being looked up exceeds the sum of the previous
8359  *       node's (the node ahead of it in the hash chain linked list)
8360  *       access count plus the constant VOLUME_HASH_REORDER_THRESHOLD.
8361  *
8362  * @note For DAFS, the hint parameter allows us to short-circuit if the
8363  *       cacheCheck fields match between the hash chain head and the
8364  *       hint volume object.
8365  */
8366 Volume *
8367 VLookupVolume_r(Error * ec, VolId volumeId, Volume * hint)
8368 {
8369     int looks = 0;
8370     Volume * vp, *np;
8371 #ifdef AFS_DEMAND_ATTACH_FS
8372     Volume *pp;
8373 #endif
8374     VolumeHashChainHead * head;
8375     *ec = 0;
8376
8377     head = &VolumeHashTable.Table[VOLUME_HASH(volumeId)];
8378
8379 #ifdef AFS_DEMAND_ATTACH_FS
8380     /* wait for the hash chain to become available */
8381     VHashWait_r(head);
8382
8383     /* check to see if we can short circuit without walking the hash chain */
8384     if (hint && (hint->chainCacheCheck == head->cacheCheck)) {
8385         IncUInt64(&hint->stats.hash_short_circuits);
8386         return hint;
8387     }
8388 #endif /* AFS_DEMAND_ATTACH_FS */
8389
8390     /* someday we need to either do per-chain locks, RWlocks,
8391      * or both for volhash access.
8392      * (and move to a data structure with better cache locality) */
8393
8394     /* search the chain for this volume id */
8395     for(queue_Scan(head, vp, np, Volume)) {
8396         looks++;
8397         if ((vp->hashid == volumeId)) {
8398             break;
8399         }
8400     }
8401
8402     if (queue_IsEnd(head, vp)) {
8403         vp = NULL;
8404     }
8405
8406 #ifdef AFS_DEMAND_ATTACH_FS
8407     /* update hash chain statistics */
8408     {
8409         afs_uint64 lks;
8410         FillInt64(lks, 0, looks);
8411         AddUInt64(head->looks, lks, &head->looks);
8412         AddUInt64(VStats.hash_looks, lks, &VStats.hash_looks);
8413         IncUInt64(&head->gets);
8414     }
8415
8416     if (vp) {
8417         afs_uint64 thresh;
8418         IncUInt64(&vp->stats.hash_lookups);
8419
8420         /* for demand attach fileserver, we permit occasional hash chain reordering
8421          * so that frequently looked up volumes move towards the head of the chain */
8422         pp = queue_Prev(vp, Volume);
8423         if (!queue_IsEnd(head, pp)) {
8424             FillInt64(thresh, 0, VOLUME_HASH_REORDER_THRESHOLD);
8425             AddUInt64(thresh, pp->stats.hash_lookups, &thresh);
8426             if (GEInt64(vp->stats.hash_lookups, thresh)) {
8427                 VReorderHash_r(head, pp, vp);
8428             }
8429         }
8430
8431         /* update the short-circuit cache check */
8432         vp->chainCacheCheck = head->cacheCheck;
8433     }
8434 #endif /* AFS_DEMAND_ATTACH_FS */
8435
8436     return vp;
8437 }
8438
8439 #ifdef AFS_DEMAND_ATTACH_FS
8440 /* perform volume hash chain reordering.
8441  *
8442  * advance a subchain beginning at vp ahead of
8443  * the adjacent subchain ending at pp */
8444 static void
8445 VReorderHash_r(VolumeHashChainHead * head, Volume * pp, Volume * vp)
8446 {
8447     Volume *tp, *np, *lp;
8448     afs_uint64 move_thresh;
8449
8450     /* this should never be called if the chain is already busy, so
8451      * no need to wait for other exclusive chain ops to finish */
8452
8453     /* this is a rather heavy set of operations,
8454      * so let's set the chain busy flag and drop
8455      * the vol_glock */
8456     VHashBeginExclusive_r(head);
8457     VOL_UNLOCK;
8458
8459     /* scan forward in the chain from vp looking for the last element
8460      * in the chain we want to advance */
8461     FillInt64(move_thresh, 0, VOLUME_HASH_REORDER_CHAIN_THRESH);
8462     AddUInt64(move_thresh, pp->stats.hash_lookups, &move_thresh);
8463     for(queue_ScanFrom(head, vp, tp, np, Volume)) {
8464         if (LTInt64(tp->stats.hash_lookups, move_thresh)) {
8465             break;
8466         }
8467     }
8468     lp = queue_Prev(tp, Volume);
8469
8470     /* scan backwards from pp to determine where to splice and
8471      * insert the subchain we're advancing */
8472     for(queue_ScanBackwardsFrom(head, pp, tp, np, Volume)) {
8473         if (GTInt64(tp->stats.hash_lookups, move_thresh)) {
8474             break;
8475         }
8476     }
8477     tp = queue_Next(tp, Volume);
8478
8479     /* rebalance chain(vp,...,lp) ahead of chain(tp,...,pp) */
8480     queue_MoveChainBefore(tp,vp,lp);
8481
8482     VOL_LOCK;
8483     IncUInt64(&VStats.hash_reorders);
8484     head->cacheCheck++;
8485     IncUInt64(&head->reorders);
8486
8487     /* wake up any threads waiting for the hash chain */
8488     VHashEndExclusive_r(head);
8489 }
8490
8491
8492 /* demand-attach fs volume hash
8493  * asynchronous exclusive operations */
8494
8495 /**
8496  * begin an asynchronous exclusive operation on a volume hash chain.
8497  *
8498  * @param[in] head   pointer to volume hash chain head object
8499  *
8500  * @pre VOL_LOCK held.  hash chain is quiescent.
8501  *
8502  * @post hash chain marked busy.
8503  *
8504  * @note this interface is used in conjunction with VHashEndExclusive_r and
8505  *       VHashWait_r to perform asynchronous (wrt VOL_LOCK) operations on a
8506  *       volume hash chain.  Its main use case is hash chain reordering, which
8507  *       has the potential to be a highly latent operation.
8508  *
8509  * @see VHashEndExclusive_r
8510  * @see VHashWait_r
8511  *
8512  * @note DAFS only
8513  *
8514  * @internal volume package internal use only.
8515  */
8516 static void
8517 VHashBeginExclusive_r(VolumeHashChainHead * head)
8518 {
8519     osi_Assert(head->busy == 0);
8520     head->busy = 1;
8521 }
8522
8523 /**
8524  * relinquish exclusive ownership of a volume hash chain.
8525  *
8526  * @param[in] head   pointer to volume hash chain head object
8527  *
8528  * @pre VOL_LOCK held.  thread owns the hash chain exclusively.
8529  *
8530  * @post hash chain is marked quiescent.  threads awaiting use of
8531  *       chain are awakened.
8532  *
8533  * @see VHashBeginExclusive_r
8534  * @see VHashWait_r
8535  *
8536  * @note DAFS only
8537  *
8538  * @internal volume package internal use only.
8539  */
8540 static void
8541 VHashEndExclusive_r(VolumeHashChainHead * head)
8542 {
8543     osi_Assert(head->busy);
8544     head->busy = 0;
8545     CV_BROADCAST(&head->chain_busy_cv);
8546 }
8547
8548 /**
8549  * wait for all asynchronous operations on a hash chain to complete.
8550  *
8551  * @param[in] head   pointer to volume hash chain head object
8552  *
8553  * @pre VOL_LOCK held.
8554  *
8555  * @post hash chain object is quiescent.
8556  *
8557  * @see VHashBeginExclusive_r
8558  * @see VHashEndExclusive_r
8559  *
8560  * @note DAFS only
8561  *
8562  * @note This interface should be called before any attempt to
8563  *       traverse the hash chain.  It is permissible for a thread
8564  *       to gain exclusive access to the chain, and then perform
8565  *       latent operations on the chain asynchronously wrt the
8566  *       VOL_LOCK.
8567  *
8568  * @warning if waiting is necessary, VOL_LOCK is dropped
8569  *
8570  * @internal volume package internal use only.
8571  */
8572 static void
8573 VHashWait_r(VolumeHashChainHead * head)
8574 {
8575     while (head->busy) {
8576         VOL_CV_WAIT(&head->chain_busy_cv);
8577     }
8578 }
8579 #endif /* AFS_DEMAND_ATTACH_FS */
8580
8581
8582 /***************************************************/
8583 /* Volume by Partition List routines               */
8584 /***************************************************/
8585
8586 /*
8587  * demand attach fileserver adds a
8588  * linked list of volumes to each
8589  * partition object, thus allowing
8590  * for quick enumeration of all
8591  * volumes on a partition
8592  */
8593
8594 #ifdef AFS_DEMAND_ATTACH_FS
8595 /**
8596  * add a volume to its disk partition VByPList.
8597  *
8598  * @param[in] vp  pointer to volume object
8599  *
8600  * @pre either the disk partition VByPList is owned exclusively
8601  *      by the calling thread, or the list is quiescent and
8602  *      VOL_LOCK is held.
8603  *
8604  * @post volume is added to disk partition VByPList
8605  *
8606  * @note DAFS only
8607  *
8608  * @warning it is the caller's responsibility to ensure list
8609  *          quiescence.
8610  *
8611  * @see VVByPListWait_r
8612  * @see VVByPListBeginExclusive_r
8613  * @see VVByPListEndExclusive_r
8614  *
8615  * @internal volume package internal use only.
8616  */
8617 static void
8618 AddVolumeToVByPList_r(Volume * vp)
8619 {
8620     if (queue_IsNotOnQueue(&vp->vol_list)) {
8621         queue_Append(&vp->partition->vol_list, &vp->vol_list);
8622         V_attachFlags(vp) |= VOL_ON_VBYP_LIST;
8623         vp->partition->vol_list.len++;
8624     }
8625 }
8626
8627 /**
8628  * delete a volume from its disk partition VByPList.
8629  *
8630  * @param[in] vp  pointer to volume object
8631  *
8632  * @pre either the disk partition VByPList is owned exclusively
8633  *      by the calling thread, or the list is quiescent and
8634  *      VOL_LOCK is held.
8635  *
8636  * @post volume is removed from the disk partition VByPList
8637  *
8638  * @note DAFS only
8639  *
8640  * @warning it is the caller's responsibility to ensure list
8641  *          quiescence.
8642  *
8643  * @see VVByPListWait_r
8644  * @see VVByPListBeginExclusive_r
8645  * @see VVByPListEndExclusive_r
8646  *
8647  * @internal volume package internal use only.
8648  */
8649 static void
8650 DeleteVolumeFromVByPList_r(Volume * vp)
8651 {
8652     if (queue_IsOnQueue(&vp->vol_list)) {
8653         queue_Remove(&vp->vol_list);
8654         V_attachFlags(vp) &= ~(VOL_ON_VBYP_LIST);
8655         vp->partition->vol_list.len--;
8656     }
8657 }
8658
8659 /**
8660  * begin an asynchronous exclusive operation on a VByPList.
8661  *
8662  * @param[in] dp   pointer to disk partition object
8663  *
8664  * @pre VOL_LOCK held.  VByPList is quiescent.
8665  *
8666  * @post VByPList marked busy.
8667  *
8668  * @note this interface is used in conjunction with VVByPListEndExclusive_r and
8669  *       VVByPListWait_r to perform asynchronous (wrt VOL_LOCK) operations on a
8670  *       VByPList.
8671  *
8672  * @see VVByPListEndExclusive_r
8673  * @see VVByPListWait_r
8674  *
8675  * @note DAFS only
8676  *
8677  * @internal volume package internal use only.
8678  */
8679 /* take exclusive control over the list */
8680 static void
8681 VVByPListBeginExclusive_r(struct DiskPartition64 * dp)
8682 {
8683     osi_Assert(dp->vol_list.busy == 0);
8684     dp->vol_list.busy = 1;
8685 }
8686
8687 /**
8688  * relinquish exclusive ownership of a VByPList.
8689  *
8690  * @param[in] dp   pointer to disk partition object
8691  *
8692  * @pre VOL_LOCK held.  thread owns the VByPList exclusively.
8693  *
8694  * @post VByPList is marked quiescent.  threads awaiting use of
8695  *       the list are awakened.
8696  *
8697  * @see VVByPListBeginExclusive_r
8698  * @see VVByPListWait_r
8699  *
8700  * @note DAFS only
8701  *
8702  * @internal volume package internal use only.
8703  */
8704 static void
8705 VVByPListEndExclusive_r(struct DiskPartition64 * dp)
8706 {
8707     osi_Assert(dp->vol_list.busy);
8708     dp->vol_list.busy = 0;
8709     CV_BROADCAST(&dp->vol_list.cv);
8710 }
8711
8712 /**
8713  * wait for all asynchronous operations on a VByPList to complete.
8714  *
8715  * @param[in] dp  pointer to disk partition object
8716  *
8717  * @pre VOL_LOCK is held.
8718  *
8719  * @post disk partition's VByP list is quiescent
8720  *
8721  * @note DAFS only
8722  *
8723  * @note This interface should be called before any attempt to
8724  *       traverse the VByPList.  It is permissible for a thread
8725  *       to gain exclusive access to the list, and then perform
8726  *       latent operations on the list asynchronously wrt the
8727  *       VOL_LOCK.
8728  *
8729  * @warning if waiting is necessary, VOL_LOCK is dropped
8730  *
8731  * @see VVByPListEndExclusive_r
8732  * @see VVByPListBeginExclusive_r
8733  *
8734  * @internal volume package internal use only.
8735  */
8736 static void
8737 VVByPListWait_r(struct DiskPartition64 * dp)
8738 {
8739     while (dp->vol_list.busy) {
8740         VOL_CV_WAIT(&dp->vol_list.cv);
8741     }
8742 }
8743 #endif /* AFS_DEMAND_ATTACH_FS */
8744
8745 /***************************************************/
8746 /* Volume Cache Statistics routines                */
8747 /***************************************************/
8748
8749 void
8750 VPrintCacheStats_r(void)
8751 {
8752     afs_uint32 get_hi, get_lo, load_hi, load_lo;
8753     struct VnodeClassInfo *vcp;
8754     vcp = &VnodeClassInfo[vLarge];
8755     Log("Large vnode cache, %d entries, %d allocs, %d gets (%d reads), %d writes\n", vcp->cacheSize, vcp->allocs, vcp->gets, vcp->reads, vcp->writes);
8756     vcp = &VnodeClassInfo[vSmall];
8757     Log("Small vnode cache,%d entries, %d allocs, %d gets (%d reads), %d writes\n", vcp->cacheSize, vcp->allocs, vcp->gets, vcp->reads, vcp->writes);
8758     SplitInt64(VStats.hdr_gets, get_hi, get_lo);
8759     SplitInt64(VStats.hdr_loads, load_hi, load_lo);
8760     Log("Volume header cache, %d entries, %d gets, %d replacements\n",
8761         VStats.hdr_cache_size, get_lo, load_lo);
8762 }
8763
8764 void
8765 VPrintCacheStats(void)
8766 {
8767     VOL_LOCK;
8768     VPrintCacheStats_r();
8769     VOL_UNLOCK;
8770 }
8771
8772 #ifdef AFS_DEMAND_ATTACH_FS
8773 static double
8774 UInt64ToDouble(afs_uint64 * x)
8775 {
8776     static double c32 = 4.0 * 1.073741824 * 1000000000.0;
8777     afs_uint32 h, l;
8778     SplitInt64(*x, h, l);
8779     return (((double)h) * c32) + ((double) l);
8780 }
8781
8782 static char *
8783 DoubleToPrintable(double x, char * buf, int len)
8784 {
8785     static double billion = 1000000000.0;
8786     afs_uint32 y[3];
8787
8788     y[0] = (afs_uint32) (x / (billion * billion));
8789     y[1] = (afs_uint32) ((x - (((double)y[0]) * billion * billion)) / billion);
8790     y[2] = (afs_uint32) (x - ((((double)y[0]) * billion * billion) + (((double)y[1]) * billion)));
8791
8792     if (y[0]) {
8793         snprintf(buf, len, "%d%09d%09d", y[0], y[1], y[2]);
8794     } else if (y[1]) {
8795         snprintf(buf, len, "%d%09d", y[1], y[2]);
8796     } else {
8797         snprintf(buf, len, "%d", y[2]);
8798     }
8799     buf[len-1] = '\0';
8800     return buf;
8801 }
8802
8803 struct VLRUExtStatsEntry {
8804     VolumeId volid;
8805 };
8806
8807 struct VLRUExtStats {
8808     afs_uint32 len;
8809     afs_uint32 used;
8810     struct {
8811         afs_uint32 start;
8812         afs_uint32 len;
8813     } queue_info[VLRU_QUEUE_INVALID];
8814     struct VLRUExtStatsEntry * vec;
8815 };
8816
8817 /**
8818  * add a 256-entry fudge factor onto the vector in case state changes
8819  * out from under us.
8820  */
8821 #define VLRU_EXT_STATS_VEC_LEN_FUDGE   256
8822
8823 /**
8824  * collect extended statistics for the VLRU subsystem.
8825  *
8826  * @param[out] stats  pointer to stats structure to be populated
8827  * @param[in] nvols   number of volumes currently known to exist
8828  *
8829  * @pre VOL_LOCK held
8830  *
8831  * @post stats->vec allocated and populated
8832  *
8833  * @return operation status
8834  *    @retval 0 success
8835  *    @retval 1 failure
8836  */
8837 static int
8838 VVLRUExtStats_r(struct VLRUExtStats * stats, afs_uint32 nvols)
8839 {
8840     afs_uint32 cur, idx, len;
8841     struct rx_queue * qp, * nqp;
8842     Volume * vp;
8843     struct VLRUExtStatsEntry * vec;
8844
8845     len = nvols + VLRU_EXT_STATS_VEC_LEN_FUDGE;
8846     vec = stats->vec = calloc(len,
8847                               sizeof(struct VLRUExtStatsEntry));
8848     if (vec == NULL) {
8849         return 1;
8850     }
8851
8852     cur = 0;
8853     for (idx = VLRU_QUEUE_NEW; idx < VLRU_QUEUE_INVALID; idx++) {
8854         VLRU_Wait_r(&volume_LRU.q[idx]);
8855         VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
8856         VOL_UNLOCK;
8857
8858         stats->queue_info[idx].start = cur;
8859
8860         for (queue_Scan(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
8861             if (cur == len) {
8862                 /* out of space in vec */
8863                 break;
8864             }
8865             vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
8866             vec[cur].volid = vp->hashid;
8867             cur++;
8868         }
8869
8870         stats->queue_info[idx].len = cur - stats->queue_info[idx].start;
8871
8872         VOL_LOCK;
8873         VLRU_EndExclusive_r(&volume_LRU.q[idx]);
8874     }
8875
8876     stats->len = len;
8877     stats->used = cur;
8878     return 0;
8879 }
8880
8881 #define ENUMTOSTRING(en)  #en
8882 #define ENUMCASE(en) \
8883     case en: return ENUMTOSTRING(en)
8884
8885 static char *
8886 vlru_idx_to_string(int idx)
8887 {
8888     switch (idx) {
8889         ENUMCASE(VLRU_QUEUE_NEW);
8890         ENUMCASE(VLRU_QUEUE_MID);
8891         ENUMCASE(VLRU_QUEUE_OLD);
8892         ENUMCASE(VLRU_QUEUE_CANDIDATE);
8893         ENUMCASE(VLRU_QUEUE_HELD);
8894         ENUMCASE(VLRU_QUEUE_INVALID);
8895     default:
8896         return "**UNKNOWN**";
8897     }
8898 }
8899
8900 void
8901 VPrintExtendedCacheStats_r(int flags)
8902 {
8903     int i;
8904     afs_uint32 vol_sum = 0;
8905     struct stats {
8906         double min;
8907         double max;
8908         double sum;
8909         double avg;
8910     };
8911     struct stats looks, gets, reorders, len;
8912     struct stats ch_looks, ch_gets, ch_reorders;
8913     char pr_buf[4][32];
8914     VolumeHashChainHead *head;
8915     Volume *vp, *np;
8916     struct VLRUExtStats vlru_stats;
8917
8918     /* zero out stats */
8919     memset(&looks, 0, sizeof(struct stats));
8920     memset(&gets, 0, sizeof(struct stats));
8921     memset(&reorders, 0, sizeof(struct stats));
8922     memset(&len, 0, sizeof(struct stats));
8923     memset(&ch_looks, 0, sizeof(struct stats));
8924     memset(&ch_gets, 0, sizeof(struct stats));
8925     memset(&ch_reorders, 0, sizeof(struct stats));
8926
8927     for (i = 0; i < VolumeHashTable.Size; i++) {
8928         head = &VolumeHashTable.Table[i];
8929
8930         VHashWait_r(head);
8931         VHashBeginExclusive_r(head);
8932         VOL_UNLOCK;
8933
8934         ch_looks.sum    = UInt64ToDouble(&head->looks);
8935         ch_gets.sum     = UInt64ToDouble(&head->gets);
8936         ch_reorders.sum = UInt64ToDouble(&head->reorders);
8937
8938         /* update global statistics */
8939         {
8940             looks.sum    += ch_looks.sum;
8941             gets.sum     += ch_gets.sum;
8942             reorders.sum += ch_reorders.sum;
8943             len.sum      += (double)head->len;
8944             vol_sum      += head->len;
8945
8946             if (i == 0) {
8947                 len.min      = (double) head->len;
8948                 len.max      = (double) head->len;
8949                 looks.min    = ch_looks.sum;
8950                 looks.max    = ch_looks.sum;
8951                 gets.min     = ch_gets.sum;
8952                 gets.max     = ch_gets.sum;
8953                 reorders.min = ch_reorders.sum;
8954                 reorders.max = ch_reorders.sum;
8955             } else {
8956                 if (((double)head->len) < len.min)
8957                     len.min = (double) head->len;
8958                 if (((double)head->len) > len.max)
8959                     len.max = (double) head->len;
8960                 if (ch_looks.sum < looks.min)
8961                     looks.min = ch_looks.sum;
8962                 else if (ch_looks.sum > looks.max)
8963                     looks.max = ch_looks.sum;
8964                 if (ch_gets.sum < gets.min)
8965                     gets.min = ch_gets.sum;
8966                 else if (ch_gets.sum > gets.max)
8967                     gets.max = ch_gets.sum;
8968                 if (ch_reorders.sum < reorders.min)
8969                     reorders.min = ch_reorders.sum;
8970                 else if (ch_reorders.sum > reorders.max)
8971                     reorders.max = ch_reorders.sum;
8972             }
8973         }
8974
8975         if ((flags & VOL_STATS_PER_CHAIN2) && queue_IsNotEmpty(head)) {
8976             /* compute detailed per-chain stats */
8977             struct stats hdr_loads, hdr_gets;
8978             double v_looks, v_loads, v_gets;
8979
8980             /* initialize stats with data from first element in chain */
8981             vp = queue_First(head, Volume);
8982             v_looks = UInt64ToDouble(&vp->stats.hash_lookups);
8983             v_loads = UInt64ToDouble(&vp->stats.hdr_loads);
8984             v_gets  = UInt64ToDouble(&vp->stats.hdr_gets);
8985             ch_gets.min = ch_gets.max = v_looks;
8986             hdr_loads.min = hdr_loads.max = v_loads;
8987             hdr_gets.min = hdr_gets.max = v_gets;
8988             hdr_loads.sum = hdr_gets.sum = 0;
8989
8990             vp = queue_Next(vp, Volume);
8991
8992             /* pull in stats from remaining elements in chain */
8993             for (queue_ScanFrom(head, vp, vp, np, Volume)) {
8994                 v_looks = UInt64ToDouble(&vp->stats.hash_lookups);
8995                 v_loads = UInt64ToDouble(&vp->stats.hdr_loads);
8996                 v_gets  = UInt64ToDouble(&vp->stats.hdr_gets);
8997
8998                 hdr_loads.sum += v_loads;
8999                 hdr_gets.sum += v_gets;
9000
9001                 if (v_looks < ch_gets.min)
9002                     ch_gets.min = v_looks;
9003                 else if (v_looks > ch_gets.max)
9004                     ch_gets.max = v_looks;
9005
9006                 if (v_loads < hdr_loads.min)
9007                     hdr_loads.min = v_loads;
9008                 else if (v_loads > hdr_loads.max)
9009                     hdr_loads.max = v_loads;
9010
9011                 if (v_gets < hdr_gets.min)
9012                     hdr_gets.min = v_gets;
9013                 else if (v_gets > hdr_gets.max)
9014                     hdr_gets.max = v_gets;
9015             }
9016
9017             /* compute per-chain averages */
9018             ch_gets.avg = ch_gets.sum / ((double)head->len);
9019             hdr_loads.avg = hdr_loads.sum / ((double)head->len);
9020             hdr_gets.avg = hdr_gets.sum / ((double)head->len);
9021
9022             /* dump per-chain stats */
9023             Log("Volume hash chain %d : len=%d, looks=%s, reorders=%s\n",
9024                 i, head->len,
9025                 DoubleToPrintable(ch_looks.sum, pr_buf[0], sizeof(pr_buf[0])),
9026                 DoubleToPrintable(ch_reorders.sum, pr_buf[1], sizeof(pr_buf[1])));
9027             Log("\tVolume gets : min=%s, max=%s, avg=%s, total=%s\n",
9028                 DoubleToPrintable(ch_gets.min, pr_buf[0], sizeof(pr_buf[0])),
9029                 DoubleToPrintable(ch_gets.max, pr_buf[1], sizeof(pr_buf[1])),
9030                 DoubleToPrintable(ch_gets.avg, pr_buf[2], sizeof(pr_buf[2])),
9031                 DoubleToPrintable(ch_gets.sum, pr_buf[3], sizeof(pr_buf[3])));
9032             Log("\tHDR gets : min=%s, max=%s, avg=%s, total=%s\n",
9033                 DoubleToPrintable(hdr_gets.min, pr_buf[0], sizeof(pr_buf[0])),
9034                 DoubleToPrintable(hdr_gets.max, pr_buf[1], sizeof(pr_buf[1])),
9035                 DoubleToPrintable(hdr_gets.avg, pr_buf[2], sizeof(pr_buf[2])),
9036                 DoubleToPrintable(hdr_gets.sum, pr_buf[3], sizeof(pr_buf[3])));
9037             Log("\tHDR loads : min=%s, max=%s, avg=%s, total=%s\n",
9038                 DoubleToPrintable(hdr_loads.min, pr_buf[0], sizeof(pr_buf[0])),
9039                 DoubleToPrintable(hdr_loads.max, pr_buf[1], sizeof(pr_buf[1])),
9040                 DoubleToPrintable(hdr_loads.avg, pr_buf[2], sizeof(pr_buf[2])),
9041                 DoubleToPrintable(hdr_loads.sum, pr_buf[3], sizeof(pr_buf[3])));
9042         } else if (flags & VOL_STATS_PER_CHAIN) {
9043             /* dump simple per-chain stats */
9044             Log("Volume hash chain %d : len=%d, looks=%s, gets=%s, reorders=%s\n",
9045                 i, head->len,
9046                 DoubleToPrintable(ch_looks.sum, pr_buf[0], sizeof(pr_buf[0])),
9047                 DoubleToPrintable(ch_gets.sum, pr_buf[1], sizeof(pr_buf[1])),
9048                 DoubleToPrintable(ch_reorders.sum, pr_buf[2], sizeof(pr_buf[2])));
9049         }
9050
9051         VOL_LOCK;
9052         VHashEndExclusive_r(head);
9053     }
9054
9055     VOL_UNLOCK;
9056
9057     /* compute global averages */
9058     len.avg      = len.sum      / ((double)VolumeHashTable.Size);
9059     looks.avg    = looks.sum    / ((double)VolumeHashTable.Size);
9060     gets.avg     = gets.sum     / ((double)VolumeHashTable.Size);
9061     reorders.avg = reorders.sum / ((double)VolumeHashTable.Size);
9062
9063     /* dump global stats */
9064     Log("Volume hash summary: %d buckets\n", VolumeHashTable.Size);
9065     Log(" chain length : min=%s, max=%s, avg=%s, total=%s\n",
9066         DoubleToPrintable(len.min, pr_buf[0], sizeof(pr_buf[0])),
9067         DoubleToPrintable(len.max, pr_buf[1], sizeof(pr_buf[1])),
9068         DoubleToPrintable(len.avg, pr_buf[2], sizeof(pr_buf[2])),
9069         DoubleToPrintable(len.sum, pr_buf[3], sizeof(pr_buf[3])));
9070     Log(" looks : min=%s, max=%s, avg=%s, total=%s\n",
9071         DoubleToPrintable(looks.min, pr_buf[0], sizeof(pr_buf[0])),
9072         DoubleToPrintable(looks.max, pr_buf[1], sizeof(pr_buf[1])),
9073         DoubleToPrintable(looks.avg, pr_buf[2], sizeof(pr_buf[2])),
9074         DoubleToPrintable(looks.sum, pr_buf[3], sizeof(pr_buf[3])));
9075     Log(" gets : min=%s, max=%s, avg=%s, total=%s\n",
9076         DoubleToPrintable(gets.min, pr_buf[0], sizeof(pr_buf[0])),
9077         DoubleToPrintable(gets.max, pr_buf[1], sizeof(pr_buf[1])),
9078         DoubleToPrintable(gets.avg, pr_buf[2], sizeof(pr_buf[2])),
9079         DoubleToPrintable(gets.sum, pr_buf[3], sizeof(pr_buf[3])));
9080     Log(" reorders : min=%s, max=%s, avg=%s, total=%s\n",
9081         DoubleToPrintable(reorders.min, pr_buf[0], sizeof(pr_buf[0])),
9082         DoubleToPrintable(reorders.max, pr_buf[1], sizeof(pr_buf[1])),
9083         DoubleToPrintable(reorders.avg, pr_buf[2], sizeof(pr_buf[2])),
9084         DoubleToPrintable(reorders.sum, pr_buf[3], sizeof(pr_buf[3])));
9085
9086     /* print extended disk related statistics */
9087     {
9088         struct DiskPartition64 * diskP;
9089         afs_uint32 vol_count[VOLMAXPARTS+1];
9090         byte part_exists[VOLMAXPARTS+1];
9091         Device id;
9092         int i;
9093
9094         memset(vol_count, 0, sizeof(vol_count));
9095         memset(part_exists, 0, sizeof(part_exists));
9096
9097         VOL_LOCK;
9098
9099         for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
9100             id = diskP->index;
9101             vol_count[id] = diskP->vol_list.len;
9102             part_exists[id] = 1;
9103         }
9104
9105         VOL_UNLOCK;
9106         for (i = 0; i <= VOLMAXPARTS; i++) {
9107             if (part_exists[i]) {
9108                 /* XXX while this is currently safe, it is a violation
9109                  *     of the VGetPartitionById_r interface contract. */
9110                 diskP = VGetPartitionById_r(i, 0);
9111                 if (diskP) {
9112                     Log("Partition %s has %d online volumes\n",
9113                         VPartitionPath(diskP), diskP->vol_list.len);
9114                 }
9115             }
9116         }
9117         VOL_LOCK;
9118     }
9119
9120     /* print extended VLRU statistics */
9121     if (VVLRUExtStats_r(&vlru_stats, vol_sum) == 0) {
9122         afs_uint32 idx, cur, lpos;
9123         VolumeId line[5];
9124
9125         VOL_UNLOCK;
9126
9127         Log("VLRU State Dump:\n\n");
9128
9129         for (idx = VLRU_QUEUE_NEW; idx < VLRU_QUEUE_INVALID; idx++) {
9130             Log("\t%s:\n", vlru_idx_to_string(idx));
9131
9132             lpos = 0;
9133             for (cur = vlru_stats.queue_info[idx].start;
9134                  cur < vlru_stats.queue_info[idx].len;
9135                  cur++) {
9136                 line[lpos++] = vlru_stats.vec[cur].volid;
9137                 if (lpos==5) {
9138                     Log("\t\t%u, %u, %u, %u, %u,\n",
9139                         line[0], line[1], line[2], line[3], line[4]);
9140                     lpos = 0;
9141                 }
9142             }
9143
9144             if (lpos) {
9145                 while (lpos < 5) {
9146                     line[lpos++] = 0;
9147                 }
9148                 Log("\t\t%u, %u, %u, %u, %u\n",
9149                     line[0], line[1], line[2], line[3], line[4]);
9150             }
9151             Log("\n");
9152         }
9153
9154         free(vlru_stats.vec);
9155
9156         VOL_LOCK;
9157     }
9158 }
9159
9160 void
9161 VPrintExtendedCacheStats(int flags)
9162 {
9163     VOL_LOCK;
9164     VPrintExtendedCacheStats_r(flags);
9165     VOL_UNLOCK;
9166 }
9167 #endif /* AFS_DEMAND_ATTACH_FS */
9168
9169 afs_int32
9170 VCanScheduleSalvage(void)
9171 {
9172     return vol_opts.canScheduleSalvage;
9173 }
9174
9175 afs_int32
9176 VCanUseFSSYNC(void)
9177 {
9178     return vol_opts.canUseFSSYNC;
9179 }
9180
9181 afs_int32
9182 VCanUseSALVSYNC(void)
9183 {
9184     return vol_opts.canUseSALVSYNC;
9185 }
9186
9187 afs_int32
9188 VCanUnsafeAttach(void)
9189 {
9190     return vol_opts.unsafe_attach;
9191 }